From 5db114d9e2fc7971ea20b245e45d94c79cb5264c Mon Sep 17 00:00:00 2001 From: helintong Date: Mon, 18 Nov 2024 19:31:19 +0800 Subject: [PATCH] feat: add more example --- tool/README.md | 226 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) diff --git a/tool/README.md b/tool/README.md index d7341a21..405be7ab 100644 --- a/tool/README.md +++ b/tool/README.md @@ -160,17 +160,243 @@ Generate C++ files in optional struct pack format. protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=add_optional:./protos ``` +In the generated file, `std::string` will be converted to `std::optional`, and the 'class' type(For example `class Foo`) will be converted to `std::optional`. + +```cpp +#pragma once +#include + +enum class Color { + Red = 0, + Green = 1, + Blue = 2, +}; + +struct Vec3 { + float x; + float y; + float z; +}; +YLT_REFL(Vec3, x, y, z); + +struct Weapon { + std::optional name; + int32_t damage; +}; +YLT_REFL(Weapon, name, damage); + +struct Monster { + std::optional pos; + int32_t mana; + int32_t hp; + std::optional name; + std::optional inventory; + Color color; + std::vector> weapons; + std::optional equipped; + std::vector> path; +}; +YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path); + +struct Monsters { + std::vector> monsters; +}; +YLT_REFL(Monsters, monsters); + +struct person { + int32_t id; + std::optional name; + int32_t age; + double salary; +}; +YLT_REFL(person, id, name, age, salary); + +struct persons { + std::vector> person_list; +}; +YLT_REFL(persons, person_list); + +struct bench_int32 { + int32_t a; + int32_t b; + int32_t c; + int32_t d; +}; +YLT_REFL(bench_int32, a, b, c, d); + + +``` + ## enable_inherit Generate C++ files in non std::optional format and the file conforms to the `struct pb` standard. ```shell protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=enable_inherit:./protos +``` + +```cpp +#pragma once +#include + +enum class Color { + Red = 0, + Green = 1, + Blue = 2, +}; + +struct Vec3 : public iguana::base_impl { + Vec3() = default; + Vec3(float a, float b, float c) : x(a), y(b), z(c) {} + float x; + float y; + float z; +}; +YLT_REFL(Vec3, x, y, z); + +struct Weapon : public iguana::base_impl { + Weapon() = default; + Weapon(std::string a, int32_t b) : name(std::move(a)), damage(b) {} + std::string name; + int32_t damage; +}; +YLT_REFL(Weapon, name, damage); + +struct Monster : public iguana::base_impl { + Monster() = default; + Monster(Vec3 a, int32_t b, int32_t c, std::string d, std::string e, Color f, std::vector g, Weapon h, std::vector i) : pos(a), mana(b), hp(c), name(std::move(d)), inventory(std::move(e)), color(f), weapons(std::move(g)), equipped(h), path(std::move(i)) {} + Vec3 pos; + int32_t mana; + int32_t hp; + std::string name; + std::string inventory; + Color color; + std::vector weapons; + Weapon equipped; + std::vector path; +}; +YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path); + +struct Monsters : public iguana::base_impl { + Monsters() = default; + Monsters(std::vector a) : monsters(std::move(a)) {} + std::vector monsters; +}; +YLT_REFL(Monsters, monsters); + +struct person : public iguana::base_impl { + person() = default; + person(int32_t a, std::string b, int32_t c, double d) : id(a), name(std::move(b)), age(c), salary(d) {} + int32_t id; + std::string name; + int32_t age; + double salary; +}; +YLT_REFL(person, id, name, age, salary); + +struct persons : public iguana::base_impl { + persons() = default; + persons(std::vector a) : person_list(std::move(a)) {} + std::vector person_list; +}; +YLT_REFL(persons, person_list); + +struct bench_int32 : public iguana::base_impl { + bench_int32() = default; + bench_int32(int32_t a, int32_t b, int32_t c, int32_t d) : a(a), b(b), c(c), d(d) {} + int32_t a; + int32_t b; + int32_t c; + int32_t d; +}; +YLT_REFL(bench_int32, a, b, c, d); + ``` ## add_optional and enable_inherit +The presence of these two parameters indicates that these two functions take effect on the generated file at the same time. + ```shell protoc --plugin=protoc-gen-custom=./build/proto_to_struct data.proto --custom_out=add_optional+enable_inherit:./protos ``` + +```cpp +#pragma once +#include + +enum class Color { + Red = 0, + Green = 1, + Blue = 2, +}; + +struct Vec3 : public iguana::base_impl { + Vec3() = default; + Vec3(float a, float b, float c) : x(a), y(b), z(c) {} + float x; + float y; + float z; +}; +YLT_REFL(Vec3, x, y, z); + +struct Weapon : public iguana::base_impl { + Weapon() = default; + Weapon(std::optional a, int32_t b) : name(std::move(a)), damage(b) {} + std::optional name; + int32_t damage; +}; +YLT_REFL(Weapon, name, damage); + +struct Monster : public iguana::base_impl { + Monster() = default; + Monster(std::optional a, int32_t b, int32_t c, std::optional d, std::optional e, Color f, std::vector> g, std::optional h, std::vector> i) : pos(a), mana(b), hp(c), name(std::move(d)), inventory(std::move(e)), color(f), weapons(std::move(g)), equipped(h), path(std::move(i)) {} + std::optional pos; + int32_t mana; + int32_t hp; + std::optional name; + std::optional inventory; + Color color; + std::vector> weapons; + std::optional equipped; + std::vector> path; +}; +YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path); + +struct Monsters : public iguana::base_impl { + Monsters() = default; + Monsters(std::vector> a) : monsters(std::move(a)) {} + std::vector> monsters; +}; +YLT_REFL(Monsters, monsters); + +struct person : public iguana::base_impl { + person() = default; + person(int32_t a, std::optional b, int32_t c, double d) : id(a), name(std::move(b)), age(c), salary(d) {} + int32_t id; + std::optional name; + int32_t age; + double salary; +}; +YLT_REFL(person, id, name, age, salary); + +struct persons : public iguana::base_impl { + persons() = default; + persons(std::vector> a) : person_list(std::move(a)) {} + std::vector> person_list; +}; +YLT_REFL(persons, person_list); + +struct bench_int32 : public iguana::base_impl { + bench_int32() = default; + bench_int32(int32_t a, int32_t b, int32_t c, int32_t d) : a(a), b(b), c(c), d(d) {} + int32_t a; + int32_t b; + int32_t c; + int32_t d; +}; +YLT_REFL(bench_int32, a, b, c, d); + + +``` \ No newline at end of file