Fix thrift compiler typedef support
Summary: Typedefed containers were wonk'd
Reviewed By: martin
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664877 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 01345c7..78278a2 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -102,7 +102,7 @@
*/
void t_cpp_generator::generate_typedef(t_typedef* ttypedef) {
f_types_ <<
- indent() << "typedef " << type_name(ttypedef->get_type()) << " " << ttypedef->get_symbolic() << ";" << endl <<
+ indent() << "typedef " << type_name(ttypedef->get_type(), true) << " " << ttypedef->get_symbolic() << ";" << endl <<
endl;
}
@@ -1277,9 +1277,9 @@
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)(tfield->get_type()), name);
+ generate_deserialize_struct(out, (t_struct*)type, name);
} else if (type->is_container()) {
- generate_deserialize_container(out, tfield->get_type(), name);
+ generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
indent(out) <<
"xfer += iprot->";
@@ -1478,11 +1478,11 @@
if (type->is_struct() || type->is_xception()) {
generate_serialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
prefix + tfield->get_name());
} else if (type->is_container()) {
generate_serialize_container(out,
- tfield->get_type(),
+ type,
prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
@@ -1704,7 +1704,7 @@
* @param ttype The type
* @return String of the type name, i.e. std::set<type>
*/
-string t_cpp_generator::type_name(t_type* ttype) {
+string t_cpp_generator::type_name(t_type* ttype, bool in_typedef) {
if (ttype->is_base_type()) {
return base_type_name(((t_base_type*)ttype)->get_base());
}
@@ -1720,27 +1720,33 @@
if (ttype->is_map()) {
t_map* tmap = (t_map*) ttype;
return "std::map<" +
- type_name(tmap->get_key_type()) + ", " +
- type_name(tmap->get_val_type()) + "> ";
+ type_name(tmap->get_key_type(), in_typedef) + ", " +
+ type_name(tmap->get_val_type(), in_typedef) + "> ";
}
if (ttype->is_set()) {
t_set* tset = (t_set*) ttype;
- return "std::set<" + type_name(tset->get_elem_type()) + "> ";
+ return "std::set<" + type_name(tset->get_elem_type(), in_typedef) + "> ";
}
if (ttype->is_list()) {
t_list* tlist = (t_list*) ttype;
- return "std::vector<" + type_name(tlist->get_elem_type()) + "> ";
+ return "std::vector<" + type_name(tlist->get_elem_type(), in_typedef) + "> ";
+ }
+
+ string class_prefix;
+ if (in_typedef && (ttype->is_struct() || ttype->is_xception())) {
+ class_prefix = "class ";
}
// Check if it needs to be namespaced
t_program* program = ttype->get_program();
if (program != NULL && program != program_) {
return
+ class_prefix +
namespace_prefix(program->get_cpp_namespace()) +
ttype->get_name();
}
- return ttype->get_name();
+ return class_prefix + ttype->get_name();
}
/**
diff --git a/compiler/cpp/src/generate/t_cpp_generator.h b/compiler/cpp/src/generate/t_cpp_generator.h
index 45cbdac..6070e37 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.h
+++ b/compiler/cpp/src/generate/t_cpp_generator.h
@@ -114,7 +114,7 @@
std::string namespace_prefix(std::string ns);
std::string namespace_open(std::string ns);
std::string namespace_close(std::string ns);
- std::string type_name(t_type* ttype);
+ std::string type_name(t_type* ttype, bool in_typedef=false);
std::string base_type_name(t_base_type::t_base tbase);
std::string declare_field(t_field* tfield, bool init=false);
std::string function_signature(t_function* tfunction, std::string prefix="");
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index f61db38..21ade43 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -923,10 +923,10 @@
if (type->is_struct() || type->is_xception()) {
generate_deserialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
name);
} else if (type->is_container()) {
- generate_deserialize_container(out, tfield->get_type(), name);
+ generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
indent(out) <<
@@ -1130,11 +1130,11 @@
if (type->is_struct() || type->is_xception()) {
generate_serialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
prefix + tfield->get_name());
} else if (type->is_container()) {
generate_serialize_container(out,
- tfield->get_type(),
+ type,
prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 4e01edf..dd848e7 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -158,26 +158,28 @@
out << endl;
- out <<
- indent() << "public function __construct($vals=null) {" << endl;
- indent_up();
- out <<
- indent() << "if (is_array($vals)) {" << endl;
- indent_up();
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ // Generate constructor from array
+ if (members.size() > 0) {
out <<
- indent() << "if (isset($vals['" << (*m_iter)->get_name() << "'])) {" << endl <<
- indent() << " $this->" << (*m_iter)->get_name() << " = $vals['" << (*m_iter)->get_name() << "'];" << endl <<
+ indent() << "public function __construct($vals=null) {" << endl;
+ indent_up();
+ out <<
+ indent() << "if (is_array($vals)) {" << endl;
+ indent_up();
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ out <<
+ indent() << "if (isset($vals['" << (*m_iter)->get_name() << "'])) {" << endl <<
+ indent() << " $this->" << (*m_iter)->get_name() << " = $vals['" << (*m_iter)->get_name() << "'];" << endl <<
+ indent() << "}" << endl;
+ }
+ indent_down();
+ out <<
indent() << "}" << endl;
+ indent_down();
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
- indent_down();
- out <<
- indent() << "}" << endl;
-
- indent_down();
- out <<
- indent() << "}" << endl <<
- endl;
generate_php_struct_reader(out, tstruct);
generate_php_struct_writer(out, tstruct);
@@ -947,10 +949,10 @@
if (type->is_struct() || type->is_xception()) {
generate_deserialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
name);
} else if (type->is_container()) {
- generate_deserialize_container(out, tfield->get_type(), name);
+ generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
if (binary_inline_) {
@@ -1254,11 +1256,11 @@
if (type->is_struct() || type->is_xception()) {
generate_serialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
prefix + tfield->get_name());
} else if (type->is_container()) {
generate_serialize_container(out,
- tfield->get_type(),
+ type,
prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc
index c753b7b..1cbcfd9 100644
--- a/compiler/cpp/src/generate/t_py_generator.cc
+++ b/compiler/cpp/src/generate/t_py_generator.cc
@@ -896,10 +896,10 @@
if (type->is_struct() || type->is_xception()) {
generate_deserialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
name);
} else if (type->is_container()) {
- generate_deserialize_container(out, tfield->get_type(), name);
+ generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
indent(out) <<
name << " = iprot.";
@@ -1088,11 +1088,11 @@
if (type->is_struct() || type->is_xception()) {
generate_serialize_struct(out,
- (t_struct*)(tfield->get_type()),
+ (t_struct*)type,
prefix + tfield->get_name());
} else if (type->is_container()) {
generate_serialize_container(out,
- tfield->get_type(),
+ type,
prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {