诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 line
7.4 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_FIELD_LITE_H__
  31. #define GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__
  32. #include <type_traits>
  33. #include <google/protobuf/parse_context.h>
  34. #include <google/protobuf/io/coded_stream.h>
  35. #include <google/protobuf/map.h>
  36. #include <google/protobuf/map_entry_lite.h>
  37. #include <google/protobuf/port.h>
  38. #include <google/protobuf/wire_format_lite.h>
  39. #include <google/protobuf/port_def.inc>
  40. #ifdef SWIG
  41. #error "You cannot SWIG proto headers"
  42. #endif
  43. namespace google {
  44. namespace protobuf {
  45. namespace internal {
  46. // This class provides access to map field using generated api. It is used for
  47. // internal generated message implentation only. Users should never use this
  48. // directly.
  49. template <typename Derived, typename Key, typename T,
  50. WireFormatLite::FieldType key_wire_type,
  51. WireFormatLite::FieldType value_wire_type, int default_enum_value = 0>
  52. class MapFieldLite {
  53. // Define message type for internal repeated field.
  54. typedef Derived EntryType;
  55. public:
  56. typedef Map<Key, T> MapType;
  57. typedef EntryType EntryTypeTrait;
  58. MapFieldLite() { SetDefaultEnumValue(); }
  59. explicit MapFieldLite(Arena* arena) : map_(arena) { SetDefaultEnumValue(); }
  60. // Accessors
  61. const Map<Key, T>& GetMap() const { return map_; }
  62. Map<Key, T>* MutableMap() { return &map_; }
  63. // Convenient methods for generated message implementation.
  64. int size() const { return static_cast<int>(map_.size()); }
  65. void Clear() { return map_.clear(); }
  66. void MergeFrom(const MapFieldLite& other) {
  67. for (typename Map<Key, T>::const_iterator it = other.map_.begin();
  68. it != other.map_.end(); ++it) {
  69. map_[it->first] = it->second;
  70. }
  71. }
  72. void Swap(MapFieldLite* other) { map_.swap(other->map_); }
  73. // Set default enum value only for proto2 map field whose value is enum type.
  74. void SetDefaultEnumValue() {
  75. MutableMap()->SetDefaultEnumValue(default_enum_value);
  76. }
  77. // Used in the implementation of parsing. Caller should take the ownership iff
  78. // arena_ is NULL.
  79. EntryType* NewEntry() const {
  80. return Arena::CreateMessage<EntryType>(map_.arena_);
  81. }
  82. // Used in the implementation of serializing enum value type. Caller should
  83. // take the ownership iff arena_ is NULL.
  84. EntryType* NewEnumEntryWrapper(const Key& key, const T t) const {
  85. return EntryType::EnumWrap(key, t, map_.arena_);
  86. }
  87. // Used in the implementation of serializing other value types. Caller should
  88. // take the ownership iff arena_ is NULL.
  89. EntryType* NewEntryWrapper(const Key& key, const T& t) const {
  90. return EntryType::Wrap(key, t, map_.arena_);
  91. }
  92. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  93. typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
  94. return parser._InternalParse(ptr, ctx);
  95. }
  96. template <typename Metadata>
  97. const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx,
  98. bool (*is_valid)(int), uint32 field_num,
  99. Metadata* metadata) {
  100. typename Derived::template Parser<MapFieldLite, Map<Key, T>> parser(this);
  101. return parser.ParseWithEnumValidation(ptr, ctx, is_valid, field_num,
  102. metadata);
  103. }
  104. private:
  105. typedef void DestructorSkippable_;
  106. Map<Key, T> map_;
  107. friend class ::PROTOBUF_NAMESPACE_ID::Arena;
  108. };
  109. template <typename T, typename Metadata>
  110. struct EnumParseWrapper {
  111. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  112. return map_field->ParseWithEnumValidation(ptr, ctx, is_valid, field_num,
  113. metadata);
  114. }
  115. T* map_field;
  116. bool (*is_valid)(int);
  117. uint32 field_num;
  118. Metadata* metadata;
  119. };
  120. // Helper function because the typenames of maps are horrendous to print. This
  121. // leverages compiler type deduction, to keep all type data out of the
  122. // generated code
  123. template <typename T, typename Metadata>
  124. EnumParseWrapper<T, Metadata> InitEnumParseWrapper(T* map_field,
  125. bool (*is_valid)(int),
  126. uint32 field_num,
  127. Metadata* metadata) {
  128. return EnumParseWrapper<T, Metadata>{map_field, is_valid, field_num,
  129. metadata};
  130. }
  131. // True if IsInitialized() is true for value field in all elements of t. T is
  132. // expected to be message. It's useful to have this helper here to keep the
  133. // protobuf compiler from ever having to emit loops in IsInitialized() methods.
  134. // We want the C++ compiler to inline this or not as it sees fit.
  135. template <typename Key, typename T>
  136. bool AllAreInitialized(const Map<Key, T>& t) {
  137. for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
  138. ++it) {
  139. if (!it->second.IsInitialized()) return false;
  140. }
  141. return true;
  142. }
  143. template <typename MEntry>
  144. struct MapEntryToMapField : MapEntryToMapField<typename MEntry::SuperType> {};
  145. template <typename T, typename Key, typename Value,
  146. WireFormatLite::FieldType kKeyFieldType,
  147. WireFormatLite::FieldType kValueFieldType, int default_enum_value>
  148. struct MapEntryToMapField<MapEntryLite<T, Key, Value, kKeyFieldType,
  149. kValueFieldType, default_enum_value>> {
  150. typedef MapFieldLite<MapEntryLite<T, Key, Value, kKeyFieldType,
  151. kValueFieldType, default_enum_value>,
  152. Key, Value, kKeyFieldType, kValueFieldType,
  153. default_enum_value>
  154. MapFieldType;
  155. };
  156. } // namespace internal
  157. } // namespace protobuf
  158. } // namespace google
  159. #include <google/protobuf/port_undef.inc>
  160. #endif // GOOGLE_PROTOBUF_MAP_FIELD_LITE_H__