yat  0.21pre
OstreamIterator.h
1 #ifndef _theplu_yat_utility_ostream_iterator_
2 #define _theplu_yat_utility_ostream_iterator_
3 
4 // $Id: OstreamIterator.h 4252 2022-11-18 02:54:04Z peter $
5 
6 /*
7  Copyright (C) 2013, 2014 Peter Johansson
8  Copyright (C) 2022 Jari Häkkinen
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include "yat_assert.h"
27 
28 #include <boost/iterator/function_output_iterator.hpp>
29 
30 #include <functional>
31 #include <ostream>
32 #include <string>
33 
34 namespace theplu {
35 namespace yat {
36 namespace utility {
37 
38  // private namespace
39  namespace detail {
40 
42  template<typename T>
43  class OstreamIteratorFunc : public std::unary_function<const T&, void>
44  {
45  public:
47  OstreamIteratorFunc(void) : ostream_(NULL), pristine_(true) {}
49  explicit OstreamIteratorFunc(std::ostream& os,
50  const std::string& delimiter)
51  : delimiter_(delimiter), ostream_(&os), pristine_(true) {}
52 
54  void operator()(const T& t)
55  {
56  YAT_ASSERT(ostream_);
57  if (pristine_)
58  pristine_ = false;
59  else
60  *ostream_ << delimiter_;
61  *ostream_ << t;
62  }
63 
64  private:
65  std::string delimiter_;
66  std::ostream* ostream_;
67  bool pristine_;
68  // using compiler generated copy
69  // Func(const Func&);
70  // Func& operator=(const Func&);
71  };
72  } // end of namespace detail
73 
74 
96  template<typename T>
98  public boost::function_output_iterator<detail::OstreamIteratorFunc<T> >
99  {
100  public:
107 
121  explicit OstreamIterator(std::ostream& os, const std::string& delimiter="")
122  : boost::function_output_iterator<detail::OstreamIteratorFunc<T> >(detail::OstreamIteratorFunc<T>(os, delimiter)) {}
123 
124  private:
125  // Using compiler generated copy
126  //OstreamIterator(const OstreamIterator&);
127  //OstreamIterator& operator=(const OstreamIterator&);
128  };
129 
130 }}} // of namespace utility, yat, and theplu
131 
132 #endif
OstreamIterator(void)
Default constructor.
Definition: OstreamIterator.h:106
The Department of Theoretical Physics namespace as we define it.
Definition: OstreamIterator.h:97
void operator()(const T &t)
writes to ostream using operator <<
Definition: OstreamIterator.h:54
OstreamIteratorFunc(std::ostream &os, const std::string &delimiter)
constructor
Definition: OstreamIterator.h:49
OstreamIteratorFunc(void)
default constructor
Definition: OstreamIterator.h:47
OstreamIterator(std::ostream &os, const std::string &delimiter="")
Constructor.
Definition: OstreamIterator.h:121

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14