map_field_inl.h 14 KB

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