5-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/783777.html

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

相关文章

Studying-代码随想录训练营day31| 56.合并区间、738.单调递增的数字、968.监控二叉树、贪心算法总结

第31天&#xff0c;贪心最后一节(ง •_•)ง&#x1f4aa;&#xff0c;编程语言&#xff1a;C 目录 56.合并区间 738.单调递增的数字 968.监控二叉树 贪心算法总结 56.合并区间 文档讲解&#xff1a;代码随想录合并区间 视频讲解&#xff1a;手撕合并区间 题目&#xf…

C语言下结构体、共用体、枚举类型的讲解

主要内容 结构体结构体数组结构体指针包含结构体的结构链表链表相关操作共用体枚举类型 结构体 结构体的类型的概念 结构体实现步骤 结构体变量的声明 struct struct 结构体名{ 数据类型 成员名1; 数据类型 成员名2; ..…

绝地求生PUBG兰博基尼怎么兑换 兰博基尼怎么获得

绝地求生采用虚幻4引擎制作&#xff0c;玩家们会在一个偏远的岛屿上出生&#xff0c;然后展开一场赢家通吃的生存竞赛&#xff0c;最后只会有1个人存活。当然&#xff0c;和其他生存游戏一样&#xff0c;玩家需要在广袤复杂的地图中收集武器、车辆、物资&#xff0c;而且也会有…

解决win10报“无法加载文件……profile.ps1,因为在此系统上禁止运行脚本”的问题

打开命令行报错 解决方法 使用管理员权限打开PowerShell&#xff1a;WinX, 选择“Windows PowerShell&#xff08;管理员&#xff09;” 输入&#xff1a;Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 输入&#xff1a;y确认修改安全策略 &#xff1a;y确认修改安全策略…

探讨大数据在视频汇聚平台LntonCVS国标GB28181协议中的应用

随着摄像头和视频监控系统的普及和数字化程度的提高&#xff0c;视频监控系统产生的数据量急剧增加。大数据技术因其优秀的数据管理、分析和利用能力&#xff0c;成为提升视频监控系统效能和价值的重要工具。 大数据技术可以将视频监控数据与其他数据源进行融合分析&#xff0c…

【elasticsearch】IK分词器添加自定义词库,然后更新现有的索引

进入elasticsearch中的plugins位置&#xff0c;找到ik分词器插件&#xff0c;进入ik插件的config文件夹&#xff0c;当中有一个IKAnalyzer.cfg.xml配置文件。使用vim编辑器修改配置文件&#xff1a; vim IKAnalyzer.cfg.xml 配置文件如下&#xff08;添加了自定义字典的位置&…

pygame 音乐粒子特效

代码 import pygame import numpy as np import pymunk from pymunk import Vec2d import random import librosa import pydub# 初始化pygame pygame.init()# 创建屏幕 screen pygame.display.set_mode((1920*2-10, 1080*2-10)) clock pygame.time.Clock()# 加载音乐文件 a…

人工智能的新时代:从模型到应用的转变

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

信息技术课堂上如何有效防止学生玩游戏?

防止学生在信息技术课堂上玩游戏需要综合运用教育策略和技术手段。以下是一些有效的措施&#xff0c;可以用来阻止或减少学生在课堂上玩游戏的行为&#xff1a; 1. 明确课堂规则 在课程开始之初&#xff0c;向学生清楚地说明课堂纪律&#xff0c;强调不得在上课时间玩游戏。 制…

使用tcpdump抓取本本机的所有icmp包

1、抓取本机所有icmp包 tcpdump -i any icmp -vv 图中上半部分&#xff0c;是源主机tmp179无法ping通目标主机192.168.10.79&#xff08;因为把该主机关机了&#xff09;的状态&#xff0c;注意看&#xff0c;其中有unreachable 图中下半部分&#xff0c;是源主机tmp179可以p…

Docker总结

准备环境&#xff1a; VMware17Ubuntu18.04(LTS)&#xff1a;https://releases.ubuntu.com/18.04/ubuntu-18.04.6-desktop-amd64.iso 1. Docker前瞻 docker相关文档&#xff1a; docker官网地址 : https://www.docker.com/docker文档地址 : https://docs.docker.com/docker镜像…

tomcat的优化和tomcat和nginx实现动静分离:

tomcat的优化 tomcat自身的优化 tomcat的并发处理能力不强。大项目不使用tomcat做为转发动态的中间件&#xff08;k8s集群&#xff0c;python&#xff0c;rubby&#xff09;&#xff0c;小项目会使用&#xff08;内部使用&#xff09;&#xff0c;动静分离。 优化tomcat的启动…

【Spring Boot】Spring AOP动态代理,以及静态代理

目录 Spring AOP代理一. 代理的概念二. 静态代理三. JDK代理3.1 重写 invoke 方法进⾏功能增强3.2 通过Proxy类随机生成代理对象 四. CGLIB代理4.1 自定义类来重写intercept方法4.2 通过Enhancer类的create方法来创建代理类 五. AOP源码剖析 总结(重中之重&#xff0c;精华) Sp…

一款简单、免费的web文件共享服务器

#共享文件# #远程访问# #手机访问# 文件共享已成为我们日常生活和工作中不可或缺的一部分。它如同一条无形的纽带&#xff0c;将人们紧密地联系在一起&#xff0c;促进了信息的快速传播和交流。 文件共享的魅力在于其打破了地域和时间的限制。无论我们身处世界的哪个角落&…

深度解析:当下流行的人工智能大模型生成逻辑

在过去的几年里&#xff0c;人工智能领域经历了前所未有的革新&#xff0c;其中最引人注目的就是大规模预训练模型的崛起。这些模型&#xff0c;如GPT系列、BERT、T5、DALLE和CLIP等&#xff0c;凭借其强大的语言理解和生成能力&#xff0c;已经在自然语言处理&#xff08;NLP&…

Springboot使用WebSocket发送消息

1. 创建springboot项目&#xff0c;引入spring-boot-starter-websocket依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>完整项目依赖 <?xml ver…

聊聊使用GROUP_CONCAT函数遇到的坑

问题现象 在工作中我们或多或少都会使用到函数group_concat&#xff0c;它可以合并多行的某列(或多列)数据为一行&#xff0c;默认以逗号分隔。 最近碰到了一个线上bug&#xff0c;查询DB时返回的结果信息mysql自动截取了&#xff0c;导致页面显示的时候只显示了前半段结果。 …

MATLAB环境下4种噪声生成

生成噪声包括: 1)粉红色(闪烁)噪声-功率谱密度斜率-3 dB/oct。&#xff0c; - 10db /dec 2)红色(布朗)噪声-功率谱密度斜率-6 dB/oct。&#xff0c; - 20db /dec 3)蓝色噪声-功率谱密度斜率3 dB/oct。&#xff0c; 10db /dec 4)紫色(紫色)噪声-功率谱密度斜率 6db /oct。&…

抖音商城自定义小程序源码系统 前后端分离 带完整的源代码包以及搭建教程

系统概述 在当今数字化时代&#xff0c;电商平台的便捷性和个性化体验成为了吸引用户的关键。随着短视频平台的兴起&#xff0c;抖音作为其中的佼佼者&#xff0c;其商城小程序成为了商家连接消费者的新阵地。为了帮助商家快速构建个性化、高效的小程序店铺&#xff0c;本文将…

Java线程的创建·启动和休眠

一.线程的创建和启动 Java中创建线程的两种方式 ◆继承java.lang.Thread类 ◆实现java.lang.Runnable接口 ◆使用线程的步骤 继承Thread类创建线程 ◆自定义线程类继承自Thread类 ◆重写run()方法&#xff0c;编写线程执行体 ◆创建线程对象&#xff0c;调用start()方法启动…