source: NonGTP/Boost/boost/io/ios_state.hpp @ 857

Revision 857, 10.9 KB checked in by igarcia, 19 years ago (diff)
Line 
1//  Boost io/ios_state.hpp header file  --------------------------------------//
2
3//  Copyright 2002, 2005 Daryle Walker.  Use, modification, and distribution
4//  are subject to the Boost Software License, Version 1.0.  (See accompanying
5//  file LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
6
7//  See <http://www.boost.org/libs/io/> for the library's home page.
8
9#ifndef BOOST_IO_IOS_STATE_HPP
10#define BOOST_IO_IOS_STATE_HPP
11
12#include <boost/io_fwd.hpp>  // self include
13
14#include <ios>        // for std::ios_base, std::basic_ios, etc.
15#ifndef BOOST_NO_STD_LOCALE
16#include <locale>     // for std::locale
17#endif
18#include <ostream>    // for std::basic_ostream
19#include <streambuf>  // for std::basic_streambuf
20#include <string>     // for std::char_traits
21
22
23namespace boost
24{
25namespace io
26{
27
28
29//  Basic stream state saver class declarations  -----------------------------//
30
31class ios_flags_saver
32{
33public:
34    typedef ::std::ios_base            state_type;
35    typedef ::std::ios_base::fmtflags  aspect_type;
36
37    explicit  ios_flags_saver( state_type &s )
38        : s_save_( s ), a_save_( s.flags() )
39        {}
40    ios_flags_saver( state_type &s, aspect_type const &a )
41        : s_save_( s ), a_save_( s.flags(a) )
42        {}
43    ~ios_flags_saver()
44        { this->restore(); }
45
46    void  restore()
47        { s_save_.flags( a_save_ ); }
48
49private:
50    state_type &       s_save_;
51    aspect_type const  a_save_;
52};
53
54class ios_precision_saver
55{
56public:
57    typedef ::std::ios_base    state_type;
58    typedef ::std::streamsize  aspect_type;
59
60    explicit  ios_precision_saver( state_type &s )
61        : s_save_( s ), a_save_( s.precision() )
62        {}
63    ios_precision_saver( state_type &s, aspect_type const &a )
64        : s_save_( s ), a_save_( s.precision(a) )
65        {}
66    ~ios_precision_saver()
67        { this->restore(); }
68
69    void  restore()
70        { s_save_.precision( a_save_ ); }
71
72private:
73    state_type &       s_save_;
74    aspect_type const  a_save_;
75};
76
77class ios_width_saver
78{
79public:
80    typedef ::std::ios_base    state_type;
81    typedef ::std::streamsize  aspect_type;
82
83    explicit  ios_width_saver( state_type &s )
84        : s_save_( s ), a_save_( s.width() )
85        {}
86    ios_width_saver( state_type &s, aspect_type const &a )
87        : s_save_( s ), a_save_( s.width(a) )
88        {}
89    ~ios_width_saver()
90        { this->restore(); }
91
92    void  restore()
93        { s_save_.width( a_save_ ); }
94
95private:
96    state_type &       s_save_;
97    aspect_type const  a_save_;
98};
99
100
101//  Advanced stream state saver class template declarations  -----------------//
102
103template < typename Ch, class Tr >
104class basic_ios_iostate_saver
105{
106public:
107    typedef ::std::basic_ios<Ch, Tr>  state_type;
108    typedef ::std::ios_base::iostate  aspect_type;
109
110    explicit  basic_ios_iostate_saver( state_type &s )
111        : s_save_( s ), a_save_( s.rdstate() )
112        {}
113    basic_ios_iostate_saver( state_type &s, aspect_type const &a )
114        : s_save_( s ), a_save_( s.rdstate() )
115        { s.clear(a); }
116    ~basic_ios_iostate_saver()
117        { this->restore(); }
118
119    void  restore()
120        { s_save_.clear( a_save_ ); }
121
122private:
123    state_type &       s_save_;
124    aspect_type const  a_save_;
125};
126
127template < typename Ch, class Tr >
128class basic_ios_exception_saver
129{
130public:
131    typedef ::std::basic_ios<Ch, Tr>  state_type;
132    typedef ::std::ios_base::iostate  aspect_type;
133
134    explicit  basic_ios_exception_saver( state_type &s )
135        : s_save_( s ), a_save_( s.exceptions() )
136        {}
137    basic_ios_exception_saver( state_type &s, aspect_type const &a )
138        : s_save_( s ), a_save_( s.exceptions() )
139        { s.exceptions(a); }
140    ~basic_ios_exception_saver()
141        { this->restore(); }
142
143    void  restore()
144        { s_save_.exceptions( a_save_ ); }
145
146private:
147    state_type &       s_save_;
148    aspect_type const  a_save_;
149};
150
151template < typename Ch, class Tr >
152class basic_ios_tie_saver
153{
154public:
155    typedef ::std::basic_ios<Ch, Tr>        state_type;
156    typedef ::std::basic_ostream<Ch, Tr> *  aspect_type;
157
158    explicit  basic_ios_tie_saver( state_type &s )
159        : s_save_( s ), a_save_( s.tie() )
160        {}
161    basic_ios_tie_saver( state_type &s, aspect_type const &a )
162        : s_save_( s ), a_save_( s.tie(a) )
163        {}
164    ~basic_ios_tie_saver()
165        { this->restore(); }
166
167    void  restore()
168        { s_save_.tie( a_save_ ); }
169
170private:
171    state_type &       s_save_;
172    aspect_type const  a_save_;
173};
174
175template < typename Ch, class Tr >
176class basic_ios_rdbuf_saver
177{
178public:
179    typedef ::std::basic_ios<Ch, Tr>          state_type;
180    typedef ::std::basic_streambuf<Ch, Tr> *  aspect_type;
181
182    explicit  basic_ios_rdbuf_saver( state_type &s )
183        : s_save_( s ), a_save_( s.rdbuf() )
184        {}
185    basic_ios_rdbuf_saver( state_type &s, aspect_type const &a )
186        : s_save_( s ), a_save_( s.rdbuf(a) )
187        {}
188    ~basic_ios_rdbuf_saver()
189        { this->restore(); }
190
191    void  restore()
192        { s_save_.rdbuf( a_save_ ); }
193
194private:
195    state_type &       s_save_;
196    aspect_type const  a_save_;
197};
198
199template < typename Ch, class Tr >
200class basic_ios_fill_saver
201{
202public:
203    typedef ::std::basic_ios<Ch, Tr>        state_type;
204    typedef typename state_type::char_type  aspect_type;
205
206    explicit  basic_ios_fill_saver( state_type &s )
207        : s_save_( s ), a_save_( s.fill() )
208        {}
209    basic_ios_fill_saver( state_type &s, aspect_type const &a )
210        : s_save_( s ), a_save_( s.fill(a) )
211        {}
212    ~basic_ios_fill_saver()
213        { this->restore(); }
214
215    void  restore()
216        { s_save_.fill( a_save_ ); }
217
218private:
219    state_type &       s_save_;
220    aspect_type const  a_save_;
221};
222
223#ifndef BOOST_NO_STD_LOCALE
224template < typename Ch, class Tr >
225class basic_ios_locale_saver
226{
227public:
228    typedef ::std::basic_ios<Ch, Tr> state_type;
229    typedef ::std::locale aspect_type;
230
231    explicit basic_ios_locale_saver( state_type &s )
232        : s_save_( s ), a_save_( s.getloc() )
233        {}
234    basic_ios_locale_saver( state_type &s, aspect_type const &a )
235        : s_save_( s ), a_save_( s.imbue(a) )
236        {}
237    ~basic_ios_locale_saver()
238        { this->restore(); }
239
240    void  restore()
241        { s_save_.imbue( a_save_ ); }
242
243private:
244    state_type &       s_save_;
245    aspect_type const  a_save_;
246};
247#endif
248
249
250//  User-defined stream state saver class declarations  ----------------------//
251
252class ios_iword_saver
253{
254public:
255    typedef ::std::ios_base  state_type;
256    typedef int              index_type;
257    typedef long             aspect_type;
258
259    explicit ios_iword_saver( state_type &s, index_type i )
260        : s_save_( s ), a_save_( s.iword(i) ), i_save_( i )
261        {}
262    ios_iword_saver( state_type &s, index_type i, aspect_type const &a )
263        : s_save_( s ), a_save_( s.iword(i) ), i_save_( i )
264        { s.iword(i) = a; }
265    ~ios_iword_saver()
266        { this->restore(); }
267
268    void  restore()
269        { s_save_.iword( i_save_ ) = a_save_; }
270
271private:
272    state_type &       s_save_;
273    aspect_type const  a_save_;
274    index_type const   i_save_;
275};
276
277class ios_pword_saver
278{
279public:
280    typedef ::std::ios_base  state_type;
281    typedef int              index_type;
282    typedef void *           aspect_type;
283
284    explicit  ios_pword_saver( state_type &s, index_type i )
285        : s_save_( s ), a_save_( s.pword(i) ), i_save_( i )
286        {}
287    ios_pword_saver( state_type &s, index_type i, aspect_type const &a )
288        : s_save_( s ), a_save_( s.pword(i) ), i_save_( i )
289        { s.pword(i) = a; }
290    ~ios_pword_saver()
291        { this->restore(); }
292
293    void  restore()
294        { s_save_.pword( i_save_ ) = a_save_; }
295
296private:
297    state_type &       s_save_;
298    aspect_type const  a_save_;
299    index_type const   i_save_;
300};
301
302
303//  Combined stream state saver class (template) declarations  ---------------//
304
305class ios_base_all_saver
306{
307public:
308    typedef ::std::ios_base  state_type;
309
310    explicit  ios_base_all_saver( state_type &s )
311        : s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() )
312        , a3_save_( s.width() )
313        {}
314
315    ~ios_base_all_saver()
316        { this->restore(); }
317
318    void  restore()
319    {
320        s_save_.width( a3_save_ );
321        s_save_.precision( a2_save_ );
322        s_save_.flags( a1_save_ );
323    }
324
325private:
326    state_type &                s_save_;
327    state_type::fmtflags const  a1_save_;
328    ::std::streamsize const     a2_save_;
329    ::std::streamsize const     a3_save_;
330};
331
332template < typename Ch, class Tr >
333class basic_ios_all_saver
334{
335public:
336    typedef ::std::basic_ios<Ch, Tr>  state_type;
337
338    explicit  basic_ios_all_saver( state_type &s )
339        : s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() )
340        , a3_save_( s.width() ), a4_save_( s.rdstate() )
341        , a5_save_( s.exceptions() ), a6_save_( s.tie() )
342        , a7_save_( s.rdbuf() ), a8_save_( s.fill() )
343        #ifndef BOOST_NO_STD_LOCALE
344        , a9_save_( s.getloc() )
345        #endif
346        {}
347
348    ~basic_ios_all_saver()
349        { this->restore(); }
350
351    void  restore()
352    {
353        #ifndef BOOST_NO_STD_LOCALE
354        s_save_.imbue( a9_save_ );
355        #endif
356        s_save_.fill( a8_save_ );
357        s_save_.rdbuf( a7_save_ );
358        s_save_.tie( a6_save_ );
359        s_save_.exceptions( a5_save_ );
360        s_save_.clear( a4_save_ );
361        s_save_.width( a3_save_ );
362        s_save_.precision( a2_save_ );
363        s_save_.flags( a1_save_ );
364    }
365
366private:
367    state_type &                            s_save_;
368    typename state_type::fmtflags const     a1_save_;
369    ::std::streamsize const                 a2_save_;
370    ::std::streamsize const                 a3_save_;
371    typename state_type::iostate const      a4_save_;
372    typename state_type::iostate const      a5_save_;
373    ::std::basic_ostream<Ch, Tr> * const    a6_save_;
374    ::std::basic_streambuf<Ch, Tr> * const  a7_save_;
375    typename state_type::char_type const    a8_save_;
376    #ifndef BOOST_NO_STD_LOCALE
377    ::std::locale const                     a9_save_;
378    #endif
379};
380
381class ios_all_word_saver
382{
383public:
384    typedef ::std::ios_base  state_type;
385    typedef int              index_type;
386
387    ios_all_word_saver( state_type &s, index_type i )
388        : s_save_( s ), i_save_( i ), a1_save_( s.iword(i) )
389        , a2_save_( s.pword(i) )
390        {}
391
392    ~ios_all_word_saver()
393        { this->restore(); }
394
395    void  restore()
396    {
397        s_save_.pword( i_save_ ) = a2_save_;
398        s_save_.iword( i_save_ ) = a1_save_;
399    }
400
401private:
402    state_type &      s_save_;
403    index_type const  i_save_;
404    long const        a1_save_;
405    void * const      a2_save_;
406};
407
408
409}  // namespace io
410}  // namespace boost
411
412
413#endif  // BOOST_IO_IOS_STATE_HPP
Note: See TracBrowser for help on using the repository browser.