]> git.lyx.org Git - lyx.git/blob - boost/boost/format/outsstream.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / format / outsstream.hpp
1 // -*- C++ -*-
2 //  Boost  format   ----------------------------------------------------
3 //  See http://www.boost.org for updates, documentation, and revision history.
4
5 //  (C) Samuel Krempp 2003
6 //  Permission to copy, use, modify, sell and
7 //  distribute this software is granted provided this copyright notice appears
8 //  in all copies. This software is provided "as is" without express or implied
9 //  warranty, and with no claim as to its suitability for any purpose.
10
11 // ------------------------------------------------------------------------------
12 //   outsstream<Ch, Tr>   is  a class extending stringstream by adding :
13 //     clear_buffer() method, and
14 //     access to the [pbase(), pptr()[ part of the 'output sequence' 
15 //     (see ยง27.5.1 of the C++ standard)
16 //     if sstream is not available, it uses strstream and is slightly different.
17 // ------------------------------------------------------------------------------
18
19 #ifndef BOOST_FORMAT_OUTSSTREAM_H
20 #define BOOST_FORMAT_OUTSSTREAM_H
21
22 #include <boost/assert.hpp>
23 #include <boost/utility/base_from_member.hpp>
24 #include <boost/format/detail/config_macros.hpp>
25
26 #if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_FORMAT_IGNORE_STRINGSTREAM)
27 #include <sstream>
28 #else
29 #include <strstream>
30 #include <string>
31 #endif // BOOST_NO_STRING_STREAM
32
33
34 namespace boost {
35 namespace io {
36
37 #if !defined(BOOST_NO_STRINGSTREAM) && !defined(BOOST_FORMAT_IGNORE_STRINGSTREAM)
38
39
40 //---- The stringstream way ---------------------------------------------------
41
42
43 // ---   class steal_basic_stringbuf : steals  pbase(), pptr() & co -----------
44     template<class Ch, class Tr = BOOST_IO_STD char_traits<Ch> >
45     class steal_basic_stringbuf : public std::basic_stringbuf<Ch, Tr>
46     {
47         typedef std::basic_stringbuf<Ch, Tr> buff_t;
48     public:
49         typedef std::basic_string<Ch,Tr>     string_type;
50
51         // get [pbase, pptr[   from stringbuf::str(),  which returns [pbase, epptr[  :
52         string_type cur_str() const { return string_type(this->str(), 0, pcount()); } 
53
54         // publicize those functions (protected in stringbuf) :
55         std::streamsize pcount() const { return pptr() - pbase(); }
56         const Ch * pbase() const { return buff_t::pbase(); } 
57         const Ch * pptr()  const { return buff_t::pptr(); } 
58         const Ch * epptr() const { return buff_t::epptr(); }
59         // added convenience function :
60         void clear_buffer();
61     };
62
63
64 // ---   class basic_outsstream -----------------------------------------------
65     template<class Ch, class Tr = BOOST_IO_STD char_traits<Ch> >
66     class basic_outsstream : boost::base_from_member<steal_basic_stringbuf<Ch, Tr> >, 
67                              public BOOST_IO_STD basic_ostream<Ch, Tr>
68     // use stringbuf with its stolen protected members, 
69     // publicly derived from basic_ostream to make this class a stream.
70     {
71     public:
72         typedef std::basic_string<Ch,Tr>     string_type;
73         basic_outsstream() : pbase_type(),
74                              std::basic_ostream<Ch,Tr>( & sb() ) {}
75         // buffer access via strings
76         string_type str()     const { return sb().str(); }     // [begin, end[
77         string_type cur_str() const { return sb().cur_str(); } // [begin, cur[
78             
79         // copy-less access (note the pointers can be invalidated when modifying the stream)
80         std::streamsize pcount() const { return sb().pcount(); }
81         const Ch * begin() const { return sb().pbase(); } 
82         const Ch * cur()   const { return sb().pptr(); } 
83         const Ch * end()   const { return sb().epptr(); }
84
85         void clear_buffer() { sb().clear_buffer(); }
86     private:
87         typedef boost::base_from_member<steal_basic_stringbuf<Ch, Tr> > pbase_type;
88         steal_basic_stringbuf<Ch, Tr>      &  sb()       { return pbase_type::member; }
89         steal_basic_stringbuf<Ch, Tr> const&  sb() const { return pbase_type::member; }
90     };
91 #else // BOOST_NO_STRINGSTREAM
92
93
94
95 //---- The strstream way ------------------------------------------------------
96
97     template <class Ch, 
98 #if !( BOOST_WORKAROUND(__GNUC__, <3) &&  defined(__STL_CONFIG_H) )
99         class Tr = BOOST_IO_STD char_traits<Ch> > 
100 #else
101     class Tr = std::string_char_traits<Ch> > 
102 #endif
103     class basic_outsstream; // we define only the <char> specialisaton
104
105
106 // ---   class basic_outsstream -----------------------------------------------
107     template<class Tr>
108     class basic_outsstream<char, Tr> : private BOOST_IO_STD  strstreambuf, 
109                                        public std::basic_ostream<char, Tr>
110     {
111     public:
112         typedef char Ch;
113         typedef std::basic_string<Ch,Tr>      string_type;
114         typedef BOOST_IO_STD strstreambuf     buff_t;
115         typedef std::basic_ostream<char, Tr>  stream_t;
116     public:
117         basic_outsstream();
118
119         // ! physically copy chars :
120         string_type str(); 
121         string_type cur_str() const  { return string_type(begin(), cur());  } 
122
123         int freeze() const { return buff_t::freeze(); }
124         void freeze(int x) { buff_t::freeze(x); }
125
126         // copy-less access (be careful, the pointer are invalidated when modifying the stream) :
127         std::streamsize pcount() const { return cur()-begin(); }
128         const Ch * begin() const { return buff_t::pbase(); } 
129         const Ch * cur()   const { return buff_t::pptr(); } 
130         const Ch * end()   const { return buff_t::epptr(); }
131
132         void clear_buffer();
133     };
134   
135 #endif // BOOST_NO_STRINGSTREAM
136
137
138     typedef basic_outsstream<char> outsstream;
139
140 } // namespace boost::io
141 } // namespace boost
142
143
144 #include <boost/format/outsstream_impl.hpp> // implementation
145
146 #endif // BOOST_FORMAT_OUTSSTREAM_H include guard