map_field_inl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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>
  152. int MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::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>
  159. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::Clear() {
  160. if (this->MapFieldBase::repeated_field_ != nullptr) {
  161. RepeatedPtrField<EntryType>* repeated_field =
  162. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  163. this->MapFieldBase::repeated_field_);
  164. repeated_field->Clear();
  165. }
  166. impl_.MutableMap()->clear();
  167. // Data in map and repeated field are both empty, but we can't set status
  168. // CLEAN. Because clear is a generated API, we cannot invalidate previous
  169. // reference to map.
  170. MapFieldBase::SetMapDirty();
  171. }
  172. template <typename Derived, typename Key, typename T,
  173. WireFormatLite::FieldType kKeyFieldType,
  174. WireFormatLite::FieldType kValueFieldType>
  175. void MapField<Derived, Key, T, kKeyFieldType,
  176. kValueFieldType>::SetMapIteratorValue(MapIterator* map_iter)
  177. const {
  178. const Map<Key, T>& map = impl_.GetMap();
  179. typename Map<Key, T>::const_iterator iter =
  180. TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(map_iter);
  181. if (iter == map.end()) return;
  182. SetMapKey(&map_iter->key_, iter->first);
  183. map_iter->value_.SetValue(&iter->second);
  184. }
  185. template <typename Derived, typename Key, typename T,
  186. WireFormatLite::FieldType kKeyFieldType,
  187. WireFormatLite::FieldType kValueFieldType>
  188. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::ContainsMapKey(
  189. const MapKey& map_key) const {
  190. const Map<Key, T>& map = impl_.GetMap();
  191. const Key& key = UnwrapMapKey<Key>(map_key);
  192. typename Map<Key, T>::const_iterator iter = map.find(key);
  193. return iter != map.end();
  194. }
  195. template <typename Derived, typename Key, typename T,
  196. WireFormatLite::FieldType kKeyFieldType,
  197. WireFormatLite::FieldType kValueFieldType>
  198. bool MapField<Derived, Key, T, kKeyFieldType,
  199. kValueFieldType>::InsertOrLookupMapValue(const MapKey& map_key,
  200. MapValueRef* val) {
  201. // Always use mutable map because users may change the map value by
  202. // MapValueRef.
  203. Map<Key, T>* map = MutableMap();
  204. const Key& key = UnwrapMapKey<Key>(map_key);
  205. typename Map<Key, T>::iterator iter = map->find(key);
  206. if (map->end() == iter) {
  207. val->SetValue(&((*map)[key]));
  208. return true;
  209. }
  210. // Key is already in the map. Make sure (*map)[key] is not called.
  211. // [] may reorder the map and iterators.
  212. val->SetValue(&(iter->second));
  213. return false;
  214. }
  215. template <typename Derived, typename Key, typename T,
  216. WireFormatLite::FieldType kKeyFieldType,
  217. WireFormatLite::FieldType kValueFieldType>
  218. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::LookupMapValue(
  219. const MapKey& map_key, MapValueConstRef* val) const {
  220. const Map<Key, T>& map = GetMap();
  221. const Key& key = UnwrapMapKey<Key>(map_key);
  222. typename Map<Key, T>::const_iterator iter = map.find(key);
  223. if (map.end() == iter) {
  224. return false;
  225. }
  226. // Key is already in the map. Make sure (*map)[key] is not called.
  227. // [] may reorder the map and iterators.
  228. val->SetValue(&(iter->second));
  229. return true;
  230. }
  231. template <typename Derived, typename Key, typename T,
  232. WireFormatLite::FieldType kKeyFieldType,
  233. WireFormatLite::FieldType kValueFieldType>
  234. bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::DeleteMapValue(
  235. const MapKey& map_key) {
  236. const Key& key = UnwrapMapKey<Key>(map_key);
  237. return MutableMap()->erase(key);
  238. }
  239. template <typename Derived, typename Key, typename T,
  240. WireFormatLite::FieldType kKeyFieldType,
  241. WireFormatLite::FieldType kValueFieldType>
  242. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::MergeFrom(
  243. const MapFieldBase& other) {
  244. MapFieldBase::SyncMapWithRepeatedField();
  245. const MapField& other_field = static_cast<const MapField&>(other);
  246. other_field.SyncMapWithRepeatedField();
  247. impl_.MergeFrom(other_field.impl_);
  248. MapFieldBase::SetMapDirty();
  249. }
  250. template <typename Derived, typename Key, typename T,
  251. WireFormatLite::FieldType kKeyFieldType,
  252. WireFormatLite::FieldType kValueFieldType>
  253. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::Swap(
  254. MapFieldBase* other) {
  255. MapFieldBase::Swap(other);
  256. MapField* other_field = down_cast<MapField*>(other);
  257. impl_.Swap(&other_field->impl_);
  258. }
  259. template <typename Derived, typename Key, typename T,
  260. WireFormatLite::FieldType kKeyFieldType,
  261. WireFormatLite::FieldType kValueFieldType>
  262. void MapField<Derived, Key, T, kKeyFieldType,
  263. kValueFieldType>::UnsafeShallowSwap(MapFieldBase* other) {
  264. InternalSwap(down_cast<MapField*>(other));
  265. }
  266. template <typename Derived, typename Key, typename T,
  267. WireFormatLite::FieldType kKeyFieldType,
  268. WireFormatLite::FieldType kValueFieldType>
  269. void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::InternalSwap(
  270. MapField* other) {
  271. MapFieldBase::InternalSwap(other);
  272. impl_.InternalSwap(&other->impl_);
  273. }
  274. template <typename Derived, typename Key, typename T,
  275. WireFormatLite::FieldType kKeyFieldType,
  276. WireFormatLite::FieldType kValueFieldType>
  277. void MapField<Derived, Key, T, kKeyFieldType,
  278. kValueFieldType>::SyncRepeatedFieldWithMapNoLock() const {
  279. if (this->MapFieldBase::repeated_field_ == NULL) {
  280. this->MapFieldBase::repeated_field_ =
  281. Arena::CreateMessage<RepeatedPtrField<Message> >(
  282. this->MapFieldBase::arena_);
  283. }
  284. const Map<Key, T>& map = impl_.GetMap();
  285. RepeatedPtrField<EntryType>* repeated_field =
  286. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  287. this->MapFieldBase::repeated_field_);
  288. repeated_field->Clear();
  289. // The only way we can get at this point is through reflection and the
  290. // only way we can get the reflection object is by having called GetReflection
  291. // on the encompassing field. So that type must have existed and hence we
  292. // know that this MapEntry default_type has also already been constructed.
  293. // So it's safe to just call internal_default_instance().
  294. const Message* default_entry = Derived::internal_default_instance();
  295. for (typename Map<Key, T>::const_iterator it = map.begin(); it != map.end();
  296. ++it) {
  297. EntryType* new_entry =
  298. down_cast<EntryType*>(default_entry->New(this->MapFieldBase::arena_));
  299. repeated_field->AddAllocated(new_entry);
  300. (*new_entry->mutable_key()) = it->first;
  301. (*new_entry->mutable_value()) = it->second;
  302. }
  303. }
  304. template <typename Derived, typename Key, typename T,
  305. WireFormatLite::FieldType kKeyFieldType,
  306. WireFormatLite::FieldType kValueFieldType>
  307. void MapField<Derived, Key, T, kKeyFieldType,
  308. kValueFieldType>::SyncMapWithRepeatedFieldNoLock() const {
  309. Map<Key, T>* map = const_cast<MapField*>(this)->impl_.MutableMap();
  310. RepeatedPtrField<EntryType>* repeated_field =
  311. reinterpret_cast<RepeatedPtrField<EntryType>*>(
  312. this->MapFieldBase::repeated_field_);
  313. GOOGLE_CHECK(this->MapFieldBase::repeated_field_ != NULL);
  314. map->clear();
  315. for (typename RepeatedPtrField<EntryType>::iterator it =
  316. repeated_field->begin();
  317. it != repeated_field->end(); ++it) {
  318. // Cast is needed because Map's api and internal storage is different when
  319. // value is enum. For enum, we cannot cast an int to enum. Thus, we have to
  320. // copy value. For other types, they have same exposed api type and internal
  321. // stored type. We should not introduce value copy for them. We achieve this
  322. // by casting to value for enum while casting to reference for other types.
  323. (*map)[it->key()] = static_cast<CastValueType>(it->value());
  324. }
  325. }
  326. template <typename Derived, typename Key, typename T,
  327. WireFormatLite::FieldType kKeyFieldType,
  328. WireFormatLite::FieldType kValueFieldType>
  329. size_t MapField<Derived, Key, T, kKeyFieldType,
  330. kValueFieldType>::SpaceUsedExcludingSelfNoLock() const {
  331. size_t size = 0;
  332. if (this->MapFieldBase::repeated_field_ != NULL) {
  333. size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelfLong();
  334. }
  335. size += impl_.GetMap().SpaceUsedExcludingSelfLong();
  336. return size;
  337. }
  338. } // namespace internal
  339. } // namespace protobuf
  340. } // namespace google
  341. #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__