Files
WFR-lib/include/wfr/common/eventsystem/EventSystem.h
T
2017-04-11 15:49:21 +02:00

86 lines
1.8 KiB
C++
Executable File

#pragma once
#include <mutex>
#include <thread>
#include <condition_variable>
#include <queue>
#include <unordered_set>
#include <atomic>
#include <memory>
#include <wfr/config.h>
namespace wfr {
// forward declarations
class WorkflowRuntime;
namespace eventsystem {
// forward declarations
class EventListener;
class Event;
class OStreamLogger;
class SQLiteLogger;
class EventSystem {
friend class wfr::WorkflowRuntime;
// Singleton instances
private:
static EventSystem singleton;
#ifdef WFR_ENABLE_STDLOGGER
static std::unique_ptr<OStreamLogger> oStreamLogger;
#endif
#ifdef WFR_ENABLE_PERFDATABASE
static std::unique_ptr<SQLiteLogger> sqliteLogger;
#endif
// Non-public parameters
private:
std::unordered_set<EventListener*> eventListeners;
std::queue<std::shared_ptr<Event>> eventQueue;
std::unique_ptr<std::thread> eventThread;
std::mutex eventThreadMutex;
std::condition_variable eventThreadCondVar;
bool stopThread = false;
std::atomic_bool threadStarted;
bool isFinished = false;
// Non-public static functions
private:
static void eventThreadFunc(EventSystem* eventSystem) noexcept;
static void init(int argc, char** argv);
static void finish() noexcept;
// Non-public functions
private:
EventSystem() noexcept;
void finishInstance() noexcept;
// Public functions
public:
~EventSystem() noexcept;
static EventSystem& getInstance() noexcept { return singleton; }
/**
* Adds an event to the event queue.
*
* It is discouraged to use this function directly.
* Use the static fireEvent function found in each Event class instead (e.g. GeneralLoggingEvent::fireEvent(....)).
**/
void fireEvent(std::shared_ptr<Event> event, bool noLocking = false) noexcept;
bool addListener(EventListener& listener) noexcept;
bool removeListener(EventListener& listener) noexcept;
};
} /* namespace eventsystem */
} /* namespace wfr */