C++ Channel
Loading...
Searching...
No Matches
channel.hpp
Go to the documentation of this file.
1// Copyright (C) 2020-2026 Andrei Avram
2
3#ifndef MSD_CHANNEL_CHANNEL_HPP_
4#define MSD_CHANNEL_CHANNEL_HPP_
5
7#include "nodiscard.hpp"
8#include "storage.hpp"
9
10#include <condition_variable>
11#include <cstdlib>
12#include <mutex>
13#include <stdexcept>
14#include <type_traits>
15
17
18namespace msd {
19
23class closed_channel : public std::runtime_error {
24 public:
30 explicit closed_channel(const char* msg) : std::runtime_error{msg} {}
31};
32
39template <typename T>
41
54template <typename T>
59 static constexpr bool value = std::is_default_constructible<T>::value && std::is_move_constructible<T>::value &&
60 std::is_move_assignable<T>::value && std::is_destructible<T>::value;
61};
62
66template <typename, typename = void>
67struct is_static_storage : std::false_type {};
68
74template <typename Storage>
75struct is_static_storage<Storage, decltype((void)Storage::capacity, void())> : std::true_type {};
76
86template <typename T, typename Storage = default_storage<T>>
87class channel {
88 public:
89 static_assert(is_supported_type<T>::value, "Type T does not meet all requirements.");
90
94 using value_type = T;
95
100
104 using size_type = std::size_t;
105
111 template <typename S = Storage, typename std::enable_if<is_static_storage<S>::value, int>::type = 0>
112 constexpr channel() : capacity_{Storage::capacity}
113 {
114 }
115
119 template <typename S = Storage, typename std::enable_if<!is_static_storage<S>::value, int>::type = 0>
120 constexpr channel() : storage_{0}
121 {
122 }
123
129 template <typename S = Storage, typename std::enable_if<!is_static_storage<S>::value, int>::type = 0>
130 explicit constexpr channel(const size_type capacity) : storage_{capacity}, capacity_{capacity}
131 {
132 }
133
142 template <typename Type, typename Store>
144 channel<typename std::decay<Type>::type, Store>& chan, Type&& value);
145
153 template <typename Type, typename Store>
155
164 template <typename Type>
165 bool write(Type&& value)
166 {
167 {
168 std::unique_lock<std::mutex> lock{mtx_};
169 wait_before_write(lock);
170
171 if (is_closed_) {
172 return false;
173 }
174
175 storage_.push_back(std::forward<Type>(value));
176 }
177
178 cnd_.notify_one();
179
180 return true;
181 }
182
190 bool read(T& out)
191 {
192 {
193 std::unique_lock<std::mutex> lock{mtx_};
194 wait_before_read(lock);
195
196 if (storage_.size() == 0 && is_closed_) {
197 return false;
198 }
199
200 storage_.pop_front(out);
201 }
202
203 cnd_.notify_one();
204
205 return true;
206 }
207
213 NODISCARD size_type size() const noexcept
214 {
215 std::unique_lock<std::mutex> lock{mtx_};
216 return storage_.size();
217 }
218
225 NODISCARD bool empty() const noexcept
226 {
227 std::unique_lock<std::mutex> lock{mtx_};
228 return storage_.size() == 0;
229 }
230
234 void close() noexcept
235 {
236 {
237 std::unique_lock<std::mutex> lock{mtx_};
238 is_closed_ = true;
239 }
240 cnd_.notify_all();
241 }
242
249 NODISCARD bool closed() const noexcept
250 {
251 std::unique_lock<std::mutex> lock{mtx_};
252 return is_closed_;
253 }
254
261 NODISCARD bool drained() noexcept
262 {
263 std::unique_lock<std::mutex> lock{mtx_};
264 return storage_.size() == 0 && is_closed_;
265 }
266
272 iterator begin() noexcept(std::is_nothrow_default_constructible<T>::value &&
273 std::is_nothrow_move_assignable<T>::value)
274 {
276 }
277
283 iterator end() noexcept(std::is_nothrow_default_constructible<T>::value &&
284 std::is_nothrow_move_assignable<T>::value)
285 {
286 return blocking_iterator<channel<T, Storage>>{*this, true};
287 }
288
289 channel(const channel&) = delete;
290 channel& operator=(const channel&) = delete;
291 channel(channel&&) = delete;
292 channel& operator=(channel&&) = delete;
293 virtual ~channel() = default;
294
295 private:
296 Storage storage_;
297 std::condition_variable cnd_;
298 mutable std::mutex mtx_;
299 std::size_t capacity_{};
300 bool is_closed_{};
301
302 void wait_before_read(std::unique_lock<std::mutex>& lock)
303 {
304 cnd_.wait(lock, [this]() { return storage_.size() > 0 || is_closed_; });
305 };
306
307 void wait_before_write(std::unique_lock<std::mutex>& lock)
308 {
309 if (capacity_ > 0) {
310 cnd_.wait(lock, [this]() { return storage_.size() < capacity_ || is_closed_; });
311 }
312 }
313};
314
318template <typename T, typename Storage>
319channel<typename std::decay<T>::type, Storage>& operator<<(channel<typename std::decay<T>::type, Storage>& chan,
320 T&& value)
321{
322 if (!chan.write(std::forward<T>(value))) {
323 throw closed_channel{"cannot write on closed channel"};
324 }
325
326 return chan;
327}
328
332template <typename T, typename Storage>
334{
335 chan.read(out);
336
337 return chan;
338}
339
340} // namespace msd
341
342#endif // MSD_CHANNEL_CHANNEL_HPP_
channel< typename std::decay< T >::type, Storage > & operator<<(channel< typename std::decay< T >::type, Storage > &chan, T &&value)
Pushes an element into the channel.
Definition channel.hpp:319
channel< T, Storage > & operator>>(channel< T, Storage > &chan, T &out)
Pops an element from the channel.
Definition channel.hpp:333
queue_storage< T > default_storage
Default storage for msd::channel.
Definition channel.hpp:40
An iterator that blocks the current thread, waiting to fetch elements from the channel.
Definition blocking_iterator.hpp:22
Thread-safe container for sharing data between threads.
Definition channel.hpp:87
iterator end() noexcept(std::is_nothrow_default_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Returns an iterator representing the end of the channel.
Definition channel.hpp:283
friend channel< typename std::decay< Type >::type, Store > & operator<<(channel< typename std::decay< Type >::type, Store > &chan, Type &&value)
Pushes an element into the channel.
NODISCARD bool drained() noexcept
Checks if the channel has been closed and is empty.
Definition channel.hpp:261
constexpr channel(const size_type capacity)
Creates a buffered channel if Storage is not static (does not have static capacity member).
Definition channel.hpp:130
iterator begin() noexcept(std::is_nothrow_default_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
Returns an iterator to the beginning of the channel.
Definition channel.hpp:272
constexpr channel()
Creates a buffered channel if Storage is static (has static capacity member).
Definition channel.hpp:112
NODISCARD bool closed() const noexcept
Checks if the channel has been closed.
Definition channel.hpp:249
T value_type
The type of elements stored in the channel.
Definition channel.hpp:94
bool write(Type &&value)
Pushes an element into the channel.
Definition channel.hpp:165
std::size_t size_type
The type used to represent sizes and counts.
Definition channel.hpp:104
NODISCARD bool empty() const noexcept
Checks if the channel is empty.
Definition channel.hpp:225
void close() noexcept
Closes the channel, no longer accepting new elements.
Definition channel.hpp:234
friend channel< Type, Store > & operator>>(channel< Type, Store > &chan, Type &out)
Pops an element from the channel.
blocking_iterator< channel< T, Storage > > iterator
The iterator type used to traverse the channel.
Definition channel.hpp:99
bool read(T &out)
Pops an element from the channel.
Definition channel.hpp:190
NODISCARD size_type size() const noexcept
Returns the current size of the channel.
Definition channel.hpp:213
Exception thrown if trying to write on closed channel.
Definition channel.hpp:23
closed_channel(const char *msg)
Constructs the exception with an error message.
Definition channel.hpp:30
A FIFO queue storage using std::queue.
Definition storage.hpp:23
constexpr std::size_t array_storage< T, N >::capacity
Out-of-class definition of array_storage::capacity, required pre-C++17 when the member is odr-used.
Definition storage.hpp:182
Trait to check if a storage type has a static capacity member.
Definition channel.hpp:67
Trait to check if a type is supported by msd::channel.
Definition channel.hpp:55
static constexpr bool value
Indicates if the type meets all channel requirements.
Definition channel.hpp:59