Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
node_builder.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9namespace _ {
10
11// Macros for template types so we don't go cross-eyed.
12#define FLECS_IBUILDER template<typename IBase, typename ... Components> class
13
14template<typename T, typename TDesc, typename Base, FLECS_IBUILDER IBuilder, typename ... Components>
15struct node_builder : IBuilder<Base, Components ...>
16{
17 using IBase = IBuilder<Base, Components ...>;
18
19public:
20 explicit node_builder(flecs::world_t* world, const char *name = nullptr)
21 : IBase(&desc_)
22 , desc_{}
23 , world_(world)
24 {
25 if (name != nullptr) {
26 ecs_entity_desc_t entity_desc = {};
27 entity_desc.name = name;
28 entity_desc.sep = "::";
29 entity_desc.root_sep = "::";
30 desc_.entity = ecs_entity_init(world_, &entity_desc);
31 }
32 }
33
35 : IBase(&desc_, f.term_index_)
36 {
37 world_ = f.world_;
38 desc_ = f.desc_;
39 }
40
41 node_builder(node_builder&& f) noexcept
42 : node_builder<T, TDesc, Base, IBuilder, Components...>(f) { }
43
44 template <typename Func>
45 T run(Func&& func) {
46 using Delegate = typename _::run_delegate<
47 typename std::decay<Func>::type>;
48
49 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
50 desc_.run = Delegate::run;
51 desc_.run_ctx = ctx;
52 desc_.run_ctx_free = _::free_obj<Delegate>;
53 return T(world_, &desc_);
54 }
55
56 template <typename Func, typename EachFunc>
57 T run(Func&& func, EachFunc&& each_func) {
58 using Delegate = typename _::run_delegate<
59 typename std::decay<Func>::type>;
60
61 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
62 desc_.run = Delegate::run;
63 desc_.run_ctx = ctx;
64 desc_.run_ctx_free = _::free_obj<Delegate>;
65 return each(FLECS_FWD(each_func));
66 }
67
68 template <typename Func>
69 T each(Func&& func) {
70 using Delegate = typename _::each_delegate<
71 typename std::decay<Func>::type, Components...>;
72 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
73 desc_.callback = Delegate::run;
74 desc_.callback_ctx = ctx;
75 desc_.callback_ctx_free = _::free_obj<Delegate>;
76 return T(world_, &desc_);
77 }
78
79 template <typename Func>
80 T run_each(Func&& func) {
81 using Delegate = typename _::each_delegate<
82 typename std::decay<Func>::type, Components...>;
83 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
84 desc_.run = Delegate::run_each;
85 desc_.run_ctx = ctx;
86 desc_.run_ctx_free = _::free_obj<Delegate>;
87 return T(world_, &desc_);
88 }
89
90protected:
91 flecs::world_t* world_v() override { return world_; }
92 TDesc desc_;
93 flecs::world_t *world_;
94};
95
96#undef FLECS_IBUILDER
97
98} // namespace _
99} // namespace flecs
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
Used with ecs_entity_init().
Definition flecs.h:1061
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1073
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1077
const char * name
Name of the entity.
Definition flecs.h:1068
The world.
Definition world.hpp:244