Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN Protobuf Message H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/message.h
Line Count 1487
Domains Serialization, Core_API
Key Classes Message, MessageFactory
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Core Message base class header defining the protobuf message interface. This is the fundamental abstract class from which all generated protobuf message types inherit.

Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.

Description

This header declares the Message class, the central abstraction in the protobuf C++ API. It extends MessageLite with full descriptor and reflection support, enabling:

  • Serialization/Deserialization -- SerializeToString, ParseFromString, SerializeToCodedStream, and related methods.
  • Reflection -- Access to GetDescriptor() and GetReflection() for dynamic field inspection and mutation.
  • Copying and merging -- CopyFrom, MergeFrom, Clear.
  • Debug output -- DebugString(), ShortDebugString(), Utf8DebugString().
  • Factory pattern -- MessageFactory for creating message instances from Descriptor objects at runtime.

The header also declares MessageFactory, which provides a registry for creating concrete message instances given a Descriptor. The generated factory is the default, but custom factories can be registered for extensions or dynamic messages.

Usage

#include "google/protobuf/message.h"

Included by all generated .pb.h headers that use the full (non-lite) protobuf runtime.

Code Reference

class PROTOBUF_EXPORT Message : public MessageLite {
 public:
  Message() = default;
  ~Message() override;

  // Descriptor and reflection access.
  virtual const Descriptor* GetDescriptor() const;
  virtual const Reflection* GetReflection() const;

  // Serialization.
  bool SerializeToString(std::string* output) const;
  bool SerializeToArray(void* data, int size) const;
  bool ParseFromString(const std::string& data);
  bool ParseFromArray(const void* data, int size);

  // Copy and merge.
  virtual void CopyFrom(const Message& from);
  virtual void MergeFrom(const Message& from);
  void Clear() override;

  // Debug.
  std::string DebugString() const;
  std::string ShortDebugString() const;

  // Byte size.
  size_t ByteSizeLong() const override;
};

// Factory for creating Message instances from Descriptors.
class PROTOBUF_EXPORT MessageFactory {
 public:
  virtual ~MessageFactory();
  virtual const Message* GetPrototype(const Descriptor* type) = 0;
  static MessageFactory* generated_factory();
};

I/O Contract

Direction Type Description
Input Wire-format bytes Protobuf binary data passed to ParseFrom* methods
Output Wire-format bytes Protobuf binary data produced by SerializeTo* methods
Runtime Descriptor / Reflection Schema metadata and dynamic field access

Usage Examples

// Using Message interface polymorphically
void PrintMessage(const google::protobuf::Message& msg) {
  std::cout << msg.DebugString() << std::endl;
}

// Creating a message from a Descriptor via factory
const Descriptor* desc = pool->FindMessageTypeByName("MyMessage");
const Message* prototype =
    MessageFactory::generated_factory()->GetPrototype(desc);
Message* instance = prototype->New();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment