boost_variant.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2018-2021.
  6. // Modifications copyright (c) 2018-2021, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_VARIANT_HPP
  14. #define BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_VARIANT_HPP
  15. #include <boost/variant/apply_visitor.hpp>
  16. #include <boost/variant/static_visitor.hpp>
  17. #include <boost/variant/variant.hpp>
  18. //#include <boost/variant/variant_fwd.hpp>
  19. #include <boost/geometry/core/geometry_types.hpp>
  20. #include <boost/geometry/core/point_type.hpp>
  21. #include <boost/geometry/core/tag.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/core/visit.hpp>
  24. #include <boost/geometry/util/sequence.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. namespace detail
  28. {
  29. template <typename Seq, typename ResultSeq = util::type_sequence<>>
  30. struct boost_variant_types;
  31. template <typename T, typename ...Ts, typename ...Rs>
  32. struct boost_variant_types<util::type_sequence<T, Ts...>, util::type_sequence<Rs...>>
  33. {
  34. using type = typename boost_variant_types<util::type_sequence<Ts...>, util::type_sequence<Rs..., T>>::type;
  35. };
  36. template <typename ...Ts, typename ...Rs>
  37. struct boost_variant_types<util::type_sequence<boost::detail::variant::void_, Ts...>, util::type_sequence<Rs...>>
  38. {
  39. using type = util::type_sequence<Rs...>;
  40. };
  41. template <typename ...Rs>
  42. struct boost_variant_types<util::type_sequence<>, util::type_sequence<Rs...>>
  43. {
  44. using type = util::type_sequence<Rs...>;
  45. };
  46. } // namespace detail
  47. // TODO: This is not used anywhere in the header files. Only in tests.
  48. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  49. struct point_type<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  50. : point_type
  51. <
  52. typename util::pack_front
  53. <
  54. BOOST_VARIANT_ENUM_PARAMS(T)
  55. >::type
  56. >
  57. {};
  58. namespace traits
  59. {
  60. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  61. struct tag<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
  62. {
  63. using type = dynamic_geometry_tag;
  64. };
  65. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  66. struct visit<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
  67. {
  68. template <typename Function>
  69. struct visitor
  70. : boost::static_visitor<>
  71. {
  72. visitor(Function function)
  73. : m_function(function)
  74. {}
  75. template <typename Geometry>
  76. void operator()(Geometry && geometry)
  77. {
  78. m_function(std::forward<Geometry>(geometry));
  79. }
  80. Function m_function;
  81. };
  82. template <typename Function, typename Variant>
  83. static void apply(Function function, Variant && variant)
  84. {
  85. visitor<Function> visitor(function);
  86. boost::apply_visitor(visitor, std::forward<Variant>(variant));
  87. }
  88. };
  89. template <BOOST_VARIANT_ENUM_PARAMS(typename T), BOOST_VARIANT_ENUM_PARAMS(typename U)>
  90. struct visit<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, boost::variant<BOOST_VARIANT_ENUM_PARAMS(U)>>
  91. {
  92. template <typename Function>
  93. struct visitor
  94. : boost::static_visitor<>
  95. {
  96. visitor(Function function)
  97. : m_function(function)
  98. {}
  99. template <typename Geometry1, typename Geometry2>
  100. void operator()(Geometry1 && geometry1, Geometry2 && geometry2)
  101. {
  102. m_function(std::forward<Geometry1>(geometry1),
  103. std::forward<Geometry2>(geometry2));
  104. }
  105. Function m_function;
  106. };
  107. template <typename Function, typename Variant1, typename Variant2>
  108. static void apply(Function function, Variant1 && variant1, Variant2 && variant2)
  109. {
  110. visitor<Function> visitor(function);
  111. boost::apply_visitor(visitor,
  112. std::forward<Variant1>(variant1),
  113. std::forward<Variant2>(variant2));
  114. }
  115. };
  116. #ifdef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  117. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  118. struct geometry_types<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
  119. {
  120. using type = typename geometry::detail::boost_variant_types
  121. <
  122. util::type_sequence<BOOST_VARIANT_ENUM_PARAMS(T)>
  123. >::type;
  124. };
  125. #else // BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  126. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  127. struct geometry_types<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
  128. {
  129. using type = util::type_sequence<BOOST_VARIANT_ENUM_PARAMS(T)>;
  130. };
  131. #endif // BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  132. } // namespace traits
  133. }} // namespace boost::geometry
  134. #endif // BOOST_GEOMETRY_GEOMETRIES_ADAPTED_BOOST_VARIANT_HPP