any_model.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright 2016-2018 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_ANY_MODEL_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_ANY_MODEL_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/core/addressof.hpp>
  14. #include <boost/mpl/map/map10.hpp>
  15. #include <boost/mpl/pair.hpp>
  16. #include <boost/mpl/vector/vector10.hpp>
  17. #include <boost/poly_collection/detail/any_iterator.hpp>
  18. #include <boost/poly_collection/detail/is_acceptable.hpp>
  19. #include <boost/poly_collection/detail/segment_backend.hpp>
  20. #include <boost/poly_collection/detail/split_segment.hpp>
  21. #include <boost/type_erasure/any.hpp>
  22. #include <boost/type_erasure/any_cast.hpp>
  23. #include <boost/type_erasure/binding.hpp>
  24. #include <boost/type_erasure/builtin.hpp>
  25. #include <boost/type_erasure/concept_of.hpp>
  26. #include <boost/type_erasure/is_subconcept.hpp>
  27. #include <boost/type_erasure/relaxed.hpp>
  28. #include <boost/type_erasure/static_binding.hpp>
  29. #include <boost/type_erasure/typeid_of.hpp>
  30. #include <memory>
  31. #include <type_traits>
  32. #include <typeinfo>
  33. #include <utility>
  34. namespace boost{
  35. namespace poly_collection{
  36. namespace detail{
  37. /* model for any_collection */
  38. template<typename Concept>
  39. struct any_model;
  40. /* Refine is_acceptable to cover type_erasure::any classes whose assignment
  41. * operator won't compile.
  42. */
  43. template<typename Concept,typename Concept2,typename T>
  44. struct is_acceptable<
  45. type_erasure::any<Concept2,T>,any_model<Concept>,
  46. typename std::enable_if<
  47. !type_erasure::is_relaxed<Concept2>::value&&
  48. !type_erasure::is_subconcept<type_erasure::assignable<>,Concept2>::value&&
  49. !type_erasure::is_subconcept<
  50. type_erasure::assignable<type_erasure::_self,type_erasure::_self&&>,
  51. Concept2>::value
  52. >::type
  53. >:std::false_type{};
  54. /* is_terminal defined out-class to allow for partial specialization */
  55. template<typename Concept,typename T>
  56. using any_model_enable_if_has_typeid_=typename std::enable_if<
  57. type_erasure::is_subconcept<
  58. type_erasure::typeid_<typename std::decay<T>::type>,
  59. Concept
  60. >::value
  61. >::type*;
  62. template<typename T,typename=void*>
  63. struct any_model_is_terminal:std::true_type{};
  64. template<typename Concept,typename T>
  65. struct any_model_is_terminal<
  66. type_erasure::any<Concept,T>,any_model_enable_if_has_typeid_<Concept,T>
  67. >:std::false_type{};
  68. /* used for make_value_type */
  69. template<typename T,typename Q>
  70. struct any_model_make_reference
  71. {
  72. static T& apply(Q& x){return x;}
  73. };
  74. template<typename Concept>
  75. struct any_model
  76. {
  77. using value_type=type_erasure::any<
  78. typename std::conditional<
  79. type_erasure::is_subconcept<type_erasure::typeid_<>,Concept>::value,
  80. Concept,
  81. mpl::vector2<Concept,type_erasure::typeid_<>>
  82. >::type,
  83. type_erasure::_self&
  84. >;
  85. template<typename Concrete>
  86. using is_implementation=std::true_type; /* can't compile-time check concept
  87. * compliance */
  88. template<typename T>
  89. using is_terminal=any_model_is_terminal<T>;
  90. template<typename T>
  91. static const std::type_info& subtypeid(const T&){return typeid(T);}
  92. template<
  93. typename Concept2,typename T,
  94. any_model_enable_if_has_typeid_<Concept2,T> =nullptr
  95. >
  96. static const std::type_info& subtypeid(
  97. const type_erasure::any<Concept2,T>& a)
  98. {
  99. return type_erasure::typeid_of(a);
  100. }
  101. template<typename T>
  102. static void* subaddress(T& x){return boost::addressof(x);}
  103. template<typename T>
  104. static const void* subaddress(const T& x){return boost::addressof(x);}
  105. template<
  106. typename Concept2,typename T,
  107. any_model_enable_if_has_typeid_<Concept2,T> =nullptr
  108. >
  109. static void* subaddress(type_erasure::any<Concept2,T>& a)
  110. {
  111. return type_erasure::any_cast<void*>(&a);
  112. }
  113. template<
  114. typename Concept2,typename T,
  115. any_model_enable_if_has_typeid_<Concept2,T> =nullptr
  116. >
  117. static const void* subaddress(const type_erasure::any<Concept2,T>& a)
  118. {
  119. return type_erasure::any_cast<const void*>(&a);
  120. }
  121. using base_iterator=any_iterator<value_type>;
  122. using const_base_iterator=any_iterator<const value_type>;
  123. using base_sentinel=value_type*;
  124. using const_base_sentinel=const value_type*;
  125. template<typename Concrete>
  126. using iterator=Concrete*;
  127. template<typename Concrete>
  128. using const_iterator=const Concrete*;
  129. template<typename Allocator>
  130. using segment_backend=detail::segment_backend<any_model,Allocator>;
  131. template<typename Concrete,typename Allocator>
  132. using segment_backend_implementation=
  133. split_segment<any_model,Concrete,Allocator>;
  134. static base_iterator nonconst_iterator(const_base_iterator it)
  135. {
  136. return base_iterator{
  137. const_cast<value_type*>(static_cast<const value_type*>(it))};
  138. }
  139. template<typename T>
  140. static iterator<T> nonconst_iterator(const_iterator<T> it)
  141. {
  142. return const_cast<iterator<T>>(it);
  143. }
  144. private:
  145. template<typename,typename,typename>
  146. friend class split_segment;
  147. template<typename Concrete>
  148. static value_type make_value_type(Concrete& x){return value_type{x};}
  149. template<typename Concept2,typename T>
  150. static value_type make_value_type(type_erasure::any<Concept2,T>& x)
  151. {
  152. /* I don't pretend to understand what's going on here, see
  153. * https://lists.boost.org/boost-users/2017/05/87556.php
  154. */
  155. using namespace boost::type_erasure;
  156. using ref_type=any<Concept2,T>;
  157. using make_ref=any_model_make_reference<_self,ref_type>;
  158. using concept_=typename concept_of<value_type>::type;
  159. auto b=make_binding<mpl::map1<mpl::pair<_self,ref_type>>>();
  160. return {call(binding<make_ref>{b},make_ref{},x),binding<concept_>{b}};
  161. }
  162. };
  163. } /* namespace poly_collection::detail */
  164. } /* namespace poly_collection */
  165. } /* namespace boost */
  166. #endif