/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/java/java_enum.cc

http://github.com/tomahawk-player/tomahawk · C++ · 243 lines · 173 code · 29 blank · 41 comment · 16 complexity · 9f3e30153396e71dcbe760df30b3e615 MD5 · raw file

  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. if (HasDescriptorMethods(descriptor_)) {
  63. printer->Print(
  64. "public enum $classname$\n"
  65. " implements com.google.protobuf.ProtocolMessageEnum {\n",
  66. "classname", descriptor_->name());
  67. } else {
  68. printer->Print(
  69. "public enum $classname$\n"
  70. " implements com.google.protobuf.Internal.EnumLite {\n",
  71. "classname", descriptor_->name());
  72. }
  73. printer->Indent();
  74. for (int i = 0; i < canonical_values_.size(); i++) {
  75. map<string, string> vars;
  76. vars["name"] = canonical_values_[i]->name();
  77. vars["index"] = SimpleItoa(canonical_values_[i]->index());
  78. vars["number"] = SimpleItoa(canonical_values_[i]->number());
  79. printer->Print(vars,
  80. "$name$($index$, $number$),\n");
  81. }
  82. printer->Print(
  83. ";\n"
  84. "\n");
  85. // -----------------------------------------------------------------
  86. for (int i = 0; i < aliases_.size(); i++) {
  87. map<string, string> vars;
  88. vars["classname"] = descriptor_->name();
  89. vars["name"] = aliases_[i].value->name();
  90. vars["canonical_name"] = aliases_[i].canonical_value->name();
  91. printer->Print(vars,
  92. "public static final $classname$ $name$ = $canonical_name$;\n");
  93. }
  94. for (int i = 0; i < descriptor_->value_count(); i++) {
  95. map<string, string> vars;
  96. vars["name"] = descriptor_->value(i)->name();
  97. vars["number"] = SimpleItoa(descriptor_->value(i)->number());
  98. printer->Print(vars,
  99. "public static final int $name$_VALUE = $number$;\n");
  100. }
  101. printer->Print("\n");
  102. // -----------------------------------------------------------------
  103. printer->Print(
  104. "\n"
  105. "public final int getNumber() { return value; }\n"
  106. "\n"
  107. "public static $classname$ valueOf(int value) {\n"
  108. " switch (value) {\n",
  109. "classname", descriptor_->name());
  110. printer->Indent();
  111. printer->Indent();
  112. for (int i = 0; i < canonical_values_.size(); i++) {
  113. printer->Print(
  114. "case $number$: return $name$;\n",
  115. "name", canonical_values_[i]->name(),
  116. "number", SimpleItoa(canonical_values_[i]->number()));
  117. }
  118. printer->Outdent();
  119. printer->Outdent();
  120. printer->Print(
  121. " default: return null;\n"
  122. " }\n"
  123. "}\n"
  124. "\n"
  125. "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
  126. " internalGetValueMap() {\n"
  127. " return internalValueMap;\n"
  128. "}\n"
  129. "private static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
  130. " internalValueMap =\n"
  131. " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
  132. " public $classname$ findValueByNumber(int number) {\n"
  133. " return $classname$.valueOf(number);\n"
  134. " }\n"
  135. " };\n"
  136. "\n",
  137. "classname", descriptor_->name());
  138. // -----------------------------------------------------------------
  139. // Reflection
  140. if (HasDescriptorMethods(descriptor_)) {
  141. printer->Print(
  142. "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
  143. " getValueDescriptor() {\n"
  144. " return getDescriptor().getValues().get(index);\n"
  145. "}\n"
  146. "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
  147. " getDescriptorForType() {\n"
  148. " return getDescriptor();\n"
  149. "}\n"
  150. "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
  151. " getDescriptor() {\n");
  152. // TODO(kenton): Cache statically? Note that we can't access descriptors
  153. // at module init time because it wouldn't work with descriptor.proto, but
  154. // we can cache the value the first time getDescriptor() is called.
  155. if (descriptor_->containing_type() == NULL) {
  156. printer->Print(
  157. " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
  158. "file", ClassName(descriptor_->file()),
  159. "index", SimpleItoa(descriptor_->index()));
  160. } else {
  161. printer->Print(
  162. " return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
  163. "parent", ClassName(descriptor_->containing_type()),
  164. "index", SimpleItoa(descriptor_->index()));
  165. }
  166. printer->Print(
  167. "}\n"
  168. "\n"
  169. "private static final $classname$[] VALUES = {\n"
  170. " ",
  171. "classname", descriptor_->name());
  172. for (int i = 0; i < descriptor_->value_count(); i++) {
  173. printer->Print("$name$, ",
  174. "name", descriptor_->value(i)->name());
  175. }
  176. printer->Print(
  177. "\n"
  178. "};\n"
  179. "\n"
  180. "public static $classname$ valueOf(\n"
  181. " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
  182. " if (desc.getType() != getDescriptor()) {\n"
  183. " throw new java.lang.IllegalArgumentException(\n"
  184. " \"EnumValueDescriptor is not for this type.\");\n"
  185. " }\n"
  186. " return VALUES[desc.getIndex()];\n"
  187. "}\n"
  188. "\n",
  189. "classname", descriptor_->name());
  190. // index is only used for reflection; lite implementation does not need it
  191. printer->Print("private final int index;\n");
  192. }
  193. // -----------------------------------------------------------------
  194. printer->Print(
  195. "private final int value;\n\n"
  196. "private $classname$(int index, int value) {\n",
  197. "classname", descriptor_->name());
  198. if (HasDescriptorMethods(descriptor_)) {
  199. printer->Print(" this.index = index;\n");
  200. }
  201. printer->Print(
  202. " this.value = value;\n"
  203. "}\n");
  204. printer->Print(
  205. "\n"
  206. "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
  207. "full_name", descriptor_->full_name());
  208. printer->Outdent();
  209. printer->Print("}\n\n");
  210. }
  211. } // namespace java
  212. } // namespace compiler
  213. } // namespace protobuf
  214. } // namespace google