Cutelyst  3.5.0
async.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2020-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "async.h"
6 #include "context.h"
7 
8 #include <QPointer>
9 #include <QLoggingCategory>
10 
11 Q_LOGGING_CATEGORY(CUTELYST_ASYNC, "cutelyst.async", QtInfoMsg)
12 
13 using namespace Cutelyst;
14 
15 namespace Cutelyst {
16 
17 class ASyncPrivate {
18 public:
19  ASyncPrivate(Context *_c) : c(_c) {
20 // qDebug(CUTELYST_ASYNC, "Detaching async %s", qPrintable(c->objectName()));
21  c->detachAsync();
22  }
23  ASyncPrivate(Context *_c, std::function<void(Context *c)> _cb) : c(_c), cb(_cb) {
24 // qDebug(CUTELYST_ASYNC, "Detaching async %s", qPrintable(c->objectName()));
25  c->detachAsync();
26  }
27  ~ASyncPrivate() {
28  if (!c.isNull()) {
29  if (cb) {
30  cb(c);
31  }
32 // qDebug(CUTELYST_ASYNC, "Attaching async %s", qPrintable(c->objectName()));
33  c->attachAsync();
34  }
35  }
36 
38  std::function<void(Context *c)> cb;
39 };
40 
41 }
42 
43 ASync::ASync() = default;
44 
55 ASync::ASync(Context *c) : d(std::make_shared<ASyncPrivate>(c))
56 {
57 }
58 
71 ASync::ASync(Context *c, std::function<void (Context *)> cb) : d(std::make_shared<ASyncPrivate>(c, cb))
72 {
73 }
74 
78 ASync::ASync(const ASync &other) : d(other.d)
79 {
80 }
81 
82 ASync::~ASync() = default;
83 
84 ASync &ASync::operator =(const ASync &copy) noexcept
85 {
86  d = copy.d;
87  return *this;
88 }
The Cutelyst Context.
Definition: context.h:38
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void detachAsync() noexcept
Definition: context.cpp:343
void attachAsync()
attachAsync
Definition: context.cpp:349