Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "builder_i.hpp"
9
10namespace flecs {
11
16struct term final : term_builder_i<term> {
17 term()
18 : term_builder_i<term>(&value)
19 , value({})
20 , m_world(nullptr) { value.move = true; }
21
22 term(flecs::world_t *world_ptr)
23 : term_builder_i<term>(&value)
24 , value({})
25 , m_world(world_ptr) { value.move = true; }
26
27 term(flecs::world_t *world_ptr, ecs_term_t t)
28 : term_builder_i<term>(&value)
29 , value({})
30 , m_world(world_ptr) {
31 value = t;
32 value.move = false;
33 this->set_term(&value);
34 }
35
36 term(flecs::world_t *world_ptr, id_t id)
37 : term_builder_i<term>(&value)
38 , value({})
39 , m_world(world_ptr) {
40 if (id & ECS_ID_FLAGS_MASK) {
41 value.id = id;
42 } else {
43 value.first.id = id;
44 }
45 value.move = false;
46 this->set_term(&value);
47 }
48
49 term(flecs::world_t *world_ptr, entity_t r, entity_t o)
50 : term_builder_i<term>(&value)
51 , value({})
52 , m_world(world_ptr) {
53 value.id = ecs_pair(r, o);
54 value.move = false;
55 this->set_term(&value);
56 }
57
58 term(id_t id)
59 : term_builder_i<term>(&value)
60 , value({})
61 , m_world(nullptr) {
62 if (id & ECS_ID_FLAGS_MASK) {
63 value.id = id;
64 } else {
65 value.first.id = id;
66 }
67 value.move = true;
68 }
69
70 term(id_t r, id_t o)
71 : term_builder_i<term>(&value)
72 , value({})
73 , m_world(nullptr) {
74 value.id = ecs_pair(r, o);
75 value.move = true;
76 }
77
78 term(const term& t) : term_builder_i<term>(&value) {
79 m_world = t.m_world;
80 value = ecs_term_copy(&t.value);
81 this->set_term(&value);
82 }
83
84 term(term&& t) noexcept : term_builder_i<term>(&value) {
85 m_world = t.m_world;
86 value = ecs_term_move(&t.value);
87 t.reset();
88 this->set_term(&value);
89 }
90
91 term& operator=(const term& t) {
92 ecs_assert(m_world == t.m_world, ECS_INVALID_PARAMETER, NULL);
93 ecs_term_fini(&value);
94 value = ecs_term_copy(&t.value);
95 this->set_term(&value);
96 return *this;
97 }
98
99 term& operator=(term&& t) noexcept {
100 ecs_assert(m_world == t.m_world, ECS_INVALID_PARAMETER, NULL);
101 ecs_term_fini(&value);
102 value = t.value;
103 this->set_term(&value);
104 t.reset();
105 return *this;
106 }
107
108 ~term() {
109 ecs_term_fini(&value);
110 }
111
112 void reset() {
113 value = {};
114 this->set_term(nullptr);
115 }
116
117 int finalize() {
118 return ecs_term_finalize(m_world, &value);
119 }
120
121 bool is_set() {
122 return ecs_term_is_initialized(&value);
123 }
124
125 flecs::id id() {
126 return flecs::id(m_world, value.id);
127 }
128
129 flecs::inout_kind_t inout() {
130 return static_cast<flecs::inout_kind_t>(value.inout);
131 }
132
133 flecs::oper_kind_t oper() {
134 return static_cast<flecs::oper_kind_t>(value.oper);
135 }
136
137 flecs::entity get_src() {
138 return flecs::entity(m_world, value.src.id);
139 }
140
141 flecs::entity get_first() {
142 return flecs::entity(m_world, value.first.id);
143 }
144
145 flecs::entity get_second() {
146 return flecs::entity(m_world, value.second.id);
147 }
148
149 ecs_term_t move() { /* explicit move to ecs_term_t */
150 return ecs_term_move(&value);
151 }
152
153 ecs_term_t value;
154
155protected:
156 flecs::world_t* world_v() override { return m_world; }
157
158private:
159 flecs::world_t *m_world;
160};
161
162// Term mixin implementation
163template <typename... Args>
164inline flecs::term world::term(Args &&... args) const {
165 return flecs::term(m_world, FLECS_FWD(args)...);
166}
167
168template <typename T>
169inline flecs::term world::term() const {
170 return flecs::term(m_world, _::cpp_type<T>::id(m_world));
171}
172
173template <typename First, typename Second>
174inline flecs::term world::term() const {
175 return flecs::term(m_world, ecs_pair(
176 _::cpp_type<First>::id(m_world),
177 _::cpp_type<Second>::id(m_world)));
178}
179
180}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
flecs::term term() const
Create a term for a (component) type.
ecs_term_t ecs_term_copy(const ecs_term_t *src)
Copy resources of a term to another term.
void ecs_term_fini(ecs_term_t *term)
Free resources of term.
int ecs_term_finalize(const ecs_world_t *world, ecs_term_t *term)
Finalize term.
bool ecs_term_is_initialized(const ecs_term_t *term)
Test whether a term is set.
ecs_term_t ecs_term_move(ecs_term_t *src)
Move resources of a term to another term.
ecs_entity_t id
Entity id.
Definition flecs.h:738
Type that describes a term (single element in a query)
Definition flecs.h:758
ecs_term_id_t second
Second element of pair.
Definition flecs.h:766
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:759
ecs_term_id_t src
Source of term.
Definition flecs.h:764
ecs_term_id_t first
Component or first element of pair.
Definition flecs.h:765
bool move
Used by internals.
Definition flecs.h:779
ecs_inout_kind_t inout
Access to contents matched by term.
Definition flecs.h:768
ecs_oper_kind_t oper
Operator of term.
Definition flecs.h:769
Entity.
Definition entity.hpp:30
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Term builder interface.
Class that describes a term.
Definition impl.hpp:16
Term builder interface.