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.h 46 kB

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