1 | /* -*- C++ -*- |
---|---|
2 | This file is part of ThreadWeaver, a KDE framework. |
3 | |
4 | SPDX-FileCopyrightText: 2013 Mirko Boehm <mirko@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef THREADWEAVER_QUEUEING_H |
10 | #define THREADWEAVER_QUEUEING_H |
11 | |
12 | #include "collection.h" |
13 | #include "jobinterface.h" |
14 | #include "jobpointer.h" |
15 | #include "lambda.h" |
16 | #include "managedjobpointer.h" |
17 | #include "qobjectdecorator.h" |
18 | #include "queue.h" |
19 | |
20 | namespace ThreadWeaver |
21 | { |
22 | // make a job that calls a functor, anything that responds to operator() |
23 | template<typename T> |
24 | QSharedPointer<Lambda<T>> make_job(T t) |
25 | { |
26 | QSharedPointer<Lambda<T>> ret(new Lambda<T>(t)); |
27 | return ret; |
28 | } |
29 | |
30 | // make a job pointer holding a pointer to a Job(Interface) |
31 | template<typename T> |
32 | inline QSharedPointer<T> make_job(T *job) |
33 | { |
34 | JobInterface *test = static_cast<JobInterface *>(job); |
35 | Q_UNUSED(test); |
36 | return QSharedPointer<T>(job); |
37 | } |
38 | |
39 | // make a job pointer holding anything resembling JobInterface |
40 | inline JobPointer make_job_raw(JobInterface *job) |
41 | { |
42 | return ManagedJobPointer<JobInterface>(job); |
43 | } |
44 | |
45 | // enqueue any functor type to the specified queue: |
46 | template<typename T> |
47 | JobPointer enqueue(Queue *weaver, T t) |
48 | { |
49 | JobPointer ret = make_job(t); |
50 | weaver->enqueue(job: ret); |
51 | return ret; |
52 | } |
53 | |
54 | template<typename T> |
55 | QSharedPointer<T> enqueue(Queue *weaver, T *t) |
56 | { |
57 | JobInterface *test = static_cast<JobInterface *>(t); |
58 | Q_UNUSED(test); |
59 | QSharedPointer<T> ret(make_job(t)); |
60 | weaver->enqueue(ret); |
61 | return ret; |
62 | } |
63 | |
64 | // specialize for JobPointer: |
65 | template<> |
66 | inline JobPointer enqueue<JobPointer>(Queue *weaver, JobPointer job) |
67 | { |
68 | weaver->enqueue(job); |
69 | return job; |
70 | } |
71 | |
72 | // convenience overload: enqueue the functor to the global queue: |
73 | template<typename T> |
74 | JobPointer enqueue(T t) |
75 | { |
76 | return enqueue(Queue::instance(), t); |
77 | } |
78 | |
79 | // enqueue a raw pointer with no memory management |
80 | inline JobPointer enqueue_raw(Queue *weaver, JobInterface *job) |
81 | { |
82 | return enqueue(weaver, job: make_job_raw(job)); |
83 | } |
84 | |
85 | // overload to enqueue to the global pool |
86 | inline JobPointer enqueue_raw(JobInterface *job) |
87 | { |
88 | return enqueue(weaver: Queue::instance(), job: make_job_raw(job)); |
89 | } |
90 | |
91 | } |
92 | |
93 | #endif // THREADWEAVER_QUEUEING_H |
94 |