诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

819 lines
31 KiB

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