Blender V5.3
BLI_cpp_type.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
74
75#include <atomic>
76
77#include "BLI_dynamic_stack_buffer.hh" // IWYU pragma: keep
78#include "BLI_enum_flags.hh"
79#include "BLI_hash.hh"
80#include "BLI_index_mask_fwd.hh"
81#include "BLI_map.hh"
83#include "BLI_string_ref.hh"
84#include "BLI_utility_mixins.hh"
85
86namespace blender {
87
88struct UniqueHashBytes;
89
105
107 public:
115
123
131 bool is_trivial = false;
132
141
152
156 bool is_destructible = false;
157 bool is_copy_assignable = false;
158 bool is_move_assignable = false;
159
161 const CPPType *base_type = nullptr;
163 const CPPType *generic_type = nullptr;
169 int type_index = -1;
170
171 private:
172 uintptr_t alignment_mask_ = 0;
173
174 void (*default_construct_)(void *ptr) = nullptr;
175 void (*default_construct_n_)(void *ptr, int64_t n) = nullptr;
176 void (*default_construct_indices_)(void *ptr, const IndexMask &mask) = nullptr;
177
178 void (*value_initialize_)(void *ptr) = nullptr;
179 void (*value_initialize_n_)(void *ptr, int64_t n) = nullptr;
180 void (*value_initialize_indices_)(void *ptr, const IndexMask &mask) = nullptr;
181
182 void (*destruct_)(void *ptr) = nullptr;
183 void (*destruct_n_)(void *ptr, int64_t n) = nullptr;
184 void (*destruct_indices_)(void *ptr, const IndexMask &mask) = nullptr;
185
186 void (*copy_assign_)(const void *src, void *dst) = nullptr;
187 void (*copy_assign_n_)(const void *src, void *dst, int64_t n) = nullptr;
188 void (*copy_assign_indices_)(const void *src, void *dst, const IndexMask &mask) = nullptr;
189 void (*copy_assign_compressed_)(const void *src, void *dst, const IndexMask &mask) = nullptr;
190
191 void (*copy_construct_)(const void *src, void *dst) = nullptr;
192 void (*copy_construct_n_)(const void *src, void *dst, int64_t n) = nullptr;
193 void (*copy_construct_indices_)(const void *src, void *dst, const IndexMask &mask) = nullptr;
194 void (*copy_construct_compressed_)(const void *src, void *dst, const IndexMask &mask) = nullptr;
195
196 void (*move_assign_)(void *src, void *dst) = nullptr;
197 void (*move_assign_n_)(void *src, void *dst, int64_t n) = nullptr;
198 void (*move_assign_indices_)(void *src, void *dst, const IndexMask &mask) = nullptr;
199
200 void (*move_construct_)(void *src, void *dst) = nullptr;
201 void (*move_construct_n_)(void *src, void *dst, int64_t n) = nullptr;
202 void (*move_construct_indices_)(void *src, void *dst, const IndexMask &mask) = nullptr;
203
204 void (*relocate_assign_)(void *src, void *dst) = nullptr;
205 void (*relocate_assign_n_)(void *src, void *dst, int64_t n) = nullptr;
206 void (*relocate_assign_indices_)(void *src, void *dst, const IndexMask &mask) = nullptr;
207
208 void (*relocate_construct_)(void *src, void *dst) = nullptr;
209 void (*relocate_construct_n_)(void *src, void *dst, int64_t n) = nullptr;
210 void (*relocate_construct_indices_)(void *src, void *dst, const IndexMask &mask) = nullptr;
211
212 void (*fill_assign_n_)(const void *value, void *dst, int64_t n) = nullptr;
213 void (*fill_assign_indices_)(const void *value, void *dst, const IndexMask &mask) = nullptr;
214
215 void (*fill_construct_n_)(const void *value, void *dst, int64_t n) = nullptr;
216 void (*fill_construct_indices_)(const void *value, void *dst, const IndexMask &mask) = nullptr;
217
218 void (*print_)(const void *value, std::stringstream &ss) = nullptr;
219 bool (*is_equal_)(const void *a, const void *b) = nullptr;
220 uint64_t (*hash_)(const void *value) = nullptr;
221 void (*hash_unique_)(const void *value, UniqueHashBytes &hash) = nullptr;
222
223 const void *default_value_ = nullptr;
224 std::string debug_name_;
225
226 public:
227 template<typename T, CPPTypeFlags Flags>
228 CPPType(TypeTag<T> /*type*/, TypeForValue<CPPTypeFlags, Flags> /*flags*/, StringRef debug_name);
229 virtual ~CPPType() = default;
230
236 template<typename T> static const CPPType &get();
241 template<typename T> static const CPPType *get_pre_register();
242
247 StringRefNull name() const;
248
249 bool is_printable() const;
250 bool is_equality_comparable() const;
251 bool is_hashable() const;
252
256 bool pointer_has_valid_alignment(const void *ptr) const;
257 bool pointer_can_point_to_instance(const void *ptr) const;
258
267 void default_construct(void *ptr) const;
268 void default_construct_n(void *ptr, int64_t n) const;
269 void default_construct_indices(void *ptr, const IndexMask &mask) const;
270
277 void value_initialize(void *ptr) const;
278 void value_initialize_n(void *ptr, int64_t n) const;
279 void value_initialize_indices(void *ptr, const IndexMask &mask) const;
280
289 void destruct(void *ptr) const;
290 void destruct_n(void *ptr, int64_t n) const;
291 void destruct_indices(void *ptr, const IndexMask &mask) const;
292
299 void copy_assign(const void *src, void *dst) const;
300 void copy_assign_n(const void *src, void *dst, int64_t n) const;
301 void copy_assign_indices(const void *src, void *dst, const IndexMask &mask) const;
302
306 void copy_assign_compressed(const void *src, void *dst, const IndexMask &mask) const;
307
316 void copy_construct(const void *src, void *dst) const;
317 void copy_construct_n(const void *src, void *dst, int64_t n) const;
318 void copy_construct_indices(const void *src, void *dst, const IndexMask &mask) const;
319
323 void copy_construct_compressed(const void *src, void *dst, const IndexMask &mask) const;
324
333 void move_assign(void *src, void *dst) const;
334 void move_assign_n(void *src, void *dst, int64_t n) const;
335 void move_assign_indices(void *src, void *dst, const IndexMask &mask) const;
336
345 void move_construct(void *src, void *dst) const;
346 void move_construct_n(void *src, void *dst, int64_t n) const;
347 void move_construct_indices(void *src, void *dst, const IndexMask &mask) const;
348
357 void relocate_assign(void *src, void *dst) const;
358 void relocate_assign_n(void *src, void *dst, int64_t n) const;
359 void relocate_assign_indices(void *src, void *dst, const IndexMask &mask) const;
360
369 void relocate_construct(void *src, void *dst) const;
370 void relocate_construct_n(void *src, void *dst, int64_t n) const;
371 void relocate_construct_indices(void *src, void *dst, const IndexMask &mask) const;
372
378 void fill_assign_n(const void *value, void *dst, int64_t n) const;
379 void fill_assign_indices(const void *value, void *dst, const IndexMask &mask) const;
380
386 void fill_construct_n(const void *value, void *dst, int64_t n) const;
387 void fill_construct_indices(const void *value, void *dst, const IndexMask &mask) const;
388
389 bool can_exist_in_buffer(const int64_t buffer_size, const int64_t buffer_alignment) const;
390
391 void print(const void *value, std::stringstream &ss) const;
392 std::string to_string(const void *value) const;
393 void print_or_default(const void *value, std::stringstream &ss, StringRef default_value) const;
394
395 bool is_equal(const void *a, const void *b) const;
396 bool is_equal_or_false(const void *a, const void *b) const;
397
398 uint64_t hash(const void *value) const;
399 uint64_t hash_or_fallback(const void *value, uint64_t fallback_hash) const;
400 void hash_unique(const void *value, UniqueHashBytes &hash) const;
401
406 const void *default_value() const;
407
408 uint64_t hash() const;
409
410 void (*destruct_fn() const)(void *);
411
412 template<typename T> bool is() const;
413
414 template<typename... T> bool is_any() const;
415
427 template<typename... Types, typename Fn> bool to_static_type_try(Fn &&fn) const;
428
430 template<typename... Types, typename Fn> void to_static_type(Fn &&fn) const;
431
432 private:
439 template<typename T, typename Fn> static void call_with_type_impl_(const Fn &fn)
440 {
441 fn.template operator()<T>();
442 }
443};
444
445namespace detail {
450template<typename T> inline TypedBuffer<CPPType> cpp_type_impl{};
451template<typename T> inline bool is_cpp_type_registered = false;
452} // namespace detail
453
457void register_cpp_types();
458
459/* Utility for allocating an uninitialized buffer for a single value of the given #CPPType. */
460#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name) \
461 DynamicStackBuffer<64, 64> stack_buffer_for_##variable_name((type).size, (type).alignment); \
462 void *variable_name = stack_buffer_for_##variable_name.buffer();
463
464/* Give a compile error instead of a link error when type information is missing. */
465template<> const CPPType &CPPType::get<void>() = delete;
466
471inline bool operator==(const CPPType &a, const CPPType &b)
472{
473 return &a == &b;
474}
475
476inline bool operator!=(const CPPType &a, const CPPType &b)
477{
478 return !(&a == &b);
479}
480
481template<typename T> inline const CPPType &CPPType::get()
482{
483 using DecayT = std::decay_t<T>;
484 /* Should have been initialized by #BLI_CPP_TYPE_REGISTER.
485 * If this is hit in test code, make sure the test calls `register_cpp_types` (for blenlib
486 * tests) or `BKE_cpp_types_init` (for general tests). */
488 const CPPType &type = detail::cpp_type_impl<DecayT>.ref();
489 return type;
490}
491
492template<typename T> inline const CPPType *CPPType::get_pre_register()
493{
495}
496
498{
499 return debug_name_;
500}
501
502inline bool CPPType::is_printable() const
503{
504 return print_ != nullptr;
505}
506
508{
509 return is_equal_ != nullptr;
510}
511
512inline bool CPPType::is_hashable() const
513{
514 return hash_ != nullptr;
515}
516
517inline bool CPPType::pointer_has_valid_alignment(const void *ptr) const
518{
519 return (uintptr_t(ptr) & alignment_mask_) == 0;
520}
521
522inline bool CPPType::pointer_can_point_to_instance(const void *ptr) const
523{
524 return ptr != nullptr && pointer_has_valid_alignment(ptr);
525}
526
527inline void CPPType::default_construct(void *ptr) const
528{
529 default_construct_(ptr);
530}
531
532inline void CPPType::default_construct_n(void *ptr, int64_t n) const
533{
534 default_construct_n_(ptr, n);
535}
536
537inline void CPPType::default_construct_indices(void *ptr, const IndexMask &mask) const
538{
539 default_construct_indices_(ptr, mask);
540}
541
542inline void CPPType::value_initialize(void *ptr) const
543{
544 value_initialize_(ptr);
545}
546
547inline void CPPType::value_initialize_n(void *ptr, int64_t n) const
548{
549 value_initialize_n_(ptr, n);
550}
551
552inline void CPPType::value_initialize_indices(void *ptr, const IndexMask &mask) const
553{
554 value_initialize_indices_(ptr, mask);
555}
556
557inline void CPPType::destruct(void *ptr) const
558{
559 destruct_(ptr);
560}
561
562inline void CPPType::destruct_n(void *ptr, int64_t n) const
563{
564 destruct_n_(ptr, n);
565}
566
567inline void CPPType::destruct_indices(void *ptr, const IndexMask &mask) const
568{
569 destruct_indices_(ptr, mask);
570}
571
572inline void CPPType::copy_assign(const void *src, void *dst) const
573{
574 copy_assign_(src, dst);
575}
576
577inline void CPPType::copy_assign_n(const void *src, void *dst, int64_t n) const
578{
579 copy_assign_n_(src, dst, n);
580}
581
582inline void CPPType::copy_assign_indices(const void *src, void *dst, const IndexMask &mask) const
583{
584 copy_assign_indices_(src, dst, mask);
585}
586
587inline void CPPType::copy_assign_compressed(const void *src,
588 void *dst,
589 const IndexMask &mask) const
590{
591 copy_assign_compressed_(src, dst, mask);
592}
593
594inline void CPPType::copy_construct(const void *src, void *dst) const
595{
596 copy_construct_(src, dst);
597}
598
599inline void CPPType::copy_construct_n(const void *src, void *dst, int64_t n) const
600{
601 copy_construct_n_(src, dst, n);
602}
603
604inline void CPPType::copy_construct_indices(const void *src,
605 void *dst,
606 const IndexMask &mask) const
607{
608 copy_construct_indices_(src, dst, mask);
609}
610
611inline void CPPType::copy_construct_compressed(const void *src,
612 void *dst,
613 const IndexMask &mask) const
614{
615 copy_construct_compressed_(src, dst, mask);
616}
617
618inline void CPPType::move_assign(void *src, void *dst) const
619{
620 move_assign_(src, dst);
621}
622
623inline void CPPType::move_assign_n(void *src, void *dst, int64_t n) const
624{
625 move_assign_n_(src, dst, n);
626}
627
628inline void CPPType::move_assign_indices(void *src, void *dst, const IndexMask &mask) const
629{
630 move_assign_indices_(src, dst, mask);
631}
632
633inline void CPPType::move_construct(void *src, void *dst) const
634{
635 move_construct_(src, dst);
636}
637
638inline void CPPType::move_construct_n(void *src, void *dst, int64_t n) const
639{
640 move_construct_n_(src, dst, n);
641}
642
643inline void CPPType::move_construct_indices(void *src, void *dst, const IndexMask &mask) const
644{
645 move_construct_indices_(src, dst, mask);
646}
647
648inline void CPPType::relocate_assign(void *src, void *dst) const
649{
650 relocate_assign_(src, dst);
651}
652
653inline void CPPType::relocate_assign_n(void *src, void *dst, int64_t n) const
654{
655 relocate_assign_n_(src, dst, n);
656}
657
658inline void CPPType::relocate_assign_indices(void *src, void *dst, const IndexMask &mask) const
659{
660 relocate_assign_indices_(src, dst, mask);
661}
662
663inline void CPPType::relocate_construct(void *src, void *dst) const
664{
665 relocate_construct_(src, dst);
666}
667
668inline void CPPType::relocate_construct_n(void *src, void *dst, int64_t n) const
669{
670 relocate_construct_n_(src, dst, n);
671}
672
673inline void CPPType::relocate_construct_indices(void *src, void *dst, const IndexMask &mask) const
674{
675 relocate_construct_indices_(src, dst, mask);
676}
677
678inline void CPPType::fill_assign_n(const void *value, void *dst, int64_t n) const
679{
680 fill_assign_n_(value, dst, n);
681}
682
683inline void CPPType::fill_assign_indices(const void *value, void *dst, const IndexMask &mask) const
684{
685 fill_assign_indices_(value, dst, mask);
686}
687
688inline void CPPType::fill_construct_n(const void *value, void *dst, int64_t n) const
689{
690 fill_construct_n_(value, dst, n);
691}
692
693inline void CPPType::fill_construct_indices(const void *value,
694 void *dst,
695 const IndexMask &mask) const
696{
697 fill_construct_indices_(value, dst, mask);
698}
699
700inline bool CPPType::can_exist_in_buffer(const int64_t buffer_size,
701 const int64_t buffer_alignment) const
702{
703 return this->size <= buffer_size && this->alignment <= buffer_alignment;
704}
705
706inline void CPPType::print(const void *value, std::stringstream &ss) const
707{
709 print_(value, ss);
710}
711
712inline bool CPPType::is_equal(const void *a, const void *b) const
713{
714 return is_equal_(a, b);
715}
716
717inline bool CPPType::is_equal_or_false(const void *a, const void *b) const
718{
719 if (this->is_equality_comparable()) {
720 return this->is_equal(a, b);
721 }
722 return false;
723}
724
725inline uint64_t CPPType::hash(const void *value) const
726{
727 return hash_(value);
728}
729
730inline uint64_t CPPType::hash_or_fallback(const void *value, uint64_t fallback_hash) const
731{
732 if (this->is_hashable()) {
733 return this->hash(value);
734 }
735 return fallback_hash;
736}
737
738inline void CPPType::hash_unique(const void *value, UniqueHashBytes &hash) const
739{
740 this->hash_unique_(value, hash);
741}
742
743inline const void *CPPType::default_value() const
744{
745 return default_value_;
746}
747
749{
750 return get_default_hash(this);
751}
752
753inline void (*CPPType::destruct_fn() const)(void *)
754{
755 return destruct_;
756}
757
758template<typename T> inline bool CPPType::is() const
759{
760 return this == &CPPType::get<std::decay_t<T>>();
761}
762
763template<typename... T> inline bool CPPType::is_any() const
764{
765 return (this->is<T>() || ...);
766}
767
768template<typename... Types, typename Fn> inline void CPPType::to_static_type(Fn &&fn) const
769{
770 if (this->to_static_type_try<Types...>(fn)) {
771 return;
772 }
774}
775
776template<typename... Types, typename Fn> inline bool CPPType::to_static_type_try(Fn &&fn) const
777{
778 /* Strip any reference from Fn to normalize the type used for the static map, ensuring the
779 * same static is used regardless of whether fn is an lvalue or rvalue. */
780 using Fn_ = std::remove_reference_t<Fn>;
781 using Callback = void (*)(const Fn_ &);
782
783 /* Use an array indexed by #CPPType::type_index instead of a #Map for faster lookup and less
784 * generated code. The array can be quite a bit larger at run-time than the number of types but
785 * the total number of types is fairly limited, so that should be fine. */
786 static Array<Callback, 0> callback_array = [&]() {
787 /* This way to compute the max generates less code than using std::max with an initializer
788 * list. */
789 int max_type_index = 0;
790 ((max_type_index = std::max(max_type_index, CPPType::get<Types>().type_index)), ...);
791 Array<Callback, 0> array(max_type_index + 1, nullptr);
792 /* Using call_with_type_impl_ instead of a lambda due to an MSVC bug. */
793 ((array[CPPType::get<Types>().type_index] = call_with_type_impl_<Types, Fn_>), ...);
794 return array;
795 }();
796
797 if (this->type_index >= callback_array.size()) {
798 return false;
799 }
800 const Callback callback = callback_array[this->type_index];
801 if (!callback) {
802 return false;
803 }
804 callback(fn);
805 return true;
806}
807
808} // namespace blender
#define ENUM_OPERATORS(_enum_type)
#define BLI_assert_unreachable()
int64_t size() const
Definition BLI_array.hh:267
void copy_construct_n(const void *src, void *dst, int64_t n) const
void(*)(void *) destruct_fn() const
bool is_any() const
static const CPPType & get()
void value_initialize_indices(void *ptr, const IndexMask &mask) const
void default_construct_n(void *ptr, int64_t n) const
virtual ~CPPType()=default
void destruct_n(void *ptr, int64_t n) const
void copy_assign_indices(const void *src, void *dst, const IndexMask &mask) const
void move_construct_n(void *src, void *dst, int64_t n) const
const CPPType * generic_type
void fill_assign_n(const void *value, void *dst, int64_t n) const
uint64_t hash_or_fallback(const void *value, uint64_t fallback_hash) const
bool is_equal(const void *a, const void *b) const
void to_static_type(Fn &&fn) const
void fill_construct_indices(const void *value, void *dst, const IndexMask &mask) const
void relocate_assign_n(void *src, void *dst, int64_t n) const
static const CPPType * get_pre_register()
uint64_t hash() const
void copy_construct_indices(const void *src, void *dst, const IndexMask &mask) const
void fill_assign_indices(const void *value, void *dst, const IndexMask &mask) const
void move_assign(void *src, void *dst) const
bool is_printable() const
std::string to_string(const void *value) const
Definition cpp_type.cc:15
void move_assign_indices(void *src, void *dst, const IndexMask &mask) const
void fill_construct_n(const void *value, void *dst, int64_t n) const
bool is_hashable() const
void relocate_construct(void *src, void *dst) const
void move_assign_n(void *src, void *dst, int64_t n) const
bool is_equal_or_false(const void *a, const void *b) const
StringRefNull name() const
void relocate_assign_indices(void *src, void *dst, const IndexMask &mask) const
void hash_unique(const void *value, UniqueHashBytes &hash) const
bool is() const
void copy_construct_compressed(const void *src, void *dst, const IndexMask &mask) const
void copy_assign_n(const void *src, void *dst, int64_t n) const
bool is_equality_comparable() const
void value_initialize_n(void *ptr, int64_t n) const
bool has_special_member_functions
uint64_t hash(const void *value) const
void destruct_indices(void *ptr, const IndexMask &mask) const
void copy_construct(const void *src, void *dst) const
void destruct(void *ptr) const
bool pointer_has_valid_alignment(const void *ptr) const
bool to_static_type_try(Fn &&fn) const
const CPPType * base_type
void default_construct_indices(void *ptr, const IndexMask &mask) const
void print_or_default(const void *value, std::stringstream &ss, StringRef default_value) const
Definition cpp_type.cc:22
void copy_assign(const void *src, void *dst) const
const void * default_value() const
void move_construct(void *src, void *dst) const
bool can_exist_in_buffer(const int64_t buffer_size, const int64_t buffer_alignment) const
void print(const void *value, std::stringstream &ss) const
bool pointer_can_point_to_instance(const void *ptr) const
void relocate_construct_n(void *src, void *dst, int64_t n) const
CPPType(TypeTag< T >, TypeForValue< CPPTypeFlags, Flags >, StringRef debug_name)
void relocate_assign(void *src, void *dst) const
void default_construct(void *ptr) const
void relocate_construct_indices(void *src, void *dst, const IndexMask &mask) const
void copy_assign_compressed(const void *src, void *dst, const IndexMask &mask) const
void move_construct_indices(void *src, void *dst, const IndexMask &mask) const
void value_initialize(void *ptr) const
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
#define T
bool operator==(const Error &left, const Error &right)
TypedBuffer< CPPType > cpp_type_impl
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
bool operator!=(const CPPType &a, const CPPType &b)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252