-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathentitymanager.hpp
More file actions
169 lines (166 loc) · 8.92 KB
/
entitymanager.hpp
File metadata and controls
169 lines (166 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <fea/config.hpp>
#include <fea/entity/entitystorage.hpp>
#include <memory>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <stdint.h>
#include <set>
namespace fea
{
class Entity;
using EntityPtr = std::shared_ptr<Entity>;
using WeakEntityPtr = std::weak_ptr<Entity>;
using EntityId = int32_t;
using EntitySet = std::set<WeakEntityPtr, std::owner_less<WeakEntityPtr>>;
class FEA_API EntityManager
{
public:
WeakEntityPtr createEntity(const std::set<std::string>& attributes);
WeakEntityPtr findEntity(EntityId id) const;
void removeEntity(const EntityId id);
template<class DataType>
const DataType& getAttribute(const EntityId id, const std::string& attribute) const;
template<class DataType>
DataType& getAttribute(const EntityId id, const std::string& attribute);
template<class DataType>
void setAttribute(const EntityId id, const std::string& attribute, DataType attributeData);
bool hasAttribute(const EntityId id, const std::string& attribute) const;
template<class DataType>
void registerAttribute(const std::string& attributeName);
bool attributeIsValid(const std::string& attributeName) const;
EntitySet getAll() const;
void removeAll();
void clear();
std::unordered_set<std::string> getAttributes(EntityId id) const;
private:
std::unordered_map<EntityId, EntityPtr> mEntities;
EntityStorage mStorage;
};
#include <fea/entity/entitymanager.inl>
/** @addtogroup EntitySystem
*@{
* @class EntityManager
*
* @typedef EntityPtr
*
* @typedef WeakEntityPtr
*
* @typedef EntityId
*
* @typedef EntitySet
*@}
***
* @typedef EntityPtr
* @brief A shared pointer to an Entity instance.
*
* @typedef WeakEntityPtr
* @brief A weak pointer to an Entity instance.
*
* @typedef EntityId
* @brief The ID of an Entity instance. This is a normal 32 bit unsigned integer.
*
* @typedef EntitySet
* @brief An std::set containing a number of entities represented with WeakEntityPtr instances.
*
* @class EntityManager
* @brief Takes care of managing multiple Entity instances.
*
* This class is needed to create Entity instances. All entities will be owned by the EntityManager. It provides ways of accessing and deleting Entity instances of a given entity ID.
*
* The only way to access Entity instances created by the EntityManager is through WeakEntityPtr instances. WeakEntityPtr is simply an alias to std::weak_ptr<Entity>. Entity pointers received from the EntityManager should never be manually freed, since that is the EntityManager's responsibility. If an entity is meant to be deleted, use the EntityManager::removeEntity function.
*
* Prior to creating any Entity instances, attributes must be registered. Attributes are values belonging to entities. Some examples of attributes includes "health", "weight", "position" and "velocity". The type for the attribute is remembered by the entity manager. Registration is done using EntityManager::registerAttribute.
*
* After attributes have been registered, entities can be created. Entities have zero or more of registered attributes and they can be set for individual entities.
***
* @fn WeakEntityPtr EntityManager::createEntity(const std::set<std::string>& attributes)
* @brief Create an Entity with the given attributes.
*
* The attributes must have been registered prior to creating the Entity. If the function succeeds in creating the Entity, it will be assigned a unique ID and a WeakEntityPtr pointing to the Entity will be returned. The returned pointer is not meant to be stored in a locked state since that entity would still become invalid if the entity is deleted using the EntityManager::removeEntity method.
* @param attributes The names of the attributes the entity should have.
* @return A pointer to the created Entity.
***
* @fn WeakEntityPtr EntityManager::findEntity(EntityId id) const
* @brief Search for an entity with a given ID.
* @param id ID of the entity to find.
* @return Pointer to the entity. Will be null if no such entity exists.
***
* @fn void EntityManager::removeEntity(const EntityId id)
* @brief Remove an Entity.
*
* The removed Entity will become invalid instantly after the deletion. This means that if there are any WeakEntityPtr pointing to this Entity elsewhere in the application, those pointers must not be dereferenced. In the case there is a risk for pointing at an invalid Entity, the sanity of the pointer should always be checked before dereferencing it.
* Assert/undefined behavior when the entity does not exist.
*
* @param id ID of the Entity to remove.
***
* @fn const DataType& EntityManager::getAttribute(const EntityId id, const std::string& attribute) const
* @brief Retrieve the value of an attribute of a selected Entity.
*
* Assert/undefined behavior when the attribute does not exist or the wrong template argument is provided or the entity does not exist.
* @tparam DataType of the attribute to get.
* @param attribute Name of the attribute to get.
* @param id ID of the Entity to get the attribute from.
***
* @fn DataType& EntityManager::getAttribute(const EntityId id, const std::string& attribute)
* @brief Retrieve the value of an attribute of a selected Entity.
*
* Assert/undefined behavior when the attribute does not exist or the wrong template argument is provided or the entity does not exist.
* @tparam DataType of the attribute to get.
* @param attribute Name of the attribute to get.
* @param id ID of the Entity to get the attribute from.
***
* @fn void EntityManager::setAttribute(const EntityId id, const std::string& attribute, DataType attributeData)
* @brief Set the value of an attribute of a selected Entity.
*
* Assert/undefined behavior when the attribute does not exist or the wrong template argument is provide or the entity does not exist..
* @tparam DataType of the attribute to set.
* @param id ID of the Entity to set the attribute of.
* @param attribute Name of the attribute to set.
* @param attributeData Value to set the attribute to.
***
* @fn bool EntityManager::hasAttribute(const EntityId id, const std::string& attribute) const
* @brief Check if an attribute exists for a specific Entity.
*
* Assert/undefined behavior when the entity does not exist.
* @param id Entity to check an attribute for.
* @param attribute Name of the attribute to check for.
* @return True if the attribute exists, otherwise false.
***
* @fn void EntityManager::registerAttribute(const std::string& attribute)
* @brief Register an attribute.
*
* For an Entity to be able to have a certain attribute, the attribute needs to be registered, otherwise it can't be stored by the EntityManager. This only needs to be done once for every attribute.
*
* For example, this is how you would register an attribute called "Health points" as a 4 byte integer:
* @code
* entityManager.registerAttribute<int32_t>("Health points");
* @endcode
* Assert/undefined behavior when the attribute is already registered.
* @tparam DataType Type of the attribute.
* @param attribute Name of the attribute to register.
***
* @fn bool EntityManager::attributeIsValid(const std::string& attributeName) const
* @brief Check if an attribute has been registered.
* @param attributeName Name of the attribute.
* @return True if valid.
***
* @fn EntitySet EntityManager::getAll() const
* @brief Retrieve an EntitySet filled with all entities currently managed by the EntityManager.
* @return All entities in a set.
***
* @fn void EntityManager::removeAll()
* @brief Remove all Entity instances managed by the EntityManager, leaving all pointers to them invalid.
***
* @fn void EntityManager::clear()
* @brief Reset the whole state of the EntityManager to the original state. Effectively removing all Entity instances, leaving all pointers to them invalid, as well as clearing out any registered attributes. Not to be confused with EntityManager::removeAll which only removes all entities.
***
* @fn std::unordered_set<std::string> EntityManager::getAttributes(EntityId id) const
* @brief Get a set containing all the attributes of an entity.
*
* Assert/undefined behavior if the entity does not exist.
* @param id Id of the entity to get attributes for.
* @return Set with attributes.
***/
}