blob: 6e1929e5c3e7fb55735e712a5f4693726c23d2a4 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34//
kenton@google.com323e6322009-08-08 03:23:04 +000035// 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.
temporal40ee5512008-07-10 02:12:20 +000039//
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;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000082// const Descriptor* descriptor = foo->GetDescriptor();
temporal40ee5512008-07-10 02:12:20 +000083//
84// // Get the descriptors for the fields we're interested in and verify
85// // their types.
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000086// const FieldDescriptor* text_field = descriptor->FindFieldByName("text");
temporal40ee5512008-07-10 02:12:20 +000087// assert(text_field != NULL);
88// assert(text_field->type() == FieldDescriptor::TYPE_STRING);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000089// assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL);
90// const FieldDescriptor* numbers_field = descriptor->
91// FindFieldByName("numbers");
temporal40ee5512008-07-10 02:12:20 +000092// assert(numbers_field != NULL);
93// assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000094// assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED);
temporal40ee5512008-07-10 02:12:20 +000095//
96// // Parse the message.
97// foo->ParseFromString(data);
98//
99// // Use the reflection interface to examine the contents.
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000100// const Reflection* reflection = foo->GetReflection();
temporal779f61c2008-08-13 03:15:00 +0000101// assert(reflection->GetString(foo, text_field) == "Hello World!");
kenton@google.comceb561d2009-06-25 19:05:36 +0000102// assert(reflection->FieldSize(foo, numbers_field) == 3);
kenton@google.com80b1d622009-07-29 01:13:20 +0000103// 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);
temporal40ee5512008-07-10 02:12:20 +0000106//
107// delete foo;
108// }
109
110#ifndef GOOGLE_PROTOBUF_MESSAGE_H__
111#define GOOGLE_PROTOBUF_MESSAGE_H__
112
temporal40ee5512008-07-10 02:12:20 +0000113#include <iosfwd>
jieluo@google.com4de8f552014-07-18 00:47:59 +0000114#include <string>
Feng Xiao6ef984a2014-11-10 17:34:54 -0800115#include <google/protobuf/stubs/type_traits.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +0000116#include <vector>
kenton@google.comd37d46d2009-04-25 02:53:47 +0000117
Feng Xiao6ef984a2014-11-10 17:34:54 -0800118#include <google/protobuf/arena.h>
kenton@google.com80b1d622009-07-29 01:13:20 +0000119#include <google/protobuf/message_lite.h>
120
121#include <google/protobuf/stubs/common.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000122#include <google/protobuf/descriptor.h>
kenton@google.com80b1d622009-07-29 01:13:20 +0000123
liujisi@google.com33165fe2010-11-02 13:14:58 +0000124
jieluo@google.com4de8f552014-07-18 00:47:59 +0000125#define GOOGLE_PROTOBUF_HAS_ONEOF
Feng Xiao6ef984a2014-11-10 17:34:54 -0800126#define GOOGLE_PROTOBUF_HAS_ARENAS
jieluo@google.com4de8f552014-07-18 00:47:59 +0000127
temporal40ee5512008-07-10 02:12:20 +0000128namespace google {
temporal40ee5512008-07-10 02:12:20 +0000129namespace protobuf {
130
131// Defined in this file.
132class Message;
temporal779f61c2008-08-13 03:15:00 +0000133class Reflection;
kenton@google.comfccb1462009-12-18 02:11:36 +0000134class MessageFactory;
temporal40ee5512008-07-10 02:12:20 +0000135
136// Defined in other files.
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000137class UnknownFieldSet; // unknown_field_set.h
temporal40ee5512008-07-10 02:12:20 +0000138namespace io {
139 class ZeroCopyInputStream; // zero_copy_stream.h
140 class ZeroCopyOutputStream; // zero_copy_stream.h
141 class CodedInputStream; // coded_stream.h
142 class CodedOutputStream; // coded_stream.h
143}
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000144
145
146template<typename T>
147class RepeatedField; // repeated_field.h
148
149template<typename T>
150class RepeatedPtrField; // repeated_field.h
temporal40ee5512008-07-10 02:12:20 +0000151
kenton@google.com80b1d622009-07-29 01:13:20 +0000152// A container to hold message metadata.
153struct Metadata {
154 const Descriptor* descriptor;
155 const Reflection* reflection;
156};
157
temporal40ee5512008-07-10 02:12:20 +0000158// Abstract interface for protocol messages.
159//
kenton@google.com80b1d622009-07-29 01:13:20 +0000160// See also MessageLite, which contains most every-day operations. Message
161// adds descriptors and reflection on top of that.
162//
temporal40ee5512008-07-10 02:12:20 +0000163// The methods of this class that are virtual but not pure-virtual have
164// default implementations based on reflection. Message classes which are
165// optimized for speed will want to override these with faster implementations,
166// but classes optimized for code size may be happy with keeping them. See
167// the optimize_for option in descriptor.proto.
kenton@google.com80b1d622009-07-29 01:13:20 +0000168class LIBPROTOBUF_EXPORT Message : public MessageLite {
temporal40ee5512008-07-10 02:12:20 +0000169 public:
170 inline Message() {}
171 virtual ~Message();
172
173 // Basic Operations ------------------------------------------------
174
175 // Construct a new instance of the same type. Ownership is passed to the
kenton@google.com80b1d622009-07-29 01:13:20 +0000176 // caller. (This is also defined in MessageLite, but is defined again here
177 // for return-type covariance.)
temporal40ee5512008-07-10 02:12:20 +0000178 virtual Message* New() const = 0;
179
Feng Xiao6ef984a2014-11-10 17:34:54 -0800180 // Construct a new instance on the arena. Ownership is passed to the caller
181 // if arena is a NULL. Default implementation allows for API compatibility
182 // during the Arena transition.
183 virtual Message* New(::google::protobuf::Arena* arena) const {
184 Message* message = New();
185 if (arena != NULL) {
186 arena->Own(message);
187 }
188 return message;
189 }
190
temporal40ee5512008-07-10 02:12:20 +0000191 // Make this message into a copy of the given message. The given message
192 // must have the same descriptor, but need not necessarily be the same class.
193 // By default this is just implemented as "Clear(); MergeFrom(from);".
194 virtual void CopyFrom(const Message& from);
195
196 // Merge the fields from the given message into this message. Singular
jieluo@google.com4de8f552014-07-18 00:47:59 +0000197 // fields will be overwritten, if specified in from, except for embedded
198 // messages which will be merged. Repeated fields will be concatenated.
199 // The given message must be of the same type as this message (i.e. the
200 // exact same class).
temporal40ee5512008-07-10 02:12:20 +0000201 virtual void MergeFrom(const Message& from);
202
temporal40ee5512008-07-10 02:12:20 +0000203 // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
204 // a nice error message.
205 void CheckInitialized() const;
206
207 // Slowly build a list of all required fields that are not set.
208 // This is much, much slower than IsInitialized() as it is implemented
209 // purely via reflection. Generally, you should not call this unless you
210 // have already determined that an error exists by calling IsInitialized().
Jisi Liu885b6122015-02-28 14:51:22 -0800211 void FindInitializationErrors(std::vector<string>* errors) const;
temporal40ee5512008-07-10 02:12:20 +0000212
213 // Like FindInitializationErrors, but joins all the strings, delimited by
214 // commas, and returns them.
215 string InitializationErrorString() const;
216
217 // Clears all unknown fields from this message and all embedded messages.
218 // Normally, if unknown tag numbers are encountered when parsing a message,
219 // the tag and value are stored in the message's UnknownFieldSet and
220 // then written back out when the message is serialized. This allows servers
221 // which simply route messages to other servers to pass through messages
222 // that have new field definitions which they don't yet know about. However,
223 // this behavior can have security implications. To avoid it, call this
224 // method after parsing.
225 //
226 // See Reflection::GetUnknownFields() for more on unknown fields.
227 virtual void DiscardUnknownFields();
228
kenton@google.comd2fd0632009-07-24 01:00:35 +0000229 // Computes (an estimate of) the total number of bytes currently used for
230 // storing the message in memory. The default implementation calls the
231 // Reflection object's SpaceUsed() method.
232 virtual int SpaceUsed() const;
233
kenton@google.comfccb1462009-12-18 02:11:36 +0000234 // Debugging & Testing----------------------------------------------
temporal40ee5512008-07-10 02:12:20 +0000235
236 // Generates a human readable form of this message, useful for debugging
237 // and other purposes.
238 string DebugString() const;
239 // Like DebugString(), but with less whitespace.
240 string ShortDebugString() const;
kenton@google.comfccb1462009-12-18 02:11:36 +0000241 // Like DebugString(), but do not escape UTF-8 byte sequences.
242 string Utf8DebugString() const;
temporal40ee5512008-07-10 02:12:20 +0000243 // Convenience function useful in GDB. Prints DebugString() to stdout.
244 void PrintDebugString() const;
245
kenton@google.com80b1d622009-07-29 01:13:20 +0000246 // Heavy I/O -------------------------------------------------------
247 // Additional parsing and serialization methods not implemented by
248 // MessageLite because they are not supported by the lite library.
temporal40ee5512008-07-10 02:12:20 +0000249
250 // Parse a protocol buffer from a file descriptor. If successful, the entire
251 // input will be consumed.
252 bool ParseFromFileDescriptor(int file_descriptor);
253 // Like ParseFromFileDescriptor(), but accepts messages that are missing
254 // required fields.
255 bool ParsePartialFromFileDescriptor(int file_descriptor);
256 // Parse a protocol buffer from a C++ istream. If successful, the entire
257 // input will be consumed.
258 bool ParseFromIstream(istream* input);
259 // Like ParseFromIstream(), but accepts messages that are missing
260 // required fields.
261 bool ParsePartialFromIstream(istream* input);
262
temporal40ee5512008-07-10 02:12:20 +0000263 // Serialize the message and write it to the given file descriptor. All
264 // required fields must be set.
265 bool SerializeToFileDescriptor(int file_descriptor) const;
266 // Like SerializeToFileDescriptor(), but allows missing required fields.
267 bool SerializePartialToFileDescriptor(int file_descriptor) const;
268 // Serialize the message and write it to the given C++ ostream. All
269 // required fields must be set.
270 bool SerializeToOstream(ostream* output) const;
271 // Like SerializeToOstream(), but allows missing required fields.
272 bool SerializePartialToOstream(ostream* output) const;
273
274
kenton@google.com80b1d622009-07-29 01:13:20 +0000275 // Reflection-based methods ----------------------------------------
276 // These methods are pure-virtual in MessageLite, but Message provides
277 // reflection-based default implementations.
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000278
kenton@google.com80b1d622009-07-29 01:13:20 +0000279 virtual string GetTypeName() const;
280 virtual void Clear();
281 virtual bool IsInitialized() const;
282 virtual void CheckTypeAndMergeFrom(const MessageLite& other);
283 virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
temporal40ee5512008-07-10 02:12:20 +0000284 virtual int ByteSize() const;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000285 virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
286
temporal40ee5512008-07-10 02:12:20 +0000287 private:
288 // This is called only by the default implementation of ByteSize(), to
289 // update the cached size. If you override ByteSize(), you do not need
290 // to override this. If you do not override ByteSize(), you MUST override
291 // this; the default implementation will crash.
292 //
293 // The method is private because subclasses should never call it; only
294 // override it. Yes, C++ lets you do that. Crazy, huh?
295 virtual void SetCachedSize(int size) const;
296
297 public:
298
299 // Introspection ---------------------------------------------------
300
temporal779f61c2008-08-13 03:15:00 +0000301 // Typedef for backwards-compatibility.
302 typedef google::protobuf::Reflection Reflection;
temporal40ee5512008-07-10 02:12:20 +0000303
304 // Get a Descriptor for this message's type. This describes what
305 // fields the message contains, the types of those fields, etc.
kenton@google.com80b1d622009-07-29 01:13:20 +0000306 const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
temporal40ee5512008-07-10 02:12:20 +0000307
308 // Get the Reflection interface for this Message, which can be used to
309 // read and modify the fields of the Message dynamically (in other words,
310 // without knowing the message type at compile time). This object remains
311 // property of the Message.
kenton@google.com80b1d622009-07-29 01:13:20 +0000312 //
313 // This method remains virtual in case a subclass does not implement
314 // reflection and wants to override the default behavior.
315 virtual const Reflection* GetReflection() const {
316 return GetMetadata().reflection;
317 }
318
319 protected:
320 // Get a struct containing the metadata for the Message. Most subclasses only
321 // need to implement this method, rather than the GetDescriptor() and
322 // GetReflection() wrappers.
323 virtual Metadata GetMetadata() const = 0;
temporal40ee5512008-07-10 02:12:20 +0000324
kenton@google.comfccb1462009-12-18 02:11:36 +0000325
temporal40ee5512008-07-10 02:12:20 +0000326 private:
327 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
328};
329
Feng Xiao6ef984a2014-11-10 17:34:54 -0800330namespace internal {
331// Forward-declare interfaces used to implement RepeatedFieldRef.
332// These are protobuf internals that users shouldn't care about.
333class RepeatedFieldAccessor;
334} // namespace internal
335
336// Forward-declare RepeatedFieldRef templates. The second type parameter is
337// used for SFINAE tricks. Users should ignore it.
338template<typename T, typename Enable = void>
339class RepeatedFieldRef;
340
341template<typename T, typename Enable = void>
342class MutableRepeatedFieldRef;
343
temporal40ee5512008-07-10 02:12:20 +0000344// This interface contains methods that can be used to dynamically access
345// and modify the fields of a protocol message. Their semantics are
346// similar to the accessors the protocol compiler generates.
347//
348// To get the Reflection for a given Message, call Message::GetReflection().
349//
350// This interface is separate from Message only for efficiency reasons;
351// the vast majority of implementations of Message will share the same
352// implementation of Reflection (GeneratedMessageReflection,
temporal779f61c2008-08-13 03:15:00 +0000353// defined in generated_message.h), and all Messages of a particular class
354// should share the same Reflection object (though you should not rely on
355// the latter fact).
temporal40ee5512008-07-10 02:12:20 +0000356//
357// There are several ways that these methods can be used incorrectly. For
358// example, any of the following conditions will lead to undefined
359// results (probably assertion failures):
360// - The FieldDescriptor is not a field of this message type.
361// - The method called is not appropriate for the field's type. For
362// each field type in FieldDescriptor::TYPE_*, there is only one
363// Get*() method, one Set*() method, and one Add*() method that is
364// valid for that type. It should be obvious which (except maybe
365// for TYPE_BYTES, which are represented using strings in C++).
366// - A Get*() or Set*() method for singular fields is called on a repeated
367// field.
368// - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
369// field.
temporal779f61c2008-08-13 03:15:00 +0000370// - The Message object passed to any method is not of the right type for
371// this Reflection object (i.e. message.GetReflection() != reflection).
temporal40ee5512008-07-10 02:12:20 +0000372//
373// You might wonder why there is not any abstract representation for a field
374// of arbitrary type. E.g., why isn't there just a "GetField()" method that
375// returns "const Field&", where "Field" is some class with accessors like
376// "GetInt32Value()". The problem is that someone would have to deal with
377// allocating these Field objects. For generated message classes, having to
378// allocate space for an additional object to wrap every field would at least
379// double the message's memory footprint, probably worse. Allocating the
380// objects on-demand, on the other hand, would be expensive and prone to
381// memory leaks. So, instead we ended up with this flat interface.
382//
383// TODO(kenton): Create a utility class which callers can use to read and
384// write fields from a Reflection without paying attention to the type.
temporal779f61c2008-08-13 03:15:00 +0000385class LIBPROTOBUF_EXPORT Reflection {
temporal40ee5512008-07-10 02:12:20 +0000386 public:
387 inline Reflection() {}
388 virtual ~Reflection();
389
390 // Get the UnknownFieldSet for the message. This contains fields which
391 // were seen when the Message was parsed but were not recognized according
Feng Xiao6ef984a2014-11-10 17:34:54 -0800392 // to the Message's definition. For proto3 protos, this method will always
393 // return an empty UnknownFieldSet.
temporal779f61c2008-08-13 03:15:00 +0000394 virtual const UnknownFieldSet& GetUnknownFields(
395 const Message& message) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000396 // Get a mutable pointer to the UnknownFieldSet for the message. This
397 // contains fields which were seen when the Message was parsed but were not
Feng Xiao6ef984a2014-11-10 17:34:54 -0800398 // recognized according to the Message's definition. For proto3 protos, this
399 // method will return a valid mutable UnknownFieldSet pointer but modifying
400 // it won't affect the serialized bytes of the message.
temporal779f61c2008-08-13 03:15:00 +0000401 virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000402
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000403 // Estimate the amount of memory used by the message object.
404 virtual int SpaceUsed(const Message& message) const = 0;
405
temporal40ee5512008-07-10 02:12:20 +0000406 // Check if the given non-repeated field is set.
temporal779f61c2008-08-13 03:15:00 +0000407 virtual bool HasField(const Message& message,
408 const FieldDescriptor* field) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000409
410 // Get the number of elements of a repeated field.
temporal779f61c2008-08-13 03:15:00 +0000411 virtual int FieldSize(const Message& message,
412 const FieldDescriptor* field) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000413
414 // Clear the value of a field, so that HasField() returns false or
415 // FieldSize() returns zero.
temporal779f61c2008-08-13 03:15:00 +0000416 virtual void ClearField(Message* message,
417 const FieldDescriptor* field) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000418
Feng Xiao6ef984a2014-11-10 17:34:54 -0800419 // Check if the oneof is set. Returns true if any field in oneof
jieluo@google.com4de8f552014-07-18 00:47:59 +0000420 // is set, false otherwise.
421 // TODO(jieluo) - make it pure virtual after updating all
422 // the subclasses.
Austin Schuh918e3ee2014-10-31 16:27:55 -0700423 virtual bool HasOneof(const Message& /*message*/,
424 const OneofDescriptor* /*oneof_descriptor*/) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000425 return false;
426 }
427
Austin Schuh918e3ee2014-10-31 16:27:55 -0700428 virtual void ClearOneof(Message* /*message*/,
429 const OneofDescriptor* /*oneof_descriptor*/) const {}
jieluo@google.com4de8f552014-07-18 00:47:59 +0000430
431 // Returns the field descriptor if the oneof is set. NULL otherwise.
432 // TODO(jieluo) - make it pure virtual.
433 virtual const FieldDescriptor* GetOneofFieldDescriptor(
Austin Schuh918e3ee2014-10-31 16:27:55 -0700434 const Message& /*message*/,
435 const OneofDescriptor* /*oneof_descriptor*/) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000436 return NULL;
437 }
438
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000439 // Removes the last element of a repeated field.
kenton@google.comceb561d2009-06-25 19:05:36 +0000440 // We don't provide a way to remove any element other than the last
441 // because it invites inefficient use, such as O(n^2) filtering loops
442 // that should have been O(n). If you want to remove an element other
443 // than the last, the best way to do it is to re-arrange the elements
444 // (using Swap()) so that the one you want removed is at the end, then
445 // call RemoveLast().
446 virtual void RemoveLast(Message* message,
447 const FieldDescriptor* field) const = 0;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000448 // Removes the last element of a repeated message field, and returns the
449 // pointer to the caller. Caller takes ownership of the returned pointer.
450 virtual Message* ReleaseLast(Message* message,
451 const FieldDescriptor* field) const = 0;
kenton@google.comceb561d2009-06-25 19:05:36 +0000452
453 // Swap the complete contents of two messages.
454 virtual void Swap(Message* message1, Message* message2) const = 0;
455
jieluo@google.com4de8f552014-07-18 00:47:59 +0000456 // Swap fields listed in fields vector of two messages.
457 virtual void SwapFields(Message* message1,
458 Message* message2,
Jisi Liu885b6122015-02-28 14:51:22 -0800459 const std::vector<const FieldDescriptor*>& fields)
jieluo@google.com4de8f552014-07-18 00:47:59 +0000460 const = 0;
461
kenton@google.comceb561d2009-06-25 19:05:36 +0000462 // Swap two elements of a repeated field.
463 virtual void SwapElements(Message* message,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000464 const FieldDescriptor* field,
465 int index1,
466 int index2) const = 0;
kenton@google.comceb561d2009-06-25 19:05:36 +0000467
temporal40ee5512008-07-10 02:12:20 +0000468 // List all fields of the message which are currently set. This includes
469 // extensions. Singular fields will only be listed if HasField(field) would
470 // return true and repeated fields will only be listed if FieldSize(field)
471 // would return non-zero. Fields (both normal fields and extension fields)
472 // will be listed ordered by field number.
Jisi Liu885b6122015-02-28 14:51:22 -0800473 virtual void ListFields(
474 const Message& message,
475 std::vector<const FieldDescriptor*>* output) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000476
477 // Singular field getters ------------------------------------------
478 // These get the value of a non-repeated field. They return the default
479 // value for fields that aren't set.
480
temporal779f61c2008-08-13 03:15:00 +0000481 virtual int32 GetInt32 (const Message& message,
482 const FieldDescriptor* field) const = 0;
483 virtual int64 GetInt64 (const Message& message,
484 const FieldDescriptor* field) const = 0;
485 virtual uint32 GetUInt32(const Message& message,
486 const FieldDescriptor* field) const = 0;
487 virtual uint64 GetUInt64(const Message& message,
488 const FieldDescriptor* field) const = 0;
489 virtual float GetFloat (const Message& message,
490 const FieldDescriptor* field) const = 0;
491 virtual double GetDouble(const Message& message,
492 const FieldDescriptor* field) const = 0;
493 virtual bool GetBool (const Message& message,
494 const FieldDescriptor* field) const = 0;
495 virtual string GetString(const Message& message,
496 const FieldDescriptor* field) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000497 virtual const EnumValueDescriptor* GetEnum(
temporal779f61c2008-08-13 03:15:00 +0000498 const Message& message, const FieldDescriptor* field) const = 0;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800499
500 // GetEnumValue() returns an enum field's value as an integer rather than
501 // an EnumValueDescriptor*. If the integer value does not correspond to a
502 // known value descriptor, a new value descriptor is created. (Such a value
503 // will only be present when the new unknown-enum-value semantics are enabled
504 // for a message.)
505 virtual int GetEnumValue(
506 const Message& message, const FieldDescriptor* field) const;
507
kenton@google.comfccb1462009-12-18 02:11:36 +0000508 // See MutableMessage() for the meaning of the "factory" parameter.
temporal779f61c2008-08-13 03:15:00 +0000509 virtual const Message& GetMessage(const Message& message,
kenton@google.comfccb1462009-12-18 02:11:36 +0000510 const FieldDescriptor* field,
511 MessageFactory* factory = NULL) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000512
513 // Get a string value without copying, if possible.
514 //
515 // GetString() necessarily returns a copy of the string. This can be
516 // inefficient when the string is already stored in a string object in the
517 // underlying message. GetStringReference() will return a reference to the
518 // underlying string in this case. Otherwise, it will copy the string into
519 // *scratch and return that.
520 //
521 // Note: It is perfectly reasonable and useful to write code like:
522 // str = reflection->GetStringReference(field, &str);
523 // This line would ensure that only one copy of the string is made
524 // regardless of the field's underlying representation. When initializing
525 // a newly-constructed string, though, it's just as fast and more readable
526 // to use code like:
Jisi Liu885b6122015-02-28 14:51:22 -0800527 // string str = reflection->GetString(message, field);
temporal779f61c2008-08-13 03:15:00 +0000528 virtual const string& GetStringReference(const Message& message,
529 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000530 string* scratch) const = 0;
531
532
533 // Singular field mutators -----------------------------------------
534 // These mutate the value of a non-repeated field.
535
temporal779f61c2008-08-13 03:15:00 +0000536 virtual void SetInt32 (Message* message,
537 const FieldDescriptor* field, int32 value) const = 0;
538 virtual void SetInt64 (Message* message,
539 const FieldDescriptor* field, int64 value) const = 0;
540 virtual void SetUInt32(Message* message,
541 const FieldDescriptor* field, uint32 value) const = 0;
542 virtual void SetUInt64(Message* message,
543 const FieldDescriptor* field, uint64 value) const = 0;
544 virtual void SetFloat (Message* message,
545 const FieldDescriptor* field, float value) const = 0;
546 virtual void SetDouble(Message* message,
547 const FieldDescriptor* field, double value) const = 0;
548 virtual void SetBool (Message* message,
549 const FieldDescriptor* field, bool value) const = 0;
550 virtual void SetString(Message* message,
551 const FieldDescriptor* field,
552 const string& value) const = 0;
553 virtual void SetEnum (Message* message,
554 const FieldDescriptor* field,
555 const EnumValueDescriptor* value) const = 0;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800556 // Set an enum field's value with an integer rather than EnumValueDescriptor.
557 // If the value does not correspond to a known enum value, either behavior is
558 // undefined (for proto2 messages), or the value is accepted silently for
559 // messages with new unknown-enum-value semantics.
560 virtual void SetEnumValue(Message* message,
561 const FieldDescriptor* field,
562 int value) const;
563
kenton@google.comfccb1462009-12-18 02:11:36 +0000564 // Get a mutable pointer to a field with a message type. If a MessageFactory
565 // is provided, it will be used to construct instances of the sub-message;
566 // otherwise, the default factory is used. If the field is an extension that
567 // does not live in the same pool as the containing message's descriptor (e.g.
568 // it lives in an overlay pool), then a MessageFactory must be provided.
569 // If you have no idea what that meant, then you probably don't need to worry
570 // about it (don't provide a MessageFactory). WARNING: If the
571 // FieldDescriptor is for a compiled-in extension, then
572 // factory->GetPrototype(field->message_type() MUST return an instance of the
573 // compiled-in class for this type, NOT DynamicMessage.
temporal779f61c2008-08-13 03:15:00 +0000574 virtual Message* MutableMessage(Message* message,
kenton@google.comfccb1462009-12-18 02:11:36 +0000575 const FieldDescriptor* field,
576 MessageFactory* factory = NULL) const = 0;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000577 // Replaces the message specified by 'field' with the already-allocated object
578 // sub_message, passing ownership to the message. If the field contained a
579 // message, that message is deleted. If sub_message is NULL, the field is
580 // cleared.
581 virtual void SetAllocatedMessage(Message* message,
582 Message* sub_message,
583 const FieldDescriptor* field) const = 0;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000584 // Releases the message specified by 'field' and returns the pointer,
585 // ReleaseMessage() will return the message the message object if it exists.
586 // Otherwise, it may or may not return NULL. In any case, if the return value
587 // is non-NULL, the caller takes ownership of the pointer.
588 // If the field existed (HasField() is true), then the returned pointer will
589 // be the same as the pointer returned by MutableMessage().
590 // This function has the same effect as ClearField().
591 virtual Message* ReleaseMessage(Message* message,
592 const FieldDescriptor* field,
593 MessageFactory* factory = NULL) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000594
595
596 // Repeated field getters ------------------------------------------
597 // These get the value of one element of a repeated field.
598
temporal779f61c2008-08-13 03:15:00 +0000599 virtual int32 GetRepeatedInt32 (const Message& message,
600 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000601 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000602 virtual int64 GetRepeatedInt64 (const Message& message,
603 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000604 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000605 virtual uint32 GetRepeatedUInt32(const Message& message,
606 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000607 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000608 virtual uint64 GetRepeatedUInt64(const Message& message,
609 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000610 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000611 virtual float GetRepeatedFloat (const Message& message,
612 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000613 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000614 virtual double GetRepeatedDouble(const Message& message,
615 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000616 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000617 virtual bool GetRepeatedBool (const Message& message,
618 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000619 int index) const = 0;
temporal779f61c2008-08-13 03:15:00 +0000620 virtual string GetRepeatedString(const Message& message,
621 const FieldDescriptor* field,
temporal40ee5512008-07-10 02:12:20 +0000622 int index) const = 0;
623 virtual const EnumValueDescriptor* GetRepeatedEnum(
temporal779f61c2008-08-13 03:15:00 +0000624 const Message& message,
temporal40ee5512008-07-10 02:12:20 +0000625 const FieldDescriptor* field, int index) const = 0;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800626 // GetRepeatedEnumValue() returns an enum field's value as an integer rather
627 // than an EnumValueDescriptor*. If the integer value does not correspond to a
628 // known value descriptor, a new value descriptor is created. (Such a value
629 // will only be present when the new unknown-enum-value semantics are enabled
630 // for a message.)
631 virtual int GetRepeatedEnumValue(
632 const Message& message,
633 const FieldDescriptor* field, int index) const;
temporal40ee5512008-07-10 02:12:20 +0000634 virtual const Message& GetRepeatedMessage(
temporal779f61c2008-08-13 03:15:00 +0000635 const Message& message,
temporal40ee5512008-07-10 02:12:20 +0000636 const FieldDescriptor* field, int index) const = 0;
637
638 // See GetStringReference(), above.
639 virtual const string& GetRepeatedStringReference(
temporal779f61c2008-08-13 03:15:00 +0000640 const Message& message, const FieldDescriptor* field,
641 int index, string* scratch) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000642
643
644 // Repeated field mutators -----------------------------------------
645 // These mutate the value of one element of a repeated field.
646
temporal779f61c2008-08-13 03:15:00 +0000647 virtual void SetRepeatedInt32 (Message* message,
648 const FieldDescriptor* field,
649 int index, int32 value) const = 0;
650 virtual void SetRepeatedInt64 (Message* message,
651 const FieldDescriptor* field,
652 int index, int64 value) const = 0;
653 virtual void SetRepeatedUInt32(Message* message,
654 const FieldDescriptor* field,
655 int index, uint32 value) const = 0;
656 virtual void SetRepeatedUInt64(Message* message,
657 const FieldDescriptor* field,
658 int index, uint64 value) const = 0;
659 virtual void SetRepeatedFloat (Message* message,
660 const FieldDescriptor* field,
661 int index, float value) const = 0;
662 virtual void SetRepeatedDouble(Message* message,
663 const FieldDescriptor* field,
664 int index, double value) const = 0;
665 virtual void SetRepeatedBool (Message* message,
666 const FieldDescriptor* field,
667 int index, bool value) const = 0;
668 virtual void SetRepeatedString(Message* message,
669 const FieldDescriptor* field,
670 int index, const string& value) const = 0;
671 virtual void SetRepeatedEnum(Message* message,
672 const FieldDescriptor* field, int index,
673 const EnumValueDescriptor* value) const = 0;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800674 // Set an enum field's value with an integer rather than EnumValueDescriptor.
675 // If the value does not correspond to a known enum value, either behavior is
676 // undefined (for proto2 messages), or the value is accepted silently for
677 // messages with new unknown-enum-value semantics.
678 virtual void SetRepeatedEnumValue(Message* message,
679 const FieldDescriptor* field, int index,
680 int value) const;
temporal40ee5512008-07-10 02:12:20 +0000681 // Get a mutable pointer to an element of a repeated field with a message
682 // type.
683 virtual Message* MutableRepeatedMessage(
temporal779f61c2008-08-13 03:15:00 +0000684 Message* message, const FieldDescriptor* field, int index) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000685
686
687 // Repeated field adders -------------------------------------------
688 // These add an element to a repeated field.
689
temporal779f61c2008-08-13 03:15:00 +0000690 virtual void AddInt32 (Message* message,
691 const FieldDescriptor* field, int32 value) const = 0;
692 virtual void AddInt64 (Message* message,
693 const FieldDescriptor* field, int64 value) const = 0;
694 virtual void AddUInt32(Message* message,
695 const FieldDescriptor* field, uint32 value) const = 0;
696 virtual void AddUInt64(Message* message,
697 const FieldDescriptor* field, uint64 value) const = 0;
698 virtual void AddFloat (Message* message,
699 const FieldDescriptor* field, float value) const = 0;
700 virtual void AddDouble(Message* message,
701 const FieldDescriptor* field, double value) const = 0;
702 virtual void AddBool (Message* message,
703 const FieldDescriptor* field, bool value) const = 0;
704 virtual void AddString(Message* message,
705 const FieldDescriptor* field,
706 const string& value) const = 0;
707 virtual void AddEnum (Message* message,
708 const FieldDescriptor* field,
709 const EnumValueDescriptor* value) const = 0;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800710 // Set an enum field's value with an integer rather than EnumValueDescriptor.
711 // If the value does not correspond to a known enum value, either behavior is
712 // undefined (for proto2 messages), or the value is accepted silently for
713 // messages with new unknown-enum-value semantics.
714 virtual void AddEnumValue(Message* message,
715 const FieldDescriptor* field,
716 int value) const;
kenton@google.comfccb1462009-12-18 02:11:36 +0000717 // See MutableMessage() for comments on the "factory" parameter.
temporal779f61c2008-08-13 03:15:00 +0000718 virtual Message* AddMessage(Message* message,
kenton@google.comfccb1462009-12-18 02:11:36 +0000719 const FieldDescriptor* field,
720 MessageFactory* factory = NULL) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000721
722
Feng Xiao6ef984a2014-11-10 17:34:54 -0800723 // Get a RepeatedFieldRef object that can be used to read the underlying
724 // repeated field. The type parameter T must be set according to the
725 // field's cpp type. The following table shows the mapping from cpp type
726 // to acceptable T.
727 //
728 // field->cpp_type() T
729 // CPPTYPE_INT32 int32
730 // CPPTYPE_UINT32 uint32
731 // CPPTYPE_INT64 int64
732 // CPPTYPE_UINT64 uint64
733 // CPPTYPE_DOUBLE double
734 // CPPTYPE_FLOAT float
735 // CPPTYPE_BOOL bool
736 // CPPTYPE_ENUM generated enum type or int32
737 // CPPTYPE_STRING string
738 // CPPTYPE_MESSAGE generated message type or google::protobuf::Message
739 //
740 // A RepeatedFieldRef object can be copied and the resulted object will point
741 // to the same repeated field in the same message. The object can be used as
742 // long as the message is not destroyed.
743 //
744 // Note that to use this method users need to include the header file
745 // "google/protobuf/reflection.h" (which defines the RepeatedFieldRef
746 // class templates).
747 template<typename T>
748 RepeatedFieldRef<T> GetRepeatedFieldRef(
749 const Message& message, const FieldDescriptor* field) const;
750
751 // Like GetRepeatedFieldRef() but return an object that can also be used
752 // manipulate the underlying repeated field.
753 template<typename T>
754 MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
755 Message* message, const FieldDescriptor* field) const;
756
757 // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field
758 // access. The following repeated field accesors will be removed in the
759 // future.
760 //
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000761 // Repeated field accessors -------------------------------------------------
762 // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular
763 // access to the data in a RepeatedField. The methods below provide aggregate
764 // access by exposing the RepeatedField object itself with the Message.
765 // Applying these templates to inappropriate types will lead to an undefined
766 // reference at link time (e.g. GetRepeatedField<***double>), or possibly a
767 // template matching error at compile time (e.g. GetRepeatedPtrField<File>).
768 //
769 // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd);
770
Feng Xiao6ef984a2014-11-10 17:34:54 -0800771 // DEPRECATED. Please use GetRepeatedFieldRef().
772 //
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000773 // for T = Cord and all protobuf scalar types except enums.
774 template<typename T>
775 const RepeatedField<T>& GetRepeatedField(
776 const Message&, const FieldDescriptor*) const;
777
Feng Xiao6ef984a2014-11-10 17:34:54 -0800778 // DEPRECATED. Please use GetMutableRepeatedFieldRef().
779 //
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000780 // for T = Cord and all protobuf scalar types except enums.
781 template<typename T>
782 RepeatedField<T>* MutableRepeatedField(
783 Message*, const FieldDescriptor*) const;
784
Feng Xiao6ef984a2014-11-10 17:34:54 -0800785 // DEPRECATED. Please use GetRepeatedFieldRef().
786 //
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000787 // for T = string, google::protobuf::internal::StringPieceField
788 // google::protobuf::Message & descendants.
789 template<typename T>
790 const RepeatedPtrField<T>& GetRepeatedPtrField(
791 const Message&, const FieldDescriptor*) const;
792
Feng Xiao6ef984a2014-11-10 17:34:54 -0800793 // DEPRECATED. Please use GetMutableRepeatedFieldRef().
794 //
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000795 // for T = string, google::protobuf::internal::StringPieceField
796 // google::protobuf::Message & descendants.
797 template<typename T>
798 RepeatedPtrField<T>* MutableRepeatedPtrField(
799 Message*, const FieldDescriptor*) const;
800
801 // Extensions ----------------------------------------------------------------
temporal40ee5512008-07-10 02:12:20 +0000802
803 // Try to find an extension of this message type by fully-qualified field
804 // name. Returns NULL if no extension is known for this name or number.
805 virtual const FieldDescriptor* FindKnownExtensionByName(
806 const string& name) const = 0;
807
808 // Try to find an extension of this message type by field number.
809 // Returns NULL if no extension is known for this name or number.
810 virtual const FieldDescriptor* FindKnownExtensionByNumber(
811 int number) const = 0;
812
Feng Xiao6ef984a2014-11-10 17:34:54 -0800813 // Feature Flags -------------------------------------------------------------
814
815 // Does this message support storing arbitrary integer values in enum fields?
816 // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions
817 // take arbitrary integer values, and the legacy GetEnum() getter will
818 // dynamically create an EnumValueDescriptor for any integer value without
819 // one. If |false|, setting an unknown enum value via the integer-based
820 // setters results in undefined behavior (in practice, GOOGLE_DCHECK-fails).
821 //
822 // Generic code that uses reflection to handle messages with enum fields
823 // should check this flag before using the integer-based setter, and either
824 // downgrade to a compatible value or use the UnknownFieldSet if not. For
825 // example:
826 //
827 // int new_value = GetValueFromApplicationLogic();
828 // if (reflection->SupportsUnknownEnumValues()) {
829 // reflection->SetEnumValue(message, field, new_value);
830 // } else {
831 // if (field_descriptor->enum_type()->
832 // FindValueByNumver(new_value) != NULL) {
833 // reflection->SetEnumValue(message, field, new_value);
834 // } else if (emit_unknown_enum_values) {
835 // reflection->MutableUnknownFields(message)->AddVarint(
836 // field->number(),
837 // new_value);
838 // } else {
839 // // convert value to a compatible/default value.
840 // new_value = CompatibleDowngrade(new_value);
841 // reflection->SetEnumValue(message, field, new_value);
842 // }
843 // }
844 virtual bool SupportsUnknownEnumValues() const { return false; }
845
Jisi Liu885b6122015-02-28 14:51:22 -0800846 // Returns the MessageFactory associated with this message. This can be
847 // useful for determining if a message is a generated message or not, for
848 // example:
849 //
850 // if (message->GetReflection()->GetMessageFactory() ==
851 // google::protobuf::MessageFactory::generated_factory()) {
852 // // This is a generated message.
853 // }
854 //
855 // It can also be used to create more messages of this type, though
856 // Message::New() is an easier way to accomplish this.
857 virtual MessageFactory* GetMessageFactory() const;
858
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000859 // ---------------------------------------------------------------------------
860
861 protected:
862 // Obtain a pointer to a Repeated Field Structure and do some type checking:
863 // on field->cpp_type(),
864 // on field->field_option().ctype() (if ctype >= 0)
865 // of field->message_type() (if message_type != NULL).
866 // We use 1 routine rather than 4 (const vs mutable) x (scalar vs pointer).
867 virtual void* MutableRawRepeatedField(
868 Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
869 int ctype, const Descriptor* message_type) const = 0;
870
Feng Xiao6ef984a2014-11-10 17:34:54 -0800871 // The following methods are used to implement (Mutable)RepeatedFieldRef.
872 // A Ref object will store a raw pointer to the repeated field data (obtained
873 // from RepeatedFieldData()) and a pointer to a Accessor (obtained from
874 // RepeatedFieldAccessor) which will be used to access the raw data.
875 //
876 // TODO(xiaofeng): Make these methods pure-virtual.
877
878 // Returns a raw pointer to the repeated field
879 //
880 // "cpp_type" and "message_type" are decuded from the type parameter T passed
881 // to Get(Mutable)RepeatedFieldRef. If T is a generated message type,
882 // "message_type" should be set to its descriptor. Otherwise "message_type"
883 // should be set to NULL. Implementations of this method should check whether
884 // "cpp_type"/"message_type" is consistent with the actual type of the field.
885 virtual void* RepeatedFieldData(
886 Message* message, const FieldDescriptor* field,
887 FieldDescriptor::CppType cpp_type,
888 const Descriptor* message_type) const;
889
890 // The returned pointer should point to a singleton instance which implements
891 // the RepeatedFieldAccessor interface.
892 virtual const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
893 const FieldDescriptor* field) const;
894
temporal40ee5512008-07-10 02:12:20 +0000895 private:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800896 template<typename T, typename Enable>
897 friend class RepeatedFieldRef;
898 template<typename T, typename Enable>
899 friend class MutableRepeatedFieldRef;
900
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000901 // Special version for specialized implementations of string. We can't call
902 // MutableRawRepeatedField directly here because we don't have access to
903 // FieldOptions::* which are defined in descriptor.pb.h. Including that
904 // file here is not possible because it would cause a circular include cycle.
905 void* MutableRawRepeatedString(
906 Message* message, const FieldDescriptor* field, bool is_string) const;
907
temporal40ee5512008-07-10 02:12:20 +0000908 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection);
909};
910
911// Abstract interface for a factory for message objects.
912class LIBPROTOBUF_EXPORT MessageFactory {
913 public:
914 inline MessageFactory() {}
915 virtual ~MessageFactory();
916
917 // Given a Descriptor, gets or constructs the default (prototype) Message
918 // of that type. You can then call that message's New() method to construct
919 // a mutable message of that type.
920 //
921 // Calling this method twice with the same Descriptor returns the same
922 // object. The returned object remains property of the factory. Also, any
923 // objects created by calling the prototype's New() method share some data
jieluo@google.com4de8f552014-07-18 00:47:59 +0000924 // with the prototype, so these must be destroyed before the MessageFactory
temporal40ee5512008-07-10 02:12:20 +0000925 // is destroyed.
926 //
927 // The given descriptor must outlive the returned message, and hence must
928 // outlive the MessageFactory.
929 //
930 // Some implementations do not support all types. GetPrototype() will
931 // return NULL if the descriptor passed in is not supported.
932 //
933 // This method may or may not be thread-safe depending on the implementation.
934 // Each implementation should document its own degree thread-safety.
935 virtual const Message* GetPrototype(const Descriptor* type) = 0;
936
937 // Gets a MessageFactory which supports all generated, compiled-in messages.
938 // In other words, for any compiled-in type FooMessage, the following is true:
939 // MessageFactory::generated_factory()->GetPrototype(
940 // FooMessage::descriptor()) == FooMessage::default_instance()
941 // This factory supports all types which are found in
942 // DescriptorPool::generated_pool(). If given a descriptor from any other
943 // pool, GetPrototype() will return NULL. (You can also check if a
944 // descriptor is for a generated message by checking if
945 // descriptor->file()->pool() == DescriptorPool::generated_pool().)
946 //
947 // This factory is 100% thread-safe; calling GetPrototype() does not modify
948 // any shared data.
949 //
950 // This factory is a singleton. The caller must not delete the object.
951 static MessageFactory* generated_factory();
952
kenton@google.comd37d46d2009-04-25 02:53:47 +0000953 // For internal use only: Registers a .proto file at static initialization
954 // time, to be placed in generated_factory. The first time GetPrototype()
955 // is called with a descriptor from this file, |register_messages| will be
kenton@google.com80b1d622009-07-29 01:13:20 +0000956 // called, with the file name as the parameter. It must call
957 // InternalRegisterGeneratedMessage() (below) to register each message type
958 // in the file. This strange mechanism is necessary because descriptors are
959 // built lazily, so we can't register types by their descriptor until we
960 // know that the descriptor exists. |filename| must be a permanent string.
961 static void InternalRegisterGeneratedFile(
962 const char* filename, void (*register_messages)(const string&));
kenton@google.comd37d46d2009-04-25 02:53:47 +0000963
964 // For internal use only: Registers a message type. Called only by the
965 // functions which are registered with InternalRegisterGeneratedFile(),
966 // above.
temporal40ee5512008-07-10 02:12:20 +0000967 static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
968 const Message* prototype);
969
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000970
temporal40ee5512008-07-10 02:12:20 +0000971 private:
972 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory);
973};
974
xiaofeng@google.com1e5a5e82013-01-31 15:52:58 +0000975#define DECLARE_GET_REPEATED_FIELD(TYPE) \
976template<> \
977LIBPROTOBUF_EXPORT \
978const RepeatedField<TYPE>& Reflection::GetRepeatedField<TYPE>( \
979 const Message& message, const FieldDescriptor* field) const; \
980 \
981template<> \
Feng Xiao8d5d7cc2014-12-09 17:05:10 -0800982LIBPROTOBUF_EXPORT \
xiaofeng@google.com1e5a5e82013-01-31 15:52:58 +0000983RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
984 Message* message, const FieldDescriptor* field) const;
985
986DECLARE_GET_REPEATED_FIELD(int32)
987DECLARE_GET_REPEATED_FIELD(int64)
988DECLARE_GET_REPEATED_FIELD(uint32)
989DECLARE_GET_REPEATED_FIELD(uint64)
990DECLARE_GET_REPEATED_FIELD(float)
991DECLARE_GET_REPEATED_FIELD(double)
992DECLARE_GET_REPEATED_FIELD(bool)
993
994#undef DECLARE_GET_REPEATED_FIELD
995
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000996// =============================================================================
997// Implementation details for {Get,Mutable}RawRepeatedPtrField. We provide
998// specializations for <string>, <StringPieceField> and <Message> and handle
999// everything else with the default template which will match any type having
1000// a method with signature "static const google::protobuf::Descriptor* descriptor()".
1001// Such a type presumably is a descendant of google::protobuf::Message.
1002
1003template<>
1004inline const RepeatedPtrField<string>& Reflection::GetRepeatedPtrField<string>(
1005 const Message& message, const FieldDescriptor* field) const {
1006 return *static_cast<RepeatedPtrField<string>* >(
1007 MutableRawRepeatedString(const_cast<Message*>(&message), field, true));
1008}
1009
1010template<>
1011inline RepeatedPtrField<string>* Reflection::MutableRepeatedPtrField<string>(
1012 Message* message, const FieldDescriptor* field) const {
1013 return static_cast<RepeatedPtrField<string>* >(
1014 MutableRawRepeatedString(message, field, true));
1015}
1016
1017
1018// -----
1019
1020template<>
1021inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField(
1022 const Message& message, const FieldDescriptor* field) const {
1023 return *static_cast<RepeatedPtrField<Message>* >(
1024 MutableRawRepeatedField(const_cast<Message*>(&message), field,
1025 FieldDescriptor::CPPTYPE_MESSAGE, -1,
1026 NULL));
1027}
1028
1029template<>
1030inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField(
1031 Message* message, const FieldDescriptor* field) const {
1032 return static_cast<RepeatedPtrField<Message>* >(
1033 MutableRawRepeatedField(message, field,
1034 FieldDescriptor::CPPTYPE_MESSAGE, -1,
1035 NULL));
1036}
1037
1038template<typename PB>
1039inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField(
1040 const Message& message, const FieldDescriptor* field) const {
1041 return *static_cast<RepeatedPtrField<PB>* >(
1042 MutableRawRepeatedField(const_cast<Message*>(&message), field,
1043 FieldDescriptor::CPPTYPE_MESSAGE, -1,
1044 PB::default_instance().GetDescriptor()));
1045}
1046
1047template<typename PB>
1048inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField(
1049 Message* message, const FieldDescriptor* field) const {
1050 return static_cast<RepeatedPtrField<PB>* >(
1051 MutableRawRepeatedField(message, field,
1052 FieldDescriptor::CPPTYPE_MESSAGE, -1,
1053 PB::default_instance().GetDescriptor()));
1054}
temporal40ee5512008-07-10 02:12:20 +00001055} // namespace protobuf
1056
1057} // namespace google
1058#endif // GOOGLE_PROTOBUF_MESSAGE_H__