Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
QtThreadClosure.h
Go to the documentation of this file.
1#pragma once
2#include <QThread>
3#include <misc/gen/TClosure.h>
4
5namespace sf
6{
7
12 : public QThread
13 , public TClosure<void, QThread&, QObject*>
14{
15 public:
19 explicit QtThreadClosure(QObject* parent = nullptr);
20
24 explicit QtThreadClosure(const func_type& f, QObject* parent = nullptr);
25
26 ~QtThreadClosure() override;
27
28 protected:
32 void run() override;
33};
34
35inline QtThreadClosure::QtThreadClosure(QObject* parent)
36 : QThread(parent)
37 , TClosure<void, QThread&, QObject*>()
38{}
39
40inline QtThreadClosure::QtThreadClosure(const func_type& f, QObject* parent)
41 : QThread(parent)
42 , TClosure<void, QThread&, QObject*>(f)
43{}
44
46{
47 quit();
48 requestInterruption();
49 wait();
50}
51
53{
54 // Only call when assigned.
55 if (isAssigned())
56 {
57 // Object serves a parent for objects created in the thread itself.
58 QObject threadParent;
59 // Run the thread function/method.
60 call(*this, &threadParent);
61 }
62}
63
64}// namespace sf
Class for creating Qt threads easier.
Definition QtThreadClosure.h:14
~QtThreadClosure() override
Definition QtThreadClosure.h:45
QtThreadClosure(QObject *parent=nullptr)
Standard QT constructor.
Definition QtThreadClosure.h:35
void run() override
Overrides run function and calls the closure assigned one.
Definition QtThreadClosure.h:52
Encapsulates the std::function() template.
Definition TClosure.h:14
std::function< void(Args...)> func_type
Function type of the lambda or function.
Definition TClosure.h:19
void call(Args... args) const
Makes the call to the member function.
bool isAssigned() const
Checks if the closure is valid for calling.
Definition Application.h:10