source: trunk/yat/utility/OstreamIterator.h @ 3153

Last change on this file since 3153 was 3153, checked in by Peter, 8 years ago

document OstreamIterator? closes #770

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1#ifndef _theplu_yat_utility_ostream_iterator_
2#define _theplu_yat_utility_ostream_iterator_
3
4// $Id: OstreamIterator.h 3153 2014-01-03 06:39:09Z peter $
5
6/*
7  Copyright (C) 2013 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "yat_assert.h"
26
27#include <boost/function_output_iterator.hpp>
28
29#include <functional>
30#include <ostream>
31#include <string>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  // private namespace
38  namespace detail {
39
40    /// \internal class used in OstreamIterator
41    template<typename T>
42    class OstreamIteratorFunc : public std::unary_function<const T&, void>
43    {
44    public:
45      /// default constructor
46      OstreamIteratorFunc(void) : ostream_(NULL), pristine_(true) {}
47      /// constructor
48      explicit OstreamIteratorFunc(std::ostream& os,
49                                   const std::string& delimiter)
50        : delimiter_(delimiter), ostream_(&os), pristine_(true) {}
51
52      /// writes to ostream using operator <<
53      void operator()(const T& t)
54      {
55        YAT_ASSERT(ostream_);
56        if (pristine_)
57          pristine_ = false;
58        else
59          *ostream_ << delimiter_;
60        *ostream_ << t;
61      }
62
63    private:
64      std::string delimiter_;
65      std::ostream* ostream_;
66      bool pristine_;
67      // using compiler generated copy
68      // Func(const Func&);
69      // Func& operator=(const Func&);
70    };
71  } // end of namespace detail
72
73
74  /**
75     OstreamIterator is an \output_iterator. Just like <a
76     href="http://www.sgi.com/tech/stl/ostream_iterator.html">
77     std::ostream_iterator</a> it uses \c operator<<(ostream&, T) to
78     send formatted output to a particular ostream. While <a
79     href="http://www.sgi.com/tech/stl/ostream_iterator.html">
80     std::ostream_iterator</a> inserts a string \a delimited after
81     each element, OstreamIterator only inserts \a delimiter between
82     elements.
83
84     \code
85     vector<int> x(3);
86     x[0] = 2;
87     x[1] = 3;
88     x[2] = 5;
89     copy(x.begin(), x.end(), OstreamIterator<int>(cout, ", "));
90     \endcode
91     which will give output: 2, 3, 5
92
93     \since New in yat 0.12
94  */
95  template<typename T>
96  class OstreamIterator :
97    public boost::function_output_iterator<detail::OstreamIteratorFunc<T> >
98  {
99  public:
100    /**
101       \brief Default constructor
102
103       Creates an iterator that is not dereferencable.
104     */
105    OstreamIterator(void) {}
106
107    /**
108       \brief Constructor
109
110       Creates an OstreamIterator such that assignment of `t` through
111       it is equivalent to os << delimiter << t just like
112       std::ostream_iterator. The exception is that first time
113       OstreamIterator is derefenced the delimiter is excluded in
114       order to get the desired behaviour that delimiter only occurs
115       between elements.
116
117       \param os ostream to send output
118       \param delimiter the delimiting string
119    */
120    explicit OstreamIterator(std::ostream& os, const std::string& delimiter="")
121      : boost::function_output_iterator<detail::OstreamIteratorFunc<T> >(detail::OstreamIteratorFunc<T>(os, delimiter)) {}
122
123  private:
124    // Using compiler generated copy
125    //OstreamIterator(const OstreamIterator&);
126    //OstreamIterator& operator=(const OstreamIterator&);
127  };
128
129}}} // of namespace utility, yat, and theplu
130
131#endif
Note: See TracBrowser for help on using the repository browser.