Protobuf的反射机制输出或格式化数据
protobuf的功能,这里不介绍,自己查看相应的官方文档。如以下链接:http://www.searchtb.com/2012/09/protocol-buffers.htmlhttp://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html以下是自己编写的格式化输出,protobuf代码,仅供参考:/
·
protobuf的功能,这里不介绍,自己查看相应的官方文档。
如以下链接:
http://www.searchtb.com/2012/09/protocol-buffers.html
http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
以下是自己编写的格式化输出,protobuf代码,仅供参考:
/*
* DBClentWrapperProcedure.hpp
*
* Created on: 2015年11月16日
* Author: liug
*/
#ifndef _GETPROTOBUFMESSAGE_H_
#define _GETPROTOBUFMESSAGE_H_
#include "google/protobuf/message.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
using namespace google::protobuf;
void ConvertToString(std::string& str,uint64_t value)
{
char szBuf[64] = {0};
snprintf(szBuf, sizeof(szBuf), "%lu", value);
str.assign(szBuf,strlen(szBuf));
};
void ConvertToString(std::string& str,int32_t value)
{
char szBuf[32] = {0};
snprintf(szBuf, sizeof(szBuf), "%d", value);
str.assign(szBuf,strlen(szBuf));
};
void ConvertToString(std::string& str,uint32_t value)
{
char szBuf[32] = {0};
snprintf(szBuf, sizeof(szBuf), "%u", value);
str.assign(szBuf,strlen(szBuf));
};
void ConvertToString(std::string& str,int64_t value)
{
char szBuf[64] = {0};
snprintf(szBuf, sizeof(szBuf), "%lu", value);
str.assign(szBuf,strlen(szBuf));
};
void ConvertToString(std::string& str,std::string& value)
{
str = value;
};
void ConvertToString(std::string& str,std::string value)
{
str = value;
};
#define GET_PRIMITIVE_TYPE(TYPE, CPPTYPE_METHOD) \
case FieldDescriptor::CPPTYPE_##TYPE: \
{\
ConvertToString(strValue, pReflection->Get##CPPTYPE_METHOD(Message, pFieldDescriptor));\
break;\
}
#define GET_REPEATED_PRIMITIVE_TYPE(TYPE, CPPTYPE_METHOD,INDEX) \
case FieldDescriptor::CPPTYPE_##TYPE: \
{\
ConvertToString(strValue, pReflection->GetRepeated##CPPTYPE_METHOD(Message, pFieldDescriptor,INDEX));\
break;\
}
namespace ServerLib
{
template <class ProtoType>
void PrintBaseInfoNode(ProtoType& Message,int iindex)
{
if(iindex < 0)
{
return;
}
const Reflection *pReflection = Message.GetReflection();
if(!pReflection)
{
printf("pReflection is null\n");
return;
}
const FieldDescriptor* pFieldDescriptor = NULL;
int iFiledCount = (int)Message.GetDescriptor()->field_count();
for (int i = 0; i < iFiledCount; i++)
{
pFieldDescriptor = Message.GetDescriptor()->field(i);
if(!pFieldDescriptor)
{
continue;
}
if(iindex == (int)pFieldDescriptor->number())
{
//printf("Type:%d,repeated:%d\n",pFieldDescriptor->cpp_type(),pFieldDescriptor->is_repeated());
const std::string& strFieldName = pFieldDescriptor->name();
std::string strValue;
int iFieldCount = 0;
//有重复值,打印所有的重复值
if(pFieldDescriptor->is_repeated())
{
strValue.clear();
iFieldCount = (int)pReflection->FieldSize(Message, pFieldDescriptor);
//printf("iFieldCount=%d\n",iFieldCount);
for(int i = 0; i < iFieldCount;i++)
{
switch (pFieldDescriptor->cpp_type())
{
GET_REPEATED_PRIMITIVE_TYPE(INT32, Int32,i);
GET_REPEATED_PRIMITIVE_TYPE(UINT32, UInt32,i);
GET_REPEATED_PRIMITIVE_TYPE(INT64, Int64,i);
GET_REPEATED_PRIMITIVE_TYPE(UINT64, UInt64,i);
GET_REPEATED_PRIMITIVE_TYPE(STRING, String,i);
case FieldDescriptor::CPPTYPE_MESSAGE:
{
::google::protobuf::TextFormat::PrintToString(pReflection->GetRepeatedMessage(Message, pFieldDescriptor,i), &strValue);
break;
}
default:
printf("Type:%d\n",pFieldDescriptor->cpp_type());
break;
}
printf("%s:%s\n",strFieldName.c_str(),strValue.c_str());
}
}
else
{
//没有重复值,则打印值
switch (pFieldDescriptor->cpp_type())
{
GET_PRIMITIVE_TYPE(INT32, Int32);
GET_PRIMITIVE_TYPE(UINT32, UInt32);
GET_PRIMITIVE_TYPE(INT64, Int64);
GET_PRIMITIVE_TYPE(UINT64, UInt64);
GET_PRIMITIVE_TYPE(STRING, String);
case FieldDescriptor::CPPTYPE_MESSAGE:
{
::google::protobuf::TextFormat::PrintToString(pReflection->GetMessage(Message, pFieldDescriptor), &strValue);
//pReflection->GetMessage(message, pFieldDescriptor);
break;
}
default:
printf("default\n");
break;
}
printf("%s:%s\n",strFieldName.c_str(),strValue.c_str());
}
<span style="font-family: Arial, Helvetica, sans-serif;"> break;</span>
}
}
};
};
#endif
更多推荐
已为社区贡献1条内容
所有评论(0)