From 6ef984af4b0c63c1c33127a12dcfc8e6359f0c9e Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Mon, 10 Nov 2014 17:34:54 -0800 Subject: Down-integrate from internal code base. --- src/google/protobuf/unknown_field_set.h | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/google/protobuf/unknown_field_set.h') diff --git a/src/google/protobuf/unknown_field_set.h b/src/google/protobuf/unknown_field_set.h index 31f17e2a..6f7a9fdb 100644 --- a/src/google/protobuf/unknown_field_set.h +++ b/src/google/protobuf/unknown_field_set.h @@ -88,6 +88,9 @@ class LIBPROTOBUF_EXPORT UnknownFieldSet { // Merge the contents of some other UnknownFieldSet with this one. void MergeFrom(const UnknownFieldSet& other); + // Similar to above, but this function will destroy the contents of other. + void MergeFromAndDestroy(UnknownFieldSet* other); + // Swaps the contents of some other UnknownFieldSet with this one. inline void Swap(UnknownFieldSet* x); @@ -141,12 +144,22 @@ class LIBPROTOBUF_EXPORT UnknownFieldSet { return ParseFromArray(data.data(), static_cast(data.size())); } + static const UnknownFieldSet* default_instance(); private: - + // For InternalMergeFrom + friend class UnknownField; + // Merges from other UnknownFieldSet. This method assumes, that this object + // is newly created and has fields_ == NULL; + void InternalMergeFrom(const UnknownFieldSet& other); void ClearFallback(); + // fields_ is either NULL, or a pointer to a vector that is *non-empty*. We + // never hold the empty vector because we want the 'do we have any unknown + // fields' check to be fast, and avoid a cache miss: the UFS instance gets + // embedded in the message object, so 'fields_ != NULL' tests a member + // variable hot in the cache, without the need to go touch a vector somewhere + // else in memory. std::vector* fields_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet); }; @@ -198,6 +211,10 @@ class LIBPROTOBUF_EXPORT UnknownField { // If this UnknownField contains a pointer, delete it. void Delete(); + // Reset all the underlying pointers to NULL. A special function to be only + // used while merging from a temporary UFS. + void Reset(); + // Make a deep copy of any pointers in this UnknownField. void DeepCopy(); @@ -222,13 +239,14 @@ class LIBPROTOBUF_EXPORT UnknownField { // inline implementations inline void UnknownFieldSet::Clear() { - if (fields_ != NULL) { + if (fields_) { ClearFallback(); } } inline bool UnknownFieldSet::empty() const { - return fields_ == NULL || fields_->empty(); + // Invariant: fields_ is never empty if present. + return !fields_; } inline void UnknownFieldSet::Swap(UnknownFieldSet* x) { @@ -236,9 +254,10 @@ inline void UnknownFieldSet::Swap(UnknownFieldSet* x) { } inline int UnknownFieldSet::field_count() const { - return (fields_ == NULL) ? 0 : static_cast(fields_->size()); + return fields_ ? static_cast(fields_->size()) : 0; } inline const UnknownField& UnknownFieldSet::field(int index) const { + GOOGLE_DCHECK(fields_ != NULL); return (*fields_)[index]; } inline UnknownField* UnknownFieldSet::mutable_field(int index) { -- cgit v1.2.3