google::protobuf命名空间下常用的C++ API----message.h

#include <google/protobuf/message.h>

namespace google::protobuf

    假设您有一个消息定义为:

message Foo {optional string text = 1;repeated int32 numbers = 2;
}

    然后,如果你使用 protocol编译器从上面的定义生成一个类,你可以这样使用它:

std::string data;  // Will store a serialized version of the message.{// Create a message and serialize it.Foo foo;foo.set_text("Hello World!");foo.add_numbers(1);foo.add_numbers(5);foo.add_numbers(42);foo.SerializeToString(&data);
}{// Parse the serialized message and check that it contains the// correct data.Foo foo;foo.ParseFromString(data);assert(foo.text() == "Hello World!");assert(foo.numbers_size() == 3);assert(foo.numbers(0) == 1);assert(foo.numbers(1) == 5);assert(foo.numbers(2) == 42);
}{// Same as the last block, but do it dynamically via the Message// reflection interface.Message* foo = new Foo;const Descriptor* descriptor = foo->GetDescriptor();// Get the descriptors for the fields we're interested in and verify// their types.const FieldDescriptor* text_field = descriptor->FindFieldByName("text");assert(text_field != nullptr);assert(text_field->type() == FieldDescriptor::TYPE_STRING);assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL);const FieldDescriptor* numbers_field = descriptor->FindFieldByName("numbers");assert(numbers_field != nullptr);assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED);// Parse the message.foo->ParseFromString(data);// Use the reflection interface to examine the contents.const Reflection* reflection = foo->GetReflection();assert(reflection->GetString(*foo, text_field) == "Hello World!");assert(reflection->FieldSize(*foo, numbers_field) == 3);assert(reflection->GetRepeatedInt32(*foo, numbers_field, 0) == 1);assert(reflection->GetRepeatedInt32(*foo, numbers_field, 1) == 5);assert(reflection->GetRepeatedInt32(*foo, numbers_field, 2) == 42);delete foo;
}

    这个文件中的类

    Metadata:用于保存消息元数据的容器。
    Message:协议消息的抽象接口。   
    Reflection:此接口包含可用于动态访问和修改协议消息字段的方法。
    MessageFactory:消息对象工厂的抽象接口。

    文件成员

    以下定义不是任何类的一部分。
   

 //尝试将此消息向下转换为生成的消息类型
template const T *  DynamicCastToGenerated(const Message * from)
template T * DynamicCastToGenerated(Message* from)
//调用此函数以确保此消息的反射被链接到二进制文件
template void  LinkMessageReflection ()   //调用方式
google::protobuf::LinkMessageReflection<FooMessage>();//这将确保以下查找成功:
DescriptorPool::generated_pool()->FindMessageTypeByName("FooMessage");
const RepeatedPtrField< std::string > &	
Reflection::GetRepeatedPtrFieldInternal< std::string >(const Message & message, const FieldDescriptor * field) const
RepeatedPtrField< std::string > *	
Reflection::MutableRepeatedPtrFieldInternal< std::string >(Message * message, const FieldDescriptor * field) const

结构元数据

#include <google/protobuf/message.h>

namespace  google::protobuf

用于保存消息元数据的容器。

//成员:
const Descriptor *	descriptor
const Reflection *	reflection

   class Message: public MessageLite

#include <google/protobuf/message.h>

namespace  google::protobuf

协议消息的抽象接口。

    另请参见MessageLite,它包含大多数日常操作,Message类在此基础上添加了描述符(descriptors)和反射reflection()。

    用户不能从这个类派生。只有protocol编译器和内部库允许创建子类。

