optional_io.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/lib/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. #if defined __GNUC__
  15. # if (__GNUC__ == 2 && __GNUC_MINOR__ <= 97)
  16. # define BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
  17. # endif
  18. #endif // __GNUC__
  19. #if defined BOOST_OPTIONAL_NO_TEMPLATED_STREAMS
  20. # include <iostream>
  21. #else
  22. # include <istream>
  23. # include <ostream>
  24. #endif
  25. #include "boost/optional/optional.hpp"
  26. #include "boost/utility/value_init.hpp"
  27. namespace boost
  28. {
  29. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  30. template<class T>
  31. inline std::ostream& operator<<(std::ostream& out, optional<T> const& v)
  32. #else
  33. template<class CharType, class CharTrait, class T>
  34. inline
  35. std::basic_ostream<CharType, CharTrait>&
  36. operator<<(std::basic_ostream<CharType, CharTrait>& out, optional<T> const& v)
  37. #endif
  38. {
  39. if ( out.good() )
  40. {
  41. if ( !v )
  42. out << "--" ;
  43. else out << ' ' << *v ;
  44. }
  45. return out;
  46. }
  47. #if defined (BOOST_NO_TEMPLATED_STREAMS)
  48. template<class T>
  49. inline std::istream& operator>>(std::istream& in, optional<T>& v)
  50. #else
  51. template<class CharType, class CharTrait, class T>
  52. inline
  53. std::basic_istream<CharType, CharTrait>&
  54. operator>>(std::basic_istream<CharType, CharTrait>& in, optional<T>& v)
  55. #endif
  56. {
  57. if ( in.good() )
  58. {
  59. int d = in.get();
  60. if ( d == ' ' )
  61. {
  62. T x ;
  63. in >> x;
  64. v = x ;
  65. }
  66. else
  67. v = optional<T>() ;
  68. }
  69. return in;
  70. }
  71. } // namespace boost
  72. #endif