map_field.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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_H__
  31. #define GOOGLE_PROTOBUF_MAP_FIELD_H__
  32. #include <atomic>
  33. #include <functional>
  34. #include <google/protobuf/arena.h>
  35. #include <google/protobuf/descriptor.h>
  36. #include <google/protobuf/generated_message_reflection.h>
  37. #include <google/protobuf/generated_message_util.h>
  38. #include <google/protobuf/map_entry.h>
  39. #include <google/protobuf/map_field_lite.h>
  40. #include <google/protobuf/map_type_handler.h>
  41. #include <google/protobuf/message.h>
  42. #include <google/protobuf/stubs/mutex.h>
  43. #include <google/protobuf/port.h>
  44. #include <google/protobuf/repeated_field.h>
  45. #include <google/protobuf/unknown_field_set.h>
  46. #include <google/protobuf/port_def.inc>
  47. #ifdef SWIG
  48. #error "You cannot SWIG proto headers"
  49. #endif
  50. namespace google {
  51. namespace protobuf {
  52. class DynamicMessage;
  53. class MapIterator;
  54. #define TYPE_CHECK(EXPECTEDTYPE, METHOD) \
  55. if (type() != EXPECTEDTYPE) { \
  56. GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" \
  57. << METHOD << " type does not match\n" \
  58. << " Expected : " \
  59. << FieldDescriptor::CppTypeName(EXPECTEDTYPE) << "\n" \
  60. << " Actual : " << FieldDescriptor::CppTypeName(type()); \
  61. }
  62. // MapKey is an union type for representing any possible
  63. // map key.
  64. class PROTOBUF_EXPORT MapKey {
  65. public:
  66. MapKey() : type_() {}
  67. MapKey(const MapKey& other) : type_() { CopyFrom(other); }
  68. MapKey& operator=(const MapKey& other) {
  69. CopyFrom(other);
  70. return *this;
  71. }
  72. ~MapKey() {
  73. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  74. val_.string_value_.Destruct();
  75. }
  76. }
  77. FieldDescriptor::CppType type() const {
  78. if (type_ == FieldDescriptor::CppType()) {
  79. GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n"
  80. << "MapKey::type MapKey is not initialized. "
  81. << "Call set methods to initialize MapKey.";
  82. }
  83. return type_;
  84. }
  85. void SetInt64Value(int64_t value) {
  86. SetType(FieldDescriptor::CPPTYPE_INT64);
  87. val_.int64_value_ = value;
  88. }
  89. void SetUInt64Value(uint64_t value) {
  90. SetType(FieldDescriptor::CPPTYPE_UINT64);
  91. val_.uint64_value_ = value;
  92. }
  93. void SetInt32Value(int32_t value) {
  94. SetType(FieldDescriptor::CPPTYPE_INT32);
  95. val_.int32_value_ = value;
  96. }
  97. void SetUInt32Value(uint32_t value) {
  98. SetType(FieldDescriptor::CPPTYPE_UINT32);
  99. val_.uint32_value_ = value;
  100. }
  101. void SetBoolValue(bool value) {
  102. SetType(FieldDescriptor::CPPTYPE_BOOL);
  103. val_.bool_value_ = value;
  104. }
  105. void SetStringValue(std::string val) {
  106. SetType(FieldDescriptor::CPPTYPE_STRING);
  107. *val_.string_value_.get_mutable() = std::move(val);
  108. }
  109. int64_t GetInt64Value() const {
  110. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapKey::GetInt64Value");
  111. return val_.int64_value_;
  112. }
  113. uint64_t GetUInt64Value() const {
  114. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapKey::GetUInt64Value");
  115. return val_.uint64_value_;
  116. }
  117. int32_t GetInt32Value() const {
  118. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapKey::GetInt32Value");
  119. return val_.int32_value_;
  120. }
  121. uint32_t GetUInt32Value() const {
  122. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapKey::GetUInt32Value");
  123. return val_.uint32_value_;
  124. }
  125. bool GetBoolValue() const {
  126. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapKey::GetBoolValue");
  127. return val_.bool_value_;
  128. }
  129. const std::string& GetStringValue() const {
  130. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapKey::GetStringValue");
  131. return val_.string_value_.get();
  132. }
  133. bool operator<(const MapKey& other) const {
  134. if (type_ != other.type_) {
  135. // We could define a total order that handles this case, but
  136. // there currently no need. So, for now, fail.
  137. GOOGLE_LOG(FATAL) << "Unsupported: type mismatch";
  138. }
  139. switch (type()) {
  140. case FieldDescriptor::CPPTYPE_DOUBLE:
  141. case FieldDescriptor::CPPTYPE_FLOAT:
  142. case FieldDescriptor::CPPTYPE_ENUM:
  143. case FieldDescriptor::CPPTYPE_MESSAGE:
  144. GOOGLE_LOG(FATAL) << "Unsupported";
  145. return false;
  146. case FieldDescriptor::CPPTYPE_STRING:
  147. return val_.string_value_.get() < other.val_.string_value_.get();
  148. case FieldDescriptor::CPPTYPE_INT64:
  149. return val_.int64_value_ < other.val_.int64_value_;
  150. case FieldDescriptor::CPPTYPE_INT32:
  151. return val_.int32_value_ < other.val_.int32_value_;
  152. case FieldDescriptor::CPPTYPE_UINT64:
  153. return val_.uint64_value_ < other.val_.uint64_value_;
  154. case FieldDescriptor::CPPTYPE_UINT32:
  155. return val_.uint32_value_ < other.val_.uint32_value_;
  156. case FieldDescriptor::CPPTYPE_BOOL:
  157. return val_.bool_value_ < other.val_.bool_value_;
  158. }
  159. return false;
  160. }
  161. bool operator==(const MapKey& other) const {
  162. if (type_ != other.type_) {
  163. // To be consistent with operator<, we don't allow this either.
  164. GOOGLE_LOG(FATAL) << "Unsupported: type mismatch";
  165. }
  166. switch (type()) {
  167. case FieldDescriptor::CPPTYPE_DOUBLE:
  168. case FieldDescriptor::CPPTYPE_FLOAT:
  169. case FieldDescriptor::CPPTYPE_ENUM:
  170. case FieldDescriptor::CPPTYPE_MESSAGE:
  171. GOOGLE_LOG(FATAL) << "Unsupported";
  172. break;
  173. case FieldDescriptor::CPPTYPE_STRING:
  174. return val_.string_value_.get() == other.val_.string_value_.get();
  175. case FieldDescriptor::CPPTYPE_INT64:
  176. return val_.int64_value_ == other.val_.int64_value_;
  177. case FieldDescriptor::CPPTYPE_INT32:
  178. return val_.int32_value_ == other.val_.int32_value_;
  179. case FieldDescriptor::CPPTYPE_UINT64:
  180. return val_.uint64_value_ == other.val_.uint64_value_;
  181. case FieldDescriptor::CPPTYPE_UINT32:
  182. return val_.uint32_value_ == other.val_.uint32_value_;
  183. case FieldDescriptor::CPPTYPE_BOOL:
  184. return val_.bool_value_ == other.val_.bool_value_;
  185. }
  186. GOOGLE_LOG(FATAL) << "Can't get here.";
  187. return false;
  188. }
  189. void CopyFrom(const MapKey& other) {
  190. SetType(other.type());
  191. switch (type_) {
  192. case FieldDescriptor::CPPTYPE_DOUBLE:
  193. case FieldDescriptor::CPPTYPE_FLOAT:
  194. case FieldDescriptor::CPPTYPE_ENUM:
  195. case FieldDescriptor::CPPTYPE_MESSAGE:
  196. GOOGLE_LOG(FATAL) << "Unsupported";
  197. break;
  198. case FieldDescriptor::CPPTYPE_STRING:
  199. *val_.string_value_.get_mutable() = other.val_.string_value_.get();
  200. break;
  201. case FieldDescriptor::CPPTYPE_INT64:
  202. val_.int64_value_ = other.val_.int64_value_;
  203. break;
  204. case FieldDescriptor::CPPTYPE_INT32:
  205. val_.int32_value_ = other.val_.int32_value_;
  206. break;
  207. case FieldDescriptor::CPPTYPE_UINT64:
  208. val_.uint64_value_ = other.val_.uint64_value_;
  209. break;
  210. case FieldDescriptor::CPPTYPE_UINT32:
  211. val_.uint32_value_ = other.val_.uint32_value_;
  212. break;
  213. case FieldDescriptor::CPPTYPE_BOOL:
  214. val_.bool_value_ = other.val_.bool_value_;
  215. break;
  216. }
  217. }
  218. private:
  219. template <typename K, typename V>
  220. friend class internal::TypeDefinedMapFieldBase;
  221. friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
  222. friend class internal::DynamicMapField;
  223. union KeyValue {
  224. KeyValue() {}
  225. internal::ExplicitlyConstructed<std::string> string_value_;
  226. int64_t int64_value_;
  227. int32_t int32_value_;
  228. uint64_t uint64_value_;
  229. uint32_t uint32_value_;
  230. bool bool_value_;
  231. } val_;
  232. void SetType(FieldDescriptor::CppType type) {
  233. if (type_ == type) return;
  234. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  235. val_.string_value_.Destruct();
  236. }
  237. type_ = type;
  238. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  239. val_.string_value_.DefaultConstruct();
  240. }
  241. }
  242. // type_ is 0 or a valid FieldDescriptor::CppType.
  243. // Use "CppType()" to indicate zero.
  244. FieldDescriptor::CppType type_;
  245. };
  246. } // namespace protobuf
  247. } // namespace google
  248. namespace std {
  249. template <>
  250. struct hash<::PROTOBUF_NAMESPACE_ID::MapKey> {
  251. size_t operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key) const {
  252. switch (map_key.type()) {
  253. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_DOUBLE:
  254. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_FLOAT:
  255. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_ENUM:
  256. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_MESSAGE:
  257. GOOGLE_LOG(FATAL) << "Unsupported";
  258. break;
  259. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_STRING:
  260. return hash<std::string>()(map_key.GetStringValue());
  261. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT64: {
  262. auto value = map_key.GetInt64Value();
  263. return hash<decltype(value)>()(value);
  264. }
  265. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT32: {
  266. auto value = map_key.GetInt32Value();
  267. return hash<decltype(value)>()(map_key.GetInt32Value());
  268. }
  269. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT64: {
  270. auto value = map_key.GetUInt64Value();
  271. return hash<decltype(value)>()(map_key.GetUInt64Value());
  272. }
  273. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT32: {
  274. auto value = map_key.GetUInt32Value();
  275. return hash<decltype(value)>()(map_key.GetUInt32Value());
  276. }
  277. case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_BOOL: {
  278. return hash<bool>()(map_key.GetBoolValue());
  279. }
  280. }
  281. GOOGLE_LOG(FATAL) << "Can't get here.";
  282. return 0;
  283. }
  284. bool operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key1,
  285. const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key2) const {
  286. return map_key1 < map_key2;
  287. }
  288. };
  289. } // namespace std
  290. namespace google {
  291. namespace protobuf {
  292. namespace internal {
  293. class ContendedMapCleanTest;
  294. class GeneratedMessageReflection;
  295. class MapFieldAccessor;
  296. // This class provides access to map field using reflection, which is the same
  297. // as those provided for RepeatedPtrField<Message>. It is used for internal
  298. // reflection implementation only. Users should never use this directly.
  299. class PROTOBUF_EXPORT MapFieldBase {
  300. public:
  301. MapFieldBase()
  302. : arena_(NULL), repeated_field_(NULL), state_(STATE_MODIFIED_MAP) {}
  303. // This constructor is for constant initialized global instances.
  304. // It uses a linker initialized mutex, so it is not compatible with regular
  305. // runtime instances.
  306. // Except in MSVC, where we can't have a constinit mutex.
  307. explicit constexpr MapFieldBase(ConstantInitialized)
  308. : arena_(nullptr),
  309. repeated_field_(nullptr),
  310. mutex_(GOOGLE_PROTOBUF_LINKER_INITIALIZED),
  311. state_(STATE_MODIFIED_MAP) {}
  312. explicit MapFieldBase(Arena* arena)
  313. : arena_(arena), repeated_field_(nullptr), state_(STATE_MODIFIED_MAP) {}
  314. virtual ~MapFieldBase();
  315. // Returns reference to internal repeated field. Data written using
  316. // Map's api prior to calling this function is guarantted to be
  317. // included in repeated field.
  318. const RepeatedPtrFieldBase& GetRepeatedField() const;
  319. // Like above. Returns mutable pointer to the internal repeated field.
  320. RepeatedPtrFieldBase* MutableRepeatedField();
  321. // Pure virtual map APIs for Map Reflection.
  322. virtual bool ContainsMapKey(const MapKey& map_key) const = 0;
  323. virtual bool InsertOrLookupMapValue(const MapKey& map_key,
  324. MapValueRef* val) = 0;
  325. virtual bool LookupMapValue(const MapKey& map_key,
  326. MapValueConstRef* val) const = 0;
  327. bool LookupMapValue(const MapKey&, MapValueRef*) const = delete;
  328. // Returns whether changes to the map are reflected in the repeated field.
  329. bool IsRepeatedFieldValid() const;
  330. // Insures operations after won't get executed before calling this.
  331. bool IsMapValid() const;
  332. virtual bool DeleteMapValue(const MapKey& map_key) = 0;
  333. virtual bool EqualIterator(const MapIterator& a,
  334. const MapIterator& b) const = 0;
  335. virtual void MapBegin(MapIterator* map_iter) const = 0;
  336. virtual void MapEnd(MapIterator* map_iter) const = 0;
  337. virtual void MergeFrom(const MapFieldBase& other) = 0;
  338. virtual void Swap(MapFieldBase* other);
  339. virtual void UnsafeShallowSwap(MapFieldBase* other);
  340. // Sync Map with repeated field and returns the size of map.
  341. virtual int size() const = 0;
  342. virtual void Clear() = 0;
  343. // Returns the number of bytes used by the repeated field, excluding
  344. // sizeof(*this)
  345. size_t SpaceUsedExcludingSelfLong() const;
  346. int SpaceUsedExcludingSelf() const {
  347. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  348. }
  349. protected:
  350. // Gets the size of space used by map field.
  351. virtual size_t SpaceUsedExcludingSelfNoLock() const;
  352. // Synchronizes the content in Map to RepeatedPtrField if there is any change
  353. // to Map after last synchronization.
  354. void SyncRepeatedFieldWithMap() const;
  355. virtual void SyncRepeatedFieldWithMapNoLock() const;
  356. // Synchronizes the content in RepeatedPtrField to Map if there is any change
  357. // to RepeatedPtrField after last synchronization.
  358. void SyncMapWithRepeatedField() const;
  359. virtual void SyncMapWithRepeatedFieldNoLock() const {}
  360. // Tells MapFieldBase that there is new change to Map.
  361. void SetMapDirty();
  362. // Tells MapFieldBase that there is new change to RepeatedPtrField.
  363. void SetRepeatedDirty();
  364. // Provides derived class the access to repeated field.
  365. void* MutableRepeatedPtrField() const;
  366. void InternalSwap(MapFieldBase* other);
  367. // Support thread sanitizer (tsan) by making const / mutable races
  368. // more apparent. If one thread calls MutableAccess() while another
  369. // thread calls either ConstAccess() or MutableAccess(), on the same
  370. // MapFieldBase-derived object, and there is no synchronization going
  371. // on between them, tsan will alert.
  372. #if defined(__SANITIZE_THREAD__) || defined(THREAD_SANITIZER)
  373. void ConstAccess() const { GOOGLE_CHECK_EQ(seq1_, seq2_); }
  374. void MutableAccess() {
  375. if (seq1_ & 1) {
  376. seq2_ = ++seq1_;
  377. } else {
  378. seq1_ = ++seq2_;
  379. }
  380. }
  381. unsigned int seq1_ = 0, seq2_ = 0;
  382. #else
  383. void ConstAccess() const {}
  384. void MutableAccess() {}
  385. #endif
  386. enum State {
  387. STATE_MODIFIED_MAP = 0, // map has newly added data that has not been
  388. // synchronized to repeated field
  389. STATE_MODIFIED_REPEATED = 1, // repeated field has newly added data that
  390. // has not been synchronized to map
  391. CLEAN = 2, // data in map and repeated field are same
  392. };
  393. Arena* arena_;
  394. mutable RepeatedPtrField<Message>* repeated_field_;
  395. mutable internal::WrappedMutex
  396. mutex_; // The thread to synchronize map and repeated field
  397. // needs to get lock first;
  398. mutable std::atomic<State> state_;
  399. private:
  400. friend class ContendedMapCleanTest;
  401. friend class GeneratedMessageReflection;
  402. friend class MapFieldAccessor;
  403. friend class ::PROTOBUF_NAMESPACE_ID::Reflection;
  404. friend class ::PROTOBUF_NAMESPACE_ID::DynamicMessage;
  405. // Virtual helper methods for MapIterator. MapIterator doesn't have the
  406. // type helper for key and value. Call these help methods to deal with
  407. // different types. Real helper methods are implemented in
  408. // TypeDefinedMapFieldBase.
  409. friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
  410. // Allocate map<...>::iterator for MapIterator.
  411. virtual void InitializeIterator(MapIterator* map_iter) const = 0;
  412. // DeleteIterator() is called by the destructor of MapIterator only.
  413. // It deletes map<...>::iterator for MapIterator.
  414. virtual void DeleteIterator(MapIterator* map_iter) const = 0;
  415. // Copy the map<...>::iterator from other_iterator to
  416. // this_iterator.
  417. virtual void CopyIterator(MapIterator* this_iterator,
  418. const MapIterator& other_iterator) const = 0;
  419. // IncreaseIterator() is called by operator++() of MapIterator only.
  420. // It implements the ++ operator of MapIterator.
  421. virtual void IncreaseIterator(MapIterator* map_iter) const = 0;
  422. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldBase);
  423. };
  424. // This class provides common Map Reflection implementations for generated
  425. // message and dynamic message.
  426. template <typename Key, typename T>
  427. class TypeDefinedMapFieldBase : public MapFieldBase {
  428. public:
  429. TypeDefinedMapFieldBase() {}
  430. // This constructor is for constant initialized global instances.
  431. // It uses a linker initialized mutex, so it is not compatible with regular
  432. // runtime instances.
  433. explicit constexpr TypeDefinedMapFieldBase(ConstantInitialized tag)
  434. : MapFieldBase(tag) {}
  435. explicit TypeDefinedMapFieldBase(Arena* arena) : MapFieldBase(arena) {}
  436. ~TypeDefinedMapFieldBase() override {}
  437. void MapBegin(MapIterator* map_iter) const override;
  438. void MapEnd(MapIterator* map_iter) const override;
  439. bool EqualIterator(const MapIterator& a, const MapIterator& b) const override;
  440. virtual const Map<Key, T>& GetMap() const = 0;
  441. virtual Map<Key, T>* MutableMap() = 0;
  442. protected:
  443. typename Map<Key, T>::const_iterator& InternalGetIterator(
  444. const MapIterator* map_iter) const;
  445. private:
  446. void InitializeIterator(MapIterator* map_iter) const override;
  447. void DeleteIterator(MapIterator* map_iter) const override;
  448. void CopyIterator(MapIterator* this_iteratorm,
  449. const MapIterator& that_iterator) const override;
  450. void IncreaseIterator(MapIterator* map_iter) const override;
  451. virtual void SetMapIteratorValue(MapIterator* map_iter) const = 0;
  452. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeDefinedMapFieldBase);
  453. };
  454. // This class provides access to map field using generated api. It is used for
  455. // internal generated message implementation only. Users should never use this
  456. // directly.
  457. template <typename Derived, typename Key, typename T,
  458. WireFormatLite::FieldType kKeyFieldType,
  459. WireFormatLite::FieldType kValueFieldType>
  460. class MapField : public TypeDefinedMapFieldBase<Key, T> {
  461. // Provide utilities to parse/serialize key/value. Provide utilities to
  462. // manipulate internal stored type.
  463. typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
  464. typedef MapTypeHandler<kValueFieldType, T> ValueTypeHandler;
  465. // Define message type for internal repeated field.
  466. typedef Derived EntryType;
  467. // Define abbreviation for parent MapFieldLite
  468. typedef MapFieldLite<Derived, Key, T, kKeyFieldType, kValueFieldType>
  469. MapFieldLiteType;
  470. // Enum needs to be handled differently from other types because it has
  471. // different exposed type in Map's api and repeated field's api. For
  472. // details see the comment in the implementation of
  473. // SyncMapWithRepeatedFieldNoLock.
  474. static constexpr bool kIsValueEnum = ValueTypeHandler::kIsEnum;
  475. typedef typename MapIf<kIsValueEnum, T, const T&>::type CastValueType;
  476. public:
  477. typedef typename Derived::SuperType EntryTypeTrait;
  478. typedef Map<Key, T> MapType;
  479. MapField() {}
  480. // This constructor is for constant initialized global instances.
  481. // It uses a linker initialized mutex, so it is not compatible with regular
  482. // runtime instances.
  483. explicit constexpr MapField(ConstantInitialized tag)
  484. : TypeDefinedMapFieldBase<Key, T>(tag), impl_() {}
  485. explicit MapField(Arena* arena)
  486. : TypeDefinedMapFieldBase<Key, T>(arena), impl_(arena) {}
  487. // Implement MapFieldBase
  488. bool ContainsMapKey(const MapKey& map_key) const override;
  489. bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) override;
  490. bool LookupMapValue(const MapKey& map_key,
  491. MapValueConstRef* val) const override;
  492. bool LookupMapValue(const MapKey&, MapValueRef*) const = delete;
  493. bool DeleteMapValue(const MapKey& map_key) override;
  494. const Map<Key, T>& GetMap() const override {
  495. MapFieldBase::SyncMapWithRepeatedField();
  496. return impl_.GetMap();
  497. }
  498. Map<Key, T>* MutableMap() override {
  499. MapFieldBase::SyncMapWithRepeatedField();
  500. Map<Key, T>* result = impl_.MutableMap();
  501. MapFieldBase::SetMapDirty();
  502. return result;
  503. }
  504. int size() const override;
  505. void Clear() override;
  506. void MergeFrom(const MapFieldBase& other) override;
  507. void Swap(MapFieldBase* other) override;
  508. void UnsafeShallowSwap(MapFieldBase* other) override;
  509. void InternalSwap(MapField* other);
  510. // Used in the implementation of parsing. Caller should take the ownership iff
  511. // arena_ is NULL.
  512. EntryType* NewEntry() const { return impl_.NewEntry(); }
  513. // Used in the implementation of serializing enum value type. Caller should
  514. // take the ownership iff arena_ is NULL.
  515. EntryType* NewEnumEntryWrapper(const Key& key, const T t) const {
  516. return impl_.NewEnumEntryWrapper(key, t);
  517. }
  518. // Used in the implementation of serializing other value types. Caller should
  519. // take the ownership iff arena_ is NULL.
  520. EntryType* NewEntryWrapper(const Key& key, const T& t) const {
  521. return impl_.NewEntryWrapper(key, t);
  522. }
  523. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  524. return impl_._InternalParse(ptr, ctx);
  525. }
  526. template <typename UnknownType>
  527. const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
  528. bool (*is_valid)(int), uint32_t field_num,
  529. InternalMetadata* metadata) {
  530. return impl_.template ParseWithEnumValidation<UnknownType>(
  531. ptr, ctx, is_valid, field_num, metadata);
  532. }
  533. private:
  534. MapFieldLiteType impl_;
  535. typedef void InternalArenaConstructable_;
  536. typedef void DestructorSkippable_;
  537. // Implements MapFieldBase
  538. void SyncRepeatedFieldWithMapNoLock() const override;
  539. void SyncMapWithRepeatedFieldNoLock() const override;
  540. size_t SpaceUsedExcludingSelfNoLock() const override;
  541. void SetMapIteratorValue(MapIterator* map_iter) const override;
  542. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  543. friend class MapFieldStateTest; // For testing, it needs raw access to impl_
  544. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapField);
  545. };
  546. template <typename Derived, typename Key, typename T,
  547. WireFormatLite::FieldType key_wire_type,
  548. WireFormatLite::FieldType value_wire_type>
  549. bool AllAreInitialized(
  550. const MapField<Derived, Key, T, key_wire_type, value_wire_type>& field) {
  551. const auto& t = field.GetMap();
  552. for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
  553. ++it) {
  554. if (!it->second.IsInitialized()) return false;
  555. }
  556. return true;
  557. }
  558. template <typename T, typename Key, typename Value,
  559. WireFormatLite::FieldType kKeyFieldType,
  560. WireFormatLite::FieldType kValueFieldType>
  561. struct MapEntryToMapField<
  562. MapEntry<T, Key, Value, kKeyFieldType, kValueFieldType>> {
  563. typedef MapField<T, Key, Value, kKeyFieldType, kValueFieldType> MapFieldType;
  564. };
  565. class PROTOBUF_EXPORT DynamicMapField
  566. : public TypeDefinedMapFieldBase<MapKey, MapValueRef> {
  567. public:
  568. explicit DynamicMapField(const Message* default_entry);
  569. DynamicMapField(const Message* default_entry, Arena* arena);
  570. ~DynamicMapField() override;
  571. // Implement MapFieldBase
  572. bool ContainsMapKey(const MapKey& map_key) const override;
  573. bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) override;
  574. bool LookupMapValue(const MapKey& map_key,
  575. MapValueConstRef* val) const override;
  576. bool LookupMapValue(const MapKey&, MapValueRef*) const = delete;
  577. bool DeleteMapValue(const MapKey& map_key) override;
  578. void MergeFrom(const MapFieldBase& other) override;
  579. void Swap(MapFieldBase* other) override;
  580. void UnsafeShallowSwap(MapFieldBase* other) override { Swap(other); }
  581. const Map<MapKey, MapValueRef>& GetMap() const override;
  582. Map<MapKey, MapValueRef>* MutableMap() override;
  583. int size() const override;
  584. void Clear() override;
  585. private:
  586. Map<MapKey, MapValueRef> map_;
  587. const Message* default_entry_;
  588. void AllocateMapValue(MapValueRef* map_val);
  589. // Implements MapFieldBase
  590. void SyncRepeatedFieldWithMapNoLock() const override;
  591. void SyncMapWithRepeatedFieldNoLock() const override;
  592. size_t SpaceUsedExcludingSelfNoLock() const override;
  593. void SetMapIteratorValue(MapIterator* map_iter) const override;
  594. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMapField);
  595. };
  596. } // namespace internal
  597. // MapValueConstRef points to a map value. Users can NOT modify
  598. // the map value.
  599. class PROTOBUF_EXPORT MapValueConstRef {
  600. public:
  601. MapValueConstRef() : data_(nullptr), type_() {}
  602. int64_t GetInt64Value() const {
  603. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64,
  604. "MapValueConstRef::GetInt64Value");
  605. return *reinterpret_cast<int64_t*>(data_);
  606. }
  607. uint64_t GetUInt64Value() const {
  608. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64,
  609. "MapValueConstRef::GetUInt64Value");
  610. return *reinterpret_cast<uint64_t*>(data_);
  611. }
  612. int32_t GetInt32Value() const {
  613. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32,
  614. "MapValueConstRef::GetInt32Value");
  615. return *reinterpret_cast<int32_t*>(data_);
  616. }
  617. uint32_t GetUInt32Value() const {
  618. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32,
  619. "MapValueConstRef::GetUInt32Value");
  620. return *reinterpret_cast<uint32_t*>(data_);
  621. }
  622. bool GetBoolValue() const {
  623. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueConstRef::GetBoolValue");
  624. return *reinterpret_cast<bool*>(data_);
  625. }
  626. int GetEnumValue() const {
  627. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueConstRef::GetEnumValue");
  628. return *reinterpret_cast<int*>(data_);
  629. }
  630. const std::string& GetStringValue() const {
  631. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
  632. "MapValueConstRef::GetStringValue");
  633. return *reinterpret_cast<std::string*>(data_);
  634. }
  635. float GetFloatValue() const {
  636. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT,
  637. "MapValueConstRef::GetFloatValue");
  638. return *reinterpret_cast<float*>(data_);
  639. }
  640. double GetDoubleValue() const {
  641. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE,
  642. "MapValueConstRef::GetDoubleValue");
  643. return *reinterpret_cast<double*>(data_);
  644. }
  645. const Message& GetMessageValue() const {
  646. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  647. "MapValueConstRef::GetMessageValue");
  648. return *reinterpret_cast<Message*>(data_);
  649. }
  650. protected:
  651. // data_ point to a map value. MapValueConstRef does not
  652. // own this value.
  653. void* data_;
  654. // type_ is 0 or a valid FieldDescriptor::CppType.
  655. // Use "CppType()" to indicate zero.
  656. FieldDescriptor::CppType type_;
  657. FieldDescriptor::CppType type() const {
  658. if (type_ == FieldDescriptor::CppType() || data_ == nullptr) {
  659. GOOGLE_LOG(FATAL)
  660. << "Protocol Buffer map usage error:\n"
  661. << "MapValueConstRef::type MapValueConstRef is not initialized.";
  662. }
  663. return type_;
  664. }
  665. private:
  666. template <typename Derived, typename K, typename V,
  667. internal::WireFormatLite::FieldType key_wire_type,
  668. internal::WireFormatLite::FieldType value_wire_type>
  669. friend class internal::MapField;
  670. template <typename K, typename V>
  671. friend class internal::TypeDefinedMapFieldBase;
  672. friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
  673. friend class Reflection;
  674. friend class internal::DynamicMapField;
  675. void SetType(FieldDescriptor::CppType type) { type_ = type; }
  676. void SetValue(const void* val) { data_ = const_cast<void*>(val); }
  677. void CopyFrom(const MapValueConstRef& other) {
  678. type_ = other.type_;
  679. data_ = other.data_;
  680. }
  681. };
  682. // MapValueRef points to a map value. Users are able to modify
  683. // the map value.
  684. class PROTOBUF_EXPORT MapValueRef final : public MapValueConstRef {
  685. public:
  686. MapValueRef() {}
  687. void SetInt64Value(int64_t value) {
  688. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::SetInt64Value");
  689. *reinterpret_cast<int64_t*>(data_) = value;
  690. }
  691. void SetUInt64Value(uint64_t value) {
  692. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::SetUInt64Value");
  693. *reinterpret_cast<uint64_t*>(data_) = value;
  694. }
  695. void SetInt32Value(int32_t value) {
  696. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::SetInt32Value");
  697. *reinterpret_cast<int32_t*>(data_) = value;
  698. }
  699. void SetUInt32Value(uint32_t value) {
  700. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::SetUInt32Value");
  701. *reinterpret_cast<uint32_t*>(data_) = value;
  702. }
  703. void SetBoolValue(bool value) {
  704. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::SetBoolValue");
  705. *reinterpret_cast<bool*>(data_) = value;
  706. }
  707. // TODO(jieluo) - Checks that enum is member.
  708. void SetEnumValue(int value) {
  709. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue");
  710. *reinterpret_cast<int*>(data_) = value;
  711. }
  712. void SetStringValue(const std::string& value) {
  713. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue");
  714. *reinterpret_cast<std::string*>(data_) = value;
  715. }
  716. void SetFloatValue(float value) {
  717. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue");
  718. *reinterpret_cast<float*>(data_) = value;
  719. }
  720. void SetDoubleValue(double value) {
  721. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::SetDoubleValue");
  722. *reinterpret_cast<double*>(data_) = value;
  723. }
  724. Message* MutableMessageValue() {
  725. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  726. "MapValueRef::MutableMessageValue");
  727. return reinterpret_cast<Message*>(data_);
  728. }
  729. private:
  730. friend class internal::DynamicMapField;
  731. // Only used in DynamicMapField
  732. void DeleteData() {
  733. switch (type_) {
  734. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  735. case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  736. delete reinterpret_cast<TYPE*>(data_); \
  737. break; \
  738. }
  739. HANDLE_TYPE(INT32, int32_t);
  740. HANDLE_TYPE(INT64, int64_t);
  741. HANDLE_TYPE(UINT32, uint32_t);
  742. HANDLE_TYPE(UINT64, uint64_t);
  743. HANDLE_TYPE(DOUBLE, double);
  744. HANDLE_TYPE(FLOAT, float);
  745. HANDLE_TYPE(BOOL, bool);
  746. HANDLE_TYPE(STRING, std::string);
  747. HANDLE_TYPE(ENUM, int32_t);
  748. HANDLE_TYPE(MESSAGE, Message);
  749. #undef HANDLE_TYPE
  750. }
  751. }
  752. };
  753. #undef TYPE_CHECK
  754. class PROTOBUF_EXPORT MapIterator {
  755. public:
  756. MapIterator(Message* message, const FieldDescriptor* field) {
  757. const Reflection* reflection = message->GetReflection();
  758. map_ = reflection->MutableMapData(message, field);
  759. key_.SetType(field->message_type()->FindFieldByName("key")->cpp_type());
  760. value_.SetType(field->message_type()->FindFieldByName("value")->cpp_type());
  761. map_->InitializeIterator(this);
  762. }
  763. MapIterator(const MapIterator& other) {
  764. map_ = other.map_;
  765. map_->InitializeIterator(this);
  766. map_->CopyIterator(this, other);
  767. }
  768. ~MapIterator() { map_->DeleteIterator(this); }
  769. MapIterator& operator=(const MapIterator& other) {
  770. map_ = other.map_;
  771. map_->CopyIterator(this, other);
  772. return *this;
  773. }
  774. friend bool operator==(const MapIterator& a, const MapIterator& b) {
  775. return a.map_->EqualIterator(a, b);
  776. }
  777. friend bool operator!=(const MapIterator& a, const MapIterator& b) {
  778. return !a.map_->EqualIterator(a, b);
  779. }
  780. MapIterator& operator++() {
  781. map_->IncreaseIterator(this);
  782. return *this;
  783. }
  784. MapIterator operator++(int) {
  785. // iter_ is copied from Map<...>::iterator, no need to
  786. // copy from its self again. Use the same implementation
  787. // with operator++()
  788. map_->IncreaseIterator(this);
  789. return *this;
  790. }
  791. const MapKey& GetKey() { return key_; }
  792. const MapValueRef& GetValueRef() { return value_; }
  793. MapValueRef* MutableValueRef() {
  794. map_->SetMapDirty();
  795. return &value_;
  796. }
  797. private:
  798. template <typename Key, typename T>
  799. friend class internal::TypeDefinedMapFieldBase;
  800. friend class internal::DynamicMapField;
  801. template <typename Derived, typename Key, typename T,
  802. internal::WireFormatLite::FieldType kKeyFieldType,
  803. internal::WireFormatLite::FieldType kValueFieldType>
  804. friend class internal::MapField;
  805. // reinterpret_cast from heap-allocated Map<...>::iterator*. MapIterator owns
  806. // the iterator. It is allocated by MapField<...>::InitializeIterator() called
  807. // in constructor and deleted by MapField<...>::DeleteIterator() called in
  808. // destructor.
  809. void* iter_;
  810. // Point to a MapField to call helper methods implemented in MapField.
  811. // MapIterator does not own this object.
  812. internal::MapFieldBase* map_;
  813. MapKey key_;
  814. MapValueRef value_;
  815. };
  816. } // namespace protobuf
  817. } // namespace google
  818. #include <google/protobuf/port_undef.inc>
  819. #endif // GOOGLE_PROTOBUF_MAP_FIELD_H__