| 1 | // (C) Copyright Jeremy Siek 2001. |
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // Revision History: |
| 7 | |
| 8 | // 27 Feb 2001 Jeremy Siek |
| 9 | // Initial checkin. |
| 10 | |
| 11 | #ifndef BOOST_ITERATOR_FUNCTION_OUTPUT_ITERATOR_HPP |
| 12 | #define BOOST_ITERATOR_FUNCTION_OUTPUT_ITERATOR_HPP |
| 13 | |
| 14 | #include <iterator> |
| 15 | #include <boost/config.hpp> |
| 16 | #include <boost/core/enable_if.hpp> |
| 17 | #include <boost/type_traits/is_same.hpp> |
| 18 | #include <boost/type_traits/remove_cv.hpp> |
| 19 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES |
| 20 | #include <boost/type_traits/remove_reference.hpp> |
| 21 | #endif |
| 22 | |
| 23 | namespace boost { |
| 24 | namespace iterators { |
| 25 | |
| 26 | template <class UnaryFunction> |
| 27 | class function_output_iterator { |
| 28 | private: |
| 29 | typedef function_output_iterator self; |
| 30 | |
| 31 | class output_proxy { |
| 32 | public: |
| 33 | explicit output_proxy(UnaryFunction& f) BOOST_NOEXCEPT : m_f(f) { } |
| 34 | |
| 35 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES |
| 36 | template <class T> |
| 37 | typename boost::disable_if_c< |
| 38 | boost::is_same< typename boost::remove_cv< T >::type, output_proxy >::value, |
| 39 | output_proxy& |
| 40 | >::type operator=(const T& value) { |
| 41 | m_f(value); |
| 42 | return *this; |
| 43 | } |
| 44 | #else |
| 45 | template <class T> |
| 46 | typename boost::disable_if_c< |
| 47 | boost::is_same< typename boost::remove_cv< typename boost::remove_reference< T >::type >::type, output_proxy >::value, |
| 48 | output_proxy& |
| 49 | >::type operator=(T&& value) { |
| 50 | m_f(static_cast< T&& >(value)); |
| 51 | return *this; |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | BOOST_DEFAULTED_FUNCTION(output_proxy(output_proxy const& that), BOOST_NOEXCEPT : m_f(that.m_f) {}) |
| 56 | BOOST_DELETED_FUNCTION(output_proxy& operator=(output_proxy const&)) |
| 57 | |
| 58 | private: |
| 59 | UnaryFunction& m_f; |
| 60 | }; |
| 61 | |
| 62 | public: |
| 63 | typedef std::output_iterator_tag iterator_category; |
| 64 | typedef void value_type; |
| 65 | typedef void difference_type; |
| 66 | typedef void pointer; |
| 67 | typedef void reference; |
| 68 | |
| 69 | explicit function_output_iterator() {} |
| 70 | |
| 71 | explicit function_output_iterator(const UnaryFunction& f) |
| 72 | : m_f(f) {} |
| 73 | |
| 74 | output_proxy operator*() { return output_proxy(m_f); } |
| 75 | self& operator++() { return *this; } |
| 76 | self& operator++(int) { return *this; } |
| 77 | |
| 78 | private: |
| 79 | UnaryFunction m_f; |
| 80 | }; |
| 81 | |
| 82 | template <class UnaryFunction> |
| 83 | inline function_output_iterator<UnaryFunction> |
| 84 | make_function_output_iterator(const UnaryFunction& f = UnaryFunction()) { |
| 85 | return function_output_iterator<UnaryFunction>(f); |
| 86 | } |
| 87 | |
| 88 | } // namespace iterators |
| 89 | |
| 90 | using iterators::function_output_iterator; |
| 91 | using iterators::make_function_output_iterator; |
| 92 | |
| 93 | } // namespace boost |
| 94 | |
| 95 | #endif // BOOST_ITERATOR_FUNCTION_OUTPUT_ITERATOR_HPP |
| 96 | |