Skip to content

Commit

Permalink
prepare for reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 24, 2024
1 parent 1057cf7 commit 9681565
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 279 deletions.
3 changes: 0 additions & 3 deletions include/ylt/standalone/iguana/pb_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace iguana {

template <typename T>
IGUANA_INLINE void from_pb(T& t, std::string_view pb_str);

namespace detail {

template <typename T>
Expand Down
27 changes: 24 additions & 3 deletions include/ylt/standalone/iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,30 @@ enum class WireType : uint32_t {
Unknown
};

template <typename T, typename Stream>
IGUANA_INLINE void to_pb(T& t, Stream& out);

template <typename T>
IGUANA_INLINE void from_pb(T& t, std::string_view pb_str);

struct pb_base {
size_t cache_size;
virtual void to_pb(std::string& str) {}
virtual void from_pb(std::string_view str) {}

size_t cache_size = 0;
virtual ~pb_base() {}
};

template <typename T>
struct pb_base_impl : public pb_base {
void to_pb(std::string& str) override {
iguana::to_pb(*(static_cast<T*>(this)), str);
}

void from_pb(std::string_view str) override {
iguana::from_pb(*(static_cast<T*>(this)), str);
}
virtual ~pb_base_impl() {}
};

template <typename T>
Expand Down Expand Up @@ -425,11 +447,10 @@ IGUANA_INLINE size_t pb_key_value_size(Type&& t) {
using T = std::remove_const_t<std::remove_reference_t<Type>>;
if constexpr (is_reflection_v<T> || is_custom_reflection_v<T>) {
size_t len = 0;
constexpr auto tuple = get_members_tuple<T>();
static constexpr auto tuple = get_members_tuple<T>();
constexpr size_t SIZE = std::tuple_size_v<std::decay_t<decltype(tuple)>>;
for_each_n(
[&len, &t](auto i) IGUANA__INLINE_LAMBDA {
constexpr auto tuple = get_members_tuple<T>();
using field_type =
std::tuple_element_t<decltype(i)::value,
std::decay_t<decltype(tuple)>>;
Expand Down
57 changes: 37 additions & 20 deletions src/struct_pack/benchmark/struct_pb_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,66 @@
#include "sample.hpp"

namespace pb_sample {
struct person : public iguana::pb_base {
struct person : public iguana::pb_base_impl<person> {
person() = default;
person(int32_t a, std::string b, int c, double d)
: id(a), name(std::move(b)), age(c), salary(d) {}
int32_t id;
std::string name;
int age;
double salary;
};
REFLECTION(person, id, name, age, salary);

struct persons : public iguana::pb_base {
struct persons : public iguana::pb_base_impl<persons> {
persons() = default;
explicit persons(std::vector<person> l) : list(std::move(l)) {}
std::vector<person> list;
};
REFLECTION(persons, list);

struct rect : public iguana::pb_base {
struct rect : public iguana::pb_base_impl<rect> {
rect() = default;
rect(int32_t a, int32_t b, int32_t c, int32_t d)
: x(a), y(b), width(c), height(d) {}
int32_t x = 1;
int32_t y = 0;
int32_t width = 11;
int32_t height = 1;
};
REFLECTION(rect, x, y, width, height);

struct rects : public iguana::pb_base {
struct rects : public iguana::pb_base_impl<rects> {
rects() = default;
explicit rects(std::vector<rect> l) : list(std::move(l)) {}
std::vector<rect> list;
};
REFLECTION(rects, list);

struct Vec3 : public iguana::pb_base {
struct Vec3 : public iguana::pb_base_impl<Vec3> {
Vec3() = default;
Vec3(float a, float b, float c) : x(a), y(b), z(c) {}
float x;
float y;
float z;

REFLECTION(Vec3, x, y, z);
};

struct Weapon : public iguana::pb_base {
struct Weapon : public iguana::pb_base_impl<Weapon> {
Weapon() = default;
Weapon(std::string s, int32_t d) : name(std::move(s)), damage(d) {}
std::string name;
int32_t damage;
};
REFLECTION(Weapon, name, damage);

enum Color : uint8_t { Red, Green, Blue };

struct Monster : public iguana::pb_base {
struct Monster : public iguana::pb_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) {}
Vec3 pos;
int32_t mana;
int32_t hp;
Expand All @@ -62,13 +79,15 @@ struct Monster : public iguana::pb_base {
REFLECTION(Monster, pos, mana, hp, name, inventory, color, weapons, equipped,
path);

struct Monsters : public iguana::pb_base {
struct Monsters : public iguana::pb_base_impl<Monsters> {
Monsters() = default;
explicit Monsters(std::vector<Monster> l) : list(std::move(l)) {}
std::vector<Monster> list;
};
REFLECTION(Monsters, list);

inline auto create_rects(size_t object_count) {
rect rc{{0}, 1, 0, 11, 1};
rect rc{1, 0, 11, 1};
std::vector<rect> v{};
for (std::size_t i = 0; i < object_count; i++) {
v.push_back(rc);
Expand All @@ -78,7 +97,7 @@ inline auto create_rects(size_t object_count) {

inline auto create_persons(size_t object_count) {
std::vector<person> v{};
person p{{0}, 432798, std::string(1024, 'A'), 24, 65536.42};
person p{432798, std::string(1024, 'A'), 24, 65536.42};

for (std::size_t i = 0; i < object_count; i++) {
v.push_back(p);
Expand All @@ -89,29 +108,27 @@ inline auto create_persons(size_t object_count) {
inline std::vector<Monster> create_monsters(size_t object_count) {
std::vector<Monster> v{};
Monster m = {
{0},
Vec3{{0}, 1, 2, 3},
Vec3{1, 2, 3},
16,
24,
"it is a test",
"\1\2\3\4",
Color::Red,
{{{0}, "gun", 42}, {{0}, "shotgun", 56}},
{{0}, "air craft", 67},
{{{0}, 7, 8, 9}, {{0}, 71, 81, 91}},
{{"gun", 42}, {"shotgun", 56}},
{"air craft", 67},
{{7, 8, 9}, {71, 81, 91}},
};

Monster m1 = {
{0},
{{0}, 11, 22, 33},
{11, 22, 33},
161,
241,
"it is a test, ok",
"\24\25\26\24",
Color::Red,
{{{0}, "gun", 421}, {{0}, "shotgun", 561}},
{{0}, "air craft", 671},
{{{0}, 71, 82, 93}, {{0}, 711, 821, 931}},
{{"gun", 421}, {"shotgun", 561}},
{"air craft", 671},
{{71, 82, 93}, {711, 821, 931}},
};

for (std::size_t i = 0; i < object_count / 2; i++) {
Expand Down
13 changes: 10 additions & 3 deletions src/struct_pb/examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
#include <iostream>
#include <ylt/struct_pb.hpp>

struct my_struct : struct_pb::pb_base {
struct my_struct : struct_pb::pb_base_impl<my_struct> {
my_struct() = default;
my_struct(int a, bool b, struct_pb::fixed64_t c) : x(a), y(b), z(c) {}
int x;
bool y;
struct_pb::fixed64_t z;
};
REFLECTION(my_struct, x, y, z);

struct nest : struct_pb::pb_base {
struct nest : struct_pb::pb_base_impl<nest> {
nest() = default;
nest(std::string s, my_struct t, int v)
: name(std::move(s)), value(t), var(v) {}
std::string name;
my_struct value;
int var;
};
REFLECTION(nest, name, value, var);

int main() {
nest v{0, "Hi", {0, 1, false, 3}, 5}, v2{};
nest v{"Hi", my_struct{1, false, {3}}, 5};
std::string s;
struct_pb::to_pb(v, s);

nest v2;
struct_pb::from_pb(v2, s);
assert(v.var == v2.var);
assert(v.value.y == v2.value.y);
Expand Down
Loading

0 comments on commit 9681565

Please sign in to comment.