yat  0.13.2pre
OstreamIterator.h
1 #ifndef _theplu_yat_utility_ostream_iterator_
2 #define _theplu_yat_utility_ostream_iterator_
3 
4 // $Id: OstreamIterator.h 3154 2014-01-03 06:40:04Z peter $
5 
6 /*
7  Copyright (C) 2013, 2014 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 
33 namespace theplu {
34 namespace yat {
35 namespace utility {
36 
37  // private namespace
38  namespace detail {
39 
41  template<typename T>
42  class OstreamIteratorFunc : public std::unary_function<const T&, void>
43  {
44  public:
46  OstreamIteratorFunc(void) : ostream_(NULL), pristine_(true) {}
48  explicit OstreamIteratorFunc(std::ostream& os,
49  const std::string& delimiter)
50  : delimiter_(delimiter), ostream_(&os), pristine_(true) {}
51 
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 
95  template<typename T>
97  public boost::function_output_iterator<detail::OstreamIteratorFunc<T> >
98  {
99  public:
106 
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
OstreamIterator(void)
Default constructor.
Definition: OstreamIterator.h:105
Definition: OstreamIterator.h:96
void operator()(const T &t)
writes to ostream using operator &lt;&lt;
Definition: OstreamIterator.h:53
OstreamIteratorFunc(std::ostream &os, const std::string &delimiter)
constructor
Definition: OstreamIterator.h:48
OstreamIteratorFunc(void)
default constructor
Definition: OstreamIterator.h:46
OstreamIterator(std::ostream &os, const std::string &delimiter="")
Constructor.
Definition: OstreamIterator.h:120

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5