ios_state.hpp 11 KB

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