PageRenderTime 71ms CodeModel.GetById 22ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 31// Author: kenton@google.com (Kenton Varda)
 32//  Based on original Protocol Buffers design by
 33//  Sanjay Ghemawat, Jeff Dean, and others.
 34
 35#include <map>
 36#include <string>
 37
 38#include <google/protobuf/compiler/java/java_enum.h>
 39#include <google/protobuf/compiler/java/java_helpers.h>
 40#include <google/protobuf/io/printer.h>
 41#include <google/protobuf/descriptor.pb.h>
 42#include <google/protobuf/stubs/strutil.h>
 43
 44namespace google {
 45namespace protobuf {
 46namespace compiler {
 47namespace java {
 48
 49EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
 50  : descriptor_(descriptor) {
 51  for (int i = 0; i < descriptor_->value_count(); i++) {
 52    const EnumValueDescriptor* value = descriptor_->value(i);
 53    const EnumValueDescriptor* canonical_value =
 54      descriptor_->FindValueByNumber(value->number());
 55
 56    if (value == canonical_value) {
 57      canonical_values_.push_back(value);
 58    } else {
 59      Alias alias;
 60      alias.value = value;
 61      alias.canonical_value = canonical_value;
 62      aliases_.push_back(alias);
 63    }
 64  }
 65}
 66
 67EnumGenerator::~EnumGenerator() {}
 68
 69void EnumGenerator::Generate(io::Printer* printer) {
 70  if (HasDescriptorMethods(descriptor_)) {
 71    printer->Print(
 72      "public enum $classname$\n"
 73      "    implements com.google.protobuf.ProtocolMessageEnum {\n",
 74      "classname", descriptor_->name());
 75  } else {
 76    printer->Print(
 77      "public enum $classname$\n"
 78      "    implements com.google.protobuf.Internal.EnumLite {\n",
 79      "classname", descriptor_->name());
 80  }
 81  printer->Indent();
 82
 83  for (int i = 0; i < canonical_values_.size(); i++) {
 84    map<string, string> vars;
 85    vars["name"] = canonical_values_[i]->name();
 86    vars["index"] = SimpleItoa(canonical_values_[i]->index());
 87    vars["number"] = SimpleItoa(canonical_values_[i]->number());
 88    printer->Print(vars,
 89      "$name$($index$, $number$),\n");
 90  }
 91
 92  printer->Print(
 93    ";\n"
 94    "\n");
 95
 96  // -----------------------------------------------------------------
 97
 98  for (int i = 0; i < aliases_.size(); i++) {
 99    map<string, string> vars;
100    vars["classname"] = descriptor_->name();
101    vars["name"] = aliases_[i].value->name();
102    vars["canonical_name"] = aliases_[i].canonical_value->name();
103    printer->Print(vars,
104      "public static final $classname$ $name$ = $canonical_name$;\n");
105  }
106
107  for (int i = 0; i < descriptor_->value_count(); i++) {
108    map<string, string> vars;
109    vars["name"] = descriptor_->value(i)->name();
110    vars["number"] = SimpleItoa(descriptor_->value(i)->number());
111    printer->Print(vars,
112      "public static final int $name$_VALUE = $number$;\n");
113  }
114  printer->Print("\n");
115
116  // -----------------------------------------------------------------
117
118  printer->Print(
119    "\n"
120    "public final int getNumber() { return value; }\n"
121    "\n"
122    "public static $classname$ valueOf(int value) {\n"
123    "  switch (value) {\n",
124    "classname", descriptor_->name());
125  printer->Indent();
126  printer->Indent();
127
128  for (int i = 0; i < canonical_values_.size(); i++) {
129    printer->Print(
130      "case $number$: return $name$;\n",
131      "name", canonical_values_[i]->name(),
132      "number", SimpleItoa(canonical_values_[i]->number()));
133  }
134
135  printer->Outdent();
136  printer->Outdent();
137  printer->Print(
138    "    default: return null;\n"
139    "  }\n"
140    "}\n"
141    "\n"
142    "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
143    "    internalGetValueMap() {\n"
144    "  return internalValueMap;\n"
145    "}\n"
146    "private static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
147    "    internalValueMap =\n"
148    "      new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
149    "        public $classname$ findValueByNumber(int number) {\n"
150    "          return $classname$.valueOf(number);\n"
151    "        }\n"
152    "      };\n"
153    "\n",
154    "classname", descriptor_->name());
155
156  // -----------------------------------------------------------------
157  // Reflection
158
159  if (HasDescriptorMethods(descriptor_)) {
160    printer->Print(
161      "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
162      "    getValueDescriptor() {\n"
163      "  return getDescriptor().getValues().get(index);\n"
164      "}\n"
165      "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
166      "    getDescriptorForType() {\n"
167      "  return getDescriptor();\n"
168      "}\n"
169      "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
170      "    getDescriptor() {\n");
171
172    // TODO(kenton):  Cache statically?  Note that we can't access descriptors
173    //   at module init time because it wouldn't work with descriptor.proto, but
174    //   we can cache the value the first time getDescriptor() is called.
175    if (descriptor_->containing_type() == NULL) {
176      printer->Print(
177        "  return $file$.getDescriptor().getEnumTypes().get($index$);\n",
178        "file", ClassName(descriptor_->file()),
179        "index", SimpleItoa(descriptor_->index()));
180    } else {
181      printer->Print(
182        "  return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
183        "parent", ClassName(descriptor_->containing_type()),
184        "index", SimpleItoa(descriptor_->index()));
185    }
186
187    printer->Print(
188      "}\n"
189      "\n"
190      "private static final $classname$[] VALUES = {\n"
191      "  ",
192      "classname", descriptor_->name());
193
194    for (int i = 0; i < descriptor_->value_count(); i++) {
195      printer->Print("$name$, ",
196        "name", descriptor_->value(i)->name());
197    }
198
199    printer->Print(
200      "\n"
201      "};\n"
202      "\n"
203      "public static $classname$ valueOf(\n"
204      "    com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
205      "  if (desc.getType() != getDescriptor()) {\n"
206      "    throw new java.lang.IllegalArgumentException(\n"
207      "      \"EnumValueDescriptor is not for this type.\");\n"
208      "  }\n"
209      "  return VALUES[desc.getIndex()];\n"
210      "}\n"
211      "\n",
212      "classname", descriptor_->name());
213
214    // index is only used for reflection; lite implementation does not need it
215    printer->Print("private final int index;\n");
216  }
217
218  // -----------------------------------------------------------------
219
220  printer->Print(
221    "private final int value;\n\n"
222    "private $classname$(int index, int value) {\n",
223    "classname", descriptor_->name());
224  if (HasDescriptorMethods(descriptor_)) {
225    printer->Print("  this.index = index;\n");
226  }
227  printer->Print(
228    "  this.value = value;\n"
229    "}\n");
230
231  printer->Print(
232    "\n"
233    "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
234    "full_name", descriptor_->full_name());
235
236  printer->Outdent();
237  printer->Print("}\n\n");
238}
239
240}  // namespace java
241}  // namespace compiler
242}  // namespace protobuf
243}  // namespace google