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
91 explicit entity(world_t *world, const char *name, const char *sep, const char *root_sep)
92 : entity_builder()
93 {
94 world_ = world;
95
96 ecs_entity_desc_t desc = {};
97 desc.name = name;
98 desc.sep = sep;
99 desc.root_sep = root_sep;
100 id_ = ecs_entity_init(world, &desc);
101 }
102
107 explicit entity(entity_t id)
108 : entity_builder( nullptr, id ) { }
109
110 #ifndef ensure
111
121 template <typename T>
122 T& ensure() const {
123 auto comp_id = _::type<T>::id(world_);
124 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
125 "operation invalid for empty type");
126 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id));
127 }
128
138 void* ensure(entity_t comp) const {
139 return ecs_ensure_id(world_, id_, comp);
140 }
141
148 template <typename First, typename Second, typename P = pair<First, Second>,
149 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
150 A& ensure() const {
151 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
152 _::type<First>::id(world_),
153 _::type<Second>::id(world_))));
154 }
155
162 template <typename First>
163 First& ensure(entity_t second) const {
164 auto first = _::type<First>::id(world_);
165 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
166 "operation invalid for empty type");
167 return *static_cast<First*>(
168 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
169 }
170
179 void* ensure(entity_t first, entity_t second) const {
180 return ecs_ensure_id(world_, id_, ecs_pair(first, second));
181 }
182
189 template <typename Second>
190 Second& ensure_second(entity_t first) const {
191 auto second = _::type<Second>::id(world_);
192 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
193 ECS_INVALID_PARAMETER, "pair is not a component");
194 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
195 ECS_INVALID_PARAMETER, "type of pair is not Second");
196 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
197 "operation invalid for empty type");
198 return *static_cast<Second*>(
199 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
200 }
201
202 #endif
203
208 template <typename T>
209 void modified() const {
210 auto comp_id = _::type<T>::id(world_);
211 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
212 "operation invalid for empty type");
213 this->modified(comp_id);
214 }
215
221 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
222 void modified() const {
223 auto first = _::type<First>::id(world_);
224 auto second = _::type<Second>::id(world_);
225 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
226 "operation invalid for empty type");
227 this->modified(first, second);
228 }
229
235 template <typename First>
236 void modified(entity_t second) const {
237 auto first = _::type<First>::id(world_);
238 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
239 "operation invalid for empty type");
240 this->modified(first, second);
241 }
242
250 void modified(entity_t first, entity_t second) const {
251 this->modified(ecs_pair(first, second));
252 }
253
258 void modified(entity_t comp) const {
259 ecs_modified_id(world_, id_, comp);
260 }
261
280 template <typename T, if_t< is_actual<T>::value > = 0>
281 ref<T> get_ref_w_id(flecs::id_t component) const {
282 _::type<T>::id(world_); // ensure type is registered
283 return ref<T>(world_, id_, component);
284 }
285
293 template <typename T, if_t< is_actual<T>::value > = 0>
294 ref<T> get_ref() const {
295 return ref<T>(world_, id_, _::type<T>::id(world_));
296 }
297
307 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
308 ref<A> get_ref() const {
309 return ref<A>(world_, id_,
310 ecs_pair(_::type<typename T::first>::id(world_),
311 _::type<typename T::second>::id(world_)));
312 }
313
314
315 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
316 typename A = actual_type_t<P>>
317 ref<A> get_ref() const {
318 return ref<A>(world_, id_,
319 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
320 }
321
322 template <typename First>
323 ref<First> get_ref(flecs::entity_t second) const {
324 auto first = _::type<First>::id(world_);
325 return ref<First>(world_, id_, ecs_pair(first, second));
326 }
327
328 untyped_ref get_ref(flecs::id_t component) const {
329 return untyped_ref(world_, id_, component);
330 }
331
332 untyped_ref get_ref(flecs::id_t first, flecs::id_t second) const {
333 return untyped_ref(world_, id_, ecs_pair(first, second));
334 }
335
336 template <typename Second>
337 ref<Second> get_ref_second(flecs::entity_t first) const {
338 auto second = _::type<Second>::id(world_);
339 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
340 ECS_INVALID_PARAMETER, "pair is not a component");
341 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
342 ECS_INVALID_PARAMETER, "type of pair is not Second");
343 return ref<Second>(world_, id_, ecs_pair(first, second));
344 }
345
352 void clear() const {
353 ecs_clear(world_, id_);
354 }
355
362 void destruct() const {
363 ecs_delete(world_, id_);
364 }
365
374 flecs::entity_view view() const {
375 return flecs::entity_view(
376 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
377 }
378
385 static
386 flecs::entity null(const flecs::world_t *world) {
387 flecs::entity result;
388 result.world_ = const_cast<flecs::world_t*>(world);
389 return result;
390 }
391
392 static
393 flecs::entity null() {
394 return flecs::entity();
395 }
396
397# ifdef FLECS_JSON
398# include "mixins/json/entity.inl"
399# endif
400};
401
402} // namespace flecs
403
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:1012
ecs_entity_desc_t::sep
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1024
ecs_entity_desc_t::root_sep
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:1028
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:1019
flecs::_::type
Definition flecs.hpp:39
flecs::component
Component class.
Definition component.hpp:385
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:362
flecs::entity::get_ref
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:294
flecs::entity::modified
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:236
flecs::entity::modified
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:222
flecs::entity::ensure
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:163
flecs::entity::modified
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:258
flecs::entity::clear
void clear() const
Clear an entity.
Definition entity.hpp:352
flecs::entity::get_ref_w_id
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by id.
Definition entity.hpp:281
flecs::entity::modified
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:250
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:179
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:209
flecs::entity::ensure
T & ensure() const
Get mutable component value.
Definition entity.hpp:122
flecs::entity::null
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:386
flecs::entity::ensure_second
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:190
flecs::entity::ensure
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:138
flecs::entity::entity
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:107
flecs::entity::entity
entity(world_t *world, const char *name, const char *sep, const char *root_sep)
Create a named entity.
Definition entity.hpp:91
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:374
flecs::entity::get_ref
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:308
flecs::entity::ensure
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:150
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:85
flecs::world
The world.
Definition world.hpp:137