1 // Copyright (C) 2005, Fernando Luis Cacciola Carballal.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/optional for documentation.
8 //
9 // You are welcome to contact the author at:
10 //  fernando_cacciola@hotmail.com
11 //
12 #ifndef BOOST_OPTIONAL_OPTIONAL_IO_FLC_19NOV2002_HPP
13 #define BOOST_OPTIONAL_OPTIONAL_IO_FLC_19NOV2002_HPP
14 
15 #include <istream>
16 #include <ostream>
17 
18 #include "boost/none.hpp"
19 #include "boost/optional/optional.hpp"
20 
21 
22 namespace boost
23 {
24 
25 template<class CharType, class CharTrait>
26 inline
27 std::basic_ostream<CharType, CharTrait>&
operator <<(std::basic_ostream<CharType,CharTrait> & out,none_t)28 operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t)
29 {
30   if (out.good())
31   {
32     out << "--";
33   }
34 
35   return out;
36 }
37 
38 template<class CharType, class CharTrait, class T>
39 inline
40 std::basic_ostream<CharType, CharTrait>&
operator <<(std::basic_ostream<CharType,CharTrait> & out,optional<T> const & v)41 operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v)
42 {
43   if (out.good())
44   {
45     if (!v)
46          out << "--" ;
47     else out << ' ' << *v ;
48   }
49 
50   return out;
51 }
52 
53 template<class CharType, class CharTrait, class T>
54 inline
55 std::basic_istream<CharType, CharTrait>&
operator >>(std::basic_istream<CharType,CharTrait> & in,optional<T> & v)56 operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v)
57 {
58   if (in.good())
59   {
60     int d = in.get();
61     if (d == ' ')
62     {
63       T x;
64       in >> x;
65 #ifndef  BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
66       v = boost::move(x);
67 #else
68       v = x;
69 #endif
70     }
71     else
72     {
73       if (d == '-')
74       {
75         d = in.get();
76 
77         if (d == '-')
78         {
79           v = none;
80           return in;
81         }
82       }
83 
84       in.setstate( std::ios::failbit );
85     }
86   }
87 
88   return in;
89 }
90 
91 } // namespace boost
92 
93 #endif
94 
95