Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "proto_zero_generator.h" | 5 #include "proto_zero_generator.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 // prohibited but not recommended in order to avoid name collisions. | 86 // prohibited but not recommended in order to avoid name collisions. |
| 87 template <class T> | 87 template <class T> |
| 88 inline std::string GetCppClassName(const T* descriptor, bool full = false) { | 88 inline std::string GetCppClassName(const T* descriptor, bool full = false) { |
| 89 std::string name = GetDescriptorName(descriptor); | 89 std::string name = GetDescriptorName(descriptor); |
| 90 StripString(&name, ".", '_'); | 90 StripString(&name, ".", '_'); |
| 91 if (full) | 91 if (full) |
| 92 name = full_namespace_prefix_ + name; | 92 name = full_namespace_prefix_ + name; |
| 93 return name; | 93 return name; |
| 94 } | 94 } |
| 95 | 95 |
| 96 template <class T> | |
| 97 inline std::string GetCamelCaseName(const T* descriptor, bool capitalize) { | |
|
Primiano Tucci (use gerrit)
2016/08/05 11:34:18
looks like capitalize is always = true.
Unless you
| |
| 98 std::string camel_name = descriptor->camelcase_name(); | |
| 99 if (capitalize && !camel_name.empty()) | |
| 100 camel_name.at(0) = toupper(camel_name.at(0)); | |
| 101 return camel_name; | |
| 102 } | |
| 103 | |
| 96 // Small enums can be written faster without involving VarInt encoder. | 104 // Small enums can be written faster without involving VarInt encoder. |
| 97 inline bool IsTinyEnumField(const FieldDescriptor* field) { | 105 inline bool IsTinyEnumField(const FieldDescriptor* field) { |
| 98 if (field->type() != FieldDescriptor::TYPE_ENUM) | 106 if (field->type() != FieldDescriptor::TYPE_ENUM) |
| 99 return false; | 107 return false; |
| 100 const EnumDescriptor* enumeration = field->enum_type(); | 108 const EnumDescriptor* enumeration = field->enum_type(); |
| 101 | 109 |
| 102 for (int i = 0; i < enumeration->value_count(); ++i) { | 110 for (int i = 0; i < enumeration->value_count(); ++i) { |
| 103 int32_t value = enumeration->value(i)->number(); | 111 int32_t value = enumeration->value(i)->number(); |
| 104 if (value < 0 || value > 0x7F) | 112 if (value < 0 || value > 0x7F) |
| 105 return false; | 113 return false; |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 for (int j = 0; j < nested_enum->value_count(); ++j) { | 464 for (int j = 0; j < nested_enum->value_count(); ++j) { |
| 457 const EnumValueDescriptor* value = nested_enum->value(j); | 465 const EnumValueDescriptor* value = nested_enum->value(j); |
| 458 stub_h_->Print( | 466 stub_h_->Print( |
| 459 "static const $class$ $name$ = $full_name$;\n", | 467 "static const $class$ $name$ = $full_name$;\n", |
| 460 "class", nested_enum->name(), | 468 "class", nested_enum->name(), |
| 461 "name", value->name(), | 469 "name", value->name(), |
| 462 "full_name", value_name_prefix + value->name()); | 470 "full_name", value_name_prefix + value->name()); |
| 463 } | 471 } |
| 464 } | 472 } |
| 465 | 473 |
| 466 // Fields descriptors. | 474 // Field numbers. |
| 475 if (message->field_count() > 0) { | |
| 476 stub_h_->Print("enum : int32_t {\n"); | |
| 477 stub_h_->Indent(); | |
| 478 | |
| 479 for (int i = 0; i < message->field_count(); ++i) { | |
| 480 const FieldDescriptor* field = message->field(i); | |
| 481 std::string name = GetCamelCaseName(field, true); | |
| 482 if (name.empty()) { | |
| 483 // Protoc allows fields like: 'bool _ = 1'. | |
| 484 Abort("Empty field name in camel case notation."); | |
|
Primiano Tucci (use gerrit)
2016/08/05 11:34:18
lol, TIL
| |
| 485 } | |
| 486 | |
| 487 stub_h_->Print( | |
| 488 "k$name$FieldNumber = $id$,\n", | |
| 489 "name", name, | |
| 490 "id", std::to_string(field->number())); | |
| 491 } | |
| 492 stub_h_->Outdent(); | |
| 493 stub_h_->Print("};\n"); | |
| 494 } | |
| 495 | |
| 496 // Field descriptors. | |
| 467 for (int i = 0; i < message->field_count(); ++i) { | 497 for (int i = 0; i < message->field_count(); ++i) { |
| 468 const FieldDescriptor* field = message->field(i); | 498 const FieldDescriptor* field = message->field(i); |
| 469 if (field->is_packed()) { | 499 if (field->is_packed()) { |
| 470 Abort("Packed repeated fields are not supported."); | 500 Abort("Packed repeated fields are not supported."); |
| 471 return; | 501 return; |
| 472 } | 502 } |
| 473 if (field->type() != FieldDescriptor::TYPE_MESSAGE) { | 503 if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 474 GenerateSimpleFieldDescriptor(field); | 504 GenerateSimpleFieldDescriptor(field); |
| 475 } else { | 505 } else { |
| 476 GenerateNestedMessageFieldDescriptor(field); | 506 GenerateNestedMessageFieldDescriptor(field); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); | 561 GeneratorJob job(file, &stub_h_printer, &stub_cc_printer); |
| 532 if (!job.GenerateStubs()) { | 562 if (!job.GenerateStubs()) { |
| 533 *error = job.GetFirstError(); | 563 *error = job.GetFirstError(); |
| 534 return false; | 564 return false; |
| 535 } | 565 } |
| 536 return true; | 566 return true; |
| 537 } | 567 } |
| 538 | 568 |
| 539 } // namespace proto | 569 } // namespace proto |
| 540 } // namespace tracing | 570 } // namespace tracing |
| OLD | NEW |