2017-07-26 03:46:35 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-07-26 03:46:35 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-05-21 22:27:12 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QPointer>
|
2017-08-17 22:01:00 +00:00
|
|
|
#include <QThread>
|
2018-05-21 22:27:12 +00:00
|
|
|
#include <optional>
|
2017-07-26 03:46:35 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "Common/Event.h"
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/QtUtils/QueueOnObject.h"
|
2017-07-26 03:46:35 +00:00
|
|
|
|
|
|
|
class QObject;
|
|
|
|
|
|
|
|
// QWidget and subclasses are not thread-safe! This helper takes arbitrary code from any thread,
|
|
|
|
// safely runs it on the appropriate GUI thread, waits for it to finish, and returns the result.
|
2018-05-21 22:27:12 +00:00
|
|
|
//
|
|
|
|
// If the target object is destructed before the code gets to run, the QPointer will be nulled and
|
|
|
|
// the function will return nullopt.
|
2017-07-26 03:46:35 +00:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
auto RunOnObject(QObject* object, F&& functor)
|
|
|
|
{
|
2020-06-01 21:32:57 +00:00
|
|
|
using OptionalResultT = std::optional<std::invoke_result_t<F>>;
|
2018-05-21 22:27:12 +00:00
|
|
|
|
2017-07-26 03:46:35 +00:00
|
|
|
// If we queue up a functor on the current thread, it won't run until we return to the event loop,
|
|
|
|
// which means waiting for it to finish will never complete. Instead, run it immediately.
|
|
|
|
if (object->thread() == QThread::currentThread())
|
2018-05-21 22:27:12 +00:00
|
|
|
return OptionalResultT(functor());
|
|
|
|
|
|
|
|
class FnInvokeEvent : public QEvent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FnInvokeEvent(F&& functor, QObject* obj, Common::Event& event, OptionalResultT& result)
|
|
|
|
: QEvent(QEvent::None), m_func(std::move(functor)), m_obj(obj), m_event(event),
|
|
|
|
m_result(result)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~FnInvokeEvent()
|
|
|
|
{
|
|
|
|
if (m_obj)
|
|
|
|
{
|
2018-05-22 23:07:08 +00:00
|
|
|
m_result = m_func();
|
2018-05-21 22:27:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// is already nullopt
|
|
|
|
}
|
|
|
|
m_event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F m_func;
|
|
|
|
QPointer<QObject> m_obj;
|
|
|
|
Common::Event& m_event;
|
|
|
|
OptionalResultT& m_result;
|
|
|
|
};
|
|
|
|
|
|
|
|
Common::Event event{};
|
|
|
|
OptionalResultT result = std::nullopt;
|
|
|
|
QCoreApplication::postEvent(object,
|
|
|
|
new FnInvokeEvent(std::forward<F>(functor), object, event, result));
|
2017-07-26 03:46:35 +00:00
|
|
|
event.Wait();
|
|
|
|
return result;
|
|
|
|
}
|
2017-09-15 16:08:09 +00:00
|
|
|
|
|
|
|
template <typename Base, typename Type, typename Receiver>
|
|
|
|
auto RunOnObject(Receiver* obj, Type Base::*func)
|
|
|
|
{
|
|
|
|
return RunOnObject(obj, [obj, func] { return (obj->*func)(); });
|
|
|
|
}
|