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