~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/java/java_primitive_field.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <google/protobuf/stubs/common.h>
40
40
#include <google/protobuf/compiler/java/java_helpers.h>
41
41
#include <google/protobuf/io/printer.h>
42
 
#include <google/protobuf/wire_format_inl.h>
 
42
#include <google/protobuf/wire_format.h>
43
43
#include <google/protobuf/stubs/strutil.h>
44
 
#include <google/protobuf/stubs/substitute.h>
45
44
 
46
45
namespace google {
47
46
namespace protobuf {
49
48
namespace java {
50
49
 
51
50
using internal::WireFormat;
 
51
using internal::WireFormatLite;
52
52
 
53
53
namespace {
54
54
 
121
121
  return NULL;
122
122
}
123
123
 
124
 
bool AllPrintableAscii(const string& text) {
125
 
  // Cannot use isprint() because it's locale-specific.  :(
126
 
  for (int i = 0; i < text.size(); i++) {
127
 
    if ((text[i] < 0x20) || text[i] >= 0x7F) {
128
 
      return false;
129
 
    }
130
 
  }
131
 
  return true;
132
 
}
133
 
 
134
124
// For encodings with fixed sizes, returns that size in bytes.  Otherwise
135
125
// returns -1.
136
126
int FixedSize(FieldDescriptor::Type type) {
141
131
    case FieldDescriptor::TYPE_UINT64  : return -1;
142
132
    case FieldDescriptor::TYPE_SINT32  : return -1;
143
133
    case FieldDescriptor::TYPE_SINT64  : return -1;
144
 
    case FieldDescriptor::TYPE_FIXED32 : return WireFormat::kFixed32Size;
145
 
    case FieldDescriptor::TYPE_FIXED64 : return WireFormat::kFixed64Size;
146
 
    case FieldDescriptor::TYPE_SFIXED32: return WireFormat::kSFixed32Size;
147
 
    case FieldDescriptor::TYPE_SFIXED64: return WireFormat::kSFixed64Size;
148
 
    case FieldDescriptor::TYPE_FLOAT   : return WireFormat::kFloatSize;
149
 
    case FieldDescriptor::TYPE_DOUBLE  : return WireFormat::kDoubleSize;
 
134
    case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
 
135
    case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
 
136
    case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
 
137
    case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
 
138
    case FieldDescriptor::TYPE_FLOAT   : return WireFormatLite::kFloatSize;
 
139
    case FieldDescriptor::TYPE_DOUBLE  : return WireFormatLite::kDoubleSize;
150
140
 
151
 
    case FieldDescriptor::TYPE_BOOL    : return WireFormat::kBoolSize;
 
141
    case FieldDescriptor::TYPE_BOOL    : return WireFormatLite::kBoolSize;
152
142
    case FieldDescriptor::TYPE_ENUM    : return -1;
153
143
 
154
144
    case FieldDescriptor::TYPE_STRING  : return -1;
163
153
  return -1;
164
154
}
165
155
 
166
 
string DefaultValue(const FieldDescriptor* field) {
167
 
  // Switch on cpp_type since we need to know which default_value_* method
168
 
  // of FieldDescriptor to call.
169
 
  switch (field->cpp_type()) {
170
 
    case FieldDescriptor::CPPTYPE_INT32:
171
 
      return SimpleItoa(field->default_value_int32());
172
 
    case FieldDescriptor::CPPTYPE_UINT32:
173
 
      // Need to print as a signed int since Java has no unsigned.
174
 
      return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
175
 
    case FieldDescriptor::CPPTYPE_INT64:
176
 
      return SimpleItoa(field->default_value_int64()) + "L";
177
 
    case FieldDescriptor::CPPTYPE_UINT64:
178
 
      return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
179
 
             "L";
180
 
    case FieldDescriptor::CPPTYPE_DOUBLE:
181
 
      return SimpleDtoa(field->default_value_double()) + "D";
182
 
    case FieldDescriptor::CPPTYPE_FLOAT:
183
 
      return SimpleFtoa(field->default_value_float()) + "F";
184
 
    case FieldDescriptor::CPPTYPE_BOOL:
185
 
      return field->default_value_bool() ? "true" : "false";
186
 
    case FieldDescriptor::CPPTYPE_STRING: {
187
 
      bool isBytes = field->type() == FieldDescriptor::TYPE_BYTES;
188
 
 
189
 
      if (!isBytes && AllPrintableAscii(field->default_value_string())) {
190
 
        // All chars are ASCII and printable.  In this case CEscape() works
191
 
        // fine (it will only escape quotes and backslashes).
192
 
        // Note:  If this "optimization" is removed, DescriptorProtos will
193
 
        //   no longer be able to initialize itself due to bootstrapping
194
 
        //   problems.
195
 
        return "\"" + CEscape(field->default_value_string()) + "\"";
196
 
      }
197
 
 
198
 
      if (isBytes && !field->has_default_value()) {
199
 
        return "com.google.protobuf.ByteString.EMPTY";
200
 
      }
201
 
 
202
 
      // Escaping strings correctly for Java and generating efficient
203
 
      // initializers for ByteStrings are both tricky.  We can sidestep the
204
 
      // whole problem by just grabbing the default value from the descriptor.
205
 
      return strings::Substitute(
206
 
        "(($0) $1.getDescriptor().getFields().get($2).getDefaultValue())",
207
 
        isBytes ? "com.google.protobuf.ByteString" : "java.lang.String",
208
 
        ClassName(field->containing_type()), field->index());
209
 
    }
210
 
 
211
 
    case FieldDescriptor::CPPTYPE_ENUM:
212
 
    case FieldDescriptor::CPPTYPE_MESSAGE:
213
 
      GOOGLE_LOG(FATAL) << "Can't get here.";
214
 
      return "";
215
 
 
216
 
    // No default because we want the compiler to complain if any new
217
 
    // types are added.
218
 
  }
219
 
 
220
 
  GOOGLE_LOG(FATAL) << "Can't get here.";
221
 
  return "";
222
 
}
223
 
 
224
156
void SetPrimitiveVariables(const FieldDescriptor* descriptor,
225
157
                           map<string, string>* variables) {
226
158
  (*variables)["name"] =
285
217
    "  return this;\n"
286
218
    "}\n"
287
219
    "public Builder clear$capitalized_name$() {\n"
288
 
    "  result.has$capitalized_name$ = false;\n"
289
 
    "  result.$name$_ = $default$;\n"
 
220
    "  result.has$capitalized_name$ = false;\n");
 
221
  if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
 
222
    // The default value is not a simple literal so we want to avoid executing
 
223
    // it multiple times.  Instead, get the default out of the default instance.
 
224
    printer->Print(variables_,
 
225
      "  result.$name$_ = getDefaultInstance().get$capitalized_name$();\n");
 
226
  } else {
 
227
    printer->Print(variables_,
 
228
      "  result.$name$_ = $default$;\n");
 
229
  }
 
230
  printer->Print(variables_,
290
231
    "  return this;\n"
291
232
    "}\n");
292
233
}
355
296
    "}\n");
356
297
 
357
298
  if (descriptor_->options().packed() &&
358
 
      descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
 
299
      HasGeneratedMethods(descriptor_->containing_type())) {
359
300
    printer->Print(variables_,
360
301
      "private int $name$MemoizedSerializedSize;\n");
361
302
  }