35 const void *(*get)(
const void *src);
42template<
typename ExtraInfo,
typename T>
46 +[](
void *dst,
const void *src) {
new (dst)
T(*
static_cast<const T *
>(src)); },
49 +[](
void *dst,
void *src) {
new (dst)
T(std::move(*
static_cast<T *
>(src))); },
52 +[](
void *src) { std::destroy_at((
static_cast<T *
>(src))); },
54 ExtraInfo::template get<T>()};
60template<
typename T>
using Ptr = std::unique_ptr<T>;
61template<
typename ExtraInfo,
typename T>
63 [](
void *dst,
const void *src) {
66 [](
void *dst,
void *src) {
new (dst)
Ptr<T>(
new T(std::move(**
static_cast<Ptr<T> *
>(src)))); },
67 [](
void *src) { std::destroy_at(
static_cast<Ptr<T> *
>(src)); },
68 [](
const void *src) ->
const void * {
return &**
static_cast<const Ptr<T> *
>(src); },
69 ExtraInfo::template get<T>()};
91 typename ExtraInfo = void,
96 size_t InlineBufferCapacity = 8,
101 size_t Alignment = 8,
110 using RealExtraInfo =
113 static constexpr size_t RealInlineBufferCapacity = std::max(InlineBufferCapacity,
114 sizeof(std::unique_ptr<int>));
131 const Info *info_ =
nullptr;
135 template<
typename T>
static constexpr bool is_allowed_v = std::is_copy_constructible_v<T>;
142 static constexpr bool is_inline_v = std::is_nothrow_move_constructible_v<T> &&
143 sizeof(
T) <= InlineBufferCapacity &&
alignof(
T) <= Alignment;
149 template<
typename T>
static constexpr bool is_same_any_v = std::is_same_v<std::decay_t<T>,
Any>;
152 template<
typename T>
const Info &get_info()
const
154 using DecayT = std::decay_t<T>;
157 return blenlib_detail::template info_for_inline<RealExtraInfo, DecayT>;
160 return blenlib_detail::template info_for_unique_ptr<RealExtraInfo, DecayT>;
169 if (info_ !=
nullptr) {
170 if (info_->copy_construct !=
nullptr) {
171 info_->copy_construct(&buffer_, &other.buffer_);
174 std::copy_n(
static_cast<const std::byte *
>(other.buffer_.ptr()),
175 RealInlineBufferCapacity,
176 static_cast<std::byte *
>(buffer_.ptr()));
185 Any(
Any &&other) noexcept :
extra(std::move(other.extra)), info_(other.info_)
187 if (info_ !=
nullptr) {
188 if (info_->move_construct !=
nullptr) {
189 info_->move_construct(&buffer_, &other.buffer_);
192 std::copy_n(
static_cast<const std::byte *
>(other.buffer_.ptr()),
193 RealInlineBufferCapacity,
194 static_cast<std::byte *
>(buffer_.ptr()));
203 template<
typename T,
typename... Args>
204 explicit Any(std::in_place_type_t<T> , Args &&...args)
215 :
Any(std::in_place_type<
T>, std::forward<
T>(value))
221 if (info_ !=
nullptr) {
222 if (info_->destruct !=
nullptr) {
223 info_->destruct(&buffer_);
233 if (
this == &other) {
237 new (
this)
Any(other);
245 if (
this == &other) {
250 new (
this)
Any(std::forward<T>(other));
257 if (info_ !=
nullptr) {
258 if (info_->destruct !=
nullptr) {
259 info_->destruct(&buffer_);
265 operator bool()
const
272 return info_ !=
nullptr;
275 template<
typename T,
typename... Args> std::decay_t<T> &
emplace(Args &&...args)
278 new (
this)
Any(std::in_place_type<T>, std::forward<Args>(args)...);
285 using DecayT = std::decay_t<T>;
287 info_ = &this->
template get_info<DecayT>();
290 DecayT *stored_value =
new (&buffer_) DecayT(std::forward<Args>(args)...);
291 return *stored_value;
296 std::unique_ptr<DecayT> *stored_value =
new (&buffer_)
297 std::unique_ptr<DecayT>(
new DecayT(std::forward<Args>(args)...));
298 return **stored_value;
320 info_ = &this->
template get_info<T>();
322 return buffer_.ptr();
326 T *value =
static_cast<T *
>(::operator
new(
sizeof(
T)));
327 new (&buffer_) std::unique_ptr<T>(value);
333 template<
typename T>
bool is()
const
335 return info_ == &this->
template get_info<T>();
342 if (info_->get !=
nullptr) {
343 return const_cast<void *
>(info_->get(&buffer_));
352 if (info_->get !=
nullptr) {
353 return info_->get(&buffer_);
365 return const_cast<T &
>(
const_cast<const Any *
>(
this)->get<
T>());
372 template<
typename T>
const T &
get()
const
382 buffer = info_->get(&buffer_);
384 return *
static_cast<const T *
>(buffer);
393 return info_->extra_info;
#define BLI_NO_UNIQUE_ADDRESS
Any & operator=(const Any &other)
std::decay_t< T > & emplace_on_empty(Args &&...args)
static constexpr bool is_same_any_v
Any(std::in_place_type_t< T >, Args &&...args)
const RealExtraInfo & extra_info() const
static constexpr bool is_allowed_v
void * allocate_on_empty()
BLI_NO_UNIQUE_ADDRESS blenlib_detail::EmptyType extra
std::decay_t< T > & emplace(Args &&...args)
Any & operator=(T &&other)
static constexpr bool is_inline_v
Any(Any &&other) noexcept
AnyTypeInfo< ExtraInfo > info_for_unique_ptr
AnyTypeInfo< ExtraInfo > info_for_inline
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
constexpr bool is_trivially_move_constructible_extended_v
constexpr bool is_trivially_destructible_extended_v
constexpr bool is_trivially_copy_constructible_extended_v
void(*) copy_construct(void *dst, const void *src)
void(*) move_construct(void *dst, void *src)
void(*) destruct(void *src)