dynamic_message.h revision d0332953cda33fb4f8e24ebff9c49159b69c43d6
1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Protocol Buffers - Google's data interchange format
2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2008 Google Inc.  All rights reserved.
3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// http://code.google.com/p/protobuf/
4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Redistribution and use in source and binary forms, with or without
6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// modification, are permitted provided that the following conditions are
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// met:
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//     * Redistributions of source code must retain the above copyright
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// notice, this list of conditions and the following disclaimer.
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//     * Redistributions in binary form must reproduce the above
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// in the documentation and/or other materials provided with the
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// distribution.
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// contributors may be used to endorse or promote products derived from
17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// this software without specific prior written permission.
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// (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 <google/protobuf/message.h>
42#include <google/protobuf/stubs/common.h>
43
44namespace google {
45namespace protobuf {
46
47// Defined in other files.
48class Descriptor;        // descriptor.h
49class DescriptorPool;    // descriptor.h
50
51// Constructs implementations of Message which can emulate types which are not
52// known at compile-time.
53//
54// Sometimes you want to be able to manipulate protocol types that you don't
55// know about at compile time.  It would be nice to be able to construct
56// a Message object which implements the message type given by any arbitrary
57// Descriptor.  DynamicMessage provides this.
58//
59// As it turns out, a DynamicMessage needs to construct extra
60// information about its type in order to operate.  Most of this information
61// can be shared between all DynamicMessages of the same type.  But, caching
62// this information in some sort of global map would be a bad idea, since
63// the cached information for a particular descriptor could outlive the
64// descriptor itself.  To avoid this problem, DynamicMessageFactory
65// encapsulates this "cache".  All DynamicMessages of the same type created
66// from the same factory will share the same support data.  Any Descriptors
67// used with a particular factory must outlive the factory.
68class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
69 public:
70  // Construct a DynamicMessageFactory that will search for extensions in
71  // the DescriptorPool in which the exendee is defined.
72  DynamicMessageFactory();
73
74  // Construct a DynamicMessageFactory that will search for extensions in
75  // the given DescriptorPool.
76  //
77  // DEPRECATED:  Use CodedInputStream::SetExtensionRegistry() to tell the
78  //   parser to look for extensions in an alternate pool.  However, note that
79  //   this is almost never what you want to do.  Almost all users should use
80  //   the zero-arg constructor.
81  DynamicMessageFactory(const DescriptorPool* pool);
82
83  ~DynamicMessageFactory();
84
85  // Call this to tell the DynamicMessageFactory that if it is given a
86  // Descriptor d for which:
87  //   d->file()->pool() == DescriptorPool::generated_pool(),
88  // then it should delegate to MessageFactory::generated_factory() instead
89  // of constructing a dynamic implementation of the message.  In theory there
90  // is no down side to doing this, so it may become the default in the future.
91  void SetDelegateToGeneratedFactory(bool enable) {
92    delegate_to_generated_factory_ = enable;
93  }
94
95  // implements MessageFactory ---------------------------------------
96
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 destoyed 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);
113
114 private:
115  const DescriptorPool* pool_;
116  bool delegate_to_generated_factory_;
117
118  // This struct just contains a hash_map.  We can't #include <google/protobuf/stubs/hash.h> from
119  // this header due to hacks needed for hash_map portability in the open source
120  // release.  Namely, stubs/hash.h, which defines hash_map portably, is not a
121  // public header (for good reason), but dynamic_message.h is, and public
122  // headers may only #include other public headers.
123  struct PrototypeMap;
124  scoped_ptr<PrototypeMap> prototypes_;
125  mutable Mutex prototypes_mutex_;
126
127  friend class DynamicMessage;
128  const Message* GetPrototypeNoLock(const Descriptor* type);
129
130  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessageFactory);
131};
132
133}  // namespace protobuf
134
135}  // namespace google
136#endif  // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
137