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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // If the symbol name is invalid it could break our lookup algorithm (which | 90 // If the symbol name is invalid it could break our lookup algorithm (which |
91 // relies on the fact that '.' sorts before all other characters that are | 91 // relies on the fact that '.' sorts before all other characters that are |
92 // valid in symbol names). | 92 // valid in symbol names). |
93 if (!ValidateSymbolName(name)) { | 93 if (!ValidateSymbolName(name)) { |
94 GOOGLE_LOG(ERROR) << "Invalid symbol name: " << name; | 94 GOOGLE_LOG(ERROR) << "Invalid symbol name: " << name; |
95 return false; | 95 return false; |
96 } | 96 } |
97 | 97 |
98 // Try to look up the symbol to make sure a super-symbol doesn't already | 98 // Try to look up the symbol to make sure a super-symbol doesn't already |
99 // exist. | 99 // exist. |
100 typename std::map<string, Value>::iterator iter = FindLastLessOrEqual(name); | 100 typename map<string, Value>::iterator iter = FindLastLessOrEqual(name); |
101 | 101 |
102 if (iter == by_symbol_.end()) { | 102 if (iter == by_symbol_.end()) { |
103 // Apparently the map is currently empty. Just insert and be done with it. | 103 // Apparently the map is currently empty. Just insert and be done with it. |
104 by_symbol_.insert( | 104 by_symbol_.insert(typename map<string, Value>::value_type(name, value)); |
105 typename std::map<string, Value>::value_type(name, value)); | |
106 return true; | 105 return true; |
107 } | 106 } |
108 | 107 |
109 if (IsSubSymbol(iter->first, name)) { | 108 if (IsSubSymbol(iter->first, name)) { |
110 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis
ting " | 109 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis
ting " |
111 "symbol \"" << iter->first << "\"."; | 110 "symbol \"" << iter->first << "\"."; |
112 return false; | 111 return false; |
113 } | 112 } |
114 | 113 |
115 // OK, that worked. Now we have to make sure that no symbol in the map is | 114 // OK, that worked. Now we have to make sure that no symbol in the map is |
116 // a sub-symbol of the one we are inserting. The only symbol which could | 115 // a sub-symbol of the one we are inserting. The only symbol which could |
117 // be so is the first symbol that is greater than the new symbol. Since | 116 // be so is the first symbol that is greater than the new symbol. Since |
118 // |iter| points at the last symbol that is less than or equal, we just have | 117 // |iter| points at the last symbol that is less than or equal, we just have |
119 // to increment it. | 118 // to increment it. |
120 ++iter; | 119 ++iter; |
121 | 120 |
122 if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) { | 121 if (iter != by_symbol_.end() && IsSubSymbol(name, iter->first)) { |
123 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis
ting " | 122 GOOGLE_LOG(ERROR) << "Symbol name \"" << name << "\" conflicts with the exis
ting " |
124 "symbol \"" << iter->first << "\"."; | 123 "symbol \"" << iter->first << "\"."; |
125 return false; | 124 return false; |
126 } | 125 } |
127 | 126 |
128 // OK, no conflicts. | 127 // OK, no conflicts. |
129 | 128 |
130 // Insert the new symbol using the iterator as a hint, the new entry will | 129 // Insert the new symbol using the iterator as a hint, the new entry will |
131 // appear immediately before the one the iterator is pointing at. | 130 // appear immediately before the one the iterator is pointing at. |
132 by_symbol_.insert(iter, | 131 by_symbol_.insert(iter, typename map<string, Value>::value_type(name, value)); |
133 typename std::map<string, Value>::value_type(name, value)); | |
134 | 132 |
135 return true; | 133 return true; |
136 } | 134 } |
137 | 135 |
138 template <typename Value> | 136 template <typename Value> |
139 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions( | 137 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::AddNestedExtensions( |
140 const DescriptorProto& message_type, | 138 const DescriptorProto& message_type, |
141 Value value) { | 139 Value value) { |
142 for (int i = 0; i < message_type.nested_type_size(); i++) { | 140 for (int i = 0; i < message_type.nested_type_size(); i++) { |
143 if (!AddNestedExtensions(message_type.nested_type(i), value)) return false; | 141 if (!AddNestedExtensions(message_type.nested_type(i), value)) return false; |
(...skipping 30 matching lines...) Expand all Loading... |
174 | 172 |
175 template <typename Value> | 173 template <typename Value> |
176 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindFile( | 174 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindFile( |
177 const string& filename) { | 175 const string& filename) { |
178 return FindWithDefault(by_name_, filename, Value()); | 176 return FindWithDefault(by_name_, filename, Value()); |
179 } | 177 } |
180 | 178 |
181 template <typename Value> | 179 template <typename Value> |
182 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindSymbol( | 180 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindSymbol( |
183 const string& name) { | 181 const string& name) { |
184 typename std::map<string, Value>::iterator iter = FindLastLessOrEqual(name); | 182 typename map<string, Value>::iterator iter = FindLastLessOrEqual(name); |
185 | 183 |
186 return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ? | 184 return (iter != by_symbol_.end() && IsSubSymbol(iter->first, name)) ? |
187 iter->second : Value(); | 185 iter->second : Value(); |
188 } | 186 } |
189 | 187 |
190 template <typename Value> | 188 template <typename Value> |
191 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindExtension( | 189 Value SimpleDescriptorDatabase::DescriptorIndex<Value>::FindExtension( |
192 const string& containing_type, | 190 const string& containing_type, |
193 int field_number) { | 191 int field_number) { |
194 return FindWithDefault( | 192 return FindWithDefault( |
195 by_extension_, std::make_pair(containing_type, field_number), Value()); | 193 by_extension_, std::make_pair(containing_type, field_number), Value()); |
196 } | 194 } |
197 | 195 |
198 template <typename Value> | 196 template <typename Value> |
199 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::FindAllExtensionNumbers( | 197 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::FindAllExtensionNumbers( |
200 const string& containing_type, | 198 const string& containing_type, |
201 std::vector<int>* output) { | 199 vector<int>* output) { |
202 typename std::map<pair<string, int>, Value>::const_iterator it = | 200 typename map<pair<string, int>, Value>::const_iterator it = |
203 by_extension_.lower_bound(std::make_pair(containing_type, 0)); | 201 by_extension_.lower_bound(std::make_pair(containing_type, 0)); |
204 bool success = false; | 202 bool success = false; |
205 | 203 |
206 for (; it != by_extension_.end() && it->first.first == containing_type; | 204 for (; it != by_extension_.end() && it->first.first == containing_type; |
207 ++it) { | 205 ++it) { |
208 output->push_back(it->first.second); | 206 output->push_back(it->first.second); |
209 success = true; | 207 success = true; |
210 } | 208 } |
211 | 209 |
212 return success; | 210 return success; |
213 } | 211 } |
214 | 212 |
215 template <typename Value> | 213 template <typename Value> |
216 typename map<string, Value>::iterator | 214 typename map<string, Value>::iterator |
217 SimpleDescriptorDatabase::DescriptorIndex<Value>::FindLastLessOrEqual( | 215 SimpleDescriptorDatabase::DescriptorIndex<Value>::FindLastLessOrEqual( |
218 const string& name) { | 216 const string& name) { |
219 // Find the last key in the map which sorts less than or equal to the | 217 // Find the last key in the map which sorts less than or equal to the |
220 // symbol name. Since upper_bound() returns the *first* key that sorts | 218 // symbol name. Since upper_bound() returns the *first* key that sorts |
221 // *greater* than the input, we want the element immediately before that. | 219 // *greater* than the input, we want the element immediately before that. |
222 typename std::map<string, Value>::iterator iter = | 220 typename map<string, Value>::iterator iter = by_symbol_.upper_bound(name); |
223 by_symbol_.upper_bound(name); | |
224 if (iter != by_symbol_.begin()) --iter; | 221 if (iter != by_symbol_.begin()) --iter; |
225 return iter; | 222 return iter; |
226 } | 223 } |
227 | 224 |
228 template <typename Value> | 225 template <typename Value> |
229 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::IsSubSymbol( | 226 bool SimpleDescriptorDatabase::DescriptorIndex<Value>::IsSubSymbol( |
230 const string& sub_symbol, const string& super_symbol) { | 227 const string& sub_symbol, const string& super_symbol) { |
231 return sub_symbol == super_symbol || | 228 return sub_symbol == super_symbol || |
232 (HasPrefixString(super_symbol, sub_symbol) && | 229 (HasPrefixString(super_symbol, sub_symbol) && |
233 super_symbol[sub_symbol.size()] == '.'); | 230 super_symbol[sub_symbol.size()] == '.'); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 | 277 |
281 bool SimpleDescriptorDatabase::FindFileContainingExtension( | 278 bool SimpleDescriptorDatabase::FindFileContainingExtension( |
282 const string& containing_type, | 279 const string& containing_type, |
283 int field_number, | 280 int field_number, |
284 FileDescriptorProto* output) { | 281 FileDescriptorProto* output) { |
285 return MaybeCopy(index_.FindExtension(containing_type, field_number), output); | 282 return MaybeCopy(index_.FindExtension(containing_type, field_number), output); |
286 } | 283 } |
287 | 284 |
288 bool SimpleDescriptorDatabase::FindAllExtensionNumbers( | 285 bool SimpleDescriptorDatabase::FindAllExtensionNumbers( |
289 const string& extendee_type, | 286 const string& extendee_type, |
290 std::vector<int>* output) { | 287 vector<int>* output) { |
291 return index_.FindAllExtensionNumbers(extendee_type, output); | 288 return index_.FindAllExtensionNumbers(extendee_type, output); |
292 } | 289 } |
293 | 290 |
294 | 291 |
295 bool SimpleDescriptorDatabase::MaybeCopy(const FileDescriptorProto* file, | 292 bool SimpleDescriptorDatabase::MaybeCopy(const FileDescriptorProto* file, |
296 FileDescriptorProto* output) { | 293 FileDescriptorProto* output) { |
297 if (file == NULL) return false; | 294 if (file == NULL) return false; |
298 output->CopyFrom(*file); | 295 output->CopyFrom(*file); |
299 return true; | 296 return true; |
300 } | 297 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 333 |
337 bool EncodedDescriptorDatabase::FindFileContainingSymbol( | 334 bool EncodedDescriptorDatabase::FindFileContainingSymbol( |
338 const string& symbol_name, | 335 const string& symbol_name, |
339 FileDescriptorProto* output) { | 336 FileDescriptorProto* output) { |
340 return MaybeParse(index_.FindSymbol(symbol_name), output); | 337 return MaybeParse(index_.FindSymbol(symbol_name), output); |
341 } | 338 } |
342 | 339 |
343 bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol( | 340 bool EncodedDescriptorDatabase::FindNameOfFileContainingSymbol( |
344 const string& symbol_name, | 341 const string& symbol_name, |
345 string* output) { | 342 string* output) { |
346 std::pair<const void*, int> encoded_file = index_.FindSymbol(symbol_name); | 343 pair<const void*, int> encoded_file = index_.FindSymbol(symbol_name); |
347 if (encoded_file.first == NULL) return false; | 344 if (encoded_file.first == NULL) return false; |
348 | 345 |
349 // Optimization: The name should be the first field in the encoded message. | 346 // Optimization: The name should be the first field in the encoded message. |
350 // Try to just read it directly. | 347 // Try to just read it directly. |
351 io::CodedInputStream input(reinterpret_cast<const uint8*>(encoded_file.first), | 348 io::CodedInputStream input(reinterpret_cast<const uint8*>(encoded_file.first), |
352 encoded_file.second); | 349 encoded_file.second); |
353 | 350 |
354 const uint32 kNameTag = internal::WireFormatLite::MakeTag( | 351 const uint32 kNameTag = internal::WireFormatLite::MakeTag( |
355 FileDescriptorProto::kNameFieldNumber, | 352 FileDescriptorProto::kNameFieldNumber, |
356 internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); | 353 internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED); |
357 | 354 |
358 if (input.ReadTagNoLastTag() == kNameTag) { | 355 if (input.ReadTag() == kNameTag) { |
359 // Success! | 356 // Success! |
360 return internal::WireFormatLite::ReadString(&input, output); | 357 return internal::WireFormatLite::ReadString(&input, output); |
361 } else { | 358 } else { |
362 // Slow path. Parse whole message. | 359 // Slow path. Parse whole message. |
363 FileDescriptorProto file_proto; | 360 FileDescriptorProto file_proto; |
364 if (!file_proto.ParseFromArray(encoded_file.first, encoded_file.second)) { | 361 if (!file_proto.ParseFromArray(encoded_file.first, encoded_file.second)) { |
365 return false; | 362 return false; |
366 } | 363 } |
367 *output = file_proto.name(); | 364 *output = file_proto.name(); |
368 return true; | 365 return true; |
369 } | 366 } |
370 } | 367 } |
371 | 368 |
372 bool EncodedDescriptorDatabase::FindFileContainingExtension( | 369 bool EncodedDescriptorDatabase::FindFileContainingExtension( |
373 const string& containing_type, | 370 const string& containing_type, |
374 int field_number, | 371 int field_number, |
375 FileDescriptorProto* output) { | 372 FileDescriptorProto* output) { |
376 return MaybeParse(index_.FindExtension(containing_type, field_number), | 373 return MaybeParse(index_.FindExtension(containing_type, field_number), |
377 output); | 374 output); |
378 } | 375 } |
379 | 376 |
380 bool EncodedDescriptorDatabase::FindAllExtensionNumbers( | 377 bool EncodedDescriptorDatabase::FindAllExtensionNumbers( |
381 const string& extendee_type, | 378 const string& extendee_type, |
382 std::vector<int>* output) { | 379 vector<int>* output) { |
383 return index_.FindAllExtensionNumbers(extendee_type, output); | 380 return index_.FindAllExtensionNumbers(extendee_type, output); |
384 } | 381 } |
385 | 382 |
386 bool EncodedDescriptorDatabase::MaybeParse( | 383 bool EncodedDescriptorDatabase::MaybeParse( |
387 std::pair<const void*, int> encoded_file, | 384 pair<const void*, int> encoded_file, |
388 FileDescriptorProto* output) { | 385 FileDescriptorProto* output) { |
389 if (encoded_file.first == NULL) return false; | 386 if (encoded_file.first == NULL) return false; |
390 return output->ParseFromArray(encoded_file.first, encoded_file.second); | 387 return output->ParseFromArray(encoded_file.first, encoded_file.second); |
391 } | 388 } |
392 | 389 |
393 // =================================================================== | 390 // =================================================================== |
394 | 391 |
395 DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool) | 392 DescriptorPoolDatabase::DescriptorPoolDatabase(const DescriptorPool& pool) |
396 : pool_(pool) {} | 393 : pool_(pool) {} |
397 DescriptorPoolDatabase::~DescriptorPoolDatabase() {} | 394 DescriptorPoolDatabase::~DescriptorPoolDatabase() {} |
(...skipping 29 matching lines...) Expand all Loading... |
427 pool_.FindExtensionByNumber(extendee, field_number); | 424 pool_.FindExtensionByNumber(extendee, field_number); |
428 if (extension == NULL) return false; | 425 if (extension == NULL) return false; |
429 | 426 |
430 output->Clear(); | 427 output->Clear(); |
431 extension->file()->CopyTo(output); | 428 extension->file()->CopyTo(output); |
432 return true; | 429 return true; |
433 } | 430 } |
434 | 431 |
435 bool DescriptorPoolDatabase::FindAllExtensionNumbers( | 432 bool DescriptorPoolDatabase::FindAllExtensionNumbers( |
436 const string& extendee_type, | 433 const string& extendee_type, |
437 std::vector<int>* output) { | 434 vector<int>* output) { |
438 const Descriptor* extendee = pool_.FindMessageTypeByName(extendee_type); | 435 const Descriptor* extendee = pool_.FindMessageTypeByName(extendee_type); |
439 if (extendee == NULL) return false; | 436 if (extendee == NULL) return false; |
440 | 437 |
441 std::vector<const FieldDescriptor*> extensions; | 438 vector<const FieldDescriptor*> extensions; |
442 pool_.FindAllExtensions(extendee, &extensions); | 439 pool_.FindAllExtensions(extendee, &extensions); |
443 | 440 |
444 for (int i = 0; i < extensions.size(); ++i) { | 441 for (int i = 0; i < extensions.size(); ++i) { |
445 output->push_back(extensions[i]->number()); | 442 output->push_back(extensions[i]->number()); |
446 } | 443 } |
447 | 444 |
448 return true; | 445 return true; |
449 } | 446 } |
450 | 447 |
451 // =================================================================== | 448 // =================================================================== |
452 | 449 |
453 MergedDescriptorDatabase::MergedDescriptorDatabase( | 450 MergedDescriptorDatabase::MergedDescriptorDatabase( |
454 DescriptorDatabase* source1, | 451 DescriptorDatabase* source1, |
455 DescriptorDatabase* source2) { | 452 DescriptorDatabase* source2) { |
456 sources_.push_back(source1); | 453 sources_.push_back(source1); |
457 sources_.push_back(source2); | 454 sources_.push_back(source2); |
458 } | 455 } |
459 MergedDescriptorDatabase::MergedDescriptorDatabase( | 456 MergedDescriptorDatabase::MergedDescriptorDatabase( |
460 const std::vector<DescriptorDatabase*>& sources) | 457 const vector<DescriptorDatabase*>& sources) |
461 : sources_(sources) {} | 458 : sources_(sources) {} |
462 MergedDescriptorDatabase::~MergedDescriptorDatabase() {} | 459 MergedDescriptorDatabase::~MergedDescriptorDatabase() {} |
463 | 460 |
464 bool MergedDescriptorDatabase::FindFileByName( | 461 bool MergedDescriptorDatabase::FindFileByName( |
465 const string& filename, | 462 const string& filename, |
466 FileDescriptorProto* output) { | 463 FileDescriptorProto* output) { |
467 for (int i = 0; i < sources_.size(); i++) { | 464 for (int i = 0; i < sources_.size(); i++) { |
468 if (sources_[i]->FindFileByName(filename, output)) { | 465 if (sources_[i]->FindFileByName(filename, output)) { |
469 return true; | 466 return true; |
470 } | 467 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 } | 510 } |
514 } | 511 } |
515 return true; | 512 return true; |
516 } | 513 } |
517 } | 514 } |
518 return false; | 515 return false; |
519 } | 516 } |
520 | 517 |
521 bool MergedDescriptorDatabase::FindAllExtensionNumbers( | 518 bool MergedDescriptorDatabase::FindAllExtensionNumbers( |
522 const string& extendee_type, | 519 const string& extendee_type, |
523 std::vector<int>* output) { | 520 vector<int>* output) { |
524 std::set<int> merged_results; | 521 set<int> merged_results; |
525 std::vector<int> results; | 522 vector<int> results; |
526 bool success = false; | 523 bool success = false; |
527 | 524 |
528 for (int i = 0; i < sources_.size(); i++) { | 525 for (int i = 0; i < sources_.size(); i++) { |
529 if (sources_[i]->FindAllExtensionNumbers(extendee_type, &results)) { | 526 if (sources_[i]->FindAllExtensionNumbers(extendee_type, &results)) { |
530 std::copy(results.begin(), results.end(), | 527 std::copy( |
531 std::insert_iterator<std::set<int> >(merged_results, | 528 results.begin(), results.end(), |
532 merged_results.begin())); | 529 insert_iterator<set<int> >(merged_results, merged_results.begin())); |
533 success = true; | 530 success = true; |
534 } | 531 } |
535 results.clear(); | 532 results.clear(); |
536 } | 533 } |
537 | 534 |
538 std::copy(merged_results.begin(), merged_results.end(), | 535 std::copy(merged_results.begin(), merged_results.end(), |
539 std::insert_iterator<std::vector<int> >(*output, output->end())); | 536 insert_iterator<vector<int> >(*output, output->end())); |
540 | 537 |
541 return success; | 538 return success; |
542 } | 539 } |
543 | 540 |
544 | 541 |
545 } // namespace protobuf | 542 } // namespace protobuf |
546 } // namespace google | 543 } // namespace google |
OLD | NEW |