Home | History | Annotate | Download | only in ustl-1.0
      1 // This file is part of the ustl library, an STL implementation.
      2 //
      3 // Copyright (C) 2005 by Mike Sharov <msharov (at) users.sourceforge.net>
      4 // This file is free software, distributed under the MIT License.
      5 //
      6 // uiosfunc.h
      7 //
      8 
      9 #ifndef UIOSFUNC_H_730C16E316F7650E3A02E1C6611B789A
     10 #define UIOSFUNC_H_730C16E316F7650E3A02E1C6611B789A
     11 
     12 #include "sostream.h"
     13 
     14 namespace ustl {
     15 
     16 class ios : public ios_base {
     17 public:
     18     /// \class align uiosfunc.h ustl.h
     19     /// \ingroup StreamFunctors
     20     /// \brief Stream functor to allow inline align() calls.
     21     ///
     22     /// Example: os << ios::align(sizeof(uint16_t));
     23     ///
     24     class align {
     25     public:
     26 	inline explicit		align (size_t grain = c_DefaultAlignment) : m_Grain(grain) {}
     27 	inline istream&		apply (istream& is) const { is.align (m_Grain); return (is); }
     28 	inline ostream&		apply (ostream& os) const { os.align (m_Grain); return (os); }
     29 	inline size_t		stream_size (void) const  { return (m_Grain - 1); }
     30     private:
     31 	const size_t		m_Grain;
     32     };
     33 
     34     /// \class talign uiosfunc.h ustl.h
     35     /// \ingroup StreamFunctors
     36     /// \brief Stream functor to allow type-based alignment.
     37     template <typename T>
     38     class talign : public align {
     39     public:
     40 	inline explicit		talign (void) : align (alignof (T())) {}
     41     };
     42 
     43     /// \class skip uiosfunc.h ustl.h
     44     /// \ingroup StreamFunctors
     45     /// \brief Stream functor to allow inline skip() calls.
     46     ///
     47     /// Example: os << ios::skip(sizeof(uint16_t));
     48     ///
     49     class skip {
     50     public:
     51 	inline explicit 	skip (size_t nBytes) : m_nBytes(nBytes) {}
     52 	inline istream&		apply (istream& is) const { is.skip (m_nBytes); return (is); }
     53 	inline ostream&		apply (ostream& os) const { os.skip (m_nBytes); return (os); }
     54 	inline size_t		stream_size (void) const  { return (m_nBytes); }
     55     private:
     56 	const size_t		m_nBytes;
     57     };
     58 
     59     /// \class width uiosfunc.h ustl.h
     60     /// \ingroup StreamFunctors
     61     /// \brief Stream functor to allow inline set_width() calls.
     62     ///
     63     /// Example: os << ios::width(15);
     64     ///
     65     class width {
     66     public:
     67 	inline explicit		width (size_t nBytes) : m_nBytes(nBytes) {}
     68 	inline ostringstream&	apply (ostringstream& os) const { os.set_width (m_nBytes); return (os); }
     69     private:
     70 	const size_t		m_nBytes;
     71     };
     72 
     73     /// \class base uiosfunc.h ustl.h
     74     /// \ingroup StreamFunctors
     75     /// \brief Stream functor to allow inline set_base() calls.
     76     ///
     77     /// Example: os << ios::base(15);
     78     ///
     79     class base {
     80     public:
     81 	inline explicit		base (size_t n) : m_Base(n) {}
     82 	inline ostringstream&	apply (ostringstream& os) const { os.set_base (m_Base); return (os); }
     83     private:
     84 	const size_t		m_Base;
     85     };
     86 };
     87 
     88 inline istream& operator>> (istream& is, const ios::skip& op)	{ return (op.apply (is)); }
     89 inline ostream& operator<< (ostream& os, const ios::skip& op)	{ return (op.apply (os)); }
     90 inline size_t stream_size_of (const ios::skip& op)		{ return (op.stream_size()); }
     91 inline istream& operator>> (istream& is, const ios::align& op)	{ return (op.apply (is)); }
     92 inline ostream& operator<< (ostream& os, const ios::align& op)	{ return (op.apply (os)); }
     93 inline size_t stream_size_of (const ios::align& op)		{ return (op.stream_size()); }
     94 inline ostringstream& operator<< (ostringstream& os, const ios::width& op)	{ return (op.apply (os)); }
     95 inline ostringstream& operator<< (ostringstream& os, const ios::base& op)	{ return (op.apply (os)); }
     96 
     97 } // namespace ustl
     98 
     99 CAST_STREAMABLE(ustl::ios::fmtflags, uint32_t)
    100 CAST_STREAMABLE(ustl::ios::seekdir, uint32_t)
    101 
    102 #endif
    103 
    104