map.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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_H__
  31. #define GOOGLE_PROTOBUF_MAP_H__
  32. #include <iterator>
  33. #include <google/protobuf/stubs/hash.h>
  34. #include <limits> // To support Visual Studio 2008
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/arena.h>
  37. #include <google/protobuf/generated_enum_util.h>
  38. #include <google/protobuf/map_type_handler.h>
  39. #include <google/protobuf/message.h>
  40. #include <google/protobuf/descriptor.h>
  41. namespace google {
  42. namespace protobuf {
  43. template <typename Key, typename T>
  44. class Map;
  45. template <typename Enum> struct is_proto_enum;
  46. class MapIterator;
  47. namespace internal {
  48. template <typename Key, typename T,
  49. WireFormatLite::FieldType key_wire_type,
  50. WireFormatLite::FieldType value_wire_type,
  51. int default_enum_value>
  52. class MapFieldLite;
  53. template <typename Key, typename T,
  54. WireFormatLite::FieldType key_wire_type,
  55. WireFormatLite::FieldType value_wire_type,
  56. int default_enum_value>
  57. class MapField;
  58. template <typename Key, typename T>
  59. class TypeDefinedMapFieldBase;
  60. class DynamicMapField;
  61. class GeneratedMessageReflection;
  62. } // namespace internal
  63. #define TYPE_CHECK(EXPECTEDTYPE, METHOD) \
  64. if (type() != EXPECTEDTYPE) { \
  65. GOOGLE_LOG(FATAL) \
  66. << "Protocol Buffer map usage error:\n" \
  67. << METHOD << " type does not match\n" \
  68. << " Expected : " \
  69. << FieldDescriptor::CppTypeName(EXPECTEDTYPE) << "\n" \
  70. << " Actual : " \
  71. << FieldDescriptor::CppTypeName(type()); \
  72. }
  73. // MapKey is an union type for representing any possible
  74. // map key.
  75. class LIBPROTOBUF_EXPORT MapKey {
  76. public:
  77. MapKey() : type_(0) {
  78. }
  79. MapKey(const MapKey& other) : type_(0) {
  80. CopyFrom(other);
  81. }
  82. ~MapKey() {
  83. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  84. delete val_.string_value_;
  85. }
  86. }
  87. FieldDescriptor::CppType type() const {
  88. if (type_ == 0) {
  89. GOOGLE_LOG(FATAL)
  90. << "Protocol Buffer map usage error:\n"
  91. << "MapKey::type MapKey is not initialized. "
  92. << "Call set methods to initialize MapKey.";
  93. }
  94. return (FieldDescriptor::CppType)type_;
  95. }
  96. void SetInt64Value(int64 value) {
  97. SetType(FieldDescriptor::CPPTYPE_INT64);
  98. val_.int64_value_ = value;
  99. }
  100. void SetUInt64Value(uint64 value) {
  101. SetType(FieldDescriptor::CPPTYPE_UINT64);
  102. val_.uint64_value_ = value;
  103. }
  104. void SetInt32Value(int32 value) {
  105. SetType(FieldDescriptor::CPPTYPE_INT32);
  106. val_.int32_value_ = value;
  107. }
  108. void SetUInt32Value(uint32 value) {
  109. SetType(FieldDescriptor::CPPTYPE_UINT32);
  110. val_.uint32_value_ = value;
  111. }
  112. void SetBoolValue(bool value) {
  113. SetType(FieldDescriptor::CPPTYPE_BOOL);
  114. val_.bool_value_ = value;
  115. }
  116. void SetStringValue(const string& val) {
  117. SetType(FieldDescriptor::CPPTYPE_STRING);
  118. *val_.string_value_ = val;
  119. }
  120. int64 GetInt64Value() const {
  121. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64,
  122. "MapKey::GetInt64Value");
  123. return val_.int64_value_;
  124. }
  125. uint64 GetUInt64Value() const {
  126. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64,
  127. "MapKey::GetUInt64Value");
  128. return val_.uint64_value_;
  129. }
  130. int32 GetInt32Value() const {
  131. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32,
  132. "MapKey::GetInt32Value");
  133. return val_.int32_value_;
  134. }
  135. uint32 GetUInt32Value() const {
  136. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32,
  137. "MapKey::GetUInt32Value");
  138. return val_.uint32_value_;
  139. }
  140. bool GetBoolValue() const {
  141. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL,
  142. "MapKey::GetBoolValue");
  143. return val_.bool_value_;
  144. }
  145. const string& GetStringValue() const {
  146. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
  147. "MapKey::GetStringValue");
  148. return *val_.string_value_;
  149. }
  150. bool operator==(const MapKey& other) const {
  151. if (type_ != other.type_) {
  152. return false;
  153. }
  154. switch (type()) {
  155. case FieldDescriptor::CPPTYPE_STRING:
  156. return *val_.string_value_ == *other.val_.string_value_;
  157. case FieldDescriptor::CPPTYPE_INT64:
  158. return val_.int64_value_ == other.val_.int64_value_;
  159. case FieldDescriptor::CPPTYPE_INT32:
  160. return val_.int32_value_ == other.val_.int32_value_;
  161. case FieldDescriptor::CPPTYPE_UINT64:
  162. return val_.uint64_value_ == other.val_.uint64_value_;
  163. case FieldDescriptor::CPPTYPE_UINT32:
  164. return val_.uint32_value_ == other.val_.uint32_value_;
  165. case FieldDescriptor::CPPTYPE_BOOL:
  166. return val_.bool_value_ == other.val_.bool_value_;
  167. case FieldDescriptor::CPPTYPE_DOUBLE:
  168. case FieldDescriptor::CPPTYPE_FLOAT:
  169. case FieldDescriptor::CPPTYPE_ENUM:
  170. case FieldDescriptor::CPPTYPE_MESSAGE:
  171. GOOGLE_LOG(FATAL) << "Can't get here.";
  172. return false;
  173. }
  174. GOOGLE_LOG(FATAL) << "Can't get here.";
  175. return false;
  176. }
  177. void CopyFrom(const MapKey& other) {
  178. SetType(other.type());
  179. switch (type_) {
  180. case FieldDescriptor::CPPTYPE_STRING:
  181. *val_.string_value_ = *other.val_.string_value_;
  182. break;
  183. case FieldDescriptor::CPPTYPE_INT64:
  184. val_.int64_value_ = other.val_.int64_value_;
  185. break;
  186. case FieldDescriptor::CPPTYPE_INT32:
  187. val_.int32_value_ = other.val_.int32_value_;
  188. break;
  189. case FieldDescriptor::CPPTYPE_UINT64:
  190. val_.uint64_value_ = other.val_.uint64_value_;
  191. break;
  192. case FieldDescriptor::CPPTYPE_UINT32:
  193. val_.uint32_value_ = other.val_.uint32_value_;
  194. break;
  195. case FieldDescriptor::CPPTYPE_BOOL:
  196. val_.bool_value_ = other.val_.bool_value_;
  197. break;
  198. case FieldDescriptor::CPPTYPE_DOUBLE:
  199. case FieldDescriptor::CPPTYPE_FLOAT:
  200. case FieldDescriptor::CPPTYPE_ENUM:
  201. case FieldDescriptor::CPPTYPE_MESSAGE:
  202. GOOGLE_LOG(FATAL) << "Can't get here.";
  203. break;
  204. }
  205. }
  206. private:
  207. template <typename K, typename V>
  208. friend class internal::TypeDefinedMapFieldBase;
  209. friend class MapIterator;
  210. friend class internal::DynamicMapField;
  211. union KeyValue {
  212. KeyValue() {}
  213. string* string_value_;
  214. int64 int64_value_;
  215. int32 int32_value_;
  216. uint64 uint64_value_;
  217. uint32 uint32_value_;
  218. bool bool_value_;
  219. } val_;
  220. void SetType(FieldDescriptor::CppType type) {
  221. if (type_ == type) return;
  222. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  223. delete val_.string_value_;
  224. }
  225. type_ = type;
  226. if (type_ == FieldDescriptor::CPPTYPE_STRING) {
  227. val_.string_value_ = new string;
  228. }
  229. }
  230. // type_ is 0 or a valid FieldDescriptor::CppType.
  231. int type_;
  232. };
  233. // MapValueRef points to a map value.
  234. class LIBPROTOBUF_EXPORT MapValueRef {
  235. public:
  236. MapValueRef() : data_(NULL), type_(0) {}
  237. void SetInt64Value(int64 value) {
  238. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64,
  239. "MapValueRef::SetInt64Value");
  240. *reinterpret_cast<int64*>(data_) = value;
  241. }
  242. void SetUInt64Value(uint64 value) {
  243. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64,
  244. "MapValueRef::SetUInt64Value");
  245. *reinterpret_cast<uint64*>(data_) = value;
  246. }
  247. void SetInt32Value(int32 value) {
  248. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32,
  249. "MapValueRef::SetInt32Value");
  250. *reinterpret_cast<int32*>(data_) = value;
  251. }
  252. void SetUInt32Value(uint32 value) {
  253. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32,
  254. "MapValueRef::SetUInt32Value");
  255. *reinterpret_cast<uint32*>(data_) = value;
  256. }
  257. void SetBoolValue(bool value) {
  258. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL,
  259. "MapValueRef::SetBoolValue");
  260. *reinterpret_cast<bool*>(data_) = value;
  261. }
  262. // TODO(jieluo) - Checks that enum is member.
  263. void SetEnumValue(int value) {
  264. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM,
  265. "MapValueRef::SetEnumValue");
  266. *reinterpret_cast<int*>(data_) = value;
  267. }
  268. void SetStringValue(const string& value) {
  269. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
  270. "MapValueRef::SetStringValue");
  271. *reinterpret_cast<string*>(data_) = value;
  272. }
  273. void SetFloatValue(float value) {
  274. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT,
  275. "MapValueRef::SetFloatValue");
  276. *reinterpret_cast<float*>(data_) = value;
  277. }
  278. void SetDoubleValue(double value) {
  279. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE,
  280. "MapValueRef::SetDoubleValue");
  281. *reinterpret_cast<double*>(data_) = value;
  282. }
  283. int64 GetInt64Value() const {
  284. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64,
  285. "MapValueRef::GetInt64Value");
  286. return *reinterpret_cast<int64*>(data_);
  287. }
  288. uint64 GetUInt64Value() const {
  289. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64,
  290. "MapValueRef::GetUInt64Value");
  291. return *reinterpret_cast<uint64*>(data_);
  292. }
  293. int32 GetInt32Value() const {
  294. TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32,
  295. "MapValueRef::GetInt32Value");
  296. return *reinterpret_cast<int32*>(data_);
  297. }
  298. uint32 GetUInt32Value() const {
  299. TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32,
  300. "MapValueRef::GetUInt32Value");
  301. return *reinterpret_cast<uint32*>(data_);
  302. }
  303. bool GetBoolValue() const {
  304. TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL,
  305. "MapValueRef::GetBoolValue");
  306. return *reinterpret_cast<bool*>(data_);
  307. }
  308. int GetEnumValue() const {
  309. TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM,
  310. "MapValueRef::GetEnumValue");
  311. return *reinterpret_cast<int*>(data_);
  312. }
  313. const string& GetStringValue() const {
  314. TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
  315. "MapValueRef::GetStringValue");
  316. return *reinterpret_cast<string*>(data_);
  317. }
  318. float GetFloatValue() const {
  319. TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT,
  320. "MapValueRef::GetFloatValue");
  321. return *reinterpret_cast<float*>(data_);
  322. }
  323. double GetDoubleValue() const {
  324. TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE,
  325. "MapValueRef::GetDoubleValue");
  326. return *reinterpret_cast<double*>(data_);
  327. }
  328. const Message& GetMessageValue() const {
  329. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  330. "MapValueRef::GetMessageValue");
  331. return *reinterpret_cast<Message*>(data_);
  332. }
  333. Message* MutableMessageValue() {
  334. TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE,
  335. "MapValueRef::MutableMessageValue");
  336. return reinterpret_cast<Message*>(data_);
  337. }
  338. private:
  339. template <typename K, typename V,
  340. internal::WireFormatLite::FieldType key_wire_type,
  341. internal::WireFormatLite::FieldType value_wire_type,
  342. int default_enum_value>
  343. friend class internal::MapField;
  344. template <typename K, typename V>
  345. friend class internal::TypeDefinedMapFieldBase;
  346. friend class MapIterator;
  347. friend class internal::GeneratedMessageReflection;
  348. friend class internal::DynamicMapField;
  349. void SetType(FieldDescriptor::CppType type) {
  350. type_ = type;
  351. }
  352. FieldDescriptor::CppType type() const {
  353. if (type_ == 0 || data_ == NULL) {
  354. GOOGLE_LOG(FATAL)
  355. << "Protocol Buffer map usage error:\n"
  356. << "MapValueRef::type MapValueRef is not initialized.";
  357. }
  358. return (FieldDescriptor::CppType)type_;
  359. }
  360. void SetValue(const void* val) {
  361. data_ = const_cast<void*>(val);
  362. }
  363. void CopyFrom(const MapValueRef& other) {
  364. type_ = other.type_;
  365. data_ = other.data_;
  366. }
  367. // Only used in DynamicMapField
  368. void DeleteData() {
  369. switch (type_) {
  370. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  371. case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  372. delete reinterpret_cast<TYPE*>(data_); \
  373. break; \
  374. }
  375. HANDLE_TYPE(INT32, int32);
  376. HANDLE_TYPE(INT64, int64);
  377. HANDLE_TYPE(UINT32, uint32);
  378. HANDLE_TYPE(UINT64, uint64);
  379. HANDLE_TYPE(DOUBLE, double);
  380. HANDLE_TYPE(FLOAT, float);
  381. HANDLE_TYPE(BOOL, bool);
  382. HANDLE_TYPE(STRING, string);
  383. HANDLE_TYPE(ENUM, int32);
  384. HANDLE_TYPE(MESSAGE, Message);
  385. #undef HANDLE_TYPE
  386. }
  387. }
  388. // data_ point to a map value. MapValueRef does not
  389. // own this value.
  390. void* data_;
  391. // type_ is 0 or a valid FieldDescriptor::CppType.
  392. int type_;
  393. };
  394. #undef TYPE_CHECK
  395. // This is the class for google::protobuf::Map's internal value_type. Instead of using
  396. // std::pair as value_type, we use this class which provides us more control of
  397. // its process of construction and destruction.
  398. template <typename Key, typename T>
  399. class MapPair {
  400. public:
  401. typedef const Key first_type;
  402. typedef T second_type;
  403. MapPair(const Key& other_first, const T& other_second)
  404. : first(other_first), second(other_second) {}
  405. explicit MapPair(const Key& other_first) : first(other_first), second() {}
  406. MapPair(const MapPair& other)
  407. : first(other.first), second(other.second) {}
  408. ~MapPair() {}
  409. // Implicitly convertible to std::pair of compatible types.
  410. template <typename T1, typename T2>
  411. operator std::pair<T1, T2>() const {
  412. return std::pair<T1, T2>(first, second);
  413. }
  414. const Key first;
  415. T second;
  416. private:
  417. friend class ::google::protobuf::Arena;
  418. friend class Map<Key, T>;
  419. };
  420. // google::protobuf::Map is an associative container type used to store protobuf map
  421. // fields. Its interface is similar to std::unordered_map. Users should use this
  422. // interface directly to visit or change map fields.
  423. template <typename Key, typename T>
  424. class Map {
  425. public:
  426. typedef Key key_type;
  427. typedef T mapped_type;
  428. typedef MapPair<Key, T> value_type;
  429. typedef value_type* pointer;
  430. typedef const value_type* const_pointer;
  431. typedef value_type& reference;
  432. typedef const value_type& const_reference;
  433. typedef size_t size_type;
  434. typedef hash<Key> hasher;
  435. typedef equal_to<Key> key_equal;
  436. Map()
  437. : arena_(NULL),
  438. allocator_(arena_),
  439. elements_(0, hasher(), key_equal(), allocator_),
  440. default_enum_value_(0) {}
  441. explicit Map(Arena* arena)
  442. : arena_(arena),
  443. allocator_(arena_),
  444. elements_(0, hasher(), key_equal(), allocator_),
  445. default_enum_value_(0) {
  446. arena_->OwnDestructor(&elements_);
  447. }
  448. Map(const Map& other)
  449. : arena_(NULL),
  450. allocator_(arena_),
  451. elements_(0, hasher(), key_equal(), allocator_),
  452. default_enum_value_(other.default_enum_value_) {
  453. insert(other.begin(), other.end());
  454. }
  455. template <class InputIt>
  456. explicit Map(const InputIt& first, const InputIt& last)
  457. : arena_(NULL),
  458. allocator_(arena_),
  459. elements_(0, hasher(), key_equal(), allocator_),
  460. default_enum_value_(0) {
  461. insert(first, last);
  462. }
  463. ~Map() { clear(); }
  464. private:
  465. // re-implement std::allocator to use arena allocator for memory allocation.
  466. // Used for google::protobuf::Map implementation. Users should not use this class
  467. // directly.
  468. template <typename U>
  469. class MapAllocator {
  470. public:
  471. typedef U value_type;
  472. typedef value_type* pointer;
  473. typedef const value_type* const_pointer;
  474. typedef value_type& reference;
  475. typedef const value_type& const_reference;
  476. typedef size_t size_type;
  477. typedef ptrdiff_t difference_type;
  478. MapAllocator() : arena_(NULL) {}
  479. explicit MapAllocator(Arena* arena) : arena_(arena) {}
  480. template <typename X>
  481. MapAllocator(const MapAllocator<X>& allocator)
  482. : arena_(allocator.arena_) {}
  483. pointer allocate(size_type n, const_pointer hint = 0) {
  484. // If arena is not given, malloc needs to be called which doesn't
  485. // construct element object.
  486. if (arena_ == NULL) {
  487. return reinterpret_cast<pointer>(malloc(n * sizeof(value_type)));
  488. } else {
  489. return reinterpret_cast<pointer>(
  490. Arena::CreateArray<uint8>(arena_, n * sizeof(value_type)));
  491. }
  492. }
  493. void deallocate(pointer p, size_type n) {
  494. if (arena_ == NULL) {
  495. free(p);
  496. }
  497. }
  498. #if __cplusplus >= 201103L && !defined(GOOGLE_PROTOBUF_OS_APPLE) && \
  499. !defined(GOOGLE_PROTOBUF_OS_NACL) && !defined(GOOGLE_PROTOBUF_OS_ANDROID)
  500. template<class NodeType, class... Args>
  501. void construct(NodeType* p, Args&&... args) {
  502. new (static_cast<void*>(p)) NodeType(std::forward<Args>(args)...);
  503. }
  504. template<class NodeType>
  505. void destroy(NodeType* p) {
  506. p->~NodeType();
  507. }
  508. #else
  509. void construct(pointer p, const_reference t) { new (p) value_type(t); }
  510. void destroy(pointer p) { p->~value_type(); }
  511. #endif
  512. template <typename X>
  513. struct rebind {
  514. typedef MapAllocator<X> other;
  515. };
  516. template <typename X>
  517. bool operator==(const MapAllocator<X>& other) const {
  518. return arena_ == other.arena_;
  519. }
  520. template <typename X>
  521. bool operator!=(const MapAllocator<X>& other) const {
  522. return arena_ != other.arena_;
  523. }
  524. // To support Visual Studio 2008
  525. /* size_type max_size() const {
  526. return std::numeric_limits<size_type>::max();
  527. }*/
  528. private:
  529. typedef void DestructorSkippable_;
  530. Arena* arena_;
  531. template <typename X>
  532. friend class MapAllocator;
  533. };
  534. public:
  535. typedef MapAllocator<std::pair<const Key, MapPair<Key, T>*> > Allocator;
  536. // Iterators
  537. class const_iterator
  538. : public std::iterator<std::forward_iterator_tag, value_type, ptrdiff_t,
  539. const value_type*, const value_type&> {
  540. typedef typename hash_map<Key, value_type*, hash<Key>, equal_to<Key>,
  541. Allocator>::const_iterator InnerIt;
  542. public:
  543. const_iterator() {}
  544. explicit const_iterator(const InnerIt& it) : it_(it) {}
  545. const_reference operator*() const { return *it_->second; }
  546. const_pointer operator->() const { return it_->second; }
  547. const_iterator& operator++() {
  548. ++it_;
  549. return *this;
  550. }
  551. const_iterator operator++(int) { return const_iterator(it_++); }
  552. friend bool operator==(const const_iterator& a, const const_iterator& b) {
  553. return a.it_ == b.it_;
  554. }
  555. friend bool operator!=(const const_iterator& a, const const_iterator& b) {
  556. return a.it_ != b.it_;
  557. }
  558. private:
  559. InnerIt it_;
  560. };
  561. class iterator : public std::iterator<std::forward_iterator_tag, value_type> {
  562. typedef typename hash_map<Key, value_type*, hasher, equal_to<Key>,
  563. Allocator>::iterator InnerIt;
  564. public:
  565. iterator() {}
  566. explicit iterator(const InnerIt& it) : it_(it) {}
  567. reference operator*() const { return *it_->second; }
  568. pointer operator->() const { return it_->second; }
  569. iterator& operator++() {
  570. ++it_;
  571. return *this;
  572. }
  573. iterator operator++(int) { return iterator(it_++); }
  574. // Implicitly convertible to const_iterator.
  575. operator const_iterator() const { return const_iterator(it_); }
  576. friend bool operator==(const iterator& a, const iterator& b) {
  577. return a.it_ == b.it_;
  578. }
  579. friend bool operator!=(const iterator& a, const iterator& b) {
  580. return a.it_ != b.it_;
  581. }
  582. private:
  583. friend class Map;
  584. InnerIt it_;
  585. };
  586. iterator begin() { return iterator(elements_.begin()); }
  587. iterator end() { return iterator(elements_.end()); }
  588. const_iterator begin() const { return const_iterator(elements_.begin()); }
  589. const_iterator end() const { return const_iterator(elements_.end()); }
  590. const_iterator cbegin() const { return begin(); }
  591. const_iterator cend() const { return end(); }
  592. // Capacity
  593. size_type size() const { return elements_.size(); }
  594. bool empty() const { return elements_.empty(); }
  595. // Element access
  596. T& operator[](const key_type& key) {
  597. value_type** value = &elements_[key];
  598. if (*value == NULL) {
  599. *value = CreateValueTypeInternal(key);
  600. internal::MapValueInitializer<google::protobuf::is_proto_enum<T>::value,
  601. T>::Initialize((*value)->second,
  602. default_enum_value_);
  603. }
  604. return (*value)->second;
  605. }
  606. const T& at(const key_type& key) const {
  607. const_iterator it = find(key);
  608. GOOGLE_CHECK(it != end());
  609. return it->second;
  610. }
  611. T& at(const key_type& key) {
  612. iterator it = find(key);
  613. GOOGLE_CHECK(it != end());
  614. return it->second;
  615. }
  616. // Lookup
  617. size_type count(const key_type& key) const {
  618. return elements_.count(key);
  619. }
  620. const_iterator find(const key_type& key) const {
  621. return const_iterator(elements_.find(key));
  622. }
  623. iterator find(const key_type& key) {
  624. return iterator(elements_.find(key));
  625. }
  626. std::pair<const_iterator, const_iterator> equal_range(
  627. const key_type& key) const {
  628. const_iterator it = find(key);
  629. if (it == end()) {
  630. return std::pair<const_iterator, const_iterator>(it, it);
  631. } else {
  632. const_iterator begin = it++;
  633. return std::pair<const_iterator, const_iterator>(begin, it);
  634. }
  635. }
  636. std::pair<iterator, iterator> equal_range(const key_type& key) {
  637. iterator it = find(key);
  638. if (it == end()) {
  639. return std::pair<iterator, iterator>(it, it);
  640. } else {
  641. iterator begin = it++;
  642. return std::pair<iterator, iterator>(begin, it);
  643. }
  644. }
  645. // insert
  646. std::pair<iterator, bool> insert(const value_type& value) {
  647. iterator it = find(value.first);
  648. if (it != end()) {
  649. return std::pair<iterator, bool>(it, false);
  650. } else {
  651. return std::pair<iterator, bool>(
  652. iterator(elements_.insert(std::pair<Key, value_type*>(
  653. value.first, CreateValueTypeInternal(value))).first), true);
  654. }
  655. }
  656. template <class InputIt>
  657. void insert(InputIt first, InputIt last) {
  658. for (InputIt it = first; it != last; ++it) {
  659. iterator exist_it = find(it->first);
  660. if (exist_it == end()) {
  661. operator[](it->first) = it->second;
  662. }
  663. }
  664. }
  665. // Erase
  666. size_type erase(const key_type& key) {
  667. typename hash_map<Key, value_type*, hash<Key>, equal_to<Key>,
  668. Allocator>::iterator it = elements_.find(key);
  669. if (it == elements_.end()) {
  670. return 0;
  671. } else {
  672. if (arena_ == NULL) delete it->second;
  673. elements_.erase(it);
  674. return 1;
  675. }
  676. }
  677. void erase(iterator pos) {
  678. if (arena_ == NULL) delete pos.it_->second;
  679. elements_.erase(pos.it_);
  680. }
  681. void erase(iterator first, iterator last) {
  682. for (iterator it = first; it != last;) {
  683. if (arena_ == NULL) delete it.it_->second;
  684. elements_.erase((it++).it_);
  685. }
  686. }
  687. void clear() {
  688. for (iterator it = begin(); it != end(); ++it) {
  689. if (arena_ == NULL) delete it.it_->second;
  690. }
  691. elements_.clear();
  692. }
  693. // Assign
  694. Map& operator=(const Map& other) {
  695. if (this != &other) {
  696. clear();
  697. insert(other.begin(), other.end());
  698. }
  699. return *this;
  700. }
  701. private:
  702. // Set default enum value only for proto2 map field whose value is enum type.
  703. void SetDefaultEnumValue(int default_enum_value) {
  704. default_enum_value_ = default_enum_value;
  705. }
  706. value_type* CreateValueTypeInternal(const Key& key) {
  707. if (arena_ == NULL) {
  708. return new value_type(key);
  709. } else {
  710. value_type* value = reinterpret_cast<value_type*>(
  711. Arena::CreateArray<uint8>(arena_, sizeof(value_type)));
  712. Arena::CreateInArenaStorage(const_cast<Key*>(&value->first), arena_);
  713. Arena::CreateInArenaStorage(&value->second, arena_);
  714. const_cast<Key&>(value->first) = key;
  715. return value;
  716. }
  717. }
  718. value_type* CreateValueTypeInternal(const value_type& value) {
  719. if (arena_ == NULL) {
  720. return new value_type(value);
  721. } else {
  722. value_type* p = reinterpret_cast<value_type*>(
  723. Arena::CreateArray<uint8>(arena_, sizeof(value_type)));
  724. Arena::CreateInArenaStorage(const_cast<Key*>(&p->first), arena_);
  725. Arena::CreateInArenaStorage(&p->second, arena_);
  726. const_cast<Key&>(p->first) = value.first;
  727. p->second = value.second;
  728. return p;
  729. }
  730. }
  731. Arena* arena_;
  732. Allocator allocator_;
  733. hash_map<Key, value_type*, hash<Key>, equal_to<Key>, Allocator> elements_;
  734. int default_enum_value_;
  735. friend class ::google::protobuf::Arena;
  736. typedef void InternalArenaConstructable_;
  737. typedef void DestructorSkippable_;
  738. template <typename K, typename V,
  739. internal::WireFormatLite::FieldType key_wire_type,
  740. internal::WireFormatLite::FieldType value_wire_type,
  741. int default_enum_value>
  742. friend class internal::MapFieldLite;
  743. };
  744. } // namespace protobuf
  745. } // namespace google
  746. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
  747. template<>
  748. struct hash<google::protobuf::MapKey> {
  749. size_t
  750. operator()(const google::protobuf::MapKey& map_key) const {
  751. switch (map_key.type()) {
  752. case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
  753. return hash<string>()(map_key.GetStringValue());
  754. case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
  755. return hash< ::google::protobuf::int64>()(map_key.GetInt64Value());
  756. case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
  757. return hash< ::google::protobuf::int32>()(map_key.GetInt32Value());
  758. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
  759. return hash< ::google::protobuf::uint64>()(map_key.GetUInt64Value());
  760. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
  761. return hash< ::google::protobuf::uint32>()(map_key.GetUInt32Value());
  762. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
  763. return hash<bool>()(map_key.GetBoolValue());
  764. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
  765. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
  766. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
  767. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
  768. GOOGLE_LOG(FATAL) << "Can't get here.";
  769. return 0;
  770. }
  771. GOOGLE_LOG(FATAL) << "Can't get here.";
  772. return 0;
  773. }
  774. bool
  775. operator()(const google::protobuf::MapKey& map_key1,
  776. const google::protobuf::MapKey& map_key2) const {
  777. switch (map_key1.type()) {
  778. #define COMPARE_CPPTYPE(CPPTYPE, CPPTYPE_METHOD) \
  779. case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: \
  780. return map_key1.Get##CPPTYPE_METHOD##Value() < \
  781. map_key2.Get##CPPTYPE_METHOD##Value();
  782. COMPARE_CPPTYPE(STRING, String)
  783. COMPARE_CPPTYPE(INT64, Int64)
  784. COMPARE_CPPTYPE(INT32, Int32)
  785. COMPARE_CPPTYPE(UINT64, UInt64)
  786. COMPARE_CPPTYPE(UINT32, UInt32)
  787. COMPARE_CPPTYPE(BOOL, Bool)
  788. #undef COMPARE_CPPTYPE
  789. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
  790. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
  791. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
  792. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
  793. GOOGLE_LOG(FATAL) << "Can't get here.";
  794. return true;
  795. }
  796. GOOGLE_LOG(FATAL) << "Can't get here.";
  797. return true;
  798. }
  799. };
  800. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END
  801. #endif // GOOGLE_PROTOBUF_MAP_H__