/* SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once /** \file * \ingroup bli */ #include "BLI_cpp_type.hh" #include "BLI_utildefines.h" namespace blender::cpp_type_util { template void default_construct_cb(void *ptr) { new (ptr) T; } template void default_construct_indices_cb(void *ptr, IndexMask mask) { mask.foreach_index([&](int64_t i) { new (static_cast(ptr) + i) T; }); } template void value_initialize_cb(void *ptr) { new (ptr) T(); } template void value_initialize_indices_cb(void *ptr, IndexMask mask) { mask.foreach_index([&](int64_t i) { new (static_cast(ptr) + i) T(); }); } template void destruct_cb(void *ptr) { (static_cast(ptr))->~T(); } template void destruct_indices_cb(void *ptr, IndexMask mask) { T *ptr_ = static_cast(ptr); mask.foreach_index([&](int64_t i) { ptr_[i].~T(); }); } template void copy_assign_cb(const void *src, void *dst) { *static_cast(dst) = *static_cast(src); } template void copy_assign_indices_cb(const void *src, void *dst, IndexMask mask) { const T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; }); } template void copy_assign_compressed_cb(const void *src, void *dst, IndexMask mask) { const T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.to_best_mask_type([&](auto best_mask) { for (const int64_t i : IndexRange(best_mask.size())) { dst_[i] = src_[best_mask[i]]; } }); } template void copy_construct_cb(const void *src, void *dst) { blender::uninitialized_copy_n(static_cast(src), 1, static_cast(dst)); } template void copy_construct_indices_cb(const void *src, void *dst, IndexMask mask) { const T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); }); } template void copy_construct_compressed_cb(const void *src, void *dst, IndexMask mask) { const T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.to_best_mask_type([&](auto best_mask) { for (const int64_t i : IndexRange(best_mask.size())) { new (dst_ + i) T(src_[best_mask[i]]); } }); } template void move_assign_cb(void *src, void *dst) { blender::initialized_move_n(static_cast(src), 1, static_cast(dst)); } template void move_assign_indices_cb(void *src, void *dst, IndexMask mask) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { dst_[i] = std::move(src_[i]); }); } template void move_construct_cb(void *src, void *dst) { blender::uninitialized_move_n(static_cast(src), 1, static_cast(dst)); } template void move_construct_indices_cb(void *src, void *dst, IndexMask mask) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { new (dst_ + i) T(std::move(src_[i])); }); } template void relocate_assign_cb(void *src, void *dst) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); *dst_ = std::move(*src_); src_->~T(); } template void relocate_assign_indices_cb(void *src, void *dst, IndexMask mask) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { dst_[i] = std::move(src_[i]); src_[i].~T(); }); } template void relocate_construct_cb(void *src, void *dst) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); new (dst_) T(std::move(*src_)); src_->~T(); } template void relocate_construct_indices_cb(void *src, void *dst, IndexMask mask) { T *src_ = static_cast(src); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { new (dst_ + i) T(std::move(src_[i])); src_[i].~T(); }); } template void fill_assign_cb(const void *value, void *dst, int64_t n) { const T &value_ = *static_cast(value); T *dst_ = static_cast(dst); for (int64_t i = 0; i < n; i++) { dst_[i] = value_; } } template void fill_assign_indices_cb(const void *value, void *dst, IndexMask mask) { const T &value_ = *static_cast(value); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { dst_[i] = value_; }); } template void fill_construct_cb(const void *value, void *dst, int64_t n) { const T &value_ = *static_cast(value); T *dst_ = static_cast(dst); for (int64_t i = 0; i < n; i++) { new (dst_ + i) T(value_); } } template void fill_construct_indices_cb(const void *value, void *dst, IndexMask mask) { const T &value_ = *static_cast(value); T *dst_ = static_cast(dst); mask.foreach_index([&](int64_t i) { new (dst_ + i) T(value_); }); } template void print_cb(const void *value, std::stringstream &ss) { const T &value_ = *static_cast(value); ss << value_; } template bool is_equal_cb(const void *a, const void *b) { const T &a_ = *static_cast(a); const T &b_ = *static_cast(b); return a_ == b_; } template uint64_t hash_cb(const void *value) { const T &value_ = *static_cast(value); return get_default_hash(value_); } } // namespace blender::cpp_type_util namespace blender { template CPPType::CPPType(CPPTypeParam /* unused */, StringRef debug_name) { using namespace cpp_type_util; debug_name_ = debug_name; size_ = int64_t(sizeof(T)); alignment_ = int64_t(alignof(T)); is_trivial_ = std::is_trivial_v; is_trivially_destructible_ = std::is_trivially_destructible_v; if constexpr (std::is_default_constructible_v) { default_construct_ = default_construct_cb; default_construct_indices_ = default_construct_indices_cb; value_initialize_ = value_initialize_cb; value_initialize_indices_ = value_initialize_indices_cb; static T default_value; default_value_ = &default_value; } if constexpr (std::is_destructible_v) { destruct_ = destruct_cb; destruct_indices_ = destruct_indices_cb; } if constexpr (std::is_copy_assignable_v) { copy_assign_ = copy_assign_cb; copy_assign_indices_ = copy_assign_indices_cb; copy_assign_compressed_ = copy_assign_compressed_cb; } if constexpr (std::is_copy_constructible_v) { copy_construct_ = copy_construct_cb; copy_construct_indices_ = copy_construct_indices_cb; copy_construct_compressed_ = copy_construct_compressed_cb; } if constexpr (std::is_move_assignable_v) { move_assign_ = move_assign_cb; move_assign_indices_ = move_assign_indices_cb; } if constexpr (std::is_move_constructible_v) { move_construct_ = move_construct_cb; move_construct_indices_ = move_construct_indices_cb; } if constexpr (std::is_destructible_v) { if constexpr (std::is_move_assignable_v) { relocate_assign_ = relocate_assign_cb; relocate_assign_indices_ = relocate_assign_indices_cb; } if constexpr (std::is_move_constructible_v) { relocate_construct_ = relocate_construct_cb; relocate_construct_indices_ = relocate_construct_indices_cb; } } if constexpr (std::is_copy_assignable_v) { fill_assign_indices_ = fill_assign_indices_cb; } if constexpr (std::is_copy_constructible_v) { fill_construct_indices_ = fill_construct_indices_cb; } if constexpr ((bool)(Flags & CPPTypeFlags::Hashable)) { hash_ = hash_cb; } if constexpr ((bool)(Flags & CPPTypeFlags::Printable)) { print_ = print_cb; } if constexpr ((bool)(Flags & CPPTypeFlags::EqualityComparable)) { is_equal_ = is_equal_cb; } alignment_mask_ = uintptr_t(alignment_) - uintptr_t(1); has_special_member_functions_ = (default_construct_ && copy_construct_ && copy_assign_ && move_construct_ && move_assign_ && destruct_); } } // namespace blender #define BLI_CPP_TYPE_MAKE(IDENTIFIER, TYPE_NAME, FLAGS) \ template<> const blender::CPPType &blender::CPPType::get_impl() \ { \ static CPPType cpp_type{blender::CPPTypeParam(), STRINGIFY(IDENTIFIER)}; \ return cpp_type; \ }