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