mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Merge branch 'main' of github.com:getml/reflect-cpp
This commit is contained in:
@@ -281,8 +281,7 @@ Finally, it is very easy to extend full support to your own classes, refer to th
|
||||
reflect-cpp currently supports the following serialization formats:
|
||||
|
||||
- **JSON**: Out-of-the-box support, no additional dependencies required.
|
||||
- **XML**: Requires [libxml2](https://github.com/GNOME/libxml2) (TODO).
|
||||
- **flexbuffers**: Requires [flatbuffers](https://github.com/google/flatbuffers) (TODO).
|
||||
- **flexbuffers**: Requires [flatbuffers](https://github.com/google/flatbuffers).
|
||||
|
||||
reflect-cpp is deliberately designed in a very modular format, using [concepts](https://en.cppreference.com/w/cpp/language/constraints), to make it as easy as possible to support additional serialization formats. Refer to the [documentation](https://github.com/getml/reflect-cpp/tree/main/docs) for details. PRs related to serialization formats are welcome.
|
||||
|
||||
|
||||
@@ -52,6 +52,8 @@
|
||||
|
||||
5.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md)
|
||||
|
||||
5.2) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md)
|
||||
|
||||
## 6) Advanced topics
|
||||
|
||||
6.1) [Supporting your own format](https://github.com/getml/reflect-cpp/blob/main/docs/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
||||
|
||||
+38
-38
@@ -154,53 +154,53 @@ But what it more, unlike "normal" flatbuffers, flexbuffers also supports field n
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
|
||||
const auto weapons = std::vector<Weapon>({sword, axe});
|
||||
const auto weapons = std::vector<Weapon>({sword, axe});
|
||||
|
||||
const auto position = Vec3{1.0f, 2.0f, 3.0f};
|
||||
const auto position = Vec3{1.0f, 2.0f, 3.0f};
|
||||
|
||||
const auto inventory =
|
||||
std::vector<std::uint8_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
const auto inventory =
|
||||
std::vector<std::uint8_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
const auto orc = Monster{.pos = position,
|
||||
.mana = 150,
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe),
|
||||
.path = rfl::default_value};
|
||||
const auto orc = Monster{.pos = position,
|
||||
.mana = 150,
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe),
|
||||
.path = rfl::default_value};
|
||||
|
||||
const auto bytes = rfl::flexbuf::write(orc);
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Tests for flexbuffers
|
||||
|
||||
This folder contains the tests for flexbuffers. A Docker-based build pipeline is provided for Linux users.
|
||||
|
||||
Build the Docker container containing the build environment:
|
||||
|
||||
```
|
||||
./build_docker.sh
|
||||
```
|
||||
|
||||
Compile the tests:
|
||||
|
||||
```
|
||||
./compile.sh
|
||||
```
|
||||
|
||||
Run the tests:
|
||||
|
||||
```
|
||||
./build/reflect-cpp-tests
|
||||
```
|
||||
|
||||
To run the scripts, you may have to run `chmod +x build_docker.sh` and `chmod +x compile.sh` first.
|
||||
Reference in New Issue
Block a user