Line data Source code
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 Message, the abstract interface implemented by non-lite
36 : // protocol message objects. Although it's possible to implement this
37 : // interface manually, most users will use the protocol compiler to
38 : // generate implementations.
39 : //
40 : // Example usage:
41 : //
42 : // Say you have a message defined as:
43 : //
44 : // message Foo {
45 : // optional string text = 1;
46 : // repeated int32 numbers = 2;
47 : // }
48 : //
49 : // Then, if you used the protocol compiler to generate a class from the above
50 : // definition, you could use it like so:
51 : //
52 : // string data; // Will store a serialized version of the message.
53 : //
54 : // {
55 : // // Create a message and serialize it.
56 : // Foo foo;
57 : // foo.set_text("Hello World!");
58 : // foo.add_numbers(1);
59 : // foo.add_numbers(5);
60 : // foo.add_numbers(42);
61 : //
62 : // foo.SerializeToString(&data);
63 : // }
64 : //
65 : // {
66 : // // Parse the serialized message and check that it contains the
67 : // // correct data.
68 : // Foo foo;
69 : // foo.ParseFromString(data);
70 : //
71 : // assert(foo.text() == "Hello World!");
72 : // assert(foo.numbers_size() == 3);
73 : // assert(foo.numbers(0) == 1);
74 : // assert(foo.numbers(1) == 5);
75 : // assert(foo.numbers(2) == 42);
76 : // }
77 : //
78 : // {
79 : // // Same as the last block, but do it dynamically via the Message
80 : // // reflection interface.
81 : // Message* foo = new Foo;
82 : // const Descriptor* descriptor = foo->GetDescriptor();
83 : //
84 : // // Get the descriptors for the fields we're interested in and verify
85 : // // their types.
86 : // const FieldDescriptor* text_field = descriptor->FindFieldByName("text");
87 : // assert(text_field != NULL);
88 : // assert(text_field->type() == FieldDescriptor::TYPE_STRING);
89 : // assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL);
90 : // const FieldDescriptor* numbers_field = descriptor->
91 : // FindFieldByName("numbers");
92 : // assert(numbers_field != NULL);
93 : // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
94 : // assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED);
95 : //
96 : // // Parse the message.
97 : // foo->ParseFromString(data);
98 : //
99 : // // Use the reflection interface to examine the contents.
100 : // const Reflection* reflection = foo->GetReflection();
101 : // assert(reflection->GetString(*foo, text_field) == "Hello World!");
102 : // assert(reflection->FieldSize(*foo, numbers_field) == 3);
103 : // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 0) == 1);
104 : // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 1) == 5);
105 : // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 2) == 42);
106 : //
107 : // delete foo;
108 : // }
109 :
110 : #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
111 : #define GOOGLE_PROTOBUF_MESSAGE_H__
112 :
113 : #include <iosfwd>
114 : #include <string>
115 : #include <type_traits>
116 : #include <vector>
117 :
118 : #include <google/protobuf/arena.h>
119 : #include <google/protobuf/message_lite.h>
120 :
121 : #include <google/protobuf/stubs/common.h>
122 : #include <google/protobuf/descriptor.h>
123 :
124 :
125 : #define GOOGLE_PROTOBUF_HAS_ONEOF
126 : #define GOOGLE_PROTOBUF_HAS_ARENAS
127 :
128 : namespace google {
129 : namespace protobuf {
130 :
131 : // Defined in this file.
132 : class Message;
133 : class Reflection;
134 : class MessageFactory;
135 :
136 : // Defined in other files.
137 : class MapKey;
138 : class MapValueRef;
139 : class MapIterator;
140 : class MapReflectionTester;
141 :
142 : namespace internal {
143 : class MapFieldBase;
144 : }
145 : class UnknownFieldSet; // unknown_field_set.h
146 : namespace io {
147 : class ZeroCopyInputStream; // zero_copy_stream.h
148 : class ZeroCopyOutputStream; // zero_copy_stream.h
149 : class CodedInputStream; // coded_stream.h
150 : class CodedOutputStream; // coded_stream.h
151 : }
152 : namespace python {
153 : class MapReflectionFriend; // scalar_map_container.h
154 : }
155 : namespace expr {
156 : class CelMapReflectionFriend; // field_backed_map_impl.cc
157 : }
158 :
159 :
160 : namespace internal {
161 : class ReflectionOps; // reflection_ops.h
162 : class MapKeySorter; // wire_format.cc
163 : class WireFormat; // wire_format.h
164 : class MapFieldReflectionTest; // map_test.cc
165 : }
166 :
167 : template<typename T>
168 : class RepeatedField; // repeated_field.h
169 :
170 : template<typename T>
171 : class RepeatedPtrField; // repeated_field.h
172 :
173 : // A container to hold message metadata.
174 : struct Metadata {
175 : const Descriptor* descriptor;
176 : const Reflection* reflection;
177 : };
178 :
179 : // Abstract interface for protocol messages.
180 : //
181 : // See also MessageLite, which contains most every-day operations. Message
182 : // adds descriptors and reflection on top of that.
183 : //
184 : // The methods of this class that are virtual but not pure-virtual have
185 : // default implementations based on reflection. Message classes which are
186 : // optimized for speed will want to override these with faster implementations,
187 : // but classes optimized for code size may be happy with keeping them. See
188 : // the optimize_for option in descriptor.proto.
189 : class LIBPROTOBUF_EXPORT Message : public MessageLite {
190 : public:
191 420 : inline Message() {}
192 0 : virtual ~Message() {}
193 :
194 : // Basic Operations ------------------------------------------------
195 :
196 : // Construct a new instance of the same type. Ownership is passed to the
197 : // caller. (This is also defined in MessageLite, but is defined again here
198 : // for return-type covariance.)
199 : virtual Message* New() const = 0;
200 :
201 : // Construct a new instance on the arena. Ownership is passed to the caller
202 : // if arena is a NULL. Default implementation allows for API compatibility
203 : // during the Arena transition.
204 : virtual Message* New(::google::protobuf::Arena* arena) const {
205 : Message* message = New();
206 : if (arena != NULL) {
207 : arena->Own(message);
208 : }
209 : return message;
210 : }
211 :
212 : // Make this message into a copy of the given message. The given message
213 : // must have the same descriptor, but need not necessarily be the same class.
214 : // By default this is just implemented as "Clear(); MergeFrom(from);".
215 : virtual void CopyFrom(const Message& from);
216 :
217 : // Merge the fields from the given message into this message. Singular
218 : // fields will be overwritten, if specified in from, except for embedded
219 : // messages which will be merged. Repeated fields will be concatenated.
220 : // The given message must be of the same type as this message (i.e. the
221 : // exact same class).
222 : virtual void MergeFrom(const Message& from);
223 :
224 : // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
225 : // a nice error message.
226 : void CheckInitialized() const;
227 :
228 : // Slowly build a list of all required fields that are not set.
229 : // This is much, much slower than IsInitialized() as it is implemented
230 : // purely via reflection. Generally, you should not call this unless you
231 : // have already determined that an error exists by calling IsInitialized().
232 : void FindInitializationErrors(std::vector<string>* errors) const;
233 :
234 : // Like FindInitializationErrors, but joins all the strings, delimited by
235 : // commas, and returns them.
236 : string InitializationErrorString() const;
237 :
238 : // Clears all unknown fields from this message and all embedded messages.
239 : // Normally, if unknown tag numbers are encountered when parsing a message,
240 : // the tag and value are stored in the message's UnknownFieldSet and
241 : // then written back out when the message is serialized. This allows servers
242 : // which simply route messages to other servers to pass through messages
243 : // that have new field definitions which they don't yet know about. However,
244 : // this behavior can have security implications. To avoid it, call this
245 : // method after parsing.
246 : //
247 : // See Reflection::GetUnknownFields() for more on unknown fields.
248 : virtual void DiscardUnknownFields();
249 :
250 : // Computes (an estimate of) the total number of bytes currently used for
251 : // storing the message in memory. The default implementation calls the
252 : // Reflection object's SpaceUsed() method.
253 : //
254 : // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented
255 : // using reflection (rather than the generated code implementation for
256 : // ByteSize()). Like ByteSize(), its CPU time is linear in the number of
257 : // fields defined for the proto.
258 : virtual size_t SpaceUsedLong() const;
259 :
260 : PROTOBUF_RUNTIME_DEPRECATED("Please use SpaceUsedLong() instead")
261 : int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); }
262 :
263 : // Debugging & Testing----------------------------------------------
264 :
265 : // Generates a human readable form of this message, useful for debugging
266 : // and other purposes.
267 : string DebugString() const;
268 : // Like DebugString(), but with less whitespace.
269 : string ShortDebugString() const;
270 : // Like DebugString(), but do not escape UTF-8 byte sequences.
271 : string Utf8DebugString() const;
272 : // Convenience function useful in GDB. Prints DebugString() to stdout.
273 : void PrintDebugString() const;
274 :
275 : // Heavy I/O -------------------------------------------------------
276 : // Additional parsing and serialization methods not implemented by
277 : // MessageLite because they are not supported by the lite library.
278 :
279 : // Parse a protocol buffer from a file descriptor. If successful, the entire
280 : // input will be consumed.
281 : bool ParseFromFileDescriptor(int file_descriptor);
282 : // Like ParseFromFileDescriptor(), but accepts messages that are missing
283 : // required fields.
284 : bool ParsePartialFromFileDescriptor(int file_descriptor);
285 : // Parse a protocol buffer from a C++ istream. If successful, the entire
286 : // input will be consumed.
287 : bool ParseFromIstream(std::istream* input);
288 : // Like ParseFromIstream(), but accepts messages that are missing
289 : // required fields.
290 : bool ParsePartialFromIstream(std::istream* input);
291 :
292 : // Serialize the message and write it to the given file descriptor. All
293 : // required fields must be set.
294 : bool SerializeToFileDescriptor(int file_descriptor) const;
295 : // Like SerializeToFileDescriptor(), but allows missing required fields.
296 : bool SerializePartialToFileDescriptor(int file_descriptor) const;
297 : // Serialize the message and write it to the given C++ ostream. All
298 : // required fields must be set.
299 : bool SerializeToOstream(std::ostream* output) const;
300 : // Like SerializeToOstream(), but allows missing required fields.
301 : bool SerializePartialToOstream(std::ostream* output) const;
302 :
303 :
304 : // Reflection-based methods ----------------------------------------
305 : // These methods are pure-virtual in MessageLite, but Message provides
306 : // reflection-based default implementations.
307 :
308 : virtual string GetTypeName() const;
309 : virtual void Clear();
310 : virtual bool IsInitialized() const;
311 : virtual void CheckTypeAndMergeFrom(const MessageLite& other);
312 : virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
313 : virtual size_t ByteSizeLong() const;
314 : virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
315 :
316 : private:
317 : // This is called only by the default implementation of ByteSize(), to
318 : // update the cached size. If you override ByteSize(), you do not need
319 : // to override this. If you do not override ByteSize(), you MUST override
320 : // this; the default implementation will crash.
321 : //
322 : // The method is private because subclasses should never call it; only
323 : // override it. Yes, C++ lets you do that. Crazy, huh?
324 : virtual void SetCachedSize(int size) const;
325 :
326 : public:
327 :
328 : // Introspection ---------------------------------------------------
329 :
330 : // Typedef for backwards-compatibility.
331 : typedef google::protobuf::Reflection Reflection;
332 :
333 : // Get a non-owning pointer to a Descriptor for this message's type. This
334 : // describes what fields the message contains, the types of those fields, etc.
335 : // This object remains property of the Message.
336 : const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
337 :
338 : // Get a non-owning pointer to the Reflection interface for this Message,
339 : // which can be used to read and modify the fields of the Message dynamically
340 : // (in other words, without knowing the message type at compile time). This
341 : // object remains property of the Message.
342 : //
343 : // This method remains virtual in case a subclass does not implement
344 : // reflection and wants to override the default behavior.
345 0 : virtual const Reflection* GetReflection() const final {
346 0 : return GetMetadata().reflection;
347 : }
348 :
349 : protected:
350 : // Get a struct containing the metadata for the Message. Most subclasses only
351 : // need to implement this method, rather than the GetDescriptor() and
352 : // GetReflection() wrappers.
353 : virtual Metadata GetMetadata() const = 0;
354 :
355 :
356 : private:
357 : GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
358 : };
359 :
360 : namespace internal {
361 : // Forward-declare interfaces used to implement RepeatedFieldRef.
362 : // These are protobuf internals that users shouldn't care about.
363 : class RepeatedFieldAccessor;
364 : } // namespace internal
365 :
366 : // Forward-declare RepeatedFieldRef templates. The second type parameter is
367 : // used for SFINAE tricks. Users should ignore it.
368 : template<typename T, typename Enable = void>
369 : class RepeatedFieldRef;
370 :
371 : template<typename T, typename Enable = void>
372 : class MutableRepeatedFieldRef;
373 :
374 : // This interface contains methods that can be used to dynamically access
375 : // and modify the fields of a protocol message. Their semantics are
376 : // similar to the accessors the protocol compiler generates.
377 : //
378 : // To get the Reflection for a given Message, call Message::GetReflection().
379 : //
380 : // This interface is separate from Message only for efficiency reasons;
381 : // the vast majority of implementations of Message will share the same
382 : // implementation of Reflection (GeneratedMessageReflection,
383 : // defined in generated_message.h), and all Messages of a particular class
384 : // should share the same Reflection object (though you should not rely on
385 : // the latter fact).
386 : //
387 : // There are several ways that these methods can be used incorrectly. For
388 : // example, any of the following conditions will lead to undefined
389 : // results (probably assertion failures):
390 : // - The FieldDescriptor is not a field of this message type.
391 : // - The method called is not appropriate for the field's type. For
392 : // each field type in FieldDescriptor::TYPE_*, there is only one
393 : // Get*() method, one Set*() method, and one Add*() method that is
394 : // valid for that type. It should be obvious which (except maybe
395 : // for TYPE_BYTES, which are represented using strings in C++).
396 : // - A Get*() or Set*() method for singular fields is called on a repeated
397 : // field.
398 : // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
399 : // field.
400 : // - The Message object passed to any method is not of the right type for
401 : // this Reflection object (i.e. message.GetReflection() != reflection).
402 : //
403 : // You might wonder why there is not any abstract representation for a field
404 : // of arbitrary type. E.g., why isn't there just a "GetField()" method that
405 : // returns "const Field&", where "Field" is some class with accessors like
406 : // "GetInt32Value()". The problem is that someone would have to deal with
407 : // allocating these Field objects. For generated message classes, having to
408 : // allocate space for an additional object to wrap every field would at least
409 : // double the message's memory footprint, probably worse. Allocating the
410 : // objects on-demand, on the other hand, would be expensive and prone to
411 : // memory leaks. So, instead we ended up with this flat interface.
412 : class LIBPROTOBUF_EXPORT Reflection {
413 : public:
414 : inline Reflection() {}
415 : virtual ~Reflection();
416 :
417 : // Get the UnknownFieldSet for the message. This contains fields which
418 : // were seen when the Message was parsed but were not recognized according
419 : // to the Message's definition. For proto3 protos, this method will always
420 : // return an empty UnknownFieldSet.
421 : virtual const UnknownFieldSet& GetUnknownFields(
422 : const Message& message) const = 0;
423 : // Get a mutable pointer to the UnknownFieldSet for the message. This
424 : // contains fields which were seen when the Message was parsed but were not
425 : // recognized according to the Message's definition. For proto3 protos, this
426 : // method will return a valid mutable UnknownFieldSet pointer but modifying
427 : // it won't affect the serialized bytes of the message.
428 : virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0;
429 :
430 : // Estimate the amount of memory used by the message object.
431 : virtual size_t SpaceUsedLong(const Message& message) const = 0;
432 :
433 : PROTOBUF_RUNTIME_DEPRECATED("Please use SpaceUsedLong() instead")
434 : int SpaceUsed(const Message& message) const {
435 : return internal::ToIntSize(SpaceUsedLong(message));
436 : }
437 :
438 : // Check if the given non-repeated field is set.
439 : virtual bool HasField(const Message& message,
440 : const FieldDescriptor* field) const = 0;
441 :
442 : // Get the number of elements of a repeated field.
443 : virtual int FieldSize(const Message& message,
444 : const FieldDescriptor* field) const = 0;
445 :
446 : // Clear the value of a field, so that HasField() returns false or
447 : // FieldSize() returns zero.
448 : virtual void ClearField(Message* message,
449 : const FieldDescriptor* field) const = 0;
450 :
451 : // Check if the oneof is set. Returns true if any field in oneof
452 : // is set, false otherwise.
453 : // TODO(jieluo) - make it pure virtual after updating all
454 : // the subclasses.
455 : virtual bool HasOneof(const Message& /*message*/,
456 : const OneofDescriptor* /*oneof_descriptor*/) const {
457 : return false;
458 : }
459 :
460 : virtual void ClearOneof(Message* /*message*/,
461 : const OneofDescriptor* /*oneof_descriptor*/) const {}
462 :
463 : // Returns the field descriptor if the oneof is set. NULL otherwise.
464 : // TODO(jieluo) - make it pure virtual.
465 : virtual const FieldDescriptor* GetOneofFieldDescriptor(
466 : const Message& /*message*/,
467 : const OneofDescriptor* /*oneof_descriptor*/) const {
468 : return NULL;
469 : }
470 :
471 : // Removes the last element of a repeated field.
472 : // We don't provide a way to remove any element other than the last
473 : // because it invites inefficient use, such as O(n^2) filtering loops
474 : // that should have been O(n). If you want to remove an element other
475 : // than the last, the best way to do it is to re-arrange the elements
476 : // (using Swap()) so that the one you want removed is at the end, then
477 : // call RemoveLast().
478 : virtual void RemoveLast(Message* message,
479 : const FieldDescriptor* field) const = 0;
480 : // Removes the last element of a repeated message field, and returns the
481 : // pointer to the caller. Caller takes ownership of the returned pointer.
482 : virtual Message* ReleaseLast(Message* message,
483 : const FieldDescriptor* field) const = 0;
484 :
485 : // Swap the complete contents of two messages.
486 : virtual void Swap(Message* message1, Message* message2) const = 0;
487 :
488 : // Swap fields listed in fields vector of two messages.
489 : virtual void SwapFields(Message* message1,
490 : Message* message2,
491 : const std::vector<const FieldDescriptor*>& fields)
492 : const = 0;
493 :
494 : // Swap two elements of a repeated field.
495 : virtual void SwapElements(Message* message,
496 : const FieldDescriptor* field,
497 : int index1,
498 : int index2) const = 0;
499 :
500 : // List all fields of the message which are currently set, except for unknown
501 : // fields, but including extension known to the parser (i.e. compiled in).
502 : // Singular fields will only be listed if HasField(field) would return true
503 : // and repeated fields will only be listed if FieldSize(field) would return
504 : // non-zero. Fields (both normal fields and extension fields) will be listed
505 : // ordered by field number.
506 : // Use Reflection::GetUnknownFields() or message.unknown_fields() to also get
507 : // access to fields/extensions unknown to the parser.
508 : virtual void ListFields(
509 : const Message& message,
510 : std::vector<const FieldDescriptor*>* output) const = 0;
511 :
512 : // Singular field getters ------------------------------------------
513 : // These get the value of a non-repeated field. They return the default
514 : // value for fields that aren't set.
515 :
516 : virtual int32 GetInt32 (const Message& message,
517 : const FieldDescriptor* field) const = 0;
518 : virtual int64 GetInt64 (const Message& message,
519 : const FieldDescriptor* field) const = 0;
520 : virtual uint32 GetUInt32(const Message& message,
521 : const FieldDescriptor* field) const = 0;
522 : virtual uint64 GetUInt64(const Message& message,
523 : const FieldDescriptor* field) const = 0;
524 : virtual float GetFloat (const Message& message,
525 : const FieldDescriptor* field) const = 0;
526 : virtual double GetDouble(const Message& message,
527 : const FieldDescriptor* field) const = 0;
528 : virtual bool GetBool (const Message& message,
529 : const FieldDescriptor* field) const = 0;
530 : virtual string GetString(const Message& message,
531 : const FieldDescriptor* field) const = 0;
532 : virtual const EnumValueDescriptor* GetEnum(
533 : const Message& message, const FieldDescriptor* field) const = 0;
534 :
535 : // GetEnumValue() returns an enum field's value as an integer rather than
536 : // an EnumValueDescriptor*. If the integer value does not correspond to a
537 : // known value descriptor, a new value descriptor is created. (Such a value
538 : // will only be present when the new unknown-enum-value semantics are enabled
539 : // for a message.)
540 : virtual int GetEnumValue(
541 : const Message& message, const FieldDescriptor* field) const = 0;
542 :
543 : // See MutableMessage() for the meaning of the "factory" parameter.
544 : virtual const Message& GetMessage(const Message& message,
545 : const FieldDescriptor* field,
546 : MessageFactory* factory = NULL) const = 0;
547 :
548 : // Get a string value without copying, if possible.
549 : //
550 : // GetString() necessarily returns a copy of the string. This can be
551 : // inefficient when the string is already stored in a string object in the
552 : // underlying message. GetStringReference() will return a reference to the
553 : // underlying string in this case. Otherwise, it will copy the string into
554 : // *scratch and return that.
555 : //
556 : // Note: It is perfectly reasonable and useful to write code like:
557 : // str = reflection->GetStringReference(field, &str);
558 : // This line would ensure that only one copy of the string is made
559 : // regardless of the field's underlying representation. When initializing
560 : // a newly-constructed string, though, it's just as fast and more readable
561 : // to use code like:
562 : // string str = reflection->GetString(message, field);
563 : virtual const string& GetStringReference(const Message& message,
564 : const FieldDescriptor* field,
565 : string* scratch) const = 0;
566 :
567 :
568 : // Singular field mutators -----------------------------------------
569 : // These mutate the value of a non-repeated field.
570 :
571 : virtual void SetInt32 (Message* message,
572 : const FieldDescriptor* field, int32 value) const = 0;
573 : virtual void SetInt64 (Message* message,
574 : const FieldDescriptor* field, int64 value) const = 0;
575 : virtual void SetUInt32(Message* message,
576 : const FieldDescriptor* field, uint32 value) const = 0;
577 : virtual void SetUInt64(Message* message,
578 : const FieldDescriptor* field, uint64 value) const = 0;
579 : virtual void SetFloat (Message* message,
580 : const FieldDescriptor* field, float value) const = 0;
581 : virtual void SetDouble(Message* message,
582 : const FieldDescriptor* field, double value) const = 0;
583 : virtual void SetBool (Message* message,
584 : const FieldDescriptor* field, bool value) const = 0;
585 : virtual void SetString(Message* message,
586 : const FieldDescriptor* field,
587 : const string& value) const = 0;
588 : virtual void SetEnum (Message* message,
589 : const FieldDescriptor* field,
590 : const EnumValueDescriptor* value) const = 0;
591 : // Set an enum field's value with an integer rather than EnumValueDescriptor.
592 : // If the value does not correspond to a known enum value, either behavior is
593 : // undefined (for proto2 messages), or the value is accepted silently for
594 : // messages with new unknown-enum-value semantics.
595 : virtual void SetEnumValue(Message* message,
596 : const FieldDescriptor* field,
597 : int value) const = 0;
598 :
599 : // Get a mutable pointer to a field with a message type. If a MessageFactory
600 : // is provided, it will be used to construct instances of the sub-message;
601 : // otherwise, the default factory is used. If the field is an extension that
602 : // does not live in the same pool as the containing message's descriptor (e.g.
603 : // it lives in an overlay pool), then a MessageFactory must be provided.
604 : // If you have no idea what that meant, then you probably don't need to worry
605 : // about it (don't provide a MessageFactory). WARNING: If the
606 : // FieldDescriptor is for a compiled-in extension, then
607 : // factory->GetPrototype(field->message_type()) MUST return an instance of
608 : // the compiled-in class for this type, NOT DynamicMessage.
609 : virtual Message* MutableMessage(Message* message,
610 : const FieldDescriptor* field,
611 : MessageFactory* factory = NULL) const = 0;
612 : // Replaces the message specified by 'field' with the already-allocated object
613 : // sub_message, passing ownership to the message. If the field contained a
614 : // message, that message is deleted. If sub_message is NULL, the field is
615 : // cleared.
616 : virtual void SetAllocatedMessage(Message* message,
617 : Message* sub_message,
618 : const FieldDescriptor* field) const = 0;
619 : // Releases the message specified by 'field' and returns the pointer,
620 : // ReleaseMessage() will return the message the message object if it exists.
621 : // Otherwise, it may or may not return NULL. In any case, if the return value
622 : // is non-NULL, the caller takes ownership of the pointer.
623 : // If the field existed (HasField() is true), then the returned pointer will
624 : // be the same as the pointer returned by MutableMessage().
625 : // This function has the same effect as ClearField().
626 : virtual Message* ReleaseMessage(Message* message,
627 : const FieldDescriptor* field,
628 : MessageFactory* factory = NULL) const = 0;
629 :
630 :
631 : // Repeated field getters ------------------------------------------
632 : // These get the value of one element of a repeated field.
633 :
634 : virtual int32 GetRepeatedInt32 (const Message& message,
635 : const FieldDescriptor* field,
636 : int index) const = 0;
637 : virtual int64 GetRepeatedInt64 (const Message& message,
638 : const FieldDescriptor* field,
639 : int index) const = 0;
640 : virtual uint32 GetRepeatedUInt32(const Message& message,
641 : const FieldDescriptor* field,
642 : int index) const = 0;
643 : virtual uint64 GetRepeatedUInt64(const Message& message,
644 : const FieldDescriptor* field,
645 : int index) const = 0;
646 : virtual float GetRepeatedFloat (const Message& message,
647 : const FieldDescriptor* field,
648 : int index) const = 0;
649 : virtual double GetRepeatedDouble(const Message& message,
650 : const FieldDescriptor* field,
651 : int index) const = 0;
652 : virtual bool GetRepeatedBool (const Message& message,
653 : const FieldDescriptor* field,
654 : int index) const = 0;
655 : virtual string GetRepeatedString(const Message& message,
656 : const FieldDescriptor* field,
657 : int index) const = 0;
658 : virtual const EnumValueDescriptor* GetRepeatedEnum(
659 : const Message& message,
660 : const FieldDescriptor* field, int index) const = 0;
661 : // GetRepeatedEnumValue() returns an enum field's value as an integer rather
662 : // than an EnumValueDescriptor*. If the integer value does not correspond to a
663 : // known value descriptor, a new value descriptor is created. (Such a value
664 : // will only be present when the new unknown-enum-value semantics are enabled
665 : // for a message.)
666 : virtual int GetRepeatedEnumValue(
667 : const Message& message,
668 : const FieldDescriptor* field, int index) const = 0;
669 : virtual const Message& GetRepeatedMessage(
670 : const Message& message,
671 : const FieldDescriptor* field, int index) const = 0;
672 :
673 : // See GetStringReference(), above.
674 : virtual const string& GetRepeatedStringReference(
675 : const Message& message, const FieldDescriptor* field,
676 : int index, string* scratch) const = 0;
677 :
678 :
679 : // Repeated field mutators -----------------------------------------
680 : // These mutate the value of one element of a repeated field.
681 :
682 : virtual void SetRepeatedInt32 (Message* message,
683 : const FieldDescriptor* field,
684 : int index, int32 value) const = 0;
685 : virtual void SetRepeatedInt64 (Message* message,
686 : const FieldDescriptor* field,
687 : int index, int64 value) const = 0;
688 : virtual void SetRepeatedUInt32(Message* message,
689 : const FieldDescriptor* field,
690 : int index, uint32 value) const = 0;
691 : virtual void SetRepeatedUInt64(Message* message,
692 : const FieldDescriptor* field,
693 : int index, uint64 value) const = 0;
694 : virtual void SetRepeatedFloat (Message* message,
695 : const FieldDescriptor* field,
696 : int index, float value) const = 0;
697 : virtual void SetRepeatedDouble(Message* message,
698 : const FieldDescriptor* field,
699 : int index, double value) const = 0;
700 : virtual void SetRepeatedBool (Message* message,
701 : const FieldDescriptor* field,
702 : int index, bool value) const = 0;
703 : virtual void SetRepeatedString(Message* message,
704 : const FieldDescriptor* field,
705 : int index, const string& value) const = 0;
706 : virtual void SetRepeatedEnum(Message* message,
707 : const FieldDescriptor* field, int index,
708 : const EnumValueDescriptor* value) const = 0;
709 : // Set an enum field's value with an integer rather than EnumValueDescriptor.
710 : // If the value does not correspond to a known enum value, either behavior is
711 : // undefined (for proto2 messages), or the value is accepted silently for
712 : // messages with new unknown-enum-value semantics.
713 : virtual void SetRepeatedEnumValue(Message* message,
714 : const FieldDescriptor* field, int index,
715 : int value) const = 0;
716 : // Get a mutable pointer to an element of a repeated field with a message
717 : // type.
718 : virtual Message* MutableRepeatedMessage(
719 : Message* message, const FieldDescriptor* field, int index) const = 0;
720 :
721 :
722 : // Repeated field adders -------------------------------------------
723 : // These add an element to a repeated field.
724 :
725 : virtual void AddInt32 (Message* message,
726 : const FieldDescriptor* field, int32 value) const = 0;
727 : virtual void AddInt64 (Message* message,
728 : const FieldDescriptor* field, int64 value) const = 0;
729 : virtual void AddUInt32(Message* message,
730 : const FieldDescriptor* field, uint32 value) const = 0;
731 : virtual void AddUInt64(Message* message,
732 : const FieldDescriptor* field, uint64 value) const = 0;
733 : virtual void AddFloat (Message* message,
734 : const FieldDescriptor* field, float value) const = 0;
735 : virtual void AddDouble(Message* message,
736 : const FieldDescriptor* field, double value) const = 0;
737 : virtual void AddBool (Message* message,
738 : const FieldDescriptor* field, bool value) const = 0;
739 : virtual void AddString(Message* message,
740 : const FieldDescriptor* field,
741 : const string& value) const = 0;
742 : virtual void AddEnum (Message* message,
743 : const FieldDescriptor* field,
744 : const EnumValueDescriptor* value) const = 0;
745 : // Set an enum field's value with an integer rather than EnumValueDescriptor.
746 : // If the value does not correspond to a known enum value, either behavior is
747 : // undefined (for proto2 messages), or the value is accepted silently for
748 : // messages with new unknown-enum-value semantics.
749 : virtual void AddEnumValue(Message* message,
750 : const FieldDescriptor* field,
751 : int value) const = 0;
752 : // See MutableMessage() for comments on the "factory" parameter.
753 : virtual Message* AddMessage(Message* message,
754 : const FieldDescriptor* field,
755 : MessageFactory* factory = NULL) const = 0;
756 :
757 : // Appends an already-allocated object 'new_entry' to the repeated field
758 : // specifyed by 'field' passing ownership to the message.
759 : // TODO(tmarek): Make virtual after all subclasses have been
760 : // updated.
761 : virtual void AddAllocatedMessage(Message* message,
762 : const FieldDescriptor* field,
763 : Message* new_entry) const;
764 :
765 :
766 : // Get a RepeatedFieldRef object that can be used to read the underlying
767 : // repeated field. The type parameter T must be set according to the
768 : // field's cpp type. The following table shows the mapping from cpp type
769 : // to acceptable T.
770 : //
771 : // field->cpp_type() T
772 : // CPPTYPE_INT32 int32
773 : // CPPTYPE_UINT32 uint32
774 : // CPPTYPE_INT64 int64
775 : // CPPTYPE_UINT64 uint64
776 : // CPPTYPE_DOUBLE double
777 : // CPPTYPE_FLOAT float
778 : // CPPTYPE_BOOL bool
779 : // CPPTYPE_ENUM generated enum type or int32
780 : // CPPTYPE_STRING string
781 : // CPPTYPE_MESSAGE generated message type or google::protobuf::Message
782 : //
783 : // A RepeatedFieldRef object can be copied and the resulted object will point
784 : // to the same repeated field in the same message. The object can be used as
785 : // long as the message is not destroyed.
786 : //
787 : // Note that to use this method users need to include the header file
788 : // "google/protobuf/reflection.h" (which defines the RepeatedFieldRef
789 : // class templates).
790 : template<typename T>
791 : RepeatedFieldRef<T> GetRepeatedFieldRef(
792 : const Message& message, const FieldDescriptor* field) const;
793 :
794 : // Like GetRepeatedFieldRef() but return an object that can also be used
795 : // manipulate the underlying repeated field.
796 : template<typename T>
797 : MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
798 : Message* message, const FieldDescriptor* field) const;
799 :
800 : // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field
801 : // access. The following repeated field accesors will be removed in the
802 : // future.
803 : //
804 : // Repeated field accessors -------------------------------------------------
805 : // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular
806 : // access to the data in a RepeatedField. The methods below provide aggregate
807 : // access by exposing the RepeatedField object itself with the Message.
808 : // Applying these templates to inappropriate types will lead to an undefined
809 : // reference at link time (e.g. GetRepeatedField<***double>), or possibly a
810 : // template matching error at compile time (e.g. GetRepeatedPtrField<File>).
811 : //
812 : // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd);
813 :
814 : // DEPRECATED. Please use GetRepeatedFieldRef().
815 : //
816 : // for T = Cord and all protobuf scalar types except enums.
817 : template<typename T>
818 : PROTOBUF_RUNTIME_DEPRECATED("Please use GetRepeatedFieldRef() instead")
819 : const RepeatedField<T>& GetRepeatedField(
820 : const Message&, const FieldDescriptor*) const;
821 :
822 : // DEPRECATED. Please use GetMutableRepeatedFieldRef().
823 : //
824 : // for T = Cord and all protobuf scalar types except enums.
825 : template<typename T>
826 : PROTOBUF_RUNTIME_DEPRECATED("Please use GetMutableRepeatedFieldRef() instead")
827 : RepeatedField<T>* MutableRepeatedField(
828 : Message*, const FieldDescriptor*) const;
829 :
830 : // DEPRECATED. Please use GetRepeatedFieldRef().
831 : //
832 : // for T = string, google::protobuf::internal::StringPieceField
833 : // google::protobuf::Message & descendants.
834 : template<typename T>
835 : PROTOBUF_RUNTIME_DEPRECATED("Please use GetRepeatedFieldRef() instead")
836 : const RepeatedPtrField<T>& GetRepeatedPtrField(
837 : const Message&, const FieldDescriptor*) const;
838 :
839 : // DEPRECATED. Please use GetMutableRepeatedFieldRef().
840 : //
841 : // for T = string, google::protobuf::internal::StringPieceField
842 : // google::protobuf::Message & descendants.
843 : template<typename T>
844 : PROTOBUF_RUNTIME_DEPRECATED("Please use GetMutableRepeatedFieldRef() instead")
845 : RepeatedPtrField<T>* MutableRepeatedPtrField(
846 : Message*, const FieldDescriptor*) const;
847 :
848 : // Extensions ----------------------------------------------------------------
849 :
850 : // Try to find an extension of this message type by fully-qualified field
851 : // name. Returns NULL if no extension is known for this name or number.
852 : virtual const FieldDescriptor* FindKnownExtensionByName(
853 : const string& name) const = 0;
854 :
855 : // Try to find an extension of this message type by field number.
856 : // Returns NULL if no extension is known for this name or number.
857 : virtual const FieldDescriptor* FindKnownExtensionByNumber(
858 : int number) const = 0;
859 :
860 : // Feature Flags -------------------------------------------------------------
861 :
862 : // Does this message support storing arbitrary integer values in enum fields?
863 : // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions
864 : // take arbitrary integer values, and the legacy GetEnum() getter will
865 : // dynamically create an EnumValueDescriptor for any integer value without
866 : // one. If |false|, setting an unknown enum value via the integer-based
867 : // setters results in undefined behavior (in practice, GOOGLE_DCHECK-fails).
868 : //
869 : // Generic code that uses reflection to handle messages with enum fields
870 : // should check this flag before using the integer-based setter, and either
871 : // downgrade to a compatible value or use the UnknownFieldSet if not. For
872 : // example:
873 : //
874 : // int new_value = GetValueFromApplicationLogic();
875 : // if (reflection->SupportsUnknownEnumValues()) {
876 : // reflection->SetEnumValue(message, field, new_value);
877 : // } else {
878 : // if (field_descriptor->enum_type()->
879 : // FindValueByNumber(new_value) != NULL) {
880 : // reflection->SetEnumValue(message, field, new_value);
881 : // } else if (emit_unknown_enum_values) {
882 : // reflection->MutableUnknownFields(message)->AddVarint(
883 : // field->number(), new_value);
884 : // } else {
885 : // // convert value to a compatible/default value.
886 : // new_value = CompatibleDowngrade(new_value);
887 : // reflection->SetEnumValue(message, field, new_value);
888 : // }
889 : // }
890 : virtual bool SupportsUnknownEnumValues() const { return false; }
891 :
892 : // Returns the MessageFactory associated with this message. This can be
893 : // useful for determining if a message is a generated message or not, for
894 : // example:
895 : // if (message->GetReflection()->GetMessageFactory() ==
896 : // google::protobuf::MessageFactory::generated_factory()) {
897 : // // This is a generated message.
898 : // }
899 : // It can also be used to create more messages of this type, though
900 : // Message::New() is an easier way to accomplish this.
901 : virtual MessageFactory* GetMessageFactory() const;
902 :
903 : // ---------------------------------------------------------------------------
904 :
905 : protected:
906 : // Obtain a pointer to a Repeated Field Structure and do some type checking:
907 : // on field->cpp_type(),
908 : // on field->field_option().ctype() (if ctype >= 0)
909 : // of field->message_type() (if message_type != NULL).
910 : // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer).
911 : virtual void* MutableRawRepeatedField(
912 : Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
913 : int ctype, const Descriptor* message_type) const = 0;
914 :
915 : // TODO(jieluo) - make it pure virtual after updating all the subclasses.
916 : virtual const void* GetRawRepeatedField(
917 : const Message& message, const FieldDescriptor* field,
918 : FieldDescriptor::CppType cpptype, int ctype,
919 : const Descriptor* message_type) const {
920 : return MutableRawRepeatedField(
921 : const_cast<Message*>(&message), field, cpptype, ctype, message_type);
922 : }
923 :
924 : // The following methods are used to implement (Mutable)RepeatedFieldRef.
925 : // A Ref object will store a raw pointer to the repeated field data (obtained
926 : // from RepeatedFieldData()) and a pointer to a Accessor (obtained from
927 : // RepeatedFieldAccessor) which will be used to access the raw data.
928 : //
929 : // TODO(xiaofeng): Make these methods pure-virtual.
930 :
931 : // Returns a raw pointer to the repeated field
932 : //
933 : // "cpp_type" and "message_type" are deduced from the type parameter T passed
934 : // to Get(Mutable)RepeatedFieldRef. If T is a generated message type,
935 : // "message_type" should be set to its descriptor. Otherwise "message_type"
936 : // should be set to NULL. Implementations of this method should check whether
937 : // "cpp_type"/"message_type" is consistent with the actual type of the field.
938 : // We use 1 routine rather than 2 (const vs mutable) because it is protected
939 : // and it doesn't change the message.
940 : virtual void* RepeatedFieldData(
941 : Message* message, const FieldDescriptor* field,
942 : FieldDescriptor::CppType cpp_type,
943 : const Descriptor* message_type) const;
944 :
945 : // The returned pointer should point to a singleton instance which implements
946 : // the RepeatedFieldAccessor interface.
947 : virtual const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
948 : const FieldDescriptor* field) const;
949 :
950 : private:
951 : template<typename T, typename Enable>
952 : friend class RepeatedFieldRef;
953 : template<typename T, typename Enable>
954 : friend class MutableRepeatedFieldRef;
955 : friend class ::google::protobuf::python::MapReflectionFriend;
956 : #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND
957 : friend class ::google::protobuf::expr::CelMapReflectionFriend;
958 : friend class internal::MapFieldReflectionTest;
959 : friend class internal::MapKeySorter;
960 : friend class internal::WireFormat;
961 : friend class internal::ReflectionOps;
962 :
963 : // Special version for specialized implementations of string. We can't call
964 : // MutableRawRepeatedField directly here because we don't have access to
965 : // FieldOptions::* which are defined in descriptor.pb.h. Including that
966 : // file here is not possible because it would cause a circular include cycle.
967 : // We use 1 routine rather than 2 (const vs mutable) because it is private
968 : // and mutable a repeated string field doesn't change the message.
969 : void* MutableRawRepeatedString(
970 : Message* message, const FieldDescriptor* field, bool is_string) const;
971 :
972 : friend class MapReflectionTester;
973 : // TODO(jieluo) - make the map APIs pure virtual after updating
974 : // all the subclasses.
975 : // Returns true if key is in map. Returns false if key is not in map field.
976 : virtual bool ContainsMapKey(const Message& /* message */,
977 : const FieldDescriptor* /* field */,
978 : const MapKey& /* key */) const {
979 : return false;
980 : }
981 :
982 : // If key is in map field: Saves the value pointer to val and returns
983 : // false. If key in not in map field: Insert the key into map, saves
984 : // value pointer to val and retuns true.
985 : virtual bool InsertOrLookupMapValue(Message* /* message */,
986 : const FieldDescriptor* /* field */,
987 : const MapKey& /* key */,
988 : MapValueRef* /* val */) const {
989 : return false;
990 : }
991 :
992 : // Delete and returns true if key is in the map field. Returns false
993 : // otherwise.
994 : virtual bool DeleteMapValue(Message* /* message */,
995 : const FieldDescriptor* /* field */,
996 : const MapKey& /* key */) const {
997 : return false;
998 : }
999 :
1000 : // Returns a MapIterator referring to the first element in the map field.
1001 : // If the map field is empty, this function returns the same as
1002 : // reflection::MapEnd. Mutation to the field may invalidate the iterator.
1003 : virtual MapIterator MapBegin(
1004 : Message* message,
1005 : const FieldDescriptor* field) const;
1006 :
1007 : // Returns a MapIterator referring to the theoretical element that would
1008 : // follow the last element in the map field. It does not point to any
1009 : // real element. Mutation to the field may invalidate the iterator.
1010 : virtual MapIterator MapEnd(
1011 : Message* message,
1012 : const FieldDescriptor* field) const;
1013 :
1014 : // Get the number of <key, value> pair of a map field. The result may be
1015 : // different from FieldSize which can have duplicate keys.
1016 : virtual int MapSize(const Message& /* message */,
1017 : const FieldDescriptor* /* field */) const {
1018 : return 0;
1019 : }
1020 :
1021 : // Help method for MapIterator.
1022 : friend class MapIterator;
1023 : virtual internal::MapFieldBase* MapData(
1024 : Message* /* message */, const FieldDescriptor* /* field */) const {
1025 : return NULL;
1026 : }
1027 :
1028 : GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection);
1029 : };
1030 :
1031 : // Abstract interface for a factory for message objects.
1032 : class LIBPROTOBUF_EXPORT MessageFactory {
1033 : public:
1034 : inline MessageFactory() {}
1035 : virtual ~MessageFactory();
1036 :
1037 : // Given a Descriptor, gets or constructs the default (prototype) Message
1038 : // of that type. You can then call that message's New() method to construct
1039 : // a mutable message of that type.
1040 : //
1041 : // Calling this method twice with the same Descriptor returns the same
1042 : // object. The returned object remains property of the factory. Also, any
1043 : // objects created by calling the prototype's New() method share some data
1044 : // with the prototype, so these must be destroyed before the MessageFactory
1045 : // is destroyed.
1046 : //
1047 : // The given descriptor must outlive the returned message, and hence must
1048 : // outlive the MessageFactory.
1049 : //
1050 : // Some implementations do not support all types. GetPrototype() will
1051 : // return NULL if the descriptor passed in is not supported.
1052 : //
1053 : // This method may or may not be thread-safe depending on the implementation.
1054 : // Each implementation should document its own degree thread-safety.
1055 : virtual const Message* GetPrototype(const Descriptor* type) = 0;
1056 :
1057 : // Gets a MessageFactory which supports all generated, compiled-in messages.
1058 : // In other words, for any compiled-in type FooMessage, the following is true:
1059 : // MessageFactory::generated_factory()->GetPrototype(
1060 : // FooMessage::descriptor()) == FooMessage::default_instance()
1061 : // This factory supports all types which are found in
1062 : // DescriptorPool::generated_pool(). If given a descriptor from any other
1063 : // pool, GetPrototype() will return NULL. (You can also check if a
1064 : // descriptor is for a generated message by checking if
1065 : // descriptor->file()->pool() == DescriptorPool::generated_pool().)
1066 : //
1067 : // This factory is 100% thread-safe; calling GetPrototype() does not modify
1068 : // any shared data.
1069 : //
1070 : // This factory is a singleton. The caller must not delete the object.
1071 : static MessageFactory* generated_factory();
1072 :
1073 : // For internal use only: Registers a .proto file at static initialization
1074 : // time, to be placed in generated_factory. The first time GetPrototype()
1075 : // is called with a descriptor from this file, |register_messages| will be
1076 : // called, with the file name as the parameter. It must call
1077 : // InternalRegisterGeneratedMessage() (below) to register each message type
1078 : // in the file. This strange mechanism is necessary because descriptors are
1079 : // built lazily, so we can't register types by their descriptor until we
1080 : // know that the descriptor exists. |filename| must be a permanent string.
1081 : static void InternalRegisterGeneratedFile(
1082 : const char* filename, void (*register_messages)(const string&));
1083 :
1084 : // For internal use only: Registers a message type. Called only by the
1085 : // functions which are registered with InternalRegisterGeneratedFile(),
1086 : // above.
1087 : static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
1088 : const Message* prototype);
1089 :
1090 :
1091 : private:
1092 : GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory);
1093 : };
1094 :
1095 : #define DECLARE_GET_REPEATED_FIELD(TYPE) \
1096 : template<> \
1097 : LIBPROTOBUF_EXPORT \
1098 : const RepeatedField<TYPE>& Reflection::GetRepeatedField<TYPE>( \
1099 : const Message& message, const FieldDescriptor* field) const; \
1100 : \
1101 : template<> \
1102 : LIBPROTOBUF_EXPORT \
1103 : RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
1104 : Message* message, const FieldDescriptor* field) const;
1105 :
1106 : DECLARE_GET_REPEATED_FIELD(int32)
1107 : DECLARE_GET_REPEATED_FIELD(int64)
1108 : DECLARE_GET_REPEATED_FIELD(uint32)
1109 : DECLARE_GET_REPEATED_FIELD(uint64)
1110 : DECLARE_GET_REPEATED_FIELD(float)
1111 : DECLARE_GET_REPEATED_FIELD(double)
1112 : DECLARE_GET_REPEATED_FIELD(bool)
1113 :
1114 : #undef DECLARE_GET_REPEATED_FIELD
1115 :
1116 : // =============================================================================
1117 : // Implementation details for {Get,Mutable}RawRepeatedPtrField. We provide
1118 : // specializations for <string>, <StringPieceField> and <Message> and handle
1119 : // everything else with the default template which will match any type having
1120 : // a method with signature "static const google::protobuf::Descriptor* descriptor()".
1121 : // Such a type presumably is a descendant of google::protobuf::Message.
1122 :
1123 : template<>
1124 : inline const RepeatedPtrField<string>& Reflection::GetRepeatedPtrField<string>(
1125 : const Message& message, const FieldDescriptor* field) const {
1126 : return *static_cast<RepeatedPtrField<string>* >(
1127 : MutableRawRepeatedString(const_cast<Message*>(&message), field, true));
1128 : }
1129 :
1130 : template<>
1131 : inline RepeatedPtrField<string>* Reflection::MutableRepeatedPtrField<string>(
1132 : Message* message, const FieldDescriptor* field) const {
1133 : return static_cast<RepeatedPtrField<string>* >(
1134 : MutableRawRepeatedString(message, field, true));
1135 : }
1136 :
1137 :
1138 : // -----
1139 :
1140 : template<>
1141 : inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField(
1142 : const Message& message, const FieldDescriptor* field) const {
1143 : return *static_cast<const RepeatedPtrField<Message>* >(
1144 : GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE,
1145 : -1, NULL));
1146 : }
1147 :
1148 : template<>
1149 : inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField(
1150 : Message* message, const FieldDescriptor* field) const {
1151 : return static_cast<RepeatedPtrField<Message>* >(
1152 : MutableRawRepeatedField(message, field,
1153 : FieldDescriptor::CPPTYPE_MESSAGE, -1,
1154 : NULL));
1155 : }
1156 :
1157 : template<typename PB>
1158 : inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField(
1159 : const Message& message, const FieldDescriptor* field) const {
1160 : return *static_cast<const RepeatedPtrField<PB>* >(
1161 : GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE,
1162 : -1, PB::default_instance().GetDescriptor()));
1163 : }
1164 :
1165 : template<typename PB>
1166 : inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField(
1167 : Message* message, const FieldDescriptor* field) const {
1168 : return static_cast<RepeatedPtrField<PB>* >(
1169 : MutableRawRepeatedField(message, field,
1170 : FieldDescriptor::CPPTYPE_MESSAGE, -1,
1171 : PB::default_instance().GetDescriptor()));
1172 : }
1173 : } // namespace protobuf
1174 :
1175 : } // namespace google
1176 : #endif // GOOGLE_PROTOBUF_MESSAGE_H__
|