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