map_entry.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_MAP_ENTRY_H__
  31. #define GOOGLE_PROTOBUF_MAP_ENTRY_H__
  32. #include <google/protobuf/generated_message_reflection.h>
  33. #include <google/protobuf/map_entry_lite.h>
  34. #include <google/protobuf/map_type_handler.h>
  35. #include <google/protobuf/port.h>
  36. #include <google/protobuf/reflection_ops.h>
  37. #include <google/protobuf/unknown_field_set.h>
  38. #include <google/protobuf/wire_format_lite.h>
  39. #include <google/protobuf/port_def.inc>
  40. #ifdef SWIG
  41. #error "You cannot SWIG proto headers"
  42. #endif
  43. namespace google {
  44. namespace protobuf {
  45. class Arena;
  46. namespace internal {
  47. template <typename Derived, typename Key, typename Value,
  48. WireFormatLite::FieldType kKeyFieldType,
  49. WireFormatLite::FieldType kValueFieldType>
  50. class MapField;
  51. }
  52. } // namespace protobuf
  53. } // namespace google
  54. namespace google {
  55. namespace protobuf {
  56. namespace internal {
  57. // MapEntry is the returned google::protobuf::Message when calling AddMessage of
  58. // google::protobuf::Reflection. In order to let it work with generated message
  59. // reflection, its in-memory type is the same as generated message with the same
  60. // fields. However, in order to decide the in-memory type of key/value, we need
  61. // to know both their cpp type in generated api and proto type. In
  62. // implementation, all in-memory types have related wire format functions to
  63. // support except ArenaStringPtr. Therefore, we need to define another type with
  64. // supporting wire format functions. Since this type is only used as return type
  65. // of MapEntry accessors, it's named MapEntry accessor type.
  66. //
  67. // cpp type: the type visible to users in public API.
  68. // proto type: WireFormatLite::FieldType of the field.
  69. // in-memory type: type of the data member used to stored this field.
  70. // MapEntry accessor type: type used in MapEntry getters/mutators to access the
  71. // field.
  72. //
  73. // cpp type | proto type | in-memory type | MapEntry accessor type
  74. // int32 TYPE_INT32 int32 int32
  75. // int32 TYPE_FIXED32 int32 int32
  76. // string TYPE_STRING ArenaStringPtr string
  77. // FooEnum TYPE_ENUM int int
  78. // FooMessage TYPE_MESSAGE FooMessage* FooMessage
  79. //
  80. // The in-memory types of primitive types can be inferred from its proto type,
  81. // while we need to explicitly specify the cpp type if proto type is
  82. // TYPE_MESSAGE to infer the in-memory type.
  83. template <typename Derived, typename Key, typename Value,
  84. WireFormatLite::FieldType kKeyFieldType,
  85. WireFormatLite::FieldType kValueFieldType>
  86. class MapEntry : public MapEntryImpl<Derived, Message, Key, Value,
  87. kKeyFieldType, kValueFieldType> {
  88. public:
  89. constexpr MapEntry() : _internal_metadata_() {}
  90. explicit MapEntry(Arena* arena)
  91. : MapEntryImpl<Derived, Message, Key, Value, kKeyFieldType,
  92. kValueFieldType>(arena),
  93. _internal_metadata_(arena) {}
  94. ~MapEntry() {
  95. Message::_internal_metadata_.template Delete<UnknownFieldSet>();
  96. _internal_metadata_.Delete<UnknownFieldSet>();
  97. }
  98. typedef void InternalArenaConstructable_;
  99. typedef void DestructorSkippable_;
  100. typedef typename MapEntryImpl<Derived, Message, Key, Value, kKeyFieldType,
  101. kValueFieldType>::KeyTypeHandler KeyTypeHandler;
  102. typedef
  103. typename MapEntryImpl<Derived, Message, Key, Value, kKeyFieldType,
  104. kValueFieldType>::ValueTypeHandler ValueTypeHandler;
  105. size_t SpaceUsedLong() const override {
  106. size_t size = sizeof(Derived);
  107. size += KeyTypeHandler::SpaceUsedInMapEntryLong(this->key_);
  108. size += ValueTypeHandler::SpaceUsedInMapEntryLong(this->value_);
  109. return size;
  110. }
  111. InternalMetadata _internal_metadata_;
  112. private:
  113. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  114. template <typename C, typename K, typename V,
  115. WireFormatLite::FieldType k_wire_type, WireFormatLite::FieldType>
  116. friend class internal::MapField;
  117. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntry);
  118. };
  119. // Specialization for the full runtime
  120. template <typename Derived, typename Key, typename Value,
  121. WireFormatLite::FieldType kKeyFieldType,
  122. WireFormatLite::FieldType kValueFieldType>
  123. struct MapEntryHelper<
  124. MapEntry<Derived, Key, Value, kKeyFieldType, kValueFieldType> >
  125. : MapEntryHelper<
  126. MapEntryLite<Derived, Key, Value, kKeyFieldType, kValueFieldType> > {
  127. explicit MapEntryHelper(const MapPair<Key, Value>& map_pair)
  128. : MapEntryHelper<
  129. MapEntryLite<Derived, Key, Value, kKeyFieldType, kValueFieldType> >(
  130. map_pair) {}
  131. };
  132. template <typename Derived, typename K, typename V,
  133. WireFormatLite::FieldType key, WireFormatLite::FieldType value>
  134. struct DeconstructMapEntry<MapEntry<Derived, K, V, key, value> > {
  135. typedef K Key;
  136. typedef V Value;
  137. static constexpr WireFormatLite::FieldType kKeyFieldType = key;
  138. static constexpr WireFormatLite::FieldType kValueFieldType = value;
  139. };
  140. } // namespace internal
  141. } // namespace protobuf
  142. } // namespace google
  143. #include <google/protobuf/port_undef.inc>
  144. #endif // GOOGLE_PROTOBUF_MAP_ENTRY_H__