Skip to content

Commit

Permalink
rename and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 28, 2024
1 parent 1689890 commit 5154d54
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 47 deletions.
22 changes: 13 additions & 9 deletions iguana/pb_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstddef>
#include <cstring>
#include <map>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
Expand Down Expand Up @@ -34,15 +35,15 @@ IGUANA_INLINE void to_pb(T& t, Stream& out);
template <typename T>
IGUANA_INLINE void from_pb(T& t, std::string_view pb_str);

using pb_base = detail::pb_base;
using base = detail::base;

template <typename T, typename U>
IGUANA_INLINE constexpr size_t member_offset(T* t, U T::*member) {
return (char*)&(t->*member) - (char*)t;
}

template <typename T>
struct pb_base_impl : public pb_base {
struct base_impl : public base {
void to_pb(std::string& str) override {
iguana::to_pb(*(static_cast<T*>(this)), str);
}
Expand Down Expand Up @@ -89,17 +90,20 @@ struct pb_base_impl : public pb_base {
return vec;
}

virtual ~pb_base_impl() {}
virtual ~base_impl() {}

size_t cache_size = 0;
};

template <typename T>
constexpr bool inherits_from_pb_base_v = std::is_base_of_v<pb_base, T>;
constexpr bool inherits_from_base_v = std::is_base_of_v<base, T>;

IGUANA_INLINE std::shared_ptr<pb_base> create_instance(std::string_view name) {
IGUANA_INLINE std::shared_ptr<base> create_instance(std::string_view name) {
auto it = iguana::detail::g_pb_map.find(name);
assert(it != iguana::detail::g_pb_map.end());
if (it == iguana::detail::g_pb_map.end()) {
throw std::invalid_argument(std::string(name) +
"not inheried from iguana::base_impl");
}
return it->second();
}

Expand Down Expand Up @@ -514,7 +518,7 @@ IGUANA_INLINE size_t pb_key_value_size(Type&& t, Arr& size_arr) {
static constexpr auto tuple = get_members_tuple<T>();
constexpr size_t SIZE = std::tuple_size_v<std::decay_t<decltype(tuple)>>;
size_t pre_index = -1;
if constexpr (!inherits_from_pb_base_v<T> && key_size != 0) {
if constexpr (!inherits_from_base_v<T> && key_size != 0) {
pre_index = size_arr.size();
size_arr.push_back(0); // placeholder
}
Expand Down Expand Up @@ -543,7 +547,7 @@ IGUANA_INLINE size_t pb_key_value_size(Type&& t, Arr& size_arr) {
}
},
std::make_index_sequence<SIZE>{});
if constexpr (inherits_from_pb_base_v<T>) {
if constexpr (inherits_from_base_v<T>) {
t.cache_size = len;
}
else if constexpr (key_size != 0) {
Expand Down Expand Up @@ -612,7 +616,7 @@ template <bool skip_next = true, typename Type>
IGUANA_INLINE size_t pb_value_size(Type&& t, uint32_t*& sz_ptr) {
using T = std::remove_const_t<std::remove_reference_t<Type>>;
if constexpr (is_reflection_v<T> || is_custom_reflection_v<T>) {
if constexpr (inherits_from_pb_base_v<T>) {
if constexpr (inherits_from_base_v<T>) {
return t.cache_size;
}
else {
Expand Down
8 changes: 4 additions & 4 deletions iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ struct field_info {
std::string_view type_name;
};

struct pb_base {
struct base {
virtual void to_pb(std::string &str) {}
virtual void from_pb(std::string_view str) {}
virtual std::vector<std::string_view> get_fields_name() { return {}; }
Expand All @@ -585,7 +585,7 @@ struct pb_base {

*((T *)ptr) = std::move(val);
}
virtual ~pb_base() {}
virtual ~base() {}

private:
template <typename T>
Expand All @@ -608,14 +608,14 @@ struct pb_base {
};

inline std::unordered_map<std::string_view,
std::function<std::shared_ptr<pb_base>()>>
std::function<std::shared_ptr<base>()>>
g_pb_map;

template <typename T>
inline bool register_type() {
#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
if constexpr (std::is_base_of_v<pb_base, T>) {
if constexpr (std::is_base_of_v<base, T>) {
auto it = g_pb_map.emplace(type_string<T>(), [] {
return std::make_shared<T>();
});
Expand Down
70 changes: 63 additions & 7 deletions lang/struct_pb_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ struct_pb 是基于C++17 开发的高性能、易用、header only的protobuf格
```cpp
#include <ylt/struct_pb.hpp>

struct my_struct : struct_pb::pb_base_impl<my_struct> {
struct my_struct {
int x;
bool y;
struct_pb::fixed64_t z;
};
REFLECTION(my_struct, x, y, z);

struct nest : struct_pb::pb_base_impl<nest> {
struct nest {
std::string name;
my_struct value;
int var;
Expand All @@ -29,10 +29,10 @@ REFLECTION(nest, name, value, var);
### 序列化
```cpp
int main() {
nest v{0, "Hi", {0, 1, false, 3}, 5}, v2{};
nest v{"Hi", {1, false, {3}}, 5}, v2{};
std::string s;
struct_pb::to_pb(v, s);
struct_pb::from_pb(v2, s);
iguana::to_pb(v, s);
iguana::from_pb(v2, s);
assert(v.var == v2.var);
assert(v.value.y == v2.value.y);
assert(v.value.z == v2.value.z);
Expand All @@ -53,6 +53,62 @@ message nest {
}
```

## 动态反射
特性:
- 根据对象名称创建实例;
- 获取对象的所有字段名;
- 根据对象实例和字段名获取或设置字段的值

### 根据名称创建对象
```cpp
struct my_struct {
int x;
bool y;
iguana::fixed64_t z;
};
REFLECTION(my_struct, x, y, z);

struct nest1 : public iguana::base_imple<nest1> {
nest1() = default;
nest1(std::string s, my_struct t, int d)
: name(std::move(s)), value(t), var(d) {}
std::string name;
my_struct value;
int var;
};
REFLECTION(nest1, name, value, var);
```
```cpp
std::shared_ptr<base> t = iguana::create_instance("nest1");
```
根据对象nest1创建了实例,返回的是基类指针。

“根据对象名称创建实例” 要求对象必须从iguana::base_impl 派生,如果没有派生则创建实例会抛异常。

### 根据名称设置字段的值
```cpp
auto t = iguana::create_instance("nest1");

std::vector<std::string_view> fields_name = t->get_fields_name();
CHECK(fields_name == std::vector<std::string_view>{"name", "value", "var"});

my_struct mt{2, true, {42}};
t->set_field_value("value", mt);
t->set_field_value("name", std::string("test"));
t->set_field_value("var", 41);
nest1 *st = dynamic_cast<nest1 *>(t.get());
auto p = *st;
std::cout << p.name << "\n";
auto &r0 = t->get_field_value<std::string>("name");
CHECK(r0 == "test");
auto &r = t->get_field_value<int>("var");
CHECK(r == 41);
auto &r1 = t->get_field_value<my_struct>("value");
CHECK(r1.x == 2);
```
“根据对象实例和字段名获取或设置字段的值” 如果字段名不存在则会抛异常;如果设置的值类型和结构体字段类型不相同则会抛异常;需要类型完全一样,不允许隐式转换。比如字段类型是double,但是设置字段的值类型是int也会抛异常,必须显式传double;如果字段类型是std::string, 设置值类型是const char * 同样会报错;如果字段类型是int32_t, 设置值类型是uint_8也会抛异常,因为类型不相同。
## benchmark
在benchmark monster场景下,struct_pb 性能比protobuf 更好,序列化速度是protobuf的2.4倍,反序列化是protobuf的3.4倍。详情可以自行运行struct_pack 中的benchmark复现结果。
Expand Down Expand Up @@ -96,10 +152,10 @@ oneof -> `std::variant <...>`
- 目前还只支持proto3,不支持proto2;
- 目前还没支持反射;
- 还没支持unkonwn字段;
- struct_pb 结构体必须派生于pb_base_impl
- struct_pb 结构体必须派生于base_impl
## roadmap
- 支持proto2;
- 支持反射;
- 支持unkonwn字段;
- 去除struct_pb 结构体必须派生于pb_base_impl的约束
- 去除struct_pb 结构体必须派生于base_impl的约束
2 changes: 1 addition & 1 deletion test/proto/unittest_proto3.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

#define PB_CHECK assert
#define PUBLIC(T) : public iguana::pb_base_impl<T>
#define PUBLIC(T) : public iguana::base_impl<T>

// define the struct as msg in proto
namespace stpb {
Expand Down
Loading

0 comments on commit 5154d54

Please sign in to comment.