1 // Copyright Ruslan Arutyunyan, 2019-2021.
2 // Copyright Antony Polukhin, 2021-2022.
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)
8 // Contributed by Ruslan Arutyunyan
10 #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
11 #define BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
13 #include <boost/config.hpp>
14 #ifdef BOOST_HAS_PRAGMA_ONCE
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>
41 template <std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
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");
59 template <typename ValueType>
60 static void* small_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
65 BOOST_ASSERT(!left.empty());
66 reinterpret_cast<ValueType*>(&left.content.small_value)->~ValueType();
69 BOOST_ASSERT(left.empty());
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));
77 new (&left.content.small_value) ValueType(*value);
79 left.man = right->man;
80 reinterpret_cast<ValueType const*>(&right->content.small_value)->~ValueType();
81 const_cast<basic_any*>(right)->man = 0;
87 BOOST_ASSERT(left.empty());
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;
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;
100 BOOST_ASSERT(!left.empty());
101 return reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value);
103 return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
109 template <typename ValueType>
110 static void* large_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
115 BOOST_ASSERT(!left.empty());
116 delete static_cast<ValueType*>(left.content.large_value);
119 BOOST_ASSERT(left.empty());
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;
129 BOOST_ASSERT(left.empty());
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;
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;
142 BOOST_ASSERT(!left.empty());
143 return reinterpret_cast<typename remove_cv<ValueType>::type *>(left.content.large_value);
145 return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
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>
157 template <typename ValueType>
158 static void create(basic_any& any, const ValueType& value, boost::true_type)
160 typedef typename boost::decay<const ValueType>::type DecayedType;
162 any.man = &small_manager<DecayedType>;
163 new (&any.content.small_value) ValueType(value);
166 template <typename ValueType>
167 static void create(basic_any& any, const ValueType& value, boost::false_type)
169 typedef typename boost::decay<const ValueType>::type DecayedType;
171 any.man = &large_manager<DecayedType>;
172 any.content.large_value = new DecayedType(value);
175 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
176 template <typename ValueType>
177 static void create(basic_any& any, ValueType&& value, boost::true_type)
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));
184 template <typename ValueType>
185 static void create(basic_any& any, ValueType&& value, boost::false_type)
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));
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;
198 BOOST_CONSTEXPR basic_any() BOOST_NOEXCEPT
203 template<typename ValueType>
204 basic_any(const ValueType & value)
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"
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>"
215 create(*this, value, is_small_object<ValueType>());
218 basic_any(const basic_any & other)
223 other.man(Copy, *this, &other, 0);
227 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
229 basic_any(basic_any&& other) BOOST_NOEXCEPT
234 other.man(Move, *this, &other, 0);
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&&`
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"
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>"
254 create(*this, static_cast<ValueType&&>(value), is_small_object<DecayedType>());
258 ~basic_any() BOOST_NOEXCEPT
262 man(Destroy, *this, 0, 0);
268 basic_any & swap(basic_any & rhs) BOOST_NOEXCEPT
278 rhs.man(Move, tmp, &rhs, 0);
279 man(Move, rhs, this, 0);
280 tmp.man(Move, *this, &tmp, 0);
284 man(Move, rhs, this, 0);
288 rhs.man(Move, *this, &rhs, 0);
294 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
295 template<typename ValueType>
296 basic_any & operator=(const ValueType & rhs)
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"
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>"
306 basic_any(rhs).swap(*this);
310 basic_any & operator=(basic_any rhs)
317 basic_any & operator=(const basic_any& rhs)
319 basic_any(rhs).swap(*this);
324 basic_any & operator=(basic_any&& rhs) BOOST_NOEXCEPT
327 basic_any().swap(rhs);
331 // Perfect forwarding of ValueType
332 template <class ValueType>
333 basic_any & operator=(ValueType&& rhs)
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"
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>"
344 basic_any(static_cast<ValueType&&>(rhs)).swap(*this);
351 bool empty() const BOOST_NOEXCEPT
356 void clear() BOOST_NOEXCEPT
358 basic_any().swap(*this);
361 const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
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();
368 private: // representation
370 template<typename ValueType, std::size_t Size, std::size_t Alignment>
371 friend ValueType * any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
373 template<typename ValueType, std::size_t Size, std::size_t Alignment>
374 friend ValueType * unsafe_any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
376 typedef void*(*manager)(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info);
382 typename boost::aligned_storage<OptimizeForSize, OptimizeForAlignment>::type small_value;
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
392 template<typename ValueType, std::size_t Size, std::size_t Alignment>
393 ValueType * any_cast(basic_any<Size, Alignment> * operand) BOOST_NOEXCEPT
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()))
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
403 return any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
406 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
407 ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
409 typedef typename remove_reference<ValueType>::type nonref;
411 nonref * result = any_cast<nonref>(boost::addressof(operand));
413 boost::throw_exception(bad_any_cast());
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,
422 typename boost::add_reference<ValueType>::type
426 # pragma warning(push)
427 # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
429 return static_cast<ref_type>(*result);
431 # pragma warning(pop)
435 template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
436 inline ValueType any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
438 typedef typename remove_reference<ValueType>::type nonref;
439 return any_cast<const nonref &>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> &>(operand));
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)
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"
451 return any_cast<ValueType>(operand);
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
464 return static_cast<ValueType*>(operand->man(basic_any<OptimizedForSize, OptimizeForAlignment>::UnsafeCast, *operand, 0, 0));
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
470 return unsafe_any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
475 using boost::anys::any_cast;
476 using boost::anys::unsafe_any_cast;
480 #endif // #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED