| 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 | #include <QList> |
| 10 | |
| 11 | #include "queue.h" |
| 12 | #include "queueing.h" |
| 13 | #include "queuestream.h" |
| 14 | |
| 15 | namespace ThreadWeaver |
| 16 | { |
| 17 | class Q_DECL_HIDDEN QueueStream::Private |
| 18 | { |
| 19 | public: |
| 20 | Private(Queue *w) |
| 21 | : weaver(w) |
| 22 | { |
| 23 | } |
| 24 | Queue *weaver; |
| 25 | QList<JobPointer> jobs; |
| 26 | }; |
| 27 | |
| 28 | QueueStream::QueueStream(Queue *queue) |
| 29 | : d(new Private(queue)) |
| 30 | { |
| 31 | Q_ASSERT(queue); |
| 32 | } |
| 33 | |
| 34 | QueueStream::~QueueStream() |
| 35 | { |
| 36 | flush(); |
| 37 | delete d; |
| 38 | } |
| 39 | |
| 40 | void ThreadWeaver::QueueStream::add(const ThreadWeaver::JobPointer &job) |
| 41 | { |
| 42 | d->jobs.append(t: job); |
| 43 | } |
| 44 | |
| 45 | void QueueStream::flush() |
| 46 | { |
| 47 | if (d->jobs.isEmpty()) { |
| 48 | return; |
| 49 | } |
| 50 | Q_ASSERT(d->weaver); |
| 51 | d->weaver->enqueue(jobs: d->jobs); |
| 52 | d->jobs.clear(); |
| 53 | } |
| 54 | |
| 55 | QueueStream &QueueStream::operator<<(const JobPointer &job) |
| 56 | { |
| 57 | add(job); |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | QueueStream &QueueStream::operator<<(JobInterface *job) |
| 62 | { |
| 63 | add(job: make_job(job)); |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | QueueStream &QueueStream::operator<<(Job &job) |
| 68 | { |
| 69 | add(job: make_job_raw(job: &job)); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | /** @brief Return a stream the enqueues jobs in the ThreadWeaver global queue. |
| 74 | * Using this is synonymous to Weaver::instance()::stream(). */ |
| 75 | QueueStream stream() |
| 76 | { |
| 77 | return QueueStream(Queue::instance()); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 |
