Chris@16: // ---------------------------------------------------------------------------- Chris@16: // alt_sstream.hpp : alternative stringstream Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: // Copyright Samuel Krempp 2003. Use, modification, and distribution are Chris@16: // subject to the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/format for library home page Chris@16: Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: Chris@16: Chris@16: #ifndef BOOST_SK_ALT_SSTREAM_HPP Chris@16: #define BOOST_SK_ALT_SSTREAM_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace io { Chris@16: Chris@16: template, Chris@16: class Alloc=::std::allocator > Chris@16: class basic_altstringbuf; Chris@16: Chris@16: template, Chris@16: class Alloc=::std::allocator > Chris@16: class basic_oaltstringstream; Chris@16: Chris@16: Chris@16: template Chris@16: class basic_altstringbuf Chris@16: : public ::std::basic_streambuf Chris@16: { Chris@16: typedef ::std::basic_streambuf streambuf_t; Chris@16: typedef typename CompatAlloc::compatible_type compat_allocator_type; Chris@16: typedef typename CompatTraits::compatible_type compat_traits_type; Chris@16: public: Chris@16: typedef Ch char_type; Chris@16: typedef Tr traits_type; Chris@16: typedef typename compat_traits_type::int_type int_type; Chris@16: typedef typename compat_traits_type::pos_type pos_type; Chris@16: typedef typename compat_traits_type::off_type off_type; Chris@16: typedef Alloc allocator_type; Chris@16: typedef ::std::basic_string string_type; Chris@16: typedef typename string_type::size_type size_type; Chris@16: Chris@16: typedef ::std::streamsize streamsize; Chris@16: Chris@16: Chris@16: explicit basic_altstringbuf(std::ios_base::openmode mode Chris@16: = std::ios_base::in | std::ios_base::out) Chris@16: : putend_(NULL), is_allocated_(false), mode_(mode) Chris@16: {} Chris@16: explicit basic_altstringbuf(const string_type& s, Chris@16: ::std::ios_base::openmode mode Chris@16: = ::std::ios_base::in | ::std::ios_base::out) Chris@16: : putend_(NULL), is_allocated_(false), mode_(mode) Chris@16: { dealloc(); str(s); } Chris@16: virtual ~basic_altstringbuf() Chris@16: { dealloc(); } Chris@16: using streambuf_t::pbase; Chris@16: using streambuf_t::pptr; Chris@16: using streambuf_t::epptr; Chris@16: using streambuf_t::eback; Chris@16: using streambuf_t::gptr; Chris@16: using streambuf_t::egptr; Chris@16: Chris@16: void clear_buffer(); Chris@16: void str(const string_type& s); Chris@16: Chris@16: // 0-copy access : Chris@16: Ch * begin() const; Chris@16: size_type size() const; Chris@16: size_type cur_size() const; // stop at current pointer Chris@16: Ch * pend() const // the highest position reached by pptr() since creation Chris@16: { return ((putend_ < pptr()) ? pptr() : putend_); } Chris@16: size_type pcount() const Chris@16: { return static_cast( pptr() - pbase()) ;} Chris@16: Chris@16: // copy buffer to string : Chris@16: string_type str() const Chris@16: { return string_type(begin(), size()); } Chris@16: string_type cur_str() const Chris@16: { return string_type(begin(), cur_size()); } Chris@16: protected: Chris@16: explicit basic_altstringbuf (basic_altstringbuf * s, Chris@16: ::std::ios_base::openmode mode Chris@16: = ::std::ios_base::in | ::std::ios_base::out) Chris@16: : putend_(NULL), is_allocated_(false), mode_(mode) Chris@16: { dealloc(); str(s); } Chris@16: Chris@16: virtual pos_type seekoff(off_type off, ::std::ios_base::seekdir way, Chris@16: ::std::ios_base::openmode which Chris@16: = ::std::ios_base::in | ::std::ios_base::out); Chris@16: virtual pos_type seekpos (pos_type pos, Chris@16: ::std::ios_base::openmode which Chris@16: = ::std::ios_base::in | ::std::ios_base::out); Chris@16: virtual int_type underflow(); Chris@16: virtual int_type pbackfail(int_type meta = compat_traits_type::eof()); Chris@16: virtual int_type overflow(int_type meta = compat_traits_type::eof()); Chris@16: void dealloc(); Chris@16: private: Chris@16: enum { alloc_min = 256}; // minimum size of allocations Chris@16: Chris@16: Ch *putend_; // remembers (over seeks) the highest value of pptr() Chris@16: bool is_allocated_; Chris@16: ::std::ios_base::openmode mode_; Chris@16: compat_allocator_type alloc_; // the allocator object Chris@16: }; Chris@16: Chris@16: Chris@16: // --- class basic_oaltstringstream ---------------------------------------- Chris@16: template Chris@16: class basic_oaltstringstream Chris@16: : private base_from_member< shared_ptr< basic_altstringbuf< Ch, Tr, Alloc> > >, Chris@16: public ::std::basic_ostream Chris@16: { Chris@16: class No_Op { Chris@16: // used as no-op deleter for (not-owner) shared_pointers Chris@16: public: Chris@16: template Chris@16: const T & operator()(const T & arg) { return arg; } Chris@16: }; Chris@16: typedef ::std::basic_ostream stream_t; Chris@16: typedef boost::base_from_member > > Chris@16: pbase_type; Chris@16: typedef ::std::basic_string string_type; Chris@16: typedef typename string_type::size_type size_type; Chris@16: typedef basic_altstringbuf stringbuf_t; Chris@16: public: Chris@16: typedef Alloc allocator_type; Chris@16: basic_oaltstringstream() Chris@16: : pbase_type(new stringbuf_t), stream_t(rdbuf()) Chris@16: { } Chris@16: basic_oaltstringstream(::boost::shared_ptr buf) Chris@16: : pbase_type(buf), stream_t(rdbuf()) Chris@16: { } Chris@16: basic_oaltstringstream(stringbuf_t * buf) Chris@16: : pbase_type(buf, No_Op() ), stream_t(rdbuf()) Chris@16: { } Chris@16: stringbuf_t * rdbuf() const Chris@16: { return pbase_type::member.get(); } Chris@16: void clear_buffer() Chris@16: { rdbuf()->clear_buffer(); } Chris@16: Chris@16: // 0-copy access : Chris@16: Ch * begin() const Chris@16: { return rdbuf()->begin(); } Chris@16: size_type size() const Chris@16: { return rdbuf()->size(); } Chris@16: size_type cur_size() const // stops at current position Chris@16: { return rdbuf()->cur_size(); } Chris@16: Chris@16: // copy buffer to string : Chris@16: string_type str() const // [pbase, epptr[ Chris@16: { return rdbuf()->str(); } Chris@16: string_type cur_str() const // [pbase, pptr[ Chris@16: { return rdbuf()->cur_str(); } Chris@16: void str(const string_type& s) Chris@16: { rdbuf()->str(s); } Chris@16: }; Chris@16: Chris@16: } // N.S. io Chris@16: } // N.S. boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // include guard Chris@16: