Skip to content
Flecs v4.1
decl.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/id/decl.hpp
3 * @brief ID class.
4 */
5
6#pragma once
7
8namespace flecs {
9
10struct id;
11struct entity;
12
13/**
14 * @defgroup cpp_ids IDs
15 * @ingroup cpp_core
16 * Class for working with entity, component, tag, and pair IDs.
17 *
18 * @{
19 */
20
21/** Class that wraps around a flecs::id_t.
22 * A flecs ID is an identifier that can be added to entities. IDs can be:
23 * - entities (including components, tags)
24 * - pair IDs
25 * - entities with ID flags set (like flecs::AUTO_OVERRIDE, flecs::TOGGLE)
26 */
27struct id {
28 id()
29 : world_(nullptr)
30 , id_(0) { }
31
32 explicit id(flecs::id_t value)
33 : world_(nullptr)
34 , id_(value) { }
35
36 explicit id(flecs::world_t *world, flecs::id_t value = 0)
37 : world_(world)
38 , id_(value) { }
39
41 : world_(world)
42 , id_(ecs_pair(first, second)) { }
43
44 explicit id(flecs::world_t *world, const char *expr)
45 : world_(world)
46 , id_(ecs_id_from_str(world, expr)) { }
47
49 : world_(nullptr)
50 , id_(ecs_pair(first, second)) { }
51
52 explicit id(const flecs::id& first, const flecs::id& second)
53 : world_(first.world_)
54 , id_(ecs_pair(first.id_, second.id_)) { }
55
56 /** Test if ID is a pair (has first, second). */
57 bool is_pair() const {
58 return ecs_id_is_pair(id_);
59 }
60
61 /** Test if ID is a value pair (has first, value). */
62 bool is_value_pair() const {
63 return (id_ & ECS_ID_FLAGS_MASK) == flecs::VALUE_PAIR;
64 }
65
66 /** Test if ID is a wildcard. */
67 bool is_wildcard() const {
68 return ecs_id_is_wildcard(id_);
69 }
70
71 /** Test if ID is an entity. */
72 bool is_entity() const {
73 return !(id_ & ECS_ID_FLAGS_MASK);
74 }
75
76 /** Return ID as entity (only allowed when ID is a valid entity). */
77 flecs::entity entity() const;
78
79 /** Return ID with flags added. */
81
82 /** Return ID with flags removed. */
84
85 /** Return ID without flags. */
87
88 /** Return ID without generation. */
90
91 /** Return component type of ID. */
92 flecs::entity type_id() const;
93
94 /** Test if ID has specified flags. */
96 return ((id_ & flags) == flags);
97 }
98
99 /** Test if ID has any flags. */
100 bool has_flags() const {
101 return (id_ & ECS_ID_FLAGS_MASK) != 0;
102 }
103
104 /** Return ID flags set on ID. */
105 flecs::entity flags() const;
106
107 /** Test if ID has the specified first element. */
109 if (!is_pair()) {
110 return false;
111 }
112 return ECS_PAIR_FIRST(id_) == first;
113 }
114
115 /** Get first element from a pair.
116 * If the ID is not a pair, this operation will fail. When the ID has a
117 * world, the operation will ensure that the returned ID has the correct
118 * generation count. */
119 flecs::entity first() const;
120
121 /** Get second element from a pair.
122 * If the ID is not a pair, this operation will fail. When the ID has a
123 * world, the operation will ensure that the returned ID has the correct
124 * generation count. */
125 flecs::entity second() const;
126
127 /** Convert ID to string. */
130 }
131
132 /** Convert flags of ID to string. */
134 return flecs::string_view( ecs_id_flag_str(id_ & ECS_ID_FLAGS_MASK));
135 }
136
137 /** Return flecs::id_t value. */
139 return id_;
140 }
141
142 operator flecs::id_t() const {
143 return id_;
144 }
145
146 /** Get the world. */
147 flecs::world world() const;
148
149protected:
150 /** World is optional, but guarantees that entity identifiers extracted from
151 * the ID are valid. */
153 /** The raw ID value. */
155};
156
157/** @} */
158
159}
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_world_t world_t
World type.
Definition c_types.hpp:18
char * ecs_id_str(const ecs_world_t *world, ecs_id_t component)
Convert a component ID to a string.
const char * ecs_id_flag_str(uint64_t component_flags)
Convert a component flag to a string.
bool ecs_id_is_pair(ecs_id_t component)
Utility to check if a component is a pair.
ecs_id_t ecs_id_from_str(const ecs_world_t *world, const char *expr)
Convert a string to a component.
bool ecs_id_is_wildcard(ecs_id_t component)
Utility to check if a component is a wildcard.
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::string str() const
Convert ID to string.
Definition decl.hpp:128
bool is_pair() const
Test if ID is a pair (has first, second).
Definition decl.hpp:57
flecs::world world() const
Get the world.
Definition impl.hpp:60
flecs::entity flags() const
Return ID flags set on ID.
Definition impl.hpp:16
flecs::entity remove_flags() const
Return ID without flags.
Definition impl.hpp:52
flecs::entity type_id() const
Return component type of ID.
Definition impl.hpp:64
flecs::entity add_flags(flecs::id_t flags) const
Return ID with flags added.
Definition impl.hpp:42
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
bool has_relation(flecs::id_t first) const
Test if ID has the specified first element.
Definition decl.hpp:108
bool has_flags(flecs::id_t flags) const
Test if ID has specified flags.
Definition decl.hpp:95
flecs::id_t raw_id() const
Return flecs::id_t value.
Definition decl.hpp:138
bool is_entity() const
Test if ID is an entity.
Definition decl.hpp:72
bool is_value_pair() const
Test if ID is a value pair (has first, value).
Definition decl.hpp:62
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
bool has_flags() const
Test if ID has any flags.
Definition decl.hpp:100
flecs::string flags_str() const
Convert flags of ID to string.
Definition decl.hpp:133
flecs::entity remove_generation() const
Return ID without generation.
Definition impl.hpp:56
bool is_wildcard() const
Test if ID is a wildcard.
Definition decl.hpp:67
Non-owning string view.
Definition string.hpp:169
Owned string wrapper.
Definition string.hpp:15
The world.
Definition world.hpp:129
world()
Create a world.
Definition world.hpp:132