| 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 | // Authors: wink@google.com (Wink Saville), |
| 32 | // kenton@google.com (Kenton Varda) |
| 33 | // Based on original Protocol Buffers design by |
| 34 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 35 | // |
| 36 | // Defines MessageLite, the abstract interface implemented by all (lite |
| 37 | // and non-lite) protocol message objects. |
| 38 | |
| 39 | #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
| 40 | #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
| 41 | |
| 42 | |
| 43 | #include <climits> |
| 44 | #include <string> |
| 45 | |
| 46 | #include <google/protobuf/stubs/common.h> |
| 47 | #include <google/protobuf/stubs/logging.h> |
| 48 | #include <google/protobuf/io/coded_stream.h> |
| 49 | #include <google/protobuf/arena.h> |
| 50 | #include <google/protobuf/stubs/once.h> |
| 51 | #include <google/protobuf/port.h> |
| 52 | #include <google/protobuf/stubs/strutil.h> |
| 53 | #include <google/protobuf/explicitly_constructed.h> |
| 54 | #include <google/protobuf/metadata_lite.h> |
| 55 | #include <google/protobuf/stubs/hash.h> // TODO(b/211442718): cleanup |
| 56 | |
| 57 | // clang-format off |
| 58 | #include <google/protobuf/port_def.inc> |
| 59 | // clang-format on |
| 60 | |
| 61 | #ifdef SWIG |
| 62 | #error "You cannot SWIG proto headers" |
| 63 | #endif |
| 64 | |
| 65 | namespace google { |
| 66 | namespace protobuf { |
| 67 | |
| 68 | template <typename T> |
| 69 | class RepeatedPtrField; |
| 70 | |
| 71 | class FastReflectionMessageMutator; |
| 72 | class FastReflectionStringSetter; |
| 73 | class Reflection; |
| 74 | |
| 75 | namespace io { |
| 76 | |
| 77 | class CodedInputStream; |
| 78 | class CodedOutputStream; |
| 79 | class ZeroCopyInputStream; |
| 80 | class ZeroCopyOutputStream; |
| 81 | |
| 82 | } // namespace io |
| 83 | namespace internal { |
| 84 | |
| 85 | class SwapFieldHelper; |
| 86 | |
| 87 | // See parse_context.h for explanation |
| 88 | class ParseContext; |
| 89 | |
| 90 | class ExtensionSet; |
| 91 | class LazyField; |
| 92 | class RepeatedPtrFieldBase; |
| 93 | class TcParser; |
| 94 | class WireFormatLite; |
| 95 | class WeakFieldMap; |
| 96 | |
| 97 | template <typename Type> |
| 98 | class GenericTypeHandler; // defined in repeated_field.h |
| 99 | |
| 100 | // We compute sizes as size_t but cache them as int. This function converts a |
| 101 | // computed size to a cached size. Since we don't proceed with serialization |
| 102 | // if the total size was > INT_MAX, it is not important what this function |
| 103 | // returns for inputs > INT_MAX. However this case should not error or |
| 104 | // GOOGLE_CHECK-fail, because the full size_t resolution is still returned from |
| 105 | // ByteSizeLong() and checked against INT_MAX; we can catch the overflow |
| 106 | // there. |
| 107 | inline int ToCachedSize(size_t size) { return static_cast<int>(size); } |
| 108 | |
| 109 | // We mainly calculate sizes in terms of size_t, but some functions that |
| 110 | // compute sizes return "int". These int sizes are expected to always be |
| 111 | // positive. This function is more efficient than casting an int to size_t |
| 112 | // directly on 64-bit platforms because it avoids making the compiler emit a |
| 113 | // sign extending instruction, which we don't want and don't want to pay for. |
| 114 | inline size_t FromIntSize(int size) { |
| 115 | // Convert to unsigned before widening so sign extension is not necessary. |
| 116 | return static_cast<unsigned int>(size); |
| 117 | } |
| 118 | |
| 119 | // For cases where a legacy function returns an integer size. We GOOGLE_DCHECK() |
| 120 | // that the conversion will fit within an integer; if this is false then we |
| 121 | // are losing information. |
| 122 | inline int ToIntSize(size_t size) { |
| 123 | GOOGLE_DCHECK_LE(size, static_cast<size_t>(INT_MAX)); |
| 124 | return static_cast<int>(size); |
| 125 | } |
| 126 | |
| 127 | // Default empty string object. Don't use this directly. Instead, call |
| 128 | // GetEmptyString() to get the reference. This empty string is aligned with a |
| 129 | // minimum alignment of 8 bytes to match the requirement of ArenaStringPtr. |
| 130 | PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString |
| 131 | fixed_address_empty_string; |
| 132 | |
| 133 | |
| 134 | PROTOBUF_EXPORT constexpr const std::string& GetEmptyStringAlreadyInited() { |
| 135 | return fixed_address_empty_string.get(); |
| 136 | } |
| 137 | |
| 138 | PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str); |
| 139 | |
| 140 | } // namespace internal |
| 141 | |
| 142 | // Interface to light weight protocol messages. |
| 143 | // |
| 144 | // This interface is implemented by all protocol message objects. Non-lite |
| 145 | // messages additionally implement the Message interface, which is a |
| 146 | // subclass of MessageLite. Use MessageLite instead when you only need |
| 147 | // the subset of features which it supports -- namely, nothing that uses |
| 148 | // descriptors or reflection. You can instruct the protocol compiler |
| 149 | // to generate classes which implement only MessageLite, not the full |
| 150 | // Message interface, by adding the following line to the .proto file: |
| 151 | // |
| 152 | // option optimize_for = LITE_RUNTIME; |
| 153 | // |
| 154 | // This is particularly useful on resource-constrained systems where |
| 155 | // the full protocol buffers runtime library is too big. |
| 156 | // |
| 157 | // Note that on non-constrained systems (e.g. servers) when you need |
| 158 | // to link in lots of protocol definitions, a better way to reduce |
| 159 | // total code footprint is to use optimize_for = CODE_SIZE. This |
| 160 | // will make the generated code smaller while still supporting all the |
| 161 | // same features (at the expense of speed). optimize_for = LITE_RUNTIME |
| 162 | // is best when you only have a small number of message types linked |
| 163 | // into your binary, in which case the size of the protocol buffers |
| 164 | // runtime itself is the biggest problem. |
| 165 | // |
| 166 | // Users must not derive from this class. Only the protocol compiler and |
| 167 | // the internal library are allowed to create subclasses. |
| 168 | class PROTOBUF_EXPORT MessageLite { |
| 169 | public: |
| 170 | constexpr MessageLite() {} |
| 171 | virtual ~MessageLite() = default; |
| 172 | |
| 173 | // Basic Operations ------------------------------------------------ |
| 174 | |
| 175 | // Get the name of this message type, e.g. "foo.bar.BazProto". |
| 176 | virtual std::string GetTypeName() const = 0; |
| 177 | |
| 178 | // Construct a new instance of the same type. Ownership is passed to the |
| 179 | // caller. |
| 180 | MessageLite* New() const { return New(arena: nullptr); } |
| 181 | |
| 182 | // Construct a new instance on the arena. Ownership is passed to the caller |
| 183 | // if arena is a nullptr. |
| 184 | virtual MessageLite* New(Arena* arena) const = 0; |
| 185 | |
| 186 | // Returns user-owned arena; nullptr if it's message owned. |
| 187 | Arena* GetArena() const { return _internal_metadata_.user_arena(); } |
| 188 | |
| 189 | // Clear all fields of the message and set them to their default values. |
| 190 | // Clear() assumes that any memory allocated to hold parts of the message |
| 191 | // will likely be needed again, so the memory used may not be freed. |
| 192 | // To ensure that all memory used by a Message is freed, you must delete it. |
| 193 | virtual void Clear() = 0; |
| 194 | |
| 195 | // Quickly check if all required fields have values set. |
| 196 | virtual bool IsInitialized() const = 0; |
| 197 | |
| 198 | // This is not implemented for Lite messages -- it just returns "(cannot |
| 199 | // determine missing fields for lite message)". However, it is implemented |
| 200 | // for full messages. See message.h. |
| 201 | virtual std::string InitializationErrorString() const; |
| 202 | |
| 203 | // If |other| is the exact same class as this, calls MergeFrom(). Otherwise, |
| 204 | // results are undefined (probably crash). |
| 205 | virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0; |
| 206 | |
| 207 | // These methods return a human-readable summary of the message. Note that |
| 208 | // since the MessageLite interface does not support reflection, there is very |
| 209 | // little information that these methods can provide. They are shadowed by |
| 210 | // methods of the same name on the Message interface which provide much more |
| 211 | // information. The methods here are intended primarily to facilitate code |
| 212 | // reuse for logic that needs to interoperate with both full and lite protos. |
| 213 | // |
| 214 | // The format of the returned string is subject to change, so please do not |
| 215 | // assume it will remain stable over time. |
| 216 | std::string DebugString() const; |
| 217 | std::string ShortDebugString() const { return DebugString(); } |
| 218 | // MessageLite::DebugString is already Utf8 Safe. This is to add compatibility |
| 219 | // with Message. |
| 220 | std::string Utf8DebugString() const { return DebugString(); } |
| 221 | |
| 222 | // Parsing --------------------------------------------------------- |
| 223 | // Methods for parsing in protocol buffer format. Most of these are |
| 224 | // just simple wrappers around MergeFromCodedStream(). Clear() will be |
| 225 | // called before merging the input. |
| 226 | |
| 227 | // Fill the message with a protocol buffer parsed from the given input |
| 228 | // stream. Returns false on a read error or if the input is in the wrong |
| 229 | // format. A successful return does not indicate the entire input is |
| 230 | // consumed, ensure you call ConsumedEntireMessage() to check that if |
| 231 | // applicable. |
| 232 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromCodedStream( |
| 233 | io::CodedInputStream* input); |
| 234 | // Like ParseFromCodedStream(), but accepts messages that are missing |
| 235 | // required fields. |
| 236 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCodedStream( |
| 237 | io::CodedInputStream* input); |
| 238 | // Read a protocol buffer from the given zero-copy input stream. If |
| 239 | // successful, the entire input will be consumed. |
| 240 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromZeroCopyStream( |
| 241 | io::ZeroCopyInputStream* input); |
| 242 | // Like ParseFromZeroCopyStream(), but accepts messages that are missing |
| 243 | // required fields. |
| 244 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromZeroCopyStream( |
| 245 | io::ZeroCopyInputStream* input); |
| 246 | // Parse a protocol buffer from a file descriptor. If successful, the entire |
| 247 | // input will be consumed. |
| 248 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromFileDescriptor( |
| 249 | int file_descriptor); |
| 250 | // Like ParseFromFileDescriptor(), but accepts messages that are missing |
| 251 | // required fields. |
| 252 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromFileDescriptor( |
| 253 | int file_descriptor); |
| 254 | // Parse a protocol buffer from a C++ istream. If successful, the entire |
| 255 | // input will be consumed. |
| 256 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromIstream(std::istream* input); |
| 257 | // Like ParseFromIstream(), but accepts messages that are missing |
| 258 | // required fields. |
| 259 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromIstream( |
| 260 | std::istream* input); |
| 261 | // Read a protocol buffer from the given zero-copy input stream, expecting |
| 262 | // the message to be exactly "size" bytes long. If successful, exactly |
| 263 | // this many bytes will have been consumed from the input. |
| 264 | bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, |
| 265 | int size); |
| 266 | // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are |
| 267 | // missing required fields. |
| 268 | bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size); |
| 269 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromBoundedZeroCopyStream( |
| 270 | io::ZeroCopyInputStream* input, int size); |
| 271 | // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are |
| 272 | // missing required fields. |
| 273 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream( |
| 274 | io::ZeroCopyInputStream* input, int size); |
| 275 | // Parses a protocol buffer contained in a string. Returns true on success. |
| 276 | // This function takes a string in the (non-human-readable) binary wire |
| 277 | // format, matching the encoding output by MessageLite::SerializeToString(). |
| 278 | // If you'd like to convert a human-readable string into a protocol buffer |
| 279 | // object, see google::protobuf::TextFormat::ParseFromString(). |
| 280 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(ConstStringParam data); |
| 281 | // Like ParseFromString(), but accepts messages that are missing |
| 282 | // required fields. |
| 283 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString( |
| 284 | ConstStringParam data); |
| 285 | // Parse a protocol buffer contained in an array of bytes. |
| 286 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, |
| 287 | int size); |
| 288 | // Like ParseFromArray(), but accepts messages that are missing |
| 289 | // required fields. |
| 290 | PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data, |
| 291 | int size); |
| 292 | |
| 293 | |
| 294 | // Reads a protocol buffer from the stream and merges it into this |
| 295 | // Message. Singular fields read from the what is |
| 296 | // already in the Message and repeated fields are appended to those |
| 297 | // already present. |
| 298 | // |
| 299 | // It is the responsibility of the caller to call input->LastTagWas() |
| 300 | // (for groups) or input->ConsumedEntireMessage() (for non-groups) after |
| 301 | // this returns to verify that the message's end was delimited correctly. |
| 302 | // |
| 303 | // ParseFromCodedStream() is implemented as Clear() followed by |
| 304 | // MergeFromCodedStream(). |
| 305 | bool MergeFromCodedStream(io::CodedInputStream* input); |
| 306 | |
| 307 | // Like MergeFromCodedStream(), but succeeds even if required fields are |
| 308 | // missing in the input. |
| 309 | // |
| 310 | // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream() |
| 311 | // followed by IsInitialized(). |
| 312 | bool MergePartialFromCodedStream(io::CodedInputStream* input); |
| 313 | |
| 314 | // Merge a protocol buffer contained in a string. |
| 315 | bool MergeFromString(ConstStringParam data); |
| 316 | |
| 317 | |
| 318 | // Serialization --------------------------------------------------- |
| 319 | // Methods for serializing in protocol buffer format. Most of these |
| 320 | // are just simple wrappers around ByteSize() and SerializeWithCachedSizes(). |
| 321 | |
| 322 | // Write a protocol buffer of this message to the given output. Returns |
| 323 | // false on a write error. If the message is missing required fields, |
| 324 | // this may GOOGLE_CHECK-fail. |
| 325 | bool SerializeToCodedStream(io::CodedOutputStream* output) const; |
| 326 | // Like SerializeToCodedStream(), but allows missing required fields. |
| 327 | bool SerializePartialToCodedStream(io::CodedOutputStream* output) const; |
| 328 | // Write the message to the given zero-copy output stream. All required |
| 329 | // fields must be set. |
| 330 | bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const; |
| 331 | // Like SerializeToZeroCopyStream(), but allows missing required fields. |
| 332 | bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const; |
| 333 | // Serialize the message and store it in the given string. All required |
| 334 | // fields must be set. |
| 335 | bool SerializeToString(std::string* output) const; |
| 336 | // Like SerializeToString(), but allows missing required fields. |
| 337 | bool SerializePartialToString(std::string* output) const; |
| 338 | // Serialize the message and store it in the given byte array. All required |
| 339 | // fields must be set. |
| 340 | bool SerializeToArray(void* data, int size) const; |
| 341 | // Like SerializeToArray(), but allows missing required fields. |
| 342 | bool SerializePartialToArray(void* data, int size) const; |
| 343 | |
| 344 | // Make a string encoding the message. Is equivalent to calling |
| 345 | // SerializeToString() on a string and using that. Returns the empty |
| 346 | // string if SerializeToString() would have returned an error. |
| 347 | // Note: If you intend to generate many such strings, you may |
| 348 | // reduce heap fragmentation by instead re-using the same string |
| 349 | // object with calls to SerializeToString(). |
| 350 | std::string SerializeAsString() const; |
| 351 | // Like SerializeAsString(), but allows missing required fields. |
| 352 | std::string SerializePartialAsString() const; |
| 353 | |
| 354 | // Serialize the message and write it to the given file descriptor. All |
| 355 | // required fields must be set. |
| 356 | bool SerializeToFileDescriptor(int file_descriptor) const; |
| 357 | // Like SerializeToFileDescriptor(), but allows missing required fields. |
| 358 | bool SerializePartialToFileDescriptor(int file_descriptor) const; |
| 359 | // Serialize the message and write it to the given C++ ostream. All |
| 360 | // required fields must be set. |
| 361 | bool SerializeToOstream(std::ostream* output) const; |
| 362 | // Like SerializeToOstream(), but allows missing required fields. |
| 363 | bool SerializePartialToOstream(std::ostream* output) const; |
| 364 | |
| 365 | // Like SerializeToString(), but appends to the data to the string's |
| 366 | // existing contents. All required fields must be set. |
| 367 | bool AppendToString(std::string* output) const; |
| 368 | // Like AppendToString(), but allows missing required fields. |
| 369 | bool AppendPartialToString(std::string* output) const; |
| 370 | |
| 371 | |
| 372 | // Computes the serialized size of the message. This recursively calls |
| 373 | // ByteSizeLong() on all embedded messages. |
| 374 | // |
| 375 | // ByteSizeLong() is generally linear in the number of fields defined for the |
| 376 | // proto. |
| 377 | virtual size_t ByteSizeLong() const = 0; |
| 378 | |
| 379 | // Legacy ByteSize() API. |
| 380 | PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead" ) |
| 381 | int ByteSize() const { return internal::ToIntSize(size: ByteSizeLong()); } |
| 382 | |
| 383 | // Serializes the message without recomputing the size. The message must not |
| 384 | // have changed since the last call to ByteSize(), and the value returned by |
| 385 | // ByteSize must be non-negative. Otherwise the results are undefined. |
| 386 | void SerializeWithCachedSizes(io::CodedOutputStream* output) const { |
| 387 | output->SetCur(_InternalSerialize(ptr: output->Cur(), stream: output->EpsCopy())); |
| 388 | } |
| 389 | |
| 390 | // Functions below here are not part of the public interface. It isn't |
| 391 | // enforced, but they should be treated as private, and will be private |
| 392 | // at some future time. Unfortunately the implementation of the "friend" |
| 393 | // keyword in GCC is broken at the moment, but we expect it will be fixed. |
| 394 | |
| 395 | // Like SerializeWithCachedSizes, but writes directly to *target, returning |
| 396 | // a pointer to the byte immediately after the last byte written. "target" |
| 397 | // must point at a byte array of at least ByteSize() bytes. Whether to use |
| 398 | // deterministic serialization, e.g., maps in sorted order, is determined by |
| 399 | // CodedOutputStream::IsDefaultSerializationDeterministic(). |
| 400 | uint8_t* SerializeWithCachedSizesToArray(uint8_t* target) const; |
| 401 | |
| 402 | // Returns the result of the last call to ByteSize(). An embedded message's |
| 403 | // size is needed both to serialize it (because embedded messages are |
| 404 | // length-delimited) and to compute the outer message's size. Caching |
| 405 | // the size avoids computing it multiple times. |
| 406 | // |
| 407 | // ByteSize() does not automatically use the cached size when available |
| 408 | // because this would require invalidating it every time the message was |
| 409 | // modified, which would be too hard and expensive. (E.g. if a deeply-nested |
| 410 | // sub-message is changed, all of its parents' cached sizes would need to be |
| 411 | // invalidated, which is too much work for an otherwise inlined setter |
| 412 | // method.) |
| 413 | virtual int GetCachedSize() const = 0; |
| 414 | |
| 415 | virtual const char* _InternalParse(const char* /*ptr*/, |
| 416 | internal::ParseContext* /*ctx*/) { |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
| 420 | virtual void OnDemandRegisterArenaDtor(Arena* /*arena*/) {} |
| 421 | |
| 422 | protected: |
| 423 | template <typename T> |
| 424 | static T* CreateMaybeMessage(Arena* arena) { |
| 425 | return Arena::CreateMaybeMessage<T>(arena); |
| 426 | } |
| 427 | |
| 428 | inline explicit MessageLite(Arena* arena, bool is_message_owned = false) |
| 429 | : _internal_metadata_(arena, is_message_owned) {} |
| 430 | |
| 431 | // Returns the arena, if any, that directly owns this message and its internal |
| 432 | // memory (Arena::Own is different in that the arena doesn't directly own the |
| 433 | // internal memory). This method is used in proto's implementation for |
| 434 | // swapping, moving and setting allocated, for deciding whether the ownership |
| 435 | // of this message or its internal memory could be changed. |
| 436 | Arena* GetOwningArena() const { return _internal_metadata_.owning_arena(); } |
| 437 | |
| 438 | // Returns the arena, used for allocating internal objects(e.g., child |
| 439 | // messages, etc), or owning incoming objects (e.g., set allocated). |
| 440 | Arena* GetArenaForAllocation() const { return _internal_metadata_.arena(); } |
| 441 | |
| 442 | // Returns true if this message is enabled for message-owned arena (MOA) |
| 443 | // trials. No lite messages are eligible for MOA. |
| 444 | static bool InMoaTrial() { return false; } |
| 445 | |
| 446 | internal::InternalMetadata _internal_metadata_; |
| 447 | |
| 448 | public: |
| 449 | enum ParseFlags { |
| 450 | kMerge = 0, |
| 451 | kParse = 1, |
| 452 | kMergePartial = 2, |
| 453 | kParsePartial = 3, |
| 454 | kMergeWithAliasing = 4, |
| 455 | kParseWithAliasing = 5, |
| 456 | kMergePartialWithAliasing = 6, |
| 457 | kParsePartialWithAliasing = 7 |
| 458 | }; |
| 459 | |
| 460 | template <ParseFlags flags, typename T> |
| 461 | bool ParseFrom(const T& input); |
| 462 | |
| 463 | // Fast path when conditions match (ie. non-deterministic) |
| 464 | // uint8_t* _InternalSerialize(uint8_t* ptr) const; |
| 465 | virtual uint8_t* _InternalSerialize( |
| 466 | uint8_t* ptr, io::EpsCopyOutputStream* stream) const = 0; |
| 467 | |
| 468 | // Identical to IsInitialized() except that it logs an error message. |
| 469 | bool IsInitializedWithErrors() const { |
| 470 | if (IsInitialized()) return true; |
| 471 | LogInitializationErrorMessage(); |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | private: |
| 476 | friend class FastReflectionMessageMutator; |
| 477 | friend class FastReflectionStringSetter; |
| 478 | friend class Message; |
| 479 | friend class Reflection; |
| 480 | friend class internal::ExtensionSet; |
| 481 | friend class internal::LazyField; |
| 482 | friend class internal::SwapFieldHelper; |
| 483 | friend class internal::TcParser; |
| 484 | friend class internal::WeakFieldMap; |
| 485 | friend class internal::WireFormatLite; |
| 486 | |
| 487 | template <typename Type> |
| 488 | friend class Arena::InternalHelper; |
| 489 | template <typename Type> |
| 490 | friend class internal::GenericTypeHandler; |
| 491 | |
| 492 | void LogInitializationErrorMessage() const; |
| 493 | |
| 494 | bool MergeFromImpl(io::CodedInputStream* input, ParseFlags parse_flags); |
| 495 | |
| 496 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite); |
| 497 | }; |
| 498 | |
| 499 | namespace internal { |
| 500 | |
| 501 | template <bool alias> |
| 502 | bool MergeFromImpl(StringPiece input, MessageLite* msg, |
| 503 | MessageLite::ParseFlags parse_flags); |
| 504 | extern template bool MergeFromImpl<false>(StringPiece input, |
| 505 | MessageLite* msg, |
| 506 | MessageLite::ParseFlags parse_flags); |
| 507 | extern template bool MergeFromImpl<true>(StringPiece input, |
| 508 | MessageLite* msg, |
| 509 | MessageLite::ParseFlags parse_flags); |
| 510 | |
| 511 | template <bool alias> |
| 512 | bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, |
| 513 | MessageLite::ParseFlags parse_flags); |
| 514 | extern template bool MergeFromImpl<false>(io::ZeroCopyInputStream* input, |
| 515 | MessageLite* msg, |
| 516 | MessageLite::ParseFlags parse_flags); |
| 517 | extern template bool MergeFromImpl<true>(io::ZeroCopyInputStream* input, |
| 518 | MessageLite* msg, |
| 519 | MessageLite::ParseFlags parse_flags); |
| 520 | |
| 521 | struct BoundedZCIS { |
| 522 | io::ZeroCopyInputStream* zcis; |
| 523 | int limit; |
| 524 | }; |
| 525 | |
| 526 | template <bool alias> |
| 527 | bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, |
| 528 | MessageLite::ParseFlags parse_flags); |
| 529 | extern template bool MergeFromImpl<false>(BoundedZCIS input, MessageLite* msg, |
| 530 | MessageLite::ParseFlags parse_flags); |
| 531 | extern template bool MergeFromImpl<true>(BoundedZCIS input, MessageLite* msg, |
| 532 | MessageLite::ParseFlags parse_flags); |
| 533 | |
| 534 | template <typename T> |
| 535 | struct SourceWrapper; |
| 536 | |
| 537 | template <bool alias, typename T> |
| 538 | bool MergeFromImpl(const SourceWrapper<T>& input, MessageLite* msg, |
| 539 | MessageLite::ParseFlags parse_flags) { |
| 540 | return input.template MergeInto<alias>(msg, parse_flags); |
| 541 | } |
| 542 | |
| 543 | } // namespace internal |
| 544 | |
| 545 | template <MessageLite::ParseFlags flags, typename T> |
| 546 | bool MessageLite::ParseFrom(const T& input) { |
| 547 | if (flags & kParse) Clear(); |
| 548 | constexpr bool alias = (flags & kMergeWithAliasing) != 0; |
| 549 | return internal::MergeFromImpl<alias>(input, this, flags); |
| 550 | } |
| 551 | |
| 552 | // =================================================================== |
| 553 | // Shutdown support. |
| 554 | |
| 555 | |
| 556 | // Shut down the entire protocol buffers library, deleting all static-duration |
| 557 | // objects allocated by the library or by generated .pb.cc files. |
| 558 | // |
| 559 | // There are two reasons you might want to call this: |
| 560 | // * You use a draconian definition of "memory leak" in which you expect |
| 561 | // every single malloc() to have a corresponding free(), even for objects |
| 562 | // which live until program exit. |
| 563 | // * You are writing a dynamically-loaded library which needs to clean up |
| 564 | // after itself when the library is unloaded. |
| 565 | // |
| 566 | // It is safe to call this multiple times. However, it is not safe to use |
| 567 | // any other part of the protocol buffers library after |
| 568 | // ShutdownProtobufLibrary() has been called. Furthermore this call is not |
| 569 | // thread safe, user needs to synchronize multiple calls. |
| 570 | PROTOBUF_EXPORT void ShutdownProtobufLibrary(); |
| 571 | |
| 572 | namespace internal { |
| 573 | |
| 574 | // Register a function to be called when ShutdownProtocolBuffers() is called. |
| 575 | PROTOBUF_EXPORT void OnShutdown(void (*func)()); |
| 576 | // Run an arbitrary function on an arg |
| 577 | PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg); |
| 578 | |
| 579 | template <typename T> |
| 580 | T* OnShutdownDelete(T* p) { |
| 581 | OnShutdownRun([](const void* pp) { delete static_cast<const T*>(pp); }, p); |
| 582 | return p; |
| 583 | } |
| 584 | |
| 585 | } // namespace internal |
| 586 | } // namespace protobuf |
| 587 | } // namespace google |
| 588 | |
| 589 | #include <google/protobuf/port_undef.inc> |
| 590 | |
| 591 | #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__ |
| 592 | |