dynamic_message.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <memory>
  39. #include <google/protobuf/message.h>
  40. #include <google/protobuf/stubs/common.h>
  41. namespace google {
  42. namespace protobuf {
  43. // Defined in other files.
  44. class Descriptor; // descriptor.h
  45. class DescriptorPool; // descriptor.h
  46. // Constructs implementations of Message which can emulate types which are not
  47. // known at compile-time.
  48. //
  49. // Sometimes you want to be able to manipulate protocol types that you don't
  50. // know about at compile time. It would be nice to be able to construct
  51. // a Message object which implements the message type given by any arbitrary
  52. // Descriptor. DynamicMessage provides this.
  53. //
  54. // As it turns out, a DynamicMessage needs to construct extra
  55. // information about its type in order to operate. Most of this information
  56. // can be shared between all DynamicMessages of the same type. But, caching
  57. // this information in some sort of global map would be a bad idea, since
  58. // the cached information for a particular descriptor could outlive the
  59. // descriptor itself. To avoid this problem, DynamicMessageFactory
  60. // encapsulates this "cache". All DynamicMessages of the same type created
  61. // from the same factory will share the same support data. Any Descriptors
  62. // used with a particular factory must outlive the factory.
  63. class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
  64. public:
  65. // Construct a DynamicMessageFactory that will search for extensions in
  66. // the DescriptorPool in which the extendee is defined.
  67. DynamicMessageFactory();
  68. // Construct a DynamicMessageFactory that will search for extensions in
  69. // the given DescriptorPool.
  70. //
  71. // DEPRECATED: Use CodedInputStream::SetExtensionRegistry() to tell the
  72. // parser to look for extensions in an alternate pool. However, note that
  73. // this is almost never what you want to do. Almost all users should use
  74. // the zero-arg constructor.
  75. DynamicMessageFactory(const DescriptorPool* pool);
  76. ~DynamicMessageFactory();
  77. // Call this to tell the DynamicMessageFactory that if it is given a
  78. // Descriptor d for which:
  79. // d->file()->pool() == DescriptorPool::generated_pool(),
  80. // then it should delegate to MessageFactory::generated_factory() instead
  81. // of constructing a dynamic implementation of the message. In theory there
  82. // is no down side to doing this, so it may become the default in the future.
  83. void SetDelegateToGeneratedFactory(bool enable) {
  84. delegate_to_generated_factory_ = enable;
  85. }
  86. // implements MessageFactory ---------------------------------------
  87. // Given a Descriptor, constructs the default (prototype) Message of that
  88. // type. You can then call that message's New() method to construct a
  89. // mutable message of that type.
  90. //
  91. // Calling this method twice with the same Descriptor returns the same
  92. // object. The returned object remains property of the factory and will
  93. // be destroyed when the factory is destroyed. Also, any objects created
  94. // by calling the prototype's New() method share some data with the
  95. // prototype, so these must be destroyed before the DynamicMessageFactory
  96. // is destroyed.
  97. //
  98. // The given descriptor must outlive the returned message, and hence must
  99. // outlive the DynamicMessageFactory.
  100. //
  101. // The method is thread-safe.
  102. const Message* GetPrototype(const Descriptor* type);
  103. private:
  104. const DescriptorPool* pool_;
  105. bool delegate_to_generated_factory_;
  106. // This struct just contains a hash_map. We can't #include <google/protobuf/stubs/hash.h> from
  107. // this header due to hacks needed for hash_map portability in the open source
  108. // release. Namely, stubs/hash.h, which defines hash_map portably, is not a
  109. // public header (for good reason), but dynamic_message.h is, and public
  110. // headers may only #include other public headers.
  111. struct PrototypeMap;
  112. scoped_ptr<PrototypeMap> prototypes_;
  113. mutable Mutex prototypes_mutex_;
  114. friend class DynamicMessage;
  115. const Message* GetPrototypeNoLock(const Descriptor* type);
  116. // Construct default oneof instance for reflection usage if oneof
  117. // is defined.
  118. static void ConstructDefaultOneofInstance(const Descriptor* type,
  119. const int offsets[],
  120. void* default_oneof_instance);
  121. // Delete default oneof instance. Called by ~DynamicMessageFactory.
  122. static void DeleteDefaultOneofInstance(const Descriptor* type,
  123. const int offsets[],
  124. void* default_oneof_instance);
  125. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessageFactory);
  126. };
  127. } // namespace protobuf
  128. } // namespace google
  129. #endif // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__