any.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Boost.Geometry
  2. // Copyright (c) 2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP
  7. #define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP
  8. #include <utility>
  9. #include <boost/geometry/util/sequence.hpp>
  10. #include <boost/geometry/util/type_traits_std.hpp>
  11. namespace boost { namespace geometry
  12. {
  13. namespace detail
  14. {
  15. template
  16. <
  17. typename CastPolicy,
  18. typename TypeSequence,
  19. std::size_t N = util::sequence_size<TypeSequence>::value
  20. >
  21. struct visit_any
  22. {
  23. static const std::size_t M = N / 2;
  24. template <std::size_t Offset, typename Function, typename Any>
  25. static bool apply(Function && function, Any && any)
  26. {
  27. return visit_any<CastPolicy, TypeSequence, M>::template apply<Offset>(
  28. std::forward<Function>(function), std::forward<Any>(any))
  29. || visit_any<CastPolicy, TypeSequence, N - M>::template apply<Offset + M>(
  30. std::forward<Function>(function), std::forward<Any>(any));
  31. }
  32. };
  33. template <typename CastPolicy, typename TypeSequence>
  34. struct visit_any<CastPolicy, TypeSequence, 1>
  35. {
  36. template <std::size_t Offset, typename Function, typename Any>
  37. static bool apply(Function && function, Any && any)
  38. {
  39. using elem_t = typename util::sequence_element<Offset, TypeSequence>::type;
  40. using geom_t = util::transcribe_const_t<std::remove_reference_t<Any>, elem_t>;
  41. geom_t * g = CastPolicy::template apply<geom_t>(&any);
  42. if (g != nullptr)
  43. {
  44. using geom_ref_t = std::conditional_t
  45. <
  46. std::is_rvalue_reference<decltype(any)>::value,
  47. geom_t&&, geom_t&
  48. >;
  49. function(static_cast<geom_ref_t>(*g));
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. };
  58. template <typename CastPolicy, typename TypeSequence>
  59. struct visit_any<CastPolicy, TypeSequence, 0>
  60. {
  61. template <std::size_t Offset, typename Function, typename Any>
  62. static bool apply(Function &&, Any &&)
  63. {
  64. return false;
  65. }
  66. };
  67. } // namespace detail
  68. }} // namespace boost::geometry
  69. #endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_DETAIL_ANY_HPP