诸暨麻将添加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.
 
 
 
 
 
 

241 rivejä
10 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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Defines an implementation of Message which can emulate types which are not
  35. // known at compile-time.
  36. #ifndef GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
  37. #define GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
  38. #include <algorithm>
  39. #include <memory>
  40. #include <vector>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/message.h>
  43. #include <google/protobuf/reflection.h>
  44. #include <google/protobuf/stubs/mutex.h>
  45. #include <google/protobuf/reflection.h>
  46. #include <google/protobuf/repeated_field.h>
  47. #ifdef SWIG
  48. #error "You cannot SWIG proto headers"
  49. #endif
  50. #include <google/protobuf/port_def.inc>
  51. namespace google {
  52. namespace protobuf {
  53. // Defined in other files.
  54. class Descriptor; // descriptor.h
  55. class DescriptorPool; // descriptor.h
  56. // Constructs implementations of Message which can emulate types which are not
  57. // known at compile-time.
  58. //
  59. // Sometimes you want to be able to manipulate protocol types that you don't
  60. // know about at compile time. It would be nice to be able to construct
  61. // a Message object which implements the message type given by any arbitrary
  62. // Descriptor. DynamicMessage provides this.
  63. //
  64. // As it turns out, a DynamicMessage needs to construct extra
  65. // information about its type in order to operate. Most of this information
  66. // can be shared between all DynamicMessages of the same type. But, caching
  67. // this information in some sort of global map would be a bad idea, since
  68. // the cached information for a particular descriptor could outlive the
  69. // descriptor itself. To avoid this problem, DynamicMessageFactory
  70. // encapsulates this "cache". All DynamicMessages of the same type created
  71. // from the same factory will share the same support data. Any Descriptors
  72. // used with a particular factory must outlive the factory.
  73. class PROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
  74. public:
  75. // Construct a DynamicMessageFactory that will search for extensions in
  76. // the DescriptorPool in which the extendee is defined.
  77. DynamicMessageFactory();
  78. // Construct a DynamicMessageFactory that will search for extensions in
  79. // the given DescriptorPool.
  80. //
  81. // DEPRECATED: Use CodedInputStream::SetExtensionRegistry() to tell the
  82. // parser to look for extensions in an alternate pool. However, note that
  83. // this is almost never what you want to do. Almost all users should use
  84. // the zero-arg constructor.
  85. DynamicMessageFactory(const DescriptorPool* pool);
  86. ~DynamicMessageFactory();
  87. // Call this to tell the DynamicMessageFactory that if it is given a
  88. // Descriptor d for which:
  89. // d->file()->pool() == DescriptorPool::generated_pool(),
  90. // then it should delegate to MessageFactory::generated_factory() instead
  91. // of constructing a dynamic implementation of the message. In theory there
  92. // is no down side to doing this, so it may become the default in the future.
  93. void SetDelegateToGeneratedFactory(bool enable) {
  94. delegate_to_generated_factory_ = enable;
  95. }
  96. // implements MessageFactory ---------------------------------------
  97. // Given a Descriptor, constructs the default (prototype) Message of that
  98. // type. You can then call that message's New() method to construct a
  99. // mutable message of that type.
  100. //
  101. // Calling this method twice with the same Descriptor returns the same
  102. // object. The returned object remains property of the factory and will
  103. // be destroyed when the factory is destroyed. Also, any objects created
  104. // by calling the prototype's New() method share some data with the
  105. // prototype, so these must be destroyed before the DynamicMessageFactory
  106. // is destroyed.
  107. //
  108. // The given descriptor must outlive the returned message, and hence must
  109. // outlive the DynamicMessageFactory.
  110. //
  111. // The method is thread-safe.
  112. const Message* GetPrototype(const Descriptor* type) override;
  113. private:
  114. const DescriptorPool* pool_;
  115. bool delegate_to_generated_factory_;
  116. // This struct just contains a hash_map. We can't #include <hash_map> from
  117. // this header due to hacks needed for hash_map portability in the open source
  118. // release. Namely, stubs/hash.h, which defines hash_map portably, is not a
  119. // public header (for good reason), but dynamic_message.h is, and public
  120. // headers may only #include other public headers.
  121. struct PrototypeMap;
  122. std::unique_ptr<PrototypeMap> prototypes_;
  123. mutable internal::WrappedMutex prototypes_mutex_;
  124. friend class DynamicMessage;
  125. const Message* GetPrototypeNoLock(const Descriptor* type);
  126. // Construct default oneof instance for reflection usage if oneof
  127. // is defined.
  128. static void ConstructDefaultOneofInstance(const Descriptor* type,
  129. const uint32 offsets[],
  130. void* default_oneof_instance);
  131. // Delete default oneof instance. Called by ~DynamicMessageFactory.
  132. static void DeleteDefaultOneofInstance(const Descriptor* type,
  133. const uint32 offsets[],
  134. const void* default_oneof_instance);
  135. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessageFactory);
  136. };
  137. // Helper for computing a sorted list of map entries via reflection.
  138. class PROTOBUF_EXPORT DynamicMapSorter {
  139. public:
  140. static std::vector<const Message*> Sort(const Message& message, int map_size,
  141. const Reflection* reflection,
  142. const FieldDescriptor* field) {
  143. std::vector<const Message*> result;
  144. result.reserve(map_size);
  145. RepeatedFieldRef<Message> map_field =
  146. reflection->GetRepeatedFieldRef<Message>(message, field);
  147. for (auto it = map_field.begin(); it != map_field.end(); ++it) {
  148. result.push_back(&*it);
  149. }
  150. MapEntryMessageComparator comparator(field->message_type());
  151. std::stable_sort(result.begin(), result.end(), comparator);
  152. // Complain if the keys aren't in ascending order.
  153. #ifndef NDEBUG
  154. for (size_t j = 1; j < static_cast<size_t>(map_size); j++) {
  155. if (!comparator(result[j - 1], result[j])) {
  156. GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1])
  157. ? "internal error in map key sorting"
  158. : "map keys are not unique");
  159. }
  160. }
  161. #endif
  162. return result;
  163. }
  164. private:
  165. class PROTOBUF_EXPORT MapEntryMessageComparator {
  166. public:
  167. explicit MapEntryMessageComparator(const Descriptor* descriptor)
  168. : field_(descriptor->field(0)) {}
  169. bool operator()(const Message* a, const Message* b) {
  170. const Reflection* reflection = a->GetReflection();
  171. switch (field_->cpp_type()) {
  172. case FieldDescriptor::CPPTYPE_BOOL: {
  173. bool first = reflection->GetBool(*a, field_);
  174. bool second = reflection->GetBool(*b, field_);
  175. return first < second;
  176. }
  177. case FieldDescriptor::CPPTYPE_INT32: {
  178. int32 first = reflection->GetInt32(*a, field_);
  179. int32 second = reflection->GetInt32(*b, field_);
  180. return first < second;
  181. }
  182. case FieldDescriptor::CPPTYPE_INT64: {
  183. int64 first = reflection->GetInt64(*a, field_);
  184. int64 second = reflection->GetInt64(*b, field_);
  185. return first < second;
  186. }
  187. case FieldDescriptor::CPPTYPE_UINT32: {
  188. uint32 first = reflection->GetUInt32(*a, field_);
  189. uint32 second = reflection->GetUInt32(*b, field_);
  190. return first < second;
  191. }
  192. case FieldDescriptor::CPPTYPE_UINT64: {
  193. uint64 first = reflection->GetUInt64(*a, field_);
  194. uint64 second = reflection->GetUInt64(*b, field_);
  195. return first < second;
  196. }
  197. case FieldDescriptor::CPPTYPE_STRING: {
  198. std::string first = reflection->GetString(*a, field_);
  199. std::string second = reflection->GetString(*b, field_);
  200. return first < second;
  201. }
  202. default:
  203. GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
  204. return true;
  205. }
  206. }
  207. private:
  208. const FieldDescriptor* field_;
  209. };
  210. };
  211. } // namespace protobuf
  212. } // namespace google
  213. #include <google/protobuf/port_undef.inc>
  214. #endif // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__