106 lines
3.0 KiB
C++
106 lines
3.0 KiB
C++
|
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
|
|||
|
//
|
|||
|
#include <stdio.h>
|
|||
|
#include "protobuf/include/google/protobuf/message_lite.h"
|
|||
|
#include "protobuf/include/google/protobuf/descriptor.h"
|
|||
|
#include "protobuf/bin/ProtoOption.pb.h"
|
|||
|
using namespace OPTIONTEST;
|
|||
|
using namespace std;
|
|||
|
|
|||
|
struct PB_Message
|
|||
|
{
|
|||
|
string pbName;
|
|||
|
map<string, string> code;
|
|||
|
};
|
|||
|
using MessageList = vector<PB_Message>;
|
|||
|
struct PBInfo
|
|||
|
{
|
|||
|
std::string pbName = ("");
|
|||
|
int pbNum = 0;
|
|||
|
std::map<std::string, std::string> field;
|
|||
|
};
|
|||
|
void Print(PBInfo& info)
|
|||
|
{
|
|||
|
cout << " PBName = " << info.pbName << endl;
|
|||
|
for (auto& f : info.field)
|
|||
|
{
|
|||
|
cout << " field Name = " << f.first << " comment = " << f.second << endl;
|
|||
|
}
|
|||
|
}
|
|||
|
template <typename T>
|
|||
|
static void parser(const T& obj, PBInfo& info)
|
|||
|
{
|
|||
|
const google::protobuf::Descriptor* descriptor = obj.GetDescriptor();
|
|||
|
info.pbName = descriptor->options().GetExtension(messageOption).s();
|
|||
|
info.pbNum = descriptor->options().GetExtension(messageOption).n();
|
|||
|
|
|||
|
for (int i = 0; i < descriptor->field_count(); ++i)
|
|||
|
{
|
|||
|
const google::protobuf::FieldDescriptor* fieldDescriptor = descriptor->field(i);
|
|||
|
auto fieldComment = fieldDescriptor->options().GetExtension(filedOptipn);
|
|||
|
info.field[fieldDescriptor->name()] = fieldComment.s();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
const google::protobuf::DescriptorPool* pool = google::protobuf::DescriptorPool::generated_pool();
|
|||
|
std::vector<std::string> file_names;
|
|||
|
string fileName = "ProtoOption.proto";
|
|||
|
const google::protobuf::FileDescriptor* fileDescriptor = pool->FindFileByName(fileName);
|
|||
|
auto opts = fileDescriptor->options().GetExtension(File_Option);
|
|||
|
cout << "File Option is : " << opts << endl;;
|
|||
|
cout << endl;
|
|||
|
|
|||
|
cout << " 无注释pb " << endl;
|
|||
|
Non_Option_Message pb1;
|
|||
|
PBInfo info1;
|
|||
|
parser(pb1, info1);
|
|||
|
Print(info1);
|
|||
|
cout << endl;
|
|||
|
|
|||
|
cout << " 无注释pb size = " << endl;
|
|||
|
Single_Option_Message pb2;
|
|||
|
PBInfo info2;
|
|||
|
parser(pb2, info2);
|
|||
|
Print(info2);
|
|||
|
cout << endl;
|
|||
|
|
|||
|
cout << " 无注释pb size = " << endl;
|
|||
|
Full_Option_Message pb3;
|
|||
|
PBInfo info3;
|
|||
|
parser(pb3, info3);
|
|||
|
Print(info3);
|
|||
|
|
|||
|
/*
|
|||
|
Non_Option_Message pb1;
|
|||
|
pb1.set_id(1);
|
|||
|
pb1.set_sourceid("2");
|
|||
|
pb1.set_d(1.11);
|
|||
|
pb1.set_b(true);
|
|||
|
auto p1 = pb1.mutable_fcff();
|
|||
|
p1->set_value(1.22);
|
|||
|
cout << " 无注释pb size = " << pb1.ByteSize() << endl;
|
|||
|
|
|||
|
Single_Option_Message pb2;
|
|||
|
pb2.set_id(1);
|
|||
|
pb2.set_sourceid("2");
|
|||
|
pb2.set_d(1.11);
|
|||
|
pb2.set_b(true);
|
|||
|
auto p2 = pb2.mutable_fcff();
|
|||
|
p2->set_value(1.22);
|
|||
|
cout << " 单注释pb size = " << pb2.ByteSize() << endl;
|
|||
|
|
|||
|
Full_Option_Message pb3;
|
|||
|
pb3.set_id(1);
|
|||
|
pb3.set_sourceid("2");
|
|||
|
pb3.set_d(1.11);
|
|||
|
pb3.set_b(true);
|
|||
|
auto p3 = pb3.mutable_fcff();
|
|||
|
p3->set_value(1.22);
|
|||
|
cout << " 全注释pb size = " << pb3.ByteSize() << endl;
|
|||
|
*/
|
|||
|
|
|||
|
system("pause");
|
|||
|
return 0;
|
|||
|
}
|