digiKam
parallelworkers.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2012-01-13
7  * Description : Multithreaded worker objects, working in parallel
8  *
9  * Copyright (C) 2010-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #ifndef DIGIKAM_PARALLEL_WORKERS_H
25 #define DIGIKAM_PARALLEL_WORKERS_H
26 
27 // Qt includes
28 
29 #include <QObject>
30 
31 // Local includes
32 
33 #include "digikam_export.h"
34 #include "workerobject.h"
35 
36 namespace Digikam
37 {
38 
39 class DIGIKAM_EXPORT ParallelWorkers
40 {
41 
42 public:
43 
49  explicit ParallelWorkers();
50  virtual ~ParallelWorkers();
51 
55  void schedule();
57  void wait();
58 
59  void setPriority(QThread::Priority priority);
60 
64  bool optimalWorkerCountReached() const;
65 
70  static int optimalWorkerCount();
71 
72 public:
73 
75 
76  bool connect(const char* signal,
77  const QObject* receiver,
78  const char* method,
79  Qt::ConnectionType type = Qt::AutoConnection) const;
80 
81 protected:
82 
83  void add(WorkerObject* const worker);
84 
85  // Internal implementation
86 
90  int replacementQtMetacall(QMetaObject::Call _c, int _id, void** _a);
91  const QMetaObject* replacementMetaObject() const;
92 
96  virtual QObject* asQObject() = 0;
97 
101  virtual int WorkerObjectQtMetacall(QMetaObject::Call _c, int _id, void** _a) = 0;
102 
106  virtual const QMetaObject* mocMetaObject() const = 0;
107 
108  int replacementStaticQtMetacall(QMetaObject::Call _c, int _id, void** _a);
109  typedef void (*StaticMetacallFunction)(QObject*, QMetaObject::Call, int, void**);
110  virtual StaticMetacallFunction staticMetacallPointer() = 0;
111 
112 protected:
113 
117 
118  StaticMetacallFunction m_originalStaticMetacall;
119 
120 private:
121 
122  // Disable
123  ParallelWorkers(const ParallelWorkers&) = delete;
124  ParallelWorkers& operator=(const ParallelWorkers&) = delete;
125 };
126 
127 // -------------------------------------------------------------------------------------------------
128 
129 template <class A>
130 
131 class ParallelAdapter : public A, public ParallelWorkers
132 {
133 public:
134 
145  explicit ParallelAdapter()
146  {
147  }
148 
149  ~ParallelAdapter() override
150  {
151  }
152 
153  void add(A* const worker)
154  {
155  ParallelWorkers::add(worker);
156  }
157 
158  // Internal Implementation
159  // I know this is a hack
160 
161  int WorkerObjectQtMetacall(QMetaObject::Call _c, int _id, void** _a) override
162  {
163  return WorkerObject::qt_metacall(_c, _id, _a);
164  }
165 
166  const QMetaObject* mocMetaObject() const override
167  {
168  return A::metaObject();
169  }
170 
171  static void qt_static_metacall(QObject* o, QMetaObject::Call _c, int _id, void** _a)
172  {
173  static_cast<ParallelAdapter*>(o)->replacementStaticQtMetacall(_c, _id, _a);
174  }
175 
177  {
178  return qt_static_metacall;
179  }
180 
181  const QMetaObject* metaObject() const override
182  {
184  }
185 
186  int qt_metacall(QMetaObject::Call _c, int _id, void** _a) override
187  {
188  return ParallelWorkers::replacementQtMetacall(_c, _id, _a);
189  }
190 
191  QObject* asQObject() override
192  {
193  return this;
194  }
195 
196  void schedule()
197  {
199  }
200 
202  {
204  }
205 
206  void wait()
207  {
209  }
210 
211  bool connect(const char* signal,
212  const QObject* receiver,
213  const char* method,
214  Qt::ConnectionType type = Qt::AutoConnection) const
215  {
216  return ParallelWorkers::connect(signal, receiver, method, type);
217  }
218 
219 private:
220 
221  // Disable
222  ParallelAdapter(const ParallelAdapter&) = delete;
223  ParallelAdapter& operator=(const ParallelAdapter&) = delete;
224 };
225 
226 } // namespace Digikam
227 
228 #endif // DIGIKAM_PARALLEL_WORKERS_H
Definition: parallelworkers.h:132
int qt_metacall(QMetaObject::Call _c, int _id, void **_a) override
Definition: parallelworkers.h:186
void deactivate(WorkerObject::DeactivatingMode mode=WorkerObject::FlushSignals)
Definition: parallelworkers.h:201
StaticMetacallFunction staticMetacallPointer() override
Definition: parallelworkers.h:176
const QMetaObject * metaObject() const override
Definition: parallelworkers.h:181
bool connect(const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type=Qt::AutoConnection) const
Definition: parallelworkers.h:211
void wait()
Definition: parallelworkers.h:206
~ParallelAdapter() override
Definition: parallelworkers.h:149
void schedule()
Definition: parallelworkers.h:196
static void qt_static_metacall(QObject *o, QMetaObject::Call _c, int _id, void **_a)
Definition: parallelworkers.h:171
ParallelAdapter()
Definition: parallelworkers.h:145
int WorkerObjectQtMetacall(QMetaObject::Call _c, int _id, void **_a) override
Definition: parallelworkers.h:161
QObject * asQObject() override
Definition: parallelworkers.h:191
const QMetaObject * mocMetaObject() const override
Definition: parallelworkers.h:166
void add(A *const worker)
Definition: parallelworkers.h:153
Definition: parallelworkers.h:40
virtual StaticMetacallFunction staticMetacallPointer()=0
int replacementStaticQtMetacall(QMetaObject::Call _c, int _id, void **_a)
Definition: parallelworkers.cpp:157
QMetaObject * m_replacementMetaObject
Definition: parallelworkers.h:116
void(* StaticMetacallFunction)(QObject *, QMetaObject::Call, int, void **)
Definition: parallelworkers.h:109
void wait()
Definition: parallelworkers.cpp:87
void schedule()
Definition: parallelworkers.cpp:71
void deactivate(WorkerObject::DeactivatingMode mode=WorkerObject::FlushSignals)
Definition: parallelworkers.cpp:79
QList< WorkerObject * > m_workers
Definition: parallelworkers.h:114
virtual const QMetaObject * mocMetaObject() const =0
void add(WorkerObject *const worker)
Definition: parallelworkers.cpp:103
int replacementQtMetacall(QMetaObject::Call _c, int _id, void **_a)
Definition: parallelworkers.cpp:249
virtual int WorkerObjectQtMetacall(QMetaObject::Call _c, int _id, void **_a)=0
StaticMetacallFunction m_originalStaticMetacall
Definition: parallelworkers.h:118
bool connect(const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type=Qt::AutoConnection) const
Connects signals outbound from all workers to a given receiver.
Definition: parallelworkers.cpp:141
const QMetaObject * replacementMetaObject() const
Definition: parallelworkers.cpp:266
int m_currentIndex
Definition: parallelworkers.h:115
virtual QObject * asQObject()=0
Definition: workerobject.h:45
DeactivatingMode
Definition: workerobject.h:59
@ FlushSignals
Already sent signals are cleared.
Definition: workerobject.h:60
Definition: piwigotalker.h:48
Definition: datefolderview.cpp:43