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