OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
EventSystem.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16#ifndef ENGINE_EVENTSYSTEM_H
17#define ENGINE_EVENTSYSTEM_H
18
19#include <open-cpp-utils/unique_id.h>
20
21#include <cstdint>
22#include <list>
23#include <mutex>
24
25
26#define MAX_EVENT_TYPES 256
27
28namespace ocu = open_cpp_utils;
29
30namespace OpenShaderDesigner
31{
35 struct Event
36 {
37 template<typename T>
38 static uint8_t TypeOf() { return static_cast<uint8_t>(ocu::unique_id<uint8_t, T>()); }
39
44 virtual inline uint8_t GetID() const = 0;
45 };
46
47
52 {
53 virtual bool _HandleEvent(const Event* event) = 0;
54
55 friend class EventSystem;
56 };
57
62 template<typename EventType>
64 {
65 public:
66 using HandledType = EventType;
67
72 virtual bool HandleEvent(const HandledType* event) = 0;
73 private:
74
79 bool _HandleEvent(const Event* event) override;
80 };
81
86 {
87 public:
91 static void PostEvent(const Event*);
92
97 template<typename T>
98 static void RegisterHandler(EventHandler<T>*);
99
104 template<typename T>
106
107 private:
108 inline static std::list<_ImplEventHandler*> HandlerMap[MAX_EVENT_TYPES];
109 inline static std::mutex Lock;
110
111 EventSystem(const EventSystem&) = delete;
112 EventSystem(EventSystem&&) = delete;
113 };
114
115 template<typename T>
117 {
118 // Thread safe
119 std::lock_guard guard(Lock);
120 const uint8_t index = T::ID;
121 std::erase(HandlerMap[index], reinterpret_cast<_ImplEventHandler*>(handler));
122 }
123
124 template<typename T>
126 {
127 // Thread safe
128 std::lock_guard guard(Lock);
129 const uint8_t index = T::ID;
130 HandlerMap[index].push_back(reinterpret_cast<_ImplEventHandler*>(handler));
131 }
132
133 template<typename EventType>
135 {
136 if(EventType::ID != event->GetID()) return false;
137 return HandleEvent(reinterpret_cast<const EventType*>(event));
138 }
139}
140
141#define BeginEvent(EVENT) struct EVENT : OpenShaderDesigner::Event \
142 { \
143 static inline const uint8_t ID = Event::TypeOf<EVENT>(); \
144 inline uint8_t GetID() const override { return ID; }
145
146#define EndEvent };
147
148#endif //ENGINE_EVENTSYSTEM_H
Base EventHandler for abstraction.
Definition EventSystem.h:52
EventHandler interface for creating custom EventHandlers.
Definition EventSystem.h:64
virtual bool HandleEvent(const HandledType *event)=0
Virtual function for custom EventHandler implementations.
EventType HandledType
The type handled by the EventHandler.
Definition EventSystem.h:66
EventSystem for posting Events to be handled.
Definition EventSystem.h:86
static void UnregisterHandler(EventHandler< T > *)
Unregister an EventHandler with the EventSystem.
Definition EventSystem.h:116
static void PostEvent(const Event *)
Post an Event to be Handled.
Definition EventSystem.cpp:23
static void RegisterHandler(EventHandler< T > *)
Register an EventHandler with the EventSystem.
Definition EventSystem.h:125
Base Event class for sending events to the Engine.
Definition EventSystem.h:36
virtual uint8_t GetID() const =0
Get the Event's type ID.