Flecs: include/flecs/addons/cpp/entity.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "entity_view.hpp"
11#include "mixins/entity/builder.hpp"
12
20
21namespace flecs
22{
23
29 struct entity : entity_builder<entity>
30{
31 entity() : entity_builder<entity>() { }
32
37 explicit entity(world_t *world)
38 : entity_builder()
39 {
40 world_ = world;
41 if (!ecs_get_scope(world_) && !ecs_get_with(world_)) {
42 id_ = ecs_new(world);
43 } else {
44 ecs_entity_desc_t desc = {};
45 id_ = ecs_entity_init(world_, &desc);
46 }
47 }
48
54 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
55 world_ = const_cast<flecs::world_t*>(world);
56 id_ = id;
57 }
58
68 explicit entity(world_t *world, const char *name)
69 : entity_builder()
70 {
71 world_ = world;
72
73 ecs_entity_desc_t desc = {};
74 desc.name = name;
75 desc.sep = "::";
76 desc.root_sep = "::";
77 id_ = ecs_entity_init(world, &desc);
78 }
79
84 explicit entity(entity_t id)
85 : entity_builder( nullptr, id ) { }
86
87 #ifndef ensure
88
98 template <typename T>
99 T& ensure() const {
100 auto comp_id = _::type<T>::id(world_);
101 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
102 "operation invalid for empty type");
103 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id));
104 }
105
115 void* ensure(entity_t comp) const {
116 return ecs_ensure_id(world_, id_, comp);
117 }
118
125 template <typename First, typename Second, typename P = pair<First, Second>,
126 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
127 A& ensure() const {
128 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
129 _::type<First>::id(world_),
130 _::type<Second>::id(world_))));
131 }
132
139 template <typename First>
140 First& ensure(entity_t second) const {
141 auto first = _::type<First>::id(world_);
142 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
143 "operation invalid for empty type");
144 return *static_cast<First*>(
145 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
146 }
147
156 void* ensure(entity_t first, entity_t second) const {
157 return ecs_ensure_id(world_, id_, ecs_pair(first, second));
158 }
159
166 template <typename Second>
167 Second& ensure_second(entity_t first) const {
168 auto second = _::type<Second>::id(world_);
169 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
170 ECS_INVALID_PARAMETER, "pair is not a component");
171 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
172 ECS_INVALID_PARAMETER, "type of pair is not Second");
173 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
174 "operation invalid for empty type");
175 return *static_cast<Second*>(
176 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
177 }
178
179 #endif
180
185 template <typename T>
186 void modified() const {
187 auto comp_id = _::type<T>::id(world_);
188 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
189 "operation invalid for empty type");
190 this->modified(comp_id);
191 }
192
198 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
199 void modified() const {
200 auto first = _::type<First>::id(world_);
201 auto second = _::type<Second>::id(world_);
202 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
203 "operation invalid for empty type");
204 this->modified(first, second);
205 }
206
212 template <typename First>
213 void modified(entity_t second) const {
214 auto first = _::type<First>::id(world_);
215 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
216 "operation invalid for empty type");
217 this->modified(first, second);
218 }
219
227 void modified(entity_t first, entity_t second) const {
228 this->modified(ecs_pair(first, second));
229 }
230
235 void modified(entity_t comp) const {
236 ecs_modified_id(world_, id_, comp);
237 }
238
246 template <typename T, if_t< is_actual<T>::value > = 0>
247 ref<T> get_ref() const {
248 return ref<T>(world_, id_, _::type<T>::id(world_));
249 }
250
260 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
261 ref<A> get_ref() const {
262 return ref<A>(world_, id_,
263 ecs_pair(_::type<typename T::first>::id(world_),
264 _::type<typename T::second>::id(world_)));
265 }
266
267
268 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
269 typename A = actual_type_t<P>>
270 ref<A> get_ref() const {
271 return ref<A>(world_, id_,
272 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
273 }
274
275 template <typename First>
276 ref<First> get_ref(flecs::entity_t second) const {
277 auto first = _::type<First>::id(world_);
278 return ref<First>(world_, id_, ecs_pair(first, second));
279 }
280
281 template <typename Second>
282 ref<Second> get_ref_second(flecs::entity_t first) const {
283 auto second = _::type<Second>::id(world_);
284 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
285 ECS_INVALID_PARAMETER, "pair is not a component");
286 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
287 ECS_INVALID_PARAMETER, "type of pair is not Second");
288 return ref<Second>(world_, id_, ecs_pair(first, second));
289 }
290
297 void clear() const {
298 ecs_clear(world_, id_);
299 }
300
307 void destruct() const {
308 ecs_delete(world_, id_);
309 }
310
319 flecs::entity_view view() const {
320 return flecs::entity_view(
321 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
322 }
323
330 static
331 flecs::entity null(const flecs::world_t *world) {
332 flecs::entity result;
333 result.world_ = const_cast<flecs::world_t*>(world);
334 return result;
335 }
336
337 static
338 flecs::entity null() {
339 return flecs::entity();
340 }
341
342# ifdef FLECS_JSON
343# include "mixins/json/entity.inl"
344# endif
345};
346
347} // namespace flecs
348
entity_view.hpp
Entity class with only readonly operations.
ecs_clear
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
ecs_get_with
ecs_id_t ecs_get_with(const ecs_world_t *world)
Get current with id.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_get_type_info
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t id)
Get the type for an id.
ecs_new
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity id.
ecs_entity_init
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
ecs_delete
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.
ecs_ensure_id
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
ecs_modified_id
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
ecs_get_scope
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
builder.hpp
Entity builder.
ecs_entity_desc_t
Used with ecs_entity_init().
Definition flecs.h:975
ecs_entity_desc_t::sep
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:987
ecs_entity_desc_t::root_sep
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:991
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:982
flecs::_::type
Definition flecs.hpp:38
flecs::component
Component class.
Definition flecs.hpp:30
flecs::entity_builder
Entity builder.
Definition builder.hpp:15
flecs::entity_view
Entity view.
Definition entity_view.hpp:28
flecs::entity_view::name
flecs::string_view name() const
Return the entity name.
Definition entity_view.hpp:78
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::destruct
void destruct() const
Delete an entity.
Definition entity.hpp:307
flecs::entity::get_ref
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:247
flecs::entity::modified
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:213
flecs::entity::modified
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:199
flecs::entity::ensure
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:140
flecs::entity::modified
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:235
flecs::entity::clear
void clear() const
Clear an entity.
Definition entity.hpp:297
flecs::entity::modified
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:227
flecs::entity::entity
entity(world_t *world, const char *name)
Create a named entity.
Definition entity.hpp:68
flecs::entity::ensure
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:156
flecs::entity::entity
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity id.
Definition entity.hpp:54
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:186
flecs::entity::ensure
T & ensure() const
Get mutable component value.
Definition entity.hpp:99
flecs::entity::null
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:331
flecs::entity::ensure_second
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:167
flecs::entity::ensure
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:115
flecs::entity::entity
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:84
flecs::entity::entity
entity(world_t *world)
Create entity.
Definition entity.hpp:37
flecs::entity::view
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:319
flecs::entity::get_ref
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:261
flecs::entity::ensure
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:127
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id::second
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::id::first
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
flecs::ref
Component reference.
Definition ref.hpp:23
flecs::world
The world.
Definition world.hpp:137