variant_model.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright 2024 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_VARIANT_MODEL_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_VARIANT_MODEL_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #if defined(_MSC_VER)
  14. #pragma once
  15. #endif
  16. #include <boost/config.hpp>
  17. #include <boost/core/addressof.hpp>
  18. #include <boost/mp11/algorithm.hpp>
  19. #include <boost/mp11/list.hpp>
  20. #include <boost/mp11/set.hpp>
  21. #include <boost/poly_collection/detail/is_acceptable.hpp>
  22. #include <boost/poly_collection/detail/fixed_variant.hpp>
  23. #include <boost/poly_collection/detail/fixed_variant_iterator.hpp>
  24. #include <boost/poly_collection/detail/packed_segment.hpp>
  25. #include <boost/poly_collection/detail/segment_backend.hpp>
  26. #include <memory>
  27. #include <type_traits>
  28. #include <typeinfo>
  29. #include <utility>
  30. #if !defined(BOOST_NO_CXX17_HDR_VARIANT)
  31. #include <variant>
  32. #endif
  33. namespace boost{namespace variant2{
  34. template<class... T> class variant;
  35. }} /* namespace boost::variant2 */
  36. namespace boost_poly_collection_invoke_visit_variant2{
  37. /* defined here to avoid ADL ambiguities with visit */
  38. template<typename F,typename... Ts>
  39. auto invoke_visit(F&& f,const boost::variant2::variant<Ts...>&x)->
  40. decltype(visit(std::forward<F>(f),x))
  41. {
  42. return visit(std::forward<F>(f),x);
  43. }
  44. } /* namespace boost_poly_collection_invoke_visit_variant2 */
  45. namespace boost{
  46. namespace poly_collection{
  47. namespace detail{
  48. /* model for variant_collection */
  49. template<typename... Ts>
  50. struct variant_model;
  51. template<typename V,typename...Ts>
  52. struct variant_model_is_subvariant:std::false_type{};
  53. template<
  54. typename TL1,typename TL2,
  55. typename TS1=mp11::mp_set_union<mp11::mp_list<>,TL1>,
  56. typename TS2=mp11::mp_set_union<mp11::mp_list<>,TL2>
  57. >
  58. struct variant_model_is_subset:std::integral_constant<
  59. bool,
  60. mp11::mp_size<mp11::mp_set_union<TS1,TS2>>::value==
  61. mp11::mp_size<TS2>::value
  62. >{};
  63. template<typename... Us,typename... Ts>
  64. struct variant_model_is_subvariant<
  65. fixed_variant_impl::fixed_variant<Us...>,
  66. Ts...
  67. >:variant_model_is_subset<mp11::mp_list<Us...>,mp11::mp_list<Ts...>>{};
  68. template<typename... Us,typename... Ts>
  69. struct variant_model_is_subvariant<
  70. variant2::variant<Us...>,
  71. Ts...
  72. >:variant_model_is_subset<mp11::mp_list<Us...>,mp11::mp_list<Ts...>>{};
  73. #if !defined(BOOST_NO_CXX17_HDR_VARIANT)
  74. template<typename... Us,typename... Ts>
  75. struct variant_model_is_subvariant<
  76. std::variant<Us...>,
  77. Ts...
  78. >:variant_model_is_subset<mp11::mp_list<Us...>,mp11::mp_list<Ts...>>{};
  79. #endif
  80. template<typename F,typename... Ts>
  81. auto invoke_visit(F&& f,const fixed_variant_impl::fixed_variant<Ts...>&x)->
  82. decltype(boost::poly_collection::visit(std::forward<F>(f),x))
  83. {
  84. return boost::poly_collection::visit(std::forward<F>(f),x);
  85. }
  86. using boost_poly_collection_invoke_visit_variant2::invoke_visit;
  87. #if !defined(BOOST_NO_CXX17_HDR_VARIANT)
  88. template<typename F,typename... Ts>
  89. auto invoke_visit(F&& f,const std::variant<Ts...>&x)->
  90. decltype(std::visit(std::forward<F>(f),x))
  91. {
  92. return std::visit(std::forward<F>(f),x);
  93. }
  94. #endif
  95. template<typename... Ts>
  96. struct variant_model
  97. {
  98. using value_type=fixed_variant_impl::fixed_variant<Ts...>;
  99. using type_index=std::size_t;
  100. using acceptable_type_list=mp11::mp_list<Ts...>;
  101. template<typename T>
  102. struct is_terminal: /* using makes VS2015 choke, hence we derive */
  103. mp11::mp_contains<acceptable_type_list,T>{};
  104. template<typename T>
  105. struct is_implementation:std::integral_constant< /* idem */
  106. bool,
  107. is_terminal<T>::value||
  108. variant_model_is_subvariant<T,Ts...>::value
  109. >{};
  110. private:
  111. template<typename T>
  112. using enable_if_terminal=
  113. typename std::enable_if<is_terminal<T>::value>::type*;
  114. template<typename T>
  115. using enable_if_not_terminal=
  116. typename std::enable_if<!is_terminal<T>::value>::type*;
  117. public:
  118. template<typename T> static type_index index()
  119. {
  120. return mp11::mp_find<acceptable_type_list,T>::value;
  121. }
  122. template<typename T,enable_if_terminal<T> =nullptr>
  123. static type_index subindex(const T&){return index<T>();}
  124. private:
  125. template<typename... Qs>
  126. struct subindex_lambda
  127. {
  128. template<typename I>
  129. std::size_t operator()(I)const
  130. {
  131. return mp11::mp_find<
  132. acceptable_type_list,mp11::mp_at<mp11::mp_list<Qs...>,I>>::value;
  133. }
  134. };
  135. public:
  136. template<
  137. template<typename...> class V,typename... Qs,
  138. enable_if_not_terminal<V<Qs...>> =nullptr>
  139. static type_index subindex(const V<Qs...>& x)
  140. {
  141. static constexpr auto not_found=mp11::mp_size<acceptable_type_list>::value;
  142. auto i=x.index();
  143. if(i>=sizeof...(Qs))return not_found;
  144. else return mp11::mp_with_index<sizeof...(Qs)>(
  145. i,subindex_lambda<Qs...>{});
  146. }
  147. template<typename T,enable_if_terminal<T> =nullptr>
  148. static void* subaddress(T& x){return boost::addressof(x);}
  149. template<typename T,enable_if_terminal<T> =nullptr>
  150. static const void* subaddress(const T& x){return boost::addressof(x);}
  151. template<typename T,enable_if_not_terminal<T> =nullptr>
  152. static void* subaddress(T& x)
  153. {
  154. return const_cast<void*>(subaddress(const_cast<const T&>(x)));
  155. }
  156. private:
  157. struct subaddress_visitor
  158. {
  159. template<typename T>
  160. const void* operator()(const T& x)const
  161. {
  162. return static_cast<const void*>(boost::addressof(x));
  163. }
  164. };
  165. public:
  166. template<typename T,enable_if_not_terminal<T> =nullptr>
  167. static const void* subaddress(const T& x)
  168. {
  169. return invoke_visit(subaddress_visitor{},x);
  170. }
  171. template<typename T,enable_if_terminal<T> =nullptr>
  172. static const std::type_info& subtype_info(const T& x)
  173. {
  174. return typeid(x);
  175. }
  176. private:
  177. struct subtype_info_visitor
  178. {
  179. template<typename T>
  180. const std::type_info& operator()(const T&)const{return typeid(T);}
  181. };
  182. public:
  183. template<typename T,enable_if_not_terminal<T> =nullptr>
  184. static const std::type_info& subtype_info(const T& x)
  185. {
  186. return invoke_visit(subtype_info_visitor{},x);
  187. }
  188. using base_iterator=fixed_variant_iterator<value_type>;
  189. using const_base_iterator=fixed_variant_iterator<const value_type>;
  190. using base_sentinel=value_type*;
  191. using const_base_sentinel=const value_type*;
  192. template<typename T>
  193. using iterator=fixed_variant_alternative_iterator<value_type,T>;
  194. template<typename T>
  195. using const_iterator=fixed_variant_alternative_iterator<value_type,const T>;
  196. template<typename Allocator>
  197. using segment_backend=detail::segment_backend<variant_model,Allocator>;
  198. template<typename T,typename Allocator>
  199. using segment_backend_implementation=
  200. packed_segment<variant_model,T,Allocator>;
  201. static base_iterator nonconst_iterator(const_base_iterator it)
  202. {
  203. return base_iterator{
  204. const_cast<value_type*>(static_cast<const value_type*>(it)),it.stride()};
  205. }
  206. template<typename T>
  207. static iterator<T> nonconst_iterator(const_iterator<T> it)
  208. {
  209. return {const_cast<T*>(static_cast<const T*>(it))};
  210. }
  211. private:
  212. template<typename,typename,typename>
  213. friend class packed_segment;
  214. template<typename T>
  215. using final_type=fixed_variant_impl::fixed_variant_closure<
  216. T,fixed_variant_impl::fixed_variant<Ts...>>;
  217. template<typename T>
  218. static const value_type* value_ptr(const T* p)noexcept
  219. {
  220. return reinterpret_cast<const final_type<T>*>(p);
  221. }
  222. };
  223. } /* namespace poly_collection::detail */
  224. } /* namespace poly_collection */
  225. } /* namespace boost */
  226. #endif