diff options
Diffstat (limited to 'src/LazyArray.h')
-rw-r--r-- | src/LazyArray.h | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/src/LazyArray.h b/src/LazyArray.h index dbb772585..7d7c66e2d 100644 --- a/src/LazyArray.h +++ b/src/LazyArray.h @@ -4,14 +4,12 @@ /** A dynamic array that defers allocation to the first modifying access. Const references from the array before allocation will all be to the same default constructed value. It is therefore important that default constructed values are indistinguishable from each other. */ -template <typename T> -class cLazyArray +template <typename T> class cLazyArray { - static_assert((!std::is_reference<T>::value && !std::is_array<T>::value), - "cLazyArray<T>: T must be a value type"); - static_assert(std::is_default_constructible<T>::value, - "cLazyArray<T>: T must be default constructible"); -public: + static_assert((!std::is_reference<T>::value && !std::is_array<T>::value), "cLazyArray<T>: T must be a value type"); + static_assert(std::is_default_constructible<T>::value, "cLazyArray<T>: T must be default constructible"); + + public: using value_type = T; using pointer = T *; using const_pointer = const T *; @@ -21,14 +19,14 @@ public: using iterator = pointer; using const_iterator = const_pointer; - cLazyArray(size_type a_Size) noexcept: - m_Size{ a_Size } + cLazyArray(size_type a_Size) noexcept : + m_Size {a_Size} { ASSERT(a_Size > 0); } - cLazyArray(const cLazyArray & a_Other): - m_Size{ a_Other.m_Size } + cLazyArray(const cLazyArray & a_Other) : + m_Size {a_Other.m_Size} { if (a_Other.IsStorageAllocated()) { @@ -37,45 +35,41 @@ public: } } - cLazyArray(cLazyArray && a_Other) noexcept: - m_Array{ std::move(a_Other.m_Array) }, - m_Size{ a_Other.m_Size } + cLazyArray(cLazyArray && a_Other) noexcept : + m_Array {std::move(a_Other.m_Array)}, m_Size {a_Other.m_Size} { } - cLazyArray & operator = (const cLazyArray & a_Other) + cLazyArray & operator=(const cLazyArray & a_Other) { cLazyArray(a_Other).swap(*this); return *this; } - cLazyArray & operator = (cLazyArray && a_Other) noexcept + cLazyArray & operator=(cLazyArray && a_Other) noexcept { m_Array = std::move(a_Other.m_Array); m_Size = a_Other.m_Size; return *this; } - T & operator [] (size_type a_Idx) + T & operator[](size_type a_Idx) { ASSERT((0 <= a_Idx) && (a_Idx < m_Size)); return data()[a_Idx]; } - const T & operator [] (size_type a_Idx) const - { - return GetAt(a_Idx); - } + const T & operator[](size_type a_Idx) const { return GetAt(a_Idx); } // STL style interface const_iterator cbegin() const { return data(); } - iterator begin() { return data(); } - const_iterator begin() const { return cbegin(); } + iterator begin() { return data(); } + const_iterator begin() const { return cbegin(); } const_iterator cend() const { return data() + m_Size; } - iterator end() { return data() + m_Size; } - const_iterator end() const { return cend(); } + iterator end() { return data() + m_Size; } + const_iterator end() const { return cend(); } size_type size() const noexcept { return m_Size; } @@ -101,10 +95,7 @@ public: std::swap(m_Size, a_Other.m_Size); } - friend void swap(cLazyArray & a_Lhs, cLazyArray & a_Rhs) noexcept - { - a_Lhs.swap(a_Rhs); - } + friend void swap(cLazyArray & a_Lhs, cLazyArray & a_Rhs) noexcept { a_Lhs.swap(a_Rhs); } // Extra functions to help avoid allocation @@ -126,9 +117,8 @@ public: /** Returns true if the array has already been allocated. */ bool IsStorageAllocated() const noexcept { return (m_Array != nullptr); } -private: + private: // Mutable so const data() can allocate the array mutable std::unique_ptr<T[]> m_Array; size_type m_Size; }; - |