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 , instanced_(false)
25 {
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 template <typename Func>
34 T run(Func&& func) {
35 using Delegate = typename _::run_delegate<
36 typename std::decay<Func>::type>;
37
38 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
39 desc_.run = Delegate::run;
40 desc_.run_ctx = ctx;
41 desc_.run_ctx_free = reinterpret_cast<
42 ecs_ctx_free_t>(_::free_obj<Delegate>);
43 return T(world_, &desc_, false);
44 }
45
46 template <typename Func, typename EachFunc>
47 T run(Func&& func, EachFunc&& each_func) {
48 using Delegate = typename _::run_delegate<
49 typename std::decay<Func>::type>;
50
51 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
52 desc_.run = Delegate::run;
53 desc_.run_ctx = ctx;
54 desc_.run_ctx_free = reinterpret_cast<
55 ecs_ctx_free_t>(_::free_obj<Delegate>);
56 return each(FLECS_FWD(each_func));
57 }
58
59 template <typename Func>
60 T each(Func&& func) {
61 using Delegate = typename _::each_delegate<
62 typename std::decay<Func>::type, Components...>;
63 instanced_ = true;
64
65 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
66 desc_.callback = Delegate::run;
67 desc_.callback_ctx = ctx;
68 desc_.callback_ctx_free = reinterpret_cast<
69 ecs_ctx_free_t>(_::free_obj<Delegate>);
70 return T(world_, &desc_, true);
71 }
72
73protected:
74 flecs::world_t* world_v() override { return world_; }
75 TDesc desc_;
76 flecs::world_t *world_;
77 bool instanced_;
78};
79
80#undef FLECS_IBUILDER
81
82} // namespace _
83} // 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:584
Used with ecs_entity_init().
Definition flecs.h:901
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:913
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:917
const char * name
Name of the entity.
Definition flecs.h:908
The world.
Definition world.hpp:137