Skip to content
Flecs v4.1
builder.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/script/builder.hpp
3 * @brief Script builder.
4 */
5
6#pragma once
7
8namespace flecs {
9
10struct function_builder {
11 function_builder(flecs::world_t *world, const char *name,
12 flecs::entity_t parent = 0, bool is_method = false)
13 : world_(world)
14 , desc_{}
15 , param_count_(0)
16 , is_method_(is_method)
17 {
18 desc_.name = name;
19 desc_.parent = parent ? parent : ecs_get_scope(world);
20 }
21
22 function_builder& parent(flecs::entity_t value) {
23 desc_.parent = value;
24 return *this;
25 }
26
27 function_builder& return_type(flecs::entity_t type) {
28 desc_.return_type = type;
29 return *this;
30 }
31
32 template <typename T>
33 function_builder& return_type() {
34 return return_type(_::type<T>::id(world_));
35 }
36
37 function_builder& param(const char *name, flecs::entity_t type) {
38 ecs_assert(param_count_ < FLECS_SCRIPT_FUNCTION_ARGS_MAX - is_method_,
39 ECS_INVALID_PARAMETER, "too many function parameters");
40 desc_.params[param_count_++] = {name, type};
41 return *this;
42 }
43
44 template <typename T>
45 function_builder& param(const char *name) {
46 return param(name, _::type<T>::id(world_));
47 }
48
49 function_builder& callback(ecs_function_callback_t value) {
50 desc_.callback = value;
51 return *this;
52 }
53
54 function_builder& vector_callback(ecs_primitive_kind_t kind,
56 {
57 ecs_assert(kind > 0 && kind < FLECS_SCRIPT_VECTOR_FUNCTION_COUNT,
58 ECS_INVALID_PARAMETER, "invalid vector element kind");
59 desc_.vector_callbacks[kind] = value;
60 return *this;
61 }
62
63 function_builder& vector_callback(flecs::entity_t type,
65 {
68 "vector element type is not a primitive");
69 return vector_callback(primitive->kind, value);
70 }
71
72 function_builder& ctx(void *value) {
73 desc_.ctx = value;
74 return *this;
75 }
76
77 flecs::entity build() const;
78
79private:
80 flecs::world_t *world_;
82 int32_t param_count_;
83 bool is_method_;
84};
85
86/**
87 * @ingroup cpp_addons_script
88 * @{
89 */
90
91/** Script builder interface. */
93 /** Construct a script builder.
94 * @param world The world.
95 * @param name Optional name for the script entity.
96 */
97 script_builder(flecs::world_t *world, const char *name = nullptr)
98 : world_(world)
99 , desc_{}
100 {
101 if (name != nullptr) {
102 ecs_entity_desc_t entity_desc = {};
103 entity_desc.name = name;
104 entity_desc.sep = "::";
105 entity_desc.root_sep = "::";
106 this->desc_.entity = ecs_entity_init(world, &entity_desc);
107 }
108 }
109
110 /** Set the script code. */
111 script_builder& code(const char *str) {
112 desc_.code = str;
113 return *this;
114 }
115
116 /** Set the script filename. */
117 script_builder& filename(const char *str) {
118 desc_.filename = str;
119 return *this;
120 }
121
122 /** Run the script and return the script entity. */
123 flecs::entity run() const;
124
125protected:
126 flecs::world_t *world_;
127 ecs_script_desc_t desc_;
128};
129
130}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_primitive_kind_t
Primitive type kinds supported by meta addon.
Definition meta.h:177
void(*) ecs_function_callback_t(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result)
Script function callback.
Definition script.h:143
void(*) ecs_vector_function_callback_t(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result, int32_t elem_count)
Script vector function callback.
Definition script.h:150
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
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.
#define ecs_get(world, entity, T)
Get a component.
Definition flecs_c.h:339
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
Component added to primitive types.
Definition meta.h:200
Used with ecs_entity_init().
Definition flecs.h:1076
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1088
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1092
const char * name
Name of the entity.
Definition flecs.h:1083
Used with ecs_function_init() and ecs_method_init().
Definition script.h:1396
Used with ecs_script_init().
Definition script.h:783
Entity.
Definition entity.hpp:30
script_builder(flecs::world_t *world, const char *name=nullptr)
Construct a script builder.
Definition builder.hpp:97
flecs::entity run() const
Run the script and return the script entity.
Definition impl.hpp:13
script_builder & filename(const char *str)
Set the script filename.
Definition builder.hpp:117
script_builder & code(const char *str)
Set the script code.
Definition builder.hpp:111
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:129
flecs::entity primitive(flecs::meta::primitive_kind_t kind)
Create a primitive type.