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() const

int	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.mfbz.cn/a/773643.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

[C++][设计模式][访问器]详细讲解

目录 1.动机2.模式定义3.要点总结4.代码感受1.代码一2.代码二 1.动机 在软件构件过程中&#xff0c;由于需求的变化&#xff0c;某些类层次结构中常常需要增加新的行为(方法)&#xff0c;如果直接在基类中做这样的更改&#xff0c; 将会给子类带来很繁重的变更负担&#xff0c…

快手矩阵管理系统:开启短视频营销的智能时代

在短视频内容营销的浪潮中&#xff0c;快手矩阵管理系统以其独特的优势和功能&#xff0c;成为品牌和个人创作者不可或缺的工具。本文将详细解析快手矩阵管理系统的核心功能&#xff0c;探讨它如何帮助用户高效管理多平台、多账号的内容发布和互动。 快手矩阵管理系统概述 快…

【Java EE】Spring IOCDI

Spring IOC & DI 文章目录 Spring IOC & DI一、Spring是什么&#xff1f;二、IOC(控制反转)2.1 通俗理解2.2 造汽车的例子理解IOC2.3 IOC详解1. 获取Bean2. 方法注解——Bean1. 应用场景&#xff1a;2. 应用方法&#xff1a;3. 注意要点&#xff1a; 特别注意: 四、DI4…

Superset超火的企业级可视化BI分析工具

Superset&#xff0c;听起来就像是超级集合&#xff0c;确实&#xff0c;它几乎集合了所有你需要的数据功能。简单说&#xff0c;它就是一个现代化、功能强大的数据可视化工具。 它支持各种数据库&#xff0c;有着丰富的可视化选项&#xff0c;可以用来创建漂亮的数据仪表盘&a…

【数据清洗中分段线性插值法原理】

数据清洗中分段线性插值法原理 一、什么是分段线性插值法&#xff1f;二、分段线性插值法的数学原理三、分段线性插值法的应用步骤1. 引入库2. 创建示例数据3. 应用分段线性插值法4. 可视化插值结果 一、什么是分段线性插值法&#xff1f; 分段线性插值法通过在已知数据点之间…

【C语言】return 关键字

在C语言中&#xff0c;return是一个关键字&#xff0c;用于从函数中返回值或者结束函数的执行。它是函数的重要组成部分&#xff0c;负责将函数的计算结果返回给调用者&#xff0c;并可以提前终止函数的执行。 主要用途和原理&#xff1a; 返回值给调用者&#xff1a; 当函数执…

[leetcode hot 150]第一百一十七题,填充每个节点的下一个右侧节点

题目&#xff1a; 给定一个二叉树&#xff1a; struct Node {int val;Node *left;Node *right;Node *next; } 填充它的每个 next 指针&#xff0c;让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点&#xff0c;则将 next 指针设置为 NULL 。 初始状态下&#x…

【图卷积网络】GCN基础原理简单python实现

基础原理讲解 应用路径 卷积网络最经典的就是CNN&#xff0c;其 可以提取图片中的有效信息&#xff0c;而生活中存在大量拓扑结构的数据。图卷积网络主要特点就是在于其输入数据是图结构数据&#xff0c;即 G ( V , E ) G(V,E) G(V,E)&#xff0c;其中V是节点&#xff0c;E是…

C语言 指针和数组——指针的算术运算

目录 指针的算术运算 指针加上一个整数 指针减去一个整数 指针相减 指针的关系比较运算 小结 指针的算术运算 指针加上一个整数 指针减去一个整数 指针相减 指针的关系比较运算 小结  指针变量 – 指针类型的变量&#xff0c;保存地址型数据  指针变量与其他类型…

关于SQL NOT IN判断失效的情况记录

1.准备测试数据 CREATE TABLE tmp_1 (val integer);CREATE TABLE tmp_2 (val integer, val2 integer);INSERT INTO tmp_1 (val) VALUES (1); INSERT INTO tmp_1 (val) VALUES (2); INSERT INTO tmp_2 (val) VALUES (1); INSERT INTO tmp_2 (val, val2) VALUES (NULL,0);2.测…

swiftui中设置建议最多5个tabItem项,多个tabItem项会被自动折叠起来

在swiftui中设置底部的菜单栏的时候&#xff0c;最多建议设置5个&#xff0c;如果超过了&#xff0c;会被自动折叠到More中&#xff0c;点击More就会出现类似list的样式显示&#xff0c;不是很友好。 最多按照5个默认设置的话&#xff0c;就会正常全部显示出来&#xff1a; 测…

(七)glDrawArry绘制

几何数据&#xff1a;vao和vbo 材质程序&#xff1a;vs和fs(顶点着色器和片元着色器) 接下来只需要告诉GPU&#xff0c;使用几何数据和材质程序来进行绘制。 #include <glad/glad.h>//glad必须在glfw头文件之前包含 #include <GLFW/glfw3.h> #include <iostrea…

红海云签约海新域集团,产业服务运营领军企业加速人力资源数字化转型

北京海新域城市更新集团有限公司&#xff08;以下简称“海新域集团”&#xff09;是北京市海淀国有资产投资集团有限公司一级监管企业&#xff0c;致力于成为国内领先的产业服务运营商。集团积极探索城市和产业升级新模式&#xff0c;通过对老旧、低效等空间载体重新定位规划、…

2024年新考纲下的PMP考试有多难?全面解读!

一、PMP考试是什么&#xff0c;PMP证书有什么用&#xff1f; PMP&#xff08;Project Management Professional&#xff09;是指项目管理专业人士。PMP考试由美国PMI发起&#xff0c;旨在严格评估项目管理人员的知识和技能&#xff0c;以确定其是否具备高品质的资格认证。 PM…

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

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

手机如何充当电脑摄像头,新手使用教程分享(新)

手机如何充当电脑摄像头&#xff1f;随着科技的发展&#xff0c;智能手机已经成为我们日常生活中不可或缺的一部分。手机的摄像头除了拍摄记录美好瞬间之外&#xff0c;其实还有个妙用&#xff0c;那就是充当电脑的摄像头。手机摄像头充当电脑摄像头使用的话&#xff0c;我们就…

上位机图像处理和嵌入式模块部署(mcu 项目1:固件编写)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 说完了上位机的开发&#xff0c;接下来就是固件的开发。前面我们说过&#xff0c;目前使用的开发板是极海apm32f103的开发板。它自身包含了iap示例…

Linux - Shell 以及 权限问题

目录 Shell的运行原理 Linux权限问题 Linux权限的概念 如何实现用户账号之间的切换 如何仅提升当前指令的权限 如何将普通用户添加到信任列表 Linux权限管理 文件访问者的分类&#xff08;人&#xff09; 文件类型和访问权限&#xff08;事物属性&#xff09; 文件权限值的表…

【linux高级IO(一)】理解五种IO模型

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:Linux从入门到精通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学更多操作系统知识   &#x1f51d;&#x1f51d; Linux高级IO 1. 前言2. 重谈对…

Linux之文本三剑客

Linux之三剑客 Linux的三个命令,主要是用来处理文本,grep,sed,awk,处理日志的时候使用的非常多 1 grep 对文本的内容进行查找 1) 基础用法 语法 grep 选项 内容|正则表达式 文件选项: -i 不区分大小写 -v 排除,反选 -n 显示行号 -c 统计个数查看文件里包含有的内容 [roo…