日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ProtoBuf(Google Protocol Buffers)—— 反射原理以及反射具体流程介绍

發布時間:2024/1/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ProtoBuf(Google Protocol Buffers)—— 反射原理以及反射具体流程介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ProtoBuf—— 反射原理解析

  • ProtoBuf—— 反射原理
    • 1、反射原理
      • 1.1、反射機制的背景
      • 1.2、定義
      • 1.3、反射原理關注的一些問題
      • 1.4、反射原理的優勢和應用
    • 2、ProtoBuf反射原理——獲取并改造 元信息
      • 2.1 、 .proto 文件
      • 2.2 、 反射原理過程
      • 2.3 、 反射相關的類和API
        • 2.3.1、google::protobuf::Message
        • 2.3.2、 google::protobuf::Descriptor
        • 2.3.3、 google::protobuf::Reflection
        • 2.3.4、 google::protobuf::FieldDescriptor
        • 2.3.5、 其他相關接口
          • 2.3.5.1、DescriptorPool(元信息池)
          • 2.3.5.2、DescriptorDatabase( .proto 文件庫)
          • 2.3.5.3、MessageFactory( 實例工廠)
      • 2.4 、 反射原理過程具體解析
        • 2.4.1、獲取元信息
        • 2.4.2、創建和獲取實例(查表)
        • 2.4.3、實例對象的讀寫
      • 2.5 、 反射原理具體實例介紹
  • 參考

ProtoBuf—— 反射原理

1、反射原理

1.1、反射機制的背景

有關某個外部世界表示”的計算過程, 并通過它來對那個外部世界進行推理; 那么我們也可以構造能夠對自身表示和計算進行推理的計算過程,它包含負責管理有關自身的操作和結構表示的內部過程。
—— 1982年 Smith, Brian Cantwell 博士論文首次提出

  • 所謂編程實際上就是在構造 “關于外部世界” 的計算過程。如果用 F 表示這個構造過程,用 X表示外部世界,那么編寫一個計算系統可表示為 F(X)。
  • 我們完全可以構造對上述過程本身進行描述和推理的計算過程。即將 F(X) 視為新的 “世界” 和 研究對象,構造 F(F(X))。
    • 所以我們為了構建和改造世界,前提需要充分認識世界

計算機程序在運行時可以訪問、檢測和修改它本身狀態或行為
—— 反射 (計算機科學) Wikipedia

我們平時編寫的計算系統是面向特定領域的(通常是面向現實建模), 系統中包含 用來描述領域中的實體 和 實體間關系 的數據結構以及處理這些數據結構的規則。那么反射系統面向領域便是這個系統本身。

1.2、定義

在對程序本身進行構建的同時,在運行時可以訪問和修改自身狀態和行為,這建立在對系統系統元信息充分感知的基礎上。

系統元信息:

元信息:即系統自描述信息,用于描述系統本身。舉例來講,即系統有哪些類?類中有哪些字段、哪些方法?字段屬于什么類型、方法又有怎樣的參數和返回值?

反射則提供了封裝程序集、模塊和類型的對象。您可以使用反射動態地創建類型的實例,將類型綁定到現有對象,或從現有對象中獲取類型。然后,可以調用類型的方法或訪問其字段和屬性。

1.3、反射原理關注的一些問題

  • 1、如何在程序運行過程中通過類型名字(一個字符串,合法但是內容在編譯期間未知,比如是在配置文件中獲取的)創建出類型對象.
  • 2、如何在程序運行過程中通過對象和對象的屬性的名字(一個字符串,合法但是內容在編譯期間未知,比如是通過通訊包獲取的)獲取,修改對應屬性.
  • 3、如何在程序運行過程中通過對象和對象方法的名字(一個字符串,合法但是內容在編譯期間未知,比如是從用戶輸入獲取的)調用對應的方法.

1.4、反射原理的優勢和應用

優勢:
反射在運行時檢測或修改程序行為的程序中,提高了程序的靈活性和擴展性,降低耦合性,提高自適應能力。它允許程序創建和控制任何類的對象,無需提前硬編碼目標類.
應用:

  • 1、多并發的服務器框架
  • 2、網絡序列化數據傳輸方法等
  • 3、微軟的MFC

在開發編輯器的階段,并沒有相應的類,但是使用者想通過在編輯器中傳入類的名字,然后在開發中,根據編輯器傳入的類名,新建一個類去實現的話,這種方法非常適用。

