Skip to content

Commit

Permalink
增加了继承结构的反射REFLECTION
Browse files Browse the repository at this point in the history
  • Loading branch information
guanshoukui committed Mar 5, 2024
1 parent 873ebea commit d722a00
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 194 deletions.
35 changes: 22 additions & 13 deletions src/struct_pb/protoc-plugin/FileGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void FileGenerator::generate_header(google::protobuf::io2::Printer *p) {
generate_dependency_includes(p); // 包含的其他头文件
generate_ns_open(p); // 写命名空间
generate_shared_header_code(p);
// generate_message_tostring_func_definitions(p);
generate_message_tostring_func_definitions(p);
generate_message_struct2class_definitions(p);
generate_ns_close(p); // 关闭命名空间
// generate_message_struct_pb_func_definitions(p);
Expand Down Expand Up @@ -233,29 +233,38 @@ void FileGenerator::generate_message_tostring_func_definitions(
std::vector<const Descriptor *> msgs = flatten_messages_in_file(file_);
Formatter format(p);


for (auto msg : msgs) {
if(is_eigen_type(msg)) {continue;}
if (is_eigen_type(msg)) {
continue;
}

auto raw_name = msg->name();
auto name = qualified_class_name(msg, options_);
format("// $1$\n", name);
format("REFLECTION($1$", raw_name);
for(int i = 0; i < msg->field_count();++i)
{
format(", $1$", msg->field(i)->name());
for (int i = 0; i < msg->field_count(); ++i) {
auto fd = msg->field(i);
if (fd->options().GetExtension(inherits_from)) {
auto fd2 = fd->message_type();
for (int j = 0; j < fd2->field_count(); ++j) {
format(", $1$", fd2->field(j)->name());
}
}
else {
format(", $1$", fd->name());
}
}
format(");\n");
}

// enum
std::vector<const EnumDescriptor *> enums = flatten_enums_in_file(file_);
for (auto single_enum : enums) {
auto name = qualified_enum_name(single_enum, options_);
format("// $1$\n", name);
format("extern std::map<$1$, std::string> k$2$_EnumToString;", name, resolve_keyword(single_enum->name()));
format("\n");
}
// std::vector<const EnumDescriptor *> enums = flatten_enums_in_file(file_);
// for (auto single_enum : enums) {
// auto name = qualified_enum_name(single_enum, options_);
// format("// $1$\n", name);
// format("extern std::map<$1$, std::string> k$2$_EnumToString;", name, resolve_keyword(single_enum->name()));
// format("\n");
// }

}

Expand Down
99 changes: 99 additions & 0 deletions src/struct_pb/protoc-plugin/example/iguana/detail/utf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <cassert>
#include <stdexcept>

#include "iguana/define.h"

namespace iguana {
// https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/reader.h
template <typename Ch = char, typename It>
Expand Down Expand Up @@ -48,4 +50,101 @@ inline void encode_utf8(OutputStream &os, unsigned codepoint) {
os.push_back(static_cast<Ch>(0x80 | (codepoint & 0x3F)));
}
}

// https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/encodings.h
static inline unsigned char GetRange(unsigned char c) {
static const unsigned char type[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3,
11, 6, 6, 6, 5, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8,
};
return type[c];
}

// https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/encodings.h
template <typename Ch = char, typename It>
inline bool decode_utf8(It &&it, unsigned &codepoint) {
auto c = *(it++);
bool result = true;
auto copy = [&]() IGUANA__INLINE_LAMBDA {
c = *(it++);
codepoint = (codepoint << 6) | (static_cast<unsigned char>(c) & 0x3Fu);
};
auto trans = [&](unsigned mask) IGUANA__INLINE_LAMBDA {
result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0);
};
auto tail = [&]() IGUANA__INLINE_LAMBDA {
copy();
trans(0x70);
};
if (!(c & 0x80)) {
codepoint = static_cast<unsigned char>(c);
return true;
}
unsigned char type = GetRange(static_cast<unsigned char>(c));
if (type >= 32) {
codepoint = 0;
}
else {
codepoint = (0xFFu >> type) & static_cast<unsigned char>(c);
}
switch (type) {
case 2:
tail();
return result;
case 3:
tail();
tail();
return result;
case 4:
copy();
trans(0x50);
tail();
return result;
case 5:
copy();
trans(0x10);
tail();
tail();
return result;
case 6:
tail();
tail();
tail();
return result;
case 10:
copy();
trans(0x20);
tail();
return result;
case 11:
copy();
trans(0x60);
tail();
tail();
return result;
default:
return false;
}
}

} // namespace iguana
Loading

0 comments on commit d722a00

Please sign in to comment.