kDB: Knowledge DataBase
Loading...
Searching...
No Matches
knowl
src
knowCore
AsynchronousFunction.h
1
#pragma once
2
3
#include <functional>
4
#include <type_traits>
5
6
#include <QObject>
7
8
class
QThread;
9
10
namespace
knowCore
11
{
12
namespace
details
13
{
14
class
AsynchronousFunctionCall
:
public
QObject
15
{
16
Q_OBJECT
17
public
slots:
18
void
call(
void
* arg) { m_function(arg); }
19
void
setFunction(
const
std::function<
void
(
void
*)>& _function) { m_function = _function; }
20
private
:
21
std::function<void(
void
*)> m_function;
22
};
23
}
// namespace details
27
template
<
typename
_TRet_,
typename
... _Args_>
28
class
AsynchronousFunction
29
{
30
typedef
std::tuple<std::decay_t<_Args_>...> ArgsTuple;
31
public
:
32
AsynchronousFunction
(
const
std::function<_TRet_(_Args_...)>& _function, QThread* _thread,
33
const
std::function<
void
(
const
_TRet_&)>& _ret_handler)
34
: m_call(
new
details::AsynchronousFunctionCall
)
35
{
36
m_call->moveToThread(_thread);
37
m_call->setFunction(
38
[_function, _ret_handler](
void
* _args)
39
{
40
ArgsTuple* args =
reinterpret_cast<
ArgsTuple*
>
(_args);
41
_ret_handler(std::apply(_function, *args));
42
delete
args;
43
});
44
}
45
~AsynchronousFunction
() {
delete
m_call; }
49
void
operator()
(_Args_... _args)
50
{
51
ArgsTuple* t
52
=
new
ArgsTuple(
/*std::make_tuple<details::remove_cvref<_Args_...>>(*/
_args...)
/*)*/
;
53
QMetaObject::invokeMethod(m_call,
"call"
, Qt::QueuedConnection, Q_ARG(
void
*, t));
54
}
55
private
:
56
details::AsynchronousFunctionCall
* m_call;
57
};
58
template
<
typename
... _Args_>
59
class
AsynchronousFunction
<void, _Args_...>
60
{
61
typedef
std::tuple<std::decay_t<_Args_>...> ArgsTuple;
62
public
:
63
AsynchronousFunction
(
const
std::function<
void
(_Args_...)>& _function, QThread* _thread)
64
: m_call(
new
details::AsynchronousFunctionCall
)
65
{
66
m_call->moveToThread(_thread);
67
m_call->setFunction(
68
[_function](
void
* _args)
69
{
70
ArgsTuple* args =
reinterpret_cast<
ArgsTuple*
>
(_args);
71
std::apply(_function, *args);
72
delete
args;
73
});
74
}
75
~AsynchronousFunction
() {
delete
m_call; }
79
void
operator()
(_Args_... _args)
80
{
81
ArgsTuple* t
82
=
new
ArgsTuple(
/*std::make_tuple<details::remove_cvref<_Args_...>>(*/
_args...)
/*)*/
;
83
QMetaObject::invokeMethod(m_call,
"call"
, Qt::QueuedConnection, Q_ARG(
void
*, t));
84
}
85
private
:
86
details::AsynchronousFunctionCall
* m_call;
87
};
88
}
// namespace knowCore
knowCore::AsynchronousFunction< void, _Args_... >::operator()
void operator()(_Args_... _args)
Definition
AsynchronousFunction.h:79
knowCore::AsynchronousFunction
Definition
AsynchronousFunction.h:29
knowCore::AsynchronousFunction::operator()
void operator()(_Args_... _args)
Definition
AsynchronousFunction.h:49
knowCore::details::AsynchronousFunctionCall
Definition
AsynchronousFunction.h:15
Generated by
1.12.0