Skip to content

Commit

Permalink
feat: add more example
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Nov 18, 2024
1 parent 2a2ed33 commit 5db114d
Showing 1 changed file with 226 additions and 0 deletions.
226 changes: 226 additions & 0 deletions tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>`, and the 'class' type(For example `class Foo`) will be converted to `std::optional<Foo>`.

```cpp
#pragma once
#include <ylt/struct_pb.hpp>

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<std::string> name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);

struct Monster {
std::optional<Vec3> pos;
int32_t mana;
int32_t hp;
std::optional<std::string> name;
std::optional<std::string> inventory;
Color color;
std::vector<std::optional<Weapon>> weapons;
std::optional<Weapon> equipped;
std::vector<std::optional<Vec3>> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);

struct Monsters {
std::vector<std::optional<Monster>> monsters;
};
YLT_REFL(Monsters, monsters);

struct person {
int32_t id;
std::optional<std::string> name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);

struct persons {
std::vector<std::optional<person>> 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 <ylt/struct_pb.hpp>

enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};

struct Vec3 : public iguana::base_impl<Vec3> {
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> {
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> {
Monster() = default;
Monster(Vec3 a, int32_t b, int32_t c, std::string d, std::string e, Color f, std::vector<Weapon> g, Weapon h, std::vector<Vec3> 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<Weapon> weapons;
Weapon equipped;
std::vector<Vec3> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);

struct Monsters : public iguana::base_impl<Monsters> {
Monsters() = default;
Monsters(std::vector<Monster> a) : monsters(std::move(a)) {}
std::vector<Monster> monsters;
};
YLT_REFL(Monsters, monsters);

struct person : public iguana::base_impl<person> {
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> {
persons() = default;
persons(std::vector<person> a) : person_list(std::move(a)) {}
std::vector<person> person_list;
};
YLT_REFL(persons, person_list);

struct bench_int32 : public iguana::base_impl<bench_int32> {
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 <ylt/struct_pb.hpp>

enum class Color {
Red = 0,
Green = 1,
Blue = 2,
};

struct Vec3 : public iguana::base_impl<Vec3> {
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> {
Weapon() = default;
Weapon(std::optional<std::string> a, int32_t b) : name(std::move(a)), damage(b) {}
std::optional<std::string> name;
int32_t damage;
};
YLT_REFL(Weapon, name, damage);

struct Monster : public iguana::base_impl<Monster> {
Monster() = default;
Monster(std::optional<Vec3> a, int32_t b, int32_t c, std::optional<std::string> d, std::optional<std::string> e, Color f, std::vector<std::optional<Weapon>> g, std::optional<Weapon> h, std::vector<std::optional<Vec3>> 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<Vec3> pos;
int32_t mana;
int32_t hp;
std::optional<std::string> name;
std::optional<std::string> inventory;
Color color;
std::vector<std::optional<Weapon>> weapons;
std::optional<Weapon> equipped;
std::vector<std::optional<Vec3>> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);

struct Monsters : public iguana::base_impl<Monsters> {
Monsters() = default;
Monsters(std::vector<std::optional<Monster>> a) : monsters(std::move(a)) {}
std::vector<std::optional<Monster>> monsters;
};
YLT_REFL(Monsters, monsters);

struct person : public iguana::base_impl<person> {
person() = default;
person(int32_t a, std::optional<std::string> b, int32_t c, double d) : id(a), name(std::move(b)), age(c), salary(d) {}
int32_t id;
std::optional<std::string> name;
int32_t age;
double salary;
};
YLT_REFL(person, id, name, age, salary);

struct persons : public iguana::base_impl<persons> {
persons() = default;
persons(std::vector<std::optional<person>> a) : person_list(std::move(a)) {}
std::vector<std::optional<person>> person_list;
};
YLT_REFL(persons, person_list);

struct bench_int32 : public iguana::base_impl<bench_int32> {
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);


```

0 comments on commit 5db114d

Please sign in to comment.