diff options
author | Jacques Lucke <jacques@blender.org> | 2020-07-08 23:27:25 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-07-08 23:27:25 +0300 |
commit | 403384998a6bb5f428e15ced5503206b45032b25 (patch) | |
tree | 23e09b183faa196603aafb1572ed0f13b3964333 /source/blender/blenlib/BLI_vector.hh | |
parent | 4b85ed819dc0746f6fc46eab1019aab4c4fcf9c9 (diff) |
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.
Diffstat (limited to 'source/blender/blenlib/BLI_vector.hh')
-rw-r--r-- | source/blender/blenlib/BLI_vector.hh | 43 |
1 files changed, 31 insertions, 12 deletions
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<typename U, typename std::enable_if_t<std::is_convertible_v<U, T>> * = nullptr> + Vector(Span<U> 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<U, T>(values.data(), size, begin_); } /** @@ -152,24 +162,21 @@ class Vector { * This allows you to write code like: * Vector<int> vec = {3, 4, 5}; */ - Vector(const std::initializer_list<T> &values) : Vector(Span<T>(values)) + template<typename U, typename std::enable_if_t<std::is_convertible_v<U, T>> * = nullptr> + Vector(const std::initializer_list<U> &values) : Vector(Span<U>(values)) { } - /** - * Create a vector from an array ref. The values in the vector are copy constructed. - */ - Vector(Span<T> values, Allocator allocator = {}) : Vector(allocator) + template<typename U, + size_t N, + typename std::enable_if_t<std::is_convertible_v<U, T>> * = nullptr> + Vector(const std::array<U, N> &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<typename ContainerT> static Vector FromContainer(const ContainerT &container) { @@ -313,6 +320,18 @@ class Vector { return MutableSpan<T>(begin_, this->size()); } + template<typename U, typename std::enable_if_t<is_convertible_pointer_v<T, U>> * = nullptr> + operator Span<U>() const + { + return Span<U>(begin_, this->size()); + } + + template<typename U, typename std::enable_if_t<is_convertible_pointer_v<T, U>> * = nullptr> + operator MutableSpan<U>() + { + return MutableSpan<U>(begin_, this->size()); + } + Span<T> as_span() const { return *this; |