Flecs v3.2
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(&m_desc)
22 , m_desc{}
23 , m_world(world)
24 , m_instanced(false)
25 {
26 ecs_entity_desc_t entity_desc = {};
27 entity_desc.name = name;
28 entity_desc.sep = "::";
29 m_desc.entity = ecs_entity_init(m_world, &entity_desc);
30 }
31
32 /* Iter (or each) is mandatory and always the last thing that
33 * is added in the fluent method chain. Create system signature from both
34 * template parameters and anything provided by the signature method. */
35 template <typename Func>
36 T iter(Func&& func) {
37 using Invoker = typename _::iter_invoker<
38 typename std::decay<Func>::type, Components...>;
39 return build<Invoker>(FLECS_FWD(func));
40 }
41
42 /* Each is similar to action, but accepts a function that operates on a
43 * single entity */
44 template <typename Func>
45 T each(Func&& func) {
46 using Invoker = typename _::each_invoker<
47 typename std::decay<Func>::type, Components...>;
48 m_instanced = true;
49 return build<Invoker>(FLECS_FWD(func));
50 }
51
52protected:
53 flecs::world_t* world_v() override { return m_world; }
54 TDesc m_desc;
55 flecs::world_t *m_world;
56 bool m_instanced;
57
58private:
59 template <typename Invoker, typename Func>
60 T build(Func&& func) {
61 auto ctx = FLECS_NEW(Invoker)(FLECS_FWD(func));
62 m_desc.callback = Invoker::run;
63 m_desc.binding_ctx = ctx;
64 m_desc.binding_ctx_free = reinterpret_cast<
65 ecs_ctx_free_t>(_::free_obj<Invoker>);
66
67 return T(m_world, &m_desc, m_instanced);
68 }
69};
70
71#undef FLECS_IBUILDER
72
73} // namespace _
74} // 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:478
Used with ecs_entity_init.
Definition flecs.h:760
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:770
const char * name
Name of the entity.
Definition flecs.h:765
Class for iterating over query results.
Definition iter.hpp:169
The world.
Definition world.hpp:113