swift
activeobserver.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
7 
9 {
11  {
12  CPromise<CVariant> promise;
13  emit requestPosted(param, promise);
14  return promise.future().result();
15  }
16 
17  void CActiveObserver::requestAsync(const CVariant &param, std::function<void(const CVariant &)> callback)
18  {
19  CPromise<CVariant> promise;
20  emit requestPosted(param, promise);
21  doAfter(promise.future(), this,
22  [callback = std::move(callback), weakRef = weakRef()](const QFuture<CVariant> &reply) {
23  const auto lock = weakRef.lock();
24  if (lock) { callback(reply.result()); }
25  });
26  }
27 } // namespace swift::misc::shared_state
A promise-based interface to QFuture, similar to std::promise for std::future.
Definition: promise.h:78
QFuture< T > future()
Return a future that can be used to access the result.
Definition: promise.h:81
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
QWeakPointer< const CActiveObserver > weakRef() const
Get a QWeakPointer pointing to this object.
void requestAsync(const CVariant &param, std::function< void(const CVariant &)> callback)
Send a request and receive an asynchronous reply. The callback will not be called if the reply is rec...
void requestPosted(const swift::misc::CVariant &param, swift::misc::CPromise< swift::misc::CVariant > o_reply)
Emitted by request and requestAsync.
CVariant request(const CVariant &param)
Send a request and receive a synchronous reply.
Utilities for sharing state between multiple objects.
Definition: application.h:48
void doAfter(QFuture< T > future, QObject *context, F &&func)
Connect a slot or function to be invoked in the given context when a QFuture is finished.
Definition: promise.h:59