From 2ddbb2c64f00f1ba357fe41813523088a9215326 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sun, 21 Mar 2021 15:33:30 +0100 Subject: Functions: move CPPType creation related code to separate header This does not need to be included everywhere, because it is only needed in very few translation units that actually define CPPType's. --- source/blender/functions/CMakeLists.txt | 1 + source/blender/functions/FN_cpp_type.hh | 264 ------------------- source/blender/functions/FN_cpp_type_make.hh | 287 +++++++++++++++++++++ source/blender/functions/intern/cpp_types.cc | 2 +- source/blender/functions/tests/FN_cpp_type_test.cc | 1 + 5 files changed, 290 insertions(+), 265 deletions(-) create mode 100644 source/blender/functions/FN_cpp_type_make.hh (limited to 'source/blender/functions') diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt index e4a0b154a07..608d7287bb2 100644 --- a/source/blender/functions/CMakeLists.txt +++ b/source/blender/functions/CMakeLists.txt @@ -36,6 +36,7 @@ set(SRC FN_array_spans.hh FN_cpp_type.hh + FN_cpp_type_make.hh FN_generic_pointer.hh FN_generic_value_map.hh FN_generic_vector_array.hh diff --git a/source/blender/functions/FN_cpp_type.hh b/source/blender/functions/FN_cpp_type.hh index b8ac97d6dbd..665296f30e1 100644 --- a/source/blender/functions/FN_cpp_type.hh +++ b/source/blender/functions/FN_cpp_type.hh @@ -670,272 +670,8 @@ class CPPType : NonCopyable, NonMovable { } }; -/* -------------------------------------------------------------------- - * Utility for creating CPPType instances for C++ types. - */ - -namespace cpp_type_util { - -template void construct_default_cb(void *ptr) -{ - new (ptr) T; -} -template void construct_default_n_cb(void *ptr, int64_t n) -{ - blender::default_construct_n(static_cast(ptr), n); -} -template void construct_default_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_n_cb(void *ptr, int64_t n) -{ - blender::destruct_n(static_cast(ptr), n); -} -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_to_initialized_cb(const void *src, void *dst) -{ - *static_cast(dst) = *static_cast(src); -} -template void copy_to_initialized_n_cb(const void *src, void *dst, int64_t n) -{ - const T *src_ = static_cast(src); - T *dst_ = static_cast(dst); - - for (int64_t i = 0; i < n; i++) { - dst_[i] = src_[i]; - } -} -template -void copy_to_initialized_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_to_uninitialized_cb(const void *src, void *dst) -{ - blender::uninitialized_copy_n(static_cast(src), 1, static_cast(dst)); -} -template void copy_to_uninitialized_n_cb(const void *src, void *dst, int64_t n) -{ - blender::uninitialized_copy_n(static_cast(src), n, static_cast(dst)); -} -template -void copy_to_uninitialized_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 move_to_initialized_cb(void *src, void *dst) -{ - blender::initialized_move_n(static_cast(src), 1, static_cast(dst)); -} -template void move_to_initialized_n_cb(void *src, void *dst, int64_t n) -{ - blender::initialized_move_n(static_cast(src), n, static_cast(dst)); -} -template void move_to_initialized_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_to_uninitialized_cb(void *src, void *dst) -{ - blender::uninitialized_move_n(static_cast(src), 1, static_cast(dst)); -} -template void move_to_uninitialized_n_cb(void *src, void *dst, int64_t n) -{ - blender::uninitialized_move_n(static_cast(src), n, static_cast(dst)); -} -template void move_to_uninitialized_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_to_initialized_cb(void *src, void *dst) -{ - T *src_ = static_cast(src); - T *dst_ = static_cast(dst); - - *dst_ = std::move(*src_); - src_->~T(); -} -template void relocate_to_initialized_n_cb(void *src, void *dst, int64_t n) -{ - blender::initialized_relocate_n(static_cast(src), n, static_cast(dst)); -} -template void relocate_to_initialized_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_to_uninitialized_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_to_uninitialized_n_cb(void *src, void *dst, int64_t n) -{ - blender::uninitialized_relocate_n(static_cast(src), n, static_cast(dst)); -} -template -void relocate_to_uninitialized_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_initialized_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_initialized_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_uninitialized_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_uninitialized_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 debug_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 DefaultHash{}(value_); -} - -} // namespace cpp_type_util - -template -inline std::unique_ptr create_cpp_type(StringRef name, const T &default_value) -{ - using namespace cpp_type_util; - const CPPType *type = new CPPType(name, - sizeof(T), - alignof(T), - std::is_trivially_destructible_v, - construct_default_cb, - construct_default_n_cb, - construct_default_indices_cb, - destruct_cb, - destruct_n_cb, - destruct_indices_cb, - copy_to_initialized_cb, - copy_to_initialized_n_cb, - copy_to_initialized_indices_cb, - copy_to_uninitialized_cb, - copy_to_uninitialized_n_cb, - copy_to_uninitialized_indices_cb, - move_to_initialized_cb, - move_to_initialized_n_cb, - move_to_initialized_indices_cb, - move_to_uninitialized_cb, - move_to_uninitialized_n_cb, - move_to_uninitialized_indices_cb, - relocate_to_initialized_cb, - relocate_to_initialized_n_cb, - relocate_to_initialized_indices_cb, - relocate_to_uninitialized_cb, - relocate_to_uninitialized_n_cb, - relocate_to_uninitialized_indices_cb, - fill_initialized_cb, - fill_initialized_indices_cb, - fill_uninitialized_cb, - fill_uninitialized_indices_cb, - debug_print_cb, - is_equal_cb, - hash_cb, - static_cast(&default_value)); - return std::unique_ptr(type); -} - } // namespace blender::fn -#define MAKE_CPP_TYPE(IDENTIFIER, TYPE_NAME) \ - template<> const blender::fn::CPPType &blender::fn::CPPType::get() \ - { \ - static TYPE_NAME default_value; \ - static std::unique_ptr cpp_type = blender::fn::create_cpp_type( \ - STRINGIFY(IDENTIFIER), default_value); \ - return *cpp_type; \ - } \ - /* Support using `CPPType::get()`. Otherwise the caller would have to remove const. */ \ - template<> const blender::fn::CPPType &blender::fn::CPPType::get() \ - { \ - return blender::fn::CPPType::get(); \ - } - /* Utility for allocating an uninitialized buffer for a single value of the given #CPPType. */ #define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name) \ blender::DynamicStackBuffer<64, 64> stack_buffer_for_##variable_name(type.size(), \ diff --git a/source/blender/functions/FN_cpp_type_make.hh b/source/blender/functions/FN_cpp_type_make.hh new file mode 100644 index 00000000000..342161d01b6 --- /dev/null +++ b/source/blender/functions/FN_cpp_type_make.hh @@ -0,0 +1,287 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#pragma once + +/** \file + * \ingroup fn + */ + +#include "FN_cpp_type.hh" + +namespace blender::fn::cpp_type_util { + +template void construct_default_cb(void *ptr) +{ + new (ptr) T; +} +template void construct_default_n_cb(void *ptr, int64_t n) +{ + blender::default_construct_n(static_cast(ptr), n); +} +template void construct_default_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_n_cb(void *ptr, int64_t n) +{ + blender::destruct_n(static_cast(ptr), n); +} +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_to_initialized_cb(const void *src, void *dst) +{ + *static_cast(dst) = *static_cast(src); +} +template void copy_to_initialized_n_cb(const void *src, void *dst, int64_t n) +{ + const T *src_ = static_cast(src); + T *dst_ = static_cast(dst); + + for (int64_t i = 0; i < n; i++) { + dst_[i] = src_[i]; + } +} +template +void copy_to_initialized_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_to_uninitialized_cb(const void *src, void *dst) +{ + blender::uninitialized_copy_n(static_cast(src), 1, static_cast(dst)); +} +template void copy_to_uninitialized_n_cb(const void *src, void *dst, int64_t n) +{ + blender::uninitialized_copy_n(static_cast(src), n, static_cast(dst)); +} +template +void copy_to_uninitialized_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 move_to_initialized_cb(void *src, void *dst) +{ + blender::initialized_move_n(static_cast(src), 1, static_cast(dst)); +} +template void move_to_initialized_n_cb(void *src, void *dst, int64_t n) +{ + blender::initialized_move_n(static_cast(src), n, static_cast(dst)); +} +template void move_to_initialized_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_to_uninitialized_cb(void *src, void *dst) +{ + blender::uninitialized_move_n(static_cast(src), 1, static_cast(dst)); +} +template void move_to_uninitialized_n_cb(void *src, void *dst, int64_t n) +{ + blender::uninitialized_move_n(static_cast(src), n, static_cast(dst)); +} +template void move_to_uninitialized_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_to_initialized_cb(void *src, void *dst) +{ + T *src_ = static_cast(src); + T *dst_ = static_cast(dst); + + *dst_ = std::move(*src_); + src_->~T(); +} +template void relocate_to_initialized_n_cb(void *src, void *dst, int64_t n) +{ + blender::initialized_relocate_n(static_cast(src), n, static_cast(dst)); +} +template void relocate_to_initialized_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_to_uninitialized_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_to_uninitialized_n_cb(void *src, void *dst, int64_t n) +{ + blender::uninitialized_relocate_n(static_cast(src), n, static_cast(dst)); +} +template +void relocate_to_uninitialized_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_initialized_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_initialized_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_uninitialized_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_uninitialized_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 debug_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 DefaultHash{}(value_); +} + +} // namespace blender::fn::cpp_type_util + +namespace blender::fn { + +template +inline std::unique_ptr create_cpp_type(StringRef name, const T &default_value) +{ + using namespace cpp_type_util; + const CPPType *type = new CPPType(name, + sizeof(T), + alignof(T), + std::is_trivially_destructible_v, + construct_default_cb, + construct_default_n_cb, + construct_default_indices_cb, + destruct_cb, + destruct_n_cb, + destruct_indices_cb, + copy_to_initialized_cb, + copy_to_initialized_n_cb, + copy_to_initialized_indices_cb, + copy_to_uninitialized_cb, + copy_to_uninitialized_n_cb, + copy_to_uninitialized_indices_cb, + move_to_initialized_cb, + move_to_initialized_n_cb, + move_to_initialized_indices_cb, + move_to_uninitialized_cb, + move_to_uninitialized_n_cb, + move_to_uninitialized_indices_cb, + relocate_to_initialized_cb, + relocate_to_initialized_n_cb, + relocate_to_initialized_indices_cb, + relocate_to_uninitialized_cb, + relocate_to_uninitialized_n_cb, + relocate_to_uninitialized_indices_cb, + fill_initialized_cb, + fill_initialized_indices_cb, + fill_uninitialized_cb, + fill_uninitialized_indices_cb, + debug_print_cb, + is_equal_cb, + hash_cb, + static_cast(&default_value)); + return std::unique_ptr(type); +} + +} // namespace blender::fn + +#define MAKE_CPP_TYPE(IDENTIFIER, TYPE_NAME) \ + template<> const blender::fn::CPPType &blender::fn::CPPType::get() \ + { \ + static TYPE_NAME default_value; \ + static std::unique_ptr cpp_type = blender::fn::create_cpp_type( \ + STRINGIFY(IDENTIFIER), default_value); \ + return *cpp_type; \ + } \ + /* Support using `CPPType::get()`. Otherwise the caller would have to remove const. */ \ + template<> const blender::fn::CPPType &blender::fn::CPPType::get() \ + { \ + return blender::fn::CPPType::get(); \ + } diff --git a/source/blender/functions/intern/cpp_types.cc b/source/blender/functions/intern/cpp_types.cc index 35d93f997cb..53c5def57e9 100644 --- a/source/blender/functions/intern/cpp_types.cc +++ b/source/blender/functions/intern/cpp_types.cc @@ -14,7 +14,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "FN_cpp_type.hh" +#include "FN_cpp_type_make.hh" #include "BLI_color.hh" #include "BLI_float2.hh" diff --git a/source/blender/functions/tests/FN_cpp_type_test.cc b/source/blender/functions/tests/FN_cpp_type_test.cc index 479526b5e3a..3f92d2e1ac6 100644 --- a/source/blender/functions/tests/FN_cpp_type_test.cc +++ b/source/blender/functions/tests/FN_cpp_type_test.cc @@ -3,6 +3,7 @@ #include "testing/testing.h" #include "FN_cpp_type.hh" +#include "FN_cpp_type_make.hh" namespace blender::fn::tests { -- cgit v1.2.3