proto_utils.h#
General-purpose protobuf utility templates covering debug string formatting
(onnx::ProtoDebugString()), byte-buffer deserialization
(onnx::ParseProtoFromBytes()), and typed attribute-value extraction
(onnx::RetrieveValues()).
General-purpose protobuf utility templates: debug formatting, byte-buffer parsing, and attribute-value extraction.
-
namespace ONNX_LIGHT_NAMESPACE
Functions
-
template<typename Proto>
inline std::string ProtoDebugString(const Proto &proto)# Returns a human-readable debug representation of a protobuf message.
Calls
PrintToVectorString()onprotowith default print options and joins the resulting lines with newline characters.- Template Parameters:
Proto – A protobuf-like message type that exposes a
PrintToVectorString(utils::PrintOptions&)method.- Parameters:
proto – The message to format.
- Returns:
A multi-line string representation of
proto.
-
template<typename Proto>
inline bool ParseProtoFromBytes(Proto *proto, const char *buffer, size_t length)# Parses a protobuf message from a raw byte buffer.
Constructs a
StringStreamover the supplied buffer and callsParseFromStream()onproto. Returnsfalsewithout modifyingprotowhenprotois null, whenbufferis null butlengthis positive, when parsing raises astd::runtime_error, or when bytes remain unconsumed after parsing completes.- Template Parameters:
Proto – A protobuf-like message type that exposes a
ParseFromStream(utils::StringStream&, ParseOptions&)method.- Parameters:
proto – Destination message that receives the parsed content. Must not be
nullptr.buffer – Pointer to the serialized byte data. May be
nullptronly whenlengthis0.length – Number of bytes in
buffer.
- Returns:
trueif the entire buffer was consumed and parsed successfully;falseotherwise.
-
template<typename T>
inline std::vector<T> RetrieveValues(const AttributeProto &attr)# Extracts a typed list of values from an
AttributeProto.The primary template is declared but not defined; callers must use one of the explicit specializations for
int64_t,std::string, orfloat.- Template Parameters:
T – Element type to retrieve. Must be
int64_t,std::string, orfloat.- Parameters:
attr – Source attribute whose repeated field is extracted.
- Returns:
A
std::vector<T>containing the attribute values.
-
template<>
inline std::vector<int64_t> RetrieveValues(const AttributeProto &attr)# Extracts a list of
int64_tvalues from anAttributeProto.- Parameters:
attr – Source attribute (must have type
INTorINTS).- Returns:
A
std::vector<int64_t>copied from theintsrepeated field.
-
template<>
inline std::vector<std::string> RetrieveValues(const AttributeProto &attr)# Extracts a list of
std::stringvalues from anAttributeProto.- Parameters:
attr – Source attribute (must have type
STRINGorSTRINGS).- Returns:
A
std::vector<std::string>copied from thestringsrepeated field.
-
template<>
inline std::vector<float> RetrieveValues(const AttributeProto &attr)# Extracts a list of
floatvalues from anAttributeProto.- Parameters:
attr – Source attribute (must have type
FLOATorFLOATS).- Returns:
A
std::vector<float>copied from thefloatsrepeated field.
-
template<typename Proto>