Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:53

0001 // ----------------------------------------------------------------------------
0002 //  alt_sstream.hpp : alternative stringstream 
0003 // ----------------------------------------------------------------------------
0004 
0005 //  Copyright Samuel Krempp 2003. Use, modification, and distribution are
0006 //  subject to the Boost Software License, Version 1.0. (See accompanying
0007 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 //  See http://www.boost.org/libs/format for library home page
0010 
0011 // ----------------------------------------------------------------------------
0012 
0013 
0014 
0015 #ifndef BOOST_SK_ALT_SSTREAM_HPP
0016 #define BOOST_SK_ALT_SSTREAM_HPP
0017 
0018 #include <string>
0019 #include <boost/core/allocator_access.hpp>
0020 #include <boost/format/detail/compat_workarounds.hpp>
0021 #include <boost/utility/base_from_member.hpp>
0022 #include <boost/shared_ptr.hpp>
0023 #include <boost/config.hpp>
0024 #include <boost/assert.hpp>
0025 
0026 namespace boost {
0027     namespace io {
0028 
0029         template<class Ch, class Tr=::std::char_traits<Ch>, 
0030                  class Alloc=::std::allocator<Ch> >
0031         class basic_altstringbuf;
0032 
0033         template<class Ch, class Tr =::std::char_traits<Ch>, 
0034                  class Alloc=::std::allocator<Ch> >
0035         class basic_oaltstringstream;
0036 
0037 
0038         template<class Ch, class Tr, class Alloc>
0039         class basic_altstringbuf 
0040             : public ::std::basic_streambuf<Ch, Tr>
0041         {
0042             typedef ::std::basic_streambuf<Ch, Tr>  streambuf_t;
0043             typedef typename CompatAlloc<Alloc>::compatible_type compat_allocator_type;
0044             typedef typename CompatTraits<Tr>::compatible_type   compat_traits_type;
0045         public:
0046             typedef Ch     char_type;
0047             typedef Tr     traits_type;
0048             typedef typename compat_traits_type::int_type     int_type;
0049             typedef typename compat_traits_type::pos_type     pos_type;
0050             typedef typename compat_traits_type::off_type     off_type;
0051             typedef Alloc                     allocator_type;
0052             typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
0053             typedef typename string_type::size_type    size_type;
0054 
0055             typedef ::std::streamsize streamsize;
0056 
0057 
0058             explicit basic_altstringbuf(std::ios_base::openmode mode
0059                                         = std::ios_base::in | std::ios_base::out)
0060                 : putend_(NULL), is_allocated_(false), mode_(mode) 
0061                 {}
0062             explicit basic_altstringbuf(const string_type& s,
0063                                         ::std::ios_base::openmode mode
0064                                         = ::std::ios_base::in | ::std::ios_base::out)
0065                 : putend_(NULL), is_allocated_(false), mode_(mode) 
0066                 { dealloc(); str(s); }
0067             virtual ~basic_altstringbuf() BOOST_NOEXCEPT_OR_NOTHROW
0068                 { dealloc(); }
0069             using streambuf_t::pbase;
0070             using streambuf_t::pptr;
0071             using streambuf_t::epptr;
0072             using streambuf_t::eback;
0073             using streambuf_t::gptr;
0074             using streambuf_t::egptr;
0075     
0076             void clear_buffer();
0077             void str(const string_type& s);
0078 
0079             // 0-copy access :
0080             Ch * begin() const; 
0081             size_type size() const;
0082             size_type cur_size() const; // stop at current pointer
0083             Ch * pend() const // the highest position reached by pptr() since creation
0084                 { return ((putend_ < pptr()) ? pptr() : putend_); }
0085             size_type pcount() const 
0086                 { return static_cast<size_type>( pptr() - pbase()) ;}
0087 
0088             // copy buffer to string :
0089             string_type str() const 
0090                 { return string_type(begin(), size()); }
0091             string_type cur_str() const 
0092                 { return string_type(begin(), cur_size()); }
0093         protected:
0094             explicit basic_altstringbuf (basic_altstringbuf * s,
0095                                          ::std::ios_base::openmode mode 
0096                                          = ::std::ios_base::in | ::std::ios_base::out)
0097                 : putend_(NULL), is_allocated_(false), mode_(mode) 
0098                 { dealloc(); str(s); }
0099 
0100             virtual pos_type seekoff(off_type off, ::std::ios_base::seekdir way, 
0101                                      ::std::ios_base::openmode which 
0102                                      = ::std::ios_base::in | ::std::ios_base::out);
0103             virtual pos_type seekpos (pos_type pos, 
0104                                       ::std::ios_base::openmode which 
0105                                       = ::std::ios_base::in | ::std::ios_base::out);
0106             virtual int_type underflow();
0107             virtual int_type pbackfail(int_type meta = compat_traits_type::eof());
0108             virtual int_type overflow(int_type meta = compat_traits_type::eof());
0109             void dealloc();
0110         private:
0111             enum { alloc_min = 256}; // minimum size of allocations
0112 
0113             Ch *putend_;  // remembers (over seeks) the highest value of pptr()
0114             bool is_allocated_;
0115             ::std::ios_base::openmode mode_;
0116             compat_allocator_type alloc_;  // the allocator object
0117         };
0118 
0119 
0120 // ---   class basic_oaltstringstream ----------------------------------------
0121         template <class Ch, class Tr, class Alloc>
0122         class basic_oaltstringstream 
0123             : private base_from_member< shared_ptr< basic_altstringbuf< Ch, Tr, Alloc> > >,
0124               public ::std::basic_ostream<Ch, Tr>
0125         {
0126             class No_Op { 
0127                 // used as no-op deleter for (not-owner) shared_pointers
0128             public: 
0129                 template<class T>
0130                 const T & operator()(const T & arg) { return arg; }
0131             };
0132             typedef ::std::basic_ostream<Ch, Tr> stream_t;
0133             typedef boost::base_from_member<boost::shared_ptr<
0134                 basic_altstringbuf<Ch,Tr, Alloc> > > 
0135                 pbase_type;
0136             typedef ::std::basic_string<Ch, Tr, Alloc>  string_type;
0137             typedef typename string_type::size_type     size_type;
0138             typedef basic_altstringbuf<Ch, Tr, Alloc>   stringbuf_t;
0139         public:
0140             typedef Alloc  allocator_type;
0141             basic_oaltstringstream() 
0142                 : pbase_type(new stringbuf_t), stream_t(pbase_type::member.get())
0143                 { }
0144             basic_oaltstringstream(::boost::shared_ptr<stringbuf_t> buf) 
0145                 : pbase_type(buf), stream_t(pbase_type::member.get())
0146                 { }
0147             basic_oaltstringstream(stringbuf_t * buf) 
0148                 : pbase_type(buf, No_Op() ), stream_t(pbase_type::member.get())
0149                 { }
0150             stringbuf_t * rdbuf() const 
0151                 { return pbase_type::member.get(); }
0152             void clear_buffer() 
0153                 { rdbuf()->clear_buffer(); }
0154 
0155             // 0-copy access :
0156             Ch * begin() const 
0157                 { return rdbuf()->begin(); }
0158             size_type size() const 
0159                 { return rdbuf()->size(); }
0160             size_type cur_size() const // stops at current position
0161                 { return rdbuf()->cur_size(); }
0162 
0163             // copy buffer to string :
0164             string_type str()     const   // [pbase, epptr[
0165                 { return rdbuf()->str(); } 
0166             string_type cur_str() const   // [pbase, pptr[
0167                 { return rdbuf()->cur_str(); }
0168             void str(const string_type& s) 
0169                 { rdbuf()->str(s); }
0170         };
0171 
0172     } // N.S. io
0173 } // N.S. boost
0174 
0175 #include <boost/format/alt_sstream_impl.hpp>
0176 
0177 #endif // include guard
0178