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 Type Handler H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/map_type_handler.h
Line Count 691
Domains Serialization, Data_Structures
Key Classes MapTypeHandler, MapWireFieldTypeTraits
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Type handler traits for map field serialization. Defines compile-time type mappings that control how map keys and values are serialized, deserialized, and managed in memory.

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

Description

This header provides the MapTypeHandler template and associated MapWireFieldTypeTraits that map protobuf wire types to C++ types for use within MapField containers. These traits control:

  • Memory management -- whether values are allocated on the heap (messages, strings) or stored inline (scalars).
  • Serialization size calculation -- computing the byte size of each key or value for wire encoding.
  • Wire format read/write -- delegating to the appropriate WireFormatLite methods for each concrete type.

The handler is specialized for each WireFormatLite::FieldType, covering all protobuf scalar types, strings, bytes, enums, and embedded messages.

Usage

#include "google/protobuf/map_type_handler.h"

This header is consumed by map_field.h and generated map field code. It is not used directly by application code.

Code Reference

// Traits class mapping FieldType to C++ type and operations.
template <WireFormatLite::FieldType kFieldType, typename Type>
class MapTypeHandler {
 public:
  typedef Type CppType;

  static inline int GetCachedSize(const Type& value);
  static inline int ByteSize(const Type& value);

  static inline void Write(int field, const Type& value,
                           io::CodedOutputStream* output);
  static inline void Read(io::CodedInputStream* input, Type* value);

  // Memory management for heap-allocated types.
  static inline void Initialize(Type** value, Arena* arena);
  static inline void Delete(Type* value, Arena* arena);
  static inline void Clear(Type* value);
};

// Wire field type traits for map keys and values.
template <WireFormatLite::FieldType kFieldType>
struct MapWireFieldTypeTraits {
  static const WireFormatLite::WireType kWireType;
};

I/O Contract

Direction Type Description
Input Raw wire bytes Encoded key or value from a map entry
Output C++ typed value Deserialized key/value in native C++ representation
Compile-time FieldType enum Selects the appropriate serialization strategy

Usage Examples

// Internal usage within MapField serialization (simplified)
// Writing a map entry key of type INT32:
MapTypeHandler<WireFormatLite::TYPE_INT32, int32_t>::Write(
    kKeyFieldNumber, key, output);

// Reading a map entry value of type STRING:
MapTypeHandler<WireFormatLite::TYPE_STRING, std::string>::Read(
    input, &value);

Related Pages

Page Connections

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