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