Skip to content
Flecs v4.1
entity_builder.inl
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/json/entity_builder.inl
3 * @brief JSON entity mixin.
4 */
5
6/** Set a component from JSON.
7 *
8 * @memberof flecs::entity_builder
9 * @ingroup cpp_addons_json
10 */
11const Self& set_json(
13 const char *json,
14 flecs::from_json_desc_t *desc = nullptr) const
15{
17 if (!ti) {
18 ecs_err("id is not a type");
19 return to_base();
20 }
21
23
24 void *ptr = ecs_ensure_id(world_, id_, e, static_cast<size_t>(ti->size));
25 ecs_assert(ptr != nullptr, ECS_INTERNAL_ERROR, nullptr);
26 ecs_ptr_from_json(world_, type, ptr, json, desc);
28
29 return to_base();
30}
31
32/** Set a pair from JSON.
33 *
34 * @memberof flecs::entity_builder
35 * @ingroup cpp_addons_json
36 */
37const Self& set_json(
40 const char *json,
41 flecs::from_json_desc_t *desc = nullptr) const
42{
43 return set_json(ecs_pair(r, t), json, desc);
44}
45
46/** Set a component from JSON.
47 *
48 * @memberof flecs::entity_builder
49 * @ingroup cpp_addons_json
50 */
51template <typename T>
52const Self& set_json(
53 const char *json,
54 flecs::from_json_desc_t *desc = nullptr) const
55{
56 return set_json(_::type<T>::id(world_), json, desc);
57}
58
59/** Set a pair from JSON.
60 *
61 * @memberof flecs::entity_builder
62 * @ingroup cpp_addons_json
63 */
64template <typename R, typename T>
65const Self& set_json(
66 const char *json,
67 flecs::from_json_desc_t *desc = nullptr) const
68{
69 return set_json(
72 json, desc);
73}
74
75/** Set a pair from JSON.
76 *
77 * @memberof flecs::entity_builder
78 * @ingroup cpp_addons_json
79 */
80template <typename R>
81const Self& set_json(
83 const char *json,
84 flecs::from_json_desc_t *desc = nullptr) const
85{
86 return set_json(
88 json, desc);
89}
90
91/** Set a pair from JSON.
92 *
93 * @memberof flecs::entity_builder
94 * @ingroup cpp_addons_json
95 */
96template <typename T>
97const Self& set_json_second(
99 const char *json,
100 flecs::from_json_desc_t *desc = nullptr) const
101{
102 return set_json(
104 json, desc);
105}
FLECS_API const char * ecs_ptr_from_json(const ecs_world_t *world, ecs_entity_t type, void *ptr, const char *json, const ecs_from_json_desc_t *desc)
Parse JSON string into value.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ecs_err(...)
Error macro.
Definition log.h:319
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:681
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for a component.
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure an entity has a component and return a pointer.
ecs_size_t size
Size of the type.
Definition flecs.h:1053
ecs_entity_t component
Handle to component (do not set).
Definition flecs.h:1056
const Self & set_json_second(flecs::entity_t r, const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a pair from JSON.
const Self & set_json(flecs::entity_t r, flecs::entity_t t, const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a pair from JSON.
const Self & set_json(flecs::id_t e, const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a component from JSON.
const Self & set_json(const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a pair from JSON.
const Self & set_json(const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a component from JSON.
const Self & set_json(flecs::entity_t t, const char *json, flecs::from_json_desc_t *desc=nullptr) const
Set a pair from JSON.
flecs::type type() const
Get the entity's type.
Definition impl.hpp:94
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152