]> git.lyx.org Git - lyx.git/blob - boost/boost/tuple/tuple_io.hpp
Don't need this.
[lyx.git] / boost / boost / tuple / tuple_io.hpp
1 // tuple_io.hpp --------------------------------------------------------------
2
3 // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 //               2001 Gary Powell (gary.powell@sierra.com)
5 //
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 
10
11 // ----------------------------------------------------------------------------
12
13 #ifndef BOOST_TUPLE_IO_HPP
14 #define BOOST_TUPLE_IO_HPP
15
16
17 // add to boost/config.hpp
18 // for now
19 # if defined __GNUC__
20 #   if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97) 
21 #define BOOST_NO_TEMPLATED_STREAMS
22 #endif
23 #endif // __GNUC__
24
25 #if defined BOOST_NO_TEMPLATED_STREAMS
26 #include <iostream>
27 #else 
28 #include <istream>
29 #include <ostream>
30 #endif  
31
32 #include "boost/tuple/tuple.hpp"
33
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.
39
40 #if !defined (BOOST_NO_STD_LOCALE)
41
42 #include <locale> // for two-arg isspace
43
44 #else 
45
46 #include <cctype> // for one-arg (old) isspace 
47 #include <ctype.h> // Metrowerks does not find one-arg isspace from cctype
48
49 #endif
50
51 namespace boost {
52 namespace tuples {
53
54 namespace detail {
55
56 class format_info {
57 public:   
58
59    enum manipulator_type { open, close, delimiter };
60    BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
61 private:
62    
63    static int get_stream_index (int m)
64    {
65      static const int stream_index[number_of_manipulators]
66         = { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
67
68      return stream_index[m];
69    }
70
71    format_info(const format_info&);
72    format_info();   
73
74
75 public:
76
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))); 
80      
81      // parentheses and space are the default manipulators
82      if (!c) {
83        switch(m) {
84          case detail::format_info::open : c = '('; break;
85          case detail::format_info::close : c = ')'; break;
86          case detail::format_info::delimiter : c = ' '; break;
87        }
88      }
89      return c;
90    }
91
92    static void set_manipulator(std::ios& i, manipulator_type m, char c) {
93       i.iword(get_stream_index(m)) = static_cast<long>(c);
94    }
95 #else
96    template<class CharType, class CharTrait>
97    static CharType get_manipulator(std::basic_ios<CharType, CharTrait>& i, 
98                                    manipulator_type m) {
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 
102      // to CharType
103      CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) ); 
104      // parentheses and space are the default manipulators
105      if (!c) {
106        switch(m) {
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;
110        }
111      }
112      return c;
113    }
114
115
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 
122      // convertible long.
123       i.iword(get_stream_index(m)) = static_cast<long>(c);
124    }
125 #endif // BOOST_NO_TEMPLATED_STREAMS
126 };
127
128 } // end of namespace detail
129  
130 template<class CharType>    
131 class tuple_manipulator {
132   const detail::format_info::manipulator_type mt;
133   CharType f_c;
134 public:
135   explicit tuple_manipulator(detail::format_info::manipulator_type m, 
136                              const char c = 0)
137      : mt(m), f_c(c) {}
138   
139 #if defined (BOOST_NO_TEMPLATED_STREAMS)
140   void set(std::ios &io) const {
141      detail::format_info::set_manipulator(io, mt, f_c);
142   }
143 #else
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);
148   }
149 #else
150    template<class CharTrait>
151   void set(std::basic_ios<CharType, CharTrait> &io) const {
152      detail::format_info::set_manipulator(io, mt, f_c);
153   }
154 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
155 #endif // BOOST_NO_TEMPLATED_STREAMS
156 };
157
158 #if defined (BOOST_NO_TEMPLATED_STREAMS)
159 inline std::ostream&
160 operator<<(std::ostream& o, const tuple_manipulator<char>& m) {
161   m.set(o);
162   return o;
163 }
164
165 inline std::istream&
166 operator>>(std::istream& i, const tuple_manipulator<char>& m) {
167   m.set(i);
168   return i;
169 }
170
171 #else
172
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) {
176   m.set(o);
177   return o;
178 }
179
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) {
183   m.set(i);
184   return i;
185 }
186
187 #endif   // BOOST_NO_TEMPLATED_STREAMS
188    
189 template<class CharType>
190 inline tuple_manipulator<CharType> set_open(const CharType c) {
191    return tuple_manipulator<CharType>(detail::format_info::open, c);
192 }
193
194 template<class CharType>
195 inline tuple_manipulator<CharType> set_close(const CharType c) {
196    return tuple_manipulator<CharType>(detail::format_info::close, c);
197 }
198
199 template<class CharType>
200 inline tuple_manipulator<CharType> set_delimiter(const CharType c) {
201    return tuple_manipulator<CharType>(detail::format_info::delimiter, c);
202 }
203
204
205
206    
207    
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
212    
213 namespace detail {
214
215 // Note: The order of the print functions is critical 
216 // to let a conforming compiler  find and select the correct one.
217
218 #if defined (BOOST_NO_TEMPLATED_STREAMS)
219
220 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
221 template<class T1>
222 inline std::ostream& print(std::ostream& o, const cons<T1, null_type>& t) {
223   return o << t.head;
224 }
225 #endif // BOOST_NO_TEMPLATED_STREAMS
226  
227 inline std::ostream& print(std::ostream& o, const null_type&) { return o; }
228
229 template<class T1, class T2>
230 inline std::ostream& 
231 print(std::ostream& o, const cons<T1, T2>& t) {
232   
233   const char d = format_info::get_manipulator(o, format_info::delimiter);
234    
235   o << t.head;
236
237 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
238   if (tuples::length<T2>::value == 0)
239     return o;
240 #endif  // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
241   o << d;
242   
243   return print(o, t.tail );
244
245 }
246
247
248
249 #else
250
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) {
255   return o << t.head;
256 }
257 #endif  // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
258
259  
260 template<class CharType, class CharTrait>
261 inline std::basic_ostream<CharType, CharTrait>& 
262 print(std::basic_ostream<CharType, CharTrait>& o, const null_type&) { 
263   return o; 
264 }
265
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) {
269   
270   const CharType d = format_info::get_manipulator(o, format_info::delimiter);
271    
272   o << t.head;
273
274 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
275   if (tuples::length<T2>::value == 0)
276     return o;
277 #endif  // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
278   o << d;
279
280   return print(o, t.tail);
281 }
282
283 #endif  // BOOST_NO_TEMPLATED_STREAMS
284
285 } // namespace detail
286
287 #if defined (BOOST_NO_TEMPLATED_STREAMS)
288
289 inline std::ostream& operator<<(std::ostream& o, const null_type& t) {
290   if (!o.good() ) return o;
291  
292   const char l = 
293     detail::format_info::get_manipulator(o, detail::format_info::open);
294   const char r = 
295     detail::format_info::get_manipulator(o, detail::format_info::close);
296    
297   o << l;
298   o << r;
299
300   return o;
301 }
302
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;
306  
307   const char l = 
308     detail::format_info::get_manipulator(o, detail::format_info::open);
309   const char r = 
310     detail::format_info::get_manipulator(o, detail::format_info::close);
311    
312   o << l;
313   
314   detail::print(o, t);  
315
316   o << r;
317
318   return o;
319 }
320
321 #else
322
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;
328  
329   const CharType l = 
330     detail::format_info::get_manipulator(o, detail::format_info::open);
331   const CharType r = 
332     detail::format_info::get_manipulator(o, detail::format_info::close);
333    
334   o << l;
335   o << r;
336
337   return o;
338 }
339
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;
345  
346   const CharType l = 
347     detail::format_info::get_manipulator(o, detail::format_info::open);
348   const CharType r = 
349     detail::format_info::get_manipulator(o, detail::format_info::close);
350    
351   o << l;   
352
353   detail::print(o, t);  
354
355   o << r;
356
357   return o;
358 }
359 #endif // BOOST_NO_TEMPLATED_STREAMS
360
361    
362 // -------------------------------------------------------------
363 // input stream operators
364
365 namespace detail {
366
367 #if defined (BOOST_NO_TEMPLATED_STREAMS)
368
369 inline std::istream& 
370 extract_and_check_delimiter(
371   std::istream& is, format_info::manipulator_type del)
372 {
373   const char d = format_info::get_manipulator(is, del);
374
375 #if defined (BOOST_NO_STD_LOCALE)
376   const bool is_delimiter = !isspace(d);
377 #else
378   const bool is_delimiter = (!std::isspace(d, is.getloc()) );            
379 #endif
380
381   char c;
382   if (is_delimiter) { 
383     is >> c; 
384     if (is.good() && c!=d) {
385       is.setstate(std::ios::failbit);
386     } 
387   }
388   return is;
389 }
390
391
392 // Note: The order of the read functions is critical to let a 
393 // (conforming?) compiler find and select the correct one.
394
395 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
396 template<class T1>
397 inline  std::istream & 
398 read (std::istream &is, cons<T1, null_type>& t1) {
399
400   if (!is.good()) return is;   
401    
402   return is >> t1.head ;
403 }
404 #else
405 inline std::istream& read(std::istream& i, const null_type&) { return i; }
406 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
407    
408 template<class T1, class T2>
409 inline std::istream& 
410 read(std::istream &is, cons<T1, T2>& t1) {
411
412   if (!is.good()) return is;
413    
414   is >> t1.head;
415
416 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
417   if (tuples::length<T2>::value == 0)
418     return is;
419 #endif  // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
420
421   extract_and_check_delimiter(is, format_info::delimiter);
422
423   return read(is, t1.tail);
424 }
425
426 } // end namespace detail
427
428 inline std::istream& 
429 operator>>(std::istream &is, null_type&) {
430
431   if (!is.good() ) return is;
432
433   detail::extract_and_check_delimiter(is, detail::format_info::open);
434   detail::extract_and_check_delimiter(is, detail::format_info::close);
435
436   return is;
437 }
438
439
440 template<class T1, class T2>
441 inline std::istream& 
442 operator>>(std::istream& is, cons<T1, T2>& t1) {
443
444   if (!is.good() ) return is;
445
446   detail::extract_and_check_delimiter(is, detail::format_info::open);
447                       
448   detail::read(is, t1);
449    
450   detail::extract_and_check_delimiter(is, detail::format_info::close);
451
452   return is;
453 }
454
455
456
457 #else
458
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)
463 {
464   const CharType d = format_info::get_manipulator(is, del);
465
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);
471 #else
472   const bool is_delimiter = (!std::isspace(d, is.getloc()) );            
473 #endif
474
475   CharType c;
476   if (is_delimiter) { 
477     is >> c;
478     if (is.good() && c!=d) { 
479       is.setstate(std::ios::failbit);
480     }
481   }
482   return is;
483 }
484
485    
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) {
490
491   if (!is.good()) return is;   
492    
493   return is >> t1.head; 
494 }
495 #else
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; }
499
500 #endif // !BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
501
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) {
505
506   if (!is.good()) return is;
507    
508   is >> t1.head;
509
510 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
511   if (tuples::length<T2>::value == 0)
512     return is;
513 #endif  // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
514
515   extract_and_check_delimiter(is, format_info::delimiter);
516
517   return read(is, t1.tail);
518 }
519
520 } // end namespace detail
521
522
523 template<class CharType, class CharTrait>
524 inline std::basic_istream<CharType, CharTrait>& 
525 operator>>(std::basic_istream<CharType, CharTrait> &is, null_type&) {
526
527   if (!is.good() ) return is;
528
529   detail::extract_and_check_delimiter(is, detail::format_info::open);
530   detail::extract_and_check_delimiter(is, detail::format_info::close);
531
532   return is;
533 }
534
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) {
538
539   if (!is.good() ) return is;
540
541   detail::extract_and_check_delimiter(is, detail::format_info::open);
542                       
543   detail::read(is, t1);
544    
545   detail::extract_and_check_delimiter(is, detail::format_info::close);
546
547   return is;
548 }
549
550 #endif // BOOST_NO_TEMPLATED_STREAMS
551
552 } // end of namespace tuples
553 } // end of namespace boost
554
555 #endif // BOOST_TUPLE_IO_HPP
556
557