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) {
new (dst)
Ptr<T>(
new T(**(
const Ptr<T> *)src)); },
64 [](
void *dst,
void *src) {
new (dst)
Ptr<T>(
new T(std::move(**(
Ptr<T> *)src))); },
65 [](
void *src) { std::destroy_at((
Ptr<T> *)src); },
66 [](
const void *src) ->
const void * {
return &**(
const Ptr<T> *)src; },
67 ExtraInfo::template get<T>()};
87 typename ExtraInfo = void,
92 size_t InlineBufferCapacity = 8,
101 using RealExtraInfo =
104 static constexpr size_t RealInlineBufferCapacity = std::max(InlineBufferCapacity,
105 sizeof(std::unique_ptr<int>));
117 const Info *info_ =
nullptr;
121 template<
typename T>
static constexpr inline bool is_allowed_v = std::is_copy_constructible_v<T>;
128 static constexpr inline bool is_inline_v = std::is_nothrow_move_constructible_v<T> &&
129 sizeof(
T) <= InlineBufferCapacity &&
130 alignof(T) <= Alignment;
140 template<
typename T>
const Info &get_info()
const
142 using DecayT = std::decay_t<T>;
145 return detail::template info_for_inline<RealExtraInfo, DecayT>;
148 return detail::template info_for_unique_ptr<RealExtraInfo, DecayT>;
155 Any(
const Any &other) : info_(other.info_)
157 if (info_ !=
nullptr) {
162 std::copy_n(
static_cast<const std::byte *
>(other.buffer_.ptr()),
163 RealInlineBufferCapacity,
164 static_cast<std::byte *
>(buffer_.
ptr()));
173 Any(
Any &&other) noexcept : info_(other.info_)
175 if (info_ !=
nullptr) {
180 std::copy_n(
static_cast<const std::byte *
>(other.buffer_.ptr()),
181 RealInlineBufferCapacity,
182 static_cast<std::byte *
>(buffer_.
ptr()));
191 template<
typename T,
typename... Args>
explicit Any(std::in_place_type_t<T>, Args &&...args)
199 template<
typename T, BLI_ENABLE_IF((!is_same_any_v<T>))>
200 Any(T &&value) :
Any(std::in_place_type<T>, std::forward<T>(value))
206 if (info_ !=
nullptr) {
218 if (
this == &other) {
222 new (
this)
Any(other);
230 if (
this == &other) {
235 new (
this)
Any(std::forward<T>(other));
242 if (info_ !=
nullptr) {
250 operator bool()
const
257 return info_ !=
nullptr;
260 template<
typename T,
typename... Args> std::decay_t<T> &
emplace(Args &&...args)
263 new (
this)
Any(std::in_place_type<T>, std::forward<Args>(args)...);
270 using DecayT = std::decay_t<T>;
272 info_ = &this->
template get_info<DecayT>();
275 DecayT *stored_value =
new (&buffer_) DecayT(std::forward<Args>(args)...);
276 return *stored_value;
281 std::unique_ptr<DecayT> *stored_value =
new (&buffer_)
282 std::unique_ptr<DecayT>(
new DecayT(std::forward<Args>(args)...));
283 return **stored_value;
305 info_ = &this->
template get_info<T>();
307 return buffer_.
ptr();
311 T *value =
static_cast<T *
>(::operator
new(
sizeof(
T)));
312 new (&buffer_) std::unique_ptr<T>(value);
318 template<
typename T>
bool is()
const
320 return info_ == &this->
template get_info<T>();
327 if (info_->
get !=
nullptr) {
328 return const_cast<void *
>(info_->
get(&buffer_));
337 if (info_->
get !=
nullptr) {
338 return info_->
get(&buffer_);
347 template<
typename T> T &
get()
350 return const_cast<T &
>(
const_cast<const Any *
>(
this)->
get<T>());
357 template<
typename T>
const T &
get()
const
367 buffer = info_->
get(&buffer_);
369 return *
static_cast<const T *
>(buffer);
static constexpr bool is_same_any_v
Any(std::in_place_type_t< T >, Args &&...args)
Any & operator=(T &&other)
std::decay_t< T > & emplace(Args &&...args)
std::decay_t< T > & emplace_on_empty(Args &&...args)
const RealExtraInfo & extra_info() const
static constexpr bool is_inline_v
void * allocate_on_empty()
Any(Any &&other) noexcept
static constexpr bool is_allowed_v
Any & operator=(const Any &other)
constexpr AnyTypeInfo< ExtraInfo > info_for_inline
constexpr AnyTypeInfo< ExtraInfo > info_for_unique_ptr
constexpr bool is_trivially_move_constructible_extended_v
constexpr bool is_trivially_destructible_extended_v
constexpr bool is_trivially_copy_constructible_extended_v
void(* move_construct)(void *dst, void *src)
void(* destruct)(void *src)
void(* copy_construct)(void *dst, const void *src)
const void *(* get)(const void *src)