Play-/Source/MailBox.h

46 lines
892 B
C
Raw Permalink Normal View History

#pragma once
#include <functional>
#include <deque>
#include <mutex>
#include <condition_variable>
#include <future>
class CMailBox
{
public:
2018-04-30 21:01:23 +01:00
virtual ~CMailBox() = default;
2018-04-30 21:01:23 +01:00
typedef std::function<void()> FunctionType;
2018-04-30 21:01:23 +01:00
void SendCall(const FunctionType&, bool = false);
void SendCall(FunctionType&&);
void FlushCalls();
2018-04-30 21:01:23 +01:00
bool IsPending() const;
void ReceiveCall();
void WaitForCall();
void WaitForCall(unsigned int);
private:
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;
std::promise<void> promise;
};
typedef std::deque<MESSAGE> FunctionCallQueue;
2018-04-30 21:01:23 +01:00
FunctionCallQueue m_calls;
std::mutex m_callMutex;
std::condition_variable m_waitCondition;
};