1 // tuple_io.hpp --------------------------------------------------------------
3 // Copyright (C) 2001 Jaakko Jarvi (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)
289 inline std::ostream& operator<<(std::ostream& o, const null_type& 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);
303 template<class T1, class T2>
304 inline std::ostream& operator<<(std::ostream& o, const cons<T1, T2>& t) {
305 if (!o.good() ) return o;
308 detail::format_info::get_manipulator(o, detail::format_info::open);
310 detail::format_info::get_manipulator(o, detail::format_info::close);
323 template<class CharType, class CharTrait>
324 inline std::basic_ostream<CharType, CharTrait>&
325 operator<<(std::basic_ostream<CharType, CharTrait>& o,
326 const null_type& t) {
327 if (!o.good() ) return o;
330 detail::format_info::get_manipulator(o, detail::format_info::open);
332 detail::format_info::get_manipulator(o, detail::format_info::close);
340 template<class CharType, class CharTrait, class T1, class T2>
341 inline std::basic_ostream<CharType, CharTrait>&
342 operator<<(std::basic_ostream<CharType, CharTrait>& o,
343 const cons<T1, T2>& t) {
344 if (!o.good() ) return o;
347 detail::format_info::get_manipulator(o, detail::format_info::open);
349 detail::format_info::get_manipulator(o, detail::format_info::close);
359 #endif // BOOST_NO_TEMPLATED_STREAMS
362 // -------------------------------------------------------------
363 // input stream operators
367 #if defined (BOOST_NO_TEMPLATED_STREAMS)
370 extract_and_check_delimiter(
371 std::istream& is, format_info::manipulator_type del)
373 const char d = format_info::get_manipulator(is, del);
375 #if defined (BOOST_NO_STD_LOCALE)
376 const bool is_delimiter = !isspace(d);
378 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
384 if (is.good() && c!=d) {
385 is.setstate(std::ios::failbit);
392 // Note: The order of the read functions is critical to let a
393 // (conforming?) compiler find and select the correct one.
395 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
397 inline std::istream &
398 read (std::istream &is, cons<T1, null_type>& t1) {
400 if (!is.good()) return is;
402 return is >> t1.head ;
405 inline std::istream& read(std::istream& i, const null_type&) { return i; }
406 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
408 template<class T1, class T2>
410 read(std::istream &is, cons<T1, T2>& t1) {
412 if (!is.good()) return is;
416 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
417 if (tuples::length<T2>::value == 0)
419 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
421 extract_and_check_delimiter(is, format_info::delimiter);
423 return read(is, t1.tail);
426 } // end namespace detail
429 operator>>(std::istream &is, null_type&) {
431 if (!is.good() ) return is;
433 detail::extract_and_check_delimiter(is, detail::format_info::open);
434 detail::extract_and_check_delimiter(is, detail::format_info::close);
440 template<class T1, class T2>
442 operator>>(std::istream& is, cons<T1, T2>& t1) {
444 if (!is.good() ) return is;
446 detail::extract_and_check_delimiter(is, detail::format_info::open);
448 detail::read(is, t1);
450 detail::extract_and_check_delimiter(is, detail::format_info::close);
459 template<class CharType, class CharTrait>
460 inline std::basic_istream<CharType, CharTrait>&
461 extract_and_check_delimiter(
462 std::basic_istream<CharType, CharTrait> &is, format_info::manipulator_type del)
464 const CharType d = format_info::get_manipulator(is, del);
466 #if defined (BOOST_NO_STD_LOCALE)
467 const bool is_delimiter = !isspace(d);
468 #elif defined ( __BORLANDC__ )
469 const bool is_delimiter = !std::use_facet< std::ctype< CharType > >
470 (is.getloc() ).is( std::ctype_base::space, d);
472 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
478 if (is.good() && c!=d) {
479 is.setstate(std::ios::failbit);
486 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
487 template<class CharType, class CharTrait, class T1>
488 inline std::basic_istream<CharType, CharTrait> &
489 read (std::basic_istream<CharType, CharTrait> &is, cons<T1, null_type>& t1) {
491 if (!is.good()) return is;
493 return is >> t1.head;
496 template<class CharType, class CharTrait>
497 inline std::basic_istream<CharType, CharTrait>&
498 read(std::basic_istream<CharType, CharTrait>& i, const null_type&) { return i; }
500 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
502 template<class CharType, class CharTrait, class T1, class T2>
503 inline std::basic_istream<CharType, CharTrait>&
504 read(std::basic_istream<CharType, CharTrait> &is, cons<T1, T2>& t1) {
506 if (!is.good()) return is;
510 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
511 if (tuples::length<T2>::value == 0)
513 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
515 extract_and_check_delimiter(is, format_info::delimiter);
517 return read(is, t1.tail);
520 } // end namespace detail
523 template<class CharType, class CharTrait>
524 inline std::basic_istream<CharType, CharTrait>&
525 operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
527 if (!is.good() ) return is;
529 detail::extract_and_check_delimiter(is, detail::format_info::open);
530 detail::extract_and_check_delimiter(is, detail::format_info::close);
535 template<class CharType, class CharTrait, class T1, class T2>
536 inline std::basic_istream<CharType, CharTrait>&
537 operator>>(std::basic_istream<CharType, CharTrait>& is, cons<T1, T2>& t1) {
539 if (!is.good() ) return is;
541 detail::extract_and_check_delimiter(is, detail::format_info::open);
543 detail::read(is, t1);
545 detail::extract_and_check_delimiter(is, detail::format_info::close);
550 #endif // BOOST_NO_TEMPLATED_STREAMS
552 } // end of namespace tuples
553 } // end of namespace boost
555 #endif // BOOST_TUPLE_IO_HPP