| 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 | #ifndef BOOST_NO_IOSTREAM |
| 16 | #include <istream> |
| 17 | #include <ostream> |
| 18 | |
| 19 | #include "boost/none.hpp" |
| 20 | #include "boost/optional/optional.hpp" |
| 21 | |
| 22 | |
| 23 | namespace boost |
| 24 | { |
| 25 | |
| 26 | template<class CharType, class CharTrait> |
| 27 | inline |
| 28 | std::basic_ostream<CharType, CharTrait>& |
| 29 | operator<<(std::basic_ostream<CharType, CharTrait>& out, none_t) |
| 30 | { |
| 31 | if (out.good()) |
| 32 | { |
| 33 | out << "--" ; |
| 34 | } |
| 35 | |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | template<class CharType, class CharTrait, class T> |
| 40 | inline |
| 41 | std::basic_ostream<CharType, CharTrait>& |
| 42 | operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v) |
| 43 | { |
| 44 | if (out.good()) |
| 45 | { |
| 46 | if (!v) |
| 47 | out << "--" ; |
| 48 | else out << ' ' << *v ; |
| 49 | } |
| 50 | |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | template<class CharType, class CharTrait, class T> |
| 55 | inline |
| 56 | std::basic_istream<CharType, CharTrait>& |
| 57 | operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v) |
| 58 | { |
| 59 | if (in.good()) |
| 60 | { |
| 61 | int d = in.get(); |
| 62 | if (d == ' ') |
| 63 | { |
| 64 | T x; |
| 65 | in >> x; |
| 66 | #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES |
| 67 | v = boost::move(x); |
| 68 | #else |
| 69 | v = x; |
| 70 | #endif |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | if (d == '-') |
| 75 | { |
| 76 | d = in.get(); |
| 77 | |
| 78 | if (d == '-') |
| 79 | { |
| 80 | v = none; |
| 81 | return in; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | in.setstate( std::ios::failbit ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return in; |
| 90 | } |
| 91 | |
| 92 | } // namespace boost |
| 93 | |
| 94 | #endif // BOOST_NO_IOSTREAM |
| 95 | #endif |
| 96 | |