diff options
author | Jacques Lucke <jacques@blender.org> | 2022-07-07 20:19:01 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-07-07 20:19:18 +0300 |
commit | ba62e20af67e1de25f781456df7175fa1b31db67 (patch) | |
tree | 28e4dcd2701e94d4c4e5f9c2c41a9635da7e6389 /source/blender/blenlib/BLI_virtual_array.hh | |
parent | 7cfea48752ebc544104d57d19736c201a98bc3cd (diff) |
BLI: make some spans default constructible
`GSpan` and spans based on virtual arrays were not default constructible
before, which made them hard to use sometimes. It's generally fine for
spans to be empty.
The main thing the keep in mind is that the type pointer in `GSpan` may
be null now. Generally, code receiving spans as input can assume that
the type is not-null, but sometimes that may be valid. The old #type() method
that returned a reference to the type still exists. It asserts when the
type is null.
Diffstat (limited to 'source/blender/blenlib/BLI_virtual_array.hh')
-rw-r--r-- | source/blender/blenlib/BLI_virtual_array.hh | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh index 84f6223580a..56b2f9f6c6f 100644 --- a/source/blender/blenlib/BLI_virtual_array.hh +++ b/source/blender/blenlib/BLI_virtual_array.hh @@ -1130,6 +1130,9 @@ template<typename T> class VArraySpan final : public Span<T> { VArraySpan(VArray<T> varray) : Span<T>(), varray_(std::move(varray)) { + if (!varray_) { + return; + } this->size_ = varray_.size(); const CommonVArrayInfo info = varray_.common_info(); if (info.type == CommonVArrayInfo::Type::Span) { @@ -1146,6 +1149,9 @@ template<typename T> class VArraySpan final : public Span<T> { VArraySpan(VArraySpan &&other) : varray_(std::move(other.varray_)), owned_data_(std::move(other.owned_data_)) { + if (!varray_) { + return; + } this->size_ = varray_.size(); const CommonVArrayInfo info = varray_.common_info(); if (info.type == CommonVArrayInfo::Type::Span) { @@ -1184,11 +1190,17 @@ template<typename T> class MutableVArraySpan final : public MutableSpan<T> { bool show_not_saved_warning_ = true; public: + MutableVArraySpan() = default; + /* Create a span for any virtual array. This is cheap when the virtual array is a span itself. If * not, a new array has to be allocated as a wrapper for the underlying virtual array. */ MutableVArraySpan(VMutableArray<T> varray, const bool copy_values_to_span = true) : MutableSpan<T>(), varray_(std::move(varray)) { + if (!varray_) { + return; + } + this->size_ = varray_.size(); const CommonVArrayInfo info = varray_.common_info(); if (info.type == CommonVArrayInfo::Type::Span) { @@ -1212,10 +1224,14 @@ template<typename T> class MutableVArraySpan final : public MutableSpan<T> { owned_data_(std::move(owned_data_)), show_not_saved_warning_(other.show_not_saved_warning_) { + if (!varray_) { + return; + } + this->size_ = varray_.size(); const CommonVArrayInfo info = varray_.common_info(); if (info.type == CommonVArrayInfo::Type::Span) { - this->data_ = reinterpret_cast<T *>(info.data); + this->data_ = static_cast<T *>(const_cast<void *>(info.data)); } else { this->data_ = owned_data_.data(); @@ -1245,6 +1261,11 @@ template<typename T> class MutableVArraySpan final : public MutableSpan<T> { return *this; } + const VMutableArray<T> &varray() const + { + return varray_; + } + /* Write back all values from a temporary allocated array to the underlying virtual array. */ void save() { |