]> git.lyx.org Git - lyx.git/blob - boost/boost/format/alt_sstream.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / format / alt_sstream.hpp
1 // ----------------------------------------------------------------------------
2 //  alt_sstream.hpp : alternative stringstream 
3 // ----------------------------------------------------------------------------
4
5 //  Copyright Samuel Krempp 2003. Use, modification, and distribution are
6 //  subject to the Boost Software License, Version 1.0. (See accompanying
7 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 //  See http://www.boost.org/libs/format for library home page
10
11 // ----------------------------------------------------------------------------
12
13
14
15 #ifndef BOOST_SK_ALT_SSTREAM_HPP
16 #define BOOST_SK_ALT_SSTREAM_HPP
17
18 #include <string>
19 #include <boost/format/detail/compat_workarounds.hpp>
20 #include <boost/utility/base_from_member.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/assert.hpp>
23
24 namespace boost {
25     namespace io {
26
27         template<class Ch, class Tr=::std::char_traits<Ch>, 
28                  class Alloc=::std::allocator<Ch> >
29         class basic_altstringbuf;
30
31         template<class Ch, class Tr =::std::char_traits<Ch>, 
32                  class Alloc=::std::allocator<Ch> >
33         class basic_oaltstringstream;
34
35
36         template<class Ch, class Tr, class Alloc>
37         class basic_altstringbuf 
38             : public ::std::basic_streambuf<Ch, Tr>
39         {
40             typedef ::std::basic_streambuf<Ch, Tr>  streambuf_t;
41             typedef typename CompatAlloc<Alloc>::compatible_type compat_allocator_type;
42             typedef typename CompatTraits<Tr>::compatible_type   compat_traits_type;
43         public:
44             typedef Ch     char_type;
45             typedef Tr     traits_type;
46             typedef typename compat_traits_type::int_type     int_type;
47             typedef typename compat_traits_type::pos_type     pos_type;
48             typedef typename compat_traits_type::off_type     off_type;
49             typedef Alloc                     allocator_type;
50             typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
51
52             typedef ::std::streamsize streamsize;
53
54
55             explicit basic_altstringbuf(std::ios_base::openmode mode
56                                         = std::ios_base::in | std::ios_base::out)
57                 : putend_(NULL), is_allocated_(false), mode_(mode) 
58                 {}
59             explicit basic_altstringbuf(const string_type& s,
60                                         ::std::ios_base::openmode mode
61                                         = ::std::ios_base::in | ::std::ios_base::out)
62                 : putend_(NULL), is_allocated_(false), mode_(mode) 
63                 { dealloc(); str(s); }
64             virtual ~basic_altstringbuf() 
65                 { dealloc(); }
66             using streambuf_t::pbase;
67             using streambuf_t::pptr;
68             using streambuf_t::epptr;
69             using streambuf_t::eback;
70             using streambuf_t::gptr;
71             using streambuf_t::egptr;
72     
73             void clear_buffer();
74             void str(const string_type& s);
75
76             // 0-copy access :
77             Ch * begin() const; 
78             streamsize size() const;
79             streamsize cur_size() const; // stop at current pointer
80             Ch * pend() const // the highest position reached by pptr() since creation
81                 { return ((putend_ < pptr()) ? pptr() : putend_); }
82             streamsize pcount() const 
83                 { return static_cast<streamsize>( pptr() - pbase()) ;}
84
85             // copy buffer to string :
86             string_type str() const 
87                 { return string_type(begin(), size()); }
88             string_type cur_str() const 
89                 { return string_type(begin(), cur_size()); }
90         protected:
91             explicit basic_altstringbuf (basic_altstringbuf * s,
92                                          ::std::ios_base::openmode mode 
93                                          = ::std::ios_base::in | ::std::ios_base::out)
94                 : putend_(NULL), is_allocated_(false), mode_(mode) 
95                 { dealloc(); str(s); }
96
97             virtual pos_type seekoff(off_type off, ::std::ios_base::seekdir way, 
98                                      ::std::ios_base::openmode which 
99                                      = ::std::ios_base::in | ::std::ios_base::out);
100             virtual pos_type seekpos (pos_type pos, 
101                                       ::std::ios_base::openmode which 
102                                       = ::std::ios_base::in | ::std::ios_base::out);
103             virtual int_type underflow();
104             virtual int_type pbackfail(int_type meta = compat_traits_type::eof());
105             virtual int_type overflow(int_type meta = compat_traits_type::eof());
106             void dealloc();
107         private:
108             enum { alloc_min = 256}; // minimum size of allocations
109
110             Ch *putend_;  // remembers (over seeks) the highest value of pptr()
111             bool is_allocated_;
112             ::std::ios_base::openmode mode_;
113             compat_allocator_type alloc_;  // the allocator object
114         };
115
116
117 // ---   class basic_oaltstringstream ----------------------------------------
118         template <class Ch, class Tr, class Alloc>
119         class basic_oaltstringstream 
120             : private base_from_member< shared_ptr< basic_altstringbuf< Ch, Tr, Alloc> > >,
121               public ::std::basic_ostream<Ch, Tr>
122         {
123             class No_Op { 
124                 // used as no-op deleter for (not-owner) shared_pointers
125             public: 
126                 template<class T>
127                 const T & operator()(const T & arg) { return arg; }
128             };
129             typedef ::std::basic_ostream<Ch, Tr> stream_t;
130             typedef boost::base_from_member<boost::shared_ptr<
131                 basic_altstringbuf<Ch,Tr, Alloc> > > 
132                 pbase_type;
133             typedef ::std::basic_string<Ch, Tr, Alloc>  string_type;
134             typedef basic_altstringbuf<Ch, Tr, Alloc>   stringbuf_t;
135         public:
136             typedef Alloc  allocator_type;
137             basic_oaltstringstream() 
138                 : pbase_type(new stringbuf_t), stream_t(rdbuf()) 
139                 { }
140             basic_oaltstringstream(::boost::shared_ptr<stringbuf_t> buf) 
141                 : pbase_type(buf), stream_t(rdbuf()) 
142                 { }
143             basic_oaltstringstream(stringbuf_t * buf) 
144                 : pbase_type(buf, No_Op() ), stream_t(rdbuf()) 
145                 { }
146             stringbuf_t * rdbuf() const 
147                 { return pbase_type::member.get(); }
148             void clear_buffer() 
149                 { rdbuf()->clear_buffer(); }
150
151             // 0-copy access :
152             Ch * begin() const 
153                 { return rdbuf()->begin(); }
154             ::std::streamsize size() const 
155                 { return rdbuf()->size(); }
156             ::std::streamsize cur_size() const // stops at current position
157                 { return rdbuf()->cur_size(); }
158
159             // copy buffer to string :
160             string_type str()     const   // [pbase, epptr[
161                 { return rdbuf()->str(); } 
162             string_type cur_str() const   // [pbase, pptr[
163                 { return rdbuf()->cur_str(); }
164         };
165
166     } // N.S. io
167 } // N.S. boost
168
169 #include <boost/format/alt_sstream_impl.hpp>
170
171 #endif // include guard
172