诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

677 Zeilen
26 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_ENTRY_LITE_H__
  31. #define GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__
  32. #include <assert.h>
  33. #include <string>
  34. #include <google/protobuf/stubs/casts.h>
  35. #include <google/protobuf/parse_context.h>
  36. #include <google/protobuf/io/coded_stream.h>
  37. #include <google/protobuf/arena.h>
  38. #include <google/protobuf/arenastring.h>
  39. #include <google/protobuf/generated_message_util.h>
  40. #include <google/protobuf/map.h>
  41. #include <google/protobuf/map_type_handler.h>
  42. #include <google/protobuf/port.h>
  43. #include <google/protobuf/wire_format_lite.h>
  44. #include <google/protobuf/port_def.inc>
  45. #ifdef SWIG
  46. #error "You cannot SWIG proto headers"
  47. #endif
  48. namespace google {
  49. namespace protobuf {
  50. namespace internal {
  51. template <typename Derived, typename Key, typename Value,
  52. WireFormatLite::FieldType kKeyFieldType,
  53. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  54. class MapEntry;
  55. template <typename Derived, typename Key, typename Value,
  56. WireFormatLite::FieldType kKeyFieldType,
  57. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  58. class MapFieldLite;
  59. } // namespace internal
  60. } // namespace protobuf
  61. } // namespace google
  62. namespace google {
  63. namespace protobuf {
  64. namespace internal {
  65. // MoveHelper::Move is used to set *dest. It copies *src, or moves it (in
  66. // the C++11 sense), or swaps it. *src is left in a sane state for
  67. // subsequent destruction, but shouldn't be used for anything.
  68. template <bool is_enum, bool is_message, bool is_stringlike, typename T>
  69. struct MoveHelper { // primitives
  70. static void Move(T* src, T* dest) { *dest = *src; }
  71. };
  72. template <bool is_message, bool is_stringlike, typename T>
  73. struct MoveHelper<true, is_message, is_stringlike, T> { // enums
  74. static void Move(T* src, T* dest) { *dest = *src; }
  75. // T is an enum here, so allow conversions to and from int.
  76. static void Move(T* src, int* dest) { *dest = static_cast<int>(*src); }
  77. static void Move(int* src, T* dest) { *dest = static_cast<T>(*src); }
  78. };
  79. template <bool is_stringlike, typename T>
  80. struct MoveHelper<false, true, is_stringlike, T> { // messages
  81. static void Move(T* src, T* dest) { dest->Swap(src); }
  82. };
  83. template <typename T>
  84. struct MoveHelper<false, false, true, T> { // strings and similar
  85. static void Move(T* src, T* dest) {
  86. #if __cplusplus >= 201103L
  87. *dest = std::move(*src);
  88. #else
  89. dest->swap(*src);
  90. #endif
  91. }
  92. };
  93. // Functions for operating on a map entry. Does not contain any representation
  94. // (this class is not intended to be instantiated).
  95. template <typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType,
  96. WireFormatLite::FieldType kValueFieldType>
  97. struct MapEntryFuncs {
  98. typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
  99. typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler;
  100. static const int kKeyFieldNumber = 1;
  101. static const int kValueFieldNumber = 2;
  102. static uint8* InternalSerialize(int field_number, const Key& key,
  103. const Value& value, uint8* ptr,
  104. io::EpsCopyOutputStream* stream) {
  105. ptr = stream->EnsureSpace(ptr);
  106. ptr = WireFormatLite::WriteTagToArray(
  107. field_number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, ptr);
  108. ptr = io::CodedOutputStream::WriteVarint32ToArray(GetCachedSize(key, value),
  109. ptr);
  110. ptr = KeyTypeHandler::Write(kKeyFieldNumber, key, ptr, stream);
  111. return ValueTypeHandler::Write(kValueFieldNumber, value, ptr, stream);
  112. }
  113. static size_t ByteSizeLong(const Key& key, const Value& value) {
  114. // Tags for key and value will both be one byte (field numbers 1 and 2).
  115. size_t inner_length =
  116. 2 + KeyTypeHandler::ByteSize(key) + ValueTypeHandler::ByteSize(value);
  117. return inner_length + io::CodedOutputStream::VarintSize32(inner_length);
  118. }
  119. static int GetCachedSize(const Key& key, const Value& value) {
  120. // Tags for key and value will both be one byte (field numbers 1 and 2).
  121. return 2 + KeyTypeHandler::GetCachedSize(key) +
  122. ValueTypeHandler::GetCachedSize(value);
  123. }
  124. };
  125. // MapEntryImpl is used to implement parsing and serialization of map entries.
  126. // It uses Curious Recursive Template Pattern (CRTP) to provide the type of
  127. // the eventual code to the template code.
  128. template <typename Derived, typename Base, typename Key, typename Value,
  129. WireFormatLite::FieldType kKeyFieldType,
  130. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  131. class MapEntryImpl : public Base {
  132. public:
  133. typedef MapEntryFuncs<Key, Value, kKeyFieldType, kValueFieldType> Funcs;
  134. protected:
  135. // Provide utilities to parse/serialize key/value. Provide utilities to
  136. // manipulate internal stored type.
  137. typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
  138. typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler;
  139. // Define internal memory layout. Strings and messages are stored as
  140. // pointers, while other types are stored as values.
  141. typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory;
  142. typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory;
  143. // Enum type cannot be used for MapTypeHandler::Read. Define a type
  144. // which will replace Enum with int.
  145. typedef typename KeyTypeHandler::MapEntryAccessorType KeyMapEntryAccessorType;
  146. typedef
  147. typename ValueTypeHandler::MapEntryAccessorType ValueMapEntryAccessorType;
  148. // Constants for field number.
  149. static const int kKeyFieldNumber = 1;
  150. static const int kValueFieldNumber = 2;
  151. // Constants for field tag.
  152. static const uint8 kKeyTag =
  153. GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kKeyFieldNumber, KeyTypeHandler::kWireType);
  154. static const uint8 kValueTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
  155. kValueFieldNumber, ValueTypeHandler::kWireType);
  156. static const size_t kTagSize = 1;
  157. public:
  158. // Work-around for a compiler bug (see repeated_field.h).
  159. typedef void MapEntryHasMergeTypeTrait;
  160. typedef Derived EntryType;
  161. typedef Key EntryKeyType;
  162. typedef Value EntryValueType;
  163. static const WireFormatLite::FieldType kEntryKeyFieldType = kKeyFieldType;
  164. static const WireFormatLite::FieldType kEntryValueFieldType = kValueFieldType;
  165. static const int kEntryDefaultEnumValue = default_enum_value;
  166. MapEntryImpl() : arena_(NULL) {
  167. KeyTypeHandler::Initialize(&key_, NULL);
  168. ValueTypeHandler::InitializeMaybeByDefaultEnum(&value_, default_enum_value,
  169. NULL);
  170. _has_bits_[0] = 0;
  171. }
  172. explicit MapEntryImpl(Arena* arena) : arena_(arena) {
  173. KeyTypeHandler::Initialize(&key_, arena);
  174. ValueTypeHandler::InitializeMaybeByDefaultEnum(&value_, default_enum_value,
  175. arena);
  176. _has_bits_[0] = 0;
  177. }
  178. ~MapEntryImpl() {
  179. if (GetArenaNoVirtual() != NULL) return;
  180. KeyTypeHandler::DeleteNoArena(key_);
  181. ValueTypeHandler::DeleteNoArena(value_);
  182. }
  183. // accessors ======================================================
  184. virtual inline const KeyMapEntryAccessorType& key() const {
  185. return KeyTypeHandler::GetExternalReference(key_);
  186. }
  187. virtual inline const ValueMapEntryAccessorType& value() const {
  188. return ValueTypeHandler::DefaultIfNotInitialized(
  189. value_, Derived::internal_default_instance()->value_);
  190. }
  191. inline KeyMapEntryAccessorType* mutable_key() {
  192. set_has_key();
  193. return KeyTypeHandler::EnsureMutable(&key_, GetArenaNoVirtual());
  194. }
  195. inline ValueMapEntryAccessorType* mutable_value() {
  196. set_has_value();
  197. return ValueTypeHandler::EnsureMutable(&value_, GetArenaNoVirtual());
  198. }
  199. // implements MessageLite =========================================
  200. // MapEntryImpl is for implementation only and this function isn't called
  201. // anywhere. Just provide a fake implementation here for MessageLite.
  202. std::string GetTypeName() const override { return ""; }
  203. void CheckTypeAndMergeFrom(const MessageLite& other) override {
  204. MergeFromInternal(*::google::protobuf::internal::DownCast<const Derived*>(&other));
  205. }
  206. const char* _InternalParse(const char* ptr, ParseContext* ctx) final {
  207. while (!ctx->Done(&ptr)) {
  208. uint32 tag;
  209. ptr = ReadTag(ptr, &tag);
  210. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  211. if (tag == kKeyTag) {
  212. set_has_key();
  213. KeyMapEntryAccessorType* key = mutable_key();
  214. ptr = KeyTypeHandler::Read(ptr, ctx, key);
  215. if (!Derived::ValidateKey(key)) return nullptr;
  216. } else if (tag == kValueTag) {
  217. set_has_value();
  218. ValueMapEntryAccessorType* value = mutable_value();
  219. ptr = ValueTypeHandler::Read(ptr, ctx, value);
  220. if (!Derived::ValidateValue(value)) return nullptr;
  221. } else {
  222. if (tag == 0 || WireFormatLite::GetTagWireType(tag) ==
  223. WireFormatLite::WIRETYPE_END_GROUP) {
  224. ctx->SetLastTag(tag);
  225. return ptr;
  226. }
  227. ptr = UnknownFieldParse(tag, static_cast<string*>(nullptr), ptr, ctx);
  228. }
  229. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  230. }
  231. return ptr;
  232. }
  233. size_t ByteSizeLong() const override {
  234. size_t size = 0;
  235. size += has_key() ? kTagSize +
  236. static_cast<size_t>(KeyTypeHandler::ByteSize(key()))
  237. : 0;
  238. size += has_value()
  239. ? kTagSize +
  240. static_cast<size_t>(ValueTypeHandler::ByteSize(value()))
  241. : 0;
  242. return size;
  243. }
  244. ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
  245. ::google::protobuf::uint8* ptr, io::EpsCopyOutputStream* stream) const override {
  246. ptr = KeyTypeHandler::Write(kKeyFieldNumber, key(), ptr, stream);
  247. return ValueTypeHandler::Write(kValueFieldNumber, value(), ptr, stream);
  248. }
  249. // Don't override SerializeWithCachedSizesToArray. Use MessageLite's.
  250. int GetCachedSize() const override {
  251. int size = 0;
  252. size += has_key() ? static_cast<int>(kTagSize) +
  253. KeyTypeHandler::GetCachedSize(key())
  254. : 0;
  255. size += has_value() ? static_cast<int>(kTagSize) +
  256. ValueTypeHandler::GetCachedSize(value())
  257. : 0;
  258. return size;
  259. }
  260. bool IsInitialized() const override {
  261. return ValueTypeHandler::IsInitialized(value_);
  262. }
  263. Base* New() const override {
  264. Derived* entry = new Derived;
  265. return entry;
  266. }
  267. Base* New(Arena* arena) const override {
  268. Derived* entry = Arena::CreateMessage<Derived>(arena);
  269. return entry;
  270. }
  271. protected:
  272. // We can't declare this function directly here as it would hide the other
  273. // overload (const Message&).
  274. void MergeFromInternal(const MapEntryImpl& from) {
  275. if (from._has_bits_[0]) {
  276. if (from.has_key()) {
  277. KeyTypeHandler::EnsureMutable(&key_, GetArenaNoVirtual());
  278. KeyTypeHandler::Merge(from.key(), &key_, GetArenaNoVirtual());
  279. set_has_key();
  280. }
  281. if (from.has_value()) {
  282. ValueTypeHandler::EnsureMutable(&value_, GetArenaNoVirtual());
  283. ValueTypeHandler::Merge(from.value(), &value_, GetArenaNoVirtual());
  284. set_has_value();
  285. }
  286. }
  287. }
  288. public:
  289. void Clear() override {
  290. KeyTypeHandler::Clear(&key_, GetArenaNoVirtual());
  291. ValueTypeHandler::ClearMaybeByDefaultEnum(&value_, GetArenaNoVirtual(),
  292. default_enum_value);
  293. clear_has_key();
  294. clear_has_value();
  295. }
  296. static void InitAsDefaultInstance() {
  297. Derived* d = const_cast<Derived*>(Derived::internal_default_instance());
  298. KeyTypeHandler::AssignDefaultValue(&d->key_);
  299. ValueTypeHandler::AssignDefaultValue(&d->value_);
  300. }
  301. Arena* GetArena() const override { return GetArenaNoVirtual(); }
  302. // Parsing using MergePartialFromCodedStream, above, is not as
  303. // efficient as it could be. This helper class provides a speedier way.
  304. template <typename MapField, typename Map>
  305. class Parser {
  306. public:
  307. explicit Parser(MapField* mf) : mf_(mf), map_(mf->MutableMap()) {}
  308. ~Parser() {
  309. if (entry_ != nullptr && entry_->GetArena() == nullptr) delete entry_;
  310. }
  311. // This does what the typical MergePartialFromCodedStream() is expected to
  312. // do, with the additional side-effect that if successful (i.e., if true is
  313. // going to be its return value) it inserts the key-value pair into map_.
  314. bool MergePartialFromCodedStream(io::CodedInputStream* input) {
  315. // Look for the expected thing: a key and then a value. If it fails,
  316. // invoke the enclosing class's MergePartialFromCodedStream, or return
  317. // false if that would be pointless.
  318. if (input->ExpectTag(kKeyTag)) {
  319. if (!KeyTypeHandler::Read(input, &key_)) {
  320. return false;
  321. }
  322. // Peek at the next byte to see if it is kValueTag. If not, bail out.
  323. const void* data;
  324. int size;
  325. input->GetDirectBufferPointerInline(&data, &size);
  326. // We could use memcmp here, but we don't bother. The tag is one byte.
  327. static_assert(kTagSize == 1, "tag size must be 1");
  328. if (size > 0 && *reinterpret_cast<const char*>(data) == kValueTag) {
  329. typename Map::size_type map_size = map_->size();
  330. value_ptr_ = &(*map_)[key_];
  331. if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) {
  332. // We created a new key-value pair. Fill in the value.
  333. typedef
  334. typename MapIf<ValueTypeHandler::kIsEnum, int*, Value*>::type T;
  335. input->Skip(kTagSize); // Skip kValueTag.
  336. if (!ValueTypeHandler::Read(input,
  337. reinterpret_cast<T>(value_ptr_))) {
  338. map_->erase(key_); // Failure! Undo insertion.
  339. return false;
  340. }
  341. if (input->ExpectAtEnd()) return true;
  342. return ReadBeyondKeyValuePair(input);
  343. }
  344. }
  345. } else {
  346. key_ = Key();
  347. }
  348. NewEntry();
  349. *entry_->mutable_key() = key_;
  350. const bool result = entry_->MergePartialFromCodedStream(input);
  351. if (result) UseKeyAndValueFromEntry();
  352. return result;
  353. }
  354. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  355. if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kKeyTag)) {
  356. ptr = KeyTypeHandler::Read(ptr + 1, ctx, &key_);
  357. if (PROTOBUF_PREDICT_FALSE(!ptr || !Derived::ValidateKey(&key_))) {
  358. return nullptr;
  359. }
  360. if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kValueTag)) {
  361. typename Map::size_type map_size = map_->size();
  362. value_ptr_ = &(*map_)[key_];
  363. if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) {
  364. using T =
  365. typename MapIf<ValueTypeHandler::kIsEnum, int*, Value*>::type;
  366. ptr = ValueTypeHandler::Read(ptr + 1, ctx,
  367. reinterpret_cast<T>(value_ptr_));
  368. if (PROTOBUF_PREDICT_FALSE(!ptr ||
  369. !Derived::ValidateValue(value_ptr_))) {
  370. map_->erase(key_); // Failure! Undo insertion.
  371. return nullptr;
  372. }
  373. if (PROTOBUF_PREDICT_TRUE(ctx->Done(&ptr))) return ptr;
  374. if (!ptr) return nullptr;
  375. NewEntry();
  376. ValueMover::Move(value_ptr_, entry_->mutable_value());
  377. map_->erase(key_);
  378. goto move_key;
  379. }
  380. } else {
  381. if (!ptr) return nullptr;
  382. }
  383. NewEntry();
  384. move_key:
  385. KeyMover::Move(&key_, entry_->mutable_key());
  386. } else {
  387. if (!ptr) return nullptr;
  388. NewEntry();
  389. }
  390. ptr = entry_->_InternalParse(ptr, ctx);
  391. if (ptr) UseKeyAndValueFromEntry();
  392. return ptr;
  393. }
  394. template <typename Metadata>
  395. const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
  396. bool (*is_valid)(int), uint32 field_num,
  397. Metadata* metadata) {
  398. auto entry = NewEntry();
  399. ptr = entry->_InternalParse(ptr, ctx);
  400. if (!ptr) return nullptr;
  401. if (is_valid(entry->value())) {
  402. UseKeyAndValueFromEntry();
  403. } else {
  404. WriteLengthDelimited(field_num, entry->SerializeAsString(),
  405. metadata->mutable_unknown_fields());
  406. }
  407. return ptr;
  408. }
  409. MapEntryImpl* NewEntry() { return entry_ = mf_->NewEntry(); }
  410. const Key& key() const { return key_; }
  411. const Value& value() const { return *value_ptr_; }
  412. const Key& entry_key() const { return entry_->key(); }
  413. const Value& entry_value() const { return entry_->value(); }
  414. private:
  415. void UseKeyAndValueFromEntry() {
  416. // Update key_ in case we need it later (because key() is called).
  417. // This is potentially inefficient, especially if the key is
  418. // expensive to copy (e.g., a long string), but this is a cold
  419. // path, so it's not a big deal.
  420. key_ = entry_->key();
  421. value_ptr_ = &(*map_)[key_];
  422. ValueMover::Move(entry_->mutable_value(), value_ptr_);
  423. }
  424. // After reading a key and value successfully, and inserting that data
  425. // into map_, we are not at the end of the input. This is unusual, but
  426. // allowed by the spec.
  427. bool ReadBeyondKeyValuePair(io::CodedInputStream* input) PROTOBUF_COLD {
  428. NewEntry();
  429. ValueMover::Move(value_ptr_, entry_->mutable_value());
  430. map_->erase(key_);
  431. KeyMover::Move(&key_, entry_->mutable_key());
  432. const bool result = entry_->MergePartialFromCodedStream(input);
  433. if (result) UseKeyAndValueFromEntry();
  434. return result;
  435. }
  436. typedef MoveHelper<KeyTypeHandler::kIsEnum, KeyTypeHandler::kIsMessage,
  437. KeyTypeHandler::kWireType ==
  438. WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
  439. Key>
  440. KeyMover;
  441. typedef MoveHelper<ValueTypeHandler::kIsEnum, ValueTypeHandler::kIsMessage,
  442. ValueTypeHandler::kWireType ==
  443. WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
  444. Value>
  445. ValueMover;
  446. MapField* const mf_;
  447. Map* const map_;
  448. Key key_;
  449. Value* value_ptr_;
  450. MapEntryImpl* entry_ = nullptr;
  451. };
  452. protected:
  453. void set_has_key() { _has_bits_[0] |= 0x00000001u; }
  454. bool has_key() const { return (_has_bits_[0] & 0x00000001u) != 0; }
  455. void clear_has_key() { _has_bits_[0] &= ~0x00000001u; }
  456. void set_has_value() { _has_bits_[0] |= 0x00000002u; }
  457. bool has_value() const { return (_has_bits_[0] & 0x00000002u) != 0; }
  458. void clear_has_value() { _has_bits_[0] &= ~0x00000002u; }
  459. public:
  460. inline Arena* GetArenaNoVirtual() const { return arena_; }
  461. public: // Needed for constructing tables
  462. KeyOnMemory key_;
  463. ValueOnMemory value_;
  464. Arena* arena_;
  465. uint32 _has_bits_[1];
  466. private:
  467. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  468. typedef void InternalArenaConstructable_;
  469. typedef void DestructorSkippable_;
  470. template <typename C, typename K, typename V, WireFormatLite::FieldType,
  471. WireFormatLite::FieldType, int>
  472. friend class internal::MapEntry;
  473. template <typename C, typename K, typename V, WireFormatLite::FieldType,
  474. WireFormatLite::FieldType, int>
  475. friend class internal::MapFieldLite;
  476. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryImpl);
  477. };
  478. template <typename T, typename Key, typename Value,
  479. WireFormatLite::FieldType kKeyFieldType,
  480. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  481. class MapEntryLite
  482. : public MapEntryImpl<T, MessageLite, Key, Value, kKeyFieldType,
  483. kValueFieldType, default_enum_value> {
  484. public:
  485. typedef MapEntryImpl<T, MessageLite, Key, Value, kKeyFieldType,
  486. kValueFieldType, default_enum_value>
  487. SuperType;
  488. MapEntryLite() {}
  489. explicit MapEntryLite(Arena* arena) : SuperType(arena) {}
  490. void MergeFrom(const MapEntryLite& other) { MergeFromInternal(other); }
  491. private:
  492. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryLite);
  493. };
  494. // The completely unprincipled and unwieldy use of template parameters in
  495. // the map code necessitates wrappers to make the code a little bit more
  496. // manageable.
  497. template <typename Derived>
  498. struct DeconstructMapEntry;
  499. template <typename T, typename K, typename V, WireFormatLite::FieldType key,
  500. WireFormatLite::FieldType value, int default_enum>
  501. struct DeconstructMapEntry<MapEntryLite<T, K, V, key, value, default_enum> > {
  502. typedef K Key;
  503. typedef V Value;
  504. static const WireFormatLite::FieldType kKeyFieldType = key;
  505. static const WireFormatLite::FieldType kValueFieldType = value;
  506. static const int default_enum_value = default_enum;
  507. };
  508. // Helpers for deterministic serialization =============================
  509. // This struct can be used with any generic sorting algorithm. If the Key
  510. // type is relatively small and easy to copy then copying Keys into an
  511. // array of SortItems can be beneficial. Then all the data the sorting
  512. // algorithm needs to touch is in that one array.
  513. template <typename Key, typename PtrToKeyValuePair>
  514. struct SortItem {
  515. SortItem() {}
  516. explicit SortItem(PtrToKeyValuePair p) : first(p->first), second(p) {}
  517. Key first;
  518. PtrToKeyValuePair second;
  519. };
  520. template <typename T>
  521. struct CompareByFirstField {
  522. bool operator()(const T& a, const T& b) const { return a.first < b.first; }
  523. };
  524. template <typename T>
  525. struct CompareByDerefFirst {
  526. bool operator()(const T& a, const T& b) const { return a->first < b->first; }
  527. };
  528. // Helper for table driven serialization
  529. template <WireFormatLite::FieldType FieldType>
  530. struct FromHelper {
  531. template <typename T>
  532. static const T& From(const T& x) {
  533. return x;
  534. }
  535. };
  536. template <>
  537. struct FromHelper<WireFormatLite::TYPE_STRING> {
  538. static ArenaStringPtr From(const std::string& x) {
  539. ArenaStringPtr res;
  540. TaggedPtr<std::string> ptr;
  541. ptr.Set(const_cast<std::string*>(&x));
  542. res.UnsafeSetTaggedPointer(ptr);
  543. return res;
  544. }
  545. };
  546. template <>
  547. struct FromHelper<WireFormatLite::TYPE_BYTES> {
  548. static ArenaStringPtr From(const std::string& x) {
  549. ArenaStringPtr res;
  550. TaggedPtr<std::string> ptr;
  551. ptr.Set(const_cast<std::string*>(&x));
  552. res.UnsafeSetTaggedPointer(ptr);
  553. return res;
  554. }
  555. };
  556. template <>
  557. struct FromHelper<WireFormatLite::TYPE_MESSAGE> {
  558. template <typename T>
  559. static T* From(const T& x) {
  560. return const_cast<T*>(&x);
  561. }
  562. };
  563. template <typename MapEntryType>
  564. struct MapEntryHelper;
  565. template <typename T, typename Key, typename Value,
  566. WireFormatLite::FieldType kKeyFieldType,
  567. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  568. struct MapEntryHelper<MapEntryLite<T, Key, Value, kKeyFieldType,
  569. kValueFieldType, default_enum_value> > {
  570. // Provide utilities to parse/serialize key/value. Provide utilities to
  571. // manipulate internal stored type.
  572. typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler;
  573. typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler;
  574. // Define internal memory layout. Strings and messages are stored as
  575. // pointers, while other types are stored as values.
  576. typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory;
  577. typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory;
  578. explicit MapEntryHelper(const MapPair<Key, Value>& map_pair)
  579. : _has_bits_(3),
  580. _cached_size_(2 + KeyTypeHandler::GetCachedSize(map_pair.first) +
  581. ValueTypeHandler::GetCachedSize(map_pair.second)),
  582. key_(FromHelper<kKeyFieldType>::From(map_pair.first)),
  583. value_(FromHelper<kValueFieldType>::From(map_pair.second)) {}
  584. // Purposely not folowing the style guide naming. These are the names
  585. // the proto compiler would generate given the map entry descriptor.
  586. // The proto compiler generates the offsets in this struct as if this was
  587. // a regular message. This way the table driven code barely notices it's
  588. // dealing with a map field.
  589. uint32 _has_bits_; // NOLINT
  590. uint32 _cached_size_; // NOLINT
  591. KeyOnMemory key_; // NOLINT
  592. ValueOnMemory value_; // NOLINT
  593. };
  594. } // namespace internal
  595. } // namespace protobuf
  596. } // namespace google
  597. #include <google/protobuf/port_undef.inc>
  598. #endif // GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__