Skip to content

Commit

Permalink
fix: std::optinal before vector
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Nov 20, 2024
1 parent 5db114d commit d07ad5a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
30 changes: 15 additions & 15 deletions tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ struct Monster {
std::string name;
std::string inventory;
Color color;
std::vector<Weapon> weapons;
std::vector<Weapon>weapons;
Weapon equipped;
std::vector<Vec3> path;
std::vector<Vec3>path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);

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

Expand All @@ -135,7 +135,7 @@ struct person {
YLT_REFL(person, id, name, age, salary);

struct persons {
std::vector<person> person_list;
std::vector<person>person_list;
};
YLT_REFL(persons, person_list);

Expand Down Expand Up @@ -192,14 +192,14 @@ struct Monster {
std::optional<std::string> name;
std::optional<std::string> inventory;
Color color;
std::vector<std::optional<Weapon>> weapons;
std::optional<std::vector<Weapon>> weapons;
std::optional<Weapon> equipped;
std::vector<std::optional<Vec3>> path;
std::optional<std::vector<Vec3>> path;
};
YLT_REFL(Monster, pos, mana, hp, name, inventory, color, weapons, equipped, path);

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

Expand All @@ -212,7 +212,7 @@ struct person {
YLT_REFL(person, id, name, age, salary);

struct persons {
std::vector<std::optional<person>> person_list;
std::optional<std::vector<person>> person_list;
};
YLT_REFL(persons, person_list);

Expand Down Expand Up @@ -351,23 +351,23 @@ 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)) {}
Monster(std::optional<Vec3> a, int32_t b, int32_t c, std::optional<std::string> d, std::optional<std::string> e, Color f, std::optional<std::vector<Weapon>> g, std::optional<Weapon> h, std::optional<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)) {}
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<std::vector<Weapon>> weapons;
std::optional<Weapon> equipped;
std::vector<std::optional<Vec3>> path;
std::optional<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<std::optional<Monster>> a) : monsters(std::move(a)) {}
std::vector<std::optional<Monster>> monsters;
Monsters(std::optional<std::vector<Monster>> a) : monsters(std::move(a)) {}
std::optional<std::vector<Monster>> monsters;
};
YLT_REFL(Monsters, monsters);

Expand All @@ -383,8 +383,8 @@ 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;
persons(std::optional<std::vector<person>> a) : person_list(std::move(a)) {}
std::optional<std::vector<person>> person_list;
};
YLT_REFL(persons, person_list);

Expand Down
47 changes: 31 additions & 16 deletions tool/struct_code_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::string code_generate_struct_default(const std::string &struct_name,

std::string code_generate_struct_constructor(
const std::string &struct_name, const std::vector<struct_token> lists,
bool add_optiional) {
bool add_optional) {
std::string result = struct_name + "(";
int i = 0;
int list_size = lists.size() - 1;
Expand All @@ -56,17 +56,23 @@ std::string code_generate_struct_constructor(
}
else if (it->type == struct_token_type::proto_string) {
if (it->lable == lable_type::lable_repeated) {
result.append("std::vector<std::string> ");
if (add_optional) {
result.append("std::optional<");
}
result.append("std::vector<std::string>");
if (add_optional) {
result.append("> ");
}
result += parameter_value[i];
}
else {
if (add_optiional) {
if (add_optional) {
result.append("std::optional<");
}

result.append("std::");
result.append(it->type_name);
if (add_optiional) {
if (add_optional) {
result.append(">");
}
result.append(" ");
Expand All @@ -87,25 +93,27 @@ std::string code_generate_struct_constructor(
}
else {
if (it->lable == lable_type::lable_repeated) {
result.append("std::vector<");

if (add_optiional) {
if (add_optional) {
result.append("std::optional<");
}

result.append("std::vector<");

result.append(it->type_name);
if (add_optiional) {
result.append(">");
result.append(">");

if (add_optional) {
result.append("> ");
}
result.append("> ");

result += parameter_value[i];
}
else {
if (add_optiional) {
if (add_optional) {
result.append("std::optional<");
}
result.append(it->type_name);
if (add_optiional) {
if (add_optional) {
result.append(">");
}
result.append(" ");
Expand Down Expand Up @@ -166,20 +174,27 @@ std::string code_generate_body(const std::vector<struct_token> &lists,
result.append("\t");
if (ll.lable == lable_type::lable_repeated) {
if (ll.type == struct_token_type::proto_string) {
result.append("std::vector<std::string> ");
if (add_optional) {
result.append("std::optional<");
}

result.append("std::vector<std::string>");
if (add_optional) {
result.append("> ");
}
result.append(ll.var_name);
result.append(";\n");
}
else {
result.append("std::vector<");
if (ll.type == struct_token_type::message && add_optional) {
result.append("std::optional<");
}
result.append("std::vector<");
result.append(ll.type_name);
result.append(">");
if (ll.type == struct_token_type::message && add_optional) {
result.append(">");
result.append("> ");
}
result.append("> ");
result.append(ll.var_name);
result.append(";\n");
}
Expand Down

0 comments on commit d07ad5a

Please sign in to comment.