| 45 | typename Dims = std::vector<size_t>, |
| 46 | typename Alloc = memory::Allocator<T>> |
| 47 | class Array { |
| 48 | private: |
| 49 | static_assert(std::is_trivial_v<T>); // only handle trivial types |
| 50 | |
| 51 | /// @brief num of data objects |
| 52 | [[nodiscard]] constexpr auto size() const -> size_t { return array_impl::size(dims_); } |
| 53 | |
| 54 | /// @brief num of bytes for all data objects |
| 55 | [[nodiscard]] constexpr auto bytes() const -> size_t { return sizeof(T) * size(); } |
| 56 | |
| 57 | void destroy() { |
| 58 | size_t num_elements = size(); |
| 59 | atraits::deallocate(allocator_, pointer_, num_elements); |
| 60 | pointer_ = nullptr; |
| 61 | } |
| 62 | |
| 63 | public: |
| 64 | using allocator_type = Alloc; |
| 65 | using atraits = std::allocator_traits<allocator_type>; |
| 66 | using pointer = typename atraits::pointer; |
| 67 | using const_pointer = typename atraits::const_pointer; |
| 68 | |
| 69 | using value_type = T; |
| 70 | using reference = T&; |
| 71 | using const_reference = const T&; |
| 72 | |
| 73 | Array() = default; |
| 74 | |
| 75 | explicit Array(Dims dims, const Alloc& allocator) |
| 76 | : dims_(std::move(dims)), allocator_(allocator) { |
| 77 | size_t num_elements = size(); |
| 78 | pointer_ = atraits::allocate(allocator_, num_elements); |
| 79 | } |
| 80 | |
| 81 | explicit Array(Dims dims) : Array(std::move(dims), Alloc()) {} |
| 82 | |
| 83 | ~Array() noexcept { |
| 84 | if (pointer_ != nullptr) { |
| 85 | destroy(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// @brief move constructor |
| 90 | Array(Array&& other) noexcept |
| 91 | : pointer_{std::exchange(other.pointer_, nullptr)} |
| 92 | , dims_{std::move(other.dims_)} |
| 93 | , allocator_{std::move(other.allocator_)} {} |
| 94 | |
| 95 | Array& operator=(Array&& other) noexcept { |
| 96 | if (pointer_ != nullptr) { |
| 97 | destroy(); |
| 98 | } |
| 99 | |
| 100 | if constexpr (atraits::propagate_on_container_move_assignment::value) { |
| 101 | allocator_ = std::move(other.allocator_); |
| 102 | } |
| 103 | dims_ = std::exchange(other.dims_, Dims()); |
| 104 | pointer_ = std::exchange(other.pointer_, nullptr); |
nothing calls this directly
no outgoing calls
no test coverage detected