map_field.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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_(nullptr), repeated_field_(nullptr), 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. // Swaps state_ with another MapFieldBase
  423. void SwapState(MapFieldBase* other);
  424. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldBase);
  425. };
  426. // This class provides common Map Reflection implementations for generated
  427. // message and dynamic message.
  428. template <typename Key, typename T>
  429. class TypeDefinedMapFieldBase : public MapFieldBase {
  430. public:
  431. TypeDefinedMapFieldBase() {}
  432. // This constructor is for constant initialized global instances.
  433. // It uses a linker initialized mutex, so it is not compatible with regular
  434. // runtime instances.
  435. explicit constexpr TypeDefinedMapFieldBase(ConstantInitialized tag)
  436. : MapFieldBase(tag) {}
  437. explicit TypeDefinedMapFieldBase(Arena* arena) : MapFieldBase(arena) {}
  438. ~TypeDefinedMapFieldBase() override {}
  439. void MapBegin(MapIterator* map_iter) const override;
  440. void MapEnd(MapIterator* map_iter) const override;
  441. bool EqualIterator(const MapIterator& a, const MapIterator& b) const override;
  442. virtual const Map<Key, T>& GetMap() const = 0;
  443. virtual Map<Key, T>* MutableMap() = 0;
  444. protected:
  445. typename Map<Key, T>::const_iterator& InternalGetIterator(
  446. const MapIterator* map_iter) const;
  447. private:
  448. void InitializeIterator(MapIterator* map_iter) const override;
  449. void DeleteIterator(MapIterator* map_iter) const override;
  450. void CopyIterator(MapIterator* this_iteratorm,
  451. const MapIterator& that_iterator) const override;
  452. void IncreaseIterator(MapIterator* map_iter) const override;
  453. virtual void SetMapIteratorValue(MapIterator* map_iter) const = 0;
  454. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeDefinedMapFieldBase);
  455. };
  456. // This class provides access to map field using generated api. It is used for
  457. // internal generated message implementation only. Users should never use this
  458. // directly.
  459. template <typename Derived, typename Key, typename T,
  460. WireFormatLite::FieldType kKeyFieldType,
  461. WireFormatLite::FieldType kValueFieldType>
  462. class MapField : public TypeDefinedMapFieldBase<Key, T> {
  463. // Provide utilities to parse/serialize key/value. Provide utilities to
  464. // manipulate internal stored type.
  465. typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
  466. typedef MapTypeHandler<kValueFieldType, T> ValueTypeHandler;
  467. // Define message type for internal repeated field.
  468. typedef Derived EntryType;
  469. // Define abbreviation for parent MapFieldLite
  470. typedef MapFieldLite<Derived, Key, T, kKeyFieldType, kValueFieldType>
  471. MapFieldLiteType;
  472. // Enum needs to be handled differently from other types because it has
  473. // different exposed type in Map's api and repeated field's api. For
  474. // details see the comment in the implementation of
  475. // SyncMapWithRepeatedFieldNoLock.
  476. static constexpr bool kIsValueEnum = ValueTypeHandler::kIsEnum;
  477. typedef typename MapIf<kIsValueEnum, T, const T&>::type CastValueType;
  478. public:
  479. typedef typename Derived::SuperType EntryTypeTrait;
  480. typedef Map<Key, T> MapType;
  481. MapField() {}
  482. // This constructor is for constant initialized global instances.
  483. // It uses a linker initialized mutex, so it is not compatible with regular
  484. // runtime instances.
  485. explicit constexpr MapField(ConstantInitialized tag)
  486. : TypeDefinedMapFieldBase<Key, T>(tag), impl_() {}
  487. explicit MapField(Arena* arena)
  488. : TypeDefinedMapFieldBase<Key, T>(arena), impl_(arena) {}
  489. // Implement MapFieldBase
  490. bool ContainsMapKey(const MapKey& map_key) const override;
  491. bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) override;
  492. bool LookupMapValue(const MapKey& map_key,
  493. MapValueConstRef* val) const override;
  494. bool LookupMapValue(const MapKey&, MapValueRef*) const = delete;
  495. bool DeleteMapValue(const MapKey& map_key) override;
  496. const Map<Key, T>& GetMap() const override {
  497. MapFieldBase::SyncMapWithRepeatedField();
  498. return impl_.GetMap();
  499. }
  500. Map<Key, T>* MutableMap() override {
  501. MapFieldBase::SyncMapWithRepeatedField();
  502. Map<Key, T>* result = impl_.MutableMap();
  503. MapFieldBase::SetMapDirty();
  504. return result;
  505. }
  506. int size() const override;
  507. void Clear() override;
  508. void MergeFrom(const MapFieldBase& other) override;
  509. void Swap(MapFieldBase* other) override;
  510. void UnsafeShallowSwap(MapFieldBase* other) override;
  511. void InternalSwap(MapField* other);
  512. // Used in the implementation of parsing. Caller should take the ownership iff
  513. // arena_ is nullptr.
  514. EntryType* NewEntry() const { return impl_.NewEntry(); }
  515. // Used in the implementation of serializing enum value type. Caller should
  516. // take the ownership iff arena_ is nullptr.
  517. EntryType* NewEnumEntryWrapper(const Key& key, const T t) const {
  518. return impl_.NewEnumEntryWrapper(key, t);
  519. }
  520. // Used in the implementation of serializing other value types. Caller should
  521. // take the ownership iff arena_ is nullptr.
  522. EntryType* NewEntryWrapper(const Key& key, const T& t) const {
  523. return impl_.NewEntryWrapper(key, t);
  524. }
  525. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  526. return impl_._InternalParse(ptr, ctx);
  527. }
  528. template <typename UnknownType>
  529. const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
  530. bool (*is_valid)(int), uint32_t field_num,
  531. InternalMetadata* metadata) {
  532. return impl_.template ParseWithEnumValidation<UnknownType>(
  533. ptr, ctx, is_valid, field_num, metadata);
  534. }
  535. private:
  536. MapFieldLiteType impl_;
  537. typedef void InternalArenaConstructable_;
  538. typedef void DestructorSkippable_;
  539. // Implements MapFieldBase
  540. void SyncRepeatedFieldWithMapNoLock() const override;
  541. void SyncMapWithRepeatedFieldNoLock() const override;
  542. size_t SpaceUsedExcludingSelfNoLock() const override;
  543. void SetMapIteratorValue(MapIterator* map_iter) const override;
  544. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  545. friend class MapFieldStateTest; // For testing, it needs raw access to impl_
  546. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapField);
  547. };
  548. template <typename Derived, typename Key, typename T,
  549. WireFormatLite::FieldType key_wire_type,
  550. WireFormatLite::FieldType value_wire_type>
  551. bool AllAreInitialized(
  552. const MapField<Derived, Key, T, key_wire_type, value_wire_type>& field) {
  553. const auto& t = field.GetMap();
  554. for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
  555. ++it) {
  556. if (!it->second.IsInitialized()) return false;
  557. }
  558. return true;
  559. }
  560. template <typename T, typename Key, typename Value,
  561. WireFormatLite::FieldType kKeyFieldType,
  562. WireFormatLite::FieldType kValueFieldType>
  563. struct MapEntryToMapField<
  564. MapEntry<T, Key, Value, kKeyFieldType, kValueFieldType>> {
  565. typedef MapField<T, Key, Value, kKeyFieldType, kValueFieldType> MapFieldType;
  566. };
  567. class PROTOBUF_EXPORT DynamicMapField
  568. : public TypeDefinedMapFieldBase<MapKey, MapValueRef> {
  569. public:
  570. explicit DynamicMapField(const Message* default_entry);
  571. DynamicMapField(const Message* default_entry, Arena* arena);
  572. ~DynamicMapField() override;
  573. // Implement MapFieldBase
  574. bool ContainsMapKey(const MapKey& map_key) const override;
  575. bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val) override;
  576. bool LookupMapValue(const MapKey& map_key,
  577. MapValueConstRef* val) const override;
  578. bool LookupMapValue(const MapKey&, MapValueRef*) const = delete;
  579. bool DeleteMapValue(const MapKey& map_key) override;
  580. void MergeFrom(const MapFieldBase& other) override;
  581. void Swap(MapFieldBase* other) override;
  582. void UnsafeShallowSwap(MapFieldBase* other) override { Swap(other); }
  583. const Map<MapKey, MapValueRef>& GetMap() const override;
  584. Map<MapKey, MapValueRef>* MutableMap() override;
  585. int size() const override;
  586. void Clear() override;
  587. private:
  588. Map<MapKey, MapValueRef> map_;
  589. const Message* default_entry_;
  590. void AllocateMapValue(MapValueRef* map_val);
  591. // Implements MapFieldBase
  592. void SyncRepeatedFieldWithMapNoLock() const override;
  593. void SyncMapWithRepeatedFieldNoLock() const override;
  594. size_t SpaceUsedExcludingSelfNoLock() const override;
  595. void SetMapIteratorValue(MapIterator* map_iter) const override;
  596. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMapField);
  597. };
  598. } // namespace internal
  599. // MapValueConstRef points to a map value. Users can NOT modify
  600. // the map value.
  601. class PROTOBUF_EXPORT MapValueConstRef {
  602. public:
  603. MapValueConstRef() : data_(nullptr), type_() {}
  604. int64_t GetInt64Value() const {
  605. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64,
  606. "MapValueConstRef::GetInt64Value");
  607. return *reinterpret_cast<int64_t*>(data_);
  608. }
  609. uint64_t GetUInt64Value() const {
  610. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64,
  611. "MapValueConstRef::GetUInt64Value");
  612. return *reinterpret_cast<uint64_t*>(data_);
  613. }
  614. int32_t GetInt32Value() const {
  615. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32,
  616. "MapValueConstRef::GetInt32Value");
  617. return *reinterpret_cast<int32_t*>(data_);
  618. }
  619. uint32_t GetUInt32Value() const {
  620. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32,
  621. "MapValueConstRef::GetUInt32Value");
  622. return *reinterpret_cast<uint32_t*>(data_);
  623. }
  624. bool GetBoolValue() const {
  625. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueConstRef::GetBoolValue");
  626. return *reinterpret_cast<bool*>(data_);
  627. }
  628. int GetEnumValue() const {
  629. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueConstRef::GetEnumValue");
  630. return *reinterpret_cast<int*>(data_);
  631. }
  632. const std::string& GetStringValue() const {
  633. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
  634. "MapValueConstRef::GetStringValue");
  635. return *reinterpret_cast<std::string*>(data_);
  636. }
  637. float GetFloatValue() const {
  638. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT,
  639. "MapValueConstRef::GetFloatValue");
  640. return *reinterpret_cast<float*>(data_);
  641. }
  642. double GetDoubleValue() const {
  643. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE,
  644. "MapValueConstRef::GetDoubleValue");
  645. return *reinterpret_cast<double*>(data_);
  646. }
  647. const Message& GetMessageValue() const {
  648. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  649. "MapValueConstRef::GetMessageValue");
  650. return *reinterpret_cast<Message*>(data_);
  651. }
  652. protected:
  653. // data_ point to a map value. MapValueConstRef does not
  654. // own this value.
  655. void* data_;
  656. // type_ is 0 or a valid FieldDescriptor::CppType.
  657. // Use "CppType()" to indicate zero.
  658. FieldDescriptor::CppType type_;
  659. FieldDescriptor::CppType type() const {
  660. if (type_ == FieldDescriptor::CppType() || data_ == nullptr) {
  661. GOOGLE_LOG(FATAL)
  662. << "Protocol Buffer map usage error:\n"
  663. << "MapValueConstRef::type MapValueConstRef is not initialized.";
  664. }
  665. return type_;
  666. }
  667. private:
  668. template <typename Derived, typename K, typename V,
  669. internal::WireFormatLite::FieldType key_wire_type,
  670. internal::WireFormatLite::FieldType value_wire_type>
  671. friend class internal::MapField;
  672. template <typename K, typename V>
  673. friend class internal::TypeDefinedMapFieldBase;
  674. friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
  675. friend class Reflection;
  676. friend class internal::DynamicMapField;
  677. void SetType(FieldDescriptor::CppType type) { type_ = type; }
  678. void SetValue(const void* val) { data_ = const_cast<void*>(val); }
  679. void CopyFrom(const MapValueConstRef& other) {
  680. type_ = other.type_;
  681. data_ = other.data_;
  682. }
  683. };
  684. // MapValueRef points to a map value. Users are able to modify
  685. // the map value.
  686. class PROTOBUF_EXPORT MapValueRef final : public MapValueConstRef {
  687. public:
  688. MapValueRef() {}
  689. void SetInt64Value(int64_t value) {
  690. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, "MapValueRef::SetInt64Value");
  691. *reinterpret_cast<int64_t*>(data_) = value;
  692. }
  693. void SetUInt64Value(uint64_t value) {
  694. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, "MapValueRef::SetUInt64Value");
  695. *reinterpret_cast<uint64_t*>(data_) = value;
  696. }
  697. void SetInt32Value(int32_t value) {
  698. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, "MapValueRef::SetInt32Value");
  699. *reinterpret_cast<int32_t*>(data_) = value;
  700. }
  701. void SetUInt32Value(uint32_t value) {
  702. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, "MapValueRef::SetUInt32Value");
  703. *reinterpret_cast<uint32_t*>(data_) = value;
  704. }
  705. void SetBoolValue(bool value) {
  706. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapValueRef::SetBoolValue");
  707. *reinterpret_cast<bool*>(data_) = value;
  708. }
  709. // TODO(jieluo) - Checks that enum is member.
  710. void SetEnumValue(int value) {
  711. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue");
  712. *reinterpret_cast<int*>(data_) = value;
  713. }
  714. void SetStringValue(const std::string& value) {
  715. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue");
  716. *reinterpret_cast<std::string*>(data_) = value;
  717. }
  718. void SetFloatValue(float value) {
  719. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue");
  720. *reinterpret_cast<float*>(data_) = value;
  721. }
  722. void SetDoubleValue(double value) {
  723. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, "MapValueRef::SetDoubleValue");
  724. *reinterpret_cast<double*>(data_) = value;
  725. }
  726. Message* MutableMessageValue() {
  727. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  728. "MapValueRef::MutableMessageValue");
  729. return reinterpret_cast<Message*>(data_);
  730. }
  731. private:
  732. friend class internal::DynamicMapField;
  733. // Only used in DynamicMapField
  734. void DeleteData() {
  735. switch (type_) {
  736. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  737. case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  738. delete reinterpret_cast<TYPE*>(data_); \
  739. break; \
  740. }
  741. HANDLE_TYPE(INT32, int32_t);
  742. HANDLE_TYPE(INT64, int64_t);
  743. HANDLE_TYPE(UINT32, uint32_t);
  744. HANDLE_TYPE(UINT64, uint64_t);
  745. HANDLE_TYPE(DOUBLE, double);
  746. HANDLE_TYPE(FLOAT, float);
  747. HANDLE_TYPE(BOOL, bool);
  748. HANDLE_TYPE(STRING, std::string);
  749. HANDLE_TYPE(ENUM, int32_t);
  750. HANDLE_TYPE(MESSAGE, Message);
  751. #undef HANDLE_TYPE
  752. }
  753. }
  754. };
  755. #undef TYPE_CHECK
  756. class PROTOBUF_EXPORT MapIterator {
  757. public:
  758. MapIterator(Message* message, const FieldDescriptor* field) {
  759. const Reflection* reflection = message->GetReflection();
  760. map_ = reflection->MutableMapData(message, field);
  761. key_.SetType(field->message_type()->FindFieldByName("key")->cpp_type());
  762. value_.SetType(field->message_type()->FindFieldByName("value")->cpp_type());
  763. map_->InitializeIterator(this);
  764. }
  765. MapIterator(const MapIterator& other) {
  766. map_ = other.map_;
  767. map_->InitializeIterator(this);
  768. map_->CopyIterator(this, other);
  769. }
  770. ~MapIterator() { map_->DeleteIterator(this); }
  771. MapIterator& operator=(const MapIterator& other) {
  772. map_ = other.map_;
  773. map_->CopyIterator(this, other);
  774. return *this;
  775. }
  776. friend bool operator==(const MapIterator& a, const MapIterator& b) {
  777. return a.map_->EqualIterator(a, b);
  778. }
  779. friend bool operator!=(const MapIterator& a, const MapIterator& b) {
  780. return !a.map_->EqualIterator(a, b);
  781. }
  782. MapIterator& operator++() {
  783. map_->IncreaseIterator(this);
  784. return *this;
  785. }
  786. MapIterator operator++(int) {
  787. // iter_ is copied from Map<...>::iterator, no need to
  788. // copy from its self again. Use the same implementation
  789. // with operator++()
  790. map_->IncreaseIterator(this);
  791. return *this;
  792. }
  793. const MapKey& GetKey() { return key_; }
  794. const MapValueRef& GetValueRef() { return value_; }
  795. MapValueRef* MutableValueRef() {
  796. map_->SetMapDirty();
  797. return &value_;
  798. }
  799. private:
  800. template <typename Key, typename T>
  801. friend class internal::TypeDefinedMapFieldBase;
  802. friend class internal::DynamicMapField;
  803. template <typename Derived, typename Key, typename T,
  804. internal::WireFormatLite::FieldType kKeyFieldType,
  805. internal::WireFormatLite::FieldType kValueFieldType>
  806. friend class internal::MapField;
  807. // reinterpret_cast from heap-allocated Map<...>::iterator*. MapIterator owns
  808. // the iterator. It is allocated by MapField<...>::InitializeIterator() called
  809. // in constructor and deleted by MapField<...>::DeleteIterator() called in
  810. // destructor.
  811. void* iter_;
  812. // Point to a MapField to call helper methods implemented in MapField.
  813. // MapIterator does not own this object.
  814. internal::MapFieldBase* map_;
  815. MapKey key_;
  816. MapValueRef value_;
  817. };
  818. } // namespace protobuf
  819. } // namespace google
  820. #include <google/protobuf/port_undef.inc>
  821. #endif // GOOGLE_PROTOBUF_MAP_FIELD_H__