Any Class

class QtProtobuf::Any

A helper class to simplify working with the protobuf Any type. More...

Header: #include <Any>
CMake: find_package(Qt6 REQUIRED COMPONENTS ProtobufWellKnownTypes)
target_link_libraries(mytarget PRIVATE Qt6::ProtobufWellKnownTypes)
qmake: QT += protobufwellknowntypes
Since: Qt 6.6
Inherits: QProtobufMessage

This class is equality-comparable.

Public Functions

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)

Static Public Members

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)

Detailed Description

The Any class enables easy casting to and from other types using as() and fromMessage().

It is the type used in code generated by qtprotobufgen when a message contains a field of the google.protobuf.Any type.

Member Function Documentation

Any::Any()

Constructs a defaulted, empty, instance of Any.

Any::Any(const QtProtobuf::Any &other)

Constructs a copy of other.

[noexcept] Any::~Any()

Destroys this instance of Any

[static] template <typename T> QtProtobuf::Any Any::fromMessage(QAbstractProtobufSerializer *serializer, const T &message, QAnyStringView typeUrlPrefix)

This function serializes the given message as the value of the returned Any instance. This instance's typeUrl() is constructed from a prefix, a forward slash and the message name obtained from QProtobufMessage::propertyOrdering using serializer. If typeUrlPrefix is supplied, it is used as prefix, otherwise "type.googleapis.com" is used.

Note: T must be a class derived from QProtobufMessage with the Q_PROTOBUF_OBJECT macro or (for a nested Any message) be Any itself.

void Any::setTypeUrl(const QString &typeUrl)

Sets the type URL for the data contained to typeUrl.

Note: No verification is performed on the input to test if it's correct or in a valid format.

Note: Setter function for property typeUrl.

See also typeUrl() and fromMessage().

void Any::setValue(const QByteArray &value)

Sets the raw bytes of the value stored to value.

value must be the output of serializing a message.

Note: Setter function for property value.

See also value() and fromMessage().

QString Any::typeUrl() const

Returns the type URL of the Any object. The URL is meant as a hint for what the contained data really is.

Note: Qt has no support for dynamically obtaining any potential recipes for deconstructing types and simply uses the type URL to verify that types passed as T to as<T>() are of the correct type.

Note: Getter function for property typeUrl.

See also setTypeUrl() and value().

template <typename T> std::optional<T> Any::unpack(QAbstractProtobufSerializer *serializer) const

This function compares the message name of T with the value of typeUrl() before deserializing the data using serializer.

If the verification or deserialization fails it will return std::nullopt.

Note: T must be a class derived from QProtobufMessage with the Q_PROTOBUF_OBJECT macro or (for a nested Any message) be Any itself.

QByteArray Any::value() const

Returns the raw bytes that make up the value stored.

Consult typeUrl() to determine how to interpret these bytes.

Note: Getter function for property value.

See also setValue() and typeUrl().

QtProtobuf::Any &Any::operator=(const QtProtobuf::Any &other)

Copies the data of other into this instance.

Related Non-Members

[noexcept] bool operator!=(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)

Returns true if the two instances of Any, lhs and rhs, are distinct.

[noexcept] bool operator==(const QtProtobuf::Any &lhs, const QtProtobuf::Any &rhs)

Returns true if the two instances of Any, lhs and rhs, compare equal.

© 2024 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.