34 const void *(*get)(
const void *
src);
41 template<
typename ExtraInfo,
typename T>
43 is_trivially_copy_constructible_extended_v<T> ?
45 +[](
void *dst,
const void *
src) {
new (dst)
T(*(
const T *)
src); },
46 is_trivially_move_constructible_extended_v<T> ?
48 +[](
void *dst,
void *
src) {
new (dst)
T(std::move(*(
T *)
src)); },
49 is_trivially_destructible_extended_v<T> ? nullptr :
50 +[](
void *
src) { std::destroy_at(((
T *)
src)); },
52 ExtraInfo::template get<T>()};
58 template<
typename T>
using Ptr = std::unique_ptr<T>;
59 template<
typename ExtraInfo,
typename T>
64 [](
const void *
src) ->
const void * {
return &**(
const Ptr<T> *)
src; },
65 ExtraInfo::template get<T>()};
85 typename ExtraInfo =
void,
90 size_t InlineBufferCapacity = 8,
102 static constexpr
size_t RealInlineBufferCapacity =
std::max(InlineBufferCapacity,
103 sizeof(std::unique_ptr<int>));
115 const Info *info_ =
nullptr;
119 template<
typename T>
static constexpr
inline bool is_allowed_v = std::is_copy_constructible_v<T>;
126 static constexpr
inline bool is_inline_v = std::is_nothrow_move_constructible_v<T> &&
127 sizeof(
T) <= InlineBufferCapacity &&
128 alignof(
T) <= Alignment;
138 template<
typename T>
const Info &get_info()
const
140 using DecayT = std::decay_t<T>;
141 static_assert(is_allowed_v<DecayT>);
142 if constexpr (is_inline_v<DecayT>) {
143 return detail::template info_for_inline<RealExtraInfo, DecayT>;
146 return detail::template info_for_unique_ptr<RealExtraInfo, DecayT>;
153 Any(
const Any &other) : info_(other.info_)
155 if (info_ !=
nullptr) {
160 memcpy(&buffer_, &other.buffer_, RealInlineBufferCapacity);
169 Any(
Any &&other) noexcept : info_(other.info_)
171 if (info_ !=
nullptr) {
176 memcpy(&buffer_, &other.buffer_, RealInlineBufferCapacity);
185 template<
typename T,
typename... Args>
explicit Any(std::in_place_type_t<T>, Args &&...args)
187 this->emplace_on_empty<T>(std::forward<Args>(args)...);
193 template<
typename T, BLI_ENABLE_IF((!is_same_any_v<T>))>
200 if (info_ !=
nullptr) {
212 if (
this == &other) {
216 new (
this)
Any(other);
223 if constexpr (is_same_any_v<T>) {
224 if (
this == &other) {
229 new (
this)
Any(std::forward<T>(other));
236 if (info_ !=
nullptr) {
251 return info_ !=
nullptr;
254 template<
typename T,
typename... Args> std::decay_t<T> &
emplace(Args &&...args)
257 new (
this)
Any(std::in_place_type<T>, std::forward<Args>(args)...);
258 return this->get<T>();
264 using DecayT = std::decay_t<T>;
265 static_assert(is_allowed_v<DecayT>);
266 info_ = &this->
template get_info<DecayT>();
267 if constexpr (is_inline_v<DecayT>) {
269 DecayT *stored_value =
new (&buffer_) DecayT(std::forward<Args>(args)...);
270 return *stored_value;
275 std::unique_ptr<DecayT> *stored_value =
new (&buffer_)
276 std::unique_ptr<DecayT>(
new DecayT(std::forward<Args>(args)...));
277 return **stored_value;
282 template<
typename T>
bool is()
const
284 return info_ == &this->
template get_info<T>();
291 if (info_->
get !=
nullptr) {
292 return const_cast<void *
>(info_->
get(&buffer_));
301 if (info_->
get !=
nullptr) {
302 return info_->
get(&buffer_);
311 template<
typename T> std::decay_t<T> &
get()
314 return *
static_cast<std::decay_t<T> *
>(this->
get());
321 template<
typename T>
const std::decay_t<T> &
get()
const
324 return *
static_cast<const std::decay_t<T> *
>(this->
get());
const std::decay_t< T > & get() const
static constexpr bool is_same_any_v
Any(std::in_place_type_t< T >, Args &&...args)
std::decay_t< T > & emplace_on_empty(Args &&...args)
static constexpr bool is_inline_v
Any(Any &&other) noexcept
std::decay_t< T > & emplace(Args &&...args)
Any & operator=(const Any &other)
static constexpr bool is_allowed_v
const RealExtraInfo & extra_info() const
std::decay_t< T > & get()
Any & operator=(T &&other)
SyclQueue void void * src
SyclQueue void void size_t num_bytes void
static constexpr AnyTypeInfo< ExtraInfo > info_for_inline
static constexpr AnyTypeInfo< ExtraInfo > info_for_unique_ptr
void(* move_construct)(void *dst, void *src)
void(* destruct)(void *src)
const void *(* get)(const void *src)
void(* copy_construct)(void *dst, const void *src)