Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-08 09:12:11

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_MAP_ENTRY_H__
0009 #define GOOGLE_PROTOBUF_MAP_ENTRY_H__
0010 
0011 #include <cstddef>
0012 #include <cstdint>
0013 #include <string>
0014 #include <type_traits>
0015 
0016 #include "google/protobuf/generated_message_reflection.h"
0017 #include "google/protobuf/has_bits.h"
0018 #include "google/protobuf/map_type_handler.h"
0019 #include "google/protobuf/message.h"
0020 #include "google/protobuf/message_lite.h"
0021 #include "google/protobuf/parse_context.h"
0022 #include "google/protobuf/unknown_field_set.h"
0023 #include "google/protobuf/wire_format_lite.h"
0024 
0025 // Must be included last.
0026 #include "google/protobuf/port_def.inc"
0027 
0028 #ifdef SWIG
0029 #error "You cannot SWIG proto headers"
0030 #endif
0031 
0032 namespace google {
0033 namespace protobuf {
0034 
0035 class Arena;
0036 
0037 namespace internal {
0038 
0039 // MapEntry is the returned google::protobuf::Message when calling AddMessage of
0040 // google::protobuf::Reflection. In order to let it work with generated message
0041 // reflection, its in-memory type is the same as generated message with the same
0042 // fields. However, in order to decide the in-memory type of key/value, we need
0043 // to know both their cpp type in generated api and proto type. In
0044 // implementation, all in-memory types have related wire format functions to
0045 // support except ArenaStringPtr. Therefore, we need to define another type with
0046 // supporting wire format functions. Since this type is only used as return type
0047 // of MapEntry accessors, it's named MapEntry accessor type.
0048 //
0049 // cpp type:               the type visible to users in public API.
0050 // proto type:             WireFormatLite::FieldType of the field.
0051 // in-memory type:         type of the data member used to stored this field.
0052 // MapEntry accessor type: type used in MapEntry getters/mutators to access the
0053 //                         field.
0054 //
0055 // cpp type | proto type  | in-memory type | MapEntry accessor type
0056 // int32_t    TYPE_INT32    int32_t          int32_t
0057 // int32_t    TYPE_FIXED32  int32_t          int32_t
0058 // string     TYPE_STRING   ArenaStringPtr   string
0059 // FooEnum    TYPE_ENUM     int              int
0060 // FooMessage TYPE_MESSAGE  FooMessage*      FooMessage
0061 //
0062 // The in-memory types of primitive types can be inferred from its proto type,
0063 // while we need to explicitly specify the cpp type if proto type is
0064 // TYPE_MESSAGE to infer the in-memory type.
0065 template <typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType,
0066           WireFormatLite::FieldType kValueFieldType>
0067 class MapEntry : public Message {
0068   // Provide utilities to parse/serialize key/value.  Provide utilities to
0069   // manipulate internal stored type.
0070   using KeyTypeHandler = MapTypeHandler<kKeyFieldType, Key>;
0071   using ValueTypeHandler = MapTypeHandler<kValueFieldType, Value>;
0072 
0073   // Define internal memory layout. Strings and messages are stored as
0074   // pointers, while other types are stored as values.
0075   using KeyOnMemory = typename KeyTypeHandler::TypeOnMemory;
0076   using ValueOnMemory = typename ValueTypeHandler::TypeOnMemory;
0077 
0078  public:
0079 #if !defined(PROTOBUF_CUSTOM_VTABLE)
0080   constexpr MapEntry() {}
0081 #endif  // PROTOBUF_CUSTOM_VTABLE
0082   using Message::Message;
0083 
0084   MapEntry(const MapEntry&) = delete;
0085   MapEntry& operator=(const MapEntry&) = delete;
0086 
0087   ~MapEntry() PROTOBUF_OVERRIDE {
0088     // Make sure that `Value` is never a derived message type.
0089     // We don't want to instantiate the template with every unique derived type.
0090     // The assertion is in the destructor because we need `Value` to be
0091     // complete to test it.
0092     static_assert(!std::is_base_of<Message, Value>::value ||
0093                       std::is_same<Message, Value>::value,
0094                   "");
0095 
0096     if (GetArena() != nullptr) return;
0097     SharedDtor(*this);
0098   }
0099 
0100   using InternalArenaConstructable_ = void;
0101   using DestructorSkippable_ = void;
0102 
0103   struct _Internal;
0104 
0105  protected:
0106   friend class google::protobuf::Arena;
0107 
0108   static void SharedDtor(MessageLite& msg) {
0109     auto& this_ = static_cast<MapEntry&>(msg);
0110     this_._internal_metadata_.template Delete<UnknownFieldSet>();
0111     KeyTypeHandler::DeleteNoArena(this_._impl_.key_);
0112     ValueTypeHandler::DeleteNoArena(this_._impl_.value_);
0113   }
0114 
0115   // Field naming follows the convention of generated messages to make code
0116   // sharing easier.
0117   struct {
0118     HasBits<1> _has_bits_{};
0119     CachedSize _cached_size_{};
0120 
0121     KeyOnMemory key_{KeyTypeHandler::Constinit()};
0122     ValueOnMemory value_{ValueTypeHandler::Constinit()};
0123   } _impl_;
0124 };
0125 
0126 template <typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType,
0127           WireFormatLite::FieldType kValueFieldType>
0128 struct MapEntry<Key, Value, kKeyFieldType, kValueFieldType>::_Internal {
0129   static constexpr ::int32_t kHasBitsOffset =
0130       8 * PROTOBUF_FIELD_OFFSET(MapEntry, _impl_._has_bits_);
0131 };
0132 
0133 }  // namespace internal
0134 }  // namespace protobuf
0135 }  // namespace google
0136 
0137 #include "google/protobuf/port_undef.inc"
0138 
0139 #endif  // GOOGLE_PROTOBUF_MAP_ENTRY_H__