indexable.hpp 10 KB

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