function_output_iterator.qbk 2.4 KB

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