proto_utils.h#

General-purpose protobuf utility templates covering debug string formatting (onnx_light::ProtoDebugString()), byte-buffer deserialization (onnx_light::ParseProtoFromBytes()), and typed attribute-value extraction (onnx_light::RetrieveValues()).

General-purpose protobuf utility templates: debug formatting, byte-buffer parsing, and attribute-value extraction.

namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when lib_onnx_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

Functions

template<typename Proto>
inline std::string ProtoDebugString(const Proto &proto)#

Returns a human-readable debug representation of a protobuf message.

Creates a std::stringstream, calls PrintToStringStream() on proto with default print options, and returns the accumulated string.

Template Parameters:

Proto – A protobuf-like message type that exposes a PrintToStringStream(std::stringstream&, utils::PrintOptions&) method.

Parameters:

proto – The message to format.

Returns:

A 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.

Calls ParseFromArray() on a temporary message so that the whole buffer must parse successfully. Returns false without modifying proto when proto is null, when buffer is null but length is positive, when length exceeds the 2 GB protobuf limit, when parsing raises a std::runtime_error, or when bytes remain unconsumed after parsing completes.

Template Parameters:

Proto – A protobuf-like message type that exposes a ParseFromArray(const void*, int) method.

Parameters:
  • proto – Destination message that receives the parsed content. Must not be nullptr.

  • buffer – Pointer to the serialized byte data. May be nullptr only when length is 0.

  • length – Number of bytes in buffer.

Returns:

true if the entire buffer was consumed and parsed successfully; false otherwise.

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, or float.

Template Parameters:

T – Element type to retrieve. Must be int64_t, std::string, or float.

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_t values from an AttributeProto.

Parameters:

attr – Source attribute (must have type INT or INTS).

Returns:

A std::vector<int64_t> copied from the ints repeated field.

template<>
inline std::vector<std::string> RetrieveValues(const AttributeProto &attr)#

Extracts a list of std::string values from an AttributeProto.

Parameters:

attr – Source attribute (must have type STRING or STRINGS).

Returns:

A std::vector<std::string> copied from the strings repeated field.

template<>
inline std::vector<float> RetrieveValues(const AttributeProto &attr)#

Extracts a list of float values from an AttributeProto.

Parameters:

attr – Source attribute (must have type FLOAT or FLOATS).

Returns:

A std::vector<float> copied from the floats repeated field.