You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

map_field_inl.h 18 kB

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