Flecs v4.0
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 ecs_entity_desc_t entity_desc = {};
26 entity_desc.name = name;
27 entity_desc.sep = "::";
28 entity_desc.root_sep = "::";
29 desc_.entity = ecs_entity_init(world_, &entity_desc);
30 }
31
32 template <typename Func>
33 T run(Func&& func) {
34 using Delegate = typename _::run_delegate<
35 typename std::decay<Func>::type>;
36
37 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
38 desc_.run = Delegate::run;
39 desc_.run_ctx = ctx;
40 desc_.run_ctx_free = _::free_obj<Delegate>;
41 return T(world_, &desc_);
42 }
43
44 template <typename Func, typename EachFunc>
45 T run(Func&& func, EachFunc&& each_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 each(FLECS_FWD(each_func));
54 }
55
56 template <typename Func>
57 T each(Func&& func) {
58 using Delegate = typename _::each_delegate<
59 typename std::decay<Func>::type, Components...>;
60 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
61 desc_.callback = Delegate::run;
62 desc_.callback_ctx = ctx;
63 desc_.callback_ctx_free = _::free_obj<Delegate>;
64 return T(world_, &desc_);
65 }
66
67protected:
68 flecs::world_t* world_v() override { return world_; }
69 TDesc desc_;
70 flecs::world_t *world_;
71};
72
73#undef FLECS_IBUILDER
74
75} // namespace _
76} // namespace flecs
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:938
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:950
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:954
const char * name
Name of the entity.
Definition flecs.h:945
The world.
Definition world.hpp:137