indexable.hpp 10 KB

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