// This file is part of Desktop App Toolkit, // a set of libraries for developing nice desktop applications. // // For license and copyright information please follow this link: // https://github.com/desktop-app/legal/blob/master/LEGAL // #pragma once #include #include #include "base/basic_types.h" namespace base { class InvokeQueuedEvent : public QEvent { public: static constexpr auto kType = QEvent::Type(60666); explicit InvokeQueuedEvent(FnMut &&method) : QEvent(kType) , _method(std::move(method)) { } void invoke() { _method(); } private: FnMut _method; }; } // namespace base template inline void InvokeQueued(const QObject *context, Lambda &&lambda) { QCoreApplication::postEvent( const_cast(context), new base::InvokeQueuedEvent(std::forward(lambda))); } class SingleQueuedInvokation : public QObject { public: SingleQueuedInvokation(Fn callback) : _callback(callback) { } void call() { if (_pending.testAndSetAcquire(0, 1)) { InvokeQueued(this, [this] { if (_pending.testAndSetRelease(1, 0)) { _callback(); } }); } } private: Fn _callback; QAtomicInt _pending = { 0 }; };