From 403384998a6bb5f428e15ced5503206b45032b25 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 8 Jul 2020 22:27:25 +0200 Subject: BLI: improve constructors and conversions to span This allows us to avoid many calls to `as_span()` methods. I will remove those in the next commit. Furthermore, constructors of Vector and Array can convert from one type to another now. I tested these changes on Linux with gcc and on Windows. --- source/blender/blenlib/BLI_array.hh | 20 +++++++++++--- source/blender/blenlib/BLI_memory_utils.hh | 36 +++++++++++++++++++++++++ source/blender/blenlib/BLI_span.hh | 9 +++++-- source/blender/blenlib/BLI_vector.hh | 43 +++++++++++++++++++++--------- 4 files changed, 91 insertions(+), 17 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_array.hh b/source/blender/blenlib/BLI_array.hh index 18f9aad1000..c7a9c49c972 100644 --- a/source/blender/blenlib/BLI_array.hh +++ b/source/blender/blenlib/BLI_array.hh @@ -89,17 +89,19 @@ class Array { /** * Create a new array that contains copies of all values. */ - Array(Span values, Allocator allocator = {}) : allocator_(allocator) + template> * = nullptr> + Array(Span values, Allocator allocator = {}) : allocator_(allocator) { size_ = values.size(); data_ = this->get_buffer_for_size(values.size()); - uninitialized_copy_n(values.data(), size_, data_); + uninitialized_convert_n(values.data(), size_, data_); } /** * Create a new array that contains copies of all values. */ - Array(const std::initializer_list &values) : Array(Span(values)) + template> * = nullptr> + Array(const std::initializer_list &values) : Array(Span(values)) { } @@ -219,6 +221,18 @@ class Array { return MutableSpan(data_, size_); } + template> * = nullptr> + operator Span() const + { + return Span(data_, size_); + } + + template> * = nullptr> + operator MutableSpan() + { + return MutableSpan(data_, size_); + } + Span as_span() const { return *this; diff --git a/source/blender/blenlib/BLI_memory_utils.hh b/source/blender/blenlib/BLI_memory_utils.hh index 16d6227a551..133615f0f31 100644 --- a/source/blender/blenlib/BLI_memory_utils.hh +++ b/source/blender/blenlib/BLI_memory_utils.hh @@ -26,6 +26,7 @@ #include #include +#include #include "BLI_utildefines.h" @@ -134,6 +135,32 @@ template void uninitialized_copy_n(const T *src, uint n, T *dst) } } +/** + * Convert n values from type `From` to type `To`. + * + * Exception Safety: Strong. + * + * Before: + * src: initialized + * dst: uninitialized + * After: + * src: initialized + * dst: initialized + */ +template void uninitialized_convert_n(const From *src, uint n, To *dst) +{ + uint current = 0; + try { + for (; current < n; current++) { + new ((void *)(dst + current)) To((To)src[current]); + } + } + catch (...) { + destruct_n(dst, current); + throw; + } +} + /** * Move n values from src to dst. * @@ -364,6 +391,15 @@ template class TypedBuffer { class NoInitialization { }; +/** + * Helper variable that checks if a pointer type can be converted into another pointer type without + * issues. Possible issues are casting away const and casting a pointer to a child class. + * Adding const or casting to a parent class is fine. + */ +template +inline constexpr bool is_convertible_pointer_v = + std::is_convertible_v &&std::is_pointer_v &&std::is_pointer_v; + } // namespace blender #endif /* __BLI_MEMORY_UTILS_HH__ */ diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh index 9688c93ec14..5c841787520 100644 --- a/source/blender/blenlib/BLI_span.hh +++ b/source/blender/blenlib/BLI_span.hh @@ -100,6 +100,11 @@ template class Span { { } + template> * = nullptr> + Span(const U *start, uint size) : start_((const T *)start), size_(size) + { + } + /** * Reference an initializer_list. Note that the data in the initializer_list is only valid until * the expression containing it is fully computed. @@ -128,8 +133,8 @@ template class Span { * Span -> Span * Span -> Span */ - template> * = nullptr> - Span(Span array) : Span((T *)array.data(), array.size()) + template> * = nullptr> + Span(Span array) : start_((T *)array.data()), size_(array.size()) { } diff --git a/source/blender/blenlib/BLI_vector.hh b/source/blender/blenlib/BLI_vector.hh index f608290f5df..df885588d9b 100644 --- a/source/blender/blenlib/BLI_vector.hh +++ b/source/blender/blenlib/BLI_vector.hh @@ -141,9 +141,19 @@ class Vector { */ Vector(uint size, const T &value) : Vector() { + this->resize(size, value); + } + + /** + * Create a vector from an array ref. The values in the vector are copy constructed. + */ + template> * = nullptr> + Vector(Span values, Allocator allocator = {}) : Vector(allocator) + { + const uint size = values.size(); this->reserve(size); this->increase_size_by_unchecked(size); - blender::uninitialized_fill_n(begin_, size, value); + uninitialized_convert_n(values.data(), size, begin_); } /** @@ -152,24 +162,21 @@ class Vector { * This allows you to write code like: * Vector vec = {3, 4, 5}; */ - Vector(const std::initializer_list &values) : Vector(Span(values)) + template> * = nullptr> + Vector(const std::initializer_list &values) : Vector(Span(values)) { } - /** - * Create a vector from an array ref. The values in the vector are copy constructed. - */ - Vector(Span values, Allocator allocator = {}) : Vector(allocator) + template> * = nullptr> + Vector(const std::array &values) : Vector(Span(values)) { - const uint size = values.size(); - this->reserve(size); - this->increase_size_by_unchecked(size); - blender::uninitialized_copy_n(values.data(), size, begin_); } /** - * Create a vector from any container. It must be possible to use the container in a range-for - * loop. + * Create a vector from any container. It must be possible to use the container in a + * range-for loop. */ template static Vector FromContainer(const ContainerT &container) { @@ -313,6 +320,18 @@ class Vector { return MutableSpan(begin_, this->size()); } + template> * = nullptr> + operator Span() const + { + return Span(begin_, this->size()); + } + + template> * = nullptr> + operator MutableSpan() + { + return MutableSpan(begin_, this->size()); + } + Span as_span() const { return *this; -- cgit v1.2.3