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

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

Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
17 struct system_runner_fluent {
19 system_runner_fluent(
20 world_t *world,
21 entity_t id,
22 int32_t stage_current,
23 int32_t stage_count,
24 ecs_ftime_t delta_time,
25 void *param)
26 : stage_(world)
27 , id_(id)
28 , delta_time_(delta_time)
29 , param_(param)
30 , stage_current_(stage_current)
31 , stage_count_(stage_count) { }
32
34 system_runner_fluent& offset(int32_t offset) {
35 offset_ = offset;
36 return *this;
37 }
38
40 system_runner_fluent& limit(int32_t limit) {
41 limit_ = limit;
42 return *this;
43 }
44
46 system_runner_fluent& stage(flecs::world& stage) {
47 stage_ = stage.c_ptr();
48 return *this;
49 }
50
52 ~system_runner_fluent() {
53 if (stage_count_) {
54 ecs_run_worker(
55 stage_, id_, stage_current_, stage_count_, delta_time_,
56 param_);
57 } else {
58 ecs_run(stage_, id_, delta_time_, param_);
59 }
60 }
61
62private:
63 world_t *stage_;
64 entity_t id_;
65 ecs_ftime_t delta_time_;
66 void *param_;
67 int32_t offset_;
68 int32_t limit_;
69 int32_t stage_current_;
70 int32_t stage_count_;
71};
72
77 struct system final : entity
78{
79 using entity::entity;
80
82 explicit system() {
83 id_ = 0;
84 world_ = nullptr;
85 }
86
88 explicit system(flecs::world_t *world, ecs_system_desc_t *desc) {
89 world_ = world;
90 id_ = ecs_system_init(world, desc);
91 }
92
94 void ctx(void *ctx) {
95 ecs_system_desc_t desc = {};
96 desc.entity = id_;
97 desc.ctx = ctx;
98 ecs_system_init(world_, &desc);
99 }
100
102 void* ctx() const {
103 return ecs_system_get(world_, id_)->ctx;
104 }
105
107 flecs::query<> query() const {
108 return flecs::query<>(ecs_system_get(world_, id_)->query);
109 }
110
112 system& set_group(uint64_t group_id) {
113 ecs_system_set_group(world_, id_, group_id);
114 return *this;
115 }
116
118 template <typename Group>
119 system& set_group() {
120 ecs_system_set_group(world_, id_, _::type<Group>().id(world_));
121 return *this;
122 }
123
125 system_runner_fluent run(ecs_ftime_t delta_time = 0.0f, void *param = nullptr) const {
126 return system_runner_fluent(world_, id_, 0, 0, delta_time, param);
127 }
128
130 system_runner_fluent run_worker(
131 int32_t stage_current,
132 int32_t stage_count,
133 ecs_ftime_t delta_time = 0.0f,
134 void *param = nullptr) const
135 {
136 return system_runner_fluent(
137 world_, id_, stage_current, stage_count, delta_time, param);
138 }
139
140# ifdef FLECS_TIMER
141# include "../timer/system_mixin.inl"
142# endif
143
144};
145
147 inline system world::system(flecs::entity e) const {
148 return flecs::system(world_, e);
149}
150
151template <typename... Comps, typename... Args>
152inline system_builder<Comps...> world::system(Args &&... args) const {
153 return flecs::system_builder<Comps...>(world_, FLECS_FWD(args)...);
154}
155
156namespace _ {
157
158template <typename ArgList>
159 struct system_each_normalize_args;
160
161template <typename ... Args>
162 struct system_each_normalize_args<arg_list<Args...>> {
163 using type = arg_list<remove_reference_t<Args>...>;
164};
165
166template <typename ArgList, typename = int>
167 struct system_each_callback_args {
168 using type = typename system_each_normalize_args<ArgList>::type;
169};
170
171template <typename First, typename ... Args>
172 struct system_each_callback_args<arg_list<First, Args...>,
173 if_t<is_same<decay_t<First>, flecs::entity>::value>> {
174 using type = typename system_each_normalize_args<arg_list<Args...>>::type;
175};
176
177template <typename First, typename Second, typename ... Args>
178 struct system_each_callback_args<arg_list<First, Second, Args...>,
179 if_t<is_same<decay_t<First>, flecs::iter>::value>> {
180 using type = typename system_each_normalize_args<arg_list<Args...>>::type;
181};
182
183inline void system_init(flecs::world& world) {
184 world.component<TickSource>("flecs::system::TickSource");
185}
186
187} // namespace _
188
189template <typename ... Components>
190template <typename Func>
191inline system system_builder<Components...>::each(Func&& func) {
192 if constexpr (sizeof...(Components) == 0) {
193 using CallbackComponents =
194 typename _::system_each_callback_args<arg_list_t<Func>>::type;
195 return this->each_callback(CallbackComponents{}, FLECS_FWD(func));
196 } else {
197 // Faster version of each() that iterates the query on the C++ side.
198 return this->run_each(FLECS_FWD(func));
199 }
200}
201
202template <typename ... Components>
203template <typename ... CallbackComponents, typename Func>
204inline system system_builder<Components...>::each_callback(
205 _::arg_list<CallbackComponents...>,
206 Func&& func)
207{
208 this->template prepend_each_callback_signature<CallbackComponents...>();
209
210 using Delegate = typename _::each_delegate<
211 typename std::decay<Func>::type, CallbackComponents...>;
212
213 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
214 this->desc_.run = Delegate::run_each;
215 this->desc_.run_ctx = ctx;
216 this->desc_.run_ctx_free = _::free_obj<Delegate>;
217 return system(this->world_, &this->desc_);
218}
219
220template <typename ... Components>
221template <typename ... CallbackComponents>
222inline void system_builder<Components...>::prepend_each_callback_signature() {
223 if constexpr (sizeof...(Components) == 0 && sizeof...(CallbackComponents) != 0) {
224 constexpr int32_t callback_term_count =
225 static_cast<int32_t>(sizeof...(CallbackComponents));
226
227 ecs_assert(this->term_index_ + callback_term_count <= FLECS_TERM_COUNT_MAX,
228 ECS_INVALID_PARAMETER, "maximum number of terms exceeded");
229
230 const int32_t existing_term_count = this->term_index_;
231 this->set_term(nullptr);
232
233 for (int32_t i = existing_term_count - 1; i >= 0; i --) {
234 this->desc_.query.terms[i + callback_term_count] =
235 this->desc_.query.terms[i];
236 }
237
238 for (int32_t i = 0; i < callback_term_count; i ++) {
239 this->desc_.query.terms[i] = ecs_term_t{};
240 }
241
242 this->term_index_ = 0;
243 _::sig<CallbackComponents...>(this->world_).populate(this);
244 this->term_index_ = existing_term_count + callback_term_count;
245 }
246}
247
248} // namespace flecs
flecs::is_same
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
ecs_system_get
FLECS_API const ecs_system_t * ecs_system_get(const ecs_world_t *world, ecs_entity_t system)
Get a system object.
ecs_run
FLECS_API ecs_entity_t ecs_run(ecs_world_t *world, ecs_entity_t system, ecs_ftime_t delta_time, void *param)
Run a specific system manually.
ecs_system_init
FLECS_API ecs_entity_t ecs_system_init(ecs_world_t *world, const ecs_system_desc_t *desc)
Create a system.
ecs_system_set_group
FLECS_API void ecs_system_set_group(ecs_world_t *world, ecs_entity_t system, uint64_t group_id)
Set query group for system.
ecs_run_worker
FLECS_API ecs_entity_t ecs_run_worker(ecs_world_t *world, ecs_entity_t system, int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time, void *param)
Same as ecs_run(), but subdivides entities across a number of provided stages.
ecs_term_t
struct ecs_term_t ecs_term_t
A term is a single element in a query.
Definition flecs.h:434
flecs::world::system
flecs::system system(flecs::entity e) const
Upcast an entity to a system.
Definition impl.hpp:147
flecs::TickSource
EcsTickSource TickSource
Tick source component.
Definition decl.hpp:19
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
FLECS_TERM_COUNT_MAX
#define FLECS_TERM_COUNT_MAX
Maximum number of terms in queries.
Definition flecs.h:326
builder.hpp
System builder.
ecs_system_desc_t
Use with ecs_system_init() to create or update a system.
Definition system.h:38
ecs_system_desc_t::ctx
void * ctx
Context to be passed to callback (as ecs_iter_t::param).
Definition system.h:70
ecs_system_desc_t::entity
ecs_entity_t entity
Existing entity to associate with the system (optional).
Definition system.h:42
ecs_system_t::ctx
void * ctx
Userdata for the system.
Definition system.h:147
flecs::_::each_delegate
Definition delegate.hpp:220
flecs::_::sig
Definition signature.hpp:38
flecs::_::system_each_callback_args
Definition impl.hpp:167
flecs::_::system_each_normalize_args
Definition impl.hpp:159
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::entity
entity()
Default constructor.
Definition entity.hpp:32
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id::id_
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::id::world_
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
flecs::iter
Class for iterating over query results.
Definition iter.hpp:68
flecs::query
Typed query.
Definition impl.hpp:220
flecs::system_builder
System builder.
Definition builder.hpp:25
flecs::system_runner_fluent
Fluent interface for running a system.
Definition impl.hpp:17
flecs::system_runner_fluent::~system_runner_fluent
~system_runner_fluent()
Destructor.
Definition impl.hpp:52
flecs::system_runner_fluent::stage
system_runner_fluent & stage(flecs::world &stage)
Set the stage for the system runner.
Definition impl.hpp:46
flecs::system_runner_fluent::limit
system_runner_fluent & limit(int32_t limit)
Set the limit for the system runner.
Definition impl.hpp:40
flecs::system_runner_fluent::system_runner_fluent
system_runner_fluent(world_t *world, entity_t id, int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time, void *param)
Construct a system runner.
Definition impl.hpp:19
flecs::system_runner_fluent::offset
system_runner_fluent & offset(int32_t offset)
Set the offset for the system runner.
Definition impl.hpp:34
flecs::system
System.
Definition impl.hpp:78
flecs::system::system
system(flecs::world_t *world, ecs_system_desc_t *desc)
Construct from a world and a system descriptor.
Definition impl.hpp:88
flecs::system::set_group
system & set_group(uint64_t group_id)
Set the query group.
Definition impl.hpp:112
flecs::system::ctx
void ctx(void *ctx)
Set the system context.
Definition impl.hpp:94
flecs::system::query
flecs::query query() const
Get the query for this system.
Definition impl.hpp:107
flecs::system::run
system_runner_fluent run(ecs_ftime_t delta_time=0.0f, void *param=nullptr) const
Run the system.
Definition impl.hpp:125
flecs::system::ctx
void * ctx() const
Get the system context.
Definition impl.hpp:102
flecs::system::set_group
system & set_group()
Set the query group.
Definition impl.hpp:119
flecs::system::entity
entity()
Default constructor.
Definition entity.hpp:32
flecs::system::system
system()
Default constructor.
Definition impl.hpp:82
flecs::system::run_worker
system_runner_fluent run_worker(int32_t stage_current, int32_t stage_count, ecs_ftime_t delta_time=0.0f, void *param=nullptr) const
Run the system on a specific worker stage.
Definition impl.hpp:130
flecs::world
The world.
Definition world.hpp:246
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1545
system_mixin.inl
Timer module system mixin.
flecs::if_t
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.
Definition utils.hpp:168