LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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>
47  {
48  protected:
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 }
requires std::constructible_from< T, Args &&... > std::constructible_from< T, Args &&..., Self & > WorkerThread(Args &&... args)
Definition: workerthread.h:58
ContextTask< R > Run(F &&f, Args &&... args)
Definition: workerthread.h:87
auto MetaMethod(Ctx &ctx, F &&method, Args &&... args)
Definition: metamethod.h:80
QThread::QualityOfService QOS_
Definition: workerthread.h:26
requires std::constructible_from< T, Args &&..., Self & > WorkerThread(const Config &config, Args &&... args)
Definition: workerthread.h:65
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:1083
WorkerThread & operator=(const WorkerThread &thread)=delete
requires(Tup1Size==Tup2Size) const expr auto ZipWith(Tup1 &&tup1
requires std::constructible_from< T, Args &&... > WorkerThread(const Config &config, Args &&... args)
Definition: workerthread.h:72
WorkerThread(const WorkerThread &thread)=delete
Priority
Definition: structures.h:190
WorkerThreadBase(const Config &config)
Definition: workerthread.h:30