1#include <ser_std_optional.h>
2#include <iostream>
3#include <optional>
4
5template <typename T>
10 if (*data) {
12 }
13 return 0;
14 })
15 .count(
16 [](const std::optional<T> *data) -> size_t { return *data ? 1 : 0; })
17 .resize([](std::optional<T> *data, size_t size) {
18 switch (size) {
19 case 0:
20 *data = std::nullopt;
21 break;
22 case 1:
23 if (!data->has_value()) {
24 *data = T();
25 }
26 break;
27 default:
28 assert(false);
29 }
30 })
31 .ensure_element([](std::optional<T> *data, size_t) {
32 if (!data->has_value()) {
33 *data = T();
34 }
35 return &data->value();
36 });
37}
38
39int main(int, char *[]) {
41
42
44 .
opaque(std_optional_support<int>);
45
46 std::optional<int> empty = std::nullopt;
47 std::cout << ecs.
to_json(&empty) << std::endl;
48
49 std::optional<int> full = 1;
50 std::cout << ecs.
to_json(&full) << std::endl;
51
52 std::optional<int> o;
54 std::cout << ecs.
to_json(&o) << std::endl;
55
56
57
58
59
60}
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 & serialize(flecs::serialize< T > func)
Set the serialize function.