From: Mark Slee Date: Thu, 9 Aug 2007 03:39:18 +0000 (+0000) Subject: Fix C++ enum deserialization X-Git-Tag: 0.2.0~1276 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=7e2cda13166e5cc27f37c55647b4070f5df178a5;p=common%2Fthrift.git 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 fc205945..e0313f72 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -327,7 +327,7 @@ string t_cpp_generator::render_const_value(ofstream& out, string name, t_type* t 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 @@ void t_cpp_generator::generate_deserialize_field(ofstream& out, 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());