indexable.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  4. //
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
  9. #define BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
  10. #include <boost/mpl/assert.hpp>
  11. #include <boost/geometry/index/detail/is_indexable.hpp>
  12. namespace boost { namespace geometry { namespace index { namespace detail {
  13. /*!
  14. \brief The function object extracting Indexable from Value.
  15. It translates Value object to Indexable object. The default version handles Values which are Indexables.
  16. This template is also specialized for std::pair<Indexable, T2>, boost::tuple<Indexable, ...>
  17. and std::tuple<Indexable, ...>.
  18. \tparam Value The Value type which may be translated directly to the Indexable.
  19. \tparam IsIndexable If true, the const reference to Value is returned.
  20. */
  21. template <typename Value, bool IsIndexable = is_indexable<Value>::value>
  22. struct indexable
  23. {
  24. BOOST_MPL_ASSERT_MSG(
  25. (detail::is_indexable<Value>::value),
  26. NOT_VALID_INDEXABLE_TYPE,
  27. (Value)
  28. );
  29. /*! \brief The type of result returned by function object. */
  30. typedef Value const& result_type;
  31. /*!
  32. \brief Return indexable extracted from the value.
  33. \param v The value.
  34. \return The indexable.
  35. */
  36. inline result_type operator()(Value const& v) const
  37. {
  38. return v;
  39. }
  40. };
  41. /*!
  42. \brief The function object extracting Indexable from Value.
  43. This specialization translates from std::pair<Indexable, T2>.
  44. \tparam Indexable The Indexable type.
  45. \tparam T2 The second type.
  46. */
  47. template <typename Indexable, typename T2>
  48. struct indexable<std::pair<Indexable, T2>, false>
  49. {
  50. BOOST_MPL_ASSERT_MSG(
  51. (detail::is_indexable<Indexable>::value),
  52. NOT_VALID_INDEXABLE_TYPE,
  53. (Indexable)
  54. );
  55. /*! \brief The type of result returned by function object. */
  56. typedef Indexable const& result_type;
  57. /*!
  58. \brief Return indexable extracted from the value.
  59. \param v The value.
  60. \return The indexable.
  61. */
  62. inline result_type operator()(std::pair<Indexable, T2> const& v) const
  63. {
  64. return v.first;
  65. }
  66. };
  67. /*!
  68. \brief The function object extracting Indexable from Value.
  69. This specialization translates from boost::tuple<Indexable, ...>.
  70. \tparam Indexable The Indexable type.
  71. */
  72. template <typename Indexable, typename T1, typename T2, typename T3, typename T4,
  73. typename T5, typename T6, typename T7, typename T8, typename T9>
  74. struct indexable<boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9>, false>
  75. {
  76. typedef boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9> value_type;
  77. BOOST_MPL_ASSERT_MSG(
  78. (detail::is_indexable<Indexable>::value),
  79. NOT_VALID_INDEXABLE_TYPE,
  80. (Indexable)
  81. );
  82. /*! \brief The type of result returned by function object. */
  83. typedef Indexable const& result_type;
  84. /*!
  85. \brief Return indexable extracted from the value.
  86. \param v The value.
  87. \return The indexable.
  88. */
  89. inline result_type operator()(value_type const& v) const
  90. {
  91. return boost::get<0>(v);
  92. }
  93. };
  94. }}}} // namespace boost::geometry::index::detail
  95. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  96. #include <tuple>
  97. namespace boost { namespace geometry { namespace index { namespace detail {
  98. /*!
  99. \brief The function object extracting Indexable from Value.
  100. This specialization translates from std::tuple<Indexable, Args...>.
  101. It's defined if the compiler supports tuples and variadic templates.
  102. \tparam Indexable The Indexable type.
  103. */
  104. template <typename Indexable, typename ...Args>
  105. struct indexable<std::tuple<Indexable, Args...>, false>
  106. {
  107. typedef std::tuple<Indexable, Args...> value_type;
  108. BOOST_MPL_ASSERT_MSG(
  109. (detail::is_indexable<Indexable>::value),
  110. NOT_VALID_INDEXABLE_TYPE,
  111. (Indexable)
  112. );
  113. /*! \brief The type of result returned by function object. */
  114. typedef Indexable const& result_type;
  115. /*!
  116. \brief Return indexable extracted from the value.
  117. \param v The value.
  118. \return The indexable.
  119. */
  120. result_type operator()(value_type const& v) const
  121. {
  122. return std::get<0>(v);
  123. }
  124. };
  125. }}}} // namespace boost::geometry::index::detail
  126. #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  127. namespace boost { namespace geometry { namespace index {
  128. /*!
  129. \brief The function object extracting Indexable from Value.
  130. It translates Value object to Indexable object. By default, it can handle Values which are Indexables,
  131. std::pair<Indexable, T2>, boost::tuple<Indexable, ...> and std::tuple<Indexable, ...> if STD tuples
  132. and variadic templates are supported.
  133. \tparam Value The Value type which may be translated directly to the Indexable.
  134. */
  135. template <typename Value>
  136. struct indexable
  137. : detail::indexable<Value>
  138. {
  139. /*! \brief The type of result returned by function object. It should be const Indexable reference. */
  140. typedef typename detail::indexable<Value>::result_type result_type;
  141. /*!
  142. \brief Return indexable extracted from the value.
  143. \param v The value.
  144. \return The indexable.
  145. */
  146. inline result_type operator()(Value const& v) const
  147. {
  148. return detail::indexable<Value>::operator()(v);
  149. }
  150. };
  151. }}} // namespace boost::geometry::index
  152. #endif // BOOST_GEOMETRY_INDEX_INDEXABLE_HPP