Skip to content
Flecs v4.1
reflection/ser_opaque_vector

  • C

    View on GitHub

    1#include <ser_opaque_vector.h>
    2#include <stdio.h>
    3
    4// Simple integer vector type
    5typedef struct {
    6 size_t count;
    7 int32_t *array;
    8} IntVector;
    9
    10// Reflection support for IntVector
    11int IntVector_serialize(const ecs_serializer_t *ser, const void *ptr) {
    12 const IntVector *data = ptr;
    13 for (size_t i = 0; i < data->count; i ++) {
    14 ser->value(ser, ecs_id(ecs_i32_t), &data->array[i]);
    15 }
    16 return 0;
    17}
    18
    19size_t IntVector_count(const void *ptr) {
    20 const IntVector *data = ptr;
    21 return data->count;
    22}
    23
    24static void IntVector_resize(void *ptr, size_t size) {
    25 IntVector *data = ptr;
    26 if (data->count != size) {
    27 data->count = size;
    28 if (!data->count) {
    29 ecs_os_free(data->array);
    30 data->array = NULL;
    31 } else {
    32 data->array = ecs_os_realloc_n(data->array, int32_t, (ecs_size_t)size);
    33 }
    34 }
    35}
    36
    37static void* IntVector_ensure_element(void *ptr, size_t index) {
    38 IntVector *data = ptr;
    39 if (data->count <= index) {
    40 IntVector_resize(ptr, index + 1);
    41 }
    42 return &data->array[index];
    43}
    44
    45int main(int argc, char *argv[]) {
    46 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    47
    48 // Register type as component so the size is known
    49 ECS_COMPONENT(ecs, IntVector);
    50
    51 // Register IntVector as opaque type. An opaque type is a type of which the
    52 // reflection framework doesn't know the layout, but which it can serialize.
    53 ecs_opaque(ecs, {
    54 // Link reflection with component
    55 .entity = ecs_id(IntVector),
    56 .type = {
    57 // Let reflection framework know the type represents a vector<i32>
    58 .as_type = ecs_vector(ecs, { .type = ecs_id(ecs_i32_t) }),
    59 // Register functions for de/serializing vector contents
    60 .serialize = IntVector_serialize,
    61 .count = IntVector_count,
    62 .resize = IntVector_resize,
    63 .ensure_element = IntVector_ensure_element
    64 }
    65 });
    66
    67 IntVector v = {0};
    68
    69 // Deserialize new value into vector
    70 ecs_ptr_from_json(ecs, ecs_id(IntVector), &v, "[10, 20, 30]", NULL);
    71
    72 // Print vector contents
    73 printf("[");
    74 for (size_t i = 0; i < v.count; i ++) {
    75 if (i) printf(", ");
    76 printf("%d", v.array[i]);
    77 }
    78 printf("]\n");
    79
    80 // Serialize vector contents to JSON
    81 char *json = ecs_ptr_to_json(ecs, ecs_id(IntVector), &v);
    82 printf("%s\n", json);
    83 ecs_os_free(json);
    84
    85 // Free array of vector. If this type were used as a component, we could
    86 // register a dtor to do this for us (see the entity/hooks example).
    87 ecs_os_free(v.array);
    88
    89 ecs_fini(ecs);
    90
    91 // Output
    92 // [10, 20, 30]
    93 // [10, 20, 30]
    94}
    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.
    #define ecs_vector(world,...)
    Create a vector type.
    Definition meta.h:1765
    #define ecs_opaque(world,...)
    Create an opaque type.
    Definition meta.h:1773
    int32_t ecs_i32_t
    Built-in i32 type.
    Definition meta.h:105
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    untyped_component & array(int32_t elem_count)
    Register array metadata for a component.
    #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