QtProtobuf::Any Class
class QtProtobuf::Any用于简化 protobuf Any 类型工作的辅助类。更多
头文件: | #include <Any> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS ProtobufWellKnownTypes) target_link_libraries(mytarget PRIVATE Qt6::ProtobufWellKnownTypes) |
qmake: | QT += protobufwellknowntypes |
自 | Qt 6.6 |
继承: | QProtobufMessage |
该类可等价比较。
公共函数
Any() | |
Any(const QtProtobuf::Any &other) | |
~Any() | |
void | setTypeUrl(const QString &typeUrl) |
void | setValue(const QByteArray &value) |
QString | typeUrl() const |
std::optional<T> | unpack(QAbstractProtobufSerializer *serializer) const |
QByteArray | value() const |
QtProtobuf::Any & | operator=(const QtProtobuf::Any &other) |
静态公共成员
QtProtobuf::Any | fromMessage(QAbstractProtobufSerializer *serializer, const T &message, QAnyStringView typeUrlPrefix) |
相关非成员
bool | operator!=(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs) |
bool | operator==(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs) |
详细说明
Any 类可以使用 as() 和fromMessage() 方便地与其他类型进行转换。
当消息包含google.protobuf.Any
类型的字段时,qtprotobufgen生成的代码会使用该类型。
成员函数文档
Any::Any()
构造一个 Any 的默认空实例。
Any::Any(const QtProtobuf::Any &other)
构造other 的副本。
[noexcept]
Any::~Any()
销毁 Any
[static]
template <typename T> QtProtobuf::Any Any::fromMessage(QAbstractProtobufSerializer *serializer, const T &message, QAnyStringView typeUrlPrefix)
该函数将给定的message 序列化为返回的 Any 实例的值。该实例的typeUrl() 由前缀、正斜杠和使用serializer 从QProtobufMessage::propertyOrdering 获取的消息名称构成。如果提供了typeUrlPrefix ,则使用它作为前缀,否则使用"type.googleapis.com"
。
注意: T 必须是使用Q_PROTOBUF_OBJECT
宏从QProtobufMessage 派生的类,或者(对于嵌套的 Any 消息)本身就是 Any。
void Any::setTypeUrl(const QString &typeUrl)
设置typeUrl 所含数据的类型 URL。
注: 不对输入内容进行验证,以检验其是否正确或格式是否有效。
注: 属性typeUrl 的设置函数。
另请参阅 typeUrl() 和fromMessage()。
void Any::setValue(const QByteArray &value)
设置存储到value 的值的原始字节数。
value 必须是序列化信息的输出。
注: 属性value 的设置函数。
另请参阅 value() 和fromMessage()。
QString Any::typeUrl() const
返回 Any 对象的 URL 类型。URL 的作用是提示所包含的数据到底是什么。
注意: Qt XML 不支持动态获取解构类型的任何潜在配方,而只是使用类型 URL 来验证作为 T 传递给as<T>()
的类型是否为正确类型。
注: typeUrl 属性的获取函数。
另请参见 setTypeUrl() 和value()。
template <typename T> std::optional<T> Any::unpack(QAbstractProtobufSerializer *serializer) const
在使用serializer 对数据进行反序列化之前,该函数会将 T 的信息名称与typeUrl() 的值进行比较。
如果验证或反序列化失败,它将返回std::nullopt
。
注意: T 必须是使用Q_PROTOBUF_OBJECT
宏从QProtobufMessage 派生的类,或者(对于嵌套的 Any 消息)本身就是 Any。
QByteArray Any::value() const
返回存储值的原始字节。
请查阅typeUrl() 以确定如何解释这些字节。
注: 属性值的获取函数。
QtProtobuf::Any &Any::operator=(const QtProtobuf::Any &other)
将other 的数据复制到此实例中。
相关非会员
[noexcept]
bool operator!=(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)
如果 Any 的两个实例lhs 和rhs 是不同的,则返回 true。
[noexcept]
bool operator==(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)
如果 Any 的两个实例lhs 和rhs 比较结果相等,则返回 true。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.