ostream_iterator.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #if !defined(BOOST_SPIRIT_KARMA_OSTREAM_ITERATOR_MAY_26_2007_1016PM)
  6. #define BOOST_SPIRIT_KARMA_OSTREAM_ITERATOR_MAY_26_2007_1016PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <iosfwd>
  11. #include <iterator>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace spirit { namespace karma
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. // We need our own implementation of an ostream_iterator just to be able
  17. // to access the wrapped ostream, which is necessary for the
  18. // stream_generator, where we must generate the output using the original
  19. // ostream to retain possibly registered facets.
  20. ///////////////////////////////////////////////////////////////////////////
  21. template <
  22. typename T, typename Elem = char
  23. , typename Traits = std::char_traits<Elem> >
  24. class ostream_iterator
  25. {
  26. public:
  27. typedef std::output_iterator_tag iterator_category;
  28. typedef void value_type;
  29. typedef void difference_type;
  30. typedef void pointer;
  31. typedef void reference;
  32. typedef Elem char_type;
  33. typedef Traits traits_type;
  34. typedef std::basic_ostream<Elem, Traits> ostream_type;
  35. typedef ostream_iterator<T, Elem, Traits> self_type;
  36. ostream_iterator(ostream_type& os_, Elem const* delim_ = 0)
  37. : os(&os_), delim(delim_) {}
  38. self_type& operator= (T const& val)
  39. {
  40. *os << val;
  41. if (0 != delim)
  42. *os << delim;
  43. return *this;
  44. }
  45. self_type& operator*() { return *this; }
  46. self_type& operator++() { return *this; }
  47. self_type operator++(int) { return *this; }
  48. // expose underlying stream
  49. ostream_type& get_ostream() { return *os; }
  50. ostream_type const& get_ostream() const { return *os; }
  51. // expose good bit of underlying stream object
  52. bool good() const { return get_ostream().good(); }
  53. protected:
  54. ostream_type *os;
  55. Elem const* delim;
  56. };
  57. }}}
  58. #endif