|
| 1 | +// (C) Copyright David Abrahams 2002. |
| 2 | +// (C) Copyright Jeremy Siek 2002. |
| 3 | +// (C) Copyright Thomas Witt 2002. |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 5 | +// accompanying file LICENSE_1_0.txt or copy at |
| 6 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 7 | +#ifndef BOOST_TRANSFORM_ITERATOR_23022003THW_HPP |
| 8 | +#define BOOST_TRANSFORM_ITERATOR_23022003THW_HPP |
| 9 | + |
| 10 | +#include <boost/iterator.hpp> |
| 11 | +#include <boost/iterator/detail/enable_if.hpp> |
| 12 | +#include <boost/iterator/iterator_adaptor.hpp> |
| 13 | +#include <boost/iterator/iterator_categories.hpp> |
| 14 | +#include <boost/mpl/not.hpp> |
| 15 | +#include <boost/mpl/bool.hpp> |
| 16 | +#include <boost/type_traits/function_traits.hpp> |
| 17 | +#include <boost/type_traits/is_const.hpp> |
| 18 | +#include <boost/type_traits/is_class.hpp> |
| 19 | +#include <boost/type_traits/is_function.hpp> |
| 20 | +#include <boost/type_traits/is_reference.hpp> |
| 21 | +#include <boost/type_traits/remove_const.hpp> |
| 22 | +#include <boost/type_traits/remove_reference.hpp> |
| 23 | +#include <boost/utility/result_of.hpp> |
| 24 | + |
| 25 | + |
| 26 | +#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310)) |
| 27 | +# include <boost/type_traits/is_base_and_derived.hpp> |
| 28 | + |
| 29 | +#endif |
| 30 | +#include <boost/iterator/detail/config_def.hpp> |
| 31 | + |
| 32 | + |
| 33 | +namespace riakboost{} namespace boost = riakboost; namespace riakboost{ |
| 34 | + template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default> |
| 35 | + class transform_iterator; |
| 36 | + |
| 37 | + namespace detail |
| 38 | + { |
| 39 | + // Compute the iterator_adaptor instantiation to be used for transform_iterator |
| 40 | + template <class UnaryFunc, class Iterator, class Reference, class Value> |
| 41 | + struct transform_iterator_base |
| 42 | + { |
| 43 | + private: |
| 44 | + // By default, dereferencing the iterator yields the same as |
| 45 | + // the function. |
| 46 | + typedef typename ia_dflt_help< |
| 47 | + Reference |
| 48 | + , result_of<UnaryFunc(typename std::iterator_traits<Iterator>::reference)> |
| 49 | + >::type reference; |
| 50 | + |
| 51 | + // To get the default for Value: remove any reference on the |
| 52 | + // result type, but retain any constness to signal |
| 53 | + // non-writability. Note that if we adopt Thomas' suggestion |
| 54 | + // to key non-writability *only* on the Reference argument, |
| 55 | + // we'd need to strip constness here as well. |
| 56 | + typedef typename ia_dflt_help< |
| 57 | + Value |
| 58 | + , remove_reference<reference> |
| 59 | + >::type cv_value_type; |
| 60 | + |
| 61 | + public: |
| 62 | + typedef iterator_adaptor< |
| 63 | + transform_iterator<UnaryFunc, Iterator, Reference, Value> |
| 64 | + , Iterator |
| 65 | + , cv_value_type |
| 66 | + , use_default // Leave the traversal category alone |
| 67 | + , reference |
| 68 | + > type; |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + template <class UnaryFunc, class Iterator, class Reference, class Value> |
| 73 | + class transform_iterator |
| 74 | + : public riakboost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type |
| 75 | + { |
| 76 | + typedef typename |
| 77 | + riakboost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type |
| 78 | + super_t; |
| 79 | + |
| 80 | + friend class iterator_core_access; |
| 81 | + |
| 82 | + public: |
| 83 | + transform_iterator() { } |
| 84 | + |
| 85 | + transform_iterator(Iterator const& x, UnaryFunc f) |
| 86 | + : super_t(x), m_f(f) { } |
| 87 | + |
| 88 | + explicit transform_iterator(Iterator const& x) |
| 89 | + : super_t(x) |
| 90 | + { |
| 91 | + // Pro8 is a little too aggressive about instantiating the |
| 92 | + // body of this function. |
| 93 | +#if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) |
| 94 | + // don't provide this constructor if UnaryFunc is a |
| 95 | + // function pointer type, since it will be 0. Too dangerous. |
| 96 | + BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value); |
| 97 | +#endif |
| 98 | + } |
| 99 | + |
| 100 | + template < |
| 101 | + class OtherUnaryFunction |
| 102 | + , class OtherIterator |
| 103 | + , class OtherReference |
| 104 | + , class OtherValue> |
| 105 | + transform_iterator( |
| 106 | + transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t |
| 107 | + , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 |
| 108 | +#if !BOOST_WORKAROUND(BOOST_MSVC, == 1310) |
| 109 | + , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0 |
| 110 | +#endif |
| 111 | + ) |
| 112 | + : super_t(t.base()), m_f(t.functor()) |
| 113 | + {} |
| 114 | + |
| 115 | + UnaryFunc functor() const |
| 116 | + { return m_f; } |
| 117 | + |
| 118 | + private: |
| 119 | + typename super_t::reference dereference() const |
| 120 | + { return m_f(*this->base()); } |
| 121 | + |
| 122 | + // Probably should be the initial base class so it can be |
| 123 | + // optimized away via EBO if it is an empty class. |
| 124 | + UnaryFunc m_f; |
| 125 | + }; |
| 126 | + |
| 127 | + template <class UnaryFunc, class Iterator> |
| 128 | + transform_iterator<UnaryFunc, Iterator> |
| 129 | + make_transform_iterator(Iterator it, UnaryFunc fun) |
| 130 | + { |
| 131 | + return transform_iterator<UnaryFunc, Iterator>(it, fun); |
| 132 | + } |
| 133 | + |
| 134 | + // Version which allows explicit specification of the UnaryFunc |
| 135 | + // type. |
| 136 | + // |
| 137 | + // This generator is not provided if UnaryFunc is a function |
| 138 | + // pointer type, because it's too dangerous: the default-constructed |
| 139 | + // function pointer in the iterator be 0, leading to a runtime |
| 140 | + // crash. |
| 141 | + template <class UnaryFunc, class Iterator> |
| 142 | +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) |
| 143 | + typename mpl::if_< |
| 144 | +#else |
| 145 | + typename iterators::enable_if< |
| 146 | +#endif |
| 147 | + is_class<UnaryFunc> // We should probably find a cheaper test than is_class<> |
| 148 | + , transform_iterator<UnaryFunc, Iterator> |
| 149 | +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) |
| 150 | + , int[3] |
| 151 | +#endif |
| 152 | + >::type |
| 153 | + make_transform_iterator(Iterator it) |
| 154 | + { |
| 155 | + return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc()); |
| 156 | + } |
| 157 | + |
| 158 | +#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
| 159 | + template <class Return, class Argument, class Iterator> |
| 160 | + transform_iterator< Return (*)(Argument), Iterator, Return> |
| 161 | + make_transform_iterator(Iterator it, Return (*fun)(Argument)) |
| 162 | + { |
| 163 | + return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun); |
| 164 | + } |
| 165 | +#endif |
| 166 | + |
| 167 | +} // namespace riakboost |
| 168 | + |
| 169 | +#include <boost/iterator/detail/config_undef.hpp> |
| 170 | + |
| 171 | +#endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP |
0 commit comments