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