Properly link flecs library
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#ifndef SER_STD_STRING_H
|
||||
#define SER_STD_STRING_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "ser_std_string/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef SER_STD_STRING_BAKE_CONFIG_H
|
||||
#define SER_STD_STRING_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "ser_std_string",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <ser_std_string.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// This example shows how to serialize a component with an std::string
|
||||
|
||||
struct StringComponent {
|
||||
std::string a;
|
||||
std::string b;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Register reflection for std::string
|
||||
ecs.component<std::string>()
|
||||
.opaque(flecs::String) // Opaque type that maps to string
|
||||
.serialize([](const flecs::serializer *s, const std::string *data) {
|
||||
const char *str = data->c_str();
|
||||
return s->value(flecs::String, &str); // Forward to serializer
|
||||
})
|
||||
.assign_string([](std::string* data, const char *value) {
|
||||
*data = value; // Assign new value to std::string
|
||||
});
|
||||
|
||||
// Register component with std::string members
|
||||
ecs.component<StringComponent>()
|
||||
.member<std::string>("a")
|
||||
.member<std::string>("b");
|
||||
|
||||
// Create value & serialize it
|
||||
StringComponent v = {"foo", "bar"};
|
||||
std::cout << ecs.to_json(&v) << std::endl;
|
||||
|
||||
// Deserialize new strings into value
|
||||
ecs.from_json(&v, "{\"a\": \"hello\", \"b\": \"world\"}");
|
||||
std::cout << "{a: " << v.a << ", b: " << v.b << "}" << std::endl;
|
||||
|
||||
// Output:
|
||||
// {"a": "foo", "b": "bar"}
|
||||
// {a: "hello", b: "world"}
|
||||
}
|
||||
Reference in New Issue
Block a user