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 = reinterpret_cast<
41 ecs_ctx_free_t>(_::free_obj<Delegate>);
42 return T(world_, &desc_);
43 }
44
45 template <typename Func, typename EachFunc>
46 T run(Func&& func, EachFunc&& each_func) {
47 using Delegate = typename _::run_delegate<
48 typename std::decay<Func>::type>;
49
50 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
51 desc_.run = Delegate::run;
52 desc_.run_ctx = ctx;
53 desc_.run_ctx_free = reinterpret_cast<
54 ecs_ctx_free_t>(_::free_obj<Delegate>);
55 return each(FLECS_FWD(each_func));
56 }
57
58 template <typename Func>
59 T each(Func&& func) {
60 using Delegate = typename _::each_delegate<
61 typename std::decay<Func>::type, Components...>;
62 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
63 desc_.callback = Delegate::run;
64 desc_.callback_ctx = ctx;
65 desc_.callback_ctx_free = reinterpret_cast<
66 ecs_ctx_free_t>(_::free_obj<Delegate>);
67 return T(world_, &desc_);
68 }
69
70protected:
71 flecs::world_t* world_v() override { return world_; }
72 TDesc desc_;
73 flecs::world_t *world_;
74};
75
76#undef FLECS_IBUILDER
77
78} // namespace _
79} // namespace flecs
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:616
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