Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
channel.hpp
Go to the documentation of this file.
1#pragma once
2
3//clashes with BOOST PP and Some Applications
4#pragma push_macro("N")
5#undef N
6
7#include <boost/asio.hpp>
8#include <boost/signals2.hpp>
9#include <boost/exception/diagnostic_information.hpp>
10
11namespace appbase {
12
13 using erased_channel_ptr = std::unique_ptr<void, void(*)(void*)>;
14
20 drop_exceptions() = default;
21 using result_type = void;
22
23 template<typename InputIterator>
24 result_type operator()(InputIterator first, InputIterator last) {
25 while (first != last) {
26 try {
27 *first;
28 } catch (...) {
29 // drop
30 }
31 ++first;
32 }
33 }
34 };
35
46 template<typename Data, typename DispatchPolicy>
47 class channel final {
48 public:
53 class handle {
54 public:
57 }
58
63 void unsubscribe() {
64 if (_handle.connected()) {
65 _handle.disconnect();
66 }
67 }
68
69 // This handle can be constructed and moved
70 handle() = default;
71 handle(handle&&) = default;
72 handle& operator= (handle&& rhs) = default;
73
74 // dont allow copying since this protects the resource
75 handle(const handle& ) = delete;
76 handle& operator= (const handle& ) = delete;
77
78 private:
79 using handle_type = boost::signals2::connection;
80 handle_type _handle;
81
88 handle(handle_type&& _handle)
89 :_handle(std::move(_handle))
90 {}
91
92 friend class channel;
93 };
94
100 void publish(int priority, const Data& data);
101
108 template<typename Callback>
109 handle subscribe(Callback cb) {
110 return handle(_signal.connect(cb));
111 }
112
121 auto set_dispatcher(const DispatchPolicy& policy ) -> std::enable_if_t<std::is_copy_constructible<DispatchPolicy>::value,void>
122 {
123 _signal.set_combiner(policy);
124 }
125
130 auto connections = _signal.num_slots();
131 return connections > 0;
132 }
133
134 private:
135 channel()
136 {
137 }
138
139 virtual ~channel() = default;
140
147 static void deleter(void* erased_channel_ptr) {
148 auto ptr = reinterpret_cast<channel*>(erased_channel_ptr);
149 delete ptr;
150 }
151
158 static channel* get_channel(erased_channel_ptr& ptr) {
159 return reinterpret_cast<channel*>(ptr.get());
160 }
161
166 static erased_channel_ptr make_unique()
167 {
168 return erased_channel_ptr(new channel(), &deleter);
169 }
170
171 boost::signals2::signal<void(const Data&), DispatchPolicy> _signal;
172
174 };
175
182 template< typename Tag, typename Data, typename DispatchPolicy = drop_exceptions >
187
188 template <typename...Ts>
190
191 std::false_type is_channel_decl_impl(...);
192
193 template <typename T>
194 using is_channel_decl = decltype(is_channel_decl_impl(std::declval<T*>()));
195}
196
197#pragma pop_macro("N")
handle(handle &&)=default
handle & operator=(handle &&rhs)=default
handle(const handle &)=delete
handle subscribe(Callback cb)
Definition channel.hpp:109
void publish(int priority, const Data &data)
bool has_subscribers()
Definition channel.hpp:129
auto set_dispatcher(const DispatchPolicy &policy) -> std::enable_if_t< std::is_copy_constructible< DispatchPolicy >::value, void >
Definition channel.hpp:121
std::true_type is_channel_decl_impl(const channel_decl< Ts... > *)
decltype(is_channel_decl_impl(std::declval< T * >())) is_channel_decl
Definition channel.hpp:194
std::unique_ptr< void, void(*)(void *)> erased_channel_ptr
Definition channel.hpp:13
Definition name.hpp:106
Definition zm2.cpp:48
result_type operator()(InputIterator first, InputIterator last)
Definition channel.hpp:24