dynamic_message.h
Go to the documentation of this file.
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 <algorithm>
42 #include <memory>
43 #include <vector>
44 
49 
50 #ifdef SWIG
51 #error "You cannot SWIG proto headers"
52 #endif
53 
54 #include <google/protobuf/port_def.inc>
55 
56 namespace google {
57 namespace protobuf {
58 
59 // Defined in other files.
60 class Descriptor; // descriptor.h
61 class DescriptorPool; // descriptor.h
62 
63 // Constructs implementations of Message which can emulate types which are not
64 // known at compile-time.
65 //
66 // Sometimes you want to be able to manipulate protocol types that you don't
67 // know about at compile time. It would be nice to be able to construct
68 // a Message object which implements the message type given by any arbitrary
69 // Descriptor. DynamicMessage provides this.
70 //
71 // As it turns out, a DynamicMessage needs to construct extra
72 // information about its type in order to operate. Most of this information
73 // can be shared between all DynamicMessages of the same type. But, caching
74 // this information in some sort of global map would be a bad idea, since
75 // the cached information for a particular descriptor could outlive the
76 // descriptor itself. To avoid this problem, DynamicMessageFactory
77 // encapsulates this "cache". All DynamicMessages of the same type created
78 // from the same factory will share the same support data. Any Descriptors
79 // used with a particular factory must outlive the factory.
80 class PROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
81  public:
82  // Construct a DynamicMessageFactory that will search for extensions in
83  // the DescriptorPool in which the extendee is defined.
85 
86  // Construct a DynamicMessageFactory that will search for extensions in
87  // the given DescriptorPool.
88  //
89  // DEPRECATED: Use CodedInputStream::SetExtensionRegistry() to tell the
90  // parser to look for extensions in an alternate pool. However, note that
91  // this is almost never what you want to do. Almost all users should use
92  // the zero-arg constructor.
94 
96 
97  // Call this to tell the DynamicMessageFactory that if it is given a
98  // Descriptor d for which:
99  // d->file()->pool() == DescriptorPool::generated_pool(),
100  // then it should delegate to MessageFactory::generated_factory() instead
101  // of constructing a dynamic implementation of the message. In theory there
102  // is no down side to doing this, so it may become the default in the future.
103  void SetDelegateToGeneratedFactory(bool enable) {
104  delegate_to_generated_factory_ = enable;
105  }
106 
107  // implements MessageFactory ---------------------------------------
108 
109  // Given a Descriptor, constructs the default (prototype) Message of that
110  // type. You can then call that message's New() method to construct a
111  // mutable message of that type.
112  //
113  // Calling this method twice with the same Descriptor returns the same
114  // object. The returned object remains property of the factory and will
115  // be destroyed when the factory is destroyed. Also, any objects created
116  // by calling the prototype's New() method share some data with the
117  // prototype, so these must be destroyed before the DynamicMessageFactory
118  // is destroyed.
119  //
120  // The given descriptor must outlive the returned message, and hence must
121  // outlive the DynamicMessageFactory.
122  //
123  // The method is thread-safe.
124  const Message* GetPrototype(const Descriptor* type) override;
125 
126  private:
129 
130  // This struct just contains a hash_map. We can't #include <hash_map> from
131  // this header due to hacks needed for hash_map portability in the open source
132  // release. Namely, stubs/hash.h, which defines hash_map portably, is not a
133  // public header (for good reason), but dynamic_message.h is, and public
134  // headers may only #include other public headers.
135  struct PrototypeMap;
136  std::unique_ptr<PrototypeMap> prototypes_;
137  mutable internal::WrappedMutex prototypes_mutex_;
138 
139  friend class DynamicMessage;
140  const Message* GetPrototypeNoLock(const Descriptor* type);
141 
142  // Construct default oneof instance for reflection usage if oneof
143  // is defined.
144  static void ConstructDefaultOneofInstance(const Descriptor* type,
145  const uint32 offsets[],
146  void* default_oneof_instance);
147  // Delete default oneof instance. Called by ~DynamicMessageFactory.
148  static void DeleteDefaultOneofInstance(const Descriptor* type,
149  const uint32 offsets[],
150  const void* default_oneof_instance);
151 
153 };
154 
155 // Helper for computing a sorted list of map entries via reflection.
156 class PROTOBUF_EXPORT DynamicMapSorter {
157  public:
158  static std::vector<const Message*> Sort(const Message& message, int map_size,
159  const Reflection* reflection,
160  const FieldDescriptor* field) {
161  std::vector<const Message*> result(static_cast<size_t>(map_size));
162  const RepeatedPtrField<Message>& map_field =
163  reflection->GetRepeatedPtrField<Message>(message, field);
164  size_t i = 0;
166  map_field.pointer_begin();
167  it != map_field.pointer_end();) {
168  result[i++] = *it++;
169  }
170  GOOGLE_DCHECK_EQ(result.size(), i);
171  MapEntryMessageComparator comparator(field->message_type());
172  std::stable_sort(result.begin(), result.end(), comparator);
173  // Complain if the keys aren't in ascending order.
174 #ifndef NDEBUG
175  for (size_t j = 1; j < static_cast<size_t>(map_size); j++) {
176  if (!comparator(result[j - 1], result[j])) {
177  GOOGLE_LOG(ERROR) << (comparator(result[j], result[j - 1])
178  ? "internal error in map key sorting"
179  : "map keys are not unique");
180  }
181  }
182 #endif
183  return result;
184  }
185 
186  private:
187  class PROTOBUF_EXPORT MapEntryMessageComparator {
188  public:
190  : field_(descriptor->field(0)) {}
191 
192  bool operator()(const Message* a, const Message* b) {
193  const Reflection* reflection = a->GetReflection();
194  switch (field_->cpp_type()) {
196  bool first = reflection->GetBool(*a, field_);
197  bool second = reflection->GetBool(*b, field_);
198  return first < second;
199  }
201  int32 first = reflection->GetInt32(*a, field_);
202  int32 second = reflection->GetInt32(*b, field_);
203  return first < second;
204  }
206  int64 first = reflection->GetInt64(*a, field_);
207  int64 second = reflection->GetInt64(*b, field_);
208  return first < second;
209  }
211  uint32 first = reflection->GetUInt32(*a, field_);
212  uint32 second = reflection->GetUInt32(*b, field_);
213  return first < second;
214  }
216  uint64 first = reflection->GetUInt64(*a, field_);
217  uint64 second = reflection->GetUInt64(*b, field_);
218  return first < second;
219  }
221  std::string first = reflection->GetString(*a, field_);
222  std::string second = reflection->GetString(*b, field_);
223  return first < second;
224  }
225  default:
226  GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
227  return true;
228  }
229  }
230 
231  private:
233  };
234 };
235 
236 } // namespace protobuf
237 } // namespace google
238 
239 #include <google/protobuf/port_undef.inc>
240 
241 #endif // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
google::protobuf::Reflection::GetString
std::string GetString(const Message &message, const FieldDescriptor *field) const
Definition: generated_message_reflection.cc:1151
google::protobuf::DynamicMessageFactory::prototypes_mutex_
internal::WrappedMutex prototypes_mutex_
Definition: dynamic_message.h:137
google::protobuf::RepeatedPtrField
Definition: command_line_interface.h:62
google::protobuf::FieldDescriptor::CPPTYPE_STRING
@ CPPTYPE_STRING
Definition: src/google/protobuf/descriptor.h:562
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::DynamicMessageFactory::delegate_to_generated_factory_
bool delegate_to_generated_factory_
Definition: dynamic_message.h:128
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::DynamicMessageFactory::PrototypeMap
Definition: dynamic_message.cc:628
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::FieldDescriptor::CPPTYPE_UINT64
@ CPPTYPE_UINT64
Definition: src/google/protobuf/descriptor.h:557
google::protobuf::Reflection::GetUInt32
uint32 GetUInt32(const Message &message, const FieldDescriptor *field) const
google::protobuf::DynamicMapSorter::MapEntryMessageComparator::field_
const FieldDescriptor * field_
Definition: dynamic_message.h:232
google::protobuf::FieldDescriptor::CPPTYPE_INT64
@ CPPTYPE_INT64
Definition: src/google/protobuf/descriptor.h:555
google::protobuf::FieldDescriptor::CPPTYPE_UINT32
@ CPPTYPE_UINT32
Definition: src/google/protobuf/descriptor.h:556
google::protobuf::DynamicMapSorter::Sort
static std::vector< const Message * > Sort(const Message &message, int map_size, const Reflection *reflection, const FieldDescriptor *field)
Definition: dynamic_message.h:158
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::Reflection
Definition: src/google/protobuf/message.h:400
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::Reflection::GetBool
bool GetBool(const Message &message, const FieldDescriptor *field) const
google::protobuf::MessageFactory
Definition: src/google/protobuf/message.h:1069
Descriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:113
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::DynamicMapSorter::MapEntryMessageComparator
Definition: dynamic_message.h:187
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
message.h
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::RepeatedPtrField::pointer_begin
pointer_iterator pointer_begin()
Definition: repeated_field.h:2475
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
repeated_field.h
google::protobuf::Reflection::GetRepeatedPtrField
const RepeatedPtrField< T > & GetRepeatedPtrField(const Message &, const FieldDescriptor *) const
google::protobuf::DynamicMessage
Definition: dynamic_message.cc:232
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::ERROR
static const LogLevel ERROR
Definition: protobuf/src/google/protobuf/testing/googletest.h:70
google::protobuf::Reflection::GetInt32
int32 GetInt32(const Message &message, const FieldDescriptor *field) const
google::protobuf::DynamicMapSorter::MapEntryMessageComparator::MapEntryMessageComparator
MapEntryMessageComparator(const Descriptor *descriptor)
Definition: dynamic_message.h:189
pool
InternalDescriptorPool * pool
Definition: php/ext/google/protobuf/protobuf.h:798
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::DynamicMessageFactory::SetDelegateToGeneratedFactory
void SetDelegateToGeneratedFactory(bool enable)
Definition: dynamic_message.h:103
google::protobuf::DynamicMessageFactory
Definition: dynamic_message.h:80
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::FieldDescriptor::CPPTYPE_BOOL
@ CPPTYPE_BOOL
Definition: src/google/protobuf/descriptor.h:560
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
common.h
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::Reflection::GetInt64
int64 GetInt64(const Message &message, const FieldDescriptor *field) const
first
GLint first
Definition: glcorearb.h:2830
mutex.h
google::protobuf::DynamicMessageFactory::prototypes_
std::unique_ptr< PrototypeMap > prototypes_
Definition: dynamic_message.h:135
google::protobuf::FieldDescriptor::CPPTYPE_INT32
@ CPPTYPE_INT32
Definition: src/google/protobuf/descriptor.h:554
DescriptorPool
Definition: ruby/ext/google/protobuf_c/protobuf.h:109
google::protobuf::DynamicMapSorter::MapEntryMessageComparator::operator()
bool operator()(const Message *a, const Message *b)
Definition: dynamic_message.h:192
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf::Reflection::GetUInt64
uint64 GetUInt64(const Message &message, const FieldDescriptor *field) const
google::protobuf::DynamicMapSorter
Definition: dynamic_message.h:156
google::protobuf.internal::RepeatedPtrOverPtrsIterator
Definition: repeated_field.h:371
google::protobuf::DynamicMessageFactory::pool_
const DescriptorPool * pool_
Definition: dynamic_message.h:127
google::protobuf::RepeatedPtrField::pointer_end
pointer_iterator pointer_end()
Definition: repeated_field.h:2485
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: logging.h:196


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:50