Skip to content

Commit

Permalink
Add entity_builder::set_json, remove redundant template parameter fro…
Browse files Browse the repository at this point in the history
…m world::from_json
  • Loading branch information
SanderMertens committed Oct 6, 2023
1 parent d8dadaa commit fbb237f
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 5 deletions.
110 changes: 108 additions & 2 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20952,7 +20952,6 @@ flecs::string to_json() {
* \memberof flecs::world
* \ingroup cpp_addons_json
*/
template <typename T>
const char* from_json(flecs::entity_t tid, void* value, const char *json, flecs::from_json_desc_t *desc = nullptr) {
return ecs_ptr_from_json(m_world, tid, value, json, desc);
}
Expand Down Expand Up @@ -23473,6 +23472,114 @@ Self& quantity() {

# endif

# ifdef FLECS_JSON
/**
* @file addons/cpp/mixins/json/entity_builder.inl
* @brief JSON entity mixin.
*/

/** Set component from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
Self& set_json(
flecs::id_t e,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
flecs::entity_t type = ecs_get_typeid(m_world, e);
if (!type) {
ecs_err("id is not a type");
return to_base();
}

void *ptr = ecs_get_mut_id(m_world, m_id, e);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_ptr_from_json(m_world, type, ptr, json, desc);
ecs_modified_id(m_world, m_id, e);

return to_base();
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
Self& set_json(
flecs::entity_t r,
flecs::entity_t t,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(ecs_pair(r, t), json, desc);
}

/** Set component from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename T>
Self& set_json(
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(_::cpp_type<T>::id(m_world), json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename R, typename T>
Self& set_json(
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
_::cpp_type<R>::id(m_world),
_::cpp_type<T>::id(m_world),
json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename R>
Self& set_json(
flecs::entity_t t,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
_::cpp_type<R>::id(m_world), t,
json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename T>
Self& set_json_second(
flecs::entity_t r,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
r, _::cpp_type<T>::id(m_world),
json, desc);
}

# endif


protected:
Self& to_base() {
return *static_cast<Self*>(this);
Expand Down Expand Up @@ -23783,7 +23890,6 @@ const char* from_json(const char *json) {
return ecs_entity_from_json(m_world, m_id, json, nullptr);
}


# endif
};

Expand Down
5 changes: 5 additions & 0 deletions include/flecs/addons/cpp/mixins/entity/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,11 @@ struct entity_builder : entity_view {
# include "../meta/entity_builder.inl"
# endif

# ifdef FLECS_JSON
# include "../json/entity_builder.inl"
# endif


protected:
Self& to_base() {
return *static_cast<Self*>(this);
Expand Down
1 change: 0 additions & 1 deletion include/flecs/addons/cpp/mixins/json/entity.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
const char* from_json(const char *json) {
return ecs_entity_from_json(m_world, m_id, json, nullptr);
}

103 changes: 103 additions & 0 deletions include/flecs/addons/cpp/mixins/json/entity_builder.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @file addons/cpp/mixins/json/entity_builder.inl
* @brief JSON entity mixin.
*/

/** Set component from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
Self& set_json(
flecs::id_t e,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
flecs::entity_t type = ecs_get_typeid(m_world, e);
if (!type) {
ecs_err("id is not a type");
return to_base();
}

void *ptr = ecs_get_mut_id(m_world, m_id, e);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_ptr_from_json(m_world, type, ptr, json, desc);
ecs_modified_id(m_world, m_id, e);

return to_base();
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
Self& set_json(
flecs::entity_t r,
flecs::entity_t t,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(ecs_pair(r, t), json, desc);
}

/** Set component from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename T>
Self& set_json(
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(_::cpp_type<T>::id(m_world), json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename R, typename T>
Self& set_json(
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
_::cpp_type<R>::id(m_world),
_::cpp_type<T>::id(m_world),
json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename R>
Self& set_json(
flecs::entity_t t,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
_::cpp_type<R>::id(m_world), t,
json, desc);
}

/** Set pair from JSON.
*
* \memberof flecs::entity_builder
* \ingroup cpp_addons_json
*/
template <typename T>
Self& set_json_second(
flecs::entity_t r,
const char *json,
flecs::from_json_desc_t *desc = nullptr)
{
return set_json(
r, _::cpp_type<T>::id(m_world),
json, desc);
}
1 change: 0 additions & 1 deletion include/flecs/addons/cpp/mixins/json/world.inl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ flecs::string to_json() {
* \memberof flecs::world
* \ingroup cpp_addons_json
*/
template <typename T>
const char* from_json(flecs::entity_t tid, void* value, const char *json, flecs::from_json_desc_t *desc = nullptr) {
return ecs_ptr_from_json(m_world, tid, value, json, desc);
}
Expand Down
7 changes: 7 additions & 0 deletions test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1288,10 +1288,17 @@
"vector_type",
"i32_from_json",
"struct_from_json",
"void_from_json",
"entity_from_json_empty",
"entity_from_json_w_path",
"entity_from_json_w_ids",
"entity_from_json_w_values",
"set_type_json",
"set_pair_R_T_json",
"set_pair_R_t_json",
"set_pair_r_T_json",
"set_pair_r_t_json",
"set_id_json",
"ser_deser_std_string",
"ser_deser_std_vector_int",
"ser_deser_std_vector_std_string",
Expand Down
Loading

0 comments on commit fbb237f

Please sign in to comment.