Akonadi

akthread.h
1 /*
2  SPDX-FileCopyrightText: 2015 Daniel Vr├ítil <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QObject>
10 #include <QThread>
11 
12 #include <atomic>
13 #include <type_traits>
14 
15 namespace Akonadi
16 {
17 namespace Server
18 {
19 
20 class AkThread : public QObject
21 {
22  Q_OBJECT
23 public:
24  enum StartMode {
25  AutoStart,
26  ManualStart,
27  NoThread // for unit-tests
28  };
29 
30  template<typename T, typename... Args>
31  static std::enable_if_t<std::is_base_of_v<AkThread, T>, std::unique_ptr<T>> create(Args &&...args) noexcept
32  {
33  // Workaround T having a protected constructor
34  struct TConstructor : T {
35  explicit TConstructor(Args &&...args)
36  : T(std::forward<Args>(args)...)
37  {
38  }
39  };
40  std::unique_ptr<T> thread = std::make_unique<TConstructor>(std::forward<Args>(args)...);
41  if (thread->m_startMode == AkThread::AutoStart) {
42  thread->startThread();
43  }
44  return thread;
45  }
46 
47  ~AkThread() override;
48 
49  bool isInitialized() const
50  {
51  return m_initialized;
52  }
53 
54  void waitForInitialized();
55 
56 Q_SIGNALS:
57  void initialized();
58 
59 protected:
60  explicit AkThread(const QString &objectName, QThread::Priority priority = QThread::InheritPriority, QObject *parent = nullptr);
61  explicit AkThread(const QString &objectName, StartMode startMode, QThread::Priority priority = QThread::InheritPriority, QObject *parent = nullptr);
62 
63  void quitThread();
64  void startThread();
65 
66 protected Q_SLOTS:
67  virtual void init();
68  virtual void quit();
69 
70 private:
71  StartMode m_startMode = AutoStart;
72  bool m_quitCalled = false;
73  std::atomic_bool m_initialized = false;
74 };
75 
76 } // namespace Server
77 } // namespace Akonadi
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QCA_EXPORT void init()
QThread * thread() const const
QAction * create(StandardGameAction id, const QObject *recvr, const char *slot, QObject *parent)
Q_SIGNALSQ_SIGNALS
const QList< QKeySequence > & quit()
A glue between Qt and the standard library.
const QList< QKeySequence > & forward()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:52:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.