transform_iterator.hpp 5.6 KB

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