# OpenShaderDesigner: Include/Core/EventSystem.h Source File

Loading...
Searching...
No Matches
EventSystem.h
// =====================================================================================================================
// Copyright 2024 Medusa Slockbower
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =====================================================================================================================
#ifndef ENGINE_EVENTSYSTEM_H
#define ENGINE_EVENTSYSTEM_H
#include <open-cpp-utils/unique_id.h>
#include <cstdint>
#include <list>
#include <mutex>
#define MAX_EVENT_TYPES 256
namespace ocu = open_cpp_utils;
namespace OpenShaderDesigner
{
struct Event
{
template<typename T>
static uint8_t TypeOf() { return static_cast<uint8_t>(ocu::unique_id<uint8_t, T>()); }
virtual inline uint8_t GetID() const = 0;
};
class _ImplEventHandler
{
virtual bool _HandleEvent(const Event* event) = 0;
friend class EventSystem;
};
template<typename EventType>
class EventHandler : private _ImplEventHandler
{
public:
using HandledType = EventType;
virtual bool HandleEvent(const HandledType* event) = 0;
private:
bool _HandleEvent(const Event* event) override;
};
class EventSystem
{
public:
static void PostEvent(const Event*);
template<typename T>
static void RegisterHandler(EventHandler<T>*);
template<typename T>
static void UnregisterHandler(EventHandler<T>*);
private:
inline static std::list<_ImplEventHandler*> HandlerMap[MAX_EVENT_TYPES];
inline static std::mutex Lock;
EventSystem(const EventSystem&) = delete;
EventSystem(EventSystem&&) = delete;
};
template<typename T>
void EventSystem::UnregisterHandler(EventHandler<T>* handler)
{
// Thread safe
std::lock_guard guard(Lock);
const uint8_t index = T::ID;
std::erase(HandlerMap[index], reinterpret_cast< _ImplEventHandler*>(handler));
}
template<typename T>
void EventSystem::RegisterHandler(EventHandler<T>* handler)
{
// Thread safe
std::lock_guard guard(Lock);
const uint8_t index = T::ID;
HandlerMap[index].push_back(reinterpret_cast< _ImplEventHandler*>(handler));
}
template<typename EventType>
bool EventHandler<EventType>::_HandleEvent(const Event *event)
{
if(EventType::ID != event->GetID()) return false;
return HandleEvent(reinterpret_cast<const EventType*>(event));
}
}
#define BeginEvent(EVENT) struct EVENT : OpenShaderDesigner::Event \
{ \
static inline const uint8_t ID = Event::TypeOf<EVENT>(); \
inline uint8_t GetID() const override { return ID; }
#define EndEvent };
#endif //ENGINE_EVENTSYSTEM_H
OpenShaderDesigner::_ImplEventHandler
Base EventHandler for abstraction.
Definition EventSystem.h:52
OpenShaderDesigner::EventHandler
EventHandler interface for creating custom EventHandlers.
Definition EventSystem.h:64
OpenShaderDesigner::EventHandler::HandleEvent
virtual bool HandleEvent(const HandledType *event)=0
Virtual function for custom EventHandler implementations.
OpenShaderDesigner::EventHandler::HandledType
EventType HandledType
The type handled by the EventHandler.
Definition EventSystem.h:66
OpenShaderDesigner::EventSystem
EventSystem for posting Events to be handled.
Definition EventSystem.h:86
OpenShaderDesigner::EventSystem::UnregisterHandler
static void UnregisterHandler(EventHandler< T > *)
Unregister an EventHandler with the EventSystem.
Definition EventSystem.h:116
OpenShaderDesigner::EventSystem::PostEvent
static void PostEvent(const Event *)
Post an Event to be Handled.
Definition EventSystem.cpp:23
OpenShaderDesigner::EventSystem::RegisterHandler
static void RegisterHandler(EventHandler< T > *)
Register an EventHandler with the EventSystem.
Definition EventSystem.h:125
OpenShaderDesigner::Event
Base Event class for sending events to the Engine.
Definition EventSystem.h:36
OpenShaderDesigner::Event::GetID
virtual uint8_t GetID() const =0
Get the Event's type ID.