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)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 #ifndef BOOST_POLY_COLLECTION_DETAIL_ANY_MODEL_HPP
10 #define BOOST_POLY_COLLECTION_DETAIL_ANY_MODEL_HPP
16 #include <boost/core/addressof.hpp>
17 #include <boost/mpl/map/map10.hpp>
18 #include <boost/mpl/pair.hpp>
19 #include <boost/mpl/vector/vector10.hpp>
20 #include <boost/poly_collection/detail/any_iterator.hpp>
21 #include <boost/poly_collection/detail/is_acceptable.hpp>
22 #include <boost/poly_collection/detail/segment_backend.hpp>
23 #include <boost/poly_collection/detail/split_segment.hpp>
24 #include <boost/type_erasure/any.hpp>
25 #include <boost/type_erasure/any_cast.hpp>
26 #include <boost/type_erasure/binding.hpp>
27 #include <boost/type_erasure/builtin.hpp>
28 #include <boost/type_erasure/concept_of.hpp>
29 #include <boost/type_erasure/is_subconcept.hpp>
30 #include <boost/type_erasure/relaxed.hpp>
31 #include <boost/type_erasure/static_binding.hpp>
32 #include <boost/type_erasure/typeid_of.hpp>
34 #include <type_traits>
40 namespace poly_collection{
44 /* model for any_collection */
46 template<typename Concept>
49 /* Refine is_acceptable to cover type_erasure::any classes whose assignment
50 * operator won't compile.
53 template<typename Concept,typename Concept2,typename T>
55 type_erasure::any<Concept2,T>,any_model<Concept>,
56 typename std::enable_if<
57 !type_erasure::is_relaxed<Concept2>::value&&
58 !type_erasure::is_subconcept<type_erasure::assignable<>,Concept2>::value&&
59 !type_erasure::is_subconcept<
60 type_erasure::assignable<type_erasure::_self,type_erasure::_self&&>,
65 /* is_terminal defined out-class to allow for partial specialization */
67 template<typename Concept,typename T>
68 using any_model_enable_if_has_typeid_=typename std::enable_if<
69 type_erasure::is_subconcept<
70 type_erasure::typeid_<typename std::decay<T>::type>,
75 template<typename T,typename=void*>
76 struct any_model_is_terminal:std::true_type{};
78 template<typename Concept,typename T>
79 struct any_model_is_terminal<
80 type_erasure::any<Concept,T>,any_model_enable_if_has_typeid_<Concept,T>
83 /* used for make_value_type */
85 template<typename T,typename Q>
86 struct any_model_make_reference
88 static T& apply(Q& x){return x;}
91 template<typename Concept>
94 using value_type=type_erasure::any<
95 typename std::conditional<
96 type_erasure::is_subconcept<type_erasure::typeid_<>,Concept>::value,
98 mpl::vector2<Concept,type_erasure::typeid_<>>
103 template<typename Concrete>
104 using is_implementation=std::true_type; /* can't compile-time check concept
107 using is_terminal=any_model_is_terminal<T>;
110 static const std::type_info& subtypeid(const T&){return typeid(T);}
113 typename Concept2,typename T,
114 any_model_enable_if_has_typeid_<Concept2,T> =nullptr
116 static const std::type_info& subtypeid(
117 const type_erasure::any<Concept2,T>& a)
119 return type_erasure::typeid_of(a);
123 static void* subaddress(T& x){return boost::addressof(x);}
126 static const void* subaddress(const T& x){return boost::addressof(x);}
129 typename Concept2,typename T,
130 any_model_enable_if_has_typeid_<Concept2,T> =nullptr
132 static void* subaddress(type_erasure::any<Concept2,T>& a)
134 return type_erasure::any_cast<void*>(&a);
138 typename Concept2,typename T,
139 any_model_enable_if_has_typeid_<Concept2,T> =nullptr
141 static const void* subaddress(const type_erasure::any<Concept2,T>& a)
143 return type_erasure::any_cast<const void*>(&a);
146 using base_iterator=any_iterator<value_type>;
147 using const_base_iterator=any_iterator<const value_type>;
148 using base_sentinel=value_type*;
149 using const_base_sentinel=const value_type*;
150 template<typename Concrete>
151 using iterator=Concrete*;
152 template<typename Concrete>
153 using const_iterator=const Concrete*;
154 template<typename Allocator>
155 using segment_backend=detail::segment_backend<any_model,Allocator>;
156 template<typename Concrete,typename Allocator>
157 using segment_backend_implementation=
158 split_segment<any_model,Concrete,Allocator>;
160 static base_iterator nonconst_iterator(const_base_iterator it)
162 return base_iterator{
163 const_cast<value_type*>(static_cast<const value_type*>(it))};
167 static iterator<T> nonconst_iterator(const_iterator<T> it)
169 return const_cast<iterator<T>>(it);
173 template<typename,typename,typename>
174 friend class split_segment;
176 template<typename Concrete>
177 static value_type make_value_type(Concrete& x){return value_type{x};}
179 template<typename Concept2,typename T>
180 static value_type make_value_type(type_erasure::any<Concept2,T>& x)
182 /* I don't pretend to understand what's going on here, see
183 * https://lists.boost.org/boost-users/2017/05/87556.php
186 using namespace boost::type_erasure;
187 using ref_type=any<Concept2,T>;
188 using make_ref=any_model_make_reference<_self,ref_type>;
189 using concept_=typename concept_of<value_type>::type;
191 auto b=make_binding<mpl::map1<mpl::pair<_self,ref_type>>>();
193 return {call(binding<make_ref>{b},make_ref{},x),binding<concept_>{b}};
197 } /* namespace poly_collection::detail */
199 } /* namespace poly_collection */
201 } /* namespace boost */