Skip to content
Flecs v4.1
builder.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/utils/builder.hpp
3 * @brief Builder base class.
4 *
5 * Generic functionality for builder classes.
6 */
7
8#pragma once
9
10namespace flecs {
11namespace _ {
12
13template <typename T, typename TDesc, typename Base,
14 template <typename, typename...> class IBuilder, typename... Components>
15struct builder : IBuilder<Base, Components...> {
16 using IBase = IBuilder<Base, Components...>;
17
18 explicit builder(world_t *world, const char *name = nullptr)
19 : IBase(&desc_), desc_{}, world_(world)
20 {
21 set_name(name);
22 }
23
24 builder(const builder& other)
25 : IBase(&desc_, other.term_index_), desc_(other.desc_), world_(other.world_) { }
26
27 builder(builder&& other) noexcept : builder(other) { }
28
29 operator TDesc*() {
30 return &desc_;
31 }
32
33 operator const TDesc*() const {
34 return &desc_;
35 }
36
37 T build() const {
38 return T(world_, &desc_);
39 }
40
41 template <typename Func, typename... Each>
42 T run(Func&& func, Each&&... each_func) {
43 using Delegate = run_delegate<decay_t<Func>>;
44 set_callback<Delegate, Delegate::run, true>(desc_, FLECS_FWD(func));
45 if constexpr (sizeof...(Each)) {
46 return each(FLECS_FWD(each_func)...);
47 } else {
48 return build();
49 }
50 }
51
52 template <typename Func>
53 T each(Func&& func) {
54 set_each_callback<false>(desc_, FLECS_FWD(func), arg_list<Components...>{});
55 return build();
56 }
57
58 template <typename Func>
59 T run_each(Func&& func) {
60 set_each_callback<true>(desc_, FLECS_FWD(func), arg_list<Components...>{});
61 return build();
62 }
63
64protected:
65 world_t* world_v() override {
66 return world_;
67 }
68
69 void set_name(const char *name) {
70 if (name) {
71 ecs_entity_desc_t desc = {};
72 desc.name = name;
73 desc.sep = "::";
74 desc.root_sep = "::";
75 desc_.entity = ecs_entity_init(world_, &desc);
76 }
77 }
78
79 TDesc desc_;
80 world_t *world_;
81
82};
83
84} // namespace _
85} // namespace flecs
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
Int to enum.
Definition component.hpp:18
Used with ecs_entity_init().
Definition flecs.h:1077
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1089
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1093
const char * name
Name of the entity.
Definition flecs.h:1084
The world.
Definition world.hpp:129