objectivec_oneof.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <map>
  31. #include <string>
  32. #include <google/protobuf/compiler/objectivec/objectivec_oneof.h>
  33. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  34. #include <google/protobuf/io/printer.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace compiler {
  39. namespace objectivec {
  40. OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor)
  41. : descriptor_(descriptor) {
  42. variables_["enum_name"] = OneofEnumName(descriptor_);
  43. variables_["name"] = OneofName(descriptor_);
  44. variables_["capitalized_name"] = OneofNameCapitalized(descriptor_);
  45. variables_["raw_index"] = SimpleItoa(descriptor_->index());
  46. const Descriptor* msg_descriptor = descriptor_->containing_type();
  47. variables_["owning_message_class"] = ClassName(msg_descriptor);
  48. string comments;
  49. SourceLocation location;
  50. if (descriptor_->GetSourceLocation(&location)) {
  51. comments = BuildCommentsString(location);
  52. } else {
  53. comments = "";
  54. }
  55. variables_["comments"] = comments;
  56. }
  57. OneofGenerator::~OneofGenerator() {}
  58. void OneofGenerator::SetOneofIndexBase(int index_base) {
  59. int index = descriptor_->index() + index_base;
  60. // Flip the sign to mark it as a oneof.
  61. variables_["index"] = SimpleItoa(-index);
  62. }
  63. void OneofGenerator::GenerateCaseEnum(io::Printer* printer) {
  64. printer->Print(
  65. variables_,
  66. "typedef GPB_ENUM($enum_name$) {\n");
  67. printer->Indent();
  68. printer->Print(
  69. variables_,
  70. "$enum_name$_GPBUnsetOneOfCase = 0,\n");
  71. string enum_name = variables_["enum_name"];
  72. for (int j = 0; j < descriptor_->field_count(); j++) {
  73. const FieldDescriptor* field = descriptor_->field(j);
  74. string field_name = FieldNameCapitalized(field);
  75. printer->Print(
  76. "$enum_name$_$field_name$ = $field_number$,\n",
  77. "enum_name", enum_name,
  78. "field_name", field_name,
  79. "field_number", SimpleItoa(field->number()));
  80. }
  81. printer->Outdent();
  82. printer->Print(
  83. "};\n"
  84. "\n");
  85. }
  86. void OneofGenerator::GeneratePublicCasePropertyDeclaration(
  87. io::Printer* printer) {
  88. printer->Print(
  89. variables_,
  90. "$comments$"
  91. "@property(nonatomic, readonly) $enum_name$ $name$OneOfCase;\n"
  92. "\n");
  93. }
  94. void OneofGenerator::GenerateClearFunctionDeclaration(io::Printer* printer) {
  95. printer->Print(
  96. variables_,
  97. "/// Clears whatever value was set for the oneof '$name$'.\n"
  98. "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message);\n");
  99. }
  100. void OneofGenerator::GeneratePropertyImplementation(io::Printer* printer) {
  101. printer->Print(
  102. variables_,
  103. "@dynamic $name$OneOfCase;\n");
  104. }
  105. void OneofGenerator::GenerateClearFunctionImplementation(io::Printer* printer) {
  106. printer->Print(
  107. variables_,
  108. "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message) {\n"
  109. " GPBDescriptor *descriptor = [message descriptor];\n"
  110. " GPBOneofDescriptor *oneof = descriptor->oneofs_[$raw_index$];\n"
  111. " GPBMaybeClearOneof(message, oneof, 0);\n"
  112. "}\n");
  113. }
  114. void OneofGenerator::GenerateDescription(io::Printer* printer) {
  115. printer->Print(
  116. variables_,
  117. "{\n"
  118. " .name = \"$name$\",\n"
  119. " .index = $index$,\n"
  120. "},\n");
  121. }
  122. } // namespace objectivec
  123. } // namespace compiler
  124. } // namespace protobuf
  125. } // namespace google