C++ Channel
Loading...
Searching...
No Matches
storage.hpp
Go to the documentation of this file.
1// Copyright (C) 2020-2025 Andrei Avram
2
3#ifndef MSD_CHANNEL_STORAGE_HPP_
4#define MSD_CHANNEL_STORAGE_HPP_
5
6#include "nodiscard.hpp"
7
8#include <array>
9#include <cstdlib>
10#include <queue>
11#include <vector>
12
15namespace msd {
16
22template <typename T>
24 public:
30 explicit queue_storage(std::size_t) {}
31
38 template <typename Type>
39 void push_back(Type&& value)
40 {
41 queue_.push(std::forward<Type>(value));
42 }
43
51 {
52 out = std::move(queue_.front());
53 queue_.pop();
54 }
55
61 NODISCARD std::size_t size() const noexcept { return queue_.size(); }
62
63 private:
64 std::queue<T> queue_;
65};
66
72template <typename T>
74 public:
82 explicit vector_storage(std::size_t capacity) { vector_.reserve(capacity); }
83
90 template <typename Type>
91 void push_back(Type&& value)
92 {
93 vector_.push_back(std::forward<Type>(value));
94 }
95
103 {
104 out = std::move(vector_.front());
105 vector_.erase(vector_.begin());
106 }
107
113 NODISCARD std::size_t size() const noexcept { return vector_.size(); }
114
115 private:
116 std::vector<T> vector_;
117};
118
126template <typename T, std::size_t N>
128 public:
129 static_assert(N > 0, "Capacity must be greater than zero.");
130
136 static constexpr std::size_t capacity = N;
137
145 template <typename Type>
146 void push_back(Type&& value)
147 {
148 array_[(front_ + size_) % N] = std::forward<Type>(value);
149 ++size_;
150 }
151
159 {
160 out = std::move(array_[front_]);
161 front_ = (front_ + 1) % N;
162 --size_;
163 }
164
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;
180
181} // namespace msd
182
183#endif // MSD_CHANNEL_STORAGE_HPP_
A fixed-size circular buffer using std::array.
Definition storage.hpp:127
static constexpr std::size_t capacity
The storage capacity.
Definition storage.hpp:136
void pop_front(T &out)
Marks the front element as removed and moves it to the output.
Definition storage.hpp:158
void push_back(Type &&value)
Adds an element to the back of the array.
Definition storage.hpp:146
NODISCARD std::size_t size() const noexcept
Returns the number of elements currently stored.
Definition storage.hpp:170
Thread-safe container for sharing data between threads.
Definition channel.hpp:87
A FIFO queue storage using std::queue.
Definition storage.hpp:23
NODISCARD std::size_t size() const noexcept
Returns the number of elements currently stored.
Definition storage.hpp:61
void push_back(Type &&value)
Adds an element to the back of the queue.
Definition storage.hpp:39
queue_storage(std::size_t)
Constructs the queue storage (parameter ignored, required for interface compatibility).
Definition storage.hpp:30
void pop_front(T &out)
Removes the front element from the queue and moves it to the output.
Definition storage.hpp:50
A FIFO queue storage using std::vector.
Definition storage.hpp:73
void push_back(Type &&value)
Adds an element to the back of the vector.
Definition storage.hpp:91
void pop_front(T &out)
Removes the front element from the vector and moves it to the output.
Definition storage.hpp:102
vector_storage(std::size_t capacity)
Constructs a queue storage with a given capacity.
Definition storage.hpp:82
NODISCARD std::size_t size() const noexcept
Returns the number of elements currently stored.
Definition storage.hpp:113