诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

358 行
14 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_INL_H__
  31. #define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
  32. #include <memory>
  33. #include <google/protobuf/stubs/casts.h>
  34. #include <google/protobuf/map.h>
  35. #include <google/protobuf/map_field.h>
  36. #include <google/protobuf/map_type_handler.h>
  37. #ifdef SWIG
  38. #error "You cannot SWIG proto headers"
  39. #endif
  40. namespace google {
  41. namespace protobuf {
  42. namespace internal {
  43. // UnwrapMapKey template
  44. template <typename T>
  45. T UnwrapMapKey(const MapKey& map_key);
  46. template <>
  47. inline int32 UnwrapMapKey<int32>(const MapKey& map_key) {
  48. return map_key.GetInt32Value();
  49. }
  50. template <>
  51. inline uint32 UnwrapMapKey<uint32>(const MapKey& map_key) {
  52. return map_key.GetUInt32Value();
  53. }
  54. template <>
  55. inline int64 UnwrapMapKey<int64>(const MapKey& map_key) {
  56. return map_key.GetInt64Value();
  57. }
  58. template <>
  59. inline uint64 UnwrapMapKey<uint64>(const MapKey& map_key) {
  60. return map_key.GetUInt64Value();
  61. }
  62. template <>
  63. inline bool UnwrapMapKey<bool>(const MapKey& map_key) {
  64. return map_key.GetBoolValue();
  65. }
  66. template <>
  67. inline std::string UnwrapMapKey<std::string>(const MapKey& map_key) {
  68. return map_key.GetStringValue();
  69. }
  70. // SetMapKey template
  71. template <typename T>
  72. inline void SetMapKey(MapKey* map_key, const T& value);
  73. template <>
  74. inline void SetMapKey<int32>(MapKey* map_key, const int32& value) {
  75. map_key->SetInt32Value(value);
  76. }
  77. template <>
  78. inline void SetMapKey<uint32>(MapKey* map_key, const uint32& value) {
  79. map_key->SetUInt32Value(value);
  80. }
  81. template <>
  82. inline void SetMapKey<int64>(MapKey* map_key, const int64& value) {
  83. map_key->SetInt64Value(value);
  84. }
  85. template <>
  86. inline void SetMapKey<uint64>(MapKey* map_key, const uint64& value) {
  87. map_key->SetUInt64Value(value);
  88. }
  89. template <>
  90. inline void SetMapKey<bool>(MapKey* map_key, const bool& value) {
  91. map_key->SetBoolValue(value);
  92. }
  93. template <>
  94. inline void SetMapKey<std::string>(MapKey* map_key, const std::string& value) {
  95. map_key->SetStringValue(value);
  96. }
  97. // ------------------------TypeDefinedMapFieldBase---------------
  98. template <typename Key, typename T>
  99. typename Map<Key, T>::const_iterator&
  100. TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(
  101. const MapIterator* map_iter) const {
  102. return *reinterpret_cast<typename Map<Key, T>::const_iterator*>(
  103. map_iter->iter_);
  104. }
  105. template <typename Key, typename T>
  106. void TypeDefinedMapFieldBase<Key, T>::MapBegin(MapIterator* map_iter) const {
  107. InternalGetIterator(map_iter) = GetMap().begin();
  108. SetMapIteratorValue(map_iter);
  109. }
  110. template <typename Key, typename T>
  111. void TypeDefinedMapFieldBase<Key, T>::MapEnd(MapIterator* map_iter) const {
  112. InternalGetIterator(map_iter) = GetMap().end();
  113. }
  114. template <typename Key, typename T>
  115. bool TypeDefinedMapFieldBase<Key, T>::EqualIterator(
  116. const MapIterator& a, const MapIterator& b) const {
  117. return InternalGetIterator(&a) == InternalGetIterator(&b);
  118. }
  119. template <typename Key, typename T>
  120. void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator(
  121. MapIterator* map_iter) const {
  122. ++InternalGetIterator(map_iter);
  123. SetMapIteratorValue(map_iter);
  124. }
  125. template <typename Key, typename T>
  126. void TypeDefinedMapFieldBase<Key, T>::InitializeIterator(
  127. MapIterator* map_iter) const {
  128. map_iter->iter_ = new typename Map<Key, T>::const_iterator;
  129. GOOGLE_CHECK(map_iter->iter_ != NULL);
  130. }
  131. template <typename Key, typename T>
  132. void TypeDefinedMapFieldBase<Key, T>::DeleteIterator(
  133. MapIterator* map_iter) const {
  134. delete reinterpret_cast<typename Map<Key, T>::const_iterator*>(
  135. map_iter->iter_);
  136. }
  137. template <typename Key, typename T>
  138. void TypeDefinedMapFieldBase<Key, T>::CopyIterator(
  139. MapIterator* this_iter, const MapIterator& that_iter) const {
  140. InternalGetIterator(this_iter) = InternalGetIterator(&that_iter);
  141. this_iter->key_.SetType(that_iter.key_.type());
  142. // MapValueRef::type() fails when containing data is null. However, if
  143. // this_iter points to MapEnd, data can be null.
  144. this_iter->value_.SetType(
  145. static_cast<FieldDescriptor::CppType>(that_iter.value_.type_));
  146. SetMapIteratorValue(this_iter);
  147. }
  148. // ----------------------------------------------------------------------
  149. template <typename Derived, typename Key, typename T,
  150. WireFormatLite::FieldType kKeyFieldType,
  151. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  152. int MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  153. default_enum_value>::size() const {
  154. MapFieldBase::SyncMapWithRepeatedField();
  155. return static_cast<int>(impl_.GetMap().size());
  156. }
  157. template <typename Derived, typename Key, typename T,
  158. WireFormatLite::FieldType kKeyFieldType,
  159. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  160. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  161. default_enum_value>::Clear() {
  162. if (this->MapFieldBase::repeated_field_ != nullptr) {
  163. RepeatedPtrField<EntryType>* repeated_field =
  164. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  165. this->MapFieldBase::repeated_field_);
  166. repeated_field->Clear();
  167. }
  168. impl_.MutableMap()->clear();
  169. // Data in map and repeated field are both empty, but we can't set status
  170. // CLEAN. Because clear is a generated API, we cannot invalidate previous
  171. // reference to map.
  172. MapFieldBase::SetMapDirty();
  173. }
  174. template <typename Derived, typename Key, typename T,
  175. WireFormatLite::FieldType kKeyFieldType,
  176. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  177. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  178. default_enum_value>::SetMapIteratorValue(MapIterator* map_iter)
  179. const {
  180. const Map<Key, T>& map = impl_.GetMap();
  181. typename Map<Key, T>::const_iterator iter =
  182. TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(map_iter);
  183. if (iter == map.end()) return;
  184. SetMapKey(&map_iter->key_, iter->first);
  185. map_iter->value_.SetValue(&iter->second);
  186. }
  187. template <typename Derived, typename Key, typename T,
  188. WireFormatLite::FieldType kKeyFieldType,
  189. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  190. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  191. default_enum_value>::ContainsMapKey(const MapKey& map_key) const {
  192. const Map<Key, T>& map = impl_.GetMap();
  193. const Key& key = UnwrapMapKey<Key>(map_key);
  194. typename Map<Key, T>::const_iterator iter = map.find(key);
  195. return iter != map.end();
  196. }
  197. template <typename Derived, typename Key, typename T,
  198. WireFormatLite::FieldType kKeyFieldType,
  199. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  200. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  201. default_enum_value>::InsertOrLookupMapValue(const MapKey& map_key,
  202. MapValueRef* val) {
  203. // Always use mutable map because users may change the map value by
  204. // MapValueRef.
  205. Map<Key, T>* map = MutableMap();
  206. const Key& key = UnwrapMapKey<Key>(map_key);
  207. typename Map<Key, T>::iterator iter = map->find(key);
  208. if (map->end() == iter) {
  209. val->SetValue(&((*map)[key]));
  210. return true;
  211. }
  212. // Key is already in the map. Make sure (*map)[key] is not called.
  213. // [] may reorder the map and iterators.
  214. val->SetValue(&(iter->second));
  215. return false;
  216. }
  217. template <typename Derived, typename Key, typename T,
  218. WireFormatLite::FieldType kKeyFieldType,
  219. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  220. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  221. default_enum_value>::DeleteMapValue(const MapKey& map_key) {
  222. const Key& key = UnwrapMapKey<Key>(map_key);
  223. return MutableMap()->erase(key);
  224. }
  225. template <typename Derived, typename Key, typename T,
  226. WireFormatLite::FieldType kKeyFieldType,
  227. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  228. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  229. default_enum_value>::MergeFrom(const MapFieldBase& other) {
  230. MapFieldBase::SyncMapWithRepeatedField();
  231. const MapField& other_field = static_cast<const MapField&>(other);
  232. other_field.SyncMapWithRepeatedField();
  233. impl_.MergeFrom(other_field.impl_);
  234. MapFieldBase::SetMapDirty();
  235. }
  236. template <typename Derived, typename Key, typename T,
  237. WireFormatLite::FieldType kKeyFieldType,
  238. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  239. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  240. default_enum_value>::Swap(MapFieldBase* other) {
  241. MapField* other_field = down_cast<MapField*>(other);
  242. std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_);
  243. impl_.Swap(&other_field->impl_);
  244. // a relaxed swap of the atomic
  245. auto other_state = other_field->state_.load(std::memory_order_relaxed);
  246. auto this_state = this->MapFieldBase::state_.load(std::memory_order_relaxed);
  247. other_field->state_.store(this_state, std::memory_order_relaxed);
  248. this->MapFieldBase::state_.store(other_state, std::memory_order_relaxed);
  249. }
  250. template <typename Derived, typename Key, typename T,
  251. WireFormatLite::FieldType kKeyFieldType,
  252. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  253. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  254. default_enum_value>::SyncRepeatedFieldWithMapNoLock() const {
  255. if (this->MapFieldBase::repeated_field_ == NULL) {
  256. if (this->MapFieldBase::arena_ == NULL) {
  257. this->MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
  258. } else {
  259. this->MapFieldBase::repeated_field_ =
  260. Arena::CreateMessage<RepeatedPtrField<Message> >(
  261. this->MapFieldBase::arena_);
  262. }
  263. }
  264. const Map<Key, T>& map = impl_.GetMap();
  265. RepeatedPtrField<EntryType>* repeated_field =
  266. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  267. this->MapFieldBase::repeated_field_);
  268. repeated_field->Clear();
  269. // The only way we can get at this point is through reflection and the
  270. // only way we can get the reflection object is by having called GetReflection
  271. // on the encompassing field. So that type must have existed and hence we
  272. // know that this MapEntry default_type has also already been constructed.
  273. // So it's safe to just call internal_default_instance().
  274. const Message* default_entry = Derived::internal_default_instance();
  275. for (typename Map<Key, T>::const_iterator it = map.begin(); it != map.end();
  276. ++it) {
  277. EntryType* new_entry =
  278. down_cast<EntryType*>(default_entry->New(this->MapFieldBase::arena_));
  279. repeated_field->AddAllocated(new_entry);
  280. (*new_entry->mutable_key()) = it->first;
  281. (*new_entry->mutable_value()) = it->second;
  282. }
  283. }
  284. template <typename Derived, typename Key, typename T,
  285. WireFormatLite::FieldType kKeyFieldType,
  286. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  287. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  288. default_enum_value>::SyncMapWithRepeatedFieldNoLock() const {
  289. Map<Key, T>* map = const_cast<MapField*>(this)->impl_.MutableMap();
  290. RepeatedPtrField<EntryType>* repeated_field =
  291. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  292. this->MapFieldBase::repeated_field_);
  293. GOOGLE_CHECK(this->MapFieldBase::repeated_field_ != NULL);
  294. map->clear();
  295. for (typename RepeatedPtrField<EntryType>::iterator it =
  296. repeated_field->begin();
  297. it != repeated_field->end(); ++it) {
  298. // Cast is needed because Map's api and internal storage is different when
  299. // value is enum. For enum, we cannot cast an int to enum. Thus, we have to
  300. // copy value. For other types, they have same exposed api type and internal
  301. // stored type. We should not introduce value copy for them. We achieve this
  302. // by casting to value for enum while casting to reference for other types.
  303. (*map)[it->key()] = static_cast<CastValueType>(it->value());
  304. }
  305. }
  306. template <typename Derived, typename Key, typename T,
  307. WireFormatLite::FieldType kKeyFieldType,
  308. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  309. size_t MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
  310. default_enum_value>::SpaceUsedExcludingSelfNoLock() const {
  311. size_t size = 0;
  312. if (this->MapFieldBase::repeated_field_ != NULL) {
  313. size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelfLong();
  314. }
  315. Map<Key, T>* map = const_cast<MapField*>(this)->impl_.MutableMap();
  316. size += sizeof(*map);
  317. for (typename Map<Key, T>::iterator it = map->begin(); it != map->end();
  318. ++it) {
  319. size += KeyTypeHandler::SpaceUsedInMapLong(it->first);
  320. size += ValueTypeHandler::SpaceUsedInMapLong(it->second);
  321. }
  322. return size;
  323. }
  324. } // namespace internal
  325. } // namespace protobuf
  326. } // namespace google
  327. #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__