]>
Commit | Line | Data |
---|---|---|
20effc67 | 1 | /* Copyright 2016-2020 Joaquin M Lopez Munoz. |
b32b8144 FG |
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 | ||
9 | #ifndef BOOST_POLY_COLLECTION_DETAIL_ANY_MODEL_HPP | |
10 | #define BOOST_POLY_COLLECTION_DETAIL_ANY_MODEL_HPP | |
11 | ||
12 | #if defined(_MSC_VER) | |
13 | #pragma once | |
14 | #endif | |
15 | ||
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> | |
33 | #include <memory> | |
34 | #include <type_traits> | |
35 | #include <typeinfo> | |
36 | #include <utility> | |
37 | ||
38 | namespace boost{ | |
39 | ||
40 | namespace poly_collection{ | |
41 | ||
42 | namespace detail{ | |
43 | ||
44 | /* model for any_collection */ | |
45 | ||
46 | template<typename Concept> | |
47 | struct any_model; | |
48 | ||
49 | /* Refine is_acceptable to cover type_erasure::any classes whose assignment | |
50 | * operator won't compile. | |
51 | */ | |
52 | ||
53 | template<typename Concept,typename Concept2,typename T> | |
54 | struct is_acceptable< | |
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&&>, | |
61 | Concept2>::value | |
62 | >::type | |
63 | >:std::false_type{}; | |
64 | ||
65 | /* is_terminal defined out-class to allow for partial specialization */ | |
66 | ||
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>, | |
71 | Concept | |
72 | >::value | |
73 | >::type*; | |
74 | ||
75 | template<typename T,typename=void*> | |
76 | struct any_model_is_terminal:std::true_type{}; | |
77 | ||
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> | |
81 | >:std::false_type{}; | |
82 | ||
83 | /* used for make_value_type */ | |
84 | ||
85 | template<typename T,typename Q> | |
86 | struct any_model_make_reference | |
87 | { | |
88 | static T& apply(Q& x){return x;} | |
89 | }; | |
90 | ||
91 | template<typename Concept> | |
92 | struct any_model | |
93 | { | |
94 | using value_type=type_erasure::any< | |
95 | typename std::conditional< | |
96 | type_erasure::is_subconcept<type_erasure::typeid_<>,Concept>::value, | |
97 | Concept, | |
98 | mpl::vector2<Concept,type_erasure::typeid_<>> | |
99 | >::type, | |
100 | type_erasure::_self& | |
101 | >; | |
102 | ||
103 | template<typename Concrete> | |
104 | using is_implementation=std::true_type; /* can't compile-time check concept | |
105 | * compliance */ | |
106 | template<typename T> | |
107 | using is_terminal=any_model_is_terminal<T>; | |
108 | ||
109 | template<typename T> | |
110 | static const std::type_info& subtypeid(const T&){return typeid(T);} | |
111 | ||
112 | template< | |
113 | typename Concept2,typename T, | |
114 | any_model_enable_if_has_typeid_<Concept2,T> =nullptr | |
115 | > | |
116 | static const std::type_info& subtypeid( | |
117 | const type_erasure::any<Concept2,T>& a) | |
118 | { | |
119 | return type_erasure::typeid_of(a); | |
120 | } | |
121 | ||
122 | template<typename T> | |
123 | static void* subaddress(T& x){return boost::addressof(x);} | |
124 | ||
125 | template<typename T> | |
126 | static const void* subaddress(const T& x){return boost::addressof(x);} | |
127 | ||
128 | template< | |
129 | typename Concept2,typename T, | |
130 | any_model_enable_if_has_typeid_<Concept2,T> =nullptr | |
131 | > | |
132 | static void* subaddress(type_erasure::any<Concept2,T>& a) | |
133 | { | |
134 | return type_erasure::any_cast<void*>(&a); | |
135 | } | |
136 | ||
137 | template< | |
138 | typename Concept2,typename T, | |
139 | any_model_enable_if_has_typeid_<Concept2,T> =nullptr | |
140 | > | |
141 | static const void* subaddress(const type_erasure::any<Concept2,T>& a) | |
142 | { | |
143 | return type_erasure::any_cast<const void*>(&a); | |
144 | } | |
145 | ||
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*; | |
92f5a8d4 TL |
154 | template<typename Allocator> |
155 | using segment_backend=detail::segment_backend<any_model,Allocator>; | |
b32b8144 | 156 | template<typename Concrete,typename Allocator> |
92f5a8d4 TL |
157 | using segment_backend_implementation= |
158 | split_segment<any_model,Concrete,Allocator>; | |
b32b8144 FG |
159 | |
160 | static base_iterator nonconst_iterator(const_base_iterator it) | |
161 | { | |
162 | return base_iterator{ | |
163 | const_cast<value_type*>(static_cast<const value_type*>(it))}; | |
164 | } | |
165 | ||
166 | template<typename T> | |
167 | static iterator<T> nonconst_iterator(const_iterator<T> it) | |
168 | { | |
169 | return const_cast<iterator<T>>(it); | |
170 | } | |
171 | ||
b32b8144 FG |
172 | private: |
173 | template<typename,typename,typename> | |
174 | friend class split_segment; | |
175 | ||
176 | template<typename Concrete> | |
177 | static value_type make_value_type(Concrete& x){return value_type{x};} | |
178 | ||
179 | template<typename Concept2,typename T> | |
180 | static value_type make_value_type(type_erasure::any<Concept2,T>& x) | |
181 | { | |
182 | /* I don't pretend to understand what's going on here, see | |
183 | * https://lists.boost.org/boost-users/2017/05/87556.php | |
184 | */ | |
185 | ||
20effc67 TL |
186 | using ref_type=type_erasure::any<Concept2,T>; |
187 | using make_ref=any_model_make_reference<type_erasure::_self,ref_type>; | |
188 | using concept_=typename type_erasure::concept_of<value_type>::type; | |
b32b8144 | 189 | |
20effc67 TL |
190 | auto b=type_erasure::make_binding< |
191 | mpl::map1<mpl::pair<type_erasure::_self,ref_type>>>(); | |
b32b8144 | 192 | |
20effc67 TL |
193 | return { |
194 | type_erasure::call(type_erasure::binding<make_ref>{b},make_ref{},x), | |
195 | type_erasure::binding<concept_>{b} | |
196 | }; | |
b32b8144 FG |
197 | } |
198 | }; | |
199 | ||
200 | } /* namespace poly_collection::detail */ | |
201 | ||
202 | } /* namespace poly_collection */ | |
203 | ||
204 | } /* namespace boost */ | |
205 | ||
206 | #endif |