]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/any/basic_any.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / any / basic_any.hpp
1 // Copyright Ruslan Arutyunyan, 2019-2021.
2 // Copyright Antony Polukhin, 2021-2022.
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // Contributed by Ruslan Arutyunyan
9
10 #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
11 #define BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
12
13 #include <boost/config.hpp>
14 #ifdef BOOST_HAS_PRAGMA_ONCE
15 # pragma once
16 #endif
17
18 #include <boost/any/bad_any_cast.hpp>
19 #include <boost/any/fwd.hpp>
20 #include <boost/assert.hpp>
21 #include <boost/aligned_storage.hpp>
22 #include <boost/type_index.hpp>
23 #include <boost/type_traits/remove_reference.hpp>
24 #include <boost/type_traits/decay.hpp>
25 #include <boost/type_traits/remove_cv.hpp>
26 #include <boost/type_traits/add_reference.hpp>
27 #include <boost/type_traits/is_reference.hpp>
28 #include <boost/type_traits/is_const.hpp>
29 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
30 #include <boost/throw_exception.hpp>
31 #include <boost/static_assert.hpp>
32 #include <boost/utility/enable_if.hpp>
33 #include <boost/core/addressof.hpp>
34 #include <boost/type_traits/is_same.hpp>
35 #include <boost/type_traits/conditional.hpp>
36
37 namespace boost {
38
39 namespace anys {
40
41 template <std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
42 class basic_any
43 {
44 BOOST_STATIC_ASSERT_MSG(OptimizeForSize > 0 && OptimizeForAlignment > 0, "Size and Align shall be positive values");
45 BOOST_STATIC_ASSERT_MSG(OptimizeForSize >= OptimizeForAlignment, "Size shall non less than Align");
46 BOOST_STATIC_ASSERT_MSG((OptimizeForAlignment & (OptimizeForAlignment - 1)) == 0, "Align shall be a power of 2");
47 BOOST_STATIC_ASSERT_MSG(OptimizeForSize % OptimizeForAlignment == 0, "Size shall be multiple of alignment");
48 private:
49 enum operation
50 {
51 Destroy,
52 Move,
53 Copy,
54 AnyCast,
55 UnsafeCast,
56 Typeinfo
57 };
58
59 template <typename ValueType>
60 static void* small_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
61 {
62 switch (op)
63 {
64 case Destroy:
65 BOOST_ASSERT(!left.empty());
66 reinterpret_cast<ValueType*>(&left.content.small_value)->~ValueType();
67 break;
68 case Move: {
69 BOOST_ASSERT(left.empty());
70 BOOST_ASSERT(right);
71 BOOST_ASSERT(!right->empty());
72 BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
73 ValueType* value = reinterpret_cast<ValueType*>(&const_cast<basic_any*>(right)->content.small_value);
74 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
75 new (&left.content.small_value) ValueType(std::move(*value));
76 #else
77 new (&left.content.small_value) ValueType(*value);
78 #endif
79 left.man = right->man;
80 reinterpret_cast<ValueType const*>(&right->content.small_value)->~ValueType();
81 const_cast<basic_any*>(right)->man = 0;
82
83 };
84 break;
85
86 case Copy:
87 BOOST_ASSERT(left.empty());
88 BOOST_ASSERT(right);
89 BOOST_ASSERT(!right->empty());
90 BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
91 new (&left.content.small_value) ValueType(*reinterpret_cast<const ValueType*>(&right->content.small_value));
92 left.man = right->man;
93 break;
94 case AnyCast:
95 BOOST_ASSERT(info);
96 BOOST_ASSERT(!left.empty());
97 return boost::typeindex::type_id<ValueType>() == *info ?
98 reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value) : 0;
99 case UnsafeCast:
100 BOOST_ASSERT(!left.empty());
101 return reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value);
102 case Typeinfo:
103 return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
104 }
105
106 return 0;
107 }
108
109 template <typename ValueType>
110 static void* large_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
111 {
112 switch (op)
113 {
114 case Destroy:
115 BOOST_ASSERT(!left.empty());
116 delete static_cast<ValueType*>(left.content.large_value);
117 break;
118 case Move:
119 BOOST_ASSERT(left.empty());
120 BOOST_ASSERT(right);
121 BOOST_ASSERT(!right->empty());
122 BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
123 left.content.large_value = right->content.large_value;
124 left.man = right->man;
125 const_cast<basic_any*>(right)->content.large_value = 0;
126 const_cast<basic_any*>(right)->man = 0;
127 break;
128 case Copy:
129 BOOST_ASSERT(left.empty());
130 BOOST_ASSERT(right);
131 BOOST_ASSERT(!right->empty());
132 BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
133 left.content.large_value = new ValueType(*static_cast<const ValueType*>(right->content.large_value));
134 left.man = right->man;
135 break;
136 case AnyCast:
137 BOOST_ASSERT(info);
138 BOOST_ASSERT(!left.empty());
139 return boost::typeindex::type_id<ValueType>() == *info ?
140 static_cast<typename remove_cv<ValueType>::type *>(left.content.large_value) : 0;
141 case UnsafeCast:
142 BOOST_ASSERT(!left.empty());
143 return reinterpret_cast<typename remove_cv<ValueType>::type *>(left.content.large_value);
144 case Typeinfo:
145 return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
146 }
147
148 return 0;
149 }
150
151 template <typename ValueType>
152 struct is_small_object : boost::integral_constant<bool, sizeof(ValueType) <= OptimizeForSize &&
153 boost::alignment_of<ValueType>::value <= OptimizeForAlignment &&
154 boost::is_nothrow_move_constructible<ValueType>::value>
155 {};
156
157 template <typename ValueType>
158 static void create(basic_any& any, const ValueType& value, boost::true_type)
159 {
160 typedef typename boost::decay<const ValueType>::type DecayedType;
161
162 any.man = &small_manager<DecayedType>;
163 new (&any.content.small_value) ValueType(value);
164 }
165
166 template <typename ValueType>
167 static void create(basic_any& any, const ValueType& value, boost::false_type)
168 {
169 typedef typename boost::decay<const ValueType>::type DecayedType;
170
171 any.man = &large_manager<DecayedType>;
172 any.content.large_value = new DecayedType(value);
173 }
174
175 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
176 template <typename ValueType>
177 static void create(basic_any& any, ValueType&& value, boost::true_type)
178 {
179 typedef typename boost::decay<const ValueType>::type DecayedType;
180 any.man = &small_manager<DecayedType>;
181 new (&any.content.small_value) DecayedType(static_cast<ValueType&&>(value));
182 }
183
184 template <typename ValueType>
185 static void create(basic_any& any, ValueType&& value, boost::false_type)
186 {
187 typedef typename boost::decay<const ValueType>::type DecayedType;
188 any.man = &large_manager<DecayedType>;
189 any.content.large_value = new DecayedType(static_cast<ValueType&&>(value));
190 }
191 #endif
192 public: // non-type template parameters accessors
193 static BOOST_CONSTEXPR_OR_CONST std::size_t buffer_size = OptimizeForSize;
194 static BOOST_CONSTEXPR_OR_CONST std::size_t buffer_align = OptimizeForAlignment;
195
196 public: // structors
197
198 BOOST_CONSTEXPR basic_any() BOOST_NOEXCEPT
199 : man(0), content()
200 {
201 }
202
203 template<typename ValueType>
204 basic_any(const ValueType & value)
205 : man(0), content()
206 {
207 BOOST_STATIC_ASSERT_MSG(
208 !(boost::is_same<ValueType, boost::any>::value),
209 "boost::anys::basic_any shall not be constructed from boost::any"
210 );
211 BOOST_STATIC_ASSERT_MSG(
212 !anys::detail::is_basic_any<ValueType>::value,
213 "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
214 );
215 create(*this, value, is_small_object<ValueType>());
216 }
217
218 basic_any(const basic_any & other)
219 : man(0), content()
220 {
221 if (other.man)
222 {
223 other.man(Copy, *this, &other, 0);
224 }
225 }
226
227 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
228 // Move constructor
229 basic_any(basic_any&& other) BOOST_NOEXCEPT
230 : man(0), content()
231 {
232 if (other.man)
233 {
234 other.man(Move, *this, &other, 0);
235 }
236 }
237
238 // Perfect forwarding of ValueType
239 template<typename ValueType>
240 basic_any(ValueType&& value
241 , typename boost::disable_if<boost::is_same<basic_any&, ValueType> >::type* = 0 // disable if value has type `basic_any&`
242 , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
243 : man(0), content()
244 {
245 typedef typename boost::decay<ValueType>::type DecayedType;
246 BOOST_STATIC_ASSERT_MSG(
247 !(boost::is_same<DecayedType, boost::any>::value),
248 "boost::anys::basic_any shall not be constructed from boost::any"
249 );
250 BOOST_STATIC_ASSERT_MSG(
251 !anys::detail::is_basic_any<DecayedType>::value,
252 "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
253 );
254 create(*this, static_cast<ValueType&&>(value), is_small_object<DecayedType>());
255 }
256 #endif
257
258 ~basic_any() BOOST_NOEXCEPT
259 {
260 if (man)
261 {
262 man(Destroy, *this, 0, 0);
263 }
264 }
265
266 public: // modifiers
267
268 basic_any & swap(basic_any & rhs) BOOST_NOEXCEPT
269 {
270 if (this == &rhs)
271 {
272 return *this;
273 }
274
275 if (man && rhs.man)
276 {
277 basic_any tmp;
278 rhs.man(Move, tmp, &rhs, 0);
279 man(Move, rhs, this, 0);
280 tmp.man(Move, *this, &tmp, 0);
281 }
282 else if (man)
283 {
284 man(Move, rhs, this, 0);
285 }
286 else if (rhs.man)
287 {
288 rhs.man(Move, *this, &rhs, 0);
289 }
290 return *this;
291 }
292
293
294 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
295 template<typename ValueType>
296 basic_any & operator=(const ValueType & rhs)
297 {
298 BOOST_STATIC_ASSERT_MSG(
299 !(boost::is_same<ValueType, boost::any>::value),
300 "boost::any shall not be assigned into boost::anys::basic_any"
301 );
302 BOOST_STATIC_ASSERT_MSG(
303 !anys::detail::is_basic_any<ValueType>::value,
304 "boost::anys::basic_any<A, B> shall not be assigned into boost::anys::basic_any<C, D>"
305 );
306 basic_any(rhs).swap(*this);
307 return *this;
308 }
309
310 basic_any & operator=(basic_any rhs)
311 {
312 rhs.swap(*this);
313 return *this;
314 }
315
316 #else
317 basic_any & operator=(const basic_any& rhs)
318 {
319 basic_any(rhs).swap(*this);
320 return *this;
321 }
322
323 // move assignment
324 basic_any & operator=(basic_any&& rhs) BOOST_NOEXCEPT
325 {
326 rhs.swap(*this);
327 basic_any().swap(rhs);
328 return *this;
329 }
330
331 // Perfect forwarding of ValueType
332 template <class ValueType>
333 basic_any & operator=(ValueType&& rhs)
334 {
335 typedef typename boost::decay<ValueType>::type DecayedType;
336 BOOST_STATIC_ASSERT_MSG(
337 !(boost::is_same<DecayedType, boost::any>::value),
338 "boost::any shall not be assigned into boost::anys::basic_any"
339 );
340 BOOST_STATIC_ASSERT_MSG(
341 (!anys::detail::is_basic_any<DecayedType>::value || boost::is_same<DecayedType, basic_any>::value),
342 "boost::anys::basic_any<A, B> shall not be assigned into boost::anys::basic_any<C, D>"
343 );
344 basic_any(static_cast<ValueType&&>(rhs)).swap(*this);
345 return *this;
346 }
347 #endif
348
349 public: // queries
350
351 bool empty() const BOOST_NOEXCEPT
352 {
353 return !man;
354 }
355
356 void clear() BOOST_NOEXCEPT
357 {
358 basic_any().swap(*this);
359 }
360
361 const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
362 {
363 return man
364 ? *static_cast<const boost::typeindex::type_info*>(man(Typeinfo, const_cast<basic_any&>(*this), 0, 0))
365 : boost::typeindex::type_id<void>().type_info();
366 }
367
368 private: // representation
369
370 template<typename ValueType, std::size_t Size, std::size_t Alignment>
371 friend ValueType * any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
372
373 template<typename ValueType, std::size_t Size, std::size_t Alignment>
374 friend ValueType * unsafe_any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
375
376 typedef void*(*manager)(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info);
377
378 manager man;
379
380 union content {
381 void * large_value;
382 typename boost::aligned_storage<OptimizeForSize, OptimizeForAlignment>::type small_value;
383 } content;
384 };
385
386 template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
387 void swap(basic_any<OptimizeForSize, OptimizeForAlignment>& lhs, basic_any<OptimizeForSize, OptimizeForAlignment>& rhs) BOOST_NOEXCEPT
388 {
389 lhs.swap(rhs);
390 }
391
392 template<typename ValueType, std::size_t Size, std::size_t Alignment>
393 ValueType * any_cast(basic_any<Size, Alignment> * operand) BOOST_NOEXCEPT
394 {
395 return operand->man ?
396 static_cast<typename remove_cv<ValueType>::type *>(operand->man(basic_any<Size, Alignment>::AnyCast, *operand, 0, &boost::typeindex::type_id<ValueType>().type_info()))
397 : 0;
398 }
399
400 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
401 inline const ValueType * any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
402 {
403 return any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
404 }
405
406 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
407 ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
408 {
409 typedef typename remove_reference<ValueType>::type nonref;
410
411 nonref * result = any_cast<nonref>(boost::addressof(operand));
412 if(!result)
413 boost::throw_exception(bad_any_cast());
414
415 // Attempt to avoid construction of a temporary object in cases when
416 // `ValueType` is not a reference. Example:
417 // `static_cast<std::string>(*result);`
418 // which is equal to `std::string(*result);`
419 typedef typename boost::conditional<
420 boost::is_reference<ValueType>::value,
421 ValueType,
422 typename boost::add_reference<ValueType>::type
423 >::type ref_type;
424
425 #ifdef BOOST_MSVC
426 # pragma warning(push)
427 # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
428 #endif
429 return static_cast<ref_type>(*result);
430 #ifdef BOOST_MSVC
431 # pragma warning(pop)
432 #endif
433 }
434
435 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
436 inline ValueType any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
437 {
438 typedef typename remove_reference<ValueType>::type nonref;
439 return any_cast<const nonref &>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> &>(operand));
440 }
441
442 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
443 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
444 inline ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment>&& operand)
445 {
446 BOOST_STATIC_ASSERT_MSG(
447 boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
448 || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
449 "boost::any_cast shall not be used for getting nonconst references to temporary objects"
450 );
451 return any_cast<ValueType>(operand);
452 }
453 #endif
454
455
456 // Note: The "unsafe" versions of any_cast are not part of the
457 // public interface and may be removed at any time. They are
458 // required where we know what type is stored in the any and can't
459 // use typeid() comparison, e.g., when our types may travel across
460 // different shared libraries.
461 template<typename ValueType, std::size_t OptimizedForSize, std::size_t OptimizeForAlignment>
462 inline ValueType * unsafe_any_cast(basic_any<OptimizedForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
463 {
464 return static_cast<ValueType*>(operand->man(basic_any<OptimizedForSize, OptimizeForAlignment>::UnsafeCast, *operand, 0, 0));
465 }
466
467 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
468 inline const ValueType * unsafe_any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
469 {
470 return unsafe_any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
471 }
472
473 } // namespace anys
474
475 using boost::anys::any_cast;
476 using boost::anys::unsafe_any_cast;
477
478 } // namespace boost
479
480 #endif // #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED