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