tuple_io.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // tuple_io.hpp --------------------------------------------------------------
  2. // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  3. // 2001 Gary Powell (gary.powell@sierra.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // For more information, see http://www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_TUPLE_IO_HPP
  11. #define BOOST_TUPLE_IO_HPP
  12. // add to boost/config.hpp
  13. // for now
  14. # if defined __GNUC__
  15. # if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
  16. #define BOOST_NO_TEMPLATED_STREAMS
  17. #endif
  18. #endif // __GNUC__
  19. #if defined BOOST_NO_TEMPLATED_STREAMS
  20. #include <iostream>
  21. #else
  22. #include <istream>
  23. #include <ostream>
  24. #endif
  25. #include <sstream>
  26. #include "boost/tuple/tuple.hpp"
  27. // This is ugly: one should be using twoargument isspace since whitspace can
  28. // be locale dependent, in theory at least.
  29. // not all libraries implement have the two-arg version, so we need to
  30. // use the one-arg one, which one should get with <cctype> but there seem
  31. // to be exceptions to this.
  32. #if !defined (BOOST_NO_STD_LOCALE)
  33. #include <locale> // for two-arg isspace
  34. #else
  35. #include <cctype> // for one-arg (old) isspace
  36. #include <ctype.h> // Metrowerks does not find one-arg isspace from cctype
  37. #endif
  38. namespace boost {
  39. namespace tuples {
  40. namespace detail {
  41. class format_info {
  42. public:
  43. enum manipulator_type { open, close, delimiter };
  44. BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
  45. private:
  46. static int get_stream_index (int m)
  47. {
  48. static const int stream_index[number_of_manipulators]
  49. = { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
  50. return stream_index[m];
  51. }
  52. format_info(const format_info&);
  53. format_info();
  54. public:
  55. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  56. static char get_manipulator(std::ios& i, manipulator_type m) {
  57. char c = static_cast<char>(i.iword(get_stream_index(m)));
  58. // parentheses and space are the default manipulators
  59. if (!c) {
  60. switch(m) {
  61. case detail::format_info::open : c = '('; break;
  62. case detail::format_info::close : c = ')'; break;
  63. case detail::format_info::delimiter : c = ' '; break;
  64. }
  65. }
  66. return c;
  67. }
  68. static void set_manipulator(std::ios& i, manipulator_type m, char c) {
  69. i.iword(get_stream_index(m)) = static_cast<long>(c);
  70. }
  71. #else
  72. template<class CharType, class CharTrait>
  73. static CharType get_manipulator(std::basic_ios<CharType, CharTrait>& i,
  74. manipulator_type m) {
  75. // The manipulators are stored as long.
  76. // A valid instanitation of basic_stream allows CharType to be any POD,
  77. // hence, the static_cast may fail (it fails if long is not convertible
  78. // to CharType
  79. CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) );
  80. // parentheses and space are the default manipulators
  81. if (!c) {
  82. switch(m) {
  83. case detail::format_info::open : c = i.widen('('); break;
  84. case detail::format_info::close : c = i.widen(')'); break;
  85. case detail::format_info::delimiter : c = i.widen(' '); break;
  86. }
  87. }
  88. return c;
  89. }
  90. template<class CharType, class CharTrait>
  91. static void set_manipulator(std::basic_ios<CharType, CharTrait>& i,
  92. manipulator_type m, CharType c) {
  93. // The manipulators are stored as long.
  94. // A valid instanitation of basic_stream allows CharType to be any POD,
  95. // hence, the static_cast may fail (it fails if CharType is not
  96. // convertible long.
  97. i.iword(get_stream_index(m)) = static_cast<long>(c);
  98. }
  99. #endif // BOOST_NO_TEMPLATED_STREAMS
  100. };
  101. } // end of namespace detail
  102. template<class CharType>
  103. class tuple_manipulator {
  104. const detail::format_info::manipulator_type mt;
  105. CharType f_c;
  106. public:
  107. explicit tuple_manipulator(detail::format_info::manipulator_type m,
  108. const char c = 0)
  109. : mt(m), f_c(c) {}
  110. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  111. void set(std::ios &io) const {
  112. detail::format_info::set_manipulator(io, mt, f_c);
  113. }
  114. #else
  115. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  116. template<class CharType2, class CharTrait>
  117. void set(std::basic_ios<CharType2, CharTrait> &io) const {
  118. detail::format_info::set_manipulator(io, mt, f_c);
  119. }
  120. #else
  121. template<class CharTrait>
  122. void set(std::basic_ios<CharType, CharTrait> &io) const {
  123. detail::format_info::set_manipulator(io, mt, f_c);
  124. }
  125. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  126. #endif // BOOST_NO_TEMPLATED_STREAMS
  127. };
  128. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  129. inline std::ostream&
  130. operator<<(std::ostream& o, const tuple_manipulator<char>& m) {
  131. m.set(o);
  132. return o;
  133. }
  134. inline std::istream&
  135. operator>>(std::istream& i, const tuple_manipulator<char>& m) {
  136. m.set(i);
  137. return i;
  138. }
  139. #else
  140. template<class CharType, class CharTrait>
  141. inline std::basic_ostream<CharType, CharTrait>&
  142. operator<<(std::basic_ostream<CharType, CharTrait>& o, const tuple_manipulator<CharType>& m) {
  143. m.set(o);
  144. return o;
  145. }
  146. template<class CharType, class CharTrait>
  147. inline std::basic_istream<CharType, CharTrait>&
  148. operator>>(std::basic_istream<CharType, CharTrait>& i, const tuple_manipulator<CharType>& m) {
  149. m.set(i);
  150. return i;
  151. }
  152. #endif // BOOST_NO_TEMPLATED_STREAMS
  153. template<class CharType>
  154. inline tuple_manipulator<CharType> set_open(const CharType c) {
  155. return tuple_manipulator<CharType>(detail::format_info::open, c);
  156. }
  157. template<class CharType>
  158. inline tuple_manipulator<CharType> set_close(const CharType c) {
  159. return tuple_manipulator<CharType>(detail::format_info::close, c);
  160. }
  161. template<class CharType>
  162. inline tuple_manipulator<CharType> set_delimiter(const CharType c) {
  163. return tuple_manipulator<CharType>(detail::format_info::delimiter, c);
  164. }
  165. // -------------------------------------------------------------
  166. // printing tuples to ostream in format (a b c)
  167. // parentheses and space are defaults, but can be overriden with manipulators
  168. // set_open, set_close and set_delimiter
  169. namespace detail {
  170. // Note: The order of the print functions is critical
  171. // to let a conforming compiler find and select the correct one.
  172. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  173. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  174. template<class T1>
  175. inline std::ostream& print(std::ostream& o, const cons<T1, null_type>& t) {
  176. return o << t.head;
  177. }
  178. #endif // BOOST_NO_TEMPLATED_STREAMS
  179. inline std::ostream& print(std::ostream& o, const null_type&) { return o; }
  180. template<class T1, class T2>
  181. inline std::ostream&
  182. print(std::ostream& o, const cons<T1, T2>& t) {
  183. const char d = format_info::get_manipulator(o, format_info::delimiter);
  184. o << t.head;
  185. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  186. if (tuples::length<T2>::value == 0)
  187. return o;
  188. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  189. o << d;
  190. return print(o, t.tail );
  191. }
  192. template<class T>
  193. inline bool handle_width(std::ostream& o, const T& t) {
  194. std::streamsize width = o.width();
  195. if(width == 0) return false;
  196. std::ostringstream ss;
  197. ss.copyfmt(o);
  198. ss.tie(0);
  199. ss.width(0);
  200. ss << t;
  201. o << ss.str();
  202. return true;
  203. }
  204. #else
  205. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  206. template<class CharType, class CharTrait, class T1>
  207. inline std::basic_ostream<CharType, CharTrait>&
  208. print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, null_type>& t) {
  209. return o << t.head;
  210. }
  211. #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  212. template<class CharType, class CharTrait>
  213. inline std::basic_ostream<CharType, CharTrait>&
  214. print(std::basic_ostream<CharType, CharTrait>& o, const null_type&) {
  215. return o;
  216. }
  217. template<class CharType, class CharTrait, class T1, class T2>
  218. inline std::basic_ostream<CharType, CharTrait>&
  219. print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, T2>& t) {
  220. const CharType d = format_info::get_manipulator(o, format_info::delimiter);
  221. o << t.head;
  222. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  223. if (tuples::length<T2>::value == 0)
  224. return o;
  225. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  226. o << d;
  227. return print(o, t.tail);
  228. }
  229. template<class CharT, class Traits, class T>
  230. inline bool handle_width(std::basic_ostream<CharT, Traits>& o, const T& t) {
  231. std::streamsize width = o.width();
  232. if(width == 0) return false;
  233. std::basic_ostringstream<CharT, Traits> ss;
  234. ss.copyfmt(o);
  235. ss.tie(0);
  236. ss.width(0);
  237. ss << t;
  238. o << ss.str();
  239. return true;
  240. }
  241. #endif // BOOST_NO_TEMPLATED_STREAMS
  242. } // namespace detail
  243. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  244. inline std::ostream& operator<<(std::ostream& o, const null_type& t) {
  245. if (!o.good() ) return o;
  246. if (detail::handle_width(o, t)) return o;
  247. const char l =
  248. detail::format_info::get_manipulator(o, detail::format_info::open);
  249. const char r =
  250. detail::format_info::get_manipulator(o, detail::format_info::close);
  251. o << l;
  252. o << r;
  253. return o;
  254. }
  255. template<class T1, class T2>
  256. inline std::ostream& operator<<(std::ostream& o, const cons<T1, T2>& t) {
  257. if (!o.good() ) return o;
  258. if (detail::handle_width(o, t)) return o;
  259. const char l =
  260. detail::format_info::get_manipulator(o, detail::format_info::open);
  261. const char r =
  262. detail::format_info::get_manipulator(o, detail::format_info::close);
  263. o << l;
  264. detail::print(o, t);
  265. o << r;
  266. return o;
  267. }
  268. #else
  269. template<class CharType, class CharTrait>
  270. inline std::basic_ostream<CharType, CharTrait>&
  271. operator<<(std::basic_ostream<CharType, CharTrait>& o,
  272. const null_type& t) {
  273. if (!o.good() ) return o;
  274. if (detail::handle_width(o, t)) return o;
  275. const CharType l =
  276. detail::format_info::get_manipulator(o, detail::format_info::open);
  277. const CharType r =
  278. detail::format_info::get_manipulator(o, detail::format_info::close);
  279. o << l;
  280. o << r;
  281. return o;
  282. }
  283. template<class CharType, class CharTrait, class T1, class T2>
  284. inline std::basic_ostream<CharType, CharTrait>&
  285. operator<<(std::basic_ostream<CharType, CharTrait>& o,
  286. const cons<T1, T2>& t) {
  287. if (!o.good() ) return o;
  288. if (detail::handle_width(o, t)) return o;
  289. const CharType l =
  290. detail::format_info::get_manipulator(o, detail::format_info::open);
  291. const CharType r =
  292. detail::format_info::get_manipulator(o, detail::format_info::close);
  293. o << l;
  294. detail::print(o, t);
  295. o << r;
  296. return o;
  297. }
  298. #endif // BOOST_NO_TEMPLATED_STREAMS
  299. // -------------------------------------------------------------
  300. // input stream operators
  301. namespace detail {
  302. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  303. inline std::istream&
  304. extract_and_check_delimiter(
  305. std::istream& is, format_info::manipulator_type del)
  306. {
  307. const char d = format_info::get_manipulator(is, del);
  308. #if defined (BOOST_NO_STD_LOCALE)
  309. const bool is_delimiter = !isspace(d);
  310. #else
  311. const bool is_delimiter = (!std::isspace(d, is.getloc()) );
  312. #endif
  313. char c;
  314. if (is_delimiter) {
  315. is >> c;
  316. if (is.good() && c!=d) {
  317. is.setstate(std::ios::failbit);
  318. }
  319. } else {
  320. is >> std::ws;
  321. }
  322. return is;
  323. }
  324. // Note: The order of the read functions is critical to let a
  325. // (conforming?) compiler find and select the correct one.
  326. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  327. template<class T1>
  328. inline std::istream &
  329. read (std::istream &is, cons<T1, null_type>& t1) {
  330. if (!is.good()) return is;
  331. return is >> t1.head ;
  332. }
  333. #else
  334. inline std::istream& read(std::istream& i, const null_type&) { return i; }
  335. #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  336. template<class T1, class T2>
  337. inline std::istream&
  338. read(std::istream &is, cons<T1, T2>& t1) {
  339. if (!is.good()) return is;
  340. is >> t1.head;
  341. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  342. if (tuples::length<T2>::value == 0)
  343. return is;
  344. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  345. extract_and_check_delimiter(is, format_info::delimiter);
  346. return read(is, t1.tail);
  347. }
  348. } // end namespace detail
  349. inline std::istream&
  350. operator>>(std::istream &is, null_type&) {
  351. if (!is.good() ) return is;
  352. detail::extract_and_check_delimiter(is, detail::format_info::open);
  353. detail::extract_and_check_delimiter(is, detail::format_info::close);
  354. return is;
  355. }
  356. template<class T1, class T2>
  357. inline std::istream&
  358. operator>>(std::istream& is, cons<T1, T2>& t1) {
  359. if (!is.good() ) return is;
  360. detail::extract_and_check_delimiter(is, detail::format_info::open);
  361. detail::read(is, t1);
  362. detail::extract_and_check_delimiter(is, detail::format_info::close);
  363. return is;
  364. }
  365. #else
  366. template<class CharType, class CharTrait>
  367. inline std::basic_istream<CharType, CharTrait>&
  368. extract_and_check_delimiter(
  369. std::basic_istream<CharType, CharTrait> &is, format_info::manipulator_type del)
  370. {
  371. const CharType d = format_info::get_manipulator(is, del);
  372. #if defined (BOOST_NO_STD_LOCALE)
  373. const bool is_delimiter = !isspace(d);
  374. #elif defined ( __BORLANDC__ )
  375. const bool is_delimiter = !std::use_facet< std::ctype< CharType > >
  376. (is.getloc() ).is( std::ctype_base::space, d);
  377. #else
  378. const bool is_delimiter = (!std::isspace(d, is.getloc()) );
  379. #endif
  380. CharType c;
  381. if (is_delimiter) {
  382. is >> c;
  383. if (is.good() && c!=d) {
  384. is.setstate(std::ios::failbit);
  385. }
  386. } else {
  387. is >> std::ws;
  388. }
  389. return is;
  390. }
  391. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  392. template<class CharType, class CharTrait, class T1>
  393. inline std::basic_istream<CharType, CharTrait> &
  394. read (std::basic_istream<CharType, CharTrait> &is, cons<T1, null_type>& t1) {
  395. if (!is.good()) return is;
  396. return is >> t1.head;
  397. }
  398. #else
  399. template<class CharType, class CharTrait>
  400. inline std::basic_istream<CharType, CharTrait>&
  401. read(std::basic_istream<CharType, CharTrait>& i, const null_type&) { return i; }
  402. #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  403. template<class CharType, class CharTrait, class T1, class T2>
  404. inline std::basic_istream<CharType, CharTrait>&
  405. read(std::basic_istream<CharType, CharTrait> &is, cons<T1, T2>& t1) {
  406. if (!is.good()) return is;
  407. is >> t1.head;
  408. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  409. if (tuples::length<T2>::value == 0)
  410. return is;
  411. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  412. extract_and_check_delimiter(is, format_info::delimiter);
  413. return read(is, t1.tail);
  414. }
  415. } // end namespace detail
  416. template<class CharType, class CharTrait>
  417. inline std::basic_istream<CharType, CharTrait>&
  418. operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
  419. if (!is.good() ) return is;
  420. detail::extract_and_check_delimiter(is, detail::format_info::open);
  421. detail::extract_and_check_delimiter(is, detail::format_info::close);
  422. return is;
  423. }
  424. template<class CharType, class CharTrait, class T1, class T2>
  425. inline std::basic_istream<CharType, CharTrait>&
  426. operator>>(std::basic_istream<CharType, CharTrait>& is, cons<T1, T2>& t1) {
  427. if (!is.good() ) return is;
  428. detail::extract_and_check_delimiter(is, detail::format_info::open);
  429. detail::read(is, t1);
  430. detail::extract_and_check_delimiter(is, detail::format_info::close);
  431. return is;
  432. }
  433. #endif // BOOST_NO_TEMPLATED_STREAMS
  434. } // end of namespace tuples
  435. } // end of namespace boost
  436. #endif // BOOST_TUPLE_IO_HPP