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
34 #include "boost/tuple/tuple.hpp"
36 // This is ugly: one should be using twoargument isspace since whitspace can
37 // be locale dependent, in theory at least.
38 // not all libraries implement have the two-arg version, so we need to
39 // use the one-arg one, which one should get with <cctype> but there seem
40 // to be exceptions to this.
42 #if !defined (BOOST_NO_STD_LOCALE)
44 #include <locale> // for two-arg isspace
48 #include <cctype> // for one-arg (old) isspace
49 #include <ctype.h> // Metrowerks does not find one-arg isspace from cctype
61 enum manipulator_type { open, close, delimiter };
62 BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
65 static int get_stream_index (int m)
67 static const int stream_index[number_of_manipulators]
68 = { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
70 return stream_index[m];
73 format_info(const format_info&);
79 #if defined (BOOST_NO_TEMPLATED_STREAMS)
80 static char get_manipulator(std::ios& i, manipulator_type m) {
81 char c = static_cast<char>(i.iword(get_stream_index(m)));
83 // parentheses and space are the default manipulators
86 case detail::format_info::open : c = '('; break;
87 case detail::format_info::close : c = ')'; break;
88 case detail::format_info::delimiter : c = ' '; break;
94 static void set_manipulator(std::ios& i, manipulator_type m, char c) {
95 i.iword(get_stream_index(m)) = static_cast<long>(c);
98 template<class CharType, class CharTrait>
99 static CharType get_manipulator(std::basic_ios<CharType, CharTrait>& i,
100 manipulator_type m) {
101 // The manipulators are stored as long.
102 // A valid instanitation of basic_stream allows CharType to be any POD,
103 // hence, the static_cast may fail (it fails if long is not convertible
105 CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) );
106 // parentheses and space are the default manipulators
109 case detail::format_info::open : c = i.widen('('); break;
110 case detail::format_info::close : c = i.widen(')'); break;
111 case detail::format_info::delimiter : c = i.widen(' '); break;
118 template<class CharType, class CharTrait>
119 static void set_manipulator(std::basic_ios<CharType, CharTrait>& i,
120 manipulator_type m, CharType c) {
121 // The manipulators are stored as long.
122 // A valid instanitation of basic_stream allows CharType to be any POD,
123 // hence, the static_cast may fail (it fails if CharType is not
125 i.iword(get_stream_index(m)) = static_cast<long>(c);
127 #endif // BOOST_NO_TEMPLATED_STREAMS
130 } // end of namespace detail
132 template<class CharType>
133 class tuple_manipulator {
134 const detail::format_info::manipulator_type mt;
137 explicit tuple_manipulator(detail::format_info::manipulator_type m,
141 #if defined (BOOST_NO_TEMPLATED_STREAMS)
142 void set(std::ios &io) const {
143 detail::format_info::set_manipulator(io, mt, f_c);
146 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
147 template<class CharType2, class CharTrait>
148 void set(std::basic_ios<CharType2, CharTrait> &io) const {
149 detail::format_info::set_manipulator(io, mt, f_c);
152 template<class CharTrait>
153 void set(std::basic_ios<CharType, CharTrait> &io) const {
154 detail::format_info::set_manipulator(io, mt, f_c);
156 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
157 #endif // BOOST_NO_TEMPLATED_STREAMS
160 #if defined (BOOST_NO_TEMPLATED_STREAMS)
162 operator<<(std::ostream& o, const tuple_manipulator<char>& m) {
168 operator>>(std::istream& i, const tuple_manipulator<char>& m) {
175 template<class CharType, class CharTrait>
176 inline std::basic_ostream<CharType, CharTrait>&
177 operator<<(std::basic_ostream<CharType, CharTrait>& o, const tuple_manipulator<CharType>& m) {
182 template<class CharType, class CharTrait>
183 inline std::basic_istream<CharType, CharTrait>&
184 operator>>(std::basic_istream<CharType, CharTrait>& i, const tuple_manipulator<CharType>& m) {
189 #endif // BOOST_NO_TEMPLATED_STREAMS
191 template<class CharType>
192 inline tuple_manipulator<CharType> set_open(const CharType c) {
193 return tuple_manipulator<CharType>(detail::format_info::open, c);
196 template<class CharType>
197 inline tuple_manipulator<CharType> set_close(const CharType c) {
198 return tuple_manipulator<CharType>(detail::format_info::close, c);
201 template<class CharType>
202 inline tuple_manipulator<CharType> set_delimiter(const CharType c) {
203 return tuple_manipulator<CharType>(detail::format_info::delimiter, c);
210 // -------------------------------------------------------------
211 // printing tuples to ostream in format (a b c)
212 // parentheses and space are defaults, but can be overriden with manipulators
213 // set_open, set_close and set_delimiter
217 // Note: The order of the print functions is critical
218 // to let a conforming compiler find and select the correct one.
220 #if defined (BOOST_NO_TEMPLATED_STREAMS)
222 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
224 inline std::ostream& print(std::ostream& o, const cons<T1, null_type>& t) {
227 #endif // BOOST_NO_TEMPLATED_STREAMS
229 inline std::ostream& print(std::ostream& o, const null_type&) { return o; }
231 template<class T1, class T2>
233 print(std::ostream& o, const cons<T1, T2>& t) {
235 const char d = format_info::get_manipulator(o, format_info::delimiter);
239 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
240 if (tuples::length<T2>::value == 0)
242 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
245 return print(o, t.tail );
250 inline bool handle_width(std::ostream& o, const T& t) {
251 std::streamsize width = o.width();
252 if(width == 0) return false;
254 std::ostringstream ss;
269 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
270 template<class CharType, class CharTrait, class T1>
271 inline std::basic_ostream<CharType, CharTrait>&
272 print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, null_type>& t) {
275 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
278 template<class CharType, class CharTrait>
279 inline std::basic_ostream<CharType, CharTrait>&
280 print(std::basic_ostream<CharType, CharTrait>& o, const null_type&) {
284 template<class CharType, class CharTrait, class T1, class T2>
285 inline std::basic_ostream<CharType, CharTrait>&
286 print(std::basic_ostream<CharType, CharTrait>& o, const cons<T1, T2>& t) {
288 const CharType d = format_info::get_manipulator(o, format_info::delimiter);
292 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
293 if (tuples::length<T2>::value == 0)
295 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
298 return print(o, t.tail);
301 template<class CharT, class Traits, class T>
302 inline bool handle_width(std::basic_ostream<CharT, Traits>& o, const T& t) {
303 std::streamsize width = o.width();
304 if(width == 0) return false;
306 std::basic_ostringstream<CharT, Traits> ss;
318 #endif // BOOST_NO_TEMPLATED_STREAMS
320 } // namespace detail
322 #if defined (BOOST_NO_TEMPLATED_STREAMS)
324 inline std::ostream& operator<<(std::ostream& o, const null_type& t) {
325 if (!o.good() ) return o;
326 if (detail::handle_width(o, t)) return o;
329 detail::format_info::get_manipulator(o, detail::format_info::open);
331 detail::format_info::get_manipulator(o, detail::format_info::close);
339 template<class T1, class T2>
340 inline std::ostream& operator<<(std::ostream& o, const cons<T1, T2>& t) {
341 if (!o.good() ) return o;
342 if (detail::handle_width(o, t)) return o;
345 detail::format_info::get_manipulator(o, detail::format_info::open);
347 detail::format_info::get_manipulator(o, detail::format_info::close);
360 template<class CharType, class CharTrait>
361 inline std::basic_ostream<CharType, CharTrait>&
362 operator<<(std::basic_ostream<CharType, CharTrait>& o,
363 const null_type& t) {
364 if (!o.good() ) return o;
365 if (detail::handle_width(o, t)) return o;
368 detail::format_info::get_manipulator(o, detail::format_info::open);
370 detail::format_info::get_manipulator(o, detail::format_info::close);
378 template<class CharType, class CharTrait, class T1, class T2>
379 inline std::basic_ostream<CharType, CharTrait>&
380 operator<<(std::basic_ostream<CharType, CharTrait>& o,
381 const cons<T1, T2>& t) {
382 if (!o.good() ) return o;
383 if (detail::handle_width(o, t)) return o;
386 detail::format_info::get_manipulator(o, detail::format_info::open);
388 detail::format_info::get_manipulator(o, detail::format_info::close);
398 #endif // BOOST_NO_TEMPLATED_STREAMS
401 // -------------------------------------------------------------
402 // input stream operators
406 #if defined (BOOST_NO_TEMPLATED_STREAMS)
409 extract_and_check_delimiter(
410 std::istream& is, format_info::manipulator_type del)
412 const char d = format_info::get_manipulator(is, del);
414 #if defined (BOOST_NO_STD_LOCALE)
415 const bool is_delimiter = !isspace(d);
417 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
423 if (is.good() && c!=d) {
424 is.setstate(std::ios::failbit);
433 // Note: The order of the read functions is critical to let a
434 // (conforming?) compiler find and select the correct one.
436 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
438 inline std::istream &
439 read (std::istream &is, cons<T1, null_type>& t1) {
441 if (!is.good()) return is;
443 return is >> t1.head ;
446 inline std::istream& read(std::istream& i, const null_type&) { return i; }
447 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
449 template<class T1, class T2>
451 read(std::istream &is, cons<T1, T2>& t1) {
453 if (!is.good()) return is;
457 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
458 if (tuples::length<T2>::value == 0)
460 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
462 extract_and_check_delimiter(is, format_info::delimiter);
464 return read(is, t1.tail);
467 } // end namespace detail
470 operator>>(std::istream &is, null_type&) {
472 if (!is.good() ) return is;
474 detail::extract_and_check_delimiter(is, detail::format_info::open);
475 detail::extract_and_check_delimiter(is, detail::format_info::close);
481 template<class T1, class T2>
483 operator>>(std::istream& is, cons<T1, T2>& t1) {
485 if (!is.good() ) return is;
487 detail::extract_and_check_delimiter(is, detail::format_info::open);
489 detail::read(is, t1);
491 detail::extract_and_check_delimiter(is, detail::format_info::close);
500 template<class CharType, class CharTrait>
501 inline std::basic_istream<CharType, CharTrait>&
502 extract_and_check_delimiter(
503 std::basic_istream<CharType, CharTrait> &is, format_info::manipulator_type del)
505 const CharType d = format_info::get_manipulator(is, del);
507 #if defined (BOOST_NO_STD_LOCALE)
508 const bool is_delimiter = !isspace(d);
509 #elif defined ( __BORLANDC__ )
510 const bool is_delimiter = !std::use_facet< std::ctype< CharType > >
511 (is.getloc() ).is( std::ctype_base::space, d);
513 const bool is_delimiter = (!std::isspace(d, is.getloc()) );
519 if (is.good() && c!=d) {
520 is.setstate(std::ios::failbit);
529 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
530 template<class CharType, class CharTrait, class T1>
531 inline std::basic_istream<CharType, CharTrait> &
532 read (std::basic_istream<CharType, CharTrait> &is, cons<T1, null_type>& t1) {
534 if (!is.good()) return is;
536 return is >> t1.head;
539 template<class CharType, class CharTrait>
540 inline std::basic_istream<CharType, CharTrait>&
541 read(std::basic_istream<CharType, CharTrait>& i, const null_type&) { return i; }
543 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
545 template<class CharType, class CharTrait, class T1, class T2>
546 inline std::basic_istream<CharType, CharTrait>&
547 read(std::basic_istream<CharType, CharTrait> &is, cons<T1, T2>& t1) {
549 if (!is.good()) return is;
553 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
554 if (tuples::length<T2>::value == 0)
556 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
558 extract_and_check_delimiter(is, format_info::delimiter);
560 return read(is, t1.tail);
563 } // end namespace detail
566 template<class CharType, class CharTrait>
567 inline std::basic_istream<CharType, CharTrait>&
568 operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
570 if (!is.good() ) return is;
572 detail::extract_and_check_delimiter(is, detail::format_info::open);
573 detail::extract_and_check_delimiter(is, detail::format_info::close);
578 template<class CharType, class CharTrait, class T1, class T2>
579 inline std::basic_istream<CharType, CharTrait>&
580 operator>>(std::basic_istream<CharType, CharTrait>& is, cons<T1, T2>& t1) {
582 if (!is.good() ) return is;
584 detail::extract_and_check_delimiter(is, detail::format_info::open);
586 detail::read(is, t1);
588 detail::extract_and_check_delimiter(is, detail::format_info::close);
593 #endif // BOOST_NO_TEMPLATED_STREAMS
595 } // end of namespace tuples
596 } // end of namespace boost
598 #endif // BOOST_TUPLE_IO_HPP