Blender V5.3
BLI_cpp_type_make.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
10
11#include <atomic>
12#include <sstream>
13
14#include "BLI_cpp_type.hh"
15#include "BLI_index_mask.hh"
16#include "BLI_unique_hash.hh"
17#include "BLI_utildefines.hh"
18
19namespace blender {
20
21namespace cpp_type_util {
22
23template<typename T> inline bool pointer_has_valid_alignment(const void *ptr)
24{
25 return (uintptr_t(ptr) % alignof(T)) == 0;
26}
27
28template<typename T> inline bool pointer_can_point_to_instance(const void *ptr)
29{
30 return ptr != nullptr && pointer_has_valid_alignment<T>(ptr);
31}
32
33template<typename T> void default_construct_cb(void *ptr)
34{
36 new (ptr) T;
37}
38template<typename T> void default_construct_indices_cb(void *ptr, const IndexMask &mask)
39{
41 if constexpr (std::is_trivially_constructible_v<T>) {
42 return;
43 }
44 mask.foreach_index_optimized<int64_t>([&](int64_t i) { new (static_cast<T *>(ptr) + i) T; });
45}
46template<typename T> void default_construct_n_cb(void *ptr, const int64_t n)
47{
49}
50
51template<typename T> void value_initialize_cb(void *ptr)
52{
54 new (ptr) T();
55}
56template<typename T> void value_initialize_indices_cb(void *ptr, const IndexMask &mask)
57{
59 mask.foreach_index_optimized<int64_t>([&](int64_t i) { new (static_cast<T *>(ptr) + i) T(); });
60}
61template<typename T> void value_initialize_n_cb(void *ptr, const int64_t n)
62{
64}
65
66template<typename T> void destruct_cb(void *ptr)
67{
69 (static_cast<T *>(ptr))->~T();
70}
71template<typename T> void destruct_indices_cb(void *ptr, const IndexMask &mask)
72{
74 if (std::is_trivially_destructible_v<T>) {
75 return;
76 }
77 T *ptr_ = static_cast<T *>(ptr);
78 mask.foreach_index_optimized<int64_t>([&](int64_t i) { ptr_[i].~T(); });
79}
80template<typename T> void destruct_n_cb(void *ptr, const int64_t n)
81{
83}
84
85template<typename T> void copy_assign_cb(const void *src, void *dst)
86{
89 *static_cast<T *>(dst) = *static_cast<const T *>(src);
90}
91template<typename T> void copy_assign_indices_cb(const void *src, void *dst, const IndexMask &mask)
92{
93 BLI_assert(mask.size() == 0 || src != dst);
96 if constexpr (std::is_trivially_copy_assignable_v<T>) {
97 index_mask::detail::copy_assign(static_cast<const T *>(src), mask, static_cast<T *>(dst));
98 }
99 else {
100 const T *src_ = static_cast<const T *>(src);
101 T *dst_ = static_cast<T *>(dst);
102 mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; });
103 }
104}
105template<typename T> void copy_assign_n_cb(const void *src, void *dst, const int64_t n)
106{
108}
109template<typename T>
110void copy_assign_compressed_cb(const void *src, void *dst, const IndexMask &mask)
111{
112 BLI_assert(mask.size() == 0 || src != dst);
115 if constexpr (std::is_trivially_copy_assignable_v<T>) {
116 index_mask::detail::gather_assign(static_cast<const T *>(src), mask, static_cast<T *>(dst));
117 }
118 else {
119 const T *src_ = static_cast<const T *>(src);
120 T *dst_ = static_cast<T *>(dst);
121 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst_[pos] = src_[i]; });
122 }
123}
124
125template<typename T> void copy_construct_cb(const void *src, void *dst)
126{
127 BLI_assert(src != dst || std::is_trivially_copy_constructible_v<T>);
130 uninitialized_copy_n(static_cast<const T *>(src), 1, static_cast<T *>(dst));
131}
132template<typename T>
133void copy_construct_indices_cb(const void *src, void *dst, const IndexMask &mask)
134{
135 BLI_assert(mask.size() == 0 || src != dst);
138 const T *src_ = static_cast<const T *>(src);
139 T *dst_ = static_cast<T *>(dst);
140
141 mask.foreach_index_optimized<int64_t>([&](int64_t i) { new (dst_ + i) T(src_[i]); });
142}
143template<typename T> void copy_construct_n_cb(const void *src, void *dst, const int64_t n)
144{
146}
147template<typename T>
148void copy_construct_compressed_cb(const void *src, void *dst, const IndexMask &mask)
149{
150 BLI_assert(mask.size() == 0 || src != dst);
153 const T *src_ = static_cast<const T *>(src);
154 T *dst_ = static_cast<T *>(dst);
155
156 mask.foreach_index_optimized<int64_t>(
157 [&](const int64_t i, const int64_t pos) { new (dst_ + pos) T(src_[i]); });
158}
159
160template<typename T> void move_assign_cb(void *src, void *dst)
161{
164 initialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
165}
166template<typename T> void move_assign_indices_cb(void *src, void *dst, const IndexMask &mask)
167{
168 BLI_assert(mask.size() == 0 || src != dst);
171 T *src_ = static_cast<T *>(src);
172 T *dst_ = static_cast<T *>(dst);
173
174 mask.foreach_index_optimized<int64_t>([&](int64_t i) { dst_[i] = std::move(src_[i]); });
175}
176template<typename T> void move_assign_n_cb(void *src, void *dst, const int64_t n)
177{
179}
180
181template<typename T> void move_construct_cb(void *src, void *dst)
182{
183 BLI_assert(src != dst || std::is_trivially_move_constructible_v<T>);
186
187 uninitialized_move_n(static_cast<T *>(src), 1, static_cast<T *>(dst));
188}
189template<typename T> void move_construct_indices_cb(void *src, void *dst, const IndexMask &mask)
190{
191 BLI_assert(mask.size() == 0 || src != dst);
194 T *src_ = static_cast<T *>(src);
195 T *dst_ = static_cast<T *>(dst);
196
197 mask.foreach_index_optimized<int64_t>([&](int64_t i) { new (dst_ + i) T(std::move(src_[i])); });
198}
199template<typename T> void move_construct_n_cb(void *src, void *dst, const int64_t n)
200{
202}
203
204template<typename T> void relocate_assign_cb(void *src, void *dst)
205{
206 BLI_assert(src != dst || std::is_trivially_move_constructible_v<T>);
209 T *src_ = static_cast<T *>(src);
210 T *dst_ = static_cast<T *>(dst);
211
212 *dst_ = std::move(*src_);
213 src_->~T();
214}
215template<typename T> void relocate_assign_indices_cb(void *src, void *dst, const IndexMask &mask)
216{
217 BLI_assert(mask.size() == 0 || src != dst);
220 T *src_ = static_cast<T *>(src);
221 T *dst_ = static_cast<T *>(dst);
222
223 mask.foreach_index_optimized<int64_t>([&](int64_t i) {
224 dst_[i] = std::move(src_[i]);
225 src_[i].~T();
226 });
227}
228template<typename T> void relocate_assign_n_cb(void *src, void *dst, const int64_t n)
229{
231}
232
233template<typename T> void relocate_construct_cb(void *src, void *dst)
234{
235 BLI_assert(src != dst || std::is_trivially_move_constructible_v<T>);
238 T *src_ = static_cast<T *>(src);
239 T *dst_ = static_cast<T *>(dst);
240
241 new (dst_) T(std::move(*src_));
242 src_->~T();
243}
244template<typename T>
245void relocate_construct_indices_cb(void *src, void *dst, const IndexMask &mask)
246{
247 BLI_assert(mask.size() == 0 || src != dst);
250 T *src_ = static_cast<T *>(src);
251 T *dst_ = static_cast<T *>(dst);
252
253 mask.foreach_index_optimized<int64_t>([&](int64_t i) {
254 new (dst_ + i) T(std::move(src_[i]));
255 src_[i].~T();
256 });
257}
258template<typename T> void relocate_construct_n_cb(void *src, void *dst, const int64_t n)
259{
261}
262
263template<typename T>
264void fill_assign_indices_cb(const void *value, void *dst, const IndexMask &mask)
265{
266 BLI_assert(mask.size() == 0 || pointer_can_point_to_instance<T>(value));
268 if constexpr (std::is_trivially_copy_assignable_v<T>) {
269 index_mask::detail::fill(static_cast<T *>(dst), *static_cast<const T *>(value), mask);
270 }
271 else {
272 const T &value_ = *static_cast<const T *>(value);
273 T *dst_ = static_cast<T *>(dst);
274 mask.foreach_index([&](int64_t i) { dst_[i] = value_; });
275 }
276}
277template<typename T> void fill_assign_n_cb(const void *value, void *dst, const int64_t n)
278{
279 fill_assign_indices_cb<T>(value, dst, IndexMask(n));
280}
281
282template<typename T> void fill_construct_cb(const void *value, void *dst, int64_t n)
283{
284 const T &value_ = *static_cast<const T *>(value);
285 T *dst_ = static_cast<T *>(dst);
286
287 for (int64_t i = 0; i < n; i++) {
288 new (dst_ + i) T(value_);
289 }
290}
291template<typename T>
292void fill_construct_indices_cb(const void *value, void *dst, const IndexMask &mask)
293{
294 const T &value_ = *static_cast<const T *>(value);
295 T *dst_ = static_cast<T *>(dst);
296
297 mask.foreach_index_optimized<int64_t>([&](int64_t i) { new (dst_ + i) T(value_); });
298}
299template<typename T> void fill_construct_n_cb(const void *value, void *dst, const int64_t n)
300{
302}
303
304template<typename T> void print_cb(const void *value, std::stringstream &ss)
305{
306 const T &value_ = *static_cast<const T *>(value);
307 ss << value_;
308}
309
310template<typename T> bool is_equal_cb(const void *a, const void *b)
311{
314 const T &a_ = *static_cast<const T *>(a);
315 const T &b_ = *static_cast<const T *>(b);
316 return a_ == b_;
317}
318
319template<typename T> uint64_t hash_cb(const void *value)
320{
322 const T &value_ = *static_cast<const T *>(value);
323 return get_default_hash(value_);
324}
325
326template<typename T> void hash_unique_cb(const void *value, UniqueHashBytes &hash)
327{
329 const T &value_ = *static_cast<const T *>(value);
330 return hash_unique_default(value_, hash);
331}
332
333inline std::atomic<int> type_index_counter{0};
334
335} // namespace cpp_type_util
336
337template<typename T, CPPTypeFlags Flags>
340 const StringRef debug_name)
341{
342 using namespace cpp_type_util;
343
344 debug_name_ = debug_name;
345 this->size = int64_t(sizeof(T));
346 this->alignment = int64_t(alignof(T));
347 this->is_trivial = std::is_trivial_v<T>;
348 this->is_trivially_destructible = std::is_trivially_destructible_v<T>;
349 if constexpr (std::is_default_constructible_v<T>) {
350 default_construct_ = default_construct_cb<T>;
351 default_construct_n_ = default_construct_n_cb<T>;
352 default_construct_indices_ = default_construct_indices_cb<T>;
353 value_initialize_ = value_initialize_cb<T>;
354 value_initialize_n_ = value_initialize_n_cb<T>;
355 value_initialize_indices_ = value_initialize_indices_cb<T>;
356 if constexpr (bool(Flags & CPPTypeFlags::IdentityDefaultValue)) {
357 static const T default_value = T::identity();
358 default_value_ = &default_value;
359 }
360 else {
361 static const T default_value = T();
362 default_value_ = &default_value;
363 }
364 }
365 if constexpr (std::is_destructible_v<T>) {
366 destruct_ = destruct_cb<T>;
367 destruct_n_ = destruct_n_cb<T>;
368 destruct_indices_ = destruct_indices_cb<T>;
369 }
370 if constexpr (std::is_copy_assignable_v<T>) {
371 copy_assign_ = copy_assign_cb<T>;
372 copy_assign_n_ = copy_assign_n_cb<T>;
373 copy_assign_indices_ = copy_assign_indices_cb<T>;
374 copy_assign_compressed_ = copy_assign_compressed_cb<T>;
375 }
376 if constexpr (std::is_copy_constructible_v<T>) {
377 if constexpr (std::is_trivially_copy_constructible_v<T>) {
378 copy_construct_ = copy_assign_;
379 copy_construct_n_ = copy_assign_n_;
380 copy_construct_indices_ = copy_assign_indices_;
381 copy_construct_compressed_ = copy_assign_compressed_;
382 }
383 else {
384 copy_construct_ = copy_construct_cb<T>;
385 copy_construct_n_ = copy_construct_n_cb<T>;
386 copy_construct_indices_ = copy_construct_indices_cb<T>;
387 copy_construct_compressed_ = copy_construct_compressed_cb<T>;
388 }
389 }
390 if constexpr (std::is_move_assignable_v<T>) {
391 if constexpr (std::is_trivially_move_assignable_v<T>) {
392 /* This casts away the const from the src pointer. This is fine for trivial types as moving
393 * them does not change the original value. */
394 move_assign_ = reinterpret_cast<decltype(move_assign_)>(copy_assign_);
395 move_assign_n_ = reinterpret_cast<decltype(move_assign_n_)>(copy_assign_n_);
396 move_assign_indices_ = reinterpret_cast<decltype(move_assign_indices_)>(
397 copy_assign_indices_);
398 }
399 else {
400 move_assign_ = move_assign_cb<T>;
401 move_assign_n_ = move_assign_n_cb<T>;
402 move_assign_indices_ = move_assign_indices_cb<T>;
403 }
404 }
405 if constexpr (std::is_move_constructible_v<T>) {
406 if constexpr (std::is_trivially_move_constructible_v<T>) {
407 move_construct_ = move_assign_;
408 move_construct_n_ = move_assign_n_;
409 move_construct_indices_ = move_assign_indices_;
410 }
411 else {
412 move_construct_ = move_construct_cb<T>;
413 move_construct_n_ = move_construct_n_cb<T>;
414 move_construct_indices_ = move_construct_indices_cb<T>;
415 }
416 }
417 if constexpr (std::is_destructible_v<T>) {
418 if constexpr (std::is_trivially_move_assignable_v<T> && std::is_trivially_destructible_v<T>) {
419 relocate_assign_ = move_assign_;
420 relocate_assign_n_ = move_assign_n_;
421 relocate_assign_indices_ = move_assign_indices_;
422
423 relocate_construct_ = move_assign_;
424 relocate_construct_n_ = move_assign_n_;
425 relocate_construct_indices_ = move_assign_indices_;
426 }
427 else {
428 if constexpr (std::is_move_assignable_v<T>) {
429 relocate_assign_ = relocate_assign_cb<T>;
430 relocate_assign_n_ = relocate_assign_n_cb<T>;
431 relocate_assign_indices_ = relocate_assign_indices_cb<T>;
432 }
433 if constexpr (std::is_move_constructible_v<T>) {
434 relocate_construct_ = relocate_construct_cb<T>;
435 relocate_construct_n_ = relocate_construct_n_cb<T>;
436 relocate_construct_indices_ = relocate_construct_indices_cb<T>;
437 }
438 }
439 }
440 if constexpr (std::is_copy_assignable_v<T>) {
441 fill_assign_n_ = fill_assign_n_cb<T>;
442 fill_assign_indices_ = fill_assign_indices_cb<T>;
443 }
444 if constexpr (std::is_copy_constructible_v<T>) {
445 if constexpr (std::is_trivially_constructible_v<T>) {
446 fill_construct_n_ = fill_assign_n_;
447 fill_construct_indices_ = fill_assign_indices_;
448 }
449 else {
450 fill_construct_n_ = fill_construct_n_cb<T>;
451 fill_construct_indices_ = fill_construct_indices_cb<T>;
452 }
453 }
454 if constexpr (bool(Flags & CPPTypeFlags::Hashable)) {
455 hash_ = hash_cb<T>;
456 hash_unique_ = hash_unique_cb<T>;
457 }
458 if constexpr (bool(Flags & CPPTypeFlags::Printable)) {
459 print_ = print_cb<T>;
460 }
461 if constexpr (bool(Flags & CPPTypeFlags::EqualityComparable)) {
462 is_equal_ = is_equal_cb<T>;
463 }
464
465 alignment_mask_ = uintptr_t(this->alignment) - uintptr_t(1);
466 this->has_special_member_functions = (default_construct_ && copy_construct_ && copy_assign_ &&
467 move_construct_ && move_assign_ && destruct_);
468 this->is_default_constructible = default_construct_ != nullptr;
469 this->is_copy_constructible = copy_construct_ != nullptr;
470 this->is_move_constructible = move_construct_ != nullptr;
471 this->is_destructible = destruct_ != nullptr;
472 this->is_copy_assignable = copy_assign_ != nullptr;
473 this->is_move_assignable = move_assign_ != nullptr;
474
475 if constexpr (requires { typename T::base_type; }) {
477 }
478 if constexpr (requires { typename T::generic_type; }) {
480 }
481 this->type_index = type_index_counter++;
482}
483
484namespace detail {
485template<typename T, CPPTypeFlags FLAGS> inline void register_cpp_type(const StringRef type_name)
486{
487 /* Check that it was not registered before already. */
490 static CPPType *cpp_type = new (detail::cpp_type_impl<T>.ptr())
492
493 /* Call destructor on exit. */
494 struct CPPTypeDestructor {
495 ~CPPTypeDestructor()
496 {
497 std::destroy_at(cpp_type);
498 }
499 };
500 static CPPTypeDestructor cpp_type_destructor;
501}
502} // namespace detail
503
505#define BLI_CPP_TYPE_REGISTER(TYPE_NAME, FLAGS) \
506 blender::detail::register_cpp_type<TYPE_NAME, FLAGS>(STRINGIFY(TYPE_NAME))
507
508} // namespace blender
const CPPType * generic_type
static const CPPType * get_pre_register()
bool has_special_member_functions
const CPPType * base_type
const void * default_value() const
CPPType(TypeTag< T >, TypeForValue< CPPTypeFlags, Flags >, StringRef debug_name)
#define T
void fill_assign_indices_cb(const void *value, void *dst, const IndexMask &mask)
void move_assign_cb(void *src, void *dst)
void destruct_indices_cb(void *ptr, const IndexMask &mask)
void fill_construct_indices_cb(const void *value, void *dst, const IndexMask &mask)
void value_initialize_indices_cb(void *ptr, const IndexMask &mask)
void copy_construct_compressed_cb(const void *src, void *dst, const IndexMask &mask)
void relocate_construct_cb(void *src, void *dst)
void default_construct_indices_cb(void *ptr, const IndexMask &mask)
void copy_assign_indices_cb(const void *src, void *dst, const IndexMask &mask)
void move_assign_n_cb(void *src, void *dst, const int64_t n)
void copy_assign_n_cb(const void *src, void *dst, const int64_t n)
void value_initialize_cb(void *ptr)
std::atomic< int > type_index_counter
void move_construct_n_cb(void *src, void *dst, const int64_t n)
void relocate_assign_indices_cb(void *src, void *dst, const IndexMask &mask)
void copy_construct_cb(const void *src, void *dst)
void print_cb(const void *value, std::stringstream &ss)
void relocate_assign_cb(void *src, void *dst)
bool pointer_can_point_to_instance(const void *ptr)
void relocate_construct_n_cb(void *src, void *dst, const int64_t n)
void move_construct_indices_cb(void *src, void *dst, const IndexMask &mask)
void value_initialize_n_cb(void *ptr, const int64_t n)
void fill_assign_n_cb(const void *value, void *dst, const int64_t n)
void hash_unique_cb(const void *value, UniqueHashBytes &hash)
void destruct_n_cb(void *ptr, const int64_t n)
void fill_construct_n_cb(const void *value, void *dst, const int64_t n)
void relocate_assign_n_cb(void *src, void *dst, const int64_t n)
bool is_equal_cb(const void *a, const void *b)
void copy_construct_indices_cb(const void *src, void *dst, const IndexMask &mask)
void move_assign_indices_cb(void *src, void *dst, const IndexMask &mask)
void copy_assign_cb(const void *src, void *dst)
void relocate_construct_indices_cb(void *src, void *dst, const IndexMask &mask)
void copy_assign_compressed_cb(const void *src, void *dst, const IndexMask &mask)
void fill_construct_cb(const void *value, void *dst, int64_t n)
uint64_t hash_cb(const void *value)
bool pointer_has_valid_alignment(const void *ptr)
void copy_construct_n_cb(const void *src, void *dst, const int64_t n)
void default_construct_cb(void *ptr)
void move_construct_cb(void *src, void *dst)
void default_construct_n_cb(void *ptr, const int64_t n)
void register_cpp_type(const StringRef type_name)
TypedBuffer< CPPType > cpp_type_impl
BLI_NOINLINE void fill(T *__restrict data, const T &value, const IndexMask &mask)
BLI_NOINLINE void copy_assign(const T *__restrict src, const IndexMask &mask, T *__restrict dst)
BLI_NOINLINE void gather_assign(const T *__restrict src, const IndexMask &indices, T *__restrict dst)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
void initialized_move_n(T *src, int64_t n, T *dst)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
void hash_unique_default(const T &value, UniqueHashBytes &hash)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
void uninitialized_move_n(T *src, int64_t n, T *dst)
#define hash
Definition noise_c.cc:156