| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #include <google/protobuf/unknown_field_set.h> | 35 #include <google/protobuf/unknown_field_set.h> |
| 36 | 36 |
| 37 #include <google/protobuf/stubs/logging.h> | 37 #include <google/protobuf/stubs/logging.h> |
| 38 #include <google/protobuf/stubs/common.h> | 38 #include <google/protobuf/stubs/common.h> |
| 39 #include <google/protobuf/io/coded_stream.h> | 39 #include <google/protobuf/io/coded_stream.h> |
| 40 #include <google/protobuf/io/zero_copy_stream.h> | 40 #include <google/protobuf/io/zero_copy_stream.h> |
| 41 #include <google/protobuf/io/zero_copy_stream_impl.h> | 41 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 42 #include <google/protobuf/metadata.h> | |
| 43 #include <google/protobuf/wire_format.h> | 42 #include <google/protobuf/wire_format.h> |
| 44 #include <google/protobuf/stubs/stl_util.h> | 43 #include <google/protobuf/stubs/stl_util.h> |
| 45 | 44 |
| 46 namespace google { | 45 namespace google { |
| 47 namespace protobuf { | 46 namespace protobuf { |
| 48 | 47 |
| 49 namespace { | 48 namespace { |
| 50 // This global instance is returned by unknown_fields() on any message class | 49 // This global instance is returned by unknown_fields() on any message class |
| 51 // when the object has no unknown fields. This is necessary because we now | 50 // when the object has no unknown fields. This is necessary because we now |
| 52 // instantiate the UnknownFieldSet dynamically only when required. | 51 // instantiate the UnknownFieldSet dynamically only when required. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 | 62 |
| 64 GOOGLE_PROTOBUF_DECLARE_ONCE(default_unknown_field_set_once_init_); | 63 GOOGLE_PROTOBUF_DECLARE_ONCE(default_unknown_field_set_once_init_); |
| 65 } | 64 } |
| 66 | 65 |
| 67 const UnknownFieldSet* UnknownFieldSet::default_instance() { | 66 const UnknownFieldSet* UnknownFieldSet::default_instance() { |
| 68 ::google::protobuf::GoogleOnceInit(&default_unknown_field_set_once_init_, | 67 ::google::protobuf::GoogleOnceInit(&default_unknown_field_set_once_init_, |
| 69 &InitDefaultUnknownFieldSet); | 68 &InitDefaultUnknownFieldSet); |
| 70 return default_unknown_field_set_instance_; | 69 return default_unknown_field_set_instance_; |
| 71 } | 70 } |
| 72 | 71 |
| 72 UnknownFieldSet::UnknownFieldSet() |
| 73 : fields_(NULL) {} |
| 74 |
| 75 UnknownFieldSet::~UnknownFieldSet() { |
| 76 Clear(); |
| 77 delete fields_; |
| 78 } |
| 79 |
| 73 void UnknownFieldSet::ClearFallback() { | 80 void UnknownFieldSet::ClearFallback() { |
| 74 GOOGLE_DCHECK(fields_ != NULL && fields_->size() > 0); | 81 if (fields_ != NULL) { |
| 75 int n = fields_->size(); | 82 for (int i = 0; i < fields_->size(); i++) { |
| 76 do { | 83 (*fields_)[i].Delete(); |
| 77 (*fields_)[--n].Delete(); | 84 } |
| 78 } while (n > 0); | 85 delete fields_; |
| 79 delete fields_; | 86 fields_ = NULL; |
| 80 fields_ = NULL; | 87 } |
| 88 } |
| 89 |
| 90 void UnknownFieldSet::ClearAndFreeMemory() { |
| 91 if (fields_ != NULL) { |
| 92 Clear(); |
| 93 } |
| 81 } | 94 } |
| 82 | 95 |
| 83 void UnknownFieldSet::InternalMergeFrom(const UnknownFieldSet& other) { | 96 void UnknownFieldSet::InternalMergeFrom(const UnknownFieldSet& other) { |
| 84 int other_field_count = other.field_count(); | 97 int other_field_count = other.field_count(); |
| 85 if (other_field_count > 0) { | 98 if (other_field_count > 0) { |
| 86 fields_ = new std::vector<UnknownField>(); | 99 fields_ = new vector<UnknownField>(); |
| 87 for (int i = 0; i < other_field_count; i++) { | 100 for (int i = 0; i < other_field_count; i++) { |
| 88 fields_->push_back((*other.fields_)[i]); | 101 fields_->push_back((*other.fields_)[i]); |
| 89 fields_->back().DeepCopy((*other.fields_)[i]); | 102 fields_->back().DeepCopy((*other.fields_)[i]); |
| 90 } | 103 } |
| 91 } | 104 } |
| 92 } | 105 } |
| 93 | 106 |
| 94 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) { | 107 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) { |
| 95 int other_field_count = other.field_count(); | 108 int other_field_count = other.field_count(); |
| 96 if (other_field_count > 0) { | 109 if (other_field_count > 0) { |
| 97 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 110 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 98 for (int i = 0; i < other_field_count; i++) { | 111 for (int i = 0; i < other_field_count; i++) { |
| 99 fields_->push_back((*other.fields_)[i]); | 112 fields_->push_back((*other.fields_)[i]); |
| 100 fields_->back().DeepCopy((*other.fields_)[i]); | 113 fields_->back().DeepCopy((*other.fields_)[i]); |
| 101 } | 114 } |
| 102 } | 115 } |
| 103 } | 116 } |
| 104 | 117 |
| 105 // A specialized MergeFrom for performance when we are merging from an UFS that | 118 // A specialized MergeFrom for performance when we are merging from an UFS that |
| 106 // is temporary and can be destroyed in the process. | 119 // is temporary and can be destroyed in the process. |
| 107 void UnknownFieldSet::MergeFromAndDestroy(UnknownFieldSet* other) { | 120 void UnknownFieldSet::MergeFromAndDestroy(UnknownFieldSet* other) { |
| 108 int other_field_count = other->field_count(); | 121 int other_field_count = other->field_count(); |
| 109 if (other_field_count > 0) { | 122 if (other_field_count > 0) { |
| 110 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 123 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 111 for (int i = 0; i < other_field_count; i++) { | 124 for (int i = 0; i < other_field_count; i++) { |
| 112 fields_->push_back((*other->fields_)[i]); | 125 fields_->push_back((*other->fields_)[i]); |
| 113 (*other->fields_)[i].Reset(); | 126 (*other->fields_)[i].Reset(); |
| 114 } | 127 } |
| 115 } | 128 } |
| 116 delete other->fields_; | 129 delete other->fields_; |
| 117 other->fields_ = NULL; | 130 other->fields_ = NULL; |
| 118 } | 131 } |
| 119 | 132 |
| 120 void UnknownFieldSet::MergeToInternalMetdata( | |
| 121 const UnknownFieldSet& other, | |
| 122 internal::InternalMetadataWithArena* metadata) { | |
| 123 metadata->mutable_unknown_fields()->MergeFrom(other); | |
| 124 } | |
| 125 | |
| 126 int UnknownFieldSet::SpaceUsedExcludingSelf() const { | 133 int UnknownFieldSet::SpaceUsedExcludingSelf() const { |
| 127 if (fields_ == NULL) return 0; | 134 if (fields_ == NULL) return 0; |
| 128 | 135 |
| 129 int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size(); | 136 int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size(); |
| 130 | 137 |
| 131 for (int i = 0; i < fields_->size(); i++) { | 138 for (int i = 0; i < fields_->size(); i++) { |
| 132 const UnknownField& field = (*fields_)[i]; | 139 const UnknownField& field = (*fields_)[i]; |
| 133 switch (field.type()) { | 140 switch (field.type()) { |
| 134 case UnknownField::TYPE_LENGTH_DELIMITED: | 141 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 135 total_size += sizeof(*field.length_delimited_.string_value_) + | 142 total_size += sizeof(*field.length_delimited_.string_value_) + |
| (...skipping 12 matching lines...) Expand all Loading... |
| 148 | 155 |
| 149 int UnknownFieldSet::SpaceUsed() const { | 156 int UnknownFieldSet::SpaceUsed() const { |
| 150 return sizeof(*this) + SpaceUsedExcludingSelf(); | 157 return sizeof(*this) + SpaceUsedExcludingSelf(); |
| 151 } | 158 } |
| 152 | 159 |
| 153 void UnknownFieldSet::AddVarint(int number, uint64 value) { | 160 void UnknownFieldSet::AddVarint(int number, uint64 value) { |
| 154 UnknownField field; | 161 UnknownField field; |
| 155 field.number_ = number; | 162 field.number_ = number; |
| 156 field.SetType(UnknownField::TYPE_VARINT); | 163 field.SetType(UnknownField::TYPE_VARINT); |
| 157 field.varint_ = value; | 164 field.varint_ = value; |
| 158 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 165 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 159 fields_->push_back(field); | 166 fields_->push_back(field); |
| 160 } | 167 } |
| 161 | 168 |
| 162 void UnknownFieldSet::AddFixed32(int number, uint32 value) { | 169 void UnknownFieldSet::AddFixed32(int number, uint32 value) { |
| 163 UnknownField field; | 170 UnknownField field; |
| 164 field.number_ = number; | 171 field.number_ = number; |
| 165 field.SetType(UnknownField::TYPE_FIXED32); | 172 field.SetType(UnknownField::TYPE_FIXED32); |
| 166 field.fixed32_ = value; | 173 field.fixed32_ = value; |
| 167 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 174 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 168 fields_->push_back(field); | 175 fields_->push_back(field); |
| 169 } | 176 } |
| 170 | 177 |
| 171 void UnknownFieldSet::AddFixed64(int number, uint64 value) { | 178 void UnknownFieldSet::AddFixed64(int number, uint64 value) { |
| 172 UnknownField field; | 179 UnknownField field; |
| 173 field.number_ = number; | 180 field.number_ = number; |
| 174 field.SetType(UnknownField::TYPE_FIXED64); | 181 field.SetType(UnknownField::TYPE_FIXED64); |
| 175 field.fixed64_ = value; | 182 field.fixed64_ = value; |
| 176 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 183 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 177 fields_->push_back(field); | 184 fields_->push_back(field); |
| 178 } | 185 } |
| 179 | 186 |
| 180 string* UnknownFieldSet::AddLengthDelimited(int number) { | 187 string* UnknownFieldSet::AddLengthDelimited(int number) { |
| 181 UnknownField field; | 188 UnknownField field; |
| 182 field.number_ = number; | 189 field.number_ = number; |
| 183 field.SetType(UnknownField::TYPE_LENGTH_DELIMITED); | 190 field.SetType(UnknownField::TYPE_LENGTH_DELIMITED); |
| 184 field.length_delimited_.string_value_ = new string; | 191 field.length_delimited_.string_value_ = new string; |
| 185 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 192 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 186 fields_->push_back(field); | 193 fields_->push_back(field); |
| 187 return field.length_delimited_.string_value_; | 194 return field.length_delimited_.string_value_; |
| 188 } | 195 } |
| 189 | 196 |
| 190 | 197 |
| 191 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) { | 198 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) { |
| 192 UnknownField field; | 199 UnknownField field; |
| 193 field.number_ = number; | 200 field.number_ = number; |
| 194 field.SetType(UnknownField::TYPE_GROUP); | 201 field.SetType(UnknownField::TYPE_GROUP); |
| 195 field.group_ = new UnknownFieldSet; | 202 field.group_ = new UnknownFieldSet; |
| 196 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 203 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 197 fields_->push_back(field); | 204 fields_->push_back(field); |
| 198 return field.group_; | 205 return field.group_; |
| 199 } | 206 } |
| 200 | 207 |
| 201 void UnknownFieldSet::AddField(const UnknownField& field) { | 208 void UnknownFieldSet::AddField(const UnknownField& field) { |
| 202 if (fields_ == NULL) fields_ = new std::vector<UnknownField>(); | 209 if (fields_ == NULL) fields_ = new vector<UnknownField>(); |
| 203 fields_->push_back(field); | 210 fields_->push_back(field); |
| 204 fields_->back().DeepCopy(field); | 211 fields_->back().DeepCopy(field); |
| 205 } | 212 } |
| 206 | 213 |
| 207 void UnknownFieldSet::DeleteSubrange(int start, int num) { | 214 void UnknownFieldSet::DeleteSubrange(int start, int num) { |
| 208 // Delete the specified fields. | 215 // Delete the specified fields. |
| 209 for (int i = 0; i < num; ++i) { | 216 for (int i = 0; i < num; ++i) { |
| 210 (*fields_)[i + start].Delete(); | 217 (*fields_)[i + start].Delete(); |
| 211 } | 218 } |
| 212 // Slide down the remaining fields. | 219 // Slide down the remaining fields. |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const { | 338 uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const { |
| 332 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type()); | 339 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type()); |
| 333 const string& data = *length_delimited_.string_value_; | 340 const string& data = *length_delimited_.string_value_; |
| 334 target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target); | 341 target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target); |
| 335 target = io::CodedOutputStream::WriteStringToArray(data, target); | 342 target = io::CodedOutputStream::WriteStringToArray(data, target); |
| 336 return target; | 343 return target; |
| 337 } | 344 } |
| 338 | 345 |
| 339 } // namespace protobuf | 346 } // namespace protobuf |
| 340 } // namespace google | 347 } // namespace google |
| OLD | NEW |