MCPcopy Create free account
hub / github.com/andreiavrammsd/cpp-channel / array_storage

Class array_storage

include/msd/storage.hpp:127–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

125 */
126template <typename T, std::size_t N>
127class array_storage {
128 public:
129 static_assert(N > 0, "Capacity must be greater than zero.");
130
131 /**
132 * @brief The storage capacity.
133 *
134 * @attention Required for static storage.
135 */
136 static constexpr std::size_t capacity = N;
137
138 /**
139 * @brief Adds an element to the back of the array.
140 *
141 * @tparam Type Type of the element to insert.
142 * @param value The value to insert (perfect forwarded).
143 * @warning It's undefined behaviour to push into a full array.
144 */
145 template <typename Type>
146 void push_back(Type&& value)
147 {
148 array_[(front_ + size_) % N] = std::forward<Type>(value);
149 ++size_;
150 }
151
152 /**
153 * @brief Marks the front element as removed and moves it to the output.
154 *
155 * @param out Reference to the variable where the front element will be moved.
156 * @warning It's undefined behaviour to pop from an empty array.
157 */
158 void pop_front(T& out)
159 {
160 out = std::move(array_[front_]);
161 front_ = (front_ + 1) % N;
162 --size_;
163 }
164
165 /**
166 * @brief Returns the number of elements currently stored.
167 *
168 * @return Current size.
169 */
170 NODISCARD std::size_t size() const noexcept { return size_; }
171
172 private:
173 std::array<T, N> array_{};
174 std::size_t size_{0};
175 std::size_t front_{0};
176};
177
178template <typename T, std::size_t N>
179constexpr std::size_t array_storage<T, N>::capacity;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected