You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic_message.h 11 kB

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