Fix C++ enum deserialization
Summary: Because we're all about strict aliasing rules in the g++, m'holmbie.
Reviewed By: aditya
Test Plan: Should be NO compiler warnings about (enum&) -> (int32_t&) typecasting
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665196 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index fc20594..e0313f7 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -327,7 +327,7 @@
throw "compiler error: no const of base type " + tbase;
}
} else if (type->is_enum()) {
- render << "(" << type->get_name() << ")" << value->get_integer();
+ render << "(" << type_name(type) << ")" << value->get_integer();
} else {
string t = tmp("tmp");
indent(out) << type_name(type) << " " << t << ";" << endl;
@@ -1697,46 +1697,46 @@
generate_deserialize_struct(out, (t_struct*)type, name);
} else if (type->is_container()) {
generate_deserialize_container(out, type, name);
- } else if (type->is_base_type() || type->is_enum()) {
+ } else if (type->is_base_type()) {
indent(out) <<
"xfer += iprot->";
-
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
- switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot serialize void field in a struct: " +
- name;
- break;
- case t_base_type::TYPE_STRING:
- out << "readString(" << name << ");";
- break;
- case t_base_type::TYPE_BOOL:
- out << "readBool(" << name << ");";
- break;
- case t_base_type::TYPE_BYTE:
- out << "readByte(" << name << ");";
- break;
- case t_base_type::TYPE_I16:
- out << "readI16(" << name << ");";
- break;
- case t_base_type::TYPE_I32:
- out << "readI32(" << name << ");";
- break;
- case t_base_type::TYPE_I64:
- out << "readI64(" << name << ");";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "readDouble(" << name << ");";
- break;
- default:
- throw "compiler error: no C++ reader for base type " + tbase + name;
- }
- } else if (type->is_enum()) {
- out << "readI32((int32_t&)" << name << ");";
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: " + name;
+ break;
+ case t_base_type::TYPE_STRING:
+ out << "readString(" << name << ");";
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "readBool(" << name << ");";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "readByte(" << name << ");";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "readI16(" << name << ");";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "readI32(" << name << ");";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "readI64(" << name << ");";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "readDouble(" << name << ");";
+ break;
+ default:
+ throw "compiler error: no C++ reader for base type " + tbase + name;
}
out <<
endl;
+ } else if (type->is_enum()) {
+ string t = tmp("ecast");
+ out <<
+ indent() << "int32_t " << t << ";" << endl <<
+ indent() << "xfer += iprot->readI32(" << t << ");" << endl <<
+ indent() << name << " = (" << type->get_name() << ")" << t << ";" << endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
tfield->get_name().c_str(), type_name(type).c_str());