• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[section:function_output Function Output Iterator]
2
3The function output iterator adaptor makes it easier to create custom
4output iterators. The adaptor takes a unary function and creates a
5model of Output Iterator. Each item assigned to the output iterator is
6passed as an argument to the unary function.  The motivation for this
7iterator is that creating a conforming output iterator is non-trivial,
8particularly because the proper implementation usually requires a
9proxy object.
10
11[h2 Example]
12
13    struct string_appender
14    {
15        string_appender(std::string& s)
16            : m_str(&s)
17        {}
18
19        void operator()(const std::string& x) const
20        {
21            *m_str += x;
22        }
23
24        std::string* m_str;
25    };
26
27    int main(int, char*[])
28    {
29      std::vector<std::string> x;
30      x.push_back("hello");
31      x.push_back(" ");
32      x.push_back("world");
33      x.push_back("!");
34
35      std::string s = "";
36      std::copy(x.begin(), x.end(),
37          boost::make_function_output_iterator(string_appender(s)));
38
39      std::cout << s << std::endl;
40
41      return 0;
42    }
43
44[h2 Reference]
45
46[h3 Synopsis]
47
48  template <class UnaryFunction>
49  class function_output_iterator {
50  public:
51    typedef std::output_iterator_tag iterator_category;
52    typedef void                     value_type;
53    typedef void                     difference_type;
54    typedef void                     pointer;
55    typedef void                     reference;
56
57    explicit function_output_iterator();
58
59    explicit function_output_iterator(const UnaryFunction& f);
60
61    /* see below */ operator*();
62    function_output_iterator& operator++();
63    function_output_iterator& operator++(int);
64  private:
65    UnaryFunction m_f;     // exposition only
66  };
67
68[h3 Requirements]
69
70`UnaryFunction` must be Assignable and Copy Constructible.
71
72[h3 Concepts]
73
74`function_output_iterator` is a model of the Writable and
75Incrementable Iterator concepts.
76
77[h3 Operations]
78
79  explicit function_output_iterator(const UnaryFunction& f = UnaryFunction());
80
81[*Effects: ] Constructs an instance of `function_output_iterator`
82  with `m_f` constructed from `f`.
83
84  unspecified_type operator*();
85
86[*Returns: ] An object `r` of unspecified type such that `r = t`
87  is equivalent to `m_f(t)` for all `t`.
88
89
90  function_output_iterator& operator++();
91
92[*Returns: ] `*this`.
93
94
95  function_output_iterator& operator++(int);
96
97[*Returns: ] `*this`.
98
99[endsect]
100