ostream_iterator.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <iterator>
  11. ///////////////////////////////////////////////////////////////////////////////
  12. namespace boost { namespace spirit { namespace karma
  13. {
  14. ///////////////////////////////////////////////////////////////////////////
  15. // We need our own implementation of an ostream_iterator just to be able
  16. // to access the wrapped ostream, which is necessary for the
  17. // stream_generator, where we must generate the output using the original
  18. // ostream to retain possibly registered facets.
  19. ///////////////////////////////////////////////////////////////////////////
  20. template <
  21. typename T, typename Elem = char
  22. , typename Traits = std::char_traits<Elem> >
  23. class ostream_iterator
  24. {
  25. public:
  26. typedef std::output_iterator_tag iterator_category;
  27. typedef void value_type;
  28. typedef void difference_type;
  29. typedef void pointer;
  30. typedef void reference;
  31. typedef Elem char_type;
  32. typedef Traits traits_type;
  33. typedef std::basic_ostream<Elem, Traits> ostream_type;
  34. typedef ostream_iterator<T, Elem, Traits> self_type;
  35. ostream_iterator(ostream_type& os_, Elem const* delim_ = 0)
  36. : os(&os_), delim(delim_) {}
  37. self_type& operator= (T const& val)
  38. {
  39. *os << val;
  40. if (0 != delim)
  41. *os << delim;
  42. return *this;
  43. }
  44. self_type& operator*() { return *this; }
  45. self_type& operator++() { return *this; }
  46. self_type operator++(int) { return *this; }
  47. // expose underlying stream
  48. ostream_type& get_ostream() { return *os; }
  49. ostream_type const& get_ostream() const { return *os; }
  50. // expose good bit of underlying stream object
  51. bool good() const { return get_ostream().good(); }
  52. protected:
  53. ostream_type *os;
  54. Elem const* delim;
  55. };
  56. }}}
  57. #endif