indexable.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2013 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. namespace boost { namespace geometry { namespace index {
  12. namespace detail {
  13. template <typename Geometry, typename GeometryTag>
  14. struct is_indexable_impl { static const bool value = false; };
  15. template <typename Point>
  16. struct is_indexable_impl<Point, geometry::point_tag> { static const bool value = true; };
  17. template <typename Box>
  18. struct is_indexable_impl<Box, geometry::box_tag> { static const bool value = true; };
  19. template <typename Indexable>
  20. struct is_indexable
  21. {
  22. static const bool value =
  23. is_indexable_impl<Indexable, typename geometry::traits::tag<Indexable>::type>::value;
  24. };
  25. } // namespace detail
  26. /*!
  27. \brief The function object extracting Indexable from Value.
  28. It translates Value object to Indexable object. The default version handles Values which are Indexables.
  29. This template is also specialized for std::pair<Indexable, T2> and boost::tuple<Indexable, ...>.
  30. \tparam Value The Value type which may be translated directly to the Indexable.
  31. */
  32. template <typename Value>
  33. struct indexable
  34. {
  35. BOOST_MPL_ASSERT_MSG(
  36. (detail::is_indexable<Value>::value),
  37. NOT_VALID_INDEXABLE_TYPE,
  38. (Value)
  39. );
  40. /*! \brief The type of result returned by function object. */
  41. typedef Value const& result_type;
  42. /*!
  43. \brief Return indexable extracted from the value.
  44. \param v The value.
  45. \return The indexable.
  46. */
  47. result_type operator()(Value const& v) const
  48. {
  49. return v;
  50. }
  51. };
  52. /*!
  53. \brief The function object extracting Indexable from Value.
  54. This specialization translates from std::pair<Indexable, T2>.
  55. \tparam Indexable The Indexable type.
  56. \tparam T2 The second type.
  57. */
  58. template <typename Indexable, typename T2>
  59. struct indexable< std::pair<Indexable, T2> >
  60. {
  61. BOOST_MPL_ASSERT_MSG(
  62. (detail::is_indexable<Indexable>::value),
  63. NOT_VALID_INDEXABLE_TYPE,
  64. (Indexable)
  65. );
  66. /*! \brief The type of result returned by function object. */
  67. typedef Indexable const& result_type;
  68. /*!
  69. \brief Return indexable extracted from the value.
  70. \param v The value.
  71. \return The indexable.
  72. */
  73. result_type operator()(std::pair<Indexable, T2> const& v) const
  74. {
  75. return v.first;
  76. }
  77. };
  78. /*!
  79. \brief The function object extracting Indexable from Value.
  80. This specialization translates from boost::tuple<Indexable, ...>.
  81. \tparam Indexable The Indexable type.
  82. */
  83. template <typename Indexable, typename T1, typename T2, typename T3, typename T4,
  84. typename T5, typename T6, typename T7, typename T8, typename T9>
  85. struct indexable< boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  86. {
  87. typedef boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9> value_type;
  88. BOOST_MPL_ASSERT_MSG(
  89. (detail::is_indexable<Indexable>::value),
  90. NOT_VALID_INDEXABLE_TYPE,
  91. (Indexable)
  92. );
  93. /*! \brief The type of result returned by function object. */
  94. typedef Indexable const& result_type;
  95. /*!
  96. \brief Return indexable extracted from the value.
  97. \param v The value.
  98. \return The indexable.
  99. */
  100. result_type operator()(value_type const& v) const
  101. {
  102. return boost::get<0>(v);
  103. }
  104. };
  105. }}} // namespace boost::geometry::index
  106. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  107. #include <tuple>
  108. namespace boost { namespace geometry { namespace index {
  109. /*!
  110. \brief The function object extracting Indexable from Value.
  111. This specialization translates from std::tuple<Indexable, Args...>.
  112. It's defined if the compiler supports tuples and variadic templates.
  113. \tparam Indexable The Indexable type.
  114. */
  115. template <typename Indexable, typename ...Args>
  116. struct indexable< std::tuple<Indexable, Args...> >
  117. {
  118. typedef std::tuple<Indexable, Args...> value_type;
  119. BOOST_MPL_ASSERT_MSG(
  120. (detail::is_indexable<Indexable>::value),
  121. NOT_VALID_INDEXABLE_TYPE,
  122. (Indexable)
  123. );
  124. /*! \brief The type of result returned by function object. */
  125. typedef Indexable const& result_type;
  126. /*!
  127. \brief Return indexable extracted from the value.
  128. \param v The value.
  129. \return The indexable.
  130. */
  131. result_type operator()(value_type const& v) const
  132. {
  133. return std::get<0>(v);
  134. }
  135. };
  136. }}} // namespace boost::geometry::index
  137. #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  138. #endif // BOOST_GEOMETRY_INDEX_INDEXABLE_HPP