Cogs.Foundation
Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace Cogs
6{
7 namespace Reflection
8 {
9 class Type;
10 }
11
12 namespace ComponentModel
13 {
18 {
19 public:
21 static void registerType();
22
25 void addComponent(ComponentHandle component);
26
29 void removeComponent(ComponentHandle component);
30
34 template<typename T>
35 [[nodiscard]] T * getComponent() const
36 {
37 static_assert(std::is_base_of<Component, T>::value, "<T> not derived from Component");
38 static Reflection::TypeId typeId = Component::getComponentTypeId(::getName<T>());
39
40 return static_cast<T *>(getComponent(typeId));
41 }
42
51 template<typename T>
52 void getComponents(ComponentCollection<T> & collection) const
53 {
54 static_assert(std::is_base_of<Component, T>::value, "<T> not derived from Component");
55 getComponents(Reflection::Name(::getName<T>()), collection);
56 }
57
59 [[nodiscard]] Component * getComponent(const Reflection::Name & typeName) const;
60
62 [[nodiscard]] Component * getComponent(const Reflection::TypeId & id) const;
63
71 void getComponents(const Reflection::Name & typeName, ComponentCollectionBase& collection) const;
72
74 template<typename T>
75 [[nodiscard]] T * getComponent(const Reflection::Type & type) const
76 {
77 static_assert(std::is_base_of<Component, T>::value, "<T> not derived from Component");
78 return static_cast<T *>(getComponentPtr(type));
79 }
80
86 [[nodiscard]] Component * getComponentPtr(const Reflection::Type & type) const;
87
89 template<typename T>
90 [[nodiscard]] ComponentHandle getComponentHandle() const
91 {
92 return getComponentHandle(Reflection::Name(::getName<T>()));
93 }
94
96 [[nodiscard]] ComponentHandle getComponentHandle(const Reflection::Name & componentName) const;
97
103 [[nodiscard]] ComponentHandle getComponentHandle(const Reflection::Type & type) const;
104
110 [[nodiscard]] ComponentHandle getComponentHandle(const Reflection::TypeId & id) const;
111
113 [[nodiscard]] constexpr size_t getId() const noexcept { return id; }
114
117 constexpr void setId(const size_t id) noexcept { this->id = id; }
118
120 [[nodiscard]] const std::string & getName() const noexcept { return name; }
121
124 void setName(const StringView& name);
125
127 [[nodiscard]] const ComponentCollectionBase & getComponents() const { return components; }
128
131 constexpr void setUserData(void* userData) noexcept { this->userData = userData; }
132
134 [[deprecated]] void setUserData(intptr_t userData) { this->userData = reinterpret_cast<void*>(userData); }
135
137 [[nodiscard]] constexpr void* getUserData() const noexcept { return this->userData; }
138
139 private:
142
144 std::string name;
145
147 size_t id = static_cast<size_t>(-1);
148
150 void* userData = nullptr;
151 };
152 }
153}
154
155template<> inline Cogs::StringView getName<Cogs::ComponentModel::Entity>() { return "Entity"; }
Cogs::StringView getName< Cogs::ComponentModel::Entity >()
Definition: Entity.h:155
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
A collection of components, held by component handles.
Definition: ComponentCollection.h:19
Typed collection of components.
Definition: ComponentCollection.h:157
Base class for Component instances.
Definition: Component.h:143
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
T * getComponent(const Reflection::Type &type) const
Get a pointer to the first component implementing the given type. Note T and type represented by type...
Definition: Entity.h:75
constexpr size_t getId() const noexcept
Get the unique identifier of this entity.
Definition: Entity.h:113
ComponentCollectionBase components
Handles to components attached to this entity.
Definition: Entity.h:141
constexpr void setId(const size_t id) noexcept
Definition: Entity.h:117
std::string name
Name of the entity.
Definition: Entity.h:144
void getComponents(ComponentCollection< T > &collection) const
Get all the components implementing the templated type.
Definition: Entity.h:52
const std::string & getName() const noexcept
Get the name of this entity.
Definition: Entity.h:120
const ComponentCollectionBase & getComponents() const
Get the collection of Component instances owned by this entity.
Definition: Entity.h:127
constexpr void * getUserData() const noexcept
Get user data.
Definition: Entity.h:137
constexpr void setUserData(void *userData) noexcept
Definition: Entity.h:131
void setUserData(intptr_t userData)
Deprecated use void*. Delete when Usage removed.
Definition: Entity.h:134
ComponentHandle getComponentHandle() const
Get a component handle to the first component implementing the given type.
Definition: Entity.h:90
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
Main Cogs namespace.
Definition: MortonCode.h:5
Handle to a Component instance.
Definition: Component.h:67
Represents an unique name.
Definition: Name.h:70