2、ProtoBuf反射原理——獲取并改造 元信息

2.1 、 .proto 文件

ProtoBuf 反射所需的元信息在哪?答案便是使用 ProtoBuf 的第一步就會接觸到的:.proto 文件。

  • .proto 文件
    • 使用 ProtoBuf 內置的工具 protoc 編譯器編譯,protoc 將 .proto 文件內容編碼并寫入生成的代碼中(.pb.cc文件)
    • 使用 ProtoBuf 提供的編譯 API 在運行時手動(指編碼)解析 .proto 文件內容。實際上 protoc底層調用的也正是這個編譯 API。
// 在 xxx.proto 文件中定義 Example1 message message Example1 {optional string stringVal = 1;optional bytes bytesVal = 2;message EmbeddedMessage {int32 int32Val = 1;string stringVal = 2;}optional EmbeddedMessage embeddedExample1 = 3;repeated int32 repeatedInt32Val = 4;repeated string repeatedStringVal = 5; } //提供我們數據元信息的過程,這些元信息包括數據由哪些字段構成,字段又屬于什么類型以及字段之間的組合關系等

2.2 、 反射原理過程

無論 .proto 文件來源于何處,我們都需要對其做進一步的處理,將其解析成內存對象,并構建其與實例的映射,同時也要計算每個字段的內存偏移:

  • 1、提供 .proto (范指 ProtoBuf Message 語法描述的元信息)
  • 2、解析 .proto 構建 FileDescriptor、FieldDescriptor 等,即 .proto 對應的內存模型(對象)
  • 3、之后每創建一個實例,就將其存到相應的實例池中
  • 4、將 Descriptor 和 instance 的映射維護到表中備查
  • 5、通過 Descriptor 可查到相應的 instance,又由于了解 instance中字段類型(FieldDescriptor),所以知道字段的內存偏移,那么就可以訪問或修改字段的值
    • 任何一個對象最終都對應一段內存,有內存起始(start_addr)和結束地址, 而對象的每一個屬性,都位于start_addr+$offset ,所以當對象和對應屬性的offset已知的時候, 屬性的內存地址也就是可以獲取的。

2.3 、 反射相關的類和API

  • 1、google::protobuf::Message
  • 2、google::protobuf::Descriptor
  • 3、google::protobuf::Reflection
  • 4、google::protobuf::FieldDescriptor

    Person是自定義的protobuf類型

2.3.1、google::protobuf::Message

  • Person是自定義的pb類型,繼承自Message.
  • MessageLite作為Message基類,更加輕量級一些。
  • 通過Message的兩個接口GetDescriptor/GetReflection,可以獲取該類型對應的Descriptor/Reflection。
//google::protobuf::Message const Descriptor* GetDescriptor() const; const Reflection* GetReflection() const;

2.3.2、 google::protobuf::Descriptor

Descriptor是對message類型定義的描述,包括message的名字、所有字段的描述、原始的proto文件內容等。

//Descriptor //返回message的屬性個數 int field_count() const; //返回第index個屬性 const FieldDescriptor* field(int index) const; //通過proto文件里面定義的tag, 返回屬性 const FieldDescriptor* FindFieldByNumber(int number) const; //通過屬性名,返回屬性 const FieldDescriptor* FindFieldByName(const std::string& name) const; //通過小寫的屬性名,返回屬性 const FieldDescriptor* FindFieldByLowercaseName(const std::string& lowercase_name) const; //通過駝峰屬性名,返回屬性 const FieldDescriptor* FindFieldByCamelcaseName(const std::string& camelcase_name) const; //返回枚舉類型的數量 int enum_type_count() const; //返回第index個枚舉類型屬性 const EnumDescriptor* enum_type(int index) const; //通過名稱,返回枚舉屬性 const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; //通過屬性值,返回枚舉值屬性 const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const;

2.3.3、 google::protobuf::Reflection

  • Reflection:接口類,主要提供了動態讀寫pb字段的接口,對pb對象的自動讀寫主要通過該類完成。
  • 提供方法來動態訪問/修改message中的field的接口類,對每種類型,Reflection都提供了一個單獨的接口用于讀寫字段對應的值。
