1 #pragma once
2 
3 #include <functional>
4 #include <deque>
5 #include <mutex>
6 #include <condition_variable>
7 
8 class CMailBox
9 {
10 public:
11 	virtual ~CMailBox() = default;
12 
13 	typedef std::function<void()> FunctionType;
14 
15 	void SendCall(const FunctionType&, bool = false);
16 	void SendCall(FunctionType&&);
17 	void FlushCalls();
18 
19 	bool IsPending() const;
20 	void ReceiveCall();
21 	void WaitForCall();
22 	void WaitForCall(unsigned int);
23 
24 private:
25 	struct MESSAGE
26 	{
27 		MESSAGE() = default;
28 
29 		MESSAGE(MESSAGE&&) = default;
30 		MESSAGE(const MESSAGE&) = delete;
31 
32 		MESSAGE& operator=(MESSAGE&&) = default;
33 		MESSAGE& operator=(const MESSAGE&) = delete;
34 
35 		FunctionType function;
36 		bool sync;
37 	};
38 
39 	typedef std::deque<MESSAGE> FunctionCallQueue;
40 
41 	FunctionCallQueue m_calls;
42 	std::mutex m_callMutex;
43 	std::condition_variable m_callFinished;
44 	std::condition_variable m_waitCondition;
45 	bool m_callDone;
46 };
47