CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Action.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
14#include <type_traits>
15
16namespace CeresEngine {
17
19 using EntityActionID = unsigned int;
20
23
33 template<typename T, typename R = void> struct EntityAction : public AbstractEntityAction {
34 public:
36 using ReturnType = R;
37
38 private:
41
42 public:
45 };
46
50
54
60
67
70
74 static_assert(isEntityAction<A>, "A must be an Action type!");
75 static_assert(std::is_final_v<A>, "Actions must be final!");
76 }
77
78 template<typename A> class EntityActionHandler;
79 class EntityActionManager;
80
81 template<CEntityAction... As> struct EntityActionSet {
83 static_assert(areEntityActions<As...>, "All parameters in As must be actions.");
84
88 template<CEntityAction A> static inline constexpr bool includes = (std::is_same_v<A, As> || ...);
89 };
90
91 template<> struct EntityActionSet<> {
95 template<CEntityAction A> static inline constexpr bool includes = false;
96 };
97
101 public:
105
106 protected:
107 explicit EntityActionType(const EntityActionID actionID) : actionID(actionID) {}
108
109 public:
114
115 protected: // EntityActionID allocation
117 static inline EntityActionID nextActionID = 0;
118
119 public:
122 friend bool operator==(const EntityActionType& lhs, const EntityActionType& rhs) noexcept {
123 // NOTE: This is safe as we only ever create a single instance for
124 // any given action type.
125 //
126 return &lhs == &rhs;
127 }
128
131 friend bool operator!=(const EntityActionType& lhs, const EntityActionType& rhs) noexcept { return !(lhs == rhs); }
132
133 public:
138 template<typename A> static const EntityActionType& get() noexcept;
139 };
140
143 template<typename A> class TEntityActionType : public EntityActionType {
144 private:
145 friend class EntityActionType;
146
149 explicit TEntityActionType() : EntityActionType(getActionID()) {}
150
151 protected: // EntityAction ID generation & retrieval
153 [[nodiscard]] static EntityActionID getActionID() noexcept {
154 static EntityActionID id = nextActionID++;
155 return id;
156 }
157 };
158
159 template<typename A> inline const EntityActionType& EntityActionType::get() noexcept {
160 static const TEntityActionType<A> instance;
161 return instance;
162 }
163
164 template<typename A, typename R> [[nodiscard]] EntityActionID EntityAction<A, R>::getActionID() noexcept { return EntityActionType::get<A>().actionID; }
165
166#define CE_EXTERN_ENTITY_ACTION_BASE(T, EXTERN) \
167 EXTERN template struct ::CeresEngine::EntityAction<T>; \
168 EXTERN template class ::CeresEngine::TEntityActionType<T>; \
169 EXTERN template const ::CeresEngine::EntityActionType& ::CeresEngine::EntityActionType::get<T>() noexcept;
170#define CE_EXTERN_ENTITY_ACTION(T) CE_EXTERN_ENTITY_ACTION_BASE(T, extern)
171#define CE_EXTERN_ENTITY_ACTION_IMPL(T) CE_EXTERN_ENTITY_ACTION_BASE(T, )
172
173} // namespace CeresEngine
#define CE_EXPORT
The CE_EXPORT macro marks a symbol as exported in a DLL.
Definition Macros.hpp:229
#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
A type that describes and provides type-erased operations on a entity action.
Definition Action.hpp:100
friend bool operator!=(const EntityActionType &lhs, const EntityActionType &rhs) noexcept
Compares two EntityActionType instances for inequality.
Definition Action.hpp:131
EntityActionType(EntityActionType &&)=delete
EntityActionType(const EntityActionID actionID)
Definition Action.hpp:107
const EntityActionID actionID
The action type ID.
Definition Action.hpp:104
EntityActionType & operator=(const EntityActionType &)=delete
EntityActionType(const EntityActionType &)=delete
friend bool operator==(const EntityActionType &lhs, const EntityActionType &rhs) noexcept
Compares two EntityActionType instances for equality.
Definition Action.hpp:122
EntityActionType & operator=(EntityActionType &&)=delete
An implementation of the EntityActionType interface that implements type-erased operations for action...
Definition Action.hpp:143
TEntityActionType()
Creates a new instance of a action type object for the given A action type.
Definition Action.hpp:149
static EntityActionID getActionID() noexcept
Definition Action.hpp:153
Definition Action.hpp:69
Definition Application.hpp:19
constexpr bool isEntityAction
A trait that checks if the type C is a action.
Definition Action.hpp:49
typename std::enable_if< isEntityAction< A >, T >::type ifEntityAction
If the type A is a action (as defined by isAction<A>), this type is aliased to T.
Definition Action.hpp:59
typename std::enable_if< areEntityActions< As... >, T >::type ifEntityActions
If the types As are all actions (as defined by areActions<As>), this type is aliased to T.
Definition Action.hpp:66
unsigned int EntityActionID
A numeric type that represents a action.
Definition Action.hpp:19
constexpr bool areEntityActions
A trait that checks if the types As are all actions.
Definition Action.hpp:53
constexpr void checkAction()
A trait that checks if the type A is a valid action type.
Definition Action.hpp:73
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
A base class shared by all actions triggered by an entity.
Definition Action.hpp:22
A class that must be inherited by concrete action types.
Definition Action.hpp:33
static const EntityActionID actionID
This action type ID.
Definition Action.hpp:44
R ReturnType
The type that must be returned by action handlers.
Definition Action.hpp:36
static EntityActionID getActionID() noexcept
Definition Action.hpp:164
Definition Action.hpp:81