-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.hpp
More file actions
82 lines (60 loc) · 1.79 KB
/
channel.hpp
File metadata and controls
82 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <utility>
#include "memory.hpp"
#include "optional.hpp"
#include "sync_queue.hpp"
namespace ift630 {
using std::pair;
template <typename T>
class Chan {
shared_ptr<SyncQueue<T>> _queue;
public:
explicit Chan(shared_ptr<SyncQueue<T>> queue) noexcept : _queue{std::move(queue)} {}
Chan(Chan&&) noexcept = default;
Chan(const Chan&) = delete;
virtual ~Chan() noexcept = default;
protected:
[[nodiscard]] auto queue() noexcept -> shared_ptr<SyncQueue<T>> { return _queue; }
public:
auto operator=(Chan&&) noexcept -> Chan& = default;
auto operator=(const Chan&) -> Chan& = delete;
};
template <typename T>
class SendChan : private Chan<T> {
using Chan<T>::queue;
public:
using Chan<T>::Chan;
void send(const T& message) { queue()->push(message); }
void send(T&& message) { queue()->push(std::forward<T>(message)); }
auto operator<<(const T& message) -> SendChan& {
send(message);
return *this;
}
auto operator<<(T&& message) -> SendChan& {
send(std::forward<T>(message));
return *this;
}
};
template <typename T>
class ReceiveChan : private Chan<T> {
using Chan<T>::queue;
public:
using Chan<T>::Chan;
auto receive() -> T { return queue()->pop(); }
auto try_receive() -> optional<T> { return queue->try_pop(); }
auto operator>>(T& item) -> ReceiveChan& {
item = receive();
return *this;
}
auto operator>>(optional<T>& item) -> ReceiveChan& {
item = try_receive();
return *this;
}
};
/// Creates a pair of objects representing a Single Producer, Single Consummer (SPSC) channel
template <typename T>
auto make_chan(size_t size) noexcept -> pair<SendChan<T>, ReceiveChan<T>> {
const auto queue = std::make_shared<SyncQueue<T>>(size);
return {SendChan<T>{queue}, ReceiveChan<T>{queue}};
}
} // namespace ift630