LeechCraft
0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
Toggle main menu visibility
Loading...
Searching...
No Matches
workerthread.h
Go to the documentation of this file.
1
/**********************************************************************
2
* LeechCraft - modular cross-platform feature rich internet client.
3
* Copyright (C) 2006-2014 Georg Rudoy
4
*
5
* Distributed under the Boost Software License, Version 1.0.
6
* (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7
**********************************************************************/
8
9
#pragma once
10
11
#include <QString>
12
#include <QThread>
13
#include "
../coro.h
"
14
#include "
metamethod.h
"
15
16
namespace
LC::Util::Coro
17
{
18
class
WorkerThreadBase
:
public
QObject
19
{
20
protected
:
21
QThread
Thread_
;
22
public
:
23
struct
Config
24
{
25
QAnyStringView
ThreadName_
= {};
26
QThread::QualityOfService
QOS_
= QThread::QualityOfService::Eco;
27
QThread::Priority
Priority_
= QThread::LowPriority;
28
};
29
30
explicit
WorkerThreadBase
(
const
Config
& config)
31
{
32
if
(!config.
ThreadName_
.isEmpty ())
33
Thread_
.setObjectName (config.
ThreadName_
);
34
Thread_
.setServiceLevel (config.
QOS_
);
35
Thread_
.start (config.
Priority_
);
36
}
37
38
~WorkerThreadBase
()
override
39
{
40
Thread_
.quit ();
41
Thread_
.wait ();
42
}
43
};
44
45
template
<
typename
T,
typename
Self>
46
class
WorkerThread
:
public
WorkerThreadBase
47
{
48
protected
:
49
T
Worker_
;
50
public
:
51
WorkerThread
(
const
WorkerThread
& thread) =
delete
;
52
WorkerThread
(
WorkerThread
&& thread) =
delete
;
53
WorkerThread
&
operator=
(
const
WorkerThread
& thread) =
delete
;
54
WorkerThread
&
operator=
(
WorkerThread
&& thread) =
delete
;
55
56
template
<
typename
... Args>
57
requires
std::constructible_from<T, Args&&...> || std::constructible_from<T, Args&&..., Self&>
58
explicit
WorkerThread
(Args&&... args)
59
:
WorkerThread
{
Config
{}, std::forward<Args> (args)... }
60
{
61
}
62
63
template
<
typename
... Args>
64
requires
std::constructible_from<T, Args&&..., Self&>
65
explicit
WorkerThread
(
const
Config
& config, Args&&... args)
66
:
WorkerThread
{ config, std::forward<Args> (args)..., static_cast<Self&> (*this) }
67
{
68
}
69
70
template
<
typename
... Args>
71
requires
std::constructible_from<T, Args&&...>
72
explicit
WorkerThread
(
const
Config
& config, Args&&... args)
73
:
WorkerThreadBase
{ config }
74
,
Worker_
{ std::forward<Args> (args)... }
75
{
76
Worker_
.moveToThread (&
Thread_
);
77
}
78
79
~WorkerThread
()
override
80
{
81
QMetaObject::invokeMethod (&
Worker_
,
82
[
this
, thread = thread ()] {
Worker_
.moveToThread (thread); },
83
Qt::BlockingQueuedConnection);
84
}
85
86
template
<
typename
F,
typename
... Args,
typename
R = std::invoke_result_t<F, T&, Args...>>
87
ContextTask<R>
Run
(F&& f, Args&&... args)
88
{
89
co_return
co_await
Util::MetaMethod
(
Worker_
, std::forward<F> (f), std::forward<Args> (args)...);
90
}
91
};
92
}
LC::Util::Coro::WorkerThreadBase::~WorkerThreadBase
~WorkerThreadBase() override
Definition
workerthread.h:38
LC::Util::Coro::WorkerThreadBase::Thread_
QThread Thread_
Definition
workerthread.h:21
LC::Util::Coro::WorkerThreadBase::WorkerThreadBase
WorkerThreadBase(const Config &config)
Definition
workerthread.h:30
LC::Util::Coro::WorkerThread::WorkerThread
WorkerThread(const Config &config, Args &&... args)
Definition
workerthread.h:65
LC::Util::Coro::WorkerThread::WorkerThread
WorkerThread(Args &&... args)
Definition
workerthread.h:58
LC::Util::Coro::WorkerThread::operator=
WorkerThread & operator=(const WorkerThread &thread)=delete
LC::Util::Coro::WorkerThread::Run
ContextTask< R > Run(F &&f, Args &&... args)
Definition
workerthread.h:87
LC::Util::Coro::WorkerThread::~WorkerThread
~WorkerThread() override
Definition
workerthread.h:79
LC::Util::Coro::WorkerThread::WorkerThread
WorkerThread(WorkerThread &&thread)=delete
LC::Util::Coro::WorkerThread::WorkerThread
WorkerThread(const WorkerThread &thread)=delete
LC::Util::Coro::WorkerThread::Worker_
T Worker_
Definition
workerthread.h:49
LC::Util::Coro::WorkerThread::WorkerThread
WorkerThread(const Config &config, Args &&... args)
Definition
workerthread.h:72
coro.h
metamethod.h
LC::Util::Coro
Definition
workerthread.h:17
LC::Util::MetaMethod
auto MetaMethod(Ctx &ctx, F &&method, Args &&... args)
Definition
metamethod.h:80
LC::Util::ContextTask
Task< R, ContextExtensions > ContextTask
Definition
taskfwd.h:20
LC::Util::Coro::WorkerThreadBase::Config
Definition
workerthread.h:24
LC::Util::Coro::WorkerThreadBase::Config::QOS_
QThread::QualityOfService QOS_
Definition
workerthread.h:26
LC::Util::Coro::WorkerThreadBase::Config::ThreadName_
QAnyStringView ThreadName_
Definition
workerthread.h:25
LC::Util::Coro::WorkerThreadBase::Config::Priority_
QThread::Priority Priority_
Definition
workerthread.h:27
src
util
threads
coro
workerthread.h
Generated by
1.17.0