From 27dfb2557079fa9f13f7c0bafe34a6b306e264da Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Tue, 27 Feb 2001 05:50:51 +0000 Subject: [PATCH] added function output iterator adaptor [SVN r9351] --- fun_out_iter_example.cpp | 41 +++++++++ function_output_iterator.htm | 169 +++++++++++++++++++++++++++++++++++ iterator_adaptors.htm | 10 ++- 3 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 fun_out_iter_example.cpp create mode 100644 function_output_iterator.htm diff --git a/fun_out_iter_example.cpp b/fun_out_iter_example.cpp new file mode 100644 index 0000000..fbd22f9 --- /dev/null +++ b/fun_out_iter_example.cpp @@ -0,0 +1,41 @@ +// (C) Copyright Jeremy Siek 2001. Permission to copy, use, modify, +// sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. + +// Revision History: + +// 27 Feb 2001 Jeremy Siek +// Initial checkin. + +#include +#include +#include + +#include + +struct string_appender { + string_appender(std::string& s) : m_str(s) { } + void operator()(const std::string& x) const { + m_str += x; + } + std::string& m_str; +}; + +int main(int, char*[]) +{ + std::vector x; + x.push_back("hello"); + x.push_back(" "); + x.push_back("world"); + x.push_back("!"); + + std::string s = ""; + std::copy(x.begin(), x.end(), + boost::make_function_output_iterator(string_appender(s))); + + std::cout << s << std::endl; + + return 0; +} diff --git a/function_output_iterator.htm b/function_output_iterator.htm new file mode 100644 index 0000000..6061a7b --- /dev/null +++ b/function_output_iterator.htm @@ -0,0 +1,169 @@ + + + + + + + + + + Function Output Iterator Adaptor Documentation + + + + + c++boost.gif (8819 bytes) + +

Function Output Iterator Adaptor

+ Defined in header boost/function_output_iterator.hpp + +

The function output iterator adaptor makes it easier to create + custom output iterators. The adaptor takes a Unary + Function and creates a model of Output + Iterator. Each item assigned to the output iterator is passed + as an argument to the unary function. The motivation for this + iterator is that creating a C++ Standard conforming output + iterator is non-trivial, particularly because the proper + implementation usually requires a proxy object. On the other hand, + creating a function (or function object) is much simpler. + +

Synopsis

+ +
+
+namespace boost {
+  template <class UnaryFunction>
+  class function_output_iterator;
+
+  template <class UnaryFunction>
+  function_output_iterator<UnaryFunction>
+  make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
+}
+
+
+ +

Example

+ + In this example we create an output iterator that appends + each item onto the end of a string, using the string_appender + function. + +
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <boost/function_output_iterator.hpp>
+
+struct string_appender {
+  string_appender(std::string& s) : m_str(s) { }
+  void operator()(const std::string& x) const {
+    m_str += x;
+  }
+  std::string& m_str;
+};
+
+int main(int, char*[])
+{
+  std::vector<std::string> x;
+  x.push_back("hello");
+  x.push_back(" ");
+  x.push_back("world");
+  x.push_back("!");
+
+  std::string s = "";
+  std::copy(x.begin(), x.end(), 
+            boost::make_function_output_iterator(string_appender(s)));
+  
+  std::cout << s << std::endl;
+
+  return 0;
+}
+
+
+ +
+ +

The Function Output Iterator Class

+ +
+
+template <class UnaryFunction>
+class function_output_iterator;
+
+
+ + The function_output_iterator class creates an Output + Iterator out of a + Unary + Function. Each item assigned to the output iterator is passed + as an argument to the unary function. + +

Template Parameters

+ + + + + +
Parameter + + Description + +
UnaryFunction + + The function type being wrapped. The return type of the + function is not used, so it can be void. The + function must be a model of Unary + Function.
+ +

Concept Model

+ The function output iterator class is a model of Output + Iterator. + +

Members

+ The function output iterator implements the member functions + and operators required of the Output + Iterator concept. In addition it has the following constructor: +
+explicit function_output_iterator(const UnaryFunction& f = UnaryFunction())
+
+
+
+ +
+

The Function Output Iterator Object + Generator

+ + The make_function_output_iterator() function provides a + more convenient way to create function output iterator objects. The + function saves the user the trouble of explicitly writing out the + iterator types. If the default argument is used, the function + type must be provided as an explicit template argument. + +
+
+template <class UnaryFunction>
+function_output_iterator<UnaryFunction>
+make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
+
+
+ +
+ +

© Copyright Jeremy Siek 2001. Permission to copy, use, + modify, sell and distribute this document is granted provided this + copyright notice appears in all copies. This document is provided + "as is" without express or implied warranty, and with no claim as + to its suitability for any purpose. + + + diff --git a/iterator_adaptors.htm b/iterator_adaptors.htm index 7cfb439..a743cfa 100644 --- a/iterator_adaptors.htm +++ b/iterator_adaptors.htm @@ -84,6 +84,11 @@ "../../boost/counting_iterator.hpp">boost/counting_iterator.hpp
Counting Iterator Adaptor + +

  • Header boost/function_output_iterator.hpp
    + + Function Output Iterator Adaptor

    Dave @@ -98,8 +103,9 @@ Jeremy Siek contributed the transform iterator adaptor, the integer-only version of counting_iterator_generator, and most of - the documentation.
    + "counting_iterator.htm">counting_iterator_generator, + the function output iterator + adaptor, and most of the documentation.
    John Potter contributed the projection_ and