You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

map_field_lite.h 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_FIELD_LITE_H__
  31. #define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__
  32. #include <type_traits>
  33. #include <google/protobuf/io/coded_stream.h>
  34. #include <google/protobuf/port.h>
  35. #include <google/protobuf/map.h>
  36. #include <google/protobuf/map_entry_lite.h>
  37. #include <google/protobuf/parse_context.h>
  38. #include <google/protobuf/wire_format_lite.h>
  39. // Must be included last.
  40. #include <google/protobuf/port_def.inc>
  41. #ifdef SWIG
  42. #error "You cannot SWIG proto headers"
  43. #endif
  44. namespace google {
  45. namespace protobuf {
  46. namespace internal {
  47. #ifndef NDEBUG
  48. void MapFieldLiteNotDestructed(void* map_field_lite);
  49. #endif
  50. // This class provides access to map field using generated api. It is used for
  51. // internal generated message implementation only. Users should never use this
  52. // directly.
  53. template <typename Derived, typename Key, typename T,
  54. WireFormatLite::FieldType key_wire_type,
  55. WireFormatLite::FieldType value_wire_type>
  56. class MapFieldLite {
  57. // Define message type for internal repeated field.
  58. typedef Derived EntryType;
  59. public:
  60. typedef Map<Key, T> MapType;
  61. constexpr MapFieldLite() : map_() {}
  62. explicit MapFieldLite(Arena* arena) : map_(arena) {}
  63. MapFieldLite(ArenaInitialized, Arena* arena) : MapFieldLite(arena) {}
  64. #ifdef NDEBUG
  65. void Destruct() { map_.~Map(); }
  66. ~MapFieldLite() {}
  67. #else
  68. void Destruct() {
  69. // We want to destruct the map in such a way that we can verify
  70. // that we've done that, but also be sure that we've deallocated
  71. // everything (as opposed to leaving an allocation behind with no
  72. // data in it, as would happen if a vector was resize'd to zero.
  73. // Map::Swap with an empty map accomplishes that.
  74. decltype(map_) swapped_map(map_.arena());
  75. map_.InternalSwap(swapped_map);
  76. }
  77. ~MapFieldLite() {
  78. if (map_.arena() == nullptr && !map_.empty()) {
  79. MapFieldLiteNotDestructed(this);
  80. }
  81. }
  82. #endif
  83. // Accessors
  84. const Map<Key, T>& GetMap() const { return map_; }
  85. Map<Key, T>* MutableMap() { return &map_; }
  86. // Convenient methods for generated message implementation.
  87. int size() const { return static_cast<int>(map_.size()); }
  88. void Clear() { return map_.clear(); }
  89. void MergeFrom(const MapFieldLite& other) {
  90. for (typename Map<Key, T>::const_iterator it = other.map_.begin();
  91. it != other.map_.end(); ++it) {
  92. map_[it->first] = it->second;
  93. }
  94. }
  95. void Swap(MapFieldLite* other) { map_.swap(other->map_); }
  96. void InternalSwap(MapFieldLite* other) { map_.InternalSwap(other->map_); }
  97. // Used in the implementation of parsing. Caller should take the ownership iff
  98. // arena_ is nullptr.
  99. EntryType* NewEntry() const {
  100. return Arena::CreateMessage<EntryType>(map_.arena());
  101. }
  102. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  103. typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
  104. return parser._InternalParse(ptr, ctx);
  105. }
  106. template <typename UnknownType>
  107. const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
  108. bool (*is_valid)(int), uint32_t field_num,
  109. InternalMetadata* metadata) {
  110. typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
  111. return parser.template ParseWithEnumValidation<UnknownType>(
  112. ptr, ctx, is_valid, field_num, metadata);
  113. }
  114. private:
  115. typedef void DestructorSkippable_;
  116. // map_ is inside an anonymous union so we can explicitly control its
  117. // destruction
  118. union {
  119. Map<Key, T> map_;
  120. };
  121. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  122. };
  123. template <typename UnknownType, typename T>
  124. struct EnumParseWrapper {
  125. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  126. return map_field->template ParseWithEnumValidation<UnknownType>(
  127. ptr, ctx, is_valid, field_num, metadata);
  128. }
  129. T* map_field;
  130. bool (*is_valid)(int);
  131. uint32_t field_num;
  132. InternalMetadata* metadata;
  133. };
  134. // Helper function because the typenames of maps are horrendous to print. This
  135. // leverages compiler type deduction, to keep all type data out of the
  136. // generated code
  137. template <typename UnknownType, typename T>
  138. EnumParseWrapper<UnknownType, T> InitEnumParseWrapper(
  139. T* map_field, bool (*is_valid)(int), uint32_t field_num,
  140. InternalMetadata* metadata) {
  141. return EnumParseWrapper<UnknownType, T>{map_field, is_valid, field_num,
  142. metadata};
  143. }
  144. // True if IsInitialized() is true for value field in all elements of t. T is
  145. // expected to be message. It's useful to have this helper here to keep the
  146. // protobuf compiler from ever having to emit loops in IsInitialized() methods.
  147. // We want the C++ compiler to inline this or not as it sees fit.
  148. template <typename Derived, typename Key, typename T,
  149. WireFormatLite::FieldType key_wire_type,
  150. WireFormatLite::FieldType value_wire_type>
  151. bool AllAreInitialized(const MapFieldLite<Derived, Key, T, key_wire_type,
  152. value_wire_type>& field) {
  153. const auto& t = field.GetMap();
  154. for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
  155. ++it) {
  156. if (!it->second.IsInitialized()) return false;
  157. }
  158. return true;
  159. }
  160. template <typename MEntry>
  161. struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {};
  162. template <typename T, typename Key, typename Value,
  163. WireFormatLite::FieldType kKeyFieldType,
  164. WireFormatLite::FieldType kValueFieldType>
  165. struct MapEntryToMapField<
  166. MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType>> {
  167. typedef MapFieldLite<
  168. MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType>, Key, Value,
  169. kKeyFieldType, kValueFieldType>
  170. MapFieldType;
  171. };
  172. #ifndef NDEBUG
  173. inline PROTOBUF_NOINLINE void MapFieldLiteNotDestructed(void* map_field_lite) {
  174. bool proper_destruct = false;
  175. GOOGLE_CHECK(proper_destruct) << map_field_lite;
  176. }
  177. #endif
  178. } // namespace internal
  179. } // namespace protobuf
  180. } // namespace google
  181. #include <google/protobuf/port_undef.inc>
  182. #endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__