CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
13
15
16#include <type_traits>
17
18namespace CeresEngine {
19
22 using EntityEventID = unsigned int;
23
27 friend class EntityEventManager;
28
29 protected:
31 EntityManager* mEntityManager = nullptr;
32
34 EntityEventManager* mEventManager = nullptr;
35
36 public:
38 virtual ~AbstractEntityEvent() = default;
39 };
40
43 template<typename T> struct EntityEvent : public AbstractEntityEvent {
44 private:
47
48 public:
51
55 };
56
60
64
71
74
78 static_assert(isEntityEvent<E>, "E must be an Event type!");
79 static_assert(std::is_final_v<E>, "Events must be final!");
80 }
81
82 template<CEntityEvent E> class EntityEventListener;
83
84 template<CEntityEvent... Es> struct EntityEventSet {
86 static_assert(areEntityEvents<Es...>, "All parameters in Es must be events.");
87
91 template<CEntityEvent E> static inline constexpr bool includes = (std::is_same_v<E, Es> || ...);
92 };
93
94 template<> struct EntityEventSet<> {
98 template<CEntityEvent E> static inline constexpr bool includes = false;
99 };
100
104 public:
108
109 protected:
110 explicit EntityEventType(const EntityEventID eventID) : eventID(eventID) {}
111
112 public:
117
118 protected: // EntityEventID allocation
120 static inline EntityEventID nextEventID = 0;
121
122 public:
125 friend bool operator==(const EntityEventType& lhs, const EntityEventType& rhs) noexcept {
126 // NOTE: This is safe as we only ever create a single instance for
127 // any given event type.
128 //
129 return &lhs == &rhs;
130 }
131
134 friend bool operator!=(const EntityEventType& lhs, const EntityEventType& rhs) noexcept { return !(lhs == rhs); }
135
136 public:
141 template<typename E> static const EntityEventType& get() noexcept;
142 };
143
146 template<typename E> class TEntityEventType : public EntityEventType {
147 private:
148 friend class EntityEventType;
149
152 explicit TEntityEventType() : EntityEventType(getEventID()) {}
153
154 protected: // EntityEvent ID generation & retrieval
156 [[nodiscard]] static EntityEventID getEventID() noexcept {
157 static EntityEventID id = nextEventID++;
158 return id;
159 }
160 };
161
162 template<typename E> inline const EntityEventType& EntityEventType::get() noexcept {
163 static const TEntityEventType<E> instance;
164 return instance;
165 }
166
167 template<typename E> [[nodiscard]] EntityEventID EntityEvent<E>::getEventID() noexcept { return EntityEventType::get<E>().eventID; }
168
169#define CE_EXTERN_ENTITY_EVENT_BASE(T, EXTERN) \
170 EXTERN template struct ::CeresEngine::EntityEvent<T>; \
171 EXTERN template class ::CeresEngine::TEntityEventType<T>; \
172 EXTERN template const ::CeresEngine::EntityEventType& ::CeresEngine::EntityEventType::get<T>() noexcept;
173#define CE_EXTERN_ENTITY_EVENT(T) CE_EXTERN_ENTITY_EVENT_BASE(T, extern)
174#define CE_EXTERN_ENTITY_EVENT_IMPL(T) CE_EXTERN_ENTITY_EVENT_BASE(T, )
175
176} // namespace CeresEngine
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
Definition EventManager.hpp:24
A type that describes and provides type-erased operations on a entity event.
Definition Event.hpp:103
friend bool operator==(const EntityEventType &lhs, const EntityEventType &rhs) noexcept
Compares two EntityEventType instances for equality.
Definition Event.hpp:125
EntityEventType(EntityEventType &&)=delete
EntityEventType(const EntityEventType &)=delete
EntityEventType(const EntityEventID eventID)
Definition Event.hpp:110
friend bool operator!=(const EntityEventType &lhs, const EntityEventType &rhs) noexcept
Compares two EntityEventType instances for inequality.
Definition Event.hpp:134
EntityEventType & operator=(const EntityEventType &)=delete
const EntityEventID eventID
The event type ID.
Definition Event.hpp:107
EntityEventType & operator=(EntityEventType &&)=delete
The base entity class.
Definition Entity.hpp:41
Definition EntityManager.hpp:49
An implementation of the EntityEventType interface that implements type-erased operations for events.
Definition Event.hpp:146
TEntityEventType()
Creates a new instance of a event type object for the given E event type.
Definition Event.hpp:152
static EntityEventID getEventID() noexcept
Definition Event.hpp:156
Base template for the event class.
Definition Event.hpp:27
Definition Event.hpp:73
Definition Application.hpp:19
constexpr bool areEntityEvents
A trait that checks if the types Es are all events.
Definition Event.hpp:63
constexpr void checkEvent()
A trait that checks if the type E is a valid event type.
Definition Event.hpp:77
constexpr bool isEntityEvent
A trait that checks if the type E is a event.
Definition Event.hpp:59
typename std::enable_if< isEntityEvent< E >, T >::type ifEntityEvent
If the type E is a event (as defined by isEntityEvent<E>), this type is aliased to T.
Definition Event.hpp:70
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
unsigned int EntityEventID
A numeric type that represents a event.
Definition Event.hpp:22
Definition Span.hpp:668
A abstract class that provides a trait that allows checking for event implementations.
Definition Event.hpp:26
virtual ~AbstractEntityEvent()=default
Defaulted virtual destructor.
A template class that wraps a event.
Definition Event.hpp:43
static EntityEventID getEventID() noexcept
Definition Event.hpp:167
static const EntityEventID eventID
This entity event type ID.
Definition Event.hpp:54
Definition Event.hpp:84