diff options
Diffstat (limited to 'source/blender/blenlib/BLI_span.hh')
-rw-r--r-- | source/blender/blenlib/BLI_span.hh | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh index 5f55efe3f63..f4960df8ab9 100644 --- a/source/blender/blenlib/BLI_span.hh +++ b/source/blender/blenlib/BLI_span.hh @@ -132,12 +132,11 @@ template<typename T> class Span { } /** - * Support implicit conversions like the ones below: + * Support implicit conversions like the one below: * Span<T *> -> Span<const T *> */ - template<typename U, typename std::enable_if_t<is_span_convertible_pointer_v<U, T>> * = nullptr> - constexpr Span(Span<U> array) : data_(static_cast<const T *>(array.data())), size_(array.size()) + constexpr Span(Span<U> span) : data_(static_cast<const T *>(span.data())), size_(span.size()) { } @@ -467,11 +466,27 @@ template<typename T> class MutableSpan { { } + /** + * Support implicit conversions like the one below: + * MutableSpan<T *> -> MutableSpan<const T *> + */ + template<typename U, typename std::enable_if_t<is_span_convertible_pointer_v<U, T>> * = nullptr> + constexpr MutableSpan(MutableSpan<U> span) + : data_(static_cast<T *>(span.data())), size_(span.size()) + { + } + constexpr operator Span<T>() const { return Span<T>(data_, size_); } + template<typename U, typename std::enable_if_t<is_span_convertible_pointer_v<T, U>> * = nullptr> + constexpr operator Span<U>() const + { + return Span<U>(static_cast<const U *>(data_), size_); + } + /** * Returns the number of elements in the array. */ @@ -653,12 +668,13 @@ template<typename T> class MutableSpan { /** * Returns a new span to the same underlying memory buffer. No conversions are done. + * The caller is responsible for making sure that the type cast is valid. */ template<typename NewT> constexpr MutableSpan<NewT> cast() const { BLI_assert((size_ * sizeof(T)) % sizeof(NewT) == 0); int64_t new_size = size_ * sizeof(T) / sizeof(NewT); - return MutableSpan<NewT>(reinterpret_cast<NewT *>(data_), new_size); + return MutableSpan<NewT>((NewT *)data_, new_size); } }; |