PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/protobuf/src/google/protobuf/compiler/java/java_enum.cc

http://decs.googlecode.com/
C++ | 213 lines | 144 code | 27 blank | 42 comment | 12 complexity | fca995f20c628aa553a939f13c9f7dee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <map>
  34. #include <string>
  35. #include <google/protobuf/compiler/java/java_enum.h>
  36. #include <google/protobuf/compiler/java/java_helpers.h>
  37. #include <google/protobuf/io/printer.h>
  38. #include <google/protobuf/descriptor.pb.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace java {
  44. EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
  45. : descriptor_(descriptor) {
  46. for (int i = 0; i < descriptor_->value_count(); i++) {
  47. const EnumValueDescriptor* value = descriptor_->value(i);
  48. const EnumValueDescriptor* canonical_value =
  49. descriptor_->FindValueByNumber(value->number());
  50. if (value == canonical_value) {
  51. canonical_values_.push_back(value);
  52. } else {
  53. Alias alias;
  54. alias.value = value;
  55. alias.canonical_value = canonical_value;
  56. aliases_.push_back(alias);
  57. }
  58. }
  59. }
  60. EnumGenerator::~EnumGenerator() {}
  61. void EnumGenerator::Generate(io::Printer* printer) {
  62. bool is_own_file =
  63. descriptor_->containing_type() == NULL &&
  64. descriptor_->file()->options().java_multiple_files();
  65. printer->Print(
  66. "public $static$ enum $classname$\n"
  67. " implements com.google.protobuf.ProtocolMessageEnum {\n",
  68. "static", is_own_file ? "" : "static",
  69. "classname", descriptor_->name());
  70. printer->Indent();
  71. for (int i = 0; i < canonical_values_.size(); i++) {
  72. map<string, string> vars;
  73. vars["name"] = canonical_values_[i]->name();
  74. vars["index"] = SimpleItoa(canonical_values_[i]->index());
  75. vars["number"] = SimpleItoa(canonical_values_[i]->number());
  76. printer->Print(vars,
  77. "$name$($index$, $number$),\n");
  78. }
  79. printer->Print(
  80. ";\n"
  81. "\n");
  82. // -----------------------------------------------------------------
  83. for (int i = 0; i < aliases_.size(); i++) {
  84. map<string, string> vars;
  85. vars["classname"] = descriptor_->name();
  86. vars["name"] = aliases_[i].value->name();
  87. vars["canonical_name"] = aliases_[i].canonical_value->name();
  88. printer->Print(vars,
  89. "public static final $classname$ $name$ = $canonical_name$;\n");
  90. }
  91. // -----------------------------------------------------------------
  92. printer->Print(
  93. "\n"
  94. "public final int getNumber() { return value; }\n"
  95. "\n"
  96. "public static $classname$ valueOf(int value) {\n"
  97. " switch (value) {\n",
  98. "classname", descriptor_->name());
  99. printer->Indent();
  100. printer->Indent();
  101. for (int i = 0; i < canonical_values_.size(); i++) {
  102. printer->Print(
  103. "case $number$: return $name$;\n",
  104. "name", canonical_values_[i]->name(),
  105. "number", SimpleItoa(canonical_values_[i]->number()));
  106. }
  107. printer->Outdent();
  108. printer->Outdent();
  109. printer->Print(
  110. " default: return null;\n"
  111. " }\n"
  112. "}\n"
  113. "\n");
  114. // -----------------------------------------------------------------
  115. // Reflection
  116. printer->Print(
  117. "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
  118. " getValueDescriptor() {\n"
  119. " return getDescriptor().getValues().get(index);\n"
  120. "}\n"
  121. "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
  122. " getDescriptorForType() {\n"
  123. " return getDescriptor();\n"
  124. "}\n"
  125. "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
  126. " getDescriptor() {\n");
  127. // TODO(kenton): Cache statically? Note that we can't access descriptors
  128. // at module init time because it wouldn't work with descriptor.proto, but
  129. // we can cache the value the first time getDescriptor() is called.
  130. if (descriptor_->containing_type() == NULL) {
  131. printer->Print(
  132. " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
  133. "file", ClassName(descriptor_->file()),
  134. "index", SimpleItoa(descriptor_->index()));
  135. } else {
  136. printer->Print(
  137. " return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
  138. "parent", ClassName(descriptor_->containing_type()),
  139. "index", SimpleItoa(descriptor_->index()));
  140. }
  141. printer->Print(
  142. "}\n"
  143. "\n"
  144. "private static final $classname$[] VALUES = {\n"
  145. " ",
  146. "classname", descriptor_->name());
  147. for (int i = 0; i < descriptor_->value_count(); i++) {
  148. printer->Print("$name$, ",
  149. "name", descriptor_->value(i)->name());
  150. }
  151. printer->Print(
  152. "\n"
  153. "};\n"
  154. "public static $classname$ valueOf(\n"
  155. " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
  156. " if (desc.getType() != getDescriptor()) {\n"
  157. " throw new java.lang.IllegalArgumentException(\n"
  158. " \"EnumValueDescriptor is not for this type.\");\n"
  159. " }\n"
  160. " return VALUES[desc.getIndex()];\n"
  161. "}\n",
  162. "classname", descriptor_->name());
  163. // -----------------------------------------------------------------
  164. printer->Print(
  165. "private final int index;\n"
  166. "private final int value;\n"
  167. "private $classname$(int index, int value) {\n"
  168. " this.index = index;\n"
  169. " this.value = value;\n"
  170. "}\n",
  171. "classname", descriptor_->name());
  172. // Force the static initialization code for the file to run, since it may
  173. // initialize static variables declared in this class.
  174. printer->Print(
  175. "\n"
  176. "static {\n"
  177. " $file$.getDescriptor();\n"
  178. "}\n",
  179. "file", ClassName(descriptor_->file()));
  180. printer->Outdent();
  181. printer->Print("}\n\n");
  182. }
  183. } // namespace java
  184. } // namespace compiler
  185. } // namespace protobuf
  186. } // namespace google