Update contrib.
1 // tuple_io.hpp --------------------------------------------------------------
3 // Copyright (C) 2001 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
4 // 2001 Gary Powell (gary.powell@sierra.com)
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org
11 // ----------------------------------------------------------------------------
13 #ifndef BOOST_TUPLE_IO_HPP
14 #define BOOST_TUPLE_IO_HPP
17 // add to boost/config.hpp
20 # if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
21 #define BOOST_NO_TEMPLATED_STREAMS
25 #if defined BOOST_NO_TEMPLATED_STREAMS
32 #include "boost/tuple/tuple.hpp"
34 // This is ugly: one should be using twoargument isspace since whitspace can
35 // be locale dependent, in theory at least.
36 // not all libraries implement have the two-arg version, so we need to
37 // use the one-arg one, which one should get with <cctype> but there seem
38 // to be exceptions to this.
40 #if !defined (BOOST_NO_STD_LOCALE)
42 #include <locale> // for two-arg isspace
46 #include <cctype> // for one-arg (old) isspace
47 #include <ctype.h> // Metrowerks does not find one-arg isspace from cctype
59 enum manipulator_type { open, close, delimiter };
60 BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
63 static int get_stream_index (int m)
65 static const int stream_index[number_of_manipulators]
66 = { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
68 return stream_index[m];
71 format_info(const format_info&);
77 #if defined (BOOST_NO_TEMPLATED_STREAMS)
78 static char get_manipulator(std::ios& i, manipulator_type m) {
79 char c = static_cast<char>(i.iword(get_stream_index(m)));
81 // parentheses and space are the default manipulators
84 case detail::format_info::open : c = '('; break;
85 case detail::format_info::close : c = ')'; break;
86 case detail::format_info::delimiter : c = ' '; break;
92 static void set_manipulator(std::ios& i, manipulator_type m, char c) {
93 i.iword(get_stream_index(m)) = static_cast<long>(c);
96 template<class CharType, class CharTrait>
97 static CharType get_manipulator(std::basic_ios<CharType, CharTrait>& i,
99 // The manipulators are stored as long.
100 // A valid instanitation of basic_stream allows CharType to be any POD,
101 // hence, the static_cast may fail (it fails if long is not convertible
103 CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) );
104 // parentheses and space are the default manipulators
107 case detail::format_info::open : c = i.widen('('); break;
108 case detail::format_info::close : c = i.widen(')'); break;
109 case detail::format_info::delimiter : c = i.widen(' '); break;
116 template<class CharType, class CharTrait>
117 static void set_manipulator(std::basic_ios<CharType, CharTrait>& i,
118 manipulator_type m, CharType c) {
119 // The manipulators are stored as long.
120 // A valid instanitation of basic_stream allows CharType to be any POD,
121 // hence, the static_cast may fail (it fails if CharType is not
123 i.iword(get_stream_index(m)) = static_cast<long>(c);
125 #endif // BOOST_NO_TEMPLATED_STREAMS
128 } // end of namespace detail
130 template<class CharType>
131 class tuple_manipulator {
132 const detail::format_info::manipulator_type mt;
135 explicit tuple_manipulator(detail::format_info::manipulator_type m,
139 #if defined (BOOST_NO_TEMPLATED_STREAMS)
140 void set(std::ios &io) const {
141 detail::format_info::set_manipulator(io, mt, f_c);
144 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
145 template<class CharType2, class CharTrait>
146 void set(std::basic_ios<CharType2, CharTrait> &io) const {
147 detail::format_info::set_manipulator(io, mt, f_c);
150 template<class CharTrait>
151 void set(std::basic_ios<CharType, CharTrait> &io) const {
152 detail::format_info::set_manipulator(io, mt, f_c);
154 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
155 #endif // BOOST_NO_TEMPLATED_STREAMS
158 #if defined (BOOST_NO_TEMPLATED_STREAMS)
160 operator<<(std::ostream& o, const tuple_manipulator<char>& m) {
166 operator>>(std::istream& i, const tuple_manipulator<char>& m) {
173 template<class CharType, class CharTrait>
174 inline std::basic_ostream<CharType, CharTrait>&
175 operator<<(std::basic_ostream<CharType, CharTrait>& o, const tuple_manipulator<CharType>& m) {
180 template<class CharType, class CharTrait>
181 inline std::basic_istream<CharType, CharTrait>&
182 operator>>(std::basic_istream<CharType, CharTrait>& i, const tuple_manipulator<CharType>& m) {
187 #endif // BOOST_NO_TEMPLATED_STREAMS
189 template<class CharType>
190 inline tuple_manipulator<CharType> set_open(const CharType c) {
191 return tuple_manipulator<CharType>(detail::format_info::open, c);
194 template<class CharType>
195 inline tuple_manipulator<CharType> set_close(const CharType c) {
196 return tuple_manipulator<CharType>(detail::format_info::close, c);
199 template<class CharType>
200 inline tuple_manipulator<CharType> set_delimiter(const CharType c) {
201 return tuple_manipulator<CharType>(detail::format_info::delimiter, c);
208 // -------------------------------------------------------------
209 // printing tuples to ostream in format (a b c)
210 // parentheses and space are defaults, but can be overriden with manipulators
211 // set_open, set_close and set_delimiter
215 // Note: The order of the print functions is critical
216 // to let a conforming compiler find and select the correct one.
218 #if defined (BOOST_NO_TEMPLATED_STREAMS)
220 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
222 inline std::ostream& print(std::ostream& o, const cons<T1, null_type>& t) {
225 #endif // BOOST_NO_TEMPLATED_STREAMS
227 inline std::ostream& print(std::ostream& o, const null_type&) { return o; }
229 template<class T1, class T2>
231 print(std::ostream& o, const cons<T1, T2>& t) {
233 const char d = format_info::get_manipulator(o, format_info::delimiter);
237 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
238 if (tuples::length<T2>::value == 0)
240 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
243 return print(o, t.tail );
251 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
252 template<class CharType, class CharTrait, class T1>
253 inline std::basic_ostream<CharType, CharTrait>&
254 print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, null_type>& t) {
257 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
260 template<class CharType, class CharTrait>
261 inline std::basic_ostream<CharType, CharTrait>&
262 print(std::basic_ostream<CharType, CharTrait>& o, const null_type&) {
266 template<class CharType, class CharTrait, class T1, class T2>
267 inline std::basic_ostream<CharType, CharTrait>&
268 print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, T2>& t) {
270 const CharType d = format_info::get_manipulator(o, format_info::delimiter);
274 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
275 if (tuples::length<T2>::value == 0)
277 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
280 return print(o, t.tail);
283 #endif // BOOST_NO_TEMPLATED_STREAMS
285 } // namespace detail
287 #if defined (BOOST_NO_TEMPLATED_STREAMS)
288 template<class T1, class T2>
289 inline std::ostream& operator<<(std::ostream& o, const cons<T1, T2>& t) {
290 if (!o.good() ) return o;
293 detail::format_info::get_manipulator(o, detail::format_info::open);
295 detail::format_info::get_manipulator(o, detail::format_info::close);
308 template<class CharType, class CharTrait, class T1, class T2>
309 inline std::basic_ostream<CharType, CharTrait>&
310 operator<<(std::basic_ostream<CharType, CharTrait>& o,
311 const cons<T1, T2>& t) {
312 if (!o.good() ) return o;
315 detail::format_info::get_manipulator(o, detail::format_info::open);
317 detail::format_info::get_manipulator(o, detail::format_info::close);
327 #endif // BOOST_NO_TEMPLATED_STREAMS
330 // -------------------------------------------------------------
331 // input stream operators
335 #if defined (BOOST_NO_TEMPLATED_STREAMS)
338 extract_and_check_delimiter(
339 std::istream& is, format_info::manipulator_type del)
341 const char d = format_info::get_manipulator(is, del);
343 #if defined (BOOST_NO_STD_LOCALE)
344 const bool is_delimiter = !isspace(d);
346 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
352 if (is.good() && c!=d) {
353 is.setstate(std::ios::failbit);
360 // Note: The order of the read functions is critical to let a
361 // (conforming?) compiler find and select the correct one.
363 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
365 inline std::istream &
366 read (std::istream &is, cons<T1, null_type>& t1) {
368 if (!is.good()) return is;
370 return is >> t1.head ;
373 inline std::istream& read(std::istream& i, const null_type&) { return i; }
374 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
376 template<class T1, class T2>
378 read(std::istream &is, cons<T1, T2>& t1) {
380 if (!is.good()) return is;
384 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
385 if (tuples::length<T2>::value == 0)
387 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
389 extract_and_check_delimiter(is, format_info::delimiter);
391 return read(is, t1.tail);
394 } // end namespace detail
397 operator>>(std::istream &is, null_type&) {
399 if (!is.good() ) return is;
401 detail::extract_and_check_delimiter(is, detail::format_info::open);
402 detail::extract_and_check_delimiter(is, detail::format_info::close);
408 template<class T1, class T2>
410 operator>>(std::istream& is, cons<T1, T2>& t1) {
412 if (!is.good() ) return is;
414 detail::extract_and_check_delimiter(is, detail::format_info::open);
416 detail::read(is, t1);
418 detail::extract_and_check_delimiter(is, detail::format_info::close);
427 template<class CharType, class CharTrait>
428 inline std::basic_istream<CharType, CharTrait>&
429 extract_and_check_delimiter(
430 std::basic_istream<CharType, CharTrait> &is, format_info::manipulator_type del)
432 const CharType d = format_info::get_manipulator(is, del);
434 #if defined (BOOST_NO_STD_LOCALE)
435 const bool is_delimiter = !isspace(d);
436 #elif defined ( __BORLANDC__ )
437 const bool is_delimiter = !std::use_facet< std::ctype< CharType > >
438 (is.getloc() ).is( std::ctype_base::space, d);
440 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
446 if (is.good() && c!=d) {
447 is.setstate(std::ios::failbit);
454 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
455 template<class CharType, class CharTrait, class T1>
456 inline std::basic_istream<CharType, CharTrait> &
457 read (std::basic_istream<CharType, CharTrait> &is, cons<T1, null_type>& t1) {
459 if (!is.good()) return is;
461 return is >> t1.head;
464 template<class CharType, class CharTrait>
465 inline std::basic_istream<CharType, CharTrait>&
466 read(std::basic_istream<CharType, CharTrait>& i, const null_type&) { return i; }
468 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
470 template<class CharType, class CharTrait, class T1, class T2>
471 inline std::basic_istream<CharType, CharTrait>&
472 read(std::basic_istream<CharType, CharTrait> &is, cons<T1, T2>& t1) {
474 if (!is.good()) return is;
478 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
479 if (tuples::length<T2>::value == 0)
481 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
483 extract_and_check_delimiter(is, format_info::delimiter);
485 return read(is, t1.tail);
488 } // end namespace detail
491 template<class CharType, class CharTrait>
492 inline std::basic_istream<CharType, CharTrait>&
493 operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
495 if (!is.good() ) return is;
497 detail::extract_and_check_delimiter(is, detail::format_info::open);
498 detail::extract_and_check_delimiter(is, detail::format_info::close);
503 template<class CharType, class CharTrait, class T1, class T2>
504 inline std::basic_istream<CharType, CharTrait>&
505 operator>>(std::basic_istream<CharType, CharTrait>& is, cons<T1, T2>& t1) {
507 if (!is.good() ) return is;
509 detail::extract_and_check_delimiter(is, detail::format_info::open);
511 detail::read(is, t1);
513 detail::extract_and_check_delimiter(is, detail::format_info::close);
518 #endif // BOOST_NO_TEMPLATED_STREAMS
520 } // end of namespace tuples
521 } // end of namespace boost
523 #endif // BOOST_TUPLE_IO_HPP