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/include/siadrive_api/eventsystem.h
Scott E. Graves e82081fd41 Renter settings
2017-03-25 12:25:45 -05:00

64 lines
1.4 KiB
C++

#ifndef _EVENTSYSTEM_H
#define _EVENTSYSTEM_H
#include <siacommon.h>
NS_BEGIN(Sia)
NS_BEGIN(Api)
class SIADRIVE_EXPORTABLE CEvent
{
public:
virtual ~CEvent() {}
public:
virtual SString 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 SIADRIVE_EXPORTABLE 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:
#ifdef _WIN32
HANDLE _stopEvent;
#endif
std::deque<CEventPtr> _eventQueue;
std::deque<std::function<void(const CEvent&)>> _eventConsumers;
std::deque<std::function<bool(const CEvent&)>> _oneShotEventConsumers;
std::mutex _eventMutex;
std::unique_ptr<std::thread> _processThread;
std::mutex _oneShotMutex;
public:
static CEventSystem EventSystem;
private:
void ProcessEvents();
public:
void AddEventConsumer(std::function<void(const CEvent&)> consumer);
void AddOneShotEventConsumer(std::function<bool(const CEvent&)> consumer);
void NotifyEvent(CEventPtr eventData);
void Start();
void Stop();
};
NS_END(2)
#endif //_EVENTSYSTEM_H