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