3151 |
23 Dec 13 |
peter |
1 |
#ifndef _theplu_yat_utility_ostream_iterator_ |
3151 |
23 Dec 13 |
peter |
2 |
#define _theplu_yat_utility_ostream_iterator_ |
3151 |
23 Dec 13 |
peter |
3 |
|
3151 |
23 Dec 13 |
peter |
// $Id$ |
3151 |
23 Dec 13 |
peter |
5 |
|
3151 |
23 Dec 13 |
peter |
6 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2013, 2014, 2022 Peter Johansson |
3151 |
23 Dec 13 |
peter |
8 |
|
3151 |
23 Dec 13 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3151 |
23 Dec 13 |
peter |
10 |
|
3151 |
23 Dec 13 |
peter |
The yat library is free software; you can redistribute it and/or |
3151 |
23 Dec 13 |
peter |
modify it under the terms of the GNU General Public License as |
3151 |
23 Dec 13 |
peter |
published by the Free Software Foundation; either version 3 of the |
3151 |
23 Dec 13 |
peter |
License, or (at your option) any later version. |
3151 |
23 Dec 13 |
peter |
15 |
|
3151 |
23 Dec 13 |
peter |
The yat library is distributed in the hope that it will be useful, |
3151 |
23 Dec 13 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3151 |
23 Dec 13 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3151 |
23 Dec 13 |
peter |
General Public License for more details. |
3151 |
23 Dec 13 |
peter |
20 |
|
3151 |
23 Dec 13 |
peter |
You should have received a copy of the GNU General Public License |
3151 |
23 Dec 13 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3151 |
23 Dec 13 |
peter |
23 |
*/ |
3151 |
23 Dec 13 |
peter |
24 |
|
3151 |
23 Dec 13 |
peter |
25 |
#include "yat_assert.h" |
3151 |
23 Dec 13 |
peter |
26 |
|
4252 |
18 Nov 22 |
peter |
27 |
#include <boost/iterator/function_output_iterator.hpp> |
3151 |
23 Dec 13 |
peter |
28 |
|
3153 |
03 Jan 14 |
peter |
29 |
#include <functional> |
3151 |
23 Dec 13 |
peter |
30 |
#include <ostream> |
3151 |
23 Dec 13 |
peter |
31 |
#include <string> |
3151 |
23 Dec 13 |
peter |
32 |
|
3151 |
23 Dec 13 |
peter |
33 |
namespace theplu { |
3151 |
23 Dec 13 |
peter |
34 |
namespace yat { |
3151 |
23 Dec 13 |
peter |
35 |
namespace utility { |
3151 |
23 Dec 13 |
peter |
36 |
|
3151 |
23 Dec 13 |
peter |
// private namespace |
3151 |
23 Dec 13 |
peter |
38 |
namespace detail { |
3151 |
23 Dec 13 |
peter |
39 |
|
3151 |
23 Dec 13 |
peter |
/// \internal class used in OstreamIterator |
3151 |
23 Dec 13 |
peter |
41 |
template<typename T> |
4339 |
15 Apr 23 |
peter |
42 |
class OstreamIteratorFunc |
3151 |
23 Dec 13 |
peter |
43 |
{ |
3151 |
23 Dec 13 |
peter |
44 |
public: |
3151 |
23 Dec 13 |
peter |
/// default constructor |
3151 |
23 Dec 13 |
peter |
46 |
OstreamIteratorFunc(void) : ostream_(NULL), pristine_(true) {} |
3151 |
23 Dec 13 |
peter |
/// constructor |
3151 |
23 Dec 13 |
peter |
48 |
explicit OstreamIteratorFunc(std::ostream& os, |
3151 |
23 Dec 13 |
peter |
49 |
const std::string& delimiter) |
3151 |
23 Dec 13 |
peter |
50 |
: delimiter_(delimiter), ostream_(&os), pristine_(true) {} |
3151 |
23 Dec 13 |
peter |
51 |
|
3151 |
23 Dec 13 |
peter |
/// writes to ostream using operator << |
3151 |
23 Dec 13 |
peter |
53 |
void operator()(const T& t) |
3151 |
23 Dec 13 |
peter |
54 |
{ |
3151 |
23 Dec 13 |
peter |
55 |
YAT_ASSERT(ostream_); |
3151 |
23 Dec 13 |
peter |
56 |
if (pristine_) |
3151 |
23 Dec 13 |
peter |
57 |
pristine_ = false; |
3151 |
23 Dec 13 |
peter |
58 |
else |
3151 |
23 Dec 13 |
peter |
59 |
*ostream_ << delimiter_; |
3151 |
23 Dec 13 |
peter |
60 |
*ostream_ << t; |
3151 |
23 Dec 13 |
peter |
61 |
} |
3151 |
23 Dec 13 |
peter |
62 |
|
3151 |
23 Dec 13 |
peter |
63 |
private: |
3151 |
23 Dec 13 |
peter |
64 |
std::string delimiter_; |
3151 |
23 Dec 13 |
peter |
65 |
std::ostream* ostream_; |
3151 |
23 Dec 13 |
peter |
66 |
bool pristine_; |
3151 |
23 Dec 13 |
peter |
// using compiler generated copy |
3151 |
23 Dec 13 |
peter |
// Func(const Func&); |
3151 |
23 Dec 13 |
peter |
// Func& operator=(const Func&); |
3151 |
23 Dec 13 |
peter |
70 |
}; |
3151 |
23 Dec 13 |
peter |
71 |
} // end of namespace detail |
3151 |
23 Dec 13 |
peter |
72 |
|
3151 |
23 Dec 13 |
peter |
73 |
|
3151 |
23 Dec 13 |
peter |
74 |
/** |
3153 |
03 Jan 14 |
peter |
OstreamIterator is an \output_iterator. Just like <a |
3153 |
03 Jan 14 |
peter |
href="http://www.sgi.com/tech/stl/ostream_iterator.html"> |
3153 |
03 Jan 14 |
peter |
std::ostream_iterator</a> it uses \c operator<<(ostream&, T) to |
3153 |
03 Jan 14 |
peter |
send formatted output to a particular ostream. While <a |
3153 |
03 Jan 14 |
peter |
href="http://www.sgi.com/tech/stl/ostream_iterator.html"> |
3153 |
03 Jan 14 |
peter |
std::ostream_iterator</a> inserts a string \a delimited after |
3153 |
03 Jan 14 |
peter |
each element, OstreamIterator only inserts \a delimiter between |
3153 |
03 Jan 14 |
peter |
elements. |
3153 |
03 Jan 14 |
peter |
83 |
|
3153 |
03 Jan 14 |
peter |
\code |
3153 |
03 Jan 14 |
peter |
vector<int> x(3); |
3153 |
03 Jan 14 |
peter |
x[0] = 2; |
3153 |
03 Jan 14 |
peter |
x[1] = 3; |
3153 |
03 Jan 14 |
peter |
x[2] = 5; |
3153 |
03 Jan 14 |
peter |
copy(x.begin(), x.end(), OstreamIterator<int>(cout, ", ")); |
3153 |
03 Jan 14 |
peter |
\endcode |
3153 |
03 Jan 14 |
peter |
which will give output: 2, 3, 5 |
3153 |
03 Jan 14 |
peter |
92 |
|
3151 |
23 Dec 13 |
peter |
\since New in yat 0.12 |
3151 |
23 Dec 13 |
peter |
94 |
*/ |
3151 |
23 Dec 13 |
peter |
95 |
template<typename T> |
3151 |
23 Dec 13 |
peter |
96 |
class OstreamIterator : |
3151 |
23 Dec 13 |
peter |
97 |
public boost::function_output_iterator<detail::OstreamIteratorFunc<T> > |
3151 |
23 Dec 13 |
peter |
98 |
{ |
3151 |
23 Dec 13 |
peter |
99 |
public: |
3151 |
23 Dec 13 |
peter |
100 |
/** |
3151 |
23 Dec 13 |
peter |
\brief Default constructor |
3151 |
23 Dec 13 |
peter |
102 |
|
3151 |
23 Dec 13 |
peter |
Creates an iterator that is not dereferencable. |
3151 |
23 Dec 13 |
peter |
104 |
*/ |
3151 |
23 Dec 13 |
peter |
105 |
OstreamIterator(void) {} |
3151 |
23 Dec 13 |
peter |
106 |
|
3151 |
23 Dec 13 |
peter |
107 |
/** |
3151 |
23 Dec 13 |
peter |
\brief Constructor |
3153 |
03 Jan 14 |
peter |
109 |
|
3153 |
03 Jan 14 |
peter |
Creates an OstreamIterator such that assignment of `t` through |
3153 |
03 Jan 14 |
peter |
it is equivalent to os << delimiter << t just like |
3153 |
03 Jan 14 |
peter |
std::ostream_iterator. The exception is that first time |
3153 |
03 Jan 14 |
peter |
OstreamIterator is derefenced the delimiter is excluded in |
3153 |
03 Jan 14 |
peter |
order to get the desired behaviour that delimiter only occurs |
3153 |
03 Jan 14 |
peter |
between elements. |
3153 |
03 Jan 14 |
peter |
116 |
|
3153 |
03 Jan 14 |
peter |
\param os ostream to send output |
3153 |
03 Jan 14 |
peter |
\param delimiter the delimiting string |
3151 |
23 Dec 13 |
peter |
119 |
*/ |
3151 |
23 Dec 13 |
peter |
120 |
explicit OstreamIterator(std::ostream& os, const std::string& delimiter="") |
3151 |
23 Dec 13 |
peter |
121 |
: boost::function_output_iterator<detail::OstreamIteratorFunc<T> >(detail::OstreamIteratorFunc<T>(os, delimiter)) {} |
3151 |
23 Dec 13 |
peter |
122 |
|
3151 |
23 Dec 13 |
peter |
123 |
private: |
3151 |
23 Dec 13 |
peter |
// Using compiler generated copy |
3151 |
23 Dec 13 |
peter |
//OstreamIterator(const OstreamIterator&); |
3151 |
23 Dec 13 |
peter |
//OstreamIterator& operator=(const OstreamIterator&); |
3151 |
23 Dec 13 |
peter |
127 |
}; |
3151 |
23 Dec 13 |
peter |
128 |
|
3151 |
23 Dec 13 |
peter |
129 |
}}} // of namespace utility, yat, and theplu |
3151 |
23 Dec 13 |
peter |
130 |
|
3151 |
23 Dec 13 |
peter |
131 |
#endif |