Blender V5.3
BLI_any.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
16
17#include <algorithm>
18#include <utility>
19
20#include "BLI_memory_utils.hh"
21
22namespace blender {
23
24namespace blenlib_detail {
25
30template<typename ExtraInfo> struct AnyTypeInfo {
31 /* The pointers are allowed to be null, which means that the implementation is trivial. */
32 void (*copy_construct)(void *dst, const void *src);
33 void (*move_construct)(void *dst, void *src);
34 void (*destruct)(void *src);
35 const void *(*get)(const void *src);
36 ExtraInfo extra_info;
37};
38
42template<typename ExtraInfo, typename T>
45 nullptr :
46 +[](void *dst, const void *src) { new (dst) T(*static_cast<const T *>(src)); },
48 nullptr :
49 +[](void *dst, void *src) { new (dst) T(std::move(*static_cast<T *>(src))); },
51 nullptr :
52 +[](void *src) { std::destroy_at((static_cast<T *>(src))); },
53 nullptr,
54 ExtraInfo::template get<T>()};
55
60template<typename T> using Ptr = std::unique_ptr<T>;
61template<typename ExtraInfo, typename T>
63 [](void *dst, const void *src) {
64 new (dst) Ptr<T>(new T(**static_cast<const Ptr<T> *>(src)));
65 },
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>()};
70
75 template<typename T> static constexpr NoExtraInfo get()
76 {
77 return {};
78 }
79};
80
81struct EmptyType {};
82
83} // namespace blenlib_detail
84
85template<
91 typename ExtraInfo = void,
96 size_t InlineBufferCapacity = 8,
101 size_t Alignment = 8,
106 typename ExtraData = blenlib_detail::EmptyType>
107class Any {
108 private:
109 /* Makes it possible to use void in the template parameters. */
110 using RealExtraInfo =
111 std::conditional_t<std::is_void_v<ExtraInfo>, blenlib_detail::NoExtraInfo, ExtraInfo>;
113 static constexpr size_t RealInlineBufferCapacity = std::max(InlineBufferCapacity,
114 sizeof(std::unique_ptr<int>));
115
121
122 public:
125
126 private:
131 const Info *info_ = nullptr;
132
133 public:
135 template<typename T> static constexpr bool is_allowed_v = std::is_copy_constructible_v<T>;
136
141 template<typename T>
142 static constexpr bool is_inline_v = std::is_nothrow_move_constructible_v<T> &&
143 sizeof(T) <= InlineBufferCapacity && alignof(T) <= Alignment;
144
149 template<typename T> static constexpr bool is_same_any_v = std::is_same_v<std::decay_t<T>, Any>;
150
151 private:
152 template<typename T> const Info &get_info() const
153 {
154 using DecayT = std::decay_t<T>;
155 static_assert(is_allowed_v<DecayT>);
156 if constexpr (is_inline_v<DecayT>) {
157 return blenlib_detail::template info_for_inline<RealExtraInfo, DecayT>;
158 }
159 else {
160 return blenlib_detail::template info_for_unique_ptr<RealExtraInfo, DecayT>;
161 }
162 }
163
164 public:
165 Any() = default;
166
167 Any(const Any &other) : extra(other.extra), info_(other.info_)
168 {
169 if (info_ != nullptr) {
170 if (info_->copy_construct != nullptr) {
171 info_->copy_construct(&buffer_, &other.buffer_);
172 }
173 else {
174 std::copy_n(static_cast<const std::byte *>(other.buffer_.ptr()),
175 RealInlineBufferCapacity,
176 static_cast<std::byte *>(buffer_.ptr()));
177 }
178 }
179 }
180
185 Any(Any &&other) noexcept : extra(std::move(other.extra)), info_(other.info_)
186 {
187 if (info_ != nullptr) {
188 if (info_->move_construct != nullptr) {
189 info_->move_construct(&buffer_, &other.buffer_);
190 }
191 else {
192 std::copy_n(static_cast<const std::byte *>(other.buffer_.ptr()),
193 RealInlineBufferCapacity,
194 static_cast<std::byte *>(buffer_.ptr()));
195 }
196 }
197 }
198
203 template<typename T, typename... Args>
204 explicit Any(std::in_place_type_t<T> /*tag*/, Args &&...args)
205 {
206 this->emplace_on_empty<T>(std::forward<Args>(args)...);
207 }
208
212 template<typename T>
213 Any(T &&value)
214 requires(!is_same_any_v<T>)
215 : Any(std::in_place_type<T>, std::forward<T>(value))
216 {
217 }
218
220 {
221 if (info_ != nullptr) {
222 if (info_->destruct != nullptr) {
223 info_->destruct(&buffer_);
224 }
225 }
226 }
227
231 Any &operator=(const Any &other)
232 {
233 if (this == &other) {
234 return *this;
235 }
236 this->~Any();
237 new (this) Any(other);
238 return *this;
239 }
240
242 template<typename T> Any &operator=(T &&other)
243 {
244 if constexpr (is_same_any_v<T>) {
245 if (this == &other) {
246 return *this;
247 }
248 }
249 this->~Any();
250 new (this) Any(std::forward<T>(other));
251 return *this;
252 }
253
255 void reset()
256 {
257 if (info_ != nullptr) {
258 if (info_->destruct != nullptr) {
259 info_->destruct(&buffer_);
260 }
261 }
262 info_ = nullptr;
263 }
264
265 operator bool() const
266 {
267 return this->has_value();
268 }
269
270 bool has_value() const
271 {
272 return info_ != nullptr;
273 }
274
275 template<typename T, typename... Args> std::decay_t<T> &emplace(Args &&...args)
276 {
277 this->~Any();
278 new (this) Any(std::in_place_type<T>, std::forward<Args>(args)...);
279 return this->get<T>();
280 }
281
282 template<typename T, typename... Args> std::decay_t<T> &emplace_on_empty(Args &&...args)
283 {
284 BLI_assert(!this->has_value());
285 using DecayT = std::decay_t<T>;
286 static_assert(is_allowed_v<DecayT>);
287 info_ = &this->template get_info<DecayT>();
288 if constexpr (is_inline_v<DecayT>) {
289 /* Construct the value directly in the inline buffer. */
290 DecayT *stored_value = new (&buffer_) DecayT(std::forward<Args>(args)...);
291 return *stored_value;
292 }
293 else {
294 /* Construct the value in a new allocation and store a #std::unique_ptr to it in the inline
295 * buffer. */
296 std::unique_ptr<DecayT> *stored_value = new (&buffer_)
297 std::unique_ptr<DecayT>(new DecayT(std::forward<Args>(args)...));
298 return **stored_value;
299 }
300 }
301
306 template<typename T> void *allocate()
307 {
308 this->reset();
309 return this->allocate_on_empty<T>();
310 }
311
316 template<typename T> void *allocate_on_empty()
317 {
318 BLI_assert(!this->has_value());
319 static_assert(is_allowed_v<T>);
320 info_ = &this->template get_info<T>();
321 if constexpr (is_inline_v<T>) {
322 return buffer_.ptr();
323 }
324 else {
325 /* Using raw allocation here. The caller is responsible for constructing the value. */
326 T *value = static_cast<T *>(::operator new(sizeof(T)));
327 new (&buffer_) std::unique_ptr<T>(value);
328 return value;
329 }
330 }
331
333 template<typename T> bool is() const
334 {
335 return info_ == &this->template get_info<T>();
336 }
337
339 void *get()
340 {
341 BLI_assert(info_ != nullptr);
342 if (info_->get != nullptr) {
343 return const_cast<void *>(info_->get(&buffer_));
344 }
345 return &buffer_;
346 }
347
349 const void *get() const
350 {
351 BLI_assert(info_ != nullptr);
352 if (info_->get != nullptr) {
353 return info_->get(&buffer_);
354 }
355 return &buffer_;
356 }
357
362 template<typename T> T &get()
363 {
364 /* Use const-cast to be able to reuse the const method above. */
365 return const_cast<T &>(const_cast<const Any *>(this)->get<T>());
366 }
367
372 template<typename T> const T &get() const
373 {
374 BLI_assert(this->is<T>());
375 const void *buffer;
376 /* Can avoid the `info_->get == nullptr` check because the result is known statically. */
377 if constexpr (is_inline_v<T>) {
378 buffer = &buffer_;
379 }
380 else {
381 BLI_assert(info_->get != nullptr);
382 buffer = info_->get(&buffer_);
383 }
384 return *static_cast<const T *>(buffer);
385 }
386
390 const RealExtraInfo &extra_info() const
391 {
392 BLI_assert(info_ != nullptr);
393 return info_->extra_info;
394 }
395};
396
397} // namespace blender
#define BLI_NO_UNIQUE_ADDRESS
Any & operator=(const Any &other)
Definition BLI_any.hh:231
Any(const Any &other)
Definition BLI_any.hh:167
const void * get() const
Definition BLI_any.hh:349
std::decay_t< T > & emplace_on_empty(Args &&...args)
Definition BLI_any.hh:282
void reset()
Definition BLI_any.hh:255
Any(std::in_place_type_t< T >, Args &&...args)
Definition BLI_any.hh:204
const T & get() const
Definition BLI_any.hh:372
const RealExtraInfo & extra_info() const
Definition BLI_any.hh:390
bool is() const
Definition BLI_any.hh:333
Any()=default
BLI_NO_UNIQUE_ADDRESS blenlib_detail::EmptyType extra
Definition BLI_any.hh:124
Any(T &&value)
Definition BLI_any.hh:213
void * allocate()
Definition BLI_any.hh:306
std::decay_t< T > & emplace(Args &&...args)
Definition BLI_any.hh:275
Any & operator=(T &&other)
Definition BLI_any.hh:242
Any(Any &&other) noexcept
Definition BLI_any.hh:185
#define T
AnyTypeInfo< ExtraInfo > info_for_unique_ptr
Definition BLI_any.hh:62
AnyTypeInfo< ExtraInfo > info_for_inline
Definition BLI_any.hh:43
std::unique_ptr< T > Ptr
Definition BLI_any.hh:60
BMesh Iterators.
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)
Definition BLI_any.hh:32
void(*) move_construct(void *dst, void *src)
Definition BLI_any.hh:33
static constexpr NoExtraInfo get()
Definition BLI_any.hh:75