Skip to content
Flecs v4.1
reflection/ser_opaque_string

  • C

    View on GitHub

    1#include <ser_opaque_string.h>
    2#include <stdio.h>
    3
    4// Simple string type
    5typedef struct {
    6 char *value;
    7} String;
    8
    9// Reflection support for String
    10int String_serialize(const ecs_serializer_t *ser, const void *ptr) {
    11 const String *data = ptr;
    12 return ser->value(ser, ecs_id(ecs_string_t), &data->value);
    13}
    14
    15static void String_assign_string(void *ptr, const char *value) {
    16 String *data = ptr;
    17
    18 if (data->value) {
    19 ecs_os_free(data->value);
    20 }
    21
    22 data->value = ecs_os_strdup(value);
    23}
    24
    25int main(int argc, char *argv[]) {
    26 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    27
    28 // Register type as component so the size is known
    29 ECS_COMPONENT(ecs, String);
    30
    31 // Register String as opaque type. An opaque type is a type of which the
    32 // reflection framework doesn't know the layout, but which it can serialize.
    33 ecs_opaque(ecs, {
    34 // Link reflection with component
    35 .entity = ecs_id(String),
    36 // Let reflection framework know the type represents a string
    37 .type.as_type = ecs_id(ecs_string_t),
    38 // Register functions for de/serializing vector contents
    39 .type.serialize = String_serialize,
    40 .type.assign_string = String_assign_string
    41 });
    42
    43 String v = {0};
    44
    45 // Deserialize new value into string
    46 ecs_ptr_from_json(ecs, ecs_id(String), &v, "\"Hello World\"", NULL);
    47
    48 // Print string contents
    49 printf("%s\n", v.value);
    50
    51 // Serialize string contents to JSON
    52 char *json = ecs_ptr_to_json(ecs, ecs_id(String), &v);
    53 printf("%s\n", json);
    54 ecs_os_free(json);
    55
    56 // Free string. If this type were used as a component, we could
    57 // register a dtor to do this for us (see the entity/hooks example).
    58 ecs_os_free(v.value);
    59
    60 ecs_fini(ecs);
    61
    62 // Output
    63 // Hello World
    64 // "Hello World"
    65}
    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.
    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.
    char * ecs_string_t
    Built-in string type.
    Definition meta.h:110
    #define ecs_opaque(world,...)
    Create an opaque type.
    Definition meta.h:1773
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    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.
    Serializer interface.
    Definition meta.h:348
    int(*) value(const struct ecs_serializer_t *ser, ecs_entity_t type, const void *value)
    Serialize value.
    Definition meta.h:350