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

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

implement OstreamIterator? refs #770

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1#ifndef _theplu_yat_utility_ostream_iterator_
2#define _theplu_yat_utility_ostream_iterator_
3
4// $Id: OstreamIterator.h 3151 2013-12-23 06:04:51Z 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 <iterator>
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     \since New in yat 0.12
76  */
77  template<typename T>
78  class OstreamIterator :
79    public boost::function_output_iterator<detail::OstreamIteratorFunc<T> >
80  {
81  public:
82    /**
83       \brief Default constructor
84
85       Creates an iterator that is not dereferencable.
86     */
87    OstreamIterator(void) {}
88
89    /**
90       \brief Constructor
91       \param os istream to extract string elements from.
92       \param delimiter the delimiting character.
93    */
94    explicit OstreamIterator(std::ostream& os, const std::string& delimiter="")
95      : boost::function_output_iterator<detail::OstreamIteratorFunc<T> >(detail::OstreamIteratorFunc<T>(os, delimiter)) {}
96
97  private:
98    // Using compiler generated copy
99    //OstreamIterator(const OstreamIterator&);
100    //OstreamIterator& operator=(const OstreamIterator&);
101  };
102
103}}} // of namespace utility, yat, and theplu
104
105#endif
Note: See TracBrowser for help on using the repository browser.