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