Skip to content
Flecs v4.1
reflection/auto_define_nested_struct

  • C

    View on GitHub

    1#include <auto_define_nested_struct.h>
    2#include <stdio.h>
    3
    4// Point is used by Line, so it must also be captured
    5ECS_STRUCT(Point, {
    6 int32_t x;
    7 int32_t y;
    8});
    9
    10ECS_STRUCT(Line, {
    11 Point start;
    12 Point stop;
    13});
    14
    15int main(int argc, char *argv[]) {
    16 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    17
    18 // Register both components. Note that Point is registered first, so that
    19 // when Line is registered, the type information of Point can be looked up
    20 ECS_META_COMPONENT(ecs, Point);
    21 ECS_META_COMPONENT(ecs, Line);
    22
    23 // Serialize as usual
    24 Line value = {{1, 2}, {3, 4}};
    25 char *json = ecs_ptr_to_json(ecs, ecs_id(Line), &value);
    26 printf("%s\n", json); // {"start": {"x": 1, "y": 2}, "stop": {"x": 3, "y": 4}}
    27 ecs_os_free(json);
    28
    29 return ecs_fini(ecs);
    30}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    FLECS_API char * ecs_ptr_to_json(const ecs_world_t *world, ecs_entity_t type, const void *data)
    Serialize value into JSON string.
    #define ECS_META_COMPONENT(world, name)
    Declare component with descriptor.
    Definition meta_c.h:40
    #define ECS_STRUCT(name,...)
    ECS_STRUCT(name, body).
    Definition meta_c.h:46
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.

  • C++

    View on GitHub

    1#include <auto_define_nested_struct.h>
    2#include <iostream>
    3
    4// Capture reflection data for Point and Line.
    5ECS_STRUCT(Point, {
    6 int32_t x;
    7 int32_t y;
    8});
    9
    10ECS_STRUCT(Line, {
    11 Point start;
    12 Point stop;
    13});
    14
    15int main(int, char *[]) {
    16 flecs::world ecs;
    17
    18 // Point won't be detected by automatic component registration, so register
    19 // it explicitly.
    20 ecs.component<Point>();
    21
    22 // Serialize as usual. Component registration automatically detects the
    23 // reflection data for Line.
    24 Line value = {{1, 2}, {3, 4}};
    25 std::cout << ecs.to_json(&value) << "\n"; // {"start":{"x":1, "y":2}, "stop":{"x":3, "y":4}}
    26}
    flecs::string to_json(flecs::entity_t tid, const void *value) const
    Serialize an untyped value to JSON.
    Definition world.hpp:12
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    The world.
    Definition world.hpp:129