constexpr	Message()
protected virtual Metadata
//获取一个包含消息元数据的结构体,该结构体依次用于实现上面的GetDescriptor()和GetReflection()。	
GetMetadata() const = 0
protected explicit	Message(Arena * arena)
protected static uint64	GetInvariantPerBuild(uint64 salt)
//基本操作//Construct a new instance of the same type.
virtual Message * New() const = 0//Construct a new instance on the arena. 
virtual Message *	New(Arena * arena) const//Make this message into a copy of the given message. 
virtual void CopyFrom(const Message & from)//Merge the fields from the given message into this message. 
virtual void MergeFrom(const Message & from)//Verifies that IsInitialized() returns true. 
void	CheckInitialized() const//Slowly build a list of all required fields that are not set. more...
void	FindInitializationErrors(std::vector< std::string > * errors) const//Like FindInitializationErrors, but joins all the strings, delimited by commas, and //returns them.
virtual std::string	InitializationErrorString() const//Clears all unknown fields from this message and all embedded messages. 
virtual void	DiscardUnknownFields()//Computes (an estimate of) the total number of bytes currently used for storing the //message in memory. 
virtual size_t	SpaceUsedLong() constint	SpaceUsed() const

调试和测试

//Generates a human readable form of this message, useful for debugging and other purposes.
std::string	DebugString() const//Like DebugString(), but with less whitespace.
std::string	ShortDebugString() const//Like DebugString(), but do not escape UTF-8 byte sequences.
std::string	Utf8DebugString() const//Convenience function useful in GDB. Prints DebugString() to stdout.
void PrintDebugString() const

基于反射的方法

    这些方法在MessageLite中是纯虚拟的,但是Message提供了基于反射的默认实现。

//Get the name of this message type, e.g. "foo.bar.BazProto".
virtual std::string	GetTypeName() const//Clear all fields of the message and set them to their default values. 
virtual void Clear()//Returns whether all required fields have been set. 
virtual bool IsInitialized() const//If |other| is the exact same class as this, calls MergeFrom(). more...
virtual void CheckTypeAndMergeFrom(const MessageLite & other)//Reflective parser.
virtual const char * _InternalParse(const char * ptr, internal::ParseContext * ctx)//Computes the serialized size of the message. 
virtual size_t ByteSizeLong() const//Fast path when conditions match 
virtual uint8 *	_InternalSerialize(uint8 * ptr, io::EpsCopyOutputStream * stream) const

//自省//Get a non-owning pointer to a Descriptor for this message's type. 
const Descriptor *	GetDescriptor() const//Get a non-owning pointer to the Reflection interface for this Message, which can be used to read and modify the fields of the Message dynamically (in other words, without knowing the message type at compile time). 
const Reflection *	GetReflection() const

Reflection类

    #include <google/protobuf/message.h>

    命名空间google::protobuf

    此接口包含可用于动态访问和修改协议消息字段的方法。

    它们的语义类似于protocol编译器生成的访问器。

    要获取给定消息的反射,请调用Message:: getrereflect()。

    该接口与Message分离只是出于效率的考虑;Message的绝大多数实现将共享相同的Reflection实现(GeneratedMessageReflection,在generated_message.h中定义),并且特定类的所有消息应该共享相同的Reflection对象。

    这些方法有几种不正确的使用方式。例如,以下任何条件都会导致未定义的结果:

    FieldDescriptor不是此消息类型的字段。
    所调用的方法不适合字段的类型。对于FieldDescriptor::TYPE_*中的每个字段类型,只有一个Get*()方法、一个Set*()方法和一个Add*()方法对该类型有效。
    在重复字段上调用单个字段的Get*()或Set*()方法。
    在非重复字段上调用GetRepeated*(), SetRepeated*()或Add*()。
    传递给任何方法的Message对象都不是这个Reflection对象的正确类型(即Message . getreflection () != Reflection)。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1472413.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

docker介绍与详细安装

1 docker 介绍 1.1 虚拟化 在计算机中&#xff0c;虚拟化&#xff08;英语&#xff1a;Virtualization&#xff09;是一种资源管理技术&#xff0c;是将计算机的各种实体资源&#xff0c;如服务器、网络、内存及存储等&#xff0c;予以抽象、转换后呈现出来&#xff0c;打破实…

