ios_state.hpp 12 KB

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