SDEngine
Game Engine
Loading...
Searching...
No Matches
Command.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "SD/core/types.hpp"
11
12namespace sd {
13
14class Command;
15class CommandQueue;
16class EntityManager;
17
19public:
20 static u32 register_(const char* name, u32 component_id = 0);
21 static u32 get_id(const char* name, u32 componentId = 0);
22 static const char* get_name(u32 id);
23
24private:
25 static inline std::vector<const char*> names;
26 static inline std::unordered_map<std::string, u32> map;
27};
28
30public:
31 using CreatorFn = std::function<std::unique_ptr<Command>()>;
32
33 static void register_(u32 type_id, CreatorFn creator);
34 static std::unique_ptr<Command> create(u32 type_id);
35
36private:
37 static inline std::vector<CreatorFn> creators;
38};
39
40#define COMMAND_ID(Name) \
41public: \
42 inline static u32 s_type_id; \
43 u32 get_type_id() const override { \
44 return s_type_id; \
45 } \
46 \
47private:
48
49#define COMMAND_ID_T(Name, T) \
50public: \
51 inline static u32 s_type_id; \
52 u32 get_type_id() const override { \
53 return s_type_id; \
54 } \
55 \
56private:
57
58#define REGISTER_COMMAND(Name) \
59 namespace { \
60 struct Name##_CommandRegistrar { \
61 Name##_CommandRegistrar() { \
62 sd::Name::s_type_id = sd::CommandRegistry::register_(#Name); \
63 sd::CommandFactory::register_(sd::Name::s_type_id, \
64 [] { return std::make_unique<sd::Name>(); }); \
65 } \
66 }; \
67 static Name##_CommandRegistrar s_##Name##_registrar; \
68 }
69
70#define REGISTER_COMPONENT_COMMANDS(T) \
71 namespace { \
72 struct AddComponentCmd##T##_Registrar { \
73 AddComponentCmd##T##_Registrar() { \
74 sd::AddComponentCmd<T>::s_type_id = \
75 sd::CommandRegistry::register_("AddComponentCmd", sd::ComponentTraits<T>::id); \
76 sd::CommandFactory::register_(sd::AddComponentCmd<T>::s_type_id, \
77 [] { return std::make_unique<sd::AddComponentCmd<T>>(); }); \
78 } \
79 }; \
80 static AddComponentCmd##T##_Registrar s_AddComponentCmd##T##_registrar; \
81 } \
82 namespace { \
83 struct RemoveComponentCmd##T##_Registrar { \
84 RemoveComponentCmd##T##_Registrar() { \
85 sd::RemoveComponentCmd<T>::s_type_id = \
86 sd::CommandRegistry::register_("RemoveComponentCmd", sd::ComponentTraits<T>::id); \
87 sd::CommandFactory::register_(sd::RemoveComponentCmd<T>::s_type_id, \
88 [] { return std::make_unique<sd::RemoveComponentCmd<T>>(); }); \
89 } \
90 }; \
91 static RemoveComponentCmd##T##_Registrar s_RemoveComponentCmd##T##_registrar; \
92 }
93
100 explicit EntityHandle(const u32 id) : id(id) {}
101
102 [[nodiscard]] bool is_valid() const { return id != g_type_max<u32>; }
103
104 bool operator==(const EntityHandle& other) const { return id == other.id; }
105 bool operator!=(const EntityHandle& other) const { return !(*this == other); }
106};
107
108class Command {
109public:
110 virtual ~Command() = default;
111
112 virtual u32 get_type_id() const = 0;
113
118 virtual void execute(EntityManager& em, CommandQueue& queue) = 0;
119
124 virtual void serialize(Serializer& serializer) const = 0;
125
131};
132} // namespace sd
133
134template<>
135struct std::hash<sd::EntityHandle> {
136 inline std::size_t operator()(sd::EntityHandle h) const noexcept {
137 return std::hash<u32>{}(h.id);
138 }
139};
Definition Command.hpp:29
static std::vector< CreatorFn > creators
Definition Command.hpp:37
static std::unique_ptr< Command > create(u32 type_id)
Definition CommandQueue.cpp:43
std::function< std::unique_ptr< Command >()> CreatorFn
Definition Command.hpp:31
static void register_(u32 type_id, CreatorFn creator)
Definition CommandQueue.cpp:36
Definition CommandQueue.hpp:7
Definition Command.hpp:18
static std::unordered_map< std::string, u32 > map
Definition Command.hpp:26
static u32 get_id(const char *name, u32 componentId=0)
Definition CommandQueue.cpp:20
static u32 register_(const char *name, u32 component_id=0)
Definition CommandQueue.cpp:8
static const char * get_name(u32 id)
Definition CommandQueue.cpp:29
static std::vector< const char * > names
Definition Command.hpp:25
Definition Command.hpp:108
virtual void deserialize(Serializer &serializer)=0
virtual u32 get_type_id() const =0
virtual ~Command()=default
virtual void serialize(Serializer &serializer) const =0
virtual void execute(EntityManager &em, CommandQueue &queue)=0
Definition EntityManager.hpp:83
Definition serialization.hpp:36
Definition Application.hpp:28
Definition Command.hpp:97
EntityHandle()
Definition Command.hpp:99
EntityHandle(const u32 id)
Definition Command.hpp:100
bool operator==(const EntityHandle &other) const
Definition Command.hpp:104
u32 id
Definition Command.hpp:98
bool operator!=(const EntityHandle &other) const
Definition Command.hpp:105
bool is_valid() const
Definition Command.hpp:102
std::size_t operator()(sd::EntityHandle h) const noexcept
Definition Command.hpp:136
std::uint32_t u32
Definition types.hpp:15
constexpr T g_type_max
Definition types.hpp:21