1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Api/EventSystem.h
2017-02-23 23:45:06 -06:00

58 lines
1.1 KiB
C++

#pragma once
#include <SiaCommon.h>
NS_BEGIN(Sia)
NS_BEGIN(Api)
class AFX_EXT_CLASS CEvent
{
public:
virtual ~CEvent() {}
public:
virtual String GetSingleLineMessage() const = 0;
virtual std::shared_ptr<CEvent> Clone() const = 0;
};
typedef std::shared_ptr<CEvent> CEventPtr;
#define CreateSystemEvent(E) CEventPtr(new E)
#define CreateSystemEventConsumer(E) [=](const CEvent&) -> void { E(e); }
// Singleton
class AFX_EXT_CLASS CEventSystem
{
private:
CEventSystem();
private:
~CEventSystem();
public:
// Singleton setup
CEventSystem(const CEventSystem&) = delete;
CEventSystem(CEventSystem&&) = delete;
CEventSystem& operator=(CEventSystem&&) = delete;
CEventSystem& operator=(const CEventSystem&) = delete;
private:
HANDLE _stopEvent;
std::deque<CEventPtr> _eventQueue;
std::deque<std::function<void(const CEvent&)>> _eventConsumers;
std::mutex _eventMutex;
std::unique_ptr<std::thread> _processThread;
public:
static CEventSystem EventSystem;
private:
void ProcessEvents();
public:
void AddEventConsumer(std::function<void(const CEvent&)> consumer);
void NotifyEvent(CEventPtr eventData);
void Start();
void Stop();
};
NS_END(2)