indexable.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2019 Adam Wulkiewicz, Lodz, Poland.
  4. //
  5. // This file was modified by Oracle on 2020.
  6. // Modifications copyright (c) 2020 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. //
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
  13. #define BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
  14. #include <boost/tuple/tuple.hpp>
  15. #include <boost/geometry/core/static_assert.hpp>
  16. #include <boost/geometry/index/detail/is_indexable.hpp>
  17. #include <boost/geometry/util/type_traits.hpp>
  18. namespace boost { namespace geometry { namespace index { namespace detail
  19. {
  20. template <typename From, typename To>
  21. struct is_referencable
  22. : std::is_same
  23. <
  24. typename util::remove_cref<From>::type,
  25. typename util::remove_cref<To>::type
  26. >
  27. {};
  28. template <typename Indexable, typename V>
  29. inline Indexable const& indexable_prevent_any_type(V const& )
  30. {
  31. BOOST_GEOMETRY_STATIC_ASSERT_FALSE("Unexpected type.", V);
  32. return Indexable();
  33. }
  34. /*!
  35. \brief The function object extracting Indexable from Value.
  36. It translates Value object to Indexable object. The default version handles Values which are Indexables.
  37. This template is also specialized for std::pair<Indexable, T2>, boost::tuple<Indexable, ...>
  38. and std::tuple<Indexable, ...>.
  39. \tparam Value The Value type which may be translated directly to the Indexable.
  40. \tparam IsIndexable If true, the const reference to Value is returned.
  41. */
  42. template <typename Value, bool IsIndexable = is_indexable<Value>::value>
  43. struct indexable
  44. {
  45. BOOST_GEOMETRY_STATIC_ASSERT(
  46. (detail::is_indexable<Value>::value),
  47. "Value has to be an Indexable.",
  48. Value);
  49. /*! \brief The type of result returned by function object. */
  50. typedef Value const& result_type;
  51. /*!
  52. \brief Return indexable extracted from the value.
  53. \param v The value.
  54. \return The indexable.
  55. */
  56. inline result_type operator()(Value const& v) const
  57. {
  58. return v;
  59. }
  60. /*!
  61. \brief Prevent reference to temporary for types convertible to Value.
  62. */
  63. template <typename V>
  64. inline result_type operator()(V const& v) const
  65. {
  66. return indexable_prevent_any_type<Value>(v);
  67. }
  68. };
  69. /*!
  70. \brief The function object extracting Indexable from Value.
  71. This specialization translates from std::pair<Indexable, T2>.
  72. \tparam Indexable The Indexable type.
  73. \tparam Second The second type.
  74. */
  75. template <typename Indexable, typename Second>
  76. struct indexable<std::pair<Indexable, Second>, false>
  77. {
  78. typedef std::pair<Indexable, Second> value_type;
  79. BOOST_GEOMETRY_STATIC_ASSERT(
  80. (detail::is_indexable<Indexable>::value),
  81. "The first type of std::pair has to be an Indexable.",
  82. Indexable);
  83. /*! \brief The type of result returned by function object. */
  84. typedef Indexable const& result_type;
  85. /*!
  86. \brief Return indexable extracted from the value.
  87. \param v The value.
  88. \return The indexable.
  89. */
  90. inline result_type operator()(value_type const& v) const
  91. {
  92. return v.first;
  93. }
  94. /*!
  95. \brief Return indexable extracted from compatible type different than value_type.
  96. \param v The value.
  97. \return The indexable.
  98. */
  99. template <typename I, typename S>
  100. inline result_type operator()(std::pair<I, S> const& v) const
  101. {
  102. BOOST_GEOMETRY_STATIC_ASSERT(
  103. (is_referencable<I, result_type>::value),
  104. "Unexpected type.",
  105. std::pair<I, S>);
  106. return v.first;
  107. }
  108. /*!
  109. \brief Prevent reference to temporary for types convertible to Value.
  110. */
  111. template <typename V>
  112. inline result_type operator()(V const& v) const
  113. {
  114. return indexable_prevent_any_type<Indexable>(v);
  115. }
  116. };
  117. /*!
  118. \brief The function object extracting Indexable from Value.
  119. This specialization translates from boost::tuple<Indexable, ...>
  120. or boost::tuples::cons<Indexable, ...>.
  121. \tparam Value The Value type.
  122. \tparam Indexable The Indexable type.
  123. */
  124. template <typename Value, typename Indexable>
  125. struct indexable_boost_tuple
  126. {
  127. typedef Value value_type;
  128. BOOST_GEOMETRY_STATIC_ASSERT(
  129. (detail::is_indexable<Indexable>::value),
  130. "The first type of boost::tuple has to be an Indexable.",
  131. Indexable);
  132. /*! \brief The type of result returned by function object. */
  133. typedef Indexable const& result_type;
  134. /*!
  135. \brief Return indexable extracted from the value.
  136. \param v The value.
  137. \return The indexable.
  138. */
  139. inline result_type operator()(value_type const& v) const
  140. {
  141. return boost::get<0>(v);
  142. }
  143. /*!
  144. \brief Return indexable extracted from compatible type different than value_type.
  145. \param v The value.
  146. \return The indexable.
  147. */
  148. template <typename I, typename U1, typename U2, typename U3, typename U4,
  149. typename U5, typename U6, typename U7, typename U8, typename U9>
  150. inline result_type operator()(boost::tuple<I, U1, U2, U3, U4, U5, U6, U7, U8, U9> const& v) const
  151. {
  152. BOOST_GEOMETRY_STATIC_ASSERT(
  153. (is_referencable<I, result_type>::value),
  154. "Unexpected type.",
  155. boost::tuple<I, U1, U2, U3, U4, U5, U6, U7, U8, U9>);
  156. return boost::get<0>(v);
  157. }
  158. /*!
  159. \brief Return indexable extracted from compatible type different than value_type.
  160. \param v The value.
  161. \return The indexable.
  162. */
  163. template <typename I, typename T>
  164. inline result_type operator()(boost::tuples::cons<I, T> const& v) const
  165. {
  166. BOOST_GEOMETRY_STATIC_ASSERT(
  167. (is_referencable<I, result_type>::value),
  168. "Unexpected type.",
  169. boost::tuples::cons<I, T>);
  170. return boost::get<0>(v);
  171. }
  172. /*!
  173. \brief Prevent reference to temporary for types convertible to Value.
  174. */
  175. template <typename V>
  176. inline result_type operator()(V const& v) const
  177. {
  178. return indexable_prevent_any_type<Indexable>(v);
  179. }
  180. };
  181. /*!
  182. \brief The function object extracting Indexable from Value.
  183. This specialization translates from boost::tuple<Indexable, ...>.
  184. \tparam Indexable The Indexable type.
  185. */
  186. template <typename Indexable, typename T1, typename T2, typename T3, typename T4,
  187. typename T5, typename T6, typename T7, typename T8, typename T9>
  188. struct indexable<boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9>, false>
  189. : indexable_boost_tuple
  190. <
  191. boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9>,
  192. Indexable
  193. >
  194. {};
  195. /*!
  196. \brief The function object extracting Indexable from Value.
  197. This specialization translates from boost::tuples::cons<Indexable, ...>.
  198. \tparam Indexable The Indexable type.
  199. */
  200. template <typename Indexable, typename Tail>
  201. struct indexable<boost::tuples::cons<Indexable, Tail>, false>
  202. : indexable_boost_tuple
  203. <
  204. boost::tuples::cons<Indexable, Tail>,
  205. Indexable
  206. >
  207. {};
  208. }}}} // namespace boost::geometry::index::detail
  209. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  210. #include <tuple>
  211. namespace boost { namespace geometry { namespace index { namespace detail {
  212. /*!
  213. \brief The function object extracting Indexable from Value.
  214. This specialization translates from std::tuple<Indexable, Args...>.
  215. It's defined if the compiler supports tuples and variadic templates.
  216. \tparam Indexable The Indexable type.
  217. */
  218. template <typename Indexable, typename ...Args>
  219. struct indexable<std::tuple<Indexable, Args...>, false>
  220. {
  221. typedef std::tuple<Indexable, Args...> value_type;
  222. BOOST_GEOMETRY_STATIC_ASSERT(
  223. (detail::is_indexable<Indexable>::value),
  224. "The first type of std::tuple has to be an Indexable.",
  225. Indexable);
  226. /*! \brief The type of result returned by function object. */
  227. typedef Indexable const& result_type;
  228. /*!
  229. \brief Return indexable extracted from the value.
  230. \param v The value.
  231. \return The indexable.
  232. */
  233. result_type operator()(value_type const& v) const
  234. {
  235. return std::get<0>(v);
  236. }
  237. /*!
  238. \brief Return indexable extracted from compatible type different than value_type.
  239. \param v The value.
  240. \return The indexable.
  241. */
  242. template <typename I, typename ...A>
  243. inline result_type operator()(std::tuple<I, A...> const& v) const
  244. {
  245. BOOST_GEOMETRY_STATIC_ASSERT(
  246. (is_referencable<I, result_type>::value),
  247. "Unexpected type.",
  248. std::tuple<I, A...>);
  249. return std::get<0>(v);
  250. }
  251. /*!
  252. \brief Prevent reference to temporary for types convertible to Value.
  253. */
  254. template <typename V>
  255. inline result_type operator()(V const& v) const
  256. {
  257. return indexable_prevent_any_type<Indexable>(v);
  258. }
  259. };
  260. }}}} // namespace boost::geometry::index::detail
  261. #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  262. namespace boost { namespace geometry { namespace index {
  263. /*!
  264. \brief The function object extracting Indexable from Value.
  265. It translates Value object to Indexable object. By default, it can handle Values which are Indexables,
  266. std::pair<Indexable, T2>, boost::tuple<Indexable, ...> and std::tuple<Indexable, ...> if STD tuples
  267. and variadic templates are supported.
  268. \tparam Value The Value type which may be translated directly to the Indexable.
  269. */
  270. template <typename Value>
  271. struct indexable
  272. : detail::indexable<Value>
  273. {
  274. /*! \brief The type of result returned by function object. It should be const Indexable reference. */
  275. typedef typename detail::indexable<Value>::result_type result_type;
  276. /*!
  277. \brief Return indexable extracted from the value.
  278. \param v The value.
  279. \return The indexable.
  280. */
  281. inline result_type operator()(Value const& v) const
  282. {
  283. return detail::indexable<Value>::operator()(v);
  284. }
  285. /*!
  286. \brief Return indexable extracted from the value. Overload for types
  287. compatible with Value but different yet holding referencable
  288. Indexable, e.g. tuple containing a reference.
  289. \param v The value.
  290. \return The indexable.
  291. */
  292. template <typename V>
  293. inline result_type operator()(V const& v) const
  294. {
  295. return detail::indexable<Value>::operator()(v);
  296. }
  297. };
  298. }}} // namespace boost::geometry::index
  299. #endif // BOOST_GEOMETRY_INDEX_INDEXABLE_HPP