transform_iterator.hpp 5.8 KB

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