Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
ThreadRelay.h
Go to the documentation of this file.
1#pragma once
2#include <atomic>
4#include <misc/gen/Sync.h>
5#include <misc/gen/Thread.h>
6#include <misc/global.h>
7
8namespace sf
9{
10
37{
38 public:
43
49
53 void makeOwner(Thread::id_type threadId = 0);
54
59 {
60 public:
64 inline explicit RelayBase(ThreadRelay& tr);
65
69 virtual ~RelayBase() = default;
70
74 virtual void call() = 0;
75
80 explicit operator bool();
81
82 protected:
86 std::atomic<bool> _sentry{false};
95
97 };
98
102 template<typename ClassType, typename MethodType, typename Ret>
103 class Relay0 final : public RelayBase
104 {
105 public:
106 Relay0(ThreadRelay& tr, ClassType* cls, MethodType mtd, Ret& ret)
107 : RelayBase(tr)
108 , _cls(cls)
109 , _mtd(mtd)
110 , _ret(ret)
111 {}
112
113 private:
114 void call() override
115 {
116 _ret = ((*_cls).*static_cast<Ret (ClassType::*)()>(_mtd))();
117 }
118
119 ClassType* _cls;
120 MethodType _mtd;
121 Ret& _ret;
122 };
123
127 template<typename ClassType, typename MethodType, typename Ret, typename Arg1>
128 class Relay1 : public RelayBase
129 {
130 public:
131 Relay1(ThreadRelay& tr, ClassType* cls, MethodType mtd, Ret& ret, Arg1& arg1)
132 : RelayBase(tr)
133 , _cls(cls)
134 , _mtd(mtd)
135 , _ret(ret)
136 , _arg1(arg1)
137 {}
138
139 private:
140 void call() override
141 {
142 _ret = ((*_cls).*static_cast<Ret (ClassType::*)(Arg1)>(_mtd))(_arg1);
143 }
144
145 ClassType* _cls;
146 MethodType _mtd;
147 Ret& _ret;
148 Arg1& _arg1;
149 };
150
154 template<typename ClassType, typename MethodType, typename Ret, typename Arg1, typename Arg2>
155 class Relay2 : public RelayBase
156 {
157 public:
158 Relay2(ThreadRelay& tr, ClassType* cls, MethodType mtd, Ret& ret, Arg1& arg1, Arg2& arg2)
159 : RelayBase(tr)
160 , _cls(cls)
161 , _mtd(mtd)
162 , _ret(ret)
163 , _arg1(arg1)
164 , _arg2(arg2)
165 {}
166
167 private:
168 void call() override
169 {
170 _ret = ((*_cls).*static_cast<Ret (ClassType::*)(Arg1, Arg2)>(_mtd))(_arg1, _arg2);
171 }
172
173 ClassType* _cls;
174 MethodType _mtd;
175 Ret& _ret;
176 Arg1& _arg1;
177 Arg2& _arg2;
178 };
179
183 template<typename ClassType, typename MethodType, typename Ret, typename Arg1, typename Arg2, typename Arg3>
184 class Relay3 : public RelayBase
185 {
186 public:
187 Relay3(ThreadRelay& tr, ClassType* cls, MethodType mtd, Ret& ret, Arg1& arg1, Arg2& arg2, Arg3& arg3)
188 : RelayBase(tr)
189 , _cls(cls)
190 , _mtd(mtd)
191 , _ret(ret)
192 , _arg1(arg1)
193 , _arg2(arg2)
194 , _arg3(arg3)
195 {}
196
197 private:
198 void call() override
199 {
200 _ret = ((*_cls).*static_cast<Ret (ClassType::*)(Arg1, Arg2, Arg3)>(_mtd))(_arg1, _arg2, _arg3);
201 }
202
203 ClassType* _cls;
204 MethodType _mtd;
205 Ret& _ret;
206 Arg1& _arg1;
207 Arg2& _arg2;
208 Arg3& _arg3;
209 };
210
214 template<typename ClassType, typename MethodType, typename Ret, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
215 class Relay4 final : public RelayBase
216 {
217 public:
218 Relay4(ThreadRelay& tr, ClassType* cls, MethodType mtd, Ret& ret, Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4)
219 : RelayBase(tr)
220 , _cls(cls)
221 , _mtd(mtd)
222 , _ret(ret)
223 , _arg1(arg1)
224 , _arg2(arg2)
225 , _arg3(arg3)
226 , _arg4(arg4)
227 {}
228
229 private:
230 void call() override
231 {
232 _ret = ((*_cls).*static_cast<Ret (ClassType::*)(Arg1, Arg2, Arg3, Arg4)>(_mtd))(_arg1, _arg2, _arg3, _arg4);
233 }
234
235 ClassType* _cls;
236 MethodType _mtd;
237 Ret& _ret;
238 Arg1& _arg1;
239 Arg2& _arg2;
240 Arg3& _arg3;
241 Arg4& _arg4;
242 };
243
244 private:
245 // Thread in which the methods are called to be called in.
246 Thread::id_type _threadId;
247 // List containing the instance waiting to be called.
248 std::vector<RelayBase*> _list{};
249};
250
252 : _tr(tr)
253{}
254
255}// namespace sf
Semaphore class built around posix sem_xxx functionality.
Definition Semaphore.h:13
Sync provides a system-independent interface to build classes that act like monitors,...
Definition Sync.h:56
Template for a method having 0 arguments.
Definition ThreadRelay.h:104
Relay0(ThreadRelay &tr, ClassType *cls, MethodType mtd, Ret &ret)
Definition ThreadRelay.h:106
Template for a method having 1 argument.
Definition ThreadRelay.h:129
Relay1(ThreadRelay &tr, ClassType *cls, MethodType mtd, Ret &ret, Arg1 &arg1)
Definition ThreadRelay.h:131
Template for a method having 2 arguments.
Definition ThreadRelay.h:156
Relay2(ThreadRelay &tr, ClassType *cls, MethodType mtd, Ret &ret, Arg1 &arg1, Arg2 &arg2)
Definition ThreadRelay.h:158
Template for a method having 3 arguments.
Definition ThreadRelay.h:185
Relay3(ThreadRelay &tr, ClassType *cls, MethodType mtd, Ret &ret, Arg1 &arg1, Arg2 &arg2, Arg3 &arg3)
Definition ThreadRelay.h:187
Template for a method having 4 arguments.
Definition ThreadRelay.h:216
Relay4(ThreadRelay &tr, ClassType *cls, MethodType mtd, Ret &ret, Arg1 &arg1, Arg2 &arg2, Arg3 &arg3, Arg4 &arg4)
Definition ThreadRelay.h:218
Base class for the ThreadRelay::Relay0,1,2,3 templates for storing in a list.
Definition ThreadRelay.h:59
Semaphore _semaphore
Holds the semaphore to stop the thread until the method is executed.
Definition ThreadRelay.h:94
virtual ~RelayBase()=default
Default virtual destructor.
RelayBase(ThreadRelay &tr)
Constructor.
Definition ThreadRelay.h:251
friend ThreadRelay
Definition ThreadRelay.h:96
virtual void call()=0
Pure virtual function to override in the derived template class to call a method.
ThreadRelay & _tr
Prevents the execution of the method more the once.
Definition ThreadRelay.h:90
Provides a way to stop executing a thread until the main thread enables the thread again.
Definition ThreadRelay.h:37
void makeOwner(Thread::id_type threadId=0)
The calling thread takes thread execution ownership when zero is passed or sets it when not zero.
int checkForWork()
Called by main thread for instance. When a lock of this calling thread is requested.
ThreadRelay()
Default constructor which also sets the thread execution ownership.
pid_t id_type
Local declaration of the thread id type.
Definition Thread.h:41
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10