Flecs: include/flecs/addons/cpp/utils/node_builder.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
node_builder.hpp
Go to the documentation of this file.
1
5
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>
15 struct 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
67 template <typename Func>
68 T run_each(Func&& func) {
69 using Delegate = typename _::each_delegate<
70 typename std::decay<Func>::type, Components...>;
71 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
72 desc_.run = Delegate::run_each;
73 desc_.run_ctx = ctx;
74 desc_.run_ctx_free = _::free_obj<Delegate>;
75 return T(world_, &desc_);
76 }
77
78protected:
79 flecs::world_t* world_v() override { return world_; }
80 TDesc desc_;
81 flecs::world_t *world_;
82};
83
84#undef FLECS_IBUILDER
85
86} // namespace _
87} // namespace flecs
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_entity_init
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
flecs::_
Int to enum.
Definition component.hpp:18
ecs_entity_desc_t
Used with ecs_entity_init().
Definition flecs.h:1046
ecs_entity_desc_t::sep
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1058
ecs_entity_desc_t::root_sep
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1062
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:1053
flecs::_::each_delegate
Definition delegate.hpp:220
flecs::_::run_delegate
Definition delegate.hpp:544
flecs::world
The world.
Definition world.hpp:246