File indexing completed on 2024-04-21 04:01:28
0001 /* -*- C++ -*- 0002 This file is part of ThreadWeaver, a KDE framework. 0003 0004 SPDX-FileCopyrightText: 2013 Mirko Boehm <mirko@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #ifndef THREADWEAVER_QUEUEING_H 0010 #define THREADWEAVER_QUEUEING_H 0011 0012 #include "collection.h" 0013 #include "jobinterface.h" 0014 #include "jobpointer.h" 0015 #include "lambda.h" 0016 #include "managedjobpointer.h" 0017 #include "qobjectdecorator.h" 0018 #include "queue.h" 0019 0020 namespace ThreadWeaver 0021 { 0022 // make a job that calls a functor, anything that responds to operator() 0023 template<typename T> 0024 QSharedPointer<Lambda<T>> make_job(T t) 0025 { 0026 QSharedPointer<Lambda<T>> ret(new Lambda<T>(t)); 0027 return ret; 0028 } 0029 0030 // make a job pointer holding a pointer to a Job(Interface) 0031 template<typename T> 0032 inline QSharedPointer<T> make_job(T *job) 0033 { 0034 JobInterface *test = static_cast<JobInterface *>(job); 0035 Q_UNUSED(test); 0036 return QSharedPointer<T>(job); 0037 } 0038 0039 // make a job pointer holding anything resembling JobInterface 0040 inline JobPointer make_job_raw(JobInterface *job) 0041 { 0042 return ManagedJobPointer<JobInterface>(job); 0043 } 0044 0045 // enqueue any functor type to the specified queue: 0046 template<typename T> 0047 JobPointer enqueue(Queue *weaver, T t) 0048 { 0049 JobPointer ret = make_job(t); 0050 weaver->enqueue(ret); 0051 return ret; 0052 } 0053 0054 template<typename T> 0055 QSharedPointer<T> enqueue(Queue *weaver, T *t) 0056 { 0057 JobInterface *test = static_cast<JobInterface *>(t); 0058 Q_UNUSED(test); 0059 QSharedPointer<T> ret(make_job(t)); 0060 weaver->enqueue(ret); 0061 return ret; 0062 } 0063 0064 // specialize for JobPointer: 0065 template<> 0066 inline JobPointer enqueue<JobPointer>(Queue *weaver, JobPointer job) 0067 { 0068 weaver->enqueue(job); 0069 return job; 0070 } 0071 0072 // convenience overload: enqueue the functor to the global queue: 0073 template<typename T> 0074 JobPointer enqueue(T t) 0075 { 0076 return enqueue(Queue::instance(), t); 0077 } 0078 0079 // enqueue a raw pointer with no memory management 0080 inline JobPointer enqueue_raw(Queue *weaver, JobInterface *job) 0081 { 0082 return enqueue(weaver, make_job_raw(job)); 0083 } 0084 0085 // overload to enqueue to the global pool 0086 inline JobPointer enqueue_raw(JobInterface *job) 0087 { 0088 return enqueue(Queue::instance(), make_job_raw(job)); 0089 } 0090 0091 } 0092 0093 #endif // THREADWEAVER_QUEUEING_H