~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to protobuf/files/src/google/protobuf/compiler/java/java_enum.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
44
 
namespace google {
45
 
namespace protobuf {
46
 
namespace compiler {
47
 
namespace java {
48
 
 
49
 
EnumGenerator::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
 
 
67
 
EnumGenerator::~EnumGenerator() {}
68
 
 
69
 
void 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
 
  // -----------------------------------------------------------------
108
 
 
109
 
  printer->Print(
110
 
    "\n"
111
 
    "public final int getNumber() { return value; }\n"
112
 
    "\n"
113
 
    "public static $classname$ valueOf(int value) {\n"
114
 
    "  switch (value) {\n",
115
 
    "classname", descriptor_->name());
116
 
  printer->Indent();
117
 
  printer->Indent();
118
 
 
119
 
  for (int i = 0; i < canonical_values_.size(); i++) {
120
 
    printer->Print(
121
 
      "case $number$: return $name$;\n",
122
 
      "name", canonical_values_[i]->name(),
123
 
      "number", SimpleItoa(canonical_values_[i]->number()));
124
 
  }
125
 
 
126
 
  printer->Outdent();
127
 
  printer->Outdent();
128
 
  printer->Print(
129
 
    "    default: return null;\n"
130
 
    "  }\n"
131
 
    "}\n"
132
 
    "\n"
133
 
    "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
134
 
    "    internalGetValueMap() {\n"
135
 
    "  return internalValueMap;\n"
136
 
    "}\n"
137
 
    "private static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
138
 
    "    internalValueMap =\n"
139
 
    "      new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
140
 
    "        public $classname$ findValueByNumber(int number) {\n"
141
 
    "          return $classname$.valueOf(number);\n"
142
 
    "        }\n"
143
 
    "      };\n"
144
 
    "\n",
145
 
    "classname", descriptor_->name());
146
 
 
147
 
  // -----------------------------------------------------------------
148
 
  // Reflection
149
 
 
150
 
  if (HasDescriptorMethods(descriptor_)) {
151
 
    printer->Print(
152
 
      "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
153
 
      "    getValueDescriptor() {\n"
154
 
      "  return getDescriptor().getValues().get(index);\n"
155
 
      "}\n"
156
 
      "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
157
 
      "    getDescriptorForType() {\n"
158
 
      "  return getDescriptor();\n"
159
 
      "}\n"
160
 
      "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
161
 
      "    getDescriptor() {\n");
162
 
 
163
 
    // TODO(kenton):  Cache statically?  Note that we can't access descriptors
164
 
    //   at module init time because it wouldn't work with descriptor.proto, but
165
 
    //   we can cache the value the first time getDescriptor() is called.
166
 
    if (descriptor_->containing_type() == NULL) {
167
 
      printer->Print(
168
 
        "  return $file$.getDescriptor().getEnumTypes().get($index$);\n",
169
 
        "file", ClassName(descriptor_->file()),
170
 
        "index", SimpleItoa(descriptor_->index()));
171
 
    } else {
172
 
      printer->Print(
173
 
        "  return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
174
 
        "parent", ClassName(descriptor_->containing_type()),
175
 
        "index", SimpleItoa(descriptor_->index()));
176
 
    }
177
 
 
178
 
    printer->Print(
179
 
      "}\n"
180
 
      "\n"
181
 
      "private static final $classname$[] VALUES = {\n"
182
 
      "  ",
183
 
      "classname", descriptor_->name());
184
 
 
185
 
    for (int i = 0; i < descriptor_->value_count(); i++) {
186
 
      printer->Print("$name$, ",
187
 
        "name", descriptor_->value(i)->name());
188
 
    }
189
 
 
190
 
    printer->Print(
191
 
      "\n"
192
 
      "};\n"
193
 
      "\n"
194
 
      "public static $classname$ valueOf(\n"
195
 
      "    com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
196
 
      "  if (desc.getType() != getDescriptor()) {\n"
197
 
      "    throw new java.lang.IllegalArgumentException(\n"
198
 
      "      \"EnumValueDescriptor is not for this type.\");\n"
199
 
      "  }\n"
200
 
      "  return VALUES[desc.getIndex()];\n"
201
 
      "}\n"
202
 
      "\n",
203
 
      "classname", descriptor_->name());
204
 
 
205
 
    // index is only used for reflection; lite implementation does not need it
206
 
    printer->Print("private final int index;\n");
207
 
  }
208
 
 
209
 
  // -----------------------------------------------------------------
210
 
 
211
 
  printer->Print(
212
 
    "private final int value;\n\n"
213
 
    "private $classname$(int index, int value) {\n",
214
 
    "classname", descriptor_->name());
215
 
  if (HasDescriptorMethods(descriptor_)) {
216
 
    printer->Print("  this.index = index;\n");
217
 
  }
218
 
  printer->Print(
219
 
    "  this.value = value;\n"
220
 
    "}\n");
221
 
 
222
 
  if (HasDescriptorMethods(descriptor_)) {
223
 
    // Force the static initialization code for the file to run, since it may
224
 
    // initialize static variables declared in this class.
225
 
    printer->Print(
226
 
      "\n"
227
 
      "static {\n"
228
 
      "  $file$.getDescriptor();\n"
229
 
      "}\n",
230
 
      "file", ClassName(descriptor_->file()));
231
 
  }
232
 
 
233
 
  printer->Print(
234
 
    "\n"
235
 
    "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
236
 
    "full_name", descriptor_->full_name());
237
 
 
238
 
  printer->Outdent();
239
 
  printer->Print("}\n\n");
240
 
}
241
 
 
242
 
}  // namespace java
243
 
}  // namespace compiler
244
 
}  // namespace protobuf
245
 
}  // namespace google