1#include <ser_std_vector.h>
2#include <iostream>
3#include <string>
4#include <vector>
5
6
7
8struct VectorComponent {
9 std::vector<int32_t> ints;
10 std::vector<std::string> strings;
11};
12
13
14template <typename Elem, typename Vector = std::vector<Elem>>
18
19
21 for (const auto& el : *data) {
23 }
24 return 0;
25 })
26
27
28 .count([](const Vector *data) {
29 return data->size();
30 })
31
32
33 .resize([](Vector *data, size_t size) {
34 data->resize(size);
35 })
36
37
38 .ensure_element([](Vector *data, size_t elem) {
39 if (data->size() <= elem) {
40 data->resize(elem + 1);
41 }
42
43 return &data->data()[elem];
44 });
45}
46
47int main(int, char *[]) {
49
50
54 const char *str = data->c_str();
55 return s->
value(flecs::String, &str);
56 })
57 .assign_string([](std::string* data, const char *value) {
58 *data = value;
59 });
60
61
63 .
opaque(std_vector_support<int>);
64
65
67 .
opaque(std_vector_support<std::string>);
68
69
71 .member<std::vector<int>>("ints")
72 .member<std::vector<std::string>>("strings");
73
74
75 VectorComponent v = {{1, 2, 3}, {"foo", "bar"}};
76 std::cout << ecs.
to_json(&v) << std::endl;
77
78
80 "{\"ints\": [4, 5], \"strings\":[\"hello\", \"flecs\", \"reflection\"]}");
81
82
83 std::cout << ecs.
to_json(&v) << std::endl;
84
85
86
87
88}
component & opaque(const Func &type_support)
Register an opaque type interface.
const char * from_json(flecs::entity_t tid, void *value, const char *json, flecs::from_json_desc_t *desc=nullptr)
Deserialize a value from JSON.
flecs::string to_json(flecs::entity_t tid, const void *value) const
Serialize an untyped value to JSON.
flecs::component< T > component(Args &&... args) const
Find or register a component.
int(*) value(const struct ecs_serializer_t *ser, ecs_entity_t type, const void *value)
Serialize value.
Type-safe interface for opaque types.
opaque & as_type(flecs::id_t func)
Set the type that describes the kind/structure of the opaque type.
opaque & serialize(flecs::serialize< T > func)
Set the serialize function.