Cutelyst  3.4.0
async.cpp
1 /*
2  * Copyright (C) 2021 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "async.h"
19 #include "context.h"
20 
21 #include <QPointer>
22 #include <QLoggingCategory>
23 
24 Q_LOGGING_CATEGORY(CUTELYST_ASYNC, "cutelyst.async", QtInfoMsg)
25 
26 using namespace Cutelyst;
27 
28 namespace Cutelyst {
29 
30 class ASyncPrivate {
31 public:
32  ASyncPrivate(Context *_c) : c(_c) {
33 // qDebug(CUTELYST_ASYNC, "Detaching async %s", qPrintable(c->objectName()));
34  c->detachAsync();
35  }
36  ASyncPrivate(Context *_c, std::function<void(Context *c)> _cb) : c(_c), cb(_cb) {
37 // qDebug(CUTELYST_ASYNC, "Detaching async %s", qPrintable(c->objectName()));
38  c->detachAsync();
39  }
40  ~ASyncPrivate() {
41  if (!c.isNull()) {
42  if (cb) {
43  cb(c);
44  }
45 // qDebug(CUTELYST_ASYNC, "Attaching async %s", qPrintable(c->objectName()));
46  c->attachAsync();
47  }
48  }
49 
51  std::function<void(Context *c)> cb;
52 };
53 
54 }
55 
56 ASync::ASync() = default;
57 
68 ASync::ASync(Context *c) : d(std::make_shared<ASyncPrivate>(c))
69 {
70 }
71 
84 ASync::ASync(Context *c, std::function<void (Context *)> cb) : d(std::make_shared<ASyncPrivate>(c, cb))
85 {
86 }
87 
91 ASync::ASync(const ASync &other) : d(other.d)
92 {
93 }
94 
95 ASync::~ASync() = default;
96 
97 ASync &ASync::operator =(const ASync &copy) noexcept
98 {
99  d = copy.d;
100  return *this;
101 }
The Cutelyst Context.
Definition: context.h:52
void attachAsync()
attachAsync
Definition: context.cpp:362
void detachAsync() noexcept
Definition: context.cpp:356
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8