Skip to content
Flecs v4.1
reflection/auto_define_enum

  • C

    View on GitHub

    1#include <auto_define_enum.h>
    2#include <stdio.h>
    3
    4// Color is used by Car, so it must also be captured
    5ECS_ENUM(Color, {
    6 Red, Black, White, StainlessSteel
    7});
    8
    9ECS_STRUCT(Car, {
    10 const char *brand;
    11 Color color;
    12 float speed;
    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 Color is registered first, so that
    19 // when Car is registered, the type information of Color can be looked up
    20 ECS_META_COMPONENT(ecs, Color);
    21 ECS_META_COMPONENT(ecs, Car);
    22
    23 // Serialize as usual
    24 Car value = {"Delorean", StainlessSteel, 1.21f};
    25 char *json = ecs_ptr_to_json(ecs, ecs_id(Car), &value);
    26 printf("%s\n", json); // {"brand": "Delorean", "color": "StainlessSteel", "speed": 1.21}
    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
    #define ECS_ENUM(name,...)
    ECS_ENUM(name, body).
    Definition meta_c.h:51
    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_enum.h>
    2#include <iostream>
    3
    4// Capture reflection data for Color and Car
    5ECS_ENUM(Color, {
    6 Red, Black, White, StainlessSteel
    7});
    8
    9ECS_STRUCT(Car, {
    10 const char *brand;
    11 Color color;
    12 float speed;
    13});
    14
    15int main(int, char *[]) {
    16 flecs::world ecs;
    17
    18 // Color won't be detected by automatic component registration, so register
    19 // it explicitly.
    20 ecs.component<Color>();
    21
    22 // Serialize as usual. Component registration automatically detects the
    23 // reflection data for Car.
    24 Car value = {"Delorean", StainlessSteel, 1.21f};
    25 std::cout << ecs.to_json(&value) << "\n"; // {"brand":"Delorean", "color":"StainlessSteel", "speed":1.2100000381}
    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