Loading...
Searching...
No Matches
Thread.hpp
Go to the documentation of this file.
#pragma once
#include "QEventBus.hpp"
namespace mce::core {
class ThreadImpl;
struct ThreadFunc {
virtual ~ThreadFunc() {}
virtual void run() = 0;
};
template<typename F>
struct ThreadFunctor : ThreadFunc {
ThreadFunctor(F functor) : functor(functor) {}
virtual void run() override { functor(); };
F functor;
};
template<typename C>
struct ThreadMemberFunctor : ThreadFunc {
ThreadMemberFunctor(void(C::* function)(), C* object) : functor(function), object(object) {}
virtual void run() override { functor(); };
void(C::* functor)();
C* object;
};
template<typename C, typename... Args>
struct ThreadMemberFunctorWithArgs : ThreadFunc {
ThreadMemberFunctorWithArgs(void(C::* function)(Args...), C* object, Args&&... args) : functor(functor), object(object) {}
virtual void run() override {
call(std::index_sequence_for<Args...>{});
}
void(C::* functor)(Args...);
C* object;
std::tuple<Args...> args;
private:
template <std::size_t... I>
void call(std::index_sequence<I...>) {
(object->*functor)(std::get<I>(args)...);
}
};
class Thread {
public:
template<typename F>
Thread(QEventBus& qBus, F function);
template<typename C>
Thread(QEventBus& qBus, void(C::* function)(), C* object);
template<typename C, typename... Args>
Thread(QEventBus& qBus, void(C::* function)(), C* object, Args&&... args);
~Thread();
void launch();
void wait();
private:
friend class ThreadImpl;
void run();
ThreadImpl* pImpl;//pointer to implementation
ThreadFunc* entryPoint;
QEventBus& qBus;
};
template<typename F>
inline Thread::Thread(QEventBus& qBus, F function) :
pImpl(nullptr),
entryPoint(new ThreadFunctor<F>(function)),
qBus(qBus) {
}
template<typename C>
inline Thread::Thread(QEventBus& qBus, void(C::* function)(), C* object) :
pImpl(nullptr),
entryPoint(new ThreadMemberFunctor<C>(function, object)),
qBus(qBus) {
}
template<typename C, typename ...Args>
inline Thread::Thread(QEventBus& qBus, void(C::* function)(), C* object, Args && ...args) :
pImpl(nullptr),
entryPoint(new ThreadMemberFunctorWithArgs<C>(function, object)),
qBus(qBus) {
}
}
QEventBus.hpp
Thread-safe event bus.
mce::core::QEventBus
Thread-safe event bus.
Definition QEventBus.hpp:58
mce::core::Thread::launch
void launch()
Start execution of the stored callable on a new thread.
Definition Thread.cpp:10
mce::core::Thread::~Thread
~Thread()
Destructor; cleans up stored callable and implementation pointer.
Definition Thread.cpp:5
mce::core::Thread::Thread
Thread(QEventBus &qBus, F function)
Construct a Thread from a generic callable (lambda, functor, function pointer).
Definition Thread.hpp:203
mce::core::Thread::wait
void wait()
Block until the thread has finished executing.
Definition Thread.cpp:17
mce::core::ThreadImpl
Platform-specific thread implementation.
Definition ThreadImpl.hpp:32
mce::core::ThreadFunc
Abstract type-erased callable interface used as a thread entry point.
Definition Thread.hpp:22
mce::core::ThreadFunc::~ThreadFunc
virtual ~ThreadFunc()
Virtual destructor to allow safe deletion via base pointer.
Definition Thread.hpp:26
mce::core::ThreadFunc::run
virtual void run()=0
Execute the wrapped callable.
mce::core::ThreadFunctor
Adapter that wraps a generic functor/lambda.
Definition Thread.hpp:41
mce::core::ThreadFunctor::functor
F functor
The stored functor instance.
Definition Thread.hpp:54
mce::core::ThreadFunctor::ThreadFunctor
ThreadFunctor(F functor)
Construct the adapter with the provided functor.
Definition Thread.hpp:46
mce::core::ThreadFunctor::run
virtual void run() override
Invoke the stored functor.
Definition Thread.hpp:51
mce::core::ThreadMemberFunctor
Adapter that wraps a member function without extra arguments.
Definition Thread.hpp:62
mce::core::ThreadMemberFunctor::functor
void(C::* functor)()
Pointer to the member function to invoke.
Definition Thread.hpp:79
mce::core::ThreadMemberFunctor::ThreadMemberFunctor
ThreadMemberFunctor(void(C::*function)(), C *object)
Construct with member function pointer and target object.
Definition Thread.hpp:68
mce::core::ThreadMemberFunctor::run
virtual void run() override
Invoke the stored member function on the stored object.
Definition Thread.hpp:76
mce::core::ThreadMemberFunctor::object
C * object
Pointer to the object instance used when invoking the member function.
Definition Thread.hpp:82
mce::core::ThreadMemberFunctorWithArgs
Adapter that wraps a member function with arbitrary arguments.
Definition Thread.hpp:91
mce::core::ThreadMemberFunctorWithArgs::run
virtual void run() override
Invoke the stored member function forwarding the stored arguments.
Definition Thread.hpp:103
mce::core::ThreadMemberFunctorWithArgs::object
C * object
Pointer to the object instance used when invoking the member function.
Definition Thread.hpp:111
mce::core::ThreadMemberFunctorWithArgs::args
std::tuple< Args... > args
Tuple holding the arguments to forward to the member function.
Definition Thread.hpp:114
mce::core::ThreadMemberFunctorWithArgs::ThreadMemberFunctorWithArgs
ThreadMemberFunctorWithArgs(void(C::*function)(Args...), C *object, Args &&... args)
Construct with member function pointer, target object and arguments.
Definition Thread.hpp:98
mce::core::ThreadMemberFunctorWithArgs::functor
void(C::* functor)(Args...)
Pointer to the member function to invoke.
Definition Thread.hpp:108