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