| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <concepts> |
| 7 |
#include <functional> |
| 8 |
#include <future> |
| 9 |
|
| 10 |
#include "Common/Functional.h" |
| 11 |
#include "Common/SPSCQueue.h" |
| 12 |
|
| 13 |
struct EfbPokeData; |
| 14 |
class PointerWrap; |
| 15 |
|
| 16 |
class AsyncRequests |
| 17 |
{ |
| 18 |
public: |
| 19 |
AsyncRequests(); |
| 20 |
|
| 21 |
|
| 22 |
void PullEvents(); |
| 23 |
|
| 24 |
|
| 25 |
void WaitForEmptyQueue(); |
| 26 |
|
| 27 |
template <std::invocable<> F> |
| 28 |
void PushEvent(F&& callback) |
| 29 |
{ |
| 30 |
if (m_passthrough) |
| 31 |
{ |
| 32 |
std::invoke(std::forward<F>(callback)); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
QueueEvent(Event{std::forward<F>(callback)}); |
| 37 |
} |
| 38 |
|
| 39 |
template <std::invocable<> F> |
| 40 |
auto PushBlockingEvent(F&& callback) -> std::invoke_result_t<F> |
| 41 |
{ |
| 42 |
if (m_passthrough) |
| 43 |
return std::invoke(std::forward<F>(callback)); |
| 44 |
|
| 45 |
std::packaged_task task{std::forward<F>(callback)}; |
| 46 |
QueueEvent(Event{[&] { task(); }}); |
| 47 |
|
| 48 |
return task.get_future().get(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
void SetPassthrough(bool enable); |
| 53 |
|
| 54 |
static AsyncRequests* GetInstance() { return &s_singleton; } |
| 55 |
|
| 56 |
private: |
| 57 |
using Event = Common::MoveOnlyFunction<void()>; |
| 58 |
|
| 59 |
void QueueEvent(Event&& event); |
| 60 |
|
| 61 |
static AsyncRequests s_singleton; |
| 62 |
|
| 63 |
Common::WaitableSPSCQueue<Event> m_queue; |
| 64 |
|
| 65 |
bool m_passthrough = true; |
| 66 |
}; |