tuple_io.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #include <istream>
  13. #include <ostream>
  14. #include <sstream>
  15. #include <boost/tuple/tuple.hpp>
  16. // This is ugly: one should be using twoargument isspace since whitspace can
  17. // be locale dependent, in theory at least.
  18. // not all libraries implement have the two-arg version, so we need to
  19. // use the one-arg one, which one should get with <cctype> but there seem
  20. // to be exceptions to this.
  21. #if !defined (BOOST_NO_STD_LOCALE)
  22. #include <locale> // for two-arg isspace
  23. #else
  24. #include <cctype> // for one-arg (old) isspace
  25. #include <ctype.h> // Metrowerks does not find one-arg isspace from cctype
  26. #endif
  27. namespace boost {
  28. namespace tuples {
  29. namespace detail {
  30. class format_info {
  31. public:
  32. enum manipulator_type { open, close, delimiter };
  33. BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
  34. private:
  35. static int get_stream_index (int m)
  36. {
  37. static const int stream_index[number_of_manipulators]
  38. = { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
  39. return stream_index[m];
  40. }
  41. format_info(const format_info&);
  42. format_info();
  43. public:
  44. template<class CharType, class CharTrait>
  45. static CharType get_manipulator(std::basic_ios<CharType, CharTrait>& i,
  46. manipulator_type m) {
  47. // The manipulators are stored as long.
  48. // A valid instanitation of basic_stream allows CharType to be any POD,
  49. // hence, the static_cast may fail (it fails if long is not convertible
  50. // to CharType
  51. CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) );
  52. // parentheses and space are the default manipulators
  53. if (!c) {
  54. switch(m) {
  55. case detail::format_info::open : c = i.widen('('); break;
  56. case detail::format_info::close : c = i.widen(')'); break;
  57. case detail::format_info::delimiter : c = i.widen(' '); break;
  58. }
  59. }
  60. return c;
  61. }
  62. template<class CharType, class CharTrait>
  63. static void set_manipulator(std::basic_ios<CharType, CharTrait>& i,
  64. manipulator_type m, CharType c) {
  65. // The manipulators are stored as long.
  66. // A valid instanitation of basic_stream allows CharType to be any POD,
  67. // hence, the static_cast may fail (it fails if CharType is not
  68. // convertible long.
  69. i.iword(get_stream_index(m)) = static_cast<long>(c);
  70. }
  71. };
  72. } // end of namespace detail
  73. template<class CharType>
  74. class tuple_manipulator {
  75. const detail::format_info::manipulator_type mt;
  76. CharType f_c;
  77. public:
  78. explicit tuple_manipulator(detail::format_info::manipulator_type m,
  79. CharType c = CharType())
  80. : mt(m), f_c(c) {}
  81. template<class CharTrait>
  82. void set(std::basic_ios<CharType, CharTrait> &io) const {
  83. detail::format_info::set_manipulator(io, mt, f_c);
  84. }
  85. };
  86. template<class CharType, class CharTrait>
  87. inline std::basic_ostream<CharType, CharTrait>&
  88. operator<<(std::basic_ostream<CharType, CharTrait>& o, const tuple_manipulator<CharType>& m) {
  89. m.set(o);
  90. return o;
  91. }
  92. template<class CharType, class CharTrait>
  93. inline std::basic_istream<CharType, CharTrait>&
  94. operator>>(std::basic_istream<CharType, CharTrait>& i, const tuple_manipulator<CharType>& m) {
  95. m.set(i);
  96. return i;
  97. }
  98. template<class CharType>
  99. inline tuple_manipulator<CharType> set_open(const CharType c) {
  100. return tuple_manipulator<CharType>(detail::format_info::open, c);
  101. }
  102. template<class CharType>
  103. inline tuple_manipulator<CharType> set_close(const CharType c) {
  104. return tuple_manipulator<CharType>(detail::format_info::close, c);
  105. }
  106. template<class CharType>
  107. inline tuple_manipulator<CharType> set_delimiter(const CharType c) {
  108. return tuple_manipulator<CharType>(detail::format_info::delimiter, c);
  109. }
  110. // -------------------------------------------------------------
  111. // printing tuples to ostream in format (a b c)
  112. // parentheses and space are defaults, but can be overriden with manipulators
  113. // set_open, set_close and set_delimiter
  114. namespace detail {
  115. // Note: The order of the print functions is critical
  116. // to let a conforming compiler find and select the correct one.
  117. template<class CharType, class CharTrait, class T1>
  118. inline std::basic_ostream<CharType, CharTrait>&
  119. print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, null_type>& t) {
  120. return o << t.head;
  121. }
  122. template<class CharType, class CharTrait>
  123. inline std::basic_ostream<CharType, CharTrait>&
  124. print(std::basic_ostream<CharType, CharTrait>& o, const null_type&) {
  125. return o;
  126. }
  127. template<class CharType, class CharTrait, class T1, class T2>
  128. inline std::basic_ostream<CharType, CharTrait>&
  129. print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, T2>& t) {
  130. const CharType d = format_info::get_manipulator(o, format_info::delimiter);
  131. o << t.head;
  132. o << d;
  133. return print(o, t.tail);
  134. }
  135. template<class CharT, class Traits, class T>
  136. inline bool handle_width(std::basic_ostream<CharT, Traits>& o, const T& t) {
  137. std::streamsize width = o.width();
  138. if(width == 0) return false;
  139. std::basic_ostringstream<CharT, Traits> ss;
  140. ss.copyfmt(o);
  141. ss.tie(0);
  142. ss.width(0);
  143. ss << t;
  144. o << ss.str();
  145. return true;
  146. }
  147. } // namespace detail
  148. template<class CharType, class CharTrait>
  149. inline std::basic_ostream<CharType, CharTrait>&
  150. operator<<(std::basic_ostream<CharType, CharTrait>& o,
  151. const null_type& t) {
  152. if (!o.good() ) return o;
  153. if (detail::handle_width(o, t)) return o;
  154. const CharType l =
  155. detail::format_info::get_manipulator(o, detail::format_info::open);
  156. const CharType r =
  157. detail::format_info::get_manipulator(o, detail::format_info::close);
  158. o << l;
  159. o << r;
  160. return o;
  161. }
  162. template<class CharType, class CharTrait, class T1, class T2>
  163. inline std::basic_ostream<CharType, CharTrait>&
  164. operator<<(std::basic_ostream<CharType, CharTrait>& o,
  165. const cons<T1, T2>& t) {
  166. if (!o.good() ) return o;
  167. if (detail::handle_width(o, t)) return o;
  168. const CharType l =
  169. detail::format_info::get_manipulator(o, detail::format_info::open);
  170. const CharType r =
  171. detail::format_info::get_manipulator(o, detail::format_info::close);
  172. o << l;
  173. detail::print(o, t);
  174. o << r;
  175. return o;
  176. }
  177. // -------------------------------------------------------------
  178. // input stream operators
  179. namespace detail {
  180. template<class CharType, class CharTrait>
  181. inline std::basic_istream<CharType, CharTrait>&
  182. extract_and_check_delimiter(
  183. std::basic_istream<CharType, CharTrait> &is, format_info::manipulator_type del)
  184. {
  185. const CharType d = format_info::get_manipulator(is, del);
  186. #if defined (BOOST_NO_STD_LOCALE)
  187. const bool is_delimiter = !isspace(d);
  188. #elif defined ( BOOST_BORLANDC )
  189. const bool is_delimiter = !std::use_facet< std::ctype< CharType > >
  190. (is.getloc() ).is( std::ctype_base::space, d);
  191. #else
  192. const bool is_delimiter = (!std::isspace(d, is.getloc()) );
  193. #endif
  194. CharType c;
  195. if (is_delimiter) {
  196. is >> c;
  197. if (is.good() && c!=d) {
  198. is.setstate(std::ios::failbit);
  199. }
  200. } else {
  201. is >> std::ws;
  202. }
  203. return is;
  204. }
  205. template<class CharType, class CharTrait, class T1>
  206. inline std::basic_istream<CharType, CharTrait> &
  207. read (std::basic_istream<CharType, CharTrait> &is, cons<T1, null_type>& t1) {
  208. if (!is.good()) return is;
  209. return is >> t1.head;
  210. }
  211. template<class CharType, class CharTrait, class T1, class T2>
  212. inline std::basic_istream<CharType, CharTrait>&
  213. read(std::basic_istream<CharType, CharTrait> &is, cons<T1, T2>& t1) {
  214. if (!is.good()) return is;
  215. is >> t1.head;
  216. extract_and_check_delimiter(is, format_info::delimiter);
  217. return read(is, t1.tail);
  218. }
  219. } // end namespace detail
  220. template<class CharType, class CharTrait>
  221. inline std::basic_istream<CharType, CharTrait>&
  222. operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
  223. if (!is.good() ) return is;
  224. detail::extract_and_check_delimiter(is, detail::format_info::open);
  225. detail::extract_and_check_delimiter(is, detail::format_info::close);
  226. return is;
  227. }
  228. template<class CharType, class CharTrait, class T1, class T2>
  229. inline std::basic_istream<CharType, CharTrait>&
  230. operator>>(std::basic_istream<CharType, CharTrait>& is, cons<T1, T2>& t1) {
  231. if (!is.good() ) return is;
  232. detail::extract_and_check_delimiter(is, detail::format_info::open);
  233. detail::read(is, t1);
  234. detail::extract_and_check_delimiter(is, detail::format_info::close);
  235. return is;
  236. }
  237. } // end of namespace tuples
  238. } // end of namespace boost
  239. #endif // BOOST_TUPLE_IO_HPP