Implementation:Alibaba MNN Protobuf Message CC
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/message.cc
|
| Line Count | 400 |
| Domains | Serialization, Core_API |
| Key Methods | Message::SerializeToString, Message::ParseFromString
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Core Message class implementation with reflection support. Contains the runtime implementations of serialization, deserialization, and message utility methods declared in message.h.
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
This source file implements the non-inline methods of the Message base class, including:
- Serialization --
SerializeToString,SerializeToArray,SerializeToCodedStreamand related overloads that convert a populated message into wire-format bytes. - Deserialization --
ParseFromString,ParseFromArray,ParseFromCodedStreamwhich reconstruct a message from wire-format input. - Reflection-based operations --
CopyFrom,MergeFrom, andDiscardUnknownFieldsusing theReflectioninterface. - Message factory -- Default
MessageFactoryimplementation for creating message instances by descriptor.
This file also provides utilities for computing byte sizes, checking initialization status, and debug string generation.
Usage
#include "google/protobuf/message.h"
All generated protobuf message types inherit from Message and rely on the implementations provided in this file.
Code Reference
// Serialize the message to a string in wire format.
bool Message::SerializeToString(std::string* output) const {
output->clear();
return AppendToString(output);
}
// Parse a message from a wire-format string.
bool Message::ParseFromString(const std::string& data) {
return ParseFromArray(data.data(), data.size());
}
// Reflection-based merge from another message of the same type.
void Message::MergeFrom(const Message& from) {
const Descriptor* descriptor = GetDescriptor();
const Reflection* reflection = GetReflection();
// ... field-by-field merge via reflection
}
// Default MessageFactory for creating message instances by Descriptor.
MessageFactory* MessageFactory::generated_factory() {
static auto* factory = new GeneratedMessageFactory();
return factory;
}
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | std::string / byte array |
Wire-format protobuf bytes for deserialization |
| Output | std::string / byte array |
Wire-format protobuf bytes from serialization |
| Input/Output | Message object |
Populated message fields after parsing or before serialization |
Usage Examples
// Serialize a protobuf message to a string
MyProtoMessage msg;
msg.set_name("example");
std::string serialized;
msg.SerializeToString(&serialized);
// Parse a protobuf message from a string
MyProtoMessage parsed;
parsed.ParseFromString(serialized);