co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
entity_ref.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace co_ecs {
6
14public:
21 constexpr entity_ref(base_registry& registry, entity entity) noexcept : _entity(entity), _registry(registry) {
22 }
23
31 [[nodiscard]] constexpr auto archetype() const noexcept -> const archetype& {
32 return *_registry.get().get_location(_entity).archetype;
33 }
34
41 [[nodiscard]] constexpr auto alive() const noexcept -> bool {
42 return _registry.get().alive(_entity);
43 }
44
54 template<component... C>
55 [[nodiscard]] constexpr auto has() const noexcept -> bool {
56 return _registry.get().template has<C...>(_entity);
57 }
58
76 template<component C>
77 [[nodiscard]] constexpr auto get() -> C& {
78 return std::get<0>(base_registry::template get_impl<C>(_registry.get(), _entity));
79 }
80
98 template<component C>
99 [[nodiscard]] constexpr auto get() const -> const C& {
100 return std::get<0>(base_registry::template get_impl<C>(_registry.get(), _entity));
101 }
102
118 template<component C, component O, component... Args>
119 [[nodiscard]] constexpr auto get() -> decltype(auto) {
120 return base_registry::template get_impl<C, O, Args...>(_registry.get(), _entity);
121 }
122
133 template<component C, component O, component... Args>
134 [[nodiscard]] constexpr auto get() const -> decltype(auto) {
135 return base_registry::template get_impl<C, O, Args...>(_registry.get(), _entity);
136 }
137
149 template<component C>
150 [[nodiscard]] constexpr auto get_or_insert(auto&&... args) -> C& {
151 auto [inserted, ptr] = _registry.get().set_impl<C>(_entity);
152 if (inserted) {
153 std::construct_at(ptr, std::forward<decltype(args)>(args)...);
154 }
155 return *ptr;
156 }
157
176 template<component C, typename... Args>
177 constexpr auto set(Args&&... args) -> entity_ref {
178 auto [inserted, ptr] = _registry.get().set_impl<C>(_entity);
179 if (inserted) {
180 std::construct_at(ptr, std::forward<Args>(args)...);
181 } else {
182 *ptr = C{ std::forward<Args>(args)... };
183 }
184 return *this;
185 }
186
201 template<component C>
202 constexpr auto remove() -> entity_ref {
203 _registry.get().remove<C>(_entity);
204 return *this;
205 }
206
215 void destroy() {
216 _registry.get().destroy(_entity);
217 }
218
229 constexpr auto visit(auto&& func) {
230 _registry.get().visit(_entity, std::forward<decltype(func)>(func));
231 }
232
244 constexpr auto visit(auto&& func) const {
245 static_cast<const base_registry&>(_registry.get()).visit(_entity, std::forward<decltype(func)>(func));
246 }
247
265 auto clone() const -> entity_ref {
266 return entity_ref{ _registry, _registry.get().clone(_entity) };
267 }
268
275 auto clone(placeholder_entity placeholder) {
276 return entity_ref{ _registry, _registry.get().clone(_entity, placeholder) };
277 }
278
286 auto copy(base_registry& destination) const -> entity_ref {
287 return entity_ref{ destination, _registry.get().copy(_entity, destination) };
288 }
289
298 auto copy(base_registry& destination, placeholder_entity placeholder) const {
299 return entity_ref{ destination, _registry.get().copy(_entity, destination, placeholder) };
300 }
301
309 auto move(base_registry& destination) -> entity_ref {
310 return entity_ref{ destination, _registry.get().move(_entity, destination) };
311 }
312
321 auto move(base_registry& destination, placeholder_entity placeholder) {
322 return entity_ref{ destination, _registry.get().move(_entity, destination, placeholder) };
323 }
324
331 [[nodiscard]] constexpr operator entity() const noexcept {
332 return _entity;
333 }
334
341 [[nodiscard]] constexpr operator bool() const noexcept {
342 return _entity.valid();
343 }
344
345private:
346 std::reference_wrapper<base_registry> _registry;
347 entity _entity;
348};
349
350
356public:
361 constexpr const_entity_ref(const base_registry& registry, entity entity) : _entity(entity), _registry(registry) {
362 }
363
371 [[nodiscard]] constexpr auto archetype() const noexcept -> const archetype& {
372 return *_registry.get().get_location(_entity).archetype;
373 }
374
381 [[nodiscard]] constexpr auto alive() const noexcept -> bool {
382 return _registry.get().alive(_entity);
383 }
384
394 template<component... C>
395 [[nodiscard]] constexpr auto has() const noexcept -> bool {
396 return _registry.get().template has<C...>(_entity);
397 }
398
408 template<component C>
409 [[nodiscard]] constexpr auto get() const -> const C& {
410 return std::get<0>(base_registry::template get_impl<C>(_registry.get(), _entity));
411 }
412
423 template<component C, component O, component... Args>
424 [[nodiscard]] constexpr auto get() const -> decltype(auto) {
425 return base_registry::template get_impl<C, O, Args...>(_registry.get(), _entity);
426 }
427
439 constexpr auto visit(auto&& func) const {
440 static_cast<const base_registry&>(_registry.get()).visit(_entity, std::forward<decltype(func)>(func));
441 }
442
450 auto copy(base_registry& destination) const -> entity_ref {
451 return entity_ref{ destination, _registry.get().copy(_entity, destination) };
452 }
453
462 auto copy(base_registry& destination, placeholder_entity placeholder) const {
463 return entity_ref{ destination, _registry.get().copy(_entity, destination, placeholder) };
464 }
465
472 [[nodiscard]] constexpr operator entity() const noexcept {
473 return _entity;
474 }
475
482 [[nodiscard]] constexpr operator bool() const noexcept {
483 return _entity.valid();
484 }
485
486private:
487 std::reference_wrapper<const base_registry> _registry;
488 entity _entity;
489};
490
491} // namespace co_ecs
Archetype groups entities that share the same types of components. Archetype has a list of fixed size...
Definition archetype.hpp:15
Definition base_registry.hpp:35
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:355
auto copy(base_registry &destination, placeholder_entity placeholder) const
Copies the current entity to another registry into a placeholder entity.
Definition entity_ref.hpp:462
constexpr const_entity_ref(const base_registry &registry, entity entity)
Constructs a const entity reference for a specific entity in the given registry.
Definition entity_ref.hpp:361
constexpr auto get() const -> const C &
Retrieves a const-qualified component of type C from the entity in a read-only manner.
Definition entity_ref.hpp:409
constexpr auto has() const noexcept -> bool
Determines if the entity has all specified components.
Definition entity_ref.hpp:395
auto copy(base_registry &destination) const -> entity_ref
Copies the current entity to another registry.
Definition entity_ref.hpp:450
constexpr auto alive() const noexcept -> bool
Checks if the entity is currently active in the registry.
Definition entity_ref.hpp:381
constexpr auto get() const -> decltype(auto)
Retrieves multiple components from the underlying registry for the current entity (const version).
Definition entity_ref.hpp:424
constexpr auto archetype() const noexcept -> const archetype &
Retrieves a constant reference to the archetype of the entity.
Definition entity_ref.hpp:371
constexpr auto visit(auto &&func) const
Visit all components of an entity and apply a function to them (const version).
Definition entity_ref.hpp:439
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:13
auto clone() const -> entity_ref
Creates a duplicate of the current entity.
Definition entity_ref.hpp:265
constexpr auto archetype() const noexcept -> const archetype &
Retrieves a constant reference to the archetype of the entity.
Definition entity_ref.hpp:31
constexpr auto remove() -> entity_ref
Remove component C from an entity. In case entity does not have component attached nothing is done an...
Definition entity_ref.hpp:202
constexpr auto get() const -> const C &
Retrieves a const-qualified component of type C from the entity in a read-only manner.
Definition entity_ref.hpp:99
constexpr entity_ref(base_registry &registry, entity entity) noexcept
Constructs an entity reference for a specific entity in the given registry.
Definition entity_ref.hpp:21
constexpr auto get() const -> decltype(auto)
Retrieves multiple components from the underlying registry for the current entity (const version).
Definition entity_ref.hpp:134
constexpr auto has() const noexcept -> bool
Determines if the entity has all specified components.
Definition entity_ref.hpp:55
auto move(base_registry &destination, placeholder_entity placeholder)
Moves the current entity to another registry into a placeholder entity.
Definition entity_ref.hpp:321
auto clone(placeholder_entity placeholder)
Clones the current entity to a placeholder entity.
Definition entity_ref.hpp:275
constexpr auto visit(auto &&func)
Visit all components of an entity and apply a function to them.
Definition entity_ref.hpp:229
auto copy(base_registry &destination, placeholder_entity placeholder) const
Copies the current entity to another registry into a placeholder entity.
Definition entity_ref.hpp:298
auto copy(base_registry &destination) const -> entity_ref
Copies the current entity to another registry.
Definition entity_ref.hpp:286
void destroy()
Destroys the current entity.
Definition entity_ref.hpp:215
constexpr auto alive() const noexcept -> bool
Checks if the entity is currently active in the registry.
Definition entity_ref.hpp:41
auto move(base_registry &destination) -> entity_ref
Moves the current entity to another registry.
Definition entity_ref.hpp:309
constexpr auto get_or_insert(auto &&... args) -> C &
Get reference to component C, or insert it with provided arguments if not present.
Definition entity_ref.hpp:150
constexpr auto get() -> decltype(auto)
Retrieves multiple components from the underlying registry for the current entity.
Definition entity_ref.hpp:119
constexpr auto get() -> C &
Retrieves a component of type C from the entity.
Definition entity_ref.hpp:77
constexpr auto set(Args &&... args) -> entity_ref
Set component to an entity. It can either override a component value that is already assigned to an e...
Definition entity_ref.hpp:177
constexpr auto visit(auto &&func) const
Visit all components of an entity and apply a function to them (const version).
Definition entity_ref.hpp:244
Placeholder (reserved) entity.
Definition base_registry.hpp:13
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
Component concept. The component must be a struct/class that can be move constructed and move assigna...
Definition component.hpp:86
Definition archetype.hpp:11
detail::handle< struct entity_tag_t > entity
Represents an entity, consisting of an ID and generation.
Definition entity.hpp:13