map_field.h 29 KB

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