//檢查message中的非repeated屬性是否存在 bool HasField(const Message& message, const FieldDescriptor* field) const; //返回message中repeated屬性的長度 int FieldSize(const Message& message, const FieldDescriptor* field) const; //清除message中的屬性,非repeated屬性HashField返回false,repeated屬性FieldSize返回0 void ClearField(Message* message, const FieldDescriptor* field) const; //返回message中除Unknown的屬性,包括所有HashField返回為true,FieldSize非0的屬性 void ListFields(const Message& message,std::vector<const FieldDescriptor*>* output) const; //返回message對應屬性的,對應類型的返回值 int32 GetInt32(const Message& message, const FieldDescriptor* field) const;int64 GetInt64(const Message& message, const FieldDescriptor* field) const;uint32 GetUInt32(const Message& message, const FieldDescriptor* field) const;uint64 GetUInt64(const Message& message, const FieldDescriptor* field) const;float GetFloat(const Message& message, const FieldDescriptor* field) const;double GetDouble(const Message& message, const FieldDescriptor* field) const;bool GetBool(const Message& message, const FieldDescriptor* field) const;std::string GetString(const Message& message,const FieldDescriptor* field) const;const EnumValueDescriptor* GetEnum(const Message& message,const FieldDescriptor* field) const;//返回message,屬性的value的int類型值 int GetEnumValue(const Message& message, const FieldDescriptor* field) const; const Message& GetMessage(const Message& message,const FieldDescriptor* field,MessageFactory* factory = nullptr) const;//設置message,對應屬性的對應類型的值 void SetInt32(Message* message, const FieldDescriptor* field,int32 value) const;void SetInt64(Message* message, const FieldDescriptor* field,int64 value) const;void SetUInt32(Message* message, const FieldDescriptor* field,uint32 value) const;void SetUInt64(Message* message, const FieldDescriptor* field,uint64 value) const;void SetFloat(Message* message, const FieldDescriptor* field,float value) const;void SetDouble(Message* message, const FieldDescriptor* field,double value) const;void SetBool(Message* message, const FieldDescriptor* field,bool value) const;void SetString(Message* message, const FieldDescriptor* field,const std::string& value) const;void SetEnum(Message* message, const FieldDescriptor* field,const EnumValueDescriptor* value) const;void SetEnumValue(Message* message, const FieldDescriptor* field,int value) const;Message* MutableMessage(Message* message, const FieldDescriptor* field,MessageFactory* factory = nullptr) const;//返回message對應屬性repeated字段的index位置的值 int32 GetRepeatedInt32(const Message& message, const FieldDescriptor* field,int index) const;int64 GetRepeatedInt64(const Message& message, const FieldDescriptor* field,int index) const;uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field,int index) const;uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field,int index) const;float GetRepeatedFloat(const Message& message, const FieldDescriptor* field,int index) const;double GetRepeatedDouble(const Message& message, const FieldDescriptor* field,int index) const;bool GetRepeatedBool(const Message& message, const FieldDescriptor* field,int index) const;std::string GetRepeatedString(const Message& message,const FieldDescriptor* field, int index) const;const EnumValueDescriptor* GetRepeatedEnum(const Message& message,const FieldDescriptor* field,int index) const;int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field,int index) const;const Message& GetRepeatedMessage(const Message& message,const FieldDescriptor* field,int index) const; //設置message的repeated屬性的index位置對應類型的value的值 void SetRepeatedInt32(Message* message, const FieldDescriptor* field,int index, int32 value) const;void SetRepeatedInt64(Message* message, const FieldDescriptor* field,int index, int64 value) const;void SetRepeatedUInt32(Message* message, const FieldDescriptor* field,int index, uint32 value) const;void SetRepeatedUInt64(Message* message, const FieldDescriptor* field,int index, uint64 value) const;void SetRepeatedFloat(Message* message, const FieldDescriptor* field,int index, float value) const;void SetRepeatedDouble(Message* message, const FieldDescriptor* field,int index, double value) const;void SetRepeatedBool(Message* message, const FieldDescriptor* field,int index, bool value) const;void SetRepeatedString(Message* message, const FieldDescriptor* field,int index, const std::string& value) const;void SetRepeatedEnum(Message* message, const FieldDescriptor* field,int index, const EnumValueDescriptor* value) const;void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,int index, int value) const;Message* MutableRepeatedMessage(Message* message,const FieldDescriptor* field,int index) const;

2.3.4、 google::protobuf::FieldDescriptor

FieldDescriptor描述message中的單個字段,例如字段名,字段屬(optional/required/repeated)等。對于proto定義里的每種類型,都有一種對應的C++類型,

//是否是必須的bool is_required() const; // shorthand for label() == LABEL_REQUIRED//是否是可選的bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL//是否是repeatedbool is_repeated() const; // shorthand for label() == LABEL_REPEATED//是否可以packbool is_packable() const; // shorthand for is_repeated() &&// IsTypePackable(type())//是否是packedbool is_packed() const; // shorthand for is_packable() &&// options().packed()//是否是messagebool is_map() const; // shorthand for type() == TYPE_MESSAGE &&// message_type()->options().map_entry()//屬性在message的索引位置int index() const;

2.3.5、 其他相關接口

2.3.5.1、DescriptorPool(元信息池)
  • 任何時候想要查詢一個Descriptor , 都是去DescriptorPool里面查詢。
  • 緩存所有查詢的文件的Descriptor 。對外提供了諸如 FindServiceByName、FindMessageTypeByName等各類接口以便外部查詢所需的元信息。
  • 當 DescriptorPool 不存在時需要查詢的元信息時,將進一步到 DescriptorDatabase 中去查找。
2.3.5.2、DescriptorDatabase( .proto 文件庫)
  • DescriptorDatabase是一個純虛基類,描述了一系列符合通過名字(文件名,符號名。。。)
    來獲取FileDescriptorProto的接口
  • 可從硬編碼或磁盤中查詢對應名稱的 .proto 文件內容,解析后返回查詢需要的元信息。
// 這里我干掉了里面的英文注釋. class LIBPROTOBUF_EXPORT DescriptorDatabase {public:inline DescriptorDatabase() {}virtual ~DescriptorDatabase();virtual ~DescriptorDatabase();// 通過文件名字找.virtual bool FindFileByName(const string& filename,FileDescriptorProto* output) = 0;// 通過符號名字找.virtual bool FindFileContainingSymbol(const string& symbol_name,FileDescriptorProto* output) = 0;// 通過擴展信息找.virtual bool FindFileContainingExtension(const string& containing_type,int field_number,FileDescriptorProto* output) = 0;// 通過擴展信息的tag數字找...virtual bool FindAllExtensionNumbers(const string& /* extendee_type */,vector<int>* /* output */) {return false;}private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorDatabase); };

核心的兩個派生類是 :

  • EncodedDescriptorDatabase
    • 支持DescriptorDatabase的全部接口
    • 接收序列化之后的FileDescriptorProto, 保存在map中備查.
    • 這個類對應著預先編譯鏈接好的那些類型的反射機制。
  • SourceTreeDescriptorDatabase
    • 僅支持DescriptorDatabase的FindFileByName接口。其余直接返回false.
    • 每次查詢某個文件都是從磁盤讀入proto的源文件,編譯解析后返回對應的FileDescriptorProto .
    • 這個類對應著動態編譯proto源文件的時候的反射機制.
2.3.5.3、MessageFactory( 實例工廠)

任何時候想要獲取一個類型的instance , 都要去MessageFactory里面獲取。
MessageFactory 是一個純虛的基類,定義了通過Descripor來獲取對應類型instance的接口.

{public:inline MessageFactory() {}virtual ~MessageFactory();// 了通過Descripor來獲取對應類型instance 的接口virtual const Message* GetPrototype(const Descriptor* type) = 0;// 這個是獲取編譯鏈接好的那些類型的factory單例的入口.static MessageFactory* generated_factory();// 這個是對應的像上面哪個單例內填裝數據的接口,protoc自動生成的文件都會有調用.static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,const Message* prototype);private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory); }

同樣有兩個核心的派生類

  • GeneratedMessageFactory
    • 一個map , 保存著Descriptor和Message
    • 這個類對應著預先編譯鏈接好的那些類型的反射機制。
  • DynamicMessageFactory
    • 有簡單的緩存,保存自己解析過的Descriptor`` < /li >
    • < li >可以通過Descriptor,動態的基于內存構造出一個Message

2.4 、 反射原理過程具體解析

/* 反射創建實例 */ auto descriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName("Dog"); auto prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor); auto instance = prototype->New();/* 反射相關接口 */ auto reflecter = instance.GetReflection(); auto field = descriptor->FindFieldByName("name"); reflecter->SetString(&instance, field, "雞你太美") ;// 獲取屬性的值. std::cout<<reflecter->GetString(instance , field)<< std::endl ; return 0 ;

2.4.1、獲取元信息

  • 數據存儲在哪里
    • 1、所有的Descriptor存儲在單例的DescriptorPool 中。
      • google::protobuf::DescriptorPool::generated_pool()來獲取他的指針。
    • 2、所有的instance 存儲在單例的MessageFactory中。
      • google::protobuf::MessageFactory::generated_factory()來獲取他的指針。
    • 3、將所有的Descriptor & instance 提前維護到表中備查

DescriptorPool 相當于緩存了文件的 Descriptor(底層使用 Map),查詢時將先到緩存中查詢,如果未能找到再進一步到 DB 中(即 DescriptorDatabase)查詢,此時可能需要從磁盤中讀取文件內容,然后再解析成 Descriptor 返回,這里需要消耗一定的時間。

不難看出,DescriptorPool 和 DescriptorDatabase 通過緩存機制提高了反射運行效率,但這只是反射工程實現上的一種優化,我們更感興趣的應該是 Descriptor 的來源。

DescriptorDatabase 從磁盤中讀取 .proto 內容并解析成 Descriptor 這一來源很容易理解,但我們大多數時候并不會采用這種方式,反射時也不會去讀取 .proto 文件。那么我們的 .proto 內容在哪?

實際上我們在使用 protoc 生成 xxx.pb.cc 和 xxx.pb.h 文件時,其中不僅僅包含了讀寫數據的接口,還包含了 .proto 文件內容。閱讀任意一個 xxx.pb.cc 的內容,你可以看到如下類似代碼

static void AddDescriptorsImpl() {InitDefaults();// .proto 內容static const char descriptor[] GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {"\n\022single_int32.proto\"\035\n\010Example1\022\021\n\010int3""2Val\030\232\005 \001(\005\" \n\010Example2\022\024\n\010int32Val\030\377\377\377\377""\001 \003(\005b\006proto3"};// 注冊 descriptor::google::protobuf::DescriptorPool::InternalAddGeneratedFile(descriptor, 93);// 注冊 instance::google::protobuf::MessageFactory::InternalRegisterGeneratedFile("single_int32.proto", &protobuf_RegisterTypes); }

其中 descriptor 數組存儲的便是 .proto 內容。這里當然不是簡單的存儲原始文本字符串,而是經過了 SerializeToString 序列化處理,而后將結果以硬編碼的形式保存在 xxx.pb.cc 中

硬編碼的 .proto 元信息內容將以懶加載(類似于單例模式中的懶漢模式)的方式(被調用時才觸發)被 DescriptorDatabase 加載、解析,并緩存到 DescriptorPool 中。

2.4.2、創建和獲取實例(查表)

根據 MessageFactory 獲得了一個實例。MessageFactory 是實例工廠,對外提供了根據元信息 descriptor 獲取相應實例的能力。

// 注冊對應 descriptor 的 instance 到 MessageFactory // InternalRegisterGeneratedFile 函數內部,會將創建一個實例并做好 descriptor 與 instance 的映射 // xxx 應該替換為文件名,根據自己的需求取`在這里插入代碼片` namespace {//! 將本文件內的全部類型的instance注冊進入MessageFactory的接口. void protobuf_RegisterTypes(const ::std::string&) {// 初始化本文件的reflection數據.protobuf_AssignDescriptorsOnce();::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(Test_descriptor_, &Test::default_instance()); } //! 本文件的初始接口. void protobuf_AddDesc_xxx_2eproto() {static bool already_here = false;if (already_here) return;already_here = true;GOOGLE_PROTOBUF_VERIFY_VERSION;// 注冊本文件的Descriptor包. 這樣就可以用名字通過generated_pool獲取對應的Descriptor。::google::protobuf::DescriptorPool::InternalAddGeneratedFile("\n\013xxx.proto\022\001T\"\022\n\004Test\022\n\n\002id\030\001 \001(\005", 36);// 將本文件的類型instance注冊接口注冊給MessageFactory.// 這里注冊接口是為了實現類型的lazy注冊。如果沒有使用請求某個文件的類型,就不注冊對應文件的類型。::google::protobuf::MessageFactory::InternalRegisterGeneratedFile("xxx.proto", &protobuf_RegisterTypes);// 構造并且初始化全部instance.Test::default_instance_ = new Test();Test::default_instance_->InitAsDefaultInstance();// 注冊清理接口.::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_xxx_2eproto); } //! 下面利用全局變量的構造函數確保main函數執行之前數據已經進行注冊. struct StaticDescriptorInitializer_xxx_2eproto {StaticDescriptorInitializer_xxx_2eproto() {protobuf_AddDesc_xxx_2eproto();} } static_descriptor_initializer_xxx_2eproto_; }

每次構建實例后,都將 descriptor 和 instance 維護到一個 _table 中,即映射表以便獲取。后續所謂通過反射獲得某個類的某個實例子,實際就是查表的過程。

2.4.3、實例對象的讀寫

實例對象的 reflection 里面存儲了對象屬性的偏移地址,而這些信息其實與 .proto 內容信息一樣,在 protoc 編譯時通過解析 proto 文件內容獲得且記錄在 xxx.pb.cc 中。

有了屬性的內存偏移,自然可以對屬性進行讀寫操作

  • 任何一個對象最終都對應一段內存,有內存起始(start_addr)和結束地址, 而對象的每一個屬性,都位于start_addr+$offset ,所以當對象和對應屬性的offset已知的時候, 屬性的內存地址也就是可以獲取的。
  • GeneratedMessageReflection 的填裝和獲取
    對于每一個message , 都有一個對應的GeneratedMessageReflection 對象.
    這個對象保存了對應message反射操作需要的信息.
//!初始化本文件的所有GeneratedMessageReflection對象. void protobuf_AssignDesc_xxx_2eproto() {protobuf_AddDesc_xxx_2eproto();const ::google::protobuf::FileDescriptor* file =::google::protobuf::DescriptorPool::generated_pool()->FindFileByName("xxx.proto");GOOGLE_CHECK(file != NULL);Test_descriptor_ = file->message_type(0);static const int Test_offsets_[1] = {//這里在計算屬性的內存偏移.GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Test, id_),};// 這里是個test包填裝的GeneratedMessageReflection對象.Test_reflection_ =new ::google::protobuf::internal::GeneratedMessageReflection(Test_descriptor_,Test::default_instance_,Test_offsets_,GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Test, _has_bits_[0]),GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Test, _unknown_fields_),-1,::google::protobuf::DescriptorPool::generated_pool(),::google::protobuf::MessageFactory::generated_factory(),sizeof(Test)); } inline void protobuf_AssignDescriptorsOnce() {::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,&protobuf_AssignDesc_xxx_2eproto); }// message.h 中 message的基本接口. virtual const Reflection* GetReflection() const {return GetMetadata().reflection; } // 每個message獲取自己基本信息的接口. ::google::protobuf::Metadata Test::GetMetadata() const {protobuf_AssignDescriptorsOnce();::google::protobuf::Metadata metadata;metadata.descriptor = Test_descriptor_;metadata.reflection = Test_reflection_;return metadata; }

內存賦值,可以執行修改操作:

// 找到對應的內存地址,返回合適類型的指針. template <typename Type> inline Type* GeneratedMessageReflection::MutableRaw(Message* message, const FieldDescriptor* field) const {int index = field->containing_oneof() ?descriptor_->field_count() + field->containing_oneof()->index() :field->index();void* ptr = reinterpret_cast<uint8*>(message) + offsets_[index];return reinterpret_cast<Type*>(ptr); } // 設置protobuf的標志bit. inline void GeneratedMessageReflection::SetBit(Message* message, const FieldDescriptor* field) const {if (has_bits_offset_ == -1) {return;}MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32)); } // 設置某個字段的值 template <typename Type> inline void GeneratedMessageReflection::SetField(Message* message, const FieldDescriptor* field, const Type& value) const {if (field->containing_oneof() && !HasOneofField(*message, field)) {ClearOneof(message, field->containing_oneof()); // V3 oneof 類型的清理。}*MutableRaw<Type>(message, field) = value; // 先直接覆蓋field->containing_oneof() ?SetOneofCase(message, field) : SetBit(message, field); // 添加標記bit }

2.5 、 反射原理具體實例介紹

參考
下面的代碼展示了protobuf 對象反射的例子。將一個對象按照反射的字段順序序列化到string,然后反序列化到對象。最后調用反射打印其字段值,可以看到對象能夠還原。

1、proto文件

package tutorial; message Person {required string name = 1;required int32 id = 2;optional string email = 3;enum PhoneType {MOBILE = 0;HOME = 1;WORK = 2;}message PhoneNumber {required string number = 1;optional PhoneType type = 2 [default = HOME];}optional PhoneNumber phone = 4;} #include <string> #include <map> #include <iostream> #include <stdio.h> #include "person.pb.h" using namespace tutorial; using namespace google::protobuf;using std::cout; using std::endl; using std::string;void serialize_message(const google::protobuf::Message& message, std::string* serialized_string) {const google::protobuf::Descriptor* descriptor = message.GetDescriptor();const google::protobuf::Reflection* reflection = message.GetReflection();for (int i = 0; i < descriptor->field_count(); ++i) {const google::protobuf::FieldDescriptor* field = descriptor->field(i);bool has_field = reflection->HasField(message, field);if (has_field) {//arrays not supportedassert(!field->is_repeated());switch (field->cpp_type()) { #define CASE_FIELD_TYPE(cpptype, method, valuetype)\case google::protobuf::FieldDescriptor::CPPTYPE_##cpptype:{\valuetype value = reflection->Get##method(message, field);\int wsize = field->name().size();\serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));\serialized_string->append(field->name().c_str(), field->name().size());\wsize = sizeof(value);\serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));\serialized_string->append(reinterpret_cast<char*>(&value), sizeof(value));\break;\}CASE_FIELD_TYPE(INT32, Int32, int);CASE_FIELD_TYPE(UINT32, UInt32, uint32_t);CASE_FIELD_TYPE(FLOAT, Float, float);CASE_FIELD_TYPE(DOUBLE, Double, double);CASE_FIELD_TYPE(BOOL, Bool, bool);CASE_FIELD_TYPE(INT64, Int64, int64_t);CASE_FIELD_TYPE(UINT64, UInt64, uint64_t); #undef CASE_FIELD_TYPEcase google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {int value = reflection->GetEnum(message, field)->number();int wsize = field->name().size();//寫入name占用字節數serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));//寫入nameserialized_string->append(field->name().c_str(), field->name().size());wsize = sizeof(value);//寫入value占用字節數serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));//寫入valueserialized_string->append(reinterpret_cast<char*>(&value), sizeof(value));break;}case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {std::string value = reflection->GetString(message, field);int wsize = field->name().size();serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));serialized_string->append(field->name().c_str(), field->name().size());wsize = value.size();serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));serialized_string->append(value.c_str(), value.size());break;}case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {std::string value;int wsize = field->name().size();serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));serialized_string->append(field->name().c_str(), field->name().size());const google::protobuf::Message& submessage = reflection->GetMessage(message, field);serialize_message(submessage, &value);wsize = value.size();serialized_string->append(reinterpret_cast<char*>(&wsize), sizeof(wsize));serialized_string->append(value.c_str(), value.size());break;}}}} }void parse_message(const std::string& serialized_string, google::protobuf::Message* message) {const google::protobuf::Descriptor* descriptor = message->GetDescriptor();const google::protobuf::Reflection* reflection = message->GetReflection();std::map<std::string, const google::protobuf::FieldDescriptor*> field_map;for (int i = 0; i < descriptor->field_count(); ++i) {const google::protobuf::FieldDescriptor* field = descriptor->field(i);field_map[field->name()] = field;}const google::protobuf::FieldDescriptor* field = NULL;size_t pos = 0;while (pos < serialized_string.size()) {int name_size = *(reinterpret_cast<const int*>(serialized_string.substr(pos, sizeof(int)).c_str()));pos += sizeof(int);std::string name = serialized_string.substr(pos, name_size);pos += name_size;int value_size = *(reinterpret_cast<const int*>(serialized_string.substr(pos, sizeof(int)).c_str()));pos += sizeof(int);std::string value = serialized_string.substr(pos, value_size);pos += value_size;std::map<std::string, const google::protobuf::FieldDescriptor*>::iterator iter =field_map.find(name);if (iter == field_map.end()) {fprintf(stderr, "no field found.\n");continue;} else {field = iter->second;}assert(!field->is_repeated());switch (field->cpp_type()) { #define CASE_FIELD_TYPE(cpptype, method, valuetype)\case google::protobuf::FieldDescriptor::CPPTYPE_##cpptype: {\reflection->Set##method(\message,\field,\*(reinterpret_cast<const valuetype*>(value.c_str())));\std::cout << field->name() << "\t" << *(reinterpret_cast<const valuetype*>(value.c_str())) << std::endl;\break;\}CASE_FIELD_TYPE(INT32, Int32, int);CASE_FIELD_TYPE(UINT32, UInt32, uint32_t);CASE_FIELD_TYPE(FLOAT, Float, float);CASE_FIELD_TYPE(DOUBLE, Double, double);CASE_FIELD_TYPE(BOOL, Bool, bool);CASE_FIELD_TYPE(INT64, Int64, int64_t);CASE_FIELD_TYPE(UINT64, UInt64, uint64_t); #undef CASE_FIELD_TYPEcase google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {const google::protobuf::EnumValueDescriptor* enum_value_descriptor =field->enum_type()->FindValueByNumber(*(reinterpret_cast<const int*>(value.c_str())));reflection->SetEnum(message, field, enum_value_descriptor);std::cout << field->name() << "\t" << *(reinterpret_cast<const int*>(value.c_str())) << std::endl;break;}case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {reflection->SetString(message, field, value);std::cout << field->name() << "\t" << value << std::endl;break;}case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {google::protobuf::Message* submessage = reflection->MutableMessage(message, field);parse_message(value, submessage);break;}default: {break;}}} }void print_field(const Message& message) {const Descriptor* descriptor = message.GetDescriptor();const Reflection* reflection = message.GetReflection();for (int i = 0; i < descriptor->field_count(); ++i) {const FieldDescriptor* field = descriptor->field(i);bool has_field = reflection->HasField(message, field);assert(!field->is_repeated());switch (field->cpp_type()) { #define CASE_FIELD_TYPE(cpptype, method, valuetype)\case FieldDescriptor::CPPTYPE_##cpptype:{\valuetype value = reflection->Get##method(message, field);\if (has_field) {\cout << field->name() << " : " << value << ", type : " << #valuetype << "\n";\} else {\cout << field->name() << " : " << "None" << ", type : " << #valuetype << "\n";\}\break;\}CASE_FIELD_TYPE(INT32, Int32, int);CASE_FIELD_TYPE(UINT32, UInt32, uint32_t);CASE_FIELD_TYPE(FLOAT, Float, float);CASE_FIELD_TYPE(DOUBLE, Double, double);CASE_FIELD_TYPE(BOOL, Bool, bool);CASE_FIELD_TYPE(INT64, Int64, int64_t);CASE_FIELD_TYPE(UINT64, UInt64, uint64_t); #undef CASE_FIELD_TYPEcase FieldDescriptor::CPPTYPE_ENUM: {int value = reflection->GetEnum(message, field)->number();if (has_field) {cout << field->name() << " : " << value << ", type : " << "enum \n"; }else {cout << field->name() << " : " << "None" << ", type : " << "enum \n";}break;}case FieldDescriptor::CPPTYPE_STRING: {string value = reflection->GetString(message, field);if (has_field) {cout << field->name() << " : " << value << ", type : " << "string \n"; }else {cout << field->name() << " : " << "None" << ", type : " << "string \n";}break;}case FieldDescriptor::CPPTYPE_MESSAGE: {const Message& submessage = reflection->GetMessage(message, field);print_field(submessage);break;}}} }int main() {string str;Person person;person.set_name("shonm");person.set_id(123);person.mutable_phone()->set_number("1380000");person.mutable_phone()->set_type(Person_PhoneType_WORK);serialize_message(person, &str); //按照自己的方式(反射的字段)序列化Person person2;parse_message(str, &person2); //按照自己的方式反序列化printf("\n\n");print_field(person); //根據反射打印字段printf("\n\n");print_field(person2); }

參考

1、https://www.jianshu.com/p/ddc1aaca3691
2、https://blog.csdn.net/cchd0001/article/details/52452204
3、https://blog.csdn.net/yindongjie1221/article/details/90575989
4、https://blog.csdn.net/boshuzhang/article/details/64920286
5、https://blog.csdn.net/zxm342698145/article/details/83931027

總結

以上是生活随笔為你收集整理的ProtoBuf(Google Protocol Buffers)—— 反射原理以及反射具体流程介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。