transform_iterator.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/config.hpp>
  10. #include <boost/config/workaround.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/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. #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  27. #include <boost/static_assert.hpp>
  28. #endif
  29. #include <boost/iterator/detail/config_def.hpp>
  30. namespace boost {
  31. namespace iterators {
  32. template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
  33. class transform_iterator;
  34. namespace detail
  35. {
  36. // Compute the iterator_adaptor instantiation to be used for transform_iterator
  37. template <class UnaryFunc, class Iterator, class Reference, class Value>
  38. struct transform_iterator_base
  39. {
  40. private:
  41. // By default, dereferencing the iterator yields the same as
  42. // the function.
  43. typedef typename ia_dflt_help<
  44. Reference
  45. #ifdef BOOST_RESULT_OF_USE_TR1
  46. , result_of<const UnaryFunc(typename std::iterator_traits<Iterator>::reference)>
  47. #else
  48. , result_of<const UnaryFunc&(typename std::iterator_traits<Iterator>::reference)>
  49. #endif
  50. >::type reference;
  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. public:
  61. typedef iterator_adaptor<
  62. transform_iterator<UnaryFunc, Iterator, Reference, Value>
  63. , Iterator
  64. , cv_value_type
  65. , use_default // Leave the traversal category alone
  66. , reference
  67. > type;
  68. };
  69. }
  70. template <class UnaryFunc, class Iterator, class Reference, class Value>
  71. class transform_iterator
  72. : public boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
  73. {
  74. typedef typename
  75. boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
  76. super_t;
  77. friend class iterator_core_access;
  78. public:
  79. transform_iterator() { }
  80. transform_iterator(Iterator const& x, UnaryFunc f)
  81. : super_t(x), m_f(f) { }
  82. explicit transform_iterator(Iterator const& x)
  83. : super_t(x)
  84. {
  85. // Pro8 is a little too aggressive about instantiating the
  86. // body of this function.
  87. #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  88. // don't provide this constructor if UnaryFunc is a
  89. // function pointer type, since it will be 0. Too dangerous.
  90. BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value);
  91. #endif
  92. }
  93. template <
  94. class OtherUnaryFunction
  95. , class OtherIterator
  96. , class OtherReference
  97. , class OtherValue>
  98. transform_iterator(
  99. transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
  100. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
  101. #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  102. , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0
  103. #endif
  104. )
  105. : super_t(t.base()), m_f(t.functor())
  106. {}
  107. UnaryFunc functor() const
  108. { return m_f; }
  109. private:
  110. typename super_t::reference dereference() const
  111. { return m_f(*this->base()); }
  112. // Probably should be the initial base class so it can be
  113. // optimized away via EBO if it is an empty class.
  114. UnaryFunc m_f;
  115. };
  116. template <class UnaryFunc, class Iterator>
  117. inline transform_iterator<UnaryFunc, Iterator>
  118. make_transform_iterator(Iterator it, UnaryFunc fun)
  119. {
  120. return transform_iterator<UnaryFunc, Iterator>(it, fun);
  121. }
  122. // Version which allows explicit specification of the UnaryFunc
  123. // type.
  124. //
  125. // This generator is not provided if UnaryFunc is a function
  126. // pointer type, because it's too dangerous: the default-constructed
  127. // function pointer in the iterator be 0, leading to a runtime
  128. // crash.
  129. template <class UnaryFunc, class Iterator>
  130. inline typename iterators::enable_if<
  131. is_class<UnaryFunc> // We should probably find a cheaper test than is_class<>
  132. , transform_iterator<UnaryFunc, Iterator>
  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. inline 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 iterators
  147. using iterators::transform_iterator;
  148. using iterators::make_transform_iterator;
  149. } // namespace boost
  150. #include <boost/iterator/detail/config_undef.hpp>
  151. #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP