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 Map Field H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/map_field.h
Line Count 923
Domains Serialization, Data_Structures
Key Classes MapField, MapFieldBase
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Map field type handler for protobuf map<K,V> fields. Provides the internal representation and access layer for protobuf map fields within the MNN vendored copy of Google Protobuf.

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

Description

This header defines MapFieldBase and the templated MapField<Key, Value> class that implement the storage and serialization logic for protobuf map fields. Map fields are represented as repeated message entries internally but exposed as associative containers to user code.

MapFieldBase provides the type-erased base with virtual methods for synchronization between the map representation and the underlying repeated field representation. MapField extends this with concrete key/value type parameters, handling type-specific serialization, merging, and lookup operations.

The synchronization model ensures that either the map or the repeated field representation is authoritative at any given time, with lazy conversion between the two as needed.

Usage

#include "google/protobuf/map_field.h"

Used internally by generated protobuf code for any .proto file that declares map<K,V> fields. The MapField template is instantiated by the protobuf compiler output.

Code Reference

// Base class for all map field implementations
class PROTOBUF_EXPORT MapFieldBase {
 public:
  MapFieldBase();
  virtual ~MapFieldBase();

  // Returns the underlying map as a type-erased MapFieldBase.
  virtual const MapFieldBase& GetMap() const = 0;

  // Synchronizes the repeated field from the map or vice versa.
  void SyncRepeatedFieldWithMap() const;
  void SyncMapWithRepeatedField() const;

  // Returns the number of entries.
  virtual int size() const = 0;
};

// Typed map field for map<Key, Value> proto fields.
template <typename Key, typename Value,
          WireFormatLite::FieldType kKeyFieldType,
          WireFormatLite::FieldType kValueFieldType>
class MapField : public MapFieldBase {
 public:
  typedef Map<Key, Value> MapType;

  const Map<Key, Value>& GetMap() const;
  Map<Key, Value>* MutableMap();

  void MergeFrom(const MapFieldBase& other);
  void Swap(MapField* other);
  int size() const override;
};

I/O Contract

Direction Type Description
Input Wire-format bytes Serialized map entries as repeated key-value pair messages
Output Map<Key, Value> Associative container accessible via GetMap()
Sync Repeated field <-> Map Lazy bidirectional synchronization between representations

Usage Examples

// Accessing a map field from a generated message (internal usage)
const auto& my_map = msg.my_map_field();
for (const auto& entry : my_map) {
  // entry.first is the key, entry.second is the value
}

// Mutating a map field
(*msg.mutable_my_map_field())[key] = value;

Related Pages

Page Connections

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