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 Unknown Field Set H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/unknown_field_set.h
Line Count 411
Domains Serialization, Forward_Compatibility
Key Classes UnknownFieldSet, UnknownField
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Unknown field set header for forward compatibility. Declares the data structures that store unrecognized protobuf fields, allowing messages to round-trip through systems with different schema versions without data loss.

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

Description

This header declares two classes:

  • UnknownFieldSet -- A collection of UnknownField objects. Every protobuf Message contains an UnknownFieldSet that captures fields not defined in the message's schema. Methods include:
    • field_count(), field(int index) -- access stored fields.
    • AddVarint, AddFixed32, AddFixed64, AddLengthDelimited, AddGroup -- add unknown fields by wire type.
    • MergeFrom, Clear, Swap -- set-level operations.
    • SerializeToString, ParseFromString -- standalone serialization.
  • UnknownField -- A single unknown field entry storing the field number, wire type, and raw data. Supports five data types matching the protobuf wire types:
    • TYPE_VARINT -- stores a uint64.
    • TYPE_FIXED32 -- stores a uint32.
    • TYPE_FIXED64 -- stores a uint64.
    • TYPE_LENGTH_DELIMITED -- stores a std::string of raw bytes.
    • TYPE_GROUP -- stores a nested UnknownFieldSet.

Usage

#include "google/protobuf/unknown_field_set.h"

Referenced by message.h, reflection.h, and the protobuf runtime for forward-compatible parsing.

Code Reference

// Collection of unknown fields in a message.
class PROTOBUF_EXPORT UnknownFieldSet {
 public:
  UnknownFieldSet();
  ~UnknownFieldSet();

  int field_count() const;
  const UnknownField& field(int index) const;

  // Add unknown fields by wire type.
  void AddVarint(int number, uint64_t value);
  void AddFixed32(int number, uint32_t value);
  void AddFixed64(int number, uint64_t value);
  std::string* AddLengthDelimited(int number);
  UnknownFieldSet* AddGroup(int number);

  void Clear();
  void MergeFrom(const UnknownFieldSet& other);
  bool SerializeToString(std::string* output) const;
  bool ParseFromString(const std::string& data);

 private:
  std::vector<UnknownField> fields_;
};

// A single unknown field entry.
class PROTOBUF_EXPORT UnknownField {
 public:
  enum Type {
    TYPE_VARINT,
    TYPE_FIXED32,
    TYPE_FIXED64,
    TYPE_LENGTH_DELIMITED,
    TYPE_GROUP,
  };

  int number() const;
  Type type() const;

  uint64_t varint() const;
  uint32_t fixed32() const;
  uint64_t fixed64() const;
  const std::string& length_delimited() const;
  const UnknownFieldSet& group() const;
};

I/O Contract

Direction Type Description
Input Unrecognized wire data Fields not matching current schema, captured during parsing
Output Preserved wire data Re-serialized during message output for round-trip fidelity
Access Reflection::GetUnknownFields() Retrieved from a message via the reflection API

Usage Examples

// Inspecting unknown fields in a message
const UnknownFieldSet& unknowns =
    msg.GetReflection()->GetUnknownFields(msg);

for (int i = 0; i < unknowns.field_count(); ++i) {
  const UnknownField& f = unknowns.field(i);
  if (f.type() == UnknownField::TYPE_VARINT) {
    std::cout << "Field " << f.number()
              << " = " << f.varint() << std::endl;
  }
}

Related Pages

Page Connections

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