docker部署wg-easy和firefly

Background WireGuard是一种新型的VPN协议,它通过在内核层运行,提供高效、安全、简单和现代的VPN解决方案。wg-easy是一个专为简化 WireGuard VPN配置和管理而设计的工具&#xff0c;提供了界面化的管理&#xff0c;进一步降低WireGuard 的使用门槛&#xff0c;让用户无需深入了…

嵌入式Linux系统编程 — 6.7 实时信号

目录 1 什么是实时信号 2 sigqueue函数 3 sigpending()函数 1 什么是实时信号 等待信号集只是一个掩码&#xff0c;它并不追踪信号的发生次数。这意味着&#xff0c;如果相同的信号在被阻塞的状态下多次产生&#xff0c;它只会在信号集中被记录一次&#xff0c;并且在信号集…

利用谷歌云serverless代码托管服务Cloud Functions构建Gemini Pro API

谷歌在2024年4月发布了全新一代的多模态模型Gemini 1.5 Pro&#xff0c;Gemini 1.5 Pro不仅能够生成创意文本和代码&#xff0c;还能理解、总结上传的图片、视频和音频内容&#xff0c;并且支持高达100万tokens的上下文。在多个基准测试中表现优异&#xff0c;性能超越了ChatGP…

node 下载文件到网络共享目录

1、登录网络共享计算器 2、登录进入后复制要存储文件的目录路径 例如&#xff1a; \\WIN-desktop\aa\bb\cc 3、node 下载后写入网络共享目录 注意&#xff08;重要&#xff09;:在使用UNC路径时&#xff0c;请确保你正确转义了反斜杠&#xff08;使用两个反斜杠来表示一个&…

Ubuntu 22.04远程自动登录桌面环境

如果需要远程自动登录桌面环境&#xff0c;首先需要将Ubuntu的自动登录打开&#xff0c;在【settings】-【user】下面 然后要设置【Sharing】进行桌面共享&#xff0c;Ubuntu有自带的桌面共享功能&#xff0c;不需要另外去安装xrdp或者vnc之类的工具了 点开【Remote Desktop】…

跑冒滴漏智能识别摄像机

对于现代城市管理来说&#xff0c;跑冒滴漏智能识别摄像机正逐渐成为解决水管漏水问题和保护城市供水安全的重要工具。这类摄像机通过先进的视觉识别和数据分析技术&#xff0c;能够有效监测和识别城市管道系统中的漏水现象&#xff0c;对于提升供水系统效率和保障城市居民生活…

探寻操作系统文件名字符限制的规则和历史

引言 从最早的电脑系统到现代的操作系统&#xff0c;文件命名的规则一直在不断发展&#xff0c;这些规则体现了不同操作系统设计哲学的差异。作为开发者&#xff0c;了解这些差异和背后的历史渊源非常有价值&#xff0c;本文将详细探讨Windows、macOS和Linux三大主流操作系统在…

栈实现队列与队列实现堆

ok呀&#xff0c;上一篇博客写了队列的相关知识&#xff0c;接下来就是我们提及过的&#xff0c;栈与队列的相互实现了。堆与这个问题咧&#xff0c;其实大家完全可以当一个知识扩展因为&#xff0c;这个问题也是没有太多的实践意义的&#xff0c;更多的是教学意义。所以咧。大…

服务器数据恢复—DS5300存储raid5阵列数据恢复案例

服务器存储数据恢复环境&#xff1a; 某单位一台某品牌DS5300存储&#xff0c;1个机头4个扩展柜&#xff0c;50块硬盘组建2组RAID5磁盘阵列&#xff08;一组raid5阵列有27块成员盘&#xff0c;存放Oracle数据库文件&#xff1b;另外一组raid5阵列有23块成员盘&#xff09;。存储…

Jmeter使用JSON Extractor提取多个变量

1.当正则不好使时&#xff0c;用json extractor 2.提取多个值时&#xff0c;默认值必填&#xff0c;否则读不到变量

c进阶篇(四):内存函数

内存函数以字节为单位更改 1.memcpy memcpy 是 C/C 中的一个标准库函数&#xff0c;用于内存拷贝操作。它的原型通常定义在 <cstring> 头文件中&#xff0c;其作用是将一块内存中的数据复制到另一块内存中。 函数原型&#xff1a;void *memcpy(void *dest, const void…

长难句打卡7.4

But policymakers who refocus efforts on improving well-being rather than simply worrying about GDP figures could avoid the forecasted doom and may even see progress. 但政策制定者们应重新集中精力于提升&#xff08;社会&#xff09;幸福感&#xff0c;而非仅仅担…

Unity 资源 之 Sweet Cakes Icon套装,110个高品质蛋糕图标分享

Sweet Cakes Icon 套装 - 为 Unity 开发者带来甜蜜惊喜 前言资源包内容领取兑换码 前言 亲爱的 Unity 开发者们&#xff0c;今天要向你们介绍一款令人心动的图标套装 - Sweet Cakes Icon。 Sweet Cakes Icon 套装包含了超过 110 种高品质的蛋糕和纸杯蛋糕图标&#xff0c;这无…

鸿蒙开发HarmonyOS NEXT (三) 熟悉ArkTs

一、自定义组件 1、自定义组件 自定义组件&#xff0c;最基础的结构如下&#xff1a; Component struct Header {build() {} } 提取头部标题部分的代码&#xff0c;写成自定义组件。 1、新建ArkTs文件&#xff0c;把Header内容写好。 2、在需要用到的地方&#xff0c;导入…

去中心化 RAG 先行者,KIP Protocol 如何保护数据所有权、激活 AI 资产

AI 时代&#xff0c;人人都应实现 KnowledgeFi 的梦想或许并不遥远&#xff0c;KIP Protocol 正在生动践行这一价值理念&#xff0c;带动去中心化数字产权的创建与盈利&#xff0c;面向 CryptoAI 的蓝海市场迈出创新探索的技术步伐&#xff0c;朝着 Web3 行业打造去中心化 AI 的…

30斤用什么快递便宜?大件物品怎么寄划算省钱?

大学生小李最近因为毕业要搬家&#xff0c;不得不把一堆书籍、衣服和一些生活用品寄回家。作为一个精打细算的“穷学生”&#xff0c;小李可是不愿意在快递费上花冤枉钱的。于是&#xff0c;他开始研究各种寄快递省钱的方法&#xff0c;今天我们就来看看小李是怎么操作的。一、…

一款纯 js 实现的大模型应用服务 FastGPT 解读

背景介绍 最近被不同的人安利了 FastGPT 项目&#xff0c;实际上手体验了一下&#xff0c;使用流程类似之前调研过的 Dify, 包含的功能主要是&#xff1a;任务流的编排&#xff0c;知识库管理&#xff0c;另外还有一些外部工具的调用能力。使用页面如下所示&#xff1a; 实际…

【C++】 解决 C++ 语言报错:Undefined Reference

文章目录 引言 未定义引用&#xff08;Undefined Reference&#xff09;是 C 编程中常见的错误之一&#xff0c;通常在链接阶段出现。当编译器无法找到函数或变量的定义时&#xff0c;就会引发未定义引用错误。这种错误会阻止生成可执行文件&#xff0c;影响程序的正常构建。本…

武汉免费 【FPGA实战训练】 Vivado入门与设计师资课程

一&#xff0e;背景介绍 当今高度数字化和智能化的工业领域&#xff0c;对高效、灵活且可靠的技术解决方案的需求日益迫切。随着工业 4.0 时代的到来&#xff0c;工业生产过程正经历着前所未有的变革&#xff0c;从传统的机械化、自动化逐步迈向智能化和信息化。在这一背景下&…