Minecraft Community Edition 0.1.0
Loading...
Searching...
No Matches
Thread.hpp
Go to the documentation of this file.
1#pragma once
2#include "QEventBus.hpp"
3
12
13namespace mce::core {
14 class ThreadImpl;
15
22 struct ThreadFunc {
26 virtual ~ThreadFunc() {}
27
33 virtual void run() = 0;
34 };
35
40 template<typename F>
47
51 virtual void run() override { functor(); };
52
55 };
56
61 template<typename C>
68 ThreadMemberFunctor(void(C::* function)(), C* object) : functor(function), object(object) {}
69
76 virtual void run() override { functor(); };
77
79 void(C::* functor)();
80
83 };
84
90 template<typename C, typename... Args>
98 ThreadMemberFunctorWithArgs(void(C::* function)(Args...), C* object, Args&&... args) : functor(functor), object(object) {}
99
103 virtual void run() override {
104 call(std::index_sequence_for<Args...>{});
105 }
106
108 void(C::* functor)(Args...);
109
112
114 std::tuple<Args...> args;
115 private:
120 template <std::size_t... I>
121 void call(std::index_sequence<I...>) {
122 (object->*functor)(std::get<I>(args)...);
123 }
124 };
125
134 class Thread {
135 public:
142 template<typename F>
143 Thread(QEventBus& qBus, F function);
144
152 template<typename C>
153 Thread(QEventBus& qBus, void(C::* function)(), C* object);
154
164 template<typename C, typename... Args>
165 Thread(QEventBus& qBus, void(C::* function)(), C* object, Args&&... args);
166
170 ~Thread();
171
175 void launch();
176
180 void wait();
181
182 private:
183 friend class ThreadImpl;
184
191 void run();
192
194 ThreadImpl* pImpl;//pointer to implementation
195
197 ThreadFunc* entryPoint;
198
200 QEventBus& qBus;
201 };
202 template<typename F>
203 inline Thread::Thread(QEventBus& qBus, F function) :
204 pImpl(nullptr),
205 entryPoint(new ThreadFunctor<F>(function)),
206 qBus(qBus) {
207
208 }
209 template<typename C>
210 inline Thread::Thread(QEventBus& qBus, void(C::* function)(), C* object) :
211 pImpl(nullptr),
212 entryPoint(new ThreadMemberFunctor<C>(function, object)),
213 qBus(qBus) {
214
215 }
216 template<typename C, typename ...Args>
217 inline Thread::Thread(QEventBus& qBus, void(C::* function)(), C* object, Args && ...args) :
218 pImpl(nullptr),
219 entryPoint(new ThreadMemberFunctorWithArgs<C>(function, object)),
220 qBus(qBus) {
221
222 }
223}
Thread-safe event bus.
Thread-safe event bus.
Definition QEventBus.hpp:58
void launch()
Start execution of the stored callable on a new thread.
Definition Thread.cpp:10
~Thread()
Destructor; cleans up stored callable and implementation pointer.
Definition Thread.cpp:5
Thread(QEventBus &qBus, F function)
Construct a Thread from a generic callable (lambda, functor, function pointer).
Definition Thread.hpp:203
void wait()
Block until the thread has finished executing.
Definition Thread.cpp:17
Platform-specific thread implementation.
Definition ThreadImpl.hpp:32
Abstract type-erased callable interface used as a thread entry point.
Definition Thread.hpp:22
virtual ~ThreadFunc()
Virtual destructor to allow safe deletion via base pointer.
Definition Thread.hpp:26
virtual void run()=0
Execute the wrapped callable.
Adapter that wraps a generic functor/lambda.
Definition Thread.hpp:41
F functor
The stored functor instance.
Definition Thread.hpp:54
ThreadFunctor(F functor)
Construct the adapter with the provided functor.
Definition Thread.hpp:46
virtual void run() override
Invoke the stored functor.
Definition Thread.hpp:51
Adapter that wraps a member function without extra arguments.
Definition Thread.hpp:62
void(C::* functor)()
Pointer to the member function to invoke.
Definition Thread.hpp:79
ThreadMemberFunctor(void(C::*function)(), C *object)
Construct with member function pointer and target object.
Definition Thread.hpp:68
virtual void run() override
Invoke the stored member function on the stored object.
Definition Thread.hpp:76
C * object
Pointer to the object instance used when invoking the member function.
Definition Thread.hpp:82
Adapter that wraps a member function with arbitrary arguments.
Definition Thread.hpp:91
virtual void run() override
Invoke the stored member function forwarding the stored arguments.
Definition Thread.hpp:103
C * object
Pointer to the object instance used when invoking the member function.
Definition Thread.hpp:111
std::tuple< Args... > args
Tuple holding the arguments to forward to the member function.
Definition Thread.hpp:114
ThreadMemberFunctorWithArgs(void(C::*function)(Args...), C *object, Args &&... args)
Construct with member function pointer, target object and arguments.
Definition Thread.hpp:98
void(C::* functor)(Args...)
Pointer to the member function to invoke.
Definition Thread.hpp:108