50 rhs.vtable->copy(rhs.storage, this->storage);
61 rhs.vtable->move(rhs.storage, this->storage);
76 template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type,
any>::value>
::type>
77 any(ValueType&& value)
79 static_assert(std::is_copy_constructible<typename std::decay<ValueType>::type>::value,
80 "T shall satisfy the CopyConstructible requirements.");
81 this->construct(std::forward<ValueType>(value));
97 any(std::move(rhs)).
swap(*
this);
105 template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type,
any>::value>
::type>
108 static_assert(std::is_copy_constructible<typename std::decay<ValueType>::type>::value,
109 "T shall satisfy the CopyConstructible requirements.");
110 any(std::forward<ValueType>(value)).
swap(*
this);
119 this->vtable->destroy(storage);
120 this->vtable =
nullptr;
127 return this->vtable ==
nullptr;
131 const std::type_info&
type() const noexcept
133 return empty()?
typeid(void) : this->vtable->type();
139 if(this->vtable != rhs.vtable)
141 any tmp(std::move(rhs));
144 rhs.vtable = this->vtable;
145 if(this->vtable !=
nullptr)
147 this->vtable->move(this->storage, rhs.storage);
152 this->vtable = tmp.vtable;
153 if(tmp.vtable !=
nullptr)
155 tmp.vtable->move(tmp.storage, this->storage);
156 tmp.vtable =
nullptr;
161 if(this->vtable !=
nullptr)
162 this->vtable->swap(this->storage, rhs.storage);
170 using stack_storage_t =
typename std::aligned_storage<2 *
sizeof(
void*), std::alignment_of<void*>::value>
::type;
173 stack_storage_t stack;
183 const std::type_info& (*type)()
noexcept;
187 void(*destroy)(storage_union&)
noexcept;
191 void(*copy)(
const storage_union& src, storage_union& dest);
195 void(*move)(storage_union& src, storage_union& dest)
noexcept;
198 void(*swap)(storage_union& lhs, storage_union& rhs)
noexcept;
203 struct vtable_dynamic
205 static const std::type_info& type() noexcept
210 static void destroy(storage_union& storage)
noexcept
213 delete reinterpret_cast<T*
>(storage.dynamic);
216 static void copy(
const storage_union& src, storage_union& dest)
218 dest.dynamic =
new T(*
reinterpret_cast<const T*
>(src.dynamic));
221 static void move(storage_union& src, storage_union& dest)
noexcept
223 dest.dynamic = src.dynamic;
224 src.dynamic =
nullptr;
227 static void swap(storage_union& lhs, storage_union& rhs)
noexcept
230 std::swap(lhs.dynamic, rhs.dynamic);
238 static const std::type_info& type() noexcept
243 static void destroy(storage_union& storage)
noexcept
245 reinterpret_cast<T*
>(&storage.stack)->~T();
248 static void copy(
const storage_union& src, storage_union& dest)
250 new (&dest.stack) T(
reinterpret_cast<const T&
>(src.stack));
253 static void move(storage_union& src, storage_union& dest)
noexcept
257 new (&dest.stack) T(std::move(
reinterpret_cast<T&
>(src.stack)));
261 static void swap(storage_union& lhs, storage_union& rhs)
noexcept
263 std::swap(
reinterpret_cast<T&
>(lhs.stack),
reinterpret_cast<T&
>(rhs.stack));
269 struct requires_allocation :
270 std::integral_constant<bool,
271 !(std::is_nothrow_move_constructible<T>::value
272 && sizeof(T) <= sizeof(storage_union::stack)
273 && std::alignment_of<T>::value <= std::alignment_of<storage_union::stack_storage_t>::value)>
278 static vtable_type* vtable_for_type()
280 using VTableType = typename std::conditional<requires_allocation<T>::value, vtable_dynamic<T>, vtable_stack<T>>::type;
281 static vtable_type table = {
282 VTableType::type, VTableType::destroy,
283 VTableType::copy, VTableType::move,
291 friend const T* any_cast(const any* operand) noexcept;
293 friend T* any_cast(any* operand) noexcept;
296 bool is_typed(const std::type_info& t) const
298 return is_same(this->type(), t);
307 static bool is_same(const std::type_info& a, const std::type_info& b)
309#ifdef ANY_IMPL_FAST_TYPE_INFO_COMPARE
318 const T* cast() const noexcept
320 return requires_allocation<typename std::decay<T>::type>::value?
321 reinterpret_cast<const T*>(storage.dynamic) :
322 reinterpret_cast<const T*>(&storage.stack);
329 return requires_allocation<typename std::decay<T>::type>::value?
330 reinterpret_cast<T*>(storage.dynamic) :
331 reinterpret_cast<T*>(&storage.stack);
335 storage_union storage;
338 template<typename ValueType, typename T>
339 typename std::enable_if<requires_allocation<T>::value>::type
340 do_construct(ValueType&& value)
342 storage.dynamic = new T(std::forward<ValueType>(value));
345 template<typename ValueType, typename T>
346 typename std::enable_if<!requires_allocation<T>::value>::type
347 do_construct(ValueType&& value)
349 new (&storage.stack) T(std::forward<ValueType>(value));
354 template<typename ValueType>
355 void construct(ValueType&& value)
357 using T = typename std::decay<ValueType>::type;
359 this->vtable = vtable_for_type<T>();
361 do_construct<ValueType,T>(std::forward<ValueType>(value));