/src/LPM/external.protobuf/include/google/protobuf/message.h
Line | Count | Source (jump to first uncovered line) |
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/common.h" |
119 | | #include "google/protobuf/arena.h" |
120 | | #include "google/protobuf/port.h" |
121 | | #include "absl/base/call_once.h" |
122 | | #include "absl/base/casts.h" |
123 | | #include "absl/functional/function_ref.h" |
124 | | #include "absl/strings/string_view.h" |
125 | | #include "google/protobuf/descriptor.h" |
126 | | #include "google/protobuf/generated_message_reflection.h" |
127 | | #include "google/protobuf/generated_message_tctable_decl.h" |
128 | | #include "google/protobuf/generated_message_util.h" |
129 | | #include "google/protobuf/map.h" // TODO(b/211442718): cleanup |
130 | | #include "google/protobuf/message_lite.h" |
131 | | #include "google/protobuf/port.h" |
132 | | |
133 | | |
134 | | // Must be included last. |
135 | | #include "google/protobuf/port_def.inc" |
136 | | |
137 | | #ifdef SWIG |
138 | | #error "You cannot SWIG proto headers" |
139 | | #endif |
140 | | |
141 | | namespace google { |
142 | | namespace protobuf { |
143 | | |
144 | | // Defined in this file. |
145 | | class Message; |
146 | | class Reflection; |
147 | | class MessageFactory; |
148 | | |
149 | | // Defined in other files. |
150 | | class AssignDescriptorsHelper; |
151 | | class DynamicMessageFactory; |
152 | | class GeneratedMessageReflectionTestHelper; |
153 | | class MapKey; |
154 | | class MapValueConstRef; |
155 | | class MapValueRef; |
156 | | class MapIterator; |
157 | | class MapReflectionTester; |
158 | | |
159 | | namespace internal { |
160 | | struct FuzzPeer; |
161 | | struct DescriptorTable; |
162 | | class MapFieldBase; |
163 | | class SwapFieldHelper; |
164 | | class CachedSize; |
165 | | struct TailCallTableInfo; |
166 | | } // namespace internal |
167 | | class UnknownFieldSet; // unknown_field_set.h |
168 | | namespace io { |
169 | | class ZeroCopyInputStream; // zero_copy_stream.h |
170 | | class ZeroCopyOutputStream; // zero_copy_stream.h |
171 | | class CodedInputStream; // coded_stream.h |
172 | | class CodedOutputStream; // coded_stream.h |
173 | | } // namespace io |
174 | | namespace python { |
175 | | class MapReflectionFriend; // scalar_map_container.h |
176 | | class MessageReflectionFriend; |
177 | | } // namespace python |
178 | | namespace expr { |
179 | | class CelMapReflectionFriend; // field_backed_map_impl.cc |
180 | | } |
181 | | |
182 | | namespace internal { |
183 | | class MapFieldPrinterHelper; // text_format.cc |
184 | | void PerformAbslStringify( |
185 | | const Message& message, |
186 | | absl::FunctionRef<void(absl::string_view)> append); // text_format.cc |
187 | | } // namespace internal |
188 | | namespace util { |
189 | | class MessageDifferencer; |
190 | | } |
191 | | |
192 | | |
193 | | namespace internal { |
194 | | class ReflectionAccessor; // message.cc |
195 | | class ReflectionOps; // reflection_ops.h |
196 | | class MapKeySorter; // wire_format.cc |
197 | | class WireFormat; // wire_format.h |
198 | | class MapFieldReflectionTest; // map_test.cc |
199 | | } // namespace internal |
200 | | |
201 | | template <typename T> |
202 | | class RepeatedField; // repeated_field.h |
203 | | |
204 | | template <typename T> |
205 | | class RepeatedPtrField; // repeated_field.h |
206 | | |
207 | | // A container to hold message metadata. |
208 | | struct Metadata { |
209 | | const Descriptor* descriptor; |
210 | | const Reflection* reflection; |
211 | | }; |
212 | | |
213 | | namespace internal { |
214 | | template <class To> |
215 | 0 | inline To* GetPointerAtOffset(void* message, uint32_t offset) { |
216 | 0 | return reinterpret_cast<To*>(reinterpret_cast<char*>(message) + offset); |
217 | 0 | } |
218 | | |
219 | | template <class To> |
220 | 0 | const To* GetConstPointerAtOffset(const void* message, uint32_t offset) { |
221 | 0 | return reinterpret_cast<const To*>(reinterpret_cast<const char*>(message) + |
222 | 0 | offset); |
223 | 0 | } Unexecuted instantiation: unsigned int const* google::protobuf::internal::GetConstPointerAtOffset<unsigned int>(void const*, unsigned int) Unexecuted instantiation: void* const* google::protobuf::internal::GetConstPointerAtOffset<void*>(void const*, unsigned int) |
224 | | |
225 | | template <class To> |
226 | 0 | const To& GetConstRefAtOffset(const Message& message, uint32_t offset) { |
227 | 0 | return *GetConstPointerAtOffset<To>(&message, offset); |
228 | 0 | } |
229 | | |
230 | | bool CreateUnknownEnumValues(const FieldDescriptor* field); |
231 | | |
232 | | // Returns true if "message" is a descendant of "root". |
233 | | PROTOBUF_EXPORT bool IsDescendant(Message& root, const Message& message); |
234 | | } // namespace internal |
235 | | |
236 | | // Abstract interface for protocol messages. |
237 | | // |
238 | | // See also MessageLite, which contains most every-day operations. Message |
239 | | // adds descriptors and reflection on top of that. |
240 | | // |
241 | | // The methods of this class that are virtual but not pure-virtual have |
242 | | // default implementations based on reflection. Message classes which are |
243 | | // optimized for speed will want to override these with faster implementations, |
244 | | // but classes optimized for code size may be happy with keeping them. See |
245 | | // the optimize_for option in descriptor.proto. |
246 | | // |
247 | | // Users must not derive from this class. Only the protocol compiler and |
248 | | // the internal library are allowed to create subclasses. |
249 | | class PROTOBUF_EXPORT Message : public MessageLite { |
250 | | public: |
251 | 0 | constexpr Message() {} |
252 | | Message(const Message&) = delete; |
253 | | Message& operator=(const Message&) = delete; |
254 | | |
255 | | // Basic Operations ------------------------------------------------ |
256 | | |
257 | | // Construct a new instance of the same type. Ownership is passed to the |
258 | | // caller. (This is also defined in MessageLite, but is defined again here |
259 | | // for return-type covariance.) |
260 | 0 | Message* New() const { return New(nullptr); } |
261 | | |
262 | | // Construct a new instance on the arena. Ownership is passed to the caller |
263 | | // if arena is a nullptr. |
264 | | Message* New(Arena* arena) const override = 0; |
265 | | |
266 | | // Make this message into a copy of the given message. The given message |
267 | | // must have the same descriptor, but need not necessarily be the same class. |
268 | | // By default this is just implemented as "Clear(); MergeFrom(from);". |
269 | | void CopyFrom(const Message& from); |
270 | | |
271 | | // Merge the fields from the given message into this message. Singular |
272 | | // fields will be overwritten, if specified in from, except for embedded |
273 | | // messages which will be merged. Repeated fields will be concatenated. |
274 | | // The given message must be of the same type as this message (i.e. the |
275 | | // exact same class). |
276 | | virtual void MergeFrom(const Message& from); |
277 | | |
278 | | // Verifies that IsInitialized() returns true. ABSL_CHECK-fails otherwise, |
279 | | // with a nice error message. |
280 | | void CheckInitialized() const; |
281 | | |
282 | | // Slowly build a list of all required fields that are not set. |
283 | | // This is much, much slower than IsInitialized() as it is implemented |
284 | | // purely via reflection. Generally, you should not call this unless you |
285 | | // have already determined that an error exists by calling IsInitialized(). |
286 | | void FindInitializationErrors(std::vector<std::string>* errors) const; |
287 | | |
288 | | // Like FindInitializationErrors, but joins all the strings, delimited by |
289 | | // commas, and returns them. |
290 | | std::string InitializationErrorString() const override; |
291 | | |
292 | | // Clears all unknown fields from this message and all embedded messages. |
293 | | // Normally, if unknown tag numbers are encountered when parsing a message, |
294 | | // the tag and value are stored in the message's UnknownFieldSet and |
295 | | // then written back out when the message is serialized. This allows servers |
296 | | // which simply route messages to other servers to pass through messages |
297 | | // that have new field definitions which they don't yet know about. However, |
298 | | // this behavior can have security implications. To avoid it, call this |
299 | | // method after parsing. |
300 | | // |
301 | | // See Reflection::GetUnknownFields() for more on unknown fields. |
302 | | void DiscardUnknownFields(); |
303 | | |
304 | | // Computes (an estimate of) the total number of bytes currently used for |
305 | | // storing the message in memory. The default implementation calls the |
306 | | // Reflection object's SpaceUsed() method. |
307 | | // |
308 | | // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented |
309 | | // using reflection (rather than the generated code implementation for |
310 | | // ByteSize()). Like ByteSize(), its CPU time is linear in the number of |
311 | | // fields defined for the proto. |
312 | | // |
313 | | // Note: The precise value of this method should never be depended on, and can |
314 | | // change substantially due to internal details. In debug builds, this will |
315 | | // include a random fuzz factor to prevent these dependencies. |
316 | | virtual size_t SpaceUsedLong() const; |
317 | | |
318 | | PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") |
319 | 0 | int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); } |
320 | | |
321 | | // Debugging & Testing---------------------------------------------- |
322 | | |
323 | | // Generates a human-readable form of this message for debugging purposes. |
324 | | // Note that the format and content of a debug string is not guaranteed, may |
325 | | // change without notice, and should not be depended on. Code that does |
326 | | // anything except display a string to assist in debugging should use |
327 | | // TextFormat instead. |
328 | | std::string DebugString() const; |
329 | | // Like DebugString(), but with less whitespace. |
330 | | std::string ShortDebugString() const; |
331 | | // Like DebugString(), but do not escape UTF-8 byte sequences. |
332 | | std::string Utf8DebugString() const; |
333 | | // Convenience function useful in GDB. Prints DebugString() to stdout. |
334 | | void PrintDebugString() const; |
335 | | |
336 | | // Implementation of the `AbslStringify` interface. This adds something |
337 | | // similar to either `ShortDebugString()` or `DebugString()` to the sink. |
338 | | // Do not rely on exact format. |
339 | | template <typename Sink> |
340 | | friend void AbslStringify(Sink& sink, const google::protobuf::Message& message) { |
341 | | internal::PerformAbslStringify( |
342 | | message, [&](absl::string_view content) { sink.Append(content); }); |
343 | | } |
344 | | |
345 | | // Reflection-based methods ---------------------------------------- |
346 | | // These methods are pure-virtual in MessageLite, but Message provides |
347 | | // reflection-based default implementations. |
348 | | |
349 | | std::string GetTypeName() const override; |
350 | | void Clear() override; |
351 | | |
352 | | // Returns whether all required fields have been set. Note that required |
353 | | // fields no longer exist starting in proto3. |
354 | | bool IsInitialized() const override; |
355 | | |
356 | | void CheckTypeAndMergeFrom(const MessageLite& other) override; |
357 | | // Reflective parser |
358 | | const char* _InternalParse(const char* ptr, |
359 | | internal::ParseContext* ctx) override; |
360 | | size_t ByteSizeLong() const override; |
361 | | uint8_t* _InternalSerialize(uint8_t* target, |
362 | | io::EpsCopyOutputStream* stream) const override; |
363 | | |
364 | | private: |
365 | | // This is called only by the default implementation of ByteSize(), to |
366 | | // update the cached size. If you override ByteSize(), you do not need |
367 | | // to override this. If you do not override ByteSize(), you MUST override |
368 | | // this; the default implementation will crash. |
369 | | // |
370 | | // The method is private because subclasses should never call it; only |
371 | | // override it. Yes, C++ lets you do that. Crazy, huh? |
372 | | virtual void SetCachedSize(int size) const; |
373 | | |
374 | | public: |
375 | | // Introspection --------------------------------------------------- |
376 | | |
377 | | |
378 | | // Get a non-owning pointer to a Descriptor for this message's type. This |
379 | | // describes what fields the message contains, the types of those fields, etc. |
380 | | // This object remains property of the Message. |
381 | 0 | const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; } |
382 | | |
383 | | // Get a non-owning pointer to the Reflection interface for this Message, |
384 | | // which can be used to read and modify the fields of the Message dynamically |
385 | | // (in other words, without knowing the message type at compile time). This |
386 | | // object remains property of the Message. |
387 | 0 | const Reflection* GetReflection() const { return GetMetadata().reflection; } |
388 | | |
389 | | protected: |
390 | | // Get a struct containing the metadata for the Message, which is used in turn |
391 | | // to implement GetDescriptor() and GetReflection() above. |
392 | | virtual Metadata GetMetadata() const = 0; |
393 | | |
394 | | struct ClassData { |
395 | | // Note: The order of arguments (to, then from) is chosen so that the ABI |
396 | | // of this function is the same as the CopyFrom method. That is, the |
397 | | // hidden "this" parameter comes first. |
398 | | void (*copy_to_from)(Message& to, const Message& from_msg); |
399 | | void (*merge_to_from)(Message& to, const Message& from_msg); |
400 | | }; |
401 | | // GetClassData() returns a pointer to a ClassData struct which |
402 | | // exists in global memory and is unique to each subclass. This uniqueness |
403 | | // property is used in order to quickly determine whether two messages are |
404 | | // of the same type. |
405 | | // TODO(jorg): change to pure virtual |
406 | 0 | virtual const ClassData* GetClassData() const { return nullptr; } |
407 | | |
408 | | // CopyWithSourceCheck calls Clear() and then MergeFrom(), and in debug |
409 | | // builds, checks that calling Clear() on the destination message doesn't |
410 | | // alter the source. It assumes the messages are known to be of the same |
411 | | // type, and thus uses GetClassData(). |
412 | | static void CopyWithSourceCheck(Message& to, const Message& from); |
413 | | |
414 | 2.38M | inline explicit Message(Arena* arena) : MessageLite(arena) {} |
415 | | size_t ComputeUnknownFieldsSize(size_t total_size, |
416 | | internal::CachedSize* cached_size) const; |
417 | | size_t MaybeComputeUnknownFieldsSize(size_t total_size, |
418 | | internal::CachedSize* cached_size) const; |
419 | | |
420 | | |
421 | | protected: |
422 | | static uint64_t GetInvariantPerBuild(uint64_t salt); |
423 | | }; |
424 | | |
425 | | namespace internal { |
426 | | // Creates and returns an allocation for a split message. |
427 | | void* CreateSplitMessageGeneric(Arena* arena, const void* default_split, |
428 | | size_t size, const void* message, |
429 | | const void* default_message); |
430 | | |
431 | | // Forward-declare interfaces used to implement RepeatedFieldRef. |
432 | | // These are protobuf internals that users shouldn't care about. |
433 | | class RepeatedFieldAccessor; |
434 | | } // namespace internal |
435 | | |
436 | | // Forward-declare RepeatedFieldRef templates. The second type parameter is |
437 | | // used for SFINAE tricks. Users should ignore it. |
438 | | template <typename T, typename Enable = void> |
439 | | class RepeatedFieldRef; |
440 | | |
441 | | template <typename T, typename Enable = void> |
442 | | class MutableRepeatedFieldRef; |
443 | | |
444 | | // This interface contains methods that can be used to dynamically access |
445 | | // and modify the fields of a protocol message. Their semantics are |
446 | | // similar to the accessors the protocol compiler generates. |
447 | | // |
448 | | // To get the Reflection for a given Message, call Message::GetReflection(). |
449 | | // |
450 | | // This interface is separate from Message only for efficiency reasons; |
451 | | // the vast majority of implementations of Message will share the same |
452 | | // implementation of Reflection (GeneratedMessageReflection, |
453 | | // defined in generated_message.h), and all Messages of a particular class |
454 | | // should share the same Reflection object (though you should not rely on |
455 | | // the latter fact). |
456 | | // |
457 | | // There are several ways that these methods can be used incorrectly. For |
458 | | // example, any of the following conditions will lead to undefined |
459 | | // results (probably assertion failures): |
460 | | // - The FieldDescriptor is not a field of this message type. |
461 | | // - The method called is not appropriate for the field's type. For |
462 | | // each field type in FieldDescriptor::TYPE_*, there is only one |
463 | | // Get*() method, one Set*() method, and one Add*() method that is |
464 | | // valid for that type. It should be obvious which (except maybe |
465 | | // for TYPE_BYTES, which are represented using strings in C++). |
466 | | // - A Get*() or Set*() method for singular fields is called on a repeated |
467 | | // field. |
468 | | // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated |
469 | | // field. |
470 | | // - The Message object passed to any method is not of the right type for |
471 | | // this Reflection object (i.e. message.GetReflection() != reflection). |
472 | | // |
473 | | // You might wonder why there is not any abstract representation for a field |
474 | | // of arbitrary type. E.g., why isn't there just a "GetField()" method that |
475 | | // returns "const Field&", where "Field" is some class with accessors like |
476 | | // "GetInt32Value()". The problem is that someone would have to deal with |
477 | | // allocating these Field objects. For generated message classes, having to |
478 | | // allocate space for an additional object to wrap every field would at least |
479 | | // double the message's memory footprint, probably worse. Allocating the |
480 | | // objects on-demand, on the other hand, would be expensive and prone to |
481 | | // memory leaks. So, instead we ended up with this flat interface. |
482 | | class PROTOBUF_EXPORT Reflection final { |
483 | | public: |
484 | | Reflection(const Reflection&) = delete; |
485 | | Reflection& operator=(const Reflection&) = delete; |
486 | | ~Reflection(); |
487 | | |
488 | | // Get the UnknownFieldSet for the message. This contains fields which |
489 | | // were seen when the Message was parsed but were not recognized according |
490 | | // to the Message's definition. |
491 | | const UnknownFieldSet& GetUnknownFields(const Message& message) const; |
492 | | // Get a mutable pointer to the UnknownFieldSet for the message. This |
493 | | // contains fields which were seen when the Message was parsed but were not |
494 | | // recognized according to the Message's definition. |
495 | | UnknownFieldSet* MutableUnknownFields(Message* message) const; |
496 | | |
497 | | // Estimate the amount of memory used by the message object. |
498 | | size_t SpaceUsedLong(const Message& message) const; |
499 | | |
500 | | PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead") |
501 | 0 | int SpaceUsed(const Message& message) const { |
502 | 0 | return internal::ToIntSize(SpaceUsedLong(message)); |
503 | 0 | } |
504 | | |
505 | | // Returns true if the given message is a default message instance. |
506 | 0 | bool IsDefaultInstance(const Message& message) const { |
507 | 0 | return schema_.IsDefaultInstance(message); |
508 | 0 | } |
509 | | |
510 | | // Check if the given non-repeated field is set. |
511 | | bool HasField(const Message& message, const FieldDescriptor* field) const; |
512 | | |
513 | | // Get the number of elements of a repeated field. |
514 | | int FieldSize(const Message& message, const FieldDescriptor* field) const; |
515 | | |
516 | | // Clear the value of a field, so that HasField() returns false or |
517 | | // FieldSize() returns zero. |
518 | | void ClearField(Message* message, const FieldDescriptor* field) const; |
519 | | |
520 | | // Check if the oneof is set. Returns true if any field in oneof |
521 | | // is set, false otherwise. |
522 | | bool HasOneof(const Message& message, |
523 | | const OneofDescriptor* oneof_descriptor) const; |
524 | | |
525 | | void ClearOneof(Message* message, |
526 | | const OneofDescriptor* oneof_descriptor) const; |
527 | | |
528 | | // Returns the field descriptor if the oneof is set. nullptr otherwise. |
529 | | const FieldDescriptor* GetOneofFieldDescriptor( |
530 | | const Message& message, const OneofDescriptor* oneof_descriptor) const; |
531 | | |
532 | | // Removes the last element of a repeated field. |
533 | | // We don't provide a way to remove any element other than the last |
534 | | // because it invites inefficient use, such as O(n^2) filtering loops |
535 | | // that should have been O(n). If you want to remove an element other |
536 | | // than the last, the best way to do it is to re-arrange the elements |
537 | | // (using Swap()) so that the one you want removed is at the end, then |
538 | | // call RemoveLast(). |
539 | | void RemoveLast(Message* message, const FieldDescriptor* field) const; |
540 | | // Removes the last element of a repeated message field, and returns the |
541 | | // pointer to the caller. Caller takes ownership of the returned pointer. |
542 | | PROTOBUF_NODISCARD Message* ReleaseLast(Message* message, |
543 | | const FieldDescriptor* field) const; |
544 | | |
545 | | // Similar to ReleaseLast() without internal safety and ownershp checks. This |
546 | | // method should only be used when the objects are on the same arena or paired |
547 | | // with a call to `UnsafeArenaAddAllocatedMessage`. |
548 | | Message* UnsafeArenaReleaseLast(Message* message, |
549 | | const FieldDescriptor* field) const; |
550 | | |
551 | | // Swap the complete contents of two messages. |
552 | | void Swap(Message* message1, Message* message2) const; |
553 | | |
554 | | // Swap fields listed in fields vector of two messages. |
555 | | void SwapFields(Message* message1, Message* message2, |
556 | | const std::vector<const FieldDescriptor*>& fields) const; |
557 | | |
558 | | // Swap two elements of a repeated field. |
559 | | void SwapElements(Message* message, const FieldDescriptor* field, int index1, |
560 | | int index2) const; |
561 | | |
562 | | // Swap without internal safety and ownership checks. This method should only |
563 | | // be used when the objects are on the same arena. |
564 | | void UnsafeArenaSwap(Message* lhs, Message* rhs) const; |
565 | | |
566 | | // SwapFields without internal safety and ownership checks. This method should |
567 | | // only be used when the objects are on the same arena. |
568 | | void UnsafeArenaSwapFields( |
569 | | Message* lhs, Message* rhs, |
570 | | const std::vector<const FieldDescriptor*>& fields) const; |
571 | | |
572 | | // List all fields of the message which are currently set, except for unknown |
573 | | // fields, but including extension known to the parser (i.e. compiled in). |
574 | | // Singular fields will only be listed if HasField(field) would return true |
575 | | // and repeated fields will only be listed if FieldSize(field) would return |
576 | | // non-zero. Fields (both normal fields and extension fields) will be listed |
577 | | // ordered by field number. |
578 | | // Use Reflection::GetUnknownFields() or message.unknown_fields() to also get |
579 | | // access to fields/extensions unknown to the parser. |
580 | | void ListFields(const Message& message, |
581 | | std::vector<const FieldDescriptor*>* output) const; |
582 | | |
583 | | // Singular field getters ------------------------------------------ |
584 | | // These get the value of a non-repeated field. They return the default |
585 | | // value for fields that aren't set. |
586 | | |
587 | | int32_t GetInt32(const Message& message, const FieldDescriptor* field) const; |
588 | | int64_t GetInt64(const Message& message, const FieldDescriptor* field) const; |
589 | | uint32_t GetUInt32(const Message& message, |
590 | | const FieldDescriptor* field) const; |
591 | | uint64_t GetUInt64(const Message& message, |
592 | | const FieldDescriptor* field) const; |
593 | | float GetFloat(const Message& message, const FieldDescriptor* field) const; |
594 | | double GetDouble(const Message& message, const FieldDescriptor* field) const; |
595 | | bool GetBool(const Message& message, const FieldDescriptor* field) const; |
596 | | std::string GetString(const Message& message, |
597 | | const FieldDescriptor* field) const; |
598 | | const EnumValueDescriptor* GetEnum(const Message& message, |
599 | | const FieldDescriptor* field) const; |
600 | | |
601 | | // GetEnumValue() returns an enum field's value as an integer rather than |
602 | | // an EnumValueDescriptor*. If the integer value does not correspond to a |
603 | | // known value descriptor, a new value descriptor is created. (Such a value |
604 | | // will only be present when the new unknown-enum-value semantics are enabled |
605 | | // for a message.) |
606 | | int GetEnumValue(const Message& message, const FieldDescriptor* field) const; |
607 | | |
608 | | // See MutableMessage() for the meaning of the "factory" parameter. |
609 | | const Message& GetMessage(const Message& message, |
610 | | const FieldDescriptor* field, |
611 | | MessageFactory* factory = nullptr) const; |
612 | | |
613 | | // Get a string value without copying, if possible. |
614 | | // |
615 | | // GetString() necessarily returns a copy of the string. This can be |
616 | | // inefficient when the std::string is already stored in a std::string object |
617 | | // in the underlying message. GetStringReference() will return a reference to |
618 | | // the underlying std::string in this case. Otherwise, it will copy the |
619 | | // string into *scratch and return that. |
620 | | // |
621 | | // Note: It is perfectly reasonable and useful to write code like: |
622 | | // str = reflection->GetStringReference(message, field, &str); |
623 | | // This line would ensure that only one copy of the string is made |
624 | | // regardless of the field's underlying representation. When initializing |
625 | | // a newly-constructed string, though, it's just as fast and more |
626 | | // readable to use code like: |
627 | | // std::string str = reflection->GetString(message, field); |
628 | | const std::string& GetStringReference(const Message& message, |
629 | | const FieldDescriptor* field, |
630 | | std::string* scratch) const; |
631 | | |
632 | | |
633 | | // Singular field mutators ----------------------------------------- |
634 | | // These mutate the value of a non-repeated field. |
635 | | |
636 | | void SetInt32(Message* message, const FieldDescriptor* field, |
637 | | int32_t value) const; |
638 | | void SetInt64(Message* message, const FieldDescriptor* field, |
639 | | int64_t value) const; |
640 | | void SetUInt32(Message* message, const FieldDescriptor* field, |
641 | | uint32_t value) const; |
642 | | void SetUInt64(Message* message, const FieldDescriptor* field, |
643 | | uint64_t value) const; |
644 | | void SetFloat(Message* message, const FieldDescriptor* field, |
645 | | float value) const; |
646 | | void SetDouble(Message* message, const FieldDescriptor* field, |
647 | | double value) const; |
648 | | void SetBool(Message* message, const FieldDescriptor* field, |
649 | | bool value) const; |
650 | | void SetString(Message* message, const FieldDescriptor* field, |
651 | | std::string value) const; |
652 | | void SetEnum(Message* message, const FieldDescriptor* field, |
653 | | const EnumValueDescriptor* value) const; |
654 | | // Set an enum field's value with an integer rather than EnumValueDescriptor. |
655 | | // For proto3 this is just setting the enum field to the value specified, for |
656 | | // proto2 it's more complicated. If value is a known enum value the field is |
657 | | // set as usual. If the value is unknown then it is added to the unknown field |
658 | | // set. Note this matches the behavior of parsing unknown enum values. |
659 | | // If multiple calls with unknown values happen than they are all added to the |
660 | | // unknown field set in order of the calls. |
661 | | void SetEnumValue(Message* message, const FieldDescriptor* field, |
662 | | int value) const; |
663 | | |
664 | | // Get a mutable pointer to a field with a message type. If a MessageFactory |
665 | | // is provided, it will be used to construct instances of the sub-message; |
666 | | // otherwise, the default factory is used. If the field is an extension that |
667 | | // does not live in the same pool as the containing message's descriptor (e.g. |
668 | | // it lives in an overlay pool), then a MessageFactory must be provided. |
669 | | // If you have no idea what that meant, then you probably don't need to worry |
670 | | // about it (don't provide a MessageFactory). WARNING: If the |
671 | | // FieldDescriptor is for a compiled-in extension, then |
672 | | // factory->GetPrototype(field->message_type()) MUST return an instance of |
673 | | // the compiled-in class for this type, NOT DynamicMessage. |
674 | | Message* MutableMessage(Message* message, const FieldDescriptor* field, |
675 | | MessageFactory* factory = nullptr) const; |
676 | | |
677 | | // Replaces the message specified by 'field' with the already-allocated object |
678 | | // sub_message, passing ownership to the message. If the field contained a |
679 | | // message, that message is deleted. If sub_message is nullptr, the field is |
680 | | // cleared. |
681 | | void SetAllocatedMessage(Message* message, Message* sub_message, |
682 | | const FieldDescriptor* field) const; |
683 | | |
684 | | // Similar to `SetAllocatedMessage`, but omits all internal safety and |
685 | | // ownership checks. This method should only be used when the objects are on |
686 | | // the same arena or paired with a call to `UnsafeArenaReleaseMessage`. |
687 | | void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message, |
688 | | const FieldDescriptor* field) const; |
689 | | |
690 | | // Releases the message specified by 'field' and returns the pointer, |
691 | | // ReleaseMessage() will return the message the message object if it exists. |
692 | | // Otherwise, it may or may not return nullptr. In any case, if the return |
693 | | // value is non-null, the caller takes ownership of the pointer. |
694 | | // If the field existed (HasField() is true), then the returned pointer will |
695 | | // be the same as the pointer returned by MutableMessage(). |
696 | | // This function has the same effect as ClearField(). |
697 | | PROTOBUF_NODISCARD Message* ReleaseMessage( |
698 | | Message* message, const FieldDescriptor* field, |
699 | | MessageFactory* factory = nullptr) const; |
700 | | |
701 | | // Similar to `ReleaseMessage`, but omits all internal safety and ownership |
702 | | // checks. This method should only be used when the objects are on the same |
703 | | // arena or paired with a call to `UnsafeArenaSetAllocatedMessage`. |
704 | | Message* UnsafeArenaReleaseMessage(Message* message, |
705 | | const FieldDescriptor* field, |
706 | | MessageFactory* factory = nullptr) const; |
707 | | |
708 | | |
709 | | // Repeated field getters ------------------------------------------ |
710 | | // These get the value of one element of a repeated field. |
711 | | |
712 | | int32_t GetRepeatedInt32(const Message& message, const FieldDescriptor* field, |
713 | | int index) const; |
714 | | int64_t GetRepeatedInt64(const Message& message, const FieldDescriptor* field, |
715 | | int index) const; |
716 | | uint32_t GetRepeatedUInt32(const Message& message, |
717 | | const FieldDescriptor* field, int index) const; |
718 | | uint64_t GetRepeatedUInt64(const Message& message, |
719 | | const FieldDescriptor* field, int index) const; |
720 | | float GetRepeatedFloat(const Message& message, const FieldDescriptor* field, |
721 | | int index) const; |
722 | | double GetRepeatedDouble(const Message& message, const FieldDescriptor* field, |
723 | | int index) const; |
724 | | bool GetRepeatedBool(const Message& message, const FieldDescriptor* field, |
725 | | int index) const; |
726 | | std::string GetRepeatedString(const Message& message, |
727 | | const FieldDescriptor* field, int index) const; |
728 | | const EnumValueDescriptor* GetRepeatedEnum(const Message& message, |
729 | | const FieldDescriptor* field, |
730 | | int index) const; |
731 | | // GetRepeatedEnumValue() returns an enum field's value as an integer rather |
732 | | // than an EnumValueDescriptor*. If the integer value does not correspond to a |
733 | | // known value descriptor, a new value descriptor is created. (Such a value |
734 | | // will only be present when the new unknown-enum-value semantics are enabled |
735 | | // for a message.) |
736 | | int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field, |
737 | | int index) const; |
738 | | const Message& GetRepeatedMessage(const Message& message, |
739 | | const FieldDescriptor* field, |
740 | | int index) const; |
741 | | |
742 | | // See GetStringReference(), above. |
743 | | const std::string& GetRepeatedStringReference(const Message& message, |
744 | | const FieldDescriptor* field, |
745 | | int index, |
746 | | std::string* scratch) const; |
747 | | |
748 | | |
749 | | // Repeated field mutators ----------------------------------------- |
750 | | // These mutate the value of one element of a repeated field. |
751 | | |
752 | | void SetRepeatedInt32(Message* message, const FieldDescriptor* field, |
753 | | int index, int32_t value) const; |
754 | | void SetRepeatedInt64(Message* message, const FieldDescriptor* field, |
755 | | int index, int64_t value) const; |
756 | | void SetRepeatedUInt32(Message* message, const FieldDescriptor* field, |
757 | | int index, uint32_t value) const; |
758 | | void SetRepeatedUInt64(Message* message, const FieldDescriptor* field, |
759 | | int index, uint64_t value) const; |
760 | | void SetRepeatedFloat(Message* message, const FieldDescriptor* field, |
761 | | int index, float value) const; |
762 | | void SetRepeatedDouble(Message* message, const FieldDescriptor* field, |
763 | | int index, double value) const; |
764 | | void SetRepeatedBool(Message* message, const FieldDescriptor* field, |
765 | | int index, bool value) const; |
766 | | void SetRepeatedString(Message* message, const FieldDescriptor* field, |
767 | | int index, std::string value) const; |
768 | | void SetRepeatedEnum(Message* message, const FieldDescriptor* field, |
769 | | int index, const EnumValueDescriptor* value) const; |
770 | | // Set an enum field's value with an integer rather than EnumValueDescriptor. |
771 | | // For proto3 this is just setting the enum field to the value specified, for |
772 | | // proto2 it's more complicated. If value is a known enum value the field is |
773 | | // set as usual. If the value is unknown then it is added to the unknown field |
774 | | // set. Note this matches the behavior of parsing unknown enum values. |
775 | | // If multiple calls with unknown values happen than they are all added to the |
776 | | // unknown field set in order of the calls. |
777 | | void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field, |
778 | | int index, int value) const; |
779 | | // Get a mutable pointer to an element of a repeated field with a message |
780 | | // type. |
781 | | Message* MutableRepeatedMessage(Message* message, |
782 | | const FieldDescriptor* field, |
783 | | int index) const; |
784 | | |
785 | | |
786 | | // Repeated field adders ------------------------------------------- |
787 | | // These add an element to a repeated field. |
788 | | |
789 | | void AddInt32(Message* message, const FieldDescriptor* field, |
790 | | int32_t value) const; |
791 | | void AddInt64(Message* message, const FieldDescriptor* field, |
792 | | int64_t value) const; |
793 | | void AddUInt32(Message* message, const FieldDescriptor* field, |
794 | | uint32_t value) const; |
795 | | void AddUInt64(Message* message, const FieldDescriptor* field, |
796 | | uint64_t value) const; |
797 | | void AddFloat(Message* message, const FieldDescriptor* field, |
798 | | float value) const; |
799 | | void AddDouble(Message* message, const FieldDescriptor* field, |
800 | | double value) const; |
801 | | void AddBool(Message* message, const FieldDescriptor* field, |
802 | | bool value) const; |
803 | | void AddString(Message* message, const FieldDescriptor* field, |
804 | | std::string value) const; |
805 | | void AddEnum(Message* message, const FieldDescriptor* field, |
806 | | const EnumValueDescriptor* value) const; |
807 | | |
808 | | // Add an integer value to a repeated enum field rather than |
809 | | // EnumValueDescriptor. For proto3 this is just setting the enum field to the |
810 | | // value specified, for proto2 it's more complicated. If value is a known enum |
811 | | // value the field is set as usual. If the value is unknown then it is added |
812 | | // to the unknown field set. Note this matches the behavior of parsing unknown |
813 | | // enum values. If multiple calls with unknown values happen than they are all |
814 | | // added to the unknown field set in order of the calls. |
815 | | void AddEnumValue(Message* message, const FieldDescriptor* field, |
816 | | int value) const; |
817 | | // See MutableMessage() for comments on the "factory" parameter. |
818 | | Message* AddMessage(Message* message, const FieldDescriptor* field, |
819 | | MessageFactory* factory = nullptr) const; |
820 | | |
821 | | // Appends an already-allocated object 'new_entry' to the repeated field |
822 | | // specified by 'field' passing ownership to the message. |
823 | | void AddAllocatedMessage(Message* message, const FieldDescriptor* field, |
824 | | Message* new_entry) const; |
825 | | |
826 | | // Similar to AddAllocatedMessage() without internal safety and ownership |
827 | | // checks. This method should only be used when the objects are on the same |
828 | | // arena or paired with a call to `UnsafeArenaReleaseLast`. |
829 | | void UnsafeArenaAddAllocatedMessage(Message* message, |
830 | | const FieldDescriptor* field, |
831 | | Message* new_entry) const; |
832 | | |
833 | | |
834 | | // Get a RepeatedFieldRef object that can be used to read the underlying |
835 | | // repeated field. The type parameter T must be set according to the |
836 | | // field's cpp type. The following table shows the mapping from cpp type |
837 | | // to acceptable T. |
838 | | // |
839 | | // field->cpp_type() T |
840 | | // CPPTYPE_INT32 int32_t |
841 | | // CPPTYPE_UINT32 uint32_t |
842 | | // CPPTYPE_INT64 int64_t |
843 | | // CPPTYPE_UINT64 uint64_t |
844 | | // CPPTYPE_DOUBLE double |
845 | | // CPPTYPE_FLOAT float |
846 | | // CPPTYPE_BOOL bool |
847 | | // CPPTYPE_ENUM generated enum type or int32_t |
848 | | // CPPTYPE_STRING std::string |
849 | | // CPPTYPE_MESSAGE generated message type or google::protobuf::Message |
850 | | // |
851 | | // A RepeatedFieldRef object can be copied and the resulted object will point |
852 | | // to the same repeated field in the same message. The object can be used as |
853 | | // long as the message is not destroyed. |
854 | | // |
855 | | // Note that to use this method users need to include the header file |
856 | | // "reflection.h" (which defines the RepeatedFieldRef class templates). |
857 | | template <typename T> |
858 | | RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message, |
859 | | const FieldDescriptor* field) const; |
860 | | |
861 | | // Like GetRepeatedFieldRef() but return an object that can also be used |
862 | | // manipulate the underlying repeated field. |
863 | | template <typename T> |
864 | | MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef( |
865 | | Message* message, const FieldDescriptor* field) const; |
866 | | |
867 | | // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field |
868 | | // access. The following repeated field accessors will be removed in the |
869 | | // future. |
870 | | // |
871 | | // Repeated field accessors ------------------------------------------------- |
872 | | // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular |
873 | | // access to the data in a RepeatedField. The methods below provide aggregate |
874 | | // access by exposing the RepeatedField object itself with the Message. |
875 | | // Applying these templates to inappropriate types will lead to an undefined |
876 | | // reference at link time (e.g. GetRepeatedField<***double>), or possibly a |
877 | | // template matching error at compile time (e.g. GetRepeatedPtrField<File>). |
878 | | // |
879 | | // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd); |
880 | | |
881 | | // DEPRECATED. Please use GetRepeatedFieldRef(). |
882 | | // |
883 | | // for T = Cord and all protobuf scalar types except enums. |
884 | | template <typename T> |
885 | | PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead") |
886 | | const RepeatedField<T>& GetRepeatedField(const Message& msg, |
887 | | const FieldDescriptor* d) const { |
888 | | return GetRepeatedFieldInternal<T>(msg, d); |
889 | | } |
890 | | |
891 | | // DEPRECATED. Please use GetMutableRepeatedFieldRef(). |
892 | | // |
893 | | // for T = Cord and all protobuf scalar types except enums. |
894 | | template <typename T> |
895 | | PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead") |
896 | | RepeatedField<T>* MutableRepeatedField(Message* msg, |
897 | | const FieldDescriptor* d) const { |
898 | | return MutableRepeatedFieldInternal<T>(msg, d); |
899 | | } |
900 | | |
901 | | // DEPRECATED. Please use GetRepeatedFieldRef(). |
902 | | // |
903 | | // for T = std::string, google::protobuf::internal::StringPieceField |
904 | | // google::protobuf::Message & descendants. |
905 | | template <typename T> |
906 | | PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead") |
907 | | const RepeatedPtrField<T>& GetRepeatedPtrField( |
908 | | const Message& msg, const FieldDescriptor* d) const { |
909 | | return GetRepeatedPtrFieldInternal<T>(msg, d); |
910 | | } |
911 | | |
912 | | // DEPRECATED. Please use GetMutableRepeatedFieldRef(). |
913 | | // |
914 | | // for T = std::string, google::protobuf::internal::StringPieceField |
915 | | // google::protobuf::Message & descendants. |
916 | | template <typename T> |
917 | | PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead") |
918 | | RepeatedPtrField<T>* MutableRepeatedPtrField(Message* msg, |
919 | | const FieldDescriptor* d) const { |
920 | | return MutableRepeatedPtrFieldInternal<T>(msg, d); |
921 | | } |
922 | | |
923 | | // Extensions ---------------------------------------------------------------- |
924 | | |
925 | | // Try to find an extension of this message type by fully-qualified field |
926 | | // name. Returns nullptr if no extension is known for this name or number. |
927 | | const FieldDescriptor* FindKnownExtensionByName(absl::string_view name) const; |
928 | | |
929 | | // Try to find an extension of this message type by field number. |
930 | | // Returns nullptr if no extension is known for this name or number. |
931 | | const FieldDescriptor* FindKnownExtensionByNumber(int number) const; |
932 | | |
933 | | // Feature Flags ------------------------------------------------------------- |
934 | | |
935 | | // Does this message support storing arbitrary integer values in enum fields? |
936 | | // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions |
937 | | // take arbitrary integer values, and the legacy GetEnum() getter will |
938 | | // dynamically create an EnumValueDescriptor for any integer value without |
939 | | // one. If |false|, setting an unknown enum value via the integer-based |
940 | | // setters results in undefined behavior (in practice, ABSL_DCHECK-fails). |
941 | | // |
942 | | // Generic code that uses reflection to handle messages with enum fields |
943 | | // should check this flag before using the integer-based setter, and either |
944 | | // downgrade to a compatible value or use the UnknownFieldSet if not. For |
945 | | // example: |
946 | | // |
947 | | // int new_value = GetValueFromApplicationLogic(); |
948 | | // if (reflection->SupportsUnknownEnumValues()) { |
949 | | // reflection->SetEnumValue(message, field, new_value); |
950 | | // } else { |
951 | | // if (field_descriptor->enum_type()-> |
952 | | // FindValueByNumber(new_value) != nullptr) { |
953 | | // reflection->SetEnumValue(message, field, new_value); |
954 | | // } else if (emit_unknown_enum_values) { |
955 | | // reflection->MutableUnknownFields(message)->AddVarint( |
956 | | // field->number(), new_value); |
957 | | // } else { |
958 | | // // convert value to a compatible/default value. |
959 | | // new_value = CompatibleDowngrade(new_value); |
960 | | // reflection->SetEnumValue(message, field, new_value); |
961 | | // } |
962 | | // } |
963 | | bool SupportsUnknownEnumValues() const; |
964 | | |
965 | | // Returns the MessageFactory associated with this message. This can be |
966 | | // useful for determining if a message is a generated message or not, for |
967 | | // example: |
968 | | // if (message->GetReflection()->GetMessageFactory() == |
969 | | // google::protobuf::MessageFactory::generated_factory()) { |
970 | | // // This is a generated message. |
971 | | // } |
972 | | // It can also be used to create more messages of this type, though |
973 | | // Message::New() is an easier way to accomplish this. |
974 | | MessageFactory* GetMessageFactory() const; |
975 | | |
976 | | private: |
977 | | template <typename T> |
978 | | const RepeatedField<T>& GetRepeatedFieldInternal( |
979 | | const Message& message, const FieldDescriptor* field) const; |
980 | | template <typename T> |
981 | | RepeatedField<T>* MutableRepeatedFieldInternal( |
982 | | Message* message, const FieldDescriptor* field) const; |
983 | | template <typename T> |
984 | | const RepeatedPtrField<T>& GetRepeatedPtrFieldInternal( |
985 | | const Message& message, const FieldDescriptor* field) const; |
986 | | template <typename T> |
987 | | RepeatedPtrField<T>* MutableRepeatedPtrFieldInternal( |
988 | | Message* message, const FieldDescriptor* field) const; |
989 | | |
990 | | // Obtain a pointer to a Repeated Field Structure and do some type checking: |
991 | | // on field->cpp_type(), |
992 | | // on field->field_option().ctype() (if ctype >= 0) |
993 | | // of field->message_type() (if message_type != nullptr). |
994 | | // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer). |
995 | | void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field, |
996 | | FieldDescriptor::CppType, int ctype, |
997 | | const Descriptor* message_type) const; |
998 | | |
999 | | const void* GetRawRepeatedField(const Message& message, |
1000 | | const FieldDescriptor* field, |
1001 | | FieldDescriptor::CppType cpptype, int ctype, |
1002 | | const Descriptor* message_type) const; |
1003 | | |
1004 | | // The following methods are used to implement (Mutable)RepeatedFieldRef. |
1005 | | // A Ref object will store a raw pointer to the repeated field data (obtained |
1006 | | // from RepeatedFieldData()) and a pointer to a Accessor (obtained from |
1007 | | // RepeatedFieldAccessor) which will be used to access the raw data. |
1008 | | |
1009 | | // Returns a raw pointer to the repeated field |
1010 | | // |
1011 | | // "cpp_type" and "message_type" are deduced from the type parameter T passed |
1012 | | // to Get(Mutable)RepeatedFieldRef. If T is a generated message type, |
1013 | | // "message_type" should be set to its descriptor. Otherwise "message_type" |
1014 | | // should be set to nullptr. Implementations of this method should check |
1015 | | // whether "cpp_type"/"message_type" is consistent with the actual type of the |
1016 | | // field. We use 1 routine rather than 2 (const vs mutable) because it is |
1017 | | // protected and it doesn't change the message. |
1018 | | void* RepeatedFieldData(Message* message, const FieldDescriptor* field, |
1019 | | FieldDescriptor::CppType cpp_type, |
1020 | | const Descriptor* message_type) const; |
1021 | | |
1022 | | // The returned pointer should point to a singleton instance which implements |
1023 | | // the RepeatedFieldAccessor interface. |
1024 | | const internal::RepeatedFieldAccessor* RepeatedFieldAccessor( |
1025 | | const FieldDescriptor* field) const; |
1026 | | |
1027 | | // Returns true if the message field is backed by a LazyField. |
1028 | | // |
1029 | | // A message field may be backed by a LazyField without the user annotation |
1030 | | // ([lazy = true]). While the user-annotated LazyField is lazily verified on |
1031 | | // first touch (i.e. failure on access rather than parsing if the LazyField is |
1032 | | // not initialized), the inferred LazyField is eagerly verified to avoid lazy |
1033 | | // parsing error at the cost of lower efficiency. When reflecting a message |
1034 | | // field, use this API instead of checking field->options().lazy(). |
1035 | 0 | bool IsLazyField(const FieldDescriptor* field) const { |
1036 | 0 | return IsLazilyVerifiedLazyField(field) || |
1037 | 0 | IsEagerlyVerifiedLazyField(field); |
1038 | 0 | } |
1039 | | |
1040 | | // Returns true if the field is lazy extension. It is meant to allow python |
1041 | | // reparse lazy field until b/157559327 is fixed. |
1042 | | bool IsLazyExtension(const Message& message, |
1043 | | const FieldDescriptor* field) const; |
1044 | | |
1045 | | bool IsLazilyVerifiedLazyField(const FieldDescriptor* field) const; |
1046 | | bool IsEagerlyVerifiedLazyField(const FieldDescriptor* field) const; |
1047 | | |
1048 | 0 | bool IsSplit(const FieldDescriptor* field) const { |
1049 | 0 | return schema_.IsSplit(field); |
1050 | 0 | } |
1051 | | |
1052 | | friend class FastReflectionBase; |
1053 | | friend class FastReflectionMessageMutator; |
1054 | | friend bool internal::IsDescendant(Message& root, const Message& message); |
1055 | | |
1056 | | const Descriptor* const descriptor_; |
1057 | | const internal::ReflectionSchema schema_; |
1058 | | const DescriptorPool* const descriptor_pool_; |
1059 | | MessageFactory* const message_factory_; |
1060 | | |
1061 | | // Last non weak field index. This is an optimization when most weak fields |
1062 | | // are at the end of the containing message. If a message proto doesn't |
1063 | | // contain weak fields, then this field equals descriptor_->field_count(). |
1064 | | int last_non_weak_field_index_; |
1065 | | |
1066 | | // The table-driven parser table. |
1067 | | // This table is generated on demand for Message types that did not override |
1068 | | // _InternalParse. It uses the reflection information to do so. |
1069 | | mutable absl::once_flag tcparse_table_once_; |
1070 | | using TcParseTableBase = internal::TcParseTableBase; |
1071 | | mutable const TcParseTableBase* tcparse_table_ = nullptr; |
1072 | | |
1073 | 0 | const TcParseTableBase* GetTcParseTable() const { |
1074 | 0 | absl::call_once(tcparse_table_once_, |
1075 | 0 | [&] { tcparse_table_ = CreateTcParseTable(); }); |
1076 | 0 | return tcparse_table_; |
1077 | 0 | } |
1078 | | |
1079 | | const TcParseTableBase* CreateTcParseTable() const; |
1080 | | const TcParseTableBase* CreateTcParseTableForMessageSet() const; |
1081 | | void PopulateTcParseFastEntries( |
1082 | | const internal::TailCallTableInfo& table_info, |
1083 | | TcParseTableBase::FastFieldEntry* fast_entries) const; |
1084 | | void PopulateTcParseEntries(internal::TailCallTableInfo& table_info, |
1085 | | TcParseTableBase::FieldEntry* entries) const; |
1086 | | void PopulateTcParseFieldAux(const internal::TailCallTableInfo& table_info, |
1087 | | TcParseTableBase::FieldAux* field_aux) const; |
1088 | | |
1089 | | template <typename T, typename Enable> |
1090 | | friend class RepeatedFieldRef; |
1091 | | template <typename T, typename Enable> |
1092 | | friend class MutableRepeatedFieldRef; |
1093 | | friend class Message; |
1094 | | friend class ::PROTOBUF_NAMESPACE_ID::MessageLayoutInspector; |
1095 | | friend class ::PROTOBUF_NAMESPACE_ID::AssignDescriptorsHelper; |
1096 | | friend class DynamicMessageFactory; |
1097 | | friend class GeneratedMessageReflectionTestHelper; |
1098 | | friend class python::MapReflectionFriend; |
1099 | | friend class python::MessageReflectionFriend; |
1100 | | friend class util::MessageDifferencer; |
1101 | | #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND |
1102 | | friend class expr::CelMapReflectionFriend; |
1103 | | friend class internal::MapFieldReflectionTest; |
1104 | | friend class internal::MapKeySorter; |
1105 | | friend class internal::WireFormat; |
1106 | | friend class internal::ReflectionOps; |
1107 | | friend class internal::SwapFieldHelper; |
1108 | | friend struct internal::FuzzPeer; |
1109 | | // Needed for implementing text format for map. |
1110 | | friend class internal::MapFieldPrinterHelper; |
1111 | | |
1112 | | Reflection(const Descriptor* descriptor, |
1113 | | const internal::ReflectionSchema& schema, |
1114 | | const DescriptorPool* pool, MessageFactory* factory); |
1115 | | |
1116 | | // Special version for specialized implementations of string. We can't |
1117 | | // call MutableRawRepeatedField directly here because we don't have access to |
1118 | | // FieldOptions::* which are defined in descriptor.pb.h. Including that |
1119 | | // file here is not possible because it would cause a circular include cycle. |
1120 | | // We use 1 routine rather than 2 (const vs mutable) because it is private |
1121 | | // and mutable a repeated string field doesn't change the message. |
1122 | | void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field, |
1123 | | bool is_string) const; |
1124 | | |
1125 | | friend class MapReflectionTester; |
1126 | | // Returns true if key is in map. Returns false if key is not in map field. |
1127 | | bool ContainsMapKey(const Message& message, const FieldDescriptor* field, |
1128 | | const MapKey& key) const; |
1129 | | |
1130 | | // If key is in map field: Saves the value pointer to val and returns |
1131 | | // false. If key in not in map field: Insert the key into map, saves |
1132 | | // value pointer to val and returns true. Users are able to modify the |
1133 | | // map value by MapValueRef. |
1134 | | bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field, |
1135 | | const MapKey& key, MapValueRef* val) const; |
1136 | | |
1137 | | // If key is in map field: Saves the value pointer to val and returns true. |
1138 | | // Returns false if key is not in map field. Users are NOT able to modify |
1139 | | // the value by MapValueConstRef. |
1140 | | bool LookupMapValue(const Message& message, const FieldDescriptor* field, |
1141 | | const MapKey& key, MapValueConstRef* val) const; |
1142 | | bool LookupMapValue(const Message&, const FieldDescriptor*, const MapKey&, |
1143 | | MapValueRef*) const = delete; |
1144 | | |
1145 | | // Delete and returns true if key is in the map field. Returns false |
1146 | | // otherwise. |
1147 | | bool DeleteMapValue(Message* message, const FieldDescriptor* field, |
1148 | | const MapKey& key) const; |
1149 | | |
1150 | | // Returns a MapIterator referring to the first element in the map field. |
1151 | | // If the map field is empty, this function returns the same as |
1152 | | // reflection::MapEnd. Mutation to the field may invalidate the iterator. |
1153 | | MapIterator MapBegin(Message* message, const FieldDescriptor* field) const; |
1154 | | |
1155 | | // Returns a MapIterator referring to the theoretical element that would |
1156 | | // follow the last element in the map field. It does not point to any |
1157 | | // real element. Mutation to the field may invalidate the iterator. |
1158 | | MapIterator MapEnd(Message* message, const FieldDescriptor* field) const; |
1159 | | |
1160 | | // Get the number of <key, value> pair of a map field. The result may be |
1161 | | // different from FieldSize which can have duplicate keys. |
1162 | | int MapSize(const Message& message, const FieldDescriptor* field) const; |
1163 | | |
1164 | | // Help method for MapIterator. |
1165 | | friend class MapIterator; |
1166 | | friend class WireFormatForMapFieldTest; |
1167 | | internal::MapFieldBase* MutableMapData(Message* message, |
1168 | | const FieldDescriptor* field) const; |
1169 | | |
1170 | | const internal::MapFieldBase* GetMapData(const Message& message, |
1171 | | const FieldDescriptor* field) const; |
1172 | | |
1173 | | template <class T> |
1174 | | const T& GetRawNonOneof(const Message& message, |
1175 | | const FieldDescriptor* field) const; |
1176 | | template <class T> |
1177 | | T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const; |
1178 | | |
1179 | | template <typename Type> |
1180 | | const Type& GetRaw(const Message& message, |
1181 | | const FieldDescriptor* field) const; |
1182 | | template <typename Type> |
1183 | | inline Type* MutableRaw(Message* message, const FieldDescriptor* field) const; |
1184 | | template <typename Type> |
1185 | | const Type& DefaultRaw(const FieldDescriptor* field) const; |
1186 | | |
1187 | | const Message* GetDefaultMessageInstance(const FieldDescriptor* field) const; |
1188 | | |
1189 | | inline const uint32_t* GetHasBits(const Message& message) const; |
1190 | | inline uint32_t* MutableHasBits(Message* message) const; |
1191 | | inline uint32_t GetOneofCase(const Message& message, |
1192 | | const OneofDescriptor* oneof_descriptor) const; |
1193 | | inline uint32_t* MutableOneofCase( |
1194 | | Message* message, const OneofDescriptor* oneof_descriptor) const; |
1195 | 0 | inline bool HasExtensionSet(const Message& /* message */) const { |
1196 | 0 | return schema_.HasExtensionSet(); |
1197 | 0 | } |
1198 | | const internal::ExtensionSet& GetExtensionSet(const Message& message) const; |
1199 | | internal::ExtensionSet* MutableExtensionSet(Message* message) const; |
1200 | | |
1201 | | const internal::InternalMetadata& GetInternalMetadata( |
1202 | | const Message& message) const; |
1203 | | |
1204 | | internal::InternalMetadata* MutableInternalMetadata(Message* message) const; |
1205 | | |
1206 | | inline bool IsInlined(const FieldDescriptor* field) const; |
1207 | | |
1208 | | inline bool HasBit(const Message& message, |
1209 | | const FieldDescriptor* field) const; |
1210 | | inline void SetBit(Message* message, const FieldDescriptor* field) const; |
1211 | | inline void ClearBit(Message* message, const FieldDescriptor* field) const; |
1212 | | inline void SwapBit(Message* message1, Message* message2, |
1213 | | const FieldDescriptor* field) const; |
1214 | | |
1215 | | inline const uint32_t* GetInlinedStringDonatedArray( |
1216 | | const Message& message) const; |
1217 | | inline uint32_t* MutableInlinedStringDonatedArray(Message* message) const; |
1218 | | inline bool IsInlinedStringDonated(const Message& message, |
1219 | | const FieldDescriptor* field) const; |
1220 | | inline void SwapInlinedStringDonated(Message* lhs, Message* rhs, |
1221 | | const FieldDescriptor* field) const; |
1222 | | |
1223 | | // Returns the `_split_` pointer. Requires: IsSplit() == true. |
1224 | | inline const void* GetSplitField(const Message* message) const; |
1225 | | // Returns the address of the `_split_` pointer. Requires: IsSplit() == true. |
1226 | | inline void** MutableSplitField(Message* message) const; |
1227 | | |
1228 | | // Allocate the split instance if needed. |
1229 | | void PrepareSplitMessageForWrite(Message* message) const; |
1230 | | |
1231 | | // Shallow-swap fields listed in fields vector of two messages. It is the |
1232 | | // caller's responsibility to make sure shallow swap is safe. |
1233 | | void UnsafeShallowSwapFields( |
1234 | | Message* message1, Message* message2, |
1235 | | const std::vector<const FieldDescriptor*>& fields) const; |
1236 | | |
1237 | | // This function only swaps the field. Should swap corresponding has_bit |
1238 | | // before or after using this function. |
1239 | | void SwapField(Message* message1, Message* message2, |
1240 | | const FieldDescriptor* field) const; |
1241 | | |
1242 | | // Unsafe but shallow version of SwapField. |
1243 | | void UnsafeShallowSwapField(Message* message1, Message* message2, |
1244 | | const FieldDescriptor* field) const; |
1245 | | |
1246 | | template <bool unsafe_shallow_swap> |
1247 | | void SwapFieldsImpl(Message* message1, Message* message2, |
1248 | | const std::vector<const FieldDescriptor*>& fields) const; |
1249 | | |
1250 | | template <bool unsafe_shallow_swap> |
1251 | | void SwapOneofField(Message* lhs, Message* rhs, |
1252 | | const OneofDescriptor* oneof_descriptor) const; |
1253 | | |
1254 | | void InternalSwap(Message* lhs, Message* rhs) const; |
1255 | | |
1256 | | inline bool HasOneofField(const Message& message, |
1257 | | const FieldDescriptor* field) const; |
1258 | | inline void SetOneofCase(Message* message, |
1259 | | const FieldDescriptor* field) const; |
1260 | | inline void ClearOneofField(Message* message, |
1261 | | const FieldDescriptor* field) const; |
1262 | | |
1263 | | template <typename Type> |
1264 | | inline const Type& GetField(const Message& message, |
1265 | | const FieldDescriptor* field) const; |
1266 | | template <typename Type> |
1267 | | inline void SetField(Message* message, const FieldDescriptor* field, |
1268 | | const Type& value) const; |
1269 | | template <typename Type> |
1270 | | inline Type* MutableField(Message* message, |
1271 | | const FieldDescriptor* field) const; |
1272 | | template <typename Type> |
1273 | | inline const Type& GetRepeatedField(const Message& message, |
1274 | | const FieldDescriptor* field, |
1275 | | int index) const; |
1276 | | template <typename Type> |
1277 | | inline const Type& GetRepeatedPtrField(const Message& message, |
1278 | | const FieldDescriptor* field, |
1279 | | int index) const; |
1280 | | template <typename Type> |
1281 | | inline void SetRepeatedField(Message* message, const FieldDescriptor* field, |
1282 | | int index, Type value) const; |
1283 | | template <typename Type> |
1284 | | inline Type* MutableRepeatedField(Message* message, |
1285 | | const FieldDescriptor* field, |
1286 | | int index) const; |
1287 | | template <typename Type> |
1288 | | inline void AddField(Message* message, const FieldDescriptor* field, |
1289 | | const Type& value) const; |
1290 | | template <typename Type> |
1291 | | inline Type* AddField(Message* message, const FieldDescriptor* field) const; |
1292 | | |
1293 | | int GetExtensionNumberOrDie(const Descriptor* type) const; |
1294 | | |
1295 | | // Internal versions of EnumValue API perform no checking. Called after checks |
1296 | | // by public methods. |
1297 | | void SetEnumValueInternal(Message* message, const FieldDescriptor* field, |
1298 | | int value) const; |
1299 | | void SetRepeatedEnumValueInternal(Message* message, |
1300 | | const FieldDescriptor* field, int index, |
1301 | | int value) const; |
1302 | | void AddEnumValueInternal(Message* message, const FieldDescriptor* field, |
1303 | | int value) const; |
1304 | | |
1305 | | friend inline // inline so nobody can call this function. |
1306 | | void |
1307 | | RegisterAllTypesInternal(const Metadata* file_level_metadata, int size); |
1308 | | friend inline const char* ParseLenDelim(int field_number, |
1309 | | const FieldDescriptor* field, |
1310 | | Message* msg, |
1311 | | const Reflection* reflection, |
1312 | | const char* ptr, |
1313 | | internal::ParseContext* ctx); |
1314 | | friend inline const char* ParsePackedField(const FieldDescriptor* field, |
1315 | | Message* msg, |
1316 | | const Reflection* reflection, |
1317 | | const char* ptr, |
1318 | | internal::ParseContext* ctx); |
1319 | | }; |
1320 | | |
1321 | | // Abstract interface for a factory for message objects. |
1322 | | // |
1323 | | // The thread safety for this class is implementation dependent, see comments |
1324 | | // around GetPrototype for details |
1325 | | class PROTOBUF_EXPORT MessageFactory { |
1326 | | public: |
1327 | 0 | inline MessageFactory() {} |
1328 | | MessageFactory(const MessageFactory&) = delete; |
1329 | | MessageFactory& operator=(const MessageFactory&) = delete; |
1330 | | virtual ~MessageFactory(); |
1331 | | |
1332 | | // Given a Descriptor, gets or constructs the default (prototype) Message |
1333 | | // of that type. You can then call that message's New() method to construct |
1334 | | // a mutable message of that type. |
1335 | | // |
1336 | | // Calling this method twice with the same Descriptor returns the same |
1337 | | // object. The returned object remains property of the factory. Also, any |
1338 | | // objects created by calling the prototype's New() method share some data |
1339 | | // with the prototype, so these must be destroyed before the MessageFactory |
1340 | | // is destroyed. |
1341 | | // |
1342 | | // The given descriptor must outlive the returned message, and hence must |
1343 | | // outlive the MessageFactory. |
1344 | | // |
1345 | | // Some implementations do not support all types. GetPrototype() will |
1346 | | // return nullptr if the descriptor passed in is not supported. |
1347 | | // |
1348 | | // This method may or may not be thread-safe depending on the implementation. |
1349 | | // Each implementation should document its own degree thread-safety. |
1350 | | virtual const Message* GetPrototype(const Descriptor* type) = 0; |
1351 | | |
1352 | | // Gets a MessageFactory which supports all generated, compiled-in messages. |
1353 | | // In other words, for any compiled-in type FooMessage, the following is true: |
1354 | | // MessageFactory::generated_factory()->GetPrototype( |
1355 | | // FooMessage::descriptor()) == FooMessage::default_instance() |
1356 | | // This factory supports all types which are found in |
1357 | | // DescriptorPool::generated_pool(). If given a descriptor from any other |
1358 | | // pool, GetPrototype() will return nullptr. (You can also check if a |
1359 | | // descriptor is for a generated message by checking if |
1360 | | // descriptor->file()->pool() == DescriptorPool::generated_pool().) |
1361 | | // |
1362 | | // This factory is 100% thread-safe; calling GetPrototype() does not modify |
1363 | | // any shared data. |
1364 | | // |
1365 | | // This factory is a singleton. The caller must not delete the object. |
1366 | | static MessageFactory* generated_factory(); |
1367 | | |
1368 | | // For internal use only: Registers a .proto file at static initialization |
1369 | | // time, to be placed in generated_factory. The first time GetPrototype() |
1370 | | // is called with a descriptor from this file, |register_messages| will be |
1371 | | // called, with the file name as the parameter. It must call |
1372 | | // InternalRegisterGeneratedMessage() (below) to register each message type |
1373 | | // in the file. This strange mechanism is necessary because descriptors are |
1374 | | // built lazily, so we can't register types by their descriptor until we |
1375 | | // know that the descriptor exists. |filename| must be a permanent string. |
1376 | | static void InternalRegisterGeneratedFile( |
1377 | | const google::protobuf::internal::DescriptorTable* table); |
1378 | | |
1379 | | // For internal use only: Registers a message type. Called only by the |
1380 | | // functions which are registered with InternalRegisterGeneratedFile(), |
1381 | | // above. |
1382 | | static void InternalRegisterGeneratedMessage(const Descriptor* descriptor, |
1383 | | const Message* prototype); |
1384 | | |
1385 | | }; |
1386 | | |
1387 | | #define DECLARE_GET_REPEATED_FIELD(TYPE) \ |
1388 | | template <> \ |
1389 | | PROTOBUF_EXPORT const RepeatedField<TYPE>& \ |
1390 | | Reflection::GetRepeatedFieldInternal<TYPE>( \ |
1391 | | const Message& message, const FieldDescriptor* field) const; \ |
1392 | | \ |
1393 | | template <> \ |
1394 | | PROTOBUF_EXPORT RepeatedField<TYPE>* \ |
1395 | | Reflection::MutableRepeatedFieldInternal<TYPE>( \ |
1396 | | Message * message, const FieldDescriptor* field) const; |
1397 | | |
1398 | | DECLARE_GET_REPEATED_FIELD(int32_t) |
1399 | | DECLARE_GET_REPEATED_FIELD(int64_t) |
1400 | | DECLARE_GET_REPEATED_FIELD(uint32_t) |
1401 | | DECLARE_GET_REPEATED_FIELD(uint64_t) |
1402 | | DECLARE_GET_REPEATED_FIELD(float) |
1403 | | DECLARE_GET_REPEATED_FIELD(double) |
1404 | | DECLARE_GET_REPEATED_FIELD(bool) |
1405 | | |
1406 | | #undef DECLARE_GET_REPEATED_FIELD |
1407 | | |
1408 | | // Tries to downcast this message to a generated message type. Returns nullptr |
1409 | | // if this class is not an instance of T. This works even if RTTI is disabled. |
1410 | | // |
1411 | | // This also has the effect of creating a strong reference to T that will |
1412 | | // prevent the linker from stripping it out at link time. This can be important |
1413 | | // if you are using a DynamicMessageFactory that delegates to the generated |
1414 | | // factory. |
1415 | | template <typename T> |
1416 | | const T* DynamicCastToGenerated(const Message* from) { |
1417 | | // Compile-time assert that T is a generated type that has a |
1418 | | // default_instance() accessor, but avoid actually calling it. |
1419 | | const T& (*get_default_instance)() = &T::default_instance; |
1420 | | (void)get_default_instance; |
1421 | | |
1422 | | // Compile-time assert that T is a subclass of google::protobuf::Message. |
1423 | | const Message* unused = static_cast<T*>(nullptr); |
1424 | | (void)unused; |
1425 | | |
1426 | | #if PROTOBUF_RTTI |
1427 | | return dynamic_cast<const T*>(from); |
1428 | | #else |
1429 | | bool ok = from != nullptr && |
1430 | | T::default_instance().GetReflection() == from->GetReflection(); |
1431 | | return ok ? internal::DownCast<const T*>(from) : nullptr; |
1432 | | #endif |
1433 | | } |
1434 | | |
1435 | | template <typename T> |
1436 | | T* DynamicCastToGenerated(Message* from) { |
1437 | | const Message* message_const = from; |
1438 | | return const_cast<T*>(DynamicCastToGenerated<T>(message_const)); |
1439 | | } |
1440 | | |
1441 | | // Call this function to ensure that this message's reflection is linked into |
1442 | | // the binary: |
1443 | | // |
1444 | | // google::protobuf::LinkMessageReflection<pkg::FooMessage>(); |
1445 | | // |
1446 | | // This will ensure that the following lookup will succeed: |
1447 | | // |
1448 | | // DescriptorPool::generated_pool()->FindMessageTypeByName("pkg.FooMessage"); |
1449 | | // |
1450 | | // As a side-effect, it will also guarantee that anything else from the same |
1451 | | // .proto file will also be available for lookup in the generated pool. |
1452 | | // |
1453 | | // This function does not actually register the message, so it does not need |
1454 | | // to be called before the lookup. However it does need to occur in a function |
1455 | | // that cannot be stripped from the binary (ie. it must be reachable from main). |
1456 | | // |
1457 | | // Best practice is to call this function as close as possible to where the |
1458 | | // reflection is actually needed. This function is very cheap to call, so you |
1459 | | // should not need to worry about its runtime overhead except in the tightest |
1460 | | // of loops (on x86-64 it compiles into two "mov" instructions). |
1461 | | template <typename T> |
1462 | | void LinkMessageReflection() { |
1463 | | internal::StrongReference(T::default_instance); |
1464 | | } |
1465 | | |
1466 | | // ============================================================================= |
1467 | | // Implementation details for {Get,Mutable}RawRepeatedPtrField. We provide |
1468 | | // specializations for <std::string>, <StringPieceField> and <Message> and |
1469 | | // handle everything else with the default template which will match any type |
1470 | | // having a method with signature "static const google::protobuf::Descriptor* |
1471 | | // descriptor()". Such a type presumably is a descendant of google::protobuf::Message. |
1472 | | |
1473 | | template <> |
1474 | | inline const RepeatedPtrField<std::string>& |
1475 | | Reflection::GetRepeatedPtrFieldInternal<std::string>( |
1476 | 0 | const Message& message, const FieldDescriptor* field) const { |
1477 | 0 | return *static_cast<RepeatedPtrField<std::string>*>( |
1478 | 0 | MutableRawRepeatedString(const_cast<Message*>(&message), field, true)); |
1479 | 0 | } |
1480 | | |
1481 | | template <> |
1482 | | inline RepeatedPtrField<std::string>* |
1483 | | Reflection::MutableRepeatedPtrFieldInternal<std::string>( |
1484 | 0 | Message* message, const FieldDescriptor* field) const { |
1485 | 0 | return static_cast<RepeatedPtrField<std::string>*>( |
1486 | 0 | MutableRawRepeatedString(message, field, true)); |
1487 | 0 | } |
1488 | | |
1489 | | |
1490 | | // ----- |
1491 | | |
1492 | | template <> |
1493 | | inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrFieldInternal( |
1494 | 0 | const Message& message, const FieldDescriptor* field) const { |
1495 | 0 | return *static_cast<const RepeatedPtrField<Message>*>(GetRawRepeatedField( |
1496 | 0 | message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr)); |
1497 | 0 | } |
1498 | | |
1499 | | template <> |
1500 | | inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrFieldInternal( |
1501 | 0 | Message* message, const FieldDescriptor* field) const { |
1502 | 0 | return static_cast<RepeatedPtrField<Message>*>(MutableRawRepeatedField( |
1503 | 0 | message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr)); |
1504 | 0 | } |
1505 | | |
1506 | | template <typename PB> |
1507 | | inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrFieldInternal( |
1508 | | const Message& message, const FieldDescriptor* field) const { |
1509 | | return *static_cast<const RepeatedPtrField<PB>*>( |
1510 | | GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, |
1511 | | PB::default_instance().GetDescriptor())); |
1512 | | } |
1513 | | |
1514 | | template <typename PB> |
1515 | | inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrFieldInternal( |
1516 | | Message* message, const FieldDescriptor* field) const { |
1517 | | return static_cast<RepeatedPtrField<PB>*>( |
1518 | | MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, |
1519 | | -1, PB::default_instance().GetDescriptor())); |
1520 | | } |
1521 | | |
1522 | | template <typename Type> |
1523 | | const Type& Reflection::DefaultRaw(const FieldDescriptor* field) const { |
1524 | | return *reinterpret_cast<const Type*>(schema_.GetFieldDefault(field)); |
1525 | | } |
1526 | | |
1527 | | uint32_t Reflection::GetOneofCase( |
1528 | 0 | const Message& message, const OneofDescriptor* oneof_descriptor) const { |
1529 | 0 | ABSL_DCHECK(!oneof_descriptor->is_synthetic()); |
1530 | 0 | return internal::GetConstRefAtOffset<uint32_t>( |
1531 | 0 | message, schema_.GetOneofCaseOffset(oneof_descriptor)); |
1532 | 0 | } |
1533 | | |
1534 | | bool Reflection::HasOneofField(const Message& message, |
1535 | 0 | const FieldDescriptor* field) const { |
1536 | 0 | return (GetOneofCase(message, field->containing_oneof()) == |
1537 | 0 | static_cast<uint32_t>(field->number())); |
1538 | 0 | } |
1539 | | |
1540 | 0 | const void* Reflection::GetSplitField(const Message* message) const { |
1541 | 0 | ABSL_DCHECK(schema_.IsSplit()); |
1542 | 0 | return *internal::GetConstPointerAtOffset<void*>(message, |
1543 | 0 | schema_.SplitOffset()); |
1544 | 0 | } |
1545 | | |
1546 | 0 | void** Reflection::MutableSplitField(Message* message) const { |
1547 | 0 | ABSL_DCHECK(schema_.IsSplit()); |
1548 | 0 | return internal::GetPointerAtOffset<void*>(message, schema_.SplitOffset()); |
1549 | 0 | } |
1550 | | |
1551 | | template <typename Type> |
1552 | | const Type& Reflection::GetRaw(const Message& message, |
1553 | | const FieldDescriptor* field) const { |
1554 | | ABSL_DCHECK(!schema_.InRealOneof(field) || HasOneofField(message, field)) |
1555 | | << "Field = " << field->full_name(); |
1556 | | if (schema_.IsSplit(field)) { |
1557 | | return *internal::GetConstPointerAtOffset<Type>( |
1558 | | GetSplitField(&message), schema_.GetFieldOffset(field)); |
1559 | | } |
1560 | | return internal::GetConstRefAtOffset<Type>(message, |
1561 | | schema_.GetFieldOffset(field)); |
1562 | | } |
1563 | | } // namespace protobuf |
1564 | | } // namespace google |
1565 | | |
1566 | | #include "google/protobuf/port_undef.inc" |
1567 | | |
1568 | | #endif // GOOGLE_PROTOBUF_MESSAGE_H__ |