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 10 kB

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