1 // Boost.Geometry Index
3 // Copyright (c) 2011-2019 Adam Wulkiewicz, Lodz, Poland.
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
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)
13 #ifndef BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
14 #define BOOST_GEOMETRY_INDEX_INDEXABLE_HPP
16 #include <boost/tuple/tuple.hpp>
18 #include <boost/geometry/core/static_assert.hpp>
20 #include <boost/geometry/index/detail/is_indexable.hpp>
22 #include <boost/geometry/util/type_traits.hpp>
24 namespace boost { namespace geometry { namespace index { namespace detail
27 template <typename From, typename To>
28 struct is_referencable
31 typename util::remove_cref<From>::type,
32 typename util::remove_cref<To>::type
36 template <typename Indexable, typename V>
37 inline Indexable const& indexable_prevent_any_type(V const& )
39 BOOST_GEOMETRY_STATIC_ASSERT_FALSE("Unexpected type.", V);
44 \brief The function object extracting Indexable from Value.
46 It translates Value object to Indexable object. The default version handles Values which are Indexables.
47 This template is also specialized for std::pair<Indexable, T2>, boost::tuple<Indexable, ...>
48 and std::tuple<Indexable, ...>.
50 \tparam Value The Value type which may be translated directly to the Indexable.
51 \tparam IsIndexable If true, the const reference to Value is returned.
53 template <typename Value, bool IsIndexable = is_indexable<Value>::value>
56 BOOST_GEOMETRY_STATIC_ASSERT(
57 (detail::is_indexable<Value>::value),
58 "Value has to be an Indexable.",
61 /*! \brief The type of result returned by function object. */
62 typedef Value const& result_type;
65 \brief Return indexable extracted from the value.
68 \return The indexable.
70 inline result_type operator()(Value const& v) const
76 \brief Prevent reference to temporary for types convertible to Value.
79 inline result_type operator()(V const& v) const
81 return indexable_prevent_any_type<Value>(v);
86 \brief The function object extracting Indexable from Value.
88 This specialization translates from std::pair<Indexable, T2>.
90 \tparam Indexable The Indexable type.
91 \tparam Second The second type.
93 template <typename Indexable, typename Second>
94 struct indexable<std::pair<Indexable, Second>, false>
96 typedef std::pair<Indexable, Second> value_type;
98 BOOST_GEOMETRY_STATIC_ASSERT(
99 (detail::is_indexable<Indexable>::value),
100 "The first type of std::pair has to be an Indexable.",
103 /*! \brief The type of result returned by function object. */
104 typedef Indexable const& result_type;
107 \brief Return indexable extracted from the value.
110 \return The indexable.
112 inline result_type operator()(value_type const& v) const
118 \brief Return indexable extracted from compatible type different than value_type.
121 \return The indexable.
123 template <typename I, typename S>
124 inline result_type operator()(std::pair<I, S> const& v) const
126 BOOST_GEOMETRY_STATIC_ASSERT(
127 (is_referencable<I, result_type>::value),
134 \brief Prevent reference to temporary for types convertible to Value.
136 template <typename V>
137 inline result_type operator()(V const& v) const
139 return indexable_prevent_any_type<Indexable>(v);
144 \brief The function object extracting Indexable from Value.
146 This specialization translates from boost::tuple<Indexable, ...>
147 or boost::tuples::cons<Indexable, ...>.
149 \tparam Value The Value type.
150 \tparam Indexable The Indexable type.
152 template <typename Value, typename Indexable>
153 struct indexable_boost_tuple
155 typedef Value value_type;
157 BOOST_GEOMETRY_STATIC_ASSERT(
158 (detail::is_indexable<Indexable>::value),
159 "The first type of boost::tuple has to be an Indexable.",
162 /*! \brief The type of result returned by function object. */
163 typedef Indexable const& result_type;
166 \brief Return indexable extracted from the value.
169 \return The indexable.
171 inline result_type operator()(value_type const& v) const
173 return boost::get<0>(v);
177 \brief Return indexable extracted from compatible type different than value_type.
180 \return The indexable.
182 template <typename I, typename U1, typename U2, typename U3, typename U4,
183 typename U5, typename U6, typename U7, typename U8, typename U9>
184 inline result_type operator()(boost::tuple<I, U1, U2, U3, U4, U5, U6, U7, U8, U9> const& v) const
186 BOOST_GEOMETRY_STATIC_ASSERT(
187 (is_referencable<I, result_type>::value),
189 boost::tuple<I, U1, U2, U3, U4, U5, U6, U7, U8, U9>);
190 return boost::get<0>(v);
194 \brief Return indexable extracted from compatible type different than value_type.
197 \return The indexable.
199 template <typename I, typename T>
200 inline result_type operator()(boost::tuples::cons<I, T> const& v) const
202 BOOST_GEOMETRY_STATIC_ASSERT(
203 (is_referencable<I, result_type>::value),
205 boost::tuples::cons<I, T>);
206 return boost::get<0>(v);
210 \brief Prevent reference to temporary for types convertible to Value.
212 template <typename V>
213 inline result_type operator()(V const& v) const
215 return indexable_prevent_any_type<Indexable>(v);
220 \brief The function object extracting Indexable from Value.
222 This specialization translates from boost::tuple<Indexable, ...>.
224 \tparam Indexable The Indexable type.
226 template <typename Indexable, typename T1, typename T2, typename T3, typename T4,
227 typename T5, typename T6, typename T7, typename T8, typename T9>
228 struct indexable<boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9>, false>
229 : indexable_boost_tuple
231 boost::tuple<Indexable, T1, T2, T3, T4, T5, T6, T7, T8, T9>,
237 \brief The function object extracting Indexable from Value.
239 This specialization translates from boost::tuples::cons<Indexable, ...>.
241 \tparam Indexable The Indexable type.
243 template <typename Indexable, typename Tail>
244 struct indexable<boost::tuples::cons<Indexable, Tail>, false>
245 : indexable_boost_tuple
247 boost::tuples::cons<Indexable, Tail>,
252 }}}} // namespace boost::geometry::index::detail
254 #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
258 namespace boost { namespace geometry { namespace index { namespace detail {
261 \brief The function object extracting Indexable from Value.
263 This specialization translates from std::tuple<Indexable, Args...>.
264 It's defined if the compiler supports tuples and variadic templates.
266 \tparam Indexable The Indexable type.
268 template <typename Indexable, typename ...Args>
269 struct indexable<std::tuple<Indexable, Args...>, false>
271 typedef std::tuple<Indexable, Args...> value_type;
273 BOOST_GEOMETRY_STATIC_ASSERT(
274 (detail::is_indexable<Indexable>::value),
275 "The first type of std::tuple has to be an Indexable.",
278 /*! \brief The type of result returned by function object. */
279 typedef Indexable const& result_type;
282 \brief Return indexable extracted from the value.
285 \return The indexable.
287 result_type operator()(value_type const& v) const
289 return std::get<0>(v);
293 \brief Return indexable extracted from compatible type different than value_type.
296 \return The indexable.
298 template <typename I, typename ...A>
299 inline result_type operator()(std::tuple<I, A...> const& v) const
301 BOOST_GEOMETRY_STATIC_ASSERT(
302 (is_referencable<I, result_type>::value),
304 std::tuple<I, A...>);
305 return std::get<0>(v);
309 \brief Prevent reference to temporary for types convertible to Value.
311 template <typename V>
312 inline result_type operator()(V const& v) const
314 return indexable_prevent_any_type<Indexable>(v);
318 }}}} // namespace boost::geometry::index::detail
320 #endif // !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
322 namespace boost { namespace geometry { namespace index {
325 \brief The function object extracting Indexable from Value.
327 It translates Value object to Indexable object. By default, it can handle Values which are Indexables,
328 std::pair<Indexable, T2>, boost::tuple<Indexable, ...> and std::tuple<Indexable, ...> if STD tuples
329 and variadic templates are supported.
331 \tparam Value The Value type which may be translated directly to the Indexable.
333 template <typename Value>
335 : detail::indexable<Value>
337 /*! \brief The type of result returned by function object. It should be const Indexable reference. */
338 typedef typename detail::indexable<Value>::result_type result_type;
341 \brief Return indexable extracted from the value.
344 \return The indexable.
346 inline result_type operator()(Value const& v) const
348 return detail::indexable<Value>::operator()(v);
352 \brief Return indexable extracted from the value. Overload for types
353 compatible with Value but different yet holding referencable
354 Indexable, e.g. tuple containing a reference.
357 \return The indexable.
359 template <typename V>
360 inline result_type operator()(V const& v) const
362 return detail::indexable<Value>::operator()(v);
366 }}} // namespace boost::geometry::index
368 #endif // BOOST_GEOMETRY_INDEX_INDEXABLE_HPP