2013-04-21 03:12:06 +00:00
|
|
|
#pragma once
|
2007-12-01 04:08:34 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <deque>
|
2013-04-21 03:12:06 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2023-10-25 13:20:43 +01:00
|
|
|
#include <future>
|
2007-12-01 04:08:34 +00:00
|
|
|
|
|
|
|
class CMailBox
|
|
|
|
{
|
|
|
|
public:
|
2018-04-30 21:01:23 +01:00
|
|
|
virtual ~CMailBox() = default;
|
2007-12-01 04:08:34 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
typedef std::function<void()> FunctionType;
|
2007-12-01 04:08:34 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
void SendCall(const FunctionType&, bool = false);
|
|
|
|
void SendCall(FunctionType&&);
|
|
|
|
void FlushCalls();
|
2007-12-01 04:08:34 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
bool IsPending() const;
|
|
|
|
void ReceiveCall();
|
|
|
|
void WaitForCall();
|
|
|
|
void WaitForCall(unsigned int);
|
2007-12-01 04:08:34 +00:00
|
|
|
|
|
|
|
private:
|
2008-12-13 00:30:38 +00:00
|
|
|
struct MESSAGE
|
|
|
|
{
|
2017-03-25 21:31:59 -04:00
|
|
|
MESSAGE() = default;
|
|
|
|
|
|
|
|
MESSAGE(MESSAGE&&) = default;
|
|
|
|
MESSAGE(const MESSAGE&) = delete;
|
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
MESSAGE& operator=(MESSAGE&&) = default;
|
|
|
|
MESSAGE& operator=(const MESSAGE&) = delete;
|
2017-03-25 21:31:59 -04:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
FunctionType function;
|
2024-05-18 11:28:47 -04:00
|
|
|
std::unique_ptr<std::promise<void>> promise;
|
2008-12-13 00:30:38 +00:00
|
|
|
};
|
|
|
|
|
2012-03-11 20:06:14 +00:00
|
|
|
typedef std::deque<MESSAGE> FunctionCallQueue;
|
2007-12-01 04:08:34 +00:00
|
|
|
|
2018-04-30 21:01:23 +01:00
|
|
|
FunctionCallQueue m_calls;
|
|
|
|
std::mutex m_callMutex;
|
|
|
|
std::condition_variable m_waitCondition;
|
2007-12-01 04:08:34 +00:00
|
|
|
};
|