Permalink
Browse files

Move DefaultValue function from primitive_field.cc to helpers.cc. Aug…

…ment it

to handle enum defaults.  Use the new DefaultValue function in enum_field.cc.
  • Loading branch information...
1 parent 4dd87ac commit 2fe74ca4f6e024be35dde5753549554be796aa5a @brown committed Jan 31, 2012
Showing with 38 additions and 32 deletions.
  1. +2 −3 protoc/lisp/enum_field.cc
  2. +35 −0 protoc/lisp/helpers.cc
  3. +1 −0 protoc/lisp/helpers.h
  4. +0 −29 protoc/lisp/primitive_field.cc
@@ -47,11 +47,10 @@ namespace {
// repeat code between this and the other field types.
void SetEnumVariables(const FieldDescriptor* descriptor,
map<string, string>* variables) {
- const EnumValueDescriptor* default_value = descriptor->default_value_enum();
-
(*variables)["name"] = FieldName(descriptor);
(*variables)["type"] = ClassName(descriptor->enum_type(), true);
- (*variables)["default"] = SimpleItoa(default_value->number());
+ (*variables)["default"] = DefaultValue(descriptor);
+
(*variables)["index"] = SimpleItoa(descriptor->index());
(*variables)["number"] = SimpleItoa(descriptor->number());
// (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
View
@@ -186,6 +186,41 @@ string LispSimpleDtoa(double value) {
return c_result + "d0";
}
+string DefaultValue(const FieldDescriptor* field) {
+ switch (field->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_INT32:
+ return SimpleItoa(field->default_value_int32());
+ case FieldDescriptor::CPPTYPE_UINT32:
+ return SimpleItoa(field->default_value_uint32());
+ case FieldDescriptor::CPPTYPE_INT64:
+ return SimpleItoa(field->default_value_int64());
+ case FieldDescriptor::CPPTYPE_UINT64:
+ return SimpleItoa(field->default_value_uint64());
+ case FieldDescriptor::CPPTYPE_DOUBLE:
+ return LispSimpleDtoa(field->default_value_double());
+ case FieldDescriptor::CPPTYPE_FLOAT:
+ return LispSimpleFtoa(field->default_value_float());
+ case FieldDescriptor::CPPTYPE_BOOL:
+ return field->default_value_bool() ? "cl:t" : "cl:nil";
+
+ case FieldDescriptor::CPPTYPE_ENUM:
+ return ("#.+"
+ + ClassName(field->enum_type(), false)
+ + "-"
+ + LispifyName(field->default_value_enum()->name())
+ + "+");
+
+ case FieldDescriptor::CPPTYPE_STRING:
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ GOOGLE_LOG(FATAL) << "Shouldn't get here.";
+ return "";
+ }
+ // Can't actually get here; make compiler happy. We could add a default
+ // case above but then we wouldn't get the nice compiler warning when a
+ // new type is added.
+ return "";
+}
+
string OctetSize(FieldDescriptor::Type type, string reference) {
switch (type) {
case FieldDescriptor::TYPE_INT32:
View
@@ -77,6 +77,7 @@ string FieldName(const FieldDescriptor* field);
string StringOctets(const string string_default);
string LispSimpleFtoa(float value);
string LispSimpleDtoa(double value);
+string DefaultValue(const FieldDescriptor* field);
string OctetSize(FieldDescriptor::Type type, string reference);
// Returns the scope where the field was defined (for extensions, this is
@@ -85,35 +85,6 @@ int FixedSize(FieldDescriptor::Type type) {
return -1;
}
-string DefaultValue(const FieldDescriptor* field) {
- switch (field->cpp_type()) {
- case FieldDescriptor::CPPTYPE_INT32:
- return SimpleItoa(field->default_value_int32());
- case FieldDescriptor::CPPTYPE_UINT32:
- return SimpleItoa(field->default_value_uint32());
- case FieldDescriptor::CPPTYPE_INT64:
- return SimpleItoa(field->default_value_int64());
- case FieldDescriptor::CPPTYPE_UINT64:
- return SimpleItoa(field->default_value_uint64());
- case FieldDescriptor::CPPTYPE_DOUBLE:
- return LispSimpleDtoa(field->default_value_double());
- case FieldDescriptor::CPPTYPE_FLOAT:
- return LispSimpleFtoa(field->default_value_float());
- case FieldDescriptor::CPPTYPE_BOOL:
- return field->default_value_bool() ? "cl:t" : "cl:nil";
-
- case FieldDescriptor::CPPTYPE_ENUM:
- case FieldDescriptor::CPPTYPE_STRING:
- case FieldDescriptor::CPPTYPE_MESSAGE:
- GOOGLE_LOG(FATAL) << "Shouldn't get here.";
- return "";
- }
- // Can't actually get here; make compiler happy. We could add a default
- // case above but then we wouldn't get the nice compiler warning when a
- // new type is added.
- return "";
-}
-
string Serialize(const FieldDescriptor* field) {
switch (field->type()) {
case FieldDescriptor::TYPE_INT32:

0 comments on commit 2fe74ca

Please sign in to comment.