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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值