Revert "THRIFT-2729: C++ - .clang-format created and applied"
This reverts commit 74260aa9099c3bb209bc8e524b0e8ba603f62c41.
diff --git a/compiler/cpp/Makefile.am b/compiler/cpp/Makefile.am
index 559a839..d3dadab 100644
--- a/compiler/cpp/Makefile.am
+++ b/compiler/cpp/Makefile.am
@@ -117,6 +117,3 @@
$(RM) thriftl.cc thrifty.cc thrifty.h thrifty.hh version.h windows/version.h
src/main.cc: version.h
-
-style-local:
- $(CPPSTYLE_CMD)
diff --git a/compiler/cpp/src/generate/t_as3_generator.cc b/compiler/cpp/src/generate/t_as3_generator.cc
index 2c43430..a8c4889 100644
--- a/compiler/cpp/src/generate/t_as3_generator.cc
+++ b/compiler/cpp/src/generate/t_as3_generator.cc
@@ -37,19 +37,21 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* AS3 code generator.
*
*/
class t_as3_generator : public t_oop_generator {
-public:
- t_as3_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_as3_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("bindable");
@@ -71,22 +73,14 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
- void print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval = false);
- std::string render_const_value(ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
+ std::string render_const_value(ofstream& out, std::string name, t_type* type, t_const_value* value);
/**
* Service-level generation functions
@@ -94,12 +88,8 @@
void generate_as3_struct(t_struct* tstruct, bool is_exception);
- void generate_as3_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool in_class = false,
- bool is_result = false);
- // removed -- equality,compare_to
+ void generate_as3_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
+ //removed -- equality,compare_to
void generate_as3_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_as3_validator(std::ofstream& out, t_struct* tstruct);
void generate_as3_struct_result_writer(std::ofstream& out, t_struct* tstruct);
@@ -108,14 +98,8 @@
void generate_as3_meta_data_map(std::ofstream& out, t_struct* tstruct);
void generate_field_value_meta_data(std::ofstream& out, t_type* type);
std::string get_as3_type_string(t_type* type);
- void generate_reflection_setters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
- void generate_reflection_getters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
+ void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
void generate_generic_isset_method(std::ofstream& out, t_struct* tstruct);
void generate_as3_bean_boilerplate(std::ofstream& out, t_struct* tstruct, bool bindable);
@@ -125,50 +109,72 @@
std::string generate_isset_check(t_field* field);
std::string generate_isset_check(std::string field);
void generate_isset_set(ofstream& out, t_field* field);
- // removed std::string isset_field_id(t_field* field);
+ //removed std::string isset_field_id(t_field* field);
- void generate_service_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter);
- void generate_as3_doc(std::ofstream& out, t_doc* tdoc);
+ void generate_as3_doc (std::ofstream& out,
+ t_doc* tdoc);
- void generate_as3_doc(std::ofstream& out, t_function* tdoc);
+ void generate_as3_doc (std::ofstream& out,
+ t_function* tdoc);
/**
* Helper rendering functions
@@ -179,10 +185,10 @@
std::string as3_thrift_imports();
std::string as3_thrift_gen_imports(t_struct* tstruct, string& imports);
std::string as3_thrift_gen_imports(t_service* tservice);
- std::string type_name(t_type* ttype, bool in_container = false, bool in_init = false);
- std::string base_type_name(t_base_type* tbase, bool in_container = false);
- std::string declare_field(t_field* tfield, bool init = false);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false);
+ std::string declare_field(t_field* tfield, bool init=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string get_enum_class_name(t_type* type);
@@ -190,13 +196,17 @@
bool type_can_be_null(t_type* ttype) {
ttype = get_true_type(ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception()
- || ttype->is_string();
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string();
}
std::string constant_name(std::string name);
-private:
+ private:
+
/**
* File streams
*/
@@ -208,6 +218,7 @@
bool bindable_;
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -225,7 +236,7 @@
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
if (dir.size() > 0) {
subdir = subdir + "/" + dir;
@@ -253,8 +264,11 @@
* @return List of imports for As3 types that are used in here
*/
string t_as3_generator::as3_type_imports() {
- return string() + "import org.apache.thrift.Set;\n" + "import flash.utils.ByteArray;\n"
- + "import flash.utils.Dictionary;\n\n";
+ return
+ string() +
+ "import org.apache.thrift.Set;\n" +
+ "import flash.utils.ByteArray;\n" +
+ "import flash.utils.Dictionary;\n\n";
}
/**
@@ -263,8 +277,11 @@
* @return List of imports necessary for thrift
*/
string t_as3_generator::as3_thrift_imports() {
- return string() + "import org.apache.thrift.*;\n" + "import org.apache.thrift.meta_data.*;\n"
- + "import org.apache.thrift.protocol.*;\n\n";
+ return
+ string() +
+ "import org.apache.thrift.*;\n" +
+ "import org.apache.thrift.meta_data.*;\n" +
+ "import org.apache.thrift.protocol.*;\n\n";
}
/**
@@ -277,7 +294,7 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- // For each type check if it is from a differnet namespace
+ //For each type check if it is from a differnet namespace
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_program* program = (*m_iter)->get_type()->get_program();
if (program != NULL && program != program_) {
@@ -292,6 +309,7 @@
return imports;
}
+
/**
* Prints imports needed for a given type
*
@@ -302,31 +320,31 @@
const vector<t_function*>& functions = tservice->get_functions();
vector<t_function*>::const_iterator f_iter;
- // For each type check if it is from a differnet namespace
+ //For each type check if it is from a differnet namespace
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_program* program = (*f_iter)->get_returntype()->get_program();
if (program != NULL && program != program_) {
string package = program->get_namespace("as3");
if (!package.empty()) {
if (imports.find(package + "." + (*f_iter)->get_returntype()->get_name()) == string::npos) {
- imports.append("import " + package + "." + (*f_iter)->get_returntype()->get_name()
- + ";\n");
+ imports.append("import " + package + "." + (*f_iter)->get_returntype()->get_name() + ";\n");
}
}
}
as3_thrift_gen_imports((*f_iter)->get_arglist(), imports);
as3_thrift_gen_imports((*f_iter)->get_xceptions(), imports);
+
}
return imports;
+
}
/**
* Nothing in As3
*/
-void t_as3_generator::close_generator() {
-}
+void t_as3_generator::close_generator() {}
/**
* Generates a typedef. This is not done in As3, since it does
@@ -336,7 +354,7 @@
* @param ttypedef The type definition
*/
void t_as3_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -346,27 +364,32 @@
*/
void t_as3_generator::generate_enum(t_enum* tenum) {
// Make output file
- string f_enum_name = package_dir_ + "/" + (tenum->get_name()) + ".as";
+ string f_enum_name = package_dir_+"/"+(tenum->get_name())+".as";
ofstream f_enum;
f_enum.open(f_enum_name.c_str());
// Comment and package it
- f_enum << autogen_comment() << as3_package() << endl;
+ f_enum <<
+ autogen_comment() <<
+ as3_package() << endl;
scope_up(f_enum);
// Add as3 imports
- f_enum << string() + "import org.apache.thrift.Set;" << endl << "import flash.utils.Dictionary;"
- << endl;
+ f_enum << string() +
+ "import org.apache.thrift.Set;" << endl <<
+ "import flash.utils.Dictionary;" << endl;
- indent(f_enum) << "public class " << tenum->get_name() << " ";
+ indent(f_enum) <<
+ "public class " << tenum->get_name() << " ";
scope_up(f_enum);
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
- indent(f_enum) << "public static const " << (*c_iter)->get_name() << ":int = " << value << ";"
- << endl;
+ indent(f_enum) <<
+ "public static const " << (*c_iter)->get_name() <<
+ ":int = " << value << ";" << endl;
}
// Create a static Set with all valid values for this enum
@@ -387,8 +410,7 @@
scope_up(f_enum);
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
- indent(f_enum) << "VALUES_TO_NAMES[" << (*c_iter)->get_name() << "] = \""
- << (*c_iter)->get_name() << "\";" << endl;
+ indent(f_enum) << "VALUES_TO_NAMES[" << (*c_iter)->get_name() << "] = \"" << (*c_iter)->get_name() << "\";" << endl;
}
f_enum << endl;
@@ -409,19 +431,24 @@
return;
}
- string f_consts_name = package_dir_ + "/" + program_name_ + "Constants.as";
+ string f_consts_name = package_dir_+ "/" + program_name_ + "Constants.as";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
// Print header
- f_consts << autogen_comment() << as3_package();
+ f_consts <<
+ autogen_comment() << as3_package();
scope_up(f_consts);
f_consts << endl;
f_consts << as3_type_imports();
- indent(f_consts) << "public class " << program_name_ << "Constants {" << endl << endl;
+
+
+ indent(f_consts) <<
+ "public class " << program_name_ << "Constants {" << endl <<
+ endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
@@ -432,22 +459,19 @@
false);
}
indent_down();
- indent(f_consts) << "}" << endl;
+ indent(f_consts) <<
+ "}" << endl;
scope_down(f_consts);
f_consts.close();
}
-void t_as3_generator::print_const_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval) {
+void t_as3_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
type = get_true_type(type);
indent(out);
if (!defval) {
- out << (in_static ? "var " : "public static const ");
+ out <<
+ (in_static ? "var " : "public static const ");
}
if (type->is_base_type()) {
string v2 = render_const_value(out, name, type, value);
@@ -458,7 +482,7 @@
out << " = " << v2 << ";" << endl << endl;
} else if (type->is_enum()) {
out << name;
- if (!defval) {
+ if(!defval) {
out << ":" << type_name(type);
}
out << " = " << value->get_integer() << ";" << endl << endl;
@@ -467,8 +491,7 @@
vector<t_field*>::const_iterator f_iter;
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
- out << name << ":" << type_name(type) << " = new " << type_name(type, false, true) << "();"
- << endl;
+ out << name << ":" << type_name(type) << " = new " << type_name(type, false, true) << "();" << endl;
if (!in_static) {
indent(out) << "{" << endl;
indent_up();
@@ -498,7 +521,7 @@
out << endl;
} else if (type->is_map()) {
out << name;
- if (!defval) {
+ if(!defval){
out << ":" << type_name(type);
}
out << " = new " << type_name(type, false, true) << "();" << endl;
@@ -526,7 +549,7 @@
out << endl;
} else if (type->is_list() || type->is_set()) {
out << name;
- if (!defval) {
+ if(!defval) {
out << ":" << type_name(type);
}
out << " = new " << type_name(type, false, true) << "();" << endl;
@@ -546,8 +569,7 @@
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string val = render_const_value(out, name, etype, *v_iter);
- indent(out) << name << "." << (type->is_list() ? "push" : "add") << "(" << val << ");"
- << endl;
+ indent(out) << name << "." << (type->is_list() ? "push" : "add") << "(" << val << ");" << endl;
}
if (!in_static) {
indent_down();
@@ -561,44 +583,41 @@
}
}
-string t_as3_generator::render_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+string t_as3_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
type = get_true_type(type);
std::ostringstream render;
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << '"' << get_escaped_string(value) << '"';
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() > 0) ? "true" : "false");
- break;
- case t_base_type::TYPE_BYTE:
- render << "(byte)" << value->get_integer();
- break;
- case t_base_type::TYPE_I16:
- render << "(short)" << value->get_integer();
- break;
- case t_base_type::TYPE_I32:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_I64:
- render << value->get_integer() << "L";
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- render << "(double)" << value->get_integer();
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_STRING:
+ render << '"' << get_escaped_string(value) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ render << "(byte)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I16:
+ render << "(short)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I32:
+ render << value->get_integer();
+ break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "L";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << "(double)" << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
render << value->get_integer();
@@ -611,6 +630,7 @@
return render.str();
}
+
/**
* Generates a struct definition for a thrift data type. This is a class
* with data members, read(), write(), and an inner Isset class.
@@ -630,33 +650,42 @@
generate_as3_struct(txception, true);
}
+
/**
* As3 struct definition.
*
* @param tstruct The struct definition
*/
-void t_as3_generator::generate_as3_struct(t_struct* tstruct, bool is_exception) {
+void t_as3_generator::generate_as3_struct(t_struct* tstruct,
+ bool is_exception) {
// Make output file
- string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".as";
+ string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".as";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << as3_package();
+ f_struct <<
+ autogen_comment() <<
+ as3_package();
scope_up(f_struct);
f_struct << endl;
string imports;
- f_struct << as3_type_imports() << as3_thrift_imports() << as3_thrift_gen_imports(tstruct, imports)
- << endl;
+ f_struct <<
+ as3_type_imports() <<
+ as3_thrift_imports() <<
+ as3_thrift_gen_imports(tstruct, imports) << endl;
- if (bindable_ && !is_exception) {
- f_struct << "import flash.events.Event;" << endl << "import flash.events.EventDispatcher;"
- << endl << "import mx.events.PropertyChangeEvent;" << endl;
+ if (bindable_ && ! is_exception) {
+ f_struct << "import flash.events.Event;" << endl <<
+ "import flash.events.EventDispatcher;" << endl <<
+ "import mx.events.PropertyChangeEvent;" << endl;
}
- generate_as3_struct_definition(f_struct, tstruct, is_exception);
+ generate_as3_struct_definition(f_struct,
+ tstruct,
+ is_exception);
scope_down(f_struct); // end of package
f_struct.close();
@@ -672,51 +701,51 @@
* @param in_class If inside a class, needs to be static class
* @param is_result If this is a result it needs a different writer
*/
-void t_as3_generator::generate_as3_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool in_class,
- bool is_result) {
+void t_as3_generator::generate_as3_struct_definition(ofstream &out,
+ t_struct* tstruct,
+ bool is_exception,
+ bool in_class,
+ bool is_result) {
generate_as3_doc(out, tstruct);
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- bool bindable = !is_exception && !in_class && bindable_;
+ bool bindable = ! is_exception && ! in_class && bindable_;
- indent(out) << (in_class ? "" : "public ") << (is_final ? "final " : "") << "class "
- << tstruct->get_name() << " ";
+ indent(out) << (in_class ? "" : "public ") << (is_final ? "final " : "") <<
+ "class " << tstruct->get_name() << " ";
if (is_exception) {
out << "extends Error ";
- } else if (bindable) {
+ }
+ else if (bindable) {
out << "extends EventDispatcher ";
}
out << "implements TBase ";
scope_up(out);
- indent(out) << "private static const STRUCT_DESC:TStruct = new TStruct(\"" << tstruct->get_name()
- << "\");" << endl;
+ indent(out) <<
+ "private static const STRUCT_DESC:TStruct = new TStruct(\"" << tstruct->get_name() << "\");" << endl;
// Members are public for -as3, private for -as3bean
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "private static const " << constant_name((*m_iter)->get_name())
- << "_FIELD_DESC:TField = new TField(\"" << (*m_iter)->get_name() << "\", "
- << type_to_enum((*m_iter)->get_type()) << ", " << (*m_iter)->get_key() << ");"
- << endl;
+ indent(out) <<
+ "private static const " << constant_name((*m_iter)->get_name()) <<
+ "_FIELD_DESC:TField = new TField(\"" << (*m_iter)->get_name() << "\", " <<
+ type_to_enum((*m_iter)->get_type()) << ", " <<
+ (*m_iter)->get_key() << ");" << endl;
}
out << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
generate_as3_doc(out, *m_iter);
- indent(out) << "private var _" << (*m_iter)->get_name() + ":" + type_name((*m_iter)->get_type())
- << ";" << endl;
+ indent(out) << "private var _" << (*m_iter)->get_name() + ":" + type_name((*m_iter)->get_type()) << ";" << endl;
- indent(out) << "public static const " << upcase_string((*m_iter)->get_name())
- << ":int = " << (*m_iter)->get_key() << ";" << endl;
+ indent(out) << "public static const " << upcase_string((*m_iter)->get_name()) << ":int = " << (*m_iter)->get_key() << ";" << endl;
}
out << endl;
@@ -724,11 +753,11 @@
// Inner Isset class
if (members.size() > 0) {
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- if (!type_can_be_null((*m_iter)->get_type())) {
- indent(out) << "private var __isset_" << (*m_iter)->get_name() << ":Boolean = false;"
- << endl;
+ if (!type_can_be_null((*m_iter)->get_type())){
+ indent(out) <<
+ "private var __isset_" << (*m_iter)->get_name() << ":Boolean = false;" << endl;
+ }
}
- }
}
out << endl;
@@ -738,18 +767,18 @@
// Static initializer to populate global class to struct metadata map
indent(out) << "{" << endl;
indent_up();
- indent(out) << "FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ", metaDataMap);"
- << endl;
+ indent(out) << "FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ", metaDataMap);" << endl;
indent_down();
indent(out) << "}" << endl << endl;
// Default constructor
- indent(out) << "public function " << tstruct->get_name() << "() {" << endl;
+ indent(out) <<
+ "public function " << tstruct->get_name() << "() {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_value() != NULL) {
- indent(out) << "this._" << (*m_iter)->get_name() << " = "
- << (*m_iter)->get_value()->get_integer() << ";" << endl;
+ indent(out) << "this._" << (*m_iter)->get_name() << " = " << (*m_iter)->get_value()->get_integer() << ";" <<
+ endl;
}
}
indent_down();
@@ -776,87 +805,107 @@
*
* @param tstruct The struct definition
*/
-void t_as3_generator::generate_as3_struct_reader(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function read(iprot:TProtocol):void {" << endl;
+void t_as3_generator::generate_as3_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function read(iprot:TProtocol):void {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// Declare stack tmp variables and read struct header
- out << indent() << "var field:TField;" << endl << indent() << "iprot.readStructBegin();" << endl;
+ out <<
+ indent() << "var field:TField;" << endl <<
+ indent() << "iprot.readStructBegin();" << endl;
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
- scope_up(out);
+ indent(out) <<
+ "while (true)" << endl;
+ scope_up(out);
- // Read beginning field marker
- indent(out) << "field = iprot.readFieldBegin();" << endl;
+ // Read beginning field marker
+ indent(out) <<
+ "field = iprot.readFieldBegin();" << endl;
- // Check for field STOP marker and break
- indent(out) << "if (field.type == TType.STOP) { " << endl;
- indent_up();
- indent(out) << "break;" << endl;
- indent_down();
- indent(out) << "}" << endl;
-
- // Switch statement on the field we are reading
- indent(out) << "switch (field.id)" << endl;
-
- scope_up(out);
-
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << upcase_string((*f_iter)->get_name()) << ":" << endl;
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (field.type == TType.STOP) { " << endl;
indent_up();
- indent(out) << "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent_up();
-
- generate_deserialize_field(out, *f_iter, "this.");
- generate_isset_set(out, *f_iter);
+ indent(out) <<
+ "break;" << endl;
indent_down();
- out << indent() << "} else { " << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
- << endl << indent() << "}" << endl << indent() << "break;" << endl;
- indent_down();
- }
+ indent(out) <<
+ "}" << endl;
- // In the default case we skip the field
- out << indent() << "default:" << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
- << endl << indent() << " break;" << endl;
+ // Switch statement on the field we are reading
+ indent(out) <<
+ "switch (field.id)" << endl;
- scope_down(out);
+ scope_up(out);
- // Read field end marker
- indent(out) << "iprot.readFieldEnd();" << endl;
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << upcase_string((*f_iter)->get_name()) << ":" << endl;
+ indent_up();
+ indent(out) <<
+ "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
- scope_down(out);
+ generate_deserialize_field(out, *f_iter, "this.");
+ generate_isset_set(out, *f_iter);
+ indent_down();
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
+ }
- out << indent() << "iprot.readStructEnd();" << endl << endl;
+ // In the default case we skip the field
+ out <<
+ indent() << "default:" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
+ indent() << " break;" << endl;
- // in non-beans style, check for required fields of primitive type
- // (which can be checked here but not in the general validate method)
- out << endl << indent() << "// check for required fields of primitive type, which can't be "
- "checked in the validate method" << endl;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
- out << indent() << "if (!__isset_" << (*f_iter)->get_name() << ") {" << endl << indent()
- << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '"
- << (*f_iter)->get_name()
- << "' was not found in serialized data! Struct: \" + toString());" << endl << indent()
- << "}" << endl;
+ scope_down(out);
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd();" << endl;
+
+ scope_down(out);
+
+ out <<
+ indent() << "iprot.readStructEnd();" << endl << endl;
+
+ // in non-beans style, check for required fields of primitive type
+ // (which can be checked here but not in the general validate method)
+ out << endl << indent() << "// check for required fields of primitive type, which can't be checked in the validate method" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
+ out <<
+ indent() << "if (!__isset_" << (*f_iter)->get_name() << ") {" << endl <<
+ indent() << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '" << (*f_iter)->get_name() << "' was not found in serialized data! Struct: \" + toString());" << endl <<
+ indent() << "}" << endl;
+ }
}
- }
- // performs various checks (e.g. check that all required fields are set)
- indent(out) << "validate();" << endl;
+ // performs various checks (e.g. check that all required fields are set)
+ indent(out) << "validate();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
// generates as3 method to perform various checks
// (e.g. check that all required fields are set)
-void t_as3_generator::generate_as3_validator(ofstream& out, t_struct* tstruct) {
+void t_as3_generator::generate_as3_validator(ofstream& out,
+ t_struct* tstruct){
indent(out) << "public function validate():void {" << endl;
indent_up();
@@ -868,13 +917,10 @@
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
if (type_can_be_null((*f_iter)->get_type())) {
indent(out) << "if (" << (*f_iter)->get_name() << " == null) {" << endl;
- indent(out) << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '"
- << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());"
- << endl;
+ indent(out) << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '" << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl;
indent(out) << "}" << endl;
} else {
- indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name()
- << "' because it's a primitive and you chose the non-beans generator." << endl;
+ indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name() << "' because it's a primitive and you chose the non-beans generator." << endl;
}
}
}
@@ -885,13 +931,10 @@
t_field* field = (*f_iter);
t_type* type = field->get_type();
// if field is an enum, check that its value is valid
- if (type->is_enum()) {
- indent(out) << "if (" << generate_isset_check(field) << " && !" << get_enum_class_name(type)
- << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
+ if (type->is_enum()){
+ indent(out) << "if (" << generate_isset_check(field) << " && !" << get_enum_class_name(type) << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
indent_up();
- indent(out) << "throw new TProtocolError(TProtocolError.UNKNOWN, \"The field '"
- << field->get_name() << "' has been assigned the invalid value \" + "
- << field->get_name() << ");" << endl;
+ indent(out) << "throw new TProtocolError(TProtocolError.UNKNOWN, \"The field '" << field->get_name() << "' has been assigned the invalid value \" + " << field->get_name() << ");" << endl;
indent_down();
indent(out) << "}" << endl;
}
@@ -906,8 +949,10 @@
*
* @param tstruct The struct definition
*/
-void t_as3_generator::generate_as3_struct_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function write(oprot:TProtocol):void {" << endl;
+void t_as3_generator::generate_as3_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function write(oprot:TProtocol):void {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -922,18 +967,19 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
+ out <<
+ indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
indent_up();
}
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
if (null_allowed) {
indent_down();
@@ -941,11 +987,14 @@
}
}
// Write the struct map
- out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
- << endl;
+ out <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -956,8 +1005,10 @@
*
* @param tstruct The struct definition
*/
-void t_as3_generator::generate_as3_struct_result_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function write(oprot:TProtocol):void {" << endl;
+void t_as3_generator::generate_as3_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function write(oprot:TProtocol):void {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -970,7 +1021,9 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
- out << endl << indent() << "if ";
+ out <<
+ endl <<
+ indent() << "if ";
} else {
out << " else if ";
}
@@ -979,44 +1032,42 @@
indent_up();
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
indent_down();
indent(out) << "}";
}
// Write the struct map
- out << endl << indent() << "oprot.writeFieldStop();" << endl << indent()
- << "oprot.writeStructEnd();" << endl;
+ out <<
+ endl <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
-void t_as3_generator::generate_reflection_getters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
- (void)type;
- (void)cap_name;
+void t_as3_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ (void) type;
+ (void) cap_name;
indent(out) << "case " << upcase_string(field_name) << ":" << endl;
indent_up();
indent(out) << "return this." << field_name << ";" << endl;
indent_down();
}
-void t_as3_generator::generate_reflection_setters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
- (void)type;
- (void)cap_name;
+void t_as3_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ (void) type;
+ (void) cap_name;
indent(out) << "case " << upcase_string(field_name) << ":" << endl;
indent_up();
indent(out) << "if (value == null) {" << endl;
@@ -1029,8 +1080,7 @@
indent_down();
}
-void t_as3_generator::generate_generic_field_getters_setters(std::ofstream& out,
- t_struct* tstruct) {
+void t_as3_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) {
std::ostringstream getter_stream;
std::ostringstream setter_stream;
@@ -1050,6 +1100,7 @@
indent_down();
}
+
// create the setter
indent(out) << "public function setFieldValue(fieldID:int, value:*):void {" << endl;
indent_up();
@@ -1085,13 +1136,12 @@
}
// Creates a generic isSet method that takes the field number as argument
-void t_as3_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct) {
+void t_as3_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct){
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// create the isSet method
- indent(out) << "// Returns true if field corresponding to fieldID is set (has been assigned a "
- "value) and false otherwise" << endl;
+ indent(out) << "// Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise" << endl;
indent(out) << "public function isSet(fieldID:int):Boolean {" << endl;
indent_up();
indent(out) << "switch (fieldID) {" << endl;
@@ -1120,8 +1170,7 @@
* @param tstruct The struct definition
*/
void t_as3_generator::generate_as3_bean_boilerplate(ofstream& out,
- t_struct* tstruct,
- bool bindable) {
+ t_struct* tstruct, bool bindable) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1132,7 +1181,8 @@
// Simple getter
generate_as3_doc(out, field);
- indent(out) << "public function get " << field_name << "():" << type_name(type) << " {" << endl;
+ indent(out) << "public function get " << field_name << "():" <<
+ type_name(type) << " {" << endl;
indent_up();
indent(out) << "return this._" << field_name << ";" << endl;
indent_down();
@@ -1144,27 +1194,23 @@
if (bindable) {
indent(out) << "[Bindable(event=\"" << propName << "\")]" << endl;
}
- indent(out) << "public function set " << field_name << "(" << field_name << ":"
- << type_name(type) << "):void {" << endl;
+ indent(out) << "public function set " << field_name << "(" << field_name
+ << ":" << type_name(type) << "):void {" << endl;
indent_up();
- indent(out) << "this._" << field_name << " = " << field_name << ";" << endl;
+ indent(out) << "this._" << field_name << " = " << field_name << ";" <<
+ endl;
generate_isset_set(out, field);
if (bindable) {
// We have to use a custom event rather than the default, because if you use the default,
- // the setter only gets called if the value has changed - this means calling
- // foo.setIntValue(0)
- // will not cause foo.isIntValueSet() to return true since the value of foo._intValue wasn't
- // changed
+ // the setter only gets called if the value has changed - this means calling foo.setIntValue(0)
+ // will not cause foo.isIntValueSet() to return true since the value of foo._intValue wasn't changed
// so the setter was never called.
indent(out) << "dispatchEvent(new Event(\"" << propName << "\"));" << endl;
- // However, if you just use a custom event, then collections won't be able to detect when
- // elements
- // in the collections have changed since they listed for PropertyChangeEvents. So, we
- // dispatch both.
- indent(out) << "dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE));"
- << endl;
+ // However, if you just use a custom event, then collections won't be able to detect when elements
+ // in the collections have changed since they listed for PropertyChangeEvents. So, we dispatch both.
+ indent(out) << "dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE));" << endl;
}
indent_down();
indent(out) << "}" << endl << endl;
@@ -1181,10 +1227,8 @@
indent(out) << "}" << endl << endl;
// isSet method
- indent(out) << "// Returns true if field " << field_name
- << " is set (has been assigned a value) and false otherwise" << endl;
- indent(out) << "public function is" << get_cap_name("set") << cap_name << "():Boolean {"
- << endl;
+ indent(out) << "// Returns true if field " << field_name << " is set (has been assigned a value) and false otherwise" << endl;
+ indent(out) << "public function is" << get_cap_name("set") << cap_name << "():Boolean {" << endl;
indent_up();
if (type_can_be_null(type)) {
indent(out) << "return this." << field_name << " != null;" << endl;
@@ -1202,14 +1246,13 @@
* @param tstruct The struct definition
*/
void t_as3_generator::generate_as3_struct_tostring(ofstream& out,
- t_struct* tstruct,
- bool bindable) {
+ t_struct* tstruct, bool bindable) {
// If it's bindable, it extends EventDispatcher so toString is an override.
- out << indent() << "public " << (bindable ? "override " : "") << "function toString():String {"
- << endl;
+ out << indent() << "public " << (bindable ? "override " : "") << "function toString():String {" << endl;
indent_up();
- out << indent() << "var ret:String = new String(\"" << tstruct->get_name() << "(\");" << endl;
+ out <<
+ indent() << "var ret:String = new String(\"" << tstruct->get_name() << "(\");" << endl;
out << indent() << "var first:Boolean = true;" << endl << endl;
const vector<t_field*>& fields = tstruct->get_members();
@@ -1217,7 +1260,7 @@
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
- if (could_be_unset) {
+ if(could_be_unset) {
indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
}
@@ -1238,10 +1281,8 @@
if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
indent(out) << " ret += \"BINARY\";" << endl;
- } else if (field->get_type()->is_enum()) {
- indent(out) << "var " << field->get_name()
- << "_name:String = " << get_enum_class_name(field->get_type())
- << ".VALUES_TO_NAMES[this." << (*f_iter)->get_name() << "];" << endl;
+ } else if(field->get_type()->is_enum()) {
+ indent(out) << "var " << field->get_name() << "_name:String = " << get_enum_class_name(field->get_type()) << ".VALUES_TO_NAMES[this." << (*f_iter)->get_name() << "];"<< endl;
indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
indent(out) << " ret += " << field->get_name() << "_name;" << endl;
indent(out) << " ret += \" (\";" << endl;
@@ -1260,16 +1301,19 @@
}
indent(out) << "first = false;" << endl;
- if (could_be_unset) {
+ if(could_be_unset) {
indent_down();
indent(out) << "}" << endl;
}
first = false;
}
- out << indent() << "ret += \")\";" << endl << indent() << "return ret;" << endl;
+ out <<
+ indent() << "ret += \")\";" << endl <<
+ indent() << "return ret;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
/**
@@ -1278,7 +1322,8 @@
*
* @param tstruct The struct definition
*/
-void t_as3_generator::generate_as3_meta_data_map(ofstream& out, t_struct* tstruct) {
+void t_as3_generator::generate_as3_meta_data_map(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -1291,8 +1336,7 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = *f_iter;
std::string field_name = field->get_name();
- indent(out) << "metaDataMap[" << upcase_string(field_name) << "] = new FieldMetaData(\""
- << field_name << "\", ";
+ indent(out) << "metaDataMap[" << upcase_string(field_name) << "] = new FieldMetaData(\"" << field_name << "\", ";
// Set field requirement type (required, optional, etc.)
if (field->get_req() == t_field::T_REQUIRED) {
@@ -1305,7 +1349,7 @@
// Create value meta data
generate_field_value_meta_data(out, field->get_type());
- out << ");" << endl;
+ out << ");" << endl;
}
scope_down(out);
}
@@ -1316,7 +1360,7 @@
* (e.g. for the type struct it returns "TType.STRUCT")
*/
std::string t_as3_generator::get_as3_type_string(t_type* type) {
- if (type->is_list()) {
+ if (type->is_list()){
return "TType.LIST";
} else if (type->is_map()) {
return "TType.MAP";
@@ -1330,58 +1374,37 @@
return get_as3_type_string(((t_typedef*)type)->get_type());
} else if (type->is_base_type()) {
switch (((t_base_type*)type)->get_base()) {
- case t_base_type::TYPE_VOID:
- return "TType.VOID";
- break;
- case t_base_type::TYPE_STRING:
- return "TType.STRING";
- break;
- case t_base_type::TYPE_BOOL:
- return "TType.BOOL";
- break;
- case t_base_type::TYPE_BYTE:
- return "TType.BYTE";
- break;
- case t_base_type::TYPE_I16:
- return "TType.I16";
- break;
- case t_base_type::TYPE_I32:
- return "TType.I32";
- break;
- case t_base_type::TYPE_I64:
- return "TType.I64";
- break;
- case t_base_type::TYPE_DOUBLE:
- return "TType.DOUBLE";
- break;
- default:
- throw std::runtime_error("Unknown thrift type \"" + type->get_name()
- + "\" passed to t_as3_generator::get_as3_type_string!");
- break; // This should never happen!
+ case t_base_type::TYPE_VOID : return "TType.VOID"; break;
+ case t_base_type::TYPE_STRING : return "TType.STRING"; break;
+ case t_base_type::TYPE_BOOL : return "TType.BOOL"; break;
+ case t_base_type::TYPE_BYTE : return "TType.BYTE"; break;
+ case t_base_type::TYPE_I16 : return "TType.I16"; break;
+ case t_base_type::TYPE_I32 : return "TType.I32"; break;
+ case t_base_type::TYPE_I64 : return "TType.I64"; break;
+ case t_base_type::TYPE_DOUBLE : return "TType.DOUBLE"; break;
+ default : throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_as3_generator::get_as3_type_string!"); break; // This should never happen!
}
} else {
- throw std::runtime_error(
- "Unknown thrift type \"" + type->get_name()
- + "\" passed to t_as3_generator::get_as3_type_string!"); // This should never happen!
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_as3_generator::get_as3_type_string!"); // This should never happen!
}
}
-void t_as3_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type) {
+void t_as3_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){
out << endl;
indent_up();
indent_up();
- if (type->is_struct()) {
+ if (type->is_struct()){
indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type);
- } else if (type->is_container()) {
- if (type->is_list()) {
+ } else if (type->is_container()){
+ if (type->is_list()){
indent(out) << "new ListMetaData(TType.LIST, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else if (type->is_set()) {
+ } else if (type->is_set()){
indent(out) << "new SetMetaData(TType.SET, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else { // map
+ } else{ // map
indent(out) << "new MapMetaData(TType.MAP, ";
t_type* key_type = ((t_map*)type)->get_key_type();
t_type* val_type = ((t_map*)type)->get_val_type();
@@ -1397,6 +1420,7 @@
indent_down();
}
+
/**
* Generates a thrift service. In C++, this comprises an entirely separate
* header and source file. The header file defines the methods and includes
@@ -1407,20 +1431,23 @@
*/
void t_as3_generator::generate_service(t_service* tservice) {
// Make interface file
- string f_service_name = package_dir_ + "/" + service_name_ + ".as";
+ string f_service_name = package_dir_+"/"+service_name_+".as";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << as3_package();
+ f_service_ <<
+ autogen_comment() << as3_package();
scope_up(f_service_);
- f_service_ << endl << as3_type_imports() << as3_thrift_imports()
- << as3_thrift_gen_imports(tservice);
+ f_service_ << endl <<
+ as3_type_imports() <<
+ as3_thrift_imports() <<
+ as3_thrift_gen_imports(tservice);
- if (tservice->get_extends() != NULL) {
+ if(tservice->get_extends() != NULL) {
t_type* parent = tservice->get_extends();
string parent_namespace = parent->get_program()->get_namespace("as3");
- if (!parent_namespace.empty() && parent_namespace != package_name_) {
+ if(!parent_namespace.empty() && parent_namespace != package_name_) {
f_service_ << "import " << type_name(parent) << ";" << endl;
}
}
@@ -1433,20 +1460,24 @@
f_service_.close();
// Now make the implementation/client file
- f_service_name = package_dir_ + "/" + service_name_ + "Impl.as";
+ f_service_name = package_dir_+"/"+service_name_+"Impl.as";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << as3_package();
+ f_service_ <<
+ autogen_comment() << as3_package();
scope_up(f_service_);
- f_service_ << endl << as3_type_imports() << as3_thrift_imports()
- << as3_thrift_gen_imports(tservice);
+ f_service_ << endl <<
+ as3_type_imports() <<
+ as3_thrift_imports() <<
+ as3_thrift_gen_imports(tservice);
- if (tservice->get_extends() != NULL) {
+
+ if(tservice->get_extends() != NULL) {
t_type* parent = tservice->get_extends();
string parent_namespace = parent->get_program()->get_namespace("as3");
- if (!parent_namespace.empty() && parent_namespace != package_name_) {
+ if(!parent_namespace.empty() && parent_namespace != package_name_) {
f_service_ << "import " << type_name(parent) << "Impl;" << endl;
}
}
@@ -1459,7 +1490,7 @@
f_service_ << as3_type_imports();
f_service_ << as3_thrift_imports();
f_service_ << as3_thrift_gen_imports(tservice);
- if (!package_name_.empty()) {
+ if(!package_name_.empty()) {
f_service_ << "import " << package_name_ << ".*;" << endl;
}
@@ -1468,29 +1499,33 @@
f_service_.close();
// Now make the processor/server file
- f_service_name = package_dir_ + "/" + service_name_ + "Processor.as";
+ f_service_name = package_dir_+"/"+service_name_+"Processor.as";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << as3_package();
+ f_service_ <<
+ autogen_comment() << as3_package();
scope_up(f_service_);
- f_service_ << endl << as3_type_imports() << as3_thrift_imports()
- << as3_thrift_gen_imports(tservice) << endl;
+ f_service_ << endl <<
+ as3_type_imports() <<
+ as3_thrift_imports() <<
+ as3_thrift_gen_imports(tservice) << endl;
generate_service_server(tservice);
scope_down(f_service_);
f_service_ << as3_type_imports();
f_service_ << as3_thrift_imports();
- f_service_ << as3_thrift_gen_imports(tservice) << endl;
- if (!package_name_.empty()) {
+ f_service_ << as3_thrift_gen_imports(tservice) <<endl;
+ if(!package_name_.empty()) {
f_service_ << "import " << package_name_ << ".*;" << endl;
}
generate_service_helpers(tservice);
f_service_.close();
+
}
/**
@@ -1505,8 +1540,8 @@
}
generate_as3_doc(f_service_, tservice);
- f_service_ << indent() << "public interface " << service_name_ << extends_iface << " {" << endl
- << endl;
+ f_service_ << indent() << "public interface " << service_name_ << extends_iface <<
+ " {" << endl << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
@@ -1516,16 +1551,19 @@
if ((*f_iter)->get_returntype()->is_void()) {
indent(f_service_) << "//function onError(Error):void;" << endl;
indent(f_service_) << "//function onSuccess():void;" << endl;
- } else {
+ }
+ else {
indent(f_service_) << "//function onError(Error):void;" << endl;
- indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype())
- << "):void;" << endl;
+ indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype()) << "):void;" << endl;
}
}
- indent(f_service_) << function_signature(*f_iter) << ";" << endl << endl;
+ indent(f_service_) << function_signature(*f_iter) << ";" <<
+ endl << endl;
}
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -1556,15 +1594,16 @@
extends_client = " extends " + extends + "Impl";
}
- indent(f_service_) << "public class " << service_name_ << "Impl" << extends_client
- << " implements " << service_name_ << " {" << endl;
+ indent(f_service_) <<
+ "public class " << service_name_ << "Impl" << extends_client << " implements " << service_name_ << " {" << endl;
indent_up();
- indent(f_service_) << "public function " << service_name_ << "Impl"
- << "(iprot:TProtocol, oprot:TProtocol=null)" << endl;
+ indent(f_service_) <<
+ "public function " << service_name_ << "Impl" << "(iprot:TProtocol, oprot:TProtocol=null)" << endl;
scope_up(f_service_);
if (extends.empty()) {
- f_service_ << indent() << "iprot_ = iprot;" << endl;
+ f_service_ <<
+ indent() << "iprot_ = iprot;" << endl;
f_service_ << indent() << "if (oprot == null) {" << endl;
indent_up();
f_service_ << indent() << "oprot_ = iprot;" << endl;
@@ -1575,27 +1614,36 @@
indent_down();
f_service_ << indent() << "}";
} else {
- f_service_ << indent() << "super(iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "super(iprot, oprot);" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected var iprot_:TProtocol;" << endl << indent()
- << "protected var oprot_:TProtocol;" << endl << endl << indent()
- << "protected var seqid_:int;" << endl << endl;
+ f_service_ <<
+ indent() << "protected var iprot_:TProtocol;" << endl <<
+ indent() << "protected var oprot_:TProtocol;" << endl <<
+ endl <<
+ indent() << "protected var seqid_:int;" << endl <<
+ endl;
- indent(f_service_) << "public function getInputProtocol():TProtocol" << endl;
+ indent(f_service_) <<
+ "public function getInputProtocol():TProtocol" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.iprot_;" << endl;
+ indent(f_service_) <<
+ "return this.iprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public function getOutputProtocol():TProtocol" << endl;
+ indent(f_service_) <<
+ "public function getOutputProtocol():TProtocol" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.oprot_;" << endl;
+ indent(f_service_) <<
+ "return this.oprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
+
}
// Generate client method implementations
@@ -1609,15 +1657,17 @@
if ((*f_iter)->get_returntype()->is_void()) {
indent(f_service_) << "//function onError(Error):void;" << endl;
indent(f_service_) << "//function onSuccess():void;" << endl;
- } else {
+ }
+ else {
indent(f_service_) << "//function onError(Error):void;" << endl;
- indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype())
- << "):void;" << endl;
+ indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype()) << "):void;" << endl;
}
}
- indent(f_service_) << "public " << function_signature(*f_iter) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(*f_iter) << endl;
scope_up(f_service_);
+
// Get the struct of function call params
t_struct* arg_struct = (*f_iter)->get_arglist();
@@ -1626,71 +1676,85 @@
const vector<t_field*>& fields = arg_struct->get_members();
// Serialize the request
- f_service_ << indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", "
- << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
- << ", seqid_));" << endl << indent() << "var args:" << argsname << " = new "
- << argsname << "();" << endl;
+ f_service_ <<
+ indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", " <<
+ ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL") <<
+ ", seqid_));" << endl <<
+ indent() << "var args:" << argsname << " = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = "
- << (*fld_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << ";" << endl;
}
- f_service_ << indent() << "args.write(oprot_);" << endl << indent()
- << "oprot_.writeMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "args.write(oprot_);" << endl <<
+ indent() << "oprot_.writeMessageEnd();" << endl;
if ((*f_iter)->is_oneway()) {
f_service_ << indent() << "oprot_.getTransport().flush();" << endl;
- } else {
+ }
+ else {
f_service_ << indent() << "oprot_.getTransport().flush(function(error:Error):void {" << endl;
indent_up();
f_service_ << indent() << "try {" << endl;
indent_up();
string resultname = (*f_iter)->get_name() + "_result";
- f_service_ << indent() << "if (error != null) {" << endl << indent()
- << " if (onError != null) onError(error);" << endl << indent() << " return;"
- << endl << indent() << "}" << endl << indent()
- << "var msg:TMessage = iprot_.readMessageBegin();" << endl << indent()
- << "if (msg.type == TMessageType.EXCEPTION) {" << endl << indent()
- << " var x:TApplicationError = TApplicationError.read(iprot_);" << endl
- << indent() << " iprot_.readMessageEnd();" << endl << indent()
- << " if (onError != null) onError(x);" << endl << indent() << " return;" << endl
- << indent() << "}" << endl << indent() << "var result :" << resultname << " = new "
- << resultname << "();" << endl << indent() << "result.read(iprot_);" << endl
- << indent() << "iprot_.readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "if (error != null) {" << endl <<
+ indent() << " if (onError != null) onError(error);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "var msg:TMessage = iprot_.readMessageBegin();" << endl <<
+ indent() << "if (msg.type == TMessageType.EXCEPTION) {" << endl <<
+ indent() << " var x:TApplicationError = TApplicationError.read(iprot_);" << endl <<
+ indent() << " iprot_.readMessageEnd();" << endl <<
+ indent() << " if (onError != null) onError(x);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "var result :" << resultname << " = new " << resultname << "();" << endl <<
+ indent() << "result.read(iprot_);" << endl <<
+ indent() << "iprot_.readMessageEnd();" << endl;
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (result." << generate_isset_check("success") << ") {" << endl
- << indent() << " if (onSuccess != null) onSuccess(result.success);" << endl
- << indent() << " return;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << generate_isset_check("success") << ") {" << endl <<
+ indent() << " if (onSuccess != null) onSuccess(result.success);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl
- << indent() << " if (onError != null) onError(result." << (*x_iter)->get_name()
- << ");" << endl << indent() << " return;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl <<
+ indent() << " if (onError != null) onError(result." << (*x_iter)->get_name() << ");" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
}
// If you get here it's an exception, unless a void function
if ((*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (onSuccess != null) onSuccess();" << endl << indent()
- << "return;" << endl;
+ f_service_ <<
+ indent() << "if (onSuccess != null) onSuccess();" << endl <<
+ indent() << "return;" << endl;
} else {
- f_service_ << indent() << "if (onError != null) onError(new "
- "TApplicationError(TApplicationError.MISSING_RESULT, \""
- << (*f_iter)->get_name() << " failed: unknown result\"));" << endl;
+ f_service_ <<
+ indent() << "if (onError != null) onError(new TApplicationError(TApplicationError.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\"));" << endl;
}
indent_down();
- f_service_ << indent() << "} catch (e:TError) {" << endl << indent()
- << " if (onError != null) onError(e);" << endl << indent() << "}" << endl;
+ f_service_ << indent() << "} catch (e:TError) {" << endl <<
+ indent() << " if (onError != null) onError(e);" << endl <<
+ indent() << "}" << endl;
+
indent_down();
- indent(f_service_) << "});" << endl;
+ indent(f_service_) <<
+ "});" << endl;
}
// Close function
scope_down(f_service_);
@@ -1698,7 +1762,8 @@
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) <<
+ "}" << endl;
}
/**
@@ -1720,30 +1785,34 @@
}
// Generate the header portion
- indent(f_service_) << "public class " << service_name_ << "Processor" << extends_processor
- << " implements TProcessor {" << endl;
+ indent(f_service_) <<
+ "public class " << service_name_ << "Processor" << extends_processor << " implements TProcessor {" << endl;
indent_up();
- indent(f_service_) << "public function " << service_name_ << "Processor(iface:" << service_name_
- << ")" << endl;
+ indent(f_service_) <<
+ "public function " << service_name_ << "Processor(iface:" << service_name_ << ")" << endl;
scope_up(f_service_);
if (!extends.empty()) {
- f_service_ << indent() << "super(iface);" << endl;
+ f_service_ <<
+ indent() << "super(iface);" << endl;
}
- f_service_ << indent() << "iface_ = iface;" << endl;
+ f_service_ <<
+ indent() << "iface_ = iface;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "PROCESS_MAP[\"" << (*f_iter)->get_name()
- << "\"] = " << (*f_iter)->get_name() << "();" << endl;
+ f_service_ <<
+ indent() << "PROCESS_MAP[\"" << (*f_iter)->get_name() << "\"] = " << (*f_iter)->get_name() << "();" << endl;
}
scope_down(f_service_);
f_service_ << endl;
- f_service_ << indent() << "private var iface_:" << service_name_ << ";" << endl;
+ f_service_ <<
+ indent() << "private var iface_:" << service_name_ << ";" << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected const PROCESS_MAP:Dictionary = new Dictionary();" << endl;
+ f_service_ <<
+ indent() << "protected const PROCESS_MAP:Dictionary = new Dictionary();" << endl;
}
f_service_ << endl;
@@ -1751,31 +1820,34 @@
// Generate the server implementation
string override = "";
if (tservice->get_extends() != NULL) {
- override = "override ";
+ override = "override ";
}
- indent(f_service_) << override
- << "public function process(iprot:TProtocol, oprot:TProtocol):Boolean" << endl;
+ indent(f_service_) << override << "public function process(iprot:TProtocol, oprot:TProtocol):Boolean" << endl;
scope_up(f_service_);
- f_service_ << indent() << "var msg:TMessage = iprot.readMessageBegin();" << endl;
+ f_service_ <<
+ indent() << "var msg:TMessage = iprot.readMessageBegin();" << endl;
// TODO(mcslee): validate message, was the seqid etc. legit?
// AS- If all method is oneway:
// do you have an oprot?
// do you you need nullcheck?
- f_service_
- << indent() << "var fn:Function = PROCESS_MAP[msg.name];" << endl << indent()
- << "if (fn == null) {" << endl << indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);"
- << endl << indent() << " iprot.readMessageEnd();" << endl << indent()
- << " var x:TApplicationError = new TApplicationError(TApplicationError.UNKNOWN_METHOD, "
- "\"Invalid method name: '\"+msg.name+\"'\");" << endl << indent()
- << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));"
- << endl << indent() << " x.write(oprot);" << endl << indent() << " oprot.writeMessageEnd();"
- << endl << indent() << " oprot.getTransport().flush();" << endl << indent()
- << " return true;" << endl << indent() << "}" << endl << indent()
- << "fn.call(this,msg.seqid, iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "var fn:Function = PROCESS_MAP[msg.name];" << endl <<
+ indent() << "if (fn == null) {" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl <<
+ indent() << " iprot.readMessageEnd();" << endl <<
+ indent() << " var x:TApplicationError = new TApplicationError(TApplicationError.UNKNOWN_METHOD, \"Invalid method name: '\"+msg.name+\"'\");" << endl <<
+ indent() << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));" << endl <<
+ indent() << " x.write(oprot);" << endl <<
+ indent() << " oprot.writeMessageEnd();" << endl <<
+ indent() << " oprot.getTransport().flush();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "fn.call(this,msg.seqid, iprot, oprot);" << endl;
- f_service_ << indent() << "return true;" << endl;
+ f_service_ <<
+ indent() << "return true;" << endl;
scope_down(f_service_);
f_service_ << endl;
@@ -1786,7 +1858,9 @@
}
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -1820,22 +1894,27 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_as3_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_as3_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open class
- indent(f_service_) << "private function " << tfunction->get_name() << "():Function {" << endl;
+ indent(f_service_) <<
+ "private function " << tfunction->get_name() << "():Function {" << endl;
indent_up();
// Open function
- indent(f_service_) << "return function(seqid:int, iprot:TProtocol, oprot:TProtocol):void" << endl;
+ indent(f_service_) <<
+ "return function(seqid:int, iprot:TProtocol, oprot:TProtocol):void"
+ << endl;
scope_up(f_service_);
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
- f_service_ << indent() << "var args:" << argsname << " = new " << argsname << "();" << endl
- << indent() << "args.read(iprot);" << endl << indent() << "iprot.readMessageEnd();"
- << endl;
+ f_service_ <<
+ indent() << "var args:"<< argsname << " = new " << argsname << "();" << endl <<
+ indent() << "args.read(iprot);" << endl <<
+ indent() << "iprot.readMessageEnd();" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -1843,13 +1922,14 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "var result:" << resultname << " = new " << resultname << "();"
- << endl;
+ f_service_ <<
+ indent() << "var result:" << resultname << " = new " << resultname << "();" << endl;
}
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "try {" << endl;
+ f_service_ <<
+ indent() << "try {" << endl;
indent_up();
}
@@ -1859,8 +1939,9 @@
vector<t_field*>::const_iterator f_iter;
f_service_ << indent();
- if (tfunction->is_oneway()) {
- f_service_ << "iface_." << tfunction->get_name() << "(";
+ if (tfunction->is_oneway()){
+ f_service_ <<
+ "iface_." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1877,22 +1958,20 @@
}
// Set isset on success field
- if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()
- && !type_can_be_null(tfunction->get_returntype())) {
- f_service_ << indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet")
- << "(true);" << endl;
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void() && !type_can_be_null(tfunction->get_returntype())) {
+ f_service_ <<
+ indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl;
}
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
f_service_ << indent() << "}";
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << " catch (" << (*x_iter)->get_name() << ":"
- << type_name((*x_iter)->get_type(), false, false) << ") {" << endl;
+ f_service_ << " catch (" << (*x_iter)->get_name() << ":" << type_name((*x_iter)->get_type(), false, false) << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent() << "}";
} else {
@@ -1901,34 +1980,37 @@
}
f_service_ << " catch (th:Error) {" << endl;
indent_up();
- f_service_ << indent() << "trace(\"Internal error processing " << tfunction->get_name()
- << "\", th);" << endl << indent()
- << "var x:TApplicationError = new "
- "TApplicationError(TApplicationError.INTERNAL_ERROR, \"Internal error processing "
- << tfunction->get_name() << "\");" << endl << indent()
- << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.EXCEPTION, seqid));" << endl << indent() << "x.write(oprot);"
- << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
- << "oprot.getTransport().flush();" << endl << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "trace(\"Internal error processing " << tfunction->get_name() << "\", th);" << endl <<
+ indent() << "var x:TApplicationError = new TApplicationError(TApplicationError.INTERNAL_ERROR, \"Internal error processing " << tfunction->get_name() << "\");" << endl <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl <<
+ indent() << "return;" << endl;
indent_down();
f_service_ << indent() << "}" << endl;
}
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
scope_down(f_service_);
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
return;
}
- f_service_ << indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid));" << endl << indent() << "result.write(oprot);"
- << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
- << "oprot.getTransport().flush();" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid));" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl;
// Close function
scope_down(f_service_);
@@ -1936,7 +2018,9 @@
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -1945,28 +2029,35 @@
* @param tfield The field
* @param prefix The variable name or container for this field
*/
-void t_as3_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_as3_generator::generate_deserialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
- indent(out) << name << " = iprot.";
+ indent(out) <<
+ name << " = 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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -1999,26 +2090,31 @@
} else if (type->is_enum()) {
out << "readI32();";
}
- out << endl;
+ out <<
+ endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
/**
* Generates an unserializer for a struct, invokes read()
*/
-void t_as3_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- out << indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << indent()
- << prefix << ".read(iprot);" << endl;
+void t_as3_generator::generate_deserialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = new " << type_name(tstruct) << "();" << endl <<
+ indent() << prefix << ".read(iprot);" << endl;
}
/**
* Deserializes a container by reading its size and then iterating
*/
-void t_as3_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_as3_generator::generate_deserialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
string obj;
@@ -2040,28 +2136,30 @@
indent(out) << "var " << obj << ":TList = iprot.readListBegin();" << endl;
}
- indent(out) << prefix << " = new " << type_name(ttype, false, true)
- // size the collection correctly
- << "("
- << ");" << endl;
+ indent(out)
+ << prefix << " = new " << type_name(ttype, false, true)
+ // size the collection correctly
+ << "("
+ << ");" << endl;
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for (var " << i << ":int = 0; " << i << " < " << obj << ".size"
- << "; "
- << "++" << i << ")" << endl;
+ indent(out) <<
+ "for (var " << i << ":int = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- scope_down(out);
+ scope_down(out);
// Read container end
if (ttype->is_map()) {
@@ -2075,82 +2173,104 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
-void t_as3_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_as3_generator::generate_deserialize_map_element(ofstream& out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey) << endl;
- indent(out) << declare_field(&fval) << endl;
+ indent(out) <<
+ declare_field(&fkey) << endl;
+ indent(out) <<
+ declare_field(&fval) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << ";" << endl;
}
/**
* Deserializes a set element
*/
-void t_as3_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_as3_generator::generate_deserialize_set_element(ofstream& out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".add(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".add(" << elem << ");" << endl;
}
/**
* Deserializes a list element
*/
void t_as3_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix) {
+ t_list* tlist,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".push(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".push(" << elem << ");" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_as3_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_as3_generator::generate_serialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
string name = prefix + tfield->get_name();
- indent(out) << "oprot.";
+ indent(out) <<
+ "oprot.";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -2198,9 +2318,12 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_as3_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
- out << indent() << prefix << ".write(oprot);" << endl;
+void t_as3_generator::generate_serialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
+ out <<
+ indent() << prefix << ".write(oprot);" << endl;
}
/**
@@ -2209,7 +2332,9 @@
* @param ttype The type of container
* @param prefix String prefix for fields
*/
-void t_as3_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_as3_generator::generate_serialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
@@ -2220,45 +2345,56 @@
indent(out) << " " << counter << +"++;" << endl;
indent(out) << "}" << endl;
- indent(out) << "oprot.writeMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << counter << "));"
- << endl;
+ indent(out) <<
+ "oprot.writeMapBegin(new TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ counter << "));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", " << prefix << ".size));" << endl;
+ indent(out) <<
+ "oprot.writeSetBegin(new TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListBegin(new TList("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".length));"
- << endl;
+ indent(out) <<
+ "oprot.writeListBegin(new TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length));" << endl;
}
string iter = tmp("elem");
if (ttype->is_map()) {
- indent(out) << "for (var " << iter << ":* in " << prefix << ")";
+ indent(out) <<
+ "for (var " << iter << ":* in " << prefix << ")";
} else if (ttype->is_set()) {
- indent(out) << "for each (var " << iter << ":* in " << prefix << ".toArray())";
+ indent(out) <<
+ "for each (var " << iter << ":* in " << prefix << ".toArray())";
} else if (ttype->is_list()) {
- indent(out) << "for each (var " << iter << ":* in " << prefix << ")";
+ indent(out) <<
+ "for each (var " << iter << ":* in " << prefix << ")";
}
- scope_up(out);
+ scope_up(out);
+
+ if (ttype->is_map()) {
+ generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
+ } else if (ttype->is_set()) {
+ generate_serialize_set_element(out, (t_set*)ttype, iter);
+ } else if (ttype->is_list()) {
+ generate_serialize_list_element(out, (t_list*)ttype, iter);
+ }
+
+ scope_down(out);
if (ttype->is_map()) {
- generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
+ indent(out) <<
+ "oprot.writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- generate_serialize_set_element(out, (t_set*)ttype, iter);
+ indent(out) <<
+ "oprot.writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- generate_serialize_list_element(out, (t_list*)ttype, iter);
- }
-
- scope_down(out);
-
- if (ttype->is_map()) {
- indent(out) << "oprot.writeMapEnd();" << endl;
- } else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetEnd();" << endl;
- } else if (ttype->is_list()) {
- indent(out) << "oprot.writeListEnd();" << endl;
+ indent(out) <<
+ "oprot.writeListEnd();" << endl;
}
scope_down(out);
@@ -2268,9 +2404,9 @@
* Serializes the members of a map.
*/
void t_as3_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string iter,
- string map) {
+ t_map* tmap,
+ string iter,
+ string map) {
t_field kfield(tmap->get_key_type(), iter);
generate_serialize_field(out, &kfield, "");
t_field vfield(tmap->get_val_type(), map + "[" + iter + "]");
@@ -2280,7 +2416,9 @@
/**
* Serializes the members of a set.
*/
-void t_as3_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_as3_generator::generate_serialize_set_element(ofstream& out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2288,7 +2426,9 @@
/**
* Serializes the members of a list.
*/
-void t_as3_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_as3_generator::generate_serialize_list_element(ofstream& out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2301,7 +2441,7 @@
* @return As3 type name, i.e. HashMap<Key,Value>
*/
string t_as3_generator::type_name(t_type* ttype, bool in_container, bool in_init) {
- (void)in_init;
+ (void) in_init;
// In As3 typedefs are just resolved to their real type
ttype = get_true_type(ttype);
string prefix;
@@ -2336,8 +2476,9 @@
* @param tbase The base type
* @param container Is it going in a As3 container?
*/
-string t_as3_generator::base_type_name(t_base_type* type, bool in_container) {
- (void)in_container;
+string t_as3_generator::base_type_name(t_base_type* type,
+ bool in_container) {
+ (void) in_container;
t_base_type::t_base tbase = type->get_base();
switch (tbase) {
@@ -2397,15 +2538,14 @@
case t_base_type::TYPE_DOUBLE:
result += " = (double)0";
break;
- }
+ }
} else if (ttype->is_enum()) {
result += " = 0";
} else if (ttype->is_container()) {
result += " = new " + type_name(ttype, false, true) + "()";
} else {
- result += " = new " + type_name(ttype, false, true) + "()";
- ;
+ result += " = new " + type_name(ttype, false, true) + "()";;
}
}
return result + ";";
@@ -2417,16 +2557,18 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_as3_generator::function_signature(t_function* tfunction, string prefix) {
+string t_as3_generator::function_signature(t_function* tfunction,
+ string prefix) {
std::string arguments = argument_list(tfunction->get_arglist());
- if (!tfunction->is_oneway()) {
+ if (! tfunction->is_oneway()) {
if (arguments != "") {
arguments += ", ";
}
arguments += "onError:Function, onSuccess:Function";
}
- std::string result = "function " + prefix + tfunction->get_name() + "(" + arguments + "):void";
+ std::string result = "function " +
+ prefix + tfunction->get_name() + "(" + arguments + "):void";
return result;
}
@@ -2494,7 +2636,7 @@
/**
* Applies the correct style to a string based on the value of nocamel_style_
*/
-std::string t_as3_generator::get_cap_name(std::string name) {
+std::string t_as3_generator::get_cap_name(std::string name){
name[0] = toupper(name[0]);
return name;
}
@@ -2524,16 +2666,21 @@
/**
* Emits a As3Doc comment if the provided object has a doc in Thrift
*/
-void t_as3_generator::generate_as3_doc(ofstream& out, t_doc* tdoc) {
+void t_as3_generator::generate_as3_doc(ofstream &out,
+ t_doc* tdoc) {
if (tdoc->has_doc()) {
- generate_docstring_comment(out, "/**\n", " * ", tdoc->get_doc(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", tdoc->get_doc(),
+ " */\n");
}
}
/**
* Emits a As3Doc comment if the provided function object has a doc in Thrift
*/
-void t_as3_generator::generate_as3_doc(ofstream& out, t_function* tfunction) {
+void t_as3_generator::generate_as3_doc(ofstream &out,
+ t_function* tfunction) {
if (tfunction->has_doc()) {
stringstream ss;
ss << tfunction->get_doc();
@@ -2546,7 +2693,10 @@
ss << " " << p->get_doc();
}
}
- generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
}
}
@@ -2573,7 +2723,7 @@
return package + type->get_name();
}
-THRIFT_REGISTER_GENERATOR(
- as3,
- "AS3",
- " bindable: Add [bindable] metadata to all the struct classes.\n")
+THRIFT_REGISTER_GENERATOR(as3, "AS3",
+" bindable: Add [bindable] metadata to all the struct classes.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_c_glib_generator.cc b/compiler/cpp/src/generate/t_c_glib_generator.cc
index a97267e..dbebedb 100644
--- a/compiler/cpp/src/generate/t_c_glib_generator.cc
+++ b/compiler/cpp/src/generate/t_c_glib_generator.cc
@@ -38,7 +38,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/* forward declarations */
string initial_caps_to_underscores(string name);
@@ -50,14 +50,15 @@
* C code generator, using glib for C typing.
*/
class t_c_glib_generator : public t_oop_generator {
-public:
+ public:
+
/* constructor */
- t_c_glib_generator(t_program* program,
- const map<string, string>& parsed_options,
- const string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ t_c_glib_generator(t_program *program,
+ const map<string, string> &parsed_options,
+ const string &option_string) : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
/* set the output directory */
this->out_dir_base_ = "gen-c_glib";
@@ -71,7 +72,7 @@
this->nspace_lc = "";
} else {
/* replace dots with underscores */
- char* tmp = strdup(this->nspace.c_str());
+ char *tmp = strdup(this->nspace.c_str());
for (unsigned int i = 0; i < strlen(tmp); i++) {
if (tmp[i] == '.') {
tmp[i] = '_';
@@ -100,14 +101,15 @@
void close_generator();
/* generation functions */
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_consts(vector<t_const*> consts);
- void generate_struct(t_struct* tstruct);
- void generate_service(t_service* tservice);
- void generate_xception(t_struct* tstruct);
+ void generate_typedef(t_typedef *ttypedef);
+ void generate_enum(t_enum *tenum);
+ void generate_consts(vector<t_const *> consts);
+ void generate_struct(t_struct *tstruct);
+ void generate_service(t_service *tservice);
+ void generate_xception(t_struct *tstruct);
-private:
+ private:
+
/* file streams */
ofstream f_types_;
ofstream f_types_impl_;
@@ -121,87 +123,50 @@
string nspace_lc;
/* helper functions */
- bool is_complex_type(t_type* ttype);
- string type_name(t_type* ttype, bool in_typedef = false, bool is_const = false);
- string property_type_name(t_type* ttype, bool in_typedef = false, bool is_const = false);
- string base_type_name(t_base_type* type);
- string type_to_enum(t_type* type);
- string constant_literal(t_type* type, t_const_value* value);
- string constant_value(string name, t_type* type, t_const_value* value);
- string function_signature(t_function* tfunction);
- string argument_list(t_struct* tstruct);
- string xception_list(t_struct* tstruct);
- string declare_field(t_field* tfield,
- bool init = false,
- bool pointer = false,
- bool constant = false,
- bool reference = false);
- void declare_local_variable(ofstream& out, t_type* ttype, string& base_name);
+ bool is_complex_type(t_type *ttype);
+ string type_name(t_type* ttype, bool in_typedef=false, bool is_const=false);
+ string property_type_name(t_type* ttype, bool in_typedef=false, bool is_const=false);
+ string base_type_name(t_base_type *type);
+ string type_to_enum(t_type *type);
+ string constant_literal(t_type *type, t_const_value *value);
+ string constant_value(string name, t_type *type, t_const_value *value);
+ string function_signature(t_function *tfunction);
+ string argument_list(t_struct *tstruct);
+ string xception_list(t_struct *tstruct);
+ string declare_field(t_field *tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false);
+ void declare_local_variable(ofstream &out, t_type *ttype, string &base_name);
/* generation functions */
- void generate_const_initializer(string name, t_type* type, t_const_value* value);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_handler(t_service* tservice);
- void generate_service_processor(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_object(t_struct* tstruct);
- void generate_struct_writer(ofstream& out,
- t_struct* tstruct,
- string this_name,
- string this_get = "",
- bool is_function = true);
- void generate_struct_reader(ofstream& out,
- t_struct* tstruct,
- string this_name,
- string this_get = "",
- bool is_function = true);
+ void generate_const_initializer(string name, t_type *type, t_const_value *value);
+ void generate_service_helpers(t_service *tservice);
+ void generate_service_client(t_service *tservice);
+ void generate_service_handler(t_service *tservice);
+ void generate_service_processor(t_service *tservice);
+ void generate_service_server(t_service *tservice);
+ void generate_object(t_struct *tstruct);
+ void generate_struct_writer(ofstream &out, t_struct *tstruct, string this_name, string this_get="", bool is_function=true);
+ void generate_struct_reader(ofstream &out, t_struct *tstruct, string this_name, string this_get="", bool is_function=true);
- void generate_serialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- string suffix,
- int error_ret);
- void generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix, int error_ret);
- void generate_serialize_container(ofstream& out, t_type* ttype, string prefix, int error_ret);
- void generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string key,
- string value,
- int error_ret);
- void generate_serialize_set_element(ofstream& out, t_set* tset, string element, int error_ret);
- void generate_serialize_list_element(ofstream& out,
- t_list* tlist,
- string list,
- string index,
- int error_ret);
+ void generate_serialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret);
+ void generate_serialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret);
+ void generate_serialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret);
+ void generate_serialize_map_element(ofstream &out, t_map *tmap, string key, string value, int error_ret);
+ void generate_serialize_set_element(ofstream &out, t_set *tset, string element, int error_ret);
+ void generate_serialize_list_element(ofstream &out, t_list *tlist, string list, string index, int error_ret);
- void generate_deserialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- string suffix,
- int error_ret,
- bool allocate = true);
- void generate_deserialize_struct(ofstream& out,
- t_struct* tstruct,
- string prefix,
- int error_ret,
- bool allocate = true);
- void generate_deserialize_container(ofstream& out, t_type* ttype, string prefix, int error_ret);
- void generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix, int error_ret);
- void generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix, int error_ret);
- void generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix,
- string index,
- int error_ret);
+ void generate_deserialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret, bool allocate=true);
+ void generate_deserialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret, bool allocate=true);
+ void generate_deserialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret);
+ void generate_deserialize_map_element(ofstream &out, t_map *tmap, string prefix, int error_ret);
+ void generate_deserialize_set_element(ofstream &out, t_set *tset, string prefix, int error_ret);
+ void generate_deserialize_list_element(ofstream &out, t_list *tlist, string prefix, string index, int error_ret);
- string generate_new_hash_from_type(t_type* key, t_type* value);
- string generate_new_array_from_type(t_type* ttype);
+ string generate_new_hash_from_type(t_type * key, t_type * value);
+ string generate_new_array_from_type(t_type * ttype);
- string generate_free_func_from_type(t_type* ttype);
- string generate_hash_func_from_type(t_type* ttype);
- string generate_cmp_func_from_type(t_type* ttype);
+ string generate_free_func_from_type(t_type * ttype);
+ string generate_hash_func_from_type(t_type * ttype);
+ string generate_cmp_func_from_type(t_type * ttype);
};
/**
@@ -217,9 +182,11 @@
string program_name_lc = to_lower_case(program_name_u);
/* create output files */
- string f_types_name = get_out_dir() + this->nspace_lc + program_name_lc + "_types.h";
+ string f_types_name = get_out_dir() + this->nspace_lc
+ + program_name_lc + "_types.h";
f_types_.open(f_types_name.c_str());
- string f_types_impl_name = get_out_dir() + this->nspace_lc + program_name_lc + "_types.c";
+ string f_types_impl_name = get_out_dir() + this->nspace_lc
+ + program_name_lc + "_types.c";
f_types_impl_.open(f_types_impl_name.c_str());
/* add thrift boilerplate headers */
@@ -227,53 +194,70 @@
f_types_impl_ << autogen_comment();
/* include inclusion guard */
- f_types_ << "#ifndef " << this->nspace_uc << program_name_uc << "_TYPES_H" << endl << "#define "
- << this->nspace_uc << program_name_uc << "_TYPES_H" << endl << endl;
+ f_types_ <<
+ "#ifndef " << this->nspace_uc << program_name_uc << "_TYPES_H" << endl <<
+ "#define " << this->nspace_uc << program_name_uc << "_TYPES_H" << endl <<
+ endl;
/* include base types */
- f_types_ << "/* base includes */" << endl << "#include <glib-object.h>" << endl
- << "#include <thrift/c_glib/thrift_struct.h>" << endl
- << "#include <thrift/c_glib/protocol/thrift_protocol.h>" << endl;
+ f_types_ <<
+ "/* base includes */" << endl <<
+ "#include <glib-object.h>" << endl <<
+ "#include <thrift/c_glib/thrift_struct.h>" << endl <<
+ "#include <thrift/c_glib/protocol/thrift_protocol.h>" << endl;
/* include other thrift includes */
- const vector<t_program*>& includes = program_->get_includes();
+ const vector<t_program *> &includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
- f_types_ << "/* other thrift includes */" << endl << "#include \"" << this->nspace_lc
- << initial_caps_to_underscores(includes[i]->get_name()) << "_types.h\"" << endl;
+ f_types_ <<
+ "/* other thrift includes */" << endl <<
+ "#include \"" << this->nspace_lc << initial_caps_to_underscores(includes[i]->get_name()) <<
+ "_types.h\"" << endl;
}
f_types_ << endl;
/* include custom headers */
- const vector<string>& c_includes = program_->get_c_includes();
+ const vector<string> &c_includes = program_->get_c_includes();
f_types_ << "/* custom thrift includes */" << endl;
for (size_t i = 0; i < c_includes.size(); ++i) {
if (c_includes[i][0] == '<') {
- f_types_ << "#include " << c_includes[i] << endl;
+ f_types_ <<
+ "#include " << c_includes[i] << endl;
} else {
- f_types_ << "#include \"" << c_includes[i] << "\"" << endl;
+ f_types_ <<
+ "#include \"" << c_includes[i] << "\"" << endl;
}
}
f_types_ << endl;
/* include math.h (for "INFINITY") in the implementation file, in case we
encounter a struct with a member of type double */
- f_types_impl_ << endl << "#include <math.h>" << endl;
+ f_types_impl_ <<
+ endl <<
+ "#include <math.h>" << endl;
// include the types file
- f_types_impl_ << endl << "#include \"" << this->nspace_lc << program_name_u << "_types.h\""
- << endl << "#include <thrift/c_glib/thrift.h>" << endl << endl;
+ f_types_impl_ <<
+ endl <<
+ "#include \"" << this->nspace_lc << program_name_u <<
+ "_types.h\"" << endl <<
+ "#include <thrift/c_glib/thrift.h>" << endl <<
+ endl;
- f_types_ << "/* begin types */" << endl << endl;
+ f_types_ <<
+ "/* begin types */" << endl << endl;
}
/**
* Finish up generation and close all file streams.
*/
void t_c_glib_generator::close_generator() {
- string program_name_uc = to_upper_case(initial_caps_to_underscores(program_name_));
+ string program_name_uc = to_upper_case
+ (initial_caps_to_underscores(program_name_));
/* end the header inclusion guard */
- f_types_ << "#endif /* " << this->nspace_uc << program_name_uc << "_TYPES_H */" << endl;
+ f_types_ <<
+ "#endif /* " << this->nspace_uc << program_name_uc << "_TYPES_H */" << endl;
/* close output file */
f_types_.close();
@@ -290,8 +274,10 @@
* typedef GHashTable * ThriftSomeMap;
*/
void t_c_glib_generator::generate_typedef(t_typedef* ttypedef) {
- f_types_ << indent() << "typedef " << type_name(ttypedef->get_type(), true) << " " << this->nspace
- << ttypedef->get_symbolic() << ";" << endl << endl;
+ f_types_ <<
+ indent() << "typedef " << type_name(ttypedef->get_type(), true) <<
+ " " << this->nspace << ttypedef->get_symbolic() << ";" << endl <<
+ endl;
}
/**
@@ -310,16 +296,17 @@
* };
* typedef enum _ThriftMyEnum ThriftMyEnum;
*/
-void t_c_glib_generator::generate_enum(t_enum* tenum) {
+void t_c_glib_generator::generate_enum(t_enum *tenum) {
string name = tenum->get_name();
string name_uc = to_upper_case(initial_caps_to_underscores(name));
- f_types_ << indent() << "enum _" << this->nspace << name << " {" << endl;
+ f_types_ <<
+ indent() << "enum _" << this->nspace << name << " {" << endl;
indent_up();
- vector<t_enum_value*> constants = tenum->get_constants();
- vector<t_enum_value*>::iterator c_iter;
+ vector<t_enum_value *> constants = tenum->get_constants();
+ vector<t_enum_value *>::iterator c_iter;
bool first = true;
/* output each of the enumeration elements */
@@ -330,28 +317,32 @@
f_types_ << "," << endl;
}
- f_types_ << indent() << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name();
- f_types_ << " = " << (*c_iter)->get_value();
+ f_types_ <<
+ indent() << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name();
+ f_types_ <<
+ " = " << (*c_iter)->get_value();
}
indent_down();
- f_types_ << endl << "};" << endl << "typedef enum _" << this->nspace << name << " "
- << this->nspace << name << ";" << endl << endl;
+ f_types_ <<
+ endl <<
+ "};" << endl <<
+ "typedef enum _" << this->nspace << name << " " << this->nspace << name << ";" << endl <<
+ endl;
f_types_ << "/* return the name of the constant */" << endl;
f_types_ << "const char *" << endl;
- f_types_ << "toString_" << name << "(int value); " << endl << endl;
- ;
+ f_types_ << "toString_"<<name<<"(int value); "<<endl<<endl;;
f_types_impl_ << "/* return the name of the constant */" << endl;
f_types_impl_ << "const char *" << endl;
- f_types_impl_ << "toString_" << name << "(int value) " << endl;
+ f_types_impl_ << "toString_"<<name<<"(int value) "<<endl;
f_types_impl_ << "{" << endl;
f_types_impl_ << " static __thread char buf[16] = {0};" << endl;
f_types_impl_ << " switch(value) {" << endl;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
- f_types_impl_ << " case " << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name() << ":"
- << "return \"" << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name()
- << "\";" << endl;
+ f_types_impl_ << " case " << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name()
+ << ":" << "return \"" << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name()
+ << "\";"<<endl;
}
f_types_impl_ << " default: g_snprintf(buf, 16, \"%d\", value); return buf;" << endl;
f_types_impl_ << " }" << endl;
@@ -361,22 +352,23 @@
/**
* Generates Thrift constants in C code.
*/
-void t_c_glib_generator::generate_consts(vector<t_const*> consts) {
+void t_c_glib_generator::generate_consts (vector<t_const *> consts) {
f_types_ << "/* constants */" << endl;
f_types_impl_ << "/* constants */" << endl;
- vector<t_const*>::iterator c_iter;
+ vector<t_const *>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
string name_uc = to_upper_case(name);
string name_lc = to_lower_case(name);
- t_type* type = (*c_iter)->get_type();
- t_const_value* value = (*c_iter)->get_value();
+ t_type *type = (*c_iter)->get_type();
+ t_const_value *value = (*c_iter)->get_value();
- f_types_ << indent() << "#define " << this->nspace_uc << name_uc << " "
- << constant_value(name_lc, type, value) << endl;
+ f_types_ <<
+ indent() << "#define " << this->nspace_uc << name_uc << " " <<
+ constant_value (name_lc, type, value) << endl;
- generate_const_initializer(name_lc, type, value);
+ generate_const_initializer (name_lc, type, value);
}
f_types_ << endl;
@@ -404,7 +396,7 @@
* typedef struct _ThriftBonk ThriftBonk
* // ... additional GObject boilerplate ...
*/
-void t_c_glib_generator::generate_struct(t_struct* tstruct) {
+void t_c_glib_generator::generate_struct (t_struct *tstruct) {
f_types_ << "/* struct " << tstruct->get_name() << " */" << endl;
generate_object(tstruct);
}
@@ -413,7 +405,7 @@
* Generate C code to represent Thrift services. Creates a new GObject
* which can be used to access the service.
*/
-void t_c_glib_generator::generate_service(t_service* tservice) {
+void t_c_glib_generator::generate_service (t_service *tservice) {
string svcname_u = initial_caps_to_underscores(tservice->get_name());
string svcname_uc = this->nspace_uc + to_upper_case(svcname_u);
string filename = this->nspace_lc + to_lower_case(svcname_u);
@@ -426,22 +418,27 @@
string program_name_lc = to_lower_case(program_name_u);
// add header file boilerplate
- f_header_ << autogen_comment();
+ f_header_ <<
+ autogen_comment();
// add an inclusion guard
- f_header_ << "#ifndef " << svcname_uc << "_H" << endl << "#define " << svcname_uc << "_H" << endl
- << endl;
+ f_header_ <<
+ "#ifndef " << svcname_uc << "_H" << endl <<
+ "#define " << svcname_uc << "_H" << endl <<
+ endl;
// add standard includes
- f_header_ << "#include <thrift/c_glib/processor/thrift_dispatch_processor.h>" << endl << endl;
- f_header_ << "#include \"" << this->nspace_lc << program_name_lc << "_types.h\"" << endl;
+ f_header_ <<
+ "#include <thrift/c_glib/processor/thrift_dispatch_processor.h>" << endl <<
+ endl;
+ f_header_ <<
+ "#include \"" << this->nspace_lc << program_name_lc << "_types.h\"" << endl;
// if we are inheriting from another service, include its header
- t_service* extends_service = tservice->get_extends();
+ t_service *extends_service = tservice->get_extends();
if (extends_service != NULL) {
- f_header_ << "#include \"" << this->nspace_lc
- << to_lower_case(initial_caps_to_underscores(extends_service->get_name())) << ".h\""
- << endl;
+ f_header_ <<
+ "#include \"" << this->nspace_lc << to_lower_case(initial_caps_to_underscores(extends_service->get_name())) << ".h\"" << endl;
}
f_header_ << endl;
@@ -450,24 +447,29 @@
f_service_.open(f_service_name.c_str());
// add the boilerplace header
- f_service_ << autogen_comment();
+ f_service_ <<
+ autogen_comment();
// include the headers
- f_service_ << "#include <string.h>" << endl << "#include <thrift/c_glib/thrift.h>" << endl
- << "#include <thrift/c_glib/thrift_application_exception.h>" << endl << "#include \""
- << filename << ".h\"" << endl << endl;
+ f_service_ <<
+ "#include <string.h>" << endl <<
+ "#include <thrift/c_glib/thrift.h>" << endl <<
+ "#include <thrift/c_glib/thrift_application_exception.h>" << endl <<
+ "#include \"" << filename << ".h\"" << endl <<
+ endl;
// generate the service-helper classes
- generate_service_helpers(tservice);
+ generate_service_helpers (tservice);
// generate the client objects
- generate_service_client(tservice);
+ generate_service_client (tservice);
// generate the server objects
- generate_service_server(tservice);
+ generate_service_server (tservice);
// end the header inclusion guard
- f_header_ << "#endif /* " << svcname_uc << "_H */" << endl;
+ f_header_ <<
+ "#endif /* " << svcname_uc << "_H */" << endl;
// close the files
f_service_.close();
@@ -477,7 +479,7 @@
/**
*
*/
-void t_c_glib_generator::generate_xception(t_struct* tstruct) {
+void t_c_glib_generator::generate_xception (t_struct *tstruct) {
string name = tstruct->get_name();
string name_u = initial_caps_to_underscores(name);
string name_lc = to_lower_case(name_u);
@@ -485,18 +487,29 @@
generate_object(tstruct);
- f_types_ << "/* exception */" << endl << "typedef enum" << endl << "{" << endl << " "
- << this->nspace_uc << name_uc << "_ERROR_CODE," << endl << "} " << this->nspace << name
- << "Error;" << endl << endl << "GQuark " << this->nspace_lc << name_lc
- << "_error_quark (void);" << endl << "#define " << this->nspace_uc << name_uc
- << "_ERROR (" << this->nspace_lc << name_lc << "_error_quark())" << endl << endl << endl;
+ f_types_ << "/* exception */" << endl <<
+ "typedef enum" << endl <<
+ "{" << endl <<
+ " " << this->nspace_uc << name_uc << "_ERROR_CODE," << endl <<
+ "} " << this->nspace << name << "Error;" << endl <<
+ endl <<
+ "GQuark " << this->nspace_lc << name_lc << "_error_quark (void);" << endl <<
+ "#define " << this->nspace_uc << name_uc << "_ERROR (" <<
+ this->nspace_lc << name_lc << "_error_quark())" << endl <<
+ endl <<
+ endl;
- f_types_impl_ << "/* define the GError domain for exceptions */" << endl << "#define "
- << this->nspace_uc << name_uc << "_ERROR_DOMAIN \"" << this->nspace_lc << name_lc
- << "_error_quark\"" << endl << "GQuark" << endl << this->nspace_lc << name_lc
- << "_error_quark (void)" << endl << "{" << endl
- << " return g_quark_from_static_string (" << this->nspace_uc << name_uc
- << "_ERROR_DOMAIN);" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "/* define the GError domain for exceptions */" << endl <<
+ "#define " << this->nspace_uc << name_uc << "_ERROR_DOMAIN \"" <<
+ this->nspace_lc << name_lc << "_error_quark\"" << endl <<
+ "GQuark" << endl <<
+ this->nspace_lc << name_lc << "_error_quark (void)" << endl <<
+ "{" << endl <<
+ " return g_quark_from_static_string (" << this->nspace_uc << name_uc <<
+ "_ERROR_DOMAIN);" << endl <<
+ "}" << endl <<
+ endl;
}
/********************
@@ -506,19 +519,22 @@
/**
* Returns true if ttype is not a primitive.
*/
-bool t_c_glib_generator::is_complex_type(t_type* ttype) {
- ttype = get_true_type(ttype);
+bool t_c_glib_generator::is_complex_type(t_type *ttype) {
+ ttype = get_true_type (ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception();
+ return ttype->is_container()
+ || ttype->is_struct()
+ || ttype->is_xception();
}
+
/**
* Maps a Thrift t_type to a C type.
*/
-string t_c_glib_generator::type_name(t_type* ttype, bool in_typedef, bool is_const) {
- (void)in_typedef;
+string t_c_glib_generator::type_name (t_type* ttype, bool in_typedef, bool is_const) {
+ (void) in_typedef;
if (ttype->is_base_type()) {
- string bname = base_type_name((t_base_type*)ttype);
+ string bname = base_type_name ((t_base_type *) ttype);
if (is_const) {
return "const " + bname;
@@ -530,7 +546,7 @@
if (ttype->is_container()) {
string cname;
- t_container* tcontainer = (t_container*)ttype;
+ t_container *tcontainer = (t_container *) ttype;
if (tcontainer->has_cpp_name()) {
cname = tcontainer->get_cpp_name();
} else if (ttype->is_map()) {
@@ -544,24 +560,24 @@
} else if (ttype->is_list()) {
// TODO: investigate other implementations besides GPtrArray
cname = "GPtrArray *";
- t_type* etype = ((t_list*)ttype)->get_elem_type();
+ t_type *etype = ((t_list *) ttype)->get_elem_type();
if (etype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)etype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) etype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- cname = "GArray *";
- break;
- case t_base_type::TYPE_STRING:
- break;
- default:
- throw "compiler error: no array info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ cname = "GArray *";
+ break;
+ case t_base_type::TYPE_STRING:
+ break;
+ default:
+ throw "compiler error: no array info for type";
}
}
}
@@ -576,7 +592,7 @@
// check for a namespace
string pname = this->nspace + ttype->get_name();
- if (is_complex_type(ttype)) {
+ if (is_complex_type (ttype)) {
pname += " *";
}
@@ -595,23 +611,25 @@
* bits map to the same type, gint, as opposed to their width-specific type
* (gint8, gint16 or gint32).
*/
-string t_c_glib_generator::property_type_name(t_type* ttype, bool in_typedef, bool is_const) {
+string t_c_glib_generator::property_type_name (t_type* ttype,
+ bool in_typedef,
+ bool is_const) {
string result;
if (ttype->is_base_type()) {
- switch (((t_base_type*)ttype)->get_base()) {
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- if (is_const) {
- result = "const gint";
- } else {
- result = "gint";
- }
- break;
+ switch (((t_base_type *) ttype)->get_base()) {
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ if (is_const) {
+ result = "const gint";
+ } else {
+ result = "gint";
+ }
+ break;
- default:
- result = type_name(ttype, in_typedef, is_const);
+ default:
+ result = type_name(ttype, in_typedef, is_const);
}
} else {
result = type_name(ttype, in_typedef, is_const);
@@ -623,32 +641,33 @@
/**
* Maps a Thrift primitive to a C primitive.
*/
-string t_c_glib_generator::base_type_name(t_base_type* type) {
+string t_c_glib_generator::base_type_name(t_base_type *type) {
t_base_type::t_base tbase = type->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- return "void";
- case t_base_type::TYPE_STRING:
- if (type->is_binary()) {
- return "GByteArray *";
- } else {
- return "gchar *";
- }
- case t_base_type::TYPE_BOOL:
- return "gboolean";
- case t_base_type::TYPE_BYTE:
- return "gint8";
- case t_base_type::TYPE_I16:
- return "gint16";
- case t_base_type::TYPE_I32:
- return "gint32";
- case t_base_type::TYPE_I64:
- return "gint64";
- case t_base_type::TYPE_DOUBLE:
- return "gdouble";
- default:
- throw "compiler error: no C base type name for base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_VOID:
+ return "void";
+ case t_base_type::TYPE_STRING:
+ if (type->is_binary()) {
+ return "GByteArray *";
+ } else {
+ return "gchar *";
+ }
+ case t_base_type::TYPE_BOOL:
+ return "gboolean";
+ case t_base_type::TYPE_BYTE:
+ return "gint8";
+ case t_base_type::TYPE_I16:
+ return "gint16";
+ case t_base_type::TYPE_I32:
+ return "gint32";
+ case t_base_type::TYPE_I64:
+ return "gint64";
+ case t_base_type::TYPE_DOUBLE:
+ return "gdouble";
+ default:
+ throw "compiler error: no C base type name for base type "
+ + t_base_type::t_base_name (tbase);
}
}
@@ -656,29 +675,29 @@
* Returns a member of the ThriftType C enumeration in thrift_protocol.h
* for a Thrift type.
*/
-string t_c_glib_generator::type_to_enum(t_type* type) {
- type = get_true_type(type);
+string t_c_glib_generator::type_to_enum (t_type *type) {
+ type = get_true_type (type);
if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- return "T_STRING";
- case t_base_type::TYPE_BOOL:
- return "T_BOOL";
- case t_base_type::TYPE_BYTE:
- return "T_BYTE";
- case t_base_type::TYPE_I16:
- return "T_I16";
- case t_base_type::TYPE_I32:
- return "T_I32";
- case t_base_type::TYPE_I64:
- return "T_I64";
- case t_base_type::TYPE_DOUBLE:
- return "T_DOUBLE";
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "T_STRING";
+ case t_base_type::TYPE_BOOL:
+ return "T_BOOL";
+ case t_base_type::TYPE_BYTE:
+ return "T_BYTE";
+ case t_base_type::TYPE_I16:
+ return "T_I16";
+ case t_base_type::TYPE_I32:
+ return "T_I32";
+ case t_base_type::TYPE_I64:
+ return "T_I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "T_DOUBLE";
}
} else if (type->is_enum()) {
return "T_I32";
@@ -697,63 +716,65 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
/**
* Returns a Thrift constant formatted as a literal for inclusion in C code.
*/
-string t_c_glib_generator::constant_literal(t_type* type, t_const_value* value) {
+string t_c_glib_generator::constant_literal(t_type *type, t_const_value *value) {
ostringstream render;
if (type->is_base_type()) {
/* primitives */
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << "\"" + value->get_string() + "\"";
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() != 0) ? "TRUE" : "FALSE");
- break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_DOUBLE:
- render << value->get_double();
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_STRING:
+ render << "\"" + value->get_string() + "\"";
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() != 0) ? "TRUE" : "FALSE");
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ render << value->get_integer();
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ render << value->get_double();
+ break;
+ default:
+ throw "compiler error: no const of base type "
+ + t_base_type::t_base_name (tbase);
}
} else {
t_const_value::t_const_value_type value_type = value->get_type();
switch (value_type) {
- case t_const_value::CV_IDENTIFIER:
- render << value->get_integer();
- break;
- case t_const_value::CV_LIST:
- render << "{ ";
- {
- t_type* elem_type = ((t_list*)type)->get_elem_type();
- const vector<t_const_value*>& list = value->get_list();
- vector<t_const_value*>::const_iterator list_iter;
+ case t_const_value::CV_IDENTIFIER:
+ render << value->get_integer();
+ break;
+ case t_const_value::CV_LIST:
+ render << "{ ";
+ {
+ t_type *elem_type = ((t_list *) type)->get_elem_type();
+ const vector<t_const_value *> &list = value->get_list();
+ vector<t_const_value *>::const_iterator list_iter;
- if (list.size() > 0) {
- list_iter = list.begin();
- render << constant_literal(elem_type, *list_iter);
+ if (list.size() > 0) {
+ list_iter = list.begin();
+ render << constant_literal(elem_type, *list_iter);
- while (++list_iter != list.end()) {
- render << ", " << constant_literal(elem_type, *list_iter);
+ while (++list_iter != list.end()) {
+ render << ", " << constant_literal(elem_type, *list_iter);
+ }
}
}
- }
- render << " }";
- break;
- case t_const_value::CV_MAP:
- default:
- render << "NULL /* not supported */";
+ render << " }";
+ break;
+ case t_const_value::CV_MAP:
+ default:
+ render << "NULL /* not supported */";
}
}
@@ -763,40 +784,42 @@
/**
* Returns C code that represents a Thrift constant.
*/
-string t_c_glib_generator::constant_value(string name, t_type* type, t_const_value* value) {
+string t_c_glib_generator::constant_value(string name, t_type *type, t_const_value *value) {
ostringstream render;
if (type->is_base_type()) {
/* primitives */
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << "g_strdup (\"" + value->get_string() + "\")";
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() != 0) ? 1 : 0);
- break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
+ case t_base_type::TYPE_STRING:
+ render << "g_strdup (\"" + value->get_string() + "\")";
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() != 0) ? 1 : 0);
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
render << value->get_integer();
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type "
+ + t_base_type::t_base_name (tbase);
}
} else if (type->is_enum()) {
- render << "(" << type_name(type) << ")" << value->get_integer();
- } else if (type->is_struct() || type->is_xception() || type->is_list() || type->is_set()
- || type->is_map()) {
- render << "(" << this->nspace_lc << to_lower_case(name) << "_constant())";
+ render << "(" << type_name (type) << ")" << value->get_integer();
+ } else if (type->is_struct() || type->is_xception() || type->is_list()
+ || type->is_set() || type->is_map()) {
+ render << "(" << this->nspace_lc <<
+ to_lower_case(name) << "_constant())";
} else {
render << "NULL /* not supported */";
}
@@ -819,10 +842,13 @@
bool has_return = !ttype->is_void();
bool has_args = arglist->get_members().size() == 0;
bool has_xceptions = xlist->get_members().size() == 0;
- return "gboolean " + this->nspace_lc + fname + " (" + this->nspace + service_name_ + "If * iface"
- + (has_return ? ", " + type_name(ttype) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arglist)))
- + (has_xceptions ? "" : (", " + xception_list(xlist))) + ", GError ** error)";
+ return
+ "gboolean " + this->nspace_lc + fname + " (" + this->nspace
+ + service_name_ + "If * iface"
+ + (has_return ? ", " + type_name(ttype) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arglist)))
+ + (has_xceptions ? "" : (", " + xception_list (xlist)))
+ + ", GError ** error)";
}
/**
@@ -831,7 +857,7 @@
* @param tstruct The struct definition
* @return Comma sepearated list of all field names in that struct
*/
-string t_c_glib_generator::argument_list(t_struct* tstruct) {
+string t_c_glib_generator::argument_list (t_struct* tstruct) {
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@@ -843,7 +869,8 @@
} else {
result += ", ";
}
- result += type_name((*f_iter)->get_type(), false, true) + " " + (*f_iter)->get_name();
+ result += type_name((*f_iter)->get_type(), false, true) + " " +
+ (*f_iter)->get_name();
}
return result;
}
@@ -854,7 +881,7 @@
* @param tstruct The struct definition
* @return Comma sepearated list of all field names in that struct
*/
-string t_c_glib_generator::xception_list(t_struct* tstruct) {
+string t_c_glib_generator::xception_list (t_struct* tstruct) {
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@@ -866,15 +893,17 @@
} else {
result += ", ";
}
- result += type_name((*f_iter)->get_type(), false, false) + "* " + (*f_iter)->get_name();
+ result += type_name((*f_iter)->get_type(), false, false) + "* " +
+ (*f_iter)->get_name();
}
return result;
}
+
/**
* Declares a field, including any necessary initialization.
*/
-string t_c_glib_generator::declare_field(t_field* tfield,
+string t_c_glib_generator::declare_field(t_field *tfield,
bool init,
bool pointer,
bool constant,
@@ -895,28 +924,29 @@
t_type* type = get_true_type(tfield->get_type());
if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- break;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- result += " = 0";
- break;
- case t_base_type::TYPE_DOUBLE:
- result += " = (gdouble) 0";
- break;
- case t_base_type::TYPE_STRING:
- result += " = NULL";
- break;
- default:
- throw "compiler error: no C intializer for base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_VOID:
+ break;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ result += " = 0";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ result += " = (gdouble) 0";
+ break;
+ case t_base_type::TYPE_STRING:
+ result += " = NULL";
+ break;
+ default:
+ throw "compiler error: no C intializer for base type "
+ + t_base_type::t_base_name (tbase);
}
} else if (type->is_enum()) {
- result += " = (" + type_name(type) + ") 0";
+ result += " = (" + type_name (type) + ") 0";
} else if (type->is_struct() || type->is_container()) {
result += " = NULL";
}
@@ -932,24 +962,22 @@
/**
* Generates C code that initializes complex constants.
*/
-void t_c_glib_generator::generate_const_initializer(string name,
- t_type* type,
- t_const_value* value) {
+void t_c_glib_generator::generate_const_initializer(string name, t_type *type, t_const_value *value) {
string name_u = initial_caps_to_underscores(name);
string name_lc = to_lower_case(name_u);
string type_u = initial_caps_to_underscores(type->get_name());
string type_uc = to_upper_case(type_u);
if (type->is_struct() || type->is_xception()) {
- const vector<t_field*>& fields = ((t_struct*)type)->get_members();
- vector<t_field*>::const_iterator f_iter;
- const map<t_const_value*, t_const_value*>& val = value->get_map();
- map<t_const_value*, t_const_value*>::const_iterator v_iter;
+ const vector<t_field *> &fields = ((t_struct *) type)->get_members();
+ vector<t_field *>::const_iterator f_iter;
+ const map<t_const_value *, t_const_value *> &val = value->get_map();
+ map<t_const_value *, t_const_value *>::const_iterator v_iter;
ostringstream initializers;
// initialize any constants that may be referenced by this initializer
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- t_type* field_type = NULL;
+ t_type *field_type = NULL;
string field_name = "";
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -959,25 +987,37 @@
}
}
if (field_type == NULL) {
- throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+ throw "type error: " + type->get_name() + " has no field "
+ + v_iter->first->get_string();
}
- field_name = tmp(field_name);
+ field_name = tmp (field_name);
- generate_const_initializer(name + "_constant_" + field_name, field_type, v_iter->second);
- initializers << " constant->" << v_iter->first->get_string() << " = "
- << constant_value(name + "_constant_" + field_name, field_type, v_iter->second)
- << ";" << endl << " constant->__isset_" << v_iter->first->get_string()
- << " = TRUE;" << endl;
+ generate_const_initializer (name + "_constant_" + field_name,
+ field_type, v_iter->second);
+ initializers <<
+ " constant->" << v_iter->first->get_string() << " = " <<
+ constant_value (name + "_constant_" + field_name,
+ field_type, v_iter->second) << ";" << endl <<
+ " constant->__isset_" << v_iter->first->get_string() <<
+ " = TRUE;" << endl;
}
// implement the initializer
- f_types_impl_ << "static " << this->nspace << type->get_name() << " *" << endl
- << this->nspace_lc << name_lc << "_constant (void)" << endl << "{" << endl
- << " static " << this->nspace << type->get_name() << " *constant = NULL;" << endl
- << " if (constant == NULL)" << endl << " {" << endl
- << " constant = g_object_new (" << this->nspace_uc << "TYPE_" << type_uc
- << ", NULL);" << endl << initializers.str() << endl << " }" << endl
- << " return constant;" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "static " << this->nspace << type->get_name() << " *" << endl <<
+ this->nspace_lc << name_lc << "_constant (void)" << endl <<
+ "{" << endl <<
+ " static " << this->nspace << type->get_name() <<
+ " *constant = NULL;" << endl <<
+ " if (constant == NULL)" << endl <<
+ " {" << endl <<
+ " constant = g_object_new (" << this->nspace_uc << "TYPE_" <<
+ type_uc << ", NULL);" << endl <<
+ initializers.str() << endl <<
+ " }" << endl <<
+ " return constant;" << endl <<
+ "}" << endl <<
+ endl;
} else if (type->is_list()) {
string list_type = "GPtrArray *";
// TODO: This initialization should contain a free function for container
@@ -990,99 +1030,126 @@
vector<t_const_value*>::const_iterator v_iter;
ostringstream initializers;
- list_initializer = generate_new_array_from_type(etype);
+ list_initializer = generate_new_array_from_type (etype);
if (etype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)etype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) etype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- list_type = "GArray *";
- list_appender = "g_array_append_val";
- list_variable = true;
- break;
- case t_base_type::TYPE_STRING:
- break;
- default:
- throw "compiler error: no array info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ list_type = "GArray *";
+ list_appender = "g_array_append_val";
+ list_variable = true;
+ break;
+ case t_base_type::TYPE_STRING:
+ break;
+ default:
+ throw "compiler error: no array info for type";
}
}
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- string fname = tmp(name);
+ string fname = tmp (name);
- generate_const_initializer(fname, etype, (*v_iter));
+ generate_const_initializer (fname, etype, (*v_iter));
if (list_variable) {
- initializers << " " << type_name(etype) << " " << fname << " = "
- << constant_value(fname, (t_type*)etype, (*v_iter)) << ";" << endl << " "
- << list_appender << "(constant, " << fname << ");" << endl;
+ initializers <<
+ " " << type_name (etype) << " " << fname << " = " <<
+ constant_value (fname, (t_type *) etype, (*v_iter)) << ";" <<
+ endl <<
+ " " << list_appender << "(constant, " << fname << ");" << endl;
} else {
- initializers << " " << list_appender << "(constant, "
- << constant_value(fname, (t_type*)etype, (*v_iter)) << ");" << endl;
+ initializers <<
+ " " << list_appender << "(constant, " <<
+ constant_value (fname, (t_type *) etype, (*v_iter)) << ");" << endl;
}
}
- f_types_impl_ << "static " << list_type << endl << this->nspace_lc << name_lc
- << "_constant (void)" << endl << "{" << endl << " static " << list_type
- << " constant = NULL;" << endl << " if (constant == NULL)" << endl << " {"
- << endl << " constant = " << list_initializer << endl << initializers.str()
- << endl << " }" << endl << " return constant;" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "static " << list_type << endl <<
+ this->nspace_lc << name_lc << "_constant (void)" << endl <<
+ "{" << endl <<
+ " static " << list_type << " constant = NULL;" << endl <<
+ " if (constant == NULL)" << endl <<
+ " {" << endl <<
+ " constant = " << list_initializer << endl <<
+ initializers.str() << endl <<
+ " }" << endl <<
+ " return constant;" << endl <<
+ "}" << endl <<
+ endl;
} else if (type->is_set()) {
- t_type* etype = ((t_set*)type)->get_elem_type();
- const vector<t_const_value*>& val = value->get_list();
+ t_type *etype = ((t_set *) type)->get_elem_type();
+ const vector<t_const_value *>& val = value->get_list();
vector<t_const_value*>::const_iterator v_iter;
ostringstream initializers;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- string fname = tmp(name);
- generate_const_initializer(fname, etype, (*v_iter));
- initializers << " " << type_name(etype) << " " << fname << " = "
- << constant_value(fname, (t_type*)etype, (*v_iter)) << ";" << endl
- << " g_hash_table_insert (constant, &" << fname << ", &" << fname << ");"
- << endl;
+ string fname = tmp (name);
+ generate_const_initializer (fname, etype, (*v_iter));
+ initializers <<
+ " " << type_name (etype) << " " << fname << " = " <<
+ constant_value (fname, (t_type *) etype, (*v_iter)) << ";" << endl <<
+ " g_hash_table_insert (constant, &" << fname << ", &" << fname <<
+ ");" << endl;
}
- f_types_impl_ << "static GHashTable *" << endl << this->nspace_lc << name_lc
- << "_constant (void)" << endl << "{" << endl
- << " static GHashTable *constant = NULL;" << endl << " if (constant == NULL)"
- << endl << " {" << endl <<
- // TODO: This initialization should contain a free function for elements
- " constant = g_hash_table_new (NULL, NULL);" << endl << initializers.str() << endl
- << " }" << endl << " return constant;" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "static GHashTable *" << endl <<
+ this->nspace_lc << name_lc << "_constant (void)" << endl <<
+ "{" << endl <<
+ " static GHashTable *constant = NULL;" << endl <<
+ " if (constant == NULL)" << endl <<
+ " {" << endl <<
+ // TODO: This initialization should contain a free function for elements
+ " constant = g_hash_table_new (NULL, NULL);" << endl <<
+ initializers.str() << endl <<
+ " }" << endl <<
+ " return constant;" << endl <<
+ "}" << endl <<
+ endl;
} else if (type->is_map()) {
- t_type* ktype = ((t_map*)type)->get_key_type();
- t_type* vtype = ((t_map*)type)->get_val_type();
- const vector<t_const_value*>& val = value->get_list();
+ t_type *ktype = ((t_map *) type)->get_key_type();
+ t_type *vtype = ((t_map *) type)->get_val_type();
+ const vector<t_const_value *>& val = value->get_list();
vector<t_const_value*>::const_iterator v_iter;
ostringstream initializers;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- string fname = tmp(name);
+ string fname = tmp (name);
string kname = fname + "key";
string vname = fname + "val";
- generate_const_initializer(kname, ktype, (*v_iter));
- generate_const_initializer(vname, vtype, (*v_iter));
+ generate_const_initializer (kname, ktype, (*v_iter));
+ generate_const_initializer (vname, vtype, (*v_iter));
- initializers << " " << type_name(ktype) << " " << kname << " = "
- << constant_value(kname, (t_type*)ktype, (*v_iter)) << ";" << endl << " "
- << type_name(vtype) << " " << vname << " = "
- << constant_value(vname, (t_type*)vtype, (*v_iter)) << ";" << endl
- << " g_hash_table_insert (constant, &" << fname << ", &" << fname << ");"
- << endl;
+ initializers <<
+ " " << type_name (ktype) << " " << kname << " = " <<
+ constant_value (kname, (t_type *) ktype, (*v_iter)) << ";" << endl <<
+ " " << type_name (vtype) << " " << vname << " = " <<
+ constant_value (vname, (t_type *) vtype, (*v_iter)) << ";" << endl <<
+ " g_hash_table_insert (constant, &" << fname << ", &" << fname <<
+ ");" << endl;
}
- f_types_impl_ << "static GHashTable *" << endl << this->nspace_lc << name_lc
- << "_constant (void)" << endl << "{" << endl
- << " static GHashTable *constant = NULL;" << endl << " if (constant == NULL)"
- << endl << " {" << endl <<
- // TODO: This initialization should contain a free function for elements
- " constant = g_hash_table_new (NULL, NULL);" << endl << initializers.str() << endl
- << " }" << endl << " return constant;" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "static GHashTable *" << endl <<
+ this->nspace_lc << name_lc << "_constant (void)" << endl <<
+ "{" << endl <<
+ " static GHashTable *constant = NULL;" << endl <<
+ " if (constant == NULL)" << endl <<
+ " {" << endl <<
+ // TODO: This initialization should contain a free function for elements
+ " constant = g_hash_table_new (NULL, NULL);" << endl <<
+ initializers.str() << endl <<
+ " }" << endl <<
+ " return constant;" << endl <<
+ "}" << endl <<
+ endl;
}
}
@@ -1092,18 +1159,22 @@
*
* @param tservice The service for which to generate helper classes
*/
-void t_c_glib_generator::generate_service_helpers(t_service* tservice) {
+void t_c_glib_generator::generate_service_helpers(t_service *tservice) {
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator function_iter;
// Iterate through the service's methods
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
string function_name = (*function_iter)->get_name();
- t_struct* arg_list = (*function_iter)->get_arglist();
+ t_struct *arg_list = (*function_iter)->get_arglist();
string arg_list_name_orig = arg_list->get_name();
// Generate the arguments class
- arg_list->set_name(tservice->get_name() + underscores_to_initial_caps(function_name) + "Args");
+ arg_list->set_name(tservice->get_name() +
+ underscores_to_initial_caps(function_name) +
+ "Args");
generate_struct(arg_list);
arg_list->set_name(arg_list_name_orig);
@@ -1111,7 +1182,9 @@
// Generate the result class
if (!(*function_iter)->is_oneway()) {
t_struct result(program_,
- tservice->get_name() + underscores_to_initial_caps(function_name) + "Result");
+ tservice->get_name() +
+ underscores_to_initial_caps(function_name) +
+ "Result");
t_field success((*function_iter)->get_returntype(), "success", 0);
success.set_req(t_field::T_OPTIONAL);
if (!(*function_iter)->get_returntype()->is_void()) {
@@ -1121,7 +1194,9 @@
t_struct* xs = (*function_iter)->get_xceptions();
const vector<t_field*>& fields = xs->get_members();
vector<t_field*>::const_iterator field_iter;
- for (field_iter = fields.begin(); field_iter != fields.end(); ++field_iter) {
+ for (field_iter = fields.begin();
+ field_iter != fields.end();
+ ++field_iter) {
(*field_iter)->set_req(t_field::T_OPTIONAL);
result.append(*field_iter);
}
@@ -1134,7 +1209,7 @@
/**
* Generates C code that represents a Thrift service client.
*/
-void t_c_glib_generator::generate_service_client(t_service* tservice) {
+void t_c_glib_generator::generate_service_client(t_service *tservice) {
/* get some C friendly service names */
string service_name_lc = to_lower_case(initial_caps_to_underscores(service_name_));
string service_name_uc = to_upper_case(service_name_lc);
@@ -1148,38 +1223,47 @@
// The service this service extends, or NULL if it extends no
// service
- t_service* extends_service = tservice->get_extends();
+ t_service *extends_service = tservice->get_extends();
if (extends_service) {
// The name of the parent service
parent_service_name = extends_service->get_name();
- parent_service_name_lc = to_lower_case(initial_caps_to_underscores(parent_service_name));
+ parent_service_name_lc =
+ to_lower_case(initial_caps_to_underscores(parent_service_name));
parent_service_name_uc = to_upper_case(parent_service_name_lc);
// The names of the client class' parent class and type
parent_class_name = this->nspace + parent_service_name + "Client";
- parent_type_name = this->nspace_uc + "TYPE_" + parent_service_name_uc + "_CLIENT";
+ parent_type_name =
+ this->nspace_uc + "TYPE_" + parent_service_name_uc + "_CLIENT";
}
// The base service (the topmost in the "extends" hierarchy), on
// whose client class the "input_protocol" and "output_protocol"
// properties are defined
- t_service* base_service = tservice;
+ t_service *base_service = tservice;
while (base_service->get_extends()) {
base_service = base_service->get_extends();
}
string base_service_name = base_service->get_name();
- string base_service_name_lc = to_lower_case(initial_caps_to_underscores(base_service_name));
+ string base_service_name_lc =
+ to_lower_case(initial_caps_to_underscores(base_service_name));
string base_service_name_uc = to_upper_case(base_service_name_lc);
// Generate the client interface dummy object in the header.
- f_header_ << "/* " << service_name_ << " service interface */" << endl << "typedef struct _"
- << this->nspace << service_name_ << "If " << this->nspace << service_name_ << "If; "
- << " /* dummy object */" << endl << endl;
+ f_header_ <<
+ "/* " << service_name_ << " service interface */" << endl <<
+ "typedef struct _" << this->nspace << service_name_ << "If " <<
+ this->nspace << service_name_ << "If; " <<
+ " /* dummy object */" << endl <<
+ endl;
// Generate the client interface object in the header.
- f_header_ << "struct _" << this->nspace << service_name_ << "IfInterface" << endl << "{" << endl
- << " GTypeInterface parent;" << endl << endl;
+ f_header_ <<
+ "struct _" << this->nspace << service_name_ << "IfInterface" << endl <<
+ "{" << endl <<
+ " GTypeInterface parent;" << endl <<
+ endl;
/* write out the functions for this interface */
indent_up();
@@ -1188,135 +1272,163 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
/* make the function name C friendly */
string funname = initial_caps_to_underscores((*f_iter)->get_name());
- t_type* ttype = (*f_iter)->get_returntype();
- t_struct* arglist = (*f_iter)->get_arglist();
- t_struct* xlist = (*f_iter)->get_xceptions();
+ t_type *ttype = (*f_iter)->get_returntype();
+ t_struct *arglist = (*f_iter)->get_arglist();
+ t_struct *xlist = (*f_iter)->get_xceptions();
bool has_return = !ttype->is_void();
bool has_args = arglist->get_members().size() == 0;
bool has_xceptions = xlist->get_members().size() == 0;
string params = "(" + this->nspace + service_name_ + "If *iface"
- + (has_return ? ", " + type_name(ttype) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arglist)))
- + (has_xceptions ? "" : (", " + xception_list(xlist))) + ", GError **error)";
+ + (has_return ? ", " + type_name (ttype) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arglist)))
+ + (has_xceptions ? "" : (", " + xception_list (xlist)))
+ + ", GError **error)";
- indent(f_header_) << "gboolean (*" << funname << ") " << params << ";" << endl;
+ indent(f_header_) << "gboolean (*" << funname << ") " << params << ";" <<
+ endl;
}
indent_down();
- f_header_ << "};" << endl << "typedef struct _" << this->nspace << service_name_ << "IfInterface "
- << this->nspace << service_name_ << "IfInterface;" << endl << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << this->nspace << service_name_ << "IfInterface " <<
+ this->nspace << service_name_ << "IfInterface;" << endl <<
+ endl;
// generate all the interface boilerplate
- f_header_ << "GType " << this->nspace_lc << service_name_lc << "_if_get_type (void);" << endl
- << "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_IF "
- << "(" << this->nspace_lc << service_name_lc << "_if_get_type())" << endl << "#define "
- << this->nspace_uc << service_name_uc << "_IF(obj) "
- << "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_IF, " << this->nspace << service_name_ << "If))" << endl
- << "#define " << this->nspace_uc << "IS_" << service_name_uc << "_IF(obj) "
- << "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_IF))" << endl << "#define " << this->nspace_uc
- << service_name_uc << "_IF_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), "
- << this->nspace_uc << "TYPE_" << service_name_uc << "_IF, " << this->nspace
- << service_name_ << "IfInterface))" << endl << endl;
+ f_header_ <<
+ "GType " << this->nspace_lc << service_name_lc <<
+ "_if_get_type (void);" << endl <<
+ "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_IF " <<
+ "(" << this->nspace_lc << service_name_lc << "_if_get_type())" <<
+ endl <<
+ "#define " << this->nspace_uc << service_name_uc << "_IF(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_IF, " <<
+ this->nspace << service_name_ << "If))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << service_name_uc << "_IF(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_IF))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc <<
+ "_IF_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_IF, " <<
+ this->nspace << service_name_ << "IfInterface))" << endl <<
+ endl;
// write out all the interface function prototypes
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
/* make the function name C friendly */
string funname = initial_caps_to_underscores((*f_iter)->get_name());
- t_type* ttype = (*f_iter)->get_returntype();
- t_struct* arglist = (*f_iter)->get_arglist();
- t_struct* xlist = (*f_iter)->get_xceptions();
+ t_type *ttype = (*f_iter)->get_returntype();
+ t_struct *arglist = (*f_iter)->get_arglist();
+ t_struct *xlist = (*f_iter)->get_xceptions();
bool has_return = !ttype->is_void();
bool has_args = arglist->get_members().size() == 0;
bool has_xceptions = xlist->get_members().size() == 0;
string params = "(" + this->nspace + service_name_ + "If *iface"
- + (has_return ? ", " + type_name(ttype) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arglist)))
- + (has_xceptions ? "" : (", " + xception_list(xlist))) + ", GError **error)";
+ + (has_return ? ", " + type_name (ttype) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arglist)))
+ + (has_xceptions ? "" : (", " + xception_list (xlist)))
+ + ", GError **error)";
- f_header_ << "gboolean " << this->nspace_lc << service_name_lc << "_if_" << funname << " "
- << params << ";" << endl;
+ f_header_ << "gboolean " << this->nspace_lc << service_name_lc <<
+ "_if_" << funname << " " << params << ";" << endl;
}
f_header_ << endl;
// Generate the client object instance definition in the header.
- f_header_ << "/* " << service_name_ << " service client */" << endl << "struct _" << this->nspace
- << service_name_ << "Client" << endl << "{" << endl << " " << parent_class_name
- << " parent;" << endl;
+ f_header_ <<
+ "/* " << service_name_ << " service client */" << endl <<
+ "struct _" << this->nspace << service_name_ << "Client" << endl <<
+ "{" << endl <<
+ " " << parent_class_name << " parent;" << endl;
if (!extends_service) {
// Define "input_protocol" and "output_protocol" properties only
// for base services; child service-client classes will inherit
// these
- f_header_ << endl << " ThriftProtocol *input_protocol;" << endl
- << " ThriftProtocol *output_protocol;" << endl;
+ f_header_ <<
+ endl <<
+ " ThriftProtocol *input_protocol;" << endl <<
+ " ThriftProtocol *output_protocol;" << endl;
}
- f_header_ << "};" << endl << "typedef struct _" << this->nspace << service_name_ << "Client "
- << this->nspace << service_name_ << "Client;" << endl << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << this->nspace << service_name_ << "Client " <<
+ this->nspace << service_name_ << "Client;" << endl <<
+ endl;
// Generate the class definition in the header.
- f_header_ << "struct _" << this->nspace << service_name_ << "ClientClass" << endl << "{" << endl
- << " " << parent_class_name << "Class parent;" << endl << "};" << endl
- << "typedef struct _" << this->nspace << service_name_ << "ClientClass " << this->nspace
- << service_name_ << "ClientClass;" << endl << endl;
+ f_header_ <<
+ "struct _" << this->nspace << service_name_ << "ClientClass" << endl <<
+ "{" << endl <<
+ " " << parent_class_name << "Class parent;" << endl <<
+ "};" << endl <<
+ "typedef struct _" << this->nspace << service_name_ << "ClientClass " <<
+ this->nspace << service_name_ << "ClientClass;" << endl <<
+ endl;
// Create all the GObject boilerplate
- f_header_ << "GType " << this->nspace_lc << service_name_lc << "_client_get_type (void);" << endl
- << "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT "
- << "(" << this->nspace_lc << service_name_lc << "_client_get_type())" << endl
- << "#define " << this->nspace_uc << service_name_uc << "_CLIENT(obj) "
- << "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_CLIENT, " << this->nspace << service_name_ << "Client))" << endl
- << "#define " << this->nspace_uc << service_name_uc << "_CLIENT_CLASS(c) "
- << "(G_TYPE_CHECK_CLASS_CAST ((c), " << this->nspace_uc << "TYPE_" << service_name_uc
- << "_CLIENT, " << this->nspace << service_name_ << "ClientClass))" << endl << "#define "
- << this->nspace_uc << service_name_uc << "_IS_CLIENT(obj) "
- << "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_CLIENT))" << endl << "#define " << this->nspace_uc
- << service_name_uc << "_IS_CLIENT_CLASS(c) "
- << "(G_TYPE_CHECK_CLASS_TYPE ((c), " << this->nspace_uc << "TYPE_" << service_name_uc
- << "_CLIENT))" << endl << "#define " << this->nspace_uc << service_name_uc
- << "_CLIENT_GET_CLASS(obj) "
- << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_CLIENT, " << this->nspace << service_name_ << "ClientClass))"
- << endl << endl;
+ f_header_ <<
+ "GType " << this->nspace_lc << service_name_lc <<
+ "_client_get_type (void);" << endl <<
+ "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT " <<
+ "(" << this->nspace_lc << service_name_lc << "_client_get_type())" <<
+ endl <<
+ "#define " << this->nspace_uc << service_name_uc << "_CLIENT(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT, " <<
+ this->nspace << service_name_ << "Client))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc << "_CLIENT_CLASS(c) " <<
+ "(G_TYPE_CHECK_CLASS_CAST ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT, " <<
+ this->nspace << service_name_ << "ClientClass))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc << "_IS_CLIENT(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc <<
+ "_IS_CLIENT_CLASS(c) " << "(G_TYPE_CHECK_CLASS_TYPE ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc <<
+ "_CLIENT_GET_CLASS(obj) " << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_CLIENT, " <<
+ this->nspace << service_name_ << "ClientClass))" << endl <<
+ endl;
/* write out the function prototypes */
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
/* make the function name C friendly */
string funname = to_lower_case(initial_caps_to_underscores((*f_iter)->get_name()));
- t_function service_function((*f_iter)->get_returntype(),
- service_name_lc + string("_client_") + funname,
- (*f_iter)->get_arglist(),
- (*f_iter)->get_xceptions());
- indent(f_header_) << function_signature(&service_function) << ";" << endl;
+ t_function service_function ((*f_iter)->get_returntype(),
+ service_name_lc + string ("_client_")
+ + funname,
+ (*f_iter)->get_arglist(),
+ (*f_iter)->get_xceptions());
+ indent(f_header_) << function_signature (&service_function) << ";" << endl;
- t_function send_function(g_type_void,
- service_name_lc + string("_client_send_") + funname,
- (*f_iter)->get_arglist());
- indent(f_header_) << function_signature(&send_function) << ";" << endl;
+ t_function send_function (g_type_void,
+ service_name_lc + string ("_client_send_")
+ + funname,
+ (*f_iter)->get_arglist());
+ indent(f_header_) << function_signature (&send_function) << ";" << endl;
// implement recv if not a oneway service
if (!(*f_iter)->is_oneway()) {
- t_struct noargs(program_);
- t_function recv_function((*f_iter)->get_returntype(),
- service_name_lc + string("_client_recv_") + funname,
- &noargs,
- (*f_iter)->get_xceptions());
- indent(f_header_) << function_signature(&recv_function) << ";" << endl;
+ t_struct noargs (program_);
+ t_function recv_function ((*f_iter)->get_returntype(),
+ service_name_lc + string ("_client_recv_")
+ + funname,
+ &noargs,
+ (*f_iter)->get_xceptions());
+ indent(f_header_) << function_signature (&recv_function) << ";" << endl;
}
}
/* write out the get/set function prototypes */
- f_header_ << "void " + service_name_lc + "_client_set_property (GObject *object, guint "
- "property_id, const GValue *value, GParamSpec *pspec);"
- << endl;
- f_header_ << "void " + service_name_lc + "_client_get_property (GObject *object, guint "
- "property_id, GValue *value, GParamSpec *pspec);"
- << endl;
+ f_header_ << "void " + service_name_lc + "_client_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);" << endl;
+ f_header_ << "void " + service_name_lc + "_client_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);" << endl;
f_header_ << endl;
// end of header code
@@ -1325,104 +1437,164 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
/* make the function name C friendly */
string funname = initial_caps_to_underscores((*f_iter)->get_name());
- t_type* ttype = (*f_iter)->get_returntype();
- t_struct* arglist = (*f_iter)->get_arglist();
- t_struct* xlist = (*f_iter)->get_xceptions();
+ t_type *ttype = (*f_iter)->get_returntype();
+ t_struct *arglist = (*f_iter)->get_arglist();
+ t_struct *xlist = (*f_iter)->get_xceptions();
bool has_return = !ttype->is_void();
bool has_args = arglist->get_members().size() == 0;
bool has_xceptions = xlist->get_members().size() == 0;
string params = "(" + this->nspace + service_name_ + "If *iface"
- + (has_return ? ", " + type_name(ttype) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arglist)))
- + (has_xceptions ? "" : (", " + xception_list(xlist))) + ", GError **error)";
+ + (has_return ? ", " + type_name (ttype) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arglist)))
+ + (has_xceptions ? "" : (", " + xception_list (xlist)))
+ + ", GError **error)";
- string params_without_type = string("iface, ") + (has_return ? "_return, " : "");
+ string params_without_type = string("iface, ")
+ + (has_return ? "_return, " : "");
- const vector<t_field*>& fields = arglist->get_members();
- vector<t_field*>::const_iterator f_iter_field;
+ const vector<t_field *>& fields = arglist->get_members();
+ vector<t_field *>::const_iterator f_iter_field;
for (f_iter_field = fields.begin(); f_iter_field != fields.end(); ++f_iter_field) {
params_without_type += (*f_iter_field)->get_name();
params_without_type += ", ";
}
- const vector<t_field*>& xceptions = xlist->get_members();
- vector<t_field*>::const_iterator x_iter;
+ const vector<t_field *>& xceptions = xlist->get_members();
+ vector<t_field *>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
params_without_type += (*x_iter)->get_name();
params_without_type += ", ";
}
- f_service_ << "gboolean" << endl << this->nspace_lc << service_name_lc << "_if_" << funname
- << " " << params << endl << "{" << endl << " return " << this->nspace_uc
- << service_name_uc << "_IF_GET_INTERFACE (iface)->" << funname << " ("
- << params_without_type << "error);" << endl << "}" << endl << endl;
+ f_service_ <<
+ "gboolean" << endl <<
+ this->nspace_lc << service_name_lc << "_if_" << funname << " " <<
+ params << endl <<
+ "{" << endl <<
+ " return " << this->nspace_uc << service_name_uc <<
+ "_IF_GET_INTERFACE (iface)->" << funname << " (" <<
+ params_without_type << "error);" << endl <<
+ "}" << endl <<
+ endl;
}
// Generate interface boilerplate
- f_service_ << "GType" << endl << this->nspace_lc << service_name_lc << "_if_get_type (void)"
- << endl << "{" << endl << " static GType type = 0;" << endl << " if (type == 0)"
- << endl << " {" << endl << " static const GTypeInfo type_info =" << endl << " {"
- << endl << " sizeof (" << this->nspace << service_name_ << "IfInterface)," << endl
- << " NULL, /* base_init */" << endl << " NULL, /* base_finalize */" << endl
- << " NULL, /* class_init */" << endl << " NULL, /* class_finalize */"
- << endl << " NULL, /* class_data */" << endl
- << " 0, /* instance_size */" << endl << " 0, /* n_preallocs */"
- << endl << " NULL, /* instance_init */" << endl
- << " NULL /* value_table */" << endl << " };" << endl
- << " type = g_type_register_static (G_TYPE_INTERFACE," << endl
- << " \"" << this->nspace << service_name_ << "If\","
- << endl << " &type_info, 0);" << endl << " }"
- << endl << " return type;" << endl << "}" << endl << endl;
+ f_service_ <<
+ "GType" << endl <<
+ this->nspace_lc << service_name_lc << "_if_get_type (void)" << endl <<
+ "{" << endl <<
+ " static GType type = 0;" << endl <<
+ " if (type == 0)" << endl <<
+ " {" << endl <<
+ " static const GTypeInfo type_info =" << endl <<
+ " {" << endl <<
+ " sizeof (" << this->nspace << service_name_ << "IfInterface)," <<
+ endl <<
+ " NULL, /* base_init */" << endl <<
+ " NULL, /* base_finalize */" << endl <<
+ " NULL, /* class_init */" << endl <<
+ " NULL, /* class_finalize */" << endl <<
+ " NULL, /* class_data */" << endl <<
+ " 0, /* instance_size */" << endl <<
+ " 0, /* n_preallocs */" << endl <<
+ " NULL, /* instance_init */" << endl <<
+ " NULL /* value_table */" << endl <<
+ " };" << endl <<
+ " type = g_type_register_static (G_TYPE_INTERFACE," << endl <<
+ " \"" << this->nspace << service_name_ <<
+ "If\"," << endl <<
+ " &type_info, 0);" << endl <<
+ " }" << endl <<
+ " return type;" << endl <<
+ "}" << endl <<
+ endl;
// Generate client boilerplate
- f_service_ << "static void " << endl << this->nspace_lc << service_name_lc
- << "_if_interface_init (" << this->nspace << service_name_ << "IfInterface *iface);"
- << endl << endl << "G_DEFINE_TYPE_WITH_CODE (" << this->nspace << service_name_
- << "Client, " << this->nspace_lc << service_name_lc << "_client," << endl
- << " " << parent_type_name << ", " << endl
- << " G_IMPLEMENT_INTERFACE (" << this->nspace_uc << "TYPE_"
- << service_name_uc << "_IF," << endl
- << " " << this->nspace_lc
- << service_name_lc << "_if_interface_init));" << endl << endl;
+ f_service_ <<
+ "static void " << endl <<
+ this->nspace_lc << service_name_lc <<
+ "_if_interface_init (" << this->nspace << service_name_ <<
+ "IfInterface *iface);" << endl <<
+ endl <<
+ "G_DEFINE_TYPE_WITH_CODE (" << this->nspace << service_name_ <<
+ "Client, " << this->nspace_lc << service_name_lc << "_client," << endl <<
+ " " << parent_type_name << ", " << endl <<
+ " G_IMPLEMENT_INTERFACE (" <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_IF," << endl <<
+ " " <<
+ this->nspace_lc << service_name_lc << "_if_interface_init));" << endl <<
+ endl;
// Generate property-related code only for base services---child
// service-client classes have only properties inherited from their
// parent class
if (!extends_service) {
// Generate client properties
- f_service_ << "enum _" << this->nspace << service_name_ << "ClientProperties" << endl << "{"
- << endl << " PROP_0," << endl << " PROP_" << this->nspace_uc << service_name_uc
- << "_CLIENT_INPUT_PROTOCOL," << endl << " PROP_" << this->nspace_uc
- << service_name_uc << "_CLIENT_OUTPUT_PROTOCOL" << endl << "};" << endl << endl;
+ f_service_ <<
+ "enum _" << this->nspace << service_name_ << "ClientProperties" << endl <<
+ "{" << endl <<
+ " PROP_0," << endl <<
+ " PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_INPUT_PROTOCOL," <<
+ endl <<
+ " PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_OUTPUT_PROTOCOL" <<
+ endl <<
+ "};" << endl <<
+ endl;
// generate property setter
- f_service_ << "void" << endl << this->nspace_lc << service_name_lc << "_client_set_property ("
- << "GObject *object, guint property_id, const GValue *value, "
- << "GParamSpec *pspec)" << endl << "{" << endl << " " << this->nspace
- << service_name_ << "Client *client = " << this->nspace_uc << service_name_uc
- << "_CLIENT (object);" << endl << endl << " THRIFT_UNUSED_VAR (pspec);" << endl
- << endl << " switch (property_id)" << endl << " {" << endl << " case PROP_"
- << this->nspace_uc << service_name_uc << "_CLIENT_INPUT_PROTOCOL:" << endl
- << " client->input_protocol = g_value_get_object (value);" << endl
- << " break;" << endl << " case PROP_" << this->nspace_uc << service_name_uc
- << "_CLIENT_OUTPUT_PROTOCOL:" << endl
- << " client->output_protocol = g_value_get_object (value);" << endl
- << " break;" << endl << " }" << endl << "}" << endl << endl;
+ f_service_ <<
+ "void" << endl <<
+ this->nspace_lc << service_name_lc << "_client_set_property (" <<
+ "GObject *object, guint property_id, const GValue *value, " <<
+ "GParamSpec *pspec)" << endl <<
+ "{" << endl <<
+ " " << this->nspace << service_name_ << "Client *client = " <<
+ this->nspace_uc << service_name_uc << "_CLIENT (object);" << endl <<
+ endl <<
+ " THRIFT_UNUSED_VAR (pspec);" << endl <<
+ endl <<
+ " switch (property_id)" << endl <<
+ " {" << endl <<
+ " case PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_INPUT_PROTOCOL:" << endl <<
+ " client->input_protocol = g_value_get_object (value);" << endl <<
+ " break;" << endl <<
+ " case PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_OUTPUT_PROTOCOL:" << endl <<
+ " client->output_protocol = g_value_get_object (value);" << endl <<
+ " break;" << endl <<
+ " }" << endl <<
+ "}" << endl <<
+ endl;
// generate property getter
- f_service_ << "void" << endl << this->nspace_lc << service_name_lc << "_client_get_property ("
- << "GObject *object, guint property_id, GValue *value, "
- << "GParamSpec *pspec)" << endl << "{" << endl << " " << this->nspace
- << service_name_ << "Client *client = " << this->nspace_uc << service_name_uc
- << "_CLIENT (object);" << endl << endl << " THRIFT_UNUSED_VAR (pspec);" << endl
- << endl << " switch (property_id)" << endl << " {" << endl << " case PROP_"
- << this->nspace_uc << service_name_uc << "_CLIENT_INPUT_PROTOCOL:" << endl
- << " g_value_set_object (value, client->input_protocol);" << endl
- << " break;" << endl << " case PROP_" << this->nspace_uc << service_name_uc
- << "_CLIENT_OUTPUT_PROTOCOL:" << endl
- << " g_value_set_object (value, client->output_protocol);" << endl
- << " break;" << endl << " }" << endl << "}" << endl << endl;
+ f_service_ <<
+ "void" << endl <<
+ this->nspace_lc << service_name_lc << "_client_get_property (" <<
+ "GObject *object, guint property_id, GValue *value, " <<
+ "GParamSpec *pspec)" << endl <<
+ "{" << endl <<
+ " " << this->nspace << service_name_ << "Client *client = " <<
+ this->nspace_uc << service_name_uc << "_CLIENT (object);" << endl <<
+ endl <<
+ " THRIFT_UNUSED_VAR (pspec);" << endl <<
+ endl <<
+ " switch (property_id)" << endl <<
+ " {" << endl <<
+ " case PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_INPUT_PROTOCOL:" << endl <<
+ " g_value_set_object (value, client->input_protocol);" << endl <<
+ " break;" << endl <<
+ " case PROP_" << this->nspace_uc << service_name_uc <<
+ "_CLIENT_OUTPUT_PROTOCOL:" << endl <<
+ " g_value_set_object (value, client->output_protocol);" << endl <<
+ " break;" << endl <<
+ " }" << endl <<
+ "}" << endl <<
+ endl;
}
// Generate client method implementations
@@ -1434,92 +1606,117 @@
t_struct* arg_struct = (*f_iter)->get_arglist();
// Function for sending
- t_function send_function(g_type_void,
- service_name_lc + string("_client_send_") + funname,
- (*f_iter)->get_arglist());
+ t_function send_function (g_type_void,
+ service_name_lc + string ("_client_send_")
+ + funname,
+ (*f_iter)->get_arglist());
// Open the send function
- indent(f_service_) << function_signature(&send_function) << endl;
+ indent(f_service_) <<
+ function_signature (&send_function) << endl;
scope_up(f_service_);
string reqType = (*f_iter)->is_oneway() ? "T_ONEWAY" : "T_CALL";
// Serialize the request
- f_service_ << indent() << "gint32 cseqid = 0;" << endl << indent()
- << "ThriftProtocol * protocol = " << this->nspace_uc << base_service_name_uc
- << "_CLIENT (iface)->output_protocol;" << endl << endl << indent()
- << "if (thrift_protocol_write_message_begin (protocol, \"" << name << "\", "
- << reqType << ", cseqid, error) < 0)" << endl << indent() << " return FALSE;"
- << endl << endl;
+ f_service_ <<
+ indent() << "gint32 cseqid = 0;" << endl <<
+ indent() << "ThriftProtocol * protocol = " <<
+ this->nspace_uc << base_service_name_uc <<
+ "_CLIENT (iface)->output_protocol;" << endl <<
+ endl <<
+ indent() << "if (thrift_protocol_write_message_begin (protocol, \""
+ << name << "\", " << reqType << ", cseqid, error) < 0)" << endl <<
+ indent() << " return FALSE;" << endl <<
+ endl;
- generate_struct_writer(f_service_, arg_struct, "", "", false);
+ generate_struct_writer (f_service_, arg_struct, "", "", false);
- f_service_ << indent() << "if (thrift_protocol_write_message_end (protocol, error) < 0)" << endl
- << indent() << " return FALSE;" << endl << indent()
- << "if (!thrift_transport_flush (protocol->transport, error))" << endl << indent()
- << " return FALSE;" << endl << indent()
- << "if (!thrift_transport_write_end (protocol->transport, error))" << endl
- << indent() << " return FALSE;" << endl << endl << indent() << "return TRUE;"
- << endl;
+ f_service_ <<
+ indent() <<
+ "if (thrift_protocol_write_message_end (protocol, error) < 0)" <<
+ endl <<
+ indent() <<
+ " return FALSE;" << endl <<
+ indent() <<
+ "if (!thrift_transport_flush (protocol->transport, error))" << endl <<
+ indent() <<
+ " return FALSE;" << endl <<
+ indent() <<
+ "if (!thrift_transport_write_end (protocol->transport, error))" <<
+ endl <<
+ indent() <<
+ " return FALSE;" << endl <<
+ endl <<
+ indent() <<
+ "return TRUE;" << endl;
scope_down(f_service_);
f_service_ << endl;
// Generate recv function only if not an async function
if (!(*f_iter)->is_oneway()) {
- t_struct noargs(program_);
- t_function recv_function((*f_iter)->get_returntype(),
- service_name_lc + string("_client_recv_") + funname,
- &noargs,
- (*f_iter)->get_xceptions());
+ t_struct noargs (program_);
+ t_function recv_function ((*f_iter)->get_returntype(),
+ service_name_lc
+ + string ("_client_recv_") + funname, &noargs,
+ (*f_iter)->get_xceptions());
// Open function
- indent(f_service_) << function_signature(&recv_function) << endl;
+ indent(f_service_) <<
+ function_signature (&recv_function) << endl;
scope_up(f_service_);
- f_service_ << endl << indent() << "gint32 rseqid;" << endl << indent()
- << "gchar * fname = NULL;" << endl << indent() << "ThriftMessageType mtype;"
- << endl << indent() << "ThriftProtocol * protocol = " << this->nspace_uc
- << base_service_name_uc << "_CLIENT (iface)->input_protocol;" << endl << endl
- << indent() << "if (thrift_protocol_read_message_begin "
- << "(protocol, &fname, &mtype, &rseqid, error) < 0)" << endl << indent() << "{"
- << endl << indent() << " if (fname) g_free (fname);" << endl << indent()
- << " return FALSE;" << endl << indent() << "}" << endl << endl << indent()
- << "if (mtype == T_EXCEPTION) {" << endl << indent()
- << " if (fname) g_free (fname);" << endl << indent()
- << " ThriftApplicationException *xception = g_object_new "
- "(THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);" << endl << indent()
- << " thrift_struct_read (THRIFT_STRUCT (xception), protocol, NULL);" << endl
- << indent() << " thrift_protocol_read_message_end (protocol, NULL);" << endl
- << indent() << " thrift_transport_read_end (protocol->transport, NULL);" << endl
- << indent() << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, "
- "xception->type, \"application error: %s\", xception->message);"
- << endl << indent() << " g_object_unref (xception);" << endl << indent()
- << " return FALSE;" << endl << indent() << "} else if (mtype != T_REPLY) {"
- << endl << indent() << " if (fname) g_free (fname);" << endl << indent()
- << " thrift_protocol_skip (protocol, T_STRUCT, NULL);" << endl << indent()
- << " thrift_protocol_read_message_end (protocol, NULL);" << endl << indent()
- << " thrift_transport_read_end (protocol->transport, NULL);" << endl << indent()
- << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, "
- "THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_MESSAGE_TYPE, \"invalid message "
- "type %d, expected T_REPLY\", mtype);" << endl << indent() << " return FALSE;"
- << endl << indent() << "} else if (strncmp (fname, \"" << name << "\", "
- << name.length() << ") != 0) {" << endl << indent()
- << " thrift_protocol_skip (protocol, T_STRUCT, NULL);" << endl << indent()
- << " thrift_protocol_read_message_end (protocol, error);" << endl << indent()
- << " thrift_transport_read_end (protocol->transport, error);" << endl << indent()
- << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, "
- "THRIFT_APPLICATION_EXCEPTION_ERROR_WRONG_METHOD_NAME, \"wrong method name %s, "
- "expected " << name << "\", fname);" << endl << indent()
- << " if (fname) g_free (fname);" << endl << indent() << " return FALSE;" << endl
- << indent() << "}" << endl << indent() << "if (fname) g_free (fname);" << endl
- << endl;
+ f_service_ << endl <<
+ indent() << "gint32 rseqid;" << endl <<
+ indent() << "gchar * fname = NULL;" << endl <<
+ indent() << "ThriftMessageType mtype;" << endl <<
+ indent() << "ThriftProtocol * protocol = " <<
+ this->nspace_uc << base_service_name_uc <<
+ "_CLIENT (iface)->input_protocol;" << endl <<
+ endl <<
+ indent() << "if (thrift_protocol_read_message_begin " <<
+ "(protocol, &fname, &mtype, &rseqid, error) < 0)" <<
+ endl <<
+ indent() << "{" << endl <<
+ indent() << " if (fname) g_free (fname);" << endl <<
+ indent() << " return FALSE;" << endl <<
+ indent() << "}" << endl <<
+ endl <<
+ indent() << "if (mtype == T_EXCEPTION) {" << endl <<
+ indent() << " if (fname) g_free (fname);" << endl <<
+ indent() << " ThriftApplicationException *xception = g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION, NULL);" << endl <<
+
+ indent() << " thrift_struct_read (THRIFT_STRUCT (xception), protocol, NULL);" << endl <<
+ indent() << " thrift_protocol_read_message_end (protocol, NULL);" << endl <<
+ indent() << " thrift_transport_read_end (protocol->transport, NULL);" << endl <<
+ indent() << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, xception->type, \"application error: %s\", xception->message);" << endl <<
+ indent() << " g_object_unref (xception);" << endl <<
+ indent() << " return FALSE;" << endl <<
+ indent() << "} else if (mtype != T_REPLY) {" << endl <<
+ indent() << " if (fname) g_free (fname);" << endl <<
+ indent() << " thrift_protocol_skip (protocol, T_STRUCT, NULL);" << endl <<
+ indent() << " thrift_protocol_read_message_end (protocol, NULL);" << endl <<
+ indent() << " thrift_transport_read_end (protocol->transport, NULL);" << endl <<
+ indent() << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_MESSAGE_TYPE, \"invalid message type %d, expected T_REPLY\", mtype);" << endl <<
+ indent() << " return FALSE;" << endl <<
+ indent() << "} else if (strncmp (fname, \"" << name << "\", " << name.length() << ") != 0) {" << endl <<
+ indent() << " thrift_protocol_skip (protocol, T_STRUCT, NULL);" << endl <<
+ indent() << " thrift_protocol_read_message_end (protocol, error);" << endl <<
+ indent() << " thrift_transport_read_end (protocol->transport, error);" << endl <<
+ indent() << " g_set_error (error, THRIFT_APPLICATION_EXCEPTION_ERROR, THRIFT_APPLICATION_EXCEPTION_ERROR_WRONG_METHOD_NAME, \"wrong method name %s, expected " << name << "\", fname);" << endl <<
+ indent() << " if (fname) g_free (fname);" << endl <<
+ indent() << " return FALSE;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (fname) g_free (fname);" << endl <<
+ endl;
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
{
- t_struct result(program_, tservice->get_name() + "_" + (*f_iter)->get_name() + "_result");
+ t_struct result(program_, tservice->get_name() + "_" +
+ (*f_iter)->get_name() + "_result");
t_field success((*f_iter)->get_returntype(), "*_return", 0);
if (!(*f_iter)->get_returntype()->is_void()) {
result.append(&success);
@@ -1527,30 +1724,39 @@
// add readers for exceptions, dereferencing the pointer.
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); x_iter++) {
- t_field* xception = new t_field((*x_iter)->get_type(),
+ t_field *xception = new t_field((*x_iter)->get_type(),
"*" + (*x_iter)->get_name(),
(*x_iter)->get_key());
- result.append(xception);
+ result.append (xception);
}
- generate_struct_reader(f_service_, &result, "", "", false);
+ generate_struct_reader (f_service_, &result, "", "", false);
}
- f_service_ << indent() << "if (thrift_protocol_read_message_end (protocol, error) < 0)"
- << endl << indent() << " return FALSE;" << endl << endl << indent()
- << "if (!thrift_transport_read_end (protocol->transport, error))" << endl
- << indent() << " return FALSE;" << endl << endl;
+ f_service_ <<
+ indent() << "if (thrift_protocol_read_message_end (protocol, error) < 0)" << endl <<
+ indent() << " return FALSE;" << endl <<
+ endl <<
+ indent() << "if (!thrift_transport_read_end (protocol->transport, error))" << endl <<
+ indent() << " return FALSE;" << endl <<
+ endl;
// copy over any throw exceptions and return failure
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); x_iter++) {
- f_service_ << indent() << "if (*" << (*x_iter)->get_name() << " != NULL)" << endl
- << indent() << "{" << endl << indent() << " g_set_error (error, "
- << this->nspace_uc
- << to_upper_case(initial_caps_to_underscores((*x_iter)->get_type()->get_name()))
- << "_ERROR, " << this->nspace_uc
- << to_upper_case(initial_caps_to_underscores((*x_iter)->get_type()->get_name()))
- << "_ERROR_CODE, \"" << (*x_iter)->get_type()->get_name() << "\");" << endl
- << indent() << " return FALSE;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (*" << (*x_iter)->get_name() << " != NULL)" << endl <<
+ indent() << "{" << endl <<
+ indent() << " g_set_error (error, " << this->nspace_uc <<
+ to_upper_case(initial_caps_to_underscores(
+ (*x_iter)->get_type()->get_name())) <<
+ "_ERROR, " <<
+ this->nspace_uc <<
+ to_upper_case(initial_caps_to_underscores(
+ (*x_iter)->get_type()->get_name())) <<
+ "_ERROR_CODE, \"" << (*x_iter)->get_type()->get_name() <<
+ "\");" << endl <<
+ indent() << " return FALSE;" << endl <<
+ indent() << "}" << endl;
}
// Close function
indent(f_service_) << "return TRUE;" << endl;
@@ -1560,38 +1766,46 @@
// Open function
t_function service_function((*f_iter)->get_returntype(),
- service_name_lc + string("_client_") + funname,
- (*f_iter)->get_arglist(),
- (*f_iter)->get_xceptions());
- indent(f_service_) << function_signature(&service_function) << endl;
+ service_name_lc
+ + string ("_client_") + funname,
+ (*f_iter)->get_arglist(),
+ (*f_iter)->get_xceptions());
+ indent(f_service_) <<
+ function_signature (&service_function) << endl;
scope_up(f_service_);
// wrap each function
- f_service_ << indent() << "if (!" << this->nspace_lc << service_name_lc << "_client_send_"
- << funname << " (iface";
+ f_service_ <<
+ indent() << "if (!" << this->nspace_lc << service_name_lc <<
+ "_client_send_" << funname <<
+ " (iface";
// Declare the function arguments
- const vector<t_field*>& fields = arg_struct->get_members();
- vector<t_field*>::const_iterator fld_iter;
+ const vector<t_field *> &fields = arg_struct->get_members();
+ vector<t_field *>::const_iterator fld_iter;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
f_service_ << ", " << (*fld_iter)->get_name();
}
- f_service_ << ", error))" << endl << indent() << " return FALSE;" << endl;
+ f_service_ << ", error))" << endl <<
+ indent() << " return FALSE;" << endl;
// if not oneway, implement recv
if (!(*f_iter)->is_oneway()) {
string ret = (*f_iter)->get_returntype()->is_void() ? "" : "_return, ";
- const vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members();
- vector<t_field*>::const_iterator x_iter;
+ const vector<t_field *>& xceptions =
+ (*f_iter)->get_xceptions()->get_members();
+ vector<t_field *>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
ret += (*x_iter)->get_name();
ret += ", ";
}
- f_service_ << indent() << "if (!" << this->nspace_lc << service_name_lc << "_client_recv_"
- << funname << " (iface, " << ret << "error))" << endl << indent()
- << " return FALSE;" << endl;
+ f_service_ <<
+ indent() << "if (!" << this->nspace_lc << service_name_lc <<
+ "_client_recv_" << funname <<
+ " (iface, " << ret << "error))" << endl <<
+ indent() << " return FALSE;" << endl;
}
// return TRUE which means all functions were called OK
@@ -1601,56 +1815,79 @@
}
// create the interface initializer
- f_service_ << "static void" << endl << this->nspace_lc << service_name_lc
- << "_if_interface_init (" << this->nspace << service_name_ << "IfInterface *iface)"
- << endl << "{" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ this->nspace_lc << service_name_lc << "_if_interface_init (" <<
+ this->nspace << service_name_ << "IfInterface *iface)" << endl <<
+ "{" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
/* make the function name C friendly */
string funname = initial_caps_to_underscores((*f_iter)->get_name());
- f_service_ << " iface->" << funname << " = " << this->nspace_lc << service_name_lc
- << "_client_" << funname << ";" << endl;
+ f_service_ <<
+ " iface->" << funname << " = " << this->nspace_lc << service_name_lc <<
+ "_client_" << funname << ";" << endl;
}
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl <<
+ endl;
// create the client instance initializer
- f_service_ << "static void" << endl << this->nspace_lc << service_name_lc << "_client_init ("
- << this->nspace << service_name_ << "Client *client)" << endl << "{" << endl;
- if (!extends_service) {
- f_service_ << " client->input_protocol = NULL;" << endl << " client->output_protocol = NULL;"
- << endl;
+ f_service_ <<
+ "static void" << endl <<
+ this->nspace_lc << service_name_lc << "_client_init (" <<
+ this->nspace << service_name_ << "Client *client)" << endl <<
+ "{" << endl;
+ if(!extends_service) {
+ f_service_ <<
+ " client->input_protocol = NULL;" << endl <<
+ " client->output_protocol = NULL;" << endl;
}
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl <<
+ endl;
// create the client class initializer
- f_service_ << "static void" << endl << this->nspace_lc << service_name_lc
- << "_client_class_init (" << this->nspace << service_name_ << "ClientClass *cls)"
- << endl << "{" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ this->nspace_lc << service_name_lc << "_client_class_init (" <<
+ this->nspace << service_name_ << "ClientClass *cls)" << endl <<
+ "{" << endl;
if (!extends_service) {
- f_service_ << " GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl
- << " GParamSpec *param_spec;" << endl << endl
- << " gobject_class->set_property = " << this->nspace_lc << service_name_lc
- << "_client_set_property;" << endl
- << " gobject_class->get_property = " << this->nspace_lc << service_name_lc
- << "_client_get_property;" << endl << endl
- << " param_spec = g_param_spec_object (\"input_protocol\"," << endl
- << " \"input protocol (construct)\"," << endl
- << " \"Set the client input protocol\"," << endl
- << " THRIFT_TYPE_PROTOCOL," << endl
- << " G_PARAM_READWRITE);" << endl
- << " g_object_class_install_property (gobject_class," << endl
- << " PROP_" << this->nspace_uc << service_name_uc
- << "_CLIENT_INPUT_PROTOCOL, param_spec);" << endl << endl
- << " param_spec = g_param_spec_object (\"output_protocol\"," << endl
- << " \"output protocol (construct)\"," << endl
- << " \"Set the client output protocol\"," << endl
- << " THRIFT_TYPE_PROTOCOL," << endl
- << " G_PARAM_READWRITE);" << endl
- << " g_object_class_install_property (gobject_class," << endl
- << " PROP_" << this->nspace_uc << service_name_uc
- << "_CLIENT_OUTPUT_PROTOCOL, param_spec);" << endl;
+ f_service_ <<
+ " GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl <<
+ " GParamSpec *param_spec;" << endl <<
+ endl <<
+ " gobject_class->set_property = " << this->nspace_lc <<
+ service_name_lc << "_client_set_property;" << endl <<
+ " gobject_class->get_property = " << this->nspace_lc <<
+ service_name_lc << "_client_get_property;" << endl <<
+ endl <<
+ " param_spec = g_param_spec_object (\"input_protocol\"," << endl <<
+ " \"input protocol (construct)\"," <<
+ endl <<
+ " \"Set the client input protocol\"," <<
+ endl <<
+ " THRIFT_TYPE_PROTOCOL," << endl <<
+ " G_PARAM_READWRITE);" << endl <<
+ " g_object_class_install_property (gobject_class," << endl <<
+ " PROP_" << this->nspace_uc <<
+ service_name_uc << "_CLIENT_INPUT_PROTOCOL, param_spec);" << endl <<
+ endl <<
+ " param_spec = g_param_spec_object (\"output_protocol\"," << endl <<
+ " \"output protocol (construct)\"," <<
+ endl <<
+ " \"Set the client output protocol\"," <<
+ endl <<
+ " THRIFT_TYPE_PROTOCOL," << endl <<
+ " G_PARAM_READWRITE);" << endl <<
+ " g_object_class_install_property (gobject_class," << endl <<
+ " PROP_" << this->nspace_uc <<
+ service_name_uc << "_CLIENT_OUTPUT_PROTOCOL, param_spec);" << endl;
}
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl <<
+ endl;
}
/**
@@ -1658,15 +1895,17 @@
*
* @param tservice The service for which to generate a handler.
*/
-void t_c_glib_generator::generate_service_handler(t_service* tservice) {
+void t_c_glib_generator::generate_service_handler(t_service *tservice) {
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::const_iterator function_iter;
- string service_name_lc = to_lower_case(initial_caps_to_underscores(service_name_));
+ string service_name_lc =
+ to_lower_case(initial_caps_to_underscores(service_name_));
string service_name_uc = to_upper_case(service_name_lc);
string class_name = this->nspace + service_name_ + "Handler";
- string class_name_lc = to_lower_case(initial_caps_to_underscores(class_name));
+ string class_name_lc =
+ to_lower_case(initial_caps_to_underscores(class_name));
string class_name_uc = to_upper_case(class_name_lc);
string parent_class_name;
@@ -1675,18 +1914,22 @@
string args_indent;
// The service this service extends, or NULL if it extends no service
- t_service* extends_service = tservice->get_extends();
+ t_service *extends_service = tservice->get_extends();
// Determine the name of our parent service (if any) and the handler class'
// parent class name and type
if (extends_service) {
string parent_service_name = extends_service->get_name();
- string parent_service_name_lc = to_lower_case(initial_caps_to_underscores(parent_service_name));
+ string parent_service_name_lc =
+ to_lower_case(initial_caps_to_underscores(parent_service_name));
string parent_service_name_uc = to_upper_case(parent_service_name_lc);
- parent_class_name = this->nspace + parent_service_name + "Handler";
- parent_type_name = this->nspace_uc + "TYPE_" + parent_service_name_uc + "_HANDLER";
- } else {
+ parent_class_name =
+ this->nspace + parent_service_name + "Handler";
+ parent_type_name =
+ this->nspace_uc + "TYPE_" + parent_service_name_uc + "_HANDLER";
+ }
+ else {
parent_class_name = "GObject";
parent_type_name = "G_TYPE_OBJECT";
}
@@ -1694,128 +1937,170 @@
// Generate the handler class' definition in the header file
// Generate the handler instance definition
- f_header_ << "/* " << service_name_ << " handler (abstract base class) */" << endl << "struct _"
- << class_name << endl << "{" << endl;
+ f_header_ <<
+ "/* " << service_name_ << " handler (abstract base class) */" << endl <<
+ "struct _" << class_name << endl <<
+ "{" << endl;
indent_up();
- f_header_ << indent() << parent_class_name << " parent;" << endl;
+ f_header_ <<
+ indent() << parent_class_name << " parent;" << endl;
indent_down();
- f_header_ << "};" << endl << "typedef struct _" << class_name << " " << class_name << ";" << endl
- << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << class_name << " " << class_name << ";" << endl <<
+ endl;
// Generate the handler class definition, including its class members
// (methods)
- f_header_ << "struct _" << class_name << "Class" << endl << "{" << endl;
+ f_header_ <<
+ "struct _" << class_name << "Class" << endl <<
+ "{" << endl;
indent_up();
- f_header_ << indent() << parent_class_name << "Class parent;" << endl << endl;
+ f_header_ <<
+ indent() << parent_class_name << "Class parent;" << endl <<
+ endl;
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
- string method_name = initial_caps_to_underscores((*function_iter)->get_name());
- t_type* return_type = (*function_iter)->get_returntype();
- t_struct* arg_list = (*function_iter)->get_arglist();
- t_struct* x_list = (*function_iter)->get_xceptions();
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
+ string method_name =
+ initial_caps_to_underscores((*function_iter)->get_name());
+ t_type *return_type = (*function_iter)->get_returntype();
+ t_struct *arg_list = (*function_iter)->get_arglist();
+ t_struct *x_list = (*function_iter)->get_xceptions();
bool has_return = !return_type->is_void();
bool has_args = arg_list->get_members().size() == 0;
bool has_xceptions = x_list->get_members().size() == 0;
string params = "(" + this->nspace + service_name_ + "If *iface"
- + (has_return ? ", " + type_name(return_type) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arg_list)))
- + (has_xceptions ? "" : (", " + xception_list(x_list))) + ", GError **error)";
+ + (has_return ? ", " + type_name (return_type) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arg_list)))
+ + (has_xceptions ? "" : (", " + xception_list (x_list)))
+ + ", GError **error)";
- indent(f_header_) << "gboolean (*" << method_name << ") " << params << ";" << endl;
+ indent(f_header_) << "gboolean (*" << method_name << ") " << params << ";" <<
+ endl;
}
indent_down();
- f_header_ << "};" << endl << "typedef struct _" << class_name << "Class " << class_name
- << "Class;" << endl << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << class_name << "Class " << class_name << "Class;" << endl <<
+ endl;
// Generate the remaining header boilerplate
- f_header_ << "GType " << class_name_lc << "_get_type (void);" << endl << "#define "
- << this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER "
- << "(" << class_name_lc << "_get_type())" << endl << "#define " << class_name_uc
- << "(obj) "
- << "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_HANDLER, " << class_name << "))" << endl << "#define "
- << this->nspace_uc << "IS_" << service_name_uc << "_HANDLER(obj) "
- << "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_HANDLER))" << endl << "#define " << class_name_uc
- << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_HANDLER, " << class_name << "Class))" << endl << "#define "
- << this->nspace_uc << "IS_" << service_name_uc << "_HANDLER_CLASS(c) "
- << "(G_TYPE_CHECK_CLASS_TYPE ((c), " << this->nspace_uc << "TYPE_" << service_name_uc
- << "_HANDLER))" << endl << "#define " << this->nspace_uc << service_name_uc
- << "_HANDLER_GET_CLASS(obj) "
- << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_HANDLER, " << class_name << "Class))" << endl << endl;
+ f_header_ <<
+ "GType " << class_name_lc << "_get_type (void);" << endl <<
+ "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER " <<
+ "(" << class_name_lc << "_get_type())" << endl <<
+ "#define " << class_name_uc << "(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER, " <<
+ class_name << "))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << service_name_uc << "_HANDLER(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER))" << endl <<
+ "#define " << class_name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER, " <<
+ class_name << "Class))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << service_name_uc <<
+ "_HANDLER_CLASS(c) " << "(G_TYPE_CHECK_CLASS_TYPE ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc <<
+ "_HANDLER_GET_CLASS(obj) " << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_HANDLER, " <<
+ class_name << "Class))" << endl <<
+ endl;
// Generate the handler class' method definitions
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
- string method_name = initial_caps_to_underscores((*function_iter)->get_name());
- t_type* return_type = (*function_iter)->get_returntype();
- t_struct* arg_list = (*function_iter)->get_arglist();
- t_struct* x_list = (*function_iter)->get_xceptions();
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
+ string method_name =
+ initial_caps_to_underscores((*function_iter)->get_name());
+ t_type *return_type = (*function_iter)->get_returntype();
+ t_struct *arg_list = (*function_iter)->get_arglist();
+ t_struct *x_list = (*function_iter)->get_xceptions();
bool has_return = !return_type->is_void();
bool has_args = arg_list->get_members().size() == 0;
bool has_xceptions = x_list->get_members().size() == 0;
string params = "(" + this->nspace + service_name_ + "If *iface"
- + (has_return ? ", " + type_name(return_type) + "* _return" : "")
- + (has_args ? "" : (", " + argument_list(arg_list)))
- + (has_xceptions ? "" : (", " + xception_list(x_list))) + ", GError **error)";
+ + (has_return ? ", " + type_name (return_type) + "* _return" : "")
+ + (has_args ? "" : (", " + argument_list (arg_list)))
+ + (has_xceptions ? "" : (", " + xception_list (x_list)))
+ + ", GError **error)";
- f_header_ << "gboolean " << class_name_lc << "_" << method_name << " " << params << ";" << endl;
+ f_header_ << "gboolean " << class_name_lc << "_" << method_name << " " <<
+ params << ";" << endl;
}
f_header_ << endl;
// Generate the handler's implementation in the implementation file
// Generate the implementation boilerplate
- f_service_ << "static void" << endl << class_name_lc << "_" << service_name_lc
- << "_if_interface_init (" << this->nspace << service_name_ << "IfInterface *iface);"
- << endl << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_" << service_name_lc << "_if_interface_init (" <<
+ this->nspace << service_name_ << "IfInterface *iface);" << endl <<
+ endl;
- args_indent = string(25, ' ');
- f_service_ << "G_DEFINE_TYPE_WITH_CODE (" << class_name << ", " << endl << args_indent
- << class_name_lc << "," << endl << args_indent << parent_type_name << "," << endl
- << args_indent << "G_IMPLEMENT_INTERFACE (" << this->nspace_uc << "TYPE_"
- << service_name_uc << "_IF," << endl;
- args_indent += string(23, ' ');
- f_service_ << args_indent << class_name_lc << "_" << service_name_lc << "_if_interface_init));"
- << endl << endl;
+ args_indent = string (25, ' ');
+ f_service_ <<
+ "G_DEFINE_TYPE_WITH_CODE (" << class_name << ", " << endl <<
+ args_indent << class_name_lc << "," << endl <<
+ args_indent << parent_type_name << "," << endl <<
+ args_indent << "G_IMPLEMENT_INTERFACE (" <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_IF," << endl;
+ args_indent += string (23, ' ');
+ f_service_ <<
+ args_indent << class_name_lc << "_" << service_name_lc <<
+ "_if_interface_init));" << endl <<
+ endl;
// Generate the handler method implementations
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
string function_name = (*function_iter)->get_name();
string method_name = initial_caps_to_underscores(function_name);
- t_type* return_type = (*function_iter)->get_returntype();
- t_struct* arg_list = (*function_iter)->get_arglist();
- t_struct* x_list = (*function_iter)->get_xceptions();
+ t_type *return_type = (*function_iter)->get_returntype();
+ t_struct *arg_list = (*function_iter)->get_arglist();
+ t_struct *x_list = (*function_iter)->get_xceptions();
const vector<t_field*>& args = arg_list->get_members();
const vector<t_field*>& xceptions = x_list->get_members();
vector<t_field*>::const_iterator field_iter;
- t_function implementing_function(return_type,
- service_name_lc + "_handler_" + method_name,
- arg_list,
- x_list,
- (*function_iter)->is_oneway());
+ t_function
+ implementing_function (return_type,
+ service_name_lc + "_handler_" + method_name,
+ arg_list,
+ x_list,
+ (*function_iter)->is_oneway());
- indent(f_service_) << function_signature(&implementing_function) << endl;
+ indent(f_service_) <<
+ function_signature (&implementing_function) << endl;
scope_up(f_service_);
- f_service_ << indent() << "g_return_if_fail (" << this->nspace_uc << "IS_" << service_name_uc
- << "_HANDLER (iface));" << endl << endl << indent() << class_name_uc
- << "_GET_CLASS (iface)"
- << "->" << method_name << " (iface, ";
+ f_service_ <<
+ indent() << "g_return_if_fail (" <<
+ this->nspace_uc << "IS_" << service_name_uc << "_HANDLER (iface));" << endl <<
+ endl <<
+ indent() << class_name_uc << "_GET_CLASS (iface)" << "->" << method_name <<
+ " (iface, ";
- if (!return_type->is_void()) {
+ if(!return_type->is_void()) {
f_service_ << "_return, ";
}
- for (field_iter = args.begin(); field_iter != args.end(); ++field_iter) {
+ for (field_iter = args.begin();
+ field_iter != args.end();
+ ++field_iter) {
f_service_ << (*field_iter)->get_name() << ", ";
}
- for (field_iter = xceptions.begin(); field_iter != xceptions.end(); ++field_iter) {
+ for (field_iter = xceptions.begin();
+ field_iter != xceptions.end();
+ ++field_iter) {
f_service_ << (*field_iter)->get_name() << ", ";
}
f_service_ << "error);" << endl;
@@ -1824,37 +2109,48 @@
}
// Generate the handler interface initializer
- f_service_ << "static void" << endl << class_name_lc << "_" << service_name_lc
- << "_if_interface_init (" << this->nspace << service_name_ << "IfInterface *iface)"
- << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_" << service_name_lc << "_if_interface_init (" <<
+ this->nspace << service_name_ << "IfInterface *iface)" << endl;
scope_up(f_service_);
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
- string method_name = initial_caps_to_underscores((*function_iter)->get_name());
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
+ string method_name =
+ initial_caps_to_underscores((*function_iter)->get_name());
- f_service_ << indent() << "iface->" << method_name << " = " << class_name_lc << "_"
- << method_name << ";" << endl;
+ f_service_ <<
+ indent() << "iface->" << method_name << " = " <<
+ class_name_lc << "_" << method_name << ";" << endl;
}
scope_down(f_service_);
f_service_ << endl;
// Generate the handler instance initializer
- f_service_ << "static void" << endl << class_name_lc << "_init (" << class_name << " *self)"
- << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_init (" << class_name << " *self)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "THRIFT_UNUSED_VAR (self);" << endl;
+ f_service_ <<
+ indent() << "THRIFT_UNUSED_VAR (self);" << endl;
scope_down(f_service_);
f_service_ << endl;
// Generate the handler class initializer
- f_service_ << "static void" << endl << class_name_lc << "_class_init (" << class_name
- << "Class *cls)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_class_init (" << class_name << "Class *cls)" << endl;
scope_up(f_service_);
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
string function_name = (*function_iter)->get_name();
string method_name = initial_caps_to_underscores(function_name);
// All methods are pure virtual and must be implemented by subclasses
- f_service_ << indent() << "cls->" << method_name << " = NULL;" << endl;
+ f_service_ <<
+ indent() << "cls->" << method_name << " = NULL;" << endl;
}
scope_down(f_service_);
f_service_ << endl;
@@ -1865,39 +2161,47 @@
*
* @param tservice The service for which to generate a processor
*/
-void t_c_glib_generator::generate_service_processor(t_service* tservice) {
+void t_c_glib_generator::generate_service_processor(t_service *tservice) {
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::const_iterator function_iter;
- string service_name_lc = to_lower_case(initial_caps_to_underscores(service_name_));
+ string service_name_lc =
+ to_lower_case(initial_caps_to_underscores(service_name_));
string service_name_uc = to_upper_case(service_name_lc);
string class_name = this->nspace + service_name_ + "Processor";
- string class_name_lc = to_lower_case(initial_caps_to_underscores(class_name));
+ string class_name_lc =
+ to_lower_case(initial_caps_to_underscores(class_name));
string class_name_uc = to_upper_case(class_name_lc);
string parent_class_name;
string parent_type_name;
- string handler_class_name = this->nspace + service_name_ + "Handler";
- string handler_class_name_lc = initial_caps_to_underscores(handler_class_name);
+ string handler_class_name =
+ this->nspace + service_name_ + "Handler";
+ string handler_class_name_lc =
+ initial_caps_to_underscores(handler_class_name);
string function_name;
string args_indent;
// The service this service extends, or NULL if it extends no service
- t_service* extends_service = tservice->get_extends();
+ t_service *extends_service = tservice->get_extends();
// Determine the name of our parent service (if any) and the
// processor class' parent class name and type
if (extends_service) {
string parent_service_name = extends_service->get_name();
- string parent_service_name_lc = to_lower_case(initial_caps_to_underscores(parent_service_name));
+ string parent_service_name_lc =
+ to_lower_case(initial_caps_to_underscores(parent_service_name));
string parent_service_name_uc = to_upper_case(parent_service_name_lc);
- parent_class_name = this->nspace + parent_service_name + "Processor";
- parent_type_name = this->nspace_uc + "TYPE_" + parent_service_name_uc + "_PROCESSOR";
- } else {
+ parent_class_name =
+ this->nspace + parent_service_name + "Processor";
+ parent_type_name =
+ this->nspace_uc + "TYPE_" + parent_service_name_uc + "_PROCESSOR";
+ }
+ else {
parent_class_name = "ThriftDispatchProcessor";
parent_type_name = "THRIFT_TYPE_DISPATCH_PROCESSOR";
}
@@ -1905,178 +2209,253 @@
// Generate the processor class' definition in the header file
// Generate the processor instance definition
- f_header_ << "/* " << service_name_ << " processor */" << endl << "struct _" << class_name << endl
- << "{" << endl;
+ f_header_ <<
+ "/* " << service_name_ << " processor */" << endl <<
+ "struct _" << class_name << endl <<
+ "{" << endl;
indent_up();
- f_header_ << indent() << parent_class_name << " parent;" << endl << endl << indent()
- << "/* protected */" << endl << indent()
- << this->nspace + service_name_ + "Handler *handler;" << endl << indent()
- << "GHashTable *process_map;" << endl;
+ f_header_ <<
+ indent() << parent_class_name << " parent;" << endl <<
+ endl <<
+ indent() << "/* protected */" << endl <<
+ indent() << this->nspace + service_name_ + "Handler *handler;" << endl <<
+ indent() << "GHashTable *process_map;" << endl;
indent_down();
- f_header_ << "};" << endl << "typedef struct _" << class_name << " " << class_name << ";" << endl
- << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << class_name << " " << class_name << ";" << endl <<
+ endl;
// Generate the processor class definition
- f_header_ << "struct _" << class_name << "Class" << endl << "{" << endl;
+ f_header_ <<
+ "struct _" << class_name << "Class" << endl <<
+ "{" << endl;
indent_up();
- f_header_ << indent() << parent_class_name << "Class parent;" << endl << endl << indent()
- << "/* protected */" << endl << indent()
- << "gboolean (*dispatch_call) (ThriftDispatchProcessor *processor," << endl;
+ f_header_ <<
+ indent() << parent_class_name << "Class parent;" << endl <<
+ endl <<
+ indent() << "/* protected */" << endl <<
+ indent() <<
+ "gboolean (*dispatch_call) (ThriftDispatchProcessor *processor," << endl;
args_indent = indent() + string(27, ' ');
- f_header_ << args_indent << "ThriftProtocol *in," << endl << args_indent << "ThriftProtocol *out,"
- << endl << args_indent << "gchar *fname," << endl << args_indent << "gint32 seqid,"
- << endl << args_indent << "GError **error);" << endl;
+ f_header_ <<
+ args_indent << "ThriftProtocol *in," << endl <<
+ args_indent << "ThriftProtocol *out," << endl <<
+ args_indent << "gchar *fname," << endl <<
+ args_indent << "gint32 seqid," << endl <<
+ args_indent << "GError **error);" << endl;
indent_down();
- f_header_ << "};" << endl << "typedef struct _" << class_name << "Class " << class_name
- << "Class;" << endl << endl;
+ f_header_ <<
+ "};" << endl <<
+ "typedef struct _" << class_name << "Class " << class_name << "Class;" << endl <<
+ endl;
// Generate the remaining header boilerplate
- f_header_ << "GType " << class_name_lc << "_get_type (void);" << endl << "#define "
- << this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR "
- << "(" << class_name_lc << "_get_type())" << endl << "#define " << class_name_uc
- << "(obj) "
- << "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_PROCESSOR, " << class_name << "))" << endl << "#define "
- << this->nspace_uc << "IS_" << service_name_uc << "_PROCESSOR(obj) "
- << "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_PROCESSOR))" << endl << "#define " << class_name_uc
- << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_PROCESSOR, " << class_name << "Class))" << endl << "#define "
- << this->nspace_uc << "IS_" << service_name_uc << "_PROCESSOR_CLASS(c) "
- << "(G_TYPE_CHECK_CLASS_TYPE ((c), " << this->nspace_uc << "TYPE_" << service_name_uc
- << "_PROCESSOR))" << endl << "#define " << this->nspace_uc << service_name_uc
- << "_PROCESSOR_GET_CLASS(obj) "
- << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " << this->nspace_uc << "TYPE_"
- << service_name_uc << "_PROCESSOR, " << class_name << "Class))" << endl << endl;
+ f_header_ <<
+ "GType " << class_name_lc << "_get_type (void);" << endl <<
+ "#define " << this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR " <<
+ "(" << class_name_lc << "_get_type())" << endl <<
+ "#define " << class_name_uc << "(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_CAST ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR, " <<
+ class_name << "))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << service_name_uc << "_PROCESSOR(obj) " <<
+ "(G_TYPE_CHECK_INSTANCE_TYPE ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR))" << endl <<
+ "#define " << class_name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR, " <<
+ class_name << "Class))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << service_name_uc <<
+ "_PROCESSOR_CLASS(c) " << "(G_TYPE_CHECK_CLASS_TYPE ((c), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR))" << endl <<
+ "#define " << this->nspace_uc << service_name_uc <<
+ "_PROCESSOR_GET_CLASS(obj) " << "(G_TYPE_INSTANCE_GET_CLASS ((obj), " <<
+ this->nspace_uc << "TYPE_" << service_name_uc << "_PROCESSOR, " <<
+ class_name << "Class))" << endl <<
+ endl;
// Generate the processor's implementation in the implementation file
// Generate the processor's properties enum
- f_service_ << "enum _" << class_name << "Properties" << endl << "{" << endl;
+ f_service_ <<
+ "enum _" << class_name << "Properties" << endl <<
+ "{" << endl;
indent_up();
- f_service_ << indent() << "PROP_" << class_name_uc << "_0," << endl << indent() << "PROP_"
- << class_name_uc << "_HANDLER" << endl;
+ f_service_ <<
+ indent() << "PROP_" << class_name_uc << "_0," << endl <<
+ indent() << "PROP_" << class_name_uc << "_HANDLER" << endl;
indent_down();
- f_service_ << "};" << endl << endl;
+ f_service_ <<
+ "};" << endl <<
+ endl;
// Generate the implementation boilerplate
args_indent = string(15, ' ');
- f_service_ << "G_DEFINE_TYPE (" << class_name << "," << endl << args_indent << class_name_lc
- << "," << endl << args_indent << parent_type_name << ");" << endl << endl;
+ f_service_ <<
+ "G_DEFINE_TYPE (" << class_name << "," << endl <<
+ args_indent << class_name_lc << "," << endl <<
+ args_indent << parent_type_name << ");" << endl <<
+ endl;
// Generate the processor's processing-function type
function_name = class_name + "ProcessFunction";
args_indent = string(function_name.length() + 23, ' ');
- f_service_ << "typedef gboolean (* " << function_name << ") (" << class_name << " *, " << endl
- << args_indent << "gint32," << endl << args_indent << "ThriftProtocol *," << endl
- << args_indent << "ThriftProtocol *," << endl << args_indent << "GError **);" << endl
- << endl;
+ f_service_ <<
+ "typedef gboolean (* " << function_name << ") (" <<
+ class_name << " *, " << endl <<
+ args_indent << "gint32," << endl <<
+ args_indent << "ThriftProtocol *," << endl <<
+ args_indent << "ThriftProtocol *," << endl <<
+ args_indent << "GError **);" << endl <<
+ endl;
// Generate the processor's processing functions
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
string service_function_name = (*function_iter)->get_name();
- string service_function_name_ic = underscores_to_initial_caps(service_function_name);
- string service_function_name_lc = initial_caps_to_underscores(service_function_name);
- string service_function_name_uc = to_upper_case(service_function_name_lc);
+ string service_function_name_ic =
+ underscores_to_initial_caps(service_function_name);
+ string service_function_name_lc =
+ initial_caps_to_underscores(service_function_name);
+ string service_function_name_uc =
+ to_upper_case(service_function_name_lc);
- t_type* return_type = (*function_iter)->get_returntype();
+ t_type *return_type = (*function_iter)->get_returntype();
bool has_return_value = !return_type->is_void();
- t_struct* arg_list = (*function_iter)->get_arglist();
+ t_struct *arg_list = (*function_iter)->get_arglist();
const vector<t_field*>& args = arg_list->get_members();
vector<t_field*>::const_iterator arg_iter;
- const vector<t_field*>& xceptions = (*function_iter)->get_xceptions()->get_members();
+ const vector<t_field*>& xceptions =
+ (*function_iter)->get_xceptions()->get_members();
vector<t_field*>::const_iterator xception_iter;
- string args_class_name = this->nspace + service_name_ + service_function_name_ic + "Args";
- string args_class_type = this->nspace_uc + "TYPE_" + service_name_uc + "_"
- + service_function_name_uc + "_ARGS";
+ string args_class_name =
+ this->nspace + service_name_ + service_function_name_ic + "Args";
+ string args_class_type =
+ this->nspace_uc + "TYPE_" + service_name_uc + "_" +
+ service_function_name_uc + "_ARGS";
- string result_class_name = this->nspace + service_name_ + service_function_name_ic + "Result";
- string result_class_type = this->nspace_uc + "TYPE_" + service_name_uc + "_"
- + service_function_name_uc + "_RESULT";
+ string result_class_name =
+ this->nspace + service_name_ + service_function_name_ic + "Result";
+ string result_class_type =
+ this->nspace_uc + "TYPE_" + service_name_uc + "_" +
+ service_function_name_uc + "_RESULT";
- string handler_function_name = handler_class_name_lc + "_" + service_function_name_lc;
+ string handler_function_name =
+ handler_class_name_lc + "_" + service_function_name_lc;
- function_name = class_name_lc + "_process_"
- + initial_caps_to_underscores(service_function_name);
+ function_name =
+ class_name_lc +
+ "_process_" +
+ initial_caps_to_underscores(service_function_name);
args_indent = string(function_name.length() + 2, ' ');
- f_service_ << "static gboolean" << endl << function_name << " (" << class_name << " *self,"
- << endl << args_indent << "gint32 sequence_id," << endl << args_indent
- << "ThriftProtocol *input_protocol," << endl << args_indent
- << "ThriftProtocol *output_protocol," << endl << args_indent << "GError **error)"
- << endl;
+ f_service_ <<
+ "static gboolean" << endl <<
+ function_name << " (" << class_name << " *self," << endl <<
+ args_indent << "gint32 sequence_id," << endl <<
+ args_indent << "ThriftProtocol *input_protocol," << endl <<
+ args_indent << "ThriftProtocol *output_protocol," << endl <<
+ args_indent << "GError **error)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "gboolean result = TRUE;" << endl << indent()
- << "ThriftTransport * transport;" << endl << indent()
- << args_class_name + " * args =" << endl;
+ f_service_ <<
+ indent() << "gboolean result = TRUE;" << endl <<
+ indent() << "ThriftTransport * transport;" << endl <<
+ indent() << args_class_name + " * args =" << endl;
indent_up();
- f_service_ << indent() << "g_object_new (" << args_class_type << ", NULL);" << endl << endl;
+ f_service_ <<
+ indent() << "g_object_new (" << args_class_type << ", NULL);" << endl <<
+ endl;
indent_down();
if ((*function_iter)->is_oneway()) {
- f_service_ << indent() << "THRIFT_UNUSED_VAR (sequence_id);" << endl << indent()
- << "THRIFT_UNUSED_VAR (output_protocol);" << endl << endl;
+ f_service_ <<
+ indent() << "THRIFT_UNUSED_VAR (sequence_id);" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (output_protocol);" << endl <<
+ endl;
}
- f_service_ << indent() << "g_object_get (input_protocol, \"transport\", "
- << "&transport, NULL);" << endl << endl;
+ f_service_ <<
+ indent() << "g_object_get (input_protocol, \"transport\", " <<
+ "&transport, NULL);" << endl <<
+ endl;
// Read the method's arguments from the caller
- f_service_ << indent() << "if ((thrift_struct_read (THRIFT_STRUCT (args), "
- << "input_protocol, error) != -1) &&" << endl << indent()
- << " (thrift_protocol_read_message_end (input_protocol, "
- << "error) != -1) &&" << endl << indent()
- << " (thrift_transport_read_end (transport, error) != FALSE))" << endl;
+ f_service_ <<
+ indent() << "if ((thrift_struct_read (THRIFT_STRUCT (args), " <<
+ "input_protocol, error) != -1) &&" << endl <<
+ indent() << " (thrift_protocol_read_message_end (input_protocol, " <<
+ "error) != -1) &&" << endl <<
+ indent() << " (thrift_transport_read_end (transport, error) != FALSE))" << endl;
scope_up(f_service_);
for (arg_iter = args.begin(); arg_iter != args.end(); ++arg_iter) {
- f_service_ << indent() << property_type_name((*arg_iter)->get_type()) << " "
- << (*arg_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << property_type_name((*arg_iter)->get_type()) << " " <<
+ (*arg_iter)->get_name() << ";" << endl;
}
- for (xception_iter = xceptions.begin(); xception_iter != xceptions.end(); ++xception_iter) {
- f_service_ << indent() << type_name((*xception_iter)->get_type()) << " "
- << initial_caps_to_underscores((*xception_iter)->get_name()) << " = NULL;" << endl;
+ for (xception_iter = xceptions.begin();
+ xception_iter != xceptions.end();
+ ++xception_iter) {
+ f_service_ <<
+ indent() << type_name((*xception_iter)->get_type()) << " " <<
+ initial_caps_to_underscores((*xception_iter)->get_name()) <<
+ " = NULL;" <<
+ endl;
}
if (has_return_value) {
- f_service_ << indent() << property_type_name(return_type) << " return_value;" << endl;
+ f_service_ <<
+ indent() << property_type_name(return_type) << " return_value;" << endl;
}
if (!(*function_iter)->is_oneway()) {
- f_service_ << indent() << result_class_name << " * result_struct;" << endl;
+ f_service_ <<
+ indent() << result_class_name << " * result_struct;" << endl;
}
f_service_ << endl;
if (args.size() > 0) {
- f_service_ << indent() << "g_object_get (args," << endl;
+ f_service_ <<
+ indent() << "g_object_get (args," << endl;
args_indent = indent() + string(14, ' ');
for (arg_iter = args.begin(); arg_iter != args.end(); ++arg_iter) {
string arg_name = (*arg_iter)->get_name();
- f_service_ << args_indent << "\"" << arg_name << "\", &" << arg_name << "," << endl;
+ f_service_ <<
+ args_indent << "\"" << arg_name << "\", &" << arg_name << "," << endl;
}
- f_service_ << args_indent << "NULL);" << endl << endl;
+ f_service_ <<
+ args_indent << "NULL);" << endl <<
+ endl;
}
if (!(*function_iter)->is_oneway()) {
- f_service_ << indent() << "g_object_unref (transport);" << endl << indent()
- << "g_object_get (output_protocol, \"transport\", "
- << "&transport, NULL);" << endl << endl << indent()
- << "result_struct = g_object_new (" << result_class_type << ", NULL);" << endl;
+ f_service_ <<
+ indent() << "g_object_unref (transport);" << endl <<
+ indent() << "g_object_get (output_protocol, \"transport\", " <<
+ "&transport, NULL);" << endl <<
+ endl <<
+ indent() << "result_struct = g_object_new (" << result_class_type <<
+ ", NULL);" << endl;
if (has_return_value) {
- f_service_ << indent() << "g_object_get (result_struct, "
- "\"success\", &return_value, NULL);" << endl;
+ f_service_ <<
+ indent() << "g_object_get (result_struct, "
+ "\"success\", &return_value, NULL);" << endl;
}
- f_service_ << endl;
+ f_service_ <<
+ endl;
}
// Pass the arguments to the corresponding method in the handler
- f_service_ << indent() << "if (" << handler_function_name << " (" << this->nspace_uc
- << service_name_uc << "_IF (self->handler)," << endl;
+ f_service_ <<
+ indent() << "if (" << handler_function_name << " (" <<
+ this->nspace_uc << service_name_uc << "_IF (self->handler)," << endl;
args_indent = indent() + string(handler_function_name.length() + 6, ' ');
if (has_return_value) {
string return_type_name = type_name(return_type);
- f_service_ << args_indent;
+ f_service_ <<
+ args_indent;
// Cast return_value if it was declared as a type other than the return
// value's actual type---this is true for integer values 32 bits or fewer
@@ -2088,106 +2467,141 @@
}
return_type_name += '*';
- f_service_ << "(" << return_type_name << ")";
+ f_service_ <<
+ "(" << return_type_name << ")";
}
- f_service_ << "&return_value," << endl;
+ f_service_ <<
+ "&return_value," << endl;
}
for (arg_iter = args.begin(); arg_iter != args.end(); ++arg_iter) {
- f_service_ << args_indent << (*arg_iter)->get_name() << "," << endl;
+ f_service_ <<
+ args_indent << (*arg_iter)->get_name() << "," << endl;
}
- for (xception_iter = xceptions.begin(); xception_iter != xceptions.end(); ++xception_iter) {
- f_service_ << args_indent << "&" << initial_caps_to_underscores((*xception_iter)->get_name())
- << "," << endl;
+ for (xception_iter = xceptions.begin();
+ xception_iter != xceptions.end();
+ ++xception_iter) {
+ f_service_ <<
+ args_indent << "&" <<
+ initial_caps_to_underscores((*xception_iter)->get_name()) << "," << endl;
}
- f_service_ << args_indent << "error) == TRUE)" << endl;
+ f_service_ <<
+ args_indent << "error) == TRUE)" << endl;
scope_up(f_service_);
// The handler reported success; return the result, if any, to the caller
if (!(*function_iter)->is_oneway()) {
if (has_return_value) {
- f_service_ << indent() << "g_object_set (result_struct, \"success\", return_value, "
- << "NULL);" << endl;
+ f_service_ <<
+ indent() << "g_object_set (result_struct, \"success\", return_value, " <<
+ "NULL);" << endl;
// Deallocate (or unref) return_value
return_type = get_true_type(return_type);
if (return_type->is_base_type()) {
- t_base_type* base_type = ((t_base_type*)return_type);
+ t_base_type *base_type = ((t_base_type *) return_type);
if (base_type->get_base() == t_base_type::TYPE_STRING) {
- f_service_ << indent() << "if (return_value != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (return_value != NULL)" << endl;
indent_up();
if (base_type->is_binary()) {
- f_service_ << indent() << "g_byte_array_unref (return_value);" << endl;
+ f_service_ <<
+ indent() << "g_byte_array_unref (return_value);" << endl;
} else {
- f_service_ << indent() << "g_free (return_value);" << endl;
+ f_service_ <<
+ indent() << "g_free (return_value);" << endl;
}
indent_down();
}
} else if (return_type->is_container()) {
- f_service_ << indent() << "if (return_value != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (return_value != NULL)" << endl;
indent_up();
if (return_type->is_list()) {
- f_service_ << indent() << "g_array_unref (return_value);" << endl;
+ f_service_ <<
+ indent() << "g_array_unref (return_value);" << endl;
} else if (return_type->is_map() || return_type->is_set()) {
- f_service_ << indent() << "g_hash_table_unref (return_value);" << endl;
+ f_service_ <<
+ indent() << "g_hash_table_unref (return_value);" << endl;
}
indent_down();
} else if (return_type->is_struct()) {
- f_service_ << indent() << "if (return_value != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (return_value != NULL)" << endl;
indent_up();
- f_service_ << indent() << "g_object_unref (return_value);" << endl;
+ f_service_ <<
+ indent() << "g_object_unref (return_value);" << endl;
indent_down();
}
- f_service_ << endl;
+ f_service_ <<
+ endl;
}
- f_service_ << indent() << "result =" << endl;
+ f_service_ <<
+ indent() << "result =" << endl;
indent_up();
- f_service_ << indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
+ f_service_ <<
+ indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
args_indent = indent() + string(39, ' ');
- f_service_ << args_indent << "\"" << service_function_name << "\"," << endl << args_indent
- << "T_REPLY," << endl << args_indent << "sequence_id," << endl << args_indent
- << "error) != -1) &&" << endl << indent()
- << " (thrift_struct_write (THRIFT_STRUCT (result_struct)," << endl;
+ f_service_ <<
+ args_indent << "\"" << service_function_name << "\"," << endl <<
+ args_indent << "T_REPLY," << endl <<
+ args_indent << "sequence_id," << endl <<
+ args_indent << "error) != -1) &&" << endl <<
+ indent() << " (thrift_struct_write (THRIFT_STRUCT (result_struct)," << endl;
args_indent = indent() + string(23, ' ');
- f_service_ << args_indent << "output_protocol," << endl << args_indent << "error) != -1));"
- << endl;
+ f_service_ <<
+ args_indent << "output_protocol," << endl <<
+ args_indent << "error) != -1));" << endl;
indent_down();
}
scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
scope_up(f_service_);
// The handler reported failure; check to see if an application-defined
// exception was raised and if so, return it to the caller
f_service_ << indent();
if (xceptions.size() > 0) {
- for (xception_iter = xceptions.begin(); xception_iter != xceptions.end(); ++xception_iter) {
- f_service_ << "if (" << initial_caps_to_underscores((*xception_iter)->get_name())
- << " != NULL)" << endl;
+ for (xception_iter = xceptions.begin();
+ xception_iter != xceptions.end();
+ ++xception_iter) {
+ f_service_ <<
+ "if (" << initial_caps_to_underscores((*xception_iter)->get_name()) <<
+ " != NULL)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "g_object_set (result_struct," << endl;
+ f_service_ <<
+ indent() << "g_object_set (result_struct," << endl;
args_indent = indent() + string(14, ' ');
- f_service_ << args_indent << "\"" << (*xception_iter)->get_name() << "\", "
- << (*xception_iter)->get_name() << "," << endl << args_indent << "NULL);" << endl
- << endl;
- f_service_ << indent() << "result =" << endl;
+ f_service_ <<
+ args_indent << "\"" << (*xception_iter)->get_name() << "\", " <<
+ (*xception_iter)->get_name() << "," << endl <<
+ args_indent << "NULL);" << endl <<
+ endl;
+ f_service_ <<
+ indent() << "result =" << endl;
indent_up();
- f_service_ << indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
+ f_service_ <<
+ indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
args_indent = indent() + string(39, ' ');
- f_service_ << args_indent << "\"" << service_function_name << "\"," << endl << args_indent
- << "T_REPLY," << endl << args_indent << "sequence_id," << endl << args_indent
- << "error) != -1) &&" << endl << indent()
- << " (thrift_struct_write (THRIFT_STRUCT (result_struct)," << endl;
+ f_service_ <<
+ args_indent << "\"" << service_function_name << "\"," << endl <<
+ args_indent << "T_REPLY," << endl <<
+ args_indent << "sequence_id," << endl <<
+ args_indent << "error) != -1) &&" << endl <<
+ indent() << " (thrift_struct_write (THRIFT_STRUCT (result_struct)," << endl;
args_indent = indent() + string(23, ' ');
- f_service_ << args_indent << "output_protocol," << endl << args_indent << "error) != -1));"
- << endl;
+ f_service_ <<
+ args_indent << "output_protocol," << endl <<
+ args_indent << "error) != -1));" << endl;
indent_down();
scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
}
scope_up(f_service_);
@@ -2197,35 +2611,49 @@
// If the handler reported failure but raised no application-defined
// exception, return a Thrift application exception with the information
// returned via GLib's own error-reporting mechanism
- f_service_ << "if (*error == NULL)" << endl;
+ f_service_ <<
+ "if (*error == NULL)" << endl;
indent_up();
- f_service_ << indent() << "g_warning (\"" << service_name_ << "."
- << (*function_iter)->get_name() << " implementation returned FALSE \"" << endl
- << indent() << string(11, ' ') << "\"but did not set an error\");" << endl << endl;
+ f_service_ <<
+ indent() << "g_warning (\"" << service_name_ << "." <<
+ (*function_iter)->get_name() << " implementation returned FALSE \"" << endl <<
+ indent() << string (11, ' ') << "\"but did not set an error\");" << endl <<
+ endl;
indent_down();
- f_service_ << indent() << "ThriftApplicationException *xception =" << endl;
+ f_service_ <<
+ indent() << "ThriftApplicationException *xception =" << endl;
indent_up();
- f_service_ << indent() << "g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION," << endl;
+ f_service_ <<
+ indent() << "g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION," << endl;
args_indent = indent() + string(14, ' ');
- f_service_ << args_indent << "\"type\", *error != NULL ? (*error)->code :" << endl
- << args_indent << string(11, ' ') << "THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN,"
- << endl << args_indent << "\"message\", *error != NULL ? (*error)->message : NULL,"
- << endl << args_indent << "NULL);" << endl;
+ f_service_ <<
+ args_indent << "\"type\", *error != NULL ? (*error)->code :" << endl <<
+ args_indent << string(11, ' ') << "THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN," << endl <<
+ args_indent << "\"message\", *error != NULL ? (*error)->message : NULL," << endl <<
+ args_indent << "NULL);" << endl;
indent_down();
- f_service_ << indent() << "g_clear_error (error);" << endl << endl << indent()
- << "result =" << endl;
+ f_service_ <<
+ indent() << "g_clear_error (error);" << endl <<
+ endl <<
+ indent() << "result =" << endl;
indent_up();
- f_service_ << indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
+ f_service_ <<
+ indent() << "((thrift_protocol_write_message_begin (output_protocol," << endl;
args_indent = indent() + string(39, ' ');
- f_service_ << args_indent << "\"" << service_function_name << "\"," << endl << args_indent
- << "T_EXCEPTION," << endl << args_indent << "sequence_id," << endl << args_indent
- << "error) != -1) &&" << endl << indent()
- << " (thrift_struct_write (THRIFT_STRUCT (xception)," << endl;
+ f_service_ <<
+ args_indent << "\"" << service_function_name << "\"," << endl <<
+ args_indent << "T_EXCEPTION," << endl <<
+ args_indent << "sequence_id," << endl <<
+ args_indent << "error) != -1) &&" << endl <<
+ indent() << " (thrift_struct_write (THRIFT_STRUCT (xception)," << endl;
args_indent = indent() + string(23, ' ');
- f_service_ << args_indent << "output_protocol," << endl << args_indent << "error) != -1));"
- << endl;
+ f_service_ <<
+ args_indent << "output_protocol," << endl <<
+ args_indent << "error) != -1));" << endl;
indent_down();
- f_service_ << endl << indent() << "g_object_unref (xception);" << endl;
+ f_service_ <<
+ endl <<
+ indent() << "g_object_unref (xception);" << endl;
if (xceptions.size() > 0) {
scope_down(f_service_);
@@ -2236,63 +2664,81 @@
// Dellocate or unref retrieved argument values as necessary
for (arg_iter = args.begin(); arg_iter != args.end(); ++arg_iter) {
string arg_name = (*arg_iter)->get_name();
- t_type* arg_type = get_true_type((*arg_iter)->get_type());
+ t_type *arg_type = get_true_type((*arg_iter)->get_type());
if (arg_type->is_base_type()) {
- t_base_type* base_type = ((t_base_type*)arg_type);
+ t_base_type *base_type = ((t_base_type *) arg_type);
if (base_type->get_base() == t_base_type::TYPE_STRING) {
- f_service_ << indent() << "if (" << arg_name << " != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (" << arg_name << " != NULL)" << endl;
indent_up();
if (base_type->is_binary()) {
- f_service_ << indent() << "g_byte_array_unref (" << arg_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_byte_array_unref (" << arg_name << ");" << endl;
} else {
- f_service_ << indent() << "g_free (" << arg_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_free (" << arg_name << ");" << endl;
}
indent_down();
}
} else if (arg_type->is_container()) {
- f_service_ << indent() << "if (" << arg_name << " != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (" << arg_name << " != NULL)" << endl;
indent_up();
if (arg_type->is_list()) {
- f_service_ << indent() << "g_array_unref (" << arg_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_array_unref (" << arg_name << ");" << endl;
} else if (arg_type->is_map() || arg_type->is_set()) {
- f_service_ << indent() << "g_hash_table_unref (" << arg_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_hash_table_unref (" << arg_name << ");" << endl;
}
indent_down();
} else if (arg_type->is_struct()) {
- f_service_ << indent() << "if (" << arg_name << " != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (" << arg_name << " != NULL)" << endl;
indent_up();
- f_service_ << indent() << "g_object_unref (" << arg_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_object_unref (" << arg_name << ");" << endl;
indent_down();
}
}
- if (!(*function_iter)->is_oneway()) {
- f_service_ << indent() << "g_object_unref (result_struct);" << endl << endl << indent()
- << "if (result == TRUE)" << endl;
+ if(!(*function_iter)->is_oneway()) {
+ f_service_ <<
+ indent() << "g_object_unref (result_struct);" << endl <<
+ endl <<
+ indent() << "if (result == TRUE)" << endl;
indent_up();
- f_service_ << indent() << "result =" << endl;
+ f_service_ <<
+ indent() << "result =" << endl;
indent_up();
- f_service_ << indent() << "((thrift_protocol_write_message_end "
- << "(output_protocol, error) != -1) &&" << endl << indent()
- << " (thrift_transport_write_end (transport, error) "
- << "!= FALSE) &&" << endl << indent()
- << " (thrift_transport_flush (transport, error) "
- << "!= FALSE));" << endl;
+ f_service_ <<
+ indent() << "((thrift_protocol_write_message_end " <<
+ "(output_protocol, error) != -1) &&" << endl <<
+ indent() << " (thrift_transport_write_end (transport, error) " <<
+ "!= FALSE) &&" << endl <<
+ indent() << " (thrift_transport_flush (transport, error) " <<
+ "!= FALSE));" << endl;
indent_down();
indent_down();
}
scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
indent_up();
- f_service_ << indent() << "result = FALSE;" << endl;
+ f_service_ <<
+ indent() << "result = FALSE;" << endl;
indent_down();
- f_service_ << endl << indent() << "g_object_unref (transport);" << endl << indent()
- << "g_object_unref (args);" << endl << endl << indent() << "return result;" << endl;
+ f_service_ <<
+ endl <<
+ indent() << "g_object_unref (transport);" << endl <<
+ indent() << "g_object_unref (args);" << endl <<
+ endl <<
+ indent() << "return result;" << endl;
scope_down(f_service_);
f_service_ << endl;
@@ -2301,81 +2747,117 @@
// Generate the processor's dispatch_call implementation
function_name = class_name_lc + "_dispatch_call";
args_indent = indent() + string(function_name.length() + 2, ' ');
- f_service_ << "static gboolean" << endl << function_name
- << " (ThriftDispatchProcessor *dispatch_processor," << endl << args_indent
- << "ThriftProtocol *input_protocol," << endl << args_indent
- << "ThriftProtocol *output_protocol," << endl << args_indent << "gchar *method_name,"
- << endl << args_indent << "gint32 sequence_id," << endl << args_indent
- << "GError **error)" << endl;
+ f_service_ <<
+ "static gboolean" << endl <<
+ function_name << " (ThriftDispatchProcessor *dispatch_processor," << endl <<
+ args_indent << "ThriftProtocol *input_protocol," << endl <<
+ args_indent << "ThriftProtocol *output_protocol," << endl <<
+ args_indent << "gchar *method_name," << endl <<
+ args_indent << "gint32 sequence_id," << endl <<
+ args_indent << "GError **error)" << endl;
scope_up(f_service_);
- f_service_ << indent() << class_name << "ProcessFunction process_function; " << endl;
- f_service_ << indent() << "gboolean dispatch_result = FALSE;" << endl << endl << indent()
- << class_name << " *self = " << class_name_uc << " (dispatch_processor);" << endl;
- f_service_ << indent() << parent_class_name << "Class "
- "*parent_class =" << endl;
+ f_service_ <<
+ indent() << class_name << "ProcessFunction process_function; " << endl;
+ f_service_ <<
+ indent() << "gboolean dispatch_result = FALSE;" << endl <<
+ endl <<
+ indent() << class_name << " *self = " <<
+ class_name_uc << " (dispatch_processor);" << endl;
+ f_service_ <<
+ indent() << parent_class_name << "Class "
+ "*parent_class =" << endl;
indent_up();
- f_service_ << indent() << "g_type_class_peek_parent (" << class_name_uc << "_GET_CLASS (self));"
- << endl;
+ f_service_ <<
+ indent() << "g_type_class_peek_parent (" << class_name_uc <<
+ "_GET_CLASS (self));" << endl;
indent_down();
- f_service_ << endl << indent() << "process_function = g_hash_table_lookup ("
- << "self->process_map, method_name);" << endl << indent()
- << "if (process_function != NULL)" << endl;
+ f_service_ <<
+ endl <<
+ indent() << "process_function = g_hash_table_lookup (" <<
+ "self->process_map, method_name);" << endl <<
+ indent() << "if (process_function != NULL)" << endl;
scope_up(f_service_);
args_indent = indent() + string(39, ' ');
- f_service_ << indent() << "dispatch_result = (*process_function) (self," << endl << args_indent
- << "sequence_id," << endl << args_indent << "input_protocol," << endl << args_indent
- << "output_protocol," << endl << args_indent << "error);" << endl;
+ f_service_ <<
+ indent() << "dispatch_result = (*process_function) (self," << endl <<
+ args_indent << "sequence_id," << endl <<
+ args_indent << "input_protocol," << endl <<
+ args_indent << "output_protocol," << endl <<
+ args_indent << "error);" << endl;
scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
scope_up(f_service_);
// Method name not recognized; chain up to our parent processor---note the
// top-most implementation of this method, in ThriftDispatchProcessor itself,
// will return an application exception to the caller if no class in the
// hierarchy recognizes the method name
- f_service_ << indent() << "dispatch_result = parent_class->dispatch_call "
- "(dispatch_processor," << endl;
+ f_service_ <<
+ indent() << "dispatch_result = parent_class->dispatch_call "
+ "(dispatch_processor," << endl;
args_indent = indent() + string(47, ' ');
- f_service_ << args_indent << "input_protocol," << endl << args_indent << "output_protocol,"
- << endl << args_indent << "method_name," << endl << args_indent << "sequence_id,"
- << endl << args_indent << "error);" << endl;
+ f_service_ <<
+ args_indent << "input_protocol," << endl <<
+ args_indent << "output_protocol," << endl <<
+ args_indent << "method_name," << endl <<
+ args_indent << "sequence_id," << endl <<
+ args_indent << "error);" << endl;
scope_down(f_service_);
- f_service_ << endl << indent() << "return dispatch_result;" << endl;
+ f_service_ <<
+ endl <<
+ indent() << "return dispatch_result;" << endl;
scope_down(f_service_);
f_service_ << endl;
// Generate the processor's property setter
function_name = class_name_lc + "_set_property";
args_indent = string(function_name.length() + 2, ' ');
- f_service_ << "static void" << endl << function_name << " (GObject *object," << endl
- << args_indent << "guint property_id," << endl << args_indent << "const GValue *value,"
- << endl << args_indent << "GParamSpec *pspec)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ function_name << " (GObject *object," << endl <<
+ args_indent << "guint property_id," << endl <<
+ args_indent << "const GValue *value," << endl <<
+ args_indent << "GParamSpec *pspec)" << endl;
scope_up(f_service_);
- f_service_ << indent() << class_name << " *self = " << class_name_uc << " (object);" << endl
- << endl << indent() << "switch (property_id)" << endl;
+ f_service_ <<
+ indent() << class_name << " *self = " <<
+ class_name_uc << " (object);" << endl <<
+ endl <<
+ indent() << "switch (property_id)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "case PROP_" << class_name_uc << "_HANDLER:" << endl;
+ f_service_ <<
+ indent() << "case PROP_" << class_name_uc << "_HANDLER:" << endl;
indent_up();
- f_service_ << indent() << "if (self->handler != NULL)" << endl;
+ f_service_ <<
+ indent() << "if (self->handler != NULL)" << endl;
indent_up();
- f_service_ << indent() << "g_object_unref (self->handler);" << endl;
+ f_service_ <<
+ indent() << "g_object_unref (self->handler);" << endl;
indent_down();
- f_service_ << indent() << "self->handler = g_value_get_object (value);" << endl << indent()
- << "g_object_ref (self->handler);" << endl;
+ f_service_ <<
+ indent() << "self->handler = g_value_get_object (value);" << endl <<
+ indent() << "g_object_ref (self->handler);" << endl;
if (extends_service) {
// Chain up to set the handler in every superclass as well
- f_service_ << endl << indent() << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)->"
- << endl;
+ f_service_ <<
+ endl <<
+ indent() << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)->" << endl;
indent_up();
- f_service_ << indent() << "set_property (object, property_id, value, pspec);" << endl;
+ f_service_ <<
+ indent() << "set_property (object, property_id, value, pspec);" << endl;
indent_down();
}
- f_service_ << indent() << "break;" << endl;
+ f_service_ <<
+ indent() << "break;" << endl;
indent_down();
- f_service_ << indent() << "default:" << endl;
+ f_service_ <<
+ indent() << "default:" << endl;
indent_up();
- f_service_ << indent() << "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);"
- << endl << indent() << "break;" << endl;
+ f_service_ <<
+ indent() <<
+ "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);" << endl <<
+ indent() << "break;" << endl;
indent_down();
scope_down(f_service_);
scope_down(f_service_);
@@ -2384,121 +2866,166 @@
// Generate processor's property getter
function_name = class_name_lc + "_get_property";
args_indent = string(function_name.length() + 2, ' ');
- f_service_ << "static void" << endl << function_name << " (GObject *object," << endl
- << args_indent << "guint property_id," << endl << args_indent << "GValue *value,"
- << endl << args_indent << "GParamSpec *pspec)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ function_name << " (GObject *object," << endl <<
+ args_indent << "guint property_id," << endl <<
+ args_indent << "GValue *value," << endl <<
+ args_indent << "GParamSpec *pspec)" << endl;
scope_up(f_service_);
- f_service_ << indent() << class_name << " *self = " << class_name_uc << " (object);" << endl
- << endl << indent() << "switch (property_id)" << endl;
+ f_service_ <<
+ indent() << class_name << " *self = " <<
+ class_name_uc << " (object);" << endl <<
+ endl <<
+ indent() << "switch (property_id)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "case PROP_" << class_name_uc << "_HANDLER:" << endl;
+ f_service_ <<
+ indent() << "case PROP_" << class_name_uc << "_HANDLER:" << endl;
indent_up();
- f_service_ << indent() << "g_value_set_object (value, self->handler);" << endl << indent()
- << "break;" << endl;
+ f_service_ <<
+ indent() << "g_value_set_object (value, self->handler);" << endl <<
+ indent() << "break;" << endl;
indent_down();
- f_service_ << indent() << "default:" << endl;
+ f_service_ <<
+ indent() << "default:" << endl;
indent_up();
- f_service_ << indent() << "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);"
- << endl << indent() << "break;" << endl;
+ f_service_ <<
+ indent() <<
+ "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);" << endl <<
+ indent() << "break;" << endl;
indent_down();
scope_down(f_service_);
scope_down(f_service_);
f_service_ << endl;
// Generator the processor's dispose function
- f_service_ << "static void" << endl << class_name_lc << "_dispose (GObject *gobject)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_dispose (GObject *gobject)" << endl;
scope_up(f_service_);
- f_service_ << indent() << class_name << " *self = " << class_name_uc << " (gobject);" << endl
- << endl << indent() << "if (self->handler != NULL)" << endl;
+ f_service_ <<
+ indent() << class_name << " *self = " << class_name_uc << " (gobject);" << endl <<
+ endl <<
+ indent() << "if (self->handler != NULL)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "g_object_unref (self->handler);" << endl << indent()
- << "self->handler = NULL;" << endl;
+ f_service_ <<
+ indent() << "g_object_unref (self->handler);" << endl <<
+ indent() << "self->handler = NULL;" << endl;
scope_down(f_service_);
- f_service_ << endl << indent() << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)"
- "->dispose (gobject);"
- << endl;
+ f_service_ <<
+ endl <<
+ indent() << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)"
+ "->dispose (gobject);" << endl;
scope_down(f_service_);
f_service_ << endl;
// Generate processor finalize function
- f_service_ << "static void" << endl << class_name_lc << "_finalize (GObject *gobject)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_finalize (GObject *gobject)" << endl;
scope_up(f_service_);
- f_service_ << indent() << this->nspace << service_name_ << "Processor *self = " << this->nspace_uc
- << service_name_uc << "_PROCESSOR (gobject);" << endl << endl << indent()
- << "g_hash_table_destroy (self->process_map);" << endl << endl << indent()
- << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)"
- "->finalize (gobject);" << endl;
+ f_service_ <<
+ indent() << this->nspace << service_name_ << "Processor *self = " <<
+ this->nspace_uc << service_name_uc << "_PROCESSOR (gobject);" << endl <<
+ endl <<
+ indent() << "g_hash_table_destroy (self->process_map);" << endl <<
+ endl <<
+ indent() << "G_OBJECT_CLASS (" << class_name_lc << "_parent_class)"
+ "->finalize (gobject);" << endl;
scope_down(f_service_);
f_service_ << endl;
// Generate processor instance initializer
- f_service_ << "static void" << endl << class_name_lc << "_init (" << class_name << " *self)"
- << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_init (" << class_name << " *self)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "self->handler = NULL;" << endl << indent()
- << "self->process_map = "
- "g_hash_table_new (g_str_hash, g_str_equal);" << endl << endl;
+ f_service_ <<
+ indent() << "self->handler = NULL;" << endl <<
+ indent() << "self->process_map = "
+ "g_hash_table_new (g_str_hash, g_str_equal);" << endl <<
+ endl;
args_indent = string(21, ' ');
- for (function_iter = functions.begin(); function_iter != functions.end(); ++function_iter) {
+ for (function_iter = functions.begin();
+ function_iter != functions.end();
+ ++function_iter) {
string service_function_name = (*function_iter)->get_name();
- string process_function_name = class_name_lc + "_process_"
- + initial_caps_to_underscores(service_function_name);
+ string process_function_name =
+ class_name_lc +
+ "_process_" +
+ initial_caps_to_underscores(service_function_name);
- f_service_ << indent() << "g_hash_table_insert (self->process_map," << endl
- << indent() + args_indent + "\"" << service_function_name << "\", " << endl
- << indent() + args_indent + process_function_name << ");" << endl;
+ f_service_ <<
+ indent() << "g_hash_table_insert (self->process_map," << endl <<
+ indent() + args_indent + "\"" << service_function_name << "\", " << endl <<
+ indent() + args_indent + process_function_name << ");" << endl;
}
scope_down(f_service_);
- f_service_ << endl;
+ f_service_ <<
+ endl;
// Generate processor class initializer
- f_service_ << "static void" << endl << class_name_lc << "_class_init (" << class_name
- << "Class *cls)" << endl;
+ f_service_ <<
+ "static void" << endl <<
+ class_name_lc << "_class_init (" << class_name << "Class *cls)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl
- << indent() << "ThriftDispatchProcessorClass *dispatch_processor_class =" << endl;
+ f_service_ <<
+ indent() << "GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl <<
+ indent() << "ThriftDispatchProcessorClass *dispatch_processor_class =" << endl;
indent_up();
- f_service_ << indent() << "THRIFT_DISPATCH_PROCESSOR_CLASS (cls);" << endl;
+ f_service_ <<
+ indent() << "THRIFT_DISPATCH_PROCESSOR_CLASS (cls);" << endl;
indent_down();
- f_service_ << indent() << "GParamSpec *param_spec;" << endl << endl << indent()
- << "gobject_class->dispose = " << class_name_lc << "_dispose;" << endl << indent()
- << "gobject_class->finalize = " << class_name_lc << "_finalize;" << endl << indent()
- << "gobject_class->set_property = " << class_name_lc << "_set_property;" << endl
- << indent() << "gobject_class->get_property = " << class_name_lc << "_get_property;"
- << endl << endl << indent()
- << "dispatch_processor_class->dispatch_call = " << class_name_lc << "_dispatch_call;"
- << endl << indent() << "cls->dispatch_call = " << class_name_lc << "_dispatch_call;"
- << endl << endl << indent() << "param_spec = g_param_spec_object (\"handler\","
- << endl;
+ f_service_ <<
+ indent() << "GParamSpec *param_spec;" << endl <<
+ endl <<
+ indent() << "gobject_class->dispose = " <<
+ class_name_lc << "_dispose;" << endl <<
+ indent() << "gobject_class->finalize = " <<
+ class_name_lc << "_finalize;" << endl <<
+ indent() << "gobject_class->set_property = " <<
+ class_name_lc << "_set_property;" << endl <<
+ indent() << "gobject_class->get_property = " <<
+ class_name_lc << "_get_property;" << endl <<
+ endl <<
+ indent() << "dispatch_processor_class->dispatch_call = " <<
+ class_name_lc << "_dispatch_call;" << endl <<
+ indent() << "cls->dispatch_call = " <<
+ class_name_lc << "_dispatch_call;" << endl <<
+ endl <<
+ indent() << "param_spec = g_param_spec_object (\"handler\"," << endl;
args_indent = indent() + string(34, ' ');
- f_service_ << args_indent << "\"Service handler implementation\"," << endl << args_indent
- << "\"The service handler implementation \"" << endl << args_indent
- << "\"to which method calls are dispatched.\"," << endl << args_indent
- << this->nspace_uc + "TYPE_" + service_name_uc + "_HANDLER," << endl << args_indent
- << "G_PARAM_READWRITE);" << endl;
- f_service_ << indent() << "g_object_class_install_property (gobject_class," << endl;
- args_indent = indent() + string(33, ' ');
- f_service_ << args_indent << "PROP_" << class_name_uc << "_HANDLER," << endl << args_indent
- << "param_spec);" << endl;
+ f_service_ <<
+ args_indent << "\"Service handler implementation\"," << endl <<
+ args_indent << "\"The service handler implementation \"" << endl <<
+ args_indent << "\"to which method calls are dispatched.\"," << endl <<
+ args_indent << this->nspace_uc + "TYPE_" + service_name_uc + "_HANDLER," << endl <<
+ args_indent << "G_PARAM_READWRITE);" << endl;
+ f_service_ <<
+ indent() << "g_object_class_install_property (gobject_class," << endl;
+ args_indent = indent() + string (33, ' ');
+ f_service_ <<
+ args_indent << "PROP_" << class_name_uc << "_HANDLER," << endl <<
+ args_indent << "param_spec);" << endl;
scope_down(f_service_);
}
/**
* Generates C code that represents a Thrift service server.
*/
-void t_c_glib_generator::generate_service_server(t_service* tservice) {
- (void)tservice;
+void t_c_glib_generator::generate_service_server (t_service *tservice) {
+ (void) tservice;
// Generate the service's handler class
- generate_service_handler(tservice);
+ generate_service_handler (tservice);
// Generate the service's processor class
- generate_service_processor(tservice);
+ generate_service_processor (tservice);
}
/**
* Generates C code to represent a THrift structure as a GObject.
*/
-void t_c_glib_generator::generate_object(t_struct* tstruct) {
+void t_c_glib_generator::generate_object(t_struct *tstruct) {
string name = tstruct->get_name();
string name_u = initial_caps_to_underscores(name);
string name_uc = to_upper_case(name_u);
@@ -2511,100 +3038,125 @@
string args_indent;
// write the instance definition
- f_types_ << "struct _" << this->nspace << name << endl << "{ " << endl
- << " ThriftStruct parent; " << endl << endl << " /* public */" << endl;
+ f_types_ <<
+ "struct _" << this->nspace << name << endl <<
+ "{ " << endl <<
+ " ThriftStruct parent; " << endl <<
+ endl <<
+ " /* public */" << endl;
// for each field, add a member variable
- vector<t_field*>::const_iterator m_iter;
- const vector<t_field*>& members = tstruct->get_members();
+ vector<t_field *>::const_iterator m_iter;
+ const vector<t_field *> &members = tstruct->get_members();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- f_types_ << " " << type_name(t) << " " << (*m_iter)->get_name() << ";" << endl;
+ t_type *t = get_true_type ((*m_iter)->get_type());
+ f_types_ <<
+ " " << type_name (t) << " " << (*m_iter)->get_name() << ";" << endl;
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
- f_types_ << " gboolean __isset_" << (*m_iter)->get_name() << ";" << endl;
+ f_types_ <<
+ " gboolean __isset_" << (*m_iter)->get_name() << ";" << endl;
}
}
// close the structure definition and create a typedef
- f_types_ << "};" << endl << "typedef struct _" << this->nspace << name << " " << this->nspace
- << name << ";" << endl << endl;
+ f_types_ <<
+ "};" << endl <<
+ "typedef struct _" << this->nspace << name << " " <<
+ this->nspace << name << ";" << endl <<
+ endl;
// write the class definition
- f_types_ << "struct _" << this->nspace << name << "Class" << endl << "{" << endl
- << " ThriftStructClass parent;" << endl << "};" << endl << "typedef struct _"
- << this->nspace << name << "Class " << this->nspace << name << "Class;" << endl << endl;
+ f_types_ <<
+ "struct _" << this->nspace << name << "Class" << endl <<
+ "{" << endl <<
+ " ThriftStructClass parent;" << endl <<
+ "};" << endl <<
+ "typedef struct _" << this->nspace << name << "Class " << this->nspace << name << "Class;" << endl <<
+ endl;
// write the standard GObject boilerplate
- f_types_ << "GType " << this->nspace_lc << name_u << "_get_type (void);" << endl << "#define "
- << this->nspace_uc << "TYPE_" << name_uc << " (" << this->nspace_lc << name_u
- << "_get_type())" << endl << "#define " << this->nspace_uc << name_uc
- << "(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_" << name_uc
- << ", " << this->nspace << name << "))" << endl << "#define " << this->nspace_uc
- << name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " << this->nspace_uc << "_TYPE_"
- << name_uc << ", " << this->nspace << name << "Class))" << endl << "#define "
- << this->nspace_uc << "IS_" << name_uc << "(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), "
- << this->nspace_uc << "TYPE_" << name_uc << "))" << endl << "#define " << this->nspace_uc
- << "IS_" << name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), " << this->nspace_uc
- << "TYPE_" << name_uc << "))" << endl << "#define " << this->nspace_uc << name_uc
- << "_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), " << this->nspace_uc << "TYPE_"
- << name_uc << ", " << this->nspace << name << "Class))" << endl << endl;
+ f_types_ <<
+ "GType " << this->nspace_lc << name_u << "_get_type (void);" << endl <<
+ "#define " << this->nspace_uc << "TYPE_" << name_uc << " (" << this->nspace_lc << name_u << "_get_type())" << endl <<
+ "#define " << this->nspace_uc << name_uc << "(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), " << this->nspace_uc << "TYPE_" << name_uc << ", " << this->nspace << name << "))" << endl <<
+ "#define " << this->nspace_uc << name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), " << this->nspace_uc << "_TYPE_" << name_uc << ", " << this->nspace << name << "Class))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << name_uc << "(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), " << this->nspace_uc << "TYPE_" << name_uc << "))" << endl <<
+ "#define " << this->nspace_uc << "IS_" << name_uc << "_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), " << this->nspace_uc << "TYPE_" << name_uc << "))" << endl <<
+ "#define " << this->nspace_uc << name_uc << "_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), " << this->nspace_uc << "TYPE_" << name_uc << ", " << this->nspace << name << "Class))" << endl <<
+ endl;
// start writing the object implementation .c file
// generate properties enum
if (members.size() > 0) {
- f_types_impl_ << "enum _" << class_name << "Properties" << endl << "{" << endl;
+ f_types_impl_ <<
+ "enum _" << class_name << "Properties" << endl <<
+ "{" << endl;
indent_up();
- f_types_impl_ << indent() << "PROP_" << class_name_uc << "_0";
+ f_types_impl_ <<
+ indent() << "PROP_" << class_name_uc << "_0";
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- string member_name_uc
- = to_upper_case(to_lower_case(initial_caps_to_underscores((*m_iter)->get_name())));
+ string member_name_uc =
+ to_upper_case(to_lower_case
+ (initial_caps_to_underscores((*m_iter)->get_name())));
- f_types_impl_ << "," << endl << indent() << "PROP_" << class_name_uc << "_" << member_name_uc;
+ f_types_impl_ << "," << endl <<
+ indent() << "PROP_" << class_name_uc << "_" << member_name_uc;
}
f_types_impl_ << endl;
indent_down();
- f_types_impl_ << "};" << endl << endl;
+ f_types_impl_ <<
+ "};" << endl <<
+ endl;
}
// generate struct I/O methods
- string this_get = this->nspace + name + " * this_object = " + this->nspace_uc + name_uc
- + "(object);";
- generate_struct_reader(f_types_impl_, tstruct, "this_object->", this_get);
- generate_struct_writer(f_types_impl_, tstruct, "this_object->", this_get);
+ string this_get = this->nspace + name + " * this_object = "
+ + this->nspace_uc + name_uc + "(object);";
+ generate_struct_reader (f_types_impl_, tstruct, "this_object->", this_get);
+ generate_struct_writer (f_types_impl_, tstruct, "this_object->", this_get);
// generate property setter and getter
if (members.size() > 0) {
// generate property setter
function_name = class_name_lc + "_set_property";
args_indent = string(function_name.length() + 2, ' ');
- f_types_impl_ << "static void" << endl << function_name << " (GObject *object," << endl
- << args_indent << "guint property_id," << endl << args_indent
- << "const GValue *value," << endl << args_indent << "GParamSpec *pspec)" << endl;
+ f_types_impl_ <<
+ "static void" << endl <<
+ function_name << " (GObject *object," << endl <<
+ args_indent << "guint property_id," << endl <<
+ args_indent << "const GValue *value," << endl <<
+ args_indent << "GParamSpec *pspec)" << endl;
scope_up(f_types_impl_);
- f_types_impl_ << indent() << class_name << " *self = " << class_name_uc << " (object);" << endl
- << endl << indent() << "switch (property_id)" << endl;
+ f_types_impl_ <<
+ indent() << class_name << " *self = " <<
+ class_name_uc << " (object);" << endl <<
+ endl <<
+ indent() << "switch (property_id)" << endl;
scope_up(f_types_impl_);
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_field* member = (*m_iter);
+ t_field *member = (*m_iter);
string member_name = member->get_name();
- string member_name_uc
- = to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
- t_type* member_type = get_true_type(member->get_type());
+ string member_name_uc =
+ to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
+ t_type *member_type = get_true_type(member->get_type());
- string property_identifier = "PROP_" + class_name_uc + "_" + member_name_uc;
+ string property_identifier =
+ "PROP_" + class_name_uc + "_" + member_name_uc;
- f_types_impl_ << indent() << "case " << property_identifier + ":" << endl;
+ f_types_impl_ <<
+ indent() << "case " << property_identifier + ":" << endl;
indent_up();
if (member_type->is_base_type()) {
- t_base_type* base_type = ((t_base_type*)member_type);
+ t_base_type *base_type = ((t_base_type *) member_type);
string assign_function_name;
if (base_type->get_base() == t_base_type::TYPE_STRING) {
string release_function_name;
- f_types_impl_ << indent() << "if (self->" << member_name << " != NULL)" << endl;
+ f_types_impl_ <<
+ indent() << "if (self->" << member_name << " != NULL)" << endl;
indent_up();
if (base_type->is_binary()) {
@@ -2615,8 +3167,9 @@
assign_function_name = "g_value_dup_string";
}
- f_types_impl_ << indent() << release_function_name << " (self->" << member_name << ");"
- << endl;
+ f_types_impl_ <<
+ indent() << release_function_name <<
+ " (self->" << member_name << ");" << endl;
indent_down();
} else {
switch (base_type->get_base()) {
@@ -2634,30 +3187,31 @@
assign_function_name = "g_value_get_int64";
break;
- case t_base_type::TYPE_DOUBLE:
+ case t_base_type::TYPE_DOUBLE :
assign_function_name = "g_value_get_double";
break;
default:
throw "compiler error: "
- "unrecognized base type \"" + base_type->get_name() + "\" "
- "for struct member \""
- + member_name + "\"";
+ "unrecognized base type \"" + base_type->get_name() + "\" "
+ "for struct member \"" + member_name + "\"";
break;
}
}
- f_types_impl_ << indent() << "self->" << member_name << " = " << assign_function_name
- << " (value);" << endl;
+ f_types_impl_ <<
+ indent() << "self->" << member_name <<
+ " = " << assign_function_name << " (value);" << endl;
} else if (member_type->is_enum()) {
- f_types_impl_ << indent() << "self->" << member_name << " = g_value_get_int (value);"
- << endl;
+ f_types_impl_ <<
+ indent() << "self->" << member_name <<
+ " = g_value_get_int (value);" << endl;
} else if (member_type->is_container()) {
string release_function_name;
string assign_function_name;
if (member_type->is_list()) {
- t_type* elem_type = ((t_list*)member_type)->get_elem_type();
+ t_type *elem_type = ((t_list *) member_type)->get_elem_type();
// Lists of base types other than strings are represented as GArrays;
// all others as GPtrArrays
@@ -2673,33 +3227,46 @@
assign_function_name = "g_value_dup_boxed";
}
- f_types_impl_ << indent() << "if (self->" << member_name << " != NULL)" << endl;
+ f_types_impl_ <<
+ indent() << "if (self->" << member_name << " != NULL)" << endl;
indent_up();
- f_types_impl_ << indent() << release_function_name << " (self->" << member_name << ");"
- << endl;
+ f_types_impl_ <<
+ indent() << release_function_name << " (self->" << member_name <<
+ ");" << endl;
indent_down();
- f_types_impl_ << indent() << "self->" << member_name << " = " << assign_function_name
- << " (value);" << endl;
+ f_types_impl_ <<
+ indent() << "self->" << member_name << " = " <<
+ assign_function_name << " (value);" << endl;
} else if (member_type->is_struct() || member_type->is_xception()) {
- f_types_impl_ << indent() << "if (self->" << member_name << " != NULL)" << endl;
+ f_types_impl_ <<
+ indent() << "if (self->" << member_name << " != NULL)" << endl;
indent_up();
- f_types_impl_ << indent() << "g_object_unref (self->" << member_name << ");" << endl;
+ f_types_impl_ <<
+ indent() << "g_object_unref (self->" << member_name <<
+ ");" << endl;
indent_down();
- f_types_impl_ << indent() << "self->" << member_name << " = g_value_dup_object (value);"
- << endl;
+ f_types_impl_ <<
+ indent() << "self->" << member_name <<
+ " = g_value_dup_object (value);" << endl;
}
if (member->get_req() != t_field::T_REQUIRED) {
- f_types_impl_ << indent() << "self->__isset_" << member_name << " = TRUE;" << endl;
+ f_types_impl_ <<
+ indent() << "self->__isset_" << member_name << " = TRUE;" << endl;
}
- f_types_impl_ << indent() << "break;" << endl << endl;
+ f_types_impl_ <<
+ indent() << "break;" << endl <<
+ endl;
indent_down();
}
- f_types_impl_ << indent() << "default:" << endl;
+ f_types_impl_ <<
+ indent() << "default:" << endl;
indent_up();
- f_types_impl_ << indent() << "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);"
- << endl << indent() << "break;" << endl;
+ f_types_impl_ <<
+ indent() <<
+ "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);" << endl <<
+ indent() << "break;" << endl;
indent_down();
scope_down(f_types_impl_);
scope_down(f_types_impl_);
@@ -2708,26 +3275,33 @@
// generate property getter
function_name = class_name_lc + "_get_property";
args_indent = string(function_name.length() + 2, ' ');
- f_types_impl_ << "static void" << endl << function_name << " (GObject *object," << endl
- << args_indent << "guint property_id," << endl << args_indent << "GValue *value,"
- << endl << args_indent << "GParamSpec *pspec)" << endl;
+ f_types_impl_ <<
+ "static void" << endl <<
+ function_name << " (GObject *object," << endl <<
+ args_indent << "guint property_id," << endl <<
+ args_indent << "GValue *value," << endl <<
+ args_indent << "GParamSpec *pspec)" << endl;
scope_up(f_types_impl_);
- f_types_impl_ << indent() << class_name << " *self = " << class_name_uc << " (object);" << endl
- << endl << indent() << "switch (property_id)" << endl;
+ f_types_impl_ <<
+ indent() << class_name << " *self = " <<
+ class_name_uc << " (object);" << endl <<
+ endl <<
+ indent() << "switch (property_id)" << endl;
scope_up(f_types_impl_);
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_field* member = (*m_iter);
+ t_field *member = (*m_iter);
string member_name = (*m_iter)->get_name();
- string member_name_uc
- = to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
- t_type* member_type = get_true_type(member->get_type());
+ string member_name_uc =
+ to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
+ t_type *member_type = get_true_type(member->get_type());
- string property_identifier = "PROP_" + class_name_uc + "_" + member_name_uc;
+ string property_identifier =
+ "PROP_" + class_name_uc + "_" + member_name_uc;
string setter_function_name;
if (member_type->is_base_type()) {
- t_base_type* base_type = ((t_base_type*)member_type);
+ t_base_type *base_type = ((t_base_type *) member_type);
switch (base_type->get_base()) {
case t_base_type::TYPE_BOOL:
@@ -2744,7 +3318,7 @@
setter_function_name = "g_value_set_int64";
break;
- case t_base_type::TYPE_DOUBLE:
+ case t_base_type::TYPE_DOUBLE :
setter_function_name = "g_value_set_double";
break;
@@ -2758,9 +3332,8 @@
default:
throw "compiler error: "
- "unrecognized base type \"" + base_type->get_name() + "\" "
- "for struct member \""
- + member_name + "\"";
+ "unrecognized base type \"" + base_type->get_name() + "\" "
+ "for struct member \"" + member_name + "\"";
break;
}
} else if (member_type->is_enum()) {
@@ -2771,19 +3344,26 @@
setter_function_name = "g_value_set_boxed";
} else {
throw "compiler error: "
- "unrecognized type for struct member \"" + member_name + "\"";
+ "unrecognized type for struct member \"" + member_name + "\"";
}
- f_types_impl_ << indent() << "case " << property_identifier + ":" << endl;
+ f_types_impl_ <<
+ indent() << "case " << property_identifier + ":" << endl;
indent_up();
- f_types_impl_ << indent() << setter_function_name << " (value, self->" << member_name << ");"
- << endl << indent() << "break;" << endl << endl;
+ f_types_impl_ <<
+ indent() << setter_function_name << " (value, self->" <<
+ member_name << ");" << endl <<
+ indent() << "break;" << endl <<
+ endl;
indent_down();
}
- f_types_impl_ << indent() << "default:" << endl;
+ f_types_impl_ <<
+ indent() << "default:" << endl;
indent_up();
- f_types_impl_ << indent() << "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);"
- << endl << indent() << "break;" << endl;
+ f_types_impl_ <<
+ indent() <<
+ "G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);" << endl <<
+ indent() << "break;" << endl;
indent_down();
scope_down(f_types_impl_);
scope_down(f_types_impl_);
@@ -2792,29 +3372,33 @@
// generate the instance init function
- f_types_impl_ << "static void " << endl << this->nspace_lc << name_u << "_instance_init ("
- << this->nspace << name << " * object)" << endl << "{" << endl;
+ f_types_impl_ <<
+ "static void " << endl <<
+ this->nspace_lc << name_u << "_instance_init (" << this->nspace << name << " * object)" << endl <<
+ "{" << endl;
indent_up();
// generate default-value structures for container-type members
bool constant_declaration_output = false;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_field* member = *m_iter;
- t_const_value* member_value = member->get_value();
+ t_field *member = *m_iter;
+ t_const_value *member_value = member->get_value();
if (member_value != NULL) {
string member_name = member->get_name();
- t_type* member_type = get_true_type(member->get_type());
+ t_type* member_type = get_true_type (member->get_type());
if (member_type->is_list()) {
- const vector<t_const_value*>& list = member_value->get_list();
- t_type* elem_type = ((t_list*)member_type)->get_elem_type();
+ const vector<t_const_value *> &list = member_value->get_list();
+ t_type *elem_type = ((t_list *) member_type)->get_elem_type();
// Generate an array with the list literal
- indent(f_types_impl_) << "static " << type_name(elem_type, false, true) << " __default_"
- << member_name << "[" << list.size() << "] = " << endl;
+ indent(f_types_impl_) <<
+ "static " << type_name(elem_type, false, true) <<
+ " __default_" << member_name << "[" << list.size() << "] = " << endl;
indent_up();
- f_types_impl_ << indent() << constant_literal(member_type, member_value) << ";" << endl;
+ f_types_impl_ <<
+ indent() << constant_literal (member_type, member_value) << ";" << endl;
indent_down();
constant_declaration_output = true;
@@ -2828,29 +3412,28 @@
}
// satisfy compilers with -Wall turned on
- indent(f_types_impl_) << "/* satisfy -Wall */" << endl << indent()
- << "THRIFT_UNUSED_VAR (object);" << endl;
+ indent(f_types_impl_) << "/* satisfy -Wall */" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (object);" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
+ t_type* t = get_true_type ((*m_iter)->get_type());
if (t->is_base_type()) {
string dval = " = ";
if (t->is_enum()) {
- dval += "(" + type_name(t) + ")";
+ dval += "(" + type_name (t) + ")";
}
t_const_value* cv = (*m_iter)->get_value();
if (cv != NULL) {
- dval += constant_value("", t, cv);
+ dval += constant_value ("", t, cv);
} else {
dval += t->is_string() ? "NULL" : "0";
}
indent(f_types_impl_) << "object->" << (*m_iter)->get_name() << dval << ";" << endl;
} else if (t->is_struct()) {
string name = (*m_iter)->get_name();
- string type_name_uc
- = to_upper_case(initial_caps_to_underscores((*m_iter)->get_type()->get_name()));
- indent(f_types_impl_) << "object->" << name << " = g_object_new (" << this->nspace_uc
- << "TYPE_" << type_name_uc << ", NULL);" << endl;
+ string type_name_uc = to_upper_case
+ (initial_caps_to_underscores((*m_iter)->get_type()->get_name()));
+ indent(f_types_impl_) << "object->" << name << " = g_object_new (" << this->nspace_uc << "TYPE_" << type_name_uc << ", NULL);" << endl;
} else if (t->is_xception()) {
string name = (*m_iter)->get_name();
indent(f_types_impl_) << "object->" << name << " = NULL;" << endl;
@@ -2859,28 +3442,31 @@
string init_function;
if (t->is_map()) {
- t_type* key = ((t_map*)t)->get_key_type();
- t_type* value = ((t_map*)t)->get_val_type();
- init_function = generate_new_hash_from_type(key, value);
+ t_type *key = ((t_map *) t)->get_key_type();
+ t_type *value = ((t_map *) t)->get_val_type();
+ init_function = generate_new_hash_from_type (key, value);
} else if (t->is_set()) {
- t_type* etype = ((t_set*)t)->get_elem_type();
- init_function = generate_new_hash_from_type(etype, NULL);
+ t_type *etype = ((t_set *) t)->get_elem_type();
+ init_function = generate_new_hash_from_type (etype, NULL);
} else if (t->is_list()) {
- t_type* etype = ((t_list*)t)->get_elem_type();
- init_function = generate_new_array_from_type(etype);
+ t_type *etype = ((t_list *) t)->get_elem_type();
+ init_function = generate_new_array_from_type (etype);
}
- indent(f_types_impl_) << "object->" << name << " = " << init_function << endl;
+ indent(f_types_impl_) << "object->" << name << " = " <<
+ init_function << endl;
// Pre-populate the container with the specified default values, if any
if ((*m_iter)->get_value()) {
- t_const_value* member_value = (*m_iter)->get_value();
+ t_const_value *member_value = (*m_iter)->get_value();
if (t->is_list()) {
- const vector<t_const_value*>& list = member_value->get_list();
+ const vector<t_const_value *> &list = member_value->get_list();
- indent(f_types_impl_) << "g_array_append_vals (object->" << name << ", &__default_"
- << name << ", " << list.size() << ");" << endl;
+ indent(f_types_impl_) <<
+ "g_array_append_vals (object->" << name <<
+ ", &__default_" << name <<
+ ", " << list.size() << ");" << endl;
}
// TODO: Handle container types other than list
@@ -2894,59 +3480,68 @@
}
indent_down();
- f_types_impl_ << "}" << endl << endl;
+ f_types_impl_ << "}" << endl <<
+ endl;
/* create the destructor */
- f_types_impl_ << "static void " << endl << this->nspace_lc << name_u
- << "_finalize (GObject *object)" << endl << "{" << endl;
+ f_types_impl_ <<
+ "static void " << endl <<
+ this->nspace_lc << name_u << "_finalize (GObject *object)" << endl <<
+ "{" << endl;
indent_up();
- f_types_impl_ << indent() << this->nspace << name << " *tobject = " << this->nspace_uc << name_uc
- << " (object);" << endl << endl;
+ f_types_impl_ <<
+ indent() <<
+ this->nspace << name << " *tobject = " << this->nspace_uc <<
+ name_uc << " (object);" << endl << endl;
- f_types_impl_ << indent() << "/* satisfy -Wall in case we don't use tobject */" << endl
- << indent() << "THRIFT_UNUSED_VAR (tobject);" << endl;
+ f_types_impl_ <<
+ indent() << "/* satisfy -Wall in case we don't use tobject */" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (tobject);" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
+ t_type* t = get_true_type ((*m_iter)->get_type());
if (t->is_container()) {
string name = (*m_iter)->get_name();
if (t->is_map() || t->is_set()) {
f_types_impl_ << indent() << "if (tobject->" << name << " != NULL)" << endl;
f_types_impl_ << indent() << "{" << endl;
indent_up();
- f_types_impl_ << indent() << "g_hash_table_destroy (tobject->" << name << ");" << endl;
+ f_types_impl_ <<
+ indent() << "g_hash_table_destroy (tobject->" << name << ");" << endl;
f_types_impl_ << indent() << "tobject->" << name << " = NULL;" << endl;
indent_down();
f_types_impl_ << indent() << "}" << endl;
} else if (t->is_list()) {
- t_type* etype = ((t_list*)t)->get_elem_type();
+ t_type *etype = ((t_list *) t)->get_elem_type();
string destructor_function = "g_ptr_array_unref";
if (etype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)etype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) etype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- destructor_function = "g_array_unref";
- break;
- case t_base_type::TYPE_STRING:
- break;
- default:
- throw "compiler error: no array info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ destructor_function = "g_array_unref";
+ break;
+ case t_base_type::TYPE_STRING:
+ break;
+ default:
+ throw "compiler error: no array info for type";
}
}
f_types_impl_ << indent() << "if (tobject->" << name << " != NULL)" << endl;
f_types_impl_ << indent() << "{" << endl;
indent_up();
- f_types_impl_ << indent() << destructor_function << " (tobject->" << name << ");" << endl;
+ f_types_impl_ <<
+ indent() << destructor_function << " (tobject->" << name <<
+ ");" << endl;
f_types_impl_ << indent() << "tobject->" << name << " = NULL;" << endl;
indent_down();
f_types_impl_ << indent() << "}" << endl;
@@ -2959,7 +3554,8 @@
f_types_impl_ << indent() << "if (tobject->" << name << " != NULL)" << endl;
f_types_impl_ << indent() << "{" << endl;
indent_up();
- f_types_impl_ << indent() << "g_object_unref(tobject->" << name << ");" << endl;
+ f_types_impl_ <<
+ indent() << "g_object_unref(tobject->" << name << ");" << endl;
f_types_impl_ << indent() << "tobject->" << name << " = NULL;" << endl;
indent_down();
f_types_impl_ << indent() << "}" << endl;
@@ -2968,8 +3564,8 @@
f_types_impl_ << indent() << "if (tobject->" << name << " != NULL)" << endl;
f_types_impl_ << indent() << "{" << endl;
indent_up();
- f_types_impl_ << indent() << generate_free_func_from_type(t) << "(tobject->" << name << ");"
- << endl;
+ f_types_impl_ <<
+ indent() << generate_free_func_from_type(t) << "(tobject->" << name << ");" << endl;
f_types_impl_ << indent() << "tobject->" << name << " = NULL;" << endl;
indent_down();
f_types_impl_ << indent() << "}" << endl;
@@ -2977,73 +3573,96 @@
}
indent_down();
- f_types_impl_ << "}" << endl << endl;
+ f_types_impl_ <<
+ "}" << endl <<
+ endl;
// generate the class init function
- f_types_impl_ << "static void" << endl << class_name_lc << "_class_init (" << class_name
- << "Class * cls)" << endl;
+ f_types_impl_ <<
+ "static void" << endl <<
+ class_name_lc << "_class_init (" << class_name << "Class * cls)" << endl;
scope_up(f_types_impl_);
- f_types_impl_ << indent() << "GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl
- << indent() << "ThriftStructClass *struct_class = "
- << "THRIFT_STRUCT_CLASS (cls);" << endl << endl << indent()
- << "struct_class->read = " << class_name_lc << "_read;" << endl << indent()
- << "struct_class->write = " << class_name_lc << "_write;" << endl << endl
- << indent() << "gobject_class->finalize = " << class_name_lc << "_finalize;"
- << endl;
+ f_types_impl_ <<
+ indent() << "GObjectClass *gobject_class = G_OBJECT_CLASS (cls);" << endl <<
+ indent() << "ThriftStructClass *struct_class = " <<
+ "THRIFT_STRUCT_CLASS (cls);" << endl <<
+ endl <<
+ indent() << "struct_class->read = " << class_name_lc << "_read;" << endl <<
+ indent() << "struct_class->write = " << class_name_lc << "_write;" << endl <<
+ endl <<
+ indent() << "gobject_class->finalize = " << class_name_lc << "_finalize;" << endl;
if (members.size() > 0) {
- f_types_impl_ << indent() << "gobject_class->get_property = " << class_name_lc
- << "_get_property;" << endl << indent()
- << "gobject_class->set_property = " << class_name_lc << "_set_property;" << endl;
+ f_types_impl_ <<
+ indent() << "gobject_class->get_property = " <<
+ class_name_lc << "_get_property;" << endl <<
+ indent() << "gobject_class->set_property = " <<
+ class_name_lc << "_set_property;" << endl;
// install a property for each member
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_field* member = (*m_iter);
+ t_field *member = (*m_iter);
string member_name = member->get_name();
- string member_name_uc
- = to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
- t_type* member_type = get_true_type(member->get_type());
- t_const_value* member_value = member->get_value();
+ string member_name_uc =
+ to_upper_case(to_lower_case(initial_caps_to_underscores(member_name)));
+ t_type *member_type = get_true_type(member->get_type());
+ t_const_value *member_value = member->get_value();
- string property_identifier = "PROP_" + class_name_uc + "_" + member_name_uc;
+ string property_identifier =
+ "PROP_" + class_name_uc + "_" + member_name_uc;
- f_types_impl_ << endl << indent() << "g_object_class_install_property" << endl;
+ f_types_impl_ << endl <<
+ indent() << "g_object_class_install_property" << endl;
indent_up();
args_indent = indent() + ' ';
- f_types_impl_ << indent() << "(gobject_class," << endl << args_indent << property_identifier
- << "," << endl << args_indent;
+ f_types_impl_ <<
+ indent() << "(gobject_class," << endl <<
+ args_indent << property_identifier << "," << endl <<
+ args_indent;
if (member_type->is_base_type()) {
- t_base_type::t_base base_type = ((t_base_type*)member_type)->get_base();
+ t_base_type::t_base base_type =
+ ((t_base_type *) member_type)->get_base();
if (base_type == t_base_type::TYPE_STRING) {
- if (((t_base_type*)member_type)->is_binary()) {
+ if (((t_base_type *) member_type)->is_binary()) {
args_indent += string(20, ' ');
- f_types_impl_ << "g_param_spec_boxed (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << "G_TYPE_BYTE_ARRAY," << endl << args_indent << "G_PARAM_READWRITE));"
- << endl;
+ f_types_impl_ <<
+ "g_param_spec_boxed (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "G_TYPE_BYTE_ARRAY," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
} else {
args_indent += string(21, ' ');
- f_types_impl_ << "g_param_spec_string (\"" << member_name << "\"," << endl
- << args_indent << "NULL," << endl << args_indent << "NULL," << endl
- << args_indent
- << ((member_value != NULL) ? "\"" + member_value->get_string() + "\""
- : "NULL") << "," << endl << args_indent
- << "G_PARAM_READWRITE));" << endl;
+ f_types_impl_ <<
+ "g_param_spec_string (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent <<
+ ((member_value != NULL) ?
+ "\"" + member_value->get_string() + "\"" :
+ "NULL") <<
+ "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
}
} else if (base_type == t_base_type::TYPE_BOOL) {
args_indent += string(22, ' ');
- f_types_impl_ << "g_param_spec_boolean (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << (((member_value != NULL) && (member_value->get_integer() != 0))
- ? "TRUE"
- : "FALSE") << "," << endl << args_indent << "G_PARAM_READWRITE));"
- << endl;
- } else if ((base_type == t_base_type::TYPE_BYTE) || (base_type == t_base_type::TYPE_I16)
- || (base_type == t_base_type::TYPE_I32) || (base_type == t_base_type::TYPE_I64)
- || (base_type == t_base_type::TYPE_DOUBLE)) {
+ f_types_impl_ <<
+ "g_param_spec_boolean (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent <<
+ (((member_value != NULL) &&
+ (member_value->get_integer() != 0)) ? "TRUE" : "FALSE") <<
+ "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
+ } else if ((base_type == t_base_type::TYPE_BYTE) ||
+ (base_type == t_base_type::TYPE_I16) ||
+ (base_type == t_base_type::TYPE_I32) ||
+ (base_type == t_base_type::TYPE_I64) ||
+ (base_type == t_base_type::TYPE_DOUBLE)) {
string param_spec_function_name = "g_param_spec_int";
string min_value;
string max_value;
@@ -3079,52 +3698,68 @@
default:
throw "compiler error: "
- "unrecognized base type \"" + member_type->get_name() + "\" "
- "for struct member \""
- + member_name + "\"";
+ "unrecognized base type \"" + member_type->get_name() + "\" "
+ "for struct member \"" + member_name + "\"";
break;
}
if (member_value != NULL) {
- default_value << (base_type == t_base_type::TYPE_DOUBLE ? member_value->get_double()
- : member_value->get_integer());
+ default_value <<
+ (base_type == t_base_type::TYPE_DOUBLE ?
+ member_value->get_double() :
+ member_value->get_integer());
} else {
default_value << "0";
}
args_indent += string(param_spec_function_name.length() + 2, ' ');
- f_types_impl_ << param_spec_function_name << " (\"" << member_name << "\"," << endl
- << args_indent << "NULL," << endl << args_indent << "NULL," << endl
- << args_indent << min_value << "," << endl << args_indent << max_value
- << "," << endl << args_indent << default_value.str() << "," << endl
- << args_indent << "G_PARAM_READWRITE));" << endl;
+ f_types_impl_ <<
+ param_spec_function_name << " (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << min_value << "," << endl <<
+ args_indent << max_value << "," << endl <<
+ args_indent << default_value.str() << "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
}
indent_down();
} else if (member_type->is_enum()) {
- t_enum_value* enum_min_value = ((t_enum*)member_type)->get_min_value();
- t_enum_value* enum_max_value = ((t_enum*)member_type)->get_max_value();
- int min_value = (enum_min_value != NULL) ? enum_min_value->get_value() : 0;
- int max_value = (enum_max_value != NULL) ? enum_max_value->get_value() : 0;
+ t_enum_value *enum_min_value =
+ ((t_enum *) member_type)->get_min_value();
+ t_enum_value *enum_max_value =
+ ((t_enum *) member_type)->get_max_value();
+ int min_value =
+ (enum_min_value != NULL) ? enum_min_value->get_value() : 0;
+ int max_value =
+ (enum_max_value != NULL) ? enum_max_value->get_value() : 0;
args_indent += string(18, ' ');
- f_types_impl_ << "g_param_spec_int (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << min_value << "," << endl << args_indent << max_value << "," << endl
- << args_indent << min_value << "," << endl << args_indent
- << "G_PARAM_READWRITE));" << endl;
+ f_types_impl_ <<
+ "g_param_spec_int (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << min_value << "," << endl <<
+ args_indent << max_value << "," << endl <<
+ args_indent << min_value << "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
indent_down();
} else if (member_type->is_struct() || member_type->is_xception()) {
- string param_type = this->nspace_uc + "TYPE_"
- + to_upper_case(initial_caps_to_underscores(member_type->get_name()));
+ string param_type =
+ this->nspace_uc +
+ "TYPE_" +
+ to_upper_case(initial_caps_to_underscores(member_type->get_name()));
args_indent += string(20, ' ');
- f_types_impl_ << "g_param_spec_object (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << param_type << "," << endl << args_indent << "G_PARAM_READWRITE));" << endl;
+ f_types_impl_ <<
+ "g_param_spec_object (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << param_type << "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
indent_down();
} else if (member_type->is_list()) {
- t_type* elem_type = ((t_list*)member_type)->get_elem_type();
+ t_type *elem_type = ((t_list *) member_type)->get_elem_type();
string param_type;
if (elem_type->is_base_type() && !elem_type->is_string()) {
@@ -3134,16 +3769,21 @@
}
args_indent += string(20, ' ');
- f_types_impl_ << "g_param_spec_boxed (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << param_type << "," << endl << args_indent << "G_PARAM_READWRITE));" << endl;
+ f_types_impl_ <<
+ "g_param_spec_boxed (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << param_type << "," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
indent_down();
} else if (member_type->is_set() || member_type->is_map()) {
args_indent += string(20, ' ');
- f_types_impl_ << "g_param_spec_boxed (\"" << member_name << "\"," << endl << args_indent
- << "NULL," << endl << args_indent << "NULL," << endl << args_indent
- << "G_TYPE_HASH_TABLE," << endl << args_indent << "G_PARAM_READWRITE));"
- << endl;
+ f_types_impl_ <<
+ "g_param_spec_boxed (\"" << member_name << "\"," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "NULL," << endl <<
+ args_indent << "G_TYPE_HASH_TABLE," << endl <<
+ args_indent << "G_PARAM_READWRITE));" << endl;
indent_down();
}
}
@@ -3151,49 +3791,68 @@
scope_down(f_types_impl_);
f_types_impl_ << endl;
- f_types_impl_ << "GType" << endl << this->nspace_lc << name_u << "_get_type (void)" << endl << "{"
- << endl << " static GType type = 0;" << endl << endl << " if (type == 0) " << endl
- << " {" << endl << " static const GTypeInfo type_info = " << endl << " {"
- << endl << " sizeof (" << this->nspace << name << "Class)," << endl
- << " NULL, /* base_init */" << endl << " NULL, /* base_finalize */"
- << endl << " (GClassInitFunc) " << this->nspace_lc << name_u << "_class_init,"
- << endl << " NULL, /* class_finalize */" << endl
- << " NULL, /* class_data */" << endl << " sizeof (" << this->nspace
- << name << ")," << endl << " 0, /* n_preallocs */" << endl
- << " (GInstanceInitFunc) " << this->nspace_lc << name_u << "_instance_init,"
- << endl << " NULL, /* value_table */" << endl << " };" << endl << endl
- << " type = g_type_register_static (THRIFT_TYPE_STRUCT, " << endl
- << " \"" << this->nspace << name << "Type\","
- << endl << " &type_info, 0);" << endl << " }"
- << endl << endl << " return type;" << endl << "}" << endl << endl;
+ f_types_impl_ <<
+ "GType" << endl <<
+ this->nspace_lc << name_u << "_get_type (void)" << endl <<
+ "{" << endl <<
+ " static GType type = 0;" << endl <<
+ endl <<
+ " if (type == 0) " << endl <<
+ " {" << endl <<
+ " static const GTypeInfo type_info = " << endl <<
+ " {" << endl <<
+ " sizeof (" << this->nspace << name << "Class)," << endl <<
+ " NULL, /* base_init */" << endl <<
+ " NULL, /* base_finalize */" << endl <<
+ " (GClassInitFunc) " << this->nspace_lc << name_u << "_class_init," << endl <<
+ " NULL, /* class_finalize */" << endl <<
+ " NULL, /* class_data */" << endl <<
+ " sizeof (" << this->nspace << name << ")," << endl <<
+ " 0, /* n_preallocs */" << endl <<
+ " (GInstanceInitFunc) " << this->nspace_lc << name_u << "_instance_init," << endl <<
+ " NULL, /* value_table */" << endl <<
+ " };" << endl <<
+ endl <<
+ " type = g_type_register_static (THRIFT_TYPE_STRUCT, " << endl <<
+ " \"" << this->nspace << name << "Type\"," << endl <<
+ " &type_info, 0);" << endl <<
+ " }" << endl <<
+ endl <<
+ " return type;" << endl <<
+ "}" << endl <<
+ endl;
}
/**
* Generates functions to write Thrift structures to a stream.
*/
-void t_c_glib_generator::generate_struct_writer(ofstream& out,
- t_struct* tstruct,
- string this_name,
- string this_get,
- bool is_function) {
+void t_c_glib_generator::generate_struct_writer (ofstream &out,
+ t_struct *tstruct,
+ string this_name,
+ string this_get,
+ bool is_function) {
string name = tstruct->get_name();
string name_u = initial_caps_to_underscores(name);
string name_uc = to_upper_case(name_u);
- const vector<t_field*>& fields = tstruct->get_members();
- vector<t_field*>::const_iterator f_iter;
+ const vector<t_field *> &fields = tstruct->get_members();
+ vector <t_field *>::const_iterator f_iter;
int error_ret = 0;
if (is_function) {
error_ret = -1;
- indent(out) << "static gint32" << endl << this->nspace_lc << name_u
- << "_write (ThriftStruct *object, ThriftProtocol *protocol, GError **error)"
- << endl;
+ indent(out) <<
+ "static gint32" << endl <<
+ this->nspace_lc << name_u <<
+ "_write (ThriftStruct *object, ThriftProtocol *protocol, GError **error)" << endl;
}
indent(out) << "{" << endl;
indent_up();
- out << indent() << "gint32 ret;" << endl << indent() << "gint32 xfer = 0;" << endl << endl;
+ out <<
+ indent() << "gint32 ret;" << endl <<
+ indent() << "gint32 xfer = 0;" << endl <<
+ endl;
indent(out) << this_get << endl;
// satisfy -Wall in the case of an empty struct
@@ -3201,9 +3860,10 @@
indent(out) << "THRIFT_UNUSED_VAR (this_object);" << endl;
}
- out << indent() << "if ((ret = thrift_protocol_write_struct_begin (protocol, \"" << name
- << "\", error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl
- << indent() << "xfer += ret;" << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_struct_begin (protocol, \"" << name << "\", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
@@ -3211,14 +3871,18 @@
indent_up();
}
- out << indent() << "if ((ret = thrift_protocol_write_field_begin (protocol, "
- << "\"" << (*f_iter)->get_name() << "\", " << type_to_enum((*f_iter)->get_type()) << ", "
- << (*f_iter)->get_key() << ", error)) < 0)" << endl << indent() << " return " << error_ret
- << ";" << endl << indent() << "xfer += ret;" << endl;
- generate_serialize_field(out, *f_iter, this_name, "", error_ret);
- out << indent() << "if ((ret = thrift_protocol_write_field_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_field_begin (protocol, " <<
+ "\"" << (*f_iter)->get_name() << "\", " <<
+ type_to_enum ((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
+ generate_serialize_field (out, *f_iter, this_name, "", error_ret);
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_field_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
if ((*f_iter)->get_req() == t_field::T_OPTIONAL) {
indent_down();
@@ -3227,25 +3891,30 @@
}
// write the struct map
- out << indent() << "if ((ret = thrift_protocol_write_field_stop (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << indent() << "if ((ret = thrift_protocol_write_struct_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_field_stop (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << "if ((ret = thrift_protocol_write_struct_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
if (is_function) {
indent(out) << "return xfer;" << endl;
}
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl <<
+ endl;
}
/**
* Generates code to read Thrift structures from a stream.
*/
-void t_c_glib_generator::generate_struct_reader(ofstream& out,
- t_struct* tstruct,
+void t_c_glib_generator::generate_struct_reader(ofstream &out,
+ t_struct *tstruct,
string this_name,
string this_get,
bool is_function) {
@@ -3253,24 +3922,31 @@
string name_u = initial_caps_to_underscores(name);
string name_uc = to_upper_case(name_u);
int error_ret = 0;
- const vector<t_field*>& fields = tstruct->get_members();
- vector<t_field*>::const_iterator f_iter;
+ const vector<t_field *> &fields = tstruct->get_members();
+ vector <t_field *>::const_iterator f_iter;
if (is_function) {
error_ret = -1;
- indent(out) << "/* reads a " << name_u << " object */" << endl << "static gint32" << endl
- << this->nspace_lc << name_u
- << "_read (ThriftStruct *object, ThriftProtocol *protocol, GError **error)" << endl;
+ indent(out) <<
+ "/* reads a " << name_u << " object */" << endl <<
+ "static gint32" << endl <<
+ this->nspace_lc << name_u <<
+ "_read (ThriftStruct *object, ThriftProtocol *protocol, GError **error)" << endl;
}
indent(out) << "{" << endl;
indent_up();
// declare stack temp variables
- out << indent() << "gint32 ret;" << endl << indent() << "gint32 xfer = 0;" << endl << indent()
- << "gchar *name = NULL;" << endl << indent() << "ThriftType ftype;" << endl << indent()
- << "gint16 fid;" << endl << indent() << "guint32 len = 0;" << endl << indent()
- << "gpointer data = NULL;" << endl << indent() << this_get << endl;
+ out <<
+ indent() << "gint32 ret;" << endl <<
+ indent() << "gint32 xfer = 0;" << endl <<
+ indent() << "gchar *name = NULL;" << endl <<
+ indent() << "ThriftType ftype;" << endl <<
+ indent() << "gint16 fid;" << endl <<
+ indent() << "guint32 len = 0;" << endl <<
+ indent() << "gpointer data = NULL;" << endl <<
+ indent() << this_get << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
@@ -3281,8 +3957,10 @@
out << endl;
// satisfy -Wall in case we don't use some variables
- out << indent() << "/* satisfy -Wall in case these aren't used */" << endl << indent()
- << "THRIFT_UNUSED_VAR (len);" << endl << indent() << "THRIFT_UNUSED_VAR (data);" << endl;
+ out <<
+ indent() << "/* satisfy -Wall in case these aren't used */" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (len);" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (data);" << endl;
if (!this_get.empty()) {
out << indent() << "THRIFT_UNUSED_VAR (this_object);" << endl;
@@ -3290,103 +3968,140 @@
out << endl;
// read the beginning of the structure marker
- out << indent() << "/* read the struct begin marker */" << endl << indent()
- << "if ((ret = thrift_protocol_read_struct_begin (protocol, &name, error)) < 0)" << endl
- << indent() << "{" << endl << indent() << " if (name) g_free (name);" << endl << indent()
- << " return " << error_ret << ";" << endl << indent() << "}" << endl << indent()
- << "xfer += ret;" << endl << indent() << "if (name) g_free (name);" << endl << indent()
- << "name = NULL;" << endl << endl;
+ out <<
+ indent() << "/* read the struct begin marker */" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_struct_begin (protocol, &name, error)) < 0)" << endl <<
+ indent() << "{" << endl <<
+ indent() << " if (name) g_free (name);" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "}" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << "if (name) g_free (name);" << endl <<
+ indent() << "name = NULL;" << endl <<
+ endl;
// read the struct fields
- out << indent() << "/* read the struct fields */" << endl << indent() << "while (1)" << endl;
+ out <<
+ indent() << "/* read the struct fields */" << endl <<
+ indent() << "while (1)" << endl;
scope_up(out);
// read beginning field marker
- out << indent() << "/* read the beginning of a field */" << endl << indent()
- << "if ((ret = thrift_protocol_read_field_begin (protocol, &name, &ftype, &fid, error)) < 0)"
- << endl << indent() << "{" << endl << indent() << " if (name) g_free (name);" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "}" << endl << indent()
- << "xfer += ret;" << endl << indent() << "if (name) g_free (name);" << endl << indent()
- << "name = NULL;" << endl << endl;
+ out <<
+ indent() << "/* read the beginning of a field */" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_field_begin (protocol, &name, &ftype, &fid, error)) < 0)" << endl <<
+ indent() << "{" << endl <<
+ indent() << " if (name) g_free (name);" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "}" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << "if (name) g_free (name);" << endl <<
+ indent() << "name = NULL;" << endl <<
+ endl;
// check for field STOP marker
- out << indent() << "/* break if we get a STOP field */" << endl << indent()
- << "if (ftype == T_STOP)" << endl << indent() << "{" << endl << indent() << " break;" << endl
- << indent() << "}" << endl << endl;
+ out <<
+ indent() << "/* break if we get a STOP field */" << endl <<
+ indent() << "if (ftype == T_STOP)" << endl <<
+ indent() << "{" << endl <<
+ indent() << " break;" << endl <<
+ indent() << "}" << endl <<
+ endl;
// switch depending on the field type
- indent(out) << "switch (fid)" << endl;
+ indent(out) <<
+ "switch (fid)" << endl;
// start switch
scope_up(out);
// generate deserialization code for known types
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
indent_up();
- indent(out) << "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ")" << endl;
- indent(out) << "{" << endl;
+ indent(out) <<
+ "if (ftype == " << type_to_enum ((*f_iter)->get_type()) << ")" << endl;
+ indent(out) <<
+ "{" << endl;
+
indent_up();
// generate deserialize field
- generate_deserialize_field(out, *f_iter, this_name, "", error_ret, false);
+ generate_deserialize_field (out, *f_iter, this_name, "", error_ret, false);
indent_down();
- out << indent() << "} else {" << endl << indent()
- << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl << indent()
- << " return " << error_ret << ";" << endl << indent() << " xfer += ret;" << endl
- << indent() << "}" << endl << indent() << "break;" << endl;
+ out <<
+ indent() << "} else {" << endl <<
+ indent() << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << " xfer += ret;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
indent_down();
}
// create the default case
- out << indent() << "default:" << endl << indent()
- << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl << indent()
- << " return " << error_ret << ";" << endl << indent() << " xfer += ret;" << endl
- << indent() << " break;" << endl;
+ out <<
+ indent() << "default:" << endl <<
+ indent() << " if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << " xfer += ret;" << endl <<
+ indent() << " break;" << endl;
// end switch
scope_down(out);
// read field end marker
- out << indent() << "if ((ret = thrift_protocol_read_field_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_read_field_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
// end while loop
scope_down(out);
out << endl;
// read the end of the structure
- out << indent() << "if ((ret = thrift_protocol_read_struct_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_read_struct_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
// if a required field is missing, throw an error
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- out << indent() << "if (!isset_" << (*f_iter)->get_name() << ")" << endl << indent() << "{"
- << endl << indent() << " g_set_error (error, THRIFT_PROTOCOL_ERROR," << endl << indent()
- << " THRIFT_PROTOCOL_ERROR_INVALID_DATA," << endl << indent()
- << " \"missing field\");" << endl << indent() << " return -1;" << endl
- << indent() << "}" << endl << endl;
+ out <<
+ indent() << "if (!isset_" << (*f_iter)->get_name() << ")" << endl <<
+ indent() << "{" << endl <<
+ indent() << " g_set_error (error, THRIFT_PROTOCOL_ERROR," << endl <<
+ indent() << " THRIFT_PROTOCOL_ERROR_INVALID_DATA," << endl <<
+ indent() << " \"missing field\");" << endl <<
+ indent() << " return -1;" << endl <<
+ indent() << "}" << endl <<
+ endl;
}
}
if (is_function) {
- indent(out) << "return xfer;" << endl;
+ indent(out) <<
+ "return xfer;" << endl;
}
// end the function/structure
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl <<
+ endl;
}
-void t_c_glib_generator::generate_serialize_field(ofstream& out,
- t_field* tfield,
+void t_c_glib_generator::generate_serialize_field(ofstream &out,
+ t_field *tfield,
string prefix,
string suffix,
int error_ret) {
- t_type* type = get_true_type(tfield->get_type());
+ t_type *type = get_true_type (tfield->get_type());
string name = prefix + tfield->get_name() + suffix;
if (type->is_void()) {
@@ -3394,80 +4109,86 @@
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, name, error_ret);
+ generate_serialize_struct (out, (t_struct *) type, name, error_ret);
} else if (type->is_container()) {
- generate_serialize_container(out, type, name, error_ret);
+ generate_serialize_container (out, type, name, error_ret);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "if ((ret = thrift_protocol_write_";
+ indent(out) <<
+ "if ((ret = thrift_protocol_write_";
if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ 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_BOOL:
- out << "bool (protocol, " << name;
- break;
- case t_base_type::TYPE_BYTE:
- out << "byte (protocol, " << name;
- break;
- case t_base_type::TYPE_I16:
- out << "i16 (protocol, " << name;
- break;
- case t_base_type::TYPE_I32:
- out << "i32 (protocol, " << name;
- break;
- case t_base_type::TYPE_I64:
- out << "i64 (protocol, " << name;
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "double (protocol, " << name;
- break;
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- out << "binary (protocol, ((GByteArray *) " << name << ")->data, ((GByteArray *) " << name
- << ")->len";
- } else {
- out << "string (protocol, " << name;
- }
- break;
- default:
- throw "compiler error: no C writer for base type " + t_base_type::t_base_name(tbase) + name;
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: "
+ + name;
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "bool (protocol, " << name;
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "byte (protocol, " << name;
+ break;
+ case t_base_type::TYPE_I16:
+ out << "i16 (protocol, " << name;
+ break;
+ case t_base_type::TYPE_I32:
+ out << "i32 (protocol, " << name;
+ break;
+ case t_base_type::TYPE_I64:
+ out << "i64 (protocol, " << name;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "double (protocol, " << name;
+ break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type *) type)->is_binary()) {
+ out << "binary (protocol, ((GByteArray *) " << name <<
+ ")->data, ((GByteArray *) " << name <<
+ ")->len";
+ } else {
+ out << "string (protocol, " << name;
+ }
+ break;
+ default:
+ throw "compiler error: no C writer for base type "
+ + t_base_type::t_base_name (tbase) + name;
}
} else if (type->is_enum()) {
out << "i32 (protocol, (gint32) " << name;
}
- out << ", error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl;
+ out << ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl;
} else {
- printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
- name.c_str(),
- type_name(type).c_str());
+ printf ("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
+ name.c_str(), type_name (type).c_str());
}
}
-void t_c_glib_generator::generate_serialize_struct(ofstream& out,
- t_struct* tstruct,
+void t_c_glib_generator::generate_serialize_struct(ofstream &out,
+ t_struct *tstruct,
string prefix,
int error_ret) {
- (void)tstruct;
- out << indent() << "if ((ret = thrift_struct_write (THRIFT_STRUCT (" << prefix
- << "), protocol, error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl
- << indent() << "xfer += ret;" << endl << endl;
+ (void) tstruct;
+ out <<
+ indent() << "if ((ret = thrift_struct_write (THRIFT_STRUCT (" << prefix << "), protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
}
-void t_c_glib_generator::generate_serialize_container(ofstream& out,
- t_type* ttype,
+void t_c_glib_generator::generate_serialize_container(ofstream &out,
+ t_type *ttype,
string prefix,
int error_ret) {
scope_up(out);
if (ttype->is_map()) {
string length = "g_hash_table_size ((GHashTable *) " + prefix + ")";
- t_type* tkey = ((t_map*)ttype)->get_key_type();
- t_type* tval = ((t_map*)ttype)->get_val_type();
- string tkey_name = type_name(tkey);
- string tval_name = type_name(tval);
+ t_type *tkey = ((t_map *) ttype)->get_key_type();
+ t_type *tval = ((t_map *) ttype)->get_val_type();
+ string tkey_name = type_name (tkey);
+ string tval_name = type_name (tval);
string tkey_ptr;
string tval_ptr;
string keyname = tmp("key");
@@ -3480,11 +4201,14 @@
* This is because we may exit early before we get a chance to free the
* GList.
*/
- out << indent() << "if ((ret = thrift_protocol_write_map_begin (protocol, "
- << type_to_enum(tkey) << ", " << type_to_enum(tval) << ", (gint32) " << length
- << ", error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl
- << indent() << "xfer += ret;" << endl << endl << indent()
- << "GList *key_list = NULL, *iter = NULL;" << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_map_begin (protocol, " <<
+ type_to_enum (tkey) << ", " << type_to_enum (tval) <<
+ ", (gint32) " << length << ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl <<
+ indent() << "GList *key_list = NULL, *iter = NULL;" << endl;
declare_local_variable(out, tkey, keyname);
declare_local_variable(out, tval, valname);
@@ -3496,256 +4220,292 @@
tkey_ptr = tkey->is_string() || !tkey->is_base_type() ? "" : "*";
tval_ptr = tval->is_string() || !tval->is_base_type() ? "" : "*";
- out << indent() << "g_hash_table_foreach ((GHashTable *) " << prefix
- << ", thrift_hash_table_get_keys, &key_list);" << endl << indent() << tkey_name << tkey_ptr
- << " keys[g_list_length (key_list)];" << endl << indent()
- << "int i=0, key_count = g_list_length (key_list);" << endl << indent()
- << "for (iter = g_list_first (key_list); iter; iter = iter->next)" << endl << indent()
- << "{" << endl << indent() << " keys[i++] = (" << tkey_name << tkey_ptr << ") iter->data;"
- << endl << indent() << "}" << endl << indent() << "g_list_free (key_list);" << endl << endl
- << indent() << "for (i = 0; i < key_count; ++i)" << endl;
+ out <<
+ indent() << "g_hash_table_foreach ((GHashTable *) " << prefix <<
+ ", thrift_hash_table_get_keys, &key_list);" << endl <<
+ indent() << tkey_name << tkey_ptr <<
+ " keys[g_list_length (key_list)];" << endl <<
+ indent() << "int i=0, key_count = g_list_length (key_list);" << endl <<
+ indent() <<
+ "for (iter = g_list_first (key_list); iter; iter = iter->next)" <<
+ endl <<
+ indent() << "{" << endl <<
+ indent() << " keys[i++] = (" << tkey_name << tkey_ptr <<
+ ") iter->data;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "g_list_free (key_list);" << endl <<
+ endl <<
+ indent() << "for (i = 0; i < key_count; ++i)" << endl;
scope_up(out);
- out << indent() << keyname << " = keys[i];" << endl << indent() << valname << " = ("
- << tval_name << tval_ptr << ") g_hash_table_lookup (((GHashTable *) " << prefix
- << "), (gpointer) " << keyname << ");" << endl << endl;
- generate_serialize_map_element(out,
- (t_map*)ttype,
- tkey_ptr + " " + keyname,
- tval_ptr + " " + valname,
- error_ret);
+ out <<
+ indent() << keyname << " = keys[i];" << endl <<
+ indent() << valname << " = (" << tval_name << tval_ptr <<
+ ") g_hash_table_lookup (((GHashTable *) " << prefix <<
+ "), (gpointer) " << keyname << ");" << endl <<
+ endl;
+ generate_serialize_map_element (out,
+ (t_map *) ttype,
+ tkey_ptr + " " + keyname,
+ tval_ptr + " " + valname,
+ error_ret);
scope_down(out);
- out << indent() << "if ((ret = thrift_protocol_write_map_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_map_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
} else if (ttype->is_set()) {
string length = "g_hash_table_size ((GHashTable *) " + prefix + ")";
- t_type* telem = ((t_set*)ttype)->get_elem_type();
- string telem_name = type_name(telem);
+ t_type *telem = ((t_set *) ttype)->get_elem_type();
+ string telem_name = type_name (telem);
string telem_ptr = telem->is_string() || !telem->is_base_type() ? "" : "*";
- out << indent() << "if ((ret = thrift_protocol_write_set_begin (protocol, "
- << type_to_enum(telem) << ", (gint32) " << length << ", error)) < 0)" << endl << indent()
- << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl << indent()
- << "GList *key_list = NULL, *iter = NULL;" << endl << indent() << telem_name << telem_ptr
- << " elem;" << endl << indent() << "gpointer value;" << endl << indent()
- << "THRIFT_UNUSED_VAR (value);" << endl << endl << indent()
- << "g_hash_table_foreach ((GHashTable *) " << prefix
- << ", thrift_hash_table_get_keys, &key_list);" << endl << indent() << telem_name
- << telem_ptr << " keys[g_list_length (key_list)];" << endl << indent()
- << "int i=0, key_count = g_list_length (key_list);" << endl << indent()
- << "for (iter = g_list_first (key_list); iter; iter = iter->next)" << endl << indent()
- << "{" << endl << indent() << " keys[i++] = (" << telem_name << telem_ptr
- << ") iter->data;" << endl << indent() << "}" << endl << indent()
- << "g_list_free (key_list);" << endl << endl << indent() << "for (i=0; i<key_count; ++i)"
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_set_begin (protocol, " <<
+ type_to_enum (telem) << ", (gint32) " << length <<
+ ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << "GList *key_list = NULL, *iter = NULL;" << endl <<
+ indent() << telem_name << telem_ptr << " elem;" << endl <<
+ indent() << "gpointer value;" << endl <<
+ indent() << "THRIFT_UNUSED_VAR (value);" << endl <<
+ endl <<
+ indent() << "g_hash_table_foreach ((GHashTable *) " << prefix <<
+ ", thrift_hash_table_get_keys, &key_list);" << endl <<
+ indent() << telem_name << telem_ptr << " keys[g_list_length (key_list)];" << endl <<
+ indent() << "int i=0, key_count = g_list_length (key_list);" << endl <<
+ indent() << "for (iter = g_list_first (key_list); iter; iter = iter->next)" << endl <<
+ indent() << "{" << endl <<
+ indent() << " keys[i++] = (" << telem_name << telem_ptr << ") iter->data;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "g_list_free (key_list);" << endl <<
+ endl <<
+ indent() << "for (i=0; i<key_count; ++i)" << endl;
scope_up(out);
- out << indent() << "elem = keys[i];" << endl << indent()
- << "value = (gpointer) g_hash_table_lookup (((GHashTable *) " << prefix
- << "), (gpointer) elem);" << endl << endl;
- generate_serialize_set_element(out, (t_set*)ttype, telem_ptr + "elem", error_ret);
+ out <<
+ indent() << "elem = keys[i];" << endl <<
+ indent() << "value = (gpointer) g_hash_table_lookup (((GHashTable *) " <<
+ prefix << "), (gpointer) elem);" << endl <<
+ endl;
+ generate_serialize_set_element (out, (t_set *) ttype, telem_ptr + "elem",
+ error_ret);
scope_down(out);
- out << indent() << "if ((ret = thrift_protocol_write_set_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_set_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
} else if (ttype->is_list()) {
string length = prefix + "->len";
string i = tmp("i");
- out << indent() << "if ((ret = thrift_protocol_write_list_begin (protocol, "
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", (gint32) " << length
- << ", error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl
- << indent() << "xfer += ret;" << endl << indent() << "guint " << i << ";" << endl
- << indent() << "for (" << i << "=0; " << i << "<" << length << "; " << i << "++)" << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_list_begin (protocol, " <<
+ type_to_enum (((t_list *) ttype)->get_elem_type()) <<
+ ", (gint32) " << length << ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << "guint " << i << ";" << endl <<
+ indent() << "for ("<< i << "=0; " << i << "<" << length << "; " << i <<
+ "++)" << endl;
scope_up(out);
- generate_serialize_list_element(out, (t_list*)ttype, prefix, i, error_ret);
+ generate_serialize_list_element (out, (t_list *) ttype, prefix, i, error_ret);
scope_down(out);
- out << indent() << "if ((ret = thrift_protocol_write_list_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_write_list_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
}
scope_down(out);
}
-void t_c_glib_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
+void t_c_glib_generator::generate_serialize_map_element(ofstream &out,
+ t_map *tmap,
string key,
string value,
int error_ret) {
- t_field kfield(tmap->get_key_type(), key);
- generate_serialize_field(out, &kfield, "", "", error_ret);
+ t_field kfield (tmap->get_key_type(), key);
+ generate_serialize_field (out, &kfield, "", "", error_ret);
- t_field vfield(tmap->get_val_type(), value);
- generate_serialize_field(out, &vfield, "", "", error_ret);
+ t_field vfield (tmap->get_val_type(), value);
+ generate_serialize_field (out, &vfield, "", "", error_ret);
}
-void t_c_glib_generator::generate_serialize_set_element(ofstream& out,
- t_set* tset,
+void t_c_glib_generator::generate_serialize_set_element(ofstream &out,
+ t_set *tset,
string element,
int error_ret) {
- t_field efield(tset->get_elem_type(), element);
- generate_serialize_field(out, &efield, "", "", error_ret);
+ t_field efield (tset->get_elem_type(), element);
+ generate_serialize_field (out, &efield, "", "", error_ret);
}
-void t_c_glib_generator::generate_serialize_list_element(ofstream& out,
- t_list* tlist,
+void t_c_glib_generator::generate_serialize_list_element(ofstream &out,
+ t_list *tlist,
string list,
string index,
int error_ret) {
- t_type* ttype = tlist->get_elem_type();
+ t_type *ttype = tlist->get_elem_type();
// cast to non-const
string cast = "";
- string name = "g_ptr_array_index ((GPtrArray *) " + list + ", " + index + ")";
+ string name = "g_ptr_array_index ((GPtrArray *) " + list + ", "
+ + index + ")";
if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- break;
- case t_base_type::TYPE_BOOL:
- name = "g_array_index (" + list + ", gboolean, " + index + ")";
- break;
- case t_base_type::TYPE_BYTE:
- name = "g_array_index (" + list + ", gint8, " + index + ")";
- break;
- case t_base_type::TYPE_I16:
- name = "g_array_index (" + list + ", gint16, " + index + ")";
- break;
- case t_base_type::TYPE_I32:
- name = "g_array_index (" + list + ", gint32, " + index + ")";
- break;
- case t_base_type::TYPE_I64:
- name = "g_array_index (" + list + ", gint64, " + index + ")";
- break;
- case t_base_type::TYPE_DOUBLE:
- name = "g_array_index (" + list + ", gdouble, " + index + ")";
- break;
- case t_base_type::TYPE_STRING:
- cast = "(gchar*)";
- break;
- default:
- throw "compiler error: no array info for type";
- }
- } else if (ttype->is_map() || ttype->is_set()) {
- cast = "(GHashTable*)";
- } else if (ttype->is_list()) {
- t_type* base = ((t_list*)ttype)->get_elem_type();
- if (base->is_base_type()) {
- switch (((t_base_type*)base)->get_base()) {
case t_base_type::TYPE_VOID:
throw "compiler error: cannot determine array type";
break;
case t_base_type::TYPE_BOOL:
+ name = "g_array_index (" + list + ", gboolean, " + index + ")";
+ break;
case t_base_type::TYPE_BYTE:
+ name = "g_array_index (" + list + ", gint8, " + index + ")";
+ break;
case t_base_type::TYPE_I16:
+ name = "g_array_index (" + list + ", gint16, " + index + ")";
+ break;
case t_base_type::TYPE_I32:
+ name = "g_array_index (" + list + ", gint32, " + index + ")";
+ break;
case t_base_type::TYPE_I64:
+ name = "g_array_index (" + list + ", gint64, " + index + ")";
+ break;
case t_base_type::TYPE_DOUBLE:
- cast = "(GArray*)";
+ name = "g_array_index (" + list + ", gdouble, " + index + ")";
break;
case t_base_type::TYPE_STRING:
- cast = "(GPtrArray*)";
+ cast = "(gchar*)";
break;
default:
- throw "Compiler error: no array info for type";
+ throw "compiler error: no array info for type";
+ }
+ } else if (ttype->is_map() || ttype->is_set()) {
+ cast = "(GHashTable*)";
+ } else if (ttype->is_list()) {
+ t_type *base = ((t_list *)ttype)->get_elem_type();
+ if (base->is_base_type()) {
+ switch (((t_base_type *) base)->get_base()) {
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ break;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ cast = "(GArray*)";
+ break;
+ case t_base_type::TYPE_STRING:
+ cast = "(GPtrArray*)";
+ break;
+ default:
+ throw "Compiler error: no array info for type";
}
} else {
cast = "(GPtrArray*)";
}
}
- t_field efield(ttype, "(" + cast + name + ")");
- generate_serialize_field(out, &efield, "", "", error_ret);
+ t_field efield (ttype, "(" + cast + name + ")");
+ generate_serialize_field (out, &efield, "", "", error_ret);
}
/* deserializes a field of any type. */
-void t_c_glib_generator::generate_deserialize_field(ofstream& out,
- t_field* tfield,
+void t_c_glib_generator::generate_deserialize_field(ofstream &out,
+ t_field *tfield,
string prefix,
string suffix,
int error_ret,
bool allocate) {
- t_type* type = get_true_type(tfield->get_type());
+ t_type *type = get_true_type (tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name() + suffix;
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name, error_ret, allocate);
+ generate_deserialize_struct (out, (t_struct *) type, name, error_ret, allocate);
} else if (type->is_container()) {
- generate_deserialize_container(out, type, name, error_ret);
+ generate_deserialize_container (out, type, name, error_ret);
} else if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) type)->get_base();
if (tbase == t_base_type::TYPE_STRING) {
- indent(out) << "if (" << name << " != NULL)" << endl << indent() << "{" << endl;
+ indent(out) << "if (" << name << " != NULL)" << endl <<
+ indent() << "{" << endl;
indent_up();
- indent(out) << "g_free(" << name << ");" << endl << indent() << name << " = NULL;" << endl;
+ indent(out) << "g_free(" << name << ");" << endl <<
+ indent() << name << " = NULL;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
indent(out) << "if ((ret = thrift_protocol_read_";
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:
- if (((t_base_type*)type)->is_binary()) {
- out << "binary (protocol, &data, &len";
- } else {
- out << "string (protocol, &" << name;
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "bool (protocol, &" << name;
- break;
- case t_base_type::TYPE_BYTE:
- out << "byte (protocol, &" << name;
- break;
- case t_base_type::TYPE_I16:
- out << "i16 (protocol, &" << name;
- break;
- case t_base_type::TYPE_I32:
- out << "i32 (protocol, &" << name;
- break;
- case t_base_type::TYPE_I64:
- out << "i64 (protocol, &" << name;
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "double (protocol, &" << name;
- break;
- default:
- throw "compiler error: no C reader for base type " + t_base_type::t_base_name(tbase) + name;
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: " + name;
+ break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type *) type)->is_binary()) {
+ out << "binary (protocol, &data, &len";
+ } else {
+ out << "string (protocol, &" << name;
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "bool (protocol, &" << name;
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "byte (protocol, &" << name;
+ break;
+ case t_base_type::TYPE_I16:
+ out << "i16 (protocol, &" << name;
+ break;
+ case t_base_type::TYPE_I32:
+ out << "i32 (protocol, &" << name;
+ break;
+ case t_base_type::TYPE_I64:
+ out << "i64 (protocol, &" << name;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "double (protocol, &" << name;
+ break;
+ default:
+ throw "compiler error: no C reader for base type "
+ + t_base_type::t_base_name (tbase) + name;
}
out << ", error)) < 0)" << endl;
- out << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl;
+ out << indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
// load the byte array with the data
- if (tbase == t_base_type::TYPE_STRING && ((t_base_type*)type)->is_binary()) {
+ if (tbase == t_base_type::TYPE_STRING
+ && ((t_base_type *) type)->is_binary()) {
indent(out) << name << " = g_byte_array_new();" << endl;
indent(out) << "g_byte_array_append (" << name << ", (guint8 *) data, (guint) len);" << endl;
indent(out) << "g_free (data);" << endl;
}
} else if (type->is_enum()) {
- string t = tmp("ecast");
- out << indent() << "gint32 " << t << ";" << endl << indent()
- << "if ((ret = thrift_protocol_read_i32 (protocol, &" << t << ", error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << indent() << name << " = (" << type_name(type) << ")" << t << ";" << endl;
+ string t = tmp ("ecast");
+ out <<
+ indent() << "gint32 " << t << ";" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_i32 (protocol, &" << t << ", error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ indent() << name << " = (" << type_name (type) << ")" << 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());
+ printf ("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
+ tfield->get_name().c_str(), type_name (type).c_str());
}
// if the type is not required and this is a thrift struct (no prefix),
@@ -3758,27 +4518,33 @@
}
}
-void t_c_glib_generator::generate_deserialize_struct(ofstream& out,
- t_struct* tstruct,
+void t_c_glib_generator::generate_deserialize_struct(ofstream &out,
+ t_struct *tstruct,
string prefix,
int error_ret,
bool allocate) {
string name_uc = to_upper_case(initial_caps_to_underscores(tstruct->get_name()));
if (tstruct->is_xception()) {
- out << indent() << "/* This struct is an exception */" << endl;
+ out <<
+ indent() << "/* This struct is an exception */" << endl;
allocate = true;
}
if (allocate) {
- out << indent() << "if ( " << prefix << " != NULL)" << endl << indent() << "{" << endl;
+ out <<
+ indent() << "if ( " << prefix << " != NULL)" << endl <<
+ indent() << "{" << endl;
indent_up();
- out << indent() << "g_object_unref (" << prefix << ");" << endl;
+ out <<
+ indent() << "g_object_unref (" << prefix << ");" << endl;
indent_down();
- out << indent() << "}" << endl << indent() << prefix << " = g_object_new (" << this->nspace_uc
- << "TYPE_" << name_uc << ", NULL);" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << prefix << " = g_object_new (" << this->nspace_uc << "TYPE_" << name_uc << ", NULL);" << endl;
}
- out << indent() << "if ((ret = thrift_struct_read (THRIFT_STRUCT (" << prefix
- << "), protocol, error)) < 0)" << endl << indent() << "{" << endl;
+ out <<
+ indent() << "if ((ret = thrift_struct_read (THRIFT_STRUCT (" << prefix << "), protocol, error)) < 0)" << endl <<
+ indent() << "{" << endl;
indent_up();
if (allocate) {
indent(out) << "g_object_unref (" << prefix << ");" << endl;
@@ -3786,79 +4552,102 @@
indent(out) << prefix << " = NULL;" << endl;
}
}
- out << indent() << "return " << error_ret << ";" << endl;
+ out <<
+ indent() << "return " << error_ret << ";" << endl;
indent_down();
- out << indent() << "}" << endl << indent() << "xfer += ret;" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << "xfer += ret;" << endl;
}
-void t_c_glib_generator::generate_deserialize_container(ofstream& out,
- t_type* ttype,
- string prefix,
- int error_ret) {
+void t_c_glib_generator::generate_deserialize_container (ofstream &out, t_type *ttype,
+ string prefix, int error_ret) {
scope_up(out);
if (ttype->is_map()) {
- out << indent() << "guint32 size;" << endl << indent() << "ThriftType key_type;" << endl
- << indent() << "ThriftType value_type;" << endl << endl << indent()
- << "/* read the map begin marker */" << endl << indent()
- << "if ((ret = thrift_protocol_read_map_begin (protocol, &key_type, &value_type, &size, "
- "error)) < 0)" << endl << indent() << " return " << error_ret << ";" << endl << indent()
- << "xfer += ret;" << endl << endl;
+ out <<
+ indent() << "guint32 size;" << endl <<
+ indent() << "ThriftType key_type;" << endl <<
+ indent() << "ThriftType value_type;" << endl <<
+ endl <<
+ indent() << "/* read the map begin marker */" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_map_begin (protocol, &key_type, &value_type, &size, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
// iterate over map elements
- out << indent() << "/* iterate through each of the map's fields */" << endl << indent()
- << "guint32 i;" << endl << indent() << "for (i = 0; i < size; i++)" << endl;
+ out <<
+ indent() << "/* iterate through each of the map's fields */" << endl <<
+ indent() << "guint32 i;" << endl <<
+ indent() << "for (i = 0; i < size; i++)" << endl;
scope_up(out);
- generate_deserialize_map_element(out, (t_map*)ttype, prefix, error_ret);
+ generate_deserialize_map_element (out, (t_map *) ttype, prefix, error_ret);
scope_down(out);
out << endl;
// read map end
- out << indent() << "/* read the map end marker */" << endl << indent()
- << "if ((ret = thrift_protocol_read_map_end (protocol, error)) < 0)" << endl << indent()
- << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl;
+ out <<
+ indent() << "/* read the map end marker */" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_map_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl;
} else if (ttype->is_set()) {
- out << indent() << "guint32 size;" << endl << indent() << "ThriftType element_type;" << endl
- << indent()
- << "if ((ret = thrift_protocol_read_set_begin (protocol, &element_type, &size, error)) < 0)"
- << endl << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;"
- << endl << endl;
+ out <<
+ indent() << "guint32 size;" << endl <<
+ indent() << "ThriftType element_type;" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_set_begin (protocol, &element_type, &size, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
// iterate over the elements
- out << indent() << "/* iterate through the set elements */" << endl << indent() << "guint32 i;"
- << endl << indent() << "for (i = 0; i < size; ++i)" << endl;
+ out <<
+ indent() << "/* iterate through the set elements */" << endl <<
+ indent() << "guint32 i;" << endl <<
+ indent() << "for (i = 0; i < size; ++i)" << endl;
scope_up(out);
- generate_deserialize_set_element(out, (t_set*)ttype, prefix, error_ret);
+ generate_deserialize_set_element (out, (t_set *) ttype, prefix, error_ret);
scope_down(out);
// read set end
- out << indent() << "if ((ret = thrift_protocol_read_set_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_read_set_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
} else if (ttype->is_list()) {
- out << indent() << "guint32 size;" << endl << indent() << "ThriftType element_type;" << endl
- << indent() << "if ((ret = thrift_protocol_read_list_begin (protocol, &element_type, "
- "&size, error)) < 0)" << endl << indent() << " return " << error_ret << ";"
- << endl << indent() << "xfer += ret;" << endl << endl;
+ out <<
+ indent() << "guint32 size;" << endl <<
+ indent() << "ThriftType element_type;" << endl <<
+ indent() << "if ((ret = thrift_protocol_read_list_begin (protocol, &element_type, &size, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
- out << indent() << "/* iterate through list elements */" << endl << indent() << "guint32 i;"
- << endl << indent() << "for (i = 0; i < size; i++)" << endl;
+ out <<
+ indent() << "/* iterate through list elements */" << endl <<
+ indent() << "guint32 i;" << endl <<
+ indent() << "for (i = 0; i < size; i++)" << endl;
scope_up(out);
- generate_deserialize_list_element(out, (t_list*)ttype, prefix, "i", error_ret);
+ generate_deserialize_list_element (out, (t_list *) ttype, prefix, "i",
+ error_ret);
scope_down(out);
- out << indent() << "if ((ret = thrift_protocol_read_list_end (protocol, error)) < 0)" << endl
- << indent() << " return " << error_ret << ";" << endl << indent() << "xfer += ret;" << endl
- << endl;
+ out <<
+ indent() << "if ((ret = thrift_protocol_read_list_end (protocol, error)) < 0)" << endl <<
+ indent() << " return " << error_ret << ";" << endl <<
+ indent() << "xfer += ret;" << endl <<
+ endl;
}
scope_down(out);
}
-void t_c_glib_generator::declare_local_variable(ofstream& out, t_type* ttype, string& name) {
- string tname = type_name(ttype);
+void t_c_glib_generator::declare_local_variable(ofstream &out, t_type *ttype, string &name) {
+ string tname = type_name (ttype);
/* If the given type is a typedef, find its underlying type so we
can correctly determine how to generate a pointer to it */
@@ -3867,23 +4656,27 @@
string ptr = ttype->is_string() || !ttype->is_base_type() ? "" : "*";
if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
- out << indent() << tname << ptr << " " << name << " = "
- << generate_new_hash_from_type(tmap->get_key_type(), tmap->get_val_type()) << endl;
+ t_map *tmap = (t_map *)ttype;
+ out <<
+ indent() << tname << ptr << " " << name << " = " <<
+ generate_new_hash_from_type(tmap->get_key_type(), tmap->get_val_type()) <<
+ endl;
} else if (ttype->is_enum()) {
- out << indent() << tname << ptr << " " << name << ";" << endl;
+ out <<
+ indent() << tname << ptr << " " << name << ";" << endl;
} else {
- out << indent() << tname << ptr << " " << name
- << (ptr != "" ? " = g_new (" + tname + ", 1)" : " = NULL") << ";" << endl;
+ out <<
+ indent() << tname << ptr << " " << name << (ptr != "" ? " = g_new (" + tname + ", 1)" : " = NULL") << ";" << endl;
}
}
-void t_c_glib_generator::generate_deserialize_map_element(ofstream& out,
- t_map* tmap,
+
+void t_c_glib_generator::generate_deserialize_map_element(ofstream &out,
+ t_map *tmap,
string prefix,
int error_ret) {
- t_type* tkey = tmap->get_key_type();
- t_type* tval = tmap->get_val_type();
+ t_type *tkey = tmap->get_key_type();
+ t_type *tval = tmap->get_val_type();
string keyname = tmp("key");
string valname = tmp("val");
@@ -3900,96 +4693,97 @@
string tval_ptr = tval->is_string() || !tval->is_base_type() ? "" : "*";
// deserialize the fields of the map element
- t_field fkey(tkey, tkey_ptr + keyname);
- generate_deserialize_field(out, &fkey, "", "", error_ret);
- t_field fval(tval, tval_ptr + valname);
- generate_deserialize_field(out, &fval, "", "", error_ret);
+ t_field fkey (tkey, tkey_ptr + keyname);
+ generate_deserialize_field (out, &fkey, "", "", error_ret);
+ t_field fval (tval, tval_ptr + valname);
+ generate_deserialize_field (out, &fval, "", "", error_ret);
- indent(out) << "g_hash_table_insert ((GHashTable *)" << prefix << ", (gpointer) " << keyname
- << ", (gpointer) " << valname << ");" << endl;
+ indent(out) <<
+ "g_hash_table_insert ((GHashTable *)" << prefix << ", (gpointer) " << keyname << ", (gpointer) " << valname << ");" << endl;
}
-void t_c_glib_generator::generate_deserialize_set_element(ofstream& out,
- t_set* tset,
+void t_c_glib_generator::generate_deserialize_set_element(ofstream &out,
+ t_set *tset,
string prefix,
int error_ret) {
- t_type* telem = tset->get_elem_type();
- string elem = tmp("_elem");
+ t_type *telem = tset->get_elem_type();
+ string elem = tmp ("_elem");
string telem_ptr = telem->is_string() || !telem->is_base_type() ? "" : "*";
declare_local_variable(out, telem, elem);
- t_field felem(telem, telem_ptr + elem);
- generate_deserialize_field(out, &felem, "", "", error_ret);
+ t_field felem (telem, telem_ptr + elem);
+ generate_deserialize_field (out, &felem, "", "", error_ret);
- indent(out) << "g_hash_table_insert ((GHashTable *) " << prefix << ", (gpointer) " << elem
- << ", (gpointer) 1);" << endl;
+ indent(out) <<
+ "g_hash_table_insert ((GHashTable *) " << prefix << ", (gpointer) " <<
+ elem << ", (gpointer) 1);" << endl;
}
-void t_c_glib_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
+void t_c_glib_generator::generate_deserialize_list_element(ofstream &out,
+ t_list *tlist,
string prefix,
string index,
int error_ret) {
- (void)index;
- t_type* ttype = tlist->get_elem_type();
- string elem = tmp("_elem");
+ (void) index;
+ t_type *ttype = tlist->get_elem_type();
+ string elem = tmp ("_elem");
string telem_ptr = ttype->is_string() || !ttype->is_base_type() ? "" : "*";
declare_local_variable(out, ttype, elem);
- t_field felem(ttype, telem_ptr + elem);
- generate_deserialize_field(out, &felem, "", "", error_ret);
+ t_field felem (ttype, telem_ptr + elem);
+ generate_deserialize_field (out, &felem, "", "", error_ret);
indent(out);
if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- case t_base_type::TYPE_STRING:
- out << "g_ptr_array_add (" << prefix << ", " << elem << ");" << endl;
- return;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- out << "g_array_append_vals (" << prefix << ", " << elem << ", 1);" << endl;
- return;
- default:
- throw "compiler error: no array info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ case t_base_type::TYPE_STRING:
+ out << "g_ptr_array_add (" << prefix << ", " << elem << ");" << endl;
+ return;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ out << "g_array_append_vals (" << prefix << ", " << elem << ", 1);" << endl;
+ return;
+ default:
+ throw "compiler error: no array info for type";
}
}
out << "g_ptr_array_add (" << prefix << ", " << elem << ");" << endl;
}
-string t_c_glib_generator::generate_free_func_from_type(t_type* ttype) {
+string t_c_glib_generator::generate_free_func_from_type (t_type * ttype) {
if (ttype == NULL)
return "NULL";
if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine hash type";
- break;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- return "g_free";
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)ttype)->is_binary()) {
- return "thrift_string_free";
- }
- return "g_free";
- default:
- throw "compiler error: no hash table info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine hash type";
+ break;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ return "g_free";
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type *) ttype)->is_binary()) {
+ return "thrift_string_free";
+ }
+ return "g_free";
+ default:
+ throw "compiler error: no hash table info for type";
}
} else if (ttype->is_enum()) {
return "NULL";
@@ -3998,143 +4792,142 @@
} else if (ttype->is_struct()) {
return "g_object_unref";
} else if (ttype->is_list()) {
- t_type* etype = ((t_list*)ttype)->get_elem_type();
+ t_type *etype = ((t_list *) ttype)->get_elem_type();
if (etype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)etype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) etype)->get_base();
switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ break;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ return "(GDestroyNotify) g_array_unref";
+ case t_base_type::TYPE_STRING:
+ return "(GDestroyNotify) g_ptr_array_unref";
+ default:
+ throw "compiler error: no array info for type";
+ }
+ } else if (etype->is_container() || etype->is_struct()) {
+ return "(GDestroyNotify) g_ptr_array_unref";;
+ } else if (etype->is_enum()) {
+ return "(GDestroyNotify) g_array_unref";
+ }
+ printf("Type not expected inside the array: %s\n", etype->get_name().c_str());
+ throw "Type not expected inside array" ;
+ } else if (ttype->is_typedef()) {
+ return generate_free_func_from_type(((t_typedef *) ttype)->get_type());
+ }
+ printf("Type not expected: %s\n", ttype->get_name().c_str());
+ throw "Type not expected";
+}
+
+string t_c_glib_generator::generate_hash_func_from_type (t_type * ttype) {
+ if (ttype == NULL)
+ return "NULL";
+
+ if (ttype->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
+ switch (tbase) {
case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
+ throw "compiler error: cannot determine hash type";
break;
case t_base_type::TYPE_BOOL:
case t_base_type::TYPE_BYTE:
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
+ return "g_int_hash";
case t_base_type::TYPE_I64:
+ return "g_int64_hash";
case t_base_type::TYPE_DOUBLE:
- return "(GDestroyNotify) g_array_unref";
+ return "g_double_hash";
case t_base_type::TYPE_STRING:
- return "(GDestroyNotify) g_ptr_array_unref";
+ return "g_str_hash";
default:
- throw "compiler error: no array info for type";
- }
- } else if (etype->is_container() || etype->is_struct()) {
- return "(GDestroyNotify) g_ptr_array_unref";
- ;
- } else if (etype->is_enum()) {
- return "(GDestroyNotify) g_array_unref";
- }
- printf("Type not expected inside the array: %s\n", etype->get_name().c_str());
- throw "Type not expected inside array";
- } else if (ttype->is_typedef()) {
- return generate_free_func_from_type(((t_typedef*)ttype)->get_type());
- }
- printf("Type not expected: %s\n", ttype->get_name().c_str());
- throw "Type not expected";
-}
-
-string t_c_glib_generator::generate_hash_func_from_type(t_type* ttype) {
- if (ttype == NULL)
- return "NULL";
-
- if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
- switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine hash type";
- break;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- return "g_int_hash";
- case t_base_type::TYPE_I64:
- return "g_int64_hash";
- case t_base_type::TYPE_DOUBLE:
- return "g_double_hash";
- case t_base_type::TYPE_STRING:
- return "g_str_hash";
- default:
- throw "compiler error: no hash table info for type";
+ throw "compiler error: no hash table info for type";
}
} else if (ttype->is_enum()) {
return "g_direct_hash";
} else if (ttype->is_container() || ttype->is_struct()) {
return "g_direct_hash";
} else if (ttype->is_typedef()) {
- return generate_hash_func_from_type(((t_typedef*)ttype)->get_type());
+ return generate_hash_func_from_type(((t_typedef *) ttype)->get_type());
}
printf("Type not expected: %s\n", ttype->get_name().c_str());
throw "Type not expected";
}
-string t_c_glib_generator::generate_cmp_func_from_type(t_type* ttype) {
+string t_c_glib_generator::generate_cmp_func_from_type (t_type * ttype) {
if (ttype == NULL)
return "NULL";
if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine hash type";
- break;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- return "g_int_equal";
- case t_base_type::TYPE_I64:
- return "g_int64_equal";
- case t_base_type::TYPE_DOUBLE:
- return "g_double_equal";
- case t_base_type::TYPE_STRING:
- return "g_str_equal";
- default:
- throw "compiler error: no hash table info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine hash type";
+ break;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ return "g_int_equal";
+ case t_base_type::TYPE_I64:
+ return "g_int64_equal";
+ case t_base_type::TYPE_DOUBLE:
+ return "g_double_equal";
+ case t_base_type::TYPE_STRING:
+ return "g_str_equal";
+ default:
+ throw "compiler error: no hash table info for type";
}
} else if (ttype->is_enum()) {
return "g_direct_equal";
} else if (ttype->is_container() || ttype->is_struct()) {
return "g_direct_equal";
} else if (ttype->is_typedef()) {
- return generate_cmp_func_from_type(((t_typedef*)ttype)->get_type());
+ return generate_cmp_func_from_type(((t_typedef *) ttype)->get_type());
}
printf("Type not expected: %s\n", ttype->get_name().c_str());
throw "Type not expected";
}
-string t_c_glib_generator::generate_new_hash_from_type(t_type* key, t_type* value) {
+string t_c_glib_generator::generate_new_hash_from_type (t_type * key, t_type *value) {
string hash_func = generate_hash_func_from_type(key);
string cmp_func = generate_cmp_func_from_type(key);
string key_free_func = generate_free_func_from_type(key);
string value_free_func = generate_free_func_from_type(value);
- return "g_hash_table_new_full (" + hash_func + ", " + cmp_func + ", " + key_free_func + ", "
- + value_free_func + ");";
+ return "g_hash_table_new_full (" + hash_func + ", " + cmp_func + ", " +
+ key_free_func + ", " + value_free_func + ");";
}
-string t_c_glib_generator::generate_new_array_from_type(t_type* ttype) {
+string t_c_glib_generator::generate_new_array_from_type(t_type * ttype) {
if (ttype->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+ t_base_type::t_base tbase = ((t_base_type *) ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "compiler error: cannot determine array type";
- break;
- case t_base_type::TYPE_BOOL:
- return "g_array_new (0, 1, sizeof (gboolean));";
- case t_base_type::TYPE_BYTE:
- return "g_array_new (0, 1, sizeof (gint8));";
- case t_base_type::TYPE_I16:
- return "g_array_new (0, 1, sizeof (gint16));";
- case t_base_type::TYPE_I32:
- return "g_array_new (0, 1, sizeof (gint32));";
- case t_base_type::TYPE_I64:
- return "g_array_new (0, 1, sizeof (gint64));";
- case t_base_type::TYPE_DOUBLE:
- return "g_array_new (0, 1, sizeof (gdouble));";
- case t_base_type::TYPE_STRING:
- return "g_ptr_array_new_with_free_func (g_free);";
- default:
- throw "compiler error: no array info for type";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot determine array type";
+ break;
+ case t_base_type::TYPE_BOOL:
+ return "g_array_new (0, 1, sizeof (gboolean));";
+ case t_base_type::TYPE_BYTE:
+ return "g_array_new (0, 1, sizeof (gint8));";
+ case t_base_type::TYPE_I16:
+ return "g_array_new (0, 1, sizeof (gint16));";
+ case t_base_type::TYPE_I32:
+ return "g_array_new (0, 1, sizeof (gint32));";
+ case t_base_type::TYPE_I64:
+ return "g_array_new (0, 1, sizeof (gint64));";
+ case t_base_type::TYPE_DOUBLE:
+ return "g_array_new (0, 1, sizeof (gdouble));";
+ case t_base_type::TYPE_STRING:
+ return "g_ptr_array_new_with_free_func (g_free);";
+ default:
+ throw "compiler error: no array info for type";
}
} else if (ttype->is_enum()) {
return "g_array_new (0, 1, sizeof (gint32));";
@@ -4146,6 +4939,7 @@
return "g_ptr_array_new();";
}
+
/***************************************
* UTILITY FUNCTIONS *
***************************************/
@@ -4154,20 +4948,20 @@
* Upper case a string. Wraps boost's string utility.
*/
string to_upper_case(string name) {
- string s(name);
- std::transform(s.begin(), s.end(), s.begin(), ::toupper);
+ string s (name);
+ std::transform (s.begin(), s.end(), s.begin(), ::toupper);
return s;
- // return boost::to_upper_copy (name);
+// return boost::to_upper_copy (name);
}
/**
* Lower case a string. Wraps boost's string utility.
*/
string to_lower_case(string name) {
- string s(name);
- std::transform(s.begin(), s.end(), s.begin(), ::tolower);
+ string s (name);
+ std::transform (s.begin(), s.end(), s.begin(), ::tolower);
return s;
- // return boost::to_lower_copy (name);
+// return boost::to_lower_copy (name);
}
/**
@@ -4179,14 +4973,14 @@
*/
string initial_caps_to_underscores(string name) {
string ret;
- const char* tmp = name.c_str();
+ const char *tmp = name.c_str();
int pos = 0;
/* the first character isn't underscored if uppercase, just lowercased */
- ret += tolower(tmp[pos]);
+ ret += tolower (tmp[pos]);
pos++;
for (unsigned int i = pos; i < name.length(); i++) {
- char lc = tolower(tmp[i]);
+ char lc = tolower (tmp[i]);
if (lc != tmp[i]) {
ret += '_';
}
@@ -4207,18 +5001,20 @@
*/
string underscores_to_initial_caps(string name) {
string ret;
- const char* tmp = name.c_str();
+ const char *tmp = name.c_str();
bool uppercase_next = true;
for (unsigned int i = 0; i < name.length(); i++) {
char c = tmp[i];
if (c == '_') {
uppercase_next = true;
- } else {
+ }
+ else {
if (uppercase_next) {
- ret += toupper(c);
+ ret += toupper (c);
uppercase_next = false;
- } else {
+ }
+ else {
ret += c;
}
}
diff --git a/compiler/cpp/src/generate/t_cocoa_generator.cc b/compiler/cpp/src/generate/t_cocoa_generator.cc
index 3ad6c31..cbed831 100644
--- a/compiler/cpp/src/generate/t_cocoa_generator.cc
+++ b/compiler/cpp/src/generate/t_cocoa_generator.cc
@@ -35,7 +35,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Objective-C code generator.
@@ -43,12 +43,14 @@
* mostly copy/pasting/tweaking from mcslee's work.
*/
class t_cocoa_generator : public t_oop_generator {
-public:
- t_cocoa_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_cocoa_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("log_unexpected");
@@ -73,38 +75,26 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
- void print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool defval = false,
- bool is_property = false);
- std::string render_const_value(ofstream& out,
- t_type* type,
- t_const_value* value,
- bool containerize_it = false);
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool defval=false, bool is_property=false);
+ std::string render_const_value(ofstream& out, t_type* type, t_const_value* value, bool containerize_it=false);
void generate_cocoa_struct(t_struct* tstruct, bool is_exception);
- void generate_cocoa_struct_interface(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false);
- void generate_cocoa_struct_implementation(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_result = false);
- void generate_cocoa_struct_initializer_signature(std::ofstream& out, t_struct* tstruct);
- void generate_cocoa_struct_init_with_coder_method(ofstream& out,
+ void generate_cocoa_struct_interface(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
+ void generate_cocoa_struct_implementation(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
+ void generate_cocoa_struct_initializer_signature(std::ofstream& out,
+ t_struct* tstruct);
+ void generate_cocoa_struct_init_with_coder_method(ofstream &out,
t_struct* tstruct,
bool is_exception);
- void generate_cocoa_struct_encode_with_coder_method(ofstream& out,
- t_struct* tstruct,
- bool is_exception);
+ void generate_cocoa_struct_encode_with_coder_method(ofstream &out,
+ t_struct* tstruct,
+ bool is_exception);
void generate_cocoa_struct_field_accessor_declarations(std::ofstream& out,
t_struct* tstruct,
bool is_exception);
@@ -125,51 +115,69 @@
* Service-level generation functions
*/
- void generate_cocoa_service_protocol(std::ofstream& out, t_service* tservice);
- void generate_cocoa_service_client_interface(std::ofstream& out, t_service* tservice);
- void generate_cocoa_service_client_implementation(std::ofstream& out, t_service* tservice);
- void generate_cocoa_service_server_interface(std::ofstream& out, t_service* tservice);
- void generate_cocoa_service_server_implementation(std::ofstream& out, t_service* tservice);
- void generate_cocoa_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_cocoa_service_protocol (std::ofstream& out, t_service* tservice);
+ void generate_cocoa_service_client_interface (std::ofstream& out, t_service* tservice);
+ void generate_cocoa_service_client_implementation (std::ofstream& out, t_service* tservice);
+ void generate_cocoa_service_server_interface (std::ofstream& out, t_service* tservice);
+ void generate_cocoa_service_server_implementation (std::ofstream& out, t_service* tservice);
+ void generate_cocoa_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string fieldName);
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string fieldName);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string fieldName = "");
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string fieldName="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string index,
- std::string listName);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string index,
+ std::string listName);
/**
* Helper rendering functions
@@ -178,7 +186,7 @@
std::string cocoa_prefix();
std::string cocoa_imports();
std::string cocoa_thrift_imports();
- std::string type_name(t_type* ttype, bool class_ref = false);
+ std::string type_name(t_type* ttype, bool class_ref=false);
std::string base_type_name(t_base_type* tbase);
std::string declare_field(t_field* tfield);
std::string declare_property(t_field* tfield);
@@ -187,17 +195,21 @@
std::string type_to_enum(t_type* ttype);
std::string format_string_for_type(t_type* type);
std::string call_field_setter(t_field* tfield, std::string fieldName);
- std::string containerize(t_type* ttype, std::string fieldName);
- std::string decontainerize(t_field* tfield, std::string fieldName);
+ std::string containerize(t_type * ttype, std::string fieldName);
+ std::string decontainerize(t_field * tfield, std::string fieldName);
bool type_can_be_null(t_type* ttype) {
ttype = get_true_type(ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception()
- || ttype->is_string();
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string();
}
-private:
+ private:
+
std::string cocoa_prefix_;
std::string constants_declarations_;
@@ -212,6 +224,7 @@
bool validate_required_;
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -222,22 +235,32 @@
cocoa_prefix_ = program_->get_namespace("cocoa");
// we have a .h header file...
- string f_header_name = program_name_ + ".h";
- string f_header_fullname = get_out_dir() + f_header_name;
+ string f_header_name = program_name_+".h";
+ string f_header_fullname = get_out_dir()+f_header_name;
f_header_.open(f_header_fullname.c_str());
- f_header_ << autogen_comment() << endl;
+ f_header_ <<
+ autogen_comment() <<
+ endl;
- f_header_ << cocoa_imports() << cocoa_thrift_imports();
+ f_header_ <<
+ cocoa_imports() <<
+ cocoa_thrift_imports();
// ...and a .m implementation file
- string f_impl_name = get_out_dir() + program_name_ + ".m";
+ string f_impl_name = get_out_dir()+program_name_+".m";
f_impl_.open(f_impl_name.c_str());
- f_impl_ << autogen_comment() << endl;
+ f_impl_ <<
+ autogen_comment() <<
+ endl;
- f_impl_ << cocoa_imports() << cocoa_thrift_imports() << "#import \"" << f_header_name << "\""
- << endl << endl;
+ f_impl_ <<
+ cocoa_imports() <<
+ cocoa_thrift_imports() <<
+ "#import \"" << f_header_name << "\"" << endl <<
+ endl;
+
}
/**
@@ -246,7 +269,10 @@
* @return List of imports for Cocoa libraries
*/
string t_cocoa_generator::cocoa_imports() {
- return string() + "#import <Foundation/Foundation.h>\n" + "\n";
+ return
+ string() +
+ "#import <Foundation/Foundation.h>\n" +
+ "\n";
}
/**
@@ -255,10 +281,15 @@
* @return List of imports necessary for thrift runtime
*/
string t_cocoa_generator::cocoa_thrift_imports() {
- string result = string() + "#import \"TProtocol.h\"\n" + "#import \"TApplicationException.h\"\n"
- + "#import \"TProtocolException.h\"\n" + "#import \"TProtocolUtil.h\"\n"
- + "#import \"TProcessor.h\"\n" + "#import \"TObjective-C.h\"\n"
- + "#import \"TBase.h\"\n" + "\n";
+ string result = string() +
+ "#import \"TProtocol.h\"\n" +
+ "#import \"TApplicationException.h\"\n" +
+ "#import \"TProtocolException.h\"\n" +
+ "#import \"TProtocolUtil.h\"\n" +
+ "#import \"TProcessor.h\"\n" +
+ "#import \"TObjective-C.h\"\n" +
+ "#import \"TBase.h\"\n" +
+ "\n";
// Include other Thrift includes
const vector<t_program*>& includes = program_->get_includes();
@@ -270,10 +301,12 @@
return result;
}
+
/**
* Finish up generation.
*/
-void t_cocoa_generator::close_generator() {
+void t_cocoa_generator::close_generator()
+{
// stick our constants declarations at the end of the header file
// since they refer to things we are defining.
f_header_ << constants_declarations_ << endl;
@@ -285,8 +318,9 @@
* @param ttypedef The type definition
*/
void t_cocoa_generator::generate_typedef(t_typedef* ttypedef) {
- f_header_ << indent() << "typedef " << type_name(ttypedef->get_type()) << " " << cocoa_prefix_
- << ttypedef->get_symbolic() << ";" << endl << endl;
+ f_header_ <<
+ indent() << "typedef " << type_name(ttypedef->get_type()) << " " << cocoa_prefix_ << ttypedef->get_symbolic() << ";" << endl <<
+ endl;
}
/**
@@ -298,7 +332,8 @@
* @param tenum The enumeration
*/
void t_cocoa_generator::generate_enum(t_enum* tenum) {
- f_header_ << indent() << "enum " << cocoa_prefix_ << tenum->get_name() << " {" << endl;
+ f_header_ <<
+ indent() << "enum " << cocoa_prefix_ << tenum->get_name() << " {" << endl;
indent_up();
vector<t_enum_value*> constants = tenum->get_constants();
@@ -308,14 +343,20 @@
if (first) {
first = false;
} else {
- f_header_ << "," << endl;
+ f_header_ <<
+ "," << endl;
}
- f_header_ << indent() << tenum->get_name() << "_" << (*c_iter)->get_name();
- f_header_ << " = " << (*c_iter)->get_value();
+ f_header_ <<
+ indent() << tenum->get_name() << "_" << (*c_iter)->get_name();
+ f_header_ <<
+ " = " << (*c_iter)->get_value();
}
indent_down();
- f_header_ << endl << "};" << endl << endl;
+ f_header_ <<
+ endl <<
+ "};" << endl <<
+ endl;
}
/**
@@ -336,7 +377,8 @@
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
t_type* type = (*c_iter)->get_type();
- const_interface << "+ (" << type_name(type) << ") " << name << ";" << endl;
+ const_interface <<
+ "+ (" << type_name(type) << ") " << name << ";" << endl;
}
const_interface << "@end";
@@ -348,7 +390,8 @@
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
t_type* type = (*c_iter)->get_type();
- f_impl_ << "static " << type_name(type) << " " << cocoa_prefix_ << name;
+ f_impl_ <<
+ "static " << type_name(type) << " " << cocoa_prefix_ << name;
if (!type->is_container() && !type->is_struct()) {
f_impl_ << " = " << render_const_value(f_impl_, type, (*c_iter)->get_value());
}
@@ -363,13 +406,13 @@
scope_up(f_impl_);
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- if ((*c_iter)->get_type()->is_container() || (*c_iter)->get_type()->is_struct()) {
+ if ((*c_iter)->get_type()->is_container() ||
+ (*c_iter)->get_type()->is_struct()) {
print_const_value(f_impl_,
- cocoa_prefix_ + (*c_iter)->get_name(),
- (*c_iter)->get_type(),
- (*c_iter)->get_value(),
- false,
- false);
+ cocoa_prefix_+(*c_iter)->get_name(),
+ (*c_iter)->get_type(),
+ (*c_iter)->get_value(),
+ false, false);
f_impl_ << ";" << endl;
}
}
@@ -379,7 +422,8 @@
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
t_type* type = (*c_iter)->get_type();
- f_impl_ << "+ (" << type_name(type) << ") " << name;
+ f_impl_ <<
+ "+ (" << type_name(type) << ") " << name;
scope_up(f_impl_);
indent(f_impl_) << "return " << cocoa_prefix_ << name << ";" << endl;
scope_down(f_impl_);
@@ -388,6 +432,7 @@
f_impl_ << "@end" << endl << endl;
}
+
/**
* Generates a struct definition for a thrift data type. This is a class
* with protected data members, read(), write(), and getters and setters.
@@ -409,14 +454,15 @@
generate_cocoa_struct_implementation(f_impl_, txception, true);
}
+
/**
* Generate the interface for a struct
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_interface(ofstream& out,
- t_struct* tstruct,
- bool is_exception) {
+void t_cocoa_generator::generate_cocoa_struct_interface(ofstream &out,
+ t_struct* tstruct,
+ bool is_exception) {
out << "@interface " << cocoa_prefix_ << tstruct->get_name() << " : ";
if (is_exception) {
@@ -443,7 +489,8 @@
out << endl;
// isset fields
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "BOOL __" << (*m_iter)->get_name() << "_isset;" << endl;
+ indent(out) <<
+ "BOOL __" << (*m_iter)->get_name() << "_isset;" << endl;
}
}
@@ -452,8 +499,7 @@
// properties
if (members.size() > 0) {
- out << "#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)"
- << endl;
+ out << "#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
out << indent() << declare_property(*m_iter) << endl;
}
@@ -484,22 +530,24 @@
out << "@end" << endl << endl;
}
+
/**
* Generate signature for initializer of struct with a parameter for
* each field.
*/
-void t_cocoa_generator::generate_cocoa_struct_initializer_signature(ofstream& out,
- t_struct* tstruct) {
+void t_cocoa_generator::generate_cocoa_struct_initializer_signature(ofstream &out,
+ t_struct* tstruct) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
indent(out) << "- (id) initWith";
- for (m_iter = members.begin(); m_iter != members.end();) {
+ for (m_iter = members.begin(); m_iter != members.end(); ) {
if (m_iter == members.begin()) {
out << capitalize((*m_iter)->get_name());
} else {
out << (*m_iter)->get_name();
}
- out << ": (" << type_name((*m_iter)->get_type()) << ") " << (*m_iter)->get_name();
+ out << ": (" << type_name((*m_iter)->get_type()) << ") " <<
+ (*m_iter)->get_name();
++m_iter;
if (m_iter != members.end()) {
out << " ";
@@ -511,30 +559,31 @@
* Generate getter and setter declarations for all fields, plus an
* IsSet getter.
*/
-void t_cocoa_generator::generate_cocoa_struct_field_accessor_declarations(ofstream& out,
+void t_cocoa_generator::generate_cocoa_struct_field_accessor_declarations(ofstream &out,
t_struct* tstruct,
bool is_exception) {
- (void)is_exception;
+ (void) is_exception;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
out << indent() << "#if !__has_feature(objc_arc)" << endl;
- out << indent() << "- (" << type_name((*m_iter)->get_type()) << ") "
- << decapitalize((*m_iter)->get_name()) << ";" << endl;
- out << indent() << "- (void) set" << capitalize((*m_iter)->get_name()) << ": ("
- << type_name((*m_iter)->get_type()) << ") " << (*m_iter)->get_name() << ";" << endl;
+ out << indent() << "- (" << type_name((*m_iter)->get_type()) << ") " << decapitalize((*m_iter)->get_name()) << ";" << endl;
+ out << indent() << "- (void) set" << capitalize((*m_iter)->get_name()) <<
+ ": (" << type_name((*m_iter)->get_type()) << ") " << (*m_iter)->get_name() << ";" << endl;
out << indent() << "#endif" << endl;
out << indent() << "- (BOOL) " << (*m_iter)->get_name() << "IsSet;" << endl << endl;
}
}
+
/**
* Generate the initWithCoder method for this struct so it's compatible with
* the NSCoding protocol
*/
-void t_cocoa_generator::generate_cocoa_struct_init_with_coder_method(ofstream& out,
+void t_cocoa_generator::generate_cocoa_struct_init_with_coder_method(ofstream &out,
t_struct* tstruct,
- bool is_exception) {
+ bool is_exception)
+{
indent(out) << "- (id) initWithCoder: (NSCoder *) decoder" << endl;
scope_up(out);
if (is_exception) {
@@ -549,39 +598,42 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
- out << indent() << "if ([decoder containsValueForKey: @\"" << (*m_iter)->get_name() << "\"])"
- << endl;
+ out << indent() << "if ([decoder containsValueForKey: @\""<< (*m_iter)->get_name() <<"\"])" << endl;
scope_up(out);
out << indent() << "__" << (*m_iter)->get_name() << " = ";
- if (type_can_be_null(t)) {
- out << "[[decoder decodeObjectForKey: @\"" << (*m_iter)->get_name() << "\"] retain_stub];"
- << endl;
- } else if (t->is_enum()) {
+ if (type_can_be_null(t))
+ {
+ out << "[[decoder decodeObjectForKey: @\"" << (*m_iter)->get_name() << "\"] retain_stub];" << endl;
+ }
+ else if (t->is_enum())
+ {
out << "[decoder decodeIntForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- } else {
- t_base_type::t_base tbase = ((t_base_type*)t)->get_base();
- switch (tbase) {
- case t_base_type::TYPE_BOOL:
- out << "[decoder decodeBoolForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_BYTE:
- out << "[decoder decodeIntForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I16:
- out << "[decoder decodeIntForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I32:
- out << "[decoder decodeInt32ForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I64:
- out << "[decoder decodeInt64ForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "[decoder decodeDoubleForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
- break;
- default:
- throw "compiler error: don't know how to decode thrift type: "
- + t_base_type::t_base_name(tbase);
+ }
+ else
+ {
+ t_base_type::t_base tbase = ((t_base_type *) t)->get_base();
+ switch (tbase)
+ {
+ case t_base_type::TYPE_BOOL:
+ out << "[decoder decodeBoolForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "[decoder decodeIntForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I16:
+ out << "[decoder decodeIntForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I32:
+ out << "[decoder decodeInt32ForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I64:
+ out << "[decoder decodeInt64ForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "[decoder decodeDoubleForKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ default:
+ throw "compiler error: don't know how to decode thrift type: " + t_base_type::t_base_name(tbase);
}
}
out << indent() << "__" << (*m_iter)->get_name() << "_isset = YES;" << endl;
@@ -593,13 +645,15 @@
out << endl;
}
+
/**
* Generate the encodeWithCoder method for this struct so it's compatible with
* the NSCoding protocol
*/
-void t_cocoa_generator::generate_cocoa_struct_encode_with_coder_method(ofstream& out,
+void t_cocoa_generator::generate_cocoa_struct_encode_with_coder_method(ofstream &out,
t_struct* tstruct,
- bool is_exception) {
+ bool is_exception)
+{
indent(out) << "- (void) encodeWithCoder: (NSCoder *) encoder" << endl;
scope_up(out);
if (is_exception) {
@@ -612,45 +666,42 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
- out << indent() << "if (__" << (*m_iter)->get_name() << "_isset)" << endl;
+ out << indent() << "if (__"<< (*m_iter)->get_name() <<"_isset)" << endl;
scope_up(out);
- // out << indent() << "__" << (*m_iter)->get_name() << " = ";
- if (type_can_be_null(t)) {
- out << indent() << "[encoder encodeObject: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- } else if (t->is_enum()) {
- out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- } else {
- t_base_type::t_base tbase = ((t_base_type*)t)->get_base();
- switch (tbase) {
- case t_base_type::TYPE_BOOL:
- out << indent() << "[encoder encodeBool: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_BYTE:
- out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I16:
- out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I32:
- out << indent() << "[encoder encodeInt32: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_I64:
- out << indent() << "[encoder encodeInt64: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- out << indent() << "[encoder encodeDouble: __" << (*m_iter)->get_name() << " forKey: @\""
- << (*m_iter)->get_name() << "\"];" << endl;
- break;
- default:
- throw "compiler error: don't know how to encode thrift type: "
- + t_base_type::t_base_name(tbase);
+ //out << indent() << "__" << (*m_iter)->get_name() << " = ";
+ if (type_can_be_null(t))
+ {
+ out << indent() << "[encoder encodeObject: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ }
+ else if (t->is_enum())
+ {
+ out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ }
+ else
+ {
+ t_base_type::t_base tbase = ((t_base_type *) t)->get_base();
+ switch (tbase)
+ {
+ case t_base_type::TYPE_BOOL:
+ out << indent() << "[encoder encodeBool: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I16:
+ out << indent() << "[encoder encodeInt: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I32:
+ out << indent() << "[encoder encodeInt32: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_I64:
+ out << indent() << "[encoder encodeInt64: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << indent() << "[encoder encodeDouble: __" << (*m_iter)->get_name() << " forKey: @\"" << (*m_iter)->get_name() << "\"];" << endl;
+ break;
+ default:
+ throw "compiler error: don't know how to encode thrift type: " + t_base_type::t_base_name(tbase);
}
}
scope_down(out);
@@ -660,6 +711,7 @@
out << endl;
}
+
/**
* Generate struct implementation.
*
@@ -667,11 +719,12 @@
* @param is_exception Is this an exception?
* @param is_result If this is a result it needs a different writer
*/
-void t_cocoa_generator::generate_cocoa_struct_implementation(ofstream& out,
+void t_cocoa_generator::generate_cocoa_struct_implementation(ofstream &out,
t_struct* tstruct,
bool is_exception,
bool is_result) {
- indent(out) << "@implementation " << cocoa_prefix_ << tstruct->get_name() << endl << endl;
+ indent(out) <<
+ "@implementation " << cocoa_prefix_ << tstruct->get_name() << endl << endl;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -680,8 +733,8 @@
if (is_exception) {
out << indent() << "- (id) init" << endl;
scope_up(out);
- out << indent() << "return [super initWithName: @\"" << cocoa_prefix_ << tstruct->get_name()
- << "\" reason: @\"unknown\" userInfo: nil];" << endl;
+ out << indent() << "return [super initWithName: @\"" << cocoa_prefix_ << tstruct->get_name() <<
+ "\" reason: @\"unknown\" userInfo: nil];" << endl;
scope_down(out);
out << endl;
} else {
@@ -693,17 +746,11 @@
scope_up(out);
indent(out) << "self = [super init];" << endl;
if (members.size() > 0) {
- out << "#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)"
- << endl;
+ out << "#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL) {
- print_const_value(out,
- "self." + (*m_iter)->get_name(),
- t,
- (*m_iter)->get_value(),
- false,
- true);
+ print_const_value(out, "self."+(*m_iter)->get_name(), t, (*m_iter)->get_value(), false, true);
}
}
out << "#endif" << endl;
@@ -776,13 +823,16 @@
out << "@end" << endl << endl;
}
+
/**
* Generates a function to read all the fields of the struct.
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_reader(ofstream& out, t_struct* tstruct) {
- out << "- (void) read: (id <TProtocol>) inProtocol" << endl;
+void t_cocoa_generator::generate_cocoa_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ "- (void) read: (id <TProtocol>) inProtocol" << endl;
scope_up(out);
const vector<t_field*>& fields = tstruct->get_members();
@@ -797,80 +847,90 @@
indent(out) << "[inProtocol readStructBeginReturningName: NULL];" << endl;
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
- scope_up(out);
+ indent(out) <<
+ "while (true)" << endl;
+ scope_up(out);
- // Read beginning field marker
- indent(out)
- << "[inProtocol readFieldBeginReturningName: &fieldName type: &fieldType fieldID: &fieldID];"
- << endl;
+ // Read beginning field marker
+ indent(out) <<
+ "[inProtocol readFieldBeginReturningName: &fieldName type: &fieldType fieldID: &fieldID];" << endl;
- // Check for field STOP marker and break
- indent(out) << "if (fieldType == TType_STOP) { " << endl;
- indent_up();
- indent(out) << "break;" << endl;
- indent_down();
- indent(out) << "}" << endl;
-
- // Switch statement on the field we are reading
- indent(out) << "switch (fieldID)" << endl;
-
- scope_up(out);
-
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (fieldType == TType_STOP) { " << endl;
indent_up();
- indent(out) << "if (fieldType == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent_up();
+ indent(out) <<
+ "break;" << endl;
+ indent_down();
+ indent(out) <<
+ "}" << endl;
- generate_deserialize_field(out, *f_iter, "fieldValue");
- indent(out) << call_field_setter(*f_iter, "fieldValue") << endl;
- // if this is an allocated field, release it since the struct
- // is now retaining it
- if (type_can_be_null((*f_iter)->get_type())) {
- // deserialized strings are autorelease, so don't release them
- if (!(get_true_type((*f_iter)->get_type())->is_string())) {
- indent(out) << "[fieldValue release_stub];" << endl;
+ // Switch statement on the field we are reading
+ indent(out) <<
+ "switch (fieldID)" << endl;
+
+ scope_up(out);
+
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
+ indent_up();
+ indent(out) <<
+ "if (fieldType == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+
+ generate_deserialize_field(out, *f_iter, "fieldValue");
+ indent(out) << call_field_setter(*f_iter, "fieldValue") << endl;
+ // if this is an allocated field, release it since the struct
+ // is now retaining it
+ if (type_can_be_null((*f_iter)->get_type())) {
+ // deserialized strings are autorelease, so don't release them
+ if (!(get_true_type((*f_iter)->get_type())->is_string())) {
+ indent(out) << "[fieldValue release_stub];" << endl;
+ }
+ }
+
+ indent_down();
+ out << indent() << "} else { " << endl;
+ if (log_unexpected_) {
+ out << indent() << " NSLog(@\"%s: field ID %i has unexpected type %i. Skipping.\", __PRETTY_FUNCTION__, fieldID, fieldType);" << endl;
+ }
+ out << indent() << " [TProtocolUtil skipType: fieldType onProtocol: inProtocol];" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
}
+
+ // In the default case we skip the field
+ out << indent() << "default:" << endl;
+ if (log_unexpected_) {
+ out << indent() << " NSLog(@\"%s: unexpected field ID %i with type %i. Skipping.\", __PRETTY_FUNCTION__, fieldID, fieldType);" << endl;
+ }
+ out << indent() << " [TProtocolUtil skipType: fieldType onProtocol: inProtocol];" << endl <<
+ indent() << " break;" << endl;
+
+ scope_down(out);
+
+ // Read field end marker
+ indent(out) <<
+ "[inProtocol readFieldEnd];" << endl;
+
+ scope_down(out);
+
+ out <<
+ indent() << "[inProtocol readStructEnd];" << endl;
+
+ // performs various checks (e.g. check that all required fields are set)
+ if (validate_required_) {
+ out <<
+ indent() << "[self validate];" << endl;
}
- indent_down();
- out << indent() << "} else { " << endl;
- if (log_unexpected_) {
- out << indent() << " NSLog(@\"%s: field ID %i has unexpected type %i. Skipping.\", "
- "__PRETTY_FUNCTION__, fieldID, fieldType);" << endl;
- }
- out << indent() << " [TProtocolUtil skipType: fieldType onProtocol: inProtocol];" << endl
- << indent() << "}" << endl << indent() << "break;" << endl;
- indent_down();
- }
-
- // In the default case we skip the field
- out << indent() << "default:" << endl;
- if (log_unexpected_) {
- out << indent() << " NSLog(@\"%s: unexpected field ID %i with type %i. Skipping.\", "
- "__PRETTY_FUNCTION__, fieldID, fieldType);" << endl;
- }
- out << indent() << " [TProtocolUtil skipType: fieldType onProtocol: inProtocol];" << endl
- << indent() << " break;" << endl;
-
- scope_down(out);
-
- // Read field end marker
- indent(out) << "[inProtocol readFieldEnd];" << endl;
-
- scope_down(out);
-
- out << indent() << "[inProtocol readStructEnd];" << endl;
-
- // performs various checks (e.g. check that all required fields are set)
- if (validate_required_) {
- out << indent() << "[self validate];" << endl;
- }
-
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -878,34 +938,40 @@
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "- (void) write: (id <TProtocol>) outProtocol {" << endl;
+void t_cocoa_generator::generate_cocoa_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "- (void) write: (id <TProtocol>) outProtocol {" << endl;
indent_up();
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out << indent() << "[outProtocol writeStructBeginWithName: @\"" << name << "\"];" << endl;
+ out <<
+ indent() << "[outProtocol writeStructBeginWithName: @\"" << name << "\"];" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- out << indent() << "if (__" << (*f_iter)->get_name() << "_isset) {" << endl;
+ out <<
+ indent() << "if (__" << (*f_iter)->get_name() << "_isset) {" << endl;
indent_up();
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (__" << (*f_iter)->get_name() << " != nil) {" << endl;
+ out <<
+ indent() << "if (__" << (*f_iter)->get_name() << " != nil) {" << endl;
indent_up();
}
- indent(out) << "[outProtocol writeFieldBeginWithName: @\"" << (*f_iter)->get_name()
- << "\" type: " << type_to_enum((*f_iter)->get_type())
- << " fieldID: " << (*f_iter)->get_key() << "];" << endl;
+ indent(out) << "[outProtocol writeFieldBeginWithName: @\"" <<
+ (*f_iter)->get_name() << "\" type: " << type_to_enum((*f_iter)->get_type()) <<
+ " fieldID: " << (*f_iter)->get_key() << "];" << endl;
// Write field contents
- generate_serialize_field(out, *f_iter, "__" + (*f_iter)->get_name());
+ generate_serialize_field(out, *f_iter, "__"+(*f_iter)->get_name());
// Write field closer
- indent(out) << "[outProtocol writeFieldEnd];" << endl;
+ indent(out) <<
+ "[outProtocol writeFieldEnd];" << endl;
if (null_allowed) {
scope_down(out);
@@ -913,11 +979,14 @@
scope_down(out);
}
// Write the struct map
- out << indent() << "[outProtocol writeFieldStop];" << endl << indent()
- << "[outProtocol writeStructEnd];" << endl;
+ out <<
+ indent() << "[outProtocol writeFieldStop];" << endl <<
+ indent() << "[outProtocol writeStructEnd];" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -927,43 +996,52 @@
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_result_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "- (void) write: (id <TProtocol>) outProtocol {" << endl;
+void t_cocoa_generator::generate_cocoa_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "- (void) write: (id <TProtocol>) outProtocol {" << endl;
indent_up();
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out << indent() << "[outProtocol writeStructBeginWithName: @\"" << name << "\"];" << endl;
+ out <<
+ indent() << "[outProtocol writeStructBeginWithName: @\"" << name << "\"];" << endl;
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
- out << endl << indent() << "if ";
+ out <<
+ endl <<
+ indent() << "if ";
} else {
- out << " else if ";
+ out <<
+ " else if ";
}
- out << "(__" << (*f_iter)->get_name() << "_isset) {" << endl;
+ out <<
+ "(__" << (*f_iter)->get_name() << "_isset) {" << endl;
indent_up();
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (__" << (*f_iter)->get_name() << " != nil) {" << endl;
+ out <<
+ indent() << "if (__" << (*f_iter)->get_name() << " != nil) {" << endl;
indent_up();
}
- indent(out) << "[outProtocol writeFieldBeginWithName: @\"" << (*f_iter)->get_name()
- << "\" type: " << type_to_enum((*f_iter)->get_type())
- << " fieldID: " << (*f_iter)->get_key() << "];" << endl;
+ indent(out) << "[outProtocol writeFieldBeginWithName: @\"" <<
+ (*f_iter)->get_name() << "\" type: " << type_to_enum((*f_iter)->get_type()) <<
+ " fieldID: " << (*f_iter)->get_key() << "];" << endl;
// Write field contents
- generate_serialize_field(out, *f_iter, "__" + (*f_iter)->get_name());
+ generate_serialize_field(out, *f_iter, "__"+(*f_iter)->get_name());
// Write field closer
- indent(out) << "[outProtocol writeFieldEnd];" << endl;
+ indent(out) <<
+ "[outProtocol writeFieldEnd];" << endl;
if (null_allowed) {
indent_down();
@@ -974,11 +1052,15 @@
indent(out) << "}";
}
// Write the struct map
- out << endl << indent() << "[outProtocol writeFieldStop];" << endl << indent()
- << "[outProtocol writeStructEnd];" << endl;
+ out <<
+ endl <<
+ indent() << "[outProtocol writeFieldStop];" << endl <<
+ indent() << "[outProtocol writeStructEnd];" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -987,8 +1069,10 @@
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_validator(ofstream& out, t_struct* tstruct) {
- out << indent() << "- (void) validate {" << endl;
+void t_cocoa_generator::generate_cocoa_struct_validator(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "- (void) validate {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
@@ -998,15 +1082,18 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = (*f_iter);
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- out << indent() << "if (!__" << field->get_name() << "_isset) {" << endl << indent()
- << " @throw [TProtocolException exceptionWithName: @\"TProtocolException\"" << endl
- << indent() << " reason: @\"Required field '"
- << (*f_iter)->get_name() << "' is not set.\"];" << endl << indent() << "}" << endl;
+ out <<
+ indent() << "if (!__" << field->get_name() << "_isset) {" << endl <<
+ indent() << " @throw [TProtocolException exceptionWithName: @\"TProtocolException\"" << endl <<
+ indent() << " reason: @\"Required field '" << (*f_iter)->get_name() << "' is not set.\"];" << endl <<
+ indent() << "}" << endl;
}
}
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -1018,7 +1105,7 @@
void t_cocoa_generator::generate_cocoa_struct_field_accessor_implementations(ofstream& out,
t_struct* tstruct,
bool is_exception) {
- (void)is_exception;
+ (void) is_exception;
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1041,8 +1128,8 @@
indent(out) << "}" << endl << endl;
// Simple setter
- indent(out) << "- (void) set" << cap_name << ": (" << type_name(type) << ") " << field_name
- << " {" << endl;
+ indent(out) << "- (void) set" << cap_name << ": (" << type_name(type) <<
+ ") " << field_name << " {" << endl;
indent_up();
if (!type_can_be_null(type)) {
indent(out) << "__" << field_name << " = " << field_name << ";" << endl;
@@ -1080,12 +1167,15 @@
*
* @param tstruct The struct definition
*/
-void t_cocoa_generator::generate_cocoa_struct_description(ofstream& out, t_struct* tstruct) {
- out << indent() << "- (NSString *) description {" << endl;
+void t_cocoa_generator::generate_cocoa_struct_description(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "- (NSString *) description {" << endl;
indent_up();
- out << indent() << "NSMutableString * ms = [NSMutableString stringWithString: @\""
- << cocoa_prefix_ << tstruct->get_name() << "(\"];" << endl;
+ out <<
+ indent() << "NSMutableString * ms = [NSMutableString stringWithString: @\"" <<
+ cocoa_prefix_ << tstruct->get_name() << "(\"];" << endl;
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -1098,16 +1188,19 @@
indent(out) << "[ms appendString: @\"," << (*f_iter)->get_name() << ":\"];" << endl;
}
t_type* ttype = (*f_iter)->get_type();
- indent(out) << "[ms appendFormat: @\"" << format_string_for_type(ttype) << "\", __"
- << (*f_iter)->get_name() << "];" << endl;
+ indent(out) << "[ms appendFormat: @\"" << format_string_for_type(ttype) << "\", __" <<
+ (*f_iter)->get_name() << "];" << endl;
}
- out << indent() << "[ms appendString: @\")\"];" << endl << indent()
- << "return [NSString stringWithString: ms];" << endl;
+ out <<
+ indent() << "[ms appendString: @\")\"];" << endl <<
+ indent() << "return [NSString stringWithString: ms];" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
+
/**
* Generates a thrift service. In Objective-C this consists of a
* protocol definition, a client interface and a client implementation.
@@ -1123,6 +1216,7 @@
generate_cocoa_service_server_implementation(f_impl_, tservice);
}
+
/**
* Generates structs for all the service return types
*
@@ -1147,10 +1241,12 @@
}
}
+
string t_cocoa_generator::function_args_helper_struct_type(t_function* tfunction) {
return tfunction->get_name() + "_args";
}
+
/**
* Generates a struct and helpers for a function.
*
@@ -1181,19 +1277,21 @@
generate_cocoa_struct_implementation(f_impl_, &result, false, true);
}
+
/**
* Generates a service protocol definition.
*
* @param tservice The service to generate a protocol definition for
*/
-void t_cocoa_generator::generate_cocoa_service_protocol(ofstream& out, t_service* tservice) {
+void t_cocoa_generator::generate_cocoa_service_protocol(ofstream& out,
+ t_service* tservice) {
out << "@protocol " << cocoa_prefix_ << tservice->get_name() << " <NSObject>" << endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- out << "- " << function_signature(*f_iter) << ";"
- << " // throws ";
+ out << "- " << function_signature(*f_iter) << ";" <<
+ " // throws ";
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
@@ -1205,6 +1303,7 @@
out << "@end" << endl << endl;
}
+
/**
* Generates a service client interface definition.
*
@@ -1212,8 +1311,8 @@
*/
void t_cocoa_generator::generate_cocoa_service_client_interface(ofstream& out,
t_service* tservice) {
- out << "@interface " << cocoa_prefix_ << tservice->get_name() << "Client : NSObject <"
- << cocoa_prefix_ << tservice->get_name() << "> ";
+ out << "@interface " << cocoa_prefix_ << tservice->get_name() << "Client : NSObject <" <<
+ cocoa_prefix_ << tservice->get_name() << "> ";
scope_up(out);
out << indent() << "id <TProtocol> inProtocol;" << endl;
@@ -1221,35 +1320,33 @@
scope_down(out);
out << "- (id) initWithProtocol: (id <TProtocol>) protocol;" << endl;
- out << "- (id) initWithInProtocol: (id <TProtocol>) inProtocol outProtocol: (id <TProtocol>) "
- "outProtocol;" << endl;
+ out << "- (id) initWithInProtocol: (id <TProtocol>) inProtocol outProtocol: (id <TProtocol>) outProtocol;" << endl;
out << "@end" << endl << endl;
}
+
/**
- * Generates a service server interface definition. In other words, the TProcess implementation for
- *the
+ * Generates a service server interface definition. In other words, the TProcess implementation for the
* service definition.
*
* @param tservice The service to generate a client interface definition for
*/
void t_cocoa_generator::generate_cocoa_service_server_interface(ofstream& out,
t_service* tservice) {
- out << "@interface " << cocoa_prefix_ << tservice->get_name()
- << "Processor : NSObject <TProcessor> ";
+ out << "@interface " << cocoa_prefix_ << tservice->get_name() << "Processor : NSObject <TProcessor> ";
scope_up(out);
- out << indent() << "id <" << cocoa_prefix_ << tservice->get_name() << "> mService;" << endl;
+ out << indent() << "id <" << cocoa_prefix_ << tservice->get_name() <<"> mService;" << endl;
out << indent() << "NSDictionary * mMethodMap;" << endl;
scope_down(out);
- out << "- (id) initWith" << tservice->get_name() << ": (id <" << cocoa_prefix_
- << tservice->get_name() << ">) service;" << endl;
- out << "- (id<" << cocoa_prefix_ << tservice->get_name() << ">) service;" << endl;
+ out << "- (id) initWith" << tservice->get_name() << ": (id <" << cocoa_prefix_ << tservice->get_name() << ">) service;" << endl;
+ out << "- (id<"<<cocoa_prefix_ << tservice->get_name() << ">) service;" << endl;
out << "@end" << endl << endl;
}
+
/**
* Generates a service client implementation.
*
@@ -1266,8 +1363,7 @@
scope_down(out);
out << endl;
- out << "- (id) initWithInProtocol: (id <TProtocol>) anInProtocol outProtocol: (id <TProtocol>) "
- "anOutProtocol" << endl;
+ out << "- (id) initWithInProtocol: (id <TProtocol>) anInProtocol outProtocol: (id <TProtocol>) anOutProtocol" << endl;
scope_up(out);
out << indent() << "self = [super init];" << endl;
out << indent() << "inProtocol = [anInProtocol retain_stub];" << endl;
@@ -1298,15 +1394,18 @@
string argsname = (*f_iter)->get_name() + "_args";
// Open function
- indent(out) << "- " << function_signature(&send_function) << endl;
+ indent(out) <<
+ "- " << function_signature(&send_function) << endl;
scope_up(out);
// Serialize the request
- out << indent() << "[outProtocol writeMessageBeginWithName: @\"" << funname << "\""
- << ((*f_iter)->is_oneway() ? " type: TMessageType_ONEWAY" : " type: TMessageType_CALL")
- << " sequenceID: 0];" << endl;
+ out <<
+ indent() << "[outProtocol writeMessageBeginWithName: @\"" << funname << "\"" <<
+ ((*f_iter)->is_oneway() ? " type: TMessageType_ONEWAY" : " type: TMessageType_CALL") <<
+ " sequenceID: 0];" << endl;
- out << indent() << "[outProtocol writeStructBeginWithName: @\"" << argsname << "\"];" << endl;
+ out <<
+ indent() << "[outProtocol writeStructBeginWithName: @\"" << argsname << "\"];" << endl;
// write out function parameters
t_struct* arg_struct = (*f_iter)->get_arglist();
@@ -1318,25 +1417,29 @@
out << indent() << "if (" << fieldName << " != nil)";
scope_up(out);
}
- out << indent() << "[outProtocol writeFieldBeginWithName: @\"" << fieldName
- << "\""
- " type: " << type_to_enum((*fld_iter)->get_type())
- << " fieldID: " << (*fld_iter)->get_key() << "];" << endl;
+ out <<
+ indent() << "[outProtocol writeFieldBeginWithName: @\"" << fieldName << "\""
+ " type: " << type_to_enum((*fld_iter)->get_type()) <<
+ " fieldID: " << (*fld_iter)->get_key() << "];" << endl;
generate_serialize_field(out, *fld_iter, fieldName);
- out << indent() << "[outProtocol writeFieldEnd];" << endl;
+ out <<
+ indent() << "[outProtocol writeFieldEnd];" << endl;
if (type_can_be_null((*fld_iter)->get_type())) {
scope_down(out);
}
}
- out << indent() << "[outProtocol writeFieldStop];" << endl;
- out << indent() << "[outProtocol writeStructEnd];" << endl;
+ out <<
+ indent() << "[outProtocol writeFieldStop];" << endl;
+ out <<
+ indent() << "[outProtocol writeStructEnd];" << endl;
- out << indent() << "[outProtocol writeMessageEnd];" << endl << indent()
- << "[[outProtocol transport] flush];" << endl;
+ out <<
+ indent() << "[outProtocol writeMessageEnd];" << endl <<
+ indent() << "[[outProtocol transport] flush];" << endl;
scope_down(out);
out << endl;
@@ -1348,49 +1451,56 @@
&noargs,
(*f_iter)->get_xceptions());
// Open function
- indent(out) << "- " << function_signature(&recv_function) << endl;
+ indent(out) <<
+ "- " << function_signature(&recv_function) << endl;
scope_up(out);
// TODO(mcslee): Message validation here, was the seqid etc ok?
// check for an exception
- out << indent() << "int msgType = 0;" << endl << indent()
- << "[inProtocol readMessageBeginReturningName: nil type: &msgType sequenceID: NULL];"
- << endl << indent() << "if (msgType == TMessageType_EXCEPTION) {" << endl << indent()
- << " TApplicationException * x = [TApplicationException read: inProtocol];" << endl
- << indent() << " [inProtocol readMessageEnd];" << endl << indent() << " @throw x;"
- << endl << indent() << "}" << endl;
+ out <<
+ indent() << "int msgType = 0;" << endl <<
+ indent() << "[inProtocol readMessageBeginReturningName: nil type: &msgType sequenceID: NULL];" << endl <<
+ indent() << "if (msgType == TMessageType_EXCEPTION) {" << endl <<
+ indent() << " TApplicationException * x = [TApplicationException read: inProtocol];" << endl <<
+ indent() << " [inProtocol readMessageEnd];" << endl <<
+ indent() << " @throw x;" << endl <<
+ indent() << "}" << endl;
// FIXME - could optimize here to reduce creation of temporary objects.
string resultname = function_result_helper_struct_type(*f_iter);
- out << indent() << cocoa_prefix_ << resultname << " * result = [[[" << cocoa_prefix_
- << resultname << " alloc] init] autorelease_stub];" << endl;
+ out <<
+ indent() << cocoa_prefix_ << resultname << " * result = [[[" << cocoa_prefix_ <<
+ resultname << " alloc] init] autorelease_stub];" << endl;
indent(out) << "[result read: inProtocol];" << endl;
indent(out) << "[inProtocol readMessageEnd];" << endl;
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- out << indent() << "if ([result successIsSet]) {" << endl << indent()
- << " return [result success];" << endl << indent() << "}" << endl;
+ out <<
+ indent() << "if ([result successIsSet]) {" << endl <<
+ indent() << " return [result success];" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- out << indent() << "if ([result " << (*x_iter)->get_name() << "IsSet]) {" << endl
- << indent() << " @throw [result " << (*x_iter)->get_name() << "];" << endl << indent()
- << "}" << endl;
+ out <<
+ indent() << "if ([result " << (*x_iter)->get_name() << "IsSet]) {" << endl <<
+ indent() << " @throw [result " << (*x_iter)->get_name() << "];" << endl <<
+ indent() << "}" << endl;
}
// If you get here it's an exception, unless a void function
if ((*f_iter)->get_returntype()->is_void()) {
- indent(out) << "return;" << endl;
+ indent(out) <<
+ "return;" << endl;
} else {
- out << indent() << "@throw [TApplicationException exceptionWithType: "
- "TApplicationException_MISSING_RESULT" << endl << indent()
- << " reason: @\"" << (*f_iter)->get_name()
- << " failed: unknown result\"];" << endl;
+ out <<
+ indent() << "@throw [TApplicationException exceptionWithType: TApplicationException_MISSING_RESULT" << endl <<
+ indent() << " reason: @\"" << (*f_iter)->get_name() << " failed: unknown result\"];" << endl;
}
// Close function
@@ -1399,9 +1509,11 @@
}
// Open function
- indent(out) << "- " << function_signature(*f_iter) << endl;
+ indent(out) <<
+ "- " << function_signature(*f_iter) << endl;
scope_up(out);
- indent(out) << "[self send_" << funname;
+ indent(out) <<
+ "[self send_" << funname;
// Declare the function arguments
bool first = true;
@@ -1422,7 +1534,8 @@
if (!(*f_iter)->get_returntype()->is_void()) {
out << "return ";
}
- out << "[self recv_" << funname << "];" << endl;
+ out <<
+ "[self recv_" << funname << "];" << endl;
}
scope_down(out);
out << endl;
@@ -1433,6 +1546,7 @@
out << "@end" << endl << endl;
}
+
/**
* Generates a service server implementation. In other words the actual TProcessor implementation
* for the service.
@@ -1446,8 +1560,7 @@
// initializer
out << endl;
- out << "- (id) initWith" << tservice->get_name() << ": (id <" << cocoa_prefix_
- << tservice->get_name() << ">) service" << endl;
+ out << "- (id) initWith" << tservice->get_name() << ": (id <" << cocoa_prefix_ << tservice->get_name() << ">) service" << endl;
scope_up(out);
out << indent() << "self = [super init];" << endl;
out << indent() << "if (!self) {" << endl;
@@ -1462,11 +1575,9 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string funname = (*f_iter)->get_name();
scope_up(out);
- out << indent() << "SEL s = @selector(process_" << funname
- << "_withSequenceID:inProtocol:outProtocol:);" << endl;
+ out << indent() << "SEL s = @selector(process_" << funname << "_withSequenceID:inProtocol:outProtocol:);" << endl;
out << indent() << "NSMethodSignature * sig = [self methodSignatureForSelector: s];" << endl;
- out << indent()
- << "NSInvocation * invocation = [NSInvocation invocationWithMethodSignature: sig];" << endl;
+ out << indent() << "NSInvocation * invocation = [NSInvocation invocationWithMethodSignature: sig];" << endl;
out << indent() << "[invocation setSelector: s];" << endl;
out << indent() << "[invocation retainArguments];" << endl;
out << indent() << "[mMethodMap setValue: invocation forKey: @\"" << funname << "\"];" << endl;
@@ -1478,7 +1589,7 @@
// implementation of the 'service' method which returns the service associated with this
// processor
out << endl;
- out << indent() << "- (id<" << cocoa_prefix_ << tservice->get_name() << ">) service" << endl;
+ out << indent() << "- (id<"<<cocoa_prefix_ << tservice->get_name() << ">) service" << endl;
out << indent() << "{" << endl;
out << indent() << " return [[mService retain_stub] autorelease_stub];" << endl;
out << indent() << "}" << endl;
@@ -1486,7 +1597,7 @@
// implementation of the TProcess method, which dispatches the incoming call using the method map
out << endl;
out << indent() << "- (BOOL) processOnInputProtocol: (id <TProtocol>) inProtocol" << endl;
- out << indent() << " outputProtocol: (id <TProtocol>) outProtocol" << endl;
+ out << indent() << " outputProtocol: (id <TProtocol>) outProtocol" <<endl;
out << indent() << "{" << endl;
out << indent() << " NSString * messageName;" << endl;
out << indent() << " int messageType;" << endl;
@@ -1498,9 +1609,7 @@
out << indent() << " if (invocation == nil) {" << endl;
out << indent() << " [TProtocolUtil skipType: TType_STRUCT onProtocol: inProtocol];" << endl;
out << indent() << " [inProtocol readMessageEnd];" << endl;
- out << indent() << " TApplicationException * x = [TApplicationException exceptionWithType: "
- "TApplicationException_UNKNOWN_METHOD reason: [NSString stringWithFormat: "
- "@\"Invalid method name: '%@'\", messageName]];" << endl;
+ out << indent() << " TApplicationException * x = [TApplicationException exceptionWithType: TApplicationException_UNKNOWN_METHOD reason: [NSString stringWithFormat: @\"Invalid method name: '%@'\", messageName]];" << endl;
out << indent() << " [outProtocol writeMessageBeginWithName: messageName" << endl;
out << indent() << " type: TMessageType_EXCEPTION" << endl;
out << indent() << " sequenceID: seqID];" << endl;
@@ -1510,8 +1619,7 @@
out << indent() << " return YES;" << endl;
out << indent() << " }" << endl;
out << indent() << " // NSInvocation does not conform to NSCopying protocol" << endl;
- out << indent() << " NSInvocation * i = [NSInvocation invocationWithMethodSignature: "
- "[invocation methodSignature]];" << endl;
+ out << indent() << " NSInvocation * i = [NSInvocation invocationWithMethodSignature: [invocation methodSignature]];" << endl;
out << indent() << " [i setSelector: [invocation selector]];" << endl;
out << indent() << " [i setArgument: &seqID atIndex: 2];" << endl;
out << indent() << " [i setArgument: &inProtocol atIndex: 3];" << endl;
@@ -1521,15 +1629,12 @@
out << indent() << " return YES;" << endl;
out << indent() << "}" << endl;
- // generate a process_XXXX method for each service function, which reads args, calls the service,
- // and writes results
+ // generate a process_XXXX method for each service function, which reads args, calls the service, and writes results
functions = tservice->get_functions();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
out << endl;
string funname = (*f_iter)->get_name();
- out << indent() << "- (void) process_" << funname
- << "_withSequenceID: (int32_t) seqID inProtocol: (id<TProtocol>) inProtocol outProtocol: "
- "(id<TProtocol>) outProtocol" << endl;
+ out << indent() << "- (void) process_" << funname << "_withSequenceID: (int32_t) seqID inProtocol: (id<TProtocol>) inProtocol outProtocol: (id<TProtocol>) outProtocol" << endl;
scope_up(out);
string argstype = cocoa_prefix_ + function_args_helper_struct_type(*f_iter);
out << indent() << argstype << " * args = [[" << argstype << " alloc] init];" << endl;
@@ -1538,8 +1643,8 @@
// prepare the result if not oneway
if (!(*f_iter)->is_oneway()) {
- string resulttype = cocoa_prefix_ + function_result_helper_struct_type(*f_iter);
- out << indent() << resulttype << " * result = [[" << resulttype << " alloc] init];" << endl;
+ string resulttype = cocoa_prefix_ + function_result_helper_struct_type(*f_iter);
+ out << indent() << resulttype << " * result = [[" << resulttype << " alloc] init];" << endl;
}
// make the call to the actual service object
@@ -1570,13 +1675,13 @@
// write out the result if not oneway
if (!(*f_iter)->is_oneway()) {
- out << indent() << "[outProtocol writeMessageBeginWithName: @\"" << funname << "\"" << endl;
- out << indent() << " type: TMessageType_REPLY" << endl;
- out << indent() << " sequenceID: seqID];" << endl;
- out << indent() << "[result write: outProtocol];" << endl;
- out << indent() << "[outProtocol writeMessageEnd];" << endl;
- out << indent() << "[[outProtocol transport] flush];" << endl;
- out << indent() << "[result release_stub];" << endl;
+ out << indent() << "[outProtocol writeMessageBeginWithName: @\"" << funname << "\"" << endl;
+ out << indent() << " type: TMessageType_REPLY" << endl;
+ out << indent() << " sequenceID: seqID];" << endl;
+ out << indent() << "[result write: outProtocol];" << endl;
+ out << indent() << "[outProtocol writeMessageEnd];" << endl;
+ out << indent() << "[[outProtocol transport] flush];" << endl;
+ out << indent() << "[result release_stub];" << endl;
}
out << indent() << "[args release_stub];" << endl;
@@ -1598,6 +1703,7 @@
out << "@end" << endl << endl;
}
+
/**
* Deserializes a field of any type.
*
@@ -1610,21 +1716,26 @@
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, fieldName);
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ fieldName);
} else if (type->is_container()) {
generate_deserialize_container(out, type, fieldName);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << type_name(type) << " " << fieldName << " = [inProtocol ";
+ indent(out) <<
+ type_name(type) << " " << fieldName << " = [inProtocol ";
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: " + tfield->get_name();
+ throw "compiler error: cannot serialize void field in a struct: " +
+ tfield->get_name();
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -1652,17 +1763,16 @@
out << "readDouble];";
break;
default:
- throw "compiler error: no Objective-C name for base type "
- + t_base_type::t_base_name(tbase);
+ throw "compiler error: no Objective-C name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "readI32];";
}
- out << endl;
+ out <<
+ endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
@@ -1672,8 +1782,8 @@
void t_cocoa_generator::generate_deserialize_struct(ofstream& out,
t_struct* tstruct,
string fieldName) {
- indent(out) << type_name(tstruct) << fieldName << " = [[" << type_name(tstruct, true)
- << " alloc] init];" << endl;
+ indent(out) << type_name(tstruct) << fieldName << " = [[" <<
+ type_name(tstruct, true) << " alloc] init];" << endl;
indent(out) << "[" << fieldName << " read: inProtocol];" << endl;
}
@@ -1688,20 +1798,21 @@
// Declare variables, read header
if (ttype->is_map()) {
- indent(out) << "[inProtocol readMapBeginReturningKeyType: NULL valueType: NULL size: &" << size
- << "];" << endl;
- indent(out) << "NSMutableDictionary * " << fieldName
- << " = [[NSMutableDictionary alloc] initWithCapacity: " << size << "];" << endl;
+ indent(out)
+ << "[inProtocol readMapBeginReturningKeyType: NULL valueType: NULL size: &" <<
+ size << "];" << endl;
+ indent(out) << "NSMutableDictionary * " << fieldName <<
+ " = [[NSMutableDictionary alloc] initWithCapacity: " << size << "];" << endl;
} else if (ttype->is_set()) {
- indent(out) << "[inProtocol readSetBeginReturningElementType: NULL size: &" << size << "];"
- << endl;
- indent(out) << "NSMutableSet * " << fieldName
- << " = [[NSMutableSet alloc] initWithCapacity: " << size << "];" << endl;
+ indent(out)
+ << "[inProtocol readSetBeginReturningElementType: NULL size: &" << size << "];" << endl;
+ indent(out) << "NSMutableSet * " << fieldName <<
+ " = [[NSMutableSet alloc] initWithCapacity: " << size << "];" << endl;
} else if (ttype->is_list()) {
- indent(out) << "[inProtocol readListBeginReturningElementType: NULL size: &" << size << "];"
- << endl;
- indent(out) << "NSMutableArray * " << fieldName
- << " = [[NSMutableArray alloc] initWithCapacity: " << size << "];" << endl;
+ indent(out)
+ << "[inProtocol readListBeginReturningElementType: NULL size: &" << size << "];" << endl;
+ indent(out) << "NSMutableArray * " << fieldName <<
+ " = [[NSMutableArray alloc] initWithCapacity: " << size << "];" << endl;
}
// FIXME - the code above does not verify that the element types of
// the containers being read match the element types of the
@@ -1709,21 +1820,22 @@
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "int " << i << ";" << endl << indent() << "for (" << i << " = 0; " << i << " < "
- << size << "; "
- << "++" << i << ")" << endl;
+ indent(out) << "int " << i << ";" << endl <<
+ indent() << "for (" << i << " = 0; " <<
+ i << " < " << size << "; " <<
+ "++" << i << ")" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, fieldName);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, fieldName);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, fieldName);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, fieldName);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, fieldName);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, fieldName);
+ }
- scope_down(out);
+ scope_down(out);
// Read container end
if (ttype->is_map()) {
@@ -1733,14 +1845,18 @@
} else if (ttype->is_list()) {
indent(out) << "[inProtocol readListEnd];" << endl;
}
+
}
+
/**
* Take a variable of a given type and wrap it in code to make it
* suitable for putting into a container, if necessary. Basically,
* wrap scaler primitives in NSNumber objects.
*/
-string t_cocoa_generator::containerize(t_type* ttype, string fieldName) {
+string t_cocoa_generator::containerize(t_type * ttype,
+ string fieldName)
+{
// FIXME - optimize here to avoid autorelease pool?
ttype = get_true_type(ttype);
if (ttype->is_enum()) {
@@ -1771,6 +1887,7 @@
return fieldName;
}
+
/**
* Generates code to deserialize a map element
*/
@@ -1787,8 +1904,9 @@
generate_deserialize_field(out, &fkey, key);
generate_deserialize_field(out, &fval, val);
- indent(out) << "[" << fieldName << " setObject: " << containerize(valType, val)
- << " forKey: " << containerize(keyType, key) << "];" << endl;
+ indent(out) <<
+ "[" << fieldName << " setObject: " << containerize(valType, val) <<
+ " forKey: " << containerize(keyType, key) << "];" << endl;
if (type_can_be_null(keyType)) {
if (!(get_true_type(keyType)->is_string())) {
@@ -1815,7 +1933,8 @@
generate_deserialize_field(out, &felem, elem);
- indent(out) << "[" << fieldName << " addObject: " << containerize(type, elem) << "];" << endl;
+ indent(out) <<
+ "[" << fieldName << " addObject: " << containerize(type, elem) << "];" << endl;
if (type_can_be_null(type)) {
// deserialized strings are autorelease, so don't release them
@@ -1837,7 +1956,8 @@
generate_deserialize_field(out, &felem, elem);
- indent(out) << "[" << fieldName << " addObject: " << containerize(type, elem) << "];" << endl;
+ indent(out) <<
+ "[" << fieldName << " addObject: " << containerize(type, elem) << "];" << endl;
if (type_can_be_null(type)) {
if (!(get_true_type(type)->is_string())) {
@@ -1846,32 +1966,42 @@
}
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param fieldName Name to of the variable holding the field
*/
-void t_cocoa_generator::generate_serialize_field(ofstream& out, t_field* tfield, string fieldName) {
+void t_cocoa_generator::generate_serialize_field(ofstream& out,
+ t_field* tfield,
+ string fieldName) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, fieldName);
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ fieldName);
} else if (type->is_container()) {
- generate_serialize_container(out, type, fieldName);
+ generate_serialize_container(out,
+ type,
+ fieldName);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "[outProtocol ";
+ indent(out) <<
+ "[outProtocol ";
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: " + fieldName;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + fieldName;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -1899,8 +2029,7 @@
out << "writeDouble: " << fieldName << "];";
break;
default:
- throw "compiler error: no Objective-C name for base type "
- + t_base_type::t_base_name(tbase);
+ throw "compiler error: no Objective-C name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "writeI32: " << fieldName << "];";
@@ -1922,8 +2051,9 @@
void t_cocoa_generator::generate_serialize_struct(ofstream& out,
t_struct* tstruct,
string fieldName) {
- (void)tstruct;
- out << indent() << "[" << fieldName << " write: outProtocol];" << endl;
+ (void) tstruct;
+ out <<
+ indent() << "[" << fieldName << " write: outProtocol];" << endl;
}
/**
@@ -1938,18 +2068,21 @@
scope_up(out);
if (ttype->is_map()) {
- indent(out) << "[outProtocol writeMapBeginWithKeyType: "
- << type_to_enum(((t_map*)ttype)->get_key_type())
- << " valueType: " << type_to_enum(((t_map*)ttype)->get_val_type()) << " size: ["
- << fieldName << " count]];" << endl;
+ indent(out) <<
+ "[outProtocol writeMapBeginWithKeyType: " <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << " valueType: " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << " size: [" <<
+ fieldName << " count]];" << endl;
} else if (ttype->is_set()) {
- indent(out) << "[outProtocol writeSetBeginWithElementType: "
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << " size: [" << fieldName
- << " count]];" << endl;
+ indent(out) <<
+ "[outProtocol writeSetBeginWithElementType: " <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << " size: [" <<
+ fieldName << " count]];" << endl;
} else if (ttype->is_list()) {
- indent(out) << "[outProtocol writeListBeginWithElementType: "
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << " size: [" << fieldName
- << " count]];" << endl;
+ indent(out) <<
+ "[outProtocol writeListBeginWithElementType: " <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << " size: [" <<
+ fieldName << " count]];" << endl;
}
string iter = tmp("_iter");
@@ -1961,36 +2094,38 @@
indent(out) << "while ((" << key << " = [" << iter << " nextObject]))" << endl;
} else if (ttype->is_set()) {
key = tmp("obj");
- indent(out) << "NSEnumerator * " << iter << " = [" << fieldName << " objectEnumerator];"
- << endl;
+ indent(out) << "NSEnumerator * " << iter << " = [" << fieldName << " objectEnumerator];" << endl;
indent(out) << "id " << key << ";" << endl;
indent(out) << "while ((" << key << " = [" << iter << " nextObject]))" << endl;
} else if (ttype->is_list()) {
key = tmp("idx");
indent(out) << "int " << key << ";" << endl;
- indent(out) << "for (" << key << " = 0; " << key << " < [" << fieldName << " count]; " << key
- << "++)" << endl;
+ indent(out) <<
+ "for (" << key << " = 0; " << key << " < [" << fieldName << " count]; " << key << "++)" << endl;
}
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_serialize_map_element(out, (t_map*)ttype, key, fieldName);
- } else if (ttype->is_set()) {
- generate_serialize_set_element(out, (t_set*)ttype, key);
- } else if (ttype->is_list()) {
- generate_serialize_list_element(out, (t_list*)ttype, key, fieldName);
- }
+ if (ttype->is_map()) {
+ generate_serialize_map_element(out, (t_map*)ttype, key, fieldName);
+ } else if (ttype->is_set()) {
+ generate_serialize_set_element(out, (t_set*)ttype, key);
+ } else if (ttype->is_list()) {
+ generate_serialize_list_element(out, (t_list*)ttype, key, fieldName);
+ }
- scope_down(out);
+ scope_down(out);
- if (ttype->is_map()) {
- indent(out) << "[outProtocol writeMapEnd];" << endl;
- } else if (ttype->is_set()) {
- indent(out) << "[outProtocol writeSetEnd];" << endl;
- } else if (ttype->is_list()) {
- indent(out) << "[outProtocol writeListEnd];" << endl;
- }
+ if (ttype->is_map()) {
+ indent(out) <<
+ "[outProtocol writeMapEnd];" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "[outProtocol writeSetEnd];" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "[outProtocol writeListEnd];" << endl;
+ }
scope_down(out);
}
@@ -1999,8 +2134,10 @@
* Given a field variable name, wrap it in code that converts it to a
* primitive type, if necessary.
*/
-string t_cocoa_generator::decontainerize(t_field* tfield, string fieldName) {
- t_type* ttype = get_true_type(tfield->get_type());
+string t_cocoa_generator::decontainerize(t_field * tfield,
+ string fieldName)
+{
+ t_type * ttype = get_true_type(tfield->get_type());
if (ttype->is_enum()) {
return "[" + fieldName + " intValue]";
} else if (ttype->is_base_type()) {
@@ -2029,6 +2166,7 @@
return fieldName;
}
+
/**
* Serializes the members of a map.
*/
@@ -2063,6 +2201,7 @@
generate_serialize_field(out, &efield, decontainerize(&efield, efield.get_name()));
}
+
/**
* Returns an Objective-C name
*
@@ -2125,11 +2264,11 @@
case t_base_type::TYPE_BYTE:
return "uint8_t";
case t_base_type::TYPE_I16:
- return "int16_t";
+ return"int16_t";
case t_base_type::TYPE_I32:
return "int32_t";
case t_base_type::TYPE_I64:
- return "int64_t";
+ return"int64_t";
case t_base_type::TYPE_DOUBLE:
return "double";
default:
@@ -2142,12 +2281,7 @@
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-void t_cocoa_generator::print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool defval,
- bool is_property) {
+void t_cocoa_generator::print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool defval, bool is_property) {
type = get_true_type(type);
indent(out);
@@ -2168,8 +2302,7 @@
if (defval)
out << type_name(type) << " ";
if (defval || is_property)
- out << name << " = [[[" << type_name(type, true) << " alloc] init] autorelease_stub];"
- << endl;
+ out << name << " = [[[" << type_name(type, true) << " alloc] init] autorelease_stub];" << endl;
else
out << name << " = [[" << type_name(type, true) << " alloc] init];" << endl;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
@@ -2195,11 +2328,9 @@
if (defval)
out << "NSMutableDictionary *";
if (defval || is_property)
- out << name << " = [[[NSMutableDictionary alloc] initWithCapacity:" << val.size()
- << "] autorelease_stub]; " << endl;
+ out << name << " = [[[NSMutableDictionary alloc] initWithCapacity:" << val.size() << "] autorelease_stub]; " << endl;
else
- out << name << " = [[NSMutableDictionary alloc] initWithCapacity:" << val.size() << "]; "
- << endl;
+ out << name << " = [[NSMutableDictionary alloc] initWithCapacity:" << val.size() << "]; " << endl;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string key = render_const_value(out, ktype, v_iter->first, true);
string val = render_const_value(out, vtype, v_iter->second, true);
@@ -2213,10 +2344,9 @@
if (defval)
out << "NSMutableArray *";
if (defval || is_property)
- out << name << " = [[[NSMutableArray alloc] initWithCapacity:" << val.size()
- << "] autorelease_stub];" << endl;
+ out << name << " = [[[NSMutableArray alloc] initWithCapacity:" << val.size() <<"] autorelease_stub];" << endl;
else
- out << name << " = [[NSMutableArray alloc] initWithCapacity:" << val.size() << "];" << endl;
+ out << name << " = [[NSMutableArray alloc] initWithCapacity:" << val.size() <<"];" << endl;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string val = render_const_value(out, etype, *v_iter, true);
indent(out) << "[" << name << " addObject:" << val << "];" << endl;
@@ -2229,8 +2359,7 @@
if (defval)
out << "NSMutableSet *";
if (defval || is_property)
- out << name << " = [[[NSMutableSet alloc] initWithCapacity:" << val.size()
- << "] autorelease_stub];" << endl;
+ out << name << " = [[[NSMutableSet alloc] initWithCapacity:" << val.size() << "] autorelease_stub];" << endl;
else
out << name << " = [[NSMutableSet alloc] initWithCapacity:" << val.size() << "];" << endl;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
@@ -2243,10 +2372,7 @@
}
}
-string t_cocoa_generator::render_const_value(ofstream& out,
- t_type* type,
- t_const_value* value,
- bool containerize_it) {
+string t_cocoa_generator::render_const_value(ofstream& out, t_type* type, t_const_value* value, bool containerize_it) {
type = get_true_type(type);
std::ostringstream render;
@@ -2454,9 +2580,9 @@
if (type_can_be_null(tfield->get_type()))
render << "retain, ";
- render << "getter=" << decapitalize(tfield->get_name()) << ", setter=set"
- << capitalize(tfield->get_name()) + ":) " << type_name(tfield->get_type()) << " "
- << tfield->get_name() << ";";
+ render << "getter=" << decapitalize(tfield->get_name()) <<
+ ", setter=set" << capitalize(tfield->get_name()) + ":) " <<
+ type_name(tfield->get_type()) << " " << tfield->get_name() << ";";
return render.str();
}
@@ -2469,11 +2595,12 @@
*/
string t_cocoa_generator::function_signature(t_function* tfunction) {
t_type* ttype = tfunction->get_returntype();
- std::string result = "(" + type_name(ttype) + ") " + tfunction->get_name()
- + argument_list(tfunction->get_arglist());
+ std::string result =
+ "(" + type_name(ttype) + ") " + tfunction->get_name() + argument_list(tfunction->get_arglist());
return result;
}
+
/**
* Renders a colon separated list of types and names, suitable for an
* objective-c parameter list
@@ -2497,6 +2624,7 @@
return result;
}
+
/**
* Converts the parse type to an Objective-C enum string for the given type.
*/
@@ -2538,6 +2666,7 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
/**
* Returns a format string specifier for the supplied parse type.
*/
@@ -2590,9 +2719,10 @@
return "[self set" + capitalize(tfield->get_name()) + ": " + fieldName + "];";
}
-THRIFT_REGISTER_GENERATOR(
- cocoa,
- "Cocoa",
- " log_unexpected: Log every time an unexpected field ID or type is encountered.\n"
- " validate_required:\n"
- " Throws exception if any required field is not set.\n")
+
+THRIFT_REGISTER_GENERATOR(cocoa, "Cocoa",
+" log_unexpected: Log every time an unexpected field ID or type is encountered.\n"
+" validate_required:\n"
+" Throws exception if any required field is not set.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index 05ac216..42fe9e2 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -40,19 +40,21 @@
using std::string;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* C++ code generator. This is legitimacy incarnate.
*
*/
class t_cpp_generator : public t_oop_generator {
-public:
- t_cpp_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_cpp_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("pure_enums");
@@ -76,7 +78,8 @@
iter = parsed_options.find("templates");
gen_templates_ = (iter != parsed_options.end());
- gen_templates_only_ = (iter != parsed_options.end() && iter->second == "only");
+ gen_templates_only_ =
+ (iter != parsed_options.end() && iter->second == "only");
out_dir_base_ = "gen-cpp";
}
@@ -97,106 +100,115 @@
void generate_typedef(t_typedef* ttypedef);
void generate_enum(t_enum* tenum);
void generate_forward_declaration(t_struct* tstruct);
- void generate_struct(t_struct* tstruct) { generate_cpp_struct(tstruct, false); }
- void generate_xception(t_struct* txception) { generate_cpp_struct(txception, true); }
+ void generate_struct(t_struct* tstruct) {
+ generate_cpp_struct(tstruct, false);
+ }
+ void generate_xception(t_struct* txception) {
+ generate_cpp_struct(txception, true);
+ }
void generate_cpp_struct(t_struct* tstruct, bool is_exception);
void generate_service(t_service* tservice);
void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
- std::string render_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
+ std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
- void generate_struct_declaration(std::ofstream& out,
- t_struct* tstruct,
- bool is_exception = false,
- bool pointers = false,
- bool read = true,
- bool write = true,
- bool swap = false);
- void generate_struct_definition(std::ofstream& out,
- std::ofstream& force_cpp_out,
- t_struct* tstruct,
- bool setters = true);
- void generate_copy_constructor(std::ofstream& out, t_struct* tstruct, bool is_exception);
- void generate_assignment_operator(std::ofstream& out, t_struct* tstruct);
- void generate_struct_fingerprint(std::ofstream& out, t_struct* tstruct, bool is_definition);
- void generate_struct_reader(std::ofstream& out, t_struct* tstruct, bool pointers = false);
- void generate_struct_writer(std::ofstream& out, t_struct* tstruct, bool pointers = false);
- void generate_struct_result_writer(std::ofstream& out, t_struct* tstruct, bool pointers = false);
- void generate_struct_swap(std::ofstream& out, t_struct* tstruct);
+ void generate_struct_declaration (std::ofstream& out, t_struct* tstruct,
+ bool is_exception=false,
+ bool pointers=false,
+ bool read=true,
+ bool write=true,
+ bool swap=false);
+ void generate_struct_definition (std::ofstream& out, std::ofstream& force_cpp_out, t_struct* tstruct, bool setters=true);
+ void generate_copy_constructor (std::ofstream& out, t_struct* tstruct, bool is_exception);
+ void generate_assignment_operator (std::ofstream& out, t_struct* tstruct);
+ void generate_struct_fingerprint (std::ofstream& out, t_struct* tstruct, bool is_definition);
+ void generate_struct_reader (std::ofstream& out, t_struct* tstruct, bool pointers=false);
+ void generate_struct_writer (std::ofstream& out, t_struct* tstruct, bool pointers=false);
+ void generate_struct_result_writer (std::ofstream& out, t_struct* tstruct, bool pointers=false);
+ void generate_struct_swap (std::ofstream& out, t_struct* tstruct);
void generate_struct_ostream_operator(std::ofstream& out, t_struct* tstruct);
/**
* Service-level generation functions
*/
- void generate_service_interface(t_service* tservice, string style);
- void generate_service_interface_factory(t_service* tservice, string style);
- void generate_service_null(t_service* tservice, string style);
- void generate_service_multiface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice, string style);
- void generate_service_processor(t_service* tservice, string style);
- void generate_service_skeleton(t_service* tservice);
- void generate_process_function(t_service* tservice,
- t_function* tfunction,
- string style,
- bool specialized = false);
- void generate_function_helpers(t_service* tservice, t_function* tfunction);
- void generate_service_async_skeleton(t_service* tservice);
+ void generate_service_interface (t_service* tservice, string style);
+ void generate_service_interface_factory (t_service* tservice, string style);
+ void generate_service_null (t_service* tservice, string style);
+ void generate_service_multiface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice, string style);
+ void generate_service_processor (t_service* tservice, string style);
+ void generate_service_skeleton (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction,
+ string style, bool specialized=false);
+ void generate_function_helpers (t_service* tservice, t_function* tfunction);
+ void generate_service_async_skeleton (t_service* tservice);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- std::string suffix = "");
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="",
+ std::string suffix="");
- void generate_deserialize_struct(std::ofstream& out,
- t_struct* tstruct,
- std::string prefix = "",
- bool pointer = false);
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="",
+ bool pointer=false);
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix,
- bool push_back,
- std::string index);
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix,
+ bool push_back,
+ std::string index);
- void generate_serialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- std::string suffix = "");
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="",
+ std::string suffix="");
- void generate_serialize_struct(std::ofstream& out,
- t_struct* tstruct,
- std::string prefix = "",
- bool pointer = false);
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="",
+ bool pointer=false);
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out, t_map* tmap, std::string iter);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter);
- void generate_function_call(ostream& out,
- t_function* tfunction,
- string target,
- string iface,
- string arg_prefix);
+ void generate_function_call (ostream& out,
+ t_function* tfunction,
+ string target,
+ string iface,
+ string arg_prefix);
/*
* Helper rendering functions
*/
@@ -204,23 +216,14 @@
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, bool in_typedef = false, bool arg = false);
+ std::string type_name(t_type* ttype, bool in_typedef=false, bool arg=false);
std::string base_type_name(t_base_type::t_base tbase);
- std::string declare_field(t_field* tfield,
- bool init = false,
- bool pointer = false,
- bool constant = false,
- bool reference = false);
- std::string function_signature(t_function* tfunction,
- std::string style,
- std::string prefix = "",
- bool name_params = true);
- std::string cob_function_signature(t_function* tfunction,
- std::string prefix = "",
- bool name_params = true);
- std::string argument_list(t_struct* tstruct, bool name_params = true, bool start_comma = false);
+ std::string declare_field(t_field* tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false);
+ std::string function_signature(t_function* tfunction, std::string style, std::string prefix="", bool name_params=true);
+ std::string cob_function_signature(t_function* tfunction, std::string prefix="", bool name_params=true);
+ std::string argument_list(t_struct* tstruct, bool name_params=true, bool start_comma=false);
std::string type_to_enum(t_type* ttype);
- std::string local_reflection_name(const char*, t_type* ttype, bool external = false);
+ std::string local_reflection_name(const char*, t_type* ttype, bool external=false);
void generate_enum_constant_list(std::ofstream& f,
const vector<t_enum_value*>& constants,
@@ -234,19 +237,25 @@
void generate_local_reflection(std::ofstream& out, t_type* ttype, bool is_definition);
void generate_local_reflection_pointer(std::ofstream& out, t_type* ttype);
- bool is_reference(t_field* tfield) { return tfield->get_reference(); }
+ bool is_reference(t_field* tfield) {
+ return tfield->get_reference();
+ }
bool is_complex_type(t_type* ttype) {
ttype = get_true_type(ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception()
- || (ttype->is_base_type()
- && (((t_base_type*)ttype)->get_base() == t_base_type::TYPE_STRING));
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ (ttype->is_base_type() && (((t_base_type*)ttype)->get_base() == t_base_type::TYPE_STRING));
}
- void set_use_include_prefix(bool use_include_prefix) { use_include_prefix_ = use_include_prefix; }
+ void set_use_include_prefix(bool use_include_prefix) {
+ use_include_prefix_ = use_include_prefix;
+ }
-private:
+ private:
/**
* Returns the include prefix to use for a file generated by program, or the
* empty string if no include prefix should be used.
@@ -337,48 +346,60 @@
MKDIR(get_out_dir().c_str());
// Make output file
- string f_types_name = get_out_dir() + program_name_ + "_types.h";
+ string f_types_name = get_out_dir()+program_name_+"_types.h";
f_types_.open(f_types_name.c_str());
- string f_types_impl_name = get_out_dir() + program_name_ + "_types.cpp";
+ string f_types_impl_name = get_out_dir()+program_name_+"_types.cpp";
f_types_impl_.open(f_types_impl_name.c_str());
if (gen_templates_) {
// If we don't open the stream, it appears to just discard data,
// which is fine.
- string f_types_tcc_name = get_out_dir() + program_name_ + "_types.tcc";
+ string f_types_tcc_name = get_out_dir()+program_name_+"_types.tcc";
f_types_tcc_.open(f_types_tcc_name.c_str());
}
// Print header
- f_types_ << autogen_comment();
- f_types_impl_ << autogen_comment();
- f_types_tcc_ << autogen_comment();
+ f_types_ <<
+ autogen_comment();
+ f_types_impl_ <<
+ autogen_comment();
+ f_types_tcc_ <<
+ autogen_comment();
// Start ifndef
- f_types_ << "#ifndef " << program_name_ << "_TYPES_H" << endl << "#define " << program_name_
- << "_TYPES_H" << endl << endl;
- f_types_tcc_ << "#ifndef " << program_name_ << "_TYPES_TCC" << endl << "#define " << program_name_
- << "_TYPES_TCC" << endl << endl;
+ f_types_ <<
+ "#ifndef " << program_name_ << "_TYPES_H" << endl <<
+ "#define " << program_name_ << "_TYPES_H" << endl <<
+ endl;
+ f_types_tcc_ <<
+ "#ifndef " << program_name_ << "_TYPES_TCC" << endl <<
+ "#define " << program_name_ << "_TYPES_TCC" << endl <<
+ endl;
// Include base types
- f_types_ << "#include <iosfwd>" << endl << endl << "#include <thrift/Thrift.h>" << endl
- << "#include <thrift/TApplicationException.h>" << endl
- << "#include <thrift/protocol/TProtocol.h>" << endl
- << "#include <thrift/transport/TTransport.h>" << endl << endl;
+ f_types_ <<
+ "#include <iosfwd>" << endl << endl <<
+ "#include <thrift/Thrift.h>" << endl <<
+ "#include <thrift/TApplicationException.h>" << endl <<
+ "#include <thrift/protocol/TProtocol.h>" << endl <<
+ "#include <thrift/transport/TTransport.h>" << endl <<
+ endl;
// Include C++xx compatibility header
f_types_ << "#include <thrift/cxxfunctional.h>" << endl;
// Include other Thrift includes
const vector<t_program*>& includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
- f_types_ << "#include \"" << get_include_prefix(*(includes[i])) << includes[i]->get_name()
- << "_types.h\"" << endl;
+ f_types_ <<
+ "#include \"" << get_include_prefix(*(includes[i])) <<
+ includes[i]->get_name() << "_types.h\"" << endl;
// XXX(simpkins): If gen_templates_ is enabled, we currently assume all
// included files were also generated with templates enabled.
- f_types_tcc_ << "#include \"" << get_include_prefix(*(includes[i])) << includes[i]->get_name()
- << "_types.tcc\"" << endl;
+ f_types_tcc_ <<
+ "#include \"" << get_include_prefix(*(includes[i])) <<
+ includes[i]->get_name() << "_types.tcc\"" << endl;
}
f_types_ << endl;
@@ -386,23 +407,32 @@
const vector<string>& cpp_includes = program_->get_cpp_includes();
for (size_t i = 0; i < cpp_includes.size(); ++i) {
if (cpp_includes[i][0] == '<') {
- f_types_ << "#include " << cpp_includes[i] << endl;
+ f_types_ <<
+ "#include " << cpp_includes[i] << endl;
} else {
- f_types_ << "#include \"" << cpp_includes[i] << "\"" << endl;
+ f_types_ <<
+ "#include \"" << cpp_includes[i] << "\"" << endl;
}
}
- f_types_ << endl;
+ f_types_ <<
+ endl;
// Include the types file
- f_types_impl_ << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_types.h\"" << endl << endl;
- f_types_tcc_ << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_types.h\"" << endl << endl;
+ f_types_impl_ <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.h\"" << endl <<
+ endl;
+ f_types_tcc_ <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.h\"" << endl <<
+ endl;
// If we are generating local reflection metadata, we need to include
// the definition of TypeSpec.
if (gen_dense_) {
- f_types_impl_ << "#include <thrift/TReflectionLocal.h>" << endl << endl;
+ f_types_impl_ <<
+ "#include <thrift/TReflectionLocal.h>" << endl <<
+ endl;
}
// The swap() code needs <algorithm> for std::swap()
@@ -415,11 +445,17 @@
ns_open_ = namespace_open(program_->get_namespace("cpp"));
ns_close_ = namespace_close(program_->get_namespace("cpp"));
- f_types_ << ns_open_ << endl << endl;
+ f_types_ <<
+ ns_open_ << endl <<
+ endl;
- f_types_impl_ << ns_open_ << endl << endl;
+ f_types_impl_ <<
+ ns_open_ << endl <<
+ endl;
- f_types_tcc_ << ns_open_ << endl << endl;
+ f_types_tcc_ <<
+ ns_open_ << endl <<
+ endl;
}
/**
@@ -427,21 +463,30 @@
*/
void t_cpp_generator::close_generator() {
// Close namespace
- f_types_ << ns_close_ << endl << endl;
- f_types_impl_ << ns_close_ << endl;
- f_types_tcc_ << ns_close_ << endl << endl;
+ f_types_ <<
+ ns_close_ << endl <<
+ endl;
+ f_types_impl_ <<
+ ns_close_ << endl;
+ f_types_tcc_ <<
+ ns_close_ << endl <<
+ endl;
// Include the types.tcc file from the types header file,
// so clients don't have to explicitly include the tcc file.
// TODO(simpkins): Make this a separate option.
if (gen_templates_) {
- f_types_ << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_types.tcc\"" << endl << endl;
+ f_types_ <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.tcc\"" << endl <<
+ endl;
}
// Close ifndef
- f_types_ << "#endif" << endl;
- f_types_tcc_ << "#endif" << endl;
+ f_types_ <<
+ "#endif" << endl;
+ f_types_tcc_ <<
+ "#endif" << endl;
// Close output file
f_types_.close();
@@ -455,10 +500,12 @@
* @param ttypedef The type definition
*/
void t_cpp_generator::generate_typedef(t_typedef* ttypedef) {
- f_types_ << indent() << "typedef " << type_name(ttypedef->get_type(), true) << " "
- << ttypedef->get_symbolic() << ";" << endl << endl;
+ f_types_ <<
+ indent() << "typedef " << type_name(ttypedef->get_type(), true) << " " << ttypedef->get_symbolic() << ";" << endl <<
+ endl;
}
+
void t_cpp_generator::generate_enum_constant_list(std::ofstream& f,
const vector<t_enum_value*>& constants,
const char* prefix,
@@ -475,7 +522,8 @@
} else {
f << "," << endl;
}
- indent(f) << prefix << (*c_iter)->get_name() << suffix;
+ indent(f)
+ << prefix << (*c_iter)->get_name() << suffix;
if (include_values) {
f << " = " << (*c_iter)->get_value();
}
@@ -498,10 +546,12 @@
std::string enum_name = tenum->get_name();
if (!gen_pure_enums_) {
enum_name = "type";
- f_types_ << indent() << "struct " << tenum->get_name() << " {" << endl;
+ f_types_ <<
+ indent() << "struct " << tenum->get_name() << " {" << endl;
indent_up();
}
- f_types_ << indent() << "enum " << enum_name;
+ f_types_ <<
+ indent() << "enum " << enum_name;
generate_enum_constant_list(f_types_, constants, "", "", true);
@@ -520,20 +570,24 @@
prefix = tenum->get_name() + "::";
}
- f_types_impl_ << indent() << "int _k" << tenum->get_name() << "Values[] =";
+ f_types_impl_ <<
+ indent() << "int _k" << tenum->get_name() << "Values[] =";
generate_enum_constant_list(f_types_impl_, constants, prefix.c_str(), "", false);
- f_types_impl_ << indent() << "const char* _k" << tenum->get_name() << "Names[] =";
+ f_types_impl_ <<
+ indent() << "const char* _k" << tenum->get_name() << "Names[] =";
generate_enum_constant_list(f_types_impl_, constants, "\"", "\"", false);
- f_types_ << indent() << "extern const std::map<int, const char*> _" << tenum->get_name()
- << "_VALUES_TO_NAMES;" << endl << endl;
+ f_types_ <<
+ indent() << "extern const std::map<int, const char*> _" <<
+ tenum->get_name() << "_VALUES_TO_NAMES;" << endl << endl;
- f_types_impl_ << indent() << "const std::map<int, const char*> _" << tenum->get_name()
- << "_VALUES_TO_NAMES(::apache::thrift::TEnumIterator(" << constants.size() << ", _k"
- << tenum->get_name() << "Values"
- << ", _k" << tenum->get_name() << "Names), "
- << "::apache::thrift::TEnumIterator(-1, NULL, NULL));" << endl << endl;
+ f_types_impl_ <<
+ indent() << "const std::map<int, const char*> _" << tenum->get_name() <<
+ "_VALUES_TO_NAMES(::apache::thrift::TEnumIterator(" << constants.size() <<
+ ", _k" << tenum->get_name() << "Values" <<
+ ", _k" << tenum->get_name() << "Names), " <<
+ "::apache::thrift::TEnumIterator(-1, NULL, NULL));" << endl << endl;
generate_local_reflection(f_types_, tenum, false);
generate_local_reflection(f_types_impl_, tenum, true);
@@ -543,41 +597,59 @@
* Generates a class that holds all the constants.
*/
void t_cpp_generator::generate_consts(std::vector<t_const*> consts) {
- string f_consts_name = get_out_dir() + program_name_ + "_constants.h";
+ string f_consts_name = get_out_dir()+program_name_+"_constants.h";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
- string f_consts_impl_name = get_out_dir() + program_name_ + "_constants.cpp";
+ string f_consts_impl_name = get_out_dir()+program_name_+"_constants.cpp";
ofstream f_consts_impl;
f_consts_impl.open(f_consts_impl_name.c_str());
// Print header
- f_consts << autogen_comment();
- f_consts_impl << autogen_comment();
+ f_consts <<
+ autogen_comment();
+ f_consts_impl <<
+ autogen_comment();
// Start ifndef
- f_consts << "#ifndef " << program_name_ << "_CONSTANTS_H" << endl << "#define " << program_name_
- << "_CONSTANTS_H" << endl << endl << "#include \"" << get_include_prefix(*get_program())
- << program_name_ << "_types.h\"" << endl << endl << ns_open_ << endl << endl;
+ f_consts <<
+ "#ifndef " << program_name_ << "_CONSTANTS_H" << endl <<
+ "#define " << program_name_ << "_CONSTANTS_H" << endl <<
+ endl <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.h\"" << endl <<
+ endl <<
+ ns_open_ << endl <<
+ endl;
- f_consts_impl << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_constants.h\"" << endl << endl << ns_open_ << endl << endl;
+ f_consts_impl <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_constants.h\"" << endl <<
+ endl <<
+ ns_open_ << endl <<
+ endl;
- f_consts << "class " << program_name_ << "Constants {" << endl << " public:" << endl << " "
- << program_name_ << "Constants();" << endl << endl;
+ f_consts <<
+ "class " << program_name_ << "Constants {" << endl <<
+ " public:" << endl <<
+ " " << program_name_ << "Constants();" << endl <<
+ endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
t_type* type = (*c_iter)->get_type();
- f_consts << indent() << type_name(type) << " " << name << ";" << endl;
+ f_consts <<
+ indent() << type_name(type) << " " << name << ";" << endl;
}
indent_down();
- f_consts << "};" << endl;
+ f_consts <<
+ "};" << endl;
- f_consts_impl << "const " << program_name_ << "Constants g_" << program_name_ << "_constants;"
- << endl << endl << program_name_ << "Constants::" << program_name_
- << "Constants() {" << endl;
+ f_consts_impl <<
+ "const " << program_name_ << "Constants g_" << program_name_ << "_constants;" << endl <<
+ endl <<
+ program_name_ << "Constants::" << program_name_ << "Constants() {" << endl;
indent_up();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
print_const_value(f_consts_impl,
@@ -586,13 +658,22 @@
(*c_iter)->get_value());
}
indent_down();
- indent(f_consts_impl) << "}" << endl;
+ indent(f_consts_impl) <<
+ "}" << endl;
- f_consts << endl << "extern const " << program_name_ << "Constants g_" << program_name_
- << "_constants;" << endl << endl << ns_close_ << endl << endl << "#endif" << endl;
+ f_consts <<
+ endl <<
+ "extern const " << program_name_ << "Constants g_" << program_name_ << "_constants;" << endl <<
+ endl <<
+ ns_close_ << endl <<
+ endl <<
+ "#endif" << endl;
f_consts.close();
- f_consts_impl << endl << ns_close_ << endl << endl;
+ f_consts_impl <<
+ endl <<
+ ns_close_ << endl <<
+ endl;
}
/**
@@ -600,17 +681,15 @@
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-void t_cpp_generator::print_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
+void t_cpp_generator::print_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
type = get_true_type(type);
if (type->is_base_type()) {
string v2 = render_const_value(out, name, type, value);
- indent(out) << name << " = " << v2 << ";" << endl << endl;
+ indent(out) << name << " = " << v2 << ";" << endl <<
+ endl;
} else if (type->is_enum()) {
- indent(out) << name << " = (" << type_name(type) << ")" << value->get_integer() << ";" << endl
- << endl;
+ indent(out) << name << " = (" << type_name(type) << ")" << value->get_integer() << ";" << endl <<
+ endl;
} else if (type->is_struct() || type->is_xception()) {
const vector<t_field*>& fields = ((t_struct*)type)->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -631,8 +710,8 @@
}
string val = render_const_value(out, name, field_type, v_iter->second);
indent(out) << name << "." << v_iter->first->get_string() << " = " << val << ";" << endl;
- if (is_nonrequired_field) {
- indent(out) << name << ".__isset." << v_iter->first->get_string() << " = true;" << endl;
+ if(is_nonrequired_field) {
+ indent(out) << name << ".__isset." << v_iter->first->get_string() << " = true;" << endl;
}
}
out << endl;
@@ -673,11 +752,8 @@
/**
*
*/
-string t_cpp_generator::render_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+string t_cpp_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
std::ostringstream render;
if (type->is_base_type()) {
@@ -721,7 +797,9 @@
void t_cpp_generator::generate_forward_declaration(t_struct* tstruct) {
// Forward declare struct def
- f_types_ << indent() << "class " << tstruct->get_name() << ";" << endl << endl;
+ f_types_ <<
+ indent() << "class " << tstruct->get_name() << ";" << endl <<
+ endl;
}
/**
@@ -732,7 +810,8 @@
* @param tstruct The struct definition
*/
void t_cpp_generator::generate_cpp_struct(t_struct* tstruct, bool is_exception) {
- generate_struct_declaration(f_types_, tstruct, is_exception, false, true, true, true);
+ generate_struct_declaration(f_types_, tstruct, is_exception,
+ false, true, true, true);
generate_struct_definition(f_types_impl_, f_types_impl_, tstruct);
generate_struct_fingerprint(f_types_impl_, tstruct, true);
generate_local_reflection(f_types_, tstruct, false);
@@ -748,13 +827,15 @@
generate_struct_ostream_operator(f_types_impl_, tstruct);
}
-void t_cpp_generator::generate_copy_constructor(ofstream& out,
- t_struct* tstruct,
- bool is_exception) {
+void t_cpp_generator::generate_copy_constructor(
+ ofstream& out,
+ t_struct* tstruct,
+ bool is_exception) {
std::string tmp_name = tmp("other");
- indent(out) << tstruct->get_name() << "::" << tstruct->get_name() << "(const "
- << tstruct->get_name() << "& " << tmp_name << ") ";
+ indent(out) << tstruct->get_name() << "::" <<
+ tstruct->get_name() << "(const " << tstruct->get_name() <<
+ "& " << tmp_name << ") ";
if (is_exception)
out << ": TException() ";
out << "{" << endl;
@@ -771,8 +852,8 @@
for (f_iter = members.begin(); f_iter != members.end(); ++f_iter) {
if ((*f_iter)->get_req() != t_field::T_REQUIRED)
has_nonrequired_fields = true;
- indent(out) << (*f_iter)->get_name() << " = " << tmp_name << "." << (*f_iter)->get_name() << ";"
- << endl;
+ indent(out) << (*f_iter)->get_name() << " = " << tmp_name << "." <<
+ (*f_iter)->get_name() << ";" << endl;
}
if (has_nonrequired_fields)
@@ -782,12 +863,14 @@
indent(out) << "}" << endl;
}
-void t_cpp_generator::generate_assignment_operator(ofstream& out, t_struct* tstruct) {
+void t_cpp_generator::generate_assignment_operator(
+ ofstream& out,
+ t_struct* tstruct) {
std::string tmp_name = tmp("other");
indent(out) << tstruct->get_name() << "& " << tstruct->get_name() << "::"
- "operator=(const "
- << tstruct->get_name() << "& " << tmp_name << ") {" << endl;
+ "operator=(const " << tstruct->get_name() <<
+ "& " << tmp_name << ") {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
@@ -801,8 +884,8 @@
for (f_iter = members.begin(); f_iter != members.end(); ++f_iter) {
if ((*f_iter)->get_req() != t_field::T_REQUIRED)
has_nonrequired_fields = true;
- indent(out) << (*f_iter)->get_name() << " = " << tmp_name << "." << (*f_iter)->get_name() << ";"
- << endl;
+ indent(out) << (*f_iter)->get_name() << " = " << tmp_name << "." <<
+ (*f_iter)->get_name() << ";" << endl;
}
if (has_nonrequired_fields)
indent(out) << "__isset = " << tmp_name << ".__isset;" << endl;
@@ -819,12 +902,12 @@
* @param tstruct The struct
*/
void t_cpp_generator::generate_struct_declaration(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool pointers,
- bool read,
- bool write,
- bool swap) {
+ t_struct* tstruct,
+ bool is_exception,
+ bool pointers,
+ bool read,
+ bool write,
+ bool swap) {
string extends = "";
if (is_exception) {
extends = " : public ::apache::thrift::TException";
@@ -847,10 +930,12 @@
if (has_nonrequired_fields && (!pointers || read)) {
- out << indent() << "typedef struct _" << tstruct->get_name() << "__isset {" << endl;
+ out <<
+ indent() << "typedef struct _" << tstruct->get_name() << "__isset {" << endl;
indent_up();
- indent(out) << "_" << tstruct->get_name() << "__isset() ";
+ indent(out) <<
+ "_" << tstruct->get_name() << "__isset() ";
bool first = true;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() == t_field::T_REQUIRED) {
@@ -859,28 +944,34 @@
string isSet = ((*m_iter)->get_value() != NULL) ? "true" : "false";
if (first) {
first = false;
- out << ": " << (*m_iter)->get_name() << "(" << isSet << ")";
+ out <<
+ ": " << (*m_iter)->get_name() << "(" << isSet << ")";
} else {
- out << ", " << (*m_iter)->get_name() << "(" << isSet << ")";
+ out <<
+ ", " << (*m_iter)->get_name() << "(" << isSet << ")";
}
}
out << " {}" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
- indent(out) << "bool " << (*m_iter)->get_name() << " :1;" << endl;
+ indent(out) <<
+ "bool " << (*m_iter)->get_name() << " :1;" << endl;
+ }
}
- }
- indent_down();
- indent(out) << "} _" << tstruct->get_name() << "__isset;" << endl;
- }
+ indent_down();
+ indent(out) <<
+ "} _" << tstruct->get_name() << "__isset;" << endl;
+ }
out << endl;
// Open struct def
- out << indent() << "class " << tstruct->get_name() << extends << " {" << endl << indent()
- << " public:" << endl << endl;
+ out <<
+ indent() << "class " << tstruct->get_name() << extends << " {" << endl <<
+ indent() << " public:" << endl <<
+ endl;
indent_up();
// Put the fingerprint up top for all to see.
@@ -888,14 +979,16 @@
if (!pointers) {
// Copy constructor
- indent(out) << tstruct->get_name() << "(const " << tstruct->get_name() << "&);" << endl;
+ indent(out) <<
+ tstruct->get_name() << "(const " << tstruct->get_name() << "&);" << endl;
// Assignment Operator
- indent(out) << tstruct->get_name() << "& operator=(const " << tstruct->get_name() << "&);"
- << endl;
+ indent(out) << tstruct->get_name() << "& operator=(const " << tstruct->get_name() << "&);" << endl;
// Default constructor
- indent(out) << tstruct->get_name() << "()";
+ indent(out) <<
+ tstruct->get_name() << "()";
+
bool init_ctor = false;
@@ -938,26 +1031,29 @@
}
if (tstruct->annotations_.find("final") == tstruct->annotations_.end()) {
- out << endl << indent() << "virtual ~" << tstruct->get_name() << "() throw();" << endl;
+ out <<
+ endl <<
+ indent() << "virtual ~" << tstruct->get_name() << "() throw();" << endl;
}
// Pointer to this structure's reflection local typespec.
if (gen_dense_) {
- indent(out) << "static ::apache::thrift::reflection::local::TypeSpec* local_reflection;" << endl
- << endl;
+ indent(out) <<
+ "static ::apache::thrift::reflection::local::TypeSpec* local_reflection;" <<
+ endl << endl;
}
// Declare all fields
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << declare_field(*m_iter,
- false,
- (pointers && !(*m_iter)->get_type()->is_xception()),
- !read) << endl;
+ indent(out) <<
+ declare_field(*m_iter, false, (pointers && !(*m_iter)->get_type()->is_xception()), !read) << endl;
}
// Add the __isset data member if we need it, using the definition from above
if (has_nonrequired_fields && (!pointers || read)) {
- out << endl << indent() << "_" << tstruct->get_name() << "__isset __isset;" << endl;
+ out <<
+ endl <<
+ indent() << "_" << tstruct->get_name() << "__isset __isset;" << endl;
}
// Create a setter function for each field
@@ -966,12 +1062,16 @@
continue;
}
if (is_reference((*m_iter))) {
- out << endl << indent() << "void __set_" << (*m_iter)->get_name() << "(boost::shared_ptr<"
- << type_name((*m_iter)->get_type(), false, false) << ">";
+ out <<
+ endl <<
+ indent() << "void __set_" << (*m_iter)->get_name() <<
+ "(boost::shared_ptr<" << type_name((*m_iter)->get_type(), false, false) << ">";
out << " val);" << endl;
} else {
- out << endl << indent() << "void __set_" << (*m_iter)->get_name() << "("
- << type_name((*m_iter)->get_type(), false, true);
+ out <<
+ endl <<
+ indent() << "void __set_" << (*m_iter)->get_name() <<
+ "(" << type_name((*m_iter)->get_type(), false, true);
out << " val);" << endl;
}
}
@@ -982,53 +1082,65 @@
if (!gen_no_default_operators_) {
// Generate an equality testing operator. Make it inline since the compiler
// will do a better job than we would when deciding whether to inline it.
- out << indent() << "bool operator == (const " << tstruct->get_name() << " & "
- << (members.size() > 0 ? "rhs" : "/* rhs */") << ") const" << endl;
+ out <<
+ indent() << "bool operator == (const " << tstruct->get_name() << " & " <<
+ (members.size() > 0 ? "rhs" : "/* rhs */") << ") const" << endl;
scope_up(out);
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
// Most existing Thrift code does not use isset or optional/required,
// so we treat "default" fields as required.
if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
- out << indent() << "if (!(" << (*m_iter)->get_name() << " == rhs."
- << (*m_iter)->get_name() << "))" << endl << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (!(" << (*m_iter)->get_name()
+ << " == rhs." << (*m_iter)->get_name() << "))" << endl <<
+ indent() << " return false;" << endl;
} else {
- out << indent() << "if (__isset." << (*m_iter)->get_name() << " != rhs.__isset."
- << (*m_iter)->get_name() << ")" << endl << indent() << " return false;" << endl
- << indent() << "else if (__isset." << (*m_iter)->get_name() << " && !("
- << (*m_iter)->get_name() << " == rhs." << (*m_iter)->get_name() << "))" << endl
- << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (__isset." << (*m_iter)->get_name()
+ << " != rhs.__isset." << (*m_iter)->get_name() << ")" << endl <<
+ indent() << " return false;" << endl <<
+ indent() << "else if (__isset." << (*m_iter)->get_name() << " && !("
+ << (*m_iter)->get_name() << " == rhs." << (*m_iter)->get_name()
+ << "))" << endl <<
+ indent() << " return false;" << endl;
}
}
indent(out) << "return true;" << endl;
scope_down(out);
- out << indent() << "bool operator != (const " << tstruct->get_name() << " &rhs) const {"
- << endl << indent() << " return !(*this == rhs);" << endl << indent() << "}" << endl
- << endl;
+ out <<
+ indent() << "bool operator != (const " << tstruct->get_name() << " &rhs) const {" << endl <<
+ indent() << " return !(*this == rhs);" << endl <<
+ indent() << "}" << endl << endl;
// Generate the declaration of a less-than operator. This must be
// implemented by the application developer if they wish to use it. (They
// will get a link error if they try to use it without an implementation.)
- out << indent() << "bool operator < (const " << tstruct->get_name() << " & ) const;" << endl
- << endl;
+ out <<
+ indent() << "bool operator < (const "
+ << tstruct->get_name() << " & ) const;" << endl << endl;
}
}
if (read) {
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl << indent()
- << "uint32_t read(Protocol_* iprot);" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl <<
+ indent() << "uint32_t read(Protocol_* iprot);" << endl;
} else {
- out << indent() << "uint32_t read("
- << "::apache::thrift::protocol::TProtocol* iprot);" << endl;
+ out <<
+ indent() << "uint32_t read(" <<
+ "::apache::thrift::protocol::TProtocol* iprot);" << endl;
}
}
if (write) {
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl << indent()
- << "uint32_t write(Protocol_* oprot) const;" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl <<
+ indent() << "uint32_t write(Protocol_* oprot) const;" << endl;
} else {
- out << indent() << "uint32_t write("
- << "::apache::thrift::protocol::TProtocol* oprot) const;" << endl;
+ out <<
+ indent() << "uint32_t write(" <<
+ "::apache::thrift::protocol::TProtocol* oprot) const;" << endl;
}
}
out << endl;
@@ -1039,12 +1151,16 @@
out << ";" << endl;
indent_down();
- indent(out) << "};" << endl << endl;
+ indent(out) <<
+ "};" << endl <<
+ endl;
if (swap) {
// Generate a namespace-scope swap() function
- out << indent() << "void swap(" << tstruct->get_name() << " &a, " << tstruct->get_name()
- << " &b);" << endl << endl;
+ out <<
+ indent() << "void swap(" << tstruct->get_name() << " &a, " <<
+ tstruct->get_name() << " &b);" << endl <<
+ endl;
}
}
@@ -1056,10 +1172,12 @@
vector<t_field*>::const_iterator m_iter;
const vector<t_field*>& members = tstruct->get_members();
+
// Destructor
if (tstruct->annotations_.find("final") == tstruct->annotations_.end()) {
- force_cpp_out << endl << indent() << tstruct->get_name() << "::~" << tstruct->get_name()
- << "() throw() {" << endl;
+ force_cpp_out <<
+ endl <<
+ indent() << tstruct->get_name() << "::~" << tstruct->get_name() << "() throw() {" << endl;
indent_up();
indent_down();
@@ -1071,13 +1189,16 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (is_reference((*m_iter))) {
std::string type = type_name((*m_iter)->get_type());
- out << endl << indent() << "void " << tstruct->get_name() << "::__set_"
- << (*m_iter)->get_name() << "(boost::shared_ptr<"
- << type_name((*m_iter)->get_type(), false, false) << ">";
+ out <<
+ endl <<
+ indent() << "void " << tstruct->get_name() << "::__set_" << (*m_iter)->get_name() <<
+ "(boost::shared_ptr<" << type_name((*m_iter)->get_type(), false, false) << ">";
out << " val) {" << endl;
} else {
- out << endl << indent() << "void " << tstruct->get_name() << "::__set_"
- << (*m_iter)->get_name() << "(" << type_name((*m_iter)->get_type(), false, true);
+ out <<
+ endl <<
+ indent() << "void " << tstruct->get_name() << "::__set_" << (*m_iter)->get_name() <<
+ "(" << type_name((*m_iter)->get_type(), false, true);
out << " val) {" << endl;
}
indent_up();
@@ -1088,9 +1209,12 @@
// for optional fields change __isset.name to true
bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL;
if (is_optional) {
- out << indent() << indent() << "__isset." << (*m_iter)->get_name() << " = true;" << endl;
+ out <<
+ indent() <<
+ indent() << "__isset." << (*m_iter)->get_name() << " = true;" << endl;
}
- out << indent() << "}" << endl;
+ out <<
+ indent()<< "}" << endl;
}
}
out << endl;
@@ -1115,13 +1239,16 @@
comment = "; // ";
}
- if (!tstruct->has_fingerprint()) {
- tstruct->generate_fingerprint(); // lazy fingerprint generation
+ if (! tstruct->has_fingerprint()) {
+ tstruct->generate_fingerprint(); // lazy fingerprint generation
}
if (tstruct->has_fingerprint()) {
- out << indent() << stat << "const char* " << nspace << "ascii_fingerprint" << comment << "= \""
- << tstruct->get_ascii_fingerprint() << "\";" << endl << indent() << stat << "const uint8_t "
- << nspace << "binary_fingerprint[" << t_type::fingerprint_len << "]" << comment << "= {";
+ out <<
+ indent() << stat << "const char* " << nspace
+ << "ascii_fingerprint" << comment << "= \"" <<
+ tstruct->get_ascii_fingerprint() << "\";" << endl <<
+ indent() << stat << "const uint8_t " << nspace <<
+ "binary_fingerprint[" << t_type::fingerprint_len << "]" << comment << "= {";
const char* comma = "";
for (int i = 0; i < t_type::fingerprint_len; i++) {
out << comma << "0x" << t_struct::byte_to_hex(tstruct->get_binary_fingerprint()[i]);
@@ -1142,7 +1269,7 @@
}
ttype = get_true_type(ttype);
string key = ttype->get_ascii_fingerprint() + (is_definition ? "-defn" : "-decl");
- assert(ttype->has_fingerprint()); // test AFTER get due to lazy fingerprint generation
+ assert(ttype->has_fingerprint()); // test AFTER get due to lazy fingerprint generation
// Note that we have generated this fingerprint. If we already did, bail out.
if (!reflected_fingerprints_.insert(key).second) {
@@ -1176,34 +1303,40 @@
// For definitions of structures, do the arrays of metas and field specs also.
if (is_definition) {
- out << indent() << "::apache::thrift::reflection::local::FieldMeta" << endl << indent()
- << local_reflection_name("metas", ttype) << "[] = {" << endl;
+ out <<
+ indent() << "::apache::thrift::reflection::local::FieldMeta" << endl <<
+ indent() << local_reflection_name("metas", ttype) <<"[] = {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "{ " << (*m_iter)->get_key() << ", "
- << (((*m_iter)->get_req() == t_field::T_OPTIONAL) ? "true" : "false") << " },"
- << endl;
+ indent(out) << "{ " << (*m_iter)->get_key() << ", " <<
+ (((*m_iter)->get_req() == t_field::T_OPTIONAL) ? "true" : "false") <<
+ " }," << endl;
}
// Zero for the T_STOP marker.
indent(out) << "{ 0, false }" << endl << "};" << endl;
indent_down();
- out << indent() << "::apache::thrift::reflection::local::TypeSpec*" << endl << indent()
- << local_reflection_name("specs", ttype) << "[] = {" << endl;
+ out <<
+ indent() << "::apache::thrift::reflection::local::TypeSpec*" << endl <<
+ indent() << local_reflection_name("specs", ttype) <<"[] = {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "&" << local_reflection_name("typespec", (*m_iter)->get_type(), true) << ","
- << endl;
+ indent(out) << "&" <<
+ local_reflection_name("typespec", (*m_iter)->get_type(), true) << "," << endl;
}
- indent(out) << "&" << local_reflection_name("typespec", g_type_void) << "," << endl;
+ indent(out) << "&" <<
+ local_reflection_name("typespec", g_type_void) << "," << endl;
indent_down();
indent(out) << "};" << endl;
}
}
- out << indent() << "// " << ttype->get_fingerprint_material() << endl << indent()
- << (is_definition ? "" : "extern ") << "::apache::thrift::reflection::local::TypeSpec" << endl
- << local_reflection_name("typespec", ttype) << (is_definition ? "(" : ";") << endl;
+ out <<
+ indent() << "// " << ttype->get_fingerprint_material() << endl <<
+ indent() << (is_definition ? "" : "extern ") <<
+ "::apache::thrift::reflection::local::TypeSpec" << endl <<
+ local_reflection_name("typespec", ttype) <<
+ (is_definition ? "(" : ";") << endl;
if (!is_definition) {
out << endl;
@@ -1219,22 +1352,22 @@
}
if (ttype->is_struct()) {
- out << "," << endl << indent() << type_name(ttype) << "::binary_fingerprint," << endl
- << indent() << local_reflection_name("metas", ttype) << "," << endl << indent()
- << local_reflection_name("specs", ttype);
+ out << "," << endl <<
+ indent() << type_name(ttype) << "::binary_fingerprint," << endl <<
+ indent() << local_reflection_name("metas", ttype) << "," << endl <<
+ indent() << local_reflection_name("specs", ttype);
} else if (ttype->is_list()) {
- out << "," << endl << indent() << "&"
- << local_reflection_name("typespec", ((t_list*)ttype)->get_elem_type(), true) << "," << endl
- << indent() << "NULL";
+ out << "," << endl <<
+ indent() << "&" << local_reflection_name("typespec", ((t_list*)ttype)->get_elem_type(), true) << "," << endl <<
+ indent() << "NULL";
} else if (ttype->is_set()) {
- out << "," << endl << indent() << "&"
- << local_reflection_name("typespec", ((t_set*)ttype)->get_elem_type(), true) << "," << endl
- << indent() << "NULL";
+ out << "," << endl <<
+ indent() << "&" << local_reflection_name("typespec", ((t_set*)ttype)->get_elem_type(), true) << "," << endl <<
+ indent() << "NULL";
} else if (ttype->is_map()) {
- out << "," << endl << indent() << "&"
- << local_reflection_name("typespec", ((t_map*)ttype)->get_key_type(), true) << "," << endl
- << indent() << "&"
- << local_reflection_name("typespec", ((t_map*)ttype)->get_val_type(), true);
+ out << "," << endl <<
+ indent() << "&" << local_reflection_name("typespec", ((t_map*)ttype)->get_key_type(), true) << "," << endl <<
+ indent() << "&" << local_reflection_name("typespec", ((t_map*)ttype)->get_val_type(), true);
}
out << ");" << endl << endl;
@@ -1246,13 +1379,16 @@
* Writes the structure's static pointer to its local reflection typespec
* into the implementation file.
*/
-void t_cpp_generator::generate_local_reflection_pointer(std::ofstream& out, t_type* ttype) {
+void t_cpp_generator::generate_local_reflection_pointer(std::ofstream& out,
+ t_type* ttype) {
if (!gen_dense_) {
return;
}
- indent(out) << "::apache::thrift::reflection::local::TypeSpec* " << ttype->get_name()
- << "::local_reflection = " << endl << indent() << " &"
- << local_reflection_name("typespec", ttype) << ";" << endl << endl;
+ indent(out) <<
+ "::apache::thrift::reflection::local::TypeSpec* " <<
+ ttype->get_name() << "::local_reflection = " << endl <<
+ indent() << " &" << local_reflection_name("typespec", ttype) << ";" <<
+ endl << endl;
}
/**
@@ -1261,13 +1397,18 @@
* @param out Stream to write to
* @param tstruct The struct
*/
-void t_cpp_generator::generate_struct_reader(ofstream& out, t_struct* tstruct, bool pointers) {
+void t_cpp_generator::generate_struct_reader(ofstream& out,
+ t_struct* tstruct,
+ bool pointers) {
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl << indent() << "uint32_t "
- << tstruct->get_name() << "::read(Protocol_* iprot) {" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl <<
+ indent() << "uint32_t " << tstruct->get_name() <<
+ "::read(Protocol_* iprot) {" << endl;
} else {
- indent(out) << "uint32_t " << tstruct->get_name()
- << "::read(::apache::thrift::protocol::TProtocol* iprot) {" << endl;
+ indent(out) <<
+ "uint32_t " << tstruct->get_name() <<
+ "::read(::apache::thrift::protocol::TProtocol* iprot) {" << endl;
}
indent_up();
@@ -1275,11 +1416,17 @@
vector<t_field*>::const_iterator f_iter;
// Declare stack tmp variables
- out << endl << indent() << "uint32_t xfer = 0;" << endl << indent() << "std::string fname;"
- << endl << indent() << "::apache::thrift::protocol::TType ftype;" << endl << indent()
- << "int16_t fid;" << endl << endl << indent() << "xfer += iprot->readStructBegin(fname);"
- << endl << endl << indent() << "using ::apache::thrift::protocol::TProtocolException;" << endl
- << endl;
+ out <<
+ endl <<
+ indent() << "uint32_t xfer = 0;" << endl <<
+ indent() << "std::string fname;" << endl <<
+ indent() << "::apache::thrift::protocol::TType ftype;" << endl <<
+ indent() << "int16_t fid;" << endl <<
+ endl <<
+ indent() << "xfer += iprot->readStructBegin(fname);" << endl <<
+ endl <<
+ indent() << "using ::apache::thrift::protocol::TProtocolException;" << endl <<
+ endl;
// Required variables aren't in __isset, so we need tmp vars to check them.
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1288,34 +1435,44 @@
}
out << endl;
+
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
- scope_up(out);
-
- // Read beginning field marker
- indent(out) << "xfer += iprot->readFieldBegin(fname, ftype, fid);" << endl;
-
- // Check for field STOP marker
- out << indent() << "if (ftype == ::apache::thrift::protocol::T_STOP) {" << endl << indent()
- << " break;" << endl << indent() << "}" << endl;
-
- if (fields.empty()) {
- out << indent() << "xfer += iprot->skip(ftype);" << endl;
- } else {
- // Switch statement on the field we are reading
- indent(out) << "switch (fid)" << endl;
-
+ indent(out) <<
+ "while (true)" << endl;
scope_up(out);
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
- indent_up();
- indent(out) << "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent_up();
+ // Read beginning field marker
+ indent(out) <<
+ "xfer += iprot->readFieldBegin(fname, ftype, fid);" << endl;
- const char* isset_prefix = ((*f_iter)->get_req() != t_field::T_REQUIRED) ? "this->__isset."
- : "isset_";
+ // Check for field STOP marker
+ out <<
+ indent() << "if (ftype == ::apache::thrift::protocol::T_STOP) {" << endl <<
+ indent() << " break;" << endl <<
+ indent() << "}" << endl;
+
+ if(fields.empty()) {
+ out <<
+ indent() << "xfer += iprot->skip(ftype);" << endl;
+ }
+ else {
+ // Switch statement on the field we are reading
+ indent(out) <<
+ "switch (fid)" << endl;
+
+ scope_up(out);
+
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
+ indent_up();
+ indent(out) <<
+ "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+
+ const char *isset_prefix =
+ ((*f_iter)->get_req() != t_field::T_REQUIRED) ? "this->__isset." : "isset_";
#if 0
// This code throws an exception if the same field is encountered twice.
@@ -1327,34 +1484,42 @@
indent() << " throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
#endif
- if (pointers && !(*f_iter)->get_type()->is_xception()) {
- generate_deserialize_field(out, *f_iter, "(*(this->", "))");
- } else {
- generate_deserialize_field(out, *f_iter, "this->");
+ if (pointers && !(*f_iter)->get_type()->is_xception()) {
+ generate_deserialize_field(out, *f_iter, "(*(this->", "))");
+ } else {
+ generate_deserialize_field(out, *f_iter, "this->");
+ }
+ out <<
+ indent() << isset_prefix << (*f_iter)->get_name() << " = true;" << endl;
+ indent_down();
+ out <<
+ indent() << "} else {" << endl <<
+ indent() << " xfer += iprot->skip(ftype);" << endl <<
+ // TODO(dreiss): Make this an option when thrift structs
+ // have a common base class.
+ // indent() << " throw TProtocolException(TProtocolException::INVALID_DATA);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
}
- out << indent() << isset_prefix << (*f_iter)->get_name() << " = true;" << endl;
- indent_down();
- out << indent() << "} else {" << endl << indent() << " xfer += iprot->skip(ftype);" << endl
- <<
- // TODO(dreiss): Make this an option when thrift structs
- // have a common base class.
- // indent() << " throw TProtocolException(TProtocolException::INVALID_DATA);" << endl <<
- indent() << "}" << endl << indent() << "break;" << endl;
- indent_down();
- }
- // In the default case we skip the field
- out << indent() << "default:" << endl << indent() << " xfer += iprot->skip(ftype);" << endl
- << indent() << " break;" << endl;
+ // In the default case we skip the field
+ out <<
+ indent() << "default:" << endl <<
+ indent() << " xfer += iprot->skip(ftype);" << endl <<
+ indent() << " break;" << endl;
+
+ scope_down(out);
+ } //!fields.empty()
+ // Read field end marker
+ indent(out) <<
+ "xfer += iprot->readFieldEnd();" << endl;
scope_down(out);
- } //!fields.empty()
- // Read field end marker
- indent(out) << "xfer += iprot->readFieldEnd();" << endl;
- scope_down(out);
-
- out << endl << indent() << "xfer += iprot->readStructEnd();" << endl;
+ out <<
+ endl <<
+ indent() << "xfer += iprot->readStructEnd();" << endl;
// Throw if any required fields are missing.
// We do this after reading the struct end so that
@@ -1362,14 +1527,16 @@
out << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED)
- out << indent() << "if (!isset_" << (*f_iter)->get_name() << ')' << endl << indent()
- << " throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
+ out <<
+ indent() << "if (!isset_" << (*f_iter)->get_name() << ')' << endl <<
+ indent() << " throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
}
indent(out) << "return xfer;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl << endl;
}
/**
@@ -1378,28 +1545,35 @@
* @param out Stream to write to
* @param tstruct The struct
*/
-void t_cpp_generator::generate_struct_writer(ofstream& out, t_struct* tstruct, bool pointers) {
+void t_cpp_generator::generate_struct_writer(ofstream& out,
+ t_struct* tstruct,
+ bool pointers) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl << indent() << "uint32_t "
- << tstruct->get_name() << "::write(Protocol_* oprot) const {" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl <<
+ indent() << "uint32_t " << tstruct->get_name() <<
+ "::write(Protocol_* oprot) const {" << endl;
} else {
- indent(out) << "uint32_t " << tstruct->get_name()
- << "::write(::apache::thrift::protocol::TProtocol* oprot) const {" << endl;
+ indent(out) <<
+ "uint32_t " << tstruct->get_name() <<
+ "::write(::apache::thrift::protocol::TProtocol* oprot) const {" << endl;
}
indent_up();
- out << indent() << "uint32_t xfer = 0;" << endl;
+ out <<
+ indent() << "uint32_t xfer = 0;" << endl;
indent(out) << "oprot->incrementRecursionDepth();" << endl;
- indent(out) << "xfer += oprot->writeStructBegin(\"" << name << "\");" << endl;
+ indent(out) <<
+ "xfer += oprot->writeStructBegin(\"" << name << "\");" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- bool check_if_set = (*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_type()->is_xception();
+ bool check_if_set = (*f_iter)->get_req() == t_field::T_OPTIONAL ||
+ (*f_iter)->get_type()->is_xception();
if (check_if_set) {
out << endl << indent() << "if (this->__isset." << (*f_iter)->get_name() << ") {" << endl;
indent_up();
@@ -1408,9 +1582,11 @@
}
// Write field header
- out << indent() << "xfer += oprot->writeFieldBegin("
- << "\"" << (*f_iter)->get_name() << "\", " << type_to_enum((*f_iter)->get_type()) << ", "
- << (*f_iter)->get_key() << ");" << endl;
+ out <<
+ indent() << "xfer += oprot->writeFieldBegin(" <<
+ "\"" << (*f_iter)->get_name() << "\", " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ");" << endl;
// Write field contents
if (pointers && !(*f_iter)->get_type()->is_xception()) {
generate_serialize_field(out, *f_iter, "(*(this->", "))");
@@ -1418,7 +1594,8 @@
generate_serialize_field(out, *f_iter, "this->");
}
// Write field closer
- indent(out) << "xfer += oprot->writeFieldEnd();" << endl;
+ indent(out) <<
+ "xfer += oprot->writeFieldEnd();" << endl;
if (check_if_set) {
indent_down();
indent(out) << '}';
@@ -1428,12 +1605,16 @@
out << endl;
// Write the struct map
- out << indent() << "xfer += oprot->writeFieldStop();" << endl << indent()
- << "xfer += oprot->writeStructEnd();" << endl << indent()
- << "oprot->decrementRecursionDepth();" << endl << indent() << "return xfer;" << endl;
+ out <<
+ indent() << "xfer += oprot->writeFieldStop();" << endl <<
+ indent() << "xfer += oprot->writeStructEnd();" << endl <<
+ indent() << "oprot->decrementRecursionDepth();" << endl <<
+ indent() << "return xfer;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -1452,25 +1633,35 @@
vector<t_field*>::const_iterator f_iter;
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl << indent() << "uint32_t "
- << tstruct->get_name() << "::write(Protocol_* oprot) const {" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl <<
+ indent() << "uint32_t " << tstruct->get_name() <<
+ "::write(Protocol_* oprot) const {" << endl;
} else {
- indent(out) << "uint32_t " << tstruct->get_name()
- << "::write(::apache::thrift::protocol::TProtocol* oprot) const {" << endl;
+ indent(out) <<
+ "uint32_t " << tstruct->get_name() <<
+ "::write(::apache::thrift::protocol::TProtocol* oprot) const {" << endl;
}
indent_up();
- out << endl << indent() << "uint32_t xfer = 0;" << endl << endl;
+ out <<
+ endl <<
+ indent() << "uint32_t xfer = 0;" << endl <<
+ endl;
- indent(out) << "xfer += oprot->writeStructBegin(\"" << name << "\");" << endl;
+ indent(out) <<
+ "xfer += oprot->writeStructBegin(\"" << name << "\");" << endl;
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
- out << endl << indent() << "if ";
+ out <<
+ endl <<
+ indent() << "if ";
} else {
- out << " else if ";
+ out <<
+ " else if ";
}
out << "(this->__isset." << (*f_iter)->get_name() << ") {" << endl;
@@ -1478,9 +1669,11 @@
indent_up();
// Write field header
- out << indent() << "xfer += oprot->writeFieldBegin("
- << "\"" << (*f_iter)->get_name() << "\", " << type_to_enum((*f_iter)->get_type()) << ", "
- << (*f_iter)->get_key() << ");" << endl;
+ out <<
+ indent() << "xfer += oprot->writeFieldBegin(" <<
+ "\"" << (*f_iter)->get_name() << "\", " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ");" << endl;
// Write field contents
if (pointers) {
generate_serialize_field(out, *f_iter, "(*(this->", "))");
@@ -1495,11 +1688,16 @@
}
// Write the struct map
- out << endl << indent() << "xfer += oprot->writeFieldStop();" << endl << indent()
- << "xfer += oprot->writeStructEnd();" << endl << indent() << "return xfer;" << endl;
+ out <<
+ endl <<
+ indent() << "xfer += oprot->writeFieldStop();" << endl <<
+ indent() << "xfer += oprot->writeStructEnd();" << endl <<
+ indent() << "return xfer;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -1509,76 +1707,93 @@
* @param tstruct The struct
*/
void t_cpp_generator::generate_struct_swap(ofstream& out, t_struct* tstruct) {
- out << indent() << "void swap(" << tstruct->get_name() << " &a, " << tstruct->get_name()
- << " &b) {" << endl;
+ out <<
+ indent() << "void swap(" << tstruct->get_name() << " &a, " <<
+ tstruct->get_name() << " &b) {" << endl;
indent_up();
// Let argument-dependent name lookup find the correct swap() function to
// use based on the argument types. If none is found in the arguments'
// namespaces, fall back to ::std::swap().
- out << indent() << "using ::std::swap;" << endl;
+ out <<
+ indent() << "using ::std::swap;" << endl;
bool has_nonrequired_fields = false;
const vector<t_field*>& fields = tstruct->get_members();
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- t_field* tfield = *f_iter;
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
+ t_field *tfield = *f_iter;
if (tfield->get_req() != t_field::T_REQUIRED) {
has_nonrequired_fields = true;
}
- out << indent() << "swap(a." << tfield->get_name() << ", b." << tfield->get_name() << ");"
- << endl;
+ out <<
+ indent() << "swap(a." << tfield->get_name() <<
+ ", b." << tfield->get_name() << ");" << endl;
}
if (has_nonrequired_fields) {
- out << indent() << "swap(a.__isset, b.__isset);" << endl;
+ out <<
+ indent() << "swap(a.__isset, b.__isset);" << endl;
}
// handle empty structs
if (fields.size() == 0) {
- out << indent() << "(void) a;" << endl;
- out << indent() << "(void) b;" << endl;
+ out <<
+ indent() << "(void) a;" << endl;
+ out <<
+ indent() << "(void) b;" << endl;
}
scope_down(out);
out << endl;
}
-void t_cpp_generator::generate_struct_ostream_operator_decl(std::ofstream& out, t_struct* tstruct) {
- out << "std::ostream& operator<<(std::ostream& out, const " << tstruct->get_name() << "& obj)";
+void t_cpp_generator::generate_struct_ostream_operator_decl(std::ofstream& out,
+ t_struct* tstruct) {
+ out << "std::ostream& operator<<(std::ostream& out, const "
+ << tstruct->get_name() << "& obj)";
}
-namespace struct_ostream_operator_generator {
-void generate_required_field_value(std::ofstream& out, const t_field* field) {
+namespace struct_ostream_operator_generator
+{
+void generate_required_field_value(std::ofstream& out, const t_field* field)
+{
out << " << to_string(obj." << field->get_name() << ")";
}
-void generate_optional_field_value(std::ofstream& out, const t_field* field) {
+void generate_optional_field_value(std::ofstream& out, const t_field* field)
+{
out << "; (obj.__isset." << field->get_name() << " ? (out";
generate_required_field_value(out, field);
out << ") : (out << \"<null>\"))";
}
-void generate_field_value(std::ofstream& out, const t_field* field) {
+void generate_field_value(std::ofstream& out, const t_field* field)
+{
if (field->get_req() == t_field::T_OPTIONAL)
generate_optional_field_value(out, field);
else
generate_required_field_value(out, field);
}
-void generate_field_name(std::ofstream& out, const t_field* field) {
+void generate_field_name(std::ofstream& out, const t_field* field)
+{
out << "\"" << field->get_name() << "=\"";
}
-void generate_field(std::ofstream& out, const t_field* field) {
+void generate_field(std::ofstream& out, const t_field* field)
+{
generate_field_name(out, field);
generate_field_value(out, field);
}
void generate_fields(std::ofstream& out,
const vector<t_field*>& fields,
- const std::string& indent) {
+ const std::string& indent)
+{
const vector<t_field*>::const_iterator beg = fields.begin();
const vector<t_field*>::const_iterator end = fields.end();
@@ -1593,35 +1808,45 @@
out << ";" << endl;
}
}
+
+
}
/**
* Generates operator<<
*/
-void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out, t_struct* tstruct) {
+void t_cpp_generator::generate_struct_ostream_operator(std::ofstream& out,
+ t_struct* tstruct) {
out << indent();
generate_struct_ostream_operator_decl(out, tstruct);
out << " {" << endl;
indent_up();
- out << indent() << "using apache::thrift::to_string;" << endl;
+ out <<
+ indent() << "using apache::thrift::to_string;" << endl;
// eliminate compiler unused warning
const vector<t_field*>& fields = tstruct->get_members();
if (fields.empty())
out << indent() << "(void) obj;" << endl;
- out << indent() << "out << \"" << tstruct->get_name() << "(\";" << endl;
+ out <<
+ indent() << "out << \"" << tstruct->get_name() << "(\";" << endl;
- struct_ostream_operator_generator::generate_fields(out, fields, indent());
+ struct_ostream_operator_generator::generate_fields(out,
+ fields,
+ indent());
- out << indent() << "out << \")\";" << endl << indent() << "return out;" << endl;
+ out <<
+ indent() << "out << \")\";" << endl <<
+ indent() << "return out;" << endl;
indent_down();
out << "}" << endl << endl;
}
+
/**
* Generates a thrift service. In C++, this comprises an entirely separate
* header and source file. The header file defines the methods and includes
@@ -1634,62 +1859,85 @@
string svcname = tservice->get_name();
// Make output files
- string f_header_name = get_out_dir() + svcname + ".h";
+ string f_header_name = get_out_dir()+svcname+".h";
f_header_.open(f_header_name.c_str());
// Print header file includes
- f_header_ << autogen_comment();
- f_header_ << "#ifndef " << svcname << "_H" << endl << "#define " << svcname << "_H" << endl
- << endl;
+ f_header_ <<
+ autogen_comment();
+ f_header_ <<
+ "#ifndef " << svcname << "_H" << endl <<
+ "#define " << svcname << "_H" << endl <<
+ endl;
if (gen_cob_style_) {
- f_header_ << "#include <thrift/transport/TBufferTransports.h>" << endl << // TMemoryBuffer
- "#include <thrift/cxxfunctional.h>" << endl
- << "namespace apache { namespace thrift { namespace async {" << endl
- << "class TAsyncChannel;" << endl << "}}}" << endl;
+ f_header_ <<
+ "#include <thrift/transport/TBufferTransports.h>" << endl << // TMemoryBuffer
+ "#include <thrift/cxxfunctional.h>" << endl <<
+ "namespace apache { namespace thrift { namespace async {" << endl <<
+ "class TAsyncChannel;" << endl <<
+ "}}}" << endl;
}
- f_header_ << "#include <thrift/TDispatchProcessor.h>" << endl;
+ f_header_ <<
+ "#include <thrift/TDispatchProcessor.h>" << endl;
if (gen_cob_style_) {
- f_header_ << "#include <thrift/async/TAsyncDispatchProcessor.h>" << endl;
+ f_header_ <<
+ "#include <thrift/async/TAsyncDispatchProcessor.h>" << endl;
}
- f_header_ << "#include \"" << get_include_prefix(*get_program()) << program_name_ << "_types.h\""
- << endl;
+ f_header_ <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.h\"" << endl;
t_service* extends_service = tservice->get_extends();
if (extends_service != NULL) {
- f_header_ << "#include \"" << get_include_prefix(*(extends_service->get_program()))
- << extends_service->get_name() << ".h\"" << endl;
+ f_header_ <<
+ "#include \"" << get_include_prefix(*(extends_service->get_program())) <<
+ extends_service->get_name() << ".h\"" << endl;
}
- f_header_ << endl << ns_open_ << endl << endl;
+ f_header_ <<
+ endl <<
+ ns_open_ << endl <<
+ endl;
// Service implementation file includes
- string f_service_name = get_out_dir() + svcname + ".cpp";
+ string f_service_name = get_out_dir()+svcname+".cpp";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment();
- f_service_ << "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl;
+ f_service_ <<
+ autogen_comment();
+ f_service_ <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl;
if (gen_cob_style_) {
- f_service_ << "#include \"thrift/async/TAsyncChannel.h\"" << endl;
+ f_service_ <<
+ "#include \"thrift/async/TAsyncChannel.h\"" << endl;
}
if (gen_templates_) {
- f_service_ << "#include \"" << get_include_prefix(*get_program()) << svcname << ".tcc\""
- << endl;
+ f_service_ <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname <<
+ ".tcc\"" << endl;
- string f_service_tcc_name = get_out_dir() + svcname + ".tcc";
+ string f_service_tcc_name = get_out_dir()+svcname+".tcc";
f_service_tcc_.open(f_service_tcc_name.c_str());
- f_service_tcc_ << autogen_comment();
- f_service_tcc_ << "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\""
- << endl;
+ f_service_tcc_ <<
+ autogen_comment();
+ f_service_tcc_ <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname <<
+ ".h\"" << endl;
- f_service_tcc_ << "#ifndef " << svcname << "_TCC" << endl << "#define " << svcname << "_TCC"
- << endl << endl;
+ f_service_tcc_ <<
+ "#ifndef " << svcname << "_TCC" << endl <<
+ "#define " << svcname << "_TCC" << endl <<
+ endl;
if (gen_cob_style_) {
- f_service_tcc_ << "#include \"thrift/async/TAsyncChannel.h\"" << endl;
+ f_service_tcc_ <<
+ "#include \"thrift/async/TAsyncChannel.h\"" << endl;
}
}
- f_service_ << endl << ns_open_ << endl << endl;
- f_service_tcc_ << endl << ns_open_ << endl << endl;
+ f_service_ <<
+ endl << ns_open_ << endl << endl;
+ f_service_tcc_ <<
+ endl << ns_open_ << endl << endl;
// Generate all the components
generate_service_interface(tservice, "");
@@ -1713,19 +1961,30 @@
}
// Close the namespace
- f_service_ << ns_close_ << endl << endl;
- f_service_tcc_ << ns_close_ << endl << endl;
- f_header_ << ns_close_ << endl << endl;
+ f_service_ <<
+ ns_close_ << endl <<
+ endl;
+ f_service_tcc_ <<
+ ns_close_ << endl <<
+ endl;
+ f_header_ <<
+ ns_close_ << endl <<
+ endl;
// TODO(simpkins): Make this a separate option
if (gen_templates_) {
- f_header_ << "#include \"" << get_include_prefix(*get_program()) << svcname << ".tcc\"" << endl
- << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_types.tcc\"" << endl << endl;
+ f_header_ <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname <<
+ ".tcc\"" << endl <<
+ "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
+ "_types.tcc\"" << endl <<
+ endl;
}
- f_header_ << "#endif" << endl;
- f_service_tcc_ << "#endif" << endl;
+ f_header_ <<
+ "#endif" << endl;
+ f_service_tcc_ <<
+ "#endif" << endl;
// Close the files
f_service_tcc_.close();
@@ -1778,14 +2037,17 @@
if (gen_templates_) {
client_name += "T";
service_if_name += "T";
- indent(f_header_) << "template <class Protocol_>" << endl;
+ indent(f_header_) <<
+ "template <class Protocol_>" << endl;
}
- indent(f_header_) << "class " << client_name << ";" << endl << endl;
+ indent(f_header_) << "class " << client_name << ";" <<
+ endl << endl;
}
string extends = "";
if (tservice->get_extends() != NULL) {
- extends = " : virtual public " + type_name(tservice->get_extends()) + style + "If";
+ extends = " : virtual public " + type_name(tservice->get_extends()) +
+ style + "If";
if (style == "CobCl" && gen_templates_) {
// TODO(simpkins): If gen_templates_ is enabled, we currently assume all
// parent services were also generated with templates enabled.
@@ -1796,26 +2058,33 @@
if (style == "CobCl" && gen_templates_) {
f_header_ << "template <class Protocol_>" << endl;
}
- f_header_ << "class " << service_if_name << extends << " {" << endl << " public:" << endl;
+ f_header_ <<
+ "class " << service_if_name << extends << " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << "virtual ~" << service_if_name << "() {}" << endl;
+ f_header_ <<
+ indent() << "virtual ~" << service_if_name << "() {}" << endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- if ((*f_iter)->has_doc())
- f_header_ << endl;
+ if ((*f_iter)->has_doc()) f_header_ << endl;
generate_java_doc(f_header_, *f_iter);
- f_header_ << indent() << "virtual " << function_signature(*f_iter, style) << " = 0;" << endl;
+ f_header_ <<
+ indent() << "virtual " << function_signature(*f_iter, style) << " = 0;" << endl;
}
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
if (style == "CobCl" && gen_templates_) {
// generate a backwards-compatible typedef for clients that do not
// know about the new template-style code
- f_header_ << "typedef " << service_if_name << "< ::apache::thrift::protocol::TProtocol> "
- << service_name_ << style << "If;" << endl << endl;
+ f_header_ <<
+ "typedef " << service_if_name <<
+ "< ::apache::thrift::protocol::TProtocol> " <<
+ service_name_ << style << "If;" <<
+ endl << endl;
}
}
@@ -1824,7 +2093,8 @@
*
* @param tservice The service to generate an interface factory for.
*/
-void t_cpp_generator::generate_service_interface_factory(t_service* tservice, string style) {
+void t_cpp_generator::generate_service_interface_factory(t_service* tservice,
+ string style) {
string service_if_name = service_name_ + style + "If";
// Figure out the name of the upper-most parent class.
@@ -1855,38 +2125,56 @@
string factory_name = service_if_name + "Factory";
string extends;
if (tservice->get_extends() != NULL) {
- extends = " : virtual public " + type_name(tservice->get_extends()) + style + "IfFactory";
+ extends = " : virtual public " + type_name(tservice->get_extends()) +
+ style + "IfFactory";
}
- f_header_ << "class " << factory_name << extends << " {" << endl << " public:" << endl;
+ f_header_ <<
+ "class " << factory_name << extends << " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << "typedef " << service_if_name << " Handler;" << endl << endl << indent()
- << "virtual ~" << factory_name << "() {}" << endl << endl << indent() << "virtual "
- << service_if_name << "* getHandler("
- << "const ::apache::thrift::TConnectionInfo& connInfo) = 0;" << endl << indent()
- << "virtual void releaseHandler(" << base_if_name << "* /* handler */) = 0;" << endl;
+ f_header_ <<
+ indent() << "typedef " << service_if_name << " Handler;" << endl <<
+ endl <<
+ indent() << "virtual ~" << factory_name << "() {}" << endl <<
+ endl <<
+ indent() << "virtual " << service_if_name << "* getHandler(" <<
+ "const ::apache::thrift::TConnectionInfo& connInfo) = 0;" <<
+ endl <<
+ indent() << "virtual void releaseHandler(" << base_if_name <<
+ "* /* handler */) = 0;" << endl;
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
// Generate the singleton factory class
string singleton_factory_name = service_if_name + "SingletonFactory";
- f_header_ << "class " << singleton_factory_name << " : virtual public " << factory_name << " {"
- << endl << " public:" << endl;
+ f_header_ <<
+ "class " << singleton_factory_name <<
+ " : virtual public " << factory_name << " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << singleton_factory_name << "(const boost::shared_ptr<" << service_if_name
- << ">& iface) : iface_(iface) {}" << endl << indent() << "virtual ~"
- << singleton_factory_name << "() {}" << endl << endl << indent() << "virtual "
- << service_if_name << "* getHandler("
- << "const ::apache::thrift::TConnectionInfo&) {" << endl << indent()
- << " return iface_.get();" << endl << indent() << "}" << endl << indent()
- << "virtual void releaseHandler(" << base_if_name << "* /* handler */) {}" << endl;
+ f_header_ <<
+ indent() << singleton_factory_name << "(const boost::shared_ptr<" <<
+ service_if_name << ">& iface) : iface_(iface) {}" << endl <<
+ indent() << "virtual ~" << singleton_factory_name << "() {}" << endl <<
+ endl <<
+ indent() << "virtual " << service_if_name << "* getHandler(" <<
+ "const ::apache::thrift::TConnectionInfo&) {" << endl <<
+ indent() << " return iface_.get();" << endl <<
+ indent() << "}" << endl <<
+ indent() << "virtual void releaseHandler(" << base_if_name <<
+ "* /* handler */) {}" << endl;
- f_header_ << endl << " protected:" << endl << indent() << "boost::shared_ptr<" << service_if_name
- << "> iface_;" << endl;
+ f_header_ <<
+ endl <<
+ " protected:" << endl <<
+ indent() << "boost::shared_ptr<" << service_if_name << "> iface_;" << endl;
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
}
/**
@@ -1899,14 +2187,17 @@
if (tservice->get_extends() != NULL) {
extends = " , virtual public " + type_name(tservice->get_extends()) + style + "Null";
}
- f_header_ << "class " << service_name_ << style << "Null : virtual public " << service_name_
- << style << "If" << extends << " {" << endl << " public:" << endl;
+ f_header_ <<
+ "class " << service_name_ << style << "Null : virtual public " << service_name_ << style << "If" << extends << " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << "virtual ~" << service_name_ << style << "Null() {}" << endl;
+ f_header_ <<
+ indent() << "virtual ~" << service_name_ << style << "Null() {}" << endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_header_ << indent() << function_signature(*f_iter, style, "", false) << " {" << endl;
+ f_header_ <<
+ indent() << function_signature(*f_iter, style, "", false) << " {" << endl;
indent_up();
t_type* returntype = (*f_iter)->get_returntype();
@@ -1916,34 +2207,34 @@
if (returntype->is_void() || is_complex_type(returntype)) {
f_header_ << indent() << "return;" << endl;
} else {
- f_header_ << indent() << declare_field(&returnfield, true) << endl << indent()
- << "return _return;" << endl;
+ f_header_ <<
+ indent() << declare_field(&returnfield, true) << endl <<
+ indent() << "return _return;" << endl;
}
} else if (style == "CobSv") {
if (returntype->is_void()) {
f_header_ << indent() << "return cob();" << endl;
- } else {
- t_field returnfield(returntype, "_return");
- f_header_ << indent() << declare_field(&returnfield, true) << endl << indent()
- << "return cob(_return);" << endl;
- }
+ } else {
+ t_field returnfield(returntype, "_return");
+ f_header_ <<
+ indent() << declare_field(&returnfield, true) << endl <<
+ indent() << "return cob(_return);" << endl;
+ }
} else {
throw "UNKNOWN STYLE";
}
indent_down();
- f_header_ << indent() << "}" << endl;
+ f_header_ <<
+ indent() << "}" << endl;
}
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
}
-void t_cpp_generator::generate_function_call(ostream& out,
- t_function* tfunction,
- string target,
- string iface,
- string arg_prefix) {
+void t_cpp_generator::generate_function_call(ostream& out, t_function* tfunction, string target, string iface, string arg_prefix) {
bool first = true;
t_type* ret_type = get_true_type(tfunction->get_returntype());
out << indent();
@@ -1974,46 +2265,58 @@
string svcname = tservice->get_name();
// Service implementation file includes
- string f_skeleton_name = get_out_dir() + svcname + "_async_server.skeleton.cpp";
+ string f_skeleton_name = get_out_dir()+svcname+"_async_server.skeleton.cpp";
string ns = namespace_prefix(tservice->get_program()->get_namespace("cpp"));
ofstream f_skeleton;
f_skeleton.open(f_skeleton_name.c_str());
- f_skeleton << "// This autogenerated skeleton file illustrates one way to adapt a synchronous"
- << endl << "// interface into an asynchronous interface. You should copy it to another"
- << endl
- << "// filename to avoid overwriting it and rewrite as asynchronous any functions"
- << endl << "// that would otherwise introduce unwanted latency." << endl << endl
- << "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl
- << "#include <thrift/protocol/TBinaryProtocol.h>" << endl << endl
- << "using namespace ::apache::thrift;" << endl
- << "using namespace ::apache::thrift::protocol;" << endl
- << "using namespace ::apache::thrift::transport;" << endl
- << "using namespace ::apache::thrift::async;" << endl << endl
- << "using boost::shared_ptr;" << endl << endl;
+ f_skeleton <<
+ "// This autogenerated skeleton file illustrates one way to adapt a synchronous" << endl <<
+ "// interface into an asynchronous interface. You should copy it to another" << endl <<
+ "// filename to avoid overwriting it and rewrite as asynchronous any functions" << endl <<
+ "// that would otherwise introduce unwanted latency." << endl <<
+ endl <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl <<
+ "#include <thrift/protocol/TBinaryProtocol.h>" << endl <<
+ endl <<
+ "using namespace ::apache::thrift;" << endl <<
+ "using namespace ::apache::thrift::protocol;" << endl <<
+ "using namespace ::apache::thrift::transport;" << endl <<
+ "using namespace ::apache::thrift::async;" << endl <<
+ endl <<
+ "using boost::shared_ptr;" << endl <<
+ endl;
// the following code would not compile:
// using namespace ;
// using namespace ::;
- if ((!ns.empty()) && (ns.compare(" ::") != 0)) {
- f_skeleton << "using namespace " << string(ns, 0, ns.size() - 2) << ";" << endl << endl;
+ if ( (!ns.empty()) && (ns.compare(" ::") != 0)) {
+ f_skeleton <<
+ "using namespace " << string(ns, 0, ns.size()-2) << ";" << endl <<
+ endl;
}
- f_skeleton << "class " << svcname << "AsyncHandler : "
- << "public " << svcname << "CobSvIf {" << endl << " public:" << endl;
+ f_skeleton <<
+ "class " << svcname << "AsyncHandler : " <<
+ "public " << svcname << "CobSvIf {" << endl <<
+ " public:" << endl;
indent_up();
- f_skeleton << indent() << svcname << "AsyncHandler() {" << endl << indent()
- << " syncHandler_ = std::auto_ptr<" << svcname << "Handler>(new " << svcname
- << "Handler);" << endl << indent() << " // Your initialization goes here" << endl
- << indent() << "}" << endl;
- f_skeleton << indent() << "virtual ~" << service_name_ << "AsyncHandler();" << endl;
+ f_skeleton <<
+ indent() << svcname << "AsyncHandler() {" << endl <<
+ indent() << " syncHandler_ = std::auto_ptr<" << svcname <<
+ "Handler>(new " << svcname << "Handler);" << endl <<
+ indent() << " // Your initialization goes here" << endl <<
+ indent() << "}" << endl;
+ f_skeleton <<
+ indent() << "virtual ~" << service_name_ << "AsyncHandler();" << endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_skeleton << endl << indent() << function_signature(*f_iter, "CobSv", "", true) << " {"
- << endl;
+ f_skeleton <<
+ endl <<
+ indent() << function_signature(*f_iter, "CobSv", "", true) << " {" << endl;
indent_up();
t_type* returntype = (*f_iter)->get_returntype();
@@ -2021,17 +2324,20 @@
string target = returntype->is_void() ? "" : "_return";
if (!returntype->is_void()) {
- f_skeleton << indent() << declare_field(&returnfield, true) << endl;
+ f_skeleton <<
+ indent() << declare_field(&returnfield, true) << endl;
}
generate_function_call(f_skeleton, *f_iter, target, "syncHandler_", "");
f_skeleton << indent() << "return cob(" << target << ");" << endl;
scope_down(f_skeleton);
}
- f_skeleton << endl << " protected:" << endl << indent() << "std::auto_ptr<" << svcname
- << "Handler> syncHandler_;" << endl;
+ f_skeleton << endl <<
+ " protected:" << endl <<
+ indent() << "std::auto_ptr<" << svcname << "Handler> syncHandler_;" << endl;
indent_down();
- f_skeleton << "};" << endl << endl;
+ f_skeleton <<
+ "};" << endl << endl;
}
/**
@@ -2056,36 +2362,45 @@
string list_type = string("std::vector<boost::shared_ptr<") + service_name_ + "If> >";
// Generate the header portion
- f_header_ << "class " << service_name_ << "Multiface : "
- << "virtual public " << service_name_ << "If" << extends_multiface << " {" << endl
- << " public:" << endl;
+ f_header_ <<
+ "class " << service_name_ << "Multiface : " <<
+ "virtual public " << service_name_ << "If" <<
+ extends_multiface << " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << service_name_ << "Multiface(" << list_type
- << "& ifaces) : ifaces_(ifaces) {" << endl;
+ f_header_ <<
+ indent() << service_name_ << "Multiface(" << list_type << "& ifaces) : ifaces_(ifaces) {" << endl;
if (!extends.empty()) {
- f_header_ << indent()
- << " std::vector<boost::shared_ptr<" + service_name_ + "If> >::iterator iter;"
- << endl << indent() << " for (iter = ifaces.begin(); iter != ifaces.end(); ++iter) {"
- << endl << indent() << " " << extends << "Multiface::add(*iter);" << endl
- << indent() << " }" << endl;
+ f_header_ <<
+ indent() << " std::vector<boost::shared_ptr<" + service_name_ + "If> >::iterator iter;" << endl <<
+ indent() << " for (iter = ifaces.begin(); iter != ifaces.end(); ++iter) {" << endl <<
+ indent() << " " << extends << "Multiface::add(*iter);" << endl <<
+ indent() << " }" << endl;
}
- f_header_ << indent() << "}" << endl << indent() << "virtual ~" << service_name_
- << "Multiface() {}" << endl;
+ f_header_ <<
+ indent() << "}" << endl <<
+ indent() << "virtual ~" << service_name_ << "Multiface() {}" << endl;
indent_down();
// Protected data members
- f_header_ << " protected:" << endl;
+ f_header_ <<
+ " protected:" << endl;
indent_up();
- f_header_ << indent() << list_type << " ifaces_;" << endl << indent() << service_name_
- << "Multiface() {}" << endl << indent() << "void add(boost::shared_ptr<"
- << service_name_ << "If> iface) {" << endl;
+ f_header_ <<
+ indent() << list_type << " ifaces_;" << endl <<
+ indent() << service_name_ << "Multiface() {}" << endl <<
+ indent() << "void add(boost::shared_ptr<" << service_name_ << "If> iface) {" << endl;
if (!extends.empty()) {
- f_header_ << indent() << " " << extends << "Multiface::add(iface);" << endl;
+ f_header_ <<
+ indent() << " " << extends << "Multiface::add(iface);" << endl;
}
- f_header_ << indent() << " ifaces_.push_back(iface);" << endl << indent() << "}" << endl;
+ f_header_ <<
+ indent() << " ifaces_.push_back(iface);" << endl <<
+ indent() << "}" << endl;
indent_down();
- f_header_ << indent() << " public:" << endl;
+ f_header_ <<
+ indent() << " public:" << endl;
indent_up();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
@@ -2109,31 +2424,44 @@
}
call += ")";
- f_header_ << indent() << function_signature(*f_iter, "") << " {" << endl;
+ f_header_ <<
+ indent() << function_signature(*f_iter, "") << " {" << endl;
indent_up();
- f_header_ << indent() << "size_t sz = ifaces_.size();" << endl << indent() << "size_t i = 0;"
- << endl << indent() << "for (; i < (sz - 1); ++i) {" << endl;
+ f_header_ <<
+ indent() << "size_t sz = ifaces_.size();" << endl <<
+ indent() << "size_t i = 0;" << endl <<
+ indent() << "for (; i < (sz - 1); ++i) {" << endl;
indent_up();
- f_header_ << indent() << call << ";" << endl;
+ f_header_ <<
+ indent() << call << ";" << endl;
indent_down();
- f_header_ << indent() << "}" << endl;
+ f_header_ <<
+ indent() << "}" << endl;
if (!(*f_iter)->get_returntype()->is_void()) {
if (is_complex_type((*f_iter)->get_returntype())) {
- f_header_ << indent() << call << ";" << endl << indent() << "return;" << endl;
+ f_header_ <<
+ indent() << call << ";" << endl <<
+ indent() << "return;" << endl;
} else {
- f_header_ << indent() << "return " << call << ";" << endl;
+ f_header_ <<
+ indent() << "return " << call << ";" << endl;
}
} else {
- f_header_ << indent() << call << ";" << endl;
+ f_header_ <<
+ indent() << call << ";" << endl;
}
indent_down();
- f_header_ << indent() << "}" << endl << endl;
+ f_header_ <<
+ indent() << "}" << endl <<
+ endl;
}
indent_down();
- f_header_ << indent() << "};" << endl << endl;
+ f_header_ <<
+ indent() << "};" << endl <<
+ endl;
}
/**
@@ -2149,7 +2477,8 @@
std::ofstream& out = (gen_templates_ ? f_service_tcc_ : f_service_);
string template_header, template_suffix, short_suffix, protocol_type, _this;
- string const prot_factory_type = "::apache::thrift::protocol::TProtocolFactory";
+ string const prot_factory_type =
+ "::apache::thrift::protocol::TProtocolFactory";
if (gen_templates_) {
template_header = "template <class Protocol_>\n";
short_suffix = "T";
@@ -2176,110 +2505,136 @@
}
// Generate the header portion
- f_header_ << template_header << "class " << service_name_ << style << "Client" << short_suffix
- << " : "
- << "virtual public " << service_name_ << ifstyle << if_suffix << extends_client << " {"
- << endl << " public:" << endl;
+ f_header_ <<
+ template_header <<
+ "class " << service_name_ << style << "Client" << short_suffix << " : " <<
+ "virtual public " << service_name_ << ifstyle << if_suffix <<
+ extends_client << " {" << endl <<
+ " public:" << endl;
indent_up();
if (style != "Cob") {
- f_header_ << indent() << service_name_ << style << "Client" << short_suffix << "(" << prot_ptr
- << " prot) ";
+ f_header_ <<
+ indent() << service_name_ << style << "Client" << short_suffix <<
+ "(" << prot_ptr << " prot) ";
if (extends.empty()) {
- f_header_ << "{" << endl;
- f_header_ << indent() << " setProtocol" << short_suffix << "(prot);" << endl << indent()
- << "}" << endl;
+ f_header_ << "{" << endl;
+ f_header_ <<
+ indent() << " setProtocol" << short_suffix << "(prot);" << endl <<
+ indent() << "}" << endl;
} else {
- f_header_ << ":" << endl;
- f_header_ << indent() << " " << extends << style << client_suffix << "(prot, prot) {}"
- << endl;
+ f_header_ << ":" << endl;
+ f_header_ <<
+ indent() << " " << extends << style << client_suffix <<
+ "(prot, prot) {}" << endl;
}
- f_header_ << indent() << service_name_ << style << "Client" << short_suffix << "(" << prot_ptr
- << " iprot, " << prot_ptr << " oprot) ";
+ f_header_ <<
+ indent() << service_name_ << style << "Client" << short_suffix <<
+ "(" << prot_ptr << " iprot, " << prot_ptr << " oprot) ";
if (extends.empty()) {
- f_header_ << "{" << endl;
- f_header_ << indent() << " setProtocol" << short_suffix << "(iprot,oprot);" << endl
- << indent() << "}" << endl;
+ f_header_ << "{" << endl;
+ f_header_ <<
+ indent() << " setProtocol" << short_suffix << "(iprot,oprot);" << endl <<
+ indent() << "}" << endl;
} else {
- f_header_ << ":" << indent() << " " << extends << style << client_suffix
- << "(iprot, oprot) {}" << endl;
+ f_header_ << ":" <<
+ indent() << " " << extends << style << client_suffix <<
+ "(iprot, oprot) {}" << endl;
}
// create the setProtocol methods
if (extends.empty()) {
- f_header_ << " private:" << endl;
+ f_header_ << " private:"<<endl;
// 1: one parameter
- f_header_ << indent() << "void setProtocol" << short_suffix << "(" << prot_ptr << " prot) {"
- << endl;
- f_header_ << indent() << "setProtocol" << short_suffix << "(prot,prot);" << endl;
- f_header_ << indent() << "}" << endl;
+ f_header_ <<
+ indent() << "void setProtocol" << short_suffix << "("
+ << prot_ptr << " prot) {" <<endl;
+ f_header_ << indent() << "setProtocol" << short_suffix << "(prot,prot);"<<endl;
+ f_header_ << indent() << "}"<<endl;
// 2: two parameter
- f_header_ << indent() << "void setProtocol" << short_suffix << "(" << prot_ptr << " iprot, "
- << prot_ptr << " oprot) {" << endl;
+ f_header_ <<
+ indent() << "void setProtocol" << short_suffix <<
+ "(" << prot_ptr << " iprot, " << prot_ptr << " oprot) {"<<endl;
- f_header_ << indent() << " piprot_=iprot;" << endl << indent() << " poprot_=oprot;" << endl
- << indent() << " iprot_ = iprot.get();" << endl << indent()
- << " oprot_ = oprot.get();" << endl;
+ f_header_ <<
+ indent() << " piprot_=iprot;"<<endl <<
+ indent() << " poprot_=oprot;"<<endl <<
+ indent() << " iprot_ = iprot.get();"<<endl <<
+ indent() << " oprot_ = oprot.get();"<<endl;
- f_header_ << indent() << "}" << endl;
- f_header_ << " public:" << endl;
+ f_header_ << indent() << "}"<<endl;
+ f_header_ << " public:"<<endl;
}
+
// Generate getters for the protocols.
// Note that these are not currently templated for simplicity.
// TODO(simpkins): should they be templated?
- f_header_ << indent()
- << "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() {"
- << endl << indent() << " return " << _this << "piprot_;" << endl << indent() << "}"
- << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() {" << endl <<
+ indent() << " return " << _this << "piprot_;" << endl <<
+ indent() << "}" << endl;
- f_header_ << indent()
- << "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() {"
- << endl << indent() << " return " << _this << "poprot_;" << endl << indent() << "}"
- << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() {" << endl <<
+ indent() << " return " << _this << "poprot_;" << endl <<
+ indent() << "}" << endl;
} else /* if (style == "Cob") */ {
- f_header_ << indent() << service_name_ << style << "Client" << short_suffix << "("
- << "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel, "
- << "::apache::thrift::protocol::TProtocolFactory* protocolFactory) :" << endl;
+ f_header_ <<
+ indent() << service_name_ << style << "Client" << short_suffix << "(" <<
+ "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel, " <<
+ "::apache::thrift::protocol::TProtocolFactory* protocolFactory) :" <<
+ endl;
if (extends.empty()) {
- f_header_ << indent() << " channel_(channel)," << endl << indent()
- << " itrans_(new ::apache::thrift::transport::TMemoryBuffer())," << endl
- << indent() << " otrans_(new ::apache::thrift::transport::TMemoryBuffer()),"
- << endl;
+ f_header_ <<
+ indent() << " channel_(channel)," << endl <<
+ indent() << " itrans_(new ::apache::thrift::transport::TMemoryBuffer())," << endl <<
+ indent() << " otrans_(new ::apache::thrift::transport::TMemoryBuffer())," << endl;
if (gen_templates_) {
// TProtocolFactory classes return generic TProtocol pointers.
// We have to dynamic cast to the Protocol_ type we are expecting.
- f_header_ << indent() << " piprot_(boost::dynamic_pointer_cast<Protocol_>("
- << "protocolFactory->getProtocol(itrans_)))," << endl << indent()
- << " poprot_(boost::dynamic_pointer_cast<Protocol_>("
- << "protocolFactory->getProtocol(otrans_))) {" << endl;
+ f_header_ <<
+ indent() << " piprot_(boost::dynamic_pointer_cast<Protocol_>(" <<
+ "protocolFactory->getProtocol(itrans_)))," << endl <<
+ indent() << " poprot_(boost::dynamic_pointer_cast<Protocol_>(" <<
+ "protocolFactory->getProtocol(otrans_))) {" << endl;
// Throw a TException if either dynamic cast failed.
- f_header_ << indent() << " if (!piprot_ || !poprot_) {" << endl << indent()
- << " throw ::apache::thrift::TException(\""
- << "TProtocolFactory returned unexpected protocol type in " << service_name_
- << style << "Client" << short_suffix << " constructor\");" << endl << indent()
- << " }" << endl;
+ f_header_ <<
+ indent() << " if (!piprot_ || !poprot_) {" << endl <<
+ indent() << " throw ::apache::thrift::TException(\"" <<
+ "TProtocolFactory returned unexpected protocol type in " <<
+ service_name_ << style << "Client" << short_suffix <<
+ " constructor\");" << endl <<
+ indent() << " }" << endl;
} else {
- f_header_ << indent() << " piprot_(protocolFactory->getProtocol(itrans_))," << endl
- << indent() << " poprot_(protocolFactory->getProtocol(otrans_)) {" << endl;
+ f_header_ <<
+ indent() << " piprot_(protocolFactory->getProtocol(itrans_))," <<
+ endl <<
+ indent() << " poprot_(protocolFactory->getProtocol(otrans_)) {" <<
+ endl;
}
- f_header_ << indent() << " iprot_ = piprot_.get();" << endl << indent()
- << " oprot_ = poprot_.get();" << endl << indent() << "}" << endl;
+ f_header_ <<
+ indent() << " iprot_ = piprot_.get();" << endl <<
+ indent() << " oprot_ = poprot_.get();" << endl <<
+ indent() << "}" << endl;
} else {
- f_header_ << indent() << " " << extends << style << client_suffix
- << "(channel, protocolFactory) {}" << endl;
+ f_header_ <<
+ indent() << " " << extends << style << client_suffix <<
+ "(channel, protocolFactory) {}" << endl;
}
}
if (style == "Cob") {
- f_header_ << indent()
- << "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> getChannel() {" << endl
- << indent() << " return " << _this << "channel_;" << endl << indent() << "}" << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> getChannel() {" << endl <<
+ indent() << " return " << _this << "channel_;" << endl <<
+ indent() << "}" << endl;
if (!gen_no_client_completion_) {
- f_header_ << indent() << "virtual void completed__(bool /* success */) {}" << endl;
+ f_header_ <<
+ indent() << "virtual void completed__(bool /* success */) {}" << endl;
}
}
@@ -2289,47 +2644,51 @@
indent(f_header_) << function_signature(*f_iter, ifstyle) << ";" << endl;
// TODO(dreiss): Use private inheritance to avoid generating thise in cob-style.
t_function send_function(g_type_void,
- string("send_") + (*f_iter)->get_name(),
- (*f_iter)->get_arglist());
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
indent(f_header_) << function_signature(&send_function, "") << ";" << endl;
if (!(*f_iter)->is_oneway()) {
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
- string("recv_") + (*f_iter)->get_name(),
- &noargs);
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs);
indent(f_header_) << function_signature(&recv_function, "") << ";" << endl;
}
}
indent_down();
if (extends.empty()) {
- f_header_ << " protected:" << endl;
+ f_header_ <<
+ " protected:" << endl;
indent_up();
if (style == "Cob") {
- f_header_ << indent()
- << "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel_;" << endl
- << indent()
- << "boost::shared_ptr< ::apache::thrift::transport::TMemoryBuffer> itrans_;" << endl
- << indent()
- << "boost::shared_ptr< ::apache::thrift::transport::TMemoryBuffer> otrans_;"
- << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel_;" << endl <<
+ indent() << "boost::shared_ptr< ::apache::thrift::transport::TMemoryBuffer> itrans_;" << endl <<
+ indent() << "boost::shared_ptr< ::apache::thrift::transport::TMemoryBuffer> otrans_;" << endl;
}
- f_header_ << indent() << prot_ptr << " piprot_;" << endl << indent() << prot_ptr << " poprot_;"
- << endl << indent() << protocol_type << "* iprot_;" << endl << indent()
- << protocol_type << "* oprot_;" << endl;
+ f_header_ <<
+ indent() << prot_ptr << " piprot_;" << endl <<
+ indent() << prot_ptr << " poprot_;" << endl <<
+ indent() << protocol_type << "* iprot_;" << endl <<
+ indent() << protocol_type << "* oprot_;" << endl;
indent_down();
}
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl <<
+ endl;
if (gen_templates_) {
// Output a backwards compatibility typedef using
// TProtocol as the template parameter.
- f_header_ << "typedef " << service_name_ << style
- << "ClientT< ::apache::thrift::protocol::TProtocol> " << service_name_ << style
- << "Client;" << endl << endl;
+ f_header_ <<
+ "typedef " << service_name_ << style <<
+ "ClientT< ::apache::thrift::protocol::TProtocol> " <<
+ service_name_ << style << "Client;" << endl <<
+ endl;
}
string scope = service_name_ + style + client_suffix + "::";
@@ -2342,9 +2701,11 @@
if (gen_templates_) {
indent(out) << template_header;
}
- indent(out) << function_signature(*f_iter, ifstyle, scope) << endl;
+ indent(out) <<
+ function_signature(*f_iter, ifstyle, scope) << endl;
scope_up(out);
- indent(out) << "send_" << funname << "(";
+ indent(out) <<
+ "send_" << funname << "(";
// Get the struct of function call params
t_struct* arg_struct = (*f_iter)->get_arglist();
@@ -2373,23 +2734,26 @@
out << "return recv_" << funname << "();" << endl;
}
} else {
- out << "recv_" << funname << "();" << endl;
+ out <<
+ "recv_" << funname << "();" << endl;
}
}
} else {
if (!(*f_iter)->is_oneway()) {
- out << indent() << _this << "channel_->sendAndRecvMessage("
- << "tcxx::bind(cob, this), " << _this << "otrans_.get(), " << _this << "itrans_.get());"
- << endl;
+ out <<
+ indent() << _this << "channel_->sendAndRecvMessage(" <<
+ "tcxx::bind(cob, this), " << _this << "otrans_.get(), " <<
+ _this << "itrans_.get());" << endl;
} else {
- out << indent() << _this << "channel_->sendMessage("
- << "tcxx::bind(cob, this), " << _this << "otrans_.get());" << endl;
+ out <<
+ indent() << _this << "channel_->sendMessage(" <<
+ "tcxx::bind(cob, this), " << _this << "otrans_.get());" << endl;
}
}
scope_down(out);
out << endl;
- // if (style != "Cob") // TODO(dreiss): Libify the client and don't generate this for cob-style
+ //if (style != "Cob") // TODO(dreiss): Libify the client and don't generate this for cob-style
if (true) {
// Function for sending
t_function send_function(g_type_void,
@@ -2400,7 +2764,8 @@
if (gen_templates_) {
indent(out) << template_header;
}
- indent(out) << function_signature(&send_function, "", scope) << endl;
+ indent(out) <<
+ function_signature(&send_function, "", scope) << endl;
scope_up(out);
// Function arguments and results
@@ -2408,20 +2773,27 @@
string resultname = tservice->get_name() + "_" + (*f_iter)->get_name() + "_presult";
// Serialize the request
- out << indent() << "int32_t cseqid = 0;" << endl << indent() << _this
- << "oprot_->writeMessageBegin(\"" << (*f_iter)->get_name()
- << "\", ::apache::thrift::protocol::" << ((*f_iter)->is_oneway() ? "T_ONEWAY" : "T_CALL")
- << ", cseqid);" << endl << endl << indent() << argsname << " args;" << endl;
+ out <<
+ indent() << "int32_t cseqid = 0;" << endl <<
+ indent() << _this << "oprot_->writeMessageBegin(\"" <<
+ (*f_iter)->get_name() <<
+ "\", ::apache::thrift::protocol::" <<
+ ((*f_iter)->is_oneway() ? "T_ONEWAY" : "T_CALL") <<
+ ", cseqid);" << endl <<
+ endl <<
+ indent() << argsname << " args;" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- out << indent() << "args." << (*fld_iter)->get_name() << " = &" << (*fld_iter)->get_name()
- << ";" << endl;
+ out <<
+ indent() << "args." << (*fld_iter)->get_name() << " = &" << (*fld_iter)->get_name() << ";" << endl;
}
- out << indent() << "args.write(" << _this << "oprot_);" << endl << endl << indent() << _this
- << "oprot_->writeMessageEnd();" << endl << indent() << _this
- << "oprot_->getTransport()->writeEnd();" << endl << indent() << _this
- << "oprot_->getTransport()->flush();" << endl;
+ out <<
+ indent() << "args.write(" << _this << "oprot_);" << endl <<
+ endl <<
+ indent() << _this << "oprot_->writeMessageEnd();" << endl <<
+ indent() << _this << "oprot_->getTransport()->writeEnd();" << endl <<
+ indent() << _this << "oprot_->getTransport()->flush();" << endl;
scope_down(out);
out << endl;
@@ -2436,80 +2808,110 @@
if (gen_templates_) {
indent(out) << template_header;
}
- indent(out) << function_signature(&recv_function, "", scope) << endl;
+ indent(out) <<
+ function_signature(&recv_function, "", scope) << endl;
scope_up(out);
- out << endl << indent() << "int32_t rseqid = 0;" << endl << indent() << "std::string fname;"
- << endl << indent() << "::apache::thrift::protocol::TMessageType mtype;" << endl;
+ out <<
+ endl <<
+ indent() << "int32_t rseqid = 0;" << endl <<
+ indent() << "std::string fname;" << endl <<
+ indent() << "::apache::thrift::protocol::TMessageType mtype;" << endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << "bool completed = false;" << endl << endl << indent() << "try {";
+ out <<
+ indent() << "bool completed = false;" << endl << endl <<
+ indent() << "try {";
indent_up();
}
- out << endl << indent() << _this << "iprot_->readMessageBegin(fname, mtype, rseqid);"
- << endl << indent() << "if (mtype == ::apache::thrift::protocol::T_EXCEPTION) {" << endl
- << indent() << " ::apache::thrift::TApplicationException x;" << endl << indent()
- << " x.read(" << _this << "iprot_);" << endl << indent() << " " << _this
- << "iprot_->readMessageEnd();" << endl << indent() << " " << _this
- << "iprot_->getTransport()->readEnd();" << endl;
+ out << endl <<
+ indent() << _this << "iprot_->readMessageBegin(fname, mtype, rseqid);" << endl <<
+ indent() << "if (mtype == ::apache::thrift::protocol::T_EXCEPTION) {" << endl <<
+ indent() << " ::apache::thrift::TApplicationException x;" << endl <<
+ indent() << " x.read(" << _this << "iprot_);" << endl <<
+ indent() << " " << _this << "iprot_->readMessageEnd();" << endl <<
+ indent() << " " << _this << "iprot_->getTransport()->readEnd();" <<
+ endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(true);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(true);" << endl;
}
- out << indent() << " throw x;" << endl << indent() << "}" << endl << indent()
- << "if (mtype != ::apache::thrift::protocol::T_REPLY) {" << endl << indent() << " "
- << _this << "iprot_->skip("
- << "::apache::thrift::protocol::T_STRUCT);" << endl << indent() << " " << _this
- << "iprot_->readMessageEnd();" << endl << indent() << " " << _this
- << "iprot_->getTransport()->readEnd();" << endl;
+ out <<
+ indent() << " throw x;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (mtype != ::apache::thrift::protocol::T_REPLY) {" << endl <<
+ indent() << " " << _this << "iprot_->skip(" <<
+ "::apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " " << _this << "iprot_->readMessageEnd();" << endl <<
+ indent() << " " << _this << "iprot_->getTransport()->readEnd();" <<
+ endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(false);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(false);" << endl;
}
- out << indent() << "}" << endl << indent() << "if (fname.compare(\""
- << (*f_iter)->get_name() << "\") != 0) {" << endl << indent() << " " << _this
- << "iprot_->skip("
- << "::apache::thrift::protocol::T_STRUCT);" << endl << indent() << " " << _this
- << "iprot_->readMessageEnd();" << endl << indent() << " " << _this
- << "iprot_->getTransport()->readEnd();" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << "if (fname.compare(\"" << (*f_iter)->get_name() << "\") != 0) {" << endl <<
+ indent() << " " << _this << "iprot_->skip(" <<
+ "::apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " " << _this << "iprot_->readMessageEnd();" << endl <<
+ indent() << " " << _this << "iprot_->getTransport()->readEnd();" <<
+ endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(false);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(false);" << endl;
}
- out << indent() << "}" << endl;
+ out <<
+ indent() << "}" << endl;
- if (!(*f_iter)->get_returntype()->is_void()
- && !is_complex_type((*f_iter)->get_returntype())) {
+ if (!(*f_iter)->get_returntype()->is_void() &&
+ !is_complex_type((*f_iter)->get_returntype())) {
t_field returnfield((*f_iter)->get_returntype(), "_return");
- out << indent() << declare_field(&returnfield) << endl;
+ out <<
+ indent() << declare_field(&returnfield) << endl;
}
- out << indent() << resultname << " result;" << endl;
+ out <<
+ indent() << resultname << " result;" << endl;
if (!(*f_iter)->get_returntype()->is_void()) {
- out << indent() << "result.success = &_return;" << endl;
+ out <<
+ indent() << "result.success = &_return;" << endl;
}
- out << indent() << "result.read(" << _this << "iprot_);" << endl << indent() << _this
- << "iprot_->readMessageEnd();" << endl << indent() << _this
- << "iprot_->getTransport()->readEnd();" << endl << endl;
+ out <<
+ indent() << "result.read(" << _this << "iprot_);" << endl <<
+ indent() << _this << "iprot_->readMessageEnd();" << endl <<
+ indent() << _this << "iprot_->getTransport()->readEnd();" << endl <<
+ endl;
// Careful, only look for _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
if (is_complex_type((*f_iter)->get_returntype())) {
- out << indent() << "if (result.__isset.success) {" << endl << indent()
- << " // _return pointer has now been filled" << endl;
+ out <<
+ indent() << "if (result.__isset.success) {" << endl <<
+ indent() << " // _return pointer has now been filled" << endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(true);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(true);" << endl;
}
- out << indent() << " return;" << endl << indent() << "}" << endl;
+ out <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
} else {
- out << indent() << "if (result.__isset.success) {" << endl;
+ out <<
+ indent() << "if (result.__isset.success) {" << endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(true);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(true);" << endl;
}
- out << indent() << " return _return;" << endl << indent() << "}" << endl;
+ out <<
+ indent() << " return _return;" << endl <<
+ indent() << "}" << endl;
}
}
@@ -2517,37 +2919,45 @@
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- out << indent() << "if (result.__isset." << (*x_iter)->get_name() << ") {" << endl;
+ out <<
+ indent() << "if (result.__isset." << (*x_iter)->get_name() << ") {" << endl;
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << " completed = true;" << endl << indent() << " completed__(true);"
- << endl;
+ out <<
+ indent() << " completed = true;" << endl <<
+ indent() << " completed__(true);" << endl;
}
- out << indent() << " throw result." << (*x_iter)->get_name() << ";" << endl << indent()
- << "}" << endl;
+ out <<
+ indent() << " throw result." << (*x_iter)->get_name() << ";" << endl <<
+ indent() << "}" << endl;
}
// We only get here if we are a void function
if ((*f_iter)->get_returntype()->is_void()) {
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << "completed = true;" << endl << indent() << "completed__(true);"
- << endl;
+ out <<
+ indent() << "completed = true;" << endl <<
+ indent() << "completed__(true);" << endl;
}
- indent(out) << "return;" << endl;
+ indent(out) <<
+ "return;" << endl;
} else {
if (style == "Cob" && !gen_no_client_completion_) {
- out << indent() << "completed = true;" << endl << indent() << "completed__(true);"
- << endl;
+ out <<
+ indent() << "completed = true;" << endl <<
+ indent() << "completed__(true);" << endl;
}
- out << indent() << "throw "
- "::apache::thrift::TApplicationException(::apache::thrift::"
- "TApplicationException::MISSING_RESULT, \"" << (*f_iter)->get_name()
- << " failed: unknown result\");" << endl;
+ out <<
+ indent() << "throw ::apache::thrift::TApplicationException(::apache::thrift::TApplicationException::MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
if (style == "Cob" && !gen_no_client_completion_) {
indent_down();
- out << indent() << "} catch (...) {" << endl << indent() << " if (!completed) {" << endl
- << indent() << " completed__(false);" << endl << indent() << " }" << endl
- << indent() << " throw;" << endl << indent() << "}" << endl;
+ out <<
+ indent() << "} catch (...) {" << endl <<
+ indent() << " if (!completed) {" << endl <<
+ indent() << " completed__(false);" << endl <<
+ indent() << " }" << endl <<
+ indent() << " throw;" << endl <<
+ indent() << "}" << endl;
}
// Close function
scope_down(out);
@@ -2558,8 +2968,9 @@
}
class ProcessorGenerator {
-public:
- ProcessorGenerator(t_cpp_generator* generator, t_service* service, const string& style);
+ public:
+ ProcessorGenerator(t_cpp_generator* generator, t_service* service,
+ const string& style);
void run() {
generate_class_definition();
@@ -2581,16 +2992,24 @@
void generate_process_functions();
void generate_factory();
-protected:
- std::string type_name(t_type* ttype, bool in_typedef = false, bool arg = false) {
+ protected:
+ std::string type_name(t_type* ttype, bool in_typedef=false, bool arg=false) {
return generator_->type_name(ttype, in_typedef, arg);
}
- std::string indent() { return generator_->indent(); }
- std::ostream& indent(std::ostream& os) { return generator_->indent(os); }
+ std::string indent() {
+ return generator_->indent();
+ }
+ std::ostream& indent(std::ostream &os) {
+ return generator_->indent(os);
+ }
- void indent_up() { generator_->indent_up(); }
- void indent_down() { generator_->indent_down(); }
+ void indent_up() {
+ generator_->indent_up();
+ }
+ void indent_down() {
+ generator_->indent_down();
+ }
t_cpp_generator* generator_;
t_service* service_;
@@ -2622,7 +3041,8 @@
: generator_(generator),
service_(service),
f_header_(generator->f_header_),
- f_out_(generator->gen_templates_ ? generator->f_service_tcc_ : generator->f_service_),
+ f_out_(generator->gen_templates_ ?
+ generator->f_service_tcc_ : generator->f_service_),
service_name_(generator->service_name_),
style_(style) {
if (style_ == "Cob") {
@@ -2686,120 +3106,150 @@
}
// Generate the header portion
- f_header_ << template_header_ << "class " << class_name_ << " : public " << parent_class << " {"
- << endl;
+ f_header_ <<
+ template_header_ <<
+ "class " << class_name_ << " : public " << parent_class << " {" << endl;
// Protected data members
- f_header_ << " protected:" << endl;
+ f_header_ <<
+ " protected:" << endl;
indent_up();
- f_header_ << indent() << "boost::shared_ptr<" << if_name_ << "> iface_;" << endl;
- f_header_ << indent() << "virtual " << ret_type_ << "dispatchCall(" << finish_cob_
- << "::apache::thrift::protocol::TProtocol* iprot, "
- << "::apache::thrift::protocol::TProtocol* oprot, "
- << "const std::string& fname, int32_t seqid" << call_context_ << ");" << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr<" << if_name_ << "> iface_;" << endl;
+ f_header_ <<
+ indent() << "virtual " << ret_type_ << "dispatchCall(" << finish_cob_ <<
+ "::apache::thrift::protocol::TProtocol* iprot, " <<
+ "::apache::thrift::protocol::TProtocol* oprot, " <<
+ "const std::string& fname, int32_t seqid" << call_context_ << ");" <<
+ endl;
if (generator_->gen_templates_) {
- f_header_ << indent() << "virtual " << ret_type_ << "dispatchCallTemplated(" << finish_cob_
- << "Protocol_* iprot, Protocol_* oprot, "
- << "const std::string& fname, int32_t seqid" << call_context_ << ");" << endl;
+ f_header_ <<
+ indent() << "virtual " << ret_type_ << "dispatchCallTemplated(" <<
+ finish_cob_ << "Protocol_* iprot, Protocol_* oprot, " <<
+ "const std::string& fname, int32_t seqid" << call_context_ << ");" <<
+ endl;
}
indent_down();
// Process function declarations
- f_header_ << " private:" << endl;
+ f_header_ <<
+ " private:" << endl;
indent_up();
// Declare processMap_
- f_header_ << indent() << "typedef void (" << class_name_ << "::*"
- << "ProcessFunction)(" << finish_cob_decl_ << "int32_t, "
- << "::apache::thrift::protocol::TProtocol*, "
- << "::apache::thrift::protocol::TProtocol*" << call_context_decl_ << ");" << endl;
+ f_header_ <<
+ indent() << "typedef void (" << class_name_ << "::*" <<
+ "ProcessFunction)(" << finish_cob_decl_ << "int32_t, " <<
+ "::apache::thrift::protocol::TProtocol*, " <<
+ "::apache::thrift::protocol::TProtocol*" << call_context_decl_ << ");" <<
+ endl;
if (generator_->gen_templates_) {
- f_header_ << indent() << "typedef void (" << class_name_ << "::*"
- << "SpecializedProcessFunction)(" << finish_cob_decl_ << "int32_t, "
- << "Protocol_*, Protocol_*" << call_context_decl_ << ");" << endl << indent()
- << "struct ProcessFunctions {" << endl << indent() << " ProcessFunction generic;"
- << endl << indent() << " SpecializedProcessFunction specialized;" << endl << indent()
- << " ProcessFunctions(ProcessFunction g, "
- << "SpecializedProcessFunction s) :" << endl << indent() << " generic(g)," << endl
- << indent() << " specialized(s) {}" << endl << indent()
- << " ProcessFunctions() : generic(NULL), specialized(NULL) "
- << "{}" << endl << indent() << "};" << endl << indent()
- << "typedef std::map<std::string, ProcessFunctions> "
- << "ProcessMap;" << endl;
+ f_header_ <<
+ indent() << "typedef void (" << class_name_ << "::*" <<
+ "SpecializedProcessFunction)(" << finish_cob_decl_ << "int32_t, " <<
+ "Protocol_*, Protocol_*" << call_context_decl_ << ");" <<
+ endl <<
+ indent() << "struct ProcessFunctions {" << endl <<
+ indent() << " ProcessFunction generic;" << endl <<
+ indent() << " SpecializedProcessFunction specialized;" << endl <<
+ indent() << " ProcessFunctions(ProcessFunction g, " <<
+ "SpecializedProcessFunction s) :" << endl <<
+ indent() << " generic(g)," << endl <<
+ indent() << " specialized(s) {}" << endl <<
+ indent() << " ProcessFunctions() : generic(NULL), specialized(NULL) " <<
+ "{}" << endl <<
+ indent() << "};" << endl <<
+ indent() << "typedef std::map<std::string, ProcessFunctions> " <<
+ "ProcessMap;" << endl;
} else {
- f_header_ << indent() << "typedef std::map<std::string, ProcessFunction> "
- << "ProcessMap;" << endl;
+ f_header_ <<
+ indent() << "typedef std::map<std::string, ProcessFunction> " <<
+ "ProcessMap;" << endl;
}
- f_header_ << indent() << "ProcessMap processMap_;" << endl;
+ f_header_ <<
+ indent() << "ProcessMap processMap_;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent(f_header_) << "void process_" << (*f_iter)->get_name() << "(" << finish_cob_
- << "int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, "
- "::apache::thrift::protocol::TProtocol* oprot" << call_context_ << ");"
- << endl;
+ indent(f_header_) <<
+ "void process_" << (*f_iter)->get_name() << "(" << finish_cob_ << "int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot" << call_context_ << ");" << endl;
if (generator_->gen_templates_) {
- indent(f_header_) << "void process_" << (*f_iter)->get_name() << "(" << finish_cob_
- << "int32_t seqid, Protocol_* iprot, Protocol_* oprot" << call_context_
- << ");" << endl;
+ indent(f_header_) <<
+ "void process_" << (*f_iter)->get_name() << "(" << finish_cob_ <<
+ "int32_t seqid, Protocol_* iprot, Protocol_* oprot" <<
+ call_context_ << ");" << endl;
}
if (style_ == "Cob") {
// XXX Factor this out, even if it is a pain.
string ret_arg = ((*f_iter)->get_returntype()->is_void()
- ? ""
- : ", const " + type_name((*f_iter)->get_returntype()) + "& _return");
- f_header_ << indent() << "void return_" << (*f_iter)->get_name()
- << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << "::apache::thrift::protocol::TProtocol* oprot, "
- << "void* ctx" << ret_arg << ");" << endl;
+ ? ""
+ : ", const " + type_name((*f_iter)->get_returntype()) + "& _return");
+ f_header_ <<
+ indent() << "void return_" << (*f_iter)->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ "::apache::thrift::protocol::TProtocol* oprot, " <<
+ "void* ctx" << ret_arg << ");" << endl;
if (generator_->gen_templates_) {
- f_header_ << indent() << "void return_" << (*f_iter)->get_name()
- << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << "Protocol_* oprot, void* ctx" << ret_arg << ");" << endl;
+ f_header_ <<
+ indent() << "void return_" << (*f_iter)->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ "Protocol_* oprot, void* ctx" << ret_arg << ");" << endl;
}
// XXX Don't declare throw if it doesn't exist
- f_header_ << indent() << "void throw_" << (*f_iter)->get_name()
- << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << "::apache::thrift::protocol::TProtocol* oprot, void* ctx, "
- << "::apache::thrift::TDelayedException* _throw);" << endl;
+ f_header_ <<
+ indent() << "void throw_" << (*f_iter)->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ "::apache::thrift::protocol::TProtocol* oprot, void* ctx, " <<
+ "::apache::thrift::TDelayedException* _throw);" << endl;
if (generator_->gen_templates_) {
- f_header_ << indent() << "void throw_" << (*f_iter)->get_name()
- << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << "Protocol_* oprot, void* ctx, "
- << "::apache::thrift::TDelayedException* _throw);" << endl;
+ f_header_ <<
+ indent() << "void throw_" << (*f_iter)->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ "Protocol_* oprot, void* ctx, " <<
+ "::apache::thrift::TDelayedException* _throw);" << endl;
}
}
}
- f_header_ << " public:" << endl << indent() << class_name_ << "(boost::shared_ptr<" << if_name_
- << "> iface) :" << endl;
+ f_header_ <<
+ " public:" << endl <<
+ indent() << class_name_ <<
+ "(boost::shared_ptr<" << if_name_ << "> iface) :" << endl;
if (!extends_.empty()) {
- f_header_ << indent() << " " << extends_ << "(iface)," << endl;
+ f_header_ <<
+ indent() << " " << extends_ << "(iface)," << endl;
}
- f_header_ << indent() << " iface_(iface) {" << endl;
+ f_header_ <<
+ indent() << " iface_(iface) {" << endl;
indent_up();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_header_ << indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = ";
+ f_header_ <<
+ indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = ";
if (generator_->gen_templates_) {
f_header_ << "ProcessFunctions(" << endl;
if (generator_->gen_templates_only_) {
indent(f_header_) << " NULL," << endl;
} else {
- indent(f_header_) << " &" << class_name_ << "::process_" << (*f_iter)->get_name() << ","
- << endl;
+ indent(f_header_) << " &" << class_name_ << "::process_" <<
+ (*f_iter)->get_name() << "," << endl;
}
- indent(f_header_) << " &" << class_name_ << "::process_" << (*f_iter)->get_name() << ")";
+ indent(f_header_) << " &" << class_name_ << "::process_" <<
+ (*f_iter)->get_name() << ")";
} else {
f_header_ << "&" << class_name_ << "::process_" << (*f_iter)->get_name();
}
- f_header_ << ";" << endl;
+ f_header_ <<
+ ";" << endl;
}
indent_down();
- f_header_ << indent() << "}" << endl << endl << indent() << "virtual ~" << class_name_ << "() {}"
- << endl;
+ f_header_ <<
+ indent() << "}" << endl <<
+ endl <<
+ indent() << "virtual ~" << class_name_ << "() {}" << endl;
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
if (generator_->gen_templates_) {
// Generate a backwards compatible typedef, for callers who don't know
@@ -2811,8 +3261,10 @@
// results in a compile error if instantiated with Protocol_ == TProtocol.
// Therefore, we define TDummyProtocol solely so we can use it as the
// template parameter here.
- f_header_ << "typedef " << class_name_ << "< ::apache::thrift::protocol::TDummyProtocol > "
- << service_name_ << pstyle_ << "Processor;" << endl << endl;
+ f_header_ <<
+ "typedef " << class_name_ <<
+ "< ::apache::thrift::protocol::TDummyProtocol > " <<
+ service_name_ << pstyle_ << "Processor;" << endl << endl;
}
}
@@ -2831,59 +3283,74 @@
function_suffix = "Templated";
}
- f_out_ << template_header_ << ret_type_ << class_name_ << template_suffix_ << "::dispatchCall"
- << function_suffix << "(" << finish_cob_ << protocol << "* iprot, " << protocol
- << "* oprot, "
- << "const std::string& fname, int32_t seqid" << call_context_ << ") {" << endl;
+ f_out_ <<
+ template_header_ <<
+ ret_type_ << class_name_ <<
+ template_suffix_ << "::dispatchCall" << function_suffix << "(" <<
+ finish_cob_ << protocol << "* iprot, " << protocol << "* oprot, " <<
+ "const std::string& fname, int32_t seqid" << call_context_ << ") {" <<
+ endl;
indent_up();
// HOT: member function pointer map
- f_out_ << indent() << typename_str_ << "ProcessMap::iterator pfn;" << endl << indent()
- << "pfn = processMap_.find(fname);" << endl << indent()
- << "if (pfn == processMap_.end()) {" << endl;
+ f_out_ <<
+ indent() << typename_str_ << "ProcessMap::iterator pfn;" << endl <<
+ indent() << "pfn = processMap_.find(fname);" << endl <<
+ indent() << "if (pfn == processMap_.end()) {" << endl;
if (extends_.empty()) {
- f_out_ << indent() << " iprot->skip(::apache::thrift::protocol::T_STRUCT);" << endl << indent()
- << " iprot->readMessageEnd();" << endl << indent()
- << " iprot->getTransport()->readEnd();" << endl << indent()
- << " ::apache::thrift::TApplicationException "
- "x(::apache::thrift::TApplicationException::UNKNOWN_METHOD, \"Invalid method name: "
- "'\"+fname+\"'\");" << endl << indent()
- << " oprot->writeMessageBegin(fname, ::apache::thrift::protocol::T_EXCEPTION, seqid);"
- << endl << indent() << " x.write(oprot);" << endl << indent()
- << " oprot->writeMessageEnd();" << endl << indent()
- << " oprot->getTransport()->writeEnd();" << endl << indent()
- << " oprot->getTransport()->flush();" << endl << indent()
- << (style_ == "Cob" ? " return cob(true);" : " return true;") << endl;
+ f_out_ <<
+ indent() << " iprot->skip(::apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " iprot->readMessageEnd();" << endl <<
+ indent() << " iprot->getTransport()->readEnd();" << endl <<
+ indent() << " ::apache::thrift::TApplicationException x(::apache::thrift::TApplicationException::UNKNOWN_METHOD, \"Invalid method name: '\"+fname+\"'\");" << endl <<
+ indent() << " oprot->writeMessageBegin(fname, ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ indent() << " x.write(oprot);" << endl <<
+ indent() << " oprot->writeMessageEnd();" << endl <<
+ indent() << " oprot->getTransport()->writeEnd();" << endl <<
+ indent() << " oprot->getTransport()->flush();" << endl <<
+ indent() << (style_ == "Cob" ? " return cob(true);" : " return true;") << endl;
} else {
- f_out_ << indent() << " return " << extends_ << "::dispatchCall("
- << (style_ == "Cob" ? "cob, " : "") << "iprot, oprot, fname, seqid" << call_context_arg_
- << ");" << endl;
+ f_out_ <<
+ indent() << " return "
+ << extends_ << "::dispatchCall("
+ << (style_ == "Cob" ? "cob, " : "")
+ << "iprot, oprot, fname, seqid" << call_context_arg_ << ");" << endl;
}
- f_out_ << indent() << "}" << endl;
+ f_out_ <<
+ indent() << "}" << endl;
if (template_protocol) {
- f_out_ << indent() << "(this->*(pfn->second.specialized))";
+ f_out_ <<
+ indent() << "(this->*(pfn->second.specialized))";
} else {
if (generator_->gen_templates_only_) {
// TODO: This is a null pointer, so nothing good will come from calling
// it. Throw an exception instead.
- f_out_ << indent() << "(this->*(pfn->second.generic))";
+ f_out_ <<
+ indent() << "(this->*(pfn->second.generic))";
} else if (generator_->gen_templates_) {
- f_out_ << indent() << "(this->*(pfn->second.generic))";
+ f_out_ <<
+ indent() << "(this->*(pfn->second.generic))";
} else {
- f_out_ << indent() << "(this->*(pfn->second))";
+ f_out_ <<
+ indent() << "(this->*(pfn->second))";
}
}
- f_out_ << "(" << cob_arg_ << "seqid, iprot, oprot" << call_context_arg_ << ");" << endl;
+ f_out_ << "(" << cob_arg_ << "seqid, iprot, oprot" <<
+ call_context_arg_ << ");" << endl;
// TODO(dreiss): return pfn ret?
if (style_ == "Cob") {
- f_out_ << indent() << "return;" << endl;
+ f_out_ <<
+ indent() << "return;" << endl;
} else {
- f_out_ << indent() << "return true;" << endl;
+ f_out_ <<
+ indent() << "return true;" << endl;
}
indent_down();
- f_out_ << "}" << endl << endl;
+ f_out_ <<
+ "}" << endl <<
+ endl;
}
void ProcessorGenerator::generate_process_functions() {
@@ -2903,50 +3370,67 @@
string if_factory_name = if_name_ + "Factory";
// Generate the factory class definition
- f_header_ << template_header_ << "class " << factory_class_name_ << " : public ::apache::thrift::"
- << (style_ == "Cob" ? "async::TAsyncProcessorFactory" : "TProcessorFactory") << " {"
- << endl << " public:" << endl;
+ f_header_ <<
+ template_header_ <<
+ "class " << factory_class_name_ <<
+ " : public ::apache::thrift::" <<
+ (style_ == "Cob" ? "async::TAsyncProcessorFactory" : "TProcessorFactory") <<
+ " {" << endl <<
+ " public:" << endl;
indent_up();
- f_header_ << indent() << factory_class_name_ << "(const ::boost::shared_ptr< " << if_factory_name
- << " >& handlerFactory) :" << endl << indent()
- << " handlerFactory_(handlerFactory) {}" << endl << endl << indent()
- << "::boost::shared_ptr< ::apache::thrift::"
- << (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > "
- << "getProcessor(const ::apache::thrift::TConnectionInfo& connInfo);" << endl;
+ f_header_ <<
+ indent() << factory_class_name_ << "(const ::boost::shared_ptr< " <<
+ if_factory_name << " >& handlerFactory) :" << endl <<
+ indent() << " handlerFactory_(handlerFactory) {}" << endl <<
+ endl <<
+ indent() << "::boost::shared_ptr< ::apache::thrift::" <<
+ (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > " <<
+ "getProcessor(const ::apache::thrift::TConnectionInfo& connInfo);" <<
+ endl;
- f_header_ << endl << " protected:" << endl << indent() << "::boost::shared_ptr< "
- << if_factory_name << " > handlerFactory_;" << endl;
+ f_header_ <<
+ endl <<
+ " protected:" << endl <<
+ indent() << "::boost::shared_ptr< " << if_factory_name <<
+ " > handlerFactory_;" << endl;
indent_down();
- f_header_ << "};" << endl << endl;
+ f_header_ <<
+ "};" << endl << endl;
// If we are generating templates, output a typedef for the plain
// factory name.
if (generator_->gen_templates_) {
- f_header_ << "typedef " << factory_class_name_
- << "< ::apache::thrift::protocol::TDummyProtocol > " << service_name_ << pstyle_
- << "ProcessorFactory;" << endl << endl;
+ f_header_ <<
+ "typedef " << factory_class_name_ <<
+ "< ::apache::thrift::protocol::TDummyProtocol > " <<
+ service_name_ << pstyle_ << "ProcessorFactory;" << endl << endl;
}
// Generate the getProcessor() method
- f_out_ << template_header_ << indent() << "::boost::shared_ptr< ::apache::thrift::"
- << (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > "
- << factory_class_name_ << template_suffix_ << "::getProcessor("
- << "const ::apache::thrift::TConnectionInfo& connInfo) {" << endl;
+ f_out_ <<
+ template_header_ <<
+ indent() << "::boost::shared_ptr< ::apache::thrift::" <<
+ (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > " <<
+ factory_class_name_ << template_suffix_ << "::getProcessor(" <<
+ "const ::apache::thrift::TConnectionInfo& connInfo) {" << endl;
indent_up();
- f_out_ << indent() << "::apache::thrift::ReleaseHandler< " << if_factory_name
- << " > cleanup(handlerFactory_);" << endl << indent() << "::boost::shared_ptr< "
- << if_name_ << " > handler("
- << "handlerFactory_->getHandler(connInfo), cleanup);" << endl << indent()
- << "::boost::shared_ptr< ::apache::thrift::"
- << (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > "
- << "processor(new " << class_name_ << template_suffix_ << "(handler));" << endl << indent()
- << "return processor;" << endl;
+ f_out_ <<
+ indent() << "::apache::thrift::ReleaseHandler< " << if_factory_name <<
+ " > cleanup(handlerFactory_);" << endl <<
+ indent() << "::boost::shared_ptr< " << if_name_ << " > handler(" <<
+ "handlerFactory_->getHandler(connInfo), cleanup);" << endl <<
+ indent() << "::boost::shared_ptr< ::apache::thrift::" <<
+ (style_ == "Cob" ? "async::TAsyncProcessor" : "TProcessor") << " > " <<
+ "processor(new " << class_name_ << template_suffix_ <<
+ "(handler));" << endl <<
+ indent() << "return processor;" << endl;
indent_down();
- f_out_ << indent() << "}" << endl;
+ f_out_ <<
+ indent() << "}" << endl;
}
/**
@@ -2954,7 +3438,8 @@
*
* @param tservice The service to generate a processor for.
*/
-void t_cpp_generator::generate_service_processor(t_service* tservice, string style) {
+void t_cpp_generator::generate_service_processor(t_service* tservice,
+ string style) {
ProcessorGenerator generator(this, tservice, style);
generator.run();
}
@@ -2964,7 +3449,8 @@
*
* @param tfunction The function
*/
-void t_cpp_generator::generate_function_helpers(t_service* tservice, t_function* tfunction) {
+void t_cpp_generator::generate_function_helpers(t_service* tservice,
+ t_function* tfunction) {
if (tfunction->is_oneway()) {
return;
}
@@ -2996,6 +3482,7 @@
if (gen_cob_style_) {
generate_struct_writer(out, &result, true);
}
+
}
/**
@@ -3014,11 +3501,13 @@
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
- string service_func_name = "\"" + tservice->get_name() + "." + tfunction->get_name() + "\"";
+ string service_func_name = "\"" + tservice->get_name() + "." +
+ tfunction->get_name() + "\"";
std::ofstream& out = (gen_templates_ ? f_service_tcc_ : f_service_);
- string prot_type = (specialized ? "Protocol_" : "::apache::thrift::protocol::TProtocol");
+ string prot_type =
+ (specialized ? "Protocol_" : "::apache::thrift::protocol::TProtocol");
string class_suffix;
if (gen_templates_) {
class_suffix = "T<Protocol_>";
@@ -3028,45 +3517,64 @@
if (style != "Cob") {
// Open function
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl;
}
- const bool unnamed_oprot_seqid = tfunction->is_oneway() && !(gen_templates_ && !specialized);
- out << "void " << tservice->get_name() << "Processor" << class_suffix << "::"
- << "process_" << tfunction->get_name() << "("
- << "int32_t" << (unnamed_oprot_seqid ? ", " : " seqid, ") << prot_type << "* iprot, "
- << prot_type << "*" << (unnamed_oprot_seqid ? ", " : " oprot, ") << "void* callContext)"
- << endl;
+ const bool unnamed_oprot_seqid = tfunction->is_oneway() &&
+ !(gen_templates_ && !specialized);
+ out <<
+ "void " << tservice->get_name() << "Processor" << class_suffix << "::" <<
+ "process_" << tfunction->get_name() << "(" <<
+ "int32_t" << (unnamed_oprot_seqid ? ", " : " seqid, ") <<
+ prot_type << "* iprot, " <<
+ prot_type << "*" << (unnamed_oprot_seqid ? ", " : " oprot, ") <<
+ "void* callContext)" << endl;
scope_up(out);
- string argsname = tservice->get_name() + "_" + tfunction->get_name() + "_args";
- string resultname = tservice->get_name() + "_" + tfunction->get_name() + "_result";
+ string argsname = tservice->get_name() + "_" + tfunction->get_name() +
+ "_args";
+ string resultname = tservice->get_name() + "_" + tfunction->get_name() +
+ "_result";
if (tfunction->is_oneway() && !unnamed_oprot_seqid) {
- out << indent() << "(void) seqid;" << endl << indent() << "(void) oprot;" << endl;
+ out <<
+ indent() << "(void) seqid;" << endl <<
+ indent() << "(void) oprot;" << endl;
}
- out << indent() << "void* ctx = NULL;" << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " ctx = this->eventHandler_->getContext(" << service_func_name << ", callContext);"
- << endl << indent() << "}" << endl << indent()
- << "::apache::thrift::TProcessorContextFreer freer("
- << "this->eventHandler_.get(), ctx, " << service_func_name << ");" << endl << endl
- << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->preRead(ctx, " << service_func_name << ");" << endl << indent()
- << "}" << endl << endl << indent() << argsname << " args;" << endl << indent()
- << "args.read(iprot);" << endl << indent() << "iprot->readMessageEnd();" << endl << indent()
- << "uint32_t bytes = iprot->getTransport()->readEnd();" << endl << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->postRead(ctx, " << service_func_name << ", bytes);" << endl
- << indent() << "}" << endl << endl;
+ out <<
+ indent() << "void* ctx = NULL;" << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " ctx = this->eventHandler_->getContext(" <<
+ service_func_name << ", callContext);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "::apache::thrift::TProcessorContextFreer freer(" <<
+ "this->eventHandler_.get(), ctx, " << service_func_name << ");" <<
+ endl << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->preRead(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << argsname << " args;" << endl <<
+ indent() << "args.read(iprot);" << endl <<
+ indent() << "iprot->readMessageEnd();" << endl <<
+ indent() << "uint32_t bytes = iprot->getTransport()->readEnd();" <<
+ endl << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->postRead(ctx, " <<
+ service_func_name << ", bytes);" << endl <<
+ indent() << "}" << endl <<
+ endl;
// Declare result
if (!tfunction->is_oneway()) {
- out << indent() << resultname << " result;" << endl;
+ out <<
+ indent() << resultname << " result;" << endl;
}
// Try block for functions with exceptions
- out << indent() << "try {" << endl;
+ out <<
+ indent() << "try {" << endl;
indent_up();
// Generate the function call
@@ -3080,7 +3588,8 @@
out << "result.success = iface_->" << tfunction->get_name() << "(";
}
} else {
- out << "iface_->" << tfunction->get_name() << "(";
+ out <<
+ "iface_->" << tfunction->get_name() << "(";
}
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -3094,7 +3603,8 @@
// Set isset on success field
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
- out << indent() << "result.__isset.success = true;" << endl;
+ out <<
+ indent() << "result.__isset.success = true;" << endl;
}
indent_down();
@@ -3102,13 +3612,15 @@
if (!tfunction->is_oneway()) {
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- out << " catch (" << type_name((*x_iter)->get_type()) << " &" << (*x_iter)->get_name()
- << ") {" << endl;
+ out << " catch (" << type_name((*x_iter)->get_type()) << " &" <<
+ (*x_iter)->get_name() << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- out << indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name()
- << ";" << endl << indent() << "result.__isset." << (*x_iter)->get_name() << " = true;"
- << endl;
+ out <<
+ indent() << "result." << (*x_iter)->get_name() << " = " <<
+ (*x_iter)->get_name() << ";" << endl <<
+ indent() << "result.__isset." << (*x_iter)->get_name() <<
+ " = true;" << endl;
indent_down();
out << indent() << "}";
} else {
@@ -3118,23 +3630,29 @@
}
if (!tfunction->is_oneway()) {
- out << " catch (const std::exception& e) {" << endl;
+ out << " catch (const std::exception& e) {" << endl;
} else {
- out << " catch (const std::exception&) {" << endl;
+ out << " catch (const std::exception&) {" << endl;
}
indent_up();
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->handlerError(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->handlerError(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl;
if (!tfunction->is_oneway()) {
- out << endl << indent() << "::apache::thrift::TApplicationException x(e.what());" << endl
- << indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name()
- << "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl << indent()
- << "x.write(oprot);" << endl << indent() << "oprot->writeMessageEnd();" << endl
- << indent() << "oprot->getTransport()->writeEnd();" << endl << indent()
- << "oprot->getTransport()->flush();" << endl;
+ out <<
+ endl <<
+ indent() << "::apache::thrift::TApplicationException x(e.what());" <<
+ endl <<
+ indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "oprot->getTransport()->writeEnd();" << endl <<
+ indent() << "oprot->getTransport()->flush();" << endl;
}
out << indent() << "return;" << endl;
indent_down();
@@ -3142,25 +3660,34 @@
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->asyncComplete(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl << endl << indent() << "return;" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->asyncComplete(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "return;" << endl;
indent_down();
- out << "}" << endl << endl;
+ out << "}" << endl <<
+ endl;
return;
}
// Serialize the result into a struct
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->preWrite(ctx, " << service_func_name << ");" << endl << indent()
- << "}" << endl << endl << indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name()
- << "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl << indent()
- << "result.write(oprot);" << endl << indent() << "oprot->writeMessageEnd();" << endl
- << indent() << "bytes = oprot->getTransport()->writeEnd();" << endl << indent()
- << "oprot->getTransport()->flush();" << endl << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->postWrite(ctx, " << service_func_name << ", bytes);" << endl
- << indent() << "}" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->preWrite(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "bytes = oprot->getTransport()->writeEnd();" << endl <<
+ indent() << "oprot->getTransport()->flush();" << endl << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->postWrite(ctx, " <<
+ service_func_name << ", bytes);" << endl <<
+ indent() << "}" << endl;
// Close function
scope_down(out);
@@ -3172,11 +3699,14 @@
// Processor entry point.
// TODO(edhall) update for callContext when TEventServer is ready
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl;
}
- out << "void " << tservice->get_name() << "AsyncProcessor" << class_suffix << "::process_"
- << tfunction->get_name() << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << prot_type << "* iprot, " << prot_type << "* oprot)" << endl;
+ out <<
+ "void " << tservice->get_name() << "AsyncProcessor" << class_suffix <<
+ "::process_" << tfunction->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ prot_type << "* iprot, " << prot_type << "* oprot)" << endl;
scope_up(out);
// TODO(simpkins): we could try to consoldate this
@@ -3184,100 +3714,132 @@
if (gen_templates_ && !specialized) {
// If these are instances of Protocol_, instead of any old TProtocol,
// use the specialized process function instead.
- out << indent() << "Protocol_* _iprot = dynamic_cast<Protocol_*>(iprot);" << endl << indent()
- << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" << endl << indent()
- << "if (_iprot && _oprot) {" << endl << indent() << " return process_"
- << tfunction->get_name() << "(cob, seqid, _iprot, _oprot);" << endl << indent() << "}"
- << endl << indent() << "T_GENERIC_PROTOCOL(this, iprot, _iprot);" << endl << indent()
- << "T_GENERIC_PROTOCOL(this, oprot, _oprot);" << endl << endl;
+ out <<
+ indent() << "Protocol_* _iprot = dynamic_cast<Protocol_*>(iprot);" <<
+ endl <<
+ indent() << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
+ endl <<
+ indent() << "if (_iprot && _oprot) {" << endl <<
+ indent() << " return process_" << tfunction->get_name() <<
+ "(cob, seqid, _iprot, _oprot);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "T_GENERIC_PROTOCOL(this, iprot, _iprot);" << endl <<
+ indent() << "T_GENERIC_PROTOCOL(this, oprot, _oprot);" << endl << endl;
}
if (tfunction->is_oneway()) {
- out << indent() << "(void) seqid;" << endl << indent() << "(void) oprot;" << endl;
+ out <<
+ indent() << "(void) seqid;" << endl <<
+ indent() << "(void) oprot;" << endl;
}
- out << indent() << tservice->get_name() + "_" + tfunction->get_name() << "_args args;" << endl
- << indent() << "void* ctx = NULL;" << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " ctx = this->eventHandler_->getContext(" << service_func_name << ", NULL);" << endl
- << indent() << "}" << endl << indent() << "::apache::thrift::TProcessorContextFreer freer("
- << "this->eventHandler_.get(), ctx, " << service_func_name << ");" << endl << endl
- << indent() << "try {" << endl;
+ out <<
+ indent() << tservice->get_name() + "_" + tfunction->get_name() <<
+ "_args args;" << endl <<
+ indent() << "void* ctx = NULL;" << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " ctx = this->eventHandler_->getContext(" <<
+ service_func_name << ", NULL);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "::apache::thrift::TProcessorContextFreer freer(" <<
+ "this->eventHandler_.get(), ctx, " << service_func_name << ");" <<
+ endl << endl <<
+ indent() << "try {" << endl;
indent_up();
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->preRead(ctx, " << service_func_name << ");" << endl << indent()
- << "}" << endl << indent() << "args.read(iprot);" << endl << indent()
- << "iprot->readMessageEnd();" << endl << indent()
- << "uint32_t bytes = iprot->getTransport()->readEnd();" << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->postRead(ctx, " << service_func_name << ", bytes);" << endl
- << indent() << "}" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->preRead(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl <<
+ indent() << "args.read(iprot);" << endl <<
+ indent() << "iprot->readMessageEnd();" << endl <<
+ indent() << "uint32_t bytes = iprot->getTransport()->readEnd();" <<
+ endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->postRead(ctx, " <<
+ service_func_name << ", bytes);" << endl <<
+ indent() << "}" << endl;
scope_down(out);
// TODO(dreiss): Handle TExceptions? Expose to server?
- out << indent() << "catch (const std::exception&) {" << endl << indent()
- << " if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->handlerError(ctx, " << service_func_name << ");" << endl
- << indent() << " }" << endl << indent() << " return cob(false);" << endl << indent()
- << "}" << endl;
+ out <<
+ indent() << "catch (const std::exception&) {" << endl <<
+ indent() << " if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->handlerError(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << " }" << endl <<
+ indent() << " return cob(false);" << endl <<
+ indent() << "}" << endl;
if (tfunction->is_oneway()) {
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->asyncComplete(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->asyncComplete(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl;
}
// TODO(dreiss): Figure out a strategy for exceptions in async handlers.
- out << indent() << "freer.unregister();" << endl;
+ out <<
+ indent() << "freer.unregister();" << endl;
if (tfunction->is_oneway()) {
// No return. Just hand off our cob.
// TODO(dreiss): Call the cob immediately?
- out << indent() << "iface_->" << tfunction->get_name() << "("
- << "tcxx::bind(cob, true)" << endl;
- indent_up();
- indent_up();
+ out <<
+ indent() << "iface_->" << tfunction->get_name() << "(" <<
+ "tcxx::bind(cob, true)" << endl;
+ indent_up(); indent_up();
} else {
string ret_arg, ret_placeholder;
if (!tfunction->get_returntype()->is_void()) {
- ret_arg = ", const " + type_name(tfunction->get_returntype()) + "& _return";
+ ret_arg = ", const " + type_name(tfunction->get_returntype()) +
+ "& _return";
ret_placeholder = ", tcxx::placeholders::_1";
}
// When gen_templates_ is true, the return_ and throw_ functions are
// overloaded. We have to declare pointers to them so that the compiler
// can resolve the correct overloaded version.
- out << indent() << "void (" << tservice->get_name() << "AsyncProcessor" << class_suffix
- << "::*return_fn)(tcxx::function<void(bool ok)> "
- << "cob, int32_t seqid, " << prot_type << "* oprot, void* ctx" << ret_arg
- << ") =" << endl;
- out << indent() << " &" << tservice->get_name() << "AsyncProcessor" << class_suffix
- << "::return_" << tfunction->get_name() << ";" << endl;
+ out <<
+ indent() << "void (" << tservice->get_name() << "AsyncProcessor" <<
+ class_suffix << "::*return_fn)(tcxx::function<void(bool ok)> " <<
+ "cob, int32_t seqid, " << prot_type << "* oprot, void* ctx" <<
+ ret_arg << ") =" << endl;
+ out <<
+ indent() << " &" << tservice->get_name() << "AsyncProcessor" <<
+ class_suffix << "::return_" << tfunction->get_name() << ";" << endl;
if (!xceptions.empty()) {
- out << indent() << "void (" << tservice->get_name() << "AsyncProcessor" << class_suffix
- << "::*throw_fn)(tcxx::function<void(bool ok)> "
- << "cob, int32_t seqid, " << prot_type << "* oprot, void* ctx, "
- << "::apache::thrift::TDelayedException* _throw) =" << endl;
- out << indent() << " &" << tservice->get_name() << "AsyncProcessor" << class_suffix
- << "::throw_" << tfunction->get_name() << ";" << endl;
+ out <<
+ indent() << "void (" << tservice->get_name() << "AsyncProcessor" <<
+ class_suffix << "::*throw_fn)(tcxx::function<void(bool ok)> " <<
+ "cob, int32_t seqid, " << prot_type << "* oprot, void* ctx, " <<
+ "::apache::thrift::TDelayedException* _throw) =" << endl;
+ out <<
+ indent() << " &" << tservice->get_name() << "AsyncProcessor" <<
+ class_suffix << "::throw_" << tfunction->get_name() << ";" << endl;
}
- out << indent() << "iface_->" << tfunction->get_name() << "(" << endl;
- indent_up();
- indent_up();
- out << indent() << "tcxx::bind(return_fn, this, cob, seqid, oprot, ctx" << ret_placeholder
- << ")";
+ out <<
+ indent() << "iface_->" << tfunction->get_name() << "(" << endl;
+ indent_up(); indent_up();
+ out <<
+ indent() << "tcxx::bind(return_fn, this, cob, seqid, oprot, ctx" <<
+ ret_placeholder << ")";
if (!xceptions.empty()) {
- out << ',' << endl << indent() << "tcxx::bind(throw_fn, this, cob, seqid, oprot, "
- << "ctx, tcxx::placeholders::_1)";
+ out
+ << ',' << endl <<
+ indent() << "tcxx::bind(throw_fn, this, cob, seqid, oprot, " <<
+ "ctx, tcxx::placeholders::_1)";
}
}
// XXX Whitespace cleanup.
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- out << ',' << endl << indent() << "args." << (*f_iter)->get_name();
+ out
+ << ',' << endl <<
+ indent() << "args." << (*f_iter)->get_name();
}
out << ");" << endl;
- indent_down();
- indent_down();
+ indent_down(); indent_down();
scope_down(out);
out << endl;
@@ -3285,51 +3847,73 @@
if (!tfunction->is_oneway()) {
string ret_arg_decl, ret_arg_name;
if (!tfunction->get_returntype()->is_void()) {
- ret_arg_decl = ", const " + type_name(tfunction->get_returntype()) + "& _return";
+ ret_arg_decl = ", const " + type_name(tfunction->get_returntype()) +
+ "& _return";
ret_arg_name = ", _return";
}
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl;
}
- out << "void " << tservice->get_name() << "AsyncProcessor" << class_suffix << "::return_"
- << tfunction->get_name() << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << prot_type << "* oprot, void* ctx" << ret_arg_decl << ')' << endl;
+ out <<
+ "void " << tservice->get_name() << "AsyncProcessor" << class_suffix <<
+ "::return_" << tfunction->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ prot_type << "* oprot, void* ctx" << ret_arg_decl << ')' << endl;
scope_up(out);
if (gen_templates_ && !specialized) {
// If oprot is a Protocol_ instance,
// use the specialized return function instead.
- out << indent() << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" << endl
- << indent() << "if (_oprot) {" << endl << indent() << " return return_"
- << tfunction->get_name() << "(cob, seqid, _oprot, ctx" << ret_arg_name << ");" << endl
- << indent() << "}" << endl << indent() << "T_GENERIC_PROTOCOL(this, oprot, _oprot);"
- << endl << endl;
+ out <<
+ indent() << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
+ endl <<
+ indent() << "if (_oprot) {" << endl <<
+ indent() << " return return_" << tfunction->get_name() <<
+ "(cob, seqid, _oprot, ctx" << ret_arg_name << ");" << endl <<
+ indent() << "}" << endl <<
+ indent() << "T_GENERIC_PROTOCOL(this, oprot, _oprot);" <<
+ endl << endl;
}
- out << indent() << tservice->get_name() << "_" << tfunction->get_name() << "_presult result;"
- << endl;
+ out <<
+ indent() << tservice->get_name() << "_" << tfunction->get_name() <<
+ "_presult result;" << endl;
if (!tfunction->get_returntype()->is_void()) {
// The const_cast here is unfortunate, but it would be a pain to avoid,
// and we only do a write with this struct, which is const-safe.
- out << indent() << "result.success = const_cast<" << type_name(tfunction->get_returntype())
- << "*>(&_return);" << endl << indent() << "result.__isset.success = true;" << endl;
+ out <<
+ indent() << "result.success = const_cast<" <<
+ type_name(tfunction->get_returntype()) << "*>(&_return);" <<
+ endl <<
+ indent() << "result.__isset.success = true;" << endl;
}
// Serialize the result into a struct
- out << endl << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " ctx = this->eventHandler_->getContext(" << service_func_name << ", NULL);" << endl
- << indent() << "}" << endl << indent()
- << "::apache::thrift::TProcessorContextFreer freer("
- << "this->eventHandler_.get(), ctx, " << service_func_name << ");" << endl << endl
- << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->preWrite(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl << endl << indent() << "oprot->writeMessageBegin(\""
- << tfunction->get_name() << "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl
- << indent() << "result.write(oprot);" << endl << indent() << "oprot->writeMessageEnd();"
- << endl << indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" << endl
- << indent() << "oprot->getTransport()->flush();" << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->postWrite(ctx, " << service_func_name << ", bytes);" << endl
- << indent() << "}" << endl << indent() << "return cob(true);" << endl;
+ out <<
+ endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " ctx = this->eventHandler_->getContext(" <<
+ service_func_name << ", NULL);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "::apache::thrift::TProcessorContextFreer freer(" <<
+ "this->eventHandler_.get(), ctx, " << service_func_name <<
+ ");" << endl << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->preWrite(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" <<
+ endl <<
+ indent() << "oprot->getTransport()->flush();" << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->postWrite(ctx, " <<
+ service_func_name << ", bytes);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "return cob(true);" << endl;
scope_down(out);
out << endl;
}
@@ -3337,82 +3921,111 @@
// Exception return.
if (!tfunction->is_oneway() && !xceptions.empty()) {
if (gen_templates_) {
- out << indent() << "template <class Protocol_>" << endl;
+ out <<
+ indent() << "template <class Protocol_>" << endl;
}
- out << "void " << tservice->get_name() << "AsyncProcessor" << class_suffix << "::throw_"
- << tfunction->get_name() << "(tcxx::function<void(bool ok)> cob, int32_t seqid, "
- << prot_type << "* oprot, void* ctx, "
- << "::apache::thrift::TDelayedException* _throw)" << endl;
+ out <<
+ "void " << tservice->get_name() << "AsyncProcessor" << class_suffix <<
+ "::throw_" << tfunction->get_name() <<
+ "(tcxx::function<void(bool ok)> cob, int32_t seqid, " <<
+ prot_type << "* oprot, void* ctx, " <<
+ "::apache::thrift::TDelayedException* _throw)" << endl;
scope_up(out);
if (gen_templates_ && !specialized) {
// If oprot is a Protocol_ instance,
// use the specialized throw function instead.
- out << indent() << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" << endl
- << indent() << "if (_oprot) {" << endl << indent() << " return throw_"
- << tfunction->get_name() << "(cob, seqid, _oprot, ctx, _throw);" << endl << indent()
- << "}" << endl << indent() << "T_GENERIC_PROTOCOL(this, oprot, _oprot);" << endl
- << endl;
+ out <<
+ indent() << "Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
+ endl <<
+ indent() << "if (_oprot) {" << endl <<
+ indent() << " return throw_" << tfunction->get_name() <<
+ "(cob, seqid, _oprot, ctx, _throw);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "T_GENERIC_PROTOCOL(this, oprot, _oprot);" <<
+ endl << endl;
}
// Get the event handler context
- out << endl << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " ctx = this->eventHandler_->getContext(" << service_func_name << ", NULL);" << endl
- << indent() << "}" << endl << indent()
- << "::apache::thrift::TProcessorContextFreer freer("
- << "this->eventHandler_.get(), ctx, " << service_func_name << ");" << endl << endl;
+ out <<
+ endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " ctx = this->eventHandler_->getContext(" <<
+ service_func_name << ", NULL);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "::apache::thrift::TProcessorContextFreer freer(" <<
+ "this->eventHandler_.get(), ctx, " << service_func_name << ");" <<
+ endl << endl;
// Throw the TDelayedException, and catch the result
- out << indent() << tservice->get_name() << "_" << tfunction->get_name() << "_result result;"
- << endl << endl << indent() << "try {" << endl;
+ out <<
+ indent() << tservice->get_name() << "_" << tfunction->get_name() <<
+ "_result result;" << endl << endl <<
+ indent() << "try {" << endl;
indent_up();
- out << indent() << "_throw->throw_it();" << endl << indent() << "return cob(false);"
- << endl; // Is this possible? TBD.
+ out <<
+ indent() << "_throw->throw_it();" << endl <<
+ indent() << "return cob(false);" << endl; // Is this possible? TBD.
indent_down();
- out << indent() << '}';
+ out <<
+ indent() << '}';
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- out << " catch (" << type_name((*x_iter)->get_type()) << " &" << (*x_iter)->get_name()
- << ") {" << endl;
+ out << " catch (" << type_name((*x_iter)->get_type()) << " &" <<
+ (*x_iter)->get_name() << ") {" << endl;
indent_up();
- out << indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name()
- << ";" << endl << indent() << "result.__isset." << (*x_iter)->get_name() << " = true;"
- << endl;
+ out <<
+ indent() << "result." << (*x_iter)->get_name() << " = " <<
+ (*x_iter)->get_name() << ";" << endl <<
+ indent() << "result.__isset." << (*x_iter)->get_name() <<
+ " = true;" << endl;
scope_down(out);
}
// Handle the case where an undeclared exception is thrown
out << " catch (std::exception& e) {" << endl;
indent_up();
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->handlerError(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl << endl << indent()
- << "::apache::thrift::TApplicationException x(e.what());" << endl << indent()
- << "oprot->writeMessageBegin(\"" << tfunction->get_name()
- << "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl << indent()
- << "x.write(oprot);" << endl << indent() << "oprot->writeMessageEnd();" << endl
- << indent() << "oprot->getTransport()->writeEnd();" << endl << indent()
- << "oprot->getTransport()->flush();" << endl <<
- // We pass true to the cob here, since we did successfully write a
- // response, even though it is an exception response.
- // It looks like the argument is currently ignored, anyway.
- indent() << "return cob(true);" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->handlerError(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl <<
+ endl <<
+ indent() << "::apache::thrift::TApplicationException x(e.what());" <<
+ endl <<
+ indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "oprot->getTransport()->writeEnd();" << endl <<
+ indent() << "oprot->getTransport()->flush();" << endl <<
+ // We pass true to the cob here, since we did successfully write a
+ // response, even though it is an exception response.
+ // It looks like the argument is currently ignored, anyway.
+ indent() << "return cob(true);" << endl;
scope_down(out);
// Serialize the result into a struct
- out << indent() << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->preWrite(ctx, " << service_func_name << ");" << endl
- << indent() << "}" << endl << endl << indent() << "oprot->writeMessageBegin(\""
- << tfunction->get_name() << "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl
- << indent() << "result.write(oprot);" << endl << indent() << "oprot->writeMessageEnd();"
- << endl << indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" << endl
- << indent() << "oprot->getTransport()->flush();" << endl << indent()
- << "if (this->eventHandler_.get() != NULL) {" << endl << indent()
- << " this->eventHandler_->postWrite(ctx, " << service_func_name << ", bytes);" << endl
- << indent() << "}" << endl << indent() << "return cob(true);" << endl;
+ out <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->preWrite(ctx, " <<
+ service_func_name << ");" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" <<
+ endl <<
+ indent() << "oprot->getTransport()->flush();" << endl <<
+ indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
+ indent() << " this->eventHandler_->postWrite(ctx, " <<
+ service_func_name << ", bytes);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "return cob(true);" << endl;
scope_down(out);
out << endl;
} // for each function
- } // cob style
+ } // cob style
}
/**
@@ -3424,65 +4037,84 @@
string svcname = tservice->get_name();
// Service implementation file includes
- string f_skeleton_name = get_out_dir() + svcname + "_server.skeleton.cpp";
+ string f_skeleton_name = get_out_dir()+svcname+"_server.skeleton.cpp";
string ns = namespace_prefix(tservice->get_program()->get_namespace("cpp"));
ofstream f_skeleton;
f_skeleton.open(f_skeleton_name.c_str());
- f_skeleton << "// This autogenerated skeleton file illustrates how to build a server." << endl
- << "// You should copy it to another filename to avoid overwriting it." << endl << endl
- << "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl
- << "#include <thrift/protocol/TBinaryProtocol.h>" << endl
- << "#include <thrift/server/TSimpleServer.h>" << endl
- << "#include <thrift/transport/TServerSocket.h>" << endl
- << "#include <thrift/transport/TBufferTransports.h>" << endl << endl
- << "using namespace ::apache::thrift;" << endl
- << "using namespace ::apache::thrift::protocol;" << endl
- << "using namespace ::apache::thrift::transport;" << endl
- << "using namespace ::apache::thrift::server;" << endl << endl
- << "using boost::shared_ptr;" << endl << endl;
+ f_skeleton <<
+ "// This autogenerated skeleton file illustrates how to build a server." << endl <<
+ "// You should copy it to another filename to avoid overwriting it." << endl <<
+ endl <<
+ "#include \"" << get_include_prefix(*get_program()) << svcname << ".h\"" << endl <<
+ "#include <thrift/protocol/TBinaryProtocol.h>" << endl <<
+ "#include <thrift/server/TSimpleServer.h>" << endl <<
+ "#include <thrift/transport/TServerSocket.h>" << endl <<
+ "#include <thrift/transport/TBufferTransports.h>" << endl <<
+ endl <<
+ "using namespace ::apache::thrift;" << endl <<
+ "using namespace ::apache::thrift::protocol;" << endl <<
+ "using namespace ::apache::thrift::transport;" << endl <<
+ "using namespace ::apache::thrift::server;" << endl <<
+ endl <<
+ "using boost::shared_ptr;" << endl <<
+ endl;
// the following code would not compile:
// using namespace ;
// using namespace ::;
- if ((!ns.empty()) && (ns.compare(" ::") != 0)) {
- f_skeleton << "using namespace " << string(ns, 0, ns.size() - 2) << ";" << endl << endl;
+ if ( (!ns.empty()) && (ns.compare(" ::") != 0)) {
+ f_skeleton <<
+ "using namespace " << string(ns, 0, ns.size()-2) << ";" << endl <<
+ endl;
}
- f_skeleton << "class " << svcname << "Handler : virtual public " << svcname << "If {" << endl
- << " public:" << endl;
+ f_skeleton <<
+ "class " << svcname << "Handler : virtual public " << svcname << "If {" << endl <<
+ " public:" << endl;
indent_up();
- f_skeleton << indent() << svcname << "Handler() {" << endl << indent()
- << " // Your initialization goes here" << endl << indent() << "}" << endl << endl;
+ f_skeleton <<
+ indent() << svcname << "Handler() {" << endl <<
+ indent() << " // Your initialization goes here" << endl <<
+ indent() << "}" << endl <<
+ endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
generate_java_doc(f_skeleton, *f_iter);
- f_skeleton << indent() << function_signature(*f_iter, "") << " {" << endl << indent()
- << " // Your implementation goes here" << endl << indent() << " printf(\""
- << (*f_iter)->get_name() << "\\n\");" << endl << indent() << "}" << endl << endl;
+ f_skeleton <<
+ indent() << function_signature(*f_iter, "") << " {" << endl <<
+ indent() << " // Your implementation goes here" << endl <<
+ indent() << " printf(\"" << (*f_iter)->get_name() << "\\n\");" << endl <<
+ indent() << "}" << endl <<
+ endl;
}
indent_down();
- f_skeleton << "};" << endl << endl;
+ f_skeleton <<
+ "};" << endl <<
+ endl;
- f_skeleton << indent() << "int main(int argc, char **argv) {" << endl;
+ f_skeleton <<
+ indent() << "int main(int argc, char **argv) {" << endl;
indent_up();
- f_skeleton
- << indent() << "int port = 9090;" << endl << indent() << "shared_ptr<" << svcname
- << "Handler> handler(new " << svcname << "Handler());" << endl << indent()
- << "shared_ptr<TProcessor> processor(new " << svcname << "Processor(handler));" << endl
- << indent() << "shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));"
- << endl << indent()
- << "shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());" << endl
- << indent() << "shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());"
- << endl << endl << indent()
- << "TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);"
- << endl << indent() << "server.serve();" << endl << indent() << "return 0;" << endl;
+ f_skeleton <<
+ indent() << "int port = 9090;" << endl <<
+ indent() << "shared_ptr<" << svcname << "Handler> handler(new " << svcname << "Handler());" << endl <<
+ indent() << "shared_ptr<TProcessor> processor(new " << svcname << "Processor(handler));" << endl <<
+ indent() << "shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));" << endl <<
+ indent() << "shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());" << endl <<
+ indent() << "shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());" << endl <<
+ endl <<
+ indent() << "TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);" << endl <<
+ indent() << "server.serve();" << endl <<
+ indent() << "return 0;" << endl;
indent_down();
- f_skeleton << "}" << endl << endl;
+ f_skeleton <<
+ "}" << endl <<
+ endl;
// Close the files
f_skeleton.close();
@@ -3498,7 +4130,8 @@
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name() + suffix;
@@ -3508,7 +4141,8 @@
} else if (type->is_container()) {
generate_deserialize_container(out, type, name);
} else if (type->is_base_type()) {
- indent(out) << "xfer += iprot->";
+ indent(out) <<
+ "xfer += iprot->";
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
case t_base_type::TYPE_VOID:
@@ -3517,7 +4151,8 @@
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
out << "readBinary(" << name << ");";
- } else {
+ }
+ else {
out << "readString(" << name << ");";
}
break;
@@ -3542,15 +4177,17 @@
default:
throw "compiler error: no C++ reader for base type " + t_base_type::t_base_name(tbase) + name;
}
- out << endl;
+ 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_name(type) << ")" << t << ";" << endl;
+ out <<
+ indent() << "int32_t " << t << ";" << endl <<
+ indent() << "xfer += iprot->readI32(" << t << ");" << endl <<
+ indent() << name << " = (" << type_name(type) << ")" << 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());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
@@ -3566,25 +4203,27 @@
bool pointer) {
if (pointer) {
indent(out) << "if (!" << prefix << ") { " << endl;
- indent(out) << " " << prefix << " = boost::shared_ptr<" << type_name(tstruct) << ">(new "
- << type_name(tstruct) << ");" << endl;
+ indent(out) << " " << prefix << " = boost::shared_ptr<" << type_name(tstruct) << ">(new " << type_name(tstruct) << ");" << endl;
indent(out) << "}" << endl;
- indent(out) << "xfer += " << prefix << "->read(iprot);" << endl;
+ indent(out) <<
+ "xfer += " << prefix << "->read(iprot);" << endl;
indent(out) << "bool wasSet = false;" << endl;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = members.begin(); f_iter != members.end(); ++f_iter) {
- indent(out) << "if (" << prefix << "->__isset." << (*f_iter)->get_name()
- << ") { wasSet = true; }" << endl;
+ indent(out) << "if (" << prefix << "->__isset." << (*f_iter)->get_name() << ") { wasSet = true; }" << endl;
}
indent(out) << "if (!wasSet) { " << prefix << ".reset(); }" << endl;
} else {
- indent(out) << "xfer += " << prefix << ".read(iprot);" << endl;
+ indent(out) <<
+ "xfer += " << prefix << ".read(iprot);" << endl;
}
}
-void t_cpp_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_cpp_generator::generate_deserialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
string size = tmp("_size");
@@ -3595,40 +4234,50 @@
t_container* tcontainer = (t_container*)ttype;
bool use_push = tcontainer->has_cpp_name();
- indent(out) << prefix << ".clear();" << endl << indent() << "uint32_t " << size << ";" << endl;
+ indent(out) <<
+ prefix << ".clear();" << endl <<
+ indent() << "uint32_t " << size << ";" << endl;
// Declare variables, read header
if (ttype->is_map()) {
- out << indent() << "::apache::thrift::protocol::TType " << ktype << ";" << endl << indent()
- << "::apache::thrift::protocol::TType " << vtype << ";" << endl << indent()
- << "xfer += iprot->readMapBegin(" << ktype << ", " << vtype << ", " << size << ");" << endl;
+ out <<
+ indent() << "::apache::thrift::protocol::TType " << ktype << ";" << endl <<
+ indent() << "::apache::thrift::protocol::TType " << vtype << ";" << endl <<
+ indent() << "xfer += iprot->readMapBegin(" <<
+ ktype << ", " << vtype << ", " << size << ");" << endl;
} else if (ttype->is_set()) {
- out << indent() << "::apache::thrift::protocol::TType " << etype << ";" << endl << indent()
- << "xfer += iprot->readSetBegin(" << etype << ", " << size << ");" << endl;
+ out <<
+ indent() << "::apache::thrift::protocol::TType " << etype << ";" << endl <<
+ indent() << "xfer += iprot->readSetBegin(" <<
+ etype << ", " << size << ");" << endl;
} else if (ttype->is_list()) {
- out << indent() << "::apache::thrift::protocol::TType " << etype << ";" << endl << indent()
- << "xfer += iprot->readListBegin(" << etype << ", " << size << ");" << endl;
+ out <<
+ indent() << "::apache::thrift::protocol::TType " << etype << ";" << endl <<
+ indent() << "xfer += iprot->readListBegin(" <<
+ etype << ", " << size << ");" << endl;
if (!use_push) {
indent(out) << prefix << ".resize(" << size << ");" << endl;
}
}
+
// For loop iterates over elements
string i = tmp("_i");
- out << indent() << "uint32_t " << i << ";" << endl << indent() << "for (" << i << " = 0; " << i
- << " < " << size << "; ++" << i << ")" << endl;
+ out <<
+ indent() << "uint32_t " << i << ";" << endl <<
+ indent() << "for (" << i << " = 0; " << i << " < " << size << "; ++" << i << ")" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix, use_push, i);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix, use_push, i);
+ }
- scope_down(out);
+ scope_down(out);
// Read container end
if (ttype->is_map()) {
@@ -3642,33 +4291,42 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
-void t_cpp_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_cpp_generator::generate_deserialize_map_element(ofstream& out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- out << indent() << declare_field(&fkey) << endl;
+ out <<
+ indent() << declare_field(&fkey) << endl;
generate_deserialize_field(out, &fkey);
- indent(out) << declare_field(&fval, false, false, false, true) << " = " << prefix << "[" << key
- << "];" << endl;
+ indent(out) <<
+ declare_field(&fval, false, false, false, true) << " = " <<
+ prefix << "[" << key << "];" << endl;
generate_deserialize_field(out, &fval);
}
-void t_cpp_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_cpp_generator::generate_deserialize_set_element(ofstream& out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".insert(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".insert(" << elem << ");" << endl;
}
void t_cpp_generator::generate_deserialize_list_element(ofstream& out,
@@ -3688,6 +4346,7 @@
}
}
+
/**
* Serializes a field of any type.
*
@@ -3707,24 +4366,32 @@
throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
}
+
+
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, name, is_reference(tfield));
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ name,
+ is_reference(tfield));
} else if (type->is_container()) {
generate_serialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "xfer += oprot->";
+ indent(out) <<
+ "xfer += oprot->";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
out << "writeBinary(" << name << ");";
- } else {
+ }
+ else {
out << "writeString(" << name << ");";
}
break;
@@ -3747,8 +4414,7 @@
out << "writeDouble(" << name << ");";
break;
default:
- throw "compiler error: no C++ writer for base type " + t_base_type::t_base_name(tbase)
- + name;
+ throw "compiler error: no C++ writer for base type " + t_base_type::t_base_name(tbase) + name;
}
} else if (type->is_enum()) {
out << "writeI32((int32_t)" << name << ");";
@@ -3774,53 +4440,63 @@
if (pointer) {
indent(out) << "if (" << prefix << ") {" << endl;
indent(out) << " xfer += " << prefix << "->write(oprot); " << endl;
- indent(out) << "} else {"
- << "oprot->writeStructBegin(\"" << tstruct->get_name() << "\"); " << endl;
+ indent(out) << "} else {" << "oprot->writeStructBegin(\"" <<
+ tstruct->get_name() << "\"); " << endl;
indent(out) << " oprot->writeStructEnd();" << endl;
indent(out) << " oprot->writeFieldStop();" << endl;
indent(out) << "}" << endl;
} else {
- indent(out) << "xfer += " << prefix << ".write(oprot);" << endl;
+ indent(out) <<
+ "xfer += " << prefix << ".write(oprot);" << endl;
}
}
-void t_cpp_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_cpp_generator::generate_serialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
- indent(out) << "xfer += oprot->writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
+ indent(out) <<
+ "xfer += oprot->writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "xfer += oprot->writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", "
- << "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
+ indent(out) <<
+ "xfer += oprot->writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "xfer += oprot->writeListBegin("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", "
- << "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
+ indent(out) <<
+ "xfer += oprot->writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "static_cast<uint32_t>(" << prefix << ".size()));" << endl;
}
string iter = tmp("_iter");
- out << indent() << type_name(ttype) << "::const_iterator " << iter << ";" << endl << indent()
- << "for (" << iter << " = " << prefix << ".begin(); " << iter << " != " << prefix
- << ".end(); ++" << iter << ")" << endl;
+ out <<
+ indent() << type_name(ttype) << "::const_iterator " << iter << ";" << endl <<
+ indent() << "for (" << iter << " = " << prefix << ".begin(); " << iter << " != " << prefix << ".end(); ++" << iter << ")" << endl;
scope_up(out);
- if (ttype->is_map()) {
- generate_serialize_map_element(out, (t_map*)ttype, iter);
- } else if (ttype->is_set()) {
- generate_serialize_set_element(out, (t_set*)ttype, iter);
- } else if (ttype->is_list()) {
- generate_serialize_list_element(out, (t_list*)ttype, iter);
- }
+ if (ttype->is_map()) {
+ generate_serialize_map_element(out, (t_map*)ttype, iter);
+ } else if (ttype->is_set()) {
+ generate_serialize_set_element(out, (t_set*)ttype, iter);
+ } else if (ttype->is_list()) {
+ generate_serialize_list_element(out, (t_list*)ttype, iter);
+ }
scope_down(out);
if (ttype->is_map()) {
- indent(out) << "xfer += oprot->writeMapEnd();" << endl;
+ indent(out) <<
+ "xfer += oprot->writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "xfer += oprot->writeSetEnd();" << endl;
+ indent(out) <<
+ "xfer += oprot->writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "xfer += oprot->writeListEnd();" << endl;
+ indent(out) <<
+ "xfer += oprot->writeListEnd();" << endl;
}
scope_down(out);
@@ -3830,7 +4506,9 @@
* Serializes the members of a map.
*
*/
-void t_cpp_generator::generate_serialize_map_element(ofstream& out, t_map* tmap, string iter) {
+void t_cpp_generator::generate_serialize_map_element(ofstream& out,
+ t_map* tmap,
+ string iter) {
t_field kfield(tmap->get_key_type(), iter + "->first");
generate_serialize_field(out, &kfield, "");
@@ -3841,7 +4519,9 @@
/**
* Serializes the members of a set.
*/
-void t_cpp_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_cpp_generator::generate_serialize_set_element(ofstream& out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), "(*" + iter + ")");
generate_serialize_field(out, &efield, "");
}
@@ -3849,7 +4529,9 @@
/**
* Serializes the members of a list.
*/
-void t_cpp_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_cpp_generator::generate_serialize_list_element(ofstream& out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), "(*" + iter + ")");
generate_serialize_field(out, &efield, "");
}
@@ -3876,7 +4558,7 @@
while ((loc = ns.find(".")) != string::npos) {
result += ns.substr(0, loc);
result += "::";
- ns = ns.substr(loc + 1);
+ ns = ns.substr(loc+1);
}
if (ns.size() > 0) {
result += ns + "::";
@@ -3903,7 +4585,7 @@
result += ns.substr(0, loc);
result += " {";
separator = " ";
- ns = ns.substr(loc + 1);
+ ns = ns.substr(loc+1);
}
if (ns.size() > 0) {
result += separator + "namespace " + ns + " {";
@@ -3925,7 +4607,7 @@
string::size_type loc;
while ((loc = ns.find(".")) != string::npos) {
result += "}";
- ns = ns.substr(loc + 1);
+ ns = ns.substr(loc+1);
}
result += " // namespace";
return result;
@@ -3960,18 +4642,19 @@
if (ttype->is_container()) {
string cname;
- t_container* tcontainer = (t_container*)ttype;
+ t_container* tcontainer = (t_container*) ttype;
if (tcontainer->has_cpp_name()) {
cname = tcontainer->get_cpp_name();
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
- cname = "std::map<" + type_name(tmap->get_key_type(), in_typedef) + ", "
- + type_name(tmap->get_val_type(), in_typedef) + "> ";
+ t_map* tmap = (t_map*) ttype;
+ cname = "std::map<" +
+ type_name(tmap->get_key_type(), in_typedef) + ", " +
+ type_name(tmap->get_val_type(), in_typedef) + "> ";
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
+ t_set* tset = (t_set*) ttype;
cname = "std::set<" + type_name(tset->get_elem_type(), in_typedef) + "> ";
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
+ t_list* tlist = (t_list*) ttype;
cname = "std::vector<" + type_name(tlist->get_elem_type(), in_typedef) + "> ";
}
@@ -3991,7 +4674,10 @@
string pname;
t_program* program = ttype->get_program();
if (program != NULL && program != program_) {
- pname = class_prefix + namespace_prefix(program->get_namespace("cpp")) + ttype->get_name();
+ pname =
+ class_prefix +
+ namespace_prefix(program->get_namespace("cpp")) +
+ ttype->get_name();
} else {
pname = class_prefix + ttype->get_name();
}
@@ -4046,11 +4732,7 @@
* @param ttype The type
* @return Field declaration, i.e. int x = 0;
*/
-string t_cpp_generator::declare_field(t_field* tfield,
- bool init,
- bool pointer,
- bool constant,
- bool reference) {
+string t_cpp_generator::declare_field(t_field* tfield, bool init, bool pointer, bool constant, bool reference) {
// TODO(mcslee): do we ever need to initialize the field?
string result = "";
if (constant) {
@@ -4117,14 +4799,16 @@
if (style == "") {
if (is_complex_type(ttype)) {
- return "void " + prefix + tfunction->get_name() + "(" + type_name(ttype)
- + (name_params ? "& _return" : "& /* _return */")
- + argument_list(arglist, name_params, true) + ")";
+ return
+ "void " + prefix + tfunction->get_name() +
+ "(" + type_name(ttype) + (name_params ? "& _return" : "& /* _return */") +
+ argument_list(arglist, name_params, true) + ")";
} else {
- return type_name(ttype) + " " + prefix + tfunction->get_name() + "("
- + argument_list(arglist, name_params) + ")";
+ return
+ type_name(ttype) + " " + prefix + tfunction->get_name() +
+ "(" + argument_list(arglist, name_params) + ")";
}
- } else if (style.substr(0, 3) == "Cob") {
+ } else if (style.substr(0,3) == "Cob") {
string cob_type;
string exn_cob;
if (style == "CobCl") {
@@ -4133,18 +4817,21 @@
cob_type += "T<Protocol_>";
}
cob_type += "* client)";
- } else if (style == "CobSv") {
- cob_type = (ttype->is_void() ? "()" : ("(" + type_name(ttype) + " const& _return)"));
+ } else if (style =="CobSv") {
+ cob_type = (ttype->is_void()
+ ? "()"
+ : ("(" + type_name(ttype) + " const& _return)"));
if (has_xceptions) {
- exn_cob
- = ", tcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */";
+ exn_cob = ", tcxx::function<void(::apache::thrift::TDelayedException* _throw)> /* exn_cob */";
}
} else {
throw "UNKNOWN STYLE";
}
- return "void " + prefix + tfunction->get_name() + "(tcxx::function<void" + cob_type + "> cob"
- + exn_cob + argument_list(arglist, name_params, true) + ")";
+ return
+ "void " + prefix + tfunction->get_name() +
+ "(tcxx::function<void" + cob_type + "> cob" + exn_cob +
+ argument_list(arglist, name_params, true) + ")";
} else {
throw "UNKNOWN STYLE";
}
@@ -4168,8 +4855,8 @@
} else {
result += ", ";
}
- result += type_name((*f_iter)->get_type(), false, true) + " "
- + (name_params ? (*f_iter)->get_name() : "/* " + (*f_iter)->get_name() + " */");
+ result += type_name((*f_iter)->get_type(), false, true) + " " +
+ (name_params ? (*f_iter)->get_name() : "/* " + (*f_iter)->get_name() + " */");
}
return result;
}
@@ -4254,7 +4941,9 @@
name = ttype->get_ascii_fingerprint();
}
- if (external && ttype->get_program() != NULL && ttype->get_program() != program_) {
+ if (external &&
+ ttype->get_program() != NULL &&
+ ttype->get_program() != program_) {
nspace = namespace_prefix(ttype->get_program()->get_namespace("cpp"));
}
@@ -4263,29 +4952,32 @@
string t_cpp_generator::get_include_prefix(const t_program& program) const {
string include_prefix = program.get_include_prefix();
- if (!use_include_prefix_ || (include_prefix.size() > 0 && include_prefix[0] == '/')) {
+ if (!use_include_prefix_ ||
+ (include_prefix.size() > 0 && include_prefix[0] == '/')) {
// if flag is turned off or this is absolute path, return empty prefix
return "";
}
string::size_type last_slash = string::npos;
if ((last_slash = include_prefix.rfind("/")) != string::npos) {
- return include_prefix.substr(0, last_slash)
- + (get_program()->is_out_path_absolute() ? "/" : "/" + out_dir_base_ + "/");
+ return include_prefix.substr(0, last_slash) +
+ (get_program()->is_out_path_absolute() ? "/" : "/" + out_dir_base_ + "/");
+
}
return "";
}
-THRIFT_REGISTER_GENERATOR(
- cpp,
- "C++",
- " cob_style: Generate \"Continuation OBject\"-style classes.\n"
- " no_client_completion:\n"
- " Omit calls to completion__() in CobClient class.\n"
- " no_default_operators:\n"
- " Omits generation of default operators ==, != and <\n"
- " templates: Generate templatized reader/writer methods.\n"
- " pure_enums: Generate pure enums instead of wrapper classes.\n"
- " dense: Generate type specifications for the dense protocol.\n"
- " include_prefix: Use full include paths in generated files.\n")
+
+THRIFT_REGISTER_GENERATOR(cpp, "C++",
+" cob_style: Generate \"Continuation OBject\"-style classes.\n"
+" no_client_completion:\n"
+" Omit calls to completion__() in CobClient class.\n"
+" no_default_operators:\n"
+" Omits generation of default operators ==, != and <\n"
+" templates: Generate templatized reader/writer methods.\n"
+" pure_enums: Generate pure enums instead of wrapper classes.\n"
+" dense: Generate type specifications for the dense protocol.\n"
+" include_prefix: Use full include paths in generated files.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_csharp_generator.cc b/compiler/cpp/src/generate/t_csharp_generator.cc
index a0cddbf..0b9ae2a 100644
--- a/compiler/cpp/src/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/generate/t_csharp_generator.cc
@@ -43,203 +43,174 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
-class t_csharp_generator : public t_oop_generator {
-public:
- t_csharp_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+class t_csharp_generator : public t_oop_generator
+{
+ public:
+ t_csharp_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
- std::map<std::string, std::string>::const_iterator iter;
+ std::map<std::string, std::string>::const_iterator iter;
- iter = parsed_options.find("async");
- async_ = (iter != parsed_options.end());
- iter = parsed_options.find("asyncctp");
- async_ctp_ = (iter != parsed_options.end());
- if (async_ && async_ctp_) {
- throw "argument error: Cannot specify both async and asyncctp; they are incompatible.";
+ iter = parsed_options.find("async");
+ async_ = (iter != parsed_options.end());
+ iter = parsed_options.find("asyncctp");
+ async_ctp_ = (iter != parsed_options.end());
+ if (async_ && async_ctp_) {
+ throw "argument error: Cannot specify both async and asyncctp; they are incompatible.";
+ }
+
+ iter = parsed_options.find("nullable");
+ nullable_ = (iter != parsed_options.end());
+
+ iter = parsed_options.find("hashcode");
+ hashcode_ = (iter != parsed_options.end());
+
+ iter = parsed_options.find("union");
+ union_ = (iter != parsed_options.end());
+
+ iter = parsed_options.find("serial");
+ serialize_ = (iter != parsed_options.end());
+ if (serialize_) {
+ wcf_namespace_ = iter->second; // since there can be only one namespace
+ }
+
+ iter = parsed_options.find("wcf");
+ wcf_ = (iter != parsed_options.end());
+ if (wcf_) {
+ wcf_namespace_ = iter->second;
+ }
+
+ out_dir_base_ = "gen-csharp";
+ }
+ void init_generator();
+ void close_generator();
+
+ void generate_consts(std::vector<t_const*> consts);
+
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_union (t_struct* tunion);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+ void generate_property(ofstream& out, t_field* tfield, bool isPublic, bool generateIsset);
+ void generate_csharp_property(ofstream& out, t_field* tfield, bool isPublic, bool includeIsset=true, std::string fieldPrefix = "");
+ bool print_const_value (std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false, bool needtype=false);
+ std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
+ void print_const_constructor(std::ofstream& out, std::vector<t_const*> consts);
+ void print_const_def_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
+
+ void generate_csharp_struct(t_struct* tstruct, bool is_exception);
+ void generate_csharp_union(t_struct* tunion);
+ void generate_csharp_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
+ void generate_csharp_union_definition(std::ofstream& out, t_struct* tunion);
+ void generate_csharp_union_class(std::ofstream& out, t_struct* tunion, t_field* tfield);
+ void generate_csharp_wcffault(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_reader(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_result_writer(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_writer(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_tostring(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_equals(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_struct_hashcode(std::ofstream& out, t_struct* tstruct);
+ void generate_csharp_union_reader(std::ofstream& out, t_struct* tunion);
+
+ void generate_function_helpers(t_function* tfunction);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* function);
+
+ void generate_deserialize_field (std::ofstream& out, t_field* tfield, std::string prefix="", bool is_propertyless=false);
+ void generate_deserialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix="");
+ void generate_deserialize_container (std::ofstream& out, t_type* ttype, std::string prefix="");
+ void generate_deserialize_set_element (std::ofstream& out, t_set* tset, std::string prefix="");
+ void generate_deserialize_map_element (std::ofstream& out, t_map* tmap, std::string prefix="");
+ void generate_deserialize_list_element (std::ofstream& out, t_list* list, std::string prefix="");
+ void generate_serialize_field (std::ofstream& out, t_field* tfield, std::string prefix="", bool is_element=false, bool is_propertyless=false);
+ void generate_serialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix="");
+ void generate_serialize_container (std::ofstream& out, t_type* ttype, std::string prefix="");
+ void generate_serialize_map_element (std::ofstream& out, t_map* tmap, std::string iter, std::string map);
+ void generate_serialize_set_element (std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_list_element (std::ofstream& out, t_list* tlist, std::string iter);
+
+ void generate_csharp_doc (std::ofstream& out, t_field* field);
+ void generate_csharp_doc (std::ofstream& out, t_doc* tdoc);
+ void generate_csharp_doc (std::ofstream& out, t_function* tdoc);
+ void generate_csharp_docstring_comment (std::ofstream &out, string contents);
+
+ void start_csharp_namespace (std::ofstream& out);
+ void end_csharp_namespace (std::ofstream& out);
+
+ std::string csharp_type_usings();
+ std::string csharp_thrift_usings();
+
+ std::string type_name(t_type* ttype, bool in_countainer=false, bool in_init=false, bool in_param=false, bool is_required=false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false, bool in_param=false, bool is_required=false);
+ std::string declare_field(t_field* tfield, bool init=false, std::string prefix="");
+ std::string function_signature_async_begin(t_function* tfunction, std::string prefix = "");
+ std::string function_signature_async_end(t_function* tfunction, std::string prefix = "");
+ std::string function_signature_async(t_function* tfunction, std::string prefix = "");
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string argument_list(t_struct* tstruct);
+ std::string type_to_enum(t_type* ttype);
+ std::string prop_name(t_field* tfield, bool suppress_mapping = false);
+ std::string get_enum_class_name(t_type* type);
+
+ bool field_has_default(t_field* tfield) {
+ return tfield->get_value() != NULL;
}
- iter = parsed_options.find("nullable");
- nullable_ = (iter != parsed_options.end());
-
- iter = parsed_options.find("hashcode");
- hashcode_ = (iter != parsed_options.end());
-
- iter = parsed_options.find("union");
- union_ = (iter != parsed_options.end());
-
- iter = parsed_options.find("serial");
- serialize_ = (iter != parsed_options.end());
- if (serialize_) {
- wcf_namespace_ = iter->second; // since there can be only one namespace
+ bool field_is_required(t_field* tfield) {
+ return tfield->get_req() == t_field::T_REQUIRED;
}
- iter = parsed_options.find("wcf");
- wcf_ = (iter != parsed_options.end());
- if (wcf_) {
- wcf_namespace_ = iter->second;
+ bool type_can_be_null(t_type* ttype) {
+ while (ttype->is_typedef()) {
+ ttype = ((t_typedef*)ttype)->get_type();
+ }
+
+ return ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string();
}
- out_dir_base_ = "gen-csharp";
- }
- void init_generator();
- void close_generator();
+ private:
+ std::string namespace_name_;
+ std::ofstream f_service_;
+ std::string namespace_dir_;
+ bool async_;
+ bool async_ctp_;
+ bool nullable_;
+ bool union_;
+ bool hashcode_;
+ bool serialize_;
+ bool wcf_;
+ std::string wcf_namespace_;
- void generate_consts(std::vector<t_const*> consts);
+ std::map<std::string, int> csharp_keywords;
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_union(t_struct* tunion);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
- void generate_property(ofstream& out, t_field* tfield, bool isPublic, bool generateIsset);
- void generate_csharp_property(ofstream& out,
- t_field* tfield,
- bool isPublic,
- bool includeIsset = true,
- std::string fieldPrefix = "");
- bool print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval = false,
- bool needtype = false);
- std::string render_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
- void print_const_constructor(std::ofstream& out, std::vector<t_const*> consts);
- void print_const_def_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
+ void* member_mapping_scope;
+ std::map<std::string, std::string> member_name_mapping;
- void generate_csharp_struct(t_struct* tstruct, bool is_exception);
- void generate_csharp_union(t_struct* tunion);
- void generate_csharp_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool in_class = false,
- bool is_result = false);
- void generate_csharp_union_definition(std::ofstream& out, t_struct* tunion);
- void generate_csharp_union_class(std::ofstream& out, t_struct* tunion, t_field* tfield);
- void generate_csharp_wcffault(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_reader(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_result_writer(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_writer(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_tostring(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_equals(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_struct_hashcode(std::ofstream& out, t_struct* tstruct);
- void generate_csharp_union_reader(std::ofstream& out, t_struct* tunion);
-
- void generate_function_helpers(t_function* tfunction);
- void generate_service_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* function);
-
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool is_propertyless = false);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
- void generate_deserialize_list_element(std::ofstream& out, t_list* list, std::string prefix = "");
- void generate_serialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool is_element = false,
- bool is_propertyless = false);
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
-
- void generate_csharp_doc(std::ofstream& out, t_field* field);
- void generate_csharp_doc(std::ofstream& out, t_doc* tdoc);
- void generate_csharp_doc(std::ofstream& out, t_function* tdoc);
- void generate_csharp_docstring_comment(std::ofstream& out, string contents);
-
- void start_csharp_namespace(std::ofstream& out);
- void end_csharp_namespace(std::ofstream& out);
-
- std::string csharp_type_usings();
- std::string csharp_thrift_usings();
-
- std::string type_name(t_type* ttype,
- bool in_countainer = false,
- bool in_init = false,
- bool in_param = false,
- bool is_required = false);
- std::string base_type_name(t_base_type* tbase,
- bool in_container = false,
- bool in_param = false,
- bool is_required = false);
- std::string declare_field(t_field* tfield, bool init = false, std::string prefix = "");
- std::string function_signature_async_begin(t_function* tfunction, std::string prefix = "");
- std::string function_signature_async_end(t_function* tfunction, std::string prefix = "");
- std::string function_signature_async(t_function* tfunction, std::string prefix = "");
- std::string function_signature(t_function* tfunction, std::string prefix = "");
- std::string argument_list(t_struct* tstruct);
- std::string type_to_enum(t_type* ttype);
- std::string prop_name(t_field* tfield, bool suppress_mapping = false);
- std::string get_enum_class_name(t_type* type);
-
- bool field_has_default(t_field* tfield) { return tfield->get_value() != NULL; }
-
- bool field_is_required(t_field* tfield) { return tfield->get_req() == t_field::T_REQUIRED; }
-
- bool type_can_be_null(t_type* ttype) {
- while (ttype->is_typedef()) {
- ttype = ((t_typedef*)ttype)->get_type();
- }
-
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception()
- || ttype->is_string();
- }
-
-private:
- std::string namespace_name_;
- std::ofstream f_service_;
- std::string namespace_dir_;
- bool async_;
- bool async_ctp_;
- bool nullable_;
- bool union_;
- bool hashcode_;
- bool serialize_;
- bool wcf_;
- std::string wcf_namespace_;
-
- std::map<std::string, int> csharp_keywords;
-
- void* member_mapping_scope;
- std::map<std::string, std::string> member_name_mapping;
-
- void init_keywords();
- std::string normalize_name(std::string name);
- std::string make_valid_csharp_identifier(std::string const& fromName);
- void prepare_member_name_mapping(t_struct* tstruct);
- void prepare_member_name_mapping(void* scope,
- const vector<t_field*>& members,
- const string& structname);
- void cleanup_member_name_mapping(void* scope);
- string get_mapped_member_name(string oldname);
+ void init_keywords();
+ std::string normalize_name( std::string name);
+ std::string make_valid_csharp_identifier( std::string const & fromName);
+ void prepare_member_name_mapping( t_struct* tstruct);
+ void prepare_member_name_mapping( void* scope, const vector<t_field*>& members, const string & structname);
+ void cleanup_member_name_mapping( void* scope);
+ string get_mapped_member_name( string oldname);
};
+
void t_csharp_generator::init_generator() {
MKDIR(get_out_dir().c_str());
namespace_name_ = program_->get_namespace("csharp");
@@ -272,12 +243,12 @@
pverbose("- wcf ........ %s\n", (wcf_ ? "ON" : "off"));
}
-std::string t_csharp_generator::normalize_name(std::string name) {
- string tmp(name);
+std::string t_csharp_generator::normalize_name( std::string name) {
+ string tmp( name );
std::transform(tmp.begin(), tmp.end(), tmp.begin(), static_cast<int (*)(int)>(std::tolower));
// un-conflict keywords by prefixing with "@"
- if (csharp_keywords.find(tmp) != csharp_keywords.end()) {
+ if ( csharp_keywords.find(tmp) != csharp_keywords.end()) {
return "@" + name;
}
@@ -290,112 +261,113 @@
// C# keywords
csharp_keywords["abstract"] = 1;
- csharp_keywords["as"] = 1;
- csharp_keywords["base"] = 1;
- csharp_keywords["bool"] = 1;
- csharp_keywords["break"] = 1;
- csharp_keywords["byte"] = 1;
- csharp_keywords["case"] = 1;
- csharp_keywords["catch"] = 1;
- csharp_keywords["char"] = 1;
- csharp_keywords["checked"] = 1;
- csharp_keywords["class"] = 1;
- csharp_keywords["const"] = 1;
- csharp_keywords["continue"] = 1;
- csharp_keywords["decimal"] = 1;
- csharp_keywords["default"] = 1;
- csharp_keywords["delegate"] = 1;
- csharp_keywords["do"] = 1;
- csharp_keywords["double"] = 1;
- csharp_keywords["else"] = 1;
- csharp_keywords["enum"] = 1;
- csharp_keywords["event"] = 1;
- csharp_keywords["explicit"] = 1;
- csharp_keywords["extern"] = 1;
- csharp_keywords["false"] = 1;
- csharp_keywords["finally"] = 1;
- csharp_keywords["fixed"] = 1;
- csharp_keywords["float"] = 1;
- csharp_keywords["for"] = 1;
- csharp_keywords["foreach"] = 1;
- csharp_keywords["goto"] = 1;
- csharp_keywords["if"] = 1;
- csharp_keywords["implicit"] = 1;
- csharp_keywords["in"] = 1;
- csharp_keywords["int"] = 1;
- csharp_keywords["interface"] = 1;
- csharp_keywords["internal"] = 1;
- csharp_keywords["is"] = 1;
- csharp_keywords["lock"] = 1;
- csharp_keywords["long"] = 1;
- csharp_keywords["namespace"] = 1;
- csharp_keywords["new"] = 1;
- csharp_keywords["null"] = 1;
- csharp_keywords["object"] = 1;
- csharp_keywords["operator"] = 1;
- csharp_keywords["out"] = 1;
- csharp_keywords["override"] = 1;
- csharp_keywords["params"] = 1;
- csharp_keywords["private"] = 1;
- csharp_keywords["protected"] = 1;
- csharp_keywords["public"] = 1;
- csharp_keywords["readonly"] = 1;
- csharp_keywords["ref"] = 1;
- csharp_keywords["return"] = 1;
- csharp_keywords["sbyte"] = 1;
- csharp_keywords["sealed"] = 1;
- csharp_keywords["short"] = 1;
- csharp_keywords["sizeof"] = 1;
- csharp_keywords["stackalloc"] = 1;
- csharp_keywords["static"] = 1;
- csharp_keywords["string"] = 1;
- csharp_keywords["struct"] = 1;
- csharp_keywords["switch"] = 1;
- csharp_keywords["this"] = 1;
- csharp_keywords["throw"] = 1;
- csharp_keywords["true"] = 1;
- csharp_keywords["try"] = 1;
- csharp_keywords["typeof"] = 1;
- csharp_keywords["uint"] = 1;
- csharp_keywords["ulong"] = 1;
- csharp_keywords["unchecked"] = 1;
- csharp_keywords["unsafe"] = 1;
- csharp_keywords["ushort"] = 1;
- csharp_keywords["using"] = 1;
- csharp_keywords["virtual"] = 1;
- csharp_keywords["void"] = 1;
- csharp_keywords["volatile"] = 1;
- csharp_keywords["while"] = 1;
+ csharp_keywords["as"]= 1;
+ csharp_keywords["base"]= 1;
+ csharp_keywords["bool"]= 1;
+ csharp_keywords["break"]= 1;
+ csharp_keywords["byte"]= 1;
+ csharp_keywords["case"]= 1;
+ csharp_keywords["catch"]= 1;
+ csharp_keywords["char"]= 1;
+ csharp_keywords["checked"]= 1;
+ csharp_keywords["class"]= 1;
+ csharp_keywords["const"]= 1;
+ csharp_keywords["continue"]= 1;
+ csharp_keywords["decimal"]= 1;
+ csharp_keywords["default"]= 1;
+ csharp_keywords["delegate"]= 1;
+ csharp_keywords["do"]= 1;
+ csharp_keywords["double"]= 1;
+ csharp_keywords["else"]= 1;
+ csharp_keywords["enum"]= 1;
+ csharp_keywords["event"]= 1;
+ csharp_keywords["explicit"]= 1;
+ csharp_keywords["extern"]= 1;
+ csharp_keywords["false"]= 1;
+ csharp_keywords["finally"]= 1;
+ csharp_keywords["fixed"]= 1;
+ csharp_keywords["float"]= 1;
+ csharp_keywords["for"]= 1;
+ csharp_keywords["foreach"]= 1;
+ csharp_keywords["goto"]= 1;
+ csharp_keywords["if"]= 1;
+ csharp_keywords["implicit"]= 1;
+ csharp_keywords["in"]= 1;
+ csharp_keywords["int"]= 1;
+ csharp_keywords["interface"]= 1;
+ csharp_keywords["internal"]= 1;
+ csharp_keywords["is"]= 1;
+ csharp_keywords["lock"]= 1;
+ csharp_keywords["long"]= 1;
+ csharp_keywords["namespace"]= 1;
+ csharp_keywords["new"]= 1;
+ csharp_keywords["null"]= 1;
+ csharp_keywords["object"]= 1;
+ csharp_keywords["operator"]= 1;
+ csharp_keywords["out"]= 1;
+ csharp_keywords["override"]= 1;
+ csharp_keywords["params"]= 1;
+ csharp_keywords["private"]= 1;
+ csharp_keywords["protected"]= 1;
+ csharp_keywords["public"]= 1;
+ csharp_keywords["readonly"]= 1;
+ csharp_keywords["ref"]= 1;
+ csharp_keywords["return"]= 1;
+ csharp_keywords["sbyte"]= 1;
+ csharp_keywords["sealed"]= 1;
+ csharp_keywords["short"]= 1;
+ csharp_keywords["sizeof"]= 1;
+ csharp_keywords["stackalloc"]= 1;
+ csharp_keywords["static"]= 1;
+ csharp_keywords["string"]= 1;
+ csharp_keywords["struct"]= 1;
+ csharp_keywords["switch"]= 1;
+ csharp_keywords["this"]= 1;
+ csharp_keywords["throw"]= 1;
+ csharp_keywords["true"]= 1;
+ csharp_keywords["try"]= 1;
+ csharp_keywords["typeof"]= 1;
+ csharp_keywords["uint"]= 1;
+ csharp_keywords["ulong"]= 1;
+ csharp_keywords["unchecked"]= 1;
+ csharp_keywords["unsafe"]= 1;
+ csharp_keywords["ushort"]= 1;
+ csharp_keywords["using"]= 1;
+ csharp_keywords["virtual"]= 1;
+ csharp_keywords["void"]= 1;
+ csharp_keywords["volatile"]= 1;
+ csharp_keywords["while"]= 1;
// C# contextual keywords
- csharp_keywords["add"] = 1;
- csharp_keywords["alias"] = 1;
- csharp_keywords["ascending"] = 1;
- csharp_keywords["async"] = 1;
- csharp_keywords["await"] = 1;
- csharp_keywords["descending"] = 1;
- csharp_keywords["dynamic"] = 1;
- csharp_keywords["from"] = 1;
- csharp_keywords["get"] = 1;
- csharp_keywords["global"] = 1;
- csharp_keywords["group"] = 1;
- csharp_keywords["into"] = 1;
- csharp_keywords["join"] = 1;
- csharp_keywords["let"] = 1;
- csharp_keywords["orderby"] = 1;
- csharp_keywords["partial"] = 1;
- csharp_keywords["remove"] = 1;
- csharp_keywords["select"] = 1;
- csharp_keywords["set"] = 1;
- csharp_keywords["value"] = 1;
- csharp_keywords["var"] = 1;
- csharp_keywords["where"] = 1;
- csharp_keywords["yield"] = 1;
+ csharp_keywords["add"]= 1;
+ csharp_keywords["alias"]= 1;
+ csharp_keywords["ascending"]= 1;
+ csharp_keywords["async"]= 1;
+ csharp_keywords["await"]= 1;
+ csharp_keywords["descending"]= 1;
+ csharp_keywords["dynamic"]= 1;
+ csharp_keywords["from"]= 1;
+ csharp_keywords["get"]= 1;
+ csharp_keywords["global"]= 1;
+ csharp_keywords["group"]= 1;
+ csharp_keywords["into"]= 1;
+ csharp_keywords["join"]= 1;
+ csharp_keywords["let"]= 1;
+ csharp_keywords["orderby"]= 1;
+ csharp_keywords["partial"]= 1;
+ csharp_keywords["remove"]= 1;
+ csharp_keywords["select"]= 1;
+ csharp_keywords["set"]= 1;
+ csharp_keywords["value"]= 1;
+ csharp_keywords["var"]= 1;
+ csharp_keywords["where"]= 1;
+ csharp_keywords["yield"]= 1;
}
void t_csharp_generator::start_csharp_namespace(ofstream& out) {
if (!namespace_name_.empty()) {
- out << "namespace " << namespace_name_ << "\n";
+ out <<
+ "namespace " << namespace_name_ << "\n";
scope_up(out);
}
}
@@ -407,43 +379,53 @@
}
string t_csharp_generator::csharp_type_usings() {
- return string() + "using System;\n" + "using System.Collections;\n"
- + "using System.Collections.Generic;\n" + "using System.Text;\n" + "using System.IO;\n"
- + ((async_ || async_ctp_) ? "using System.Threading.Tasks;\n" : "") + "using Thrift;\n"
- + "using Thrift.Collections;\n" + ((serialize_ || wcf_) ? "#if !SILVERLIGHT\n" : "")
- + ((serialize_ || wcf_) ? "using System.Xml.Serialization;\n" : "")
- + ((serialize_ || wcf_) ? "#endif\n" : "") + (wcf_ ? "//using System.ServiceModel;\n" : "")
- + "using System.Runtime.Serialization;\n";
+ return string() +
+ "using System;\n" +
+ "using System.Collections;\n" +
+ "using System.Collections.Generic;\n" +
+ "using System.Text;\n" +
+ "using System.IO;\n" +
+ ((async_||async_ctp_) ? "using System.Threading.Tasks;\n" : "") +
+ "using Thrift;\n" +
+ "using Thrift.Collections;\n" +
+ ((serialize_||wcf_) ? "#if !SILVERLIGHT\n" : "") +
+ ((serialize_||wcf_) ? "using System.Xml.Serialization;\n" : "") +
+ ((serialize_||wcf_) ? "#endif\n" : "") +
+ (wcf_ ? "//using System.ServiceModel;\n" : "") +
+ "using System.Runtime.Serialization;\n";
}
string t_csharp_generator::csharp_thrift_usings() {
- return string() + "using Thrift.Protocol;\n" + "using Thrift.Transport;\n";
+ return string() +
+ "using Thrift.Protocol;\n" +
+ "using Thrift.Transport;\n";
}
-void t_csharp_generator::close_generator() {
-}
+void t_csharp_generator::close_generator() { }
void t_csharp_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
void t_csharp_generator::generate_enum(t_enum* tenum) {
- string f_enum_name = namespace_dir_ + "/" + (tenum->get_name()) + ".cs";
+ string f_enum_name = namespace_dir_+"/" + (tenum->get_name())+".cs";
ofstream f_enum;
f_enum.open(f_enum_name.c_str());
- f_enum << autogen_comment() << endl;
+ f_enum <<
+ autogen_comment() << endl;
start_csharp_namespace(f_enum);
generate_csharp_doc(f_enum, tenum);
- indent(f_enum) << "public enum " << tenum->get_name() << "\n";
+ indent(f_enum) <<
+ "public enum " << tenum->get_name() << "\n";
scope_up(f_enum);
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
- generate_csharp_doc(f_enum, *c_iter);
+ generate_csharp_doc(f_enum, *c_iter);
int value = (*c_iter)->get_value();
indent(f_enum) << (*c_iter)->get_name() << " = " << value << "," << endl;
@@ -457,30 +439,28 @@
}
void t_csharp_generator::generate_consts(std::vector<t_const*> consts) {
- if (consts.empty()) {
+ if (consts.empty()){
return;
}
- string f_consts_name = namespace_dir_ + '/' + program_name_ + ".Constants.cs";
+ string f_consts_name = namespace_dir_ + '/' + program_name_ + ".Constants.cs";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
- f_consts << autogen_comment() << csharp_type_usings() << endl;
+ f_consts <<
+ autogen_comment() <<
+ csharp_type_usings() << endl;
start_csharp_namespace(f_consts);
- indent(f_consts) << "public static class " << make_valid_csharp_identifier(program_name_)
- << "Constants" << endl;
+ indent(f_consts) <<
+ "public static class " << make_valid_csharp_identifier(program_name_) << "Constants" << endl;
scope_up(f_consts);
vector<t_const*>::iterator c_iter;
bool need_static_constructor = false;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
generate_csharp_doc(f_consts, (*c_iter));
- if (print_const_value(f_consts,
- (*c_iter)->get_name(),
- (*c_iter)->get_type(),
- (*c_iter)->get_value(),
- false)) {
+ if (print_const_value(f_consts, (*c_iter)->get_name(), (*c_iter)->get_type(), (*c_iter)->get_value(), false)) {
need_static_constructor = true;
}
}
@@ -494,10 +474,8 @@
f_consts.close();
}
-void t_csharp_generator::print_const_def_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
+void t_csharp_generator::print_const_def_value(std::ofstream& out, string name, t_type* type, t_const_value* value)
+{
if (type->is_struct() || type->is_xception()) {
const vector<t_field*>& fields = ((t_struct*)type)->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -525,8 +503,7 @@
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string key = render_const_value(out, name, ktype, v_iter->first);
string val = render_const_value(out, name, vtype, v_iter->second);
- indent(out) << name << "[" << key << "]"
- << " = " << val << ";" << endl;
+ indent(out) << name << "[" << key << "]" << " = " << val << ";" << endl;
}
} else if (type->is_list() || type->is_set()) {
t_type* etype;
@@ -546,8 +523,7 @@
}
void t_csharp_generator::print_const_constructor(std::ofstream& out, std::vector<t_const*> consts) {
- indent(out) << "static " << make_valid_csharp_identifier(program_name_).c_str() << "Constants()"
- << endl;
+ indent(out) << "static " << make_valid_csharp_identifier(program_name_).c_str() << "Constants()" << endl;
scope_up(out);
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
@@ -560,15 +536,9 @@
scope_down(out);
}
-// it seems like all that methods that call this are using in_static to be the opposite of what it
-// would imply
-bool t_csharp_generator::print_const_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval,
- bool needtype) {
+
+//it seems like all that methods that call this are using in_static to be the opposite of what it would imply
+bool t_csharp_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval, bool needtype) {
indent(out);
bool need_static_construction = !in_static;
while (type->is_typedef()) {
@@ -576,16 +546,16 @@
}
if (!defval || needtype) {
- out << (in_static ? "" : type->is_base_type() ? "public const " : "public static ")
- << type_name(type) << " ";
+ out <<
+ (in_static ? "" : type->is_base_type() ? "public const " : "public static ") <<
+ type_name(type) << " ";
}
if (type->is_base_type()) {
string v2 = render_const_value(out, name, type, value);
out << name << " = " << v2 << ";" << endl;
need_static_construction = false;
} else if (type->is_enum()) {
- out << name << " = " << type_name(type, false, true) << "." << value->get_identifier_name()
- << ";" << endl;
+ out << name << " = " << type_name(type, false, true) << "." << value->get_identifier_name() << ";" << endl;
need_static_construction = false;
} else if (type->is_struct() || type->is_xception()) {
out << name << " = new " << type_name(type) << "();" << endl;
@@ -602,37 +572,34 @@
return need_static_construction;
}
-std::string t_csharp_generator::render_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+std::string t_csharp_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
std::ostringstream render;
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << '"' << get_escaped_string(value) << '"';
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() > 0) ? "true" : "false");
- break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
+ case t_base_type::TYPE_STRING:
+ render << '"' << get_escaped_string(value) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
render << value->get_integer();
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
render << type->get_name() << "." << value->get_identifier_name();
@@ -663,18 +630,17 @@
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << csharp_type_usings() << csharp_thrift_usings() << endl;
+ f_struct <<
+ autogen_comment() <<
+ csharp_type_usings() <<
+ csharp_thrift_usings() << endl;
generate_csharp_struct_definition(f_struct, tstruct, is_exception);
f_struct.close();
}
-void t_csharp_generator::generate_csharp_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool in_class,
- bool is_result) {
+void t_csharp_generator::generate_csharp_struct_definition(ofstream &out, t_struct* tstruct, bool is_exception, bool in_class, bool is_result) {
if (!in_class) {
start_csharp_namespace(out);
@@ -683,20 +649,17 @@
out << endl;
generate_csharp_doc(out, tstruct);
- prepare_member_name_mapping(tstruct);
+ prepare_member_name_mapping( tstruct);
indent(out) << "#if !SILVERLIGHT" << endl;
indent(out) << "[Serializable]" << endl;
indent(out) << "#endif" << endl;
- if ((serialize_ || wcf_) && !is_exception) {
- indent(out) << "[DataContract(Namespace=\"" << wcf_namespace_ << "\")]"
- << endl; // do not make exception classes directly WCF serializable, we provide a
- // seperate "fault" for that
+ if ((serialize_||wcf_) &&!is_exception) {
+ indent(out) << "[DataContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl; // do not make exception classes directly WCF serializable, we provide a seperate "fault" for that
}
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- indent(out) << "public " << (is_final ? "sealed " : "") << "partial class "
- << normalize_name(tstruct->get_name()) << " : ";
+ indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << normalize_name(tstruct->get_name()) << " : ";
if (is_exception) {
out << "TException, ";
@@ -710,7 +673,7 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- // make private members with public Properties
+ //make private members with public Properties
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
// if the field is requied, then we use auto-properties
if (!field_is_required((*m_iter)) && (!nullable_ || field_has_default((*m_iter)))) {
@@ -737,18 +700,23 @@
}
}
- bool generate_isset = (nullable_ && has_non_required_default_value_fields)
- || (!nullable_ && has_non_required_fields);
+ bool generate_isset =
+ (nullable_ && has_non_required_default_value_fields)
+ || (!nullable_ && has_non_required_fields);
if (generate_isset) {
- out << endl;
- if (serialize_ || wcf_) {
- out << indent() << "[XmlIgnore] // XmlSerializer" << endl << indent()
- << "[DataMember(Order = 1)] // XmlObjectSerializer, DataContractJsonSerializer, etc."
- << endl;
+ out <<
+ endl;
+ if(serialize_||wcf_) {
+ out <<
+ indent() << "[XmlIgnore] // XmlSerializer" << endl <<
+ indent() << "[DataMember(Order = 1)] // XmlObjectSerializer, DataContractJsonSerializer, etc." << endl;
}
- out << indent() << "public Isset __isset;" << endl << indent() << "#if !SILVERLIGHT" << endl
- << indent() << "[Serializable]" << endl << indent() << "#endif" << endl;
- if (serialize_ || wcf_) {
+ out <<
+ indent() << "public Isset __isset;" << endl <<
+ indent() << "#if !SILVERLIGHT" << endl <<
+ indent() << "[Serializable]" << endl <<
+ indent() << "#endif" << endl;
+ if (serialize_||wcf_) {
indent(out) << "[DataContract]" << endl;
}
indent(out) << "public struct Isset {" << endl;
@@ -760,7 +728,7 @@
// if it is not required, if it has a default value, we need to generate Isset
// if we are not nullable, then we generate Isset
if (!is_required && (!nullable_ || has_default)) {
- if (serialize_ || wcf_) {
+ if(serialize_||wcf_) {
indent(out) << "[DataMember]" << endl;
}
indent(out) << "public bool " << normalize_name((*m_iter)->get_name()) << ";" << endl;
@@ -770,7 +738,7 @@
indent_down();
indent(out) << "}" << endl << endl;
- if (generate_isset && (serialize_ || wcf_)) {
+ if(generate_isset && (serialize_||wcf_)) {
indent(out) << "#region XmlSerializer support" << endl << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -806,15 +774,9 @@
if (field_is_required((*m_iter))) {
print_const_value(out, "this." + prop_name(*m_iter), t, (*m_iter)->get_value(), true, true);
} else {
- print_const_value(out,
- "this._" + (*m_iter)->get_name(),
- t,
- (*m_iter)->get_value(),
- true,
- true);
+ print_const_value(out, "this._" + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
// Optionals with defaults are marked set
- indent(out) << "this.__isset." << normalize_name((*m_iter)->get_name()) << " = true;"
- << endl;
+ indent(out) << "this.__isset." << normalize_name((*m_iter)->get_name()) << " = true;" << endl;
}
}
}
@@ -839,8 +801,7 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (field_is_required((*m_iter))) {
- indent(out) << "this." << prop_name((*m_iter)) << " = " << (*m_iter)->get_name() << ";"
- << endl;
+ indent(out) << "this." << prop_name((*m_iter)) << " = " << (*m_iter)->get_name() << ";" << endl;
}
}
@@ -863,11 +824,11 @@
out << endl;
// generate a corresponding WCF fault to wrap the exception
- if ((serialize_ || wcf_) && is_exception) {
+ if((serialize_||wcf_) && is_exception) {
generate_csharp_wcffault(out, tstruct);
}
- cleanup_member_name_mapping(tstruct);
+ cleanup_member_name_mapping( tstruct);
if (!in_class) {
end_csharp_namespace(out);
}
@@ -881,8 +842,7 @@
indent(out) << "[DataContract]" << endl;
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << tstruct->get_name()
- << "Fault" << endl;
+ indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << tstruct->get_name() << "Fault" << endl;
scope_up(out);
@@ -891,7 +851,8 @@
// make private members with public Properties
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "private " << declare_field(*m_iter, false, "_") << endl;
+ indent(out) <<
+ "private " << declare_field(*m_iter, false, "_") << endl;
}
out << endl;
@@ -904,7 +865,8 @@
}
void t_csharp_generator::generate_csharp_struct_reader(ofstream& out, t_struct* tstruct) {
- indent(out) << "public void Read (TProtocol iprot)" << endl;
+ indent(out) <<
+ "public void Read (TProtocol iprot)" << endl;
scope_up(out);
const vector<t_field*>& fields = tstruct->get_members();
@@ -917,28 +879,38 @@
}
}
- indent(out) << "TField field;" << endl << indent() << "iprot.ReadStructBegin();" << endl;
+ indent(out) <<
+ "TField field;" << endl <<
+ indent() << "iprot.ReadStructBegin();" << endl;
- indent(out) << "while (true)" << endl;
+ indent(out) <<
+ "while (true)" << endl;
scope_up(out);
- indent(out) << "field = iprot.ReadFieldBegin();" << endl;
+ indent(out) <<
+ "field = iprot.ReadFieldBegin();" << endl;
- indent(out) << "if (field.Type == TType.Stop) { " << endl;
+ indent(out) <<
+ "if (field.Type == TType.Stop) { " << endl;
indent_up();
- indent(out) << "break;" << endl;
+ indent(out) <<
+ "break;" << endl;
indent_down();
- indent(out) << "}" << endl;
+ indent(out) <<
+ "}" << endl;
- indent(out) << "switch (field.ID)" << endl;
+ indent(out) <<
+ "switch (field.ID)" << endl;
scope_up(out);
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool is_required = field_is_required((*f_iter));
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
indent_up();
- indent(out) << "if (field.Type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent(out) <<
+ "if (field.Type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
indent_up();
generate_deserialize_field(out, *f_iter);
@@ -947,12 +919,16 @@
}
indent_down();
- out << indent() << "} else { " << endl << indent() << " TProtocolUtil.Skip(iprot, field.Type);"
- << endl << indent() << "}" << endl << indent() << "break;" << endl;
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.Skip(iprot, field.Type);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
indent_down();
}
- indent(out) << "default: " << endl;
+ indent(out) <<
+ "default: " << endl;
indent_up();
indent(out) << "TProtocolUtil.Skip(iprot, field.Type);" << endl;
indent(out) << "break;" << endl;
@@ -960,11 +936,13 @@
scope_down(out);
- indent(out) << "iprot.ReadFieldEnd();" << endl;
+ indent(out) <<
+ "iprot.ReadFieldEnd();" << endl;
scope_down(out);
- indent(out) << "iprot.ReadStructEnd();" << endl;
+ indent(out) <<
+ "iprot.ReadStructEnd();" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (field_is_required((*f_iter))) {
@@ -978,18 +956,22 @@
indent_down();
indent(out) << "}" << endl << endl;
+
}
void t_csharp_generator::generate_csharp_struct_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public void Write(TProtocol oprot) {" << endl;
+ out <<
+ indent() << "public void Write(TProtocol oprot) {" << endl;
indent_up();
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
- indent(out) << "TStruct struc = new TStruct(\"" << name << "\");" << endl;
- indent(out) << "oprot.WriteStructBegin(struc);" << endl;
+ indent(out) <<
+ "TStruct struc = new TStruct(\"" << name << "\");" << endl;
+ indent(out) <<
+ "oprot.WriteStructBegin(struc);" << endl;
if (fields.size() > 0) {
indent(out) << "TField field = new TField();" << endl;
@@ -1002,11 +984,12 @@
} else if (!is_required) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- indent(out) << "if (" << prop_name((*f_iter)) << " != null && __isset."
- << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+ indent(out) <<
+ "if (" << prop_name((*f_iter)) << " != null && __isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
indent_up();
} else {
- indent(out) << "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+ indent(out) <<
+ "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
indent_up();
}
}
@@ -1034,15 +1017,18 @@
}
void t_csharp_generator::generate_csharp_struct_result_writer(ofstream& out, t_struct* tstruct) {
- indent(out) << "public void Write(TProtocol oprot) {" << endl;
+ indent(out) <<
+ "public void Write(TProtocol oprot) {" << endl;
indent_up();
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
- indent(out) << "TStruct struc = new TStruct(\"" << name << "\");" << endl;
- indent(out) << "oprot.WriteStructBegin(struc);" << endl;
+ indent(out) <<
+ "TStruct struc = new TStruct(\"" << name << "\");" << endl;
+ indent(out) <<
+ "oprot.WriteStructBegin(struc);" << endl;
if (fields.size() > 0) {
indent(out) << "TField field = new TField();" << endl;
@@ -1064,18 +1050,24 @@
bool null_allowed = !nullable_ && type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- indent(out) << "if (" << prop_name(*f_iter) << " != null) {" << endl;
+ indent(out) <<
+ "if (" << prop_name(*f_iter) << " != null) {" << endl;
indent_up();
}
- indent(out) << "field.Name = \"" << prop_name(*f_iter) << "\";" << endl;
- indent(out) << "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
- indent(out) << "field.ID = " << (*f_iter)->get_key() << ";" << endl;
- indent(out) << "oprot.WriteFieldBegin(field);" << endl;
+ indent(out) <<
+ "field.Name = \"" << prop_name(*f_iter) << "\";" << endl;
+ indent(out) <<
+ "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
+ indent(out) <<
+ "field.ID = " << (*f_iter)->get_key() << ";" << endl;
+ indent(out) <<
+ "oprot.WriteFieldBegin(field);" << endl;
generate_serialize_field(out, *f_iter);
- indent(out) << "oprot.WriteFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.WriteFieldEnd();" << endl;
if (null_allowed) {
indent_down();
@@ -1087,34 +1079,38 @@
}
}
- out << endl << indent() << "oprot.WriteFieldStop();" << endl << indent()
- << "oprot.WriteStructEnd();" << endl;
+ out <<
+ endl <<
+ indent() << "oprot.WriteFieldStop();" << endl <<
+ indent() << "oprot.WriteStructEnd();" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl << endl;
}
void t_csharp_generator::generate_csharp_struct_tostring(ofstream& out, t_struct* tstruct) {
- indent(out) << "public override string ToString() {" << endl;
+ indent(out) <<
+ "public override string ToString() {" << endl;
indent_up();
- indent(out) << "StringBuilder __sb = new StringBuilder(\"" << tstruct->get_name() << "(\");"
- << endl;
+ indent(out) <<
+ "StringBuilder __sb = new StringBuilder(\"" << tstruct->get_name() << "(\");" << endl;
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
bool useFirstFlag = false;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (!field_is_required((*f_iter))) {
+ if( ! field_is_required((*f_iter))) {
indent(out) << "bool __first = true;" << endl;
useFirstFlag = true;
}
break;
}
- bool had_required = false; // set to true after first required field has been processed
+ bool had_required = false; // set to true after first required field has been processed
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool is_required = field_is_required((*f_iter));
@@ -1125,55 +1121,66 @@
} else if (!is_required) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- indent(out) << "if (" << prop_name((*f_iter)) << " != null && __isset."
- << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+ indent(out) <<
+ "if (" << prop_name((*f_iter)) << " != null && __isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
indent_up();
} else {
- indent(out) << "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+ indent(out) <<
+ "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
indent_up();
}
}
- if (useFirstFlag && (!had_required)) {
+ if( useFirstFlag && (! had_required)) {
indent(out) << "if(!__first) { __sb.Append(\", \"); }" << endl;
- if (!is_required) {
+ if( ! is_required) {
indent(out) << "__first = false;" << endl;
}
- indent(out) << "__sb.Append(\"" << prop_name((*f_iter)) << ": \");" << endl;
+ indent(out) <<
+ "__sb.Append(\"" << prop_name((*f_iter)) << ": \");" << endl;
} else {
- indent(out) << "__sb.Append(\", " << prop_name((*f_iter)) << ": \");" << endl;
+ indent(out) <<
+ "__sb.Append(\", " << prop_name((*f_iter)) << ": \");" << endl;
}
+
t_type* ttype = (*f_iter)->get_type();
if (ttype->is_xception() || ttype->is_struct()) {
- indent(out) << "__sb.Append(" << prop_name((*f_iter))
- << "== null ? \"<null>\" : " << prop_name((*f_iter)) << ".ToString());" << endl;
+ indent(out) <<
+ "__sb.Append(" << prop_name((*f_iter)) << "== null ? \"<null>\" : "<< prop_name((*f_iter)) << ".ToString());" << endl;
} else {
- indent(out) << "__sb.Append(" << prop_name((*f_iter)) << ");" << endl;
+ indent(out) <<
+ "__sb.Append(" << prop_name((*f_iter)) << ");" << endl;
}
if (!is_required) {
indent_down();
indent(out) << "}" << endl;
} else {
- had_required = true; // now __first must be false, so we don't need to check it anymore
+ had_required = true; // now __first must be false, so we don't need to check it anymore
}
}
- indent(out) << "__sb.Append(\")\");" << endl;
- indent(out) << "return __sb.ToString();" << endl;
+ indent(out) <<
+ "__sb.Append(\")\");" << endl;
+ indent(out) <<
+ "return __sb.ToString();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
}
+
void t_csharp_generator::generate_csharp_union(t_struct* tunion) {
string f_union_name = namespace_dir_ + "/" + (tunion->get_name()) + ".cs";
ofstream f_union;
f_union.open(f_union_name.c_str());
- f_union << autogen_comment() << csharp_type_usings() << csharp_thrift_usings() << endl;
+ f_union <<
+ autogen_comment() <<
+ csharp_type_usings() <<
+ csharp_thrift_usings() << endl;
generate_csharp_union_definition(f_union, tunion);
@@ -1184,8 +1191,7 @@
// Let's define the class first
start_csharp_namespace(out);
- indent(out) << "public abstract partial class " << tunion->get_name() << " : TAbstractBase {"
- << endl;
+ indent(out) << "public abstract partial class " << tunion->get_name() << " : TAbstractBase {" << endl;
indent_up();
@@ -1208,8 +1214,7 @@
indent(out) << "public override void Write(TProtocol protocol) {" << endl;
indent_up();
- indent(out) << "throw new TProtocolException( TProtocolException.INVALID_DATA, \"Cannot persist "
- "an union type which is not set.\");" << endl;
+ indent(out) << "throw new TProtocolException( TProtocolException.INVALID_DATA, \"Cannot persist an union type which is not set.\");" << endl;
indent_down();
indent(out) << "}" << endl << endl;
@@ -1231,16 +1236,12 @@
end_csharp_namespace(out);
}
-void t_csharp_generator::generate_csharp_union_class(std::ofstream& out,
- t_struct* tunion,
- t_field* tfield) {
- indent(out) << "public class " << tfield->get_name() << " : " << tunion->get_name() << " {"
- << endl;
+void t_csharp_generator::generate_csharp_union_class(std::ofstream& out, t_struct* tunion, t_field* tfield) {
+ indent(out) << "public class " << tfield->get_name() << " : " << tunion->get_name() << " {" << endl;
indent_up();
indent(out) << "private " << type_name(tfield->get_type()) << " _data;" << endl;
indent(out) << "public override object Data { get { return _data; } }" << endl;
- indent(out) << "public " << tfield->get_name() << "(" << type_name(tfield->get_type())
- << " data) : base(true) {" << endl;
+ indent(out) << "public " << tfield->get_name() << "(" << type_name(tfield->get_type()) << " data) : base(true) {" << endl;
indent_up();
indent(out) << "this._data = data;" << endl;
indent_down();
@@ -1268,6 +1269,7 @@
indent(out) << "}" << endl << endl;
}
+
void t_csharp_generator::generate_csharp_struct_equals(ofstream& out, t_struct* tstruct) {
indent(out) << "public override bool Equals(object that) {" << endl;
indent_up();
@@ -1291,9 +1293,9 @@
indent(out) << "&& ";
}
if (!field_is_required((*f_iter)) && !(nullable_ && !field_has_default((*f_iter)))) {
- out << "((__isset." << normalize_name((*f_iter)->get_name()) << " == other.__isset."
- << normalize_name((*f_iter)->get_name()) << ") && ((!__isset."
- << normalize_name((*f_iter)->get_name()) << ") || (";
+ out << "((__isset." << normalize_name((*f_iter)->get_name())
+ << " == other.__isset." << normalize_name((*f_iter)->get_name())
+ << ") && ((!__isset." << normalize_name((*f_iter)->get_name()) << ") || (";
}
t_type* ttype = (*f_iter)->get_type();
if (ttype->is_container()) {
@@ -1332,16 +1334,20 @@
t_type* ttype = (*f_iter)->get_type();
indent(out) << "hashcode = (hashcode * 397) ^ ";
if (field_is_required((*f_iter))) {
- out << "(";
- } else if (nullable_) {
+ out << "(";
+ } else if ( nullable_) {
out << "(" << prop_name((*f_iter)) << " == null ? 0 : ";
- } else {
+ }else {
out << "(!__isset." << normalize_name((*f_iter)->get_name()) << " ? 0 : ";
}
if (ttype->is_container()) {
- out << "(TCollections.GetHashCode(" << prop_name((*f_iter)) << "))";
+ out << "(TCollections.GetHashCode("
+ << prop_name((*f_iter))
+ << "))";
} else {
- out << "(" << prop_name((*f_iter)) << ".GetHashCode())";
+ out << "("
+ << prop_name((*f_iter))
+ << ".GetHashCode())";
}
out << ");" << endl;
}
@@ -1358,11 +1364,15 @@
string f_service_name = namespace_dir_ + "/" + service_name_ + ".cs";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << csharp_type_usings() << csharp_thrift_usings() << endl;
+ f_service_ <<
+ autogen_comment() <<
+ csharp_type_usings() <<
+ csharp_thrift_usings() << endl;
start_csharp_namespace(f_service_);
- indent(f_service_) << "public partial class " << normalize_name(service_name_) << " {" << endl;
+ indent(f_service_) <<
+ "public partial class " << normalize_name(service_name_) << " {" << endl;
indent_up();
generate_service_interface(tservice);
@@ -1372,7 +1382,8 @@
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) <<
+ "}" << endl;
end_csharp_namespace(f_service_);
f_service_.close();
}
@@ -1388,43 +1399,51 @@
generate_csharp_doc(f_service_, tservice);
if (wcf_) {
- indent(f_service_) << "[ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
+ indent(f_service_) <<
+ "[ServiceContract(Namespace=\"" << wcf_namespace_ << "\")]" << endl;
}
- indent(f_service_) << "public interface Iface" << extends_iface << " {" << endl;
+ indent(f_service_) <<
+ "public interface Iface" << extends_iface << " {" << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- generate_csharp_doc(f_service_, *f_iter);
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
+ {
+ generate_csharp_doc(f_service_, *f_iter);
- // if we're using WCF, add the corresponding attributes
- if (wcf_) {
- indent(f_service_) << "[OperationContract]" << endl;
+ // if we're using WCF, add the corresponding attributes
+ if (wcf_) {
+ indent(f_service_) <<
+ "[OperationContract]" << endl;
- const std::vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members();
- vector<t_field*>::const_iterator x_iter;
- for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- indent(f_service_) << "[FaultContract(typeof("
- + type_name((*x_iter)->get_type(), false, false) + "Fault))]" << endl;
- }
- }
+ const std::vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ indent(f_service_) << "[FaultContract(typeof(" + type_name((*x_iter)->get_type(), false, false) + "Fault))]" << endl;
+ }
+ }
- indent(f_service_) << function_signature(*f_iter) << ";" << endl;
- if (!async_) {
+ indent(f_service_) <<
+ function_signature(*f_iter) << ";" << endl;
+ if(!async_) {
indent(f_service_) << "#if SILVERLIGHT" << endl;
}
- indent(f_service_) << function_signature_async_begin(*f_iter, "Begin_") << ";" << endl;
- indent(f_service_) << function_signature_async_end(*f_iter, "End_") << ";" << endl;
- if (async_ || async_ctp_) {
- indent(f_service_) << function_signature_async(*f_iter) << ";" << endl;
+ indent(f_service_) <<
+ function_signature_async_begin(*f_iter, "Begin_") << ";" << endl;
+ indent(f_service_) <<
+ function_signature_async_end(*f_iter, "End_") << ";" << endl;
+ if(async_||async_ctp_) {
+ indent(f_service_) <<
+ function_signature_async(*f_iter) << ";" << endl;
}
if (!async_) {
indent(f_service_) << "#endif" << endl;
}
}
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl << endl;
}
void t_csharp_generator::generate_service_helpers(t_service* tservice) {
@@ -1450,14 +1469,17 @@
generate_csharp_doc(f_service_, tservice);
- indent(f_service_) << "public class Client : " << extends_client << "Iface {" << endl;
+ indent(f_service_) <<
+ "public class Client : " << extends_client << "Iface {" << endl;
indent_up();
- indent(f_service_) << "public Client(TProtocol prot) : this(prot, prot)" << endl;
+ indent(f_service_) <<
+ "public Client(TProtocol prot) : this(prot, prot)" << endl;
scope_up(f_service_);
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public Client(TProtocol iprot, TProtocol oprot)";
+ indent(f_service_) <<
+ "public Client(TProtocol iprot, TProtocol oprot)";
if (!extends.empty()) {
f_service_ << " : base(iprot, oprot)";
}
@@ -1465,16 +1487,19 @@
scope_up(f_service_);
if (extends.empty()) {
- f_service_ << indent() << "iprot_ = iprot;" << endl << indent() << "oprot_ = oprot;" << endl;
+ f_service_ <<
+ indent() << "iprot_ = iprot;" << endl <<
+ indent() << "oprot_ = oprot;" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected TProtocol iprot_;" << endl << indent()
- << "protected TProtocol oprot_;" << endl << indent() << "protected int seqid_;"
- << endl << endl;
+ f_service_ <<
+ indent() << "protected TProtocol iprot_;" << endl <<
+ indent() << "protected TProtocol oprot_;" << endl <<
+ indent() << "protected int seqid_;" << endl << endl;
f_service_ << indent() << "public TProtocol InputProtocol" << endl;
scope_up(f_service_);
@@ -1528,13 +1553,14 @@
indent(f_service_) << "#if SILVERLIGHT" << endl;
}
// Begin_
- indent(f_service_) << "public " << function_signature_async_begin(*f_iter, "Begin_") << endl;
+ indent(f_service_) <<
+ "public " << function_signature_async_begin(*f_iter, "Begin_") << endl;
scope_up(f_service_);
- indent(f_service_) << "return "
- << "send_" << funname << "(callback, state";
+ indent(f_service_) <<
+ "return " << "send_" << funname << "(callback, state";
t_struct* arg_struct = (*f_iter)->get_arglist();
- prepare_member_name_mapping(arg_struct);
+ prepare_member_name_mapping( arg_struct);
const vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator fld_iter;
@@ -1547,28 +1573,34 @@
f_service_ << endl;
// End
- indent(f_service_) << "public " << function_signature_async_end(*f_iter, "End_") << endl;
+ indent(f_service_) <<
+ "public " << function_signature_async_end(*f_iter, "End_") << endl;
scope_up(f_service_);
- indent(f_service_) << "oprot_.Transport.EndFlush(asyncResult);" << endl;
+ indent(f_service_) <<
+ "oprot_.Transport.EndFlush(asyncResult);" << endl;
if (!(*f_iter)->is_oneway()) {
f_service_ << indent();
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "recv_" << funname << "();" << endl;
+ f_service_ <<
+ "recv_" << funname << "();" << endl;
}
scope_down(f_service_);
f_service_ << endl;
// async
bool first;
- if (async_ || async_ctp_) {
- indent(f_service_) << "public async " << function_signature_async(*f_iter, "") << endl;
+ if( async_||async_ctp_) {
+ indent(f_service_) <<
+ "public async " << function_signature_async(*f_iter, "") << endl;
scope_up(f_service_);
if (!(*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << type_name((*f_iter)->get_returntype()) << " retval;" << endl;
- indent(f_service_) << "retval = ";
+ indent(f_service_) <<
+ type_name( (*f_iter)->get_returntype()) << " retval;" << endl;
+ indent(f_service_) <<
+ "retval = ";
} else {
indent(f_service_);
}
@@ -1580,10 +1612,12 @@
scope_up(f_service_);
indent(f_service_);
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << "return ";
+ f_service_ <<
+ "return ";
}
- f_service_ << funname << "(";
- first = true;
+ f_service_ <<
+ funname << "(";
+ first = true;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
if (first) {
first = false;
@@ -1594,11 +1628,13 @@
}
f_service_ << ");" << endl;
indent_down();
- indent(f_service_) << "});" << endl;
+ indent(f_service_) <<
+ "});" << endl;
if (!(*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "return retval;" << endl;
+ indent(f_service_) <<
+ "return retval;" << endl;
}
- scope_down(f_service_);
+ scope_down(f_service_);
f_service_ << endl;
}
@@ -1608,12 +1644,14 @@
// "Normal" Synchronous invoke
generate_csharp_doc(f_service_, *f_iter);
- indent(f_service_) << "public " << function_signature(*f_iter) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(*f_iter) << endl;
scope_up(f_service_);
if (!async_) {
indent(f_service_) << "#if !SILVERLIGHT" << endl;
- indent(f_service_) << "send_" << funname << "(";
+ indent(f_service_) <<
+ "send_" << funname << "(";
first = true;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
@@ -1631,7 +1669,8 @@
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "recv_" << funname << "();" << endl;
+ f_service_ <<
+ "recv_" << funname << "();" << endl;
}
f_service_ << endl;
@@ -1650,7 +1689,8 @@
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "End_" << funname << "(asyncResult);" << endl;
+ f_service_ <<
+ "End_" << funname << "(asyncResult);" << endl;
}
f_service_ << endl;
@@ -1661,8 +1701,8 @@
// Send
t_function send_function(g_type_void,
- string("send_") + (*f_iter)->get_name(),
- (*f_iter)->get_arglist());
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
string argsname = (*f_iter)->get_name() + "_args";
@@ -1677,19 +1717,20 @@
}
scope_up(f_service_);
- f_service_ << indent() << "oprot_.WriteMessageBegin(new TMessage(\"" << funname << "\", "
- << ((*f_iter)->is_oneway() ? "TMessageType.Oneway" : "TMessageType.Call")
- << ", seqid_));" << endl << indent() << argsname << " args = new " << argsname
- << "();" << endl;
+ f_service_ <<
+ indent() << "oprot_.WriteMessageBegin(new TMessage(\"" << funname << "\", " <<
+ ((*f_iter)->is_oneway() ? "TMessageType.Oneway" : "TMessageType.Call") <<
+ ", seqid_));" << endl <<
+ indent() << argsname << " args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args." << prop_name(*fld_iter) << " = "
- << normalize_name((*fld_iter)->get_name()) << ";" << endl;
+ f_service_ <<
+ indent() << "args." << prop_name(*fld_iter) << " = " << normalize_name((*fld_iter)->get_name()) << ";" << endl;
}
- f_service_ << indent() << "args.Write(oprot_);" << endl << indent()
- << "oprot_.WriteMessageEnd();" << endl;
- ;
+ f_service_ <<
+ indent() << "args.Write(oprot_);" << endl <<
+ indent() << "oprot_.WriteMessageEnd();" << endl;;
if (!async_) {
indent(f_service_) << "#if SILVERLIGHT" << endl;
@@ -1701,7 +1742,7 @@
indent(f_service_) << "#endif" << endl;
}
- cleanup_member_name_mapping(arg_struct);
+ cleanup_member_name_mapping( arg_struct);
scope_down(f_service_);
f_service_ << endl;
@@ -1710,72 +1751,85 @@
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
- string("recv_") + (*f_iter)->get_name(),
- &noargs,
- (*f_iter)->get_xceptions());
- indent(f_service_) << "public " << function_signature(&recv_function) << endl;
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs,
+ (*f_iter)->get_xceptions());
+ indent(f_service_) <<
+ "public " << function_signature(&recv_function) << endl;
scope_up(f_service_);
- prepare_member_name_mapping((*f_iter)->get_xceptions());
+ prepare_member_name_mapping( (*f_iter)->get_xceptions());
- f_service_ << indent() << "TMessage msg = iprot_.ReadMessageBegin();" << endl << indent()
- << "if (msg.Type == TMessageType.Exception) {" << endl;
+ f_service_ <<
+ indent() << "TMessage msg = iprot_.ReadMessageBegin();" << endl <<
+ indent() << "if (msg.Type == TMessageType.Exception) {" << endl;
indent_up();
- f_service_ << indent() << "TApplicationException x = TApplicationException.Read(iprot_);"
- << endl << indent() << "iprot_.ReadMessageEnd();" << endl << indent() << "throw x;"
- << endl;
+ f_service_ <<
+ indent() << "TApplicationException x = TApplicationException.Read(iprot_);" << endl <<
+ indent() << "iprot_.ReadMessageEnd();" << endl <<
+ indent() << "throw x;" << endl;
indent_down();
- f_service_ << indent() << "}" << endl << indent() << resultname << " result = new "
- << resultname << "();" << endl << indent() << "result.Read(iprot_);" << endl
- << indent() << "iprot_.ReadMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << resultname << " result = new " << resultname << "();" << endl <<
+ indent() << "result.Read(iprot_);" << endl <<
+ indent() << "iprot_.ReadMessageEnd();" << endl;
if (!(*f_iter)->get_returntype()->is_void()) {
if (nullable_) {
if (type_can_be_null((*f_iter)->get_returntype())) {
- f_service_ << indent() << "if (result.Success != null) {" << endl << indent()
- << " return result.Success;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result.Success != null) {" << endl <<
+ indent() << " return result.Success;" << endl <<
+ indent() << "}" << endl;
} else {
- f_service_ << indent() << "if (result.Success.HasValue) {" << endl << indent()
- << " return result.Success.Value;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result.Success.HasValue) {" << endl <<
+ indent() << " return result.Success.Value;" << endl <<
+ indent() << "}" << endl;
}
} else {
- f_service_ << indent() << "if (result.__isset.success) {" << endl << indent()
- << " return result.Success;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result.__isset.success) {" << endl <<
+ indent() << " return result.Success;" << endl <<
+ indent() << "}" << endl;
}
}
- t_struct* xs = (*f_iter)->get_xceptions();
+ t_struct *xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
if (nullable_) {
- f_service_ << indent() << "if (result." << prop_name(*x_iter) << " != null) {" << endl
- << indent() << " throw result." << prop_name(*x_iter) << ";" << endl
- << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << prop_name(*x_iter) << " != null) {" << endl <<
+ indent() << " throw result." << prop_name(*x_iter) << ";" << endl <<
+ indent() << "}" << endl;
} else {
- f_service_ << indent() << "if (result.__isset." << normalize_name((*x_iter)->get_name())
- << ") {" << endl << indent() << " throw result." << prop_name(*x_iter) << ";"
- << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result.__isset." << normalize_name((*x_iter)->get_name()) << ") {" << endl <<
+ indent() << " throw result." << prop_name(*x_iter) << ";" << endl <<
+ indent() << "}" << endl;
}
}
if ((*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "return;" << endl;
+ indent(f_service_) <<
+ "return;" << endl;
} else {
- f_service_ << indent()
- << "throw new "
- "TApplicationException(TApplicationException.ExceptionType.MissingResult, \""
- << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
+ f_service_ <<
+ indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
- cleanup_member_name_mapping((*f_iter)->get_xceptions());
+ cleanup_member_name_mapping( (*f_iter)->get_xceptions());
scope_down(f_service_);
f_service_ << endl;
}
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) <<
+ "}" << endl;
}
void t_csharp_generator::generate_service_server(t_service* tservice) {
@@ -1789,84 +1843,98 @@
extends_processor = extends + ".Processor, ";
}
- indent(f_service_) << "public class Processor : " << extends_processor << "TProcessor {" << endl;
+ indent(f_service_) <<
+ "public class Processor : " << extends_processor << "TProcessor {" << endl;
indent_up();
- indent(f_service_) << "public Processor(Iface iface)";
+ indent(f_service_) <<
+ "public Processor(Iface iface)" ;
if (!extends.empty()) {
f_service_ << " : base(iface)";
}
f_service_ << endl;
scope_up(f_service_);
- f_service_ << indent() << "iface_ = iface;" << endl;
+ f_service_ <<
+ indent() << "iface_ = iface;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "processMap_[\"" << (*f_iter)->get_name()
- << "\"] = " << (*f_iter)->get_name() << "_Process;" << endl;
+ f_service_ <<
+ indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = " << (*f_iter)->get_name() << "_Process;" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_
- << indent()
- << "protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);"
- << endl;
+ f_service_ <<
+ indent() << "protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);" << endl;
}
- f_service_ << indent() << "private Iface iface_;" << endl;
+ f_service_ <<
+ indent() << "private Iface iface_;" << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected Dictionary<string, ProcessFunction> processMap_ = new "
- "Dictionary<string, ProcessFunction>();" << endl;
+ f_service_ <<
+ indent() << "protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();" << endl;
}
f_service_ << endl;
if (extends.empty()) {
- indent(f_service_) << "public bool Process(TProtocol iprot, TProtocol oprot)" << endl;
- } else {
- indent(f_service_) << "public new bool Process(TProtocol iprot, TProtocol oprot)" << endl;
+ indent(f_service_) <<
+ "public bool Process(TProtocol iprot, TProtocol oprot)" << endl;
+ }
+ else
+ {
+ indent(f_service_) <<
+ "public new bool Process(TProtocol iprot, TProtocol oprot)" << endl;
}
scope_up(f_service_);
- f_service_ << indent() << "try" << endl;
+ f_service_ << indent() << "try" << endl;
scope_up(f_service_);
- f_service_ << indent() << "TMessage msg = iprot.ReadMessageBegin();" << endl;
+ f_service_ <<
+ indent() << "TMessage msg = iprot.ReadMessageBegin();" << endl;
- f_service_
- << indent() << "ProcessFunction fn;" << endl << indent()
- << "processMap_.TryGetValue(msg.Name, out fn);" << endl << indent() << "if (fn == null) {"
- << endl << indent() << " TProtocolUtil.Skip(iprot, TType.Struct);" << endl << indent()
- << " iprot.ReadMessageEnd();" << endl << indent()
- << " TApplicationException x = new TApplicationException "
- "(TApplicationException.ExceptionType.UnknownMethod, \"Invalid method name: '\" + "
- "msg.Name + \"'\");" << endl << indent()
- << " oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));"
- << endl << indent() << " x.Write(oprot);" << endl << indent() << " oprot.WriteMessageEnd();"
- << endl << indent() << " oprot.Transport.Flush();" << endl << indent() << " return true;"
- << endl << indent() << "}" << endl << indent() << "fn(msg.SeqID, iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "ProcessFunction fn;" << endl <<
+ indent() << "processMap_.TryGetValue(msg.Name, out fn);" << endl <<
+ indent() << "if (fn == null) {" << endl <<
+ indent() << " TProtocolUtil.Skip(iprot, TType.Struct);" << endl <<
+ indent() << " iprot.ReadMessageEnd();" << endl <<
+ indent() << " TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, \"Invalid method name: '\" + msg.Name + \"'\");" << endl <<
+ indent() << " oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));" << endl <<
+ indent() << " x.Write(oprot);" << endl <<
+ indent() << " oprot.WriteMessageEnd();" << endl <<
+ indent() << " oprot.Transport.Flush();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "fn(msg.SeqID, iprot, oprot);" << endl;
scope_down(f_service_);
- f_service_ << indent() << "catch (IOException)" << endl;
+ f_service_ <<
+ indent() << "catch (IOException)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "return false;" << endl;
+ f_service_ <<
+ indent() << "return false;" << endl;
scope_down(f_service_);
- f_service_ << indent() << "return true;" << endl;
+ f_service_ <<
+ indent() << "return true;" << endl;
scope_down(f_service_);
f_service_ << endl;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
+ {
generate_process_function(tservice, *f_iter);
}
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) <<
+ "}" << endl << endl;
}
void t_csharp_generator::generate_function_helpers(t_function* tfunction) {
@@ -1880,7 +1948,7 @@
result.append(&success);
}
- t_struct* xs = tfunction->get_xceptions();
+ t_struct *xs = tfunction->get_xceptions();
const vector<t_field*>& fields = xs->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1891,27 +1959,31 @@
}
void t_csharp_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
- indent(f_service_) << "public void " << tfunction->get_name()
- << "_Process(int seqid, TProtocol iprot, TProtocol oprot)" << endl;
+ (void) tservice;
+ indent(f_service_) <<
+ "public void " << tfunction->get_name() << "_Process(int seqid, TProtocol iprot, TProtocol oprot)" << endl;
scope_up(f_service_);
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
- f_service_ << indent() << argsname << " args = new " << argsname << "();" << endl << indent()
- << "args.Read(iprot);" << endl << indent() << "iprot.ReadMessageEnd();" << endl;
+ f_service_ <<
+ indent() << argsname << " args = new " << argsname << "();" << endl <<
+ indent() << "args.Read(iprot);" << endl <<
+ indent() << "iprot.ReadMessageEnd();" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
if (!tfunction->is_oneway()) {
- f_service_ << indent() << resultname << " result = new " << resultname << "();" << endl;
+ f_service_ <<
+ indent() << resultname << " result = new " << resultname << "();" << endl;
}
if (xceptions.size() > 0) {
- f_service_ << indent() << "try {" << endl;
+ f_service_ <<
+ indent() << "try {" << endl;
indent_up();
}
@@ -1923,7 +1995,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result.Success = ";
}
- f_service_ << "iface_." << normalize_name(tfunction->get_name()) << "(";
+ f_service_ <<
+ "iface_." << normalize_name(tfunction->get_name()) << "(";
bool first = true;
prepare_member_name_mapping(arg_struct);
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1943,35 +2016,36 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
f_service_ << indent() << "}";
- prepare_member_name_mapping(xs);
+ prepare_member_name_mapping( xs);
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " "
- << (*x_iter)->get_name() << ") {" << endl;
+ f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << prop_name(*x_iter) << " = " << (*x_iter)->get_name()
- << ";" << endl;
+ f_service_ <<
+ indent() << "result." << prop_name(*x_iter) << " = " << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent() << "}";
} else {
f_service_ << "}";
}
}
- cleanup_member_name_mapping(xs);
+ cleanup_member_name_mapping( xs);
f_service_ << endl;
}
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
scope_down(f_service_);
return;
}
- f_service_ << indent() << "oprot.WriteMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.Reply, seqid)); " << endl << indent() << "result.Write(oprot);"
- << endl << indent() << "oprot.WriteMessageEnd();" << endl << indent()
- << "oprot.Transport.Flush();" << endl;
+ f_service_ <<
+ indent() << "oprot.WriteMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.Reply, seqid)); " << endl <<
+ indent() << "result.Write(oprot);" << endl <<
+ indent() << "oprot.WriteMessageEnd();" << endl <<
+ indent() << "oprot.Transport.Flush();" << endl;
scope_down(f_service_);
@@ -2000,7 +2074,8 @@
scope_up(out);
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
indent_up();
indent(out) << "if (field.Type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
indent_up();
@@ -2010,16 +2085,20 @@
indent(out) << "retval = new " << (*f_iter)->get_name() << "(temp);" << endl;
indent_down();
- out << indent() << "} else { " << endl << indent() << " TProtocolUtil.Skip(iprot, field.Type);"
- << endl << indent() << " retval = new ___undefined();" << endl << indent() << "}" << endl
- << indent() << "break;" << endl;
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.Skip(iprot, field.Type);" << endl <<
+ indent() << " retval = new ___undefined();" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
indent_down();
}
- indent(out) << "default: " << endl;
+ indent(out) <<
+ "default: " << endl;
indent_up();
- indent(out) << "TProtocolUtil.Skip(iprot, field.Type);" << endl << indent()
- << "retval = new ___undefined();" << endl;
+ indent(out) << "TProtocolUtil.Skip(iprot, field.Type);" << endl <<
+ indent() << "retval = new ___undefined();" << endl;
indent(out) << "break;" << endl;
indent_down();
@@ -2035,7 +2114,8 @@
// end of else for TStop
scope_down(out);
- indent(out) << "iprot.ReadStructEnd();" << endl;
+ indent(out) <<
+ "iprot.ReadStructEnd();" << endl;
indent(out) << "return retval;" << endl;
@@ -2044,12 +2124,9 @@
indent(out) << "}" << endl << endl;
}
-void t_csharp_generator::generate_deserialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- bool is_propertyless) {
+void t_csharp_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix, bool is_propertyless) {
t_type* type = tfield->get_type();
- while (type->is_typedef()) {
+ while(type->is_typedef()) {
type = ((t_typedef*)type)->get_type();
}
@@ -2064,9 +2141,11 @@
} else if (type->is_container()) {
generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << name << " = ";
+ indent(out) <<
+ name << " = ";
- if (type->is_enum()) {
+ if (type->is_enum())
+ {
out << "(" << type_name(type, false, true) << ")";
}
@@ -2075,62 +2154,57 @@
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:
- if (((t_base_type*)type)->is_binary()) {
- out << "ReadBinary();";
- } else {
- out << "ReadString();";
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "ReadBool();";
- break;
- case t_base_type::TYPE_BYTE:
- out << "ReadByte();";
- break;
- case t_base_type::TYPE_I16:
- out << "ReadI16();";
- break;
- case t_base_type::TYPE_I32:
- out << "ReadI32();";
- break;
- case t_base_type::TYPE_I64:
- out << "ReadI64();";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "ReadDouble();";
- break;
- default:
- throw "compiler error: no C# name for base type " + t_base_type::t_base_name(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:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "ReadBinary();";
+ } else {
+ out << "ReadString();";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "ReadBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "ReadByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "ReadI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "ReadI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "ReadI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "ReadDouble();";
+ break;
+ default:
+ throw "compiler error: no C# name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "ReadI32();";
}
out << endl;
} else {
- printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", tfield->get_name().c_str(), type_name(type).c_str());
}
}
-void t_csharp_generator::generate_deserialize_struct(ofstream& out,
- t_struct* tstruct,
- string prefix) {
+void t_csharp_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
if (union_ && tstruct->is_union()) {
out << indent() << prefix << " = " << type_name(tstruct) << ".Read(iprot);" << endl;
} else {
- out << indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << indent()
- << prefix << ".Read(iprot);" << endl;
+ out <<
+ indent() << prefix << " = new " << type_name(tstruct) << "();" << endl <<
+ indent() << prefix << ".Read(iprot);" << endl;
}
}
-void t_csharp_generator::generate_deserialize_container(ofstream& out,
- t_type* ttype,
- string prefix) {
+void t_csharp_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
scope_up(out);
string obj;
@@ -2143,19 +2217,22 @@
obj = tmp("_list");
}
- indent(out) << prefix << " = new " << type_name(ttype, false, true) << "();" << endl;
+ indent(out) <<
+ prefix << " = new " << type_name(ttype, false, true) << "();" <<endl;
if (ttype->is_map()) {
- out << indent() << "TMap " << obj << " = iprot.ReadMapBegin();" << endl;
+ out <<
+ indent() << "TMap " << obj << " = iprot.ReadMapBegin();" << endl;
} else if (ttype->is_set()) {
- out << indent() << "TSet " << obj << " = iprot.ReadSetBegin();" << endl;
+ out <<
+ indent() << "TSet " << obj << " = iprot.ReadSetBegin();" << endl;
} else if (ttype->is_list()) {
- out << indent() << "TList " << obj << " = iprot.ReadListBegin();" << endl;
+ out <<
+ indent() << "TList " << obj << " = iprot.ReadListBegin();" << endl;
}
string i = tmp("_i");
- indent(out) << "for( int " << i << " = 0; " << i << " < " << obj << ".Count"
- << "; "
- << "++" << i << ")" << endl;
+ indent(out) <<
+ "for( int " << i << " = 0; " << i << " < " << obj << ".Count" << "; " << "++" << i << ")" << endl;
scope_up(out);
if (ttype->is_map()) {
@@ -2179,55 +2256,52 @@
scope_down(out);
}
-void t_csharp_generator::generate_deserialize_map_element(ofstream& out,
- t_map* tmap,
- string prefix) {
+void t_csharp_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey) << endl;
- indent(out) << declare_field(&fval) << endl;
+ indent(out) <<
+ declare_field(&fkey) << endl;
+ indent(out) <<
+ declare_field(&fval) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << ";" << endl;
}
-void t_csharp_generator::generate_deserialize_set_element(ofstream& out,
- t_set* tset,
- string prefix) {
+void t_csharp_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".Add(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".Add(" << elem << ");" << endl;
}
-void t_csharp_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix) {
+void t_csharp_generator::generate_deserialize_list_element(ofstream& out, t_list* tlist, string prefix) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".Add(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".Add(" << elem << ");" << endl;
}
-void t_csharp_generator::generate_serialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- bool is_element,
- bool is_propertyless) {
+ void t_csharp_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix, bool is_element, bool is_propertyless) {
t_type* type = tfield->get_type();
while (type->is_typedef()) {
type = ((t_typedef*)type)->get_type();
@@ -2244,45 +2318,47 @@
} else if (type->is_container()) {
generate_serialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "oprot.";
+ indent(out) <<
+ "oprot.";
- string nullable_name = nullable_ && !is_element && !field_is_required(tfield) ? name + ".Value"
- : name;
+ string nullable_name = nullable_ && !is_element && !field_is_required(tfield)
+ ? name + ".Value"
+ : name;
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:
- if (((t_base_type*)type)->is_binary()) {
- out << "WriteBinary(";
- } else {
- out << "WriteString(";
- }
- out << name << ");";
- break;
- case t_base_type::TYPE_BOOL:
- out << "WriteBool(" << nullable_name << ");";
- break;
- case t_base_type::TYPE_BYTE:
- out << "WriteByte(" << nullable_name << ");";
- break;
- case t_base_type::TYPE_I16:
- out << "WriteI16(" << nullable_name << ");";
- break;
- case t_base_type::TYPE_I32:
- out << "WriteI32(" << nullable_name << ");";
- break;
- case t_base_type::TYPE_I64:
- out << "WriteI64(" << nullable_name << ");";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "WriteDouble(" << nullable_name << ");";
- break;
- default:
- throw "compiler error: no C# name for base type " + t_base_type::t_base_name(tbase);
+ 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:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "WriteBinary(";
+ } else {
+ out << "WriteString(";
+ }
+ out << name << ");";
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "WriteBool(" << nullable_name << ");";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "WriteByte(" << nullable_name << ");";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "WriteI16(" << nullable_name << ");";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "WriteI32(" << nullable_name << ");";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "WriteI64(" << nullable_name << ");";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "WriteDouble(" << nullable_name << ");";
+ break;
+ default:
+ throw "compiler error: no C# name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "WriteI32((int)" << nullable_name << ");";
@@ -2290,45 +2366,58 @@
out << endl;
} else {
printf("DO NOT KNOW HOW TO SERIALIZE '%s%s' TYPE '%s'\n",
- prefix.c_str(),
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ prefix.c_str(),
+ tfield->get_name().c_str(),
+ type_name(type).c_str());
}
}
-void t_csharp_generator::generate_serialize_struct(ofstream& out,
- t_struct* tstruct,
- string prefix) {
- (void)tstruct;
- out << indent() << prefix << ".Write(oprot);" << endl;
+void t_csharp_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
+ (void) tstruct;
+ out <<
+ indent() << prefix << ".Write(oprot);" << endl;
}
void t_csharp_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
scope_up(out);
if (ttype->is_map()) {
- indent(out) << "oprot.WriteMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix
- << ".Count));" << endl;
+ indent(out) <<
+ "oprot.WriteMapBegin(new TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".Count));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.WriteSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", " << prefix << ".Count));" << endl;
+ indent(out) <<
+ "oprot.WriteSetBegin(new TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".Count));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.WriteListBegin(new TList("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".Count));"
- << endl;
+ indent(out) <<
+ "oprot.WriteListBegin(new TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".Count));" << endl;
}
string iter = tmp("_iter");
if (ttype->is_map()) {
- indent(out) << "foreach (" << type_name(((t_map*)ttype)->get_key_type()) << " " << iter
- << " in " << prefix << ".Keys)";
+ indent(out) <<
+ "foreach (" <<
+ type_name(((t_map*)ttype)->get_key_type()) << " " << iter <<
+ " in " <<
+ prefix << ".Keys)";
} else if (ttype->is_set()) {
- indent(out) << "foreach (" << type_name(((t_set*)ttype)->get_elem_type()) << " " << iter
- << " in " << prefix << ")";
+ indent(out) <<
+ "foreach (" <<
+ type_name(((t_set*)ttype)->get_elem_type()) << " " << iter <<
+ " in " <<
+ prefix << ")";
} else if (ttype->is_list()) {
- indent(out) << "foreach (" << type_name(((t_list*)ttype)->get_elem_type()) << " " << iter
- << " in " << prefix << ")";
+ indent(out) <<
+ "foreach (" <<
+ type_name(((t_list*)ttype)->get_elem_type()) << " " << iter <<
+ " in " <<
+ prefix << ")";
}
out << endl;
@@ -2355,10 +2444,7 @@
scope_down(out);
}
-void t_csharp_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string iter,
- string map) {
+void t_csharp_generator::generate_serialize_map_element(ofstream& out, t_map* tmap, string iter, string map) {
t_field kfield(tmap->get_key_type(), iter);
generate_serialize_field(out, &kfield, "", true);
t_field vfield(tmap->get_val_type(), map + "[" + iter + "]");
@@ -2370,103 +2456,92 @@
generate_serialize_field(out, &efield, "", true);
}
-void t_csharp_generator::generate_serialize_list_element(ofstream& out,
- t_list* tlist,
- string iter) {
+void t_csharp_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "", true);
}
-void t_csharp_generator::generate_property(ofstream& out,
- t_field* tfield,
- bool isPublic,
- bool generateIsset) {
- generate_csharp_property(out, tfield, isPublic, generateIsset, "_");
+void t_csharp_generator::generate_property(ofstream& out, t_field* tfield, bool isPublic, bool generateIsset) {
+ generate_csharp_property(out, tfield, isPublic, generateIsset, "_");
}
-void t_csharp_generator::generate_csharp_property(ofstream& out,
- t_field* tfield,
- bool isPublic,
- bool generateIsset,
- std::string fieldPrefix) {
- if ((serialize_ || wcf_) && isPublic) {
- indent(out) << "[DataMember(Order = 0)]" << endl;
- }
- bool has_default = field_has_default(tfield);
- bool is_required = field_is_required(tfield);
- if ((nullable_ && !has_default) || (is_required)) {
- indent(out) << (isPublic ? "public " : "private ")
- << type_name(tfield->get_type(), false, false, true, is_required) << " "
- << prop_name(tfield) << " { get; set; }" << endl;
- } else {
- indent(out) << (isPublic ? "public " : "private ")
- << type_name(tfield->get_type(), false, false, true) << " " << prop_name(tfield)
- << endl;
- scope_up(out);
- indent(out) << "get" << endl;
- scope_up(out);
- bool use_nullable = false;
- if (nullable_) {
- t_type* ttype = tfield->get_type();
- while (ttype->is_typedef()) {
- ttype = ((t_typedef*)ttype)->get_type();
- }
- if (ttype->is_base_type()) {
- use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
- }
+void t_csharp_generator::generate_csharp_property(ofstream& out, t_field* tfield, bool isPublic, bool generateIsset, std::string fieldPrefix) {
+ if((serialize_||wcf_) && isPublic) {
+ indent(out) << "[DataMember(Order = 0)]" << endl;
}
- indent(out) << "return " << fieldPrefix + tfield->get_name() << ";" << endl;
- scope_down(out);
- indent(out) << "set" << endl;
- scope_up(out);
- if (use_nullable) {
- if (generateIsset) {
- indent(out) << "__isset." << normalize_name(tfield->get_name()) << " = value.HasValue;"
- << endl;
- }
- indent(out) << "if (value.HasValue) this." << fieldPrefix + tfield->get_name()
- << " = value.Value;" << endl;
+ bool has_default = field_has_default(tfield);
+ bool is_required = field_is_required(tfield);
+ if ((nullable_ && !has_default) || (is_required)) {
+ indent(out) << (isPublic ? "public " : "private ") << type_name(tfield->get_type(), false, false, true, is_required)
+ << " " << prop_name(tfield) << " { get; set; }" << endl;
} else {
- if (generateIsset) {
- indent(out) << "__isset." << normalize_name(tfield->get_name()) << " = true;" << endl;
+ indent(out) << (isPublic ? "public " : "private ") << type_name(tfield->get_type(), false, false, true)
+ << " " << prop_name(tfield) << endl;
+ scope_up(out);
+ indent(out) << "get" << endl;
+ scope_up(out);
+ bool use_nullable = false;
+ if (nullable_) {
+ t_type* ttype = tfield->get_type();
+ while (ttype->is_typedef()) {
+ ttype = ((t_typedef*)ttype)->get_type();
+ }
+ if (ttype->is_base_type()) {
+ use_nullable = ((t_base_type*)ttype)->get_base() != t_base_type::TYPE_STRING;
+ }
}
- indent(out) << "this." << fieldPrefix + tfield->get_name() << " = value;" << endl;
+ indent(out) << "return " << fieldPrefix + tfield->get_name() << ";" << endl;
+ scope_down(out);
+ indent(out) << "set" << endl;
+ scope_up(out);
+ if (use_nullable) {
+ if (generateIsset) {
+ indent(out) << "__isset." << normalize_name(tfield->get_name()) << " = value.HasValue;" << endl;
+ }
+ indent(out) << "if (value.HasValue) this." << fieldPrefix + tfield->get_name() << " = value.Value;" << endl;
+ } else {
+ if (generateIsset) {
+ indent(out) << "__isset." << normalize_name(tfield->get_name()) << " = true;" << endl;
+ }
+ indent(out) << "this." << fieldPrefix + tfield->get_name() << " = value;" << endl;
+ }
+ scope_down(out);
+ scope_down(out);
}
- scope_down(out);
- scope_down(out);
- }
- out << endl;
+ out << endl;
}
-std::string t_csharp_generator::make_valid_csharp_identifier(std::string const& fromName) {
- std::string str = fromName;
- if (str.empty()) {
+std::string t_csharp_generator::make_valid_csharp_identifier( std::string const & fromName) {
+ std::string str = fromName;
+ if( str.empty()) {
+ return str;
+ }
+
+ // tests rely on this
+ assert( ('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
+
+ // if the first letter is a number, we add an additional underscore in front of it
+ char c = str.at(0);
+ if( ('0' <= c) && (c <= '9')) {
+ str = "_" + str;
+ }
+
+ // following chars: letter, number or underscore
+ for( size_t i = 0; i < str.size(); ++i) {
+ c = str.at(i);
+ if( (('A' > c) || (c > 'Z')) &&
+ (('a' > c) || (c > 'z')) &&
+ (('0' > c) || (c > '9')) &&
+ ('_' != c) ) {
+ str.replace( i, 1, "_");
+ }
+ }
+
return str;
- }
-
- // tests rely on this
- assert(('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
-
- // if the first letter is a number, we add an additional underscore in front of it
- char c = str.at(0);
- if (('0' <= c) && (c <= '9')) {
- str = "_" + str;
- }
-
- // following chars: letter, number or underscore
- for (size_t i = 0; i < str.size(); ++i) {
- c = str.at(i);
- if ((('A' > c) || (c > 'Z')) && (('a' > c) || (c > 'z')) && (('0' > c) || (c > '9'))
- && ('_' != c)) {
- str.replace(i, 1, "_");
- }
- }
-
- return str;
}
-void t_csharp_generator::cleanup_member_name_mapping(void* scope) {
- if (member_mapping_scope != scope) {
- if (member_mapping_scope == NULL) {
+void t_csharp_generator::cleanup_member_name_mapping( void* scope) {
+ if( member_mapping_scope != scope) {
+ if( member_mapping_scope == NULL) {
throw "internal error: cleanup_member_name_mapping() not active";
} else {
throw "internal error: cleanup_member_name_mapping() called for wrong struct";
@@ -2477,24 +2552,22 @@
member_name_mapping.clear();
}
-string t_csharp_generator::get_mapped_member_name(string name) {
- map<string, string>::iterator iter = member_name_mapping.find(name);
- if (member_name_mapping.end() != iter) {
+string t_csharp_generator::get_mapped_member_name( string name) {
+ map<string,string>::iterator iter = member_name_mapping.find( name);
+ if( member_name_mapping.end() != iter) {
return iter->second;
}
pverbose("no mapping for member %s\n", name.c_str());
return name;
}
-void t_csharp_generator::prepare_member_name_mapping(t_struct* tstruct) {
- prepare_member_name_mapping(tstruct, tstruct->get_members(), tstruct->get_name());
+void t_csharp_generator::prepare_member_name_mapping( t_struct* tstruct) {
+ prepare_member_name_mapping( tstruct, tstruct->get_members(), tstruct->get_name());
}
-void t_csharp_generator::prepare_member_name_mapping(void* scope,
- const vector<t_field*>& members,
- const string& structname) {
- if (member_mapping_scope != NULL) {
- if (member_mapping_scope != scope) {
+void t_csharp_generator::prepare_member_name_mapping( void* scope, const vector<t_field*>& members, const string & structname) {
+ if( member_mapping_scope != NULL) {
+ if( member_mapping_scope != scope) {
throw "internal error: prepare_member_name_mapping() already active for different struct";
} else {
throw "internal error: prepare_member_name_mapping() already active for this struct";
@@ -2514,30 +2587,23 @@
for (iter = members.begin(); iter != members.end(); ++iter) {
string oldname = (*iter)->get_name();
string newname = prop_name(*iter, true);
- while (true) {
+ while( true) {
// name conflicts with struct (CS0542 error)
- if (structname.compare(newname) == 0) {
- pverbose("struct %s: member %s conflicts with struct (preventing CS0542)\n",
- structname.c_str(),
- newname.c_str());
+ if( structname.compare( newname) == 0) {
+ pverbose("struct %s: member %s conflicts with struct (preventing CS0542)\n", structname.c_str(), newname.c_str());
newname += '_';
}
// new name conflicts with another member
- if (used_member_names.find(newname) != used_member_names.end()) {
- pverbose("struct %s: member %s conflicts with another member\n",
- structname.c_str(),
- newname.c_str());
+ if( used_member_names.find(newname) != used_member_names.end()) {
+ pverbose("struct %s: member %s conflicts with another member\n", structname.c_str(), newname.c_str());
newname += '_';
continue;
}
// add always, this helps us to detect edge cases like
// different spellings ("foo" and "Foo") within the same struct
- pverbose("struct %s: member mapping %s => %s\n",
- structname.c_str(),
- oldname.c_str(),
- newname.c_str());
+ pverbose("struct %s: member mapping %s => %s\n", structname.c_str(), oldname.c_str(), newname.c_str());
member_name_mapping[oldname] = newname;
used_member_names.insert(newname);
break;
@@ -2546,21 +2612,17 @@
}
std::string t_csharp_generator::prop_name(t_field* tfield, bool suppress_mapping) {
- string name(tfield->get_name());
- if (suppress_mapping) {
- name[0] = toupper(name[0]);
- } else {
- name = get_mapped_member_name(name);
- }
- return name;
+ string name (tfield->get_name());
+ if( suppress_mapping) {
+ name[0] = toupper(name[0]);
+ } else {
+ name = get_mapped_member_name( name);
+ }
+ return name;
}
-string t_csharp_generator::type_name(t_type* ttype,
- bool in_container,
- bool in_init,
- bool in_param,
- bool is_required) {
- (void)in_init;
+string t_csharp_generator::type_name(t_type* ttype, bool in_container, bool in_init, bool in_param, bool is_required) {
+ (void) in_init;
while (ttype->is_typedef()) {
ttype = ((t_typedef*)ttype)->get_type();
}
@@ -2568,14 +2630,14 @@
if (ttype->is_base_type()) {
return base_type_name((t_base_type*)ttype, in_container, in_param, is_required);
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
- return "Dictionary<" + type_name(tmap->get_key_type(), true) + ", "
- + type_name(tmap->get_val_type(), true) + ">";
+ t_map *tmap = (t_map*) ttype;
+ return "Dictionary<" + type_name(tmap->get_key_type(), true) +
+ ", " + type_name(tmap->get_val_type(), true) + ">";
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
+ t_set* tset = (t_set*) ttype;
return "THashSet<" + type_name(tset->get_elem_type(), true) + ">";
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
+ t_list* tlist = (t_list*) ttype;
return "List<" + type_name(tlist->get_elem_type(), true) + ">";
}
@@ -2591,35 +2653,32 @@
return normalize_name(ttype->get_name()) + postfix;
}
-string t_csharp_generator::base_type_name(t_base_type* tbase,
- bool in_container,
- bool in_param,
- bool is_required) {
- (void)in_container;
+string t_csharp_generator::base_type_name(t_base_type* tbase, bool in_container, bool in_param, bool is_required) {
+ (void) in_container;
string postfix = (!is_required && nullable_ && in_param) ? "?" : "";
switch (tbase->get_base()) {
- case t_base_type::TYPE_VOID:
- return "void";
- case t_base_type::TYPE_STRING:
- if (tbase->is_binary()) {
- return "byte[]";
- } else {
- return "string";
- }
- case t_base_type::TYPE_BOOL:
- return "bool" + postfix;
- case t_base_type::TYPE_BYTE:
- return "sbyte" + postfix;
- case t_base_type::TYPE_I16:
- return "short" + postfix;
- case t_base_type::TYPE_I32:
- return "int" + postfix;
- case t_base_type::TYPE_I64:
- return "long" + postfix;
- case t_base_type::TYPE_DOUBLE:
- return "double" + postfix;
- default:
- throw "compiler error: no C# name for base type " + t_base_type::t_base_name(tbase->get_base());
+ case t_base_type::TYPE_VOID:
+ return "void";
+ case t_base_type::TYPE_STRING:
+ if (tbase->is_binary()) {
+ return "byte[]";
+ } else {
+ return "string";
+ }
+ case t_base_type::TYPE_BOOL:
+ return "bool" + postfix;
+ case t_base_type::TYPE_BYTE:
+ return "sbyte" + postfix;
+ case t_base_type::TYPE_I16:
+ return "short" + postfix;
+ case t_base_type::TYPE_I32:
+ return "int" + postfix;
+ case t_base_type::TYPE_I64:
+ return "long" + postfix;
+ case t_base_type::TYPE_DOUBLE:
+ return "double" + postfix;
+ default:
+ throw "compiler error: no C# name for base type " + t_base_type::t_base_name(tbase->get_base());
}
}
@@ -2636,23 +2695,23 @@
} else if (ttype->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- result += " = null";
- break;
- case t_base_type::TYPE_BOOL:
- result += " = false";
- break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- result += " = 0";
- break;
- case t_base_type::TYPE_DOUBLE:
- result += " = (double)0";
- break;
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ result += " = null";
+ break;
+ case t_base_type::TYPE_BOOL:
+ result += " = false";
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ result += " = 0";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ result += " = (double)0";
+ break;
}
} else if (ttype->is_enum()) {
result += " = (" + type_name(ttype, false, true) + ")0";
@@ -2667,32 +2726,28 @@
string t_csharp_generator::function_signature(t_function* tfunction, string prefix) {
t_type* ttype = tfunction->get_returntype();
- return type_name(ttype) + " " + normalize_name(prefix + tfunction->get_name()) + "("
- + argument_list(tfunction->get_arglist()) + ")";
+ return type_name(ttype) + " " + normalize_name(prefix + tfunction->get_name()) + "(" + argument_list(tfunction->get_arglist()) + ")";
}
string t_csharp_generator::function_signature_async_begin(t_function* tfunction, string prefix) {
string comma = (tfunction->get_arglist()->get_members().size() > 0 ? ", " : "");
- return "IAsyncResult " + normalize_name(prefix + tfunction->get_name())
- + "(AsyncCallback callback, object state" + comma + argument_list(tfunction->get_arglist())
- + ")";
+ return "IAsyncResult " + normalize_name(prefix + tfunction->get_name()) + "(AsyncCallback callback, object state" + comma + argument_list(tfunction->get_arglist()) + ")";
}
string t_csharp_generator::function_signature_async_end(t_function* tfunction, string prefix) {
t_type* ttype = tfunction->get_returntype();
- return type_name(ttype) + " " + normalize_name(prefix + tfunction->get_name())
- + "(IAsyncResult asyncResult)";
+ return type_name(ttype) + " " + normalize_name(prefix + tfunction->get_name()) + "(IAsyncResult asyncResult)";
}
string t_csharp_generator::function_signature_async(t_function* tfunction, string prefix) {
t_type* ttype = tfunction->get_returntype();
string task = "Task";
- if (!ttype->is_void())
+ if( ! ttype->is_void())
task += "<" + type_name(ttype) + ">";
- return task + " " + normalize_name(prefix + tfunction->get_name()) + "Async("
- + argument_list(tfunction->get_arglist()) + ")";
+ return task + " " + normalize_name(prefix + tfunction->get_name()) + "Async(" + argument_list(tfunction->get_arglist()) + ")";
}
+
string t_csharp_generator::argument_list(t_struct* tstruct) {
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@@ -2717,22 +2772,22 @@
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 "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- return "TType.String";
- case t_base_type::TYPE_BOOL:
- return "TType.Bool";
- case t_base_type::TYPE_BYTE:
- return "TType.Byte";
- case t_base_type::TYPE_I16:
- return "TType.I16";
- case t_base_type::TYPE_I32:
- return "TType.I32";
- case t_base_type::TYPE_I64:
- return "TType.I64";
- case t_base_type::TYPE_DOUBLE:
- return "TType.Double";
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "TType.String";
+ case t_base_type::TYPE_BOOL:
+ return "TType.Bool";
+ case t_base_type::TYPE_BYTE:
+ return "TType.Byte";
+ case t_base_type::TYPE_I16:
+ return "TType.I16";
+ case t_base_type::TYPE_I32:
+ return "TType.I32";
+ case t_base_type::TYPE_I64:
+ return "TType.I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "TType.Double";
}
} else if (type->is_enum()) {
return "TType.I32";
@@ -2749,46 +2804,49 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
-void t_csharp_generator::generate_csharp_docstring_comment(ofstream& out, string contents) {
- generate_docstring_comment(out, "/// <summary>\n", "/// ", contents, "/// </summary>\n");
+void t_csharp_generator::generate_csharp_docstring_comment(ofstream &out, string contents) {
+ generate_docstring_comment(out,
+ "/// <summary>\n",
+ "/// ", contents,
+ "/// </summary>\n");
+
+
}
-void t_csharp_generator::generate_csharp_doc(ofstream& out, t_field* field) {
+void t_csharp_generator::generate_csharp_doc(ofstream &out, t_field* field) {
if (field->get_type()->is_enum()) {
- string combined_message = field->get_doc() + "\n<seealso cref=\""
- + get_enum_class_name(field->get_type()) + "\"/>";
+ string combined_message = field->get_doc() + "\n<seealso cref=\"" + get_enum_class_name(field->get_type()) + "\"/>";
generate_csharp_docstring_comment(out, combined_message);
} else {
generate_csharp_doc(out, (t_doc*)field);
}
}
-void t_csharp_generator::generate_csharp_doc(ofstream& out, t_doc* tdoc) {
+void t_csharp_generator::generate_csharp_doc(ofstream &out, t_doc* tdoc) {
if (tdoc->has_doc()) {
generate_csharp_docstring_comment(out, tdoc->get_doc());
}
}
-void t_csharp_generator::generate_csharp_doc(ofstream& out, t_function* tfunction) {
+void t_csharp_generator::generate_csharp_doc(ofstream &out, t_function* tfunction) {
if (tfunction->has_doc()) {
- stringstream ps;
+ stringstream ps;
const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
vector<t_field*>::const_iterator p_iter;
for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
t_field* p = *p_iter;
ps << "\n<param name=\"" << p->get_name() << "\">";
if (p->has_doc()) {
- std::string str = p->get_doc();
- str.erase(std::remove(str.begin(), str.end(), '\n'),
- str.end()); // remove the newlines that appear from the parser
- ps << str;
+ std::string str = p->get_doc();
+ str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); // remove the newlines that appear from the parser
+ ps << str;
}
- ps << "</param>";
+ ps << "</param>";
}
generate_docstring_comment(out,
"",
"/// ",
- "<summary>\n" + tfunction->get_doc() + "</summary>" + ps.str(),
+ "<summary>\n" + tfunction->get_doc() + "</summary>" + ps.str(),
"");
}
}
@@ -2802,14 +2860,13 @@
return package + type->get_name();
}
-THRIFT_REGISTER_GENERATOR(
- csharp,
- "C#",
- " async: Adds Async support using Task.Run.\n"
- " asyncctp: Adds Async CTP support using TaskEx.Run.\n"
- " wcf: Adds bindings for WCF to generated classes.\n"
- " serial: Add serialization support to generated classes.\n"
- " nullable: Use nullable types for properties.\n"
- " hashcode: Generate a hashcode and equals implementation for classes.\n"
- " union: Use new union typing, which includes a static read function for union "
- "types.\n")
+THRIFT_REGISTER_GENERATOR(csharp, "C#",
+" async: Adds Async support using Task.Run.\n"
+" asyncctp: Adds Async CTP support using TaskEx.Run.\n"
+" wcf: Adds bindings for WCF to generated classes.\n"
+" serial: Add serialization support to generated classes.\n"
+" nullable: Use nullable types for properties.\n"
+" hashcode: Generate a hashcode and equals implementation for classes.\n"
+" union: Use new union typing, which includes a static read function for union types.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_d_generator.cc b/compiler/cpp/src/generate/t_d_generator.cc
index ca76485..622fb29 100644
--- a/compiler/cpp/src/generate/t_d_generator.cc
+++ b/compiler/cpp/src/generate/t_d_generator.cc
@@ -43,7 +43,7 @@
using std::string;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* D code generator.
@@ -54,17 +54,19 @@
* the passed entity.
*/
class t_d_generator : public t_oop_generator {
-public:
- t_d_generator(t_program* program,
- const std::map<string, string>& parsed_options,
- const string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_d_generator(
+ t_program* program,
+ const std::map<string, string>& parsed_options,
+ const string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-d";
}
-protected:
+ protected:
virtual void init_generator() {
// Make output directory
MKDIR(get_out_dir().c_str());
@@ -75,7 +77,7 @@
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
if (!dir.empty()) {
subdir = subdir + "/" + dir;
@@ -89,19 +91,21 @@
f_types_.open(f_types_name.c_str());
// Print header
- f_types_ << autogen_comment() << "module " << render_package(*program_) << program_name_
- << "_types;" << endl << endl;
+ f_types_ <<
+ autogen_comment() <<
+ "module " << render_package(*program_) << program_name_ << "_types;" << endl <<
+ endl;
print_default_imports(f_types_);
// Include type modules from other imported programs.
const vector<t_program*>& includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
- f_types_ << "import " << render_package(*(includes[i])) << includes[i]->get_name()
- << "_types;" << endl;
+ f_types_ <<
+ "import " << render_package(*(includes[i])) <<
+ includes[i]->get_name() << "_types;" << endl;
}
- if (!includes.empty())
- f_types_ << endl;
+ if (!includes.empty()) f_types_ << endl;
}
virtual void close_generator() {
@@ -111,26 +115,32 @@
virtual void generate_consts(std::vector<t_const*> consts) {
if (!consts.empty()) {
- string f_consts_name = package_dir_ + program_name_ + "_constants.d";
+ string f_consts_name = package_dir_+program_name_+"_constants.d";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
- f_consts << autogen_comment() << "module " << render_package(*program_) << program_name_
- << "_constants;" << endl << endl;
+ f_consts <<
+ autogen_comment() <<
+ "module " << render_package(*program_) << program_name_ << "_constants;" << endl
+ << endl;
print_default_imports(f_consts);
- f_consts << "import " << render_package(*get_program()) << program_name_ << "_types;" << endl
- << endl;
+ f_consts <<
+ "import " << render_package(*get_program()) << program_name_ << "_types;" << endl <<
+ endl;
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
string name = (*c_iter)->get_name();
t_type* type = (*c_iter)->get_type();
- indent(f_consts) << "immutable(" << render_type_name(type) << ") " << name << ";" << endl;
+ indent(f_consts) << "immutable(" << render_type_name(type) << ") " <<
+ name << ";" << endl;
}
- f_consts << endl << "static this() {" << endl;
+ f_consts <<
+ endl <<
+ "static this() {" << endl;
indent_up();
bool first = true;
@@ -145,23 +155,27 @@
if (!is_immutable_type(type)) {
f_consts << "cast(immutable(" << render_type_name(type) << ")) ";
}
- f_consts << render_const_value(type, (*c_iter)->get_value()) << ";" << endl;
+ f_consts <<
+ render_const_value(type, (*c_iter)->get_value()) << ";" << endl;
}
indent_down();
- indent(f_consts) << "}" << endl;
+ indent(f_consts) <<
+ "}" << endl;
}
}
virtual void generate_typedef(t_typedef* ttypedef) {
- f_types_ << indent() << "alias " << render_type_name(ttypedef->get_type()) << " "
- << ttypedef->get_symbolic() << ";" << endl << endl;
+ f_types_ <<
+ indent() << "alias " << render_type_name(ttypedef->get_type()) << " " <<
+ ttypedef->get_symbolic() << ";" << endl << endl;
}
virtual void generate_enum(t_enum* tenum) {
vector<t_enum_value*> constants = tenum->get_constants();
string enum_name = tenum->get_name();
- f_types_ << indent() << "enum " << enum_name << " {" << endl;
+ f_types_ <<
+ indent() << "enum " << enum_name << " {" << endl;
indent_up();
@@ -199,17 +213,21 @@
string f_servicename = package_dir_ + svc_name + ".d";
std::ofstream f_service;
f_service.open(f_servicename.c_str());
- f_service << autogen_comment() << "module " << render_package(*program_) << svc_name << ";"
- << endl << endl;
+ f_service <<
+ autogen_comment() <<
+ "module " << render_package(*program_) << svc_name << ";" << endl <<
+ endl;
print_default_imports(f_service);
- f_service << "import " << render_package(*get_program()) << program_name_ << "_types;" << endl;
+ f_service << "import " << render_package(*get_program()) << program_name_ <<
+ "_types;" << endl;
t_service* extends_service = tservice->get_extends();
if (extends_service != NULL) {
- f_service << "import " << render_package(*(extends_service->get_program()))
- << extends_service->get_name() << ";" << endl;
+ f_service <<
+ "import " << render_package(*(extends_service->get_program())) <<
+ extends_service->get_name() << ";" << endl;
}
f_service << endl;
@@ -219,7 +237,8 @@
extends = " : " + render_type_name(tservice->get_extends());
}
- f_service << indent() << "interface " << svc_name << extends << " {" << endl;
+ f_service <<
+ indent() << "interface " << svc_name << extends << " {" << endl;
indent_up();
// Collect all the exception types service methods can throw so we can
@@ -242,14 +261,12 @@
}
// Alias the exception types into the current scope.
- if (!exception_types.empty())
- f_service << endl;
+ if (!exception_types.empty()) f_service << endl;
set<t_type*>::const_iterator et_iter;
for (et_iter = exception_types.begin(); et_iter != exception_types.end(); ++et_iter) {
- indent(f_service) << "alias " << render_package(*(*et_iter)->get_program())
- << (*et_iter)->get_program()->get_name() << "_types"
- << "." << (*et_iter)->get_name() << " " << (*et_iter)->get_name() << ";"
- << endl;
+ indent(f_service) << "alias " << render_package(*(*et_iter)->get_program()) <<
+ (*et_iter)->get_program()->get_name() << "_types" << "." <<
+ (*et_iter)->get_name() << " " << (*et_iter)->get_name() << ";" << endl;
}
// Write the method metadata.
@@ -257,8 +274,9 @@
indent_up();
bool first = true;
for (fn_iter = functions.begin(); fn_iter != functions.end(); ++fn_iter) {
- if ((*fn_iter)->get_arglist()->get_members().empty()
- && (*fn_iter)->get_xceptions()->get_members().empty() && !(*fn_iter)->is_oneway()) {
+ if ((*fn_iter)->get_arglist()->get_members().empty() &&
+ (*fn_iter)->get_xceptions()->get_members().empty() &&
+ !(*fn_iter)->is_oneway()) {
continue;
}
@@ -268,12 +286,13 @@
meta << ",";
}
- meta << endl << indent() << "TMethodMeta(`" << (*fn_iter)->get_name() << "`, " << endl;
+ meta << endl <<
+ indent() << "TMethodMeta(`" << (*fn_iter)->get_name() << "`, " << endl;
indent_up();
indent(meta) << "[";
bool first = true;
- const vector<t_field*>& params = (*fn_iter)->get_arglist()->get_members();
+ const vector<t_field*> ¶ms = (*fn_iter)->get_arglist()->get_members();
vector<t_field*>::const_iterator p_iter;
for (p_iter = params.begin(); p_iter != params.end(); ++p_iter) {
if (first) {
@@ -293,11 +312,14 @@
meta << "]";
- if (!(*fn_iter)->get_xceptions()->get_members().empty() || (*fn_iter)->is_oneway()) {
- meta << "," << endl << indent() << "[";
+ if (!(*fn_iter)->get_xceptions()->get_members().empty() ||
+ (*fn_iter)->is_oneway()) {
+ meta << "," << endl <<
+ indent() << "[";
bool first = true;
- const vector<t_field*>& exceptions = (*fn_iter)->get_xceptions()->get_members();
+ const vector<t_field*>& exceptions =
+ (*fn_iter)->get_xceptions()->get_members();
vector<t_field*>::const_iterator ex_iter;
for (ex_iter = exceptions.begin(); ex_iter != exceptions.end(); ++ex_iter) {
if (first) {
@@ -306,31 +328,36 @@
meta << ", ";
}
- meta << "TExceptionMeta(`" << (*ex_iter)->get_name() << "`, " << (*ex_iter)->get_key()
- << ", `" << (*ex_iter)->get_type()->get_name() << "`)";
+ meta << "TExceptionMeta(`" << (*ex_iter)->get_name() <<
+ "`, " << (*ex_iter)->get_key() << ", `" <<
+ (*ex_iter)->get_type()->get_name() << "`)";
}
meta << "]";
}
if ((*fn_iter)->is_oneway()) {
- meta << "," << endl << indent() << "TMethodType.ONEWAY";
+ meta << "," << endl <<
+ indent() << "TMethodType.ONEWAY";
}
indent_down();
- meta << endl << indent() << ")";
+ meta << endl <<
+ indent() << ")";
}
indent_down();
string meta_str(meta.str());
if (!meta_str.empty()) {
- f_service << endl << indent() << "enum methodMeta = [" << meta_str << endl << indent() << "];"
- << endl;
+ f_service << endl <<
+ indent() << "enum methodMeta = [" << meta_str << endl <<
+ indent() << "];" << endl;
}
indent_down();
indent(f_service) << "}" << endl;
+
// Server skeleton generation.
string f_skeletonname = package_dir_ + svc_name + "_server.skeleton.d";
std::ofstream f_skeleton;
@@ -339,68 +366,89 @@
f_skeleton.close();
}
-private:
- /**
- * Writes a server skeleton for the passed service to out.
- */
- void print_server_skeleton(ostream& out, t_service* tservice) {
- string svc_name = tservice->get_name();
+ private:
+ /**
+ * Writes a server skeleton for the passed service to out.
+ */
+ void print_server_skeleton(ostream &out, t_service* tservice) {
+ string svc_name = tservice->get_name();
- out << "/*" << endl
- << " * This auto-generated skeleton file illustrates how to build a server. If you" << endl
- << " * intend to customize it, you should edit a copy with another file name to " << endl
- << " * avoid overwriting it when running the generator again." << endl << " */" << endl
- << "module " << render_package(*tservice->get_program()) << svc_name << "_server;" << endl
- << endl << "import std.stdio;" << endl << "import thrift.codegen.processor;" << endl
- << "import thrift.protocol.binary;" << endl << "import thrift.server.simple;" << endl
- << "import thrift.server.transport.socket;" << endl << "import thrift.transport.buffered;"
- << endl << "import thrift.util.hashset;" << endl << endl << "import "
- << render_package(*tservice->get_program()) << svc_name << ";" << endl << "import "
- << render_package(*get_program()) << program_name_ << "_types;" << endl << endl << endl
- << "class " << svc_name << "Handler : " << svc_name << " {" << endl;
-
- indent_up();
- out << indent() << "this() {" << endl << indent() << " // Your initialization goes here."
- << endl << indent() << "}" << endl << endl;
-
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- out << indent();
- print_function_signature(out, *f_iter);
- out << " {" << endl;
+ out <<
+ "/*" << endl <<
+ " * This auto-generated skeleton file illustrates how to build a server. If you" << endl <<
+ " * intend to customize it, you should edit a copy with another file name to " << endl <<
+ " * avoid overwriting it when running the generator again." << endl <<
+ " */" << endl <<
+ "module " << render_package(*tservice->get_program()) << svc_name << "_server;" << endl <<
+ endl <<
+ "import std.stdio;" << endl <<
+ "import thrift.codegen.processor;" << endl <<
+ "import thrift.protocol.binary;" << endl <<
+ "import thrift.server.simple;" << endl <<
+ "import thrift.server.transport.socket;" << endl <<
+ "import thrift.transport.buffered;" << endl <<
+ "import thrift.util.hashset;" << endl <<
+ endl <<
+ "import " << render_package(*tservice->get_program()) << svc_name << ";" << endl <<
+ "import " << render_package(*get_program()) << program_name_ << "_types;" << endl <<
+ endl <<
+ endl <<
+ "class " << svc_name << "Handler : " << svc_name << " {" << endl;
indent_up();
+ out <<
+ indent() << "this() {" << endl <<
+ indent() << " // Your initialization goes here." << endl <<
+ indent() << "}" << endl <<
+ endl;
- out << indent() << "// Your implementation goes here." << endl << indent() << "writeln(\""
- << (*f_iter)->get_name() << " called\");" << endl;
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ out << indent();
+ print_function_signature(out, *f_iter);
+ out << " {" << endl;
- t_base_type* rt = (t_base_type*)(*f_iter)->get_returntype();
- if (rt->get_base() != t_base_type::TYPE_VOID) {
- indent(out) << "return typeof(return).init;" << endl;
+ indent_up();
+
+ out <<
+ indent() << "// Your implementation goes here." << endl <<
+ indent() << "writeln(\"" << (*f_iter)->get_name() << " called\");" << endl;
+
+ t_base_type* rt = (t_base_type*)(*f_iter)->get_returntype();
+ if (rt->get_base() != t_base_type::TYPE_VOID) {
+ indent(out) << "return typeof(return).init;" << endl;
+ }
+
+ indent_down();
+
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
indent_down();
+ out <<
+ "}" << endl <<
+ endl;
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "void main() {" << endl;
+ indent_up();
+ out <<
+ indent() << "auto protocolFactory = new TBinaryProtocolFactory!();" << endl <<
+ indent() << "auto processor = new TServiceProcessor!" << svc_name << "(new " << svc_name << "Handler);" << endl <<
+ indent() << "auto serverTransport = new TServerSocket(9090);" << endl <<
+ indent() << "auto transportFactory = new TBufferedTransportFactory;" << endl <<
+
+ indent() << "auto server = new TSimpleServer(" << endl <<
+ indent() << " processor, serverTransport, transportFactory, protocolFactory);" << endl <<
+ indent() << "server.serve();" << endl;
+ indent_down();
+ out <<
+ "}" << endl;
}
- indent_down();
- out << "}" << endl << endl;
-
- out << indent() << "void main() {" << endl;
- indent_up();
- out << indent() << "auto protocolFactory = new TBinaryProtocolFactory!();" << endl << indent()
- << "auto processor = new TServiceProcessor!" << svc_name << "(new " << svc_name
- << "Handler);" << endl << indent() << "auto serverTransport = new TServerSocket(9090);"
- << endl << indent() << "auto transportFactory = new TBufferedTransportFactory;" << endl
- << indent() << "auto server = new TSimpleServer(" << endl << indent()
- << " processor, serverTransport, transportFactory, protocolFactory);" << endl << indent()
- << "server.serve();" << endl;
- indent_down();
- out << "}" << endl;
- }
-
/**
* Writes the definition of a struct or an exception type to out.
*/
@@ -417,12 +465,11 @@
// Declare all fields.
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << render_type_name((*m_iter)->get_type()) << " " << (*m_iter)->get_name() << ";"
- << endl;
+ indent(out) << render_type_name((*m_iter)->get_type()) << " " <<
+ (*m_iter)->get_name() << ";" << endl;
}
- if (!members.empty())
- indent(out) << endl;
+ if (!members.empty()) indent(out) << endl;
indent(out) << "mixin TStructHelpers!(";
if (!members.empty()) {
@@ -442,7 +489,8 @@
}
out << endl;
- indent(out) << "TFieldMeta(`" << (*m_iter)->get_name() << "`, " << (*m_iter)->get_key();
+ indent(out) << "TFieldMeta(`" << (*m_iter)->get_name() << "`, " <<
+ (*m_iter)->get_key();
t_const_value* cv = (*m_iter)->get_value();
t_field::e_req req = (*m_iter)->get_req();
@@ -460,7 +508,9 @@
out << ");" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -468,7 +518,8 @@
* method.
*/
void print_function_signature(ostream& out, t_function* fn) {
- out << render_type_name(fn->get_returntype()) << " " << fn->get_name() << "(";
+ out << render_type_name(fn->get_returntype()) <<
+ " " << fn->get_name() << "(";
const vector<t_field*>& fields = fn->get_arglist()->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -479,7 +530,8 @@
} else {
out << ", ";
}
- out << render_type_name((*f_iter)->get_type(), true) << " " << (*f_iter)->get_name();
+ out << render_type_name((*f_iter)->get_type(), true) << " " <<
+ (*f_iter)->get_name();
}
out << ")";
@@ -522,7 +574,8 @@
}
break;
default:
- throw "Compiler error: No const of base type " + t_base_type::t_base_name(tbase);
+ throw "Compiler error: No const of base type " +
+ t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "cast(" << render_type_name(type) << ")" << value->get_integer();
@@ -532,8 +585,8 @@
indent(out) << render_type_name(type) << " v;" << endl;
if (type->is_struct() || type->is_xception()) {
- indent(out) << "v = " << (type->is_xception() ? "new " : "") << render_type_name(type)
- << "();" << endl;
+ indent(out) << "v = " << (type->is_xception() ? "new " : "") <<
+ render_type_name(type) << "();" << endl;
const vector<t_field*>& fields = ((t_struct*)type)->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -547,11 +600,12 @@
}
}
if (field_type == NULL) {
- throw "Type error: " + type->get_name() + " has no field "
- + v_iter->first->get_string();
+ throw "Type error: " + type->get_name() + " has no field " +
+ v_iter->first->get_string();
}
string val = render_const_value(field_type, v_iter->second);
- indent(out) << "v.set!`" << v_iter->first->get_string() << "`(" << val << ");" << endl;
+ indent(out) << "v.set!`" << v_iter->first->get_string() <<
+ "`(" << val << ");" << endl;
}
} else if (type->is_map()) {
t_type* ktype = ((t_map*)type)->get_key_type();
@@ -584,7 +638,8 @@
indent(out) << "v ~= " << val << ";" << endl;
}
} else {
- throw "Compiler error: Invalid type in render_const_value: " + type->get_name();
+ throw "Compiler error: Invalid type in render_const_value: " +
+ type->get_name();
}
indent(out) << "return v;" << endl;
@@ -601,8 +656,7 @@
*/
string render_package(const t_program& program) const {
string package = program.get_namespace("d");
- if (package.size() == 0)
- return "";
+ if (package.size() == 0) return "";
return package + ".";
}
@@ -633,16 +687,17 @@
case t_base_type::TYPE_DOUBLE:
return "double";
default:
- throw "Compiler error: No D type name for base type " + t_base_type::t_base_name(tbase);
+ throw "Compiler error: No D type name for base type " +
+ t_base_type::t_base_name(tbase);
}
}
if (ttype->is_container()) {
- t_container* tcontainer = (t_container*)ttype;
+ t_container* tcontainer = (t_container*) ttype;
if (tcontainer->has_cpp_name()) {
return tcontainer->get_cpp_name();
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
+ t_map* tmap = (t_map*) ttype;
t_type* ktype = tmap->get_key_type();
string name = render_type_name(tmap->get_val_type()) + "[";
@@ -656,10 +711,10 @@
name += "]";
return name;
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
+ t_set* tset = (t_set*) ttype;
return "HashSet!(" + render_type_name(tset->get_elem_type()) + ")";
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
+ t_list* tlist = (t_list*) ttype;
return render_type_name(tlist->get_elem_type()) + "[]";
}
}
@@ -682,11 +737,13 @@
return "TReq.OPTIONAL";
case t_field::T_REQUIRED:
return "TReq.REQUIRED";
- default: {
- std::stringstream ss;
- ss << "Compiler error: Invalid requirement level " << req;
- throw ss.str();
- }
+ default:
+ {
+ std::stringstream ss;
+ ss << "Compiler error: Invalid requirement level " << req;
+ throw ss.str();
+ }
+
}
}
@@ -695,8 +752,11 @@
* module) to f.
*/
void print_default_imports(ostream& out) {
- indent(out) << "import thrift.base;" << endl << "import thrift.codegen.base;" << endl
- << "import thrift.util.hashset;" << endl << endl;
+ indent(out) <<
+ "import thrift.base;" << endl <<
+ "import thrift.codegen.base;" << endl <<
+ "import thrift.util.hashset;" << endl <<
+ endl;
}
/**
@@ -720,3 +780,4 @@
};
THRIFT_REGISTER_GENERATOR(d, "D", "")
+
diff --git a/compiler/cpp/src/generate/t_delphi_generator.cc b/compiler/cpp/src/generate/t_delphi_generator.cc
index 682c18d..1012614 100644
--- a/compiler/cpp/src/generate/t_delphi_generator.cc
+++ b/compiler/cpp/src/generate/t_delphi_generator.cc
@@ -45,375 +45,236 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
-class t_delphi_generator : public t_oop_generator {
-public:
- t_delphi_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
- indent_impl_ = 0;
- has_forward = false;
- has_enum = false;
- has_const = false;
- std::map<std::string, std::string>::const_iterator iter;
+class t_delphi_generator : public t_oop_generator
+{
+ public:
+ t_delphi_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
+ indent_impl_ = 0;
+ has_forward = false;
+ has_enum = false;
+ has_const = false;
+ std::map<std::string, std::string>::const_iterator iter;
- iter = parsed_options.find("ansistr_binary");
- ansistr_binary_ = (iter != parsed_options.end());
- iter = parsed_options.find("register_types");
- register_types_ = (iter != parsed_options.end());
- iter = parsed_options.find("constprefix");
- constprefix_ = (iter != parsed_options.end());
- iter = parsed_options.find("events");
- events_ = (iter != parsed_options.end());
- iter = parsed_options.find("xmldoc");
- xmldoc_ = (iter != parsed_options.end());
+ iter = parsed_options.find("ansistr_binary");
+ ansistr_binary_ = (iter != parsed_options.end());
+ iter = parsed_options.find("register_types");
+ register_types_ = (iter != parsed_options.end());
+ iter = parsed_options.find("constprefix");
+ constprefix_ = (iter != parsed_options.end());
+ iter = parsed_options.find("events");
+ events_ = (iter != parsed_options.end());
+ iter = parsed_options.find("xmldoc");
+ xmldoc_ = (iter != parsed_options.end());
- out_dir_base_ = "gen-delphi";
- escape_.clear();
- escape_['\''] = "''";
- }
- void init_generator();
- void close_generator();
-
- void generate_consts(std::vector<t_const*> consts);
-
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_forward_declaration(t_struct* tstruct);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
- void generate_property(ostream& out, t_field* tfield, bool isPublic, bool is_xception);
- void generate_property_writer_(ostream& out, t_field* tfield, bool isPublic);
-
- void generate_delphi_property(ostream& out,
- bool struct_is_exception,
- t_field* tfield,
- bool isPublic,
- std::string fieldPrefix = "");
- void generate_delphi_isset_reader_definition(ostream& out, t_field* tfield, bool is_xception);
- void generate_delphi_property_reader_definition(ostream& out,
- t_field* tfield,
- bool is_xception_class);
- void generate_delphi_property_writer_definition(ostream& out,
- t_field* tfield,
- bool is_xception_class);
- void generate_delphi_property_reader_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class);
- void generate_delphi_property_writer_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class,
- bool is_union,
- bool is_xception_factory,
- std::string xception_factroy_name);
- void generate_delphi_clear_union_value(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class,
- bool is_union,
- bool is_xception_factory,
- std::string xception_factroy_name);
- void generate_delphi_isset_reader_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception);
- void generate_delphi_struct_writer_impl(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception);
- void generate_delphi_struct_result_writer_impl(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception);
-
- void generate_delphi_struct_tostring_impl(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_x_factory);
-
- void add_delphi_uses_list(string unitname);
-
- void generate_delphi_struct_reader_impl(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception);
- void generate_delphi_create_exception_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception);
-
- bool const_needs_var(t_type* type);
- void print_const_prop(std::ostream& out, string name, t_type* type, t_const_value* value);
- void print_private_field(std::ostream& out, string name, t_type* type, t_const_value* value);
- void print_const_value(std::ostream& vars,
- std::ostream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
- void initialize_field(std::ostream& vars,
- std::ostream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
- void finalize_field(std::ostream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- std::string cls_nm = "");
- std::string render_const_value(std::ostream& local_vars,
- std::ostream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
- void print_const_def_value(std::ostream& vars,
- std::ostream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- std::string cls_nm = "");
- std::string make_constants_classname();
-
- void generate_delphi_struct(t_struct* tstruct, bool is_exception);
- void generate_delphi_struct_impl(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result = false,
- bool is_x_factory = false);
- void print_delphi_struct_type_factory_func(ostream& out, t_struct* tstruct);
- void generate_delphi_struct_type_factory(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result = false,
- bool is_x_factory = false);
- void generate_delphi_struct_type_factory_registration(ostream& out,
- std::string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result = false,
- bool is_x_factory = false);
- void generate_delphi_struct_definition(std::ostream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool in_class = false,
- bool is_result = false,
- bool is_x_factory = false);
- void generate_delphi_struct_reader(std::ostream& out, t_struct* tstruct);
- void generate_delphi_struct_result_writer(std::ostream& out, t_struct* tstruct);
- void generate_delphi_struct_writer(std::ostream& out, t_struct* tstruct);
- void generate_delphi_struct_tostring(std::ostream& out, t_struct* tstruct);
-
- void generate_function_helpers(t_function* tfunction);
- void generate_service_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* function);
-
- void generate_deserialize_field(std::ostream& out,
- bool is_xception,
- t_field* tfield,
- std::string prefix,
- std::ostream& local_vars);
- void generate_deserialize_struct(std::ostream& out,
- t_struct* tstruct,
- std::string name,
- std::string prefix);
- void generate_deserialize_container(ostream& out,
- bool is_xception,
- t_type* ttype,
- string name,
- std::ostream& local_vars);
-
- void generate_deserialize_set_element(std::ostream& out,
- bool is_xception,
- t_set* tset,
- std::string prefix,
- std::ostream& local_vars);
- void generate_deserialize_map_element(std::ostream& out,
- bool is_xception,
- t_map* tmap,
- std::string prefix,
- std::ostream& local_vars);
- void generate_deserialize_list_element(std::ostream& out,
- bool is_xception,
- t_list* list,
- std::string prefix,
- std::ostream& local_vars);
-
- void generate_serialize_field(std::ostream& out,
- bool is_xception,
- t_field* tfield,
- std::string prefix,
- std::ostream& local_vars);
- void generate_serialize_struct(std::ostream& out,
- t_struct* tstruct,
- std::string prefix,
- std::ostream& local_vars);
- void generate_serialize_container(std::ostream& out,
- bool is_xception,
- t_type* ttype,
- std::string prefix,
- std::ostream& local_vars);
- void generate_serialize_map_element(std::ostream& out,
- bool is_xception,
- t_map* tmap,
- std::string iter,
- std::string map,
- std::ostream& local_vars);
- void generate_serialize_set_element(std::ostream& out,
- bool is_xception,
- t_set* tmap,
- std::string iter,
- std::ostream& local_vars);
- void generate_serialize_list_element(std::ostream& out,
- bool is_xception,
- t_list* tlist,
- std::string iter,
- std::ostream& local_vars);
-
- void delphi_type_usings(std::ostream& out);
- std::string delphi_thrift_usings();
-
- std::string type_name(t_type* ttype,
- bool b_cls = false,
- bool b_no_postfix = false,
- bool b_exception_factory = false,
- bool b_full_exception_factory = false);
- std::string normalize_clsnm(std::string name,
- std::string prefix,
- bool b_no_check_keyword = false);
- std::string make_valid_delphi_identifier(std::string const& fromName);
- std::string input_arg_prefix(t_type* ttype);
-
- std::string base_type_name(t_base_type* tbase);
- std::string declare_field(t_field* tfield,
- bool init = false,
- std::string prefix = "",
- bool is_xception_class = false);
- std::string function_signature(t_function* tfunction,
- std::string full_cls = "",
- bool is_xception = false);
- std::string argument_list(t_struct* tstruct);
- std::string constructor_argument_list(t_struct* tstruct, std::string current_indent);
- std::string type_to_enum(t_type* ttype);
- std::string prop_name(t_field* tfield, bool is_xception = false);
- std::string prop_name(std::string name, bool is_xception = false);
- std::string constructor_param_name(string name);
-
- void write_enum(std::string line);
- void write_forward_decr(std::string line);
- void write_const(std::string line);
- void write_struct(std::string line);
- void write_service(std::string line);
-
- virtual std::string autogen_comment() {
- return std::string("(**\n") + " * Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + " *\n" + " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
- + " *)\n";
- }
-
- string replace_all(string contents, string search, string replace);
- string xml_encode(string contents);
- string xmldoc_encode(string contents);
- string xmlattrib_encode(string contents);
- void generate_delphi_doc(std::ostream& out, t_field* field);
- void generate_delphi_doc(std::ostream& out, t_doc* tdoc);
- void generate_delphi_doc(std::ostream& out, t_function* tdoc);
- void generate_delphi_docstring_comment(std::ostream& out, string contents);
-
- bool type_can_be_null(t_type* ttype) {
- while (ttype->is_typedef()) {
- ttype = ((t_typedef*)ttype)->get_type();
+ out_dir_base_ = "gen-delphi";
+ escape_.clear();
+ escape_['\''] = "''";
}
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception();
- }
-private:
- std::string namespace_name_;
- std::ostringstream s_forward_decr;
- std::ostringstream s_enum;
- std::ostringstream s_const;
- std::ostringstream s_struct;
- std::ostringstream s_service;
- std::ostringstream s_const_impl;
- std::ostringstream s_struct_impl;
- std::ostringstream s_service_impl;
- std::ostringstream s_type_factory_registration;
- std::ostringstream s_type_factory_funcs;
- bool has_forward;
- bool has_enum;
- bool has_const;
- std::string namespace_dir_;
- std::map<std::string, int> delphi_keywords;
- std::map<std::string, int> delphi_reserved_method;
- std::map<std::string, int> delphi_reserved_method_exception;
- std::map<std::string, int> types_known;
- std::list<t_typedef*> typedefs_pending;
- std::vector<std::string> uses_list;
- void create_keywords();
- bool find_keyword(std::map<std::string, int>& keyword_map, std::string name);
- std::string normalize_name(std::string name,
- bool b_method = false,
- bool b_exception_method = false);
- std::string empty_value(t_type* type);
- bool is_fully_defined_type(t_type* ttype);
- void add_defined_type(t_type* ttype);
- void init_known_types_list();
- bool is_void(t_type* type);
- int indent_impl_;
- bool ansistr_binary_;
- bool register_types_;
- bool constprefix_;
- bool events_;
- bool xmldoc_;
- void indent_up_impl() { ++indent_impl_; };
- void indent_down_impl() { --indent_impl_; };
- std::string indent_impl() {
- std::string ind = "";
- int i;
- for (i = 0; i < indent_impl_; ++i) {
- ind += " ";
+ void init_generator();
+ void close_generator();
+
+ void generate_consts(std::vector<t_const*> consts);
+
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_forward_declaration(t_struct* tstruct);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+ void generate_property(ostream& out, t_field* tfield, bool isPublic, bool is_xception);
+ void generate_property_writer_(ostream& out, t_field* tfield, bool isPublic);
+
+ void generate_delphi_property(ostream& out, bool struct_is_exception, t_field* tfield, bool isPublic, std::string fieldPrefix = "");
+ void generate_delphi_isset_reader_definition(ostream& out, t_field* tfield, bool is_xception);
+ void generate_delphi_property_reader_definition(ostream& out, t_field* tfield, bool is_xception_class);
+ void generate_delphi_property_writer_definition(ostream& out, t_field* tfield, bool is_xception_class);
+ void generate_delphi_property_reader_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class);
+ void generate_delphi_property_writer_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class, bool is_union, bool is_xception_factory, std::string xception_factroy_name);
+ void generate_delphi_clear_union_value(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class, bool is_union, bool is_xception_factory, std::string xception_factroy_name);
+ void generate_delphi_isset_reader_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception);
+ void generate_delphi_struct_writer_impl(ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception);
+ void generate_delphi_struct_result_writer_impl(ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception);
+
+ void generate_delphi_struct_tostring_impl(ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception, bool is_x_factory);
+
+ void add_delphi_uses_list( string unitname);
+
+ void generate_delphi_struct_reader_impl(ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception);
+ void generate_delphi_create_exception_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception);
+
+ bool const_needs_var(t_type* type);
+ void print_const_prop(std::ostream& out, string name, t_type* type, t_const_value* value);
+ void print_private_field(std::ostream& out, string name, t_type* type, t_const_value* value);
+ void print_const_value ( std::ostream& vars, std::ostream& out, std::string name, t_type* type, t_const_value* value);
+ void initialize_field(std::ostream& vars, std::ostream& out, std::string name, t_type* type, t_const_value* value);
+ void finalize_field(std::ostream& out, std::string name, t_type* type, t_const_value* value, std::string cls_nm = "");
+ std::string render_const_value( std::ostream& local_vars, std::ostream& out, std::string name, t_type* type, t_const_value* value);
+ void print_const_def_value( std::ostream& vars, std::ostream& out, std::string name, t_type* type, t_const_value* value, std::string cls_nm = "");
+ std::string make_constants_classname();
+
+ void generate_delphi_struct(t_struct* tstruct, bool is_exception);
+ void generate_delphi_struct_impl( ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result = false, bool is_x_factory = false);
+ void print_delphi_struct_type_factory_func( ostream& out, t_struct* tstruct);
+ void generate_delphi_struct_type_factory( ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result = false, bool is_x_factory = false);
+ void generate_delphi_struct_type_factory_registration( ostream& out, std::string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result = false, bool is_x_factory = false);
+ void generate_delphi_struct_definition(std::ostream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false, bool is_x_factory = false);
+ void generate_delphi_struct_reader(std::ostream& out, t_struct* tstruct);
+ void generate_delphi_struct_result_writer(std::ostream& out, t_struct* tstruct);
+ void generate_delphi_struct_writer(std::ostream& out, t_struct* tstruct);
+ void generate_delphi_struct_tostring(std::ostream& out, t_struct* tstruct);
+
+ void generate_function_helpers(t_function* tfunction);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* function);
+
+ void generate_deserialize_field (std::ostream& out, bool is_xception, t_field* tfield, std::string prefix, std::ostream& local_vars);
+ void generate_deserialize_struct (std::ostream& out, t_struct* tstruct, std::string name, std::string prefix);
+ void generate_deserialize_container(ostream& out, bool is_xception, t_type* ttype, string name, std::ostream& local_vars);
+
+ void generate_deserialize_set_element (std::ostream& out, bool is_xception, t_set* tset, std::string prefix, std::ostream& local_vars);
+ void generate_deserialize_map_element (std::ostream& out, bool is_xception, t_map* tmap, std::string prefix, std::ostream& local_vars);
+ void generate_deserialize_list_element (std::ostream& out, bool is_xception, t_list* list, std::string prefix, std::ostream& local_vars);
+
+ void generate_serialize_field (std::ostream& out, bool is_xception, t_field* tfield, std::string prefix, std::ostream& local_vars);
+ void generate_serialize_struct (std::ostream& out, t_struct* tstruct, std::string prefix, std::ostream& local_vars);
+ void generate_serialize_container (std::ostream& out, bool is_xception, t_type* ttype, std::string prefix, std::ostream& local_vars);
+ void generate_serialize_map_element (std::ostream& out, bool is_xception, t_map* tmap, std::string iter, std::string map, std::ostream& local_vars);
+ void generate_serialize_set_element (std::ostream& out, bool is_xception, t_set* tmap, std::string iter, std::ostream& local_vars);
+ void generate_serialize_list_element (std::ostream& out, bool is_xception, t_list* tlist, std::string iter, std::ostream& local_vars);
+
+ void delphi_type_usings(std::ostream& out);
+ std::string delphi_thrift_usings();
+
+ std::string type_name( t_type* ttype, bool b_cls=false, bool b_no_postfix=false, bool b_exception_factory=false, bool b_full_exception_factory = false);
+ std::string normalize_clsnm(std::string name, std::string prefix, bool b_no_check_keyword = false);
+ std::string make_valid_delphi_identifier( std::string const & fromName);
+ std::string input_arg_prefix( t_type* ttype);
+
+ std::string base_type_name(t_base_type* tbase);
+ std::string declare_field(t_field* tfield, bool init=false, std::string prefix="", bool is_xception_class = false);
+ std::string function_signature(t_function* tfunction, std::string full_cls="", bool is_xception = false);
+ std::string argument_list(t_struct* tstruct);
+ std::string constructor_argument_list(t_struct* tstruct, std::string current_indent);
+ std::string type_to_enum(t_type* ttype);
+ std::string prop_name(t_field* tfield, bool is_xception = false);
+ std::string prop_name(std::string name, bool is_xception = false);
+ std::string constructor_param_name(string name);
+
+ void write_enum(std::string line);
+ void write_forward_decr(std::string line);
+ void write_const(std::string line);
+ void write_struct(std::string line);
+ void write_service(std::string line);
+
+ virtual std::string autogen_comment() {
+ return
+ std::string("(**\n") +
+ " * Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ " *\n" +
+ " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ " *)\n";
}
- return ind;
- };
- std::ostream& indent_impl(std::ostream& os) { return os << indent_impl(); };
+
+ string replace_all( string contents, string search, string replace);
+ string xml_encode( string contents);
+ string xmldoc_encode( string contents);
+ string xmlattrib_encode( string contents);
+ void generate_delphi_doc (std::ostream& out, t_field* field);
+ void generate_delphi_doc (std::ostream& out, t_doc* tdoc);
+ void generate_delphi_doc (std::ostream& out, t_function* tdoc);
+ void generate_delphi_docstring_comment (std::ostream &out, string contents);
+
+ bool type_can_be_null(t_type* ttype) {
+ while (ttype->is_typedef()) {
+ ttype = ((t_typedef*)ttype)->get_type();
+ }
+
+ return ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception();
+ }
+
+ private:
+ std::string namespace_name_;
+ std::ostringstream s_forward_decr;
+ std::ostringstream s_enum;
+ std::ostringstream s_const;
+ std::ostringstream s_struct;
+ std::ostringstream s_service;
+ std::ostringstream s_const_impl;
+ std::ostringstream s_struct_impl;
+ std::ostringstream s_service_impl;
+ std::ostringstream s_type_factory_registration;
+ std::ostringstream s_type_factory_funcs;
+ bool has_forward;
+ bool has_enum;
+ bool has_const;
+ std::string namespace_dir_;
+ std::map<std::string, int> delphi_keywords;
+ std::map<std::string, int> delphi_reserved_method;
+ std::map<std::string, int> delphi_reserved_method_exception;
+ std::map<std::string, int> types_known;
+ std::list<t_typedef*> typedefs_pending;
+ std::vector<std::string> uses_list;
+ void create_keywords();
+ bool find_keyword( std::map<std::string, int>& keyword_map, std::string name);
+ std::string normalize_name( std::string name, bool b_method = false, bool b_exception_method = false);
+ std::string empty_value(t_type* type);
+ bool is_fully_defined_type( t_type* ttype);
+ void add_defined_type( t_type* ttype);
+ void init_known_types_list();
+ bool is_void( t_type* type );
+ int indent_impl_;
+ bool ansistr_binary_;
+ bool register_types_;
+ bool constprefix_;
+ bool events_;
+ bool xmldoc_;
+ void indent_up_impl(){
+ ++indent_impl_;
+ };
+ void indent_down_impl() {
+ --indent_impl_;
+ };
+ std::string indent_impl() {
+ std::string ind = "";
+ int i;
+ for (i = 0; i < indent_impl_; ++i) {
+ ind += " ";
+ }
+ return ind;
+ };
+ std::ostream& indent_impl(std::ostream &os) {
+ return os << indent_impl();
+ };
};
-string t_delphi_generator::replace_all(string contents, string search, string repl) {
- string str(contents);
+string t_delphi_generator::replace_all( string contents, string search, string repl) {
+ string str( contents);
size_t slen = search.length();
size_t rlen = repl.length();
size_t incr = (rlen > 0) ? rlen : 1;
- if (slen > 0) {
+ if( slen > 0) {
size_t found = str.find(search);
- while ((found != string::npos) && (found < str.length())) {
- str.replace(found, slen, repl);
- found = str.find(search, found + incr);
+ while( (found != string::npos) && (found < str.length())) {
+ str.replace( found, slen, repl);
+ found = str.find(search, found+incr);
}
}
@@ -421,56 +282,57 @@
}
// XML encoding
-string t_delphi_generator::xml_encode(string contents) {
- string str(contents);
+string t_delphi_generator::xml_encode( string contents) {
+ string str( contents);
// escape the escape
- str = replace_all(str, "&", "&");
+ str = replace_all( str, "&", "&");
// other standard XML entities
- str = replace_all(str, "<", "<");
- str = replace_all(str, ">", ">");
+ str = replace_all( str, "<", "<");
+ str = replace_all( str, ">", ">");
return str;
}
// XML attribute encoding
-string t_delphi_generator::xmlattrib_encode(string contents) {
- string str(xml_encode(contents));
+string t_delphi_generator::xmlattrib_encode( string contents) {
+ string str( xml_encode( contents));
// our attribs are enclosed in "
- str = replace_all(str, "\"", "\\\"");
+ str = replace_all( str, "\"", "\\\"");
return str;
}
// XML encoding for doc comments
-string t_delphi_generator::xmldoc_encode(string contents) {
- string str(xml_encode(contents));
+string t_delphi_generator::xmldoc_encode( string contents) {
+ string str( xml_encode( contents));
// XMLDoc specific: convert linebreaks into <para>graphs</para>
- str = replace_all(str, "\r\n", "\r");
- str = replace_all(str, "\n", "\r");
- str = replace_all(str, "\r", "</para>\n<para>");
+ str = replace_all( str, "\r\n", "\r");
+ str = replace_all( str, "\n", "\r");
+ str = replace_all( str, "\r", "</para>\n<para>");
return str;
}
-void t_delphi_generator::generate_delphi_docstring_comment(ostream& out, string contents) {
- if (xmldoc_) {
+void t_delphi_generator::generate_delphi_docstring_comment(ostream &out, string contents) {
+ if( xmldoc_) {
generate_docstring_comment(out,
"{$REGION 'XMLDoc'}/// <summary>\n",
- "/// ",
- "<para>" + contents + "</para>",
+ "/// ", "<para>" + contents + "</para>",
"/// </summary>\n{$ENDREGION}\n");
}
}
-void t_delphi_generator::generate_delphi_doc(ostream& out, t_field* field) {
- if (xmldoc_) {
+void t_delphi_generator::generate_delphi_doc(ostream &out, t_field* field) {
+ if( xmldoc_) {
if (field->get_type()->is_enum()) {
- string combined_message = xmldoc_encode(field->get_doc()) + "\n<seealso cref=\""
- + xmldoc_encode(type_name(field->get_type())) + "\"/>";
+ string combined_message = xmldoc_encode( field->get_doc())
+ + "\n<seealso cref=\""
+ + xmldoc_encode( type_name(field->get_type()))
+ + "\"/>";
generate_delphi_docstring_comment(out, combined_message);
} else {
generate_delphi_doc(out, (t_doc*)field);
@@ -478,24 +340,23 @@
}
}
-void t_delphi_generator::generate_delphi_doc(ostream& out, t_doc* tdoc) {
+void t_delphi_generator::generate_delphi_doc(ostream &out, t_doc* tdoc) {
if (tdoc->has_doc() && xmldoc_) {
- generate_delphi_docstring_comment(out, xmldoc_encode(tdoc->get_doc()));
+ generate_delphi_docstring_comment(out, xmldoc_encode( tdoc->get_doc()));
}
}
-void t_delphi_generator::generate_delphi_doc(ostream& out, t_function* tfunction) {
+void t_delphi_generator::generate_delphi_doc(ostream &out, t_function* tfunction) {
if (tfunction->has_doc() && xmldoc_) {
stringstream ps;
const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
vector<t_field*>::const_iterator p_iter;
for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
t_field* p = *p_iter;
- ps << "\n<param name=\"" << xmlattrib_encode(p->get_name()) << "\">";
+ ps << "\n<param name=\"" << xmlattrib_encode( p->get_name()) << "\">";
if (p->has_doc()) {
std::string str = p->get_doc();
- str.erase(std::remove(str.begin(), str.end(), '\n'),
- str.end()); // remove the newlines that appear from the parser
+ str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); // remove the newlines that appear from the parser
ps << xmldoc_encode(str);
}
ps << "</param>";
@@ -503,43 +364,40 @@
generate_docstring_comment(out,
"{$REGION 'XMLDoc'}",
"/// ",
- "<summary><para>" + xmldoc_encode(tfunction->get_doc())
- + "</para></summary>" + ps.str(),
+ "<summary><para>" + xmldoc_encode(tfunction->get_doc()) + "</para></summary>" + ps.str(),
"{$ENDREGION}\n");
}
}
-bool t_delphi_generator::find_keyword(std::map<std::string, int>& keyword_map, std::string name) {
+bool t_delphi_generator::find_keyword( std::map<std::string, int>& keyword_map, std::string name) {
int len = name.length();
- if (len <= 0) {
+ if ( len <= 0 ) {
return false;
}
int nlast = name.find_last_of('_');
- if (nlast >= 1) {
+ if ( nlast >= 1) {
if (nlast == (len - 1)) {
- string new_name(name, 0, nlast);
- return find_keyword(keyword_map, new_name);
+ string new_name( name, 0, nlast);
+ return find_keyword( keyword_map, new_name);
}
}
return (keyword_map[name] == 1);
}
-std::string t_delphi_generator::normalize_name(std::string name,
- bool b_method,
- bool b_exception_method) {
- string tmp(name);
+std::string t_delphi_generator::normalize_name( std::string name, bool b_method, bool b_exception_method) {
+ string tmp( name );
std::transform(tmp.begin(), tmp.end(), tmp.begin(), static_cast<int (*)(int)>(std::tolower));
bool b_found = false;
- if (find_keyword(delphi_keywords, tmp)) {
+ if ( find_keyword( delphi_keywords, tmp) ) {
b_found = true;
- } else if (b_method && find_keyword(delphi_reserved_method, tmp)) {
+ } else if ( b_method && find_keyword( delphi_reserved_method, tmp)) {
b_found = true;
- } else if (b_exception_method && find_keyword(delphi_reserved_method_exception, tmp)) {
+ } else if ( b_exception_method && find_keyword( delphi_reserved_method_exception, tmp)) {
b_found = true;
}
@@ -684,17 +542,17 @@
delphi_reserved_method_exception["throwouterexception"] = 1;
}
-void t_delphi_generator::add_delphi_uses_list(string unitname) {
+void t_delphi_generator::add_delphi_uses_list( string unitname){
vector<std::string>::const_iterator s_iter;
bool found = false;
for (s_iter = uses_list.begin(); s_iter != uses_list.end(); ++s_iter) {
- if ((*s_iter) == unitname) {
+ if ((*s_iter) == unitname ) {
found = true;
break;
}
}
- if (!found) {
- uses_list.push_back(unitname);
+ if (! found) {
+ uses_list.push_back( unitname );
}
}
@@ -714,7 +572,8 @@
add_delphi_uses_list("Thrift.Protocol");
add_delphi_uses_list("Thrift.Transport");
- if (register_types_) {
+ if (register_types_)
+ {
add_delphi_uses_list("Thrift.TypeRegistry");
}
@@ -725,37 +584,38 @@
for (size_t i = 0; i < includes.size(); ++i) {
unitname = includes[i]->get_name();
nsname = includes[i]->get_namespace("delphi");
- if ("" != nsname) {
+ if ( "" != nsname) {
unitname = nsname;
}
add_delphi_uses_list(unitname);
}
+
MKDIR(get_out_dir().c_str());
}
void t_delphi_generator::close_generator() {
std::string unitname = program_name_;
- if ("" != namespace_name_) {
+ if( "" != namespace_name_) {
unitname = namespace_name_;
}
- for (int i = 0; i < (int)unitname.size(); i++) {
- if (unitname[i] == ' ') {
- unitname.replace(i, 1, "_");
+ for ( int i = 0; i < (int)unitname.size(); i++) {
+ if ( unitname[i] == ' ' ) {
+ unitname.replace( i, 1, "_" );
}
}
std::string f_name = get_out_dir() + "/" + unitname + ".pas";
std::ofstream f_all;
- f_all.open(f_name.c_str());
+ f_all.open( f_name.c_str() );
f_all << autogen_comment() << endl;
generate_delphi_doc(f_all, program_);
f_all << "unit " << unitname << ";" << endl << endl;
f_all << "interface" << endl << endl;
- f_all << "uses" << endl;
+ f_all << "uses" << endl;
indent_up();
@@ -772,97 +632,89 @@
indent_down();
- string tmp_unit(unitname);
- for (int i = 0; i < (int)tmp_unit.size(); i++) {
- if (tmp_unit[i] == '.') {
- tmp_unit.replace(i, 1, "_");
+ string tmp_unit( unitname );
+ for ( int i = 0; i < (int)tmp_unit.size(); i++) {
+ if ( tmp_unit[i] == '.' ) {
+ tmp_unit.replace( i, 1, "_" );
}
}
- f_all << "const" << endl;
+ f_all << "const" << endl;
indent_up();
- indent(f_all) << "c" << tmp_unit
- << "_Option_AnsiStr_Binary = " << (ansistr_binary_ ? "True" : "False") << ";"
- << endl;
- indent(f_all) << "c" << tmp_unit
- << "_Option_Register_Types = " << (register_types_ ? "True" : "False") << ";"
- << endl;
- indent(f_all) << "c" << tmp_unit
- << "_Option_ConstPrefix = " << (constprefix_ ? "True" : "False") << ";" << endl;
- indent(f_all) << "c" << tmp_unit << "_Option_Events = " << (events_ ? "True" : "False")
- << ";" << endl;
- indent(f_all) << "c" << tmp_unit << "_Option_XmlDoc = " << (xmldoc_ ? "True" : "False")
- << ";" << endl;
+ indent(f_all) << "c" << tmp_unit << "_Option_AnsiStr_Binary = " << ( ansistr_binary_ ? "True" : "False") << ";" << endl;
+ indent(f_all) << "c" << tmp_unit << "_Option_Register_Types = " << ( register_types_ ? "True" : "False") << ";" << endl;
+ indent(f_all) << "c" << tmp_unit << "_Option_ConstPrefix = " << ( constprefix_ ? "True" : "False") << ";" << endl;
+ indent(f_all) << "c" << tmp_unit << "_Option_Events = " << ( events_ ? "True" : "False") << ";" << endl;
+ indent(f_all) << "c" << tmp_unit << "_Option_XmlDoc = " << ( xmldoc_ ? "True" : "False") << ";" << endl;
indent_down();
- f_all << endl;
- f_all << "type" << endl;
- if (has_forward) {
- f_all << s_forward_decr.str() << endl;
+ f_all << endl;
+ f_all << "type" << endl;
+ if(has_forward) {
+ f_all << s_forward_decr.str() << endl;
}
if (has_enum) {
indent(f_all) << endl;
indent(f_all) << "{$SCOPEDENUMS ON}" << endl << endl;
- f_all << s_enum.str();
+ f_all << s_enum.str();
indent(f_all) << "{$SCOPEDENUMS OFF}" << endl << endl;
}
- f_all << s_struct.str();
- f_all << s_service.str();
- f_all << s_const.str();
- f_all << "implementation" << endl << endl;
- f_all << s_struct_impl.str();
- f_all << s_service_impl.str();
- f_all << s_const_impl.str();
+ f_all << s_struct.str();
+ f_all << s_service.str();
+ f_all << s_const.str();
+ f_all << "implementation" << endl << endl;
+ f_all << s_struct_impl.str();
+ f_all << s_service_impl.str();
+ f_all << s_const_impl.str();
- if (register_types_) {
- f_all << endl;
- f_all << "// Type factory methods and registration" << endl;
- f_all << s_type_factory_funcs.str();
+
+ if (register_types_)
+ {
+ f_all << endl;
+ f_all << "// Type factory methods and registration" << endl;
+ f_all << s_type_factory_funcs.str();
f_all << "procedure RegisterTypeFactories;" << endl;
f_all << "begin" << endl;
f_all << s_type_factory_registration.str();
f_all << "end;" << endl;
}
- f_all << endl;
+ f_all << endl;
string constants_class = make_constants_classname();
- f_all << "initialization" << endl;
- if (has_const) {
- f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
- f_all << " " << constants_class.c_str() << "_Initialize;" << endl;
- f_all << "{$IFEND}" << endl;
+ f_all << "initialization" << endl;
+ if ( has_const ) {
+ f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
+ f_all << " " << constants_class.c_str() << "_Initialize;" << endl;
+ f_all << "{$IFEND}" << endl;
}
if (register_types_) {
f_all << " RegisterTypeFactories;" << endl;
}
- f_all << endl;
+ f_all << endl;
- f_all << "finalization" << endl;
- if (has_const) {
- f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
- f_all << " " << constants_class.c_str() << "_Finalize;" << endl;
- f_all << "{$IFEND}" << endl;
+ f_all << "finalization" << endl;
+ if ( has_const ) {
+ f_all << "{$IF CompilerVersion < 21.0} // D2010" << endl;
+ f_all << " " << constants_class.c_str() << "_Finalize;" << endl;
+ f_all << "{$IFEND}" << endl;
}
- f_all << endl << endl;
+ f_all << endl << endl;
- f_all << "end." << endl;
+ f_all << "end." << endl;
f_all.close();
- if (!typedefs_pending.empty()) {
+ if( ! typedefs_pending.empty()) {
pwarning(0, "%d typedefs with unresolved type references left:\n", typedefs_pending.size());
- for (std::list<t_typedef*>::iterator iter = typedefs_pending.begin();
- typedefs_pending.end() != iter;
- ++iter) {
+ for( std::list<t_typedef*>::iterator iter = typedefs_pending.begin(); typedefs_pending.end() != iter; ++iter) {
pwarning(0, "- %s\n", (*iter)->get_symbolic().c_str());
}
}
}
-void t_delphi_generator::delphi_type_usings(ostream& out) {
+void t_delphi_generator::delphi_type_usings( ostream& out) {
indent_up();
- indent(out) << "Classes, SysUtils, Generics.Collections, Thrift.Collections, Thrift.Protocol,"
- << endl;
+ indent(out) << "Classes, SysUtils, Generics.Collections, Thrift.Collections, Thrift.Protocol," << endl;
indent(out) << "Thrift.Transport;" << endl << endl;
indent_down();
}
@@ -875,8 +727,8 @@
string what = tstruct->is_xception() ? "class" : "interface";
indent_up();
- indent(s_forward_decr) << type_name(tstruct, tstruct->is_xception(), true) << " = " << what << ";"
- << endl;
+ indent(s_forward_decr) <<
+ type_name(tstruct,tstruct->is_xception(),true) << " = " << what << ";" << endl;
indent_down();
add_defined_type(tstruct);
@@ -886,77 +738,80 @@
t_type* type = ttypedef->get_type();
// write now or save for later?
- if (!is_fully_defined_type(type)) {
+ if( ! is_fully_defined_type( type)) {
pverbose("typedef %s: unresolved dependencies found\n", type_name(ttypedef).c_str());
- typedefs_pending.push_back(ttypedef);
+ typedefs_pending.push_back( ttypedef);
return;
}
indent_up();
generate_delphi_doc(s_struct, ttypedef);
- indent(s_struct) << type_name(ttypedef) << " = ";
+ indent(s_struct) <<
+ type_name(ttypedef) << " = ";
// commented out: the benefit is not big enough to risk breaking existing code
- // bool container = type->is_list() || type->is_map() || type->is_set();
- // if( ! container)
+ //bool container = type->is_list() || type->is_map() || type->is_set();
+ //if( ! container)
// s_struct << "type "; //the "type A = type B" syntax leads to E2574 with generics
- s_struct << type_name(ttypedef->get_type()) << ";" << endl << endl;
+ s_struct << type_name(ttypedef->get_type()) << ";" << endl <<
+ endl;
indent_down();
- add_defined_type(ttypedef);
+ add_defined_type( ttypedef);
}
-bool t_delphi_generator::is_fully_defined_type(t_type* ttype) {
- if ((NULL != ttype->get_program()) && (ttype->get_program() != program_)) {
+bool t_delphi_generator::is_fully_defined_type( t_type* ttype) {
+ if( (NULL != ttype->get_program()) && (ttype->get_program() != program_)) {
t_scope* scope = ttype->get_program()->scope();
- if (NULL != scope->get_type(ttype->get_name())) {
- // printf("type %s found in included scope %s\n", ttype->get_name().c_str(),
- // ttype->get_program()->get_name().c_str());
+ if( NULL != scope->get_type( ttype->get_name())) {
+ //printf("type %s found in included scope %s\n", ttype->get_name().c_str(), ttype->get_program()->get_name().c_str());
return true;
}
}
if (ttype->is_typedef()) {
- return (1 == types_known[type_name(ttype)]);
+ return (1 == types_known[ type_name(ttype)]);
}
if (ttype->is_base_type()) {
- return (1 == types_known[base_type_name((t_base_type*)ttype)]);
+ return (1 == types_known[ base_type_name((t_base_type*)ttype)]);
} else if (ttype->is_enum()) {
- return true; // enums are written first, before all other types
+ return true; // enums are written first, before all other types
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
- return is_fully_defined_type(tmap->get_key_type())
- && is_fully_defined_type(tmap->get_val_type());
+ t_map *tmap = (t_map*) ttype;
+ return is_fully_defined_type( tmap->get_key_type()) &&
+ is_fully_defined_type( tmap->get_val_type());
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
- return is_fully_defined_type(tset->get_elem_type());
+ t_set* tset = (t_set*) ttype;
+ return is_fully_defined_type( tset->get_elem_type());
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
- return is_fully_defined_type(tlist->get_elem_type());
+ t_list* tlist = (t_list*) ttype;
+ return is_fully_defined_type( tlist->get_elem_type());
}
- return (1 == types_known[type_name(ttype)]);
+ return (1 == types_known[ type_name(ttype)]);
}
-void t_delphi_generator::add_defined_type(t_type* ttype) {
+void t_delphi_generator::add_defined_type( t_type* ttype) {
// mark as known type
- types_known[type_name(ttype)] = 1;
+ types_known[ type_name(ttype)] = 1;
// check all pending typedefs
- std::list<t_typedef*>::iterator iter;
+ std::list<t_typedef*>::iterator iter;
bool more = true;
- while (more && (!typedefs_pending.empty())) {
+ while( more && (! typedefs_pending.empty()))
+ {
more = false;
- for (iter = typedefs_pending.begin(); typedefs_pending.end() != iter; ++iter) {
+ for( iter = typedefs_pending.begin(); typedefs_pending.end() != iter; ++iter)
+ {
t_typedef* ttypedef = (*iter);
- if (is_fully_defined_type(ttypedef->get_type())) {
- pverbose("typedef %s: all pending references are now resolved\n",
- type_name(ttypedef).c_str());
- typedefs_pending.erase(iter);
- generate_typedef(ttypedef);
+ if( is_fully_defined_type( ttypedef->get_type()))
+ {
+ pverbose("typedef %s: all pending references are now resolved\n", type_name(ttypedef).c_str());
+ typedefs_pending.erase( iter);
+ generate_typedef( ttypedef);
more = true;
break;
}
@@ -966,22 +821,22 @@
void t_delphi_generator::init_known_types_list() {
// known base types
- types_known[type_name(g_type_string)] = 1;
- types_known[type_name(g_type_binary)] = 1;
- types_known[type_name(g_type_bool)] = 1;
- types_known[type_name(g_type_byte)] = 1;
- types_known[type_name(g_type_i16)] = 1;
- types_known[type_name(g_type_i32)] = 1;
- types_known[type_name(g_type_i64)] = 1;
- types_known[type_name(g_type_double)] = 1;
+ types_known[ type_name( g_type_string)] = 1;
+ types_known[ type_name( g_type_binary)] = 1;
+ types_known[ type_name( g_type_bool)] = 1;
+ types_known[ type_name( g_type_byte)] = 1;
+ types_known[ type_name( g_type_i16)] = 1;
+ types_known[ type_name( g_type_i32)] = 1;
+ types_known[ type_name( g_type_i64)] = 1;
+ types_known[ type_name( g_type_double)] = 1;
}
void t_delphi_generator::generate_enum(t_enum* tenum) {
has_enum = true;
indent_up();
- generate_delphi_doc(s_enum, tenum);
- indent(s_enum) << type_name(tenum, true, true) << " = "
- << "(" << endl;
+ generate_delphi_doc( s_enum, tenum);
+ indent(s_enum) <<
+ type_name(tenum,true,true) << " = " << "(" << endl;
indent_up();
vector<t_enum_value*> constants = tenum->get_constants();
if (constants.empty()) {
@@ -1004,43 +859,48 @@
indent_down();
}
-std::string t_delphi_generator::make_valid_delphi_identifier(std::string const& fromName) {
- std::string str = fromName;
- if (str.empty()) {
- return str;
- }
- // tests rely on this
- assert(('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
-
- // if the first letter is a number, we add an additional underscore in front of it
- char c = str.at(0);
- if (('0' <= c) && (c <= '9')) {
- str = "_" + str;
- }
-
- // following chars: letter, number or underscore
- for (size_t i = 0; i < str.size(); ++i) {
- c = str.at(i);
- if ((('A' > c) || (c > 'Z')) && (('a' > c) || (c > 'z')) && (('0' > c) || (c > '9'))
- && ('_' != c)) {
- str.replace(i, 1, "_");
+std::string t_delphi_generator::make_valid_delphi_identifier( std::string const & fromName) {
+ std::string str = fromName;
+ if( str.empty()) {
+ return str;
}
- }
- return str;
+ // tests rely on this
+ assert( ('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
+
+ // if the first letter is a number, we add an additional underscore in front of it
+ char c = str.at(0);
+ if( ('0' <= c) && (c <= '9')) {
+ str = "_" + str;
+ }
+
+ // following chars: letter, number or underscore
+ for( size_t i = 0; i < str.size(); ++i) {
+ c = str.at(i);
+ if( (('A' > c) || (c > 'Z')) &&
+ (('a' > c) || (c > 'z')) &&
+ (('0' > c) || (c > '9')) &&
+ ('_' != c) ) {
+ str.replace( i, 1, "_");
+ }
+ }
+
+ return str;
}
+
std::string t_delphi_generator::make_constants_classname() {
- if (constprefix_) {
- return make_valid_delphi_identifier("T" + program_name_ + "Constants");
+ if( constprefix_) {
+ return make_valid_delphi_identifier( "T" + program_name_ + "Constants");
} else {
- return "TConstants"; // compatibility
+ return "TConstants"; // compatibility
}
}
+
void t_delphi_generator::generate_consts(std::vector<t_const*> consts) {
- if (consts.empty()) {
+ if (consts.empty()){
return;
}
@@ -1048,16 +908,15 @@
string constants_class = make_constants_classname();
indent_up();
- indent(s_const) << constants_class.c_str() << " = class" << endl;
+ indent(s_const) <<
+ constants_class.c_str() << " = class" << endl;
indent(s_const) << "private" << endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- if (const_needs_var((*c_iter)->get_type())) {
- print_private_field(s_const,
- normalize_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
+ if( const_needs_var((*c_iter)->get_type())) {
+ print_private_field(s_const, normalize_name((*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value());
}
}
indent_down();
@@ -1065,10 +924,8 @@
indent_up();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
generate_delphi_doc(s_const, *c_iter);
- print_const_prop(s_const,
- normalize_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
+ print_const_prop(s_const, normalize_name((*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value());
}
indent(s_const) << "{$IF CompilerVersion >= 21.0}" << endl;
indent(s_const) << "class constructor Create;" << endl;
@@ -1078,41 +935,34 @@
indent(s_const) << "end;" << endl << endl;
indent_down();
- std::ostringstream vars, code;
+ std::ostringstream vars, code;
indent_up_impl();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- initialize_field(vars,
- code,
- "F" + prop_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
+ initialize_field(vars, code, "F" + prop_name( (*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value());
}
indent_down_impl();
indent_impl(s_const_impl) << "{$IF CompilerVersion >= 21.0}" << endl;
- indent_impl(s_const_impl) << "class constructor " << constants_class.c_str() << ".Create;"
- << endl;
+ indent_impl(s_const_impl) << "class constructor " << constants_class.c_str() << ".Create;" << endl;
- if (!vars.str().empty()) {
+ if ( ! vars.str().empty() ) {
indent_impl(s_const_impl) << "var" << endl;
s_const_impl << vars.str();
}
indent_impl(s_const_impl) << "begin" << endl;
- if (!code.str().empty()) {
+ if ( ! code.str().empty() ) {
s_const_impl << code.str();
}
indent_impl(s_const_impl) << "end;" << endl << endl;
- indent_impl(s_const_impl) << "class destructor " << constants_class.c_str() << ".Destroy;"
- << endl;
+ indent_impl(s_const_impl) << "class destructor " << constants_class.c_str() << ".Destroy;" << endl;
indent_impl(s_const_impl) << "begin" << endl;
indent_up_impl();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- if (const_needs_var((*c_iter)->get_type())) {
- finalize_field(s_const_impl,
- normalize_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
+ if( const_needs_var((*c_iter)->get_type())) {
+ finalize_field(s_const_impl, normalize_name( (*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value());
}
}
indent_impl(s_const_impl) << "inherited;" << endl;
@@ -1125,23 +975,20 @@
indent_up_impl();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- if (const_needs_var((*c_iter)->get_type())) {
- initialize_field(vars,
- code,
- constants_class + ".F" + prop_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
+ if( const_needs_var((*c_iter)->get_type())) {
+ initialize_field( vars, code, constants_class + ".F" + prop_name( (*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value());
}
}
indent_down_impl();
indent_impl(s_const_impl) << "procedure " << constants_class.c_str() << "_Initialize;" << endl;
- if (!vars.str().empty()) {
+ if ( ! vars.str().empty() ) {
indent_impl(s_const_impl) << "var" << endl;
s_const_impl << vars.str();
}
indent_impl(s_const_impl) << "begin" << endl;
- if (!code.str().empty()) {
+ if ( ! code.str().empty() ) {
s_const_impl << code.str();
}
indent_impl(s_const_impl) << "end;" << endl << endl;
@@ -1150,23 +997,16 @@
indent_impl(s_const_impl) << "begin" << endl;
indent_up_impl();
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- finalize_field(s_const_impl,
- normalize_name((*c_iter)->get_name()),
- (*c_iter)->get_type(),
- (*c_iter)->get_value(),
- constants_class);
+ finalize_field(s_const_impl, normalize_name( (*c_iter)->get_name()),
+ (*c_iter)->get_type(), (*c_iter)->get_value(), constants_class);
}
indent_down_impl();
indent_impl(s_const_impl) << "end;" << endl;
indent_impl(s_const_impl) << "{$IFEND}" << endl << endl;
}
-void t_delphi_generator::print_const_def_value(std::ostream& vars,
- std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value,
- string cls_nm) {
+void t_delphi_generator::print_const_def_value(std::ostream& vars, std::ostream& out, string name, t_type* type, t_const_value* value, string cls_nm)
+{
string cls_prefix;
@@ -1191,10 +1031,8 @@
if (field_type == NULL) {
throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
}
- string val = render_const_value(vars, out, name, field_type, v_iter->second);
- indent_impl(out) << cls_prefix << normalize_name(name) << "."
- << prop_name(v_iter->first->get_string(), type->is_xception())
- << " := " << val << ";" << endl;
+ string val = render_const_value( vars, out, name, field_type, v_iter->second);
+ indent_impl(out) << cls_prefix << normalize_name(name) << "." << prop_name( v_iter->first->get_string(), type->is_xception()) << " := " << val << ";" << endl;
}
} else if (type->is_map()) {
t_type* ktype = ((t_map*)type)->get_key_type();
@@ -1202,10 +1040,9 @@
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- string key = render_const_value(vars, out, name, ktype, v_iter->first);
- string val = render_const_value(vars, out, name, vtype, v_iter->second);
- indent_impl(out) << cls_prefix << normalize_name(name) << "[" << key << "]"
- << " := " << val << ";" << endl;
+ string key = render_const_value( vars, out, name, ktype, v_iter->first);
+ string val = render_const_value( vars, out, name, vtype, v_iter->second);
+ indent_impl(out) << cls_prefix << normalize_name(name) << "[" << key << "]" << " := " << val << ";" << endl;
}
} else if (type->is_list() || type->is_set()) {
t_type* etype;
@@ -1218,48 +1055,39 @@
const vector<t_const_value*>& val = value->get_list();
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- string val = render_const_value(vars, out, name, etype, *v_iter);
+ string val = render_const_value( vars, out, name, etype, *v_iter);
indent_impl(out) << cls_prefix << normalize_name(name) << ".Add(" << val << ");" << endl;
}
}
}
-void t_delphi_generator::print_private_field(std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)value;
+void t_delphi_generator::print_private_field(std::ostream& out, string name, t_type* type, t_const_value* value) {
+ (void) value;
indent(out) << "class var F" << name << ": " << type_name(type) << ";" << endl;
}
+
bool t_delphi_generator::const_needs_var(t_type* type) {
t_type* truetype = type;
while (truetype->is_typedef()) {
truetype = ((t_typedef*)truetype)->get_type();
}
- return (!truetype->is_base_type());
+ return (! truetype->is_base_type());
}
-void t_delphi_generator::print_const_prop(std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)value;
+
+void t_delphi_generator::print_const_prop(std::ostream& out, string name, t_type* type, t_const_value* value) {
+ (void) value;
if (const_needs_var(type)) {
- indent(out) << "class property " << name << ": " << type_name(type) << " read F" << name << ";"
- << endl;
+ indent(out) << "class property " << name << ": " << type_name(type) << " read F" << name << ";" << endl;
} else {
std::ostringstream vars; // dummy
- string v2 = render_const_value(vars, out, name, type, value);
+ string v2 = render_const_value( vars, out, name, type, value);
indent(out) << "const " << name << " = " << v2 << ";" << endl;
}
}
-void t_delphi_generator::print_const_value(std::ostream& vars,
- std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value) {
+void t_delphi_generator::print_const_value( std::ostream& vars, std::ostream& out, string name, t_type* type, t_const_value* value) {
t_type* truetype = type;
while (truetype->is_typedef()) {
truetype = ((t_typedef*)truetype)->get_type();
@@ -1267,45 +1095,32 @@
if (truetype->is_base_type()) {
// already done
- // string v2 = render_const_value( vars, out, name, type, value);
- // indent_impl(out) << name << " := " << v2 << ";" << endl;
+ //string v2 = render_const_value( vars, out, name, type, value);
+ //indent_impl(out) << name << " := " << v2 << ";" << endl;
} else if (truetype->is_enum()) {
- indent_impl(out) << name << " := " << type_name(type) << "." << value->get_identifier_name()
- << ";" << endl;
+ indent_impl(out) << name << " := " << type_name(type) << "." << value->get_identifier_name() << ";" << endl;
} else {
string typname;
- typname = type_name(truetype, true, false, type->is_xception(), type->is_xception());
+ typname = type_name( truetype, true, false, type->is_xception(), type->is_xception());
indent_impl(out) << name << " := " << typname << ".Create;" << endl;
- print_const_def_value(vars, out, name, truetype, value);
+ print_const_def_value( vars, out, name, truetype, value);
}
}
-void t_delphi_generator::initialize_field(std::ostream& vars,
- std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- print_const_value(vars, out, name, type, value);
+void t_delphi_generator::initialize_field(std::ostream& vars, std::ostream& out, string name, t_type* type, t_const_value* value) {
+ print_const_value( vars, out, name, type, value );
}
-void t_delphi_generator::finalize_field(std::ostream& out,
- string name,
- t_type* type,
- t_const_value* value,
- string cls_nm) {
- (void)out;
- (void)name;
- (void)type;
- (void)value;
- (void)cls_nm;
+void t_delphi_generator::finalize_field(std::ostream& out, string name, t_type* type, t_const_value* value , string cls_nm) {
+ (void) out;
+ (void) name;
+ (void) type;
+ (void) value;
+ (void) cls_nm;
}
-string t_delphi_generator::render_const_value(ostream& vars,
- ostream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+string t_delphi_generator::render_const_value(ostream& vars, ostream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
t_type* truetype = type;
while (truetype->is_typedef()) {
@@ -1317,40 +1132,40 @@
if (truetype->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)truetype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << "'" << get_escaped_string(value) << "'";
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() > 0) ? "True" : "False");
- break;
- case t_base_type::TYPE_BYTE:
- render << "ShortInt( " << value->get_integer() << ")";
- break;
- case t_base_type::TYPE_I16:
- render << "SmallInt( " << value->get_integer() << ")";
- break;
- case t_base_type::TYPE_I32:
- render << "LongInt( " << value->get_integer() << ")";
- break;
- case t_base_type::TYPE_I64:
- render << "Int64( " << value->get_integer() << ")";
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- render << value->get_integer() << ".0"; // make it a double constant by adding ".0"
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_STRING:
+ render << "'" << get_escaped_string(value) << "'";
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "True" : "False");
+ break;
+ case t_base_type::TYPE_BYTE:
+ render << "ShortInt( " << value->get_integer() << ")";
+ break;
+ case t_base_type::TYPE_I16:
+ render << "SmallInt( " << value->get_integer() << ")";
+ break;
+ case t_base_type::TYPE_I32:
+ render << "LongInt( " << value->get_integer() << ")";
+ break;
+ case t_base_type::TYPE_I64:
+ render << "Int64( " << value->get_integer() << ")";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << value->get_integer() << ".0"; // make it a double constant by adding ".0"
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (truetype->is_enum()) {
- render << type_name(type, false) << "." << value->get_identifier_name();
+ render << type_name( type, false) << "." << value->get_identifier_name();
} else {
string t = tmp("tmp");
- vars << " " << t << " : " << type_name(type) << ";" << endl;
- print_const_value(vars, out, t, type, value);
+ vars << " " << t << " : " << type_name(type) << ";" << endl;
+ print_const_value( vars, out, t, type, value);
render << t;
}
@@ -1370,27 +1185,19 @@
generate_delphi_struct_definition(s_struct, tstruct, is_exception);
indent_down();
- add_defined_type(tstruct);
+ add_defined_type( tstruct);
generate_delphi_struct_impl(s_struct_impl, "", tstruct, is_exception);
if (register_types_) {
generate_delphi_struct_type_factory(s_type_factory_funcs, "", tstruct, is_exception);
- generate_delphi_struct_type_factory_registration(s_type_factory_registration,
- "",
- tstruct,
- is_exception);
+ generate_delphi_struct_type_factory_registration(s_type_factory_registration, "", tstruct, is_exception);
}
}
-void t_delphi_generator::generate_delphi_struct_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result,
- bool is_x_factory) {
+void t_delphi_generator::generate_delphi_struct_impl( ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result, bool is_x_factory) {
- if (is_exception && (!is_x_factory)) {
- generate_delphi_struct_impl(out, cls_prefix, tstruct, is_exception, is_result, true);
+ if (is_exception && (! is_x_factory)) {
+ generate_delphi_struct_impl( out, cls_prefix, tstruct, is_exception, is_result, true);
}
string cls_nm;
@@ -1398,13 +1205,14 @@
string exception_factory_name;
if (is_exception) {
- exception_factory_name = normalize_clsnm(tstruct->get_name(), "", true) + "Factory";
+ exception_factory_name = normalize_clsnm( tstruct->get_name(), "", true ) + "Factory";
}
if (is_exception) {
- cls_nm = type_name(tstruct, true, (!is_x_factory), is_x_factory, true);
- } else {
- cls_nm = type_name(tstruct, true, false);
+ cls_nm = type_name(tstruct,true,(! is_x_factory),is_x_factory,true);
+ }
+ else {
+ cls_nm = type_name(tstruct,true,false);
}
std::ostringstream vars, code;
@@ -1419,38 +1227,31 @@
t = ((t_typedef*)t)->get_type();
}
if ((*m_iter)->get_value() != NULL) {
- initialize_field(vars,
- code,
- "F" + prop_name((*m_iter)->get_name(), is_exception),
- t,
- (*m_iter)->get_value());
+ initialize_field( vars, code, "F" + prop_name( (*m_iter)->get_name(), is_exception), t, (*m_iter)->get_value());
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
- indent_impl(code) << "F__isset_" << prop_name((*m_iter), is_exception) << " := True;"
- << endl;
+ indent_impl(code) << "F__isset_" << prop_name((*m_iter), is_exception) << " := True;" << endl;
}
}
}
indent_down_impl();
- indent_impl(out) << "constructor " << cls_prefix << cls_nm << "."
- << "Create;" << endl;
+ indent_impl(out) << "constructor " << cls_prefix << cls_nm << "." << "Create;" << endl;
- if (!vars.str().empty()) {
+ if ( ! vars.str().empty()) {
out << "var" << endl;
out << vars.str();
}
indent_impl(out) << "begin" << endl;
indent_up_impl();
- if (is_exception && (!is_x_factory)) {
+ if (is_exception && (! is_x_factory)) {
indent_impl(out) << "inherited Create('');" << endl;
- indent_impl(out) << "F" << exception_factory_name << " := T" << exception_factory_name
- << "Impl.Create;" << endl;
+ indent_impl(out) << "F" << exception_factory_name << " := T" << exception_factory_name << "Impl.Create;" << endl;
} else {
indent_impl(out) << "inherited;" << endl;
}
- if (!code.str().empty()) {
+ if ( ! code.str().empty()) {
out << code.str();
}
@@ -1458,15 +1259,13 @@
indent_impl(out) << "end;" << endl << endl;
if ((members.size() > 0) && is_exception && (!is_x_factory)) {
- indent_impl(out) << "constructor " << cls_prefix << cls_nm << "."
- << "Create(" << constructor_argument_list(tstruct, indent_impl()) << ");"
- << endl;
+ indent_impl(out) << "constructor " << cls_prefix << cls_nm << "." << "Create(" << constructor_argument_list( tstruct, indent_impl()) << ");" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
indent_impl(out) << "Create;" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
string propname = prop_name((*m_iter)->get_name(), is_exception);
- string param_name = constructor_param_name((*m_iter)->get_name());
+ string param_name = constructor_param_name( (*m_iter)->get_name());
indent_impl(out) << propname << " := " << param_name << ";" << endl;
}
indent_impl(out) << "UpdateMessageProperty;" << endl;
@@ -1474,8 +1273,7 @@
indent_impl(out) << "end;" << endl << endl;
}
- indent_impl(out) << "destructor " << cls_prefix << cls_nm << "."
- << "Destroy;" << endl;
+ indent_impl(out) << "destructor " << cls_prefix << cls_nm << "." << "Destroy;" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
@@ -1484,16 +1282,15 @@
while (t->is_typedef()) {
t = ((t_typedef*)t)->get_type();
}
- finalize_field(out, prop_name(*m_iter, is_exception), t, (*m_iter)->get_value());
+ finalize_field( out, prop_name(*m_iter, is_exception), t, (*m_iter)->get_value());
}
indent_impl(out) << "inherited;" << endl;
indent_down_impl();
indent_impl(out) << "end;" << endl << endl;
- if (tstruct->is_union()) {
- indent_impl(out) << "procedure " << cls_prefix << cls_nm << "."
- << "ClearUnionValues;" << endl;
+ if ( tstruct->is_union() ) {
+ indent_impl(out) << "procedure " << cls_prefix << cls_nm << "." << "ClearUnionValues;" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -1502,16 +1299,7 @@
t = ((t_typedef*)t)->get_type();
}
- generate_delphi_clear_union_value(out,
- cls_prefix,
- cls_nm,
- t,
- *m_iter,
- "F",
- is_exception,
- tstruct->is_union(),
- is_x_factory,
- exception_factory_name);
+ generate_delphi_clear_union_value( out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception, tstruct->is_union(), is_x_factory, exception_factory_name);
}
indent_down_impl();
indent_impl(out) << "end;" << endl << endl;
@@ -1522,51 +1310,38 @@
while (t->is_typedef()) {
t = ((t_typedef*)t)->get_type();
}
- generate_delphi_property_reader_impl(out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception);
- generate_delphi_property_writer_impl(out,
- cls_prefix,
- cls_nm,
- t,
- *m_iter,
- "F",
- is_exception,
- tstruct->is_union(),
- is_x_factory,
- exception_factory_name);
+ generate_delphi_property_reader_impl( out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception);
+ generate_delphi_property_writer_impl( out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception, tstruct->is_union(), is_x_factory, exception_factory_name);
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
- generate_delphi_isset_reader_impl(out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception);
+ generate_delphi_isset_reader_impl( out, cls_prefix, cls_nm, t, *m_iter, "F", is_exception);
}
}
- if ((!is_exception) || is_x_factory) {
- generate_delphi_struct_reader_impl(out, cls_prefix, tstruct, is_exception);
- if (is_result) {
- generate_delphi_struct_result_writer_impl(out, cls_prefix, tstruct, is_exception);
+ if ((! is_exception) || is_x_factory) {
+ generate_delphi_struct_reader_impl( out, cls_prefix, tstruct, is_exception);
+ if ( is_result ) {
+ generate_delphi_struct_result_writer_impl( out, cls_prefix, tstruct, is_exception);
} else {
- generate_delphi_struct_writer_impl(out, cls_prefix, tstruct, is_exception);
+ generate_delphi_struct_writer_impl( out, cls_prefix, tstruct, is_exception);
}
}
- generate_delphi_struct_tostring_impl(out, cls_prefix, tstruct, is_exception, is_x_factory);
+ generate_delphi_struct_tostring_impl( out, cls_prefix, tstruct, is_exception, is_x_factory);
if (is_exception && is_x_factory) {
- generate_delphi_create_exception_impl(out, cls_prefix, tstruct, is_exception);
+ generate_delphi_create_exception_impl( out, cls_prefix, tstruct, is_exception);
}
}
-void t_delphi_generator::print_delphi_struct_type_factory_func(ostream& out, t_struct* tstruct) {
+void t_delphi_generator::print_delphi_struct_type_factory_func( ostream& out, t_struct* tstruct) {
string struct_intf_name = type_name(tstruct);
out << "Create_";
out << struct_intf_name;
out << "_Impl";
}
-void t_delphi_generator::generate_delphi_struct_type_factory(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result,
- bool is_x_factory) {
- (void)cls_prefix;
+
+void t_delphi_generator::generate_delphi_struct_type_factory( ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result, bool is_x_factory) {
+ (void) cls_prefix;
if (is_exception)
return;
if (is_result)
@@ -1575,7 +1350,7 @@
return;
string struct_intf_name = type_name(tstruct);
- string cls_nm = type_name(tstruct, true, false);
+ string cls_nm = type_name(tstruct,true,false);
out << "function ";
print_delphi_struct_type_factory_func(out, tstruct);
@@ -1589,13 +1364,8 @@
out << "end;" << endl << endl;
}
-void t_delphi_generator::generate_delphi_struct_type_factory_registration(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_result,
- bool is_x_factory) {
- (void)cls_prefix;
+void t_delphi_generator::generate_delphi_struct_type_factory_registration( ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception, bool is_result, bool is_x_factory) {
+ (void) cls_prefix;
if (is_exception)
return;
if (is_result)
@@ -1611,12 +1381,7 @@
out << endl;
}
-void t_delphi_generator::generate_delphi_struct_definition(ostream& out,
- t_struct* tstruct,
- bool is_exception,
- bool in_class,
- bool is_result,
- bool is_x_factory) {
+void t_delphi_generator::generate_delphi_struct_definition(ostream &out, t_struct* tstruct, bool is_exception, bool in_class, bool is_result, bool is_x_factory) {
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
string struct_intf_name;
string struct_name;
@@ -1624,35 +1389,38 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- string exception_factory_name = normalize_clsnm(tstruct->get_name(), "", true) + "Factory";
+ string exception_factory_name = normalize_clsnm( tstruct->get_name(), "", true ) + "Factory";
if (is_exception) {
- struct_intf_name = type_name(tstruct, false, false, true);
- } else {
+ struct_intf_name = type_name(tstruct,false,false,true);
+ }
+ else {
struct_intf_name = type_name(tstruct);
}
+
if (is_exception) {
- struct_name = type_name(tstruct, true, (!is_x_factory), is_x_factory);
- } else {
- struct_name = type_name(tstruct, true);
+ struct_name = type_name(tstruct, true, (! is_x_factory), is_x_factory);
+ }
+ else {
+ struct_name = type_name(tstruct,true);
}
- if ((!is_exception) || is_x_factory) {
+ if ((! is_exception) || is_x_factory) {
generate_delphi_doc(out, tstruct);
indent(out) << struct_intf_name << " = interface(IBase)" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- generate_delphi_property_reader_definition(out, *m_iter, is_exception);
- generate_delphi_property_writer_definition(out, *m_iter, is_exception);
+ generate_delphi_property_reader_definition( out, *m_iter, is_exception);
+ generate_delphi_property_writer_definition( out, *m_iter, is_exception);
}
if (is_x_factory) {
out << endl;
indent(out) << "// Create Exception Object" << endl;
- indent(out) << "function CreateException: " << type_name(tstruct, true, true) << ";" << endl;
+ indent(out) << "function CreateException: " << type_name(tstruct,true,true) << ";" << endl;
}
if (members.size() > 0) {
@@ -1666,7 +1434,7 @@
out << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
- generate_delphi_isset_reader_definition(out, *m_iter, is_exception);
+ generate_delphi_isset_reader_definition( out, *m_iter, is_exception);
}
}
}
@@ -1676,8 +1444,7 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
isset_name = "__isset_" + prop_name(*m_iter, is_exception);
- indent(out) << "property " << isset_name << ": Boolean read Get" << isset_name << ";"
- << endl;
+ indent(out) << "property " << isset_name << ": Boolean read Get" << isset_name << ";" << endl;
}
}
}
@@ -1692,19 +1459,19 @@
out << "sealed ";
}
out << "class(";
- if (is_exception && (!is_x_factory)) {
+ if ( is_exception && (! is_x_factory)) {
out << "TException";
} else {
out << "TInterfacedObject, IBase, " << struct_intf_name;
}
out << ")" << endl;
- if (is_exception && (!is_x_factory)) {
+ if (is_exception && (! is_x_factory)) {
indent(out) << "public" << endl;
indent_up();
indent(out) << "type" << endl;
indent_up();
- generate_delphi_struct_definition(out, tstruct, is_exception, in_class, is_result, true);
+ generate_delphi_struct_definition( out, tstruct, is_exception, in_class, is_result, true);
indent_down();
indent_down();
}
@@ -1712,7 +1479,7 @@
indent(out) << "private" << endl;
indent_up();
- if (is_exception && (!is_x_factory)) {
+ if (is_exception && (! is_x_factory)) {
indent(out) << "F" << exception_factory_name << " :" << struct_intf_name << ";" << endl << endl;
}
@@ -1733,8 +1500,8 @@
indent(out) << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- generate_delphi_property_reader_definition(out, *m_iter, is_exception);
- generate_delphi_property_writer_definition(out, *m_iter, is_exception);
+ generate_delphi_property_reader_definition( out, *m_iter, is_exception);
+ generate_delphi_property_writer_definition( out, *m_iter, is_exception);
}
if (tstruct->is_union()) {
@@ -1758,37 +1525,35 @@
indent(out) << "public" << endl;
indent_up();
- if ((members.size() > 0) && is_exception && (!is_x_factory)) {
+ if ((members.size() > 0) && is_exception && (! is_x_factory)) {
indent(out) << "constructor Create; overload;" << endl;
- indent(out) << "constructor Create(" << constructor_argument_list(tstruct, indent())
- << "); overload;" << endl;
+ indent(out) << "constructor Create(" << constructor_argument_list( tstruct, indent()) << "); overload;" << endl;
} else {
indent(out) << "constructor Create;" << endl;
}
indent(out) << "destructor Destroy; override;" << endl;
- out << endl;
+ out << endl;
indent(out) << "function ToString: string; override;" << endl;
- if (is_exception && (!is_x_factory)) {
- out << endl;
+ if (is_exception && (! is_x_factory)) {
+ out << endl;
indent(out) << "// Exception Factory" << endl;
- indent(out) << "property " << exception_factory_name << ": " << struct_intf_name << " read F"
- << exception_factory_name << " write F" << exception_factory_name << ";" << endl;
+ indent(out) << "property " << exception_factory_name << ": " << struct_intf_name << " read F" << exception_factory_name << " write F" << exception_factory_name << ";" << endl;
}
- if ((!is_exception) || is_x_factory) {
- out << endl;
+ if ((! is_exception) || is_x_factory) {
+ out << endl;
indent(out) << "// IBase" << endl;
indent(out) << "procedure Read( const iprot: IProtocol);" << endl;
indent(out) << "procedure Write( const oprot: IProtocol);" << endl;
}
if (is_exception && is_x_factory) {
- out << endl;
+ out << endl;
indent(out) << "// Create Exception Object" << endl;
- indent(out) << "function CreateException: " << type_name(tstruct, true, true) << ";" << endl;
+ indent(out) << "function CreateException: " << type_name(tstruct,true,true) << ";" << endl;
}
if (members.size() > 0) {
@@ -1805,8 +1570,7 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
isset_name = "__isset_" + prop_name(*m_iter, is_exception);
- indent(out) << "property " << isset_name << ": Boolean read Get" << isset_name << ";"
- << endl;
+ indent(out) << "property " << isset_name << ": Boolean read Get" << isset_name << ";" << endl;
}
}
}
@@ -1844,17 +1608,21 @@
extends = type_name(tservice->get_extends(), true, true);
extends_iface = extends + ".Iface";
generate_delphi_doc(s_service, tservice);
- indent(s_service) << "Iface = interface(" << extends_iface << ")" << endl;
+ indent(s_service) <<
+ "Iface = interface(" << extends_iface << ")" << endl;
} else {
- indent(s_service) << "Iface = interface" << endl;
+ indent(s_service) <<
+ "Iface = interface" << endl;
}
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
+ {
generate_delphi_doc(s_service, *f_iter);
- indent(s_service) << function_signature(*f_iter) << endl;
+ indent(s_service) <<
+ function_signature(*f_iter) << endl;
}
indent_down();
indent(s_service) << "end;" << endl << endl;
@@ -1869,10 +1637,7 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
generate_delphi_struct_definition(s_service, ts, false, true);
- generate_delphi_struct_impl(s_service_impl,
- normalize_clsnm(service_name_, "T") + ".",
- ts,
- false);
+ generate_delphi_struct_impl(s_service_impl, normalize_clsnm( service_name_, "T") + ".", ts, false);
generate_function_helpers(*f_iter);
}
}
@@ -1890,9 +1655,11 @@
if (tservice->get_extends() != NULL) {
extends = type_name(tservice->get_extends(), true, true);
extends_client = extends + ".TClient";
- indent(s_service) << "TClient = class(" << extends_client << ", Iface)" << endl;
+ indent(s_service) <<
+ "TClient = class(" << extends_client << ", Iface)" << endl;
} else {
- indent(s_service) << "TClient = class( TInterfacedObject, Iface)" << endl;
+ indent(s_service) <<
+ "TClient = class( TInterfacedObject, Iface)" << endl;
}
indent(s_service) << "public" << endl;
@@ -1900,27 +1667,25 @@
indent(s_service) << "constructor Create( prot: IProtocol); overload;" << endl;
- indent_impl(s_service_impl) << "constructor " << normalize_clsnm(service_name_, "T")
- << ".TClient.Create( prot: IProtocol);" << endl;
- indent_impl(s_service_impl) << "begin" << endl;
+ indent_impl(s_service_impl) << "constructor " << normalize_clsnm( service_name_, "T") << ".TClient.Create( prot: IProtocol);" << endl;
+ indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
indent_impl(s_service_impl) << "Create( prot, prot );" << endl;
indent_down_impl();
- indent_impl(s_service_impl) << "end;" << endl << endl;
+ indent_impl(s_service_impl) << "end;" << endl << endl;
- indent(s_service)
- << "constructor Create( const iprot: IProtocol; const oprot: IProtocol); overload;" << endl;
+ indent(s_service) << "constructor Create( const iprot: IProtocol; const oprot: IProtocol); overload;" << endl;
- indent_impl(s_service_impl) << "constructor " << normalize_clsnm(service_name_, "T")
- << ".TClient.Create( const iprot: IProtocol; const oprot: IProtocol);"
- << endl;
- indent_impl(s_service_impl) << "begin" << endl;
+ indent_impl(s_service_impl) <<
+ "constructor " << normalize_clsnm( service_name_, "T") <<
+ ".TClient.Create( const iprot: IProtocol; const oprot: IProtocol);" << endl;
+ indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
indent_impl(s_service_impl) << "inherited Create;" << endl;
indent_impl(s_service_impl) << "iprot_ := iprot;" << endl;
indent_impl(s_service_impl) << "oprot_ := oprot;" << endl;
indent_down_impl();
- indent_impl(s_service_impl) << "end;" << endl << endl;
+ indent_impl(s_service_impl) << "end;" << endl << endl;
indent_down();
@@ -1955,7 +1720,7 @@
indent(s_service) << "public" << endl;
indent_up();
- string full_cls = normalize_clsnm(service_name_, "T") + ".TClient";
+ string full_cls = normalize_clsnm(service_name_,"T") + ".TClient";
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string funname = (*f_iter)->get_name();
@@ -1976,7 +1741,7 @@
} else {
s_service_impl << ", ";
}
- s_service_impl << normalize_name((*fld_iter)->get_name());
+ s_service_impl << normalize_name( (*fld_iter)->get_name());
}
s_service_impl << ");" << endl;
@@ -1985,22 +1750,23 @@
if (!(*f_iter)->get_returntype()->is_void()) {
s_service_impl << "Result := ";
}
- s_service_impl << "recv_" << funname << "();" << endl;
+ s_service_impl <<
+ "recv_" << funname << "();" << endl;
}
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl << endl;
t_function send_function(g_type_void,
- string("send_") + (*f_iter)->get_name(),
- (*f_iter)->get_arglist());
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
- string argsname = (*f_iter)->get_name() + "_args";
- string args_clsnm = normalize_clsnm(argsname, "T");
- string args_intfnm = normalize_clsnm(argsname, "I");
+ string argsname = (*f_iter)->get_name() + "_args";
+ string args_clsnm = normalize_clsnm( argsname, "T");
+ string args_intfnm= normalize_clsnm( argsname, "I");
string argsvar = tmp("_args");
- string msgvar = tmp("_msg");
+ string msgvar = tmp("_msg");
indent(s_service) << function_signature(&send_function) << endl;
indent_impl(s_service_impl) << function_signature(&send_function, full_cls) << endl;
@@ -2012,24 +1778,26 @@
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
- indent_impl(s_service_impl) << "seqid_ := seqid_ + 1;" << endl;
- indent_impl(s_service_impl) << msgvar << " := Thrift.Protocol.TMessageImpl.Create('" << funname
- << "', " << ((*f_iter)->is_oneway() ? "TMessageType.Oneway"
- : "TMessageType.Call")
- << ", seqid_);" << endl;
+ indent_impl(s_service_impl) <<
+ "seqid_ := seqid_ + 1;" << endl;
+ indent_impl(s_service_impl) <<
+ msgvar << " := Thrift.Protocol.TMessageImpl.Create('" << funname << "', " <<
+ ((*f_iter)->is_oneway() ? "TMessageType.Oneway" : "TMessageType.Call") <<
+ ", seqid_);" << endl;
- indent_impl(s_service_impl) << "oprot_.WriteMessageBegin( " << msgvar << " );" << endl;
- indent_impl(s_service_impl) << argsvar << " := " << args_clsnm << "Impl.Create();" << endl;
+ indent_impl(s_service_impl) <<
+ "oprot_.WriteMessageBegin( " << msgvar << " );" << endl;
+ indent_impl(s_service_impl) <<
+ argsvar << " := " << args_clsnm << "Impl.Create();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- indent_impl(s_service_impl) << argsvar << "." << prop_name(*fld_iter)
- << " := " << normalize_name((*fld_iter)->get_name()) << ";"
- << endl;
+ indent_impl(s_service_impl) <<
+ argsvar << "." << prop_name(*fld_iter) << " := " << normalize_name( (*fld_iter)->get_name()) << ";" << endl;
}
indent_impl(s_service_impl) << argsvar << ".Write(oprot_);" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- indent_impl(s_service_impl) << argsvar << "." << prop_name(*fld_iter)
- << " := " << empty_value((*fld_iter)->get_type()) << ";" << endl;
+ indent_impl(s_service_impl) <<
+ argsvar << "." << prop_name(*fld_iter) << " := " << empty_value((*fld_iter)->get_type()) << ";" << endl;
}
indent_impl(s_service_impl) << "oprot_.WriteMessageEnd();" << endl;
@@ -2039,17 +1807,17 @@
indent_impl(s_service_impl) << "end;" << endl << endl;
if (!(*f_iter)->is_oneway()) {
- string org_resultname = (*f_iter)->get_name() + "_result";
- string result_clsnm = normalize_clsnm(org_resultname, "T");
- string result_intfnm = normalize_clsnm(org_resultname, "I");
+ string org_resultname = (*f_iter)->get_name() + "_result" ;
+ string result_clsnm = normalize_clsnm( org_resultname, "T");
+ string result_intfnm = normalize_clsnm( org_resultname, "I");
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
- string("recv_") + (*f_iter)->get_name(),
- &noargs,
- (*f_iter)->get_xceptions());
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs,
+ (*f_iter)->get_xceptions());
- t_struct* xs = (*f_iter)->get_xceptions();
+ t_struct *xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
string exceptvar = tmp("_ex");
@@ -2061,7 +1829,7 @@
indent_impl(s_service_impl) << "var" << endl;
indent_up_impl();
indent_impl(s_service_impl) << msgvar << " : Thrift.Protocol.IMessage;" << endl;
- if (xceptions.size() > 0) {
+ if ( xceptions.size() > 0) {
indent_impl(s_service_impl) << exceptvar << " : Exception;" << endl;
}
indent_impl(s_service_impl) << appexvar << " : TApplicationException;" << endl;
@@ -2071,8 +1839,7 @@
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
indent_impl(s_service_impl) << msgvar << " := iprot_.ReadMessageBegin();" << endl;
- indent_impl(s_service_impl) << "if (" << msgvar << ".Type_ = TMessageType.Exception) then"
- << endl;
+ indent_impl(s_service_impl) << "if (" << msgvar << ".Type_ = TMessageType.Exception) then" << endl;
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
indent_impl(s_service_impl) << appexvar << " := TApplicationException.Read(iprot_);" << endl;
@@ -2090,9 +1857,8 @@
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
indent_impl(s_service_impl) << "Result := " << retvar << ".Success;" << endl;
- t_type* type = (*f_iter)->get_returntype();
- if (type->is_struct() || type->is_xception() || type->is_map() || type->is_list()
- || type->is_set()) {
+ t_type *type = (*f_iter)->get_returntype();
+ if (type->is_struct() || type->is_xception() || type->is_map() || type->is_list() || type->is_set()) {
indent_impl(s_service_impl) << retvar << ".Success := nil;" << endl;
}
indent_impl(s_service_impl) << "Exit;" << endl;
@@ -2102,22 +1868,18 @@
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- indent_impl(s_service_impl) << "if (" << retvar << ".__isset_" << prop_name(*x_iter)
- << ") then" << endl;
+ indent_impl(s_service_impl) << "if (" << retvar << ".__isset_" << prop_name(*x_iter) << ") then" << endl;
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
- indent_impl(s_service_impl) << exceptvar << " := " << retvar << "." << prop_name(*x_iter)
- << ".CreateException;" << endl;
+ indent_impl(s_service_impl) << exceptvar << " := " << retvar << "." << prop_name(*x_iter) << ".CreateException;" << endl;
indent_impl(s_service_impl) << "raise " << exceptvar << ";" << endl;
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl;
}
if (!(*f_iter)->get_returntype()->is_void()) {
- indent_impl(s_service_impl)
- << "raise "
- "TApplicationException.Create(TApplicationException.TExceptionType.MissingResult, '"
- << (*f_iter)->get_name() << " failed: unknown result');" << endl;
+ indent_impl(s_service_impl) <<
+ "raise TApplicationException.Create(TApplicationException.TExceptionType.MissingResult, '" << (*f_iter)->get_name() << " failed: unknown result');" << endl;
}
indent_down_impl();
@@ -2136,14 +1898,16 @@
string extends = "";
string extends_processor = "";
- string full_cls = normalize_clsnm(service_name_, "T") + ".TProcessorImpl";
+ string full_cls = normalize_clsnm( service_name_, "T") + ".TProcessorImpl";
if (tservice->get_extends() != NULL) {
extends = type_name(tservice->get_extends(), true, true);
extends_processor = extends + ".TProcessorImpl";
- indent(s_service) << "TProcessorImpl = class(" << extends_processor << ", IProcessor)" << endl;
+ indent(s_service) <<
+ "TProcessorImpl = class(" << extends_processor << ", IProcessor)" << endl;
} else {
- indent(s_service) << "TProcessorImpl = class( TInterfacedObject, IProcessor)" << endl;
+ indent(s_service) <<
+ "TProcessorImpl = class( TInterfacedObject, IProcessor)" << endl;
}
indent(s_service) << "public" << endl;
@@ -2155,22 +1919,23 @@
indent_impl(s_service_impl) << "constructor " << full_cls << ".Create( iface_: Iface );" << endl;
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
- if (tservice->get_extends() != NULL) {
+ if (tservice->get_extends() != NULL)
+ {
indent_impl(s_service_impl) << "inherited Create( iface_);" << endl;
} else {
indent_impl(s_service_impl) << "inherited Create;" << endl;
}
indent_impl(s_service_impl) << "Self.iface_ := iface_;" << endl;
- if (tservice->get_extends() != NULL) {
+ if (tservice->get_extends() != NULL)
+ {
indent_impl(s_service_impl) << "ASSERT( processMap_ <> nil); // inherited" << endl;
} else {
- indent_impl(s_service_impl)
- << "processMap_ := TThriftDictionaryImpl<string, TProcessFunction>.Create;" << endl;
+ indent_impl(s_service_impl) << "processMap_ := TThriftDictionaryImpl<string, TProcessFunction>.Create;" << endl;
}
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent_impl(s_service_impl) << "processMap_.AddOrSetValue( '" << (*f_iter)->get_name() << "', "
- << (*f_iter)->get_name() << "_Process);" << endl;
+ indent_impl(s_service_impl) <<
+ "processMap_.AddOrSetValue( '" << (*f_iter)->get_name() << "', " << (*f_iter)->get_name() << "_Process);" << endl;
}
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl << endl;
@@ -2187,14 +1952,15 @@
indent(s_service) << "iface_: Iface;" << endl;
indent_down();
- if (tservice->get_extends() == NULL) {
+ if (tservice->get_extends() == NULL)
+ {
indent(s_service) << "protected" << endl;
indent_up();
indent(s_service) << "type" << endl;
indent_up();
- indent(s_service) << "TProcessFunction = reference to procedure( seqid: Integer; const iprot: "
- "IProtocol; const oprot: IProtocol"
- << (events_ ? "; const events : IRequestEvents" : "") << ");" << endl;
+ indent(s_service) << "TProcessFunction = reference to procedure( seqid: Integer; const iprot: IProtocol; const oprot: IProtocol" <<
+ (events_ ? "; const events : IRequestEvents" : "") <<
+ ");" << endl;
indent_down();
indent_down();
indent(s_service) << "protected" << endl;
@@ -2206,23 +1972,18 @@
indent(s_service) << "public" << endl;
indent_up();
if (extends.empty()) {
- indent(s_service) << "function Process( const iprot: IProtocol; const oprot: IProtocol; const "
- "events : IProcessorEvents): Boolean;" << endl;
+ indent(s_service) << "function Process( const iprot: IProtocol; const oprot: IProtocol; const events : IProcessorEvents): Boolean;" << endl;
} else {
- indent(s_service) << "function Process( const iprot: IProtocol; const oprot: IProtocol; const "
- "events : IProcessorEvents): Boolean; reintroduce;" << endl;
+ indent(s_service) << "function Process( const iprot: IProtocol; const oprot: IProtocol; const events : IProcessorEvents): Boolean; reintroduce;" << endl;
}
- indent_impl(s_service_impl) << "function " << full_cls << ".Process( const iprot: IProtocol; "
- "const oprot: IProtocol; const events "
- ": IProcessorEvents): Boolean;" << endl;
- ;
+ indent_impl(s_service_impl) << "function " << full_cls << ".Process( const iprot: IProtocol; const oprot: IProtocol; const events : IProcessorEvents): Boolean;" << endl;;
indent_impl(s_service_impl) << "var" << endl;
indent_up_impl();
indent_impl(s_service_impl) << "msg : Thrift.Protocol.IMessage;" << endl;
indent_impl(s_service_impl) << "fn : TProcessFunction;" << endl;
indent_impl(s_service_impl) << "x : TApplicationException;" << endl;
- if (events_) {
+ if( events_) {
indent_impl(s_service_impl) << "context : IRequestEvents;" << endl;
}
indent_down_impl();
@@ -2238,13 +1999,8 @@
indent_up_impl();
indent_impl(s_service_impl) << "TProtocolUtil.Skip(iprot, TType.Struct);" << endl;
indent_impl(s_service_impl) << "iprot.ReadMessageEnd();" << endl;
- indent_impl(s_service_impl) << "x := "
- "TApplicationException.Create(TApplicationException."
- "TExceptionType.UnknownMethod, 'Invalid method name: ''' + "
- "msg.Name + '''');" << endl;
- indent_impl(s_service_impl)
- << "msg := Thrift.Protocol.TMessageImpl.Create(msg.Name, TMessageType.Exception, msg.SeqID);"
- << endl;
+ indent_impl(s_service_impl) << "x := TApplicationException.Create(TApplicationException.TExceptionType.UnknownMethod, 'Invalid method name: ''' + msg.Name + '''');" << endl;
+ indent_impl(s_service_impl) << "msg := Thrift.Protocol.TMessageImpl.Create(msg.Name, TMessageType.Exception, msg.SeqID);" << endl;
indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg);" << endl;
indent_impl(s_service_impl) << "x.Write(oprot);" << endl;
indent_impl(s_service_impl) << "oprot.WriteMessageEnd();" << endl;
@@ -2253,7 +2009,7 @@
indent_impl(s_service_impl) << "Exit;" << endl;
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl;
- if (events_) {
+ if( events_) {
indent_impl(s_service_impl) << "if events <> nil" << endl;
indent_impl(s_service_impl) << "then context := events.CreateRequestContext(msg.Name)" << endl;
indent_impl(s_service_impl) << "else context := nil;" << endl;
@@ -2285,12 +2041,14 @@
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl << endl;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
+ {
generate_process_function(tservice, *f_iter);
}
indent_down();
indent(s_service) << "end;" << endl << endl;
+
}
void t_delphi_generator::generate_function_helpers(t_function* tfunction) {
@@ -2304,7 +2062,7 @@
result.append(&success);
}
- t_struct* xs = tfunction->get_xceptions();
+ t_struct *xs = tfunction->get_xceptions();
const vector<t_field*>& fields = xs->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -2312,18 +2070,15 @@
}
generate_delphi_struct_definition(s_service, &result, false, true, true);
- generate_delphi_struct_impl(s_service_impl,
- normalize_clsnm(service_name_, "T") + ".",
- &result,
- false);
+ generate_delphi_struct_impl(s_service_impl, normalize_clsnm( service_name_, "T") + ".", &result, false);
}
void t_delphi_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+ (void) tservice;
string funcname = tfunction->get_name();
- string full_cls = normalize_clsnm(service_name_, "T") + ".TProcessorImpl";
+ string full_cls = normalize_clsnm( service_name_, "T") + ".TProcessorImpl";
- string org_argsname = funcname + "_args";
+ string org_argsname = funcname + "_args";
string args_clsnm = normalize_clsnm(org_argsname, "T");
string args_intfnm = normalize_clsnm(org_argsname, "I");
@@ -2331,9 +2086,10 @@
string result_clsnm = normalize_clsnm(org_resultname, "T");
string result_intfnm = normalize_clsnm(org_resultname, "I");
- indent(s_service) << "procedure " << funcname
- << "_Process( seqid: Integer; const iprot: IProtocol; const oprot: IProtocol"
- << (events_ ? "; const events : IRequestEvents" : "") << ");" << endl;
+ indent(s_service) <<
+ "procedure " << funcname << "_Process( seqid: Integer; const iprot: IProtocol; const oprot: IProtocol" <<
+ (events_ ? "; const events : IRequestEvents" : "") <<
+ ");" << endl;
if (tfunction->is_oneway()) {
indent_impl(s_service_impl) << "// one way processor" << endl;
@@ -2341,10 +2097,10 @@
indent_impl(s_service_impl) << "// both way processor" << endl;
}
- indent_impl(s_service_impl)
- << "procedure " << full_cls << "." << funcname
- << "_Process( seqid: Integer; const iprot: IProtocol; const oprot: IProtocol"
- << (events_ ? "; const events : IRequestEvents" : "") << ");" << endl;
+ indent_impl(s_service_impl) <<
+ "procedure " << full_cls << "." << funcname << "_Process( seqid: Integer; const iprot: IProtocol; const oprot: IProtocol" <<
+ (events_ ? "; const events : IRequestEvents" : "") <<
+ ");" << endl;
indent_impl(s_service_impl) << "var" << endl;
indent_up_impl();
indent_impl(s_service_impl) << "args: " << args_intfnm << ";" << endl;
@@ -2357,13 +2113,13 @@
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
- if (events_) {
+ if( events_) {
indent_impl(s_service_impl) << "if events <> nil then events.PreRead;" << endl;
}
indent_impl(s_service_impl) << "args := " << args_clsnm << "Impl.Create;" << endl;
indent_impl(s_service_impl) << "args.Read(iprot);" << endl;
indent_impl(s_service_impl) << "iprot.ReadMessageEnd();" << endl;
- if (events_) {
+ if( events_) {
indent_impl(s_service_impl) << "if events <> nil then events.PostRead;" << endl;
}
@@ -2388,7 +2144,7 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
s_service_impl << "ret.Success := ";
}
- s_service_impl << "iface_." << normalize_name(tfunction->get_name(), true) << "(";
+ s_service_impl << "iface_." << normalize_name( tfunction->get_name(), true) << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -2401,8 +2157,8 @@
s_service_impl << ");" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent_impl(s_service_impl) << "args." << prop_name(*f_iter)
- << " := " << empty_value((*f_iter)->get_type()) << ";" << endl;
+ indent_impl(s_service_impl) <<
+ "args." << prop_name(*f_iter) << " := " << empty_value((*f_iter)->get_type()) << ";" << endl;
}
if (events_ || (!tfunction->is_oneway() && xceptions.size() > 0)) {
@@ -2410,20 +2166,18 @@
indent_impl(s_service_impl) << "except" << endl;
indent_up_impl();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- indent_impl(s_service_impl) << "on E: " << type_name((*x_iter)->get_type(), true, true)
- << " do" << endl;
+ indent_impl(s_service_impl) << "on E: " << type_name((*x_iter)->get_type(),true,true) << " do" << endl;
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
if (!tfunction->is_oneway()) {
- string factory_name = normalize_clsnm((*x_iter)->get_type()->get_name(), "", true)
- + "Factory";
- indent_impl(s_service_impl) << "ret." << prop_name(*x_iter) << " := E." << factory_name
- << ";" << endl;
+ string factory_name = normalize_clsnm((*x_iter)->get_type()->get_name(),"",true) + "Factory";
+ indent_impl(s_service_impl) <<
+ "ret." << prop_name(*x_iter) << " := E." << factory_name << ";" << endl;
}
indent_down_impl();
indent_impl(s_service_impl) << "end;" << endl;
}
- if (events_) {
+ if( events_) {
indent_impl(s_service_impl) << "on E: Exception do" << endl;
indent_impl(s_service_impl) << "begin" << endl;
indent_up_impl();
@@ -2436,13 +2190,11 @@
indent_impl(s_service_impl) << "end;" << endl;
}
- if (!tfunction->is_oneway()) {
+ if (! tfunction->is_oneway()) {
if (events_) {
indent_impl(s_service_impl) << "if events <> nil then events.PreWrite;" << endl;
}
- indent_impl(s_service_impl) << "msg := Thrift.Protocol.TMessageImpl.Create('"
- << tfunction->get_name() << "', TMessageType.Reply, seqid); "
- << endl;
+ indent_impl(s_service_impl) << "msg := Thrift.Protocol.TMessageImpl.Create('" << tfunction->get_name() << "', TMessageType.Reply, seqid); " << endl;
indent_impl(s_service_impl) << "oprot.WriteMessageBegin( msg); " << endl;
indent_impl(s_service_impl) << "ret.Write(oprot);" << endl;
indent_impl(s_service_impl) << "oprot.WriteMessageEnd();" << endl;
@@ -2458,13 +2210,9 @@
indent_impl(s_service_impl) << "end;" << endl << endl;
}
-void t_delphi_generator::generate_deserialize_field(ostream& out,
- bool is_xception,
- t_field* tfield,
- string prefix,
- ostream& local_vars) {
+void t_delphi_generator::generate_deserialize_field(ostream& out, bool is_xception, t_field* tfield, string prefix, ostream& local_vars) {
t_type* type = tfield->get_type();
- while (type->is_typedef()) {
+ while(type->is_typedef()) {
type = ((t_typedef*)type)->get_type();
}
@@ -2472,16 +2220,18 @@
throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
}
- string name = prefix + prop_name(tfield, is_xception);
+ string name = prefix + prop_name(tfield,is_xception);
if (type->is_struct() || type->is_xception()) {
generate_deserialize_struct(out, (t_struct*)type, name, "");
} else if (type->is_container()) {
generate_deserialize_container(out, is_xception, type, name, local_vars);
} else if (type->is_base_type() || type->is_enum()) {
- indent_impl(out) << name << " := ";
+ indent_impl(out) <<
+ name << " := ";
- if (type->is_enum()) {
+ if (type->is_enum())
+ {
out << type_name(type, false) << "(";
}
@@ -2490,40 +2240,40 @@
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:
- if (((t_base_type*)type)->is_binary()) {
- if (ansistr_binary_) {
- out << "ReadAnsiString();";
+ case t_base_type::TYPE_VOID:
+ throw "compiler error: cannot serialize void field in a struct: " + name;
+ break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ if (ansistr_binary_) {
+ out << "ReadAnsiString();";
+ } else {
+ out << "ReadBinary();";
+ }
} else {
- out << "ReadBinary();";
+ out << "ReadString();";
}
- } else {
- out << "ReadString();";
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "ReadBool();";
- break;
- case t_base_type::TYPE_BYTE:
- out << "ReadByte();";
- break;
- case t_base_type::TYPE_I16:
- out << "ReadI16();";
- break;
- case t_base_type::TYPE_I32:
- out << "ReadI32();";
- break;
- case t_base_type::TYPE_I64:
- out << "ReadI64();";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "ReadDouble();";
- break;
- default:
- throw "compiler error: no Delphi name for base type " + t_base_type::t_base_name(tbase);
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "ReadBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "ReadByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "ReadI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "ReadI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "ReadI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "ReadDouble();";
+ break;
+ default:
+ throw "compiler error: no Delphi name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "ReadI32()";
@@ -2531,33 +2281,24 @@
}
out << endl;
} else {
- printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", tfield->get_name().c_str(), type_name(type).c_str());
}
}
-void t_delphi_generator::generate_deserialize_struct(ostream& out,
- t_struct* tstruct,
- string name,
- string prefix) {
+void t_delphi_generator::generate_deserialize_struct(ostream& out, t_struct* tstruct, string name, string prefix) {
string typ_name;
if (tstruct->is_xception()) {
- typ_name = type_name(tstruct, true, false, true, true);
+ typ_name = type_name(tstruct,true,false,true,true);
} else {
- typ_name = type_name(tstruct, true, false);
+ typ_name = type_name(tstruct,true,false);
}
indent_impl(out) << prefix << name << " := " << typ_name << ".Create;" << endl;
indent_impl(out) << prefix << name << ".Read(iprot);" << endl;
}
-void t_delphi_generator::generate_deserialize_container(ostream& out,
- bool is_xception,
- t_type* ttype,
- string name,
- std::ostream& local_vars) {
+void t_delphi_generator::generate_deserialize_container(ostream& out, bool is_xception, t_type* ttype, string name, std::ostream& local_vars) {
string obj;
string counter;
@@ -2593,8 +2334,9 @@
indent_impl(out) << obj << " := iprot.ReadListBegin();" << endl;
}
- indent_impl(out) << "for " << counter << " := 0 to " << obj << ".Count - 1 do" << endl;
- indent_impl(out) << "begin" << endl;
+ indent_impl(out) <<
+ "for " << counter << " := 0 to " << obj << ".Count - 1 do" << endl;
+ indent_impl(out) << "begin" << endl;
indent_up_impl();
if (ttype->is_map()) {
generate_deserialize_map_element(out, is_xception, (t_map*)ttype, name, local_vars);
@@ -2615,11 +2357,7 @@
}
}
-void t_delphi_generator::generate_deserialize_map_element(ostream& out,
- bool is_xception,
- t_map* tmap,
- string prefix,
- ostream& local_vars) {
+void t_delphi_generator::generate_deserialize_map_element(ostream& out, bool is_xception, t_map* tmap, string prefix, ostream& local_vars) {
string key = tmp("_key");
string val = tmp("_val");
@@ -2634,39 +2372,31 @@
generate_deserialize_field(out, is_xception, &fkey, "", local_vars);
generate_deserialize_field(out, is_xception, &fval, "", local_vars);
- indent_impl(out) << prefix << ".AddOrSetValue( " << key << ", " << val << ");" << endl;
+ indent_impl(out) <<
+ prefix << ".AddOrSetValue( " << key << ", " << val << ");" << endl;
+
}
-void t_delphi_generator::generate_deserialize_set_element(ostream& out,
- bool is_xception,
- t_set* tset,
- string prefix,
- ostream& local_vars) {
+void t_delphi_generator::generate_deserialize_set_element(ostream& out, bool is_xception, t_set* tset, string prefix, ostream& local_vars) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
local_vars << " " << declare_field(&felem) << endl;
generate_deserialize_field(out, is_xception, &felem, "", local_vars);
- indent_impl(out) << prefix << ".Add(" << elem << ");" << endl;
+ indent_impl(out) <<
+ prefix << ".Add(" << elem << ");" << endl;
}
-void t_delphi_generator::generate_deserialize_list_element(ostream& out,
- bool is_xception,
- t_list* tlist,
- string prefix,
- ostream& local_vars) {
+void t_delphi_generator::generate_deserialize_list_element(ostream& out, bool is_xception, t_list* tlist, string prefix, ostream& local_vars) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
local_vars << " " << declare_field(&felem) << endl;
generate_deserialize_field(out, is_xception, &felem, "", local_vars);
- indent_impl(out) << prefix << ".Add(" << elem << ");" << endl;
+ indent_impl(out) <<
+ prefix << ".Add(" << elem << ");" << endl;
}
-void t_delphi_generator::generate_serialize_field(ostream& out,
- bool is_xception,
- t_field* tfield,
- string prefix,
- ostream& local_vars) {
- (void)local_vars;
+void t_delphi_generator::generate_serialize_field(ostream& out, bool is_xception, t_field* tfield, string prefix, ostream& local_vars) {
+ (void) local_vars;
t_type* type = tfield->get_type();
while (type->is_typedef()) {
@@ -2685,47 +2415,48 @@
generate_serialize_container(out, is_xception, type, name, local_vars);
} else if (type->is_base_type() || type->is_enum()) {
- indent_impl(out) << "oprot.";
+ indent_impl(out) <<
+ "oprot.";
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:
- if (((t_base_type*)type)->is_binary()) {
- if (ansistr_binary_) {
- out << "WriteAnsiString(";
+ 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:
+ if (((t_base_type*)type)->is_binary()) {
+ if (ansistr_binary_) {
+ out << "WriteAnsiString(";
+ } else {
+ out << "WriteBinary(";
+ }
} else {
- out << "WriteBinary(";
+ out << "WriteString(";
}
- } else {
- out << "WriteString(";
- }
- out << name << ");";
- break;
- case t_base_type::TYPE_BOOL:
- out << "WriteBool(" << name << ");";
- break;
- case t_base_type::TYPE_BYTE:
- out << "WriteByte(" << name << ");";
- break;
- case t_base_type::TYPE_I16:
- out << "WriteI16(" << name << ");";
- break;
- case t_base_type::TYPE_I32:
- out << "WriteI32(" << name << ");";
- break;
- case t_base_type::TYPE_I64:
- out << "WriteI64(" << name << ");";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "WriteDouble(" << name << ");";
- break;
- default:
- throw "compiler error: no Delphi name for base type " + t_base_type::t_base_name(tbase);
+ out << name << ");";
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "WriteBool(" << name << ");";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "WriteByte(" << name << ");";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "WriteI16(" << name << ");";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "WriteI32(" << name << ");";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "WriteI64(" << name << ");";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "WriteDouble(" << name << ");";
+ break;
+ default:
+ throw "compiler error: no Delphi name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "WriteI32(Integer(" << name << "));";
@@ -2733,49 +2464,45 @@
out << endl;
} else {
printf("DO NOT KNOW HOW TO SERIALIZE '%s%s' TYPE '%s'\n",
- prefix.c_str(),
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ prefix.c_str(),
+ tfield->get_name().c_str(),
+ type_name(type).c_str());
}
}
-void t_delphi_generator::generate_serialize_struct(ostream& out,
- t_struct* tstruct,
- string prefix,
- ostream& local_vars) {
- (void)local_vars;
- (void)tstruct;
- out << indent_impl() << prefix << ".Write(oprot);" << endl;
+void t_delphi_generator::generate_serialize_struct(ostream& out, t_struct* tstruct, string prefix, ostream& local_vars) {
+ (void) local_vars;
+ (void) tstruct;
+ out <<
+ indent_impl() << prefix << ".Write(oprot);" << endl;
}
-void t_delphi_generator::generate_serialize_container(ostream& out,
- bool is_xception,
- t_type* ttype,
- string prefix,
- ostream& local_vars) {
+void t_delphi_generator::generate_serialize_container(ostream& out, bool is_xception, t_type* ttype, string prefix, ostream& local_vars) {
string obj;
if (ttype->is_map()) {
obj = tmp("map");
local_vars << " " << obj << " : IMap;" << endl;
- indent_impl(out) << obj << " := TMapImpl.Create( "
- << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix
- << ".Count);" << endl;
+ indent_impl(out) << obj << " := TMapImpl.Create( " <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".Count);" << endl;
indent_impl(out) << "oprot.WriteMapBegin( " << obj << ");" << endl;
} else if (ttype->is_set()) {
obj = tmp("set_");
local_vars << " " << obj << " : ISet;" << endl;
- indent_impl(out) << obj << " := TSetImpl.Create("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " << prefix
- << ".Count);" << endl;
- indent_impl(out) << "oprot.WriteSetBegin( " << obj << ");" << endl;
+ indent_impl(out) << obj << " := TSetImpl.Create(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".Count);" << endl;
+ indent_impl(out) <<
+ "oprot.WriteSetBegin( " << obj << ");" << endl;
} else if (ttype->is_list()) {
obj = tmp("list_");
local_vars << " " << obj << " : IList;" << endl;
- indent_impl(out) << obj << " := TListImpl.Create("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix
- << ".Count);" << endl;
- indent_impl(out) << "oprot.WriteListBegin( " << obj << ");" << endl;
+ indent_impl(out) << obj << " := TListImpl.Create(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".Count);" << endl;
+ indent_impl(out) <<
+ "oprot.WriteListBegin( " << obj << ");" << endl;
}
string iter = tmp("_iter");
@@ -2785,14 +2512,12 @@
indent_impl(out) << "begin" << endl;
indent_up_impl();
} else if (ttype->is_set()) {
- local_vars << " " << iter << ": " << type_name(((t_set*)ttype)->get_elem_type()) << ";"
- << endl;
+ local_vars << " " << iter << ": " << type_name(((t_set*)ttype)->get_elem_type()) << ";" << endl;
indent_impl(out) << "for " << iter << " in " << prefix << " do" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
} else if (ttype->is_list()) {
- local_vars << " " << iter << ": " << type_name(((t_list*)ttype)->get_elem_type()) << ";"
- << endl;
+ local_vars << " " << iter << ": " << type_name(((t_list*)ttype)->get_elem_type()) << ";" << endl;
indent_impl(out) << "for " << iter << " in " << prefix << " do" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
@@ -2818,74 +2543,52 @@
}
}
-void t_delphi_generator::generate_serialize_map_element(ostream& out,
- bool is_xception,
- t_map* tmap,
- string iter,
- string map,
- ostream& local_vars) {
+void t_delphi_generator::generate_serialize_map_element(ostream& out, bool is_xception, t_map* tmap, string iter, string map, ostream& local_vars) {
t_field kfield(tmap->get_key_type(), iter);
generate_serialize_field(out, is_xception, &kfield, "", local_vars);
t_field vfield(tmap->get_val_type(), map + "[" + iter + "]");
generate_serialize_field(out, is_xception, &vfield, "", local_vars);
}
-void t_delphi_generator::generate_serialize_set_element(ostream& out,
- bool is_xception,
- t_set* tset,
- string iter,
- ostream& local_vars) {
+void t_delphi_generator::generate_serialize_set_element(ostream& out, bool is_xception, t_set* tset, string iter, ostream& local_vars) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, is_xception, &efield, "", local_vars);
}
-void t_delphi_generator::generate_serialize_list_element(ostream& out,
- bool is_xception,
- t_list* tlist,
- string iter,
- ostream& local_vars) {
+void t_delphi_generator::generate_serialize_list_element(ostream& out, bool is_xception, t_list* tlist, string iter, ostream& local_vars) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, is_xception, &efield, "", local_vars);
}
-void t_delphi_generator::generate_property(ostream& out,
- t_field* tfield,
- bool isPublic,
- bool is_xception) {
+void t_delphi_generator::generate_property(ostream& out, t_field* tfield, bool isPublic, bool is_xception) {
generate_delphi_property(out, is_xception, tfield, isPublic, "Get");
}
-void t_delphi_generator::generate_delphi_property(ostream& out,
- bool struct_is_xception,
- t_field* tfield,
- bool isPublic,
- std::string fieldPrefix) {
- (void)isPublic;
+void t_delphi_generator::generate_delphi_property(ostream& out, bool struct_is_xception, t_field* tfield, bool isPublic, std::string fieldPrefix) {
+ (void) isPublic;
t_type* ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- generate_delphi_doc(out, tfield);
- indent(out) << "property " << prop_name(tfield, struct_is_xception) << ": "
- << type_name(ftype, false, true, is_xception, true) << " read "
- << fieldPrefix + prop_name(tfield, struct_is_xception) << " write Set"
- << prop_name(tfield, struct_is_xception) << ";" << endl;
+ generate_delphi_doc(out,tfield);
+ indent(out) << "property " << prop_name(tfield, struct_is_xception) << ": " << type_name(ftype, false, true, is_xception, true) << " read " << fieldPrefix + prop_name(tfield, struct_is_xception)
+ << " write Set" << prop_name(tfield, struct_is_xception) << ";" << endl;
}
std::string t_delphi_generator::prop_name(t_field* tfield, bool is_xception) {
- return prop_name(tfield->get_name(), is_xception);
+ return prop_name(tfield->get_name(), is_xception);
}
std::string t_delphi_generator::prop_name(string name, bool is_xception) {
string ret = name;
ret[0] = toupper(ret[0]);
- return normalize_name(ret, true, is_xception);
+ return normalize_name( ret, true, is_xception);
}
std::string t_delphi_generator::constructor_param_name(string name) {
string ret = name;
ret[0] = toupper(ret[0]);
ret = "A" + ret;
- return normalize_name(ret, false, false);
+ return normalize_name( ret, false, false);
}
string t_delphi_generator::normalize_clsnm(string clsnm, string prefix, bool b_no_check_keyword) {
@@ -2895,30 +2598,22 @@
if (b_no_check_keyword) {
return prefix + clsnm;
} else {
- return normalize_name(prefix + clsnm);
+ return normalize_name( prefix + clsnm);
}
}
-string t_delphi_generator::type_name(t_type* ttype,
- bool b_cls,
- bool b_no_postfix,
- bool b_exception_factory,
- bool b_full_exception_factory) {
+string t_delphi_generator::type_name( t_type* ttype, bool b_cls, bool b_no_postfix, bool b_exception_factory, bool b_full_exception_factory) {
if (ttype->is_typedef()) {
t_typedef* tdef = (t_typedef*)ttype;
- if (tdef->is_forward_typedef()) { // forward types according to THRIFT-2421
- if (tdef->get_type() != NULL) {
- return type_name(tdef->get_type(),
- b_cls,
- b_no_postfix,
- b_exception_factory,
- b_full_exception_factory);
+ if( tdef->is_forward_typedef()) { // forward types according to THRIFT-2421
+ if( tdef->get_type() != NULL) {
+ return type_name( tdef->get_type(), b_cls, b_no_postfix, b_exception_factory, b_full_exception_factory);
} else {
throw "unresolved forward declaration: " + tdef->get_symbolic();
}
} else {
- return normalize_name("T" + tdef->get_symbolic());
+ return normalize_name( "T"+tdef->get_symbolic());
}
}
@@ -2932,16 +2627,16 @@
b_cls = true;
b_no_postfix = true;
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
+ t_map *tmap = (t_map*) ttype;
if (b_cls) {
typ_nm = "TThriftDictionaryImpl";
} else {
typ_nm = "IThriftDictionary";
}
- return typ_nm + "<" + type_name(tmap->get_key_type()) + ", " + type_name(tmap->get_val_type())
- + ">";
+ return typ_nm + "<" + type_name(tmap->get_key_type()) +
+ ", " + type_name(tmap->get_val_type()) + ">";
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
+ t_set* tset = (t_set*) ttype;
if (b_cls) {
typ_nm = "THashSetImpl";
} else {
@@ -2949,7 +2644,7 @@
}
return typ_nm + "<" + type_name(tset->get_elem_type()) + ">";
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
+ t_list* tlist = (t_list*) ttype;
if (b_cls) {
typ_nm = "TThriftListImpl";
} else {
@@ -2966,121 +2661,114 @@
type_prefix = "I";
}
- string nm = normalize_clsnm(ttype->get_name(), type_prefix);
+ string nm = normalize_clsnm( ttype->get_name(), type_prefix);
if (b_exception_factory) {
nm = nm + "Factory";
}
if (b_cls) {
- if (!b_no_postfix) {
+ if (! b_no_postfix) {
nm = nm + "Impl";
}
}
- if (b_exception_factory && b_full_exception_factory) {
- return type_name(ttype, true, true, false, false) + "." + nm;
+ if ( b_exception_factory && b_full_exception_factory) {
+ return type_name( ttype, true, true, false, false ) + "." + nm;
}
return nm;
}
// returns "const " for some argument types
-string t_delphi_generator::input_arg_prefix(t_type* ttype) {
+string t_delphi_generator::input_arg_prefix( t_type* ttype) {
// base types
if (ttype->is_base_type()) {
switch (((t_base_type*)ttype)->get_base()) {
- // these should be const'ed for optimal performamce
- case t_base_type::TYPE_STRING: // refcounted pointer
- case t_base_type::TYPE_I64: // larger than 32 bit
- case t_base_type::TYPE_DOUBLE: // larger than 32 bit
- return "const ";
+ // these should be const'ed for optimal performamce
+ case t_base_type::TYPE_STRING: // refcounted pointer
+ case t_base_type::TYPE_I64: // larger than 32 bit
+ case t_base_type::TYPE_DOUBLE: // larger than 32 bit
+ return "const ";
- // all others don't need to be
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_VOID:
- return "";
+ // all others don't need to be
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_VOID:
+ return "";
- // we better always report any unknown types
- default:
- throw "compiler error: no input_arg_prefix() for base type "
- + t_base_type::t_base_name(((t_base_type*)ttype)->get_base());
+ // we better always report any unknown types
+ default:
+ throw "compiler error: no input_arg_prefix() for base type " + t_base_type::t_base_name(((t_base_type*)ttype)->get_base());
}
- // enums
+ // enums
} else if (ttype->is_enum()) {
- return ""; // usually <= 32 bit
+ return ""; // usually <= 32 bit
- // containers
+ // containers
} else if (ttype->is_map()) {
- return "const "; // refcounted pointer
+ return "const "; // refcounted pointer
} else if (ttype->is_set()) {
- return "const "; // refcounted pointer
+ return "const "; // refcounted pointer
} else if (ttype->is_list()) {
- return "const "; // refcounted pointer
+ return "const "; // refcounted pointer
+
}
// any other type, either TSomething or ISomething
- return "const "; // possibly refcounted pointer
+ return "const "; // possibly refcounted pointer
}
string t_delphi_generator::base_type_name(t_base_type* tbase) {
switch (tbase->get_base()) {
- case t_base_type::TYPE_VOID:
- // no "void" in Delphi language
- return "";
- case t_base_type::TYPE_STRING:
- if (tbase->is_binary()) {
- if (ansistr_binary_) {
- return "AnsiString";
+ case t_base_type::TYPE_VOID:
+ // no "void" in Delphi language
+ return "";
+ case t_base_type::TYPE_STRING:
+ if (tbase->is_binary()) {
+ if ( ansistr_binary_) {
+ return "AnsiString";
+ } else {
+ return "TBytes";
+ }
} else {
- return "TBytes";
+ return "string";
}
- } else {
- return "string";
- }
- case t_base_type::TYPE_BOOL:
- return "Boolean";
- case t_base_type::TYPE_BYTE:
- return "ShortInt";
- case t_base_type::TYPE_I16:
- return "SmallInt";
- case t_base_type::TYPE_I32:
- return "Integer";
- case t_base_type::TYPE_I64:
- return "Int64";
- case t_base_type::TYPE_DOUBLE:
- return "Double";
- default:
- throw "compiler error: no Delphi name for base type "
- + t_base_type::t_base_name(tbase->get_base());
+ case t_base_type::TYPE_BOOL:
+ return "Boolean";
+ case t_base_type::TYPE_BYTE:
+ return "ShortInt";
+ case t_base_type::TYPE_I16:
+ return "SmallInt";
+ case t_base_type::TYPE_I32:
+ return "Integer";
+ case t_base_type::TYPE_I64:
+ return "Int64";
+ case t_base_type::TYPE_DOUBLE:
+ return "Double";
+ default:
+ throw "compiler error: no Delphi name for base type " + t_base_type::t_base_name(tbase->get_base());
}
}
-string t_delphi_generator::declare_field(t_field* tfield,
- bool init,
- std::string prefix,
- bool is_xception_class) {
- (void)init;
+string t_delphi_generator::declare_field(t_field* tfield, bool init, std::string prefix, bool is_xception_class) {
+ (void) init;
- t_type* ftype = tfield->get_type();
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- string result = prefix + prop_name(tfield, is_xception_class) + ": "
- + type_name(ftype, false, true, is_xception, true) + ";";
+ string result = prefix + prop_name(tfield, is_xception_class) + ": " + type_name(ftype,false,true,is_xception,true) + ";";
return result;
}
-string t_delphi_generator::function_signature(t_function* tfunction,
- std::string full_cls,
- bool is_xception) {
+string t_delphi_generator::function_signature(t_function* tfunction, std::string full_cls, bool is_xception) {
t_type* ttype = tfunction->get_returntype();
string prefix;
if (full_cls == "") {
@@ -3089,12 +2777,9 @@
prefix = full_cls + ".";
}
if (is_void(ttype)) {
- return "procedure " + prefix + normalize_name(tfunction->get_name(), true, is_xception) + "("
- + argument_list(tfunction->get_arglist()) + ");";
+ return "procedure " + prefix + normalize_name(tfunction->get_name(), true, is_xception) + "(" + argument_list(tfunction->get_arglist()) + ");";
} else {
- return "function " + prefix + normalize_name(tfunction->get_name(), true, is_xception) + "("
- + argument_list(tfunction->get_arglist()) + "): "
- + type_name(ttype, false, true, is_xception, true) + ";";
+ return "function " + prefix + normalize_name(tfunction->get_name(), true, is_xception) + "(" + argument_list(tfunction->get_arglist()) + "): " + type_name(ttype, false, true, is_xception, true) + ";";
}
}
@@ -3113,9 +2798,8 @@
}
tt = (*f_iter)->get_type();
- result += input_arg_prefix(tt); // const?
- result += normalize_name((*f_iter)->get_name()) + ": "
- + type_name(tt, false, true, tt->is_xception(), true);
+ result += input_arg_prefix(tt); // const?
+ result += normalize_name((*f_iter)->get_name()) + ": " + type_name( tt, false, true, tt->is_xception(), true);
}
return result;
}
@@ -3139,23 +2823,22 @@
}
if (line.size() > 80) {
- if (firstline) {
+ if ( firstline ) {
result << endl << newline_indent;
firstline = false;
}
result << line << endl;
line = newline_indent;
- } else if (line.size() > 0) {
+ } else if ( line.size() > 0) {
line += " ";
}
tt = (*f_iter)->get_type();
- line += input_arg_prefix(tt); // const?
- line += constructor_param_name((*f_iter)->get_name()) + ": "
- + type_name(tt, false, true, tt->is_xception(), true);
+ line += input_arg_prefix(tt); // const?
+ line += constructor_param_name((*f_iter)->get_name()) + ": " + type_name( tt, false, true, tt->is_xception(), true);
}
- if (line.size() > 0) {
+ if ( line.size() > 0) {
result << line;
}
@@ -3178,22 +2861,22 @@
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 "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- return "TType.String_";
- case t_base_type::TYPE_BOOL:
- return "TType.Bool_";
- case t_base_type::TYPE_BYTE:
- return "TType.Byte_";
- case t_base_type::TYPE_I16:
- return "TType.I16";
- case t_base_type::TYPE_I32:
- return "TType.I32";
- case t_base_type::TYPE_I64:
- return "TType.I64";
- case t_base_type::TYPE_DOUBLE:
- return "TType.Double_";
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "TType.String_";
+ case t_base_type::TYPE_BOOL:
+ return "TType.Bool_";
+ case t_base_type::TYPE_BYTE:
+ return "TType.Byte_";
+ case t_base_type::TYPE_I16:
+ return "TType.I16";
+ case t_base_type::TYPE_I32:
+ return "TType.I32";
+ case t_base_type::TYPE_I64:
+ return "TType.I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "TType.Double_";
}
} else if (type->is_enum()) {
return "TType.I32";
@@ -3218,27 +2901,27 @@
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:
- return "0";
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- if (ansistr_binary_) {
- return "''";
+ case t_base_type::TYPE_VOID:
+ return "0";
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ if (ansistr_binary_) {
+ return "''";
+ } else {
+ return "nil";
+ }
} else {
- return "nil";
+ return "''";
}
- } else {
- return "''";
- }
- case t_base_type::TYPE_BOOL:
- return "False";
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- return "0";
- case t_base_type::TYPE_DOUBLE:
- return "0.0";
+ case t_base_type::TYPE_BOOL:
+ return "False";
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ return "0";
+ case t_base_type::TYPE_DOUBLE:
+ return "0.0";
}
} else if (type->is_enum()) {
return "T" + type->get_name() + "(0)";
@@ -3255,86 +2938,53 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
-void t_delphi_generator::generate_delphi_property_writer_definition(ostream& out,
- t_field* tfield,
- bool is_xception_class) {
- t_type* ftype = tfield->get_type();
+void t_delphi_generator::generate_delphi_property_writer_definition(ostream& out, t_field* tfield, bool is_xception_class) {
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- indent(out) << "procedure Set" << prop_name(tfield, is_xception_class)
- << "( const Value: " << type_name(ftype, false, true, is_xception, true) << ");"
- << endl;
+ indent(out) << "procedure Set" << prop_name(tfield, is_xception_class) << "( const Value: " << type_name(ftype,false,true,is_xception,true) << ");" << endl;
}
-void t_delphi_generator::generate_delphi_property_reader_definition(ostream& out,
- t_field* tfield,
- bool is_xception_class) {
- t_type* ftype = tfield->get_type();
+void t_delphi_generator::generate_delphi_property_reader_definition(ostream& out, t_field* tfield, bool is_xception_class) {
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- indent(out) << "function Get" << prop_name(tfield, is_xception_class) << ": "
- << type_name(ftype, false, true, is_xception, true) << ";" << endl;
+ indent(out) << "function Get" << prop_name(tfield, is_xception_class) << ": " << type_name(ftype,false,true,is_xception,true) << ";" << endl;
}
-void t_delphi_generator::generate_delphi_isset_reader_definition(ostream& out,
- t_field* tfield,
- bool is_xception) {
- indent(out) << "function Get__isset_" << prop_name(tfield, is_xception) << ": Boolean;" << endl;
+void t_delphi_generator::generate_delphi_isset_reader_definition(ostream& out, t_field* tfield, bool is_xception) {
+ indent(out) << "function Get__isset_" << prop_name( tfield, is_xception) << ": Boolean;" << endl;
}
-void t_delphi_generator::generate_delphi_clear_union_value(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class,
- bool is_union,
- bool is_xception_factory,
- std::string xception_factory_name) {
- (void)cls_prefix;
- (void)name;
- (void)type;
- (void)is_union;
- (void)is_xception_factory;
- (void)xception_factory_name;
+void t_delphi_generator::generate_delphi_clear_union_value(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class, bool is_union, bool is_xception_factory, std::string xception_factory_name) {
+ (void) cls_prefix;
+ (void) name;
+ (void) type;
+ (void) is_union;
+ (void) is_xception_factory;
+ (void) xception_factory_name;
- t_type* ftype = tfield->get_type();
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- indent_impl(out) << "if F__isset_" << prop_name(tfield, is_xception_class) << " then begin"
- << endl;
+ indent_impl(out) << "if F__isset_" << prop_name(tfield, is_xception_class) << " then begin" << endl;
indent_up_impl();
indent_impl(out) << "F__isset_" << prop_name(tfield, is_xception_class) << " := False;" << endl;
- indent_impl(out) << fieldPrefix << prop_name(tfield, is_xception_class) << " := "
- << "Default( " << type_name(ftype, false, true, is_xception, true) << ");"
- << endl;
+ indent_impl(out) << fieldPrefix << prop_name(tfield, is_xception_class) << " := " << "Default( " << type_name(ftype,false,true,is_xception,true) << ");" << endl;
indent_down_impl();
indent_impl(out) << "end;" << endl;
}
-void t_delphi_generator::generate_delphi_property_writer_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class,
- bool is_union,
- bool is_xception_factory,
- std::string xception_factroy_name) {
- (void)type;
+void t_delphi_generator::generate_delphi_property_writer_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class, bool is_union, bool is_xception_factory, std::string xception_factroy_name) {
+ (void) type;
- t_type* ftype = tfield->get_type();
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- indent_impl(out) << "procedure " << cls_prefix << name << "."
- << "Set" << prop_name(tfield, is_xception_class)
- << "( const Value: " << type_name(ftype, false, true, is_xception, true) << ");"
- << endl;
+ indent_impl(out) << "procedure " << cls_prefix << name << "." << "Set" << prop_name(tfield, is_xception_class) << "( const Value: " << type_name(ftype,false,true,is_xception,true) << ");" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
- if (is_union) {
+ if ( is_union ) {
indent_impl(out) << "ClearUnionValues;" << endl;
}
if (tfield->get_req() != t_field::T_REQUIRED) {
@@ -3342,50 +2992,33 @@
}
indent_impl(out) << fieldPrefix << prop_name(tfield, is_xception_class) << " := Value;" << endl;
- if (is_xception_class && (!is_xception_factory)) {
- indent_impl(out) << "F" << xception_factroy_name << "." << prop_name(tfield, is_xception_class)
- << " := Value;" << endl;
+ if (is_xception_class && (! is_xception_factory) ) {
+ indent_impl(out) << "F" << xception_factroy_name << "." << prop_name(tfield, is_xception_class) << " := Value;" << endl;
}
indent_down_impl();
indent_impl(out) << "end;" << endl << endl;
}
-void t_delphi_generator::generate_delphi_property_reader_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception_class) {
- (void)type;
+void t_delphi_generator::generate_delphi_property_reader_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception_class) {
+ (void) type;
- t_type* ftype = tfield->get_type();
+ t_type * ftype = tfield->get_type();
bool is_xception = ftype->is_xception();
- indent_impl(out) << "function " << cls_prefix << name << "."
- << "Get" << prop_name(tfield, is_xception_class) << ": "
- << type_name(ftype, false, true, is_xception, true) << ";" << endl;
+ indent_impl(out) << "function " << cls_prefix << name << "." << "Get" << prop_name( tfield, is_xception_class) << ": " << type_name(ftype,false,true,is_xception,true) << ";" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
- indent_impl(out) << "Result := " << fieldPrefix << prop_name(tfield, is_xception_class) << ";"
- << endl;
+ indent_impl(out) << "Result := " << fieldPrefix << prop_name(tfield, is_xception_class) << ";" << endl;
indent_down_impl();
indent_impl(out) << "end;" << endl << endl;
}
-void t_delphi_generator::generate_delphi_isset_reader_impl(ostream& out,
- std::string cls_prefix,
- std::string name,
- t_type* type,
- t_field* tfield,
- std::string fieldPrefix,
- bool is_xception) {
- (void)type;
+void t_delphi_generator::generate_delphi_isset_reader_impl(ostream& out, std::string cls_prefix, std::string name, t_type* type, t_field* tfield, std::string fieldPrefix, bool is_xception) {
+ (void) type;
- string isset_name = "__isset_" + prop_name(tfield, is_xception);
- indent_impl(out) << "function " << cls_prefix << name << "."
- << "Get" << isset_name << ": Boolean;" << endl;
+ string isset_name = "__isset_" + prop_name( tfield, is_xception);
+ indent_impl(out) << "function " << cls_prefix << name << "." << "Get" << isset_name << ": Boolean;" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
indent_impl(out) << "Result := " << fieldPrefix << isset_name << ";" << endl;
@@ -3393,23 +3026,20 @@
indent_impl(out) << "end;" << endl << endl;
}
-void t_delphi_generator::generate_delphi_create_exception_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception) {
- (void)cls_prefix;
+void t_delphi_generator::generate_delphi_create_exception_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception) {
+ (void) cls_prefix;
- string exception_cls_nm = type_name(tstruct, true, true);
- string cls_nm = type_name(tstruct, true, false, is_exception, is_exception);
+ string exception_cls_nm = type_name(tstruct,true,true);
+ string cls_nm = type_name(tstruct,true,false,is_exception,is_exception);
- indent_impl(out) << "function " << cls_nm << ".CreateException: " << exception_cls_nm << ";"
- << endl;
+ indent_impl(out) << "function " << cls_nm << ".CreateException: " << exception_cls_nm << ";" << endl;
indent_impl(out) << "begin" << endl;
indent_up_impl();
+
indent_impl(out) << "Result := " << exception_cls_nm << ".Create;" << endl;
- string factory_name = normalize_clsnm(tstruct->get_name(), "", true) + "Factory";
+ string factory_name = normalize_clsnm(tstruct->get_name(),"",true) + "Factory";
indent_impl(out) << "Result." << factory_name << " := Self;" << endl;
const vector<t_field*>& fields = tstruct->get_members();
@@ -3424,7 +3054,7 @@
indent_impl(out) << "begin" << endl;
indent_up_impl();
}
- indent_impl(out) << "Result." << propname << " := " << propname << ";" << endl;
+ indent_impl(out) << "Result." << propname << " := " << propname << ";" << endl;
if ((*f_iter)->get_req() != t_field::T_REQUIRED) {
indent_down_impl();
indent_impl(out) << "end;" << endl;
@@ -3437,10 +3067,7 @@
indent_impl(out) << "end;" << endl << endl;
}
-void t_delphi_generator::generate_delphi_struct_reader_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception) {
+void t_delphi_generator::generate_delphi_struct_reader_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception) {
ostringstream local_vars;
ostringstream code_block;
@@ -3448,16 +3075,17 @@
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
+
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
// local bools for required fields
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- indent_impl(local_vars) << "_req_isset_" << prop_name(*f_iter, is_exception) << " : Boolean;"
- << endl;
- indent_impl(code_block) << "_req_isset_" << prop_name(*f_iter, is_exception) << " := FALSE;"
- << endl;
+ indent_impl(local_vars) <<
+ "_req_isset_" << prop_name(*f_iter, is_exception) << " : Boolean;" << endl;
+ indent_impl(code_block) <<
+ "_req_isset_" << prop_name(*f_iter, is_exception) << " := FALSE;" << endl;
}
}
@@ -3479,6 +3107,7 @@
indent_down_impl();
indent_impl(code_block) << "end;" << endl;
+
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -3494,17 +3123,16 @@
}
indent_impl(code_block) << (*f_iter)->get_key() << ": begin" << endl;
indent_up_impl();
- indent_impl(code_block) << "if (field_.Type_ = " << type_to_enum((*f_iter)->get_type())
- << ") then" << endl;
+ indent_impl(code_block) << "if (field_.Type_ = " << type_to_enum((*f_iter)->get_type()) << ") then" << endl;
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
- generate_deserialize_field(code_block, is_exception, *f_iter, "", local_vars);
+ generate_deserialize_field(code_block, is_exception, *f_iter, "", local_vars);
// required field?
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- indent_impl(code_block) << "_req_isset_" << prop_name(*f_iter, is_exception) << " := TRUE;"
- << endl;
+ indent_impl(code_block) <<
+ "_req_isset_" << prop_name(*f_iter, is_exception) << " := TRUE;" << endl;
}
indent_down_impl();
@@ -3517,9 +3145,10 @@
indent_impl(code_block) << "end;" << endl;
indent_down_impl();
indent_impl(code_block) << "end";
+
}
- if (!first) {
+ if (! first) {
code_block << endl;
indent_impl(code_block) << "else begin" << endl;
indent_up_impl();
@@ -3527,7 +3156,7 @@
indent_impl(code_block) << "TProtocolUtil.Skip(iprot, field_.Type_);" << endl;
- if (!first) {
+ if (! first) {
indent_down_impl();
indent_impl(code_block) << "end;" << endl;
indent_down_impl();
@@ -3550,10 +3179,11 @@
// all required fields have been read?
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- indent_impl(code_block) << "if not _req_isset_" << prop_name(*f_iter, is_exception) << endl;
- indent_impl(code_block)
- << "then raise TProtocolException.Create( TProtocolException.INVALID_DATA, '"
- << prop_name(*f_iter, is_exception) << "');" << endl;
+ indent_impl(code_block) <<
+ "if not _req_isset_" << prop_name(*f_iter, is_exception) << endl;
+ indent_impl(code_block) <<
+ "then raise TProtocolException.Create( TProtocolException.INVALID_DATA, '" <<
+ prop_name(*f_iter, is_exception) << "');" << endl;
}
}
@@ -3562,10 +3192,9 @@
string cls_nm;
- cls_nm = type_name(tstruct, true, false, is_exception, is_exception);
+ cls_nm = type_name(tstruct,true,false,is_exception,is_exception);
- indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Read( const iprot: IProtocol);"
- << endl;
+ indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Read( const iprot: IProtocol);" << endl;
indent_impl(out) << "var" << endl;
indent_up_impl();
indent_impl(out) << "field_ : IField;" << endl;
@@ -3575,10 +3204,7 @@
out << code_block.str();
}
-void t_delphi_generator::generate_delphi_struct_result_writer_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception) {
+void t_delphi_generator::generate_delphi_struct_result_writer_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception) {
ostringstream local_vars;
ostringstream code_block;
@@ -3587,6 +3213,7 @@
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
+
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
@@ -3597,21 +3224,24 @@
if (fields.size() > 0) {
indent_impl(code_block) << "field_ := TFieldImpl.Create;" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent_impl(code_block) << "if (__isset_" << prop_name(*f_iter, is_exception) << ") then"
- << endl;
+ indent_impl(code_block) << "if (__isset_" << prop_name(*f_iter,is_exception) << ") then" << endl;
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
- indent_impl(code_block) << "field_.Name := '" << (*f_iter)->get_name() << "';" << endl;
- indent_impl(code_block) << "field_.Type_ := " << type_to_enum((*f_iter)->get_type()) << ";"
- << endl;
- indent_impl(code_block) << "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
- indent_impl(code_block) << "oprot.WriteFieldBegin(field_);" << endl;
+ indent_impl(code_block) <<
+ "field_.Name := '" << (*f_iter)->get_name() << "';" << endl;
+ indent_impl(code_block) <<
+ "field_.Type_ := " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
+ indent_impl(code_block) <<
+ "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
+ indent_impl(code_block) <<
+ "oprot.WriteFieldBegin(field_);" << endl;
generate_serialize_field(code_block, is_exception, *f_iter, "", local_vars);
indent_impl(code_block) << "oprot.WriteFieldEnd();" << endl;
indent_down_impl();
}
}
+
indent_impl(code_block) << "oprot.WriteFieldStop();" << endl;
indent_impl(code_block) << "oprot.WriteStructEnd();" << endl;
@@ -3620,10 +3250,9 @@
string cls_nm;
- cls_nm = type_name(tstruct, true, false, is_exception, is_exception);
+ cls_nm = type_name(tstruct,true,false,is_exception,is_exception);
- indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Write( const oprot: IProtocol);"
- << endl;
+ indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Write( const oprot: IProtocol);" << endl;
indent_impl(out) << "var" << endl;
indent_up_impl();
indent_impl(out) << "struc : IStruct;" << endl;
@@ -3635,12 +3264,10 @@
out << local_vars.str();
indent_down_impl();
out << code_block.str();
+
}
-void t_delphi_generator::generate_delphi_struct_writer_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception) {
+void t_delphi_generator::generate_delphi_struct_writer_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception) {
ostringstream local_vars;
ostringstream code_block;
@@ -3649,6 +3276,7 @@
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
+
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
@@ -3662,30 +3290,35 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
- bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
+ bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
if (null_allowed) {
- indent_impl(code_block) << "if (" << prop_name((*f_iter), is_exception) << " <> nil)";
+ indent_impl(code_block) <<
+ "if (" << prop_name((*f_iter), is_exception) << " <> nil)";
if (is_optional) {
- code_block << " and __isset_" << prop_name(*f_iter, is_exception);
+ code_block <<
+ " and __isset_" << prop_name(*f_iter,is_exception);
}
- code_block << " then" << endl;
+ code_block <<
+ " then" << endl;
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
} else {
if (is_optional) {
- indent_impl(code_block) << "if (__isset_" << prop_name(*f_iter, is_exception) << ") then"
- << endl;
+ indent_impl(code_block) << "if (__isset_" << prop_name(*f_iter,is_exception) << ") then" << endl;
indent_impl(code_block) << "begin" << endl;
indent_up_impl();
} else {
indent_impl(code_block) << "// required field" << endl;
}
}
- indent_impl(code_block) << "field_.Name := '" << (*f_iter)->get_name() << "';" << endl;
- indent_impl(code_block) << "field_.Type_ := " << type_to_enum((*f_iter)->get_type()) << ";"
- << endl;
- indent_impl(code_block) << "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
- indent_impl(code_block) << "oprot.WriteFieldBegin(field_);" << endl;
+ indent_impl(code_block) <<
+ "field_.Name := '" << (*f_iter)->get_name() << "';" << endl;
+ indent_impl(code_block) <<
+ "field_.Type_ := " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
+ indent_impl(code_block) <<
+ "field_.ID := " << (*f_iter)->get_key() << ";" << endl;
+ indent_impl(code_block) <<
+ "oprot.WriteFieldBegin(field_);" << endl;
generate_serialize_field(code_block, is_exception, *f_iter, "", local_vars);
indent_impl(code_block) << "oprot.WriteFieldEnd();" << endl;
if (null_allowed || is_optional) {
@@ -3702,10 +3335,9 @@
string cls_nm;
- cls_nm = type_name(tstruct, true, false, is_exception, is_exception);
+ cls_nm = type_name(tstruct,true,false,is_exception,is_exception);
- indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Write( const oprot: IProtocol);"
- << endl;
+ indent_impl(out) << "procedure " << cls_prefix << cls_nm << ".Write( const oprot: IProtocol);" << endl;
indent_impl(out) << "var" << endl;
indent_up_impl();
indent_impl(out) << "struc : IStruct;" << endl;
@@ -3715,13 +3347,10 @@
out << local_vars.str();
indent_down_impl();
out << code_block.str();
+
}
-void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out,
- string cls_prefix,
- t_struct* tstruct,
- bool is_exception,
- bool is_x_factory) {
+void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, string cls_prefix, t_struct* tstruct, bool is_exception, bool is_x_factory) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -3729,9 +3358,9 @@
string cls_nm;
if (is_exception) {
- cls_nm = type_name(tstruct, true, (!is_x_factory), is_x_factory, true);
+ cls_nm = type_name(tstruct,true,(! is_x_factory),is_x_factory,true);
} else {
- cls_nm = type_name(tstruct, true, false);
+ cls_nm = type_name(tstruct,true,false);
}
string tmp_sb = tmp("_sb");
@@ -3743,8 +3372,8 @@
indent_up_impl();
indent_impl(out) << tmp_sb << " : TThriftStringBuilder;" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
- if (is_optional) {
+ bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
+ if( is_optional) {
indent_impl(out) << tmp_first << " : Boolean;" << endl;
useFirstFlag = true;
}
@@ -3758,40 +3387,39 @@
indent_impl(out) << "try" << endl;
indent_up_impl();
- if (useFirstFlag) {
+ if( useFirstFlag) {
indent_impl(out) << tmp_first << " := TRUE;" << endl;
}
- bool had_required = false; // set to true after first required field has been processed
+ bool had_required = false; // set to true after first required field has been processed
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
- bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
+ bool is_optional = ((*f_iter)->get_req() != t_field::T_REQUIRED);
if (null_allowed) {
indent_impl(out) << "if (" << prop_name((*f_iter), is_exception) << " <> nil)";
if (is_optional) {
- out << " and __isset_" << prop_name(*f_iter, is_exception);
+ out << " and __isset_" << prop_name(*f_iter,is_exception);
}
out << " then begin" << endl;
indent_up_impl();
} else {
if (is_optional) {
- indent_impl(out) << "if (__isset_" << prop_name(*f_iter, is_exception) << ") then begin"
- << endl;
+ indent_impl(out) << "if (__isset_" << prop_name(*f_iter,is_exception) << ") then begin" << endl;
indent_up_impl();
}
}
- if (useFirstFlag && (!had_required)) {
+ if( useFirstFlag && (! had_required)) {
indent_impl(out) << "if not " << tmp_first << " then " << tmp_sb << ".Append(',');" << endl;
if (is_optional) {
indent_impl(out) << tmp_first << " := FALSE;" << endl;
}
- indent_impl(out) << tmp_sb << ".Append('" << prop_name((*f_iter), is_exception) << ": ');"
- << endl;
+ indent_impl(out) <<
+ tmp_sb << ".Append('" << prop_name((*f_iter), is_exception) << ": ');" << endl;
} else {
- indent_impl(out) << tmp_sb << ".Append(', " << prop_name((*f_iter), is_exception) << ": ');"
- << endl;
+ indent_impl(out) <<
+ tmp_sb << ".Append(', " << prop_name((*f_iter), is_exception) << ": ');" << endl;
}
t_type* ttype = (*f_iter)->get_type();
@@ -3800,15 +3428,14 @@
}
if (ttype->is_xception() || ttype->is_struct()) {
- indent_impl(out) << "if (" << prop_name((*f_iter), is_exception) << " = nil) then " << tmp_sb
- << ".Append('<null>') else " << tmp_sb << ".Append("
- << prop_name((*f_iter), is_exception) << ".ToString());" << endl;
+ indent_impl(out) <<
+ "if (" << prop_name((*f_iter), is_exception) << " = nil) then " << tmp_sb << ".Append('<null>') else " << tmp_sb << ".Append("<< prop_name((*f_iter), is_exception) << ".ToString());" << endl;
} else if (ttype->is_enum()) {
- indent_impl(out) << tmp_sb << ".Append(Integer(" << prop_name((*f_iter), is_exception)
- << "));" << endl;
+ indent_impl(out) <<
+ tmp_sb << ".Append(Integer(" << prop_name((*f_iter), is_exception) << "));" << endl;
} else {
- indent_impl(out) << tmp_sb << ".Append(" << prop_name((*f_iter), is_exception) << ");"
- << endl;
+ indent_impl(out) <<
+ tmp_sb << ".Append(" << prop_name((*f_iter), is_exception) << ");" << endl;
}
if (null_allowed || is_optional) {
@@ -3817,14 +3444,17 @@
}
if (!is_optional) {
- had_required = true; // now __first must be false, so we don't need to check it anymore
+ had_required = true; // now __first must be false, so we don't need to check it anymore
}
}
- indent_impl(out) << tmp_sb << ".Append(')');" << endl;
- indent_impl(out) << "Result := " << tmp_sb << ".ToString;" << endl;
- if (useFirstFlag) {
- indent_impl(out) << "if " << tmp_first << " then {prevent warning};" << endl;
+ indent_impl(out) <<
+ tmp_sb << ".Append(')');" << endl;
+ indent_impl(out) <<
+ "Result := " << tmp_sb << ".ToString;" << endl;
+ if( useFirstFlag) {
+ indent_impl(out) <<
+ "if " << tmp_first << " then {prevent warning};" << endl;
}
indent_down_impl();
@@ -3838,7 +3468,7 @@
indent_impl(out) << "end;" << endl << endl;
}
-bool t_delphi_generator::is_void(t_type* type) {
+bool t_delphi_generator::is_void( t_type* type ) {
while (type->is_typedef()) {
type = ((t_typedef*)type)->get_type();
}
@@ -3852,12 +3482,11 @@
return false;
}
-THRIFT_REGISTER_GENERATOR(
- delphi,
- "delphi",
- " ansistr_binary: Use AnsiString for binary datatype (default is TBytes).\n"
- " register_types: Enable TypeRegistry, allows for creation of struct, union\n"
- " and container instances by interface or TypeInfo()\n"
- " constprefix: Name TConstants classes after IDL to reduce ambiguities\n"
- " events: Enable and use processing events in the generated code.\n"
- " xmldoc: Enable XMLDoc comments for Help Insight etc.\n")
+THRIFT_REGISTER_GENERATOR(delphi, "delphi",
+" ansistr_binary: Use AnsiString for binary datatype (default is TBytes).\n"
+" register_types: Enable TypeRegistry, allows for creation of struct, union\n"
+" and container instances by interface or TypeInfo()\n"
+" constprefix: Name TConstants classes after IDL to reduce ambiguities\n"
+" events: Enable and use processing events in the generated code.\n"
+" xmldoc: Enable XMLDoc comments for Help Insight etc.\n")
+
diff --git a/compiler/cpp/src/generate/t_erl_generator.cc b/compiler/cpp/src/generate/t_erl_generator.cc
index 8c8a2c7..0735ad1 100644
--- a/compiler/cpp/src/generate/t_erl_generator.cc
+++ b/compiler/cpp/src/generate/t_erl_generator.cc
@@ -38,20 +38,22 @@
using std::stringstream;
using std::vector;
-static const std::string endl = "\n"; // avoid ostream << std::endl flushes
+static const std::string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Erlang code generator.
*
*/
class t_erl_generator : public t_generator {
-public:
- t_erl_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_erl_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-erl";
legacy_names_ = (parsed_options.find("legacynames") != parsed_options.end());
@@ -68,21 +70,21 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
- void generate_member_type(std::ostream& out, t_type* type);
- void generate_member_value(std::ostream& out, t_type* type, t_const_value* value);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+ void generate_member_type(std::ostream & out, t_type* type);
+ void generate_member_value(std::ostream & out, t_type* type, t_const_value* value);
- std::string render_member_type(t_field* field);
- std::string render_member_value(t_field* field);
- std::string render_member_requiredness(t_field* field);
+ std::string render_member_type(t_field * field);
+ std::string render_member_value(t_field * field);
+ std::string render_member_requiredness(t_field * field);
- // std::string render_default_value(t_type* type);
- std::string render_default_value(t_field* field);
+// std::string render_default_value(t_type* type);
+ std::string render_default_value(t_field * field);
std::string render_const_value(t_type* type, t_const_value* value);
std::string render_type_term(t_type* ttype, bool expand_structs, bool extended_info = false);
@@ -92,7 +94,7 @@
void generate_erl_struct(t_struct* tstruct, bool is_exception);
void generate_erl_struct_definition(std::ostream& out, t_struct* tstruct);
- void generate_erl_struct_member(std::ostream& out, t_field* tmember);
+ void generate_erl_struct_member(std::ostream& out, t_field * tmember);
void generate_erl_struct_info(std::ostream& out, t_struct* tstruct);
void generate_erl_extended_struct_info(std::ostream& out, t_struct* tstruct);
void generate_erl_function_helpers(t_function* tfunction);
@@ -101,9 +103,9 @@
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_function_info(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_function_info (t_service* tservice, t_function* tfunction);
/**
* Helper rendering functions
@@ -114,14 +116,14 @@
std::string render_includes();
std::string type_name(t_type* ttype);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string type_module(t_type* ttype);
std::string make_safe_for_module_name(std::string in) {
- if (legacy_names_) {
+ if(legacy_names_) {
return decapitalize(in);
} else {
return underscore(in);
@@ -129,7 +131,7 @@
}
std::string atomify(std::string in) {
- if (legacy_names_) {
+ if(legacy_names_) {
return "'" + decapitalize(in) + "'";
} else {
return "'" + in + "'";
@@ -137,7 +139,7 @@
}
std::string constify(std::string in) {
- if (legacy_names_) {
+ if(legacy_names_) {
return capitalize(in);
} else {
return uppercase(in);
@@ -146,8 +148,9 @@
static std::string comment(string in);
-private:
- bool has_default_value(t_field*);
+ private:
+
+ bool has_default_value(t_field *);
/* if true retain pre 0.9.2 naming scheme for functions, atoms and consts */
bool legacy_names_;
@@ -156,10 +159,10 @@
* add function to export list
*/
- void export_function(t_function* tfunction, std::string prefix = "");
+ void export_function(t_function* tfunction, std::string prefix="");
void export_string(std::string name, int num);
- void export_types_function(t_function* tfunction, std::string prefix = "");
+ void export_types_function(t_function* tfunction, std::string prefix="");
void export_types_string(std::string name, int num);
/**
@@ -193,8 +196,10 @@
std::ostringstream f_service_;
std::ofstream f_service_file_;
std::ofstream f_service_hrl_;
+
};
+
/**
* UI for file generation by opening up the necessary file output
* streams.
@@ -218,34 +223,38 @@
hrl_header(f_types_hrl_file_, make_safe_for_module_name(program_name_) + "_types");
- f_types_file_ << erl_autogen_comment() << endl << "-module("
- << make_safe_for_module_name(program_name_) << "_types)." << endl << erl_imports()
- << endl;
+ f_types_file_ <<
+ erl_autogen_comment() << endl <<
+ "-module(" << make_safe_for_module_name(program_name_) << "_types)." << endl <<
+ erl_imports() << endl;
- f_types_file_ << "-include(\"" << make_safe_for_module_name(program_name_) << "_types.hrl\")."
- << endl << endl;
+ f_types_file_ <<
+ "-include(\"" << make_safe_for_module_name(program_name_) << "_types.hrl\")." << endl <<
+ endl;
f_types_hrl_file_ << render_includes() << endl;
// consts file
- string f_consts_name = get_out_dir() + make_safe_for_module_name(program_name_)
- + "_constants.hrl";
+ string f_consts_name = get_out_dir() + make_safe_for_module_name(program_name_) + "_constants.hrl";
f_consts_.open(f_consts_name.c_str());
- f_consts_ << erl_autogen_comment() << endl << erl_imports() << endl << "-include(\""
- << make_safe_for_module_name(program_name_) << "_types.hrl\")." << endl << endl;
+ f_consts_ <<
+ erl_autogen_comment() << endl <<
+ erl_imports() << endl <<
+ "-include(\"" << make_safe_for_module_name(program_name_) << "_types.hrl\")." << endl <<
+ endl;
}
/**
* Boilerplate at beginning and end of header files
*/
void t_erl_generator::hrl_header(ostream& out, string name) {
- out << "-ifndef(_" << name << "_included)." << endl << "-define(_" << name << "_included, yeah)."
- << endl;
+ out << "-ifndef(_" << name << "_included)." << endl <<
+ "-define(_" << name << "_included, yeah)." << endl;
}
void t_erl_generator::hrl_footer(ostream& out, string name) {
- (void)name;
+ (void) name;
out << "-endif." << endl;
}
@@ -256,8 +265,7 @@
const vector<t_program*>& includes = program_->get_includes();
string result = "";
for (size_t i = 0; i < includes.size(); ++i) {
- result += "-include(\"" + make_safe_for_module_name(includes[i]->get_name())
- + "_types.hrl\").\n";
+ result += "-include(\"" + make_safe_for_module_name(includes[i]->get_name()) + "_types.hrl\").\n";
}
if (includes.size() > 0) {
result += "\n";
@@ -269,19 +277,24 @@
* Autogen'd comment
*/
string t_erl_generator::erl_autogen_comment() {
- return std::string("%%\n") + "%% Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + "%%\n" + "%% DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
- + "%%\n";
+ return
+ std::string("%%\n") +
+ "%% Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "%%\n" +
+ "%% DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "%%\n";
}
/**
* Comment out text
*/
-string t_erl_generator::comment(string in) {
+string t_erl_generator::comment(string in)
+{
size_t pos = 0;
in.insert(pos, "%% ");
- while ((pos = in.find_first_of('\n', pos)) != string::npos) {
+ while ( (pos = in.find_first_of('\n', pos)) != string::npos )
+ {
in.insert(++pos, "%% ");
}
return in;
@@ -322,7 +335,7 @@
* @param ttypedef The type definition
*/
void t_erl_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -338,9 +351,13 @@
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
string name = (*c_iter)->get_name();
- indent(f_types_hrl_file_) << "-define(" << constify(make_safe_for_module_name(program_name_))
- << "_" << constify(tenum->get_name()) << "_" << constify(name) << ", "
- << value << ")." << endl;
+ indent(f_types_hrl_file_) <<
+ "-define(" <<
+ constify(make_safe_for_module_name(program_name_)) <<
+ "_" << constify(tenum->get_name()) <<
+ "_" << constify(name) <<
+ ", " << value << ")." <<
+ endl;
}
f_types_hrl_file_ << endl;
@@ -354,8 +371,12 @@
string name = tconst->get_name();
t_const_value* value = tconst->get_value();
- f_consts_ << "-define(" << constify(make_safe_for_module_name(program_name_)) << "_"
- << constify(name) << ", " << render_const_value(type, value) << ")." << endl << endl;
+ f_consts_ <<
+ "-define(" << constify(make_safe_for_module_name(program_name_)) <<
+ "_" << constify(name) <<
+ ", " << render_const_value(type, value) <<
+ ")." << endl <<
+ endl;
}
/**
@@ -433,9 +454,10 @@
out << "dict:from_list([";
map<t_const_value*, t_const_value*>::const_iterator i, end = value->get_map().end();
for (i = value->get_map().begin(); i != end;) {
- out << "{" << render_const_value(ktype, i->first) << ","
+ out << "{"
+ << render_const_value(ktype, i->first) << ","
<< render_const_value(vtype, i->second) << "}";
- if (++i != end) {
+ if ( ++i != end ) {
out << ",";
}
}
@@ -444,9 +466,9 @@
t_type* etype = ((t_set*)type)->get_elem_type();
out << "sets:from_list([";
vector<t_const_value*>::const_iterator i, end = value->get_list().end();
- for (i = value->get_list().begin(); i != end;) {
- out << render_const_value(etype, *i);
- if (++i != end) {
+ for( i = value->get_list().begin(); i != end; ) {
+ out << render_const_value(etype, *i) ;
+ if ( ++i != end ) {
out << ",";
}
}
@@ -461,7 +483,7 @@
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
if (first) {
- first = false;
+ first=false;
} else {
out << ",";
}
@@ -474,8 +496,9 @@
return out.str();
}
+
string t_erl_generator::render_default_value(t_field* field) {
- t_type* type = field->get_type();
+ t_type *type = field->get_type();
if (type->is_struct() || type->is_xception()) {
return "#" + atomify(type->get_name()) + "{}";
} else if (type->is_map()) {
@@ -489,8 +512,8 @@
}
}
-string t_erl_generator::render_member_type(t_field* field) {
- t_type* type = get_true_type(field->get_type());
+string t_erl_generator::render_member_type(t_field * field) {
+ t_type * type = get_true_type(field->get_type());
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
@@ -523,15 +546,12 @@
}
}
-string t_erl_generator::render_member_requiredness(t_field* field) {
- switch (field->get_req()) {
- case t_field::T_REQUIRED:
- return "required";
- case t_field::T_OPTIONAL:
- return "optional";
- default:
- return "undefined";
- }
+string t_erl_generator::render_member_requiredness(t_field * field) {
+ switch(field->get_req()) {
+ case t_field::T_REQUIRED: return "required";
+ case t_field::T_OPTIONAL: return "optional";
+ default: return "undefined";
+ }
}
/**
@@ -555,7 +575,7 @@
* Generates a struct
*/
void t_erl_generator::generate_erl_struct(t_struct* tstruct, bool is_exception) {
- (void)is_exception;
+ (void) is_exception;
generate_erl_struct_definition(f_types_hrl_file_, tstruct);
generate_erl_struct_info(f_info_, tstruct);
generate_erl_extended_struct_info(f_info_ext_, tstruct);
@@ -566,7 +586,8 @@
*
* @param tstruct The struct definition
*/
-void t_erl_generator::generate_erl_struct_definition(ostream& out, t_struct* tstruct) {
+void t_erl_generator::generate_erl_struct_definition(ostream& out, t_struct* tstruct)
+{
indent(out) << "%% struct " << type_name(tstruct) << endl << endl;
std::stringstream buf;
@@ -576,33 +597,36 @@
const vector<t_field*>& members = tstruct->get_members();
for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();) {
generate_erl_struct_member(buf, *m_iter);
- if (++m_iter != members.end()) {
+ if ( ++m_iter != members.end() ) {
buf << "," << endl << field_indent;
}
}
buf << "}).";
out << buf.str() << endl;
- out << "-type " + type_name(tstruct) << "() :: #" + type_name(tstruct) + "{}." << endl << endl;
+ out <<
+ "-type " + type_name(tstruct) << "() :: #" + type_name(tstruct) + "{}."
+ << endl << endl;
}
/**
* Generates the record field definition
*/
-void t_erl_generator::generate_erl_struct_member(ostream& out, t_field* tmember) {
+void t_erl_generator::generate_erl_struct_member(ostream & out, t_field * tmember)
+{
out << atomify(tmember->get_name());
if (has_default_value(tmember))
- out << " = " << render_member_value(tmember);
+ out << " = " << render_member_value(tmember);
out << " :: " << render_member_type(tmember);
}
-bool t_erl_generator::has_default_value(t_field* field) {
- t_type* type = field->get_type();
+bool t_erl_generator::has_default_value(t_field * field) {
+ t_type *type = field->get_type();
if (!field->get_value()) {
- if (field->get_req() == t_field::T_REQUIRED) {
- if (type->is_struct() || type->is_xception() || type->is_map() || type->is_set()
- || type->is_list()) {
+ if ( field->get_req() == t_field::T_REQUIRED) {
+ if (type->is_struct() || type->is_xception() || type->is_map() ||
+ type->is_set() || type->is_list()) {
return true;
} else {
return false;
@@ -615,7 +639,7 @@
}
}
-string t_erl_generator::render_member_value(t_field* field) {
+string t_erl_generator::render_member_value(t_field * field) {
if (!field->get_value()) {
return render_default_value(field);
} else {
@@ -623,6 +647,8 @@
}
}
+
+
/**
* Generates the read method for a struct
*/
@@ -663,13 +689,13 @@
hrl_header(f_service_hrl_, service_name_);
if (tservice->get_extends() != NULL) {
- f_service_hrl_ << "-include(\""
- << make_safe_for_module_name(tservice->get_extends()->get_name())
- << "_thrift.hrl\"). % inherit " << endl;
+ f_service_hrl_ << "-include(\"" <<
+ make_safe_for_module_name(tservice->get_extends()->get_name()) << "_thrift.hrl\"). % inherit " << endl;
}
- f_service_hrl_ << "-include(\"" << make_safe_for_module_name(program_name_) << "_types.hrl\")."
- << endl << endl;
+ f_service_hrl_ <<
+ "-include(\"" << make_safe_for_module_name(program_name_) << "_types.hrl\")." << endl <<
+ endl;
// Generate the three main parts of the service (well, two for now in PHP)
generate_service_helpers(tservice); // cpiro: New Erlang Order
@@ -678,11 +704,13 @@
// indent_down();
- f_service_file_ << erl_autogen_comment() << endl << "-module(" << service_name_ << "_thrift)."
- << endl << "-behaviour(thrift_service)." << endl << endl << erl_imports() << endl;
+ f_service_file_ <<
+ erl_autogen_comment() << endl <<
+ "-module(" << service_name_ << "_thrift)." << endl <<
+ "-behaviour(thrift_service)." << endl << endl <<
+ erl_imports() << endl;
- f_service_file_ << "-include(\"" << make_safe_for_module_name(tservice->get_name())
- << "_thrift.hrl\")." << endl << endl;
+ f_service_file_ << "-include(\"" << make_safe_for_module_name(tservice->get_name()) << "_thrift.hrl\")." << endl << endl;
f_service_file_ << "-export([" << export_lines_.str() << "])." << endl << endl;
@@ -712,7 +740,7 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
generate_erl_function_helpers(*f_iter);
}
- f_service_ << "struct_info(_) -> erlang:error(function_clause)." << endl;
+ f_service_ << "struct_info(_) -> erlang:error(function_clause)." << endl;
}
/**
@@ -721,7 +749,7 @@
* @param tfunction The function
*/
void t_erl_generator::generate_erl_function_helpers(t_function* tfunction) {
- (void)tfunction;
+ (void) tfunction;
}
/**
@@ -737,21 +765,23 @@
vector<t_function*>::iterator f_iter;
f_service_ << "%%% interface" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "% " << function_signature(*f_iter) << endl;
+ f_service_ <<
+ indent() << "% " << function_signature(*f_iter) << endl;
generate_function_info(tservice, *f_iter);
}
// Inheritance - pass unknown functions to base class
if (tservice->get_extends() != NULL) {
- indent(f_service_) << "function_info(Function, InfoType) ->" << endl;
- indent_up();
- indent(f_service_) << make_safe_for_module_name(tservice->get_extends()->get_name())
- << "_thrift:function_info(Function, InfoType)." << endl;
- indent_down();
+ indent(f_service_) << "function_info(Function, InfoType) ->" << endl;
+ indent_up();
+ indent(f_service_) << make_safe_for_module_name(tservice->get_extends()->get_name())
+ << "_thrift:function_info(Function, InfoType)." << endl;
+ indent_down();
} else {
- // return function_clause error for non-existent functions
- indent(f_service_) << "function_info(_Func, _Info) -> erlang:error(function_clause)." << endl;
+ // return function_clause error for non-existent functions
+ indent(f_service_) << "function_info(_Func, _Info) -> erlang:error(function_clause)."
+ << endl;
}
indent(f_service_) << endl;
@@ -761,15 +791,17 @@
* Generates a function_info(FunctionName, params_type) and
* function_info(FunctionName, reply_type)
*/
-void t_erl_generator::generate_function_info(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_erl_generator::generate_function_info(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
string name_atom = atomify(tfunction->get_name());
t_struct* xs = tfunction->get_xceptions();
t_struct* arg_struct = tfunction->get_arglist();
// function_info(Function, params_type):
- indent(f_service_) << "function_info(" << name_atom << ", params_type) ->" << endl;
+ indent(f_service_) <<
+ "function_info(" << name_atom << ", params_type) ->" << endl;
indent_up();
indent(f_service_) << render_type_term(arg_struct, true) << ";" << endl;
@@ -777,20 +809,22 @@
indent_down();
// function_info(Function, reply_type):
- indent(f_service_) << "function_info(" << name_atom << ", reply_type) ->" << endl;
+ indent(f_service_) <<
+ "function_info(" << name_atom << ", reply_type) ->" << endl;
indent_up();
if (!tfunction->get_returntype()->is_void())
- indent(f_service_) << render_type_term(tfunction->get_returntype(), false) << ";" << endl;
+ indent(f_service_) <<
+ render_type_term(tfunction->get_returntype(), false) << ";" << endl;
else if (tfunction->is_oneway())
indent(f_service_) << "oneway_void;" << endl;
else
- indent(f_service_) << "{struct, []}"
- << ";" << endl;
+ indent(f_service_) << "{struct, []}" << ";" << endl;
indent_down();
// function_info(Function, exceptions):
- indent(f_service_) << "function_info(" << name_atom << ", exceptions) ->" << endl;
+ indent(f_service_) <<
+ "function_info(" << name_atom << ", exceptions) ->" << endl;
indent_up();
indent(f_service_) << render_type_term(xs, true) << ";" << endl;
indent_down();
@@ -802,9 +836,11 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_erl_generator::function_signature(t_function* tfunction, string prefix) {
- return prefix + tfunction->get_name() + "(This"
- + capitalize(argument_list(tfunction->get_arglist())) + ")";
+string t_erl_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ return
+ prefix + tfunction->get_name() +
+ "(This" + capitalize(argument_list(tfunction->get_arglist())) + ")";
}
/**
@@ -819,11 +855,13 @@
export_lines_ << name << "/" << num;
}
-void t_erl_generator::export_types_function(t_function* tfunction, string prefix) {
+void t_erl_generator::export_types_function(t_function* tfunction,
+ string prefix) {
export_types_string(prefix + tfunction->get_name(),
1 // This
- + ((tfunction->get_arglist())->get_members()).size());
+ + ((tfunction->get_arglist())->get_members()).size()
+ );
}
void t_erl_generator::export_types_string(string name, int num) {
@@ -835,13 +873,16 @@
export_types_lines_ << name << "/" << num;
}
-void t_erl_generator::export_function(t_function* tfunction, string prefix) {
+void t_erl_generator::export_function(t_function* tfunction,
+ string prefix) {
export_string(prefix + tfunction->get_name(),
1 // This
- + ((tfunction->get_arglist())->get_members()).size());
+ + ((tfunction->get_arglist())->get_members()).size()
+ );
}
+
/**
* Renders a field list
*/
@@ -915,13 +956,12 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
/**
* Generate an Erlang term which represents a thrift type
*/
-std::string t_erl_generator::render_type_term(t_type* type,
- bool expand_structs,
- bool extended_info) {
- type = get_true_type(type);
+std::string t_erl_generator::render_type_term(t_type* type, bool expand_structs, bool extended_info) {
+ type = get_true_type(type);
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
@@ -954,24 +994,30 @@
t_struct::members_type const& fields = static_cast<t_struct*>(type)->get_members();
t_struct::members_type::const_iterator i, end = fields.end();
- for (i = fields.begin(); i != end;) {
+ for( i = fields.begin(); i != end; )
+ {
t_struct::members_type::value_type member = *i;
- int32_t key = member->get_key();
- string type = render_type_term(member->get_type(), false, false); // recursive call
+ int32_t key = member->get_key();
+ string type = render_type_term(member->get_type(), false, false); // recursive call
- if (!extended_info) {
+ if ( !extended_info ) {
// Convert to format: {struct, [{Fid, Type}|...]}
- buf << "{" << key << ", " << type << "}";
+ buf << "{" << key << ", " << type << "}";
} else {
// Convert to format: {struct, [{Fid, Req, Type, Name, Def}|...]}
- string name = member->get_name();
- string value = render_member_value(member);
- string requiredness = render_member_requiredness(member);
- buf << "{" << key << ", " << requiredness << ", " << type << ", " << atomify(name) << ", "
- << value << "}";
+ string name = member->get_name();
+ string value = render_member_value(member);
+ string requiredness = render_member_requiredness(member);
+ buf <<
+ "{" << key <<
+ ", " << requiredness <<
+ ", " << type <<
+ ", " << atomify(name) <<
+ ", " << value <<
+ "}";
}
- if (++i != end) {
+ if ( ++i != end ) {
buf << "," << endl << field_indent;
}
}
@@ -983,19 +1029,19 @@
}
} else if (type->is_map()) {
// {map, KeyType, ValType}
- t_type* key_type = ((t_map*)type)->get_key_type();
- t_type* val_type = ((t_map*)type)->get_val_type();
+ t_type *key_type = ((t_map*)type)->get_key_type();
+ t_type *val_type = ((t_map*)type)->get_val_type();
- return "{map, " + render_type_term(key_type, false) + ", " + render_type_term(val_type, false)
- + "}";
+ return "{map, " + render_type_term(key_type, false) + ", " +
+ render_type_term(val_type, false) + "}";
} else if (type->is_set()) {
- t_type* elem_type = ((t_set*)type)->get_elem_type();
+ t_type *elem_type = ((t_set*)type)->get_elem_type();
return "{set, " + render_type_term(elem_type, false) + "}";
} else if (type->is_list()) {
- t_type* elem_type = ((t_list*)type)->get_elem_type();
+ t_type *elem_type = ((t_list*)type)->get_elem_type();
return "{list, " + render_type_term(elem_type, false) + "}";
}
@@ -1007,7 +1053,7 @@
return make_safe_for_module_name(ttype->get_program()->get_name()) + "_types";
}
-THRIFT_REGISTER_GENERATOR(
- erl,
- "Erlang",
- " legacynames: Output files retain naming conventions of Thrift 0.9.1 and earlier.\n")
+THRIFT_REGISTER_GENERATOR(erl, "Erlang",
+" legacynames: Output files retain naming conventions of Thrift 0.9.1 and earlier.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_generator.cc b/compiler/cpp/src/generate/t_generator.cc
index e7760d7..f04c65a 100644
--- a/compiler/cpp/src/generate/t_generator.cc
+++ b/compiler/cpp/src/generate/t_generator.cc
@@ -76,7 +76,7 @@
close_generator();
}
-string t_generator::escape_string(const string& in) const {
+string t_generator::escape_string(const string &in) const {
string result = "";
for (string::const_iterator it = in.begin(); it < in.end(); it++) {
std::map<char, std::string>::const_iterator res = escape_.find(*it);
@@ -101,24 +101,23 @@
const string& line_prefix,
const string& contents,
const string& comment_end) {
- if (comment_start != "")
- indent(out) << comment_start;
+ if (comment_start != "") indent(out) << comment_start;
stringstream docs(contents, ios_base::in);
- while (!(docs.eof() || docs.fail())) {
+ while ( ! (docs.eof() || docs.fail())) {
char line[1024];
docs.getline(line, 1024);
// Just prnt a newline when the line & prefix are empty.
if (strlen(line) == 0 && line_prefix == "" && !docs.eof()) {
- out << std::endl;
- } else if (strlen(line) > 0 || !docs.eof()) { // skip the empty last line
+ out << std::endl;
+ } else if (strlen(line) > 0 || !docs.eof()) { // skip the empty last line
indent(out) << line_prefix << line << std::endl;
}
}
- if (comment_end != "")
- indent(out) << comment_end;
+ if (comment_end != "") indent(out) << comment_end;
}
+
void t_generator_registry::register_generator(t_generator_factory* factory) {
gen_map_t& the_map = get_generator_map();
if (the_map.find(factory->get_short_name()) != the_map.end()) {
@@ -127,17 +126,18 @@
the_map[factory->get_short_name()] = factory;
}
-t_generator* t_generator_registry::get_generator(t_program* program, const string& options) {
+t_generator* t_generator_registry::get_generator(t_program* program,
+ const string& options) {
string::size_type colon = options.find(':');
string language = options.substr(0, colon);
map<string, string> parsed_options;
if (colon != string::npos) {
- string::size_type pos = colon + 1;
+ string::size_type pos = colon+1;
while (pos != string::npos && pos < options.size()) {
string::size_type next_pos = options.find(',', pos);
- string option = options.substr(pos, next_pos - pos);
- pos = ((next_pos == string::npos) ? next_pos : next_pos + 1);
+ string option = options.substr(pos, next_pos-pos);
+ pos = ((next_pos == string::npos) ? next_pos : next_pos+1);
string::size_type separator = option.find('=');
string key, value;
@@ -146,7 +146,7 @@
value = "";
} else {
key = option.substr(0, separator);
- value = option.substr(separator + 1);
+ value = option.substr(separator+1);
}
parsed_options[key] = value;
@@ -169,9 +169,13 @@
return *the_map;
}
-t_generator_factory::t_generator_factory(const std::string& short_name,
- const std::string& long_name,
- const std::string& documentation)
- : short_name_(short_name), long_name_(long_name), documentation_(documentation) {
+t_generator_factory::t_generator_factory(
+ const std::string& short_name,
+ const std::string& long_name,
+ const std::string& documentation)
+ : short_name_(short_name)
+ , long_name_(long_name)
+ , documentation_(documentation)
+{
t_generator_registry::register_generator(this);
}
diff --git a/compiler/cpp/src/generate/t_generator.h b/compiler/cpp/src/generate/t_generator.h
index 99d878a..04d88d9 100644
--- a/compiler/cpp/src/generate/t_generator.h
+++ b/compiler/cpp/src/generate/t_generator.h
@@ -35,7 +35,7 @@
*
*/
class t_generator {
-public:
+ public:
t_generator(t_program* program) {
tmp_ = 0;
indent_ = 0;
@@ -44,7 +44,7 @@
escape_['\n'] = "\\n";
escape_['\r'] = "\\r";
escape_['\t'] = "\\t";
- escape_['"'] = "\\\"";
+ escape_['"'] = "\\\"";
escape_['\\'] = "\\\\";
}
@@ -74,20 +74,21 @@
* will be called for the above example.
*/
static bool is_valid_namespace(const std::string& sub_namespace) {
- (void)sub_namespace;
+ (void) sub_namespace;
return false;
}
/**
* Escape string to use one in generated sources.
*/
- virtual std::string escape_string(const std::string& in) const;
+ virtual std::string escape_string(const std::string &in) const;
std::string get_escaped_string(t_const_value* constval) {
return escape_string(constval->get_string());
}
-protected:
+ protected:
+
/**
* Optional methods that may be imlemented by subclasses to take necessary
* steps at the beginning or end of code generation.
@@ -102,13 +103,15 @@
* Pure virtual methods implemented by the generator subclasses.
*/
- virtual void generate_typedef(t_typedef* ttypedef) = 0;
- virtual void generate_enum(t_enum* tenum) = 0;
- virtual void generate_const(t_const* tconst) { (void)tconst; }
- virtual void generate_struct(t_struct* tstruct) = 0;
- virtual void generate_service(t_service* tservice) = 0;
- virtual void generate_forward_declaration(t_struct*) {}
- virtual void generate_xception(t_struct* txception) {
+ virtual void generate_typedef (t_typedef* ttypedef) = 0;
+ virtual void generate_enum (t_enum* tenum) = 0;
+ virtual void generate_const (t_const* tconst) {
+ (void) tconst;
+ }
+ virtual void generate_struct (t_struct* tstruct) = 0;
+ virtual void generate_service (t_service* tservice) = 0;
+ virtual void generate_forward_declaration (t_struct*) {}
+ virtual void generate_xception (t_struct* txception) {
// By default exceptions are the same as structs
generate_struct(txception);
}
@@ -116,12 +119,16 @@
/**
* Method to get the program name, may be overridden
*/
- virtual std::string get_program_name(t_program* tprogram) { return tprogram->get_name(); }
+ virtual std::string get_program_name(t_program* tprogram) {
+ return tprogram->get_name();
+ }
/**
* Method to get the service name, may be overridden
*/
- virtual std::string get_service_name(t_service* tservice) { return tservice->get_name(); }
+ virtual std::string get_service_name(t_service* tservice) {
+ return tservice->get_name();
+ }
/**
* Get the current output directory
@@ -148,9 +155,13 @@
* Indentation level modifiers
*/
- void indent_up() { ++indent_; }
+ void indent_up(){
+ ++indent_;
+ }
- void indent_down() { --indent_; }
+ void indent_down() {
+ --indent_;
+ }
/**
* Indentation print function
@@ -167,7 +178,9 @@
/**
* Indentation utility wrapper
*/
- std::ostream& indent(std::ostream& os) { return os << indent(); }
+ std::ostream& indent(std::ostream &os) {
+ return os << indent();
+ }
/**
* Capitalization helpers
@@ -226,7 +239,7 @@
continue;
}
if (underscore) {
- out << (char)toupper(in[i]);
+ out << (char) toupper(in[i]);
underscore = false;
continue;
}
@@ -236,13 +249,15 @@
return out.str();
}
-public:
+ public:
/**
* Get the true type behind a series of typedefs.
*/
- static t_type* get_true_type(t_type* type) { return type->get_true_type(); }
+ static t_type* get_true_type(t_type* type) {
+ return type->get_true_type();
+ }
-protected:
+ protected:
/**
* The program being generated
*/
@@ -270,7 +285,7 @@
*/
std::map<char, std::string> escape_;
-private:
+ private:
/**
* Current code indentation level
*/
diff --git a/compiler/cpp/src/generate/t_generator_registry.h b/compiler/cpp/src/generate/t_generator_registry.h
index a852385..0010e94 100644
--- a/compiler/cpp/src/generate/t_generator_registry.h
+++ b/compiler/cpp/src/generate/t_generator_registry.h
@@ -30,7 +30,7 @@
* - Providing documentation for the generators it produces.
*/
class t_generator_factory {
-public:
+ public:
t_generator_factory(const std::string& short_name,
const std::string& long_name,
const std::string& documentation);
@@ -43,7 +43,8 @@
// Note: parsed_options will not exist beyond the call to get_generator.
const std::map<std::string, std::string>& parsed_options,
// Note: option_string might not exist beyond the call to get_generator.
- const std::string& option_string) = 0;
+ const std::string& option_string)
+ = 0;
virtual bool is_valid_namespace(const std::string& sub_namespace) = 0;
@@ -51,7 +52,7 @@
std::string get_long_name() { return long_name_; }
std::string get_documentation() { return documentation_; }
-private:
+ private:
std::string short_name_;
std::string long_name_;
std::string documentation_;
@@ -59,15 +60,17 @@
template <typename generator>
class t_generator_factory_impl : public t_generator_factory {
-public:
+ public:
t_generator_factory_impl(const std::string& short_name,
const std::string& long_name,
const std::string& documentation)
- : t_generator_factory(short_name, long_name, documentation) {}
+ : t_generator_factory(short_name, long_name, documentation)
+ {}
- virtual t_generator* get_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string) {
+ virtual t_generator* get_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string) {
return new generator(program, parsed_options, option_string);
}
@@ -77,26 +80,30 @@
};
class t_generator_registry {
-public:
+ public:
static void register_generator(t_generator_factory* factory);
- static t_generator* get_generator(t_program* program, const std::string& options);
+ static t_generator* get_generator(t_program* program,
+ const std::string& options);
typedef std::map<std::string, t_generator_factory*> gen_map_t;
static gen_map_t& get_generator_map();
-private:
+ private:
t_generator_registry();
t_generator_registry(const t_generator_registry&);
};
-#define THRIFT_REGISTER_GENERATOR(language, long_name, doc) \
- class t_##language##_generator_factory_impl \
- : public t_generator_factory_impl<t_##language##_generator> { \
- public: \
- t_##language##_generator_factory_impl() \
- : t_generator_factory_impl<t_##language##_generator>(#language, long_name, doc) {} \
- }; \
+#define THRIFT_REGISTER_GENERATOR(language, long_name, doc) \
+ class t_##language##_generator_factory_impl \
+ : public t_generator_factory_impl<t_##language##_generator> \
+ { \
+ public: \
+ t_##language##_generator_factory_impl() \
+ : t_generator_factory_impl<t_##language##_generator>( \
+ #language, long_name, doc) \
+ {} \
+ }; \
static t_##language##_generator_factory_impl _registerer;
#endif
diff --git a/compiler/cpp/src/generate/t_go_generator.cc b/compiler/cpp/src/generate/t_go_generator.cc
index 7610da4..adc4221 100644
--- a/compiler/cpp/src/generate/t_go_generator.cc
+++ b/compiler/cpp/src/generate/t_go_generator.cc
@@ -46,7 +46,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* A helper for automatically formatting the emitted Go code from the Thrift
@@ -57,7 +57,7 @@
* - false, if the formatting process failed, which means the basic output was
* still generated.
*/
-bool format_go_output(const string& file_path);
+bool format_go_output(const string &file_path);
const string default_thrift_import = "git.apache.org/thrift.git/lib/go/thrift";
static std::string package_flag;
@@ -65,634 +65,646 @@
/**
* Go code generator.
*/
-class t_go_generator : public t_generator {
+class t_go_generator : public t_generator
+{
public:
- t_go_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)option_string;
- std::map<std::string, std::string>::const_iterator iter;
- out_dir_base_ = "gen-go";
- gen_thrift_import_ = default_thrift_import;
+ t_go_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program) {
+ (void) option_string;
+ std::map<std::string, std::string>::const_iterator iter;
+ out_dir_base_ = "gen-go";
+ gen_thrift_import_ = default_thrift_import;
- iter = parsed_options.find("package_prefix");
+ iter = parsed_options.find("package_prefix");
- if (iter != parsed_options.end()) {
- gen_package_prefix_ = (iter->second);
+ if (iter != parsed_options.end()) {
+ gen_package_prefix_ = (iter->second);
+ }
+
+ iter = parsed_options.find("thrift_import");
+
+ if (iter != parsed_options.end()) {
+ gen_thrift_import_ = (iter->second);
+ }
+
+ iter = parsed_options.find("package");
+
+ if (iter != parsed_options.end()) {
+ package_flag = (iter->second);
+ }
}
- iter = parsed_options.find("thrift_import");
+ /**
+ * Init and close methods
+ */
- if (iter != parsed_options.end()) {
- gen_thrift_import_ = (iter->second);
- }
+ void init_generator();
+ void close_generator();
- iter = parsed_options.find("package");
+ /**
+ * Program-level generation functions
+ */
- if (iter != parsed_options.end()) {
- package_flag = (iter->second);
- }
- }
+ void generate_typedef(t_typedef* ttypedef);
+ void generate_enum(t_enum* tenum);
+ void generate_const(t_const* tconst);
+ void generate_struct(t_struct* tstruct);
+ void generate_xception(t_struct* txception);
+ void generate_service(t_service* tservice);
- /**
- * Init and close methods
- */
+ std::string render_const_value(t_type* type, t_const_value* value, const string& name);
- void init_generator();
- void close_generator();
+ /**
+ * Struct generation code
+ */
- /**
- * Program-level generation functions
- */
+ void generate_go_struct(t_struct* tstruct, bool is_exception);
+ void generate_go_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception = false, bool is_result = false, bool is_args = false);
+ void generate_go_struct_initializer(std::ofstream& out, t_struct* tstruct, bool is_args_or_result = false);
+ void generate_isset_helpers(std::ofstream& out, t_struct* tstruct, const string& tstruct_name, bool is_result = false);
+ void generate_go_struct_reader(std::ofstream& out, t_struct* tstruct, const string& tstruct_name, bool is_result = false);
+ void generate_go_struct_writer(std::ofstream& out, t_struct* tstruct, const string& tstruct_name, bool is_result = false);
+ void generate_go_function_helpers(t_function* tfunction);
+ void get_publicized_name_and_def_value(t_field* tfield,
+ string* OUT_pub_name,
+ t_const_value** OUT_def_value) const;
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ /**
+ * Service-level generation functions
+ */
- std::string render_const_value(t_type* type, t_const_value* value, const string& name);
+ void generate_service_helpers(t_service* tservice);
+ void generate_service_interface(t_service* tservice);
+ void generate_service_client(t_service* tservice);
+ void generate_service_remote(t_service* tservice);
+ void generate_service_server(t_service* tservice);
+ void generate_process_function(t_service* tservice, t_function* tfunction);
- /**
- * Struct generation code
- */
+ /**
+ * Serialization constructs
+ */
- void generate_go_struct(t_struct* tstruct, bool is_exception);
- void generate_go_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_result = false,
- bool is_args = false);
- void generate_go_struct_initializer(std::ofstream& out,
- t_struct* tstruct,
- bool is_args_or_result = false);
- void generate_isset_helpers(std::ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result = false);
- void generate_go_struct_reader(std::ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result = false);
- void generate_go_struct_writer(std::ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result = false);
- void generate_go_function_helpers(t_function* tfunction);
- void get_publicized_name_and_def_value(t_field* tfield,
- string* OUT_pub_name,
- t_const_value** OUT_def_value) const;
+ void generate_deserialize_field(std::ofstream &out,
+ t_field* tfield,
+ bool declare,
+ std::string prefix = "",
+ bool inclass = false,
+ bool coerceData = false,
+ bool inkey = false,
+ bool in_container = false);
- /**
- * Service-level generation functions
- */
+ void generate_deserialize_struct(std::ofstream &out,
+ t_struct* tstruct,
+ bool is_pointer_field,
+ bool declare,
+ std::string prefix = "");
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_remote(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_deserialize_container(std::ofstream &out,
+ t_type* ttype,
+ bool pointer_field,
+ bool declare,
+ std::string prefix = "");
- /**
- * Serialization constructs
- */
+ void generate_deserialize_set_element(std::ofstream &out,
+ t_set* tset,
+ bool declare,
+ std::string prefix = "");
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- bool declare,
+ void generate_deserialize_map_element(std::ofstream &out,
+ t_map* tmap,
+ bool declare,
+ std::string prefix = "");
+
+ void generate_deserialize_list_element(std::ofstream &out,
+ t_list* tlist,
+ bool declare,
+ std::string prefix = "");
+
+ void generate_serialize_field(std::ofstream &out,
+ t_field* tfield,
std::string prefix = "",
- bool inclass = false,
- bool coerceData = false,
- bool inkey = false,
- bool in_container = false);
+ bool inkey = false);
- void generate_deserialize_struct(std::ofstream& out,
- t_struct* tstruct,
- bool is_pointer_field,
- bool declare,
+ void generate_serialize_struct(std::ofstream &out,
+ t_struct* tstruct,
std::string prefix = "");
- void generate_deserialize_container(std::ofstream& out,
- t_type* ttype,
- bool pointer_field,
- bool declare,
- std::string prefix = "");
+ void generate_serialize_container(std::ofstream &out,
+ t_type* ttype,
+ bool pointer_field,
+ std::string prefix = "");
- void generate_deserialize_set_element(std::ofstream& out,
- t_set* tset,
- bool declare,
- std::string prefix = "");
+ void generate_serialize_map_element(std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_deserialize_map_element(std::ofstream& out,
- t_map* tmap,
- bool declare,
- std::string prefix = "");
+ void generate_serialize_set_element(std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- bool declare,
- std::string prefix = "");
+ void generate_serialize_list_element(std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
- void generate_serialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inkey = false);
+ void generate_go_docstring(std::ofstream& out,
+ t_struct* tstruct);
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_go_docstring(std::ofstream& out,
+ t_function* tfunction);
- void generate_serialize_container(std::ofstream& out,
- t_type* ttype,
- bool pointer_field,
- std::string prefix = "");
+ void generate_go_docstring(std::ofstream& out,
+ t_doc* tdoc,
+ t_struct* tstruct,
+ const char* subheader);
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_go_docstring(std::ofstream& out,
+ t_doc* tdoc);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ /**
+ * Helper rendering functions
+ */
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ std::string go_autogen_comment();
+ std::string go_package();
+ std::string go_imports_begin();
+ std::string go_imports_end();
+ std::string render_includes();
+ std::string render_import_protection();
+ std::string render_fastbinary_includes();
+ std::string declare_argument(t_field* tfield);
+ std::string render_field_initial_value(t_field* tfield,
+ const string& name,
+ bool optional_field);
+ std::string type_name(t_type* ttype);
+ std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string function_signature_if(t_function* tfunction, std::string prefix = "", bool addError = false);
+ std::string argument_list(t_struct* tstruct);
+ std::string type_to_enum(t_type* ttype);
+ std::string type_to_go_type(t_type* ttype, bool is_container_value = false);
+ std::string type_to_go_type_with_opt(t_type* ttype, bool optional_field, bool is_container_value = false);
+ std::string type_to_go_key_type(t_type* ttype);
+ std::string type_to_spec_args(t_type* ttype);
- void generate_go_docstring(std::ofstream& out, t_struct* tstruct);
+ static std::string get_real_go_module(const t_program* program) {
- void generate_go_docstring(std::ofstream& out, t_function* tfunction);
+ if (!package_flag.empty()) {
+ return package_flag;
+ }
+ std::string real_module = program->get_namespace("go");
+ if (!real_module.empty()) {
+ return real_module;
+ }
- void generate_go_docstring(std::ofstream& out,
- t_doc* tdoc,
- t_struct* tstruct,
- const char* subheader);
-
- void generate_go_docstring(std::ofstream& out, t_doc* tdoc);
-
- /**
- * Helper rendering functions
- */
-
- std::string go_autogen_comment();
- std::string go_package();
- std::string go_imports_begin();
- std::string go_imports_end();
- std::string render_includes();
- std::string render_import_protection();
- std::string render_fastbinary_includes();
- std::string declare_argument(t_field* tfield);
- std::string render_field_initial_value(t_field* tfield, const string& name, bool optional_field);
- std::string type_name(t_type* ttype);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
- std::string function_signature_if(t_function* tfunction,
- std::string prefix = "",
- bool addError = false);
- std::string argument_list(t_struct* tstruct);
- std::string type_to_enum(t_type* ttype);
- std::string type_to_go_type(t_type* ttype, bool is_container_value = false);
- std::string type_to_go_type_with_opt(t_type* ttype,
- bool optional_field,
- bool is_container_value = false);
- std::string type_to_go_key_type(t_type* ttype);
- std::string type_to_spec_args(t_type* ttype);
-
- static std::string get_real_go_module(const t_program* program) {
-
- if (!package_flag.empty()) {
- return package_flag;
+ return lowercase(program->get_name());
}
- std::string real_module = program->get_namespace("go");
- if (!real_module.empty()) {
- return real_module;
- }
-
- return lowercase(program->get_name());
- }
private:
- std::string gen_package_prefix_;
- std::string gen_thrift_import_;
- /**
- * File streams
- */
+ std::string gen_package_prefix_;
+ std::string gen_thrift_import_;
- std::ofstream f_types_;
- std::string f_types_name_;
- std::ofstream f_consts_;
- std::string f_consts_name_;
- std::stringstream f_const_values_;
- std::ofstream f_service_;
+ /**
+ * File streams
+ */
- std::string package_name_;
- std::string package_dir_;
+ std::ofstream f_types_;
+ std::string f_types_name_;
+ std::ofstream f_consts_;
+ std::string f_consts_name_;
+ std::stringstream f_const_values_;
+ std::ofstream f_service_;
- static std::string publicize(const std::string& value, bool is_args_or_result = false);
- static std::string new_prefix(const std::string& value);
- static std::string privatize(const std::string& value);
- static std::string variable_name_to_go_name(const std::string& value);
- static bool is_pointer_field(t_field* tfield, bool in_container = false);
- static bool omit_initialization(t_field* tfield);
+ std::string package_name_;
+ std::string package_dir_;
+
+ static std::string publicize(const std::string& value, bool is_args_or_result = false);
+ static std::string new_prefix(const std::string& value);
+ static std::string privatize(const std::string& value);
+ static std::string variable_name_to_go_name(const std::string& value);
+ static bool is_pointer_field(t_field* tfield, bool in_container = false);
+ static bool omit_initialization(t_field* tfield);
};
-// returns true if field initialization can be omitted since it has corresponding go type zero value
-// or default value is not set
+//returns true if field initialization can be omitted since it has corresponding go type zero value
+//or default value is not set
bool t_go_generator::omit_initialization(t_field* tfield) {
- t_const_value* value = tfield->get_value();
- if (!value) {
- return true;
- }
- t_type* type = tfield->get_type()->get_true_type();
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ t_const_value* value = tfield->get_value();
+ if (!value) {
+ return true;
+ }
+ t_type* type = tfield->get_type()->get_true_type();
+ 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 "";
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "";
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- //[]byte are always inline
- return false;
- }
- // strings are pointers if has no default
- return value->get_string().empty();
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ //[]byte are always inline
+ return false;
+ }
+ //strings are pointers if has no default
+ return value->get_string().empty();
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- return value->get_integer() == 0;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- return value->get_integer() == 0;
- } else {
- return value->get_double() == 0.;
- }
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ return value->get_integer()==0;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ return value->get_integer()==0;
+ } else {
+ return value->get_double()==0.;
+ }
+ }
}
- }
- return false;
+ return false;
}
-// Returns true if the type need a reference if used as optional without default
+//Returns true if the type need a reference if used as optional without default
static bool type_need_reference(t_type* type) {
- type = type->get_true_type();
- if (type->is_map() || type->is_set() || type->is_list() || type->is_struct()
- || type->is_xception() || (type->is_string() && ((t_base_type*)type)->is_binary())) {
- return false;
- }
- return true;
+ type = type->get_true_type();
+ if( type->is_map()
+ || type->is_set()
+ || type->is_list()
+ || type->is_struct()
+ || type->is_xception()
+ || (type->is_string() && ((t_base_type*)type)->is_binary() )) {
+ return false;
+ }
+ return true;
}
-// returns false if field could not use comparison to default value as !IsSet*
+//returns false if field could not use comparison to default value as !IsSet*
bool t_go_generator::is_pointer_field(t_field* tfield, bool in_container_value) {
- (void)in_container_value;
- if (tfield->annotations_.count("cpp.ref") != 0) {
- return true;
- }
- t_type* type = tfield->get_type()->get_true_type();
- // Structs in containers are pointers
- if (type->is_struct() || type->is_xception()) {
- return true;
- }
- if (!(tfield->get_req() == t_field::T_OPTIONAL)) {
- return false;
- }
+ (void) in_container_value;
+ if (tfield->annotations_.count("cpp.ref")!=0) {
+ return true;
+ }
+ t_type* type = tfield->get_type()->get_true_type();
+ //Structs in containers are pointers
+ if (type->is_struct() || type->is_xception()) {
+ return true;
+ }
+ if (!(tfield->get_req() == t_field::T_OPTIONAL)) {
+ return false;
+ }
- bool has_default = tfield->get_value();
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ bool has_default = tfield->get_value();
+ 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 "";
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "";
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- //[]byte are always inline
- return false;
- }
- // strings are pointers if has no default
- return !has_default;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ //[]byte are always inline
+ return false;
+ }
+ //strings are pointers if has no default
+ return !has_default;
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- return !has_default;
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ return !has_default;
+ }
+ } else if (type->is_enum()) {
+ return !has_default;
+ } else if (type->is_struct() || type->is_xception()) {
+ return true;
+ } else if (type->is_map()) {
+ return has_default;
+ } else if (type->is_set()) {
+ return has_default;
+ } else if (type->is_list()) {
+ return has_default;
+ } else if (type->is_typedef()) {
+ return has_default;
}
- } else if (type->is_enum()) {
- return !has_default;
- } else if (type->is_struct() || type->is_xception()) {
- return true;
- } else if (type->is_map()) {
- return has_default;
- } else if (type->is_set()) {
- return has_default;
- } else if (type->is_list()) {
- return has_default;
- } else if (type->is_typedef()) {
- return has_default;
- }
- throw "INVALID TYPE IN type_to_go_type: " + type->get_name();
+ throw "INVALID TYPE IN type_to_go_type: " + type->get_name();
}
-std::string t_go_generator::publicize(const std::string& value, bool is_args_or_result) {
- if (value.size() <= 0) {
- return value;
- }
-
- std::string value2(value), prefix;
-
- string::size_type dot_pos = value.rfind('.');
- if (dot_pos != string::npos) {
- prefix = value.substr(0, dot_pos + 1) + prefix;
- value2 = value.substr(dot_pos + 1);
- }
-
- if (!isupper(value2[0])) {
- value2[0] = toupper(value2[0]);
- }
-
- // as long as we are changing things, let's change _ followed by lowercase to capital
- for (string::size_type i = 1; i < value2.size() - 1; ++i) {
- if (value2[i] == '_' && islower(value2[i + 1])) {
- value2.replace(i, 2, 1, toupper(value2[i + 1]));
+std::string t_go_generator::publicize(const std::string& value, bool is_args_or_result)
+{
+ if (value.size() <= 0) {
+ return value;
}
- }
- // final length before further checks, the string may become longer
- size_t len_before = value2.length();
+ std::string value2(value), prefix;
- // IDL identifiers may start with "New" which interferes with the CTOR pattern
- // Adding an extra underscore to all those identifiers solves this
- if ((len_before >= 3) && (value2.substr(0, 3) == "New")) {
- value2 += '_';
- }
-
- // IDL identifiers may end with "Args"/"Result" which interferes with the implicit service
- // function structs
- // Adding another extra underscore to all those identifiers solves this
- // Suppress this check for the actual helper struct names
- if (!is_args_or_result) {
- bool ends_with_args = (len_before >= 4) && (value2.substr(len_before - 4, 4) == "Args");
- bool ends_with_rslt = (len_before >= 6) && (value2.substr(len_before - 6, 6) == "Result");
- if (ends_with_args || ends_with_rslt) {
- value2 += '_';
+ string::size_type dot_pos = value.rfind('.');
+ if (dot_pos != string::npos) {
+ prefix = value.substr(0, dot_pos + 1) + prefix;
+ value2 = value.substr(dot_pos + 1);
}
- }
- return prefix + value2;
+ if (!isupper(value2[0])) {
+ value2[0] = toupper(value2[0]);
+ }
+
+ // as long as we are changing things, let's change _ followed by lowercase to capital
+ for (string::size_type i = 1; i < value2.size() - 1; ++i) {
+ if (value2[i] == '_' && islower(value2[i + 1])) {
+ value2.replace(i, 2, 1, toupper(value2[i + 1]));
+ }
+ }
+
+ // final length before further checks, the string may become longer
+ size_t len_before = value2.length();
+
+ // IDL identifiers may start with "New" which interferes with the CTOR pattern
+ // Adding an extra underscore to all those identifiers solves this
+ if( (len_before >= 3) && (value2.substr(0,3) == "New")) {
+ value2 += '_';
+ }
+
+ // IDL identifiers may end with "Args"/"Result" which interferes with the implicit service function structs
+ // Adding another extra underscore to all those identifiers solves this
+ // Suppress this check for the actual helper struct names
+ if(!is_args_or_result) {
+ bool ends_with_args = (len_before >= 4) && (value2.substr(len_before-4,4) == "Args");
+ bool ends_with_rslt = (len_before >= 6) && (value2.substr(len_before-6,6) == "Result");
+ if( ends_with_args || ends_with_rslt) {
+ value2 += '_';
+ }
+ }
+
+ return prefix + value2;
}
-std::string t_go_generator::new_prefix(const std::string& value) {
- if (value.size() <= 0) {
- return value;
- }
+std::string t_go_generator::new_prefix(const std::string& value)
+{
+ if (value.size() <= 0) {
+ return value;
+ }
- string::size_type dot_pos = value.rfind('.');
- if (dot_pos != string::npos) {
- return value.substr(0, dot_pos + 1) + "New" + publicize(value.substr(dot_pos + 1));
- }
- return "New" + publicize(value);
+ string::size_type dot_pos = value.rfind('.');
+ if (dot_pos != string::npos) {
+ return value.substr(0, dot_pos + 1) + "New" + publicize(value.substr(dot_pos + 1));
+ }
+ return "New" + publicize(value);
}
-std::string t_go_generator::privatize(const std::string& value) {
- if (value.size() <= 0) {
- return value;
- }
-
- std::string value2(value);
-
- if (!islower(value2[0])) {
- value2[0] = tolower(value2[0]);
- }
-
- // as long as we are changing things, let's change _ followed by lowercase to capital
- for (string::size_type i = 1; i < value2.size() - 1; ++i) {
- if (value2[i] == '_' && isalpha(value2[i + 1])) {
- value2.replace(i, 2, 1, toupper(value2[i + 1]));
+std::string t_go_generator::privatize(const std::string& value)
+{
+ if (value.size() <= 0) {
+ return value;
}
- }
- return value2;
+ std::string value2(value);
+
+ if (!islower(value2[0])) {
+ value2[0] = tolower(value2[0]);
+ }
+
+ // as long as we are changing things, let's change _ followed by lowercase to capital
+ for (string::size_type i = 1; i < value2.size() - 1; ++i) {
+ if (value2[i] == '_' && isalpha(value2[i + 1])) {
+ value2.replace(i, 2, 1, toupper(value2[i + 1]));
+ }
+ }
+
+ return value2;
}
-std::string t_go_generator::variable_name_to_go_name(const std::string& value) {
- if (value.size() <= 0) {
- return value;
- }
-
- std::string value2(value);
- std::transform(value2.begin(), value2.end(), value2.begin(), ::tolower);
-
- switch (value[0]) {
- case 'b':
- case 'B':
- if (value2 != "break") {
- return value;
+std::string t_go_generator::variable_name_to_go_name(const std::string& value)
+{
+ if (value.size() <= 0) {
+ return value;
}
- break;
+ std::string value2(value);
+ std::transform(value2.begin(), value2.end(), value2.begin(), ::tolower);
- case 'c':
- case 'C':
- if (value2 != "case" && value2 != "chan" && value2 != "const" && value2 != "continue") {
- return value;
+ switch (value[0]) {
+ case 'b':
+ case 'B':
+ if (value2 != "break") {
+ return value;
+ }
+
+ break;
+
+ case 'c':
+ case 'C':
+ if (value2 != "case" && value2 != "chan" && value2 != "const" && value2 != "continue") {
+ return value;
+ }
+
+ break;
+
+ case 'd':
+ case 'D':
+ if (value2 != "default" && value2 != "defer") {
+ return value;
+ }
+
+ break;
+
+ case 'e':
+ case 'E':
+ if (value2 != "else" && value2 != "error") {
+ return value;
+ }
+
+ break;
+
+ case 'f':
+ case 'F':
+ if (value2 != "fallthrough" && value2 != "for" && value2 != "func") {
+ return value;
+ }
+
+ break;
+
+ case 'g':
+ case 'G':
+ if (value2 != "go" && value2 != "goto") {
+ return value;
+ }
+
+ break;
+
+ case 'i':
+ case 'I':
+ if (value2 != "if" && value2 != "import" && value2 != "interface") {
+ return value;
+ }
+
+ break;
+
+ case 'm':
+ case 'M':
+ if (value2 != "map") {
+ return value;
+ }
+
+ break;
+
+ case 'p':
+ case 'P':
+ if (value2 != "package") {
+ return value;
+ }
+
+ break;
+
+ case 'r':
+ case 'R':
+ if (value2 != "range" && value2 != "return") {
+ return value;
+ }
+
+ break;
+
+ case 's':
+ case 'S':
+ if (value2 != "select" && value2 != "struct" && value2 != "switch") {
+ return value;
+ }
+
+ break;
+
+ case 't':
+ case 'T':
+ if (value2 != "type") {
+ return value;
+ }
+
+ break;
+
+ case 'v':
+ case 'V':
+ if (value2 != "var") {
+ return value;
+ }
+
+ break;
+
+ default:
+ return value;
}
- break;
-
- case 'd':
- case 'D':
- if (value2 != "default" && value2 != "defer") {
- return value;
- }
-
- break;
-
- case 'e':
- case 'E':
- if (value2 != "else" && value2 != "error") {
- return value;
- }
-
- break;
-
- case 'f':
- case 'F':
- if (value2 != "fallthrough" && value2 != "for" && value2 != "func") {
- return value;
- }
-
- break;
-
- case 'g':
- case 'G':
- if (value2 != "go" && value2 != "goto") {
- return value;
- }
-
- break;
-
- case 'i':
- case 'I':
- if (value2 != "if" && value2 != "import" && value2 != "interface") {
- return value;
- }
-
- break;
-
- case 'm':
- case 'M':
- if (value2 != "map") {
- return value;
- }
-
- break;
-
- case 'p':
- case 'P':
- if (value2 != "package") {
- return value;
- }
-
- break;
-
- case 'r':
- case 'R':
- if (value2 != "range" && value2 != "return") {
- return value;
- }
-
- break;
-
- case 's':
- case 'S':
- if (value2 != "select" && value2 != "struct" && value2 != "switch") {
- return value;
- }
-
- break;
-
- case 't':
- case 'T':
- if (value2 != "type") {
- return value;
- }
-
- break;
-
- case 'v':
- case 'V':
- if (value2 != "var") {
- return value;
- }
-
- break;
-
- default:
- return value;
- }
-
- return value2 + "_a1";
+ return value2 + "_a1";
}
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
*
* @param tprogram The program to generate
*/
-void t_go_generator::init_generator() {
- // Make output directory
- string module = get_real_go_module(program_);
- string target = module;
- package_dir_ = get_out_dir();
+void t_go_generator::init_generator()
+{
+ // Make output directory
+ string module = get_real_go_module(program_);
+ string target = module;
+ package_dir_ = get_out_dir();
- while (true) {
- // TODO: Do better error checking here.
- MKDIR(package_dir_.c_str());
+ while (true) {
+ // TODO: Do better error checking here.
+ MKDIR(package_dir_.c_str());
- if (module.empty()) {
- break;
+ if (module.empty()) {
+ break;
+ }
+
+ string::size_type pos = module.find('.');
+
+ if (pos == string::npos) {
+ package_dir_ += "/";
+ package_dir_ += module;
+ package_name_ = module;
+ module.clear();
+ } else {
+ package_dir_ += "/";
+ package_dir_ += module.substr(0, pos);
+ module.erase(0, pos + 1);
+ }
}
- string::size_type pos = module.find('.');
+ string::size_type loc;
- if (pos == string::npos) {
- package_dir_ += "/";
- package_dir_ += module;
- package_name_ = module;
- module.clear();
- } else {
- package_dir_ += "/";
- package_dir_ += module.substr(0, pos);
- module.erase(0, pos + 1);
+ while ((loc = target.find(".")) != string::npos) {
+ target.replace(loc, 1, 1, '/');
}
- }
- string::size_type loc;
+ // Make output files
+ f_types_name_ = package_dir_ + "/" + "ttypes.go";
+ f_types_.open(f_types_name_.c_str());
- while ((loc = target.find(".")) != string::npos) {
- target.replace(loc, 1, 1, '/');
- }
+ f_consts_name_ = package_dir_ + "/" + "constants.go";
+ f_consts_.open(f_consts_name_.c_str());
- // Make output files
- f_types_name_ = package_dir_ + "/" + "ttypes.go";
- f_types_.open(f_types_name_.c_str());
+ vector<t_service*> services = program_->get_services();
+ vector<t_service*>::iterator sv_iter;
- f_consts_name_ = package_dir_ + "/" + "constants.go";
- f_consts_.open(f_consts_name_.c_str());
+ for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
+ string service_dir = package_dir_ + "/" + underscore((*sv_iter)->get_name()) + "-remote";
+ MKDIR(service_dir.c_str());
+ }
- vector<t_service*> services = program_->get_services();
- vector<t_service*>::iterator sv_iter;
+ // Print header
+ f_types_ <<
+ go_autogen_comment() <<
+ go_package() <<
+ render_includes() <<
+ render_import_protection();
- for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
- string service_dir = package_dir_ + "/" + underscore((*sv_iter)->get_name()) + "-remote";
- MKDIR(service_dir.c_str());
- }
+ f_consts_ <<
+ go_autogen_comment() <<
+ go_package() <<
+ render_includes();
- // Print header
- f_types_ << go_autogen_comment() << go_package() << render_includes()
- << render_import_protection();
+ f_const_values_ << endl << "func init() {" << endl;
- f_consts_ << go_autogen_comment() << go_package() << render_includes();
-
- f_const_values_ << endl << "func init() {" << endl;
}
/**
* Renders all the imports necessary for including another Thrift program
*/
-string t_go_generator::render_includes() {
- const vector<t_program*>& includes = program_->get_includes();
- string result = "";
- string unused_prot = "";
+string t_go_generator::render_includes()
+{
+ const vector<t_program*>& includes = program_->get_includes();
+ string result = "";
+ string unused_prot = "";
- for (size_t i = 0; i < includes.size(); ++i) {
- string go_module = get_real_go_module(includes[i]);
- size_t found = 0;
- for (size_t j = 0; j < go_module.size(); j++) {
- // Import statement uses slashes ('/') in namespace
- if (go_module[j] == '.') {
- go_module[j] = '/';
- found = j + 1;
- }
+ for (size_t i = 0; i < includes.size(); ++i) {
+ string go_module = get_real_go_module(includes[i]);
+ size_t found = 0;
+ for (size_t j = 0; j < go_module.size(); j++) {
+ // Import statement uses slashes ('/') in namespace
+ if (go_module[j] == '.') {
+ go_module[j] = '/';
+ found = j + 1;
+ }
+ }
+
+ result += "\t\"" + gen_package_prefix_ + go_module + "\"\n";
+ unused_prot += "var _ = " + go_module.substr(found) + ".GoUnusedProtection__\n";
}
- result += "\t\"" + gen_package_prefix_ + go_module + "\"\n";
- unused_prot += "var _ = " + go_module.substr(found) + ".GoUnusedProtection__\n";
- }
+ if (includes.size() > 0) {
+ result += "\n";
+ }
- if (includes.size() > 0) {
- result += "\n";
- }
-
- return go_imports_begin() + result + go_imports_end() + unused_prot;
+ return go_imports_begin() + result + go_imports_end() + unused_prot;
}
string t_go_generator::render_import_protection() {
@@ -702,15 +714,17 @@
/**
* Renders all the imports necessary to use the accelerated TBinaryProtocol
*/
-string t_go_generator::render_fastbinary_includes() {
- return "";
+string t_go_generator::render_fastbinary_includes()
+{
+ return "";
}
/**
* Autogen'd comment
*/
-string t_go_generator::go_autogen_comment() {
- return
+string t_go_generator::go_autogen_comment()
+{
+ return
std::string() +
"// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
"// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n\n";
@@ -719,19 +733,21 @@
/**
* Prints standard thrift package
*/
-string t_go_generator::go_package() {
- return string("package ") + package_name_ + "\n\n";
+string t_go_generator::go_package()
+{
+ return string("package ") + package_name_ + "\n\n";
}
/**
* Render the beginning of the import statement
*/
-string t_go_generator::go_imports_begin() {
- return string(
- "import (\n"
- "\t\"bytes\"\n"
- "\t\"fmt\"\n"
- "\t\"" + gen_thrift_import_ + "\"\n");
+string t_go_generator::go_imports_begin()
+{
+ return
+ string("import (\n"
+ "\t\"bytes\"\n"
+ "\t\"fmt\"\n"
+ "\t\"" + gen_thrift_import_ + "\"\n");
}
/**
@@ -740,27 +756,30 @@
* These "_ =" prevent the go compiler complaining about used imports.
* This will have to do in lieu of more intelligent import statement construction
*/
-string t_go_generator::go_imports_end() {
- return string(
- ")\n\n"
- "// (needed to ensure safety because of naive import list construction.)\n"
- "var _ = thrift.ZERO\n"
- "var _ = fmt.Printf\n"
- "var _ = bytes.Equal\n\n");
+string t_go_generator::go_imports_end()
+{
+ return
+ string(
+ ")\n\n"
+ "// (needed to ensure safety because of naive import list construction.)\n"
+ "var _ = thrift.ZERO\n"
+ "var _ = fmt.Printf\n"
+ "var _ = bytes.Equal\n\n");
}
/**
* Closes the type files
*/
-void t_go_generator::close_generator() {
- f_const_values_ << "}" << endl << endl;
- f_consts_ << f_const_values_.str();
+void t_go_generator::close_generator()
+{
+ f_const_values_ << "}" << endl << endl;
+ f_consts_ << f_const_values_.str();
- // Close types and constants files
- f_consts_.close();
- f_types_.close();
- format_go_output(f_types_name_);
- format_go_output(f_consts_name_);
+ // Close types and constants files
+ f_consts_.close();
+ f_types_.close();
+ format_go_output(f_types_name_);
+ format_go_output(f_consts_name_);
}
/**
@@ -768,20 +787,23 @@
*
* @param ttypedef The type definition
*/
-void t_go_generator::generate_typedef(t_typedef* ttypedef) {
- generate_go_docstring(f_types_, ttypedef);
- string new_type_name(publicize(ttypedef->get_symbolic()));
- string base_type(type_to_go_type(ttypedef->get_type()));
+void t_go_generator::generate_typedef(t_typedef* ttypedef)
+{
+ generate_go_docstring(f_types_, ttypedef);
+ string new_type_name(publicize(ttypedef->get_symbolic()));
+ string base_type(type_to_go_type(ttypedef->get_type()));
- if (base_type == new_type_name) {
- return;
- }
+ if (base_type == new_type_name) {
+ return;
+ }
- f_types_ << "type " << new_type_name << " " << base_type << endl << endl;
- // Generate a convenience function that converts an instance of a type
- // (which may be a constant) into a pointer to an instance of a type.
- f_types_ << "func " << new_type_name << "Ptr(v " << new_type_name << ") *" << new_type_name
- << " { return &v }" << endl << endl;
+ f_types_ <<
+ "type " << new_type_name << " " << base_type << endl << endl;
+ // Generate a convenience function that converts an instance of a type
+ // (which may be a constant) into a pointer to an instance of a type.
+ f_types_ <<
+ "func " << new_type_name << "Ptr(v " << new_type_name << ") *" << new_type_name <<
+ " { return &v }" << endl << endl;
}
/**
@@ -790,73 +812,87 @@
*
* @param tenum The enumeration
*/
-void t_go_generator::generate_enum(t_enum* tenum) {
- std::ostringstream to_string_mapping, from_string_mapping;
- std::string tenum_name(publicize(tenum->get_name()));
- generate_go_docstring(f_types_, tenum);
- f_types_ << "type " << tenum_name << " int64" << endl << "const (" << endl;
- to_string_mapping << indent() << "func (p " << tenum_name << ") String() string {" << endl
- << indent() << " switch p {" << endl;
- from_string_mapping << indent() << "func " << tenum_name << "FromString(s string) (" << tenum_name
- << ", error) {" << endl << indent() << " switch s {" << endl;
- vector<t_enum_value*> constants = tenum->get_constants();
- vector<t_enum_value*>::iterator c_iter;
- int value = -1;
+void t_go_generator::generate_enum(t_enum* tenum)
+{
+ std::ostringstream to_string_mapping, from_string_mapping;
+ std::string tenum_name(publicize(tenum->get_name()));
+ generate_go_docstring(f_types_, tenum);
+ f_types_ <<
+ "type " << tenum_name << " int64" << endl <<
+ "const (" << endl;
+ to_string_mapping <<
+ indent() << "func (p " << tenum_name << ") String() string {" << endl <<
+ indent() << " switch p {" << endl;
+ from_string_mapping <<
+ indent() << "func " << tenum_name << "FromString(s string) (" << tenum_name << ", error) {" << endl <<
+ indent() << " switch s {" << endl;
+ vector<t_enum_value*> constants = tenum->get_constants();
+ vector<t_enum_value*>::iterator c_iter;
+ int value = -1;
- for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
- value = (*c_iter)->get_value();
+ for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ value = (*c_iter)->get_value();
- string iter_std_name(escape_string((*c_iter)->get_name()));
- string iter_name((*c_iter)->get_name());
- f_types_ << indent() << " " << tenum_name << "_" << iter_name << ' ' << tenum_name << " = "
- << value << endl;
- // Dictionaries to/from string names of enums
- to_string_mapping << indent() << " case " << tenum_name << "_" << iter_name << ": return \""
- << tenum_name << "_" << iter_std_name << "\"" << endl;
+ string iter_std_name(escape_string((*c_iter)->get_name()));
+ string iter_name((*c_iter)->get_name());
+ f_types_ <<
+ indent() << " " << tenum_name << "_" << iter_name << ' ' << tenum_name << " = " << value << endl;
+ // Dictionaries to/from string names of enums
+ to_string_mapping <<
+ indent() << " case " << tenum_name << "_" << iter_name << ": return \"" << tenum_name << "_" << iter_std_name << "\"" << endl;
- if (iter_std_name != escape_string(iter_name)) {
- from_string_mapping << indent() << " case \"" << tenum_name << "_" << iter_std_name
- << "\", \"" << escape_string(iter_name) << "\": return " << tenum_name
- << "_" << iter_name << ", nil " << endl;
- } else {
- from_string_mapping << indent() << " case \"" << tenum_name << "_" << iter_std_name
- << "\": return " << tenum_name << "_" << iter_name << ", nil " << endl;
+ if (iter_std_name != escape_string(iter_name)) {
+ from_string_mapping <<
+ indent() << " case \"" << tenum_name << "_" << iter_std_name << "\", \"" << escape_string(iter_name) << "\": return " <<
+ tenum_name << "_" << iter_name << ", nil " << endl;
+ } else {
+ from_string_mapping <<
+ indent() << " case \"" << tenum_name << "_" << iter_std_name << "\": return " <<
+ tenum_name << "_" << iter_name << ", nil " << endl;
+ }
}
- }
- to_string_mapping << indent() << " }" << endl << indent() << " return \"<UNSET>\"" << endl
- << indent() << "}" << endl;
- from_string_mapping << indent() << " }" << endl << indent() << " return " << tenum_name
- << "(0),"
- << " fmt.Errorf(\"not a valid " << tenum_name << " string\")" << endl
- << indent() << "}" << endl;
+ to_string_mapping <<
+ indent() << " }" << endl <<
+ indent() << " return \"<UNSET>\"" << endl <<
+ indent() << "}" << endl;
+ from_string_mapping <<
+ indent() << " }" << endl <<
+ indent() << " return " << tenum_name << "(0)," <<
+ " fmt.Errorf(\"not a valid " << tenum_name << " string\")" << endl <<
+ indent() << "}" << endl;
- f_types_ << ")" << endl << endl << to_string_mapping.str() << endl << from_string_mapping.str()
- << endl << endl;
+ f_types_ << ")" << endl << endl
+ << to_string_mapping.str() << endl
+ << from_string_mapping.str() << endl << endl;
- // Generate a convenience function that converts an instance of an enum
- // (which may be a constant) into a pointer to an instance of that enum
- // type.
- f_types_ << "func " << tenum_name << "Ptr(v " << tenum_name << ") *" << tenum_name
- << " { return &v }" << endl << endl;
+ // Generate a convenience function that converts an instance of an enum
+ // (which may be a constant) into a pointer to an instance of that enum
+ // type.
+ f_types_ <<
+ "func " << tenum_name << "Ptr(v " << tenum_name << ") *" << tenum_name <<
+ " { return &v }" << endl << endl;
}
+
/**
* Generate a constant value
*/
-void t_go_generator::generate_const(t_const* tconst) {
- t_type* type = tconst->get_type();
- string name = publicize(tconst->get_name());
- t_const_value* value = tconst->get_value();
+void t_go_generator::generate_const(t_const* tconst)
+{
+ t_type* type = tconst->get_type();
+ string name = publicize(tconst->get_name());
+ t_const_value* value = tconst->get_value();
- if (type->is_base_type() || type->is_enum()) {
- indent(f_consts_) << "const " << name << " = " << render_const_value(type, value, name) << endl;
- } else {
- f_const_values_ << indent() << name << " = " << render_const_value(type, value, name) << endl
- << endl;
+ if (type->is_base_type() || type->is_enum()) {
+ indent(f_consts_) << "const " << name << " = " << render_const_value(type, value, name) << endl;
+ } else {
+ f_const_values_ <<
+ indent() << name << " = " << render_const_value(type, value, name) << endl << endl;
- f_consts_ << indent() << "var " << name << " " << type_to_go_type(type) << endl;
- }
+ f_consts_ <<
+ indent() << "var " << name << " " << type_to_go_type(type) << endl;
+ }
}
/**
@@ -864,137 +900,150 @@
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-string t_go_generator::render_const_value(t_type* type, t_const_value* value, const string& name) {
- type = get_true_type(type);
- std::ostringstream out;
+string t_go_generator::render_const_value(t_type* type, t_const_value* value, const string& name)
+{
+ type = get_true_type(type);
+ std::ostringstream out;
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
- switch (tbase) {
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- out << "[]byte(\"" << get_escaped_string(value) << "\")";
- } else {
- out << '"' << get_escaped_string(value) << '"';
- }
+ switch (tbase) {
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "[]byte(\"" << get_escaped_string(value) << "\")";
+ } else {
+ out << '"' << get_escaped_string(value) << '"';
+ }
- break;
+ break;
- case t_base_type::TYPE_BOOL:
- out << (value->get_integer() > 0 ? "true" : "false");
- break;
+ case t_base_type::TYPE_BOOL:
+ out << (value->get_integer() > 0 ? "true" : "false");
+ break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- out << value->get_integer();
- break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ out << value->get_integer();
+ break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- out << value->get_integer();
- } else {
- out << value->get_double();
- }
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ out << value->get_integer();
+ } else {
+ out << value->get_double();
+ }
- break;
+ break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
- }
- } else if (type->is_enum()) {
- indent(out) << value->get_integer();
- } else if (type->is_struct() || type->is_xception()) {
- out << "&" << publicize(type_name(type)) << "{";
- indent_up();
- const vector<t_field*>& fields = ((t_struct*)type)->get_members();
- vector<t_field*>::const_iterator f_iter;
- const map<t_const_value*, t_const_value*>& val = value->get_map();
- map<t_const_value*, t_const_value*>::const_iterator v_iter;
-
- for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- t_type* field_type = NULL;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_name() == v_iter->first->get_string()) {
- field_type = (*f_iter)->get_type();
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
- }
+ } else if (type->is_enum()) {
+ indent(out) << value->get_integer();
+ } else if (type->is_struct() || type->is_xception()) {
+ out <<
+ "&" << publicize(type_name(type)) << "{";
+ indent_up();
+ const vector<t_field*>& fields = ((t_struct*)type)->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
- if (field_type == NULL) {
- throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
- }
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ t_type* field_type = NULL;
- if (field_type->is_base_type() || field_type->is_enum()) {
- out << endl << indent() << publicize(v_iter->first->get_string()) << ": "
- << render_const_value(field_type, v_iter->second, name) << ",";
- } else {
- string k(tmp("k"));
- string v(tmp("v"));
- out << endl << indent() << v << " := " << render_const_value(field_type, v_iter->second, v)
- << endl << indent() << name << "." << publicize(v_iter->first->get_string()) << " = "
- << v;
- }
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_name() == v_iter->first->get_string()) {
+ field_type = (*f_iter)->get_type();
+ }
+ }
+
+ if (field_type == NULL) {
+ throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+ }
+
+ if (field_type->is_base_type() || field_type->is_enum()) {
+ out << endl <<
+ indent() << publicize(v_iter->first->get_string()) << ": " << render_const_value(field_type, v_iter->second, name) << ",";
+ } else {
+ string k(tmp("k"));
+ string v(tmp("v"));
+ out << endl <<
+ indent() << v << " := " << render_const_value(field_type, v_iter->second, v) << endl <<
+ indent() << name << "." << publicize(v_iter->first->get_string()) << " = " << v;
+ }
+ }
+
+ out << "}";
+
+ indent_down();
+ } else if (type->is_map()) {
+ t_type* ktype = ((t_map*)type)->get_key_type();
+ t_type* vtype = ((t_map*)type)->get_val_type();
+ const map<t_const_value*, t_const_value*>& val = value->get_map();
+ out <<
+ "map[" << type_to_go_type(ktype) << "]" << type_to_go_type(vtype, true) << "{" << endl;
+ indent_up();
+ map<t_const_value*, t_const_value*>::const_iterator v_iter;
+
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ out <<
+ indent() << render_const_value(ktype, v_iter->first, name) << ": " <<
+ render_const_value(vtype, v_iter->second, name) << "," << endl;
+ }
+
+ indent_down();
+ out <<
+ indent() << "}";
+ } else if (type->is_list()) {
+ t_type* etype = ((t_list*)type)->get_elem_type();
+ const vector<t_const_value*>& val = value->get_list();
+ out <<
+ "[]" << type_to_go_type(etype) << "{" << endl;
+ indent_up();
+ vector<t_const_value*>::const_iterator v_iter;
+
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ out <<
+ indent() << render_const_value(etype, *v_iter, name) << ", ";
+ }
+
+ indent_down();
+ out <<
+ indent() << "}";
+ } else if (type->is_set()) {
+ t_type* etype = ((t_set*)type)->get_elem_type();
+ const vector<t_const_value*>& val = value->get_list();
+ out <<
+ "map[" << type_to_go_key_type(etype) << "]bool{" << endl;
+ indent_up();
+ vector<t_const_value*>::const_iterator v_iter;
+
+ for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+ out <<
+ indent() << render_const_value(etype, *v_iter, name) << ": true," << endl;
+
+ }
+
+ indent_down();
+ out <<
+ indent() << "}";
+ } else {
+ throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
}
- out << "}";
-
- indent_down();
- } else if (type->is_map()) {
- t_type* ktype = ((t_map*)type)->get_key_type();
- t_type* vtype = ((t_map*)type)->get_val_type();
- const map<t_const_value*, t_const_value*>& val = value->get_map();
- out << "map[" << type_to_go_type(ktype) << "]" << type_to_go_type(vtype, true) << "{" << endl;
- indent_up();
- map<t_const_value*, t_const_value*>::const_iterator v_iter;
-
- for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- out << indent() << render_const_value(ktype, v_iter->first, name) << ": "
- << render_const_value(vtype, v_iter->second, name) << "," << endl;
- }
-
- indent_down();
- out << indent() << "}";
- } else if (type->is_list()) {
- t_type* etype = ((t_list*)type)->get_elem_type();
- const vector<t_const_value*>& val = value->get_list();
- out << "[]" << type_to_go_type(etype) << "{" << endl;
- indent_up();
- vector<t_const_value*>::const_iterator v_iter;
-
- for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- out << indent() << render_const_value(etype, *v_iter, name) << ", ";
- }
-
- indent_down();
- out << indent() << "}";
- } else if (type->is_set()) {
- t_type* etype = ((t_set*)type)->get_elem_type();
- const vector<t_const_value*>& val = value->get_list();
- out << "map[" << type_to_go_key_type(etype) << "]bool{" << endl;
- indent_up();
- vector<t_const_value*>::const_iterator v_iter;
-
- for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- out << indent() << render_const_value(etype, *v_iter, name) << ": true," << endl;
- }
-
- indent_down();
- out << indent() << "}";
- } else {
- throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
- }
-
- return out.str();
+ return out.str();
}
/**
* Generates a go struct
*/
-void t_go_generator::generate_struct(t_struct* tstruct) {
- generate_go_struct(tstruct, false);
+void t_go_generator::generate_struct(t_struct* tstruct)
+{
+ generate_go_struct(tstruct, false);
}
/**
@@ -1003,46 +1052,47 @@
*
* @param txception The struct definition
*/
-void t_go_generator::generate_xception(t_struct* txception) {
- generate_go_struct(txception, true);
+void t_go_generator::generate_xception(t_struct* txception)
+{
+ generate_go_struct(txception, true);
}
/**
* Generates a go struct
*/
-void t_go_generator::generate_go_struct(t_struct* tstruct, bool is_exception) {
- generate_go_struct_definition(f_types_, tstruct, is_exception);
+void t_go_generator::generate_go_struct(t_struct* tstruct,
+ bool is_exception)
+{
+ generate_go_struct_definition(f_types_, tstruct, is_exception);
}
void t_go_generator::get_publicized_name_and_def_value(t_field* tfield,
string* OUT_pub_name,
t_const_value** OUT_def_value) const {
- const string base_field_name = tfield->get_name();
- const string escaped_field_name = escape_string(base_field_name);
- const string go_safe_name = variable_name_to_go_name(escaped_field_name);
- *OUT_pub_name = publicize(go_safe_name);
- *OUT_def_value = tfield->get_value();
+ const string base_field_name = tfield->get_name();
+ const string escaped_field_name = escape_string(base_field_name);
+ const string go_safe_name = variable_name_to_go_name(escaped_field_name);
+ *OUT_pub_name = publicize(go_safe_name);
+ *OUT_def_value = tfield->get_value();
}
void t_go_generator::generate_go_struct_initializer(ofstream& out,
- t_struct* tstruct,
- bool is_args_or_result) {
- out << publicize(type_name(tstruct), is_args_or_result) << "{";
- const vector<t_field*>& members = tstruct->get_members();
- for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();
- ++m_iter) {
- bool pointer_field = is_pointer_field(*m_iter);
- string publicized_name;
- t_const_value* def_value;
- get_publicized_name_and_def_value(*m_iter, &publicized_name, &def_value);
- if (!pointer_field && def_value != NULL && !omit_initialization(*m_iter)) {
- out << endl << indent() << publicized_name << ": "
- << render_field_initial_value(*m_iter, (*m_iter)->get_name(), pointer_field) << ","
- << endl;
+ t_struct* tstruct,
+ bool is_args_or_result)
+{
+ out << publicize(type_name(tstruct), is_args_or_result) << "{";
+ const vector<t_field*>& members = tstruct->get_members();
+ for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ bool pointer_field = is_pointer_field(*m_iter);
+ string publicized_name;
+ t_const_value* def_value;
+ get_publicized_name_and_def_value(*m_iter, &publicized_name, &def_value);
+ if (!pointer_field && def_value != NULL && !omit_initialization(*m_iter)) {
+ out << endl << indent() << publicized_name << ": " << render_field_initial_value(*m_iter, (*m_iter)->get_name(), pointer_field) << "," << endl;
+ }
}
- }
- out << "}" << endl;
+ out << "}" << endl;
}
/**
@@ -1051,369 +1101,419 @@
* @param tstruct The struct definition
*/
void t_go_generator::generate_go_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool is_result,
- bool is_args) {
- const vector<t_field*>& members = tstruct->get_members();
- const vector<t_field*>& sorted_members = tstruct->get_sorted_members();
- vector<t_field*>::const_iterator m_iter;
+ t_struct* tstruct,
+ bool is_exception,
+ bool is_result,
+ bool is_args)
+{
+ const vector<t_field*>& members = tstruct->get_members();
+ const vector<t_field*>& sorted_members = tstruct->get_sorted_members();
+ vector<t_field*>::const_iterator m_iter;
- std::string tstruct_name(publicize(tstruct->get_name(), is_args || is_result));
- out << indent() << "type " << tstruct_name << " struct {" << endl;
- /*
- Here we generate the structure specification for the fastbinary codec.
- These specifications have the following structure:
- thrift_spec -> tuple of item_spec
- item_spec -> nil | (tag, type_enum, name, spec_args, default)
- tag -> integer
- type_enum -> TType.I32 | TType.STRING | TType.STRUCT | ...
- name -> string_literal
- default -> nil # Handled by __init__
- spec_args -> nil # For simple types
- | (type_enum, spec_args) # Value type for list/set
- | (type_enum, spec_args, type_enum, spec_args)
- # Key and value for map
- | (class_name, spec_args_ptr) # For struct/exception
- class_name -> identifier # Basically a pointer to the class
- spec_args_ptr -> expression # just class_name.spec_args
+ std::string tstruct_name(publicize(tstruct->get_name(), is_args||is_result));
+ out <<
+ indent() << "type " << tstruct_name << " struct {" << endl;
+ /*
+ Here we generate the structure specification for the fastbinary codec.
+ These specifications have the following structure:
+ thrift_spec -> tuple of item_spec
+ item_spec -> nil | (tag, type_enum, name, spec_args, default)
+ tag -> integer
+ type_enum -> TType.I32 | TType.STRING | TType.STRUCT | ...
+ name -> string_literal
+ default -> nil # Handled by __init__
+ spec_args -> nil # For simple types
+ | (type_enum, spec_args) # Value type for list/set
+ | (type_enum, spec_args, type_enum, spec_args)
+ # Key and value for map
+ | (class_name, spec_args_ptr) # For struct/exception
+ class_name -> identifier # Basically a pointer to the class
+ spec_args_ptr -> expression # just class_name.spec_args
- TODO(dreiss): Consider making this work for structs with negative tags.
- */
- // TODO(dreiss): Look into generating an empty tuple instead of nil
- // for structures with no members.
- // TODO(dreiss): Test encoding of structs where some inner structs
- // don't have thrift_spec.
- indent_up();
+ TODO(dreiss): Consider making this work for structs with negative tags.
+ */
+ // TODO(dreiss): Look into generating an empty tuple instead of nil
+ // for structures with no members.
+ // TODO(dreiss): Test encoding of structs where some inner structs
+ // don't have thrift_spec.
+ indent_up();
- if (sorted_members.empty() || (sorted_members[0]->get_key() >= 0)) {
- int sorted_keys_pos = 0;
+ if (sorted_members.empty() || (sorted_members[0]->get_key() >= 0)) {
+ int sorted_keys_pos = 0;
- for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) {
- if (sorted_keys_pos != (*m_iter)->get_key()) {
- int first_unused = std::max(1, sorted_keys_pos++);
- while (sorted_keys_pos != (*m_iter)->get_key()) {
- ++sorted_keys_pos;
+ for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) {
+ if( sorted_keys_pos != (*m_iter)->get_key()) {
+ int first_unused = std::max(1,sorted_keys_pos++);
+ while( sorted_keys_pos != (*m_iter)->get_key()) {
+ ++sorted_keys_pos;
+ }
+ int last_unused = sorted_keys_pos - 1;
+ if (first_unused < last_unused) {
+ indent(out) << "// unused fields # " << first_unused << " to "<< last_unused << endl;
+ } else if (first_unused == last_unused) {
+ indent(out) << "// unused field # " << first_unused << endl;
+ }
+ }
+
+ t_type* fieldType = (*m_iter)->get_type();
+ string goType = type_to_go_type_with_opt(fieldType, is_pointer_field(*m_iter));
+ string gotag("json:\"" + escape_string((*m_iter)->get_name()) + "\"");
+ std::map<string, string>::iterator it = (*m_iter)->annotations_.find("go.tag");
+ if (it != (*m_iter)->annotations_.end()) {
+ gotag = it->second;
+ }
+ indent(out) << publicize(variable_name_to_go_name((*m_iter)->get_name())) << " "
+ << goType << " `thrift:\""
+ << escape_string((*m_iter)->get_name())
+ << "," << sorted_keys_pos;
+
+ if ((*m_iter)->get_req() == t_field::T_REQUIRED) {
+ out << ",required";
+ }
+
+ out << "\" " <<gotag <<"`" << endl;
+ sorted_keys_pos ++;
}
- int last_unused = sorted_keys_pos - 1;
- if (first_unused < last_unused) {
- indent(out) << "// unused fields # " << first_unused << " to " << last_unused << endl;
- } else if (first_unused == last_unused) {
- indent(out) << "// unused field # " << first_unused << endl;
- }
- }
-
- t_type* fieldType = (*m_iter)->get_type();
- string goType = type_to_go_type_with_opt(fieldType, is_pointer_field(*m_iter));
- string gotag("json:\"" + escape_string((*m_iter)->get_name()) + "\"");
- std::map<string, string>::iterator it = (*m_iter)->annotations_.find("go.tag");
- if (it != (*m_iter)->annotations_.end()) {
- gotag = it->second;
- }
- indent(out) << publicize(variable_name_to_go_name((*m_iter)->get_name())) << " " << goType
- << " `thrift:\"" << escape_string((*m_iter)->get_name()) << ","
- << sorted_keys_pos;
-
- if ((*m_iter)->get_req() == t_field::T_REQUIRED) {
- out << ",required";
- }
-
- out << "\" " << gotag << "`" << endl;
- sorted_keys_pos++;
- }
- } else {
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- // This fills in default values, as opposed to nulls
- out << indent() << publicize((*m_iter)->get_name()) << " "
- << type_to_go_type((*m_iter)->get_type()) << endl;
- }
- }
-
- indent_down();
- out << indent() << "}" << endl << endl << indent() << "func New" << tstruct_name << "() *"
- << tstruct_name << " {" << endl << indent() << " return &";
- generate_go_struct_initializer(out, tstruct, is_result || is_args);
- out << indent() << "}" << endl << endl;
- // Default values for optional fields
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- string publicized_name;
- t_const_value* def_value;
- get_publicized_name_and_def_value(*m_iter, &publicized_name, &def_value);
- t_type* fieldType = (*m_iter)->get_type();
- string goType = type_to_go_type_with_opt(fieldType, false);
- string def_var_name = tstruct_name + "_" + publicized_name + "_DEFAULT";
- if ((*m_iter)->get_req() == t_field::T_OPTIONAL || is_pointer_field(*m_iter)) {
- out << indent() << "var " << def_var_name << " " << goType;
- if (def_value != NULL) {
- out << " = " << render_const_value(fieldType, def_value, (*m_iter)->get_name());
- }
- out << endl;
- }
- if (is_pointer_field(*m_iter)) {
- string goOptType = type_to_go_type_with_opt(fieldType, true);
- string maybepointer = goOptType != goType ? "*" : "";
- out << indent() << "func (p *" << tstruct_name << ") Get" << publicized_name << "() "
- << goType << " {" << endl << indent() << " if !p.IsSet" << publicized_name << "() {"
- << endl << indent() << " return " << def_var_name << endl << indent() << " }" << endl
- << indent() << "return " << maybepointer << "p." << publicized_name << endl << indent()
- << "}" << endl;
} else {
- out << endl << indent() << "func (p *" << tstruct_name << ") Get" << publicized_name << "() "
- << goType << " {" << endl << indent() << "return p." << publicized_name << endl
- << indent() << "}" << endl;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ // This fills in default values, as opposed to nulls
+ out <<
+ indent() << publicize((*m_iter)->get_name()) << " " <<
+ type_to_go_type((*m_iter)->get_type()) << endl;
+ }
}
- }
- generate_isset_helpers(out, tstruct, tstruct_name, is_result);
- generate_go_struct_reader(out, tstruct, tstruct_name, is_result);
- generate_go_struct_writer(out, tstruct, tstruct_name, is_result);
+ indent_down();
+ out <<
+ indent() << "}" << endl << endl <<
+ indent() << "func New" << tstruct_name << "() *" << tstruct_name << " {" << endl <<
+ indent() << " return &";
+ generate_go_struct_initializer(out, tstruct, is_result || is_args);
+ out <<
+ indent() << "}" << endl << endl;
+ // Default values for optional fields
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ string publicized_name;
+ t_const_value* def_value;
+ get_publicized_name_and_def_value(*m_iter, &publicized_name, &def_value);
+ t_type* fieldType = (*m_iter)->get_type();
+ string goType = type_to_go_type_with_opt(fieldType, false);
+ string def_var_name = tstruct_name + "_" + publicized_name + "_DEFAULT";
+ if ((*m_iter)->get_req() == t_field::T_OPTIONAL || is_pointer_field(*m_iter)) {
+ out << indent() << "var " << def_var_name <<" "<<goType ;
+ if (def_value != NULL) {
+ out << " = " << render_const_value(fieldType, def_value, (*m_iter)->get_name()) ;
+ }
+ out <<endl;
+ }
+ if (is_pointer_field(*m_iter)) {
+ string goOptType = type_to_go_type_with_opt(fieldType, true);
+ string maybepointer = goOptType!=goType?"*":"";
+ out <<indent() << "func (p *" << tstruct_name << ") Get" << publicized_name << "() "<< goType<<" {" << endl
+ <<indent() << " if !p.IsSet" <<publicized_name <<"() {" <<endl
+ <<indent() << " return " <<def_var_name <<endl
+ <<indent() << " }" <<endl
+ <<indent() << "return "<<maybepointer <<"p." <<publicized_name <<endl
+ <<indent() << "}" <<endl;
+ } else {
+ out <<endl
+ <<indent() << "func (p *" << tstruct_name << ") Get" << publicized_name << "() "<< goType<<" {" << endl
+ <<indent() << "return p." <<publicized_name <<endl
+ <<indent() << "}" <<endl;
- out << indent() << "func (p *" << tstruct_name << ") String() string {" << endl << indent()
- << " if p == nil {" << endl << indent() << " return \"<nil>\"" << endl << indent()
- << " }" << endl << indent() << " return fmt.Sprintf(\"" << escape_string(tstruct_name)
- << "(%+v)\", *p)" << endl << indent() << "}" << endl << endl;
+ }
+ }
- if (is_exception) {
- out << indent() << "func (p *" << tstruct_name << ") Error() string {" << endl << indent()
- << " return p.String()" << endl << indent() << "}" << endl << endl;
- }
+ generate_isset_helpers(out, tstruct, tstruct_name, is_result);
+ generate_go_struct_reader(out, tstruct, tstruct_name, is_result);
+ generate_go_struct_writer(out, tstruct, tstruct_name, is_result);
+
+ out <<
+ indent() << "func (p *" << tstruct_name << ") String() string {" << endl <<
+ indent() << " if p == nil {" << endl <<
+ indent() << " return \"<nil>\"" << endl <<
+ indent() << " }" << endl <<
+ indent() << " return fmt.Sprintf(\"" << escape_string(tstruct_name) << "(%+v)\", *p)" << endl <<
+ indent() << "}" << endl << endl;
+
+ if(is_exception) {
+ out <<
+ indent() << "func (p *" << tstruct_name << ") Error() string {" << endl <<
+ indent() << " return p.String()" << endl <<
+ indent() << "}" << endl << endl;
+ }
}
/**
* Generates the IsSet helper methods for a struct
*/
void t_go_generator::generate_isset_helpers(ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result) {
- (void)is_result;
- const vector<t_field*>& fields = tstruct->get_members();
- vector<t_field*>::const_iterator f_iter;
- const string escaped_tstruct_name(escape_string(tstruct->get_name()));
+ t_struct* tstruct,
+ const string& tstruct_name,
+ bool is_result)
+{
+ (void) is_result;
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ const string escaped_tstruct_name(escape_string(tstruct->get_name()));
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- const string field_name(
- publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL || is_pointer_field(*f_iter)) {
- out << indent() << "func (p *" << tstruct_name << ") IsSet" << field_name << "() bool {"
- << endl;
- indent_up();
- t_type* ttype = (*f_iter)->get_type()->get_true_type();
- bool is_byteslice = ttype->is_base_type() && ((t_base_type*)ttype)->is_binary();
- bool compare_to_nil_only = ttype->is_set() || ttype->is_list() || ttype->is_map()
- || (is_byteslice && !(*f_iter)->get_value());
- if (is_pointer_field(*f_iter) || compare_to_nil_only) {
- out << indent() << "return p." << field_name << " != nil" << endl;
- } else {
- string def_var_name = tstruct_name + "_" + field_name + "_DEFAULT";
- if (is_byteslice) {
- out << indent() << "return !bytes.Equal(p." << field_name << ", " << def_var_name << ")"
- << endl;
- } else {
- out << indent() << "return p." << field_name << " != " << def_var_name << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ const string field_name(publicize(variable_name_to_go_name(escape_string((*f_iter)->get_name()))));
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || is_pointer_field(*f_iter)) {
+ out <<
+ indent() << "func (p *" << tstruct_name << ") IsSet" << field_name << "() bool {" << endl;
+ indent_up();
+ t_type* ttype = (*f_iter)->get_type()->get_true_type();
+ bool is_byteslice = ttype->is_base_type() && ((t_base_type*)ttype)->is_binary() ;
+ bool compare_to_nil_only = ttype->is_set()
+ || ttype->is_list()
+ || ttype->is_map()
+ || (is_byteslice && !(*f_iter)->get_value());
+ if (is_pointer_field(*f_iter) || compare_to_nil_only) {
+ out <<
+ indent() << "return p." << field_name << " != nil" << endl;
+ } else {
+ string def_var_name = tstruct_name + "_" + field_name + "_DEFAULT";
+ if (is_byteslice) {
+ out << indent() << "return !bytes.Equal(p." << field_name << ", "<<def_var_name << ")" << endl;
+ } else {
+ out << indent() << "return p." << field_name << " != "<<def_var_name << endl;
+ }
+ }
+ indent_down();
+ out <<
+ indent() << "}" << endl << endl;
}
- }
- indent_down();
- out << indent() << "}" << endl << endl;
}
- }
}
/**
* Generates the read method for a struct
*/
void t_go_generator::generate_go_struct_reader(ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result) {
- (void)is_result;
- const vector<t_field*>& fields = tstruct->get_members();
- vector<t_field*>::const_iterator f_iter;
- string escaped_tstruct_name(escape_string(tstruct->get_name()));
- out << indent() << "func (p *" << tstruct_name << ") Read(iprot thrift.TProtocol) error {"
- << endl;
- indent_up();
- out << indent() << "if _, err := iprot.ReadStructBegin(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"%T read error: %s\", p, err)" << endl << indent() << "}" << endl;
- // Loop over reading in fields
- indent(out) << "for {" << endl;
- indent_up();
- // Read beginning field marker
- out << indent() << "_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()" << endl << indent()
- << "if err != nil {" << endl << indent()
- << " return fmt.Errorf(\"%T field %d read error: %s\", p, fieldId, err)" << endl << indent()
- << "}" << endl;
- // Check for field STOP marker and break
- out << indent() << "if fieldTypeId == thrift.STOP { break; }" << endl;
+ t_struct* tstruct,
+ const string& tstruct_name,
+ bool is_result)
+{
+ (void) is_result;
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ string escaped_tstruct_name(escape_string(tstruct->get_name()));
+ out <<
+ indent() << "func (p *" << tstruct_name << ") Read(iprot thrift.TProtocol) error {" << endl;
+ indent_up();
+ out <<
+ indent() << "if _, err := iprot.ReadStructBegin(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T read error: %s\", p, err)" << endl <<
+ indent() << "}" << endl;
+ // Loop over reading in fields
+ indent(out) << "for {" << endl;
+ indent_up();
+ // Read beginning field marker
+ out <<
+ indent() << "_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T field %d read error: %s\", p, fieldId, err)" << endl <<
+ indent() << "}" << endl;
+ // Check for field STOP marker and break
+ out <<
+ indent() << "if fieldTypeId == thrift.STOP { break; }" << endl;
- string thriftFieldTypeId;
- // Generate deserialization code for known cases
- int32_t field_id = -1;
+ string thriftFieldTypeId;
+ // Generate deserialization code for known cases
+ int32_t field_id = -1;
- // Switch statement on the field we are reading, false if no fields present
- bool have_switch = !fields.empty();
- if (have_switch) {
- indent(out) << "switch fieldId {" << endl;
- }
-
- // All the fields we know
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- field_id = (*f_iter)->get_key();
-
- // if negative id, ensure we generate a valid method name
- string field_method_prefix("ReadField");
-
- if (field_id < 0) {
- field_method_prefix += "_";
- field_id *= -1;
+ // Switch statement on the field we are reading, false if no fields present
+ bool have_switch = !fields.empty();
+ if( have_switch) {
+ indent(out) << "switch fieldId {" << endl;
}
- out << indent() << "case " << field_id << ":" << endl;
- indent_up();
- thriftFieldTypeId = type_to_enum((*f_iter)->get_type());
+ // All the fields we know
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ field_id = (*f_iter)->get_key();
- if (thriftFieldTypeId == "thrift.BINARY") {
- thriftFieldTypeId = "thrift.STRING";
+ // if negative id, ensure we generate a valid method name
+ string field_method_prefix("ReadField");
+
+ if (field_id < 0) {
+ field_method_prefix += "_";
+ field_id *= -1;
+ }
+
+ out <<
+ indent() <<"case " << field_id << ":" << endl;
+ indent_up();
+ thriftFieldTypeId = type_to_enum((*f_iter)->get_type());
+
+ if (thriftFieldTypeId == "thrift.BINARY") {
+ thriftFieldTypeId = "thrift.STRING";
+ }
+
+ out <<
+ indent() << "if err := p." << field_method_prefix << field_id << "(iprot); err != nil {" << endl <<
+ indent() << " return err" << endl <<
+ indent() << "}" << endl;
+ indent_down();
}
- out << indent() << "if err := p." << field_method_prefix << field_id << "(iprot); err != nil {"
- << endl << indent() << " return err" << endl << indent() << "}" << endl;
- indent_down();
- }
-
- // Begin switch default case
- if (have_switch) {
- out << indent() << "default:" << endl;
- indent_up();
- }
-
- // Skip unknown fields in either case
- out << indent() << "if err := iprot.Skip(fieldTypeId); err != nil {" << endl << indent()
- << " return err" << endl << indent() << "}" << endl;
-
- // End switch default case
- if (have_switch) {
- indent_down();
- out << indent() << "}" << endl;
- }
-
- // Read field end marker
- out << indent() << "if err := iprot.ReadFieldEnd(); err != nil {" << endl << indent()
- << " return err" << endl << indent() << "}" << endl;
- indent_down();
- out << indent() << "}" << endl << indent() << "if err := iprot.ReadStructEnd(); err != nil {"
- << endl << indent() << " return fmt.Errorf(\"%T read struct end error: %s\", p, err)" << endl
- << indent() << "}" << endl << indent() << "return nil" << endl;
- indent_down();
- out << indent() << "}" << endl << endl;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- string field_type_name(publicize((*f_iter)->get_type()->get_name()));
- string field_name(publicize((*f_iter)->get_name()));
- string field_method_prefix("ReadField");
- int32_t field_id = (*f_iter)->get_key();
-
- if (field_id < 0) {
- field_method_prefix += "_";
- field_id *= -1;
+ // Begin switch default case
+ if( have_switch) {
+ out <<
+ indent() << "default:" << endl;
+ indent_up();
}
- out << indent() << "func (p *" << tstruct_name << ") " << field_method_prefix << field_id
- << "(iprot thrift.TProtocol) error {" << endl;
- indent_up();
- generate_deserialize_field(out, *f_iter, false, "p.");
+ // Skip unknown fields in either case
+ out <<
+ indent() << "if err := iprot.Skip(fieldTypeId); err != nil {" << endl <<
+ indent() << " return err" << endl <<
+ indent() << "}" << endl;
+
+ // End switch default case
+ if( have_switch) {
+ indent_down();
+ out <<
+ indent() << "}" << endl;
+ }
+
+ // Read field end marker
+ out <<
+ indent() << "if err := iprot.ReadFieldEnd(); err != nil {" << endl <<
+ indent() << " return err" << endl <<
+ indent() << "}" << endl;
indent_down();
- out << indent() << " return nil" << endl << indent() << "}" << endl << endl;
- }
+ out <<
+ indent() << "}" << endl <<
+ indent() << "if err := iprot.ReadStructEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T read struct end error: %s\", p, err)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "return nil" << endl;
+ indent_down();
+ out <<
+ indent() << "}" << endl << endl;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ string field_type_name(publicize((*f_iter)->get_type()->get_name()));
+ string field_name(publicize((*f_iter)->get_name()));
+ string field_method_prefix("ReadField");
+ int32_t field_id = (*f_iter)->get_key();
+
+ if (field_id < 0) {
+ field_method_prefix += "_";
+ field_id *= -1;
+ }
+
+ out <<
+ indent() << "func (p *" << tstruct_name << ") " << field_method_prefix << field_id << "(iprot thrift.TProtocol) error {" << endl;
+ indent_up();
+ generate_deserialize_field(out, *f_iter, false, "p.");
+ indent_down();
+ out <<
+ indent() << " return nil" << endl <<
+ indent() << "}" << endl << endl;
+ }
}
void t_go_generator::generate_go_struct_writer(ofstream& out,
- t_struct* tstruct,
- const string& tstruct_name,
- bool is_result) {
- (void)is_result;
- string name(tstruct->get_name());
- const vector<t_field*>& fields = tstruct->get_sorted_members();
- vector<t_field*>::const_iterator f_iter;
- indent(out) << "func (p *" << tstruct_name << ") Write(oprot thrift.TProtocol) error {" << endl;
- indent_up();
- out << indent() << "if err := oprot.WriteStructBegin(\"" << name << "\"); err != nil {" << endl
- << indent() << " return fmt.Errorf(\"%T write struct begin error: %s\", p, err) }" << endl;
-
- string field_name;
- string escape_field_name;
- // t_const_value* field_default_value;
- t_field::e_req field_required;
- int32_t field_id = -1;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- string field_method_prefix("writeField");
- field_name = (*f_iter)->get_name();
- escape_field_name = escape_string(field_name);
- field_id = (*f_iter)->get_key();
-
- if (field_id < 0) {
- field_method_prefix += "_";
- field_id *= -1;
- }
-
- out << indent() << "if err := p." << field_method_prefix << field_id
- << "(oprot); err != nil { return err }" << endl;
- }
-
- // Write the struct map
- out << indent() << "if err := oprot.WriteFieldStop(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"write field stop error: %s\", err) }" << endl << indent()
- << "if err := oprot.WriteStructEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"write struct stop error: %s\", err) }" << endl << indent()
- << "return nil" << endl;
- indent_down();
- out << indent() << "}" << endl << endl;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- string field_method_prefix("writeField");
- field_id = (*f_iter)->get_key();
- field_name = (*f_iter)->get_name();
- escape_field_name = escape_string(field_name);
- // field_default_value = (*f_iter)->get_value();
- field_required = (*f_iter)->get_req();
-
- if (field_id < 0) {
- field_method_prefix += "_";
- field_id *= -1;
- }
-
- out << indent() << "func (p *" << tstruct_name << ") " << field_method_prefix << field_id
- << "(oprot thrift.TProtocol) (err error) {" << endl;
+ t_struct* tstruct,
+ const string& tstruct_name,
+ bool is_result)
+{
+ (void) is_result;
+ string name(tstruct->get_name());
+ const vector<t_field*>& fields = tstruct->get_sorted_members();
+ vector<t_field*>::const_iterator f_iter;
+ indent(out) <<
+ "func (p *" << tstruct_name << ") Write(oprot thrift.TProtocol) error {" << endl;
indent_up();
+ out <<
+ indent() << "if err := oprot.WriteStructBegin(\"" << name << "\"); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T write struct begin error: %s\", p, err) }" << endl;
- if (field_required == t_field::T_OPTIONAL) {
- out << indent() << "if p.IsSet" << publicize(variable_name_to_go_name(field_name)) << "() {"
- << endl;
- indent_up();
+ string field_name;
+ string escape_field_name;
+ //t_const_value* field_default_value;
+ t_field::e_req field_required;
+ int32_t field_id = -1;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ string field_method_prefix("writeField");
+ field_name = (*f_iter)->get_name();
+ escape_field_name = escape_string(field_name);
+ field_id = (*f_iter)->get_key();
+
+ if (field_id < 0) {
+ field_method_prefix += "_";
+ field_id *= -1;
+ }
+
+ out <<
+ indent() << "if err := p." << field_method_prefix << field_id << "(oprot); err != nil { return err }" << endl;
+
}
- out << indent() << "if err := oprot.WriteFieldBegin(\"" << escape_field_name << "\", "
- << type_to_enum((*f_iter)->get_type()) << ", " << field_id << "); err != nil {" << endl
- << indent() << " return fmt.Errorf(\"%T write field begin error " << field_id << ":"
- << escape_field_name << ": %s\", p, err); }" << endl;
-
- // Write field contents
- generate_serialize_field(out, *f_iter, "p.");
-
- // Write field closer
- out << indent() << "if err := oprot.WriteFieldEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"%T write field end error " << field_id << ":" << escape_field_name
- << ": %s\", p, err); }" << endl;
-
- if (field_required == t_field::T_OPTIONAL) {
- indent_down();
- out << indent() << "}" << endl;
- }
-
+ // Write the struct map
+ out <<
+ indent() << "if err := oprot.WriteFieldStop(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"write field stop error: %s\", err) }" << endl <<
+ indent() << "if err := oprot.WriteStructEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"write struct stop error: %s\", err) }" << endl <<
+ indent() << "return nil" << endl;
indent_down();
- out << indent() << " return err" << endl << indent() << "}" << endl << endl;
- }
+ out <<
+ indent() << "}" << endl << endl;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ string field_method_prefix("writeField");
+ field_id = (*f_iter)->get_key();
+ field_name = (*f_iter)->get_name();
+ escape_field_name = escape_string(field_name);
+ //field_default_value = (*f_iter)->get_value();
+ field_required = (*f_iter)->get_req();
+
+ if (field_id < 0) {
+ field_method_prefix += "_";
+ field_id *= -1;
+ }
+
+ out <<
+ indent() << "func (p *" << tstruct_name << ") " << field_method_prefix << field_id << "(oprot thrift.TProtocol) (err error) {" << endl;
+ indent_up();
+
+ if (field_required == t_field::T_OPTIONAL) {
+ out <<
+ indent() << "if p.IsSet" << publicize(variable_name_to_go_name(field_name)) << "() {" << endl;
+ indent_up();
+ }
+
+ out <<
+ indent() << "if err := oprot.WriteFieldBegin(\"" <<
+ escape_field_name << "\", " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ field_id << "); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T write field begin error " << field_id << ":" << escape_field_name << ": %s\", p, err); }" << endl;
+
+ // Write field contents
+ generate_serialize_field(out, *f_iter, "p.");
+
+
+ // Write field closer
+ out <<
+ indent() << "if err := oprot.WriteFieldEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T write field end error " << field_id << ":" << escape_field_name << ": %s\", p, err); }" << endl;
+
+ if (field_required == t_field::T_OPTIONAL) {
+ indent_down();
+ out <<
+ indent() << "}" << endl;
+ }
+
+ indent_down();
+ out <<
+ indent() << " return err" << endl <<
+ indent() << "}" << endl << endl;
+ }
}
/**
@@ -1421,32 +1521,35 @@
*
* @param tservice The service definition
*/
-void t_go_generator::generate_service(t_service* tservice) {
- string test_suffix("_test");
- string filename = lowercase(service_name_);
- string f_service_name;
+void t_go_generator::generate_service(t_service* tservice)
+{
+ string test_suffix("_test");
+ string filename = lowercase(service_name_);
+ string f_service_name;
- size_t fname_len = filename.length();
- size_t suffix_len = test_suffix.length();
+ size_t fname_len = filename.length();
+ size_t suffix_len = test_suffix.length();
- if ((fname_len >= suffix_len)
- && (filename.compare(fname_len - suffix_len, suffix_len, test_suffix) == 0)) {
- f_service_name = package_dir_ + "/" + filename + "_.go";
- } else {
- f_service_name = package_dir_ + "/" + filename + ".go";
- }
- f_service_.open(f_service_name.c_str());
- f_service_ << go_autogen_comment() << go_package() << render_includes();
+ if ((fname_len >= suffix_len) && (filename.compare(fname_len-suffix_len, suffix_len, test_suffix) == 0)) {
+ f_service_name = package_dir_ + "/" + filename + "_.go";
+ } else {
+ f_service_name = package_dir_ + "/" + filename + ".go";
+ }
+ f_service_.open(f_service_name.c_str());
+ f_service_ <<
+ go_autogen_comment() <<
+ go_package() <<
+ render_includes();
- generate_service_interface(tservice);
- generate_service_client(tservice);
- generate_service_server(tservice);
- generate_service_helpers(tservice);
- generate_service_remote(tservice);
- // Close service file
- f_service_ << endl;
- f_service_.close();
- format_go_output(f_service_name);
+ generate_service_interface(tservice);
+ generate_service_client(tservice);
+ generate_service_server(tservice);
+ generate_service_helpers(tservice);
+ generate_service_remote(tservice);
+ // Close service file
+ f_service_ << endl;
+ f_service_.close();
+ format_go_output(f_service_name);
}
/**
@@ -1454,16 +1557,18 @@
*
* @param tservice The service to generate a header definition for
*/
-void t_go_generator::generate_service_helpers(t_service* tservice) {
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
- f_service_ << "// HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
+void t_go_generator::generate_service_helpers(t_service* tservice)
+{
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ f_service_ <<
+ "// HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- t_struct* ts = (*f_iter)->get_arglist();
- generate_go_struct_definition(f_service_, ts, false, false, true);
- generate_go_function_helpers(*f_iter);
- }
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* ts = (*f_iter)->get_arglist();
+ generate_go_struct_definition(f_service_, ts, false, false, true);
+ generate_go_function_helpers(*f_iter);
+ }
}
/**
@@ -1471,28 +1576,29 @@
*
* @param tfunction The function
*/
-void t_go_generator::generate_go_function_helpers(t_function* tfunction) {
- if (!tfunction->is_oneway()) {
- t_struct result(program_, tfunction->get_name() + "_result");
- t_field success(tfunction->get_returntype(), "success", 0);
- success.set_req(t_field::T_OPTIONAL);
+void t_go_generator::generate_go_function_helpers(t_function* tfunction)
+{
+ if (!tfunction->is_oneway()) {
+ t_struct result(program_, tfunction->get_name() + "_result");
+ t_field success(tfunction->get_returntype(), "success", 0);
+ success.set_req(t_field::T_OPTIONAL);
- if (!tfunction->get_returntype()->is_void()) {
- result.append(&success);
+ if (!tfunction->get_returntype()->is_void()) {
+ result.append(&success);
+ }
+
+ t_struct* xs = tfunction->get_xceptions();
+ const vector<t_field*>& fields = xs->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ t_field* f = *f_iter;
+ f->set_req(t_field::T_OPTIONAL);
+ result.append(f);
+ }
+
+ generate_go_struct_definition(f_service_, &result, false, true);
}
-
- t_struct* xs = tfunction->get_xceptions();
- const vector<t_field*>& fields = xs->get_members();
- vector<t_field*>::const_iterator f_iter;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- t_field* f = *f_iter;
- f->set_req(t_field::T_OPTIONAL);
- result.append(f);
- }
-
- generate_go_struct_definition(f_service_, &result, false, true);
- }
}
/**
@@ -1500,41 +1606,44 @@
*
* @param tservice The service to generate a header definition for
*/
-void t_go_generator::generate_service_interface(t_service* tservice) {
- string extends = "";
- string extends_if = "";
- string serviceName(publicize(tservice->get_name()));
- string interfaceName = serviceName;
+void t_go_generator::generate_service_interface(t_service* tservice)
+{
+ string extends = "";
+ string extends_if = "";
+ string serviceName(publicize(tservice->get_name()));
+ string interfaceName = serviceName;
- if (tservice->get_extends() != NULL) {
- extends = type_name(tservice->get_extends());
- size_t index = extends.rfind(".");
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ size_t index = extends.rfind(".");
- if (index != string::npos) {
- extends_if = "\n" + indent() + " " + extends.substr(0, index + 1)
- + publicize(extends.substr(index + 1)) + "\n";
- } else {
- extends_if = "\n" + indent() + publicize(extends) + "\n";
+ if (index != string::npos) {
+ extends_if = "\n" + indent() + " " + extends.substr(0, index + 1) + publicize(extends.substr(index + 1)) + "\n";
+ } else {
+ extends_if = "\n" + indent() + publicize(extends) + "\n";
+ }
}
- }
- f_service_ << indent() << "type " << interfaceName << " interface {" << extends_if;
- indent_up();
- generate_go_docstring(f_service_, tservice);
- vector<t_function*> functions = tservice->get_functions();
+ f_service_ <<
+ indent() << "type " << interfaceName << " interface {" << extends_if;
+ indent_up();
+ generate_go_docstring(f_service_, tservice);
+ vector<t_function*> functions = tservice->get_functions();
- if (!functions.empty()) {
- f_service_ << endl;
- vector<t_function*>::iterator f_iter;
+ if (!functions.empty()) {
+ f_service_ << endl;
+ vector<t_function*>::iterator f_iter;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- generate_go_docstring(f_service_, (*f_iter));
- f_service_ << indent() << function_signature_if(*f_iter, "", true) << endl;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_go_docstring(f_service_, (*f_iter));
+ f_service_ <<
+ indent() << function_signature_if(*f_iter, "", true) << endl;
+ }
}
- }
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
}
/**
@@ -1542,248 +1651,295 @@
*
* @param tservice The service to generate a server for.
*/
-void t_go_generator::generate_service_client(t_service* tservice) {
- string extends = "";
- string extends_field = "";
- string extends_client = "";
- string extends_client_new = "";
- string serviceName(publicize(tservice->get_name()));
+void t_go_generator::generate_service_client(t_service* tservice)
+{
+ string extends = "";
+ string extends_field = "";
+ string extends_client = "";
+ string extends_client_new = "";
+ string serviceName(publicize(tservice->get_name()));
- if (tservice->get_extends() != NULL) {
- extends = type_name(tservice->get_extends());
- size_t index = extends.rfind(".");
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ size_t index = extends.rfind(".");
- if (index != string::npos) {
- extends_client = extends.substr(0, index + 1) + publicize(extends.substr(index + 1))
- + "Client";
- extends_client_new = extends.substr(0, index + 1) + "New"
- + publicize(extends.substr(index + 1)) + "Client";
- } else {
- extends_client = publicize(extends) + "Client";
- extends_client_new = "New" + extends_client;
- }
- }
-
- extends_field = extends_client.substr(extends_client.find(".") + 1);
-
- generate_go_docstring(f_service_, tservice);
- f_service_ << indent() << "type " << serviceName << "Client struct {" << endl;
- indent_up();
-
- if (!extends_client.empty()) {
- f_service_ << indent() << "*" << extends_client << endl;
- } else {
- f_service_ << indent() << "Transport thrift.TTransport" << endl << indent()
- << "ProtocolFactory thrift.TProtocolFactory" << endl << indent()
- << "InputProtocol thrift.TProtocol" << endl << indent()
- << "OutputProtocol thrift.TProtocol" << endl << indent() << "SeqId int32"
- << endl /*<<
- indent() << "reqs map[int32]Deferred" << endl*/;
- }
-
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
- // Constructor function
- f_service_ << indent() << "func New" << serviceName
- << "ClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *" << serviceName
- << "Client {" << endl;
- indent_up();
- f_service_ << indent() << "return &" << serviceName << "Client";
-
- if (!extends.empty()) {
- f_service_ << "{" << extends_field << ": " << extends_client_new << "Factory(t, f)}";
- } else {
- indent_up();
- f_service_ << "{Transport: t," << endl << indent() << "ProtocolFactory: f," << endl << indent()
- << "InputProtocol: f.GetProtocol(t)," << endl << indent()
- << "OutputProtocol: f.GetProtocol(t)," << endl << indent() << "SeqId: 0,"
- << endl /*<<
- indent() << "Reqs: make(map[int32]Deferred)" << endl*/;
- indent_down();
- f_service_ << indent() << "}" << endl;
- }
-
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
- // Constructor function
- f_service_
- << indent() << "func New" << serviceName
- << "ClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *"
- << serviceName << "Client {" << endl;
- indent_up();
- f_service_ << indent() << "return &" << serviceName << "Client";
-
- if (!extends.empty()) {
- f_service_ << "{" << extends_field << ": " << extends_client_new << "Protocol(t, iprot, oprot)}"
- << endl;
- } else {
- indent_up();
- f_service_ << "{Transport: t," << endl << indent() << "ProtocolFactory: nil," << endl
- << indent() << "InputProtocol: iprot," << endl << indent()
- << "OutputProtocol: oprot," << endl << indent() << "SeqId: 0," << endl /*<<
- indent() << "Reqs: make(map[int32]interface{})" << endl*/;
- indent_down();
- f_service_ << indent() << "}" << endl;
- }
-
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
- // Generate client method implementations
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::const_iterator f_iter;
-
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- t_struct* arg_struct = (*f_iter)->get_arglist();
- const vector<t_field*>& fields = arg_struct->get_members();
- vector<t_field*>::const_iterator fld_iter;
- string funname = publicize((*f_iter)->get_name());
- // Open function
- generate_go_docstring(f_service_, (*f_iter));
- f_service_ << indent() << "func (p *" << serviceName << "Client) "
- << function_signature_if(*f_iter, "", true) << " {" << endl;
- indent_up();
- /*
- f_service_ <<
- indent() << "p.SeqId += 1" << endl;
- if (!(*f_iter)->is_oneway()) {
- f_service_ <<
- indent() << "d := defer.Deferred()" << endl <<
- indent() << "p.Reqs[p.SeqId] = d" << endl;
- }
- */
- f_service_ << indent() << "if err = p.send" << funname << "(";
- bool first = true;
-
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
- }
-
- f_service_ << variable_name_to_go_name((*fld_iter)->get_name());
- }
-
- f_service_ << "); err != nil { return }" << endl;
-
- if (!(*f_iter)->is_oneway()) {
- f_service_ << indent() << "return p.recv" << funname << "()" << endl;
- } else {
- f_service_ << indent() << "return" << endl;
- }
-
- indent_down();
- f_service_ << indent() << "}" << endl << endl << indent() << "func (p *" << serviceName
- << "Client) send" << function_signature(*f_iter) << "(err error) {" << endl;
- indent_up();
- std::string argsname = publicize((*f_iter)->get_name() + "_args", true);
- // Serialize the request header
- f_service_ << indent() << "oprot := p.OutputProtocol" << endl << indent() << "if oprot == nil {"
- << endl << indent() << " oprot = p.ProtocolFactory.GetProtocol(p.Transport)" << endl
- << indent() << " p.OutputProtocol = oprot" << endl << indent() << "}" << endl
- << indent() << "p.SeqId++" << endl << indent()
- << "if err = oprot.WriteMessageBegin(\"" << (*f_iter)->get_name() << "\", "
- << ((*f_iter)->is_oneway() ? "thrift.ONEWAY" : "thrift.CALL")
- << ", p.SeqId); err != nil {" << endl;
- indent_up();
- f_service_ << indent() << "return" << endl;
- indent_down();
- f_service_ << indent() << "}" << endl << indent() << "args := " << argsname << "{" << endl;
-
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << publicize(variable_name_to_go_name((*fld_iter)->get_name()))
- << " : " << variable_name_to_go_name((*fld_iter)->get_name()) << "," << endl;
- }
- f_service_ << indent() << "}" << endl;
-
- // Write to the stream
- f_service_ << indent() << "if err = args.Write(oprot); err != nil {" << endl;
- indent_up();
- f_service_ << indent() << "return" << endl;
- indent_down();
- f_service_ << indent() << "}" << endl << indent()
- << "if err = oprot.WriteMessageEnd(); err != nil {" << endl;
- indent_up();
- f_service_ << indent() << "return" << endl;
- indent_down();
- f_service_ << indent() << "}" << endl << indent() << "return oprot.Flush()" << endl;
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
-
- if (!(*f_iter)->is_oneway()) {
- std::string resultname = publicize((*f_iter)->get_name() + "_result", true);
- // Open function
- f_service_ << endl << indent() << "func (p *" << serviceName << "Client) recv"
- << publicize((*f_iter)->get_name()) << "() (";
-
- if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << "value " << type_to_go_type((*f_iter)->get_returntype()) << ", ";
- }
-
- f_service_ << "err error) {" << endl;
- indent_up();
- // TODO(mcslee): Validate message reply here, seq ids etc.
- string error(tmp("error"));
- string error2(tmp("error"));
- f_service_ << indent() << "iprot := p.InputProtocol" << endl << indent()
- << "if iprot == nil {" << endl << indent()
- << " iprot = p.ProtocolFactory.GetProtocol(p.Transport)" << endl << indent()
- << " p.InputProtocol = iprot" << endl << indent() << "}" << endl << indent()
- << "_, mTypeId, seqId, err := iprot.ReadMessageBegin()" << endl << indent()
- << "if err != nil {" << endl << indent() << " return" << endl << indent() << "}"
- << endl << indent() << "if mTypeId == thrift.EXCEPTION {" << endl << indent()
- << " " << error
- << " := thrift.NewTApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION, "
- "\"Unknown Exception\")" << endl << indent() << " var " << error2 << " error"
- << endl << indent() << " " << error2 << ", err = " << error << ".Read(iprot)"
- << endl << indent() << " if err != nil {" << endl << indent() << " return"
- << endl << indent() << " }" << endl << indent()
- << " if err = iprot.ReadMessageEnd(); err != nil {" << endl << indent()
- << " return" << endl << indent() << " }" << endl << indent()
- << " err = " << error2 << endl << indent() << " return" << endl << indent()
- << "}" << endl << indent() << "if p.SeqId != seqId {" << endl << indent()
- << " err = thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID, \""
- << (*f_iter)->get_name() << " failed: out of sequence response\")" << endl
- << indent() << " return" << endl << indent() << "}" << endl << indent()
- << "result := " << resultname << "{}" << endl << indent()
- << "if err = result.Read(iprot); err != nil {" << endl << indent() << " return"
- << endl << indent() << "}" << endl << indent()
- << "if err = iprot.ReadMessageEnd(); err != nil {" << endl << indent()
- << " return" << endl << indent() << "}" << endl;
-
- t_struct* xs = (*f_iter)->get_xceptions();
- const std::vector<t_field*>& xceptions = xs->get_members();
- vector<t_field*>::const_iterator x_iter;
-
- for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- const std::string varname = variable_name_to_go_name((*x_iter)->get_name());
- const std::string pubname = publicize(varname);
-
- f_service_ << indent() << "if result." << pubname << " != nil {" << endl << indent()
- << "err = result." << pubname << endl << indent() << "return " << endl
- << indent() << "}";
-
- if ((x_iter + 1) != xceptions.end()) {
- f_service_ << " else ";
+ if (index != string::npos) {
+ extends_client = extends.substr(0, index + 1) + publicize(extends.substr(index + 1)) + "Client";
+ extends_client_new = extends.substr(0, index + 1) + "New" + publicize(extends.substr(index + 1)) + "Client";
} else {
- f_service_ << endl;
+ extends_client = publicize(extends) + "Client";
+ extends_client_new = "New" + extends_client;
}
- }
-
- // Careful, only return _result if not a void function
- if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "value = result.GetSuccess()" << endl;
- }
-
- f_service_ << indent() << "return" << endl;
- // Close function
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
}
- }
- // indent_down();
- f_service_ << endl;
+ extends_field = extends_client.substr(extends_client.find(".") + 1);
+
+ generate_go_docstring(f_service_, tservice);
+ f_service_ <<
+ indent() << "type " << serviceName << "Client struct {" << endl;
+ indent_up();
+
+ if (!extends_client.empty()) {
+ f_service_ <<
+ indent() << "*" << extends_client << endl;
+ } else {
+ f_service_ <<
+ indent() << "Transport thrift.TTransport" << endl <<
+ indent() << "ProtocolFactory thrift.TProtocolFactory" << endl <<
+ indent() << "InputProtocol thrift.TProtocol" << endl <<
+ indent() << "OutputProtocol thrift.TProtocol" << endl <<
+ indent() << "SeqId int32" << endl /*<<
+ indent() << "reqs map[int32]Deferred" << endl*/;
+ }
+
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
+ // Constructor function
+ f_service_ <<
+ indent() << "func New" << serviceName << "ClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *" << serviceName << "Client {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "return &" << serviceName << "Client";
+
+ if (!extends.empty()) {
+ f_service_ <<
+ "{" << extends_field << ": " << extends_client_new << "Factory(t, f)}";
+ } else {
+ indent_up();
+ f_service_ << "{Transport: t," << endl <<
+ indent() << "ProtocolFactory: f," << endl <<
+ indent() << "InputProtocol: f.GetProtocol(t)," << endl <<
+ indent() << "OutputProtocol: f.GetProtocol(t)," << endl <<
+ indent() << "SeqId: 0," << endl /*<<
+ indent() << "Reqs: make(map[int32]Deferred)" << endl*/;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl;
+ }
+
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
+ // Constructor function
+ f_service_ <<
+ indent() << "func New" << serviceName << "ClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *" << serviceName << "Client {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "return &" << serviceName << "Client";
+
+ if (!extends.empty()) {
+ f_service_ <<
+ "{" << extends_field << ": " << extends_client_new << "Protocol(t, iprot, oprot)}" << endl;
+ } else {
+ indent_up();
+ f_service_ << "{Transport: t," << endl <<
+ indent() << "ProtocolFactory: nil," << endl <<
+ indent() << "InputProtocol: iprot," << endl <<
+ indent() << "OutputProtocol: oprot," << endl <<
+ indent() << "SeqId: 0," << endl /*<<
+ indent() << "Reqs: make(map[int32]interface{})" << endl*/;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl;
+ }
+
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
+ // Generate client method implementations
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::const_iterator f_iter;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator fld_iter;
+ string funname = publicize((*f_iter)->get_name());
+ // Open function
+ generate_go_docstring(f_service_, (*f_iter));
+ f_service_ <<
+ indent() << "func (p *" << serviceName << "Client) " << function_signature_if(*f_iter, "", true) << " {" << endl;
+ indent_up();
+ /*
+ f_service_ <<
+ indent() << "p.SeqId += 1" << endl;
+ if (!(*f_iter)->is_oneway()) {
+ f_service_ <<
+ indent() << "d := defer.Deferred()" << endl <<
+ indent() << "p.Reqs[p.SeqId] = d" << endl;
+ }
+ */
+ f_service_ <<
+ indent() << "if err = p.send" << funname << "(";
+ bool first = true;
+
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+
+ f_service_ << variable_name_to_go_name((*fld_iter)->get_name());
+ }
+
+ f_service_ << "); err != nil { return }" << endl;
+
+ if (!(*f_iter)->is_oneway()) {
+ f_service_ <<
+ indent() << "return p.recv" << funname << "()" << endl;
+ } else {
+ f_service_ <<
+ indent() << "return" << endl;
+ }
+
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << serviceName << "Client) send" << function_signature(*f_iter) << "(err error) {" << endl;
+ indent_up();
+ std::string argsname = publicize((*f_iter)->get_name() + "_args",true);
+ // Serialize the request header
+ f_service_ <<
+ indent() << "oprot := p.OutputProtocol" << endl <<
+ indent() << "if oprot == nil {" << endl <<
+ indent() << " oprot = p.ProtocolFactory.GetProtocol(p.Transport)" << endl <<
+ indent() << " p.OutputProtocol = oprot" << endl <<
+ indent() << "}" << endl <<
+ indent() << "p.SeqId++" << endl <<
+ indent() << "if err = oprot.WriteMessageBegin(\""
+ << (*f_iter)->get_name() << "\", "
+ << ((*f_iter)->is_oneway() ? "thrift.ONEWAY" : "thrift.CALL")
+ << ", p.SeqId); err != nil {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "return" << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "args := " << argsname << "{" << endl;
+
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ <<
+ indent() << publicize(variable_name_to_go_name((*fld_iter)->get_name())) << " : " << variable_name_to_go_name((*fld_iter)->get_name()) << ","<< endl;
+ }
+ f_service_ <<
+ indent() << "}" << endl;
+
+ // Write to the stream
+ f_service_ <<
+ indent() << "if err = args.Write(oprot); err != nil {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "return" << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "if err = oprot.WriteMessageEnd(); err != nil {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "return" << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "return oprot.Flush()" << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
+
+ if (!(*f_iter)->is_oneway()) {
+ std::string resultname = publicize((*f_iter)->get_name() + "_result",true);
+ // Open function
+ f_service_ << endl <<
+ indent() << "func (p *" << serviceName << "Client) recv" << publicize((*f_iter)->get_name()) <<
+ "() (";
+
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ <<
+ "value " << type_to_go_type((*f_iter)->get_returntype()) << ", ";
+ }
+
+ f_service_ <<
+ "err error) {" << endl;
+ indent_up();
+ // TODO(mcslee): Validate message reply here, seq ids etc.
+ string error(tmp("error"));
+ string error2(tmp("error"));
+ f_service_ <<
+ indent() << "iprot := p.InputProtocol" << endl <<
+ indent() << "if iprot == nil {" << endl <<
+ indent() << " iprot = p.ProtocolFactory.GetProtocol(p.Transport)" << endl <<
+ indent() << " p.InputProtocol = iprot" << endl <<
+ indent() << "}" << endl <<
+ indent() << "_, mTypeId, seqId, err := iprot.ReadMessageBegin()" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if mTypeId == thrift.EXCEPTION {" << endl <<
+ indent() << " " << error << " := thrift.NewTApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION, \"Unknown Exception\")" << endl <<
+ indent() << " var " << error2 << " error" << endl <<
+ indent() << " " << error2 << ", err = " << error << ".Read(iprot)" << endl <<
+ indent() << " if err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << " }" << endl <<
+ indent() << " if err = iprot.ReadMessageEnd(); err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << " }" << endl <<
+ indent() << " err = " << error2 << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if p.SeqId != seqId {" << endl <<
+ indent() << " err = thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID, \"" << (*f_iter)->get_name() << " failed: out of sequence response\")" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "result := " << resultname << "{}" << endl <<
+ indent() << "if err = result.Read(iprot); err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err = iprot.ReadMessageEnd(); err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl;
+
+ t_struct* xs = (*f_iter)->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ const std::string varname = variable_name_to_go_name((*x_iter)->get_name());
+ const std::string pubname = publicize(varname);
+
+ f_service_ <<
+ indent() << "if result." << pubname << " != nil {" << endl <<
+ indent() << "err = result." << pubname << endl <<
+ indent() << "return " << endl <<
+ indent() << "}";
+
+ if ((x_iter + 1) != xceptions.end()) {
+ f_service_ << " else ";
+ } else {
+ f_service_ << endl;
+ }
+ }
+
+ // Careful, only return _result if not a void function
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ <<
+ indent() << "value = result.GetSuccess()" << endl;
+ }
+
+ f_service_ <<
+ indent() << "return" << endl;
+ // Close function
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
+ }
+ }
+
+ //indent_down();
+ f_service_ <<
+ endl;
}
/**
@@ -1791,339 +1947,414 @@
*
* @param tservice The service to generate a remote for.
*/
-void t_go_generator::generate_service_remote(t_service* tservice) {
- vector<t_function*> functions = tservice->get_functions();
- t_service* parent = tservice->get_extends();
+void t_go_generator::generate_service_remote(t_service* tservice)
+{
+ vector<t_function*> functions = tservice->get_functions();
+ t_service* parent = tservice->get_extends();
- // collect inherited functions
- while (parent != NULL) {
- vector<t_function*> p_functions = parent->get_functions();
- functions.insert(functions.end(), p_functions.begin(), p_functions.end());
- parent = parent->get_extends();
- }
-
- vector<t_function*>::iterator f_iter;
- string f_remote_name = package_dir_ + "/" + underscore(service_name_) + "-remote/"
- + underscore(service_name_) + "-remote.go";
- ofstream f_remote;
- f_remote.open(f_remote_name.c_str());
- string service_module = get_real_go_module(program_);
- string::size_type loc;
-
- while ((loc = service_module.find(".")) != string::npos) {
- service_module.replace(loc, 1, 1, '/');
- }
-
- f_remote << go_autogen_comment() << indent() << "package main" << endl << endl << indent()
- << "import (" << endl << indent() << " \"flag\"" << endl << indent()
- << " \"fmt\"" << endl << indent() << " \"math\"" << endl << indent()
- << " \"net\"" << endl << indent() << " \"net/url\"" << endl << indent()
- << " \"os\"" << endl << indent() << " \"strconv\"" << endl << indent()
- << " \"strings\"" << endl << indent() << " \"" + gen_thrift_import_ + "\""
- << endl << indent() << " \"" << service_module << "\"" << endl << indent() << ")"
- << endl << indent() << endl << indent() << "func Usage() {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Usage of \", os.Args[0], \" [-h host:port] [-u url] "
- "[-f[ramed]] function [arg1 [arg2...]]:\")" << endl << indent()
- << " flag.PrintDefaults()" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"\\nFunctions:\")" << endl;
-
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_remote << " fmt.Fprintln(os.Stderr, \" " << (*f_iter)->get_returntype()->get_name() << " "
- << (*f_iter)->get_name() << "(";
- t_struct* arg_struct = (*f_iter)->get_arglist();
- const std::vector<t_field*>& args = arg_struct->get_members();
- vector<t_field*>::const_iterator a_iter;
- int num_args = args.size();
- bool first = true;
-
- for (int i = 0; i < num_args; ++i) {
- if (first) {
- first = false;
- } else {
- f_remote << ", ";
- }
-
- f_remote << args[i]->get_type()->get_name() << " " << args[i]->get_name();
+ // collect inherited functions
+ while (parent != NULL) {
+ vector<t_function*> p_functions = parent->get_functions();
+ functions.insert(functions.end(), p_functions.begin(), p_functions.end());
+ parent = parent->get_extends();
}
- f_remote << ")\")" << endl;
- }
+ vector<t_function*>::iterator f_iter;
+ string f_remote_name = package_dir_ + "/" + underscore(service_name_) + "-remote/" + underscore(service_name_) + "-remote.go";
+ ofstream f_remote;
+ f_remote.open(f_remote_name.c_str());
+ string service_module = get_real_go_module(program_);
+ string::size_type loc;
- f_remote << indent() << " fmt.Fprintln(os.Stderr)" << endl << indent() << " os.Exit(0)" << endl
- << indent() << "}" << endl << indent() << endl << indent() << "func main() {" << endl;
- indent_up();
- f_remote
- << indent() << "flag.Usage = Usage" << endl << indent() << "var host string" << endl
- << indent() << "var port int" << endl << indent() << "var protocol string" << endl << indent()
- << "var urlString string" << endl << indent() << "var framed bool" << endl << indent()
- << "var useHttp bool" << endl << indent() << "var parsedUrl url.URL" << endl << indent()
- << "var trans thrift.TTransport" << endl << indent() << "_ = strconv.Atoi" << endl << indent()
- << "_ = math.Abs" << endl << indent() << "flag.Usage = Usage" << endl << indent()
- << "flag.StringVar(&host, \"h\", \"localhost\", \"Specify host and port\")" << endl
- << indent() << "flag.IntVar(&port, \"p\", 9090, \"Specify port\")" << endl << indent()
- << "flag.StringVar(&protocol, \"P\", \"binary\", \"Specify the protocol (binary, compact, "
- "simplejson, json)\")" << endl << indent()
- << "flag.StringVar(&urlString, \"u\", \"\", \"Specify the url\")" << endl << indent()
- << "flag.BoolVar(&framed, \"framed\", false, \"Use framed transport\")" << endl << indent()
- << "flag.BoolVar(&useHttp, \"http\", false, \"Use http\")" << endl << indent()
- << "flag.Parse()" << endl << indent() << endl << indent() << "if len(urlString) > 0 {" << endl
- << indent() << " parsedUrl, err := url.Parse(urlString)" << endl << indent()
- << " if err != nil {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Error parsing URL: \", err)" << endl << indent()
- << " flag.Usage()" << endl << indent() << " }" << endl << indent()
- << " host = parsedUrl.Host" << endl << indent()
- << " useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == \"http\"" << endl
- << indent() << "} else if useHttp {" << endl << indent()
- << " _, err := url.Parse(fmt.Sprint(\"http://\", host, \":\", port))" << endl << indent()
- << " if err != nil {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Error parsing URL: \", err)" << endl << indent()
- << " flag.Usage()" << endl << indent() << " }" << endl << indent() << "}" << endl
- << indent() << endl << indent() << "cmd := flag.Arg(0)" << endl << indent() << "var err error"
- << endl << indent() << "if useHttp {" << endl << indent()
- << " trans, err = thrift.NewTHttpClient(parsedUrl.String())" << endl << indent()
- << "} else {" << endl << indent() << " portStr := fmt.Sprint(port)" << endl << indent()
- << " if strings.Contains(host, \":\") {" << endl << indent()
- << " host, portStr, err = net.SplitHostPort(host)" << endl << indent()
- << " if err != nil {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"error with host:\", err)" << endl << indent()
- << " os.Exit(1)" << endl << indent() << " }" << endl << indent()
- << " }" << endl << indent()
- << " trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))" << endl << indent()
- << " if err != nil {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"error resolving address:\", err)" << endl << indent()
- << " os.Exit(1)" << endl << indent() << " }" << endl << indent() << " if framed {"
- << endl << indent() << " trans = thrift.NewTFramedTransport(trans)" << endl << indent()
- << " }" << endl << indent() << "}" << endl << indent() << "if err != nil {" << endl
- << indent() << " fmt.Fprintln(os.Stderr, \"Error creating transport\", err)" << endl
- << indent() << " os.Exit(1)" << endl << indent() << "}" << endl << indent()
- << "defer trans.Close()" << endl << indent() << "var protocolFactory thrift.TProtocolFactory"
- << endl << indent() << "switch protocol {" << endl << indent() << "case \"compact\":" << endl
- << indent() << " protocolFactory = thrift.NewTCompactProtocolFactory()" << endl << indent()
- << " break" << endl << indent() << "case \"simplejson\":" << endl << indent()
- << " protocolFactory = thrift.NewTSimpleJSONProtocolFactory()" << endl << indent()
- << " break" << endl << indent() << "case \"json\":" << endl << indent()
- << " protocolFactory = thrift.NewTJSONProtocolFactory()" << endl << indent() << " break"
- << endl << indent() << "case \"binary\", \"\":" << endl << indent()
- << " protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()" << endl << indent()
- << " break" << endl << indent() << "default:" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Invalid protocol specified: \", protocol)" << endl << indent()
- << " Usage()" << endl << indent() << " os.Exit(1)" << endl << indent() << "}" << endl
- << indent() << "client := " << package_name_ << ".New" << publicize(service_name_)
- << "ClientFactory(trans, protocolFactory)" << endl << indent()
- << "if err := trans.Open(); err != nil {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Error opening socket to \", host, \":\", port, \" \", err)"
- << endl << indent() << " os.Exit(1)" << endl << indent() << "}" << endl << indent() << endl
- << indent() << "switch cmd {" << endl;
+ while ((loc = service_module.find(".")) != string::npos) {
+ service_module.replace(loc, 1, 1, '/');
+ }
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- t_struct* arg_struct = (*f_iter)->get_arglist();
- const std::vector<t_field*>& args = arg_struct->get_members();
- vector<t_field*>::const_iterator a_iter;
- int num_args = args.size();
- string funcName((*f_iter)->get_name());
- string pubName(publicize(funcName));
- string argumentsName(publicize(funcName + "_args", true));
- f_remote << indent() << "case \"" << escape_string(funcName) << "\":" << endl;
+ f_remote <<
+ go_autogen_comment() <<
+ indent() << "package main" << endl << endl <<
+ indent() << "import (" << endl <<
+ indent() << " \"flag\"" << endl <<
+ indent() << " \"fmt\"" << endl <<
+ indent() << " \"math\"" << endl <<
+ indent() << " \"net\"" << endl <<
+ indent() << " \"net/url\"" << endl <<
+ indent() << " \"os\"" << endl <<
+ indent() << " \"strconv\"" << endl <<
+ indent() << " \"strings\"" << endl <<
+ indent() << " \"" + gen_thrift_import_ + "\"" << endl <<
+ indent() << " \"" << service_module << "\"" << endl <<
+ indent() << ")" << endl <<
+ indent() << endl <<
+ indent() << "func Usage() {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Usage of \", os.Args[0], \" [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]:\")" << endl <<
+ indent() << " flag.PrintDefaults()" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"\\nFunctions:\")" << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_remote <<
+ " fmt.Fprintln(os.Stderr, \" " << (*f_iter)->get_returntype()->get_name() << " " << (*f_iter)->get_name() << "(";
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const std::vector<t_field*>& args = arg_struct->get_members();
+ vector<t_field*>::const_iterator a_iter;
+ int num_args = args.size();
+ bool first = true;
+
+ for (int i = 0; i < num_args; ++i) {
+ if (first) {
+ first = false;
+ } else {
+ f_remote << ", ";
+ }
+
+ f_remote <<
+ args[i]->get_type()->get_name() << " " << args[i]->get_name();
+ }
+
+ f_remote << ")\")" << endl;
+ }
+
+ f_remote <<
+ indent() << " fmt.Fprintln(os.Stderr)" << endl <<
+ indent() << " os.Exit(0)" << endl <<
+ indent() << "}" << endl <<
+ indent() << endl <<
+ indent() << "func main() {" << endl;
indent_up();
- f_remote << indent() << "if flag.NArg() - 1 != " << num_args << " {" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"" << escape_string(pubName) << " requires " << num_args
- << " args\")" << endl << indent() << " flag.Usage()" << endl << indent() << "}"
- << endl;
+ f_remote <<
+ indent() << "flag.Usage = Usage" << endl <<
+ indent() << "var host string" << endl <<
+ indent() << "var port int" << endl <<
+ indent() << "var protocol string" << endl <<
+ indent() << "var urlString string" << endl <<
+ indent() << "var framed bool" << endl <<
+ indent() << "var useHttp bool" << endl <<
+ indent() << "var parsedUrl url.URL" << endl <<
+ indent() << "var trans thrift.TTransport" << endl <<
+ indent() << "_ = strconv.Atoi" << endl <<
+ indent() << "_ = math.Abs" << endl <<
+ indent() << "flag.Usage = Usage" << endl <<
+ indent() << "flag.StringVar(&host, \"h\", \"localhost\", \"Specify host and port\")" << endl <<
+ indent() << "flag.IntVar(&port, \"p\", 9090, \"Specify port\")" << endl <<
+ indent() << "flag.StringVar(&protocol, \"P\", \"binary\", \"Specify the protocol (binary, compact, simplejson, json)\")" << endl <<
+ indent() << "flag.StringVar(&urlString, \"u\", \"\", \"Specify the url\")" << endl <<
+ indent() << "flag.BoolVar(&framed, \"framed\", false, \"Use framed transport\")" << endl <<
+ indent() << "flag.BoolVar(&useHttp, \"http\", false, \"Use http\")" << endl <<
+ indent() << "flag.Parse()" << endl <<
+ indent() << endl <<
+ indent() << "if len(urlString) > 0 {" << endl <<
+ indent() << " parsedUrl, err := url.Parse(urlString)" << endl <<
+ indent() << " if err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Error parsing URL: \", err)" << endl <<
+ indent() << " flag.Usage()" << endl <<
+ indent() << " }" << endl <<
+ indent() << " host = parsedUrl.Host" << endl <<
+ indent() << " useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == \"http\"" << endl <<
+ indent() << "} else if useHttp {" << endl <<
+ indent() << " _, err := url.Parse(fmt.Sprint(\"http://\", host, \":\", port))" << endl <<
+ indent() << " if err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Error parsing URL: \", err)" << endl <<
+ indent() << " flag.Usage()" << endl <<
+ indent() << " }" << endl <<
+ indent() << "}" << endl <<
+ indent() << endl <<
+ indent() << "cmd := flag.Arg(0)" << endl <<
+ indent() << "var err error" << endl <<
+ indent() << "if useHttp {" << endl <<
+ indent() << " trans, err = thrift.NewTHttpClient(parsedUrl.String())" << endl <<
+ indent() << "} else {" << endl <<
+ indent() << " portStr := fmt.Sprint(port)" << endl <<
+ indent() << " if strings.Contains(host, \":\") {" << endl <<
+ indent() << " host, portStr, err = net.SplitHostPort(host)" << endl <<
+ indent() << " if err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"error with host:\", err)" << endl <<
+ indent() << " os.Exit(1)" << endl <<
+ indent() << " }" << endl <<
+ indent() << " }" << endl <<
+ indent() << " trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))" << endl <<
+ indent() << " if err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"error resolving address:\", err)" << endl <<
+ indent() << " os.Exit(1)" << endl <<
+ indent() << " }" << endl <<
+ indent() << " if framed {" << endl <<
+ indent() << " trans = thrift.NewTFramedTransport(trans)" << endl <<
+ indent() << " }" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Error creating transport\", err)" << endl <<
+ indent() << " os.Exit(1)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "defer trans.Close()" << endl <<
+ indent() << "var protocolFactory thrift.TProtocolFactory" << endl <<
+ indent() << "switch protocol {" << endl <<
+ indent() << "case \"compact\":" << endl <<
+ indent() << " protocolFactory = thrift.NewTCompactProtocolFactory()" << endl <<
+ indent() << " break" << endl <<
+ indent() << "case \"simplejson\":" << endl <<
+ indent() << " protocolFactory = thrift.NewTSimpleJSONProtocolFactory()" << endl <<
+ indent() << " break" << endl <<
+ indent() << "case \"json\":" << endl <<
+ indent() << " protocolFactory = thrift.NewTJSONProtocolFactory()" << endl <<
+ indent() << " break" << endl <<
+ indent() << "case \"binary\", \"\":" << endl <<
+ indent() << " protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()" << endl <<
+ indent() << " break" << endl <<
+ indent() << "default:" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Invalid protocol specified: \", protocol)" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " os.Exit(1)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "client := " << package_name_ << ".New" << publicize(service_name_) << "ClientFactory(trans, protocolFactory)" << endl <<
+ indent() << "if err := trans.Open(); err != nil {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Error opening socket to \", host, \":\", port, \" \", err)" << endl <<
+ indent() << " os.Exit(1)" << endl <<
+ indent() << "}" << endl <<
+ indent() << endl <<
+ indent() << "switch cmd {" << endl;
- for (int i = 0; i < num_args; ++i) {
- int flagArg = i + 1;
- t_type* the_type(args[i]->get_type());
- t_type* the_type2(get_true_type(the_type));
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+ const std::vector<t_field*>& args = arg_struct->get_members();
+ vector<t_field*>::const_iterator a_iter;
+ int num_args = args.size();
+ string funcName((*f_iter)->get_name());
+ string pubName(publicize(funcName));
+ string argumentsName(publicize(funcName+"_args",true));
+ f_remote <<
+ indent() << "case \"" << escape_string(funcName) << "\":" << endl;
+ indent_up();
+ f_remote <<
+ indent() << "if flag.NArg() - 1 != " << num_args << " {" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"" << escape_string(pubName) << " requires " << num_args << " args\")" << endl <<
+ indent() << " flag.Usage()" << endl <<
+ indent() << "}" << endl;
- if (the_type2->is_enum()) {
- f_remote << indent() << "tmp" << i << ", err := (strconv.Atoi(flag.Arg(" << flagArg << ")))"
- << endl << indent() << "if err != nil {" << endl << indent() << " Usage()" << endl
- << indent() << " return" << endl << indent() << "}" << endl << indent()
- << "argvalue" << i << " := " << package_name_ << "."
- << publicize(the_type->get_name()) << "(tmp" << i << ")" << endl;
- } else if (the_type2->is_base_type()) {
- t_base_type::t_base e = ((t_base_type*)the_type2)->get_base();
- string err(tmp("err"));
+ for (int i = 0; i < num_args; ++i) {
+ int flagArg = i + 1;
+ t_type* the_type(args[i]->get_type());
+ t_type* the_type2(get_true_type(the_type));
- switch (e) {
- case t_base_type::TYPE_VOID:
- break;
+ if (the_type2->is_enum()) {
+ f_remote <<
+ indent() << "tmp" << i << ", err := (strconv.Atoi(flag.Arg(" << flagArg << ")))" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "argvalue" << i << " := " << package_name_ << "." << publicize(the_type->get_name()) << "(tmp" << i << ")" << endl;
+ } else if (the_type2->is_base_type()) {
+ t_base_type::t_base e = ((t_base_type*)the_type2)->get_base();
+ string err(tmp("err"));
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)the_type2)->is_binary()) {
- f_remote << indent() << "argvalue" << i << " := []byte(flag.Arg(" << flagArg << "))"
- << endl;
- } else {
- f_remote << indent() << "argvalue" << i << " := flag.Arg(" << flagArg << ")" << endl;
- }
- break;
+ switch (e) {
+ case t_base_type::TYPE_VOID:
+ break;
- case t_base_type::TYPE_BOOL:
- f_remote << indent() << "argvalue" << i << " := flag.Arg(" << flagArg << ") == \"true\""
- << endl;
- break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)the_type2)->is_binary()) {
+ f_remote <<
+ indent() << "argvalue" << i << " := []byte(flag.Arg(" << flagArg << "))" << endl;
+ } else {
+ f_remote <<
+ indent() << "argvalue" << i << " := flag.Arg(" << flagArg << ")" << endl;
+ }
+ break;
- case t_base_type::TYPE_BYTE:
- f_remote << indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg("
- << flagArg << ")))" << endl << indent() << "if " << err << " != nil {" << endl
- << indent() << " Usage()" << endl << indent() << " return" << endl << indent()
- << "}" << endl << indent() << "argvalue" << i << " := byte(tmp" << i << ")"
- << endl;
- break;
+ case t_base_type::TYPE_BOOL:
+ f_remote <<
+ indent() << "argvalue" << i << " := flag.Arg(" << flagArg << ") == \"true\"" << endl;
+ break;
- case t_base_type::TYPE_I16:
- f_remote << indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg("
- << flagArg << ")))" << endl << indent() << "if " << err << " != nil {" << endl
- << indent() << " Usage()" << endl << indent() << " return" << endl << indent()
- << "}" << endl << indent() << "argvalue" << i << " := byte(tmp" << i << ")"
- << endl;
- break;
+ case t_base_type::TYPE_BYTE:
+ f_remote <<
+ indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg(" << flagArg << ")))" << endl <<
+ indent() << "if " << err << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "argvalue" << i << " := byte(tmp" << i << ")" << endl;
+ break;
- case t_base_type::TYPE_I32:
- f_remote << indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg("
- << flagArg << ")))" << endl << indent() << "if " << err << " != nil {" << endl
- << indent() << " Usage()" << endl << indent() << " return" << endl << indent()
- << "}" << endl << indent() << "argvalue" << i << " := int32(tmp" << i << ")"
- << endl;
- break;
+ case t_base_type::TYPE_I16:
+ f_remote <<
+ indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg(" << flagArg << ")))" << endl <<
+ indent() << "if " << err << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "argvalue" << i << " := byte(tmp" << i << ")" << endl;
+ break;
- case t_base_type::TYPE_I64:
- f_remote << indent() << "argvalue" << i << ", " << err
- << " := (strconv.ParseInt(flag.Arg(" << flagArg << "), 10, 64))" << endl
- << indent() << "if " << err << " != nil {" << endl << indent() << " Usage()"
- << endl << indent() << " return" << endl << indent() << "}" << endl;
- break;
+ case t_base_type::TYPE_I32:
+ f_remote <<
+ indent() << "tmp" << i << ", " << err << " := (strconv.Atoi(flag.Arg(" << flagArg << ")))" << endl <<
+ indent() << "if " << err << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "argvalue" << i << " := int32(tmp" << i << ")" << endl;
+ break;
- case t_base_type::TYPE_DOUBLE:
- f_remote << indent() << "argvalue" << i << ", " << err
- << " := (strconv.ParseFloat(flag.Arg(" << flagArg << "), 64))" << endl
- << indent() << "if " << err << " != nil {" << endl << indent() << " Usage()"
- << endl << indent() << " return" << endl << indent() << "}" << endl;
- break;
+ case t_base_type::TYPE_I64:
+ f_remote <<
+ indent() << "argvalue" << i << ", " << err << " := (strconv.ParseInt(flag.Arg(" << flagArg << "), 10, 64))" << endl <<
+ indent() << "if " << err << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl;
+ break;
- default:
- throw("Invalid base type in generate_service_remote");
+ case t_base_type::TYPE_DOUBLE:
+ f_remote <<
+ indent() << "argvalue" << i << ", " << err << " := (strconv.ParseFloat(flag.Arg(" << flagArg << "), 64))" << endl <<
+ indent() << "if " << err << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl;
+ break;
+
+ default:
+ throw ("Invalid base type in generate_service_remote");
+ }
+
+ //f_remote << publicize(args[i]->get_name()) << "(strconv.Atoi(flag.Arg(" << flagArg << ")))";
+ } else if (the_type2->is_struct()) {
+ string arg(tmp("arg"));
+ string mbTrans(tmp("mbTrans"));
+ string err1(tmp("err"));
+ string factory(tmp("factory"));
+ string jsProt(tmp("jsProt"));
+ string err2(tmp("err"));
+ std::string tstruct_name(publicize(the_type->get_name()));
+ f_remote <<
+ indent() << arg << " := flag.Arg(" << flagArg << ")" << endl <<
+ indent() << mbTrans << " := thrift.NewTMemoryBufferLen(len(" << arg << "))" << endl <<
+ indent() << "defer " << mbTrans << ".Close()" << endl <<
+ indent() << "_, " << err1 << " := " << mbTrans << ".WriteString(" << arg << ")" << endl <<
+ indent() << "if " << err1 << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << factory << " := thrift.NewTSimpleJSONProtocolFactory()" << endl <<
+ indent() << jsProt << " := " << factory << ".GetProtocol(" << mbTrans << ")" << endl <<
+ indent() << "argvalue" << i << " := " << package_name_ << ".New" << tstruct_name << "()" << endl <<
+ indent() << err2 << " := argvalue" << i << ".Read(" << jsProt << ")" << endl <<
+ indent() << "if " << err2 << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl;
+ } else if (the_type2->is_container() || the_type2->is_xception()) {
+ string arg(tmp("arg"));
+ string mbTrans(tmp("mbTrans"));
+ string err1(tmp("err"));
+ string factory(tmp("factory"));
+ string jsProt(tmp("jsProt"));
+ string err2(tmp("err"));
+ std::string argName(publicize(args[i]->get_name()));
+ f_remote <<
+ indent() << arg << " := flag.Arg(" << flagArg << ")" << endl <<
+ indent() << mbTrans << " := thrift.NewTMemoryBufferLen(len(" << arg << "))" << endl <<
+ indent() << "defer " << mbTrans << ".Close()" << endl <<
+ indent() << "_, " << err1 << " := " << mbTrans << ".WriteString(" << arg << ")" << endl <<
+ indent() << "if " << err1 << " != nil { " << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << factory << " := thrift.NewTSimpleJSONProtocolFactory()" << endl <<
+ indent() << jsProt << " := " << factory << ".GetProtocol(" << mbTrans << ")" << endl <<
+ indent() << "containerStruct" << i << " := " << package_name_ << ".New" << argumentsName << "()" << endl <<
+ indent() << err2 << " := containerStruct" << i << ".ReadField" << (i + 1) << "(" << jsProt << ")" << endl <<
+ indent() << "if " << err2 << " != nil {" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "argvalue" << i << " := containerStruct" << i << "." << argName << endl;
+ } else {
+ throw ("Invalid argument type in generate_service_remote");
+ }
+
+ if (the_type->is_typedef()) {
+ f_remote <<
+ indent() << "value" << i << " := " << package_name_ << "." << publicize(the_type->get_name()) << "(argvalue" << i << ")" << endl;
+ } else {
+ f_remote <<
+ indent() << "value" << i << " := argvalue" << i << endl;
+ }
}
- // f_remote << publicize(args[i]->get_name()) << "(strconv.Atoi(flag.Arg(" << flagArg <<
- // ")))";
- } else if (the_type2->is_struct()) {
- string arg(tmp("arg"));
- string mbTrans(tmp("mbTrans"));
- string err1(tmp("err"));
- string factory(tmp("factory"));
- string jsProt(tmp("jsProt"));
- string err2(tmp("err"));
- std::string tstruct_name(publicize(the_type->get_name()));
- f_remote << indent() << arg << " := flag.Arg(" << flagArg << ")" << endl << indent()
- << mbTrans << " := thrift.NewTMemoryBufferLen(len(" << arg << "))" << endl
- << indent() << "defer " << mbTrans << ".Close()" << endl << indent() << "_, "
- << err1 << " := " << mbTrans << ".WriteString(" << arg << ")" << endl << indent()
- << "if " << err1 << " != nil {" << endl << indent() << " Usage()" << endl
- << indent() << " return" << endl << indent() << "}" << endl << indent() << factory
- << " := thrift.NewTSimpleJSONProtocolFactory()" << endl << indent() << jsProt
- << " := " << factory << ".GetProtocol(" << mbTrans << ")" << endl << indent()
- << "argvalue" << i << " := " << package_name_ << ".New" << tstruct_name << "()"
- << endl << indent() << err2 << " := argvalue" << i << ".Read(" << jsProt << ")"
- << endl << indent() << "if " << err2 << " != nil {" << endl << indent()
- << " Usage()" << endl << indent() << " return" << endl << indent() << "}"
- << endl;
- } else if (the_type2->is_container() || the_type2->is_xception()) {
- string arg(tmp("arg"));
- string mbTrans(tmp("mbTrans"));
- string err1(tmp("err"));
- string factory(tmp("factory"));
- string jsProt(tmp("jsProt"));
- string err2(tmp("err"));
- std::string argName(publicize(args[i]->get_name()));
- f_remote << indent() << arg << " := flag.Arg(" << flagArg << ")" << endl << indent()
- << mbTrans << " := thrift.NewTMemoryBufferLen(len(" << arg << "))" << endl
- << indent() << "defer " << mbTrans << ".Close()" << endl << indent() << "_, "
- << err1 << " := " << mbTrans << ".WriteString(" << arg << ")" << endl << indent()
- << "if " << err1 << " != nil { " << endl << indent() << " Usage()" << endl
- << indent() << " return" << endl << indent() << "}" << endl << indent() << factory
- << " := thrift.NewTSimpleJSONProtocolFactory()" << endl << indent() << jsProt
- << " := " << factory << ".GetProtocol(" << mbTrans << ")" << endl << indent()
- << "containerStruct" << i << " := " << package_name_ << ".New" << argumentsName
- << "()" << endl << indent() << err2 << " := containerStruct" << i << ".ReadField"
- << (i + 1) << "(" << jsProt << ")" << endl << indent() << "if " << err2
- << " != nil {" << endl << indent() << " Usage()" << endl << indent() << " return"
- << endl << indent() << "}" << endl << indent() << "argvalue" << i
- << " := containerStruct" << i << "." << argName << endl;
- } else {
- throw("Invalid argument type in generate_service_remote");
- }
+ f_remote <<
+ indent() << "fmt.Print(client." << pubName << "(";
+ bool argFirst = true;
- if (the_type->is_typedef()) {
- f_remote << indent() << "value" << i << " := " << package_name_ << "."
- << publicize(the_type->get_name()) << "(argvalue" << i << ")" << endl;
- } else {
- f_remote << indent() << "value" << i << " := argvalue" << i << endl;
- }
- }
+ for (int i = 0; i < num_args; ++i) {
+ if (argFirst) {
+ argFirst = false;
+ } else {
+ f_remote << ", ";
+ }
- f_remote << indent() << "fmt.Print(client." << pubName << "(";
- bool argFirst = true;
+ if (args[i]->get_type()->is_enum()) {
+ f_remote << "value" << i;
+ } else if (args[i]->get_type()->is_base_type()) {
+ t_base_type::t_base e = ((t_base_type*)(args[i]->get_type()))->get_base();
- for (int i = 0; i < num_args; ++i) {
- if (argFirst) {
- argFirst = false;
- } else {
- f_remote << ", ";
- }
+ switch (e) {
+ case t_base_type::TYPE_VOID:
+ break;
- if (args[i]->get_type()->is_enum()) {
- f_remote << "value" << i;
- } else if (args[i]->get_type()->is_base_type()) {
- t_base_type::t_base e = ((t_base_type*)(args[i]->get_type()))->get_base();
+ case t_base_type::TYPE_STRING:
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ f_remote << "value" << i;
+ break;
- switch (e) {
- case t_base_type::TYPE_VOID:
- break;
+ default:
+ throw ("Invalid base type in generate_service_remote");
+ }
- case t_base_type::TYPE_STRING:
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- f_remote << "value" << i;
- break;
-
- default:
- throw("Invalid base type in generate_service_remote");
+ //f_remote << publicize(args[i]->get_name()) << "(strconv.Atoi(flag.Arg(" << flagArg << ")))";
+ } else {
+ f_remote << "value" << i;
+ }
}
- // f_remote << publicize(args[i]->get_name()) << "(strconv.Atoi(flag.Arg(" << flagArg <<
- // ")))";
- } else {
- f_remote << "value" << i;
- }
+ f_remote <<
+ "))" << endl <<
+ indent() << "fmt.Print(\"\\n\")" << endl <<
+ indent() << "break" << endl;
+ indent_down();
}
- f_remote << "))" << endl << indent() << "fmt.Print(\"\\n\")" << endl << indent() << "break"
- << endl;
+ f_remote <<
+ indent() << "case \"\":" << endl <<
+ indent() << " Usage()" << endl <<
+ indent() << " break" << endl <<
+ indent() << "default:" << endl <<
+ indent() << " fmt.Fprintln(os.Stderr, \"Invalid function \", cmd)" << endl <<
+ indent() << "}" << endl;
indent_down();
- }
-
- f_remote << indent() << "case \"\":" << endl << indent() << " Usage()" << endl << indent()
- << " break" << endl << indent() << "default:" << endl << indent()
- << " fmt.Fprintln(os.Stderr, \"Invalid function \", cmd)" << endl << indent() << "}"
- << endl;
- indent_down();
- f_remote << indent() << "}" << endl;
- // Close service file
- f_remote.close();
- format_go_output(f_remote_name);
+ f_remote <<
+ indent() << "}" << endl;
+ // Close service file
+ f_remote.close();
+ format_go_output(f_remote_name);
#ifndef _MSC_VER
- // Make file executable, love that bitwise OR action
- chmod(f_remote_name.c_str(),
- S_IRUSR | S_IWUSR | S_IXUSR
+ // Make file executable, love that bitwise OR action
+ chmod(f_remote_name.c_str(),
+ S_IRUSR
+ | S_IWUSR
+ | S_IXUSR
#ifndef _WIN32
- | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
+ | S_IRGRP
+ | S_IXGRP
+ | S_IROTH
+ | S_IXOTH
#endif
- );
+ );
#endif
}
@@ -2132,101 +2363,103 @@
*
* @param tservice The service to generate a server for.
*/
-void t_go_generator::generate_service_server(t_service* tservice) {
- // Generate the dispatch methods
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
- string extends = "";
- string extends_processor = "";
- string extends_processor_new = "";
- string serviceName(publicize(tservice->get_name()));
+void t_go_generator::generate_service_server(t_service* tservice)
+{
+ // Generate the dispatch methods
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ string extends = "";
+ string extends_processor = "";
+ string extends_processor_new = "";
+ string serviceName(publicize(tservice->get_name()));
- if (tservice->get_extends() != NULL) {
- extends = type_name(tservice->get_extends());
- size_t index = extends.rfind(".");
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ size_t index = extends.rfind(".");
- if (index != string::npos) {
- extends_processor = extends.substr(0, index + 1) + publicize(extends.substr(index + 1))
- + "Processor";
- extends_processor_new = extends.substr(0, index + 1) + "New"
- + publicize(extends.substr(index + 1)) + "Processor";
+ if (index != string::npos) {
+ extends_processor = extends.substr(0, index + 1) + publicize(extends.substr(index + 1)) + "Processor";
+ extends_processor_new = extends.substr(0, index + 1) + "New" + publicize(extends.substr(index + 1)) + "Processor";
+ } else {
+ extends_processor = publicize(extends) + "Processor";
+ extends_processor_new = "New" + extends_processor;
+ }
+ }
+
+ string pServiceName(privatize(serviceName));
+ // Generate the header portion
+ string self(tmp("self"));
+
+ if (extends_processor.empty()) {
+ f_service_ <<
+ indent() << "type " << serviceName << "Processor struct {" << endl <<
+ indent() << " processorMap map[string]thrift.TProcessorFunction" << endl <<
+ indent() << " handler " << serviceName << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << serviceName << "Processor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {" << endl <<
+ indent() << " p.processorMap[key] = processor" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << serviceName << "Processor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) {" << endl <<
+ indent() << " processor, ok = p.processorMap[key]" << endl <<
+ indent() << " return processor, ok" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << serviceName << "Processor) ProcessorMap() map[string]thrift.TProcessorFunction {" << endl <<
+ indent() << " return p.processorMap" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func New" << serviceName << "Processor(handler " << serviceName << ") *" << serviceName << "Processor {" << endl << endl <<
+ indent() << " " << self << " := &" << serviceName << "Processor{handler:handler, processorMap:make(map[string]thrift.TProcessorFunction)}" << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ string escapedFuncName(escape_string((*f_iter)->get_name()));
+ f_service_ <<
+ indent() << " " << self << ".processorMap[\"" << escapedFuncName << "\"] = &" << pServiceName << "Processor" << publicize((*f_iter)->get_name()) << "{handler:handler}" << endl;
+ }
+
+ string x(tmp("x"));
+ f_service_ <<
+ indent() << "return " << self << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << serviceName << "Processor) Process(iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {" << endl <<
+ indent() << " name, _, seqId, err := iprot.ReadMessageBegin()" << endl <<
+ indent() << " if err != nil { return false, err }" << endl <<
+ indent() << " if processor, ok := p.GetProcessorFunction(name); ok {" << endl <<
+ indent() << " return processor.Process(seqId, iprot, oprot)" << endl <<
+ indent() << " }" << endl <<
+ indent() << " iprot.Skip(thrift.STRUCT)" << endl <<
+ indent() << " iprot.ReadMessageEnd()" << endl <<
+ indent() << " " << x << " := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, \"Unknown function \" + name)" << endl <<
+ indent() << " oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId)" << endl <<
+ indent() << " " << x << ".Write(oprot)" << endl <<
+ indent() << " oprot.WriteMessageEnd()" << endl <<
+ indent() << " oprot.Flush()" << endl <<
+ indent() << " return false, " << x << endl <<
+ indent() << "" << endl <<
+ indent() << "}" << endl << endl;
} else {
- extends_processor = publicize(extends) + "Processor";
- extends_processor_new = "New" + extends_processor;
+ f_service_ <<
+ indent() << "type " << serviceName << "Processor struct {" << endl <<
+ indent() << " *" << extends_processor << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func New" << serviceName << "Processor(handler " << serviceName << ") *" << serviceName << "Processor {" << endl <<
+ indent() << " " << self << " := &" << serviceName << "Processor{" << extends_processor_new << "(handler)}" << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ string escapedFuncName(escape_string((*f_iter)->get_name()));
+ f_service_ <<
+ indent() << " " << self << ".AddToProcessorMap(\"" << escapedFuncName << "\", &" << pServiceName << "Processor" << publicize((*f_iter)->get_name()) << "{handler:handler})" << endl;
+ }
+
+ f_service_ <<
+ indent() << " return " << self << endl <<
+ indent() << "}" << endl << endl;
}
- }
- string pServiceName(privatize(serviceName));
- // Generate the header portion
- string self(tmp("self"));
-
- if (extends_processor.empty()) {
- f_service_
- << indent() << "type " << serviceName << "Processor struct {" << endl << indent()
- << " processorMap map[string]thrift.TProcessorFunction" << endl << indent() << " handler "
- << serviceName << endl << indent() << "}" << endl << endl << indent() << "func (p *"
- << serviceName
- << "Processor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {" << endl
- << indent() << " p.processorMap[key] = processor" << endl << indent() << "}" << endl
- << endl << indent() << "func (p *" << serviceName
- << "Processor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok "
- "bool) {" << endl << indent() << " processor, ok = p.processorMap[key]" << endl
- << indent() << " return processor, ok" << endl << indent() << "}" << endl << endl
- << indent() << "func (p *" << serviceName
- << "Processor) ProcessorMap() map[string]thrift.TProcessorFunction {" << endl << indent()
- << " return p.processorMap" << endl << indent() << "}" << endl << endl << indent()
- << "func New" << serviceName << "Processor(handler " << serviceName << ") *" << serviceName
- << "Processor {" << endl << endl << indent() << " " << self << " := &" << serviceName
- << "Processor{handler:handler, processorMap:make(map[string]thrift.TProcessorFunction)}"
- << endl;
-
+ // Generate the process subfunctions
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- string escapedFuncName(escape_string((*f_iter)->get_name()));
- f_service_ << indent() << " " << self << ".processorMap[\"" << escapedFuncName << "\"] = &"
- << pServiceName << "Processor" << publicize((*f_iter)->get_name())
- << "{handler:handler}" << endl;
+ generate_process_function(tservice, *f_iter);
}
- string x(tmp("x"));
- f_service_ << indent() << "return " << self << endl << indent() << "}" << endl << endl
- << indent() << "func (p *" << serviceName
- << "Processor) Process(iprot, oprot thrift.TProtocol) (success bool, err "
- "thrift.TException) {" << endl << indent()
- << " name, _, seqId, err := iprot.ReadMessageBegin()" << endl << indent()
- << " if err != nil { return false, err }" << endl << indent()
- << " if processor, ok := p.GetProcessorFunction(name); ok {" << endl << indent()
- << " return processor.Process(seqId, iprot, oprot)" << endl << indent() << " }"
- << endl << indent() << " iprot.Skip(thrift.STRUCT)" << endl << indent()
- << " iprot.ReadMessageEnd()" << endl << indent() << " " << x
- << " := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, \"Unknown function "
- "\" + name)" << endl << indent()
- << " oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId)" << endl << indent()
- << " " << x << ".Write(oprot)" << endl << indent() << " oprot.WriteMessageEnd()"
- << endl << indent() << " oprot.Flush()" << endl << indent() << " return false, "
- << x << endl << indent() << "" << endl << indent() << "}" << endl << endl;
- } else {
- f_service_ << indent() << "type " << serviceName << "Processor struct {" << endl << indent()
- << " *" << extends_processor << endl << indent() << "}" << endl << endl << indent()
- << "func New" << serviceName << "Processor(handler " << serviceName << ") *"
- << serviceName << "Processor {" << endl << indent() << " " << self << " := &"
- << serviceName << "Processor{" << extends_processor_new << "(handler)}" << endl;
-
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- string escapedFuncName(escape_string((*f_iter)->get_name()));
- f_service_ << indent() << " " << self << ".AddToProcessorMap(\"" << escapedFuncName
- << "\", &" << pServiceName << "Processor" << publicize((*f_iter)->get_name())
- << "{handler:handler})" << endl;
- }
-
- f_service_ << indent() << " return " << self << endl << indent() << "}" << endl << endl;
- }
-
- // Generate the process subfunctions
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- generate_process_function(tservice, *f_iter);
- }
-
- f_service_ << endl;
+ f_service_ << endl;
}
/**
@@ -2234,476 +2467,534 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_go_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- // Open function
- string processorName = privatize(tservice->get_name()) + "Processor"
- + publicize(tfunction->get_name());
- string argsname = publicize(tfunction->get_name() + "_args", true);
- string resultname = publicize(tfunction->get_name() + "_result", true);
- // t_struct* xs = tfunction->get_xceptions();
- // const std::vector<t_field*>& xceptions = xs->get_members();
- vector<t_field*>::const_iterator x_iter;
- f_service_ << indent() << "type " << processorName << " struct {" << endl << indent()
- << " handler " << publicize(tservice->get_name()) << endl << indent() << "}" << endl
- << endl << indent() << "func (p *" << processorName
- << ") Process(seqId int32, iprot, oprot thrift.TProtocol) (success bool, err "
- "thrift.TException) {" << endl;
- indent_up();
- f_service_ << indent() << "args := " << argsname << "{}" << endl << indent()
- << "if err = args.Read(iprot); err != nil {" << endl << indent()
- << " iprot.ReadMessageEnd()" << endl;
- if (!tfunction->is_oneway()) {
- f_service_ << indent()
- << " x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())"
- << endl << indent() << " oprot.WriteMessageBegin(\""
- << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl
- << indent() << " x.Write(oprot)" << endl << indent() << " oprot.WriteMessageEnd()"
- << endl << indent() << " oprot.Flush()" << endl;
- }
- f_service_ << indent() << " return false, err" << endl << indent() << "}" << endl << endl
- << indent() << "iprot.ReadMessageEnd()" << endl;
-
- if (!tfunction->is_oneway()) {
- f_service_ << indent() << "result := " << resultname << "{}" << endl;
- }
- bool need_reference = type_need_reference(tfunction->get_returntype());
- if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
- f_service_ << "var retval " << type_to_go_type(tfunction->get_returntype()) << endl;
- }
-
- f_service_ << indent() << "var err2 error" << endl << indent() << "if ";
-
- if (!tfunction->is_oneway()) {
- if (!tfunction->get_returntype()->is_void()) {
- f_service_ << "retval, ";
+void t_go_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction)
+{
+ // Open function
+ string processorName = privatize(tservice->get_name()) + "Processor" + publicize(tfunction->get_name());
+ string argsname = publicize(tfunction->get_name() + "_args",true);
+ string resultname = publicize(tfunction->get_name() + "_result",true);
+ //t_struct* xs = tfunction->get_xceptions();
+ //const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ f_service_ <<
+ indent() << "type " << processorName << " struct {" << endl <<
+ indent() << " handler " << publicize(tservice->get_name()) << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "func (p *" << processorName << ") Process(seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "args := " << argsname << "{}" << endl <<
+ indent() << "if err = args.Read(iprot); err != nil {" << endl <<
+ indent() << " iprot.ReadMessageEnd()" << endl;
+ if (!tfunction->is_oneway()) {
+ f_service_ <<
+ indent() << " x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())" << endl <<
+ indent() << " oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl <<
+ indent() << " x.Write(oprot)" << endl <<
+ indent() << " oprot.WriteMessageEnd()" << endl <<
+ indent() << " oprot.Flush()" << endl;
}
- }
+ f_service_ <<
+ indent() << " return false, err" << endl <<
+ indent() << "}" << endl << endl <<
+ indent() << "iprot.ReadMessageEnd()" << endl;
- // Generate the function call
- t_struct* arg_struct = tfunction->get_arglist();
- const std::vector<t_field*>& fields = arg_struct->get_members();
- vector<t_field*>::const_iterator f_iter;
- f_service_ << "err2 = p.handler." << publicize(tfunction->get_name()) << "(";
- bool first = true;
+ if (!tfunction->is_oneway()) {
+ f_service_ <<
+ indent() << "result := " << resultname << "{}" << endl;
+ }
+ bool need_reference = type_need_reference(tfunction->get_returntype());
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
+ f_service_ << "var retval " <<type_to_go_type(tfunction->get_returntype()) <<endl;
+ }
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
+ f_service_ <<
+ indent() << "var err2 error" << endl <<
+ indent() << "if ";
+
+ if (!tfunction->is_oneway()) {
+ if (!tfunction->get_returntype()->is_void()) {
+ f_service_ << "retval, ";
+ }
+ }
+
+ // Generate the function call
+ t_struct* arg_struct = tfunction->get_arglist();
+ const std::vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ f_service_ <<
+ "err2 = p.handler." << publicize(tfunction->get_name()) << "(";
+ bool first = true;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+
+ f_service_ << "args." << publicize(variable_name_to_go_name((*f_iter)->get_name()));
+ }
+
+ f_service_ << "); err2 != nil {" << endl;
+
+ t_struct* exceptions = tfunction->get_xceptions();
+ const vector<t_field*>& x_fields = exceptions->get_members();
+ if( ! x_fields.empty()) {
+ f_service_ << indent() << "switch v := err2.(type) {" << endl;
+
+ vector<t_field*>::const_iterator xf_iter;
+
+ for (xf_iter = x_fields.begin(); xf_iter != x_fields.end(); ++xf_iter) {
+ f_service_ <<
+ indent() << " case " << type_to_go_type(((*xf_iter)->get_type())) << ":" << endl <<
+ indent() << "result." << publicize(variable_name_to_go_name((*xf_iter)->get_name())) << " = v" << endl;
+ }
+
+ f_service_ <<
+ indent() << " default:" << endl;
+ }
+
+ if (!tfunction->is_oneway()) {
+ f_service_ <<
+ indent() << " x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, \"Internal error processing " <<
+ escape_string(tfunction->get_name()) << ": \" + err2.Error())" << endl <<
+ indent() << " oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl <<
+ indent() << " x.Write(oprot)" << endl <<
+ indent() << " oprot.WriteMessageEnd()" << endl <<
+ indent() << " oprot.Flush()" << endl ;
+ }
+
+ f_service_ <<
+ indent() << " return true, err2" << endl ;
+
+ if( ! x_fields.empty()) {
+ f_service_ <<
+ indent() << "}" << endl;
+ }
+
+ f_service_ <<
+ indent() << "}"; // closes err2 != nil
+
+ if (!tfunction->is_oneway()) {
+ if (!tfunction->get_returntype()->is_void()) {
+ f_service_ << " else {" << endl; // make sure we set Success retval only on success
+ indent_up();
+ f_service_ <<
+ indent() << "result.Success = ";
+ if(need_reference) {
+ f_service_ << "&";
+ }
+ f_service_ <<
+ "retval" << endl;
+ indent_down();
+ f_service_ << "}" << endl;
+ } else {
+ f_service_ << endl;
+ }
+ f_service_ <<
+ indent() << "if err2 = oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) <<
+ "\", thrift.REPLY, seqId); err2 != nil {" << endl <<
+ indent() << " err = err2" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err2 = result.Write(oprot); err == nil && err2 != nil {" << endl <<
+ indent() << " err = err2" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil {" << endl <<
+ indent() << " err = err2" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err2 = oprot.Flush(); err == nil && err2 != nil {" << endl <<
+ indent() << " err = err2" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return" << endl <<
+ indent() << "}" << endl <<
+ indent() << "return true, err" << endl;
} else {
- f_service_ << ", ";
+ f_service_ << endl <<
+ indent() << "return true, nil" << endl;
}
-
- f_service_ << "args." << publicize(variable_name_to_go_name((*f_iter)->get_name()));
- }
-
- f_service_ << "); err2 != nil {" << endl;
-
- t_struct* exceptions = tfunction->get_xceptions();
- const vector<t_field*>& x_fields = exceptions->get_members();
- if (!x_fields.empty()) {
- f_service_ << indent() << "switch v := err2.(type) {" << endl;
-
- vector<t_field*>::const_iterator xf_iter;
-
- for (xf_iter = x_fields.begin(); xf_iter != x_fields.end(); ++xf_iter) {
- f_service_ << indent() << " case " << type_to_go_type(((*xf_iter)->get_type())) << ":"
- << endl << indent() << "result."
- << publicize(variable_name_to_go_name((*xf_iter)->get_name())) << " = v" << endl;
- }
-
- f_service_ << indent() << " default:" << endl;
- }
-
- if (!tfunction->is_oneway()) {
- f_service_ << indent() << " x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "
- "\"Internal error processing " << escape_string(tfunction->get_name())
- << ": \" + err2.Error())" << endl << indent() << " oprot.WriteMessageBegin(\""
- << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl
- << indent() << " x.Write(oprot)" << endl << indent() << " oprot.WriteMessageEnd()"
- << endl << indent() << " oprot.Flush()" << endl;
- }
-
- f_service_ << indent() << " return true, err2" << endl;
-
- if (!x_fields.empty()) {
- f_service_ << indent() << "}" << endl;
- }
-
- f_service_ << indent() << "}"; // closes err2 != nil
-
- if (!tfunction->is_oneway()) {
- if (!tfunction->get_returntype()->is_void()) {
- f_service_ << " else {" << endl; // make sure we set Success retval only on success
- indent_up();
- f_service_ << indent() << "result.Success = ";
- if (need_reference) {
- f_service_ << "&";
- }
- f_service_ << "retval" << endl;
- indent_down();
- f_service_ << "}" << endl;
- } else {
- f_service_ << endl;
- }
- f_service_ << indent() << "if err2 = oprot.WriteMessageBegin(\""
- << escape_string(tfunction->get_name()) << "\", thrift.REPLY, seqId); err2 != nil {"
- << endl << indent() << " err = err2" << endl << indent() << "}" << endl << indent()
- << "if err2 = result.Write(oprot); err == nil && err2 != nil {" << endl << indent()
- << " err = err2" << endl << indent() << "}" << endl << indent()
- << "if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil {" << endl
- << indent() << " err = err2" << endl << indent() << "}" << endl << indent()
- << "if err2 = oprot.Flush(); err == nil && err2 != nil {" << endl << indent()
- << " err = err2" << endl << indent() << "}" << endl << indent() << "if err != nil {"
- << endl << indent() << " return" << endl << indent() << "}" << endl << indent()
- << "return true, err" << endl;
- } else {
- f_service_ << endl << indent() << "return true, nil" << endl;
- }
- indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl << endl;
}
/**
* Deserializes a field of any type.
*/
-void t_go_generator::generate_deserialize_field(ofstream& out,
- t_field* tfield,
- bool declare,
- string prefix,
- bool inclass,
- bool coerceData,
- bool inkey,
- bool in_container_value) {
- (void)inclass;
- (void)coerceData;
- t_type* orig_type = tfield->get_type();
- t_type* type = get_true_type(orig_type);
- string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
+void t_go_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ bool declare,
+ string prefix,
+ bool inclass,
+ bool coerceData,
+ bool inkey,
+ bool in_container_value)
+{
+ (void) inclass;
+ (void) coerceData;
+ t_type* orig_type = tfield->get_type();
+ t_type* type = get_true_type(orig_type);
+ string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
- if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + name;
- }
-
- if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out,
- (t_struct*)type,
- is_pointer_field(tfield, in_container_value),
- declare,
- name);
- } else if (type->is_container()) {
- generate_deserialize_container(out, orig_type, is_pointer_field(tfield), declare, name);
- } else if (type->is_base_type() || type->is_enum()) {
-
- if (declare) {
- string type_name = inkey ? type_to_go_key_type(tfield->get_type())
- : type_to_go_type(tfield->get_type());
- out << "var " << tfield->get_name() << " " << type_name << endl;
+ if (type->is_void()) {
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + name;
}
- indent(out) << "if v, err := iprot.";
+ if (type->is_struct() || type->is_xception()) {
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ is_pointer_field(tfield, in_container_value),
+ declare,
+ name);
+ } else if (type->is_container()) {
+ generate_deserialize_container(out,
+ orig_type,
+ is_pointer_field(tfield),
+ declare,
+ name);
+ } else if (type->is_base_type() || type->is_enum()) {
- 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:
- if (((t_base_type*)type)->is_binary() && !inkey) {
- out << "ReadBinary()";
- } else {
- out << "ReadString()";
+ if (declare) {
+ string type_name = inkey ? type_to_go_key_type(tfield->get_type()) :
+ type_to_go_type(tfield->get_type());
+ out << "var " << tfield->get_name() << " " << type_name << endl;
}
- break;
+ indent(out) <<
+ "if v, err := iprot.";
- case t_base_type::TYPE_BOOL:
- out << "ReadBool()";
- break;
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
- case t_base_type::TYPE_BYTE:
- out << "ReadByte()";
- break;
+ 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_I16:
- out << "ReadI16()";
- break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary() && !inkey) {
+ out << "ReadBinary()";
+ } else {
+ out << "ReadString()";
+ }
- case t_base_type::TYPE_I32:
- out << "ReadI32()";
- break;
+ break;
- case t_base_type::TYPE_I64:
- out << "ReadI64()";
- break;
+ case t_base_type::TYPE_BOOL:
+ out << "ReadBool()";
+ break;
- case t_base_type::TYPE_DOUBLE:
- out << "ReadDouble()";
- break;
+ case t_base_type::TYPE_BYTE:
+ out << "ReadByte()";
+ break;
- default:
- throw "compiler error: no Go name for base type " + t_base_type::t_base_name(tbase);
- }
- } else if (type->is_enum()) {
- out << "ReadI32()";
- }
+ case t_base_type::TYPE_I16:
+ out << "ReadI16()";
+ break;
- out << "; err != nil {" << endl << indent() << "return fmt.Errorf(\"error reading field "
- << tfield->get_key() << ": %s\", err)" << endl;
+ case t_base_type::TYPE_I32:
+ out << "ReadI32()";
+ break;
- out << "} else {" << endl;
- string wrap;
+ case t_base_type::TYPE_I64:
+ out << "ReadI64()";
+ break;
- if (type->is_enum() || orig_type->is_typedef()) {
- wrap = publicize(type_name(orig_type));
- } else if (((t_base_type*)type)->get_base() == t_base_type::TYPE_BYTE) {
- wrap = "int8";
- }
+ case t_base_type::TYPE_DOUBLE:
+ out << "ReadDouble()";
+ break;
- string maybe_address = (is_pointer_field(tfield) ? "&" : "");
- if (wrap == "") {
- indent(out) << name << " = " << maybe_address << "v" << endl;
+ default:
+ throw "compiler error: no Go name for base type " + t_base_type::t_base_name(tbase);
+ }
+ } else if (type->is_enum()) {
+ out << "ReadI32()";
+ }
+
+ out << "; err != nil {" << endl <<
+ indent() << "return fmt.Errorf(\"error reading field " <<
+ tfield->get_key() << ": %s\", err)" << endl;
+
+ out << "} else {" << endl;
+ string wrap;
+
+ if (type->is_enum() || orig_type->is_typedef()) {
+ wrap = publicize(type_name(orig_type));
+ } else if (((t_base_type*)type)->get_base() == t_base_type::TYPE_BYTE) {
+ wrap = "int8";
+ }
+
+ string maybe_address = (is_pointer_field(tfield) ? "&" : "");
+ if (wrap == "") {
+ indent(out) << name << " = " << maybe_address << "v" << endl;
+ } else {
+ indent(out) << "temp := " << wrap << "(v)" << endl;
+ indent(out) << name << " = " << maybe_address << "temp" << endl;
+ }
+
+ out << "}" << endl;
} else {
- indent(out) << "temp := " << wrap << "(v)" << endl;
- indent(out) << name << " = " << maybe_address << "temp" << endl;
+ throw "INVALID TYPE IN generate_deserialize_field '" + type->get_name() + "' for field '" + tfield->get_name() + "'";
}
-
- out << "}" << endl;
- } else {
- throw "INVALID TYPE IN generate_deserialize_field '" + type->get_name() + "' for field '"
- + tfield->get_name() + "'";
- }
}
/**
* Generates an unserializer for a struct, calling read()
*/
-void t_go_generator::generate_deserialize_struct(ofstream& out,
- t_struct* tstruct,
- bool pointer_field,
- bool declare,
- string prefix) {
- string eq(declare ? " := " : " = ");
+void t_go_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ bool pointer_field,
+ bool declare,
+ string prefix)
+{
+ string eq(declare ? " := " : " = ");
- out << indent() << prefix << eq << (pointer_field ? "&" : "");
- generate_go_struct_initializer(out, tstruct);
- out << indent() << "if err := " << prefix << ".Read(iprot); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"%T error reading struct: %s\", " << prefix << ", err)" << endl
- << indent() << "}" << endl;
+ out << indent() << prefix << eq <<(pointer_field?"&":"");
+ generate_go_struct_initializer(out, tstruct);
+ out <<
+ indent() << "if err := " << prefix << ".Read(iprot); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T error reading struct: %s\", " << prefix << ", err)" << endl <<
+ indent() << "}" << endl;
}
/**
* Serialize a container by writing out the header followed by
* data and then a footer.
*/
-void t_go_generator::generate_deserialize_container(ofstream& out,
- t_type* orig_type,
- bool pointer_field,
- bool declare,
- string prefix) {
- t_type* ttype = get_true_type(orig_type);
- string eq(" = ");
+void t_go_generator::generate_deserialize_container(ofstream &out,
+ t_type* orig_type,
+ bool pointer_field,
+ bool declare,
+ string prefix)
+{
+ t_type* ttype = get_true_type(orig_type);
+ string eq(" = ");
- if (declare) {
- eq = " := ";
- }
+ if (declare) {
+ eq = " := ";
+ }
- // Declare variables, read header
- if (ttype->is_map()) {
- out << indent() << "_, _, size, err := iprot.ReadMapBegin()" << endl << indent()
- << "if err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading map begin: %s\", err)" << endl << indent() << "}"
- << endl << indent() << "tMap := make(" << type_to_go_type(orig_type) << ", size)" << endl
- << indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tMap" << endl;
- } else if (ttype->is_set()) {
- t_set* t = (t_set*)ttype;
- out << indent() << "_, size, err := iprot.ReadSetBegin()" << endl << indent()
- << "if err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading set begin: %s\", err)" << endl << indent() << "}"
- << endl << indent() << "tSet := make(map["
- << type_to_go_key_type(t->get_elem_type()->get_true_type()) << "]bool, size)" << endl
- << indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tSet" << endl;
- } else if (ttype->is_list()) {
- out << indent() << "_, size, err := iprot.ReadListBegin()" << endl << indent()
- << "if err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading list begin: %s\", err)" << endl << indent() << "}"
- << endl << indent() << "tSlice := make(" << type_to_go_type(orig_type) << ", 0, size)"
- << endl << indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tSlice"
- << endl;
- } else {
- throw "INVALID TYPE IN generate_deserialize_container '" + ttype->get_name() + "' for prefix '"
- + prefix + "'";
- }
+ // Declare variables, read header
+ if (ttype->is_map()) {
+ out <<
+ indent() << "_, _, size, err := iprot.ReadMapBegin()" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading map begin: %s\", err)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "tMap := make(" << type_to_go_type(orig_type) << ", size)" << endl <<
+ indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tMap" << endl;
+ } else if (ttype->is_set()) {
+ t_set* t = (t_set*)ttype;
+ out <<
+ indent() << "_, size, err := iprot.ReadSetBegin()" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading set begin: %s\", err)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "tSet := make(map[" << type_to_go_key_type(t->get_elem_type()->get_true_type()) << "]bool, size)" << endl <<
+ indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tSet" << endl;
+ } else if (ttype->is_list()) {
+ out <<
+ indent() << "_, size, err := iprot.ReadListBegin()" << endl <<
+ indent() << "if err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading list begin: %s\", err)" << endl <<
+ indent() << "}" << endl <<
+ indent() << "tSlice := make(" << type_to_go_type(orig_type) << ", 0, size)" << endl <<
+ indent() << prefix << eq << " " << (pointer_field ? "&" : "") << "tSlice" << endl;
+ } else {
+ throw "INVALID TYPE IN generate_deserialize_container '" + ttype->get_name() + "' for prefix '" + prefix + "'";
+ }
- // For loop iterates over elements
- out << indent() << "for i := 0; i < size; i ++ {" << endl;
- indent_up();
+ // For loop iterates over elements
+ out <<
+ indent() << "for i := 0; i < size; i ++ {" << endl;
+ indent_up();
- if (pointer_field) {
- prefix = "(*" + prefix + ")";
- }
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, declare, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, declare, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, declare, prefix);
- }
+ if (pointer_field) {
+ prefix = "(*" + prefix + ")";
+ }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, declare, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, declare, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, declare, prefix);
+ }
- indent_down();
- out << indent() << "}" << endl;
+ indent_down();
+ out <<
+ indent() << "}" << endl;
- // Read container end
- if (ttype->is_map()) {
- out << indent() << "if err := iprot.ReadMapEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading map end: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_set()) {
- out << indent() << "if err := iprot.ReadSetEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading set end: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_list()) {
- out << indent() << "if err := iprot.ReadListEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error reading list end: %s\", err)" << endl << indent() << "}"
- << endl;
- }
+ // Read container end
+ if (ttype->is_map()) {
+ out <<
+ indent() << "if err := iprot.ReadMapEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading map end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_set()) {
+ out <<
+ indent() << "if err := iprot.ReadSetEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading set end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_list()) {
+ out <<
+ indent() << "if err := iprot.ReadListEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error reading list end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ }
}
+
/**
* Generates code to deserialize a map
*/
-void t_go_generator::generate_deserialize_map_element(ofstream& out,
- t_map* tmap,
- bool declare,
- string prefix) {
- (void)declare;
- string key = tmp("_key");
- string val = tmp("_val");
- t_field fkey(tmap->get_key_type(), key);
- t_field fval(tmap->get_val_type(), val);
- fkey.set_req(t_field::T_OPT_IN_REQ_OUT);
- fval.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_deserialize_field(out, &fkey, true, "", false, false, true);
- generate_deserialize_field(out, &fval, true, "", false, false, false, true);
- indent(out) << prefix << "[" << key << "] = " << val << endl;
+void t_go_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ bool declare,
+ string prefix)
+{
+ (void) declare;
+ string key = tmp("_key");
+ string val = tmp("_val");
+ t_field fkey(tmap->get_key_type(), key);
+ t_field fval(tmap->get_val_type(), val);
+ fkey.set_req(t_field::T_OPT_IN_REQ_OUT);
+ fval.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_deserialize_field(out, &fkey, true, "", false, false, true);
+ generate_deserialize_field(out, &fval, true, "", false, false, false, true);
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << endl;
+
}
/**
* Write a set element
*/
-void t_go_generator::generate_deserialize_set_element(ofstream& out,
- t_set* tset,
- bool declare,
- string prefix) {
- (void)declare;
- string elem = tmp("_elem");
- t_field felem(tset->get_elem_type(), elem);
- felem.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_deserialize_field(out, &felem, true, "");
- indent(out) << prefix << "[" << elem << "] = true" << endl;
+void t_go_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ bool declare,
+ string prefix)
+{
+ (void) declare;
+ string elem = tmp("_elem");
+ t_field felem(tset->get_elem_type(), elem);
+ felem.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_deserialize_field(out, &felem, true, "");
+ indent(out) <<
+ prefix << "[" << elem << "] = true" << endl;
}
/**
* Write a list element
*/
-void t_go_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- bool declare,
- string prefix) {
- (void)declare;
- string elem = tmp("_elem");
- t_field felem(((t_list*)tlist)->get_elem_type(), elem);
- felem.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_deserialize_field(out, &felem, true, "", false, false, false, true);
- indent(out) << prefix << " = append(" << prefix << ", " << elem << ")" << endl;
+void t_go_generator::generate_deserialize_list_element(ofstream &out,
+ t_list* tlist,
+ bool declare,
+ string prefix)
+{
+ (void) declare;
+ string elem = tmp("_elem");
+ t_field felem(((t_list*)tlist)->get_elem_type(), elem);
+ felem.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_deserialize_field(out, &felem, true, "", false, false, false, true);
+ indent(out) <<
+ prefix << " = append(" << prefix << ", " << elem << ")" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_go_generator::generate_serialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- bool inkey) {
- t_type* type = get_true_type(tfield->get_type());
- string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
+void t_go_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix,
+ bool inkey)
+{
+ t_type* type = get_true_type(tfield->get_type());
+ string name(prefix + publicize(variable_name_to_go_name(tfield->get_name())));
- // Do nothing for void types
- if (type->is_void()) {
- throw "compiler error: cannot generate serialize for void type: " + name;
- }
-
- if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, name);
- } else if (type->is_container()) {
- generate_serialize_container(out, type, is_pointer_field(tfield), name);
- } else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "if err := oprot.";
-
- if (is_pointer_field(tfield)) {
- name = "*" + name;
+ // Do nothing for void types
+ if (type->is_void()) {
+ throw "compiler error: cannot generate serialize for void type: " + name;
}
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ if (type->is_struct() || type->is_xception()) {
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ name);
+ } else if (type->is_container()) {
+ generate_serialize_container(out,
+ type,
+ is_pointer_field(tfield),
+ name);
+ } else if (type->is_base_type() || type->is_enum()) {
+ indent(out) <<
+ "if err := oprot.";
- 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:
- if (((t_base_type*)type)->is_binary() && !inkey) {
- out << "WriteBinary(" << name << ")";
- } else {
- out << "WriteString(string(" << name << "))";
+ if (is_pointer_field(tfield)) {
+ name = "*" + name;
}
- break;
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
- case t_base_type::TYPE_BOOL:
- out << "WriteBool(bool(" << name << "))";
- break;
+ 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_BYTE:
- out << "WriteByte(byte(" << name << "))";
- break;
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary() && !inkey) {
+ out << "WriteBinary(" << name << ")";
+ } else {
+ out << "WriteString(string(" << name << "))";
+ }
- case t_base_type::TYPE_I16:
- out << "WriteI16(int16(" << name << "))";
- break;
+ break;
- case t_base_type::TYPE_I32:
- out << "WriteI32(int32(" << name << "))";
- break;
+ case t_base_type::TYPE_BOOL:
+ out << "WriteBool(bool(" << name << "))";
+ break;
- case t_base_type::TYPE_I64:
- out << "WriteI64(int64(" << name << "))";
- break;
+ case t_base_type::TYPE_BYTE:
+ out << "WriteByte(byte(" << name << "))";
+ break;
- case t_base_type::TYPE_DOUBLE:
- out << "WriteDouble(float64(" << name << "))";
- break;
+ case t_base_type::TYPE_I16:
+ out << "WriteI16(int16(" << name << "))";
+ break;
- default:
- throw "compiler error: no Go name for base type " + t_base_type::t_base_name(tbase);
- }
- } else if (type->is_enum()) {
- out << "WriteI32(int32(" << name << "))";
+ case t_base_type::TYPE_I32:
+ out << "WriteI32(int32(" << name << "))";
+ break;
+
+ case t_base_type::TYPE_I64:
+ out << "WriteI64(int64(" << name << "))";
+ break;
+
+ case t_base_type::TYPE_DOUBLE:
+ out << "WriteDouble(float64(" << name << "))";
+ break;
+
+ default:
+ throw "compiler error: no Go name for base type " + t_base_type::t_base_name(tbase);
+ }
+ } else if (type->is_enum()) {
+ out << "WriteI32(int32(" << name << "))";
+ }
+
+ out << "; err != nil {" << endl
+ << indent() << "return fmt.Errorf(\"%T." << escape_string(tfield->get_name())
+ << " (" << tfield->get_key() << ") field write error: %s\", p, err) }" << endl;
+ } else {
+ throw "compiler error: Invalid type in generate_serialize_field '" + type->get_name() + "' for field '" + name + "'";
}
-
- out << "; err != nil {" << endl << indent() << "return fmt.Errorf(\"%T."
- << escape_string(tfield->get_name()) << " (" << tfield->get_key()
- << ") field write error: %s\", p, err) }" << endl;
- } else {
- throw "compiler error: Invalid type in generate_serialize_field '" + type->get_name()
- + "' for field '" + name + "'";
- }
}
/**
@@ -2712,181 +3003,214 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_go_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
- out << indent() << "if err := " << prefix << ".Write(oprot); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"%T error writing struct: %s\", " << prefix << ", err)" << endl
- << indent() << "}" << endl;
+void t_go_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix)
+{
+ (void) tstruct;
+ out <<
+ indent() << "if err := " << prefix << ".Write(oprot); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"%T error writing struct: %s\", " << prefix << ", err)" << endl <<
+ indent() << "}" << endl;
}
-void t_go_generator::generate_serialize_container(ofstream& out,
- t_type* ttype,
- bool pointer_field,
- string prefix) {
- if (pointer_field) {
- prefix = "*" + prefix;
- }
- if (ttype->is_map()) {
- out << indent() << "if err := oprot.WriteMapBegin("
- << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "len(" << prefix << ")); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing map begin: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_set()) {
- out << indent() << "if err := oprot.WriteSetBegin("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", "
- << "len(" << prefix << ")); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing set begin: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_list()) {
- out << indent() << "if err := oprot.WriteListBegin("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", "
- << "len(" << prefix << ")); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing list begin: %s\", err)" << endl << indent() << "}"
- << endl;
- } else {
- throw "compiler error: Invalid type in generate_serialize_container '" + ttype->get_name()
- + "' for prefix '" + prefix + "'";
- }
+void t_go_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ bool pointer_field,
+ string prefix)
+{
+ if (pointer_field) {
+ prefix = "*" + prefix;
+ }
+ if (ttype->is_map()) {
+ out <<
+ indent() << "if err := oprot.WriteMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "len(" << prefix << ")); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing map begin: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_set()) {
+ out <<
+ indent() << "if err := oprot.WriteSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "len(" << prefix << ")); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing set begin: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_list()) {
+ out <<
+ indent() << "if err := oprot.WriteListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "len(" << prefix << ")); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing list begin: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else {
+ throw "compiler error: Invalid type in generate_serialize_container '" + ttype->get_name() + "' for prefix '" + prefix + "'";
+ }
- if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
- out << indent() << "for k, v := range " << prefix << " {" << endl;
- indent_up();
- generate_serialize_map_element(out, tmap, "k", "v");
- indent_down();
- indent(out) << "}" << endl;
- } else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
- out << indent() << "for v, _ := range " << prefix << " {" << endl;
- indent_up();
- generate_serialize_set_element(out, tset, "v");
- indent_down();
- indent(out) << "}" << endl;
- } else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
- out << indent() << "for _, v := range " << prefix << " {" << endl;
+ if (ttype->is_map()) {
+ t_map* tmap = (t_map*)ttype;
+ out <<
+ indent() << "for k, v := range " << prefix << " {" << endl;
+ indent_up();
+ generate_serialize_map_element(out, tmap, "k", "v");
+ indent_down();
+ indent(out) << "}" << endl;
+ } else if (ttype->is_set()) {
+ t_set* tset = (t_set*)ttype;
+ out <<
+ indent() << "for v, _ := range " << prefix << " {" << endl;
+ indent_up();
+ generate_serialize_set_element(out, tset, "v");
+ indent_down();
+ indent(out) << "}" << endl;
+ } else if (ttype->is_list()) {
+ t_list* tlist = (t_list*)ttype;
+ out <<
+ indent() << "for _, v := range " << prefix << " {" << endl;
- indent_up();
- generate_serialize_list_element(out, tlist, "v");
- indent_down();
- indent(out) << "}" << endl;
- }
+ indent_up();
+ generate_serialize_list_element(out, tlist, "v");
+ indent_down();
+ indent(out) << "}" << endl;
+ }
- if (ttype->is_map()) {
- out << indent() << "if err := oprot.WriteMapEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing map end: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_set()) {
- out << indent() << "if err := oprot.WriteSetEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing set end: %s\", err)" << endl << indent() << "}"
- << endl;
- } else if (ttype->is_list()) {
- out << indent() << "if err := oprot.WriteListEnd(); err != nil {" << endl << indent()
- << " return fmt.Errorf(\"error writing list end: %s\", err)" << endl << indent() << "}"
- << endl;
- }
+ if (ttype->is_map()) {
+ out <<
+ indent() << "if err := oprot.WriteMapEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing map end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_set()) {
+ out <<
+ indent() << "if err := oprot.WriteSetEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing set end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ } else if (ttype->is_list()) {
+ out <<
+ indent() << "if err := oprot.WriteListEnd(); err != nil {" << endl <<
+ indent() << " return fmt.Errorf(\"error writing list end: %s\", err)" << endl <<
+ indent() << "}" << endl;
+ }
}
/**
* Serializes the members of a map.
*
*/
-void t_go_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string kiter,
- string viter) {
- t_field kfield(tmap->get_key_type(), "");
- t_field vfield(tmap->get_val_type(), "");
- kfield.set_req(t_field::T_OPT_IN_REQ_OUT);
- vfield.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_serialize_field(out, &kfield, kiter, true);
- generate_serialize_field(out, &vfield, viter);
+void t_go_generator::generate_serialize_map_element(ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter)
+{
+ t_field kfield(tmap->get_key_type(), "");
+ t_field vfield(tmap->get_val_type(), "");
+ kfield.set_req(t_field::T_OPT_IN_REQ_OUT);
+ vfield.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_serialize_field(out, &kfield, kiter, true);
+ generate_serialize_field(out, &vfield, viter);
}
/**
* Serializes the members of a set.
*/
-void t_go_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string prefix) {
- t_field efield(tset->get_elem_type(), "");
- efield.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_serialize_field(out, &efield, prefix);
+void t_go_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix)
+{
+ t_field efield(tset->get_elem_type(), "");
+ efield.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_serialize_field(out, &efield, prefix);
}
/**
* Serializes the members of a list.
*/
-void t_go_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string prefix) {
- t_field efield(tlist->get_elem_type(), "");
- efield.set_req(t_field::T_OPT_IN_REQ_OUT);
- generate_serialize_field(out, &efield, prefix);
+void t_go_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string prefix)
+{
+ t_field efield(tlist->get_elem_type(), "");
+ efield.set_req(t_field::T_OPT_IN_REQ_OUT);
+ generate_serialize_field(out, &efield, prefix);
}
/**
* Generates the docstring for a given struct.
*/
-void t_go_generator::generate_go_docstring(ofstream& out, t_struct* tstruct) {
- generate_go_docstring(out, tstruct, tstruct, "Attributes");
+void t_go_generator::generate_go_docstring(ofstream& out,
+ t_struct* tstruct)
+{
+ generate_go_docstring(out, tstruct, tstruct, "Attributes");
}
/**
* Generates the docstring for a given function.
*/
-void t_go_generator::generate_go_docstring(ofstream& out, t_function* tfunction) {
- generate_go_docstring(out, tfunction, tfunction->get_arglist(), "Parameters");
+void t_go_generator::generate_go_docstring(ofstream& out,
+ t_function* tfunction)
+{
+ generate_go_docstring(out, tfunction, tfunction->get_arglist(), "Parameters");
}
/**
* Generates the docstring for a struct or function.
*/
void t_go_generator::generate_go_docstring(ofstream& out,
- t_doc* tdoc,
- t_struct* tstruct,
- const char* subheader) {
- bool has_doc = false;
- stringstream ss;
+ t_doc* tdoc,
+ t_struct* tstruct,
+ const char* subheader)
+{
+ bool has_doc = false;
+ stringstream ss;
- if (tdoc->has_doc()) {
- has_doc = true;
- ss << tdoc->get_doc();
- }
+ if (tdoc->has_doc()) {
+ has_doc = true;
+ ss << tdoc->get_doc();
+ }
- const vector<t_field*>& fields = tstruct->get_members();
+ const vector<t_field*>& fields = tstruct->get_members();
- if (fields.size() > 0) {
+ if (fields.size() > 0) {
+ if (has_doc) {
+ ss << endl;
+ }
+
+ has_doc = true;
+ ss << subheader << ":\n";
+ vector<t_field*>::const_iterator p_iter;
+
+ for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
+ t_field* p = *p_iter;
+ ss << " - " << publicize(variable_name_to_go_name(p->get_name()));
+
+ if (p->has_doc()) {
+ ss << ": " << p->get_doc();
+ } else {
+ ss << endl;
+ }
+ }
+ }
+
if (has_doc) {
- ss << endl;
+ generate_docstring_comment(out,
+ "",
+ "// ", ss.str(),
+ "");
}
-
- has_doc = true;
- ss << subheader << ":\n";
- vector<t_field*>::const_iterator p_iter;
-
- for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
- t_field* p = *p_iter;
- ss << " - " << publicize(variable_name_to_go_name(p->get_name()));
-
- if (p->has_doc()) {
- ss << ": " << p->get_doc();
- } else {
- ss << endl;
- }
- }
- }
-
- if (has_doc) {
- generate_docstring_comment(out, "", "// ", ss.str(), "");
- }
}
/**
* Generates the docstring for a generic object.
*/
-void t_go_generator::generate_go_docstring(ofstream& out, t_doc* tdoc) {
- if (tdoc->has_doc()) {
- generate_docstring_comment(out, "", "//", tdoc->get_doc(), "");
- }
+void t_go_generator::generate_go_docstring(ofstream& out,
+ t_doc* tdoc)
+{
+ if (tdoc->has_doc()) {
+ generate_docstring_comment(out,
+ "",
+ "//", tdoc->get_doc(),
+ "");
+ }
}
/**
@@ -2894,17 +3218,19 @@
*
* @param tfield The field
*/
-string t_go_generator::declare_argument(t_field* tfield) {
- std::ostringstream result;
- result << publicize(tfield->get_name()) << "=";
+string t_go_generator::declare_argument(t_field* tfield)
+{
+ std::ostringstream result;
+ result << publicize(tfield->get_name()) << "=";
- if (tfield->get_value() != NULL) {
- result << "thrift_spec[" << tfield->get_key() << "][4]";
- } else {
- result << "nil";
- }
+ if (tfield->get_value() != NULL) {
+ result << "thrift_spec[" <<
+ tfield->get_key() << "][4]";
+ } else {
+ result << "nil";
+ }
- return result.str();
+ return result.str();
}
/**
@@ -2914,21 +3240,22 @@
*/
string t_go_generator::render_field_initial_value(t_field* tfield,
const string& name,
- bool optional_field) {
- t_type* type = get_true_type(tfield->get_type());
+ bool optional_field)
+{
+ t_type* type = get_true_type(tfield->get_type());
- if (optional_field) {
- // The caller will make a second pass for optional fields,
- // assigning the result of render_const_value to "*field_name". It
- // is maddening that Go syntax does not allow for a type-agnostic
- // way to initialize a pointer to a const value, but so it goes.
- // The alternative would be to write type specific functions that
- // convert from const values to pointer types, but given the lack
- // of overloading it would be messy.
- return "new(" + type_to_go_type(tfield->get_type()) + ")";
- } else {
- return render_const_value(type, tfield->get_value(), name);
- }
+ if (optional_field) {
+ // The caller will make a second pass for optional fields,
+ // assigning the result of render_const_value to "*field_name". It
+ // is maddening that Go syntax does not allow for a type-agnostic
+ // way to initialize a pointer to a const value, but so it goes.
+ // The alternative would be to write type specific functions that
+ // convert from const values to pointer types, but given the lack
+ // of overloading it would be messy.
+ return "new(" + type_to_go_type(tfield->get_type()) + ")";
+ } else {
+ return render_const_value(type, tfield->get_value(), name);
+ }
}
/**
@@ -2937,10 +3264,13 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_go_generator::function_signature(t_function* tfunction, string prefix) {
- // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
- return publicize(prefix + tfunction->get_name()) + "(" + argument_list(tfunction->get_arglist())
- + ")";
+string t_go_generator::function_signature(t_function* tfunction,
+ string prefix)
+{
+ // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
+ return
+ publicize(prefix + tfunction->get_name()) +
+ "(" + argument_list(tfunction->get_arglist()) + ")";
}
/**
@@ -2949,253 +3279,270 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_go_generator::function_signature_if(t_function* tfunction, string prefix, bool addError) {
- // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
- string signature = publicize(prefix + tfunction->get_name()) + "(";
- signature += argument_list(tfunction->get_arglist()) + ") (";
- t_type* ret = tfunction->get_returntype();
- t_struct* exceptions = tfunction->get_xceptions();
- string errs = argument_list(exceptions);
+string t_go_generator::function_signature_if(t_function* tfunction,
+ string prefix,
+ bool addError)
+{
+ // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
+ string signature = publicize(prefix + tfunction->get_name()) + "(";
+ signature += argument_list(tfunction->get_arglist()) + ") (";
+ t_type* ret = tfunction->get_returntype();
+ t_struct* exceptions = tfunction->get_xceptions();
+ string errs = argument_list(exceptions);
- if (!ret->is_void()) {
- signature += "r " + type_to_go_type(ret);
+ if (!ret->is_void()) {
+ signature += "r " + type_to_go_type(ret);
- if (addError || errs.size() == 0) {
- signature += ", ";
+ if (addError || errs.size() == 0) {
+ signature += ", ";
+ }
}
- }
- if (addError) {
- signature += "err error";
- }
+ if (addError) {
+ signature += "err error";
+ }
- signature += ")";
- return signature;
+ signature += ")";
+ return signature;
}
+
/**
* Renders a field list
*/
-string t_go_generator::argument_list(t_struct* tstruct) {
- string result = "";
- const vector<t_field*>& fields = tstruct->get_members();
- vector<t_field*>::const_iterator f_iter;
- bool first = true;
+string t_go_generator::argument_list(t_struct* tstruct)
+{
+ string result = "";
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ bool first = true;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
- } else {
- result += ", ";
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ result += ", ";
+ }
+
+ result += variable_name_to_go_name((*f_iter)->get_name()) + " " + type_to_go_type((*f_iter)->get_type());
}
- result += variable_name_to_go_name((*f_iter)->get_name()) + " "
- + type_to_go_type((*f_iter)->get_type());
- }
-
- return result;
+ return result;
}
-string t_go_generator::type_name(t_type* ttype) {
- t_program* program = ttype->get_program();
+string t_go_generator::type_name(t_type* ttype)
+{
+ t_program* program = ttype->get_program();
- if (program != NULL && program != program_) {
- string module(get_real_go_module(program));
- // for namespaced includes, only keep part after dot.
- size_t dot = module.rfind('.');
- if (dot != string::npos) {
- module = module.substr(dot + 1);
+ if (program != NULL && program != program_) {
+ string module(get_real_go_module(program));
+ // for namespaced includes, only keep part after dot.
+ size_t dot = module.rfind('.');
+ if (dot != string::npos) {
+ module = module.substr(dot + 1);
+ }
+ return module + "." + ttype->get_name();
}
- return module + "." + ttype->get_name();
- }
- return ttype->get_name();
+ return ttype->get_name();
}
/**
* Converts the parse type to a go tyoe
*/
-string t_go_generator::type_to_enum(t_type* type) {
- type = get_true_type(type);
+string t_go_generator::type_to_enum(t_type* type)
+{
+ type = get_true_type(type);
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ 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 "NO T_VOID CONSTRUCT";
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- /* this is wrong, binary is still a string type internally
- if (((t_base_type*)type)->is_binary()) {
- return "thrift.BINARY";
- }
- */
- return "thrift.STRING";
+ case t_base_type::TYPE_STRING:
+ /* this is wrong, binary is still a string type internally
+ if (((t_base_type*)type)->is_binary()) {
+ return "thrift.BINARY";
+ }
+ */
+ return "thrift.STRING";
- case t_base_type::TYPE_BOOL:
- return "thrift.BOOL";
+ case t_base_type::TYPE_BOOL:
+ return "thrift.BOOL";
- case t_base_type::TYPE_BYTE:
- return "thrift.BYTE";
+ case t_base_type::TYPE_BYTE:
+ return "thrift.BYTE";
- case t_base_type::TYPE_I16:
- return "thrift.I16";
+ case t_base_type::TYPE_I16:
+ return "thrift.I16";
- case t_base_type::TYPE_I32:
- return "thrift.I32";
+ case t_base_type::TYPE_I32:
+ return "thrift.I32";
- case t_base_type::TYPE_I64:
- return "thrift.I64";
+ case t_base_type::TYPE_I64:
+ return "thrift.I64";
- case t_base_type::TYPE_DOUBLE:
- return "thrift.DOUBLE";
+ case t_base_type::TYPE_DOUBLE:
+ return "thrift.DOUBLE";
+ }
+ } else if (type->is_enum()) {
+ return "thrift.I32";
+ } else if (type->is_struct() || type->is_xception()) {
+ return "thrift.STRUCT";
+ } else if (type->is_map()) {
+ return "thrift.MAP";
+ } else if (type->is_set()) {
+ return "thrift.SET";
+ } else if (type->is_list()) {
+ return "thrift.LIST";
}
- } else if (type->is_enum()) {
- return "thrift.I32";
- } else if (type->is_struct() || type->is_xception()) {
- return "thrift.STRUCT";
- } else if (type->is_map()) {
- return "thrift.MAP";
- } else if (type->is_set()) {
- return "thrift.SET";
- } else if (type->is_list()) {
- return "thrift.LIST";
- }
- throw "INVALID TYPE IN type_to_enum: " + type->get_name();
+ throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
/**
* Converts the parse type to a go map type, will throw an exception if it will
* not produce a valid go map type.
*/
-string t_go_generator::type_to_go_key_type(t_type* type) {
- t_type* resolved_type = type;
+string t_go_generator::type_to_go_key_type(t_type* type)
+{
+ t_type* resolved_type = type;
- while (resolved_type->is_typedef()) {
- resolved_type = ((t_typedef*)resolved_type)->get_type()->get_true_type();
- }
+ while (resolved_type->is_typedef()) {
+ resolved_type = ((t_typedef*)resolved_type)->get_type()->get_true_type();
+ }
- if (resolved_type->is_map() || resolved_type->is_list() || resolved_type->is_set()) {
- throw "Cannot produce a valid type for a Go map key: " + type_to_go_type(type) + " - aborting.";
- }
+ if (resolved_type->is_map() || resolved_type->is_list() || resolved_type->is_set()) {
+ throw "Cannot produce a valid type for a Go map key: " + type_to_go_type(type) + " - aborting.";
+ }
- if (resolved_type->is_string() && ((t_base_type*)resolved_type)->is_binary())
- return "string";
+ if (resolved_type->is_string() &&
+ ((t_base_type*) resolved_type)->is_binary())
+ return "string";
- return type_to_go_type(type, true);
+ return type_to_go_type(type, true);
}
/**
* Converts the parse type to a go type
*/
string t_go_generator::type_to_go_type(t_type* type, bool is_container_value) {
- return type_to_go_type_with_opt(type, false, is_container_value);
+ return type_to_go_type_with_opt(type, false, is_container_value);
}
/**
* Converts the parse type to a go type, taking into account whether the field
* associated with the type is T_OPTIONAL.
*/
-string t_go_generator::type_to_go_type_with_opt(t_type* type,
- bool optional_field,
- bool is_container_value) {
- (void)is_container_value;
- string maybe_pointer(optional_field ? "*" : "");
- if (type->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+string t_go_generator::type_to_go_type_with_opt(t_type* type, bool optional_field, bool is_container_value)
+{
+ (void) is_container_value;
+ string maybe_pointer(optional_field ? "*" : "");
+ 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 "";
+ switch (tbase) {
+ case t_base_type::TYPE_VOID:
+ throw "";
- case t_base_type::TYPE_STRING:
- if (((t_base_type*)type)->is_binary()) {
- return maybe_pointer + "[]byte";
- }
+ case t_base_type::TYPE_STRING:
+ if (((t_base_type*)type)->is_binary()) {
+ return maybe_pointer + "[]byte";
+ }
- return maybe_pointer + "string";
+ return maybe_pointer + "string";
- case t_base_type::TYPE_BOOL:
- return maybe_pointer + "bool";
+ case t_base_type::TYPE_BOOL:
+ return maybe_pointer + "bool";
- case t_base_type::TYPE_BYTE:
- return maybe_pointer + "int8";
+ case t_base_type::TYPE_BYTE:
+ return maybe_pointer + "int8";
- case t_base_type::TYPE_I16:
- return maybe_pointer + "int16";
+ case t_base_type::TYPE_I16:
+ return maybe_pointer + "int16";
- case t_base_type::TYPE_I32:
- return maybe_pointer + "int32";
+ case t_base_type::TYPE_I32:
+ return maybe_pointer + "int32";
- case t_base_type::TYPE_I64:
- return maybe_pointer + "int64";
+ case t_base_type::TYPE_I64:
+ return maybe_pointer + "int64";
- case t_base_type::TYPE_DOUBLE:
- return maybe_pointer + "float64";
+ case t_base_type::TYPE_DOUBLE:
+ return maybe_pointer + "float64";
+ }
+ } else if (type->is_enum()) {
+ return maybe_pointer + publicize(type_name(type));
+ } else if (type->is_struct() || type->is_xception()) {
+ return "*" + publicize(type_name(type));
+ } else if (type->is_map()) {
+ t_map* t = (t_map*)type;
+ string keyType = type_to_go_key_type(t->get_key_type());
+ string valueType = type_to_go_type(t->get_val_type(), true);
+ return maybe_pointer + string("map[") + keyType + "]" + valueType;
+ } else if (type->is_set()) {
+ t_set* t = (t_set*)type;
+ string elemType = type_to_go_key_type(t->get_elem_type()->get_true_type());
+ return maybe_pointer + string("map[") + elemType + string("]bool");
+ } else if (type->is_list()) {
+ t_list* t = (t_list*)type;
+ string elemType = type_to_go_type(t->get_elem_type()->get_true_type(), true);
+ return maybe_pointer + string("[]") + elemType;
+ } else if (type->is_typedef()) {
+ return maybe_pointer + publicize(type_name(type));
}
- } else if (type->is_enum()) {
- return maybe_pointer + publicize(type_name(type));
- } else if (type->is_struct() || type->is_xception()) {
- return "*" + publicize(type_name(type));
- } else if (type->is_map()) {
- t_map* t = (t_map*)type;
- string keyType = type_to_go_key_type(t->get_key_type());
- string valueType = type_to_go_type(t->get_val_type(), true);
- return maybe_pointer + string("map[") + keyType + "]" + valueType;
- } else if (type->is_set()) {
- t_set* t = (t_set*)type;
- string elemType = type_to_go_key_type(t->get_elem_type()->get_true_type());
- return maybe_pointer + string("map[") + elemType + string("]bool");
- } else if (type->is_list()) {
- t_list* t = (t_list*)type;
- string elemType = type_to_go_type(t->get_elem_type()->get_true_type(), true);
- return maybe_pointer + string("[]") + elemType;
- } else if (type->is_typedef()) {
- return maybe_pointer + publicize(type_name(type));
- }
- throw "INVALID TYPE IN type_to_go_type: " + type->get_name();
+ throw "INVALID TYPE IN type_to_go_type: " + type->get_name();
}
/** See the comment inside generate_go_struct_definition for what this is. */
-string t_go_generator::type_to_spec_args(t_type* ttype) {
- while (ttype->is_typedef()) {
- ttype = ((t_typedef*)ttype)->get_type();
- }
+string t_go_generator::type_to_spec_args(t_type* ttype)
+{
+ while (ttype->is_typedef()) {
+ ttype = ((t_typedef*)ttype)->get_type();
+ }
- if (ttype->is_base_type() || ttype->is_enum()) {
- return "nil";
- } else if (ttype->is_struct() || ttype->is_xception()) {
- return "(" + type_name(ttype) + ", " + type_name(ttype) + ".thrift_spec)";
- } else if (ttype->is_map()) {
- return "(" + type_to_enum(((t_map*)ttype)->get_key_type()) + ","
- + type_to_spec_args(((t_map*)ttype)->get_key_type()) + ","
- + type_to_enum(((t_map*)ttype)->get_val_type()) + ","
- + type_to_spec_args(((t_map*)ttype)->get_val_type()) + ")";
- } else if (ttype->is_set()) {
- return "(" + type_to_enum(((t_set*)ttype)->get_elem_type()) + ","
- + type_to_spec_args(((t_set*)ttype)->get_elem_type()) + ")";
- } else if (ttype->is_list()) {
- return "(" + type_to_enum(((t_list*)ttype)->get_elem_type()) + ","
- + type_to_spec_args(((t_list*)ttype)->get_elem_type()) + ")";
- }
+ if (ttype->is_base_type() || ttype->is_enum()) {
+ return "nil";
+ } else if (ttype->is_struct() || ttype->is_xception()) {
+ return "(" + type_name(ttype) + ", " + type_name(ttype) + ".thrift_spec)";
+ } else if (ttype->is_map()) {
+ return "(" +
+ type_to_enum(((t_map*)ttype)->get_key_type()) + "," +
+ type_to_spec_args(((t_map*)ttype)->get_key_type()) + "," +
+ type_to_enum(((t_map*)ttype)->get_val_type()) + "," +
+ type_to_spec_args(((t_map*)ttype)->get_val_type()) +
+ ")";
+ } else if (ttype->is_set()) {
+ return "(" +
+ type_to_enum(((t_set*)ttype)->get_elem_type()) + "," +
+ type_to_spec_args(((t_set*)ttype)->get_elem_type()) +
+ ")";
+ } else if (ttype->is_list()) {
+ return "(" +
+ type_to_enum(((t_list*)ttype)->get_elem_type()) + "," +
+ type_to_spec_args(((t_list*)ttype)->get_elem_type()) +
+ ")";
+ }
- throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name();
+ throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name();
}
-bool format_go_output(const string& file_path) {
- const string command = "gofmt -w " + file_path;
+bool format_go_output(const string &file_path)
+{
+ const string command = "gofmt -w " + file_path;
- if (system(command.c_str()) == 0) {
- return true;
- }
+ if (system(command.c_str()) == 0) {
+ return true;
+ }
- fprintf(stderr, "WARNING - Running '%s' failed.\n", command.c_str());
- return false;
+ fprintf(stderr, "WARNING - Running '%s' failed.\n", command.c_str());
+ return false;
}
+
THRIFT_REGISTER_GENERATOR(go, "Go",
" package_prefix= Package prefix for generated files.\n" \
" thrift_import= Override thrift package import path (default:" + default_thrift_import + ")\n" \
diff --git a/compiler/cpp/src/generate/t_gv_generator.cc b/compiler/cpp/src/generate/t_gv_generator.cc
index 4511b29..a6ab53a 100644
--- a/compiler/cpp/src/generate/t_gv_generator.cc
+++ b/compiler/cpp/src/generate/t_gv_generator.cc
@@ -38,52 +38,54 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Graphviz code generator
*/
class t_gv_generator : public t_generator {
-public:
- t_gv_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)parsed_options;
- (void)option_string;
- out_dir_base_ = "gen-gv";
+ public:
+ t_gv_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
+ out_dir_base_ = "gen-gv";
- std::map<std::string, std::string>::const_iterator iter;
- iter = parsed_options.find("exceptions");
- exception_arrows = (iter != parsed_options.end());
- }
+ std::map<std::string, std::string>::const_iterator iter;
+ iter = parsed_options.find("exceptions");
+ exception_arrows = (iter != parsed_options.end());
+ }
- /**
- * Init and end of generator
- */
- void init_generator();
- void close_generator();
+ /**
+ * Init and end of generator
+ */
+ void init_generator();
+ void close_generator();
- /**
- * Program-level generation functions
- */
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_service(t_service* tservice);
+ /**
+ * Program-level generation functions
+ */
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_service (t_service* tservice);
-protected:
- /**
- * Helpers
- */
- void print_type(t_type* ttype, string struct_field_ref);
- void print_const_value(t_type* type, t_const_value* tvalue);
+ protected:
+ /**
+ * Helpers
+ */
+ void print_type(t_type* ttype, string struct_field_ref);
+ void print_const_value(t_type* type, t_const_value* tvalue);
-private:
- std::ofstream f_out_;
- std::list<string> edges;
- bool exception_arrows;
+ private:
+ std::ofstream f_out_;
+ std::list<string> edges;
+ bool exception_arrows;
};
/**
@@ -93,14 +95,14 @@
* - Write the file header.
*/
void t_gv_generator::init_generator() {
- escape_['{'] = "\\{";
- escape_['}'] = "\\}";
+ escape_['{'] = "\\{";
+ escape_['}'] = "\\}";
// Make output directory
MKDIR(get_out_dir().c_str());
string fname = get_out_dir() + program_->get_name() + ".gv";
f_out_.open(fname.c_str());
- f_out_ << "digraph \"" << escape_string(program_name_) << "\" {" << endl;
+ f_out_ << "digraph \"" << escape_string(program_name_) << "\" {" << endl;
f_out_ << "node [style=filled, shape=record];" << endl;
f_out_ << "edge [arrowsize=0.5];" << endl;
f_out_ << "rankdir=LR" << endl;
@@ -115,16 +117,16 @@
void t_gv_generator::close_generator() {
// Print edges
std::list<string>::iterator iter = edges.begin();
- for (; iter != edges.end(); iter++) {
+ for ( ; iter != edges.end(); iter++) {
f_out_ << (*iter) << endl;
}
// Print graph end } and close file
- f_out_ << "}" << endl;
- f_out_.close();
+f_out_ << "}" << endl;
+f_out_.close();
}
-void t_gv_generator::generate_typedef(t_typedef* ttypedef) {
+void t_gv_generator::generate_typedef (t_typedef* ttypedef) {
string name = ttypedef->get_name();
f_out_ << "node [fillcolor=azure];" << endl;
f_out_ << name << " [label=\"";
@@ -136,7 +138,7 @@
f_out_ << "\"];" << endl;
}
-void t_gv_generator::generate_enum(t_enum* tenum) {
+void t_gv_generator::generate_enum (t_enum* tenum) {
string name = tenum->get_name();
f_out_ << "node [fillcolor=white];" << endl;
f_out_ << name << " [label=\"enum " << escape_string(name);
@@ -152,7 +154,7 @@
f_out_ << "\"];" << endl;
}
-void t_gv_generator::generate_const(t_const* tconst) {
+void t_gv_generator::generate_const (t_const* tconst) {
string name = tconst->get_name();
f_out_ << "node [fillcolor=aliceblue];" << endl;
@@ -160,14 +162,14 @@
f_out_ << escape_string(name);
f_out_ << " = ";
- print_const_value(tconst->get_type(), tconst->get_value());
+ print_const_value( tconst->get_type(), tconst->get_value());
f_out_ << " :: ";
print_type(tconst->get_type(), "const_" + name);
f_out_ << "\"];" << endl;
}
-void t_gv_generator::generate_struct(t_struct* tstruct) {
+void t_gv_generator::generate_struct (t_struct* tstruct) {
string name = tstruct->get_name();
if (tstruct->is_xception()) {
@@ -186,7 +188,7 @@
vector<t_field*> members = tstruct->get_members();
vector<t_field*>::iterator mem_iter = members.begin();
- for (; mem_iter != members.end(); mem_iter++) {
+ for ( ; mem_iter != members.end(); mem_iter++) {
string field_name = (*mem_iter)->get_name();
// print port (anchor reference)
@@ -195,7 +197,8 @@
// field name :: field type
f_out_ << (*mem_iter)->get_name();
f_out_ << " :: ";
- print_type((*mem_iter)->get_type(), name + ":field_" + field_name);
+ print_type((*mem_iter)->get_type(),
+ name + ":field_" + field_name);
}
f_out_ << "\"];" << endl;
@@ -232,59 +235,61 @@
void t_gv_generator::print_const_value(t_type* type, t_const_value* tvalue) {
bool first = true;
switch (tvalue->get_type()) {
- case t_const_value::CV_INTEGER:
- f_out_ << tvalue->get_integer();
- break;
- case t_const_value::CV_DOUBLE:
- f_out_ << tvalue->get_double();
- break;
- case t_const_value::CV_STRING:
- f_out_ << "\\\"" << get_escaped_string(tvalue) << "\\\"";
- break;
- case t_const_value::CV_MAP: {
- f_out_ << "\\{ ";
- map<t_const_value*, t_const_value*> map_elems = tvalue->get_map();
- map<t_const_value*, t_const_value*>::iterator map_iter;
- for (map_iter = map_elems.begin(); map_iter != map_elems.end(); map_iter++) {
- if (!first) {
- f_out_ << ", ";
+ case t_const_value::CV_INTEGER:
+ f_out_ << tvalue->get_integer();
+ break;
+ case t_const_value::CV_DOUBLE:
+ f_out_ << tvalue->get_double();
+ break;
+ case t_const_value::CV_STRING:
+ f_out_ << "\\\"" << get_escaped_string(tvalue) << "\\\"";
+ break;
+ case t_const_value::CV_MAP:
+ {
+ f_out_ << "\\{ ";
+ map<t_const_value*, t_const_value*> map_elems = tvalue->get_map();
+ map<t_const_value*, t_const_value*>::iterator map_iter;
+ for (map_iter = map_elems.begin(); map_iter != map_elems.end(); map_iter++) {
+ if (!first) {
+ f_out_ << ", ";
+ }
+ first = false;
+ print_const_value( ((t_map*)type)->get_key_type(), map_iter->first);
+ f_out_ << " = ";
+ print_const_value( ((t_map*)type)->get_val_type(), map_iter->second);
+ }
+ f_out_ << " \\}";
}
- first = false;
- print_const_value(((t_map*)type)->get_key_type(), map_iter->first);
- f_out_ << " = ";
- print_const_value(((t_map*)type)->get_val_type(), map_iter->second);
- }
- f_out_ << " \\}";
- } break;
- case t_const_value::CV_LIST: {
- f_out_ << "\\{ ";
- vector<t_const_value*> list_elems = tvalue->get_list();
- ;
- vector<t_const_value*>::iterator list_iter;
- for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
- if (!first) {
- f_out_ << ", ";
+ break;
+ case t_const_value::CV_LIST:
+ {
+ f_out_ << "\\{ ";
+ vector<t_const_value*> list_elems = tvalue->get_list();;
+ vector<t_const_value*>::iterator list_iter;
+ for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
+ if (!first) {
+ f_out_ << ", ";
+ }
+ first = false;
+ if (type->is_list()) {
+ print_const_value( ((t_list*)type)->get_elem_type(), *list_iter);
+ } else {
+ print_const_value( ((t_set*)type)->get_elem_type(), *list_iter);
+ }
+ }
+ f_out_ << " \\}";
}
- first = false;
- if (type->is_list()) {
- print_const_value(((t_list*)type)->get_elem_type(), *list_iter);
- } else {
- print_const_value(((t_set*)type)->get_elem_type(), *list_iter);
- }
- }
- f_out_ << " \\}";
- } break;
- case t_const_value::CV_IDENTIFIER:
- f_out_ << escape_string(type->get_name()) << "."
- << escape_string(tvalue->get_identifier_name());
- break;
- default:
- f_out_ << "UNKNOWN";
- break;
+ break;
+ case t_const_value::CV_IDENTIFIER:
+ f_out_ << escape_string(type->get_name()) << "." << escape_string(tvalue->get_identifier_name());
+ break;
+ default:
+ f_out_ << "UNKNOWN";
+ break;
}
}
-void t_gv_generator::generate_service(t_service* tservice) {
+void t_gv_generator::generate_service (t_service* tservice) {
string service_name = get_service_name(tservice);
f_out_ << "subgraph cluster_" << service_name << " {" << endl;
f_out_ << "node [fillcolor=bisque];" << endl;
@@ -295,7 +300,7 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator fn_iter = functions.begin();
- for (; fn_iter != functions.end(); fn_iter++) {
+ for ( ; fn_iter != functions.end(); fn_iter++) {
string fn_name = (*fn_iter)->get_name();
f_out_ << "function_" << fn_name;
@@ -305,7 +310,7 @@
vector<t_field*> args = (*fn_iter)->get_arglist()->get_members();
vector<t_field*>::iterator arg_iter = args.begin();
- for (; arg_iter != args.end(); arg_iter++) {
+ for ( ; arg_iter != args.end(); arg_iter++) {
f_out_ << "|<param_" << (*arg_iter)->get_name() << ">";
f_out_ << (*arg_iter)->get_name();
if ((*arg_iter)->get_value() != NULL) {
@@ -314,7 +319,8 @@
}
f_out_ << " :: ";
print_type((*arg_iter)->get_type(),
- "function_" + fn_name + ":param_" + (*arg_iter)->get_name());
+ "function_" + fn_name + ":param_" + (*arg_iter)->get_name());
+
}
// end of node
f_out_ << "\"];" << endl;
@@ -323,9 +329,9 @@
if (exception_arrows) {
vector<t_field*> excepts = (*fn_iter)->get_xceptions()->get_members();
vector<t_field*>::iterator ex_iter = excepts.begin();
- for (; ex_iter != excepts.end(); ex_iter++) {
- edges.push_back("function_" + fn_name + " -> " + (*ex_iter)->get_type()->get_name()
- + " [color=red]");
+ for ( ; ex_iter != excepts.end(); ex_iter++) {
+ edges.push_back("function_" + fn_name + " -> " +
+ (*ex_iter)->get_type()->get_name() + " [color=red]");
}
}
}
@@ -333,7 +339,7 @@
f_out_ << " }" << endl;
}
-THRIFT_REGISTER_GENERATOR(
- gv,
- "Graphviz",
- " exceptions: Whether to draw arrows from functions to exception.\n")
+THRIFT_REGISTER_GENERATOR(gv, "Graphviz",
+ " exceptions: Whether to draw arrows from functions to exception.\n"
+ )
+
diff --git a/compiler/cpp/src/generate/t_haxe_generator.cc b/compiler/cpp/src/generate/t_haxe_generator.cc
index 504bea9..bd22e59 100644
--- a/compiler/cpp/src/generate/t_haxe_generator.cc
+++ b/compiler/cpp/src/generate/t_haxe_generator.cc
@@ -37,21 +37,23 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Haxe code generator.
*
*/
class t_haxe_generator : public t_oop_generator {
-public:
- t_haxe_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_haxe_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
-
+
iter = parsed_options.find("callbacks");
callbacks_ = (iter != parsed_options.end());
@@ -71,22 +73,14 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
- void print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval = false);
- std::string render_const_value(ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
+ std::string render_const_value(ofstream& out, std::string name, t_type* type, t_const_value* value);
/**
* Service-level generation functions
@@ -94,11 +88,8 @@
void generate_haxe_struct(t_struct* tstruct, bool is_exception, bool is_result = false);
- void generate_haxe_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_result = false);
- // removed -- equality,compare_to
+ void generate_haxe_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
+ //removed -- equality,compare_to
void generate_haxe_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_haxe_validator(std::ofstream& out, t_struct* tstruct);
void generate_haxe_struct_result_writer(std::ofstream& out, t_struct* tstruct);
@@ -107,14 +98,8 @@
void generate_haxe_meta_data_map(std::ofstream& out, t_struct* tstruct);
void generate_field_value_meta_data(std::ofstream& out, t_type* type);
std::string get_haxe_type_string(t_type* type);
- void generate_reflection_setters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
- void generate_reflection_getters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
+ void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
void generate_generic_isset_method(std::ofstream& out, t_struct* tstruct);
void generate_property_getters_setters(std::ofstream& out, t_struct* tstruct);
@@ -124,51 +109,73 @@
std::string generate_isset_check(t_field* field);
std::string generate_isset_check(std::string field);
void generate_isset_set(ofstream& out, t_field* field);
- // removed std::string isset_field_id(t_field* field);
-
- void generate_service_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ //removed std::string isset_field_id(t_field* field);
+
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
void generate_service_method_signature(t_function* tfunction, bool is_interface);
-
+
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter);
- void generate_haxe_doc(std::ofstream& out, t_doc* tdoc);
+ void generate_haxe_doc (std::ofstream& out,
+ t_doc* tdoc);
- void generate_haxe_doc(std::ofstream& out, t_function* tdoc);
+ void generate_haxe_doc (std::ofstream& out,
+ t_function* tdoc);
/**
* Helper rendering functions
@@ -177,11 +184,11 @@
std::string haxe_package();
std::string haxe_type_imports();
std::string haxe_thrift_imports();
- std::string haxe_thrift_gen_imports(t_struct* tstruct, string& imports);
- std::string haxe_thrift_gen_imports(t_service* tservice);
- std::string type_name(t_type* ttype, bool in_container = false, bool in_init = false);
- std::string base_type_name(t_base_type* tbase, bool in_container = false);
- std::string declare_field(t_field* tfield, bool init = false);
+ std::string haxe_thrift_gen_imports(t_struct* tstruct, string& imports);
+ std::string haxe_thrift_gen_imports(t_service* tservice);
+ std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false);
+ std::string declare_field(t_field* tfield, bool init=false);
std::string function_signature_callback(t_function* tfunction);
std::string function_signature_normal(t_function* tfunction);
std::string argument_list(t_struct* tstruct);
@@ -214,9 +221,9 @@
std::string constant_name(std::string name);
-private:
+ private:
bool callbacks_;
-
+
/**
* File streams
*/
@@ -224,8 +231,10 @@
std::string package_name_;
std::ofstream f_service_;
std::string package_dir_;
+
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -255,7 +264,7 @@
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
if (dir.size() > 0) {
subdir = subdir + "/" + dir;
@@ -283,10 +292,18 @@
* @return List of imports for haxe types that are used in here
*/
string t_haxe_generator::haxe_type_imports() {
- return string() + "import org.apache.thrift.helper.*;\n" + "import haxe.io.Bytes;\n"
- + "import haxe.ds.IntMap;\n" + "import haxe.ds.StringMap;\n"
- + "import haxe.ds.ObjectMap;\n" + "\n" + "#if flash\n"
- + "import flash.errors.ArgumentError;\n" + "#end\n" + "\n";
+ return
+ string() +
+ "import org.apache.thrift.helper.*;\n" +
+ "import haxe.io.Bytes;\n" +
+ "import haxe.ds.IntMap;\n" +
+ "import haxe.ds.StringMap;\n" +
+ "import haxe.ds.ObjectMap;\n" +
+ "\n" +
+ "#if flash\n" +
+ "import flash.errors.ArgumentError;\n" +
+ "#end\n" +
+ "\n";
}
/**
@@ -295,8 +312,12 @@
* @return List of imports necessary for thrift
*/
string t_haxe_generator::haxe_thrift_imports() {
- return string() + "import org.apache.thrift.*;\n" + "import org.apache.thrift.meta_data.*;\n"
- + "import org.apache.thrift.protocol.*;\n" + "\n";
+ return
+ string() +
+ "import org.apache.thrift.*;\n" +
+ "import org.apache.thrift.meta_data.*;\n" +
+ "import org.apache.thrift.protocol.*;\n" +
+ "\n";
}
/**
@@ -309,7 +330,7 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- // For each type check if it is from a different namespace
+ //For each type check if it is from a different namespace
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_program* program = (*m_iter)->get_type()->get_program();
if (program != NULL && program != program_) {
@@ -321,9 +342,10 @@
}
}
}
- return imports;
+ return imports;
}
+
/**
* Prints imports needed for a given type
*
@@ -334,31 +356,31 @@
const vector<t_function*>& functions = tservice->get_functions();
vector<t_function*>::const_iterator f_iter;
- // For each type check if it is from a different namespace
+ //For each type check if it is from a different namespace
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_program* program = (*f_iter)->get_returntype()->get_program();
if (program != NULL && program != program_) {
string package = program->get_namespace("haxe");
if (!package.empty()) {
if (imports.find(package + "." + (*f_iter)->get_returntype()->get_name()) == string::npos) {
- imports.append("import " + package + "." + (*f_iter)->get_returntype()->get_name()
- + ";\n");
+ imports.append("import " + package + "." + (*f_iter)->get_returntype()->get_name() + ";\n");
}
}
}
- haxe_thrift_gen_imports((*f_iter)->get_arglist(), imports);
- haxe_thrift_gen_imports((*f_iter)->get_xceptions(), imports);
- }
+ haxe_thrift_gen_imports((*f_iter)->get_arglist(), imports);
+ haxe_thrift_gen_imports((*f_iter)->get_xceptions(), imports);
+ }
+
return imports;
+
}
/**
* Nothing in haxe
*/
-void t_haxe_generator::close_generator() {
-}
+void t_haxe_generator::close_generator() {}
/**
* Generates a typedef. This is not done in haxe, since it does
@@ -368,7 +390,7 @@
* @param ttypedef The type definition
*/
void t_haxe_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -383,25 +405,31 @@
f_enum.open(f_enum_name.c_str());
// Comment and package it
- f_enum << autogen_comment() << haxe_package() << ";" << endl << endl;
-
+ f_enum <<
+ autogen_comment() <<
+ haxe_package() << ";" << endl << endl;
+
// Add haxe imports
- f_enum << string() + "import org.apache.thrift.helper.*;" << endl << endl;
-
- indent(f_enum) << "class " << get_cap_name(tenum->get_name()) << " ";
+ f_enum << string() +
+ "import org.apache.thrift.helper.*;" << endl <<
+ endl;
+
+ indent(f_enum) <<
+ "class " << get_cap_name(tenum->get_name()) << " ";
scope_up(f_enum);
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
- indent(f_enum) << "public static inline var " << (*c_iter)->get_name() << " : Int = " << value
- << ";" << endl;
+ indent(f_enum) <<
+ "public static inline var " << (*c_iter)->get_name() <<
+ " : Int = " << value << ";" << endl;
}
-
+
// Create a static Set with all valid values for this enum
f_enum << endl;
-
+
indent(f_enum) << "public static var VALID_VALUES = { new IntSet( [";
indent_up();
bool firstValue = true;
@@ -426,7 +454,7 @@
indent(f_enum) << "]; };" << endl;
scope_down(f_enum); // end class
-
+
f_enum.close();
}
@@ -443,13 +471,18 @@
f_consts.open(f_consts_name.c_str());
// Print header
- f_consts << autogen_comment() << haxe_package() << ";" << endl << endl;
-
+ f_consts <<
+ autogen_comment() << haxe_package() << ";" << endl << endl;
+
f_consts << endl;
-
+
f_consts << haxe_type_imports();
- indent(f_consts) << "class " << get_cap_name(program_name_) << "Constants {" << endl << endl;
+
+
+ indent(f_consts) <<
+ "class " << get_cap_name(program_name_) <<
+ "Constants {" << endl << endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
@@ -460,21 +493,18 @@
false);
}
indent_down();
- indent(f_consts) << "}" << endl;
+ indent(f_consts) <<
+ "}" << endl;
f_consts.close();
}
-void t_haxe_generator::print_const_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval) {
+void t_haxe_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
type = get_true_type(type);
-
+
indent(out);
if (!defval) {
- out << (in_static ? "var " : "public static inline var ");
+ out <<
+ (in_static ? "var " : "public static inline var ");
}
if (type->is_base_type()) {
string v2 = render_const_value(out, name, type, value);
@@ -485,7 +515,7 @@
out << " = " << v2 << ";" << endl << endl;
} else if (type->is_enum()) {
out << name;
- if (!defval) {
+ if(!defval) {
out << ":" << type_name(type);
}
out << " = " << value->get_integer() << ";" << endl << endl;
@@ -494,8 +524,7 @@
vector<t_field*>::const_iterator f_iter;
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
- out << name << ":" << type_name(type) << " = new " << type_name(type, false, true) << "();"
- << endl;
+ out << name << ":" << type_name(type) << " = new " << type_name(type, false, true) << "();" << endl;
if (!in_static) {
indent(out) << "{" << endl;
indent_up();
@@ -525,7 +554,7 @@
out << endl;
} else if (type->is_map()) {
out << name;
- if (!defval) {
+ if(!defval){
out << ":" << type_name(type);
}
out << " = new " << type_name(type, false, true) << "();" << endl;
@@ -553,7 +582,7 @@
out << endl;
} else if (type->is_list() || type->is_set()) {
out << name;
- if (!defval) {
+ if(!defval) {
out << ":" << type_name(type);
}
out << " = new " << type_name(type, false, true) << "();" << endl;
@@ -573,8 +602,7 @@
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string val = render_const_value(out, name, etype, *v_iter);
- indent(out) << name << "." << (type->is_list() ? "push" : "add") << "(" << val << ");"
- << endl;
+ indent(out) << name << "." << (type->is_list() ? "push" : "add") << "(" << val << ");" << endl;
}
if (!in_static) {
indent_down();
@@ -588,44 +616,41 @@
}
}
-string t_haxe_generator::render_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+string t_haxe_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
type = get_true_type(type);
std::ostringstream render;
-
+
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << '"' << get_escaped_string(value) << '"';
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() > 0) ? "true" : "false");
- break;
- case t_base_type::TYPE_BYTE:
- render << "(byte)" << value->get_integer();
- break;
- case t_base_type::TYPE_I16:
- render << "(short)" << value->get_integer();
- break;
- case t_base_type::TYPE_I32:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_I64:
- render << value->get_integer() << "L";
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- render << "(double)" << value->get_integer();
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_STRING:
+ render << '"' << get_escaped_string(value) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ render << "(byte)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I16:
+ render << "(short)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I32:
+ render << value->get_integer();
+ break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "L";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << "(double)" << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
render << value->get_integer();
@@ -634,10 +659,11 @@
print_const_value(out, t, type, value, true);
render << t;
}
-
+
return render.str();
}
+
/**
* Generates a struct definition for a thrift data type. This is a class
* with data members, read(), write(), and an inner Isset class.
@@ -657,28 +683,35 @@
generate_haxe_struct(txception, true);
}
+
/**
* Haxe struct definition.
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_struct(t_struct* tstruct, bool is_exception, bool is_result) {
+void t_haxe_generator::generate_haxe_struct(t_struct* tstruct,
+ bool is_exception,
+ bool is_result) {
// Make output file
string f_struct_name = package_dir_ + "/" + get_cap_name(tstruct->get_name()) + ".hx";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << haxe_package() << ";" << endl;
-
+ f_struct <<
+ autogen_comment() <<
+ haxe_package() << ";" << endl;
+
f_struct << endl;
-
+
string imports;
- f_struct << haxe_type_imports() << haxe_thrift_imports()
- << haxe_thrift_gen_imports(tstruct, imports) << endl;
-
+ f_struct <<
+ haxe_type_imports() <<
+ haxe_thrift_imports() <<
+ haxe_thrift_gen_imports(tstruct, imports) << endl;
+
generate_haxe_struct_definition(f_struct, tstruct, is_exception, is_result);
-
+
f_struct.close();
}
@@ -692,15 +725,16 @@
* @param in_class If inside a class, needs to be static class
* @param is_result If this is a result it needs a different writer
*/
-void t_haxe_generator::generate_haxe_struct_definition(ofstream& out,
+void t_haxe_generator::generate_haxe_struct_definition(ofstream &out,
t_struct* tstruct,
bool is_exception,
bool is_result) {
generate_haxe_doc(out, tstruct);
- string clsname = get_cap_name(tstruct->get_name());
-
- indent(out) << "class " << clsname << " ";
+ string clsname = get_cap_name( tstruct->get_name());
+
+ indent(out) <<
+ "class " << clsname << " ";
if (is_exception) {
out << "extends TException ";
@@ -710,79 +744,78 @@
scope_up(out);
indent(out) << endl;
- indent(out) << "static var STRUCT_DESC = { new TStruct(\"" << tstruct->get_name() << "\"); };"
- << endl;
+ indent(out) <<
+ "static var STRUCT_DESC = { new TStruct(\"" << tstruct->get_name() << "\"); };" << endl;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "static var " << constant_name((*m_iter)->get_name())
- << "_FIELD_DESC = { new TField(\"" << (*m_iter)->get_name() << "\", "
- << type_to_enum((*m_iter)->get_type()) << ", " << (*m_iter)->get_key() << "); };"
- << endl;
+ indent(out) <<
+ "static var " << constant_name((*m_iter)->get_name()) <<
+ "_FIELD_DESC = { new TField(\"" << (*m_iter)->get_name() << "\", " <<
+ type_to_enum((*m_iter)->get_type()) << ", " <<
+ (*m_iter)->get_key() << "); };" << endl;
}
out << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
generate_haxe_doc(out, *m_iter);
- // indent(out) << "private var _" << (*m_iter)->get_name() + " : " +
- // type_name((*m_iter)->get_type()) << ";" << endl;
+ //indent(out) << "private var _" << (*m_iter)->get_name() + " : " + type_name((*m_iter)->get_type()) << ";" << endl;
indent(out) << "@:isVar" << endl;
- indent(out) << "public var "
- << (*m_iter)->get_name() + "(get,set) : "
- + get_cap_name(type_name((*m_iter)->get_type())) << ";" << endl;
+ indent(out) << "public var " << (*m_iter)->get_name() + "(get,set) : " + get_cap_name(type_name((*m_iter)->get_type())) << ";" << endl;
}
-
+
out << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "inline static var " << upcase_string((*m_iter)->get_name())
- << "_FIELD_ID : Int = " << (*m_iter)->get_key() << ";" << endl;
+ indent(out) << "inline static var " << upcase_string((*m_iter)->get_name()) << "_FIELD_ID : Int = " << (*m_iter)->get_key() << ";" << endl;
}
-
+
out << endl;
-
+
// Inner Isset class
if (members.size() > 0) {
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- if (!type_can_be_null((*m_iter)->get_type())) {
- indent(out) << "private var __isset_" << (*m_iter)->get_name() << " : Bool = false;"
- << endl;
+ if (!type_can_be_null((*m_iter)->get_type())){
+ indent(out) <<
+ "private var __isset_" << (*m_iter)->get_name() << " : Bool = false;" << endl;
+ }
}
- }
}
-
+
out << endl;
-
+
+
// Static initializer to populate global class to struct metadata map
- if (false) {
+ if( false) {
// TODO: reactivate when needed
generate_haxe_meta_data_map(out, tstruct);
indent(out) << "{" << endl;
indent_up();
- indent(out) << "FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ", metaDataMap);"
- << endl;
+ indent(out) << "FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ", metaDataMap);" << endl;
indent_down();
indent(out) << "}" << endl;
indent(out) << "}" << endl;
}
-
+
// Default constructor
- indent(out) << "public function new() {" << endl;
+ indent(out) <<
+ "public function new() {" << endl;
indent_up();
- if (is_exception) {
- indent(out) << "super();" << endl;
+ if( is_exception) {
+ indent(out) <<
+ "super();" << endl;
}
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_value() != NULL) {
- indent(out) << "this." << (*m_iter)->get_name() << " = "
- << (*m_iter)->get_value()->get_integer() << ";" << endl;
+ indent(out) << "this." << (*m_iter)->get_name() << " = " << (*m_iter)->get_value()->get_integer() << ";" <<
+ endl;
}
}
indent_down();
indent(out) << "}" << endl << endl;
-
+
generate_property_getters_setters(out, tstruct);
generate_generic_field_getters_setters(out, tstruct);
generate_generic_isset_method(out, tstruct);
@@ -804,129 +837,139 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_struct_reader(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function read( iprot : TProtocol) : Void {" << endl;
+void t_haxe_generator::generate_haxe_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function read( iprot : TProtocol) : Void {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// Declare stack tmp variables and read struct header
- out << indent() << "var field : TField;" << endl << indent() << "iprot.readStructBegin();"
- << endl;
+ out <<
+ indent() << "var field : TField;" << endl <<
+ indent() << "iprot.readStructBegin();" << endl;
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
- scope_up(out);
+ indent(out) <<
+ "while (true)" << endl;
+ scope_up(out);
- // Read beginning field marker
- indent(out) << "field = iprot.readFieldBegin();" << endl;
+ // Read beginning field marker
+ indent(out) <<
+ "field = iprot.readFieldBegin();" << endl;
- // Check for field STOP marker and break
- indent(out) << "if (field.type == TType.STOP) { " << endl;
- indent_up();
- indent(out) << "break;" << endl;
- indent_down();
- indent(out) << "}" << endl;
-
- // Switch statement on the field we are reading
- indent(out) << "switch (field.id)" << endl;
-
- scope_up(out);
-
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << upcase_string((*f_iter)->get_name()) << "_FIELD_ID:" << endl;
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (field.type == TType.STOP) { " << endl;
indent_up();
- indent(out) << "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent_up();
-
- generate_deserialize_field(out, *f_iter, "this.");
- generate_isset_set(out, *f_iter);
+ indent(out) <<
+ "break;" << endl;
indent_down();
- out << indent() << "} else { " << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
- << endl << indent() << "}" << endl;
- indent_down();
- }
+ indent(out) <<
+ "}" << endl;
- // In the default case we skip the field
- out << indent() << "default:" << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
- << endl;
+ // Switch statement on the field we are reading
+ indent(out) <<
+ "switch (field.id)" << endl;
- scope_down(out);
+ scope_up(out);
- // Read field end marker
- indent(out) << "iprot.readFieldEnd();" << endl;
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << upcase_string((*f_iter)->get_name()) << "_FIELD_ID:" << endl;
+ indent_up();
+ indent(out) <<
+ "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
- scope_down(out);
+ generate_deserialize_field(out, *f_iter, "this.");
+ generate_isset_set(out, *f_iter);
+ indent_down();
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
+ indent() << "}" << endl;
+ indent_down();
+ }
- out << indent() << "iprot.readStructEnd();" << endl << endl;
+ // In the default case we skip the field
+ out <<
+ indent() << "default:" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl;
- // check for required fields of primitive type
- // (which can be checked here but not in the general validate method)
- out << endl << indent() << "// check for required fields of primitive type, which can't be "
- "checked in the validate method" << endl;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
- out << indent() << "if (!__isset_" << (*f_iter)->get_name() << ") {" << endl << indent()
- << " throw new TProtocolException(TProtocolException.UNKNOWN, \"Required field '"
- << (*f_iter)->get_name()
- << "' was not found in serialized data! Struct: \" + toString());" << endl << indent()
- << "}" << endl;
+ scope_down(out);
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd();" << endl;
+
+ scope_down(out);
+
+ out <<
+ indent() << "iprot.readStructEnd();" << endl << endl;
+
+ // check for required fields of primitive type
+ // (which can be checked here but not in the general validate method)
+ out << endl << indent() << "// check for required fields of primitive type, which can't be checked in the validate method" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
+ out <<
+ indent() << "if (!__isset_" << (*f_iter)->get_name() << ") {" << endl <<
+ indent() << " throw new TProtocolException(TProtocolException.UNKNOWN, \"Required field '" << (*f_iter)->get_name() << "' was not found in serialized data! Struct: \" + toString());" << endl <<
+ indent() << "}" << endl;
+ }
}
- }
- // performs various checks (e.g. check that all required fields are set)
- indent(out) << "validate();" << endl;
+ // performs various checks (e.g. check that all required fields are set)
+ indent(out) << "validate();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
// generates haxe method to perform various checks
// (e.g. check that all required fields are set)
-void t_haxe_generator::generate_haxe_validator(ofstream& out, t_struct* tstruct) {
+void t_haxe_generator::generate_haxe_validator(ofstream& out,
+ t_struct* tstruct){
indent(out) << "public function validate() : Void {" << endl;
- indent_up();
-
+ indent_up();
+
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
-
+
out << indent() << "// check for required fields" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
if (type_can_be_null((*f_iter)->get_type())) {
indent(out) << "if (" << (*f_iter)->get_name() << " == null) {" << endl;
- indent(out)
- << " throw new TProtocolException(TProtocolException.UNKNOWN, \"Required field '"
- << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl;
+ indent(out) << " throw new TProtocolException(TProtocolException.UNKNOWN, \"Required field '" << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl;
indent(out) << "}" << endl;
} else {
- indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name()
- << "' because it's a primitive." << endl;
+ indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name() << "' because it's a primitive." << endl;
}
}
}
-
+
// check that fields of type enum have valid values
out << indent() << "// check that fields of type enum have valid values" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = (*f_iter);
t_type* type = field->get_type();
// if field is an enum, check that its value is valid
- if (type->is_enum()) {
- indent(out) << "if (" << generate_isset_check(field) << " && !"
- << get_cap_name(get_enum_class_name(type)) << ".VALID_VALUES.contains("
- << field->get_name() << ")){" << endl;
+ if (type->is_enum()){
+ indent(out) << "if (" << generate_isset_check(field) << " && !" << get_cap_name(get_enum_class_name(type)) << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
indent_up();
- indent(out) << "throw new TProtocolException(TProtocolException.UNKNOWN, \"The field '"
- << field->get_name() << "' has been assigned the invalid value \" + "
- << field->get_name() << ");" << endl;
+ indent(out) << "throw new TProtocolException(TProtocolException.UNKNOWN, \"The field '" << field->get_name() << "' has been assigned the invalid value \" + " << field->get_name() << ");" << endl;
indent_down();
indent(out) << "}" << endl;
- }
- }
-
+ }
+ }
+
indent_down();
indent(out) << "}" << endl << endl;
}
@@ -936,8 +979,10 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_struct_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function write(oprot:TProtocol) : Void {" << endl;
+void t_haxe_generator::generate_haxe_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function write(oprot:TProtocol) : Void {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -952,18 +997,19 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
+ out <<
+ indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
indent_up();
}
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
if (null_allowed) {
indent_down();
@@ -971,11 +1017,14 @@
}
}
// Write the struct map
- out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
- << endl;
+ out <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -986,8 +1035,10 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_struct_result_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public function write(oprot:TProtocol) : Void {" << endl;
+void t_haxe_generator::generate_haxe_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public function write(oprot:TProtocol) : Void {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -995,12 +1046,14 @@
vector<t_field*>::const_iterator f_iter;
indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
-
+
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
- out << endl << indent() << "if ";
+ out <<
+ endl <<
+ indent() << "if ";
} else {
out << " else if ";
}
@@ -1008,45 +1061,43 @@
out << "(this." << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
-
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
indent_down();
indent(out) << "}";
}
// Write the struct map
- out << endl << indent() << "oprot.writeFieldStop();" << endl << indent()
- << "oprot.writeStructEnd();" << endl;
+ out <<
+ endl <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
-void t_haxe_generator::generate_reflection_getters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
- (void)type;
- (void)cap_name;
+void t_haxe_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ (void) type;
+ (void) cap_name;
indent(out) << "case " << upcase_string(field_name) << "_FIELD_ID:" << endl;
indent_up();
indent(out) << "return this." << field_name << ";" << endl;
indent_down();
}
-void t_haxe_generator::generate_reflection_setters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
- (void)type;
- (void)cap_name;
+void t_haxe_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) {
+ (void) type;
+ (void) cap_name;
indent(out) << "case " << upcase_string(field_name) << "_FIELD_ID:" << endl;
indent_up();
indent(out) << "if (value == null) {" << endl;
@@ -1058,8 +1109,7 @@
indent_down();
}
-void t_haxe_generator::generate_generic_field_getters_setters(std::ofstream& out,
- t_struct* tstruct) {
+void t_haxe_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) {
std::ostringstream getter_stream;
std::ostringstream setter_stream;
@@ -1079,6 +1129,7 @@
indent_down();
}
+
// create the setter
indent(out) << "public function setFieldValue(fieldID : Int, value : Dynamic) : Void {" << endl;
indent_up();
@@ -1092,7 +1143,7 @@
} else {
indent(out) << "throw new ArgumentError(\"Field \" + fieldID + \" doesn't exist!\");" << endl;
}
-
+
indent_down();
indent(out) << "}" << endl << endl;
@@ -1116,13 +1167,12 @@
}
// Creates a generic isSet method that takes the field number as argument
-void t_haxe_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct) {
+void t_haxe_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct){
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// create the isSet method
- indent(out) << "// Returns true if field corresponding to fieldID is set (has been assigned a "
- "value) and false otherwise" << endl;
+ indent(out) << "// Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise" << endl;
indent(out) << "public function isSet(fieldID : Int) : Bool {" << endl;
indent_up();
if (fields.size() > 0) {
@@ -1152,7 +1202,8 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_property_getters_setters(ofstream& out, t_struct* tstruct) {
+void t_haxe_generator::generate_property_getters_setters(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1160,29 +1211,32 @@
t_type* type = get_true_type(field->get_type());
std::string field_name = field->get_name();
std::string cap_name = get_cap_name(field_name);
-
+
// Simple getter
generate_haxe_doc(out, field);
- indent(out) << "public function get_" << field_name << "() : " << get_cap_name(type_name(type))
- << " {" << endl;
+ indent(out) << "public function get_" << field_name << "() : " <<
+ get_cap_name(type_name(type)) << " {" << endl;
indent_up();
indent(out) << "return this." << field_name << ";" << endl;
indent_down();
indent(out) << "}" << endl << endl;
-
+
// Simple setter
generate_haxe_doc(out, field);
- indent(out) << "public function set_" << field_name << "(" << field_name << ":"
- << get_cap_name(type_name(type)) << ") : " << get_cap_name(type_name(type)) << " {"
- << endl;
+ indent(out) <<
+ "public function set_" << field_name <<
+ "(" << field_name << ":" << get_cap_name(type_name(type)) << ") : " <<
+ get_cap_name(type_name(type)) << " {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = " << field_name << ";" << endl;
+ indent(out) <<
+ "this." << field_name << " = " << field_name << ";" << endl;
generate_isset_set(out, field);
- indent(out) << "return this." << field_name << ";" << endl;
-
+ indent(out) <<
+ "return this." << field_name << ";" << endl;
+
indent_down();
indent(out) << "}" << endl << endl;
-
+
// Unsetter
indent(out) << "public function unset" << cap_name << "() : Void {" << endl;
indent_up();
@@ -1193,10 +1247,9 @@
}
indent_down();
indent(out) << "}" << endl << endl;
-
+
// isSet method
- indent(out) << "// Returns true if field " << field_name
- << " is set (has been assigned a value) and false otherwise" << endl;
+ indent(out) << "// Returns true if field " << field_name << " is set (has been assigned a value) and false otherwise" << endl;
indent(out) << "public function is" << get_cap_name("set") << cap_name << "() : Bool {" << endl;
indent_up();
if (type_can_be_null(type)) {
@@ -1214,12 +1267,13 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_struct_tostring(ofstream& out, t_struct* tstruct) {
- out << indent() << "public "
- << "function toString() : String {" << endl;
+void t_haxe_generator::generate_haxe_struct_tostring(ofstream& out,
+ t_struct* tstruct) {
+ out << indent() << "public " << "function toString() : String {" << endl;
indent_up();
- out << indent() << "var ret : String = \"" << tstruct->get_name() << "(\";" << endl;
+ out <<
+ indent() << "var ret : String = \"" << tstruct->get_name() << "(\";" << endl;
out << indent() << "var first : Bool = true;" << endl << endl;
const vector<t_field*>& fields = tstruct->get_members();
@@ -1227,7 +1281,7 @@
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
- if (could_be_unset) {
+ if(could_be_unset) {
indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
}
@@ -1245,13 +1299,11 @@
indent(out) << "} else {" << endl;
indent_up();
}
-
+
if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
indent(out) << " ret += \"BINARY\";" << endl;
- } else if (field->get_type()->is_enum()) {
- indent(out) << "var " << field->get_name()
- << "_name : String = " << get_cap_name(get_enum_class_name(field->get_type()))
- << ".VALUES_TO_NAMES[this." << (*f_iter)->get_name() << "];" << endl;
+ } else if(field->get_type()->is_enum()) {
+ indent(out) << "var " << field->get_name() << "_name : String = " << get_cap_name(get_enum_class_name(field->get_type())) << ".VALUES_TO_NAMES[this." << (*f_iter)->get_name() << "];"<< endl;
indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
indent(out) << " ret += " << field->get_name() << "_name;" << endl;
indent(out) << " ret += \" (\";" << endl;
@@ -1263,23 +1315,26 @@
} else {
indent(out) << "ret += this." << (*f_iter)->get_name() << ";" << endl;
}
-
+
if (can_be_null) {
indent_down();
indent(out) << "}" << endl;
}
indent(out) << "first = false;" << endl;
- if (could_be_unset) {
+ if(could_be_unset) {
indent_down();
indent(out) << "}" << endl;
}
first = false;
}
- out << indent() << "ret += \")\";" << endl << indent() << "return ret;" << endl;
+ out <<
+ indent() << "ret += \")\";" << endl <<
+ indent() << "return ret;" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
/**
@@ -1288,7 +1343,8 @@
*
* @param tstruct The struct definition
*/
-void t_haxe_generator::generate_haxe_meta_data_map(ofstream& out, t_struct* tstruct) {
+void t_haxe_generator::generate_haxe_meta_data_map(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -1301,8 +1357,7 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = *f_iter;
std::string field_name = field->get_name();
- indent(out) << "metaDataMap[" << upcase_string(field_name)
- << "_FIELD_ID] = new FieldMetaData(\"" << field_name << "\", ";
+ indent(out) << "metaDataMap[" << upcase_string(field_name) << "_FIELD_ID] = new FieldMetaData(\"" << field_name << "\", ";
// Set field requirement type (required, optional, etc.)
if (field->get_req() == t_field::T_REQUIRED) {
@@ -1313,20 +1368,20 @@
out << "TFieldRequirementType.DEFAULT, ";
}
- // Create value meta data
+ // Create value meta data
generate_field_value_meta_data(out, field->get_type());
- out << ");" << endl;
+ out << ");" << endl;
}
scope_down(out);
}
}
-/**
+/**
* Returns a string with the haxe representation of the given thrift type
* (e.g. for the type struct it returns "TType.STRUCT")
*/
std::string t_haxe_generator::get_haxe_type_string(t_type* type) {
- if (type->is_list()) {
+ if (type->is_list()){
return "TType.LIST";
} else if (type->is_map()) {
return "TType.MAP";
@@ -1340,58 +1395,37 @@
return get_haxe_type_string(((t_typedef*)type)->get_type());
} else if (type->is_base_type()) {
switch (((t_base_type*)type)->get_base()) {
- case t_base_type::TYPE_VOID:
- return "TType.VOID";
- break;
- case t_base_type::TYPE_STRING:
- return "TType.STRING";
- break;
- case t_base_type::TYPE_BOOL:
- return "TType.BOOL";
- break;
- case t_base_type::TYPE_BYTE:
- return "TType.BYTE";
- break;
- case t_base_type::TYPE_I16:
- return "TType.I16";
- break;
- case t_base_type::TYPE_I32:
- return "TType.I32";
- break;
- case t_base_type::TYPE_I64:
- return "TType.I64";
- break;
- case t_base_type::TYPE_DOUBLE:
- return "TType.DOUBLE";
- break;
- default:
- throw std::runtime_error("Unknown thrift type \"" + type->get_name()
- + "\" passed to t_haxe_generator::get_haxe_type_string!");
- break; // This should never happen!
+ case t_base_type::TYPE_VOID : return "TType.VOID"; break;
+ case t_base_type::TYPE_STRING : return "TType.STRING"; break;
+ case t_base_type::TYPE_BOOL : return "TType.BOOL"; break;
+ case t_base_type::TYPE_BYTE : return "TType.BYTE"; break;
+ case t_base_type::TYPE_I16 : return "TType.I16"; break;
+ case t_base_type::TYPE_I32 : return "TType.I32"; break;
+ case t_base_type::TYPE_I64 : return "TType.I64"; break;
+ case t_base_type::TYPE_DOUBLE : return "TType.DOUBLE"; break;
+ default : throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_haxe_generator::get_haxe_type_string!"); break; // This should never happen!
}
} else {
- throw std::runtime_error(
- "Unknown thrift type \"" + type->get_name()
- + "\" passed to t_haxe_generator::get_haxe_type_string!"); // This should never happen!
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_haxe_generator::get_haxe_type_string!"); // This should never happen!
}
}
-void t_haxe_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type) {
+void t_haxe_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){
out << endl;
indent_up();
indent_up();
- if (type->is_struct()) {
+ if (type->is_struct()){
indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type);
- } else if (type->is_container()) {
- if (type->is_list()) {
+ } else if (type->is_container()){
+ if (type->is_list()){
indent(out) << "new ListMetaData(TType.LIST, ";
- t_type* elem_type = ((t_list*)type)->get_elem_type();
- generate_field_value_meta_data(out, elem_type);
- } else if (type->is_set()) {
+ t_type* elem_type = ((t_list*)type)->get_elem_type();
+ generate_field_value_meta_data(out, elem_type);
+ } else if (type->is_set()){
indent(out) << "new SetMetaData(TType.SET, ";
- t_type* elem_type = ((t_list*)type)->get_elem_type();
- generate_field_value_meta_data(out, elem_type);
- } else { // map
+ t_type* elem_type = ((t_list*)type)->get_elem_type();
+ generate_field_value_meta_data(out, elem_type);
+ } else{ // map
indent(out) << "new MapMetaData(TType.MAP, ";
t_type* key_type = ((t_map*)type)->get_key_type();
t_type* val_type = ((t_map*)type)->get_val_type();
@@ -1407,6 +1441,7 @@
indent_down();
}
+
/**
* Generates a thrift service. In C++, this comprises an entirely separate
* header and source file. The header file defines the methods and includes
@@ -1420,15 +1455,18 @@
string f_service_name = package_dir_ + "/" + get_cap_name(service_name_) + ".hx";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << haxe_package() << ";" << endl;
+ f_service_ <<
+ autogen_comment() << haxe_package() << ";" << endl;
+
+ f_service_ << endl <<
+ haxe_type_imports() <<
+ haxe_thrift_imports() <<
+ haxe_thrift_gen_imports(tservice);
- f_service_ << endl << haxe_type_imports() << haxe_thrift_imports()
- << haxe_thrift_gen_imports(tservice);
-
- if (tservice->get_extends() != NULL) {
+ if(tservice->get_extends() != NULL) {
t_type* parent = tservice->get_extends();
string parent_namespace = parent->get_program()->get_namespace("haxe");
- if (!parent_namespace.empty() && parent_namespace != package_name_) {
+ if(!parent_namespace.empty() && parent_namespace != package_name_) {
f_service_ << "import " << type_name(parent) << ";" << endl;
}
}
@@ -1438,18 +1476,23 @@
generate_service_interface(tservice);
f_service_.close();
-
+
// Now make the implementation/client file
- f_service_name = package_dir_ + "/" + get_cap_name(service_name_) + "Impl.hx";
+ f_service_name = package_dir_+"/"+get_cap_name(service_name_)+"Impl.hx";
f_service_.open(f_service_name.c_str());
+
+ f_service_ <<
+ autogen_comment() << haxe_package() << ";" << endl <<
+ endl <<
+ haxe_type_imports() <<
+ haxe_thrift_imports() <<
+ haxe_thrift_gen_imports(tservice) << endl;
- f_service_ << autogen_comment() << haxe_package() << ";" << endl << endl << haxe_type_imports()
- << haxe_thrift_imports() << haxe_thrift_gen_imports(tservice) << endl;
- if (tservice->get_extends() != NULL) {
+ if(tservice->get_extends() != NULL) {
t_type* parent = tservice->get_extends();
string parent_namespace = parent->get_program()->get_namespace("haxe");
- if (!parent_namespace.empty() && parent_namespace != package_name_) {
+ if(!parent_namespace.empty() && parent_namespace != package_name_) {
f_service_ << "import " << type_name(parent) << "Impl;" << endl;
}
}
@@ -1464,22 +1507,26 @@
generate_service_helpers(tservice);
// Now make the processor/server file
- f_service_name = package_dir_ + "/" + get_cap_name(service_name_) + "Processor.hx";
+ f_service_name = package_dir_+"/"+get_cap_name(service_name_)+"Processor.hx";
f_service_.open(f_service_name.c_str());
-
- f_service_ << autogen_comment() << haxe_package() << ";" << endl << endl << haxe_type_imports()
- << haxe_thrift_imports() << haxe_thrift_gen_imports(tservice) << endl;
-
- if (!package_name_.empty()) {
+
+ f_service_ <<
+ autogen_comment() << haxe_package() << ";" << endl <<
+ endl <<
+ haxe_type_imports() <<
+ haxe_thrift_imports() <<
+ haxe_thrift_gen_imports(tservice) << endl;
+
+ if(!package_name_.empty()) {
f_service_ << "import " << package_name_ << ".*;" << endl;
- f_service_ << "import " << package_name_ << "." << get_cap_name(service_name_).c_str()
- << "Impl;" << endl;
+ f_service_ << "import " << package_name_ << "." << get_cap_name(service_name_).c_str() << "Impl;" << endl;
f_service_ << endl;
}
-
+
generate_service_server(tservice);
-
+
f_service_.close();
+
}
/**
@@ -1487,33 +1534,31 @@
*
* @param tfunction The service function to generate code for.
*/
-string t_haxe_generator::generate_service_method_onsuccess(t_function* tfunction,
- bool as_type,
- bool omit_name) {
- if (tfunction->is_oneway()) {
+string t_haxe_generator::generate_service_method_onsuccess(t_function* tfunction, bool as_type, bool omit_name) {
+ if( tfunction->is_oneway()) {
return "";
}
-
+
string name = "";
- if (!omit_name) {
+ if( ! omit_name) {
name = "onSuccess";
- if (as_type) {
+ if( as_type) {
name += " : ";
}
}
-
- if (tfunction->get_returntype()->is_void()) {
- if (as_type) {
- return name + "Void->Void = null";
+
+ if( tfunction->get_returntype()->is_void()) {
+ if( as_type) {
+ return name + "Void->Void = null";
} else {
- return name + "() : Void";
+ return name + "() : Void";
}
}
-
- if (as_type) {
+
+ if( as_type) {
return name + type_name(tfunction->get_returntype()) + "->Void = null";
} else {
- return name + "( retval : " + type_name(tfunction->get_returntype()) + ")";
+ return name + "( retval : " + type_name(tfunction->get_returntype()) + ")";
}
}
@@ -1523,11 +1568,11 @@
* @param tfunction The service function to generate code for.
*/
void t_haxe_generator::generate_service_method_signature(t_function* tfunction, bool is_interface) {
- if (callbacks_) {
- generate_service_method_signature_callback(tfunction, is_interface);
+ if( callbacks_) {
+ generate_service_method_signature_callback( tfunction, is_interface);
} else {
- generate_service_method_signature_normal(tfunction, is_interface);
- }
+ generate_service_method_signature_normal( tfunction, is_interface);
+ }
}
/**
@@ -1535,12 +1580,12 @@
*
* @param tfunction The service function to generate code for.
*/
-void t_haxe_generator::generate_service_method_signature_normal(t_function* tfunction,
- bool is_interface) {
- if (is_interface) {
+void t_haxe_generator::generate_service_method_signature_normal(t_function* tfunction, bool is_interface) {
+ if( is_interface) {
indent(f_service_) << function_signature_normal(tfunction) << ";" << endl << endl;
} else {
- indent(f_service_) << "public " << function_signature_normal(tfunction) << " {" << endl;
+ indent(f_service_) <<
+ "public " << function_signature_normal(tfunction) << " {" << endl;
}
}
@@ -1549,18 +1594,19 @@
*
* @param tfunction The service function to generate code for.
*/
-void t_haxe_generator::generate_service_method_signature_callback(t_function* tfunction,
- bool is_interface) {
+void t_haxe_generator::generate_service_method_signature_callback(t_function* tfunction, bool is_interface) {
if (!tfunction->is_oneway()) {
std::string on_success_impl = generate_service_method_onsuccess(tfunction, false, false);
indent(f_service_) << "// function onError(Dynamic) : Void;" << endl;
indent(f_service_) << "// function " << on_success_impl.c_str() << ";" << endl;
}
- if (is_interface) {
- indent(f_service_) << function_signature_callback(tfunction) << ";" << endl << endl;
+ if( is_interface) {
+ indent(f_service_) << function_signature_callback(tfunction) << ";" <<
+ endl << endl;
} else {
- indent(f_service_) << "public " << function_signature_callback(tfunction) << " {" << endl;
+ indent(f_service_) <<
+ "public " << function_signature_callback(tfunction) << " {" << endl;
}
}
@@ -1576,17 +1622,19 @@
}
generate_haxe_doc(f_service_, tservice);
- f_service_ << indent() << "interface " << get_cap_name(service_name_) << extends_iface << " {"
- << endl << endl;
+ f_service_ << indent() << "interface " << get_cap_name(service_name_) << extends_iface <<
+ " {" << endl << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
generate_haxe_doc(f_service_, *f_iter);
- generate_service_method_signature(*f_iter, true);
+ generate_service_method_signature(*f_iter,true);
}
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -1605,6 +1653,7 @@
}
}
+
/**
* Generates a service client definition.
*
@@ -1618,14 +1667,19 @@
extends_client = " extends " + extends + "Impl";
}
- indent(f_service_) << "class " << get_cap_name(service_name_) << "Impl" << extends_client
- << " implements " << get_cap_name(service_name_) << " {" << endl << endl;
+ indent(f_service_) <<
+ "class " << get_cap_name(service_name_) <<
+ "Impl" << extends_client <<
+ " implements " << get_cap_name(service_name_) <<
+ " {" << endl << endl;
indent_up();
- indent(f_service_) << "public function new( iprot : TProtocol, oprot : TProtocol = null)" << endl;
+ indent(f_service_) <<
+ "public function new( iprot : TProtocol, oprot : TProtocol = null)" << endl;
scope_up(f_service_);
if (extends.empty()) {
- f_service_ << indent() << "iprot_ = iprot;" << endl;
+ f_service_ <<
+ indent() << "iprot_ = iprot;" << endl;
f_service_ << indent() << "if (oprot == null) {" << endl;
indent_up();
f_service_ << indent() << "oprot_ = iprot;" << endl;
@@ -1636,27 +1690,35 @@
indent_down();
f_service_ << indent() << "}" << endl;
} else {
- f_service_ << indent() << "super(iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "super(iprot, oprot);" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_ << indent() << "private var iprot_ : TProtocol;" << endl << indent()
- << "private var oprot_ : TProtocol;" << endl << indent()
- << "private var seqid_ : Int;" << endl << endl;
+ f_service_ <<
+ indent() << "private var iprot_ : TProtocol;" << endl <<
+ indent() << "private var oprot_ : TProtocol;" << endl <<
+ indent() << "private var seqid_ : Int;" << endl <<
+ endl;
- indent(f_service_) << "public function getInputProtocol() : TProtocol" << endl;
+ indent(f_service_) <<
+ "public function getInputProtocol() : TProtocol" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.iprot_;" << endl;
+ indent(f_service_) <<
+ "return this.iprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public function getOutputProtocol() : TProtocol" << endl;
+ indent(f_service_) <<
+ "public function getOutputProtocol() : TProtocol" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.oprot_;" << endl;
+ indent(f_service_) <<
+ "return this.oprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
+
}
// Generate client method implementations
@@ -1666,49 +1728,51 @@
string funname = (*f_iter)->get_name();
// Open function
- generate_service_method_signature(*f_iter, false);
+ generate_service_method_signature(*f_iter,false);
indent_up();
+
// Get the struct of function call params
t_struct* arg_struct = (*f_iter)->get_arglist();
-
- string argsname = get_cap_name((*f_iter)->get_name() + "_args");
+
+ string argsname = get_cap_name( (*f_iter)->get_name() + "_args");
vector<t_field*>::const_iterator fld_iter;
const vector<t_field*>& fields = arg_struct->get_members();
// Serialize the request
string calltype = (*f_iter)->is_oneway() ? "ONEWAY" : "CALL";
- f_service_ << indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname
- << "\", TMessageType." << calltype << ", seqid_));" << endl << indent()
- << "var args : " << argsname << " = new " << argsname << "();" << endl;
+ f_service_ <<
+ indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", TMessageType." << calltype << ", seqid_));" << endl <<
+ indent() << "var args : " << argsname << " = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = "
- << (*fld_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << ";" << endl;
}
- f_service_ << indent() << "args.write(oprot_);" << endl << indent()
- << "oprot_.writeMessageEnd();" << endl;
-
- if (!((*f_iter)->is_oneway() || (*f_iter)->get_returntype()->is_void())) {
- f_service_ << indent() << "var retval : " << type_name((*f_iter)->get_returntype()) << ";"
- << endl;
+ f_service_ <<
+ indent() << "args.write(oprot_);" << endl <<
+ indent() << "oprot_.writeMessageEnd();" << endl;
+
+ if( ! ((*f_iter)->is_oneway() || (*f_iter)->get_returntype()->is_void())) {
+ f_service_ << indent() << "var retval : " << type_name((*f_iter)->get_returntype()) <<";" << endl;
}
-
+
if ((*f_iter)->is_oneway()) {
f_service_ << indent() << "oprot_.getTransport().flush();" << endl;
- } else {
+ }
+ else {
indent(f_service_) << "oprot_.getTransport().flush(function(error:Dynamic) : Void {" << endl;
indent_up();
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "try {" << endl;
indent_up();
}
- string resultname = get_cap_name((*f_iter)->get_name() + "_result");
+ string resultname = get_cap_name( (*f_iter)->get_name() + "_result");
indent(f_service_) << "if (error != null) {" << endl;
indent_up();
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "if (onError != null) onError(error);" << endl;
indent(f_service_) << "return;" << endl;
} else {
@@ -1721,7 +1785,7 @@
indent_up();
indent(f_service_) << "var x = TApplicationException.read(iprot_);" << endl;
indent(f_service_) << "iprot_.readMessageEnd();" << endl;
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "if (onError != null) onError(x);" << endl;
indent(f_service_) << "return;" << endl;
} else {
@@ -1729,8 +1793,7 @@
}
indent_down();
indent(f_service_) << "}" << endl;
- indent(f_service_) << "var result : " << resultname << " = new " << resultname << "();"
- << endl;
+ indent(f_service_) << "var result : " << resultname << " = new " << resultname << "();" << endl;
indent(f_service_) << "result.read(iprot_);" << endl;
indent(f_service_) << "iprot_.readMessageEnd();" << endl;
@@ -1738,7 +1801,7 @@
if (!(*f_iter)->get_returntype()->is_void()) {
indent(f_service_) << "if (result." << generate_isset_check("success") << ") {" << endl;
indent_up();
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "if (onSuccess != null) onSuccess(result.success);" << endl;
indent(f_service_) << "return;" << endl;
} else {
@@ -1755,9 +1818,8 @@
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
indent(f_service_) << "if (result." << (*x_iter)->get_name() << " != null) {" << endl;
indent_up();
- if (callbacks_) {
- indent(f_service_) << "if (onError != null) onError(result." << (*x_iter)->get_name()
- << ");" << endl;
+ if( callbacks_) {
+ indent(f_service_) << "if (onError != null) onError(result." << (*x_iter)->get_name() << ");" << endl;
indent(f_service_) << "return;" << endl;
} else {
indent(f_service_) << "throw result." << (*x_iter)->get_name() << ";" << endl;
@@ -1768,51 +1830,49 @@
// If you get here it's an exception, unless a void function
if ((*f_iter)->get_returntype()->is_void()) {
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "if (onSuccess != null) onSuccess();" << endl;
}
indent(f_service_) << "return;" << endl;
} else {
- if (callbacks_) {
+ if( callbacks_) {
indent(f_service_) << "if (onError != null)" << endl;
indent_up();
- indent(f_service_)
- << "onError( new TApplicationException(TApplicationException.MISSING_RESULT," << endl;
- indent(f_service_) << " \"" << (*f_iter)->get_name()
- << " failed: unknown result\"));" << endl;
+ indent(f_service_) << "onError( new TApplicationException(TApplicationException.MISSING_RESULT," << endl;
+ indent(f_service_) << " \"" << (*f_iter)->get_name() << " failed: unknown result\"));" << endl;
indent_down();
} else {
- indent(f_service_)
- << "throw new TApplicationException(TApplicationException.MISSING_RESULT," << endl;
- indent(f_service_) << " \"" << (*f_iter)->get_name()
- << " failed: unknown result\");" << endl;
+ indent(f_service_) << "throw new TApplicationException(TApplicationException.MISSING_RESULT," << endl;
+ indent(f_service_) << " \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
}
- if (callbacks_) {
+ if( callbacks_) {
indent_down();
indent(f_service_) << "} catch( e : TException) {" << endl;
indent_up();
indent(f_service_) << "if (onError != null) onError(e);" << endl;
indent_down();
indent(f_service_) << "}" << endl;
- }
-
+ }
+
indent_down();
- indent(f_service_) << "});" << endl;
+ indent(f_service_) <<
+ "});" << endl;
}
-
- if (!((*f_iter)->is_oneway() || (*f_iter)->get_returntype()->is_void())) {
+
+ if( ! ((*f_iter)->is_oneway() || (*f_iter)->get_returntype()->is_void())) {
f_service_ << indent() << "return retval;" << endl;
}
-
+
// Close function
scope_down(f_service_);
f_service_ << endl;
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) <<
+ "}" << endl;
}
/**
@@ -1834,32 +1894,36 @@
}
// Generate the header portion
- indent(f_service_) << "class " << get_cap_name(service_name_) << "Processor" << extends_processor
- << " implements TProcessor {" << endl << endl;
+ indent(f_service_) <<
+ "class " << get_cap_name(service_name_) <<
+ "Processor" << extends_processor <<
+ " implements TProcessor {" <<
+ endl << endl;
indent_up();
- f_service_ << indent() << "private var " << get_cap_name(service_name_)
- << "_iface_ : " << get_cap_name(service_name_) << ";" << endl;
+ f_service_ <<
+ indent() << "private var " << get_cap_name(service_name_) << "_iface_ : " << get_cap_name(service_name_) << ";" << endl;
if (extends.empty()) {
- f_service_ << indent()
- << "private var PROCESS_MAP = new StringMap< Int->TProtocol->TProtocol->Void >();"
- << endl;
+ f_service_ <<
+ indent() << "private var PROCESS_MAP = new StringMap< Int->TProtocol->TProtocol->Void >();" << endl;
}
f_service_ << endl;
- indent(f_service_) << "public function new( iface : " << get_cap_name(service_name_) << ")"
- << endl;
+ indent(f_service_) <<
+ "public function new( iface : " << get_cap_name(service_name_) << ")" << endl;
scope_up(f_service_);
if (!extends.empty()) {
- f_service_ << indent() << "super(iface);" << endl;
+ f_service_ <<
+ indent() << "super(iface);" << endl;
}
- f_service_ << indent() << get_cap_name(service_name_) << "_iface_ = iface;" << endl;
+ f_service_ <<
+ indent() << get_cap_name(service_name_) << "_iface_ = iface;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "PROCESS_MAP.set(\"" << (*f_iter)->get_name() << "\", "
- << (*f_iter)->get_name() << "());" << endl;
+ f_service_ <<
+ indent() << "PROCESS_MAP.set(\"" << (*f_iter)->get_name() << "\", " << (*f_iter)->get_name() << "());" << endl;
}
scope_down(f_service_);
@@ -1868,32 +1932,34 @@
// Generate the server implementation
string override = "";
if (tservice->get_extends() != NULL) {
- override = "override ";
+ override = "override ";
}
- indent(f_service_) << override
- << "public function process( iprot : TProtocol, oprot : TProtocol) : Bool"
- << endl;
+ indent(f_service_) << override << "public function process( iprot : TProtocol, oprot : TProtocol) : Bool" << endl;
scope_up(f_service_);
- f_service_ << indent() << "var msg : TMessage = iprot.readMessageBegin();" << endl;
+ f_service_ <<
+ indent() << "var msg : TMessage = iprot.readMessageBegin();" << endl;
// TODO(mcslee): validate message, was the seqid etc. legit?
// AS- If all method is oneway:
// do you have an oprot?
// do you you need nullcheck?
- f_service_
- << indent() << "var fn = PROCESS_MAP.get(msg.name);" << endl << indent()
- << "if (fn == null) {" << endl << indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);"
- << endl << indent() << " iprot.readMessageEnd();" << endl << indent()
- << " var x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid "
- "method name: '\"+msg.name+\"'\");" << endl << indent()
- << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));"
- << endl << indent() << " x.write(oprot);" << endl << indent() << " oprot.writeMessageEnd();"
- << endl << indent() << " oprot.getTransport().flush();" << endl << indent()
- << " return true;" << endl << indent() << "}" << endl << indent()
- << "fn( msg.seqid, iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "var fn = PROCESS_MAP.get(msg.name);" << endl <<
+ indent() << "if (fn == null) {" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl <<
+ indent() << " iprot.readMessageEnd();" << endl <<
+ indent() << " var x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid method name: '\"+msg.name+\"'\");" << endl <<
+ indent() << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));" << endl <<
+ indent() << " x.write(oprot);" << endl <<
+ indent() << " oprot.writeMessageEnd();" << endl <<
+ indent() << " oprot.getTransport().flush();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "fn( msg.seqid, iprot, oprot);" << endl;
- f_service_ << indent() << "return true;" << endl;
+ f_service_ <<
+ indent() << "return true;" << endl;
scope_down(f_service_);
f_service_ << endl;
@@ -1904,7 +1970,9 @@
}
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -1917,7 +1985,7 @@
return;
}
- string resultname = get_cap_name(tfunction->get_name() + "_result");
+ string resultname = get_cap_name( tfunction->get_name() + "_result");
t_struct result(program_, resultname);
t_field success(tfunction->get_returntype(), "success", 0);
if (!tfunction->get_returntype()->is_void()) {
@@ -1939,24 +2007,27 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_haxe_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_haxe_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open class
- indent(f_service_) << "private function " << tfunction->get_name()
- << "() : Int->TProtocol->TProtocol->Void {" << endl;
+ indent(f_service_) <<
+ "private function " << tfunction->get_name() << "() : Int->TProtocol->TProtocol->Void {" << endl;
indent_up();
// Open function
- indent(f_service_) << "return function( seqid : Int, iprot : TProtocol, oprot : TProtocol) : Void"
- << endl;
+ indent(f_service_) <<
+ "return function( seqid : Int, iprot : TProtocol, oprot : TProtocol) : Void"
+ << endl;
scope_up(f_service_);
- string argsname = get_cap_name(tfunction->get_name() + "_args");
- string resultname = get_cap_name(tfunction->get_name() + "_result");
+ string argsname = get_cap_name( tfunction->get_name() + "_args");
+ string resultname = get_cap_name( tfunction->get_name() + "_result");
- f_service_ << indent() << "var args : " << argsname << " = new " << argsname << "();" << endl
- << indent() << "args.read(iprot);" << endl << indent() << "iprot.readMessageEnd();"
- << endl;
+ f_service_ <<
+ indent() << "var args : "<< argsname << " = new " << argsname << "();" << endl <<
+ indent() << "args.read(iprot);" << endl <<
+ indent() << "iprot.readMessageEnd();" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -1964,51 +2035,53 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "var result : " << resultname << " = new " << resultname << "();"
- << endl;
+ f_service_ <<
+ indent() << "var result : " << resultname << " = new " << resultname << "();" << endl;
}
// Try block for any function to catch (defined or undefined) exceptions
- f_service_ << indent() << "try {" << endl;
+ f_service_ <<
+ indent() << "try {" << endl;
indent_up();
- if (callbacks_) {
+ if(callbacks_) {
// callback function style onError/onSuccess
// Generate the function call
t_struct* arg_struct = tfunction->get_arglist();
const std::vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator f_iter;
-
+
f_service_ << indent();
- f_service_ << get_cap_name(service_name_) << "_iface_." << tfunction->get_name() << "(";
+ f_service_ <<
+ get_cap_name(service_name_) << "_iface_." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
} else {
f_service_ << ", ";
- }
+ }
f_service_ << "args." << (*f_iter)->get_name();
}
-
- if (tfunction->is_oneway()) {
+
+ if( tfunction->is_oneway()) {
f_service_ << ");" << endl;
} else {
if (first) {
first = false;
} else {
f_service_ << ", ";
- }
+ }
string on_success = generate_service_method_onsuccess(tfunction, false, true);
indent_up();
f_service_ << endl;
indent(f_service_) << "null, // errors are thrown by the handler" << endl;
- if (tfunction->get_returntype()->is_void()) {
+ if( tfunction->get_returntype()->is_void()) {
indent(f_service_) << "null); // no retval" << endl;
} else {
indent(f_service_) << "function" << on_success.c_str() << " {" << endl;
- if (!tfunction->get_returntype()->is_void()) {
+ if( ! tfunction->get_returntype()->is_void()) {
indent_up();
indent(f_service_) << "result.success = retval;" << endl;
indent_down();
@@ -2020,40 +2093,41 @@
} else {
// normal function():result style
-
+
// Generate the function call
t_struct* arg_struct = tfunction->get_arglist();
const std::vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator f_iter;
-
+
f_service_ << indent();
- if (!(tfunction->is_oneway() || tfunction->get_returntype()->is_void())) {
+ if( ! (tfunction->is_oneway() || tfunction->get_returntype()->is_void())) {
f_service_ << "result.success = ";
}
- f_service_ << get_cap_name(service_name_) << "_iface_." << tfunction->get_name() << "(";
+ f_service_ <<
+ get_cap_name(service_name_) << "_iface_." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
} else {
f_service_ << ", ";
- }
+ }
f_service_ << "args." << (*f_iter)->get_name();
}
f_service_ << ");" << endl;
+
}
indent_down();
f_service_ << indent() << "}";
- if (!tfunction->is_oneway()) {
+ if( ! tfunction->is_oneway()) {
// catch exceptions defined in the IDL
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << " catch (" << (*x_iter)->get_name() << ":"
- << get_cap_name(type_name((*x_iter)->get_type(), false, false)) << ") {" << endl;
+ f_service_ << " catch (" << (*x_iter)->get_name() << ":" << get_cap_name(type_name((*x_iter)->get_type(), false, false)) << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent() << "}";
} else {
@@ -2061,17 +2135,14 @@
}
}
}
-
+
// always catch all exceptions to prevent from service denial
f_service_ << " catch (th : Dynamic) {" << endl;
indent_up();
- indent(f_service_) << "trace(\"Internal error processing " << tfunction->get_name() << "\", th);"
- << endl;
- if (!tfunction->is_oneway()) {
- indent(f_service_) << "var x = new TApplicationException(TApplicationException.INTERNAL_ERROR, "
- "\"Internal error processing " << tfunction->get_name() << "\");" << endl;
- indent(f_service_) << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.EXCEPTION, seqid));" << endl;
+ indent(f_service_) << "trace(\"Internal error processing " << tfunction->get_name() << "\", th);" << endl;
+ if( ! tfunction->is_oneway()) {
+ indent(f_service_) << "var x = new TApplicationException(TApplicationException.INTERNAL_ERROR, \"Internal error processing " << tfunction->get_name() << "\");" << endl;
+ indent(f_service_) << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl;
indent(f_service_) << "x.write(oprot);" << endl;
indent(f_service_) << "oprot.writeMessageEnd();" << endl;
indent(f_service_) << "oprot.getTransport().flush();" << endl;
@@ -2082,19 +2153,23 @@
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
scope_down(f_service_);
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
return;
}
- f_service_ << indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid));" << endl << indent() << "result.write(oprot);"
- << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
- << "oprot.getTransport().flush();" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid));" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl;
// Close function
scope_down(f_service_);
@@ -2102,7 +2177,9 @@
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -2111,28 +2188,35 @@
* @param tfield The field
* @param prefix The variable name or container for this field
*/
-void t_haxe_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_haxe_generator::generate_deserialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
- indent(out) << name << " = iprot.";
+ indent(out) <<
+ name << " = 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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -2165,11 +2249,11 @@
} else if (type->is_enum()) {
out << "readI32();";
}
- out << endl;
+ out <<
+ endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
@@ -2179,14 +2263,17 @@
void t_haxe_generator::generate_deserialize_struct(ofstream& out,
t_struct* tstruct,
string prefix) {
- out << indent() << prefix << " = new " << get_cap_name(type_name(tstruct)) << "();" << endl
- << indent() << prefix << ".read(iprot);" << endl;
+ out <<
+ indent() << prefix << " = new " << get_cap_name(type_name(tstruct)) << "();" << endl <<
+ indent() << prefix << ".read(iprot);" << endl;
}
/**
* Deserializes a container by reading its size and then iterating
*/
-void t_haxe_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_haxe_generator::generate_deserialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
string obj;
@@ -2208,26 +2295,28 @@
indent(out) << "var " << obj << " = iprot.readListBegin();" << endl;
}
- indent(out) << prefix << " = new " << type_name(ttype, false, true)
- // size the collection correctly
- << "("
- << ");" << endl;
+ indent(out)
+ << prefix << " = new " << type_name(ttype, false, true)
+ // size the collection correctly
+ << "("
+ << ");" << endl;
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for( " << i << " in 0 ... " << obj << ".size)" << endl;
+ indent(out) <<
+ "for( " << i << " in 0 ... " << obj << ".size)" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- scope_down(out);
+ scope_down(out);
// Read container end
if (ttype->is_map()) {
@@ -2241,36 +2330,46 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
-void t_haxe_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_haxe_generator::generate_deserialize_map_element(ofstream& out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey) << endl;
- indent(out) << declare_field(&fval) << endl;
+ indent(out) <<
+ declare_field(&fkey) << endl;
+ indent(out) <<
+ declare_field(&fval) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << ".set( " << key << ", " << val << ");" << endl;
+ indent(out) <<
+ prefix << ".set( " << key << ", " << val << ");" << endl;
}
/**
* Deserializes a set element
*/
-void t_haxe_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_haxe_generator::generate_deserialize_set_element(ofstream& out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".add(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".add(" << elem << ");" << endl;
}
/**
@@ -2282,41 +2381,53 @@
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".add(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".add(" << elem << ");" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_haxe_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_haxe_generator::generate_serialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
string name = prefix + tfield->get_name();
- indent(out) << "oprot.";
+ indent(out) <<
+ "oprot.";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -2364,9 +2475,12 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_haxe_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
- out << indent() << prefix << ".write(oprot);" << endl;
+void t_haxe_generator::generate_serialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
+ out <<
+ indent() << prefix << ".write(oprot);" << endl;
}
/**
@@ -2375,7 +2489,9 @@
* @param ttype The type of container
* @param prefix String prefix for fields
*/
-void t_haxe_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_haxe_generator::generate_serialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
@@ -2385,26 +2501,34 @@
indent(out) << "for( " << iter << " in " << prefix << ") {" << endl;
indent(out) << " " << counter << +"++;" << endl;
indent(out) << "}" << endl;
-
- indent(out) << "oprot.writeMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << counter << "));"
- << endl;
+
+ indent(out) <<
+ "oprot.writeMapBegin(new TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ counter << "));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", " << prefix << ".size));" << endl;
+ indent(out) <<
+ "oprot.writeSetBegin(new TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListBegin(new TList("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".length));"
- << endl;
+ indent(out) <<
+ "oprot.writeListBegin(new TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length));" << endl;
}
string iter = tmp("elem");
if (ttype->is_map()) {
- indent(out) << "for( " << iter << " in " << prefix << ".keys())" << endl;
+ indent(out) <<
+ "for( " << iter << " in " << prefix << ".keys())" << endl;
} else if (ttype->is_set()) {
- indent(out) << "for( " << iter << " in " << prefix << ".toArray())" << endl;
+ indent(out) <<
+ "for( " << iter << " in " << prefix << ".toArray())" << endl;
} else if (ttype->is_list()) {
- indent(out) << "for( " << iter << " in " << prefix << ")" << endl;
+ indent(out) <<
+ "for( " << iter << " in " << prefix << ")" << endl;
}
scope_up(out);
@@ -2420,11 +2544,14 @@
scope_down(out);
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapEnd();" << endl;
+ indent(out) <<
+ "oprot.writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetEnd();" << endl;
+ indent(out) <<
+ "oprot.writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListEnd();" << endl;
+ indent(out) <<
+ "oprot.writeListEnd();" << endl;
}
scope_down(out);
@@ -2446,7 +2573,9 @@
/**
* Serializes the members of a set.
*/
-void t_haxe_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_haxe_generator::generate_serialize_set_element(ofstream& out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2454,7 +2583,9 @@
/**
* Serializes the members of a list.
*/
-void t_haxe_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_haxe_generator::generate_serialize_list_element(ofstream& out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2467,20 +2598,20 @@
* @return haxe type name, i.e. HashMap<Key,Value>
*/
string t_haxe_generator::type_name(t_type* ttype, bool in_container, bool in_init) {
- (void)in_init;
-
+ (void) in_init;
+
// typedefs are just resolved to their real type
ttype = get_true_type(ttype);
string prefix;
if (ttype->is_base_type()) {
return base_type_name((t_base_type*)ttype, in_container);
- }
-
+ }
+
if (ttype->is_enum()) {
return "Int";
}
-
+
if (ttype->is_map()) {
t_type* tkey = get_true_type(((t_map*)ttype)->get_key_type());
t_type* tval = get_true_type(((t_map*)ttype)->get_val_type());
@@ -2488,8 +2619,8 @@
t_base_type::t_base tbase = ((t_base_type*)tkey)->get_base();
switch (tbase) {
case t_base_type::TYPE_STRING:
- if (!(((t_base_type*)tkey)->is_binary())) {
- return "StringMap< " + type_name(tval) + ">";
+ if( ! (((t_base_type*)tkey)->is_binary())) {
+ return "StringMap< "+type_name(tval)+">";
}
case t_base_type::TYPE_BYTE:
case t_base_type::TYPE_I16:
@@ -2498,43 +2629,43 @@
case t_base_type::TYPE_I64:
return "Int64Map< " + type_name(tval) + ">";
default:
- break; // default to ObjectMap<>
+ break; // default to ObjectMap<>
}
}
if (tkey->is_enum()) {
- return "IntMap< " + type_name(tval) + ">";
+ return "IntMap< " + type_name(tval) + ">";
}
return "ObjectMap< " + type_name(tkey) + ", " + type_name(tval) + ">";
}
-
+
if (ttype->is_set()) {
t_type* tkey = get_true_type(((t_list*)ttype)->get_elem_type());
- if (tkey->is_base_type()) {
+ if( tkey->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)tkey)->get_base();
switch (tbase) {
case t_base_type::TYPE_STRING:
- if (!(((t_base_type*)tkey)->is_binary())) {
+ if( ! (((t_base_type*)tkey)->is_binary())) {
return "StringSet";
- }
+ }
case t_base_type::TYPE_BYTE:
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
- return "IntSet";
+ return "IntSet";
case t_base_type::TYPE_I64:
- return "Int64Set";
+ return "Int64Set";
default:
- break; // default to ObjectSet
+ break; // default to ObjectSet
}
}
if (tkey->is_enum()) {
- return "IntSet";
+ return "IntSet";
}
return "ObjectSet< " + type_name(tkey) + ">";
}
-
+
if (ttype->is_list()) {
t_type* telm = ((t_list*)ttype)->get_elem_type();
- return "List< " + type_name(telm) + ">";
+ return "List< "+type_name(telm)+">";
}
// Check for namespacing
@@ -2555,8 +2686,9 @@
* @param tbase The base type
* @param container Is it going in a haxe container?
*/
-string t_haxe_generator::base_type_name(t_base_type* type, bool in_container) {
- (void)in_container;
+string t_haxe_generator::base_type_name(t_base_type* type,
+ bool in_container) {
+ (void) in_container;
t_base_type::t_base tbase = type->get_base();
switch (tbase) {
@@ -2616,7 +2748,7 @@
case t_base_type::TYPE_DOUBLE:
result += " = (double)0";
break;
- }
+ }
} else if (ttype->is_enum()) {
result += " = 0";
@@ -2635,16 +2767,16 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_haxe_generator::function_signature_callback(t_function* tfunction) {
- std::string on_error_success = "onError : Dynamic->Void = null, "
- + generate_service_method_onsuccess(tfunction, true, false);
-
+string t_haxe_generator::function_signature_callback( t_function* tfunction) {
+ std::string on_error_success = "onError : Dynamic->Void = null, "
+ + generate_service_method_onsuccess(tfunction, true, false);
+
std::string arguments = argument_list(tfunction->get_arglist());
- if (!tfunction->is_oneway()) {
+ if (! tfunction->is_oneway()) {
if (arguments != "") {
arguments += ", ";
}
- arguments += on_error_success; //"onError : Function, onSuccess : Function";
+ arguments += on_error_success; //"onError : Function, onSuccess : Function";
}
std::string result = "function " + tfunction->get_name() + "(" + arguments + ") : Void";
@@ -2657,7 +2789,7 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_haxe_generator::function_signature_normal(t_function* tfunction) {
+string t_haxe_generator::function_signature_normal( t_function* tfunction) {
std::string arguments = argument_list(tfunction->get_arglist());
std::string resulttype;
@@ -2667,7 +2799,7 @@
resulttype = type_name(tfunction->get_returntype());
}
- std::string result = "function " + tfunction->get_name() + "(" + arguments + ") : " + resulttype;
+ std::string result = "function " + tfunction->get_name() + "(" + arguments + ") : "+resulttype;
return result;
}
@@ -2735,7 +2867,7 @@
/**
* Haxe class names must start with uppercase letter, but Haxe namespaces must not.
*/
-std::string t_haxe_generator::get_cap_name(std::string name) {
+std::string t_haxe_generator::get_cap_name(std::string name){
if (name.length() == 0) {
return name;
}
@@ -2756,22 +2888,22 @@
while (comma_pos < inner_types.length()) {
bool found = false;
switch (inner_types[comma_pos]) {
- case '<':
- ++nested;
- break;
- case '>':
- --nested;
- break;
- case ',':
- found = (nested == 0);
- break;
+ case '<':
+ ++nested;
+ break;
+ case '>':
+ --nested;
+ break;
+ case ',':
+ found = (nested == 0);
+ break;
}
if (found) {
break;
}
++comma_pos;
}
-
+
if (new_inner.length() > 0) {
new_inner += ",";
}
@@ -2784,6 +2916,7 @@
return get_cap_name(outer_type) + "<" + new_inner + ">";
}
+
// package name
size_t index = name.find_first_not_of(" \n\r\t");
if (index < name.length()) {
@@ -2837,16 +2970,21 @@
/**
* Emits a haxeDoc comment if the provided object has a doc in Thrift
*/
-void t_haxe_generator::generate_haxe_doc(ofstream& out, t_doc* tdoc) {
+void t_haxe_generator::generate_haxe_doc(ofstream &out,
+ t_doc* tdoc) {
if (tdoc->has_doc()) {
- generate_docstring_comment(out, "/**\n", " * ", tdoc->get_doc(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", tdoc->get_doc(),
+ " */\n");
}
}
/**
* Emits a haxeDoc comment if the provided function object has a doc in Thrift
*/
-void t_haxe_generator::generate_haxe_doc(ofstream& out, t_function* tfunction) {
+void t_haxe_generator::generate_haxe_doc(ofstream &out,
+ t_function* tfunction) {
if (tfunction->has_doc()) {
stringstream ss;
ss << tfunction->get_doc();
@@ -2859,7 +2997,10 @@
ss << " " << p->get_doc();
}
}
- generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
}
}
@@ -2886,7 +3027,7 @@
return package + type->get_name();
}
-THRIFT_REGISTER_GENERATOR(
- haxe,
- "Haxe",
- " callbacks: Use onError()/onSuccess() callbacks for service methods (like AS3)\n")
+THRIFT_REGISTER_GENERATOR(haxe, "Haxe",
+ " callbacks: Use onError()/onSuccess() callbacks for service methods (like AS3)\n"
+ )
+
diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc
index b217ce6..c969619 100644
--- a/compiler/cpp/src/generate/t_hs_generator.cc
+++ b/compiler/cpp/src/generate/t_hs_generator.cc
@@ -39,20 +39,21 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Haskell code generator.
*
*/
class t_hs_generator : public t_oop_generator {
-public:
+ public:
t_hs_generator(t_program* program,
const map<string, string>& parsed_options,
const string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-hs";
}
@@ -66,12 +67,12 @@
/**
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
string render_const_value(t_type* type, t_const_value* value);
@@ -79,62 +80,95 @@
* Struct generation code
*/
- void generate_hs_struct(t_struct* tstruct, bool is_exception);
+ void generate_hs_struct (t_struct* tstruct,
+ bool is_exception);
- void generate_hs_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool helper = false);
+ void generate_hs_struct_definition (ofstream &out,
+ t_struct* tstruct,
+ bool is_xception = false,
+ bool helper = false);
- void generate_hs_struct_reader(ofstream& out, t_struct* tstruct);
+ void generate_hs_struct_reader (ofstream& out,
+ t_struct* tstruct);
- void generate_hs_struct_writer(ofstream& out, t_struct* tstruct);
+ void generate_hs_struct_writer (ofstream& out,
+ t_struct* tstruct);
- void generate_hs_struct_arbitrary(ofstream& out, t_struct* tstruct);
+ void generate_hs_struct_arbitrary (ofstream& out,
+ t_struct* tstruct);
- void generate_hs_function_helpers(t_function* tfunction);
+ void generate_hs_function_helpers (t_function* tfunction);
- void generate_hs_typemap(ofstream& out, t_struct* tstruct);
+ void generate_hs_typemap (ofstream& out,
+ t_struct* tstruct);
- void generate_hs_default(ofstream& out, t_struct* tstruct);
+ void generate_hs_default (ofstream& out,
+ t_struct* tstruct);
+
/**
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice,
+ t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(ofstream& out, t_field* tfield, string prefix);
+ void generate_deserialize_field (ofstream &out,
+ t_field* tfield,
+ string prefix);
- void generate_deserialize_struct(ofstream& out, t_struct* tstruct, string name = "");
+ void generate_deserialize_struct (ofstream &out,
+ t_struct* tstruct,
+ string name = "");
- void generate_deserialize_container(ofstream& out, t_type* ttype, string arg = "");
+ void generate_deserialize_container (ofstream &out,
+ t_type* ttype,
+ string arg = "");
- void generate_deserialize_set_element(ofstream& out, t_set* tset);
+ void generate_deserialize_set_element (ofstream &out,
+ t_set* tset);
- void generate_deserialize_list_element(ofstream& out, t_list* tlist, string prefix = "");
- void generate_deserialize_type(ofstream& out, t_type* type, string arg = "");
+ void generate_deserialize_list_element (ofstream &out,
+ t_list* tlist,
+ string prefix = "");
- void generate_serialize_type(ofstream& out, t_type* type, string name = "");
+ void generate_deserialize_type (ofstream &out,
+ t_type* type,
+ string arg = "");
- void generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix = "");
+ void generate_serialize_type (ofstream &out,
+ t_type* type,
+ string name = "");
- void generate_serialize_container(ofstream& out, t_type* ttype, string prefix = "");
+ void generate_serialize_struct (ofstream &out,
+ t_struct* tstruct,
+ string prefix = "");
- void generate_serialize_map_element(ofstream& out, t_map* tmap, string kiter, string viter);
+ void generate_serialize_container (ofstream &out,
+ t_type* ttype,
+ string prefix = "");
- void generate_serialize_set_element(ofstream& out, t_set* tmap, string iter);
+ void generate_serialize_map_element (ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter);
- void generate_serialize_list_element(ofstream& out, t_list* tlist, string iter);
+ void generate_serialize_set_element (ofstream &out,
+ t_set* tmap,
+ string iter);
+
+ void generate_serialize_list_element (ofstream &out,
+ t_list* tlist,
+ string iter);
/**
* Helper rendering functions
@@ -144,7 +178,8 @@
string hs_language_pragma();
string hs_imports();
- string type_name(t_type* ttype, string function_prefix = "");
+ string type_name(t_type* ttype,
+ string function_prefix = "");
string field_name(string tname, string fname);
@@ -157,13 +192,15 @@
string type_to_default(t_type* ttype);
- string render_hs_type(t_type* type, bool needs_parens);
+ string render_hs_type(t_type* type,
+ bool needs_parens);
string type_to_constructor(t_type* ttype);
- string render_hs_type_for_function_name(t_type* type);
+ string render_hs_type_for_function_name(t_type *type);
-private:
+ private:
+
ofstream f_types_;
ofstream f_consts_;
ofstream f_service_;
@@ -203,25 +240,24 @@
}
string t_hs_generator::hs_language_pragma() {
- return string(
- "{-# LANGUAGE DeriveDataTypeable #-}\n"
- "{-# LANGUAGE OverloadedStrings #-}\n"
- "{-# OPTIONS_GHC -fno-warn-missing-fields #-}\n"
- "{-# OPTIONS_GHC -fno-warn-missing-signatures #-}\n"
- "{-# OPTIONS_GHC -fno-warn-name-shadowing #-}\n"
- "{-# OPTIONS_GHC -fno-warn-unused-imports #-}\n"
- "{-# OPTIONS_GHC -fno-warn-unused-matches #-}\n");
+ return string("{-# LANGUAGE DeriveDataTypeable #-}\n"
+ "{-# LANGUAGE OverloadedStrings #-}\n"
+ "{-# OPTIONS_GHC -fno-warn-missing-fields #-}\n"
+ "{-# OPTIONS_GHC -fno-warn-missing-signatures #-}\n"
+ "{-# OPTIONS_GHC -fno-warn-name-shadowing #-}\n"
+ "{-# OPTIONS_GHC -fno-warn-unused-imports #-}\n"
+ "{-# OPTIONS_GHC -fno-warn-unused-matches #-}\n");
}
/**
* Autogen'd comment
*/
string t_hs_generator::hs_autogen_comment() {
- return string("-----------------------------------------------------------------\n")
- + "-- Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ") --\n"
- + "-- --\n"
- + "-- DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING --\n"
- + "-----------------------------------------------------------------\n";
+ return string("-----------------------------------------------------------------\n") +
+ "-- Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ") --\n" +
+ "-- --\n" +
+ "-- DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING --\n" +
+ "-----------------------------------------------------------------\n";
}
/**
@@ -319,7 +355,7 @@
indent_down();
indent(f_types_) << "toEnum t = case t of" << endl;
indent_up();
- for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
string name = capitalize((*c_iter)->get_name());
indent(f_types_) << value << " -> " << name << endl;
@@ -399,7 +435,8 @@
} else if (type->is_enum()) {
t_enum* tenum = (t_enum*)type;
vector<t_enum_value*> constants = tenum->get_constants();
- for (vector<t_enum_value*>::iterator c_iter = constants.begin(); c_iter != constants.end();
+ for (vector<t_enum_value*>::iterator c_iter = constants.begin();
+ c_iter != constants.end();
++c_iter) {
int val = (*c_iter)->get_value();
if (val == value->get_integer()) {
@@ -424,7 +461,8 @@
++v_iter) {
t_field* field = NULL;
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end();
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
++f_iter)
if ((*f_iter)->get_name() == v_iter->first->get_string())
field = (*f_iter);
@@ -437,7 +475,8 @@
out << (first ? "" : ", ");
out << field_name(cname, fname) << " = ";
- if (field->get_req() == t_field::T_OPTIONAL || ((t_type*)field->get_type())->is_xception()) {
+ if (field->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)field->get_type())->is_xception()) {
out << "P.Just ";
}
out << const_value;
@@ -466,8 +505,9 @@
out << "])";
} else if (type->is_list() || type->is_set()) {
- t_type* etype = type->is_list() ? ((t_list*)type)->get_elem_type()
- : ((t_set*)type)->get_elem_type();
+ t_type* etype = type->is_list()
+ ? ((t_list*) type)->get_elem_type()
+ : ((t_set*) type)->get_elem_type();
const vector<t_const_value*>& val = value->get_list();
vector<t_const_value*>::const_iterator v_iter;
@@ -513,8 +553,9 @@
/**
* Generates a Haskell struct
*/
-void t_hs_generator::generate_hs_struct(t_struct* tstruct, bool is_exception) {
- generate_hs_struct_definition(f_types_, tstruct, is_exception, false);
+void t_hs_generator::generate_hs_struct(t_struct* tstruct,
+ bool is_exception) {
+ generate_hs_struct_definition(f_types_,tstruct, is_exception,false);
}
/**
@@ -526,7 +567,7 @@
t_struct* tstruct,
bool is_exception,
bool helper) {
- (void)helper;
+ (void) helper;
string tname = type_name(tstruct);
string name = tstruct->get_name();
const vector<t_field*>& members = tstruct->get_members();
@@ -535,7 +576,8 @@
if (members.size() > 0) {
indent_up();
bool first = true;
- for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();
+ for (vector<t_field*>::const_iterator m_iter = members.begin();
+ m_iter != members.end();
++m_iter) {
if (first) {
indent(out) << "{ ";
@@ -545,8 +587,8 @@
}
string mname = (*m_iter)->get_name();
out << field_name(tname, mname) << " :: ";
- if ((*m_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*m_iter)->get_type())->is_xception()) {
+ if ((*m_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*m_iter)->get_type())->is_xception()) {
out << "P.Maybe ";
}
out << render_hs_type((*m_iter)->get_type(), true) << endl;
@@ -563,8 +605,7 @@
indent(out) << "instance H.Hashable " << tname << " where" << endl;
indent_up();
indent(out) << "hashWithSalt salt record = salt";
- for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();
- ++m_iter) {
+ for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
string mname = (*m_iter)->get_name();
indent(out) << " `H.hashWithSalt` " << field_name(tname, mname) << " record";
}
@@ -584,41 +625,38 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- indent(out) << "instance QC.Arbitrary " << tname << " where " << endl;
+ indent(out) << "instance QC.Arbitrary " << tname << " where "<< endl;
indent_up();
if (members.size() > 0) {
indent(out) << "arbitrary = M.liftM " << tname;
- indent_up();
- indent_up();
- indent_up();
- indent_up();
- bool first = true;
- for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();
+ indent_up(); indent_up(); indent_up(); indent_up();
+ bool first=true;
+ for (vector<t_field*>::const_iterator m_iter = members.begin();
+ m_iter != members.end();
++m_iter) {
- if (first) {
- first = false;
+ if(first) {
+ first=false;
out << " ";
- } else {
+ }
+ else {
indent(out) << "`M.ap`";
}
out << "(";
- if ((*m_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*m_iter)->get_type())->is_xception()) {
+ if ((*m_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*m_iter)->get_type())->is_xception()) {
out << "M.liftM P.Just ";
}
out << "QC.arbitrary)" << endl;
}
- indent_down();
- indent_down();
- indent_down();
- indent_down();
+ indent_down(); indent_down(); indent_down(); indent_down();
// Shrink
indent(out) << "shrink obj | obj == default_" << tname << " = []" << endl;
indent(out) << " | P.otherwise = M.catMaybes" << endl;
indent_up();
first = true;
- for (vector<t_field*>::const_iterator m_iter = members.begin(); m_iter != members.end();
+ for (vector<t_field*>::const_iterator m_iter = members.begin();
+ m_iter != members.end();
++m_iter) {
if (first) {
first = false;
@@ -636,7 +674,7 @@
indent(out) << "]" << endl;
indent_down();
} else { /* 0 == members.size() */
- indent(out) << "arbitrary = QC.elements [" << tname << "]" << endl;
+ indent(out) << "arbitrary = QC.elements [" <<tname<< "]" << endl;
}
indent_down();
}
@@ -659,7 +697,9 @@
bool first = true;
// Generate deserialization code for known cases
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
int32_t key = (*f_iter)->get_key();
string etype = type_to_enum((*f_iter)->get_type());
string fname = (*f_iter)->get_name();
@@ -677,8 +717,9 @@
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
out << "P.error \"Missing required field: " << fname << "\"";
} else {
- if (((*f_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*f_iter)->get_type())->is_xception()) && (*f_iter)->get_value() == NULL) {
+ if (((*f_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*f_iter)->get_type())->is_xception()) &&
+ (*f_iter)->get_value() == NULL) {
out << "P.Nothing";
} else {
out << field_name(sname, fname) << " default_" << sname;
@@ -687,12 +728,13 @@
out << ") ";
out << "(\\(_," << val << ") -> ";
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*f_iter)->get_type())->is_xception())
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*f_iter)->get_type())->is_xception())
out << "P.Just ";
generate_deserialize_field(out, *f_iter, val);
out << ")";
out << " (Map.lookup (" << key << ") fields)";
+
}
out << endl;
@@ -703,18 +745,19 @@
string tmap = type_name(tstruct, "typemap_");
indent(out) << "to_" << sname << " _ = P.error \"not a struct\"" << endl;
- indent(out) << "read_" << sname << " :: (T.Transport t, T.Protocol p) => p t -> P.IO " << sname
- << endl;
+ indent(out) << "read_" << sname <<
+ " :: (T.Transport t, T.Protocol p) => p t -> P.IO " << sname << endl;
indent(out) << "read_" << sname << " iprot = to_" << sname;
out << " <$> T.readVal iprot (T.T_STRUCT " << tmap << ")" << endl;
- indent(out) << "decode_" << sname
- << " :: (T.Protocol p, T.Transport t) => p t -> LBS.ByteString -> " << sname << endl;
+ indent(out) << "decode_" << sname <<
+ " :: (T.Protocol p, T.Transport t) => p t -> LBS.ByteString -> " << sname << endl;
indent(out) << "decode_" << sname << " iprot bs = to_" << sname << " $ ";
out << "T.deserializeVal iprot (T.T_STRUCT " << tmap << ") bs" << endl;
}
-void t_hs_generator::generate_hs_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_hs_generator::generate_hs_struct_writer(ofstream& out,
+ t_struct* tstruct) {
string name = type_name(tstruct);
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
@@ -728,7 +771,9 @@
// Get Exceptions
bool hasExn = false;
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
if (((t_type*)(*f_iter)->get_type())->is_xception()) {
hasExn = true;
break;
@@ -740,7 +785,8 @@
out << endl;
indent(out) << "(let exns = M.catMaybes ";
indent_up();
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end();
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
++f_iter) {
if (((t_type*)(*f_iter)->get_type())->is_xception()) {
if (isfirst) {
@@ -751,7 +797,7 @@
}
string mname = (*f_iter)->get_name();
int32_t key = (*f_iter)->get_key();
- out << "(\\" << v << " -> (" << key << ", (\"" << mname << "\",";
+ out << "(\\" << v << " -> (" << key << ", (\"" << mname << "\",";
generate_serialize_type(out, (*f_iter)->get_type(), v);
out << "))) <$> " << field_name(name, mname) << " record";
}
@@ -769,7 +815,9 @@
out << "M.catMaybes" << endl;
// Get the Rest
isfirst = true;
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
// Write field header
if (isfirst) {
indent(out) << "[ ";
@@ -781,15 +829,15 @@
int32_t key = (*f_iter)->get_key();
out << "(\\";
out << v << " -> ";
- if ((*f_iter)->get_req() != t_field::T_OPTIONAL
- && !((t_type*)(*f_iter)->get_type())->is_xception()) {
+ if ((*f_iter)->get_req() != t_field::T_OPTIONAL &&
+ !((t_type*)(*f_iter)->get_type())->is_xception()) {
out << "P.Just ";
}
out << "(" << key << ", (\"" << mname << "\",";
generate_serialize_type(out, (*f_iter)->get_type(), v);
out << "))) ";
- if ((*f_iter)->get_req() != t_field::T_OPTIONAL
- && !((t_type*)(*f_iter)->get_type())->is_xception()) {
+ if ((*f_iter)->get_req() != t_field::T_OPTIONAL &&
+ !((t_type*)(*f_iter)->get_type())->is_xception()) {
out << "$";
} else {
out << "<$>";
@@ -810,14 +858,14 @@
indent_down();
// write
- indent(out) << "write_" << name << " :: (T.Protocol p, T.Transport t) => p t -> " << name
- << " -> P.IO ()" << endl;
+ indent(out) << "write_" << name << " :: (T.Protocol p, T.Transport t) => p t -> "
+ << name << " -> P.IO ()" << endl;
indent(out) << "write_" << name << " oprot record = T.writeVal oprot $ from_";
out << name << " record" << endl;
// encode
- indent(out) << "encode_" << name << " :: (T.Protocol p, T.Transport t) => p t -> " << name
- << " -> LBS.ByteString" << endl;
+ indent(out) << "encode_" << name << " :: (T.Protocol p, T.Transport t) => p t -> "
+ << name << " -> LBS.ByteString" << endl;
indent(out) << "encode_" << name << " oprot record = T.serializeVal oprot $ ";
out << "from_" << name << " record" << endl;
}
@@ -867,7 +915,7 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
- generate_hs_struct_definition(f_service_, ts, false);
+ generate_hs_struct_definition(f_service_,ts, false);
generate_hs_function_helpers(*f_iter);
}
}
@@ -891,14 +939,15 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter)
result.append(*f_iter);
- generate_hs_struct_definition(f_service_, &result, false);
+ generate_hs_struct_definition(f_service_,&result, false);
}
/**
* Generate the map from field names to (type, id)
* @param tstruct the Struct
*/
-void t_hs_generator::generate_hs_typemap(ofstream& out, t_struct* tstruct) {
+void t_hs_generator::generate_hs_typemap(ofstream& out,
+ t_struct* tstruct) {
string name = type_name(tstruct);
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
@@ -906,7 +955,9 @@
indent(out) << "typemap_" << name << " :: T.TypeMap" << endl;
indent(out) << "typemap_" << name << " = Map.fromList [";
bool first = true;
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
string mname = (*f_iter)->get_name();
if (!first) {
out << ",";
@@ -924,7 +975,8 @@
* generate the struct with default values filled in
* @param tstruct the Struct
*/
-void t_hs_generator::generate_hs_default(ofstream& out, t_struct* tstruct) {
+void t_hs_generator::generate_hs_default(ofstream& out,
+ t_struct* tstruct) {
string name = type_name(tstruct);
string fname = type_name(tstruct, "default_");
const vector<t_field*>& fields = tstruct->get_sorted_members();
@@ -933,7 +985,9 @@
indent(out) << fname << " = " << name << "{" << endl;
indent_up();
bool first = true;
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
string mname = (*f_iter)->get_name();
if (first) {
first = false;
@@ -944,8 +998,8 @@
t_type* type = get_true_type((*f_iter)->get_type());
t_const_value* value = (*f_iter)->get_value();
indent(out) << field_name(name, mname) << " = ";
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*f_iter)->get_type())->is_xception()) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*f_iter)->get_type())->is_xception()) {
if (value == NULL) {
out << "P.Nothing";
} else {
@@ -994,7 +1048,7 @@
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string ft = function_type(*f_iter, true, true, true);
- indent(f_iface_) << decapitalize((*f_iter)->get_name()) << " :: a -> " << ft << endl;
+ indent(f_iface_) << decapitalize((*f_iter)->get_name()) << " :: a -> " << ft << endl;
}
indent_down();
@@ -1002,8 +1056,7 @@
}
/**
- * Generates a service client definition. Note that in Haskell, the client doesn't implement iface.
- *This is because
+ * Generates a service client definition. Note that in Haskell, the client doesn't implement iface. This is because
* The client does not (and should not have to) deal with arguments being Nothing.
*
* @param tservice The service to generate a server for.
@@ -1056,15 +1109,16 @@
fargs += " arg_" + (*fld_iter)->get_name();
// Open function
- indent(f_client_) << decapitalize(funname) << " (ip,op)" << fargs << " = do" << endl;
+ indent(f_client_) << decapitalize(funname) << " (ip,op)" << fargs << " = do" << endl;
indent_up();
- indent(f_client_) << "send_" << funname << " op" << fargs;
+ indent(f_client_) << "send_" << funname << " op" << fargs;
f_client_ << endl;
if (!(*f_iter)->is_oneway())
indent(f_client_) << "recv_" << funname << " ip" << endl;
+
indent_down();
indent(f_client_) << "send_" << funname << " op" << fargs << " = do" << endl;
@@ -1077,18 +1131,18 @@
// Serialize the request header
string fname = (*f_iter)->get_name();
string msgType = (*f_iter)->is_oneway() ? "T.M_ONEWAY" : "T.M_CALL";
- indent(f_client_) << "T.writeMessageBegin op (\"" << fname << "\", " << msgType << ", seqn)"
- << endl;
+ indent(f_client_) << "T.writeMessageBegin op (\"" << fname << "\", " << msgType << ", seqn)" << endl;
indent(f_client_) << "write_" << argsname << " op (" << argsname << "{";
bool first = true;
- for (vector<t_field*>::const_iterator fld_iter = fields.begin(); fld_iter != fields.end();
+ for (vector<t_field*>::const_iterator fld_iter = fields.begin();
+ fld_iter != fields.end();
++fld_iter) {
string fieldname = (*fld_iter)->get_name();
f_client_ << (first ? "" : ",");
f_client_ << field_name(argsname, fieldname) << "=";
- if ((*fld_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*fld_iter)->get_type())->is_xception())
+ if ((*fld_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*fld_iter)->get_type())->is_xception())
f_client_ << "P.Just ";
f_client_ << "arg_" << fieldname;
first = false;
@@ -1107,13 +1161,13 @@
string funname = string("recv_") + (*f_iter)->get_name();
t_function recv_function((*f_iter)->get_returntype(), funname, &noargs);
+
// Open function
indent(f_client_) << funname << " ip = do" << endl;
indent_up();
indent(f_client_) << "(fname, mtype, rseqid) <- T.readMessageBegin ip" << endl;
- indent(f_client_) << "M.when (mtype == T.M_EXCEPTION) $ do { exn <- T.readAppExn ip ; "
- "T.readMessageEnd ip ; X.throw exn }" << endl;
+ indent(f_client_) << "M.when (mtype == T.M_EXCEPTION) $ do { exn <- T.readAppExn ip ; T.readMessageEnd ip ; X.throw exn }" << endl;
indent(f_client_) << "res <- read_" << resultname << " ip" << endl;
indent(f_client_) << "T.readMessageEnd ip" << endl;
@@ -1121,10 +1175,12 @@
t_struct* xs = (*f_iter)->get_xceptions();
const vector<t_field*>& xceptions = xs->get_members();
- for (vector<t_field*>::const_iterator x_iter = xceptions.begin(); x_iter != xceptions.end();
+ for (vector<t_field*>::const_iterator x_iter = xceptions.begin();
+ x_iter != xceptions.end();
++x_iter) {
indent(f_client_) << "P.maybe (P.return ()) X.throw ("
- << field_name(resultname, (*x_iter)->get_name()) << " res)" << endl;
+ << field_name(resultname, (*x_iter)->get_name())
+ << " res)" << endl;
}
if (!(*f_iter)->get_returntype()->is_void())
@@ -1159,22 +1215,19 @@
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string fname = (*f_iter)->get_name();
- indent(f_service_) << "\"" << fname << "\" -> process_" << decapitalize(fname)
- << " (seqid,iprot,oprot,handler)" << endl;
+ indent(f_service_) << "\"" << fname << "\" -> process_" << decapitalize(fname) << " (seqid,iprot,oprot,handler)" << endl;
}
indent(f_service_) << "_ -> ";
if (tservice->get_extends() != NULL) {
- f_service_ << type_name(tservice->get_extends())
- << ".proc_ handler (iprot,oprot) (name,typ,seqid)" << endl;
+ f_service_ << type_name(tservice->get_extends()) << ".proc_ handler (iprot,oprot) (name,typ,seqid)" << endl;
} else {
f_service_ << "do" << endl;
indent_up();
indent(f_service_) << "_ <- T.readVal iprot (T.T_STRUCT Map.empty)" << endl;
indent(f_service_) << "T.writeMessageBegin oprot (name,T.M_EXCEPTION,seqid)" << endl;
- indent(f_service_) << "T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN_METHOD (\"Unknown function "
- "\" ++ LT.unpack name))" << endl;
+ indent(f_service_) << "T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN_METHOD (\"Unknown function \" ++ LT.unpack name))" << endl;
indent(f_service_) << "T.writeMessageEnd oprot" << endl;
indent(f_service_) << "T.tFlush (T.getTransport oprot)" << endl;
indent_down();
@@ -1194,25 +1247,25 @@
}
bool hasNoArguments(t_function* func) {
- return (func->get_arglist()->get_members().empty());
+ return (func->get_arglist()->get_members().empty());
}
string t_hs_generator::render_hs_type_for_function_name(t_type* type) {
- string type_str = render_hs_type(type, false);
- int found = -1;
+ string type_str = render_hs_type(type, false);
+ int found = -1;
- while (true) {
- found = type_str.find_first_of("[]. ", found + 1);
- if (string::npos == size_t(found)) {
- break;
+ while (true) {
+ found = type_str.find_first_of("[]. ", found + 1);
+ if (string::npos == size_t(found)) {
+ break;
+ }
+
+ if (type_str[found] == '.')
+ type_str[found] = '_';
+ else
+ type_str[found] = 'Z';
}
-
- if (type_str[found] == '.')
- type_str[found] = '_';
- else
- type_str[found] = 'Z';
- }
- return type_str;
+ return type_str;
}
/**
@@ -1220,8 +1273,9 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_hs_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_hs_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open function
string funname = decapitalize(tfunction->get_name());
indent(f_service_) << "process_" << funname << " (seqid, iprot, oprot, handler) = do" << endl;
@@ -1244,7 +1298,7 @@
size_t n = xceptions.size() + 1;
// Try block for a function with exceptions
if (n > 0) {
- for (size_t i = 0; i < n; i++) {
+ for(size_t i = 0; i < n; i++) {
indent(f_service_) << "(X.catch" << endl;
indent_up();
}
@@ -1261,12 +1315,13 @@
f_service_ << "Iface." << decapitalize(tfunction->get_name()) << " handler";
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter)
- f_service_ << " (" << field_name(argsname, (*f_iter)->get_name()) << " args)";
+ f_service_ << " (" <<
+ field_name(argsname, (*f_iter)->get_name()) << " args)";
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << endl;
- indent(f_service_) << "let res = default_" << resultname << "{"
- << field_name(resultname, "success") << " = val}";
+ indent(f_service_) << "let res = default_" << resultname << "{" <<
+ field_name(resultname, "success") << " = val}";
} else if (!tfunction->is_oneway()) {
f_service_ << endl;
@@ -1278,8 +1333,7 @@
if (tfunction->is_oneway()) {
indent(f_service_) << "P.return ()";
} else {
- indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name()
- << "\", T.M_REPLY, seqid)" << endl;
+ indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name() << "\", T.M_REPLY, seqid)" << endl;
indent(f_service_) << "write_" << resultname << " oprot res" << endl;
indent(f_service_) << "T.writeMessageEnd oprot" << endl;
indent(f_service_) << "T.tFlush (T.getTransport oprot)";
@@ -1297,13 +1351,11 @@
if (!tfunction->is_oneway()) {
indent(f_service_) << "let res = default_" << resultname << "{"
- << field_name(resultname, (*x_iter)->get_name()) << " = P.Just e}"
- << endl;
- indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name()
- << "\", T.M_REPLY, seqid)" << endl;
- indent(f_service_) << "write_" << resultname << " oprot res" << endl;
+ << field_name(resultname, (*x_iter)->get_name()) << " = P.Just e}" << endl;
+ indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name() << "\", T.M_REPLY, seqid)" << endl;
+ indent(f_service_ ) << "write_" << resultname << " oprot res" << endl;
indent(f_service_) << "T.writeMessageEnd oprot" << endl;
- indent(f_service_) << "T.tFlush (T.getTransport oprot)";
+ indent(f_service_ ) << "T.tFlush (T.getTransport oprot)";
} else {
indent(f_service_) << "P.return ()";
}
@@ -1316,11 +1368,10 @@
indent_up();
if (!tfunction->is_oneway()) {
- indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name()
- << "\", T.M_EXCEPTION, seqid)" << endl;
- indent(f_service_) << "T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN \"\")" << endl;
+ indent(f_service_) << "T.writeMessageBegin oprot (\"" << tfunction->get_name() << "\", T.M_EXCEPTION, seqid)" << endl;
+ indent(f_service_ ) << "T.writeAppExn oprot (T.AppExn T.AE_UNKNOWN \"\")" << endl;
indent(f_service_) << "T.writeMessageEnd oprot" << endl;
- indent(f_service_) << "T.tFlush (T.getTransport oprot)";
+ indent(f_service_ ) << "T.tFlush (T.getTransport oprot)";
} else {
indent(f_service_) << "P.return ()";
}
@@ -1328,24 +1379,30 @@
f_service_ << ") :: X.SomeException -> P.IO ()))" << endl;
indent_down();
indent_down();
+
}
// Close function
indent_down();
+
}
/**
* Deserializes a field of any type.
*/
-void t_hs_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
- (void)prefix;
+void t_hs_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
+ (void) prefix;
t_type* type = tfield->get_type();
- generate_deserialize_type(out, type, prefix);
+ generate_deserialize_type(out,type, prefix);
}
/**
* Deserializes a field of any type.
*/
-void t_hs_generator::generate_deserialize_type(ofstream& out, t_type* type, string arg) {
+void t_hs_generator::generate_deserialize_type(ofstream &out,
+ t_type* type,
+ string arg) {
type = get_true_type(type);
string val = tmp("_val");
out << "(case " << arg << " of {" << type_to_constructor(type) << " " << val << " -> ";
@@ -1374,10 +1431,13 @@
out << "; _ -> P.error \"wrong type\"})";
}
+
/**
* Generates an unserializer for a struct, calling read()
*/
-void t_hs_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string name) {
+void t_hs_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string name) {
out << "(" << type_name(tstruct, "to_") << " (T.TStruct " << name << "))";
}
@@ -1386,28 +1446,30 @@
* Serialize a container by writing out the header followed by
* data and then a footer.
*/
-void t_hs_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string arg) {
+void t_hs_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string arg) {
string val = tmp("_v");
// Declare variables, read header
if (ttype->is_map()) {
string key = tmp("_k");
out << "(Map.fromList $ P.map (\\(" << key << "," << val << ") -> (";
- generate_deserialize_type(out, ((t_map*)ttype)->get_key_type(), key);
+ generate_deserialize_type(out,((t_map*)ttype)->get_key_type(),key);
out << ",";
- generate_deserialize_type(out, ((t_map*)ttype)->get_val_type(), val);
+ generate_deserialize_type(out,((t_map*)ttype)->get_val_type(),val);
out << ")) " << arg << ")";
} else if (ttype->is_set()) {
out << "(Set.fromList $ P.map (\\" << val << " -> ";
- generate_deserialize_type(out, ((t_map*)ttype)->get_key_type(), val);
+ generate_deserialize_type(out,((t_map*)ttype)->get_key_type(),val);
out << ") " << arg << ")";
} else if (ttype->is_list()) {
out << "(Vector.fromList $ P.map (\\" << val << " -> ";
- generate_deserialize_type(out, ((t_map*)ttype)->get_key_type(), val);
+ generate_deserialize_type(out,((t_map*)ttype)->get_key_type(),val);
out << ") " << arg << ")";
}
}
@@ -1418,7 +1480,9 @@
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_hs_generator::generate_serialize_type(ofstream& out, t_type* type, string name) {
+void t_hs_generator::generate_serialize_type(ofstream &out,
+ t_type* type,
+ string name) {
type = get_true_type(type);
// Do nothing for void types
@@ -1456,11 +1520,15 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_hs_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
+void t_hs_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
out << type_name(tstruct, "from_") << " " << prefix;
}
-void t_hs_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_hs_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
string k = tmp("_k");
string v = tmp("_v");
@@ -1476,25 +1544,28 @@
} else if (ttype->is_set()) {
out << "T.TSet " << type_to_enum(((t_list*)ttype)->get_elem_type());
- out << " $ P.map (\\" << v << " -> ";
+ out <<" $ P.map (\\" << v << " -> ";
generate_serialize_type(out, ((t_list*)ttype)->get_elem_type(), v);
out << ") $ Set.toList " << prefix;
} else if (ttype->is_list()) {
out << "T.TList " << type_to_enum(((t_list*)ttype)->get_elem_type());
- out << " $ P.map (\\" << v << " -> ";
+ out <<" $ P.map (\\" << v << " -> ";
generate_serialize_type(out, ((t_list*)ttype)->get_elem_type(), v);
out << ") $ Vector.toList " << prefix;
}
+
}
string t_hs_generator::function_type(t_function* tfunc, bool options, bool io, bool method) {
string result = "";
const vector<t_field*>& fields = tfunc->get_arglist()->get_members();
- for (vector<t_field*>::const_iterator f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || ((t_type*)(*f_iter)->get_type())->is_xception())
+ for (vector<t_field*>::const_iterator f_iter = fields.begin();
+ f_iter != fields.end();
+ ++f_iter) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL ||
+ ((t_type*)(*f_iter)->get_type())->is_xception())
result += "P.Maybe ";
result += render_hs_type((*f_iter)->get_type(), options);
result += " -> ";
@@ -1510,6 +1581,7 @@
return result;
}
+
string t_hs_generator::type_name(t_type* ttype, string function_prefix) {
string prefix = "";
t_program* program = ttype->get_program();
@@ -1534,22 +1606,14 @@
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:
- return "T.T_VOID";
- case t_base_type::TYPE_STRING:
- return "T.T_STRING";
- case t_base_type::TYPE_BOOL:
- return "T.T_BOOL";
- case t_base_type::TYPE_BYTE:
- return "T.T_BYTE";
- case t_base_type::TYPE_I16:
- return "T.T_I16";
- case t_base_type::TYPE_I32:
- return "T.T_I32";
- case t_base_type::TYPE_I64:
- return "T.T_I64";
- case t_base_type::TYPE_DOUBLE:
- return "T.T_DOUBLE";
+ case t_base_type::TYPE_VOID: return "T.T_VOID";
+ case t_base_type::TYPE_STRING: return "T.T_STRING";
+ case t_base_type::TYPE_BOOL: return "T.T_BOOL";
+ case t_base_type::TYPE_BYTE: return "T.T_BYTE";
+ case t_base_type::TYPE_I16: return "T.T_I16";
+ case t_base_type::TYPE_I32: return "T.T_I32";
+ case t_base_type::TYPE_I64: return "T.T_I64";
+ case t_base_type::TYPE_DOUBLE: return "T.T_DOUBLE";
}
} else if (type->is_enum()) {
@@ -1582,22 +1646,14 @@
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:
- return "P.error \"No default value for type T_VOID\"";
- case t_base_type::TYPE_STRING:
- return "\"\"";
- case t_base_type::TYPE_BOOL:
- return "P.False";
- case t_base_type::TYPE_BYTE:
- return "0";
- case t_base_type::TYPE_I16:
- return "0";
- case t_base_type::TYPE_I32:
- return "0";
- case t_base_type::TYPE_I64:
- return "0";
- case t_base_type::TYPE_DOUBLE:
- return "0";
+ case t_base_type::TYPE_VOID: return "P.error \"No default value for type T_VOID\"";
+ case t_base_type::TYPE_STRING: return "\"\"";
+ case t_base_type::TYPE_BOOL: return "P.False";
+ case t_base_type::TYPE_BYTE: return "0";
+ case t_base_type::TYPE_I16: return "0";
+ case t_base_type::TYPE_I32: return "0";
+ case t_base_type::TYPE_I64: return "0";
+ case t_base_type::TYPE_DOUBLE: return "0";
}
} else if (type->is_enum()) {
@@ -1619,6 +1675,7 @@
throw "INVALID TYPE IN type_to_default: " + type->get_name();
}
+
/**
* Converts the parse type to an haskell type
*/
@@ -1629,22 +1686,14 @@
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:
- return "()";
- case t_base_type::TYPE_STRING:
- return (((t_base_type*)type)->is_binary() ? "LBS.ByteString" : "LT.Text");
- case t_base_type::TYPE_BOOL:
- return "P.Bool";
- case t_base_type::TYPE_BYTE:
- return "I.Int8";
- case t_base_type::TYPE_I16:
- return "I.Int16";
- case t_base_type::TYPE_I32:
- return "I.Int32";
- case t_base_type::TYPE_I64:
- return "I.Int64";
- case t_base_type::TYPE_DOUBLE:
- return "P.Double";
+ case t_base_type::TYPE_VOID: return "()";
+ case t_base_type::TYPE_STRING: return (((t_base_type*)type)->is_binary() ? "LBS.ByteString" : "LT.Text");
+ case t_base_type::TYPE_BOOL: return "P.Bool";
+ case t_base_type::TYPE_BYTE: return "I.Int8";
+ case t_base_type::TYPE_I16: return "I.Int16";
+ case t_base_type::TYPE_I32: return "I.Int32";
+ case t_base_type::TYPE_I64: return "I.Int64";
+ case t_base_type::TYPE_DOUBLE: return "P.Double";
}
} else if (type->is_enum()) {
@@ -1660,7 +1709,7 @@
} else if (type->is_set()) {
t_type* etype = ((t_set*)type)->get_elem_type();
- type_repr = "Set.HashSet " + render_hs_type(etype, true);
+ type_repr = "Set.HashSet " + render_hs_type(etype, true) ;
} else if (type->is_list()) {
t_type* etype = ((t_list*)type)->get_elem_type();
@@ -1682,22 +1731,14 @@
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 "invalid type: T_VOID";
- case t_base_type::TYPE_STRING:
- return "T.TString";
- case t_base_type::TYPE_BOOL:
- return "T.TBool";
- case t_base_type::TYPE_BYTE:
- return "T.TByte";
- case t_base_type::TYPE_I16:
- return "T.TI16";
- case t_base_type::TYPE_I32:
- return "T.TI32";
- case t_base_type::TYPE_I64:
- return "T.TI64";
- case t_base_type::TYPE_DOUBLE:
- return "T.TDouble";
+ case t_base_type::TYPE_VOID: throw "invalid type: T_VOID";
+ case t_base_type::TYPE_STRING: return "T.TString";
+ case t_base_type::TYPE_BOOL: return "T.TBool";
+ case t_base_type::TYPE_BYTE: return "T.TByte";
+ case t_base_type::TYPE_I16: return "T.TI16";
+ case t_base_type::TYPE_I32: return "T.TI32";
+ case t_base_type::TYPE_I64: return "T.TI64";
+ case t_base_type::TYPE_DOUBLE: return "T.TDouble";
}
} else if (type->is_enum()) {
@@ -1718,4 +1759,5 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
THRIFT_REGISTER_GENERATOR(hs, "Haskell", "")
diff --git a/compiler/cpp/src/generate/t_html_generator.cc b/compiler/cpp/src/generate/t_html_generator.cc
index f5185ea..a79ed50 100644
--- a/compiler/cpp/src/generate/t_html_generator.cc
+++ b/compiler/cpp/src/generate/t_html_generator.cc
@@ -38,7 +38,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
enum input_type { INPUT_UNKNOWN, INPUT_UTF8, INPUT_PLAIN };
@@ -48,13 +48,15 @@
* mostly copy/pasting/tweaking from mcslee's work.
*/
class t_html_generator : public t_generator {
-public:
- t_html_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_html_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-html";
input_type_ = INPUT_UNKNOWN;
@@ -62,14 +64,15 @@
iter = parsed_options.find("standalone");
standalone_ = (iter != parsed_options.end());
- iter = parsed_options.find("noescape");
- unsafe_ = (iter != parsed_options.end());
+ iter = parsed_options.find("noescape");
+ unsafe_ = (iter != parsed_options.end());
+
escape_.clear();
- escape_['&'] = "&";
- escape_['<'] = "<";
- escape_['>'] = ">";
- escape_['"'] = """;
+ escape_['&'] = "&";
+ escape_['<'] = "<";
+ escape_['>'] = ">";
+ escape_['"'] = """;
escape_['\''] = "'";
init_allowed__markup();
@@ -78,62 +81,65 @@
void generate_program();
void generate_program_toc();
void generate_program_toc_row(t_program* tprog);
- void generate_program_toc_rows(t_program* tprog, std::vector<t_program*>& finished);
+ void generate_program_toc_rows(t_program* tprog,
+ std::vector<t_program*>& finished);
void generate_index();
- std::string escape_html(std::string const& str);
- std::string escape_html_tags(std::string const& str);
+ std::string escape_html(std::string const & str);
+ std::string escape_html_tags(std::string const & str);
void generate_css();
- void generate_css_content(std::ofstream& f_target);
+ void generate_css_content(std::ofstream & f_target);
void generate_style_tag();
- std::string make_file_link(std::string name);
- bool is_utf8_sequence(std::string const& str, size_t firstpos);
- void detect_input_encoding(std::string const& str, size_t firstpos);
+ std::string make_file_link( std::string name);
+ bool is_utf8_sequence(std::string const & str, size_t firstpos);
+ void detect_input_encoding(std::string const & str, size_t firstpos);
void init_allowed__markup();
/**
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_service(t_service* tservice);
- void generate_xception(t_struct* txception);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_service (t_service* tservice);
+ void generate_xception(t_struct* txception);
- void print_doc(t_doc* tdoc);
- int print_type(t_type* ttype);
+ void print_doc (t_doc* tdoc);
+ int print_type (t_type* ttype);
void print_const_value(t_type* type, t_const_value* tvalue);
void print_fn_args_doc(t_function* tfunction);
-private:
+ private:
std::ofstream f_out_;
- std::string current_file_;
+ std::string current_file_;
input_type input_type_;
std::map<std::string, int> allowed_markup;
bool standalone_;
bool unsafe_;
};
+
/**
* Emits the Table of Contents links at the top of the module's page
*/
void t_html_generator::generate_program_toc() {
- f_out_ << "<table class=\"table-bordered table-striped "
- "table-condensed\"><thead><th>Module</th><th>Services</th>"
- << "<th>Data types</th><th>Constants</th></thead>" << endl;
+ f_out_ << "<table class=\"table-bordered table-striped table-condensed\"><thead><th>Module</th><th>Services</th>"
+ << "<th>Data types</th><th>Constants</th></thead>" << endl;
generate_program_toc_row(program_);
f_out_ << "</table>" << endl;
}
+
/**
* Recurses through from the provided program and generates a ToC row
* for each discovered program exactly once by maintaining the list of
* completed rows in 'finished'
*/
void t_html_generator::generate_program_toc_rows(t_program* tprog,
- std::vector<t_program*>& finished) {
- for (vector<t_program*>::iterator iter = finished.begin(); iter != finished.end(); iter++) {
+ std::vector<t_program*>& finished) {
+ for (vector<t_program*>::iterator iter = finished.begin();
+ iter != finished.end(); iter++) {
if (tprog->get_path() == (*iter)->get_path()) {
return;
}
@@ -141,7 +147,8 @@
finished.push_back(tprog);
generate_program_toc_row(tprog);
vector<t_program*> includes = tprog->get_includes();
- for (vector<t_program*>::iterator iter = includes.begin(); iter != includes.end(); iter++) {
+ for (vector<t_program*>::iterator iter = includes.begin();
+ iter != includes.end(); iter++) {
generate_program_toc_rows(*iter, finished);
}
}
@@ -158,26 +165,26 @@
for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
string name = get_service_name(*sv_iter);
f_out_ << "<a href=\"" << make_file_link(fname) << "#Svc_" << name << "\">" << name
- << "</a><br/>" << endl;
+ << "</a><br/>" << endl;
f_out_ << "<ul>" << endl;
- map<string, string> fn_html;
+ map<string,string> fn_html;
vector<t_function*> functions = (*sv_iter)->get_functions();
vector<t_function*>::iterator fn_iter;
for (fn_iter = functions.begin(); fn_iter != functions.end(); ++fn_iter) {
string fn_name = (*fn_iter)->get_name();
- string html = "<li><a href=\"" + make_file_link(fname) + "#Fn_" + name + "_" + fn_name
- + "\">" + fn_name + "</a></li>";
- fn_html.insert(pair<string, string>(fn_name, html));
+ string html = "<li><a href=\"" + make_file_link(fname) + "#Fn_" + name + "_" +
+ fn_name + "\">" + fn_name + "</a></li>";
+ fn_html.insert(pair<string,string>(fn_name, html));
}
- for (map<string, string>::iterator html_iter = fn_html.begin(); html_iter != fn_html.end();
- html_iter++) {
+ for (map<string,string>::iterator html_iter = fn_html.begin();
+ html_iter != fn_html.end(); html_iter++) {
f_out_ << html_iter->second << endl;
}
f_out_ << "</ul>" << endl;
}
}
f_out_ << "</td>" << endl << "<td>";
- map<string, string> data_types;
+ map<string,string> data_types;
if (!tprog->get_enums().empty()) {
vector<t_enum*> enums = tprog->get_enums();
vector<t_enum*>::iterator en_iter;
@@ -185,8 +192,9 @@
string name = (*en_iter)->get_name();
// f_out_ << "<a href=\"" << make_file_link(fname) << "#Enum_" << name << "\">" << name
// << "</a><br/>" << endl;
- string html = "<a href=\"" + make_file_link(fname) + "#Enum_" + name + "\">" + name + "</a>";
- data_types.insert(pair<string, string>(name, html));
+ string html = "<a href=\"" + make_file_link(fname) + "#Enum_" + name + "\">" + name +
+ "</a>";
+ data_types.insert(pair<string,string>(name, html));
}
}
if (!tprog->get_typedefs().empty()) {
@@ -196,9 +204,9 @@
string name = (*td_iter)->get_symbolic();
// f_out_ << "<a href=\"" << make_file_link(fname) << "#Typedef_" << name << "\">" << name
// << "</a><br/>" << endl;
- string html = "<a href=\"" + make_file_link(fname) + "#Typedef_" + name + "\">" + name
- + "</a>";
- data_types.insert(pair<string, string>(name, html));
+ string html = "<a href=\"" + make_file_link(fname) + "#Typedef_" + name + "\">" + name +
+ "</a>";
+ data_types.insert(pair<string,string>(name, html));
}
}
if (!tprog->get_objects().empty()) {
@@ -206,30 +214,30 @@
vector<t_struct*>::iterator o_iter;
for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
string name = (*o_iter)->get_name();
- // f_out_ << "<a href=\"" << make_file_link(fname) << "#Struct_" << name << "\">" << name
+ //f_out_ << "<a href=\"" << make_file_link(fname) << "#Struct_" << name << "\">" << name
//<< "</a><br/>" << endl;
- string html = "<a href=\"" + make_file_link(fname) + "#Struct_" + name + "\">" + name
- + "</a>";
- data_types.insert(pair<string, string>(name, html));
+ string html = "<a href=\"" + make_file_link(fname) + "#Struct_" + name + "\">" + name +
+ "</a>";
+ data_types.insert(pair<string,string>(name, html));
}
}
- for (map<string, string>::iterator dt_iter = data_types.begin(); dt_iter != data_types.end();
- dt_iter++) {
+ for (map<string,string>::iterator dt_iter = data_types.begin();
+ dt_iter != data_types.end(); dt_iter++) {
f_out_ << dt_iter->second << "<br/>" << endl;
}
f_out_ << "</td>" << endl << "<td>";
if (!tprog->get_consts().empty()) {
- map<string, string> const_html;
+ map<string,string> const_html;
vector<t_const*> consts = tprog->get_consts();
vector<t_const*>::iterator con_iter;
for (con_iter = consts.begin(); con_iter != consts.end(); ++con_iter) {
string name = (*con_iter)->get_name();
- string html = "<code><a href=\"" + make_file_link(fname) + "#Const_" + name + "\">" + name
- + "</a></code>";
- const_html.insert(pair<string, string>(name, html));
+ string html ="<code><a href=\"" + make_file_link(fname) + "#Const_" + name +
+ "\">" + name + "</a></code>";
+ const_html.insert(pair<string,string>(name, html));
}
- for (map<string, string>::iterator con_iter = const_html.begin(); con_iter != const_html.end();
- con_iter++) {
+ for (map<string,string>::iterator con_iter = const_html.begin();
+ con_iter != const_html.end(); con_iter++) {
f_out_ << con_iter->second << "<br/>" << endl;
}
}
@@ -243,7 +251,7 @@
void t_html_generator::generate_program() {
// Make output directory
MKDIR(get_out_dir().c_str());
- current_file_ = program_->get_name() + ".html";
+ current_file_ = program_->get_name() + ".html";
string fname = get_out_dir() + current_file_;
f_out_.open(fname.c_str());
f_out_ << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" << endl;
@@ -252,9 +260,11 @@
f_out_ << "<head>" << endl;
f_out_ << "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" << endl;
generate_style_tag();
- f_out_ << "<title>Thrift module: " << program_->get_name() << "</title></head><body>" << endl
- << "<div class=\"container-fluid\">" << endl
- << "<h1>Thrift module: " << program_->get_name() << "</h1>" << endl;
+ f_out_ << "<title>Thrift module: " << program_->get_name()
+ << "</title></head><body>" << endl
+ << "<div class=\"container-fluid\">" << endl
+ << "<h1>Thrift module: "
+ << program_->get_name() << "</h1>" << endl;
print_doc(program_);
@@ -321,6 +331,7 @@
generate_css();
}
+
/**
* Emits the index.html file for the recursive set of Thrift programs
*/
@@ -330,11 +341,11 @@
f_out_.open(index_fname.c_str());
f_out_ << "<html><head>" << endl;
generate_style_tag();
- f_out_ << "<title>All Thrift declarations</title></head><body>" << endl
- << "<div class=\"container-fluid\">" << endl << "<h1>All Thrift declarations</h1>" << endl;
- f_out_ << "<table class=\"table-bordered table-striped "
- "table-condensed\"><thead><th>Module</th><th>Services</th><th>Data types</th>"
- << "<th>Constants</th></thead>" << endl;
+ f_out_ << "<title>All Thrift declarations</title></head><body>"
+ << endl << "<div class=\"container-fluid\">"
+ << endl << "<h1>All Thrift declarations</h1>" << endl;
+ f_out_ << "<table class=\"table-bordered table-striped table-condensed\"><thead><th>Module</th><th>Services</th><th>Data types</th>"
+ << "<th>Constants</th></thead>" << endl;
vector<t_program*> programs;
generate_program_toc_rows(program_, programs);
f_out_ << "</table>" << endl;
@@ -343,40 +354,38 @@
}
void t_html_generator::generate_css() {
- if (!standalone_) {
+ if( ! standalone_) {
current_file_ = "style.css";
string css_fname = get_out_dir() + current_file_;
f_out_.open(css_fname.c_str());
- generate_css_content(f_out_);
+ generate_css_content( f_out_);
f_out_.close();
}
}
-void t_html_generator::generate_css_content(std::ofstream& f_target) {
+
+void t_html_generator::generate_css_content(std::ofstream & f_target) {
f_target << BOOTSTRAP_CSS() << endl;
f_target << "/* Auto-generated CSS for generated Thrift docs */" << endl;
f_target << "h3, h4 { margin-bottom: 6px; }" << endl;
- f_target << "div.definition { border: 1px solid #CCC; margin-bottom: 10px; padding: 10px; }"
- << endl;
+ f_target << "div.definition { border: 1px solid #CCC; margin-bottom: 10px; padding: 10px; }" << endl;
f_target << "div.extends { margin: -0.5em 0 1em 5em }" << endl;
f_target << "td { vertical-align: top; }" << endl;
f_target << "table { empty-cells: show; }" << endl;
f_target << "code { line-height: 20px; }" << endl;
- f_target << ".table-bordered th, .table-bordered td { border-bottom: 1px solid #DDDDDD; }"
- << endl;
+ f_target << ".table-bordered th, .table-bordered td { border-bottom: 1px solid #DDDDDD; }" << endl;
}
/**
* Generates the CSS tag.
- * Depending on "standalone", either a CSS file link (default), or the entire CSS is embedded
- * inline.
+ * Depending on "standalone", either a CSS file link (default), or the entire CSS is embedded inline.
*/
void t_html_generator::generate_style_tag() {
- if (!standalone_) {
+ if( ! standalone_) {
f_out_ << "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/>" << endl;
} else {
f_out_ << "<style type=\"text/css\"/><!--" << endl;
- generate_css_content(f_out_);
+ generate_css_content( f_out_);
f_out_ << "--></style>" << endl;
}
}
@@ -385,8 +394,8 @@
* Returns the target file for a <a href> link
* The returned string is empty, whenever filename refers to the current file.
*/
-std::string t_html_generator::make_file_link(std::string filename) {
- return (current_file_.compare(filename) != 0) ? filename : "";
+std::string t_html_generator::make_file_link( std::string filename) {
+ return (current_file_.compare(filename) != 0) ? filename : "";
}
/**
@@ -395,7 +404,7 @@
*/
void t_html_generator::print_doc(t_doc* tdoc) {
if (tdoc->has_doc()) {
- if (unsafe_) {
+ if (unsafe_) {
f_out_ << tdoc->get_doc() << "<br/>";
} else {
f_out_ << escape_html(tdoc->get_doc()) << "<br/>";
@@ -403,32 +412,33 @@
}
}
-bool t_html_generator::is_utf8_sequence(std::string const& str, size_t firstpos) {
+bool t_html_generator::is_utf8_sequence(std::string const & str, size_t firstpos) {
// leading char determines the length of the sequence
unsigned char c = str.at(firstpos);
int count = 0;
- if ((c & 0xE0) == 0xC0) {
+ if( (c & 0xE0) == 0xC0) {
count = 1;
- } else if ((c & 0xF0) == 0xE0) {
+ } else if( (c & 0xF0) == 0xE0) {
count = 2;
- } else if ((c & 0xF8) == 0xF0) {
+ } else if( (c & 0xF8) == 0xF0) {
count = 3;
- } else if ((c & 0xFC) == 0xF8) {
+ } else if( (c & 0xFC) == 0xF8) {
count = 4;
- } else if ((c & 0xFE) == 0xFC) {
+ } else if( (c & 0xFE) == 0xFC) {
count = 5;
} else {
- // pdebug("UTF-8 test: char '%c' (%d) is not a valid UTF-8 leading byte", c, int(c));
- return false; // no UTF-8
+ //pdebug("UTF-8 test: char '%c' (%d) is not a valid UTF-8 leading byte", c, int(c));
+ return false; // no UTF-8
}
// following chars
size_t pos = firstpos + 1;
- while ((pos < str.length()) && (0 < count)) {
+ while( (pos < str.length()) && (0 < count))
+ {
c = str.at(pos);
- if ((c & 0xC0) != 0x80) {
- // pdebug("UTF-8 test: char '%c' (%d) is not a valid UTF-8 following byte", c, int(c));
- return false; // no UTF-8
+ if( (c & 0xC0) != 0x80) {
+ //pdebug("UTF-8 test: char '%c' (%d) is not a valid UTF-8 following byte", c, int(c));
+ return false; // no UTF-8
}
--count;
++pos;
@@ -438,15 +448,16 @@
return (0 == count);
}
-void t_html_generator::detect_input_encoding(std::string const& str, size_t firstpos) {
- if (is_utf8_sequence(str, firstpos)) {
- pdebug("Input seems to be already UTF-8 encoded");
+void t_html_generator::detect_input_encoding(std::string const & str, size_t firstpos) {
+ if( is_utf8_sequence(str,firstpos))
+ {
+ pdebug( "Input seems to be already UTF-8 encoded");
input_type_ = INPUT_UTF8;
return;
}
// fallback
- pwarning(1, "Input is not UTF-8, treating as plain ANSI");
+ pwarning( 1, "Input is not UTF-8, treating as plain ANSI");
input_type_ = INPUT_PLAIN;
}
@@ -509,53 +520,53 @@
allowed_markup["/h6"] = 1;
}
-std::string t_html_generator::escape_html_tags(std::string const& str) {
+std::string t_html_generator::escape_html_tags(std::string const & str) {
std::ostringstream result;
unsigned char c = '?';
size_t lastpos;
size_t firstpos = 0;
- while (firstpos < str.length()) {
+ while( firstpos < str.length()) {
// look for non-ASCII char
lastpos = firstpos;
- while (lastpos < str.length()) {
+ while( lastpos < str.length()) {
c = str.at(lastpos);
- if (('<' == c) || ('>' == c)) {
+ if( ('<' == c) || ('>' == c)) {
break;
}
++lastpos;
}
// copy what we got so far
- if (lastpos > firstpos) {
- result << str.substr(firstpos, lastpos - firstpos);
+ if( lastpos > firstpos) {
+ result << str.substr( firstpos, lastpos-firstpos);
firstpos = lastpos;
}
// reached the end?
- if (firstpos >= str.length()) {
+ if( firstpos >= str.length()) {
break;
}
// tag end without corresponding begin
++firstpos;
- if ('>' == c) {
+ if( '>' == c) {
result << ">";
continue;
}
// extract the tag
std::ostringstream tagstream;
- while (firstpos < str.length()) {
+ while( firstpos < str.length()) {
c = str.at(firstpos);
++firstpos;
- if ('<' == c) {
+ if('<'==c) {
tagstream << "<"; // nested begin?
- } else if ('>' == c) {
+ } else if('>'==c) {
break;
} else {
- tagstream << c; // not very efficient, but tags should be quite short
+ tagstream << c; // not very efficient, but tags should be quite short
}
}
@@ -563,13 +574,13 @@
string tag_content = tagstream.str();
string tag_key = tag_content;
size_t first_white = tag_key.find_first_of(" \t\f\v\n\r");
- if (first_white != string::npos) {
+ if( first_white != string::npos) {
tag_key.erase(first_white);
}
- for (std::string::size_type i = 0; i < tag_key.length(); ++i) {
+ for (std::string::size_type i=0; i<tag_key.length(); ++i) {
tag_key[i] = tolower(tag_key[i]);
}
- if (allowed_markup.find(tag_key) != allowed_markup.end()) {
+ if( allowed_markup.find(tag_key) != allowed_markup.end()) {
result << "<" << tag_content << ">";
} else {
result << "<" << tagstream.str() << ">";
@@ -580,10 +591,10 @@
return result.str();
}
-std::string t_html_generator::escape_html(std::string const& str) {
+std::string t_html_generator::escape_html(std::string const & str) {
// the generated HTML header says it is UTF-8 encoded
// if UTF-8 input has been detected before, we don't need to change anything
- if (input_type_ == INPUT_UTF8) {
+ if( input_type_ == INPUT_UTF8) {
return escape_html_tags(str);
}
@@ -593,68 +604,70 @@
unsigned int ic = 0;
size_t lastpos;
size_t firstpos = 0;
- while (firstpos < str.length()) {
+ while( firstpos < str.length()) {
// look for non-ASCII char
lastpos = firstpos;
- while (lastpos < str.length()) {
+ while( lastpos < str.length()) {
c = str.at(lastpos);
ic = c;
- if ((32 > ic) || (127 < ic)) {
+ if( (32 > ic) || (127 < ic)) {
break;
}
++lastpos;
}
// copy what we got so far
- if (lastpos > firstpos) {
- result << str.substr(firstpos, lastpos - firstpos);
+ if( lastpos > firstpos) {
+ result << str.substr( firstpos, lastpos-firstpos);
firstpos = lastpos;
}
// reached the end?
- if (firstpos >= str.length()) {
+ if( firstpos >= str.length()) {
break;
}
// some control code?
- if (ic <= 31) {
- switch (c) {
- case '\r':
- case '\n':
- case '\t':
- result << c;
- break;
- default: // silently consume all other ctrl chars
- break;
+ if(ic <= 31)
+ {
+ switch( c)
+ {
+ case '\r' :
+ case '\n' :
+ case '\t' :
+ result << c;
+ break;
+ default: // silently consume all other ctrl chars
+ break;
}
++firstpos;
continue;
}
// reached the end?
- if (firstpos >= str.length()) {
+ if( firstpos >= str.length()) {
break;
}
// try to detect input encoding
- if (input_type_ == INPUT_UNKNOWN) {
- detect_input_encoding(str, firstpos);
- if (input_type_ == INPUT_UTF8) {
+ if( input_type_ == INPUT_UNKNOWN) {
+ detect_input_encoding(str,firstpos);
+ if( input_type_ == INPUT_UTF8) {
lastpos = str.length();
- result << str.substr(firstpos, lastpos - firstpos);
+ result << str.substr( firstpos, lastpos-firstpos);
break;
}
}
// convert the character to something useful based on the detected encoding
- switch (input_type_) {
- case INPUT_PLAIN:
- result << "&#" << ic << ";";
- ++firstpos;
- break;
- default:
- throw "Unexpected or unrecognized input encoding";
+ switch( input_type_) {
+ case INPUT_PLAIN:
+ result << "&#" << ic << ";";
+ ++firstpos;
+ break;
+ default:
+ throw "Unexpected or unrecognized input encoding";
}
}
@@ -689,7 +702,7 @@
} else {
string prog_name = ttype->get_program()->get_name();
string type_name = ttype->get_name();
- f_out_ << "<a href=\"" << make_file_link(prog_name + ".html") << "#";
+ f_out_ << "<a href=\"" << make_file_link(prog_name+".html") << "#";
if (ttype->is_typedef()) {
f_out_ << "Typedef_";
} else if (ttype->is_struct() || ttype->is_xception()) {
@@ -719,9 +732,8 @@
// if tvalue is an indentifier, the constant content is already shown elsewhere
if (tvalue->get_type() == t_const_value::CV_IDENTIFIER) {
string fname = program_->get_name() + ".html";
- string name = escape_html(tvalue->get_identifier());
- f_out_ << "<code><a href=\"" + make_file_link(fname) + "#Const_" + name + "\">" + name
- + "</a></code>";
+ string name = escape_html( tvalue->get_identifier());
+ f_out_ << "<code><a href=\"" + make_file_link(fname) + "#Const_" + name +"\">" + name + "</a></code>";
return;
}
@@ -734,38 +746,37 @@
if (truetype->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)truetype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- f_out_ << '"' << escape_html(get_escaped_string(tvalue)) << '"';
- break;
- case t_base_type::TYPE_BOOL:
- f_out_ << ((tvalue->get_integer() != 0) ? "true" : "false");
- break;
- case t_base_type::TYPE_BYTE:
- f_out_ << tvalue->get_integer();
- break;
- case t_base_type::TYPE_I16:
- f_out_ << tvalue->get_integer();
- break;
- case t_base_type::TYPE_I32:
- f_out_ << tvalue->get_integer();
- break;
- case t_base_type::TYPE_I64:
- f_out_ << tvalue->get_integer();
- break;
- case t_base_type::TYPE_DOUBLE:
- if (tvalue->get_type() == t_const_value::CV_INTEGER) {
+ case t_base_type::TYPE_STRING:
+ f_out_ << '"' << escape_html( get_escaped_string(tvalue)) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ f_out_ << ((tvalue->get_integer() != 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
f_out_ << tvalue->get_integer();
- } else {
- f_out_ << tvalue->get_double();
- }
- break;
- default:
- f_out_ << "UNKNOWN BASE TYPE";
- break;
+ break;
+ case t_base_type::TYPE_I16:
+ f_out_ << tvalue->get_integer();
+ break;
+ case t_base_type::TYPE_I32:
+ f_out_ << tvalue->get_integer();
+ break;
+ case t_base_type::TYPE_I64:
+ f_out_ << tvalue->get_integer();
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (tvalue->get_type() == t_const_value::CV_INTEGER) {
+ f_out_ << tvalue->get_integer();
+ } else {
+ f_out_ << tvalue->get_double();
+ }
+ break;
+ default:
+ f_out_ << "UNKNOWN BASE TYPE";
+ break;
}
} else if (truetype->is_enum()) {
- f_out_ << escape_html(truetype->get_name()) << "."
- << escape_html(tvalue->get_identifier_name());
+ f_out_ << escape_html(truetype->get_name()) << "." << escape_html(tvalue->get_identifier_name());
} else if (truetype->is_struct() || truetype->is_xception()) {
f_out_ << "{ ";
const vector<t_field*>& fields = ((t_struct*)truetype)->get_members();
@@ -780,15 +791,14 @@
}
}
if (field_type == NULL) {
- throw "type error: " + truetype->get_name() + " has no field "
- + v_iter->first->get_string();
+ throw "type error: " + truetype->get_name() + " has no field " + v_iter->first->get_string();
}
if (!first) {
f_out_ << ", ";
}
first = false;
- f_out_ << escape_html(v_iter->first->get_string()) << " = ";
- print_const_value(field_type, v_iter->second);
+ f_out_ << escape_html( v_iter->first->get_string()) << " = ";
+ print_const_value( field_type, v_iter->second);
}
f_out_ << " }";
} else if (truetype->is_map()) {
@@ -800,35 +810,33 @@
f_out_ << ", ";
}
first = false;
- print_const_value(((t_map*)truetype)->get_key_type(), map_iter->first);
+ print_const_value( ((t_map*)truetype)->get_key_type(), map_iter->first);
f_out_ << " = ";
- print_const_value(((t_map*)truetype)->get_val_type(), map_iter->second);
+ print_const_value( ((t_map*)truetype)->get_val_type(), map_iter->second);
}
f_out_ << " }";
} else if (truetype->is_list()) {
f_out_ << "{ ";
- vector<t_const_value*> list_elems = tvalue->get_list();
- ;
+ vector<t_const_value*> list_elems = tvalue->get_list();;
vector<t_const_value*>::iterator list_iter;
for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
if (!first) {
f_out_ << ", ";
}
first = false;
- print_const_value(((t_list*)truetype)->get_elem_type(), *list_iter);
+ print_const_value( ((t_list*)truetype)->get_elem_type(), *list_iter);
}
f_out_ << " }";
} else if (truetype->is_set()) {
f_out_ << "{ ";
- vector<t_const_value*> list_elems = tvalue->get_list();
- ;
+ vector<t_const_value*> list_elems = tvalue->get_list();;
vector<t_const_value*>::iterator list_iter;
for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
if (!first) {
f_out_ << ", ";
}
first = false;
- print_const_value(((t_set*)truetype)->get_elem_type(), *list_iter);
+ print_const_value( ((t_set*)truetype)->get_elem_type(), *list_iter);
}
f_out_ << " }";
} else {
@@ -837,28 +845,27 @@
}
/**
- * Prints out documentation for arguments/exceptions of a function, if any documentation has been
- * supplied.
+ * Prints out documentation for arguments/exceptions of a function, if any documentation has been supplied.
*/
void t_html_generator::print_fn_args_doc(t_function* tfunction) {
bool has_docs = false;
vector<t_field*> args = tfunction->get_arglist()->get_members();
vector<t_field*>::iterator arg_iter = args.begin();
if (arg_iter != args.end()) {
- for (; arg_iter != args.end(); arg_iter++) {
+ for ( ; arg_iter != args.end(); arg_iter++) {
if ((*arg_iter)->has_doc() && !(*arg_iter)->get_doc().empty())
has_docs = true;
}
if (has_docs) {
arg_iter = args.begin();
f_out_ << "<br/><h4 id=\"Parameters_" << service_name_ << "_" << tfunction->get_name()
- << "\">Parameters</h4>" << endl;
+ << "\">Parameters</h4>" << endl;
f_out_ << "<table class=\"table-bordered table-striped table-condensed\">";
f_out_ << "<thead><th>Name</th><th>Description</th></thead>";
- for (; arg_iter != args.end(); arg_iter++) {
+ for ( ; arg_iter != args.end(); arg_iter++) {
f_out_ << "<tr><td>" << (*arg_iter)->get_name();
f_out_ << "</td><td>";
- f_out_ << escape_html((*arg_iter)->get_doc());
+ f_out_ << escape_html( (*arg_iter)->get_doc());
f_out_ << "</td></tr>" << endl;
}
f_out_ << "</table>";
@@ -869,20 +876,20 @@
vector<t_field*> excepts = tfunction->get_xceptions()->get_members();
vector<t_field*>::iterator ex_iter = excepts.begin();
if (ex_iter != excepts.end()) {
- for (; ex_iter != excepts.end(); ex_iter++) {
+ for ( ; ex_iter != excepts.end(); ex_iter++) {
if ((*ex_iter)->has_doc() && !(*ex_iter)->get_doc().empty())
has_docs = true;
}
if (has_docs) {
ex_iter = excepts.begin();
f_out_ << "<br/><h4 id=\"Exceptions_" << service_name_ << "_" << tfunction->get_name()
- << "\">Exceptions</h4>" << endl;
+ << "\">Exceptions</h4>" << endl;
f_out_ << "<table class=\"table-bordered table-striped table-condensed\">";
f_out_ << "<thead><th>Type</th><th>Description</th></thead>";
- for (; ex_iter != excepts.end(); ex_iter++) {
+ for ( ; ex_iter != excepts.end(); ex_iter++) {
f_out_ << "<tr><td>" << (*ex_iter)->get_type()->get_name();
f_out_ << "</td><td>";
- f_out_ << escape_html((*ex_iter)->get_doc());
+ f_out_ << escape_html( (*ex_iter)->get_doc());
f_out_ << "</td></tr>" << endl;
}
f_out_ << "</table>";
@@ -898,7 +905,8 @@
void t_html_generator::generate_typedef(t_typedef* ttypedef) {
string name = ttypedef->get_name();
f_out_ << "<div class=\"definition\">";
- f_out_ << "<h3 id=\"Typedef_" << name << "\">Typedef: " << name << "</h3>" << endl;
+ f_out_ << "<h3 id=\"Typedef_" << name << "\">Typedef: " << name
+ << "</h3>" << endl;
f_out_ << "<p><strong>Base type:</strong> ";
print_type(ttypedef->get_type());
f_out_ << "</p>" << endl;
@@ -914,7 +922,8 @@
void t_html_generator::generate_enum(t_enum* tenum) {
string name = tenum->get_name();
f_out_ << "<div class=\"definition\">";
- f_out_ << "<h3 id=\"Enum_" << name << "\">Enumeration: " << name << "</h3>" << endl;
+ f_out_ << "<h3 id=\"Enum_" << name << "\">Enumeration: " << name
+ << "</h3>" << endl;
print_doc(tenum);
vector<t_enum_value*> values = tenum->get_constants();
vector<t_enum_value*>::iterator val_iter;
@@ -936,7 +945,8 @@
*/
void t_html_generator::generate_const(t_const* tconst) {
string name = tconst->get_name();
- f_out_ << "<tr id=\"Const_" << name << "\"><td><code>" << name << "</code></td><td>";
+ f_out_ << "<tr id=\"Const_" << name << "\"><td><code>" << name
+ << "</code></td><td>";
print_type(tconst->get_type());
f_out_ << "</td><td><code>";
print_const_value(tconst->get_type(), tconst->get_value());
@@ -968,15 +978,15 @@
vector<t_field*> members = tstruct->get_members();
vector<t_field*>::iterator mem_iter = members.begin();
f_out_ << "<table class=\"table-bordered table-striped table-condensed\">";
- f_out_ << "<thead><th>Key</th><th>Field</th><th>Type</th><th>Description</th><th>Requiredness</"
- "th><th>Default value</th></thead>" << endl;
- for (; mem_iter != members.end(); mem_iter++) {
+ f_out_ << "<thead><th>Key</th><th>Field</th><th>Type</th><th>Description</th><th>Requiredness</th><th>Default value</th></thead>"
+ << endl;
+ for ( ; mem_iter != members.end(); mem_iter++) {
f_out_ << "<tr><td>" << (*mem_iter)->get_key() << "</td><td>";
f_out_ << (*mem_iter)->get_name();
f_out_ << "</td><td>";
print_type((*mem_iter)->get_type());
f_out_ << "</td><td>";
- f_out_ << escape_html((*mem_iter)->get_doc());
+ f_out_ << escape_html( (*mem_iter)->get_doc());
f_out_ << "</td><td>";
if ((*mem_iter)->get_req() == t_field::T_OPTIONAL) {
f_out_ << "optional";
@@ -1014,7 +1024,8 @@
* @param tservice The service definition
*/
void t_html_generator::generate_service(t_service* tservice) {
- f_out_ << "<h3 id=\"Svc_" << service_name_ << "\">Service: " << service_name_ << "</h3>" << endl;
+ f_out_ << "<h3 id=\"Svc_" << service_name_ << "\">Service: "
+ << service_name_ << "</h3>" << endl;
if (tservice->get_extends()) {
f_out_ << "<div class=\"extends\"><em>extends</em> ";
@@ -1024,11 +1035,12 @@
print_doc(tservice);
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator fn_iter = functions.begin();
- for (; fn_iter != functions.end(); fn_iter++) {
+ for ( ; fn_iter != functions.end(); fn_iter++) {
string fn_name = (*fn_iter)->get_name();
f_out_ << "<div class=\"definition\">";
- f_out_ << "<h4 id=\"Fn_" << service_name_ << "_" << fn_name << "\">Function: " << service_name_
- << "." << fn_name << "</h4>" << endl;
+ f_out_ << "<h4 id=\"Fn_" << service_name_ << "_" << fn_name
+ << "\">Function: " << service_name_ << "." << fn_name
+ << "</h4>" << endl;
f_out_ << "<pre>";
int offset = print_type((*fn_iter)->get_returntype());
bool first = true;
@@ -1036,7 +1048,7 @@
offset += fn_name.size() + 2;
vector<t_field*> args = (*fn_iter)->get_arglist()->get_members();
vector<t_field*>::iterator arg_iter = args.begin();
- for (; arg_iter != args.end(); arg_iter++) {
+ for ( ; arg_iter != args.end(); arg_iter++) {
if (!first) {
f_out_ << "," << endl;
for (int i = 0; i < offset; ++i) {
@@ -1057,7 +1069,7 @@
vector<t_field*>::iterator ex_iter = excepts.begin();
if (ex_iter != excepts.end()) {
f_out_ << " throws ";
- for (; ex_iter != excepts.end(); ex_iter++) {
+ for ( ; ex_iter != excepts.end(); ex_iter++) {
if (!first) {
f_out_ << ", ";
}
@@ -1073,9 +1085,8 @@
}
}
-THRIFT_REGISTER_GENERATOR(
- html,
- "HTML",
- " standalone: Self-contained mode, includes all CSS in the HTML files.\n"
- " Generates no style.css file, but HTML files will be larger.\n"
- " noescape: Do not escape html in doc text.\n")
+THRIFT_REGISTER_GENERATOR(html, "HTML",
+" standalone: Self-contained mode, includes all CSS in the HTML files.\n"
+" Generates no style.css file, but HTML files will be larger.\n"
+" noescape: Do not escape html in doc text.\n"
+)
diff --git a/compiler/cpp/src/generate/t_html_generator.h b/compiler/cpp/src/generate/t_html_generator.h
index 600b17f..e40edd3 100644
--- a/compiler/cpp/src/generate/t_html_generator.h
+++ b/compiler/cpp/src/generate/t_html_generator.h
@@ -1,240 +1,176 @@
-#define BOOTSTRAP_CSS() \
- "/*!\n" \
- " * Bootstrap v2.0.3\n" \
- " *\n" \
- " * Copyright 2012 Twitter, Inc\n" \
- " * Licensed under the Apache License v2.0\n" \
- " * http://www.apache.org/licenses/LICENSE-2.0\n" \
- " *\n" \
- " * Designed and built with all the love in the world @twitter by @mdo and @fat.\n" \
- " */\n" \
- ".clearfix{*zoom:1;}.clearfix:before,.clearfix:after{display:table;content:\"\";}\n" \
- ".clearfix:after{clear:both;}\n" \
- ".hide-text{font:0/0 " \
- "a;color:transparent;text-shadow:none;background-color:transparent;border:0;}\n" \
- ".input-block-level{display:block;width:100%;min-height:28px;-webkit-box-sizing:border-box;-" \
- "moz-box-sizing:border-box;-ms-box-sizing:border-box;box-sizing:border-box;}\n" \
- "article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block;}\n" \
- "audio,canvas,video{display:inline-block;*display:inline;*zoom:1;}\n" \
- "audio:not([controls]){display:none;}\n" \
- "html{font-size:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;}\n" \
- "a:focus{outline:thin dotted #333;outline:5px auto " \
- "-webkit-focus-ring-color;outline-offset:-2px;}\n" \
- "a:hover,a:active{outline:0;}\n" \
- "sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline;}\n" \
- "sup{top:-0.5em;}\n" \
- "sub{bottom:-0.25em;}\n" \
- "img{max-width:100%;vertical-align:middle;border:0;-ms-interpolation-mode:bicubic;}\n" \
- "button,input,select,textarea{margin:0;font-size:100%;vertical-align:middle;}\n" \
- "button,input{*overflow:visible;line-height:normal;}\n" \
- "button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0;}\n" \
- "button,input[type=\"button\"],input[type=\"reset\"],input[type=\"submit\"]{cursor:pointer;-" \
- "webkit-appearance:button;}\n" \
- "input[type=\"search\"]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:" \
- "content-box;-webkit-appearance:textfield;}\n" \
- "input[type=\"search\"]::-webkit-search-decoration,input[type=\"search\"]::-webkit-search-" \
- "cancel-button{-webkit-appearance:none;}\n" \
- "textarea{overflow:auto;vertical-align:top;}\n" \
- "body{margin:0;font-family:\"Helvetica " \
- "Neue\",Helvetica,Arial,sans-serif;font-size:13px;line-height:18px;color:#333333;background-" \
- "color:#ffffff;}\n" \
- "a{color:#0088cc;text-decoration:none;}\n" \
- "a:hover{color:#005580;text-decoration:underline;}\n" \
- ".row{margin-left:-20px;*zoom:1;}.row:before,.row:after{display:table;content:\"\";}\n" \
- ".row:after{clear:both;}\n" \
- "[class*=\"span\"]{float:left;margin-left:20px;}\n" \
- ".container,.navbar-fixed-top .container,.navbar-fixed-bottom .container{width:940px;}\n" \
- ".span12{width:940px;}\n" \
- ".span11{width:860px;}\n" \
- ".span10{width:780px;}\n" \
- ".span9{width:700px;}\n" \
- ".span8{width:620px;}\n" \
- ".span7{width:540px;}\n" \
- ".span6{width:460px;}\n" \
- ".span5{width:380px;}\n" \
- ".span4{width:300px;}\n" \
- ".span3{width:220px;}\n" \
- ".span2{width:140px;}\n" \
- ".span1{width:60px;}\n" \
- ".offset12{margin-left:980px;}\n" \
- ".offset11{margin-left:900px;}\n" \
- ".offset10{margin-left:820px;}\n" \
- ".offset9{margin-left:740px;}\n" \
- ".offset8{margin-left:660px;}\n" \
- ".offset7{margin-left:580px;}\n" \
- ".offset6{margin-left:500px;}\n" \
- ".offset5{margin-left:420px;}\n" \
- ".offset4{margin-left:340px;}\n" \
- ".offset3{margin-left:260px;}\n" \
- ".offset2{margin-left:180px;}\n" \
- ".offset1{margin-left:100px;}\n" \
- ".row-fluid{width:100%;*zoom:1;}.row-fluid:before,.row-fluid:after{display:table;content:\"\";}" \
- "\n" \
- ".row-fluid:after{clear:both;}\n" \
- ".row-fluid " \
- "[class*=\"span\"]{display:block;width:100%;min-height:28px;-webkit-box-sizing:border-box;-moz-" \
- "box-sizing:border-box;-ms-box-sizing:border-box;box-sizing:border-box;float:left;margin-left:" \
- "2.127659574%;*margin-left:2.0744680846382977%;}\n" \
- ".row-fluid [class*=\"span\"]:first-child{margin-left:0;}\n" \
- ".row-fluid .span12{width:99.99999998999999%;*width:99.94680850063828%;}\n" \
- ".row-fluid .span11{width:91.489361693%;*width:91.4361702036383%;}\n" \
- ".row-fluid .span10{width:82.97872339599999%;*width:82.92553190663828%;}\n" \
- ".row-fluid .span9{width:74.468085099%;*width:74.4148936096383%;}\n" \
- ".row-fluid .span8{width:65.95744680199999%;*width:65.90425531263828%;}\n" \
- ".row-fluid .span7{width:57.446808505%;*width:57.3936170156383%;}\n" \
- ".row-fluid .span6{width:48.93617020799999%;*width:48.88297871863829%;}\n" \
- ".row-fluid .span5{width:40.425531911%;*width:40.3723404216383%;}\n" \
- ".row-fluid .span4{width:31.914893614%;*width:31.8617021246383%;}\n" \
- ".row-fluid .span3{width:23.404255317%;*width:23.3510638276383%;}\n" \
- ".row-fluid .span2{width:14.89361702%;*width:14.8404255306383%;}\n" \
- ".row-fluid .span1{width:6.382978723%;*width:6.329787233638298%;}\n" \
- ".container{margin-right:auto;margin-left:auto;*zoom:1;}.container:before,.container:after{" \
- "display:table;content:\"\";}\n" \
- ".container:after{clear:both;}\n" \
- ".container-fluid{padding-right:20px;padding-left:20px;*zoom:1;}.container-fluid:before,." \
- "container-fluid:after{display:table;content:\"\";}\n" \
- ".container-fluid:after{clear:both;}\n" \
- "p{margin:0 0 9px;font-family:\"Helvetica " \
- "Neue\",Helvetica,Arial,sans-serif;font-size:13px;line-height:18px;}p " \
- "small{font-size:11px;color:#999999;}\n" \
- ".lead{margin-bottom:18px;font-size:20px;font-weight:200;line-height:27px;}\n" \
- "h1,h2,h3,h4,h5,h6{margin:0;font-family:inherit;font-weight:bold;color:inherit;text-rendering:" \
- "optimizelegibility;}h1 small,h2 small,h3 small,h4 small,h5 small,h6 " \
- "small{font-weight:normal;color:#999999;}\n" \
- "h1{font-size:30px;line-height:36px;}h1 small{font-size:18px;}\n" \
- "h2{font-size:24px;line-height:36px;}h2 small{font-size:18px;}\n" \
- "h3{font-size:18px;line-height:27px;}h3 small{font-size:14px;}\n" \
- "h4,h5,h6{line-height:18px;}\n" \
- "h4{font-size:14px;}h4 small{font-size:12px;}\n" \
- "h5{font-size:12px;}\n" \
- "h6{font-size:11px;color:#999999;text-transform:uppercase;}\n" \
- ".page-header{padding-bottom:17px;margin:18px 0;border-bottom:1px solid #eeeeee;}\n" \
- ".page-header h1{line-height:1;}\n" \
- "ul,ol{padding:0;margin:0 0 9px 25px;}\n" \
- "ul ul,ul ol,ol ol,ol ul{margin-bottom:0;}\n" \
- "ul{list-style:disc;}\n" \
- "ol{list-style:decimal;}\n" \
- "li{line-height:18px;}\n" \
- "ul.unstyled,ol.unstyled{margin-left:0;list-style:none;}\n" \
- "dl{margin-bottom:18px;}\n" \
- "dt,dd{line-height:18px;}\n" \
- "dt{font-weight:bold;line-height:17px;}\n" \
- "dd{margin-left:9px;}\n" \
- ".dl-horizontal " \
- "dt{float:left;width:120px;clear:left;text-align:right;overflow:hidden;text-overflow:ellipsis;" \
- "white-space:nowrap;}\n" \
- ".dl-horizontal dd{margin-left:130px;}\n" \
- "hr{margin:18px 0;border:0;border-top:1px solid #eeeeee;border-bottom:1px solid #ffffff;}\n" \
- "strong{font-weight:bold;}\n" \
- "em{font-style:italic;}\n" \
- ".muted{color:#999999;}\n" \
- "abbr[title]{cursor:help;border-bottom:1px dotted #ddd;}\n" \
- "abbr.initialism{font-size:90%;text-transform:uppercase;}\n" \
- "blockquote{padding:0 0 0 15px;margin:0 0 18px;border-left:5px solid #eeeeee;}blockquote " \
- "p{margin-bottom:0;font-size:16px;font-weight:300;line-height:22.5px;}\n" \
- "blockquote small{display:block;line-height:18px;color:#999999;}blockquote " \
- "small:before{content:'\\2014 \\00A0';}\n" \
- "blockquote.pull-right{float:right;padding-right:15px;padding-left:0;border-right:5px solid " \
- "#eeeeee;border-left:0;}blockquote.pull-right p,blockquote.pull-right " \
- "small{text-align:right;}\n" \
- "q:before,q:after,blockquote:before,blockquote:after{content:\"\";}\n" \
- "address{display:block;margin-bottom:18px;font-style:normal;line-height:18px;}\n" \
- "small{font-size:100%;}\n" \
- "cite{font-style:normal;}\n" \
- "code,pre{padding:0 3px 2px;font-family:Menlo,Monaco,Consolas,\"Courier " \
- "New\",monospace;font-size:12px;color:#333333;-webkit-border-radius:3px;-moz-border-radius:3px;" \
- "border-radius:3px;}\n" \
- "code{padding:2px 4px;color:#d14;background-color:#f7f7f9;border:1px solid #e1e1e8;}\n" \
- "pre{display:block;padding:8.5px;margin:0 0 " \
- "9px;font-size:12.025px;line-height:18px;word-break:break-all;word-wrap:break-word;white-space:" \
- "pre;white-space:pre-wrap;background-color:#f5f5f5;border:1px solid #ccc;border:1px solid " \
- "rgba(0, 0, 0, " \
- "0.15);-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}pre.prettyprint{" \
- "margin-bottom:18px;}\n" \
- "pre code{padding:0;color:inherit;background-color:transparent;border:0;}\n" \
- ".pre-scrollable{max-height:340px;overflow-y:scroll;}\n" \
- ".label,.badge{font-size:10.998px;font-weight:bold;line-height:14px;color:#ffffff;vertical-" \
- "align:baseline;white-space:nowrap;text-shadow:0 -1px 0 rgba(0, 0, 0, " \
- "0.25);background-color:#999999;}\n" \
- ".label{padding:1px 4px " \
- "2px;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;}\n" \
- ".badge{padding:1px 9px " \
- "2px;-webkit-border-radius:9px;-moz-border-radius:9px;border-radius:9px;}\n" \
- "a.label:hover,a.badge:hover{color:#ffffff;text-decoration:none;cursor:pointer;}\n" \
- ".label-important,.badge-important{background-color:#b94a48;}\n" \
- ".label-important[href],.badge-important[href]{background-color:#953b39;}\n" \
- ".label-warning,.badge-warning{background-color:#f89406;}\n" \
- ".label-warning[href],.badge-warning[href]{background-color:#c67605;}\n" \
- ".label-success,.badge-success{background-color:#468847;}\n" \
- ".label-success[href],.badge-success[href]{background-color:#356635;}\n" \
- ".label-info,.badge-info{background-color:#3a87ad;}\n" \
- ".label-info[href],.badge-info[href]{background-color:#2d6987;}\n" \
- ".label-inverse,.badge-inverse{background-color:#333333;}\n" \
- ".label-inverse[href],.badge-inverse[href]{background-color:#1a1a1a;}\n" \
- "table{max-width:100%;background-color:transparent;border-collapse:collapse;border-spacing:0;}" \
- "\n" \
- ".table{width:100%;margin-bottom:18px;}.table th,.table " \
- "td{padding:8px;line-height:18px;text-align:left;vertical-align:top;border-top:1px solid " \
- "#dddddd;}\n" \
- ".table th{font-weight:bold;}\n" \
- ".table thead th{vertical-align:bottom;}\n" \
- ".table caption+thead tr:first-child th,.table caption+thead tr:first-child td,.table " \
- "colgroup+thead tr:first-child th,.table colgroup+thead tr:first-child td,.table " \
- "thead:first-child tr:first-child th,.table thead:first-child tr:first-child " \
- "td{border-top:0;}\n" \
- ".table tbody+tbody{border-top:2px solid #dddddd;}\n" \
- ".table-condensed th,.table-condensed td{padding:4px 5px;}\n" \
- ".table-bordered{border:1px solid " \
- "#dddddd;border-collapse:separate;*border-collapse:collapsed;border-left:0;-webkit-border-" \
- "radius:4px;-moz-border-radius:4px;border-radius:4px;}.table-bordered th,.table-bordered " \
- "td{border-left:1px solid #dddddd;}\n" \
- ".table-bordered caption+thead tr:first-child th,.table-bordered caption+tbody tr:first-child " \
- "th,.table-bordered caption+tbody tr:first-child td,.table-bordered colgroup+thead " \
- "tr:first-child th,.table-bordered colgroup+tbody tr:first-child th,.table-bordered " \
- "colgroup+tbody tr:first-child td,.table-bordered thead:first-child tr:first-child " \
- "th,.table-bordered tbody:first-child tr:first-child th,.table-bordered tbody:first-child " \
- "tr:first-child td{border-top:0;}\n" \
- ".table-bordered thead:first-child tr:first-child th:first-child,.table-bordered " \
- "tbody:first-child tr:first-child " \
- "td:first-child{-webkit-border-top-left-radius:4px;border-top-left-radius:4px;-moz-border-" \
- "radius-topleft:4px;}\n" \
- ".table-bordered thead:first-child tr:first-child th:last-child,.table-bordered " \
- "tbody:first-child tr:first-child " \
- "td:last-child{-webkit-border-top-right-radius:4px;border-top-right-radius:4px;-moz-border-" \
- "radius-topright:4px;}\n" \
- ".table-bordered thead:last-child tr:last-child th:first-child,.table-bordered " \
- "tbody:last-child tr:last-child td:first-child{-webkit-border-radius:0 0 0 " \
- "4px;-moz-border-radius:0 0 0 4px;border-radius:0 0 0 " \
- "4px;-webkit-border-bottom-left-radius:4px;border-bottom-left-radius:4px;-moz-border-radius-" \
- "bottomleft:4px;}\n" \
- ".table-bordered thead:last-child tr:last-child th:last-child,.table-bordered tbody:last-child " \
- "tr:last-child " \
- "td:last-child{-webkit-border-bottom-right-radius:4px;border-bottom-right-radius:4px;-moz-" \
- "border-radius-bottomright:4px;}\n" \
- ".table-striped tbody tr:nth-child(odd) td,.table-striped tbody tr:nth-child(odd) " \
- "th{background-color:#f9f9f9;}\n" \
- ".table tbody tr:hover td,.table tbody tr:hover th{background-color:#f5f5f5;}\n" \
- "table .span1{float:none;width:44px;margin-left:0;}\n" \
- "table .span2{float:none;width:124px;margin-left:0;}\n" \
- "table .span3{float:none;width:204px;margin-left:0;}\n" \
- "table .span4{float:none;width:284px;margin-left:0;}\n" \
- "table .span5{float:none;width:364px;margin-left:0;}\n" \
- "table .span6{float:none;width:444px;margin-left:0;}\n" \
- "table .span7{float:none;width:524px;margin-left:0;}\n" \
- "table .span8{float:none;width:604px;margin-left:0;}\n" \
- "table .span9{float:none;width:684px;margin-left:0;}\n" \
- "table .span10{float:none;width:764px;margin-left:0;}\n" \
- "table .span11{float:none;width:844px;margin-left:0;}\n" \
- "table .span12{float:none;width:924px;margin-left:0;}\n" \
- "table .span13{float:none;width:1004px;margin-left:0;}\n" \
- "table .span14{float:none;width:1084px;margin-left:0;}\n" \
- "table .span15{float:none;width:1164px;margin-left:0;}\n" \
- "table .span16{float:none;width:1244px;margin-left:0;}\n" \
- "table .span17{float:none;width:1324px;margin-left:0;}\n" \
- "table .span18{float:none;width:1404px;margin-left:0;}\n" \
- "table .span19{float:none;width:1484px;margin-left:0;}\n" \
- "table .span20{float:none;width:1564px;margin-left:0;}\n" \
- "table .span21{float:none;width:1644px;margin-left:0;}\n" \
- "table .span22{float:none;width:1724px;margin-left:0;}\n" \
- "table .span23{float:none;width:1804px;margin-left:0;}\n" \
- "table .span24{float:none;width:1884px;margin-left:0;}"
+#define BOOTSTRAP_CSS() "/*!\n"\
+" * Bootstrap v2.0.3\n"\
+" *\n"\
+" * Copyright 2012 Twitter, Inc\n"\
+" * Licensed under the Apache License v2.0\n"\
+" * http://www.apache.org/licenses/LICENSE-2.0\n"\
+" *\n"\
+" * Designed and built with all the love in the world @twitter by @mdo and @fat.\n"\
+" */\n"\
+".clearfix{*zoom:1;}.clearfix:before,.clearfix:after{display:table;content:\"\";}\n"\
+".clearfix:after{clear:both;}\n"\
+".hide-text{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0;}\n"\
+".input-block-level{display:block;width:100%;min-height:28px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;-ms-box-sizing:border-box;box-sizing:border-box;}\n"\
+"article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block;}\n"\
+"audio,canvas,video{display:inline-block;*display:inline;*zoom:1;}\n"\
+"audio:not([controls]){display:none;}\n"\
+"html{font-size:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;}\n"\
+"a:focus{outline:thin dotted #333;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px;}\n"\
+"a:hover,a:active{outline:0;}\n"\
+"sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline;}\n"\
+"sup{top:-0.5em;}\n"\
+"sub{bottom:-0.25em;}\n"\
+"img{max-width:100%;vertical-align:middle;border:0;-ms-interpolation-mode:bicubic;}\n"\
+"button,input,select,textarea{margin:0;font-size:100%;vertical-align:middle;}\n"\
+"button,input{*overflow:visible;line-height:normal;}\n"\
+"button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0;}\n"\
+"button,input[type=\"button\"],input[type=\"reset\"],input[type=\"submit\"]{cursor:pointer;-webkit-appearance:button;}\n"\
+"input[type=\"search\"]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield;}\n"\
+"input[type=\"search\"]::-webkit-search-decoration,input[type=\"search\"]::-webkit-search-cancel-button{-webkit-appearance:none;}\n"\
+"textarea{overflow:auto;vertical-align:top;}\n"\
+"body{margin:0;font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:13px;line-height:18px;color:#333333;background-color:#ffffff;}\n"\
+"a{color:#0088cc;text-decoration:none;}\n"\
+"a:hover{color:#005580;text-decoration:underline;}\n"\
+".row{margin-left:-20px;*zoom:1;}.row:before,.row:after{display:table;content:\"\";}\n"\
+".row:after{clear:both;}\n"\
+"[class*=\"span\"]{float:left;margin-left:20px;}\n"\
+".container,.navbar-fixed-top .container,.navbar-fixed-bottom .container{width:940px;}\n"\
+".span12{width:940px;}\n"\
+".span11{width:860px;}\n"\
+".span10{width:780px;}\n"\
+".span9{width:700px;}\n"\
+".span8{width:620px;}\n"\
+".span7{width:540px;}\n"\
+".span6{width:460px;}\n"\
+".span5{width:380px;}\n"\
+".span4{width:300px;}\n"\
+".span3{width:220px;}\n"\
+".span2{width:140px;}\n"\
+".span1{width:60px;}\n"\
+".offset12{margin-left:980px;}\n"\
+".offset11{margin-left:900px;}\n"\
+".offset10{margin-left:820px;}\n"\
+".offset9{margin-left:740px;}\n"\
+".offset8{margin-left:660px;}\n"\
+".offset7{margin-left:580px;}\n"\
+".offset6{margin-left:500px;}\n"\
+".offset5{margin-left:420px;}\n"\
+".offset4{margin-left:340px;}\n"\
+".offset3{margin-left:260px;}\n"\
+".offset2{margin-left:180px;}\n"\
+".offset1{margin-left:100px;}\n"\
+".row-fluid{width:100%;*zoom:1;}.row-fluid:before,.row-fluid:after{display:table;content:\"\";}\n"\
+".row-fluid:after{clear:both;}\n"\
+".row-fluid [class*=\"span\"]{display:block;width:100%;min-height:28px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;-ms-box-sizing:border-box;box-sizing:border-box;float:left;margin-left:2.127659574%;*margin-left:2.0744680846382977%;}\n"\
+".row-fluid [class*=\"span\"]:first-child{margin-left:0;}\n"\
+".row-fluid .span12{width:99.99999998999999%;*width:99.94680850063828%;}\n"\
+".row-fluid .span11{width:91.489361693%;*width:91.4361702036383%;}\n"\
+".row-fluid .span10{width:82.97872339599999%;*width:82.92553190663828%;}\n"\
+".row-fluid .span9{width:74.468085099%;*width:74.4148936096383%;}\n"\
+".row-fluid .span8{width:65.95744680199999%;*width:65.90425531263828%;}\n"\
+".row-fluid .span7{width:57.446808505%;*width:57.3936170156383%;}\n"\
+".row-fluid .span6{width:48.93617020799999%;*width:48.88297871863829%;}\n"\
+".row-fluid .span5{width:40.425531911%;*width:40.3723404216383%;}\n"\
+".row-fluid .span4{width:31.914893614%;*width:31.8617021246383%;}\n"\
+".row-fluid .span3{width:23.404255317%;*width:23.3510638276383%;}\n"\
+".row-fluid .span2{width:14.89361702%;*width:14.8404255306383%;}\n"\
+".row-fluid .span1{width:6.382978723%;*width:6.329787233638298%;}\n"\
+".container{margin-right:auto;margin-left:auto;*zoom:1;}.container:before,.container:after{display:table;content:\"\";}\n"\
+".container:after{clear:both;}\n"\
+".container-fluid{padding-right:20px;padding-left:20px;*zoom:1;}.container-fluid:before,.container-fluid:after{display:table;content:\"\";}\n"\
+".container-fluid:after{clear:both;}\n"\
+"p{margin:0 0 9px;font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:13px;line-height:18px;}p small{font-size:11px;color:#999999;}\n"\
+".lead{margin-bottom:18px;font-size:20px;font-weight:200;line-height:27px;}\n"\
+"h1,h2,h3,h4,h5,h6{margin:0;font-family:inherit;font-weight:bold;color:inherit;text-rendering:optimizelegibility;}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{font-weight:normal;color:#999999;}\n"\
+"h1{font-size:30px;line-height:36px;}h1 small{font-size:18px;}\n"\
+"h2{font-size:24px;line-height:36px;}h2 small{font-size:18px;}\n"\
+"h3{font-size:18px;line-height:27px;}h3 small{font-size:14px;}\n"\
+"h4,h5,h6{line-height:18px;}\n"\
+"h4{font-size:14px;}h4 small{font-size:12px;}\n"\
+"h5{font-size:12px;}\n"\
+"h6{font-size:11px;color:#999999;text-transform:uppercase;}\n"\
+".page-header{padding-bottom:17px;margin:18px 0;border-bottom:1px solid #eeeeee;}\n"\
+".page-header h1{line-height:1;}\n"\
+"ul,ol{padding:0;margin:0 0 9px 25px;}\n"\
+"ul ul,ul ol,ol ol,ol ul{margin-bottom:0;}\n"\
+"ul{list-style:disc;}\n"\
+"ol{list-style:decimal;}\n"\
+"li{line-height:18px;}\n"\
+"ul.unstyled,ol.unstyled{margin-left:0;list-style:none;}\n"\
+"dl{margin-bottom:18px;}\n"\
+"dt,dd{line-height:18px;}\n"\
+"dt{font-weight:bold;line-height:17px;}\n"\
+"dd{margin-left:9px;}\n"\
+".dl-horizontal dt{float:left;width:120px;clear:left;text-align:right;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}\n"\
+".dl-horizontal dd{margin-left:130px;}\n"\
+"hr{margin:18px 0;border:0;border-top:1px solid #eeeeee;border-bottom:1px solid #ffffff;}\n"\
+"strong{font-weight:bold;}\n"\
+"em{font-style:italic;}\n"\
+".muted{color:#999999;}\n"\
+"abbr[title]{cursor:help;border-bottom:1px dotted #ddd;}\n"\
+"abbr.initialism{font-size:90%;text-transform:uppercase;}\n"\
+"blockquote{padding:0 0 0 15px;margin:0 0 18px;border-left:5px solid #eeeeee;}blockquote p{margin-bottom:0;font-size:16px;font-weight:300;line-height:22.5px;}\n"\
+"blockquote small{display:block;line-height:18px;color:#999999;}blockquote small:before{content:'\\2014 \\00A0';}\n"\
+"blockquote.pull-right{float:right;padding-right:15px;padding-left:0;border-right:5px solid #eeeeee;border-left:0;}blockquote.pull-right p,blockquote.pull-right small{text-align:right;}\n"\
+"q:before,q:after,blockquote:before,blockquote:after{content:\"\";}\n"\
+"address{display:block;margin-bottom:18px;font-style:normal;line-height:18px;}\n"\
+"small{font-size:100%;}\n"\
+"cite{font-style:normal;}\n"\
+"code,pre{padding:0 3px 2px;font-family:Menlo,Monaco,Consolas,\"Courier New\",monospace;font-size:12px;color:#333333;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;}\n"\
+"code{padding:2px 4px;color:#d14;background-color:#f7f7f9;border:1px solid #e1e1e8;}\n"\
+"pre{display:block;padding:8.5px;margin:0 0 9px;font-size:12.025px;line-height:18px;word-break:break-all;word-wrap:break-word;white-space:pre;white-space:pre-wrap;background-color:#f5f5f5;border:1px solid #ccc;border:1px solid rgba(0, 0, 0, 0.15);-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}pre.prettyprint{margin-bottom:18px;}\n"\
+"pre code{padding:0;color:inherit;background-color:transparent;border:0;}\n"\
+".pre-scrollable{max-height:340px;overflow-y:scroll;}\n"\
+".label,.badge{font-size:10.998px;font-weight:bold;line-height:14px;color:#ffffff;vertical-align:baseline;white-space:nowrap;text-shadow:0 -1px 0 rgba(0, 0, 0, 0.25);background-color:#999999;}\n"\
+".label{padding:1px 4px 2px;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;}\n"\
+".badge{padding:1px 9px 2px;-webkit-border-radius:9px;-moz-border-radius:9px;border-radius:9px;}\n"\
+"a.label:hover,a.badge:hover{color:#ffffff;text-decoration:none;cursor:pointer;}\n"\
+".label-important,.badge-important{background-color:#b94a48;}\n"\
+".label-important[href],.badge-important[href]{background-color:#953b39;}\n"\
+".label-warning,.badge-warning{background-color:#f89406;}\n"\
+".label-warning[href],.badge-warning[href]{background-color:#c67605;}\n"\
+".label-success,.badge-success{background-color:#468847;}\n"\
+".label-success[href],.badge-success[href]{background-color:#356635;}\n"\
+".label-info,.badge-info{background-color:#3a87ad;}\n"\
+".label-info[href],.badge-info[href]{background-color:#2d6987;}\n"\
+".label-inverse,.badge-inverse{background-color:#333333;}\n"\
+".label-inverse[href],.badge-inverse[href]{background-color:#1a1a1a;}\n"\
+"table{max-width:100%;background-color:transparent;border-collapse:collapse;border-spacing:0;}\n"\
+".table{width:100%;margin-bottom:18px;}.table th,.table td{padding:8px;line-height:18px;text-align:left;vertical-align:top;border-top:1px solid #dddddd;}\n"\
+".table th{font-weight:bold;}\n"\
+".table thead th{vertical-align:bottom;}\n"\
+".table caption+thead tr:first-child th,.table caption+thead tr:first-child td,.table colgroup+thead tr:first-child th,.table colgroup+thead tr:first-child td,.table thead:first-child tr:first-child th,.table thead:first-child tr:first-child td{border-top:0;}\n"\
+".table tbody+tbody{border-top:2px solid #dddddd;}\n"\
+".table-condensed th,.table-condensed td{padding:4px 5px;}\n"\
+".table-bordered{border:1px solid #dddddd;border-collapse:separate;*border-collapse:collapsed;border-left:0;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}.table-bordered th,.table-bordered td{border-left:1px solid #dddddd;}\n"\
+".table-bordered caption+thead tr:first-child th,.table-bordered caption+tbody tr:first-child th,.table-bordered caption+tbody tr:first-child td,.table-bordered colgroup+thead tr:first-child th,.table-bordered colgroup+tbody tr:first-child th,.table-bordered colgroup+tbody tr:first-child td,.table-bordered thead:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child th,.table-bordered tbody:first-child tr:first-child td{border-top:0;}\n"\
+".table-bordered thead:first-child tr:first-child th:first-child,.table-bordered tbody:first-child tr:first-child td:first-child{-webkit-border-top-left-radius:4px;border-top-left-radius:4px;-moz-border-radius-topleft:4px;}\n"\
+".table-bordered thead:first-child tr:first-child th:last-child,.table-bordered tbody:first-child tr:first-child td:last-child{-webkit-border-top-right-radius:4px;border-top-right-radius:4px;-moz-border-radius-topright:4px;}\n"\
+".table-bordered thead:last-child tr:last-child th:first-child,.table-bordered tbody:last-child tr:last-child td:first-child{-webkit-border-radius:0 0 0 4px;-moz-border-radius:0 0 0 4px;border-radius:0 0 0 4px;-webkit-border-bottom-left-radius:4px;border-bottom-left-radius:4px;-moz-border-radius-bottomleft:4px;}\n"\
+".table-bordered thead:last-child tr:last-child th:last-child,.table-bordered tbody:last-child tr:last-child td:last-child{-webkit-border-bottom-right-radius:4px;border-bottom-right-radius:4px;-moz-border-radius-bottomright:4px;}\n"\
+".table-striped tbody tr:nth-child(odd) td,.table-striped tbody tr:nth-child(odd) th{background-color:#f9f9f9;}\n"\
+".table tbody tr:hover td,.table tbody tr:hover th{background-color:#f5f5f5;}\n"\
+"table .span1{float:none;width:44px;margin-left:0;}\n"\
+"table .span2{float:none;width:124px;margin-left:0;}\n"\
+"table .span3{float:none;width:204px;margin-left:0;}\n"\
+"table .span4{float:none;width:284px;margin-left:0;}\n"\
+"table .span5{float:none;width:364px;margin-left:0;}\n"\
+"table .span6{float:none;width:444px;margin-left:0;}\n"\
+"table .span7{float:none;width:524px;margin-left:0;}\n"\
+"table .span8{float:none;width:604px;margin-left:0;}\n"\
+"table .span9{float:none;width:684px;margin-left:0;}\n"\
+"table .span10{float:none;width:764px;margin-left:0;}\n"\
+"table .span11{float:none;width:844px;margin-left:0;}\n"\
+"table .span12{float:none;width:924px;margin-left:0;}\n"\
+"table .span13{float:none;width:1004px;margin-left:0;}\n"\
+"table .span14{float:none;width:1084px;margin-left:0;}\n"\
+"table .span15{float:none;width:1164px;margin-left:0;}\n"\
+"table .span16{float:none;width:1244px;margin-left:0;}\n"\
+"table .span17{float:none;width:1324px;margin-left:0;}\n"\
+"table .span18{float:none;width:1404px;margin-left:0;}\n"\
+"table .span19{float:none;width:1484px;margin-left:0;}\n"\
+"table .span20{float:none;width:1564px;margin-left:0;}\n"\
+"table .span21{float:none;width:1644px;margin-left:0;}\n"\
+"table .span22{float:none;width:1724px;margin-left:0;}\n"\
+"table .span23{float:none;width:1804px;margin-left:0;}\n"\
+"table .span24{float:none;width:1884px;margin-left:0;}"
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 4e5b3ef..4e2046f 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -40,7 +40,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Java code generator.
@@ -48,17 +48,19 @@
*/
class t_java_generator : public t_oop_generator {
public:
- t_java_generator(t_program* program,
+ t_java_generator(
+ t_program* program,
const std::map<std::string, std::string>& parsed_options,
const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("beans");
bean_style_ = (iter != parsed_options.end());
- iter = parsed_options.find("android");
+ iter = parsed_options.find("android");
android_style_ = (iter != parsed_options.end());
iter = parsed_options.find("private-members");
@@ -101,19 +103,14 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_union(t_struct* tunion);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_union (t_struct* tunion);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
- void print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval = false);
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
std::string render_const_value(std::ofstream& out, t_type* type, t_const_value* value);
/**
@@ -122,11 +119,7 @@
void generate_java_struct(t_struct* tstruct, bool is_exception);
- void generate_java_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool in_class = false,
- bool is_result = false);
+ void generate_java_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
void generate_java_struct_parcelable(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_equality(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_compare_to(std::ofstream& out, t_struct* tstruct);
@@ -142,20 +135,14 @@
void generate_field_value_meta_data(std::ofstream& out, t_type* type);
std::string get_java_type_string(t_type* type);
void generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct);
- void generate_reflection_setters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
- void generate_reflection_getters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
+ void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
void generate_generic_isset_method(std::ofstream& out, t_struct* tstruct);
void generate_java_bean_boilerplate(std::ofstream& out, t_struct* tstruct);
void generate_function_helpers(t_function* tfunction);
- std::string as_camel_case(std::string name, bool ucfirst = true);
+ std::string as_camel_case(std::string name, bool ucfirst=true);
std::string get_rpc_method_name(std::string name);
std::string get_cap_name(std::string name);
std::string generate_isset_check(t_field* field);
@@ -163,15 +150,16 @@
void generate_isset_set(ofstream& out, t_field* field, std::string prefix);
std::string isset_field_id(t_field* field);
- void generate_service_interface(t_service* tservice);
+ void generate_service_interface (t_service* tservice);
void generate_service_async_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
void generate_service_async_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_service_async_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
- void generate_process_async_function(t_service* tservice, t_function* tfunction);
+ void generate_service_server (t_service* tservice);
+ void generate_service_async_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
+ void generate_process_async_function (t_service* tservice, t_function* tfunction);
+
void generate_java_union(t_struct* tstruct);
void generate_union_constructor(ofstream& out, t_struct* tstruct);
@@ -204,76 +192,73 @@
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool has_metadata = true);
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="",
+ bool has_metadata = true);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out,
- t_type* ttype,
- std::string prefix = "",
- bool has_metadata = true);
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="",
+ bool has_metadata = true);
- void generate_deserialize_set_element(std::ofstream& out,
- t_set* tset,
- std::string prefix = "",
- std::string obj = "",
- bool has_metadata = true);
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="",
+ std::string obj="",
+ bool has_metadata = true);
- void generate_deserialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string prefix = "",
- std::string obj = "",
- bool has_metadata = true);
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="",
+ std::string obj="",
+ bool has_metadata = true);
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "",
- std::string obj = "",
- bool has_metadata = true);
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="",
+ std::string obj="",
+ bool has_metadata = true);
- void generate_serialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool has_metadata = true);
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="",
+ bool has_metadata = true);
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out,
- t_type* ttype,
- std::string prefix = "",
- bool has_metadata = true);
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="",
+ bool has_metadata = true);
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map,
- bool has_metadata = true);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map,
+ bool has_metadata = true);
- void generate_serialize_set_element(std::ofstream& out,
- t_set* tmap,
- std::string iter,
- bool has_metadata = true);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter,
+ bool has_metadata = true);
- void generate_serialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string iter,
- bool has_metadata = true);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter,
+ bool has_metadata = true);
- void generate_deep_copy_container(std::ofstream& out,
- std::string source_name_p1,
- std::string source_name_p2,
- std::string result_name,
- t_type* type);
- void generate_deep_copy_non_container(std::ofstream& out,
- std::string source_name,
- std::string dest_name,
- t_type* type);
+ void generate_deep_copy_container(std::ofstream& out, std::string source_name_p1, std::string source_name_p2, std::string result_name, t_type* type);
+ void generate_deep_copy_non_container(std::ofstream& out, std::string source_name, std::string dest_name, t_type* type);
enum isset_type { ISSET_NONE, ISSET_PRIMITIVE, ISSET_BITSET };
- isset_type needs_isset(t_struct* tstruct, std::string* outPrimitiveType = NULL);
+ isset_type needs_isset(t_struct* tstruct, std::string *outPrimitiveType = NULL);
/**
* Helper rendering functions
@@ -282,43 +267,37 @@
std::string java_package();
std::string java_type_imports();
std::string java_suppressions();
- std::string type_name(t_type* ttype,
- bool in_container = false,
- bool in_init = false,
- bool skip_generic = false,
- bool force_namespace = false);
- std::string base_type_name(t_base_type* tbase, bool in_container = false);
- std::string declare_field(t_field* tfield, bool init = false, bool comment = false);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
- std::string function_signature_async(t_function* tfunction,
- bool use_base_method = false,
- std::string prefix = "");
+ std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false, bool skip_generic=false, bool force_namespace = false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false);
+ std::string declare_field(t_field* tfield, bool init=false, bool comment=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string function_signature_async(t_function* tfunction, bool use_base_method = false, std::string prefix="");
std::string argument_list(t_struct* tstruct, bool include_types = true);
- std::string async_function_call_arglist(t_function* tfunc,
- bool use_base_method = true,
- bool include_types = true);
- std::string async_argument_list(t_function* tfunct,
- t_struct* tstruct,
- t_type* ttype,
- bool include_types = false);
+ std::string async_function_call_arglist(t_function* tfunc, bool use_base_method = true, bool include_types = true);
+ std::string async_argument_list(t_function* tfunct, t_struct* tstruct, t_type* ttype, bool include_types=false);
std::string type_to_enum(t_type* ttype);
void generate_struct_desc(ofstream& out, t_struct* tstruct);
void generate_field_descs(ofstream& out, t_struct* tstruct);
void generate_field_name_constants(ofstream& out, t_struct* tstruct);
- std::string make_valid_java_filename(std::string const& fromName);
- std::string make_valid_java_identifier(std::string const& fromName);
+ std::string make_valid_java_filename( std::string const & fromName);
+ std::string make_valid_java_identifier( std::string const & fromName);
bool type_can_be_null(t_type* ttype) {
ttype = get_true_type(ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception() || ttype->is_string()
- || ttype->is_enum();
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string() ||
+ ttype->is_enum();
}
std::string constant_name(std::string name);
-private:
+ private:
+
/**
* File streams
*/
@@ -338,6 +317,7 @@
bool reuse_objects_;
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -355,7 +335,7 @@
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
if (dir.size() > 0) {
subdir = subdir + "/" + dir;
@@ -386,25 +366,40 @@
string hash_builder;
string tree_set_and_map;
if (sorted_containers_) {
- tree_set_and_map = string() + "import java.util.TreeSet;\n" + "import java.util.TreeMap;\n";
+ tree_set_and_map = string() +
+ "import java.util.TreeSet;\n" +
+ "import java.util.TreeMap;\n";
}
- return string() + hash_builder + "import org.apache.thrift.scheme.IScheme;\n"
- + "import org.apache.thrift.scheme.SchemeFactory;\n"
- + "import org.apache.thrift.scheme.StandardScheme;\n\n"
- + "import org.apache.thrift.scheme.TupleScheme;\n"
- + "import org.apache.thrift.protocol.TTupleProtocol;\n"
- + "import org.apache.thrift.protocol.TProtocolException;\n"
- + "import org.apache.thrift.EncodingUtils;\n" + "import org.apache.thrift.TException;\n"
- + "import org.apache.thrift.async.AsyncMethodCallback;\n"
- + "import org.apache.thrift.server.AbstractNonblockingServer.*;\n"
- + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "import java.util.Map;\n"
- + "import java.util.HashMap;\n" + "import java.util.EnumMap;\n" + "import java.util.Set;\n"
- + "import java.util.HashSet;\n" + "import java.util.EnumSet;\n" + tree_set_and_map
- + "import java.util.Collections;\n" + "import java.util.BitSet;\n"
- + "import java.nio.ByteBuffer;\n"
- "import java.util.Arrays;\n" + "import javax.annotation.Generated;\n"
- + "import org.slf4j.Logger;\n" + "import org.slf4j.LoggerFactory;\n\n";
+ return
+ string() +
+ hash_builder +
+ "import org.apache.thrift.scheme.IScheme;\n" +
+ "import org.apache.thrift.scheme.SchemeFactory;\n" +
+ "import org.apache.thrift.scheme.StandardScheme;\n\n" +
+ "import org.apache.thrift.scheme.TupleScheme;\n" +
+ "import org.apache.thrift.protocol.TTupleProtocol;\n" +
+ "import org.apache.thrift.protocol.TProtocolException;\n" +
+ "import org.apache.thrift.EncodingUtils;\n" +
+ "import org.apache.thrift.TException;\n" +
+ "import org.apache.thrift.async.AsyncMethodCallback;\n"+
+ "import org.apache.thrift.server.AbstractNonblockingServer.*;\n"+
+ "import java.util.List;\n" +
+ "import java.util.ArrayList;\n" +
+ "import java.util.Map;\n" +
+ "import java.util.HashMap;\n" +
+ "import java.util.EnumMap;\n" +
+ "import java.util.Set;\n" +
+ "import java.util.HashSet;\n" +
+ "import java.util.EnumSet;\n" +
+ tree_set_and_map +
+ "import java.util.Collections;\n" +
+ "import java.util.BitSet;\n" +
+ "import java.nio.ByteBuffer;\n"
+ "import java.util.Arrays;\n" +
+ "import javax.annotation.Generated;\n" +
+ "import org.slf4j.Logger;\n" +
+ "import org.slf4j.LoggerFactory;\n\n";
}
string t_java_generator::java_suppressions() {
@@ -414,8 +409,7 @@
/**
* Nothing in Java
*/
-void t_java_generator::close_generator() {
-}
+void t_java_generator::close_generator() {}
/**
* Generates a typedef. This is not done in Java, since it does
@@ -425,7 +419,7 @@
* @param ttypedef The type definition
*/
void t_java_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -435,19 +429,24 @@
*/
void t_java_generator::generate_enum(t_enum* tenum) {
// Make output file
- string f_enum_name = package_dir_ + "/" + make_valid_java_filename(tenum->get_name()) + ".java";
+ string f_enum_name = package_dir_+"/"+make_valid_java_filename(tenum->get_name())+".java";
ofstream f_enum;
f_enum.open(f_enum_name.c_str());
// Comment and package it
- f_enum << autogen_comment() << java_package() << endl;
+ f_enum <<
+ autogen_comment() <<
+ java_package() << endl;
// Add java imports
- f_enum << string() + "import java.util.Map;\n" + "import java.util.HashMap;\n"
- + "import org.apache.thrift.TEnum;" << endl << endl;
+ f_enum << string() +
+ "import java.util.Map;\n" +
+ "import java.util.HashMap;\n" +
+ "import org.apache.thrift.TEnum;" << endl << endl;
generate_java_doc(f_enum, tenum);
- indent(f_enum) << "public enum " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
+ indent(f_enum) <<
+ "public enum " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
scope_up(f_enum);
vector<t_enum_value*> constants = tenum->get_constants();
@@ -471,23 +470,21 @@
indent(f_enum) << "private final int value;" << endl << endl;
indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl;
- indent(f_enum) << " this.value = value;" << endl;
+ indent(f_enum) << " this.value = value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
- indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL."
- << endl;
+ indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " */" << endl;
indent(f_enum) << "public int getValue() {" << endl;
- indent(f_enum) << " return value;" << endl;
+ indent(f_enum) << " return value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
- indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL."
- << endl;
+ indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " * @return null if the value is not found." << endl;
indent(f_enum) << " */" << endl;
- indent(f_enum) << "public static " + tenum->get_name() + " findByValue(int value) { " << endl;
+ indent(f_enum) << "public static "+ tenum->get_name() + " findByValue(int value) { " << endl;
indent_up();
@@ -524,16 +521,20 @@
return;
}
- string f_consts_name = package_dir_ + '/' + make_valid_java_filename(program_name_)
- + "Constants.java";
+ string f_consts_name = package_dir_+ '/' + make_valid_java_filename(program_name_) + "Constants.java";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
// Print header
- f_consts << autogen_comment() << java_package() << java_type_imports() << java_suppressions();
+ f_consts <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_suppressions();
- f_consts << "public class " << make_valid_java_identifier(program_name_) << "Constants {" << endl
- << endl;
+ f_consts <<
+ "public class " << make_valid_java_identifier(program_name_) << "Constants {" << endl <<
+ endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
@@ -549,22 +550,20 @@
f_consts.close();
}
+
/**
* Prints the value of a constant with the given type. Note that type checking
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-void t_java_generator::print_const_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval) {
+void t_java_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
type = get_true_type(type);
indent(out);
if (!defval) {
- out << (in_static ? "" : "public static final ") << type_name(type) << " ";
+ out <<
+ (in_static ? "" : "public static final ") <<
+ type_name(type) << " ";
}
if (type->is_base_type()) {
string v2 = render_const_value(out, type, value);
@@ -656,37 +655,36 @@
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- render << '"' << get_escaped_string(value) << '"';
- break;
- case t_base_type::TYPE_BOOL:
- render << ((value->get_integer() > 0) ? "true" : "false");
- break;
- case t_base_type::TYPE_BYTE:
- render << "(byte)" << value->get_integer();
- break;
- case t_base_type::TYPE_I16:
- render << "(short)" << value->get_integer();
- break;
- case t_base_type::TYPE_I32:
- render << value->get_integer();
- break;
- case t_base_type::TYPE_I64:
- render << value->get_integer() << "L";
- break;
- case t_base_type::TYPE_DOUBLE:
- if (value->get_type() == t_const_value::CV_INTEGER) {
- render << "(double)" << value->get_integer();
- } else {
- render << value->get_double();
- }
- break;
- default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_STRING:
+ render << '"' << get_escaped_string(value) << '"';
+ break;
+ case t_base_type::TYPE_BOOL:
+ render << ((value->get_integer() > 0) ? "true" : "false");
+ break;
+ case t_base_type::TYPE_BYTE:
+ render << "(byte)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I16:
+ render << "(short)" << value->get_integer();
+ break;
+ case t_base_type::TYPE_I32:
+ render << value->get_integer();
+ break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "L";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ if (value->get_type() == t_const_value::CV_INTEGER) {
+ render << "(double)" << value->get_integer();
+ } else {
+ render << value->get_double();
+ }
+ break;
+ default:
+ throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
- render << type->get_program()->get_namespace("java") << "."
- << value->get_identifier_with_parent();
+ render << type->get_program()->get_namespace("java") << "." << value->get_identifier_with_parent();
} else {
string t = tmp("tmp");
print_const_value(out, t, type, value, true);
@@ -719,21 +717,28 @@
generate_java_struct(txception, true);
}
+
/**
* Java struct definition.
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct(t_struct* tstruct, bool is_exception) {
+void t_java_generator::generate_java_struct(t_struct* tstruct,
+ bool is_exception) {
// Make output file
- string f_struct_name = package_dir_ + "/" + make_valid_java_filename(tstruct->get_name())
- + ".java";
+ string f_struct_name = package_dir_+"/"+make_valid_java_filename(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << java_package() << java_type_imports() << java_suppressions();
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_suppressions();
- generate_java_struct_definition(f_struct, tstruct, is_exception);
+ generate_java_struct_definition(f_struct,
+ tstruct,
+ is_exception);
f_struct.close();
}
@@ -744,20 +749,23 @@
*/
void t_java_generator::generate_java_union(t_struct* tstruct) {
// Make output file
- string f_struct_name = package_dir_ + "/" + make_valid_java_filename(tstruct->get_name())
- + ".java";
+ string f_struct_name = package_dir_+"/"+make_valid_java_filename(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << java_package() << java_type_imports() << java_suppressions();
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_suppressions();
generate_java_doc(f_struct, tstruct);
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- indent(f_struct) << "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
- << " extends org.apache.thrift.TUnion<" << tstruct->get_name() << ", "
- << tstruct->get_name() << "._Fields> ";
+ indent(f_struct) <<
+ "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
+ << " extends org.apache.thrift.TUnion<" << tstruct->get_name() << ", " << tstruct->get_name() << "._Fields> ";
scope_up(f_struct);
@@ -823,8 +831,7 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* type = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL) {
- indent(out) << "super(_Fields." << constant_name((*m_iter)->get_name()) << ", "
- << render_const_value(out, type, (*m_iter)->get_value()) << ");" << endl;
+ indent(out) << "super(_Fields." << constant_name((*m_iter)->get_name()) << ", " << render_const_value(out, type, (*m_iter)->get_value()) << ");" << endl;
default_value = true;
break;
}
@@ -839,8 +846,7 @@
indent(out) << " super(setField, value);" << endl;
indent(out) << "}" << endl << endl;
- indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {"
- << endl;
+ indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {" << endl;
indent(out) << " super(other);" << endl;
indent(out) << "}" << endl;
@@ -851,20 +857,16 @@
// generate "constructors" for each field
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* type = (*m_iter)->get_type();
- indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "("
- << type_name(type) << " value) {" << endl;
+ indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(" << type_name(type) << " value) {" << endl;
indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl;
indent(out) << " return x;" << endl;
indent(out) << "}" << endl << endl;
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
- indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name()
- << "(byte[] value) {" << endl;
- indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();"
- << endl;
- indent(out) << " x.set" << get_cap_name((*m_iter)->get_name())
- << "(ByteBuffer.wrap(Arrays.copyOf(value, value.length)));" << endl;
+ indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(byte[] value) {" << endl;
+ indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
+ indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(ByteBuffer.wrap(Arrays.copyOf(value, value.length)));" << endl;
indent(out) << " return x;" << endl;
indent(out) << "}" << endl << endl;
}
@@ -890,38 +892,28 @@
generate_java_doc(out, field);
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public byte[] get" << cap_name << "() {" << endl;
- indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(buffer"
- << get_cap_name("for") << cap_name << "()));" << endl;
+ indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(buffer" << get_cap_name("for") << cap_name << "()));" << endl;
indent(out) << " ByteBuffer b = buffer" << get_cap_name("for") << cap_name << "();" << endl;
indent(out) << " return b == null ? null : b.array();" << endl;
indent(out) << "}" << endl;
out << endl;
- indent(out) << "public ByteBuffer buffer" << get_cap_name("for")
- << get_cap_name(field->get_name()) << "() {" << endl;
- indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {"
- << endl;
- indent(out)
- << " return org.apache.thrift.TBaseHelper.copyBinary((ByteBuffer)getFieldValue());"
- << endl;
+ indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << get_cap_name(field->get_name()) << "() {" << endl;
+ indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
+ indent(out) << " return org.apache.thrift.TBaseHelper.copyBinary((ByteBuffer)getFieldValue());" << endl;
indent(out) << " } else {" << endl;
indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
- << "' because union is currently set to \" + getFieldDesc(getSetField()).name);"
- << endl;
+ << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl;
} else {
- indent(out) << "public " << type_name(field->get_type()) << " get"
- << get_cap_name(field->get_name()) << "() {" << endl;
- indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {"
- << endl;
- indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();"
- << endl;
+ indent(out) << "public " << type_name(field->get_type()) << " get" << get_cap_name(field->get_name()) << "() {" << endl;
+ indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
+ indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();" << endl;
indent(out) << " } else {" << endl;
indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
- << "' because union is currently set to \" + getFieldDesc(getSetField()).name);"
- << endl;
+ << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl;
}
@@ -930,16 +922,13 @@
generate_java_doc(out, field);
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
- indent(out) << "public void set" << get_cap_name(field->get_name()) << "(byte[] value) {"
- << endl;
- indent(out) << " set" << get_cap_name(field->get_name())
- << "(ByteBuffer.wrap(Arrays.copyOf(value, value.length)));" << endl;
+ indent(out) << "public void set" << get_cap_name(field->get_name()) << "(byte[] value) {" << endl;
+ indent(out) << " set" << get_cap_name(field->get_name()) << "(ByteBuffer.wrap(Arrays.copyOf(value, value.length)));" << endl;
indent(out) << "}" << endl;
out << endl;
}
- indent(out) << "public void set" << get_cap_name(field->get_name()) << "("
- << type_name(field->get_type()) << " value) {" << endl;
+ indent(out) << "public void set" << get_cap_name(field->get_name()) << "(" << type_name(field->get_type()) << " value) {" << endl;
if (type_can_be_null(field->get_type())) {
indent(out) << " if (value == null) throw new NullPointerException();" << endl;
}
@@ -963,8 +952,7 @@
std::string field_name = (*m_iter)->get_name();
- indent(out) << "public boolean is" << get_cap_name("set") << get_cap_name(field_name) << "() {"
- << endl;
+ indent(out) << "public boolean is" << get_cap_name("set") << get_cap_name(field_name) << "() {" << endl;
indent_up();
indent(out) << "return setField_ == _Fields." << constant_name(field_name) << ";" << endl;
indent_down();
@@ -995,9 +983,7 @@
void t_java_generator::generate_check_type(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out)
- << "protected void checkType(_Fields setField, Object value) throws ClassCastException {"
- << endl;
+ indent(out) << "protected void checkType(_Fields setField, Object value) throws ClassCastException {" << endl;
indent_up();
indent(out) << "switch (setField) {" << endl;
@@ -1010,13 +996,12 @@
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
- indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true)
- << ") {" << endl;
+ indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true) << ") {" << endl;
indent(out) << " break;" << endl;
indent(out) << " }" << endl;
indent(out) << " throw new ClassCastException(\"Was expecting value of type "
- << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
- << "', but got \" + value.getClass().getSimpleName());" << endl;
+ << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
+ << "', but got \" + value.getClass().getSimpleName());" << endl;
// do the real check here
}
@@ -1032,9 +1017,7 @@
void t_java_generator::generate_standard_scheme_read_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "protected Object standardSchemeReadValue(org.apache.thrift.protocol.TProtocol "
- "iprot, org.apache.thrift.protocol.TField field) throws "
- "org.apache.thrift.TException {" << endl;
+ indent(out) << "protected Object standardSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TField field) throws org.apache.thrift.TException {" << endl;
indent_up();
@@ -1052,11 +1035,9 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {"
- << endl;
+ indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";"
- << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl;
generate_deserialize_field(out, field, "");
indent(out) << "return " << field->get_name() << ";" << endl;
indent_down();
@@ -1068,8 +1049,7 @@
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any "
- "of the case statements!\");" << endl;
+ indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -1088,8 +1068,7 @@
void t_java_generator::generate_standard_scheme_write_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "protected void standardSchemeWriteValue(org.apache.thrift.protocol.TProtocol "
- "oprot) throws org.apache.thrift.TException {" << endl;
+ indent(out) << "protected void standardSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl;
indent_up();
@@ -1104,16 +1083,15 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << " = ("
- << type_name(field->get_type(), true, false) << ")value_;" << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name()
+ << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl;
generate_serialize_field(out, field, "");
indent(out) << "return;" << endl;
indent_down();
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + "
- "setField_);" << endl;
+ indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -1125,8 +1103,7 @@
void t_java_generator::generate_tuple_scheme_read_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "protected Object tupleSchemeReadValue(org.apache.thrift.protocol.TProtocol "
- "iprot, short fieldID) throws org.apache.thrift.TException {" << endl;
+ indent(out) << "protected Object tupleSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, short fieldID) throws org.apache.thrift.TException {" << endl;
indent_up();
@@ -1144,16 +1121,14 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";"
- << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl;
generate_deserialize_field(out, field, "");
indent(out) << "return " << field->get_name() << ";" << endl;
indent_down();
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any "
- "of the case statements!\");" << endl;
+ indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -1161,8 +1136,7 @@
indent_down();
indent(out) << "} else {" << endl;
indent_up();
- indent(out) << "throw new TProtocolException(\"Couldn't find a field with field id \" + fieldID);"
- << endl;
+ indent(out) << "throw new TProtocolException(\"Couldn't find a field with field id \" + fieldID);" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
@@ -1171,8 +1145,7 @@
void t_java_generator::generate_tuple_scheme_write_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "protected void tupleSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot) "
- "throws org.apache.thrift.TException {" << endl;
+ indent(out) << "protected void tupleSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl;
indent_up();
@@ -1187,16 +1160,15 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << " = ("
- << type_name(field->get_type(), true, false) << ")value_;" << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name()
+ << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl;
generate_serialize_field(out, field, "");
indent(out) << "return;" << endl;
indent_down();
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + "
- "setField_);" << endl;
+ indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -1208,8 +1180,7 @@
void t_java_generator::generate_get_field_desc(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "protected org.apache.thrift.protocol.TField getFieldDesc(_Fields setField) {"
- << endl;
+ indent(out) << "protected org.apache.thrift.protocol.TField getFieldDesc(_Fields setField) {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
@@ -1235,7 +1206,7 @@
}
void t_java_generator::generate_get_struct_desc(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
+ (void) tstruct;
indent(out) << "@Override" << endl;
indent(out) << "protected org.apache.thrift.protocol.TStruct getStructDesc() {" << endl;
indent(out) << " return STRUCT_DESC;" << endl;
@@ -1255,18 +1226,15 @@
out << endl;
indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl;
- indent(out) << " return other != null && getSetField() == other.getSetField() && "
- "getFieldValue().equals(other.getFieldValue());" << endl;
+ indent(out) << " return other != null && getSetField() == other.getSetField() && getFieldValue().equals(other.getFieldValue());" << endl;
indent(out) << "}" << endl;
out << endl;
indent(out) << "@Override" << endl;
indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
- indent(out) << " int lastComparison = org.apache.thrift.TBaseHelper.compareTo(getSetField(), "
- "other.getSetField());" << endl;
+ indent(out) << " int lastComparison = org.apache.thrift.TBaseHelper.compareTo(getSetField(), other.getSetField());" << endl;
indent(out) << " if (lastComparison == 0) {" << endl;
- indent(out) << " return org.apache.thrift.TBaseHelper.compareTo(getFieldValue(), "
- "other.getFieldValue());" << endl;
+ indent(out) << " return org.apache.thrift.TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());" << endl;
indent(out) << " }" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
@@ -1274,7 +1242,7 @@
}
void t_java_generator::generate_union_hashcode(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
+ (void) tstruct;
indent(out) << "@Override" << endl;
indent(out) << "public int hashCode() {" << endl;
indent(out) << " List<Object> list = new ArrayList<Object>();" << endl;
@@ -1303,7 +1271,7 @@
* @param in_class If inside a class, needs to be static class
* @param is_result If this is a result it needs a different writer
*/
-void t_java_generator::generate_java_struct_definition(ofstream& out,
+void t_java_generator::generate_java_struct_definition(ofstream &out,
t_struct* tstruct,
bool is_exception,
bool in_class,
@@ -1316,14 +1284,14 @@
generate_javax_generated_annotation(out);
}
- indent(out) << "public " << (is_final ? "final " : "") << (in_class ? "static " : "") << "class "
- << tstruct->get_name() << " ";
+ indent(out) <<
+ "public " << (is_final ? "final " : "") <<
+ (in_class ? "static " : "") << "class " << tstruct->get_name() << " ";
if (is_exception) {
out << "extends TException ";
}
- out << "implements org.apache.thrift.TBase<" << tstruct->get_name() << ", " << tstruct->get_name()
- << "._Fields>, java.io.Serializable, Cloneable, Comparable<" << tstruct->get_name() << ">";
+ out << "implements org.apache.thrift.TBase<" << tstruct->get_name() << ", " << tstruct->get_name() << "._Fields>, java.io.Serializable, Cloneable, Comparable<" << tstruct->get_name() << ">";
if (android_style_) {
out << ", android.os.Parcelable";
@@ -1361,7 +1329,7 @@
out << endl;
- if (android_style_) {
+ if(android_style_) {
generate_java_struct_parcelable(out, tstruct);
}
@@ -1380,14 +1348,14 @@
optionals++;
}
if (!type_can_be_null((*m_iter)->get_type())) {
- indent(out) << "private static final int " << isset_field_id(*m_iter) << " = " << i << ";"
- << endl;
+ indent(out) << "private static final int " << isset_field_id(*m_iter)
+ << " = " << i << ";" << endl;
i++;
}
}
std::string primitiveType;
- switch (needs_isset(tstruct, &primitiveType)) {
+ switch(needs_isset(tstruct, &primitiveType)) {
case ISSET_NONE:
break;
case ISSET_PRIMITIVE:
@@ -1405,7 +1373,7 @@
output_string = output_string + "_Fields." + constant_name((*m_iter)->get_name()) + ",";
}
}
- indent(out) << output_string.substr(0, output_string.length() - 1) << "};" << endl;
+ indent(out) << output_string.substr(0, output_string.length() - 1) << "};" << endl;
}
}
@@ -1414,17 +1382,13 @@
bool all_optional_members = true;
// Default constructor
- indent(out) << "public " << tstruct->get_name() << "() {" << endl;
+ indent(out) <<
+ "public " << tstruct->get_name() << "() {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL) {
- print_const_value(out,
- "this." + (*m_iter)->get_name(),
- t,
- (*m_iter)->get_value(),
- true,
- true);
+ print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
}
if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
all_optional_members = false;
@@ -1435,7 +1399,8 @@
if (!members.empty() && !all_optional_members) {
// Full constructor for all fields
- indent(out) << "public " << tstruct->get_name() << "(" << endl;
+ indent(out) <<
+ "public " << tstruct->get_name() << "(" << endl;
indent_up();
bool first = true;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -1444,7 +1409,8 @@
out << "," << endl;
}
first = false;
- indent(out) << type_name((*m_iter)->get_type()) << " " << (*m_iter)->get_name();
+ indent(out) << type_name((*m_iter)->get_type()) << " " <<
+ (*m_iter)->get_name();
}
}
out << ")" << endl;
@@ -1457,11 +1423,11 @@
t_type* type = get_true_type((*m_iter)->get_type());
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "this." << (*m_iter)->get_name()
- << " = org.apache.thrift.TBaseHelper.copyBinary(" << (*m_iter)->get_name()
- << ");" << endl;
+ << " = org.apache.thrift.TBaseHelper.copyBinary("
+ << (*m_iter)->get_name() << ");" << endl;
} else {
- indent(out) << "this." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << ";"
- << endl;
+ indent(out) << "this." << (*m_iter)->get_name() << " = "
+ << (*m_iter)->get_name() << ";" << endl;
}
generate_isset_set(out, (*m_iter), "");
}
@@ -1475,11 +1441,10 @@
indent(out) << "/**" << endl;
indent(out) << " * Performs a deep copy on <i>other</i>." << endl;
indent(out) << " */" << endl;
- indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {"
- << endl;
+ indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {" << endl;
indent_up();
- switch (needs_isset(tstruct)) {
+ switch(needs_isset(tstruct)) {
case ISSET_NONE:
break;
case ISSET_PRIMITIVE:
@@ -1525,6 +1490,8 @@
indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
indent(out) << "}" << endl << endl;
+
+
generate_java_struct_clear(out, tstruct);
generate_java_bean_boilerplate(out, tstruct);
@@ -1557,208 +1524,206 @@
/**
* generates parcelable interface implementation
*/
-void t_java_generator::generate_java_struct_parcelable(ofstream& out, t_struct* tstruct) {
+void t_java_generator::generate_java_struct_parcelable(ofstream& out,
+ t_struct* tstruct) {
string tname = tstruct->get_name();
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- out << indent() << "@Override" << endl << indent()
- << "public void writeToParcel(android.os.Parcel out, int flags) {" << endl;
+ out << indent() << "@Override" << endl <<
+ indent() << "public void writeToParcel(android.os.Parcel out, int flags) {" << endl;
indent_up();
string bitsetPrimitiveType = "";
- switch (needs_isset(tstruct, &bitsetPrimitiveType)) {
+ switch(needs_isset(tstruct, &bitsetPrimitiveType)) {
case ISSET_NONE:
- break;
+ break;
case ISSET_PRIMITIVE:
- indent(out) << "//primitive bitfield of type: " << bitsetPrimitiveType << endl;
- if (bitsetPrimitiveType == "byte") {
- indent(out) << "out.writeByte(__isset_bitfield);" << endl;
- } else if (bitsetPrimitiveType == "short") {
- indent(out) << "out.writeInt(new Short(__isset_bitfield).intValue());" << endl;
- } else if (bitsetPrimitiveType == "int") {
- indent(out) << "out.writeInt(__isset_bitfield);" << endl;
- } else if (bitsetPrimitiveType == "long") {
- indent(out) << "out.writeLong(__isset_bitfield);" << endl;
- }
- out << endl;
- break;
+ indent(out) << "//primitive bitfield of type: " << bitsetPrimitiveType << endl;
+ if(bitsetPrimitiveType=="byte") {
+ indent(out) << "out.writeByte(__isset_bitfield);" << endl;
+ } else if(bitsetPrimitiveType=="short") {
+ indent(out) << "out.writeInt(new Short(__isset_bitfield).intValue());" << endl;
+ } else if(bitsetPrimitiveType=="int") {
+ indent(out) << "out.writeInt(__isset_bitfield);" << endl;
+ } else if(bitsetPrimitiveType=="long") {
+ indent(out) << "out.writeLong(__isset_bitfield);" << endl;
+ }
+ out << endl;
+ break;
case ISSET_BITSET:
- indent(out) << "//BitSet" << endl;
- indent(out) << "out.writeSerializable(__isset_bit_vector);" << endl;
- out << endl;
- break;
+ indent(out) << "//BitSet" << endl;
+ indent(out) << "out.writeSerializable(__isset_bit_vector);" << endl;
+ out << endl;
+ break;
}
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- string name = (*m_iter)->get_name();
+ t_type* t = get_true_type((*m_iter)->get_type());
+ string name = (*m_iter)->get_name();
- if (t->is_struct()) {
- indent(out) << "out.writeParcelable(" << name << ", flags);" << endl;
- } else if (type_name(t) == "float") {
- indent(out) << "out.writeFloat(" << name << ");" << endl;
- } else if (t->is_enum()) {
- indent(out) << "out.writeInt(" << name << ".getValue());" << endl;
- } else if (t->is_list()) {
- if (((t_list*)t)->get_elem_type()->get_true_type()->is_struct()) {
- indent(out) << "out.writeTypedList(" << name << ");" << endl;
- } else {
- indent(out) << "out.writeList(" << name << ");" << endl;
- }
- } else if (t->is_map()) {
- indent(out) << "out.writeMap(" << name << ");" << endl;
- } else if (t->is_base_type()) {
- if (((t_base_type*)t)->is_binary()) {
- indent(out) << "out.writeInt(" << name << "!=null ? 1 : 0);" << endl;
- indent(out) << "if(" << name << " != null) { " << endl;
- indent_up();
- indent(out) << "out.writeByteArray(" << name << ".array(), " << name << ".position() + "
- << name << ".arrayOffset(), " << name << ".limit() - " << name
- << ".position() );" << endl;
- scope_down(out);
- } else {
- switch (((t_base_type*)t)->get_base()) {
- case t_base_type::TYPE_I16:
- indent(out) << "out.writeInt(new Short(" << name << ").intValue());" << endl;
- break;
- case t_base_type::TYPE_I32:
- indent(out) << "out.writeInt(" << name << ");" << endl;
- break;
- case t_base_type::TYPE_I64:
- indent(out) << "out.writeLong(" << name << ");" << endl;
- break;
- case t_base_type::TYPE_BOOL:
- indent(out) << "out.writeInt(" << name << " ? 1 : 0);" << endl;
- break;
- case t_base_type::TYPE_BYTE:
- indent(out) << "out.writeByte(" << name << ");" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- indent(out) << "out.writeDouble(" << name << ");" << endl;
- break;
- case t_base_type::TYPE_STRING:
- indent(out) << "out.writeString(" << name << ");" << endl;
- break;
- case t_base_type::TYPE_VOID:
- break;
- }
- }
- }
+ if(t->is_struct()) {
+ indent(out) << "out.writeParcelable(" << name << ", flags);" << endl;
+ } else if(type_name(t)=="float") {
+ indent(out) << "out.writeFloat(" << name << ");" << endl;
+ } else if(t->is_enum()) {
+ indent(out) << "out.writeInt(" << name << ".getValue());" << endl;
+ } else if(t->is_list()) {
+ if(((t_list*)t)->get_elem_type()->get_true_type()->is_struct()) {
+ indent(out) << "out.writeTypedList(" << name << ");" << endl;
+ }else {
+ indent(out) << "out.writeList(" << name << ");" << endl;
+ }
+ } else if(t->is_map()) {
+ indent(out) << "out.writeMap(" << name << ");" << endl;
+ } else if(t->is_base_type()) {
+ if (((t_base_type*)t)->is_binary()) {
+ indent(out) << "out.writeInt(" << name << "!=null ? 1 : 0);" << endl;
+ indent(out) << "if(" << name << " != null) { " << endl;
+ indent_up();
+ indent(out) << "out.writeByteArray(" << name << ".array(), "
+ << name << ".position() + " << name << ".arrayOffset(), "
+ << name << ".limit() - " << name << ".position() );" << endl;
+ scope_down(out);
+ } else {
+ switch(((t_base_type*)t)->get_base()) {
+ case t_base_type::TYPE_I16:
+ indent(out) << "out.writeInt(new Short(" << name << ").intValue());" << endl;
+ break;
+ case t_base_type::TYPE_I32:
+ indent(out) << "out.writeInt(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_I64:
+ indent(out) << "out.writeLong(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << "out.writeInt(" << name << " ? 1 : 0);" << endl;
+ break;
+ case t_base_type::TYPE_BYTE:
+ indent(out) << "out.writeByte(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << "out.writeDouble(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_STRING:
+ indent(out) << "out.writeString(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_VOID:
+ break;
+ }
+ }
+ }
}
scope_down(out);
out << endl;
- out << indent() << "@Override" << endl << indent() << "public int describeContents() {" << endl;
+ out << indent() << "@Override" << endl <<
+ indent() << "public int describeContents() {" << endl;
indent_up();
- out << indent() << "return 0;" << endl;
+ out <<
+ indent() << "return 0;" << endl;
scope_down(out);
out << endl;
indent(out) << "public " << tname << "(android.os.Parcel in) {" << endl;
indent_up();
- // read in the required bitfield
- switch (needs_isset(tstruct, &bitsetPrimitiveType)) {
+ //read in the required bitfield
+ switch(needs_isset(tstruct, &bitsetPrimitiveType)) {
case ISSET_NONE:
- break;
+ break;
case ISSET_PRIMITIVE:
- indent(out) << "//primitive bitfield of type: " << bitsetPrimitiveType << endl;
- if (bitsetPrimitiveType == "byte") {
- indent(out) << "__isset_bitfield = in.readByte();" << endl;
- } else if (bitsetPrimitiveType == "short") {
- indent(out) << "__isset_bitfield = (short) in.readInt();" << endl;
- } else if (bitsetPrimitiveType == "int") {
- indent(out) << "__isset_bitfield = in.readInt();" << endl;
- } else if (bitsetPrimitiveType == "long") {
- indent(out) << "__isset_bitfield = in.readLong();" << endl;
- }
- out << endl;
- break;
+ indent(out) << "//primitive bitfield of type: " << bitsetPrimitiveType << endl;
+ if(bitsetPrimitiveType=="byte") {
+ indent(out) << "__isset_bitfield = in.readByte();" << endl;
+ } else if(bitsetPrimitiveType=="short") {
+ indent(out) << "__isset_bitfield = (short) in.readInt();" << endl;
+ } else if(bitsetPrimitiveType=="int") {
+ indent(out) << "__isset_bitfield = in.readInt();" << endl;
+ } else if(bitsetPrimitiveType=="long") {
+ indent(out) << "__isset_bitfield = in.readLong();" << endl;
+ }
+ out << endl;
+ break;
case ISSET_BITSET:
- indent(out) << "//BitSet" << endl;
- indent(out) << "__isset_bit_vector = (BitSet) in.readSerializable();" << endl;
- out << endl;
- break;
+ indent(out) << "//BitSet" << endl;
+ indent(out) << "__isset_bit_vector = (BitSet) in.readSerializable();" << endl;
+ out << endl;
+ break;
}
- // read all the fields
+ //read all the fields
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- string name = (*m_iter)->get_name();
- string prefix = "this." + name;
+ t_type* t = get_true_type((*m_iter)->get_type());
+ string name = (*m_iter)->get_name();
+ string prefix = "this." + name;
- if (t->is_struct()) {
- indent(out) << prefix << "= in.readParcelable(" << tname << ".class.getClassLoader());"
- << endl;
- } else if (t->is_enum()) {
- indent(out) << prefix << " = " << type_name(t) << ".findByValue(in.readInt());" << endl;
- } else if (t->is_list()) {
- t_list* list = (t_list*)t;
- indent(out) << prefix << " = new " << type_name(t, false, true) << "();" << endl;
- if (list->get_elem_type()->get_true_type()->is_struct()) {
- indent(out) << "in.readTypedList(" << prefix << ", " << type_name(list->get_elem_type())
- << ".CREATOR);" << endl;
- } else {
- indent(out) << "in.readList(" << prefix << ", " << tname << ".class.getClassLoader());"
- << endl;
- }
- } else if (t->is_map()) {
- indent(out) << prefix << " = new " << type_name(t, false, true) << "();" << endl;
- indent(out) << " in.readMap(" << prefix << ", " << tname << ".class.getClassLoader());"
- << endl;
- } else if (type_name(t) == "float") {
- indent(out) << prefix << " = in.readFloat();" << endl;
- } else if (t->is_base_type()) {
- t_base_type* bt = (t_base_type*)t;
- if (bt->is_binary()) {
- indent(out) << "if(in.readInt()==1) {" << endl;
- indent_up();
- indent(out) << prefix << " = ByteBuffer.wrap(in.createByteArray());" << endl;
- scope_down(out);
- } else {
- switch (bt->get_base()) {
- case t_base_type::TYPE_I16:
- indent(out) << prefix << " = (short) in.readInt();" << endl;
- break;
- case t_base_type::TYPE_I32:
- indent(out) << prefix << " = in.readInt();" << endl;
- break;
- case t_base_type::TYPE_I64:
- indent(out) << prefix << " = in.readLong();" << endl;
- break;
- case t_base_type::TYPE_BOOL:
- indent(out) << prefix << " = (in.readInt()==1);" << endl;
- break;
- case t_base_type::TYPE_BYTE:
- indent(out) << prefix << " = in.readByte();" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- indent(out) << prefix << " = in.readDouble();" << endl;
- break;
- case t_base_type::TYPE_STRING:
- indent(out) << prefix << "= in.readString();" << endl;
- break;
- case t_base_type::TYPE_VOID:
- break;
- }
- }
- }
+ if(t->is_struct()) {
+ indent(out) << prefix << "= in.readParcelable(" << tname << ".class.getClassLoader());" << endl;
+ } else if(t->is_enum()) {
+ indent(out) << prefix << " = " << type_name(t) << ".findByValue(in.readInt());" << endl;
+ } else if(t->is_list()) {
+ t_list* list = (t_list*)t;
+ indent(out) << prefix << " = new " << type_name(t, false, true) << "();" << endl;
+ if(list->get_elem_type()->get_true_type()->is_struct()) {
+ indent(out) << "in.readTypedList(" << prefix << ", " << type_name(list->get_elem_type()) << ".CREATOR);" << endl;
+ } else {
+ indent(out) << "in.readList(" << prefix << ", " << tname << ".class.getClassLoader());" << endl;
+ }
+ } else if(t->is_map()) {
+ indent(out) << prefix << " = new " << type_name(t, false, true) << "();" << endl;
+ indent(out) << " in.readMap(" << prefix << ", " << tname << ".class.getClassLoader());" << endl;
+ } else if(type_name(t)=="float") {
+ indent(out) << prefix << " = in.readFloat();" << endl;
+ } else if(t->is_base_type()) {
+ t_base_type* bt = (t_base_type*)t;
+ if (bt->is_binary()) {
+ indent(out) << "if(in.readInt()==1) {" << endl;
+ indent_up();
+ indent(out) << prefix << " = ByteBuffer.wrap(in.createByteArray());" << endl;
+ scope_down(out);
+ } else {
+ switch(bt->get_base()) {
+ case t_base_type::TYPE_I16:
+ indent(out) << prefix << " = (short) in.readInt();" << endl;
+ break;
+ case t_base_type::TYPE_I32:
+ indent(out) << prefix << " = in.readInt();" << endl;
+ break;
+ case t_base_type::TYPE_I64:
+ indent(out) << prefix << " = in.readLong();" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << prefix << " = (in.readInt()==1);" << endl;
+ break;
+ case t_base_type::TYPE_BYTE:
+ indent(out) << prefix << " = in.readByte();" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << prefix << " = in.readDouble();" << endl;
+ break;
+ case t_base_type::TYPE_STRING:
+ indent(out) << prefix << "= in.readString();" << endl;
+ break;
+ case t_base_type::TYPE_VOID:
+ break;
+ }
+ }
+ }
}
scope_down(out);
out << endl;
- indent(out) << "public static final android.os.Parcelable.Creator<" << tname
- << "> CREATOR = new android.os.Parcelable.Creator<" << tname << ">() {" << endl;
+ indent(out) << "public static final android.os.Parcelable.Creator<" << tname << "> CREATOR = new android.os.Parcelable.Creator<" << tname << ">() {" << endl;
indent_up();
- indent(out) << "@Override" << endl << indent() << "public " << tname << "[] newArray(int size) {"
- << endl;
+ indent(out) << "@Override" << endl <<
+ indent() << "public " << tname << "[] newArray(int size) {" << endl;
indent_up();
indent(out) << "return new " << tname << "[size];" << endl;
scope_down(out);
out << endl;
- indent(out) << "@Override" << endl << indent() << "public " << tname
- << " createFromParcel(android.os.Parcel in) {" << endl;
+ indent(out) << "@Override" << endl <<
+ indent() << "public " << tname << " createFromParcel(android.os.Parcel in) {" << endl;
indent_up();
indent(out) << "return new " << tname << "(in);" << endl;
scope_down(out);
@@ -1773,20 +1738,26 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct_equality(ofstream& out, t_struct* tstruct) {
- out << indent() << "@Override" << endl << indent() << "public boolean equals(Object that) {"
- << endl;
+void t_java_generator::generate_java_struct_equality(ofstream& out,
+ t_struct* tstruct) {
+ out << indent() << "@Override" << endl <<
+ indent() << "public boolean equals(Object that) {" << endl;
indent_up();
- out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl
- << indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl << indent()
- << " return this.equals((" << tstruct->get_name() << ")that);" << endl << indent()
- << "return false;" << endl;
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl <<
+ indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl <<
+ indent() << " return this.equals((" << tstruct->get_name() << ")that);" << endl <<
+ indent() << "return false;" << endl;
scope_down(out);
out << endl;
- out << indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
+ out <<
+ indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
indent_up();
- out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -1809,14 +1780,18 @@
that_present += " && that." + generate_isset_check(*m_iter);
}
- out << indent() << "boolean this_present_" << name << " = " << this_present << ";" << endl
- << indent() << "boolean that_present_" << name << " = " << that_present << ";" << endl
- << indent() << "if ("
- << "this_present_" << name << " || that_present_" << name << ") {" << endl;
+ out <<
+ indent() << "boolean this_present_" << name << " = "
+ << this_present << ";" << endl <<
+ indent() << "boolean that_present_" << name << " = "
+ << that_present << ";" << endl <<
+ indent() << "if (" << "this_present_" << name
+ << " || that_present_" << name << ") {" << endl;
indent_up();
- out << indent() << "if (!("
- << "this_present_" << name << " && that_present_" << name << "))" << endl << indent()
- << " return false;" << endl;
+ out <<
+ indent() << "if (!(" << "this_present_" << name
+ << " && that_present_" << name << "))" << endl <<
+ indent() << " return false;" << endl;
if (t->is_base_type() && ((t_base_type*)t)->is_binary()) {
unequal = "!this." + name + ".equals(that." + name + ")";
@@ -1826,7 +1801,9 @@
unequal = "this." + name + " != that." + name;
}
- out << indent() << "if (" << unequal << ")" << endl << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (" << unequal << ")" << endl <<
+ indent() << " return false;" << endl;
scope_down(out);
}
@@ -1835,7 +1812,8 @@
scope_down(out);
out << endl;
- out << indent() << "@Override" << endl << indent() << "public int hashCode() {" << endl;
+ out << indent() << "@Override" << endl <<
+ indent() << "public int hashCode() {" << endl;
indent_up();
indent(out) << "List<Object> list = new ArrayList<Object>();" << endl;
@@ -1886,15 +1864,13 @@
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = *m_iter;
- indent(out) << "lastComparison = Boolean.valueOf(" << generate_isset_check(field)
- << ").compareTo(other." << generate_isset_check(field) << ");" << endl;
+ indent(out) << "lastComparison = Boolean.valueOf(" << generate_isset_check(field) << ").compareTo(other." << generate_isset_check(field) << ");" << endl;
indent(out) << "if (lastComparison != 0) {" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
indent(out) << "if (" << generate_isset_check(field) << ") {" << endl;
- indent(out) << " lastComparison = org.apache.thrift.TBaseHelper.compareTo(this."
- << field->get_name() << ", other." << field->get_name() << ");" << endl;
+ indent(out) << " lastComparison = org.apache.thrift.TBaseHelper.compareTo(this." << field->get_name() << ", other." << field->get_name() << ");" << endl;
indent(out) << " if (lastComparison != 0) {" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << " }" << endl;
@@ -1912,19 +1888,21 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct_reader(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
- indent(out) << "public void read(org.apache.thrift.protocol.TProtocol iprot) throws "
- "org.apache.thrift.TException {" << endl;
+void t_java_generator::generate_java_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ (void) tstruct;
+ indent(out) << "public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "schemes.get(iprot.getScheme()).getScheme().read(iprot, this);" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
// generates java method to perform various checks
// (e.g. check that all required fields are set)
-void t_java_generator::generate_java_validator(ofstream& out, t_struct* tstruct) {
+void t_java_generator::generate_java_validator(ofstream& out,
+ t_struct* tstruct){
indent(out) << "public void validate() throws org.apache.thrift.TException {" << endl;
indent_up();
@@ -1935,21 +1913,17 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
if (bean_style_) {
- out << indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl << indent()
- << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '"
- << (*f_iter)->get_name() << "' is unset! Struct:\" + toString());" << endl << indent()
- << "}" << endl << endl;
- } else {
+ out <<
+ indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl <<
+ indent() << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' is unset! Struct:\" + toString());" << endl <<
+ indent() << "}" << endl << endl;
+ } else{
if (type_can_be_null((*f_iter)->get_type())) {
indent(out) << "if (" << (*f_iter)->get_name() << " == null) {" << endl;
- indent(out)
- << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '"
- << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl;
+ indent(out) << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl;
indent(out) << "}" << endl;
} else {
- indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name()
- << "' because it's a primitive and you chose the non-beans generator."
- << endl;
+ indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name() << "' because it's a primitive and you chose the non-beans generator." << endl;
}
}
}
@@ -1958,7 +1932,7 @@
out << indent() << "// check for sub-struct validity" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_type* type = (*f_iter)->get_type();
- if (type->is_struct() && !((t_struct*)type)->is_union()) {
+ if (type->is_struct() && ! ((t_struct*)type)->is_union()) {
out << indent() << "if (" << (*f_iter)->get_name() << " != null) {" << endl;
out << indent() << " " << (*f_iter)->get_name() << ".validate();" << endl;
out << indent() << "}" << endl;
@@ -1974,10 +1948,10 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct_writer(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
- indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws "
- "org.apache.thrift.TException {" << endl;
+void t_java_generator::generate_java_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ (void) tstruct;
+ indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "schemes.get(oprot.getScheme()).getScheme().write(oprot, this);" << endl;
@@ -1993,10 +1967,10 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct_result_writer(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
- indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws "
- "org.apache.thrift.TException {" << endl;
+void t_java_generator::generate_java_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ (void) tstruct;
+ indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "schemes.get(oprot.getScheme()).getScheme().write(oprot, this);" << endl;
@@ -2005,24 +1979,20 @@
}
void t_java_generator::generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
+ (void) tstruct;
indent(out) << "public _Fields fieldForId(int fieldId) {" << endl;
indent(out) << " return _Fields.findByThriftId(fieldId);" << endl;
indent(out) << "}" << endl << endl;
}
-void t_java_generator::generate_reflection_getters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
+void t_java_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
indent(out) << "case " << constant_name(field_name) << ":" << endl;
indent_up();
if (type->is_base_type() && !type->is_string()) {
t_base_type* base_type = (t_base_type*)type;
- indent(out) << "return " << type_name(type, true, false) << ".valueOf("
- << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
+ indent(out) << "return " << type_name(type, true, false) << ".valueOf(" << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
} else {
indent(out) << "return get" << cap_name << "();" << endl << endl;
}
@@ -2030,10 +2000,7 @@
indent_down();
}
-void t_java_generator::generate_reflection_setters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
+void t_java_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) {
indent(out) << "case " << constant_name(field_name) << ":" << endl;
indent_up();
indent(out) << "if (value == null) {" << endl;
@@ -2046,8 +2013,7 @@
indent_down();
}
-void t_java_generator::generate_generic_field_getters_setters(std::ofstream& out,
- t_struct* tstruct) {
+void t_java_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) {
std::ostringstream getter_stream;
std::ostringstream setter_stream;
@@ -2066,6 +2032,7 @@
indent_down();
}
+
// create the setter
indent(out) << "public void setFieldValue(_Fields field, Object value) {" << endl;
@@ -2086,13 +2053,12 @@
}
// Creates a generic isSet method that takes the field number as argument
-void t_java_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct) {
+void t_java_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct){
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// create the isSet method
- indent(out) << "/** Returns true if field corresponding to fieldID is set (has been assigned a "
- "value) and false otherwise */" << endl;
+ indent(out) << "/** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */" << endl;
indent(out) << "public boolean isSet(_Fields field) {" << endl;
indent_up();
indent(out) << "if (field == null) {" << endl;
@@ -2121,7 +2087,8 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_bean_boilerplate(ofstream& out, t_struct* tstruct) {
+void t_java_generator::generate_java_bean_boilerplate(ofstream& out,
+ t_struct* tstruct) {
isset_type issetType = needs_isset(tstruct);
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -2137,8 +2104,8 @@
out << get_cap_name("size() {") << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? 0 : "
- << "this." << field_name << ".size();" << endl;
+ indent(out) << "return (this." << field_name << " == null) ? 0 : " <<
+ "this." << field_name << ".size();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
}
@@ -2152,13 +2119,13 @@
}
// Iterator getter for sets and lists
- indent(out) << "public java.util.Iterator<" << type_name(element_type, true, false) << "> get"
- << cap_name;
+ indent(out) << "public java.util.Iterator<" <<
+ type_name(element_type, true, false) << "> get" << cap_name;
out << get_cap_name("iterator() {") << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? null : "
- << "this." << field_name << ".iterator();" << endl;
+ indent(out) << "return (this." << field_name << " == null) ? null : " <<
+ "this." << field_name << ".iterator();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
@@ -2170,8 +2137,8 @@
indent_up();
indent(out) << "if (this." << field_name << " == null) {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
- << endl;
+ indent(out) << "this." << field_name << " = new " << type_name(type, false, true) <<
+ "();" << endl;
indent_down();
indent(out) << "}" << endl;
indent(out) << "this." << field_name << ".add(elem);" << endl;
@@ -2184,14 +2151,14 @@
indent(out);
out << "public void put" << get_cap_name("to");
- out << cap_name << "(" << type_name(key_type) << " key, " << type_name(val_type) << " val) {"
- << endl;
+ out << cap_name << "(" << type_name(key_type) << " key, "
+ << type_name(val_type) << " val) {" << endl;
indent_up();
indent(out) << "if (this." << field_name << " == null) {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
- << endl;
+ indent(out) << "this." << field_name << " = new " <<
+ type_name(type, false, true) << "();" << endl;
indent_down();
indent(out) << "}" << endl;
indent(out) << "this." << field_name << ".put(key, val);" << endl;
@@ -2203,20 +2170,17 @@
generate_java_doc(out, field);
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public byte[] get" << cap_name << "() {" << endl;
- indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize("
- << field_name << "));" << endl;
- indent(out) << " return " << field_name << " == null ? null : " << field_name << ".array();"
- << endl;
+ indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(" << field_name << "));" << endl;
+ indent(out) << " return " << field_name << " == null ? null : " << field_name << ".array();" << endl;
indent(out) << "}" << endl << endl;
- indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << cap_name << "() {"
- << endl;
- indent(out) << " return org.apache.thrift.TBaseHelper.copyBinary(" << field_name << ");"
- << endl;
+ indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << cap_name << "() {" << endl;
+ indent(out) << " return org.apache.thrift.TBaseHelper.copyBinary(" << field_name << ");" << endl;
indent(out) << "}" << endl << endl;
} else {
indent(out) << "public " << type_name(type);
- if (type->is_base_type() && ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
+ if (type->is_base_type() &&
+ ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
out << " is";
} else {
out << " get";
@@ -2238,9 +2202,10 @@
out << type_name(tstruct);
}
out << " set" << cap_name << "(byte[] " << field_name << ") {" << endl;
- indent(out) << " this." << field_name << " = " << field_name << " == null ? (ByteBuffer)null"
- << " : ByteBuffer.wrap(Arrays.copyOf(" << field_name << ", " << field_name
- << ".length));" << endl;
+ indent(out) << " this." << field_name << " = "
+ << field_name << " == null ? (ByteBuffer)null"
+ << " : ByteBuffer.wrap(Arrays.copyOf("
+ << field_name << ", " << field_name << ".length));" << endl;
if (!bean_style_) {
indent(out) << " return this;" << endl;
}
@@ -2274,9 +2239,8 @@
indent_up();
if (type_can_be_null(type)) {
indent(out) << "this." << field_name << " = null;" << endl;
- } else if (issetType == ISSET_PRIMITIVE) {
- indent(out) << "__isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, "
- << isset_field_id(field) << ");" << endl;
+ } else if(issetType == ISSET_PRIMITIVE) {
+ indent(out) << "__isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, " << isset_field_id(field) << ");" << endl;
} else {
indent(out) << "__isset_bit_vector.clear(" << isset_field_id(field) << ");" << endl;
}
@@ -2284,31 +2248,27 @@
indent(out) << "}" << endl << endl;
// isSet method
- indent(out) << "/** Returns true if field " << field_name
- << " is set (has been assigned a value) and false otherwise */" << endl;
+ indent(out) << "/** Returns true if field " << field_name << " is set (has been assigned a value) and false otherwise */" << endl;
indent(out) << "public boolean is" << get_cap_name("set") << cap_name << "() {" << endl;
indent_up();
if (type_can_be_null(type)) {
indent(out) << "return this." << field_name << " != null;" << endl;
- } else if (issetType == ISSET_PRIMITIVE) {
- indent(out) << "return EncodingUtils.testBit(__isset_bitfield, " << isset_field_id(field)
- << ");" << endl;
+ } else if(issetType == ISSET_PRIMITIVE) {
+ indent(out) << "return EncodingUtils.testBit(__isset_bitfield, " << isset_field_id(field) << ");" << endl;
} else {
indent(out) << "return __isset_bit_vector.get(" << isset_field_id(field) << ");" << endl;
}
indent_down();
indent(out) << "}" << endl << endl;
- indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {"
- << endl;
+ indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {" << endl;
indent_up();
if (type_can_be_null(type)) {
indent(out) << "if (!value) {" << endl;
indent(out) << " this." << field_name << " = null;" << endl;
indent(out) << "}" << endl;
- } else if (issetType == ISSET_PRIMITIVE) {
- indent(out) << "__isset_bitfield = EncodingUtils.setBit(__isset_bitfield, "
- << isset_field_id(field) << ", value);" << endl;
+ } else if(issetType == ISSET_PRIMITIVE) {
+ indent(out) << "__isset_bitfield = EncodingUtils.setBit(__isset_bitfield, " << isset_field_id(field) << ", value);" << endl;
} else {
indent(out) << "__isset_bit_vector.set(" << isset_field_id(field) << ", value);" << endl;
}
@@ -2322,12 +2282,14 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_struct_tostring(ofstream& out, t_struct* tstruct) {
- out << indent() << "@Override" << endl << indent() << "public String toString() {" << endl;
+void t_java_generator::generate_java_struct_tostring(ofstream& out,
+ t_struct* tstruct) {
+ out << indent() << "@Override" << endl <<
+ indent() << "public String toString() {" << endl;
indent_up();
- out << indent() << "StringBuilder sb = new StringBuilder(\"" << tstruct->get_name() << "(\");"
- << endl;
+ out <<
+ indent() << "StringBuilder sb = new StringBuilder(\"" << tstruct->get_name() << "(\");" << endl;
out << indent() << "boolean first = true;" << endl << endl;
const vector<t_field*>& fields = tstruct->get_members();
@@ -2335,7 +2297,7 @@
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
- if (could_be_unset) {
+ if(could_be_unset) {
indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
}
@@ -2355,8 +2317,7 @@
}
if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
- indent(out) << "org.apache.thrift.TBaseHelper.toString(this." << field->get_name() << ", sb);"
- << endl;
+ indent(out) << "org.apache.thrift.TBaseHelper.toString(this." << field->get_name() << ", sb);" << endl;
} else {
indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
}
@@ -2367,13 +2328,15 @@
}
indent(out) << "first = false;" << endl;
- if (could_be_unset) {
+ if(could_be_unset) {
indent_down();
indent(out) << "}" << endl;
}
first = false;
}
- out << indent() << "sb.append(\")\");" << endl << indent() << "return sb.toString();" << endl;
+ out <<
+ indent() << "sb.append(\")\");" << endl <<
+ indent() << "return sb.toString();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
@@ -2385,27 +2348,23 @@
*
* @param tstruct The struct definition
*/
-void t_java_generator::generate_java_meta_data_map(ofstream& out, t_struct* tstruct) {
+void t_java_generator::generate_java_meta_data_map(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// Static Map with fieldID -> org.apache.thrift.meta_data.FieldMetaData mappings
- indent(out)
- << "public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;"
- << endl;
+ indent(out) << "public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;" << endl;
indent(out) << "static {" << endl;
indent_up();
- indent(out) << "Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new "
- "EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);"
- << endl;
+ indent(out) << "Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);" << endl;
// Populate map
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = *f_iter;
std::string field_name = field->get_name();
- indent(out) << "tmpMap.put(_Fields." << constant_name(field_name)
- << ", new org.apache.thrift.meta_data.FieldMetaData(\"" << field_name << "\", ";
+ indent(out) << "tmpMap.put(_Fields." << constant_name(field_name) << ", new org.apache.thrift.meta_data.FieldMetaData(\"" << field_name << "\", ";
// Set field requirement type (required, optional, etc.)
if (field->get_req() == t_field::T_REQUIRED) {
@@ -2418,13 +2377,12 @@
// Create value meta data
generate_field_value_meta_data(out, field->get_type());
- out << "));" << endl;
+ out << "));" << endl;
}
indent(out) << "metaDataMap = Collections.unmodifiableMap(tmpMap);" << endl;
- indent(out) << "org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap("
- << type_name(tstruct) << ".class, metaDataMap);" << endl;
+ indent(out) << "org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ".class, metaDataMap);" << endl;
indent_down();
indent(out) << "}" << endl << endl;
}
@@ -2434,7 +2392,7 @@
* (e.g. for the type struct it returns "org.apache.thrift.protocol.TType.STRUCT")
*/
std::string t_java_generator::get_java_type_string(t_type* type) {
- if (type->is_list()) {
+ if (type->is_list()){
return "org.apache.thrift.protocol.TType.LIST";
} else if (type->is_map()) {
return "org.apache.thrift.protocol.TType.MAP";
@@ -2448,68 +2406,43 @@
return get_java_type_string(((t_typedef*)type)->get_type());
} else if (type->is_base_type()) {
switch (((t_base_type*)type)->get_base()) {
- case t_base_type::TYPE_VOID:
- return "org.apache.thrift.protocol.TType.VOID";
- break;
- case t_base_type::TYPE_STRING:
- return "org.apache.thrift.protocol.TType.STRING";
- break;
- case t_base_type::TYPE_BOOL:
- return "org.apache.thrift.protocol.TType.BOOL";
- break;
- case t_base_type::TYPE_BYTE:
- return "org.apache.thrift.protocol.TType.BYTE";
- break;
- case t_base_type::TYPE_I16:
- return "org.apache.thrift.protocol.TType.I16";
- break;
- case t_base_type::TYPE_I32:
- return "org.apache.thrift.protocol.TType.I32";
- break;
- case t_base_type::TYPE_I64:
- return "org.apache.thrift.protocol.TType.I64";
- break;
- case t_base_type::TYPE_DOUBLE:
- return "org.apache.thrift.protocol.TType.DOUBLE";
- break;
- default:
- throw std::runtime_error("Unknown thrift type \"" + type->get_name()
- + "\" passed to t_java_generator::get_java_type_string!");
- return "Unknown thrift type \"" + type->get_name()
- + "\" passed to t_java_generator::get_java_type_string!";
- break; // This should never happen!
+ case t_base_type::TYPE_VOID : return "org.apache.thrift.protocol.TType.VOID"; break;
+ case t_base_type::TYPE_STRING : return "org.apache.thrift.protocol.TType.STRING"; break;
+ case t_base_type::TYPE_BOOL : return "org.apache.thrift.protocol.TType.BOOL"; break;
+ case t_base_type::TYPE_BYTE : return "org.apache.thrift.protocol.TType.BYTE"; break;
+ case t_base_type::TYPE_I16 : return "org.apache.thrift.protocol.TType.I16"; break;
+ case t_base_type::TYPE_I32 : return "org.apache.thrift.protocol.TType.I32"; break;
+ case t_base_type::TYPE_I64 : return "org.apache.thrift.protocol.TType.I64"; break;
+ case t_base_type::TYPE_DOUBLE : return "org.apache.thrift.protocol.TType.DOUBLE"; break;
+ default :
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!");
+ return "Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!";
+ break; // This should never happen!
}
} else {
- throw std::runtime_error("Unknown thrift type \"" + type->get_name()
- + "\" passed to t_java_generator::get_java_type_string!");
- return "Unknown thrift type \"" + type->get_name()
- + "\" passed to t_java_generator::get_java_type_string!";
- // This should never happen!
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!");
+ return "Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!";
+ // This should never happen!
}
}
-void t_java_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type) {
+void t_java_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){
out << endl;
indent_up();
indent_up();
- if (type->is_struct()) {
- indent(out) << "new "
- "org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType."
- "STRUCT, " << type_name(type) << ".class";
- } else if (type->is_container()) {
- if (type->is_list()) {
- indent(out)
- << "new org.apache.thrift.meta_data.ListMetaData(org.apache.thrift.protocol.TType.LIST, ";
+ if (type->is_struct()){
+ indent(out) << "new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, " << type_name(type) << ".class";
+ } else if (type->is_container()){
+ if (type->is_list()){
+ indent(out) << "new org.apache.thrift.meta_data.ListMetaData(org.apache.thrift.protocol.TType.LIST, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else if (type->is_set()) {
- indent(out)
- << "new org.apache.thrift.meta_data.SetMetaData(org.apache.thrift.protocol.TType.SET, ";
+ } else if (type->is_set()){
+ indent(out) << "new org.apache.thrift.meta_data.SetMetaData(org.apache.thrift.protocol.TType.SET, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else { // map
- indent(out)
- << "new org.apache.thrift.meta_data.MapMetaData(org.apache.thrift.protocol.TType.MAP, ";
+ } else{ // map
+ indent(out) << "new org.apache.thrift.meta_data.MapMetaData(org.apache.thrift.protocol.TType.MAP, ";
t_type* key_type = ((t_map*)type)->get_key_type();
t_type* val_type = ((t_map*)type)->get_val_type();
generate_field_value_meta_data(out, key_type);
@@ -2517,12 +2450,9 @@
generate_field_value_meta_data(out, val_type);
}
} else if (type->is_enum()) {
- indent(out)
- << "new org.apache.thrift.meta_data.EnumMetaData(org.apache.thrift.protocol.TType.ENUM, "
- << type_name(type) << ".class";
+ indent(out) << "new org.apache.thrift.meta_data.EnumMetaData(org.apache.thrift.protocol.TType.ENUM, " << type_name(type) << ".class";
} else {
- indent(out) << "new org.apache.thrift.meta_data.FieldValueMetaData("
- << get_java_type_string(type);
+ indent(out) << "new org.apache.thrift.meta_data.FieldValueMetaData(" << get_java_type_string(type);
if (type->is_typedef()) {
indent(out) << ", \"" << ((t_typedef*)type)->get_symbolic() << "\"";
} else if (((t_base_type*)type)->is_binary()) {
@@ -2534,6 +2464,7 @@
indent_down();
}
+
/**
* Generates a thrift service. In C++, this comprises an entirely separate
* header and source file. The header file defines the methods and includes
@@ -2544,10 +2475,14 @@
*/
void t_java_generator::generate_service(t_service* tservice) {
// Make output file
- string f_service_name = package_dir_ + "/" + make_valid_java_filename(service_name_) + ".java";
+ string f_service_name = package_dir_+"/"+make_valid_java_filename(service_name_)+".java";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << java_package() << java_type_imports() << java_suppressions();
+ f_service_ <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_suppressions();
generate_javax_generated_annotation(f_service_);
f_service_ << "public class " << service_name_ << " {" << endl << endl;
@@ -2563,7 +2498,8 @@
generate_service_helpers(tservice);
indent_down();
- f_service_ << "}" << endl;
+ f_service_ <<
+ "}" << endl;
f_service_.close();
}
@@ -2581,7 +2517,8 @@
}
generate_java_doc(f_service_, tservice);
- f_service_ << indent() << "public interface Iface" << extends_iface << " {" << endl << endl;
+ f_service_ << indent() << "public interface Iface" << extends_iface <<
+ " {" << endl << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
@@ -2606,13 +2543,13 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent(f_service_) << "public " << function_signature_async(*f_iter, true)
- << " throws org.apache.thrift.TException;" << endl << endl;
+ indent(f_service_) << "public " << function_signature_async(*f_iter, true) << " throws org.apache.thrift.TException;" << endl << endl;
}
indent_down();
f_service_ << indent() << "}" << endl << endl;
}
+
/**
* Generates structs for all the service args and return types
*
@@ -2643,23 +2580,19 @@
extends_client = extends + ".Client";
}
- indent(f_service_) << "public static class Client extends " << extends_client
- << " implements Iface {" << endl;
+ indent(f_service_) <<
+ "public static class Client extends " << extends_client << " implements Iface {" << endl;
indent_up();
- indent(f_service_)
- << "public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {"
- << endl;
+ indent(f_service_) << "public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {" << endl;
indent_up();
indent(f_service_) << "public Factory() {}" << endl;
- indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol prot) {"
- << endl;
+ indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol prot) {" << endl;
indent_up();
indent(f_service_) << "return new Client(prot);" << endl;
indent_down();
indent(f_service_) << "}" << endl;
- indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol iprot, "
- "org.apache.thrift.protocol.TProtocol oprot) {" << endl;
+ indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {" << endl;
indent_up();
indent(f_service_) << "return new Client(iprot, oprot);" << endl;
indent_down();
@@ -2673,8 +2606,8 @@
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public Client(org.apache.thrift.protocol.TProtocol iprot, "
- "org.apache.thrift.protocol.TProtocol oprot) {" << endl;
+ indent(f_service_) <<
+ "public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {" << endl;
indent(f_service_) << " super(iprot, oprot);" << endl;
indent(f_service_) << "}" << endl << endl;
@@ -2686,8 +2619,8 @@
string sep = "_";
string javaname = funname;
if (fullcamel_style_) {
- sep = "";
- javaname = as_camel_case(funname);
+ sep = "";
+ javaname = as_camel_case(funname);
}
// Open function
@@ -2717,7 +2650,8 @@
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "recv" << sep << javaname << "();" << endl;
+ f_service_ <<
+ "recv" << sep << javaname << "();" << endl;
}
scope_down(f_service_);
f_service_ << endl;
@@ -2729,15 +2663,15 @@
string argsname = (*f_iter)->get_name() + "_args";
// Open function
- indent(f_service_) << "public " << function_signature(&send_function) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(&send_function) << endl;
scope_up(f_service_);
// Serialize the request
indent(f_service_) << argsname << " args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- indent(f_service_) << "args.set" << get_cap_name((*fld_iter)->get_name()) << "("
- << (*fld_iter)->get_name() << ");" << endl;
+ indent(f_service_) << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl;
}
const string sendBaseName = (*f_iter)->is_oneway() ? "sendBaseOneway" : "sendBase";
@@ -2755,35 +2689,38 @@
&noargs,
(*f_iter)->get_xceptions());
// Open function
- indent(f_service_) << "public " << function_signature(&recv_function) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(&recv_function) << endl;
scope_up(f_service_);
- f_service_ << indent() << resultname << " result = new " << resultname << "();" << endl
- << indent() << "receiveBase(result, \"" << funname << "\");" << endl;
+ f_service_ <<
+ indent() << resultname << " result = new " << resultname << "();" << endl <<
+ indent() << "receiveBase(result, \"" << funname << "\");" << endl;
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (result." << generate_isset_check("success") << ") {" << endl
- << indent() << " return result.success;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << generate_isset_check("success") << ") {" << endl <<
+ indent() << " return result.success;" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl
- << indent() << " throw result." << (*x_iter)->get_name() << ";" << endl
- << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl <<
+ indent() << " throw result." << (*x_iter)->get_name() << ";" << endl <<
+ indent() << "}" << endl;
}
// If you get here it's an exception, unless a void function
if ((*f_iter)->get_returntype()->is_void()) {
indent(f_service_) << "return;" << endl;
} else {
- f_service_ << indent() << "throw new "
- "org.apache.thrift.TApplicationException(org.apache.thrift."
- "TApplicationException.MISSING_RESULT, \""
- << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
+ f_service_ <<
+ indent() << "throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
// Close function
@@ -2803,35 +2740,24 @@
extends = type_name(tservice->get_extends()) + ".AsyncClient";
}
- indent(f_service_) << "public static class AsyncClient extends " << extends
- << " implements AsyncIface {" << endl;
+ indent(f_service_) <<
+ "public static class AsyncClient extends " << extends << " implements AsyncIface {" << endl;
indent_up();
// Factory method
- indent(f_service_) << "public static class Factory implements "
- "org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {" << endl;
- indent(f_service_) << " private org.apache.thrift.async.TAsyncClientManager clientManager;"
- << endl;
- indent(f_service_) << " private org.apache.thrift.protocol.TProtocolFactory protocolFactory;"
- << endl;
- indent(f_service_) << " public Factory(org.apache.thrift.async.TAsyncClientManager "
- "clientManager, org.apache.thrift.protocol.TProtocolFactory "
- "protocolFactory) {" << endl;
+ indent(f_service_) << "public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {" << endl;
+ indent(f_service_) << " private org.apache.thrift.async.TAsyncClientManager clientManager;" << endl;
+ indent(f_service_) << " private org.apache.thrift.protocol.TProtocolFactory protocolFactory;" << endl;
+ indent(f_service_) << " public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {" << endl;
indent(f_service_) << " this.clientManager = clientManager;" << endl;
indent(f_service_) << " this.protocolFactory = protocolFactory;" << endl;
indent(f_service_) << " }" << endl;
- indent(f_service_) << " public AsyncClient "
- "getAsyncClient(org.apache.thrift.transport.TNonblockingTransport "
- "transport) {" << endl;
- indent(f_service_) << " return new AsyncClient(protocolFactory, clientManager, transport);"
- << endl;
+ indent(f_service_) << " public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {" << endl;
+ indent(f_service_) << " return new AsyncClient(protocolFactory, clientManager, transport);" << endl;
indent(f_service_) << " }" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "public AsyncClient(org.apache.thrift.protocol.TProtocolFactory "
- "protocolFactory, org.apache.thrift.async.TAsyncClientManager "
- "clientManager, org.apache.thrift.transport.TNonblockingTransport "
- "transport) {" << endl;
+ indent(f_service_) << "public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {" << endl;
indent(f_service_) << " super(protocolFactory, clientManager, transport);" << endl;
indent(f_service_) << "}" << endl << endl;
@@ -2843,8 +2769,8 @@
string sep = "_";
string javaname = funname;
if (fullcamel_style_) {
- sep = "";
- javaname = as_camel_case(javaname);
+ sep = "";
+ javaname = as_camel_case(javaname);
}
t_type* ret_type = (*f_iter)->get_returntype();
t_struct* arg_struct = (*f_iter)->get_arglist();
@@ -2856,12 +2782,9 @@
string result_name = (*f_iter)->get_name() + "_result";
// Main method body
- indent(f_service_) << "public " << function_signature_async(*f_iter, false)
- << " throws org.apache.thrift.TException {" << endl;
+ indent(f_service_) << "public " << function_signature_async(*f_iter, false) << " throws org.apache.thrift.TException {" << endl;
indent(f_service_) << " checkReady();" << endl;
- indent(f_service_) << " " << funclassname << " method_call = new " + funclassname + "("
- << async_argument_list(*f_iter, arg_struct, ret_type)
- << ", this, ___protocolFactory, ___transport);" << endl;
+ indent(f_service_) << " " << funclassname << " method_call = new " + funclassname + "(" << async_argument_list(*f_iter, arg_struct, ret_type) << ", this, ___protocolFactory, ___transport);" << endl;
indent(f_service_) << " this.___currentMethod = method_call;" << endl;
indent(f_service_) << " ___manager.call(method_call);" << endl;
indent(f_service_) << "}" << endl;
@@ -2869,55 +2792,47 @@
f_service_ << endl;
// TAsyncMethod object for this function call
- indent(f_service_) << "public static class " + funclassname
- + " extends org.apache.thrift.async.TAsyncMethodCall {" << endl;
+ indent(f_service_) << "public static class " + funclassname + " extends org.apache.thrift.async.TAsyncMethodCall {" << endl;
indent_up();
// Member variables
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- indent(f_service_) << "private " + type_name((*fld_iter)->get_type()) + " "
- + (*fld_iter)->get_name() + ";" << endl;
+ indent(f_service_) << "private " + type_name((*fld_iter)->get_type()) + " " + (*fld_iter)->get_name() + ";" << endl;
}
// NOTE since we use a new Client instance to deserialize, let's keep seqid to 0 for now
// indent(f_service_) << "private int seqid;" << endl << endl;
// Constructor
- indent(f_service_) << "public " + funclassname + "("
- + async_argument_list(*f_iter, arg_struct, ret_type, true)
- << ", org.apache.thrift.async.TAsyncClient client, "
- "org.apache.thrift.protocol.TProtocolFactory protocolFactory, "
- "org.apache.thrift.transport.TNonblockingTransport transport) throws "
- "org.apache.thrift.TException {" << endl;
- indent(f_service_) << " super(client, protocolFactory, transport, resultHandler, "
- << ((*f_iter)->is_oneway() ? "true" : "false") << ");" << endl;
+ indent(f_service_) << "public " + funclassname + "(" + async_argument_list(*f_iter, arg_struct, ret_type, true) << ", org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException {" << endl;
+ indent(f_service_) << " super(client, protocolFactory, transport, resultHandler, " << ((*f_iter)->is_oneway() ? "true" : "false") << ");" << endl;
// Assign member variables
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- indent(f_service_) << " this." + (*fld_iter)->get_name() + " = " + (*fld_iter)->get_name()
- + ";" << endl;
+ indent(f_service_) << " this." + (*fld_iter)->get_name() + " = " + (*fld_iter)->get_name() + ";" << endl;
}
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "public void write_args(org.apache.thrift.protocol.TProtocol prot) "
- "throws org.apache.thrift.TException {" << endl;
+ indent(f_service_) << "public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {" << endl;
indent_up();
// Serialize request
// NOTE we are leaving seqid as 0, for now (see above)
- f_service_ << indent() << "prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage(\""
- << funname << "\", org.apache.thrift.protocol."
- << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL") << ", 0));"
- << endl << indent() << args_name << " args = new " << args_name << "();" << endl;
+ f_service_ <<
+ indent() << "prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage(\"" <<
+ funname << "\", org.apache.thrift.protocol." <<
+ ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL") <<
+ ", 0));" << endl <<
+ indent() << args_name << " args = new " << args_name << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "("
- << (*fld_iter)->get_name() << ");" << endl;
+ f_service_ << indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl;
}
- f_service_ << indent() << "args.write(prot);" << endl << indent() << "prot.writeMessageEnd();"
- << endl;
+ f_service_ <<
+ indent() << "args.write(prot);" << endl <<
+ indent() << "prot.writeMessageEnd();" << endl;
indent_down();
indent(f_service_) << "}" << endl << endl;
@@ -2931,15 +2846,12 @@
f_service_ << "org.apache.thrift.TException {" << endl;
indent_up();
- f_service_
- << indent()
- << "if (getState() != org.apache.thrift.async.TAsyncMethodCall.State.RESPONSE_READ) {"
- << endl << indent() << " throw new IllegalStateException(\"Method call not finished!\");"
- << endl << indent() << "}" << endl << indent()
- << "org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new "
- "org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array());" << endl
- << indent() << "org.apache.thrift.protocol.TProtocol prot = "
- "client.getProtocolFactory().getProtocol(memoryTransport);" << endl;
+ f_service_ <<
+ indent() << "if (getState() != org.apache.thrift.async.TAsyncMethodCall.State.RESPONSE_READ) {" << endl <<
+ indent() << " throw new IllegalStateException(\"Method call not finished!\");" << endl <<
+ indent() << "}" << endl <<
+ indent() << "org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array());" << endl <<
+ indent() << "org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport);" << endl;
if (!(*f_iter)->is_oneway()) {
indent(f_service_);
if (!ret_type->is_void()) {
@@ -2983,34 +2895,24 @@
}
// Generate the header portion
- indent(f_service_) << "public static class Processor<I extends Iface> extends "
- << extends_processor << " implements org.apache.thrift.TProcessor {" << endl;
+ indent(f_service_) <<
+ "public static class Processor<I extends Iface> extends " << extends_processor << " implements org.apache.thrift.TProcessor {" << endl;
indent_up();
- indent(f_service_)
- << "private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());"
- << endl;
+ indent(f_service_) << "private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());" << endl;
indent(f_service_) << "public Processor(I iface) {" << endl;
- indent(f_service_) << " super(iface, getProcessMap(new HashMap<String, "
- "org.apache.thrift.ProcessFunction<I, ? extends "
- "org.apache.thrift.TBase>>()));" << endl;
+ indent(f_service_) << " super(iface, getProcessMap(new HashMap<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>>()));" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "protected Processor(I iface, Map<String, "
- "org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> "
- "processMap) {" << endl;
+ indent(f_service_) << "protected Processor(I iface, Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {" << endl;
indent(f_service_) << " super(iface, getProcessMap(processMap));" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "private static <I extends Iface> Map<String, "
- "org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> "
- "getProcessMap(Map<String, org.apache.thrift.ProcessFunction<I, ? extends "
- " org.apache.thrift.TBase>> processMap) {" << endl;
+ indent(f_service_) << "private static <I extends Iface> Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> getProcessMap(Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {" << endl;
indent_up();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent(f_service_) << "processMap.put(\"" << (*f_iter)->get_name() << "\", new "
- << (*f_iter)->get_name() << "());" << endl;
+ indent(f_service_) << "processMap.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl;
}
indent(f_service_) << "return processMap;" << endl;
indent_down();
@@ -3046,34 +2948,25 @@
}
// Generate the header portion
- indent(f_service_) << "public static class AsyncProcessor<I extends AsyncIface> extends "
- << extends_processor << " {" << endl;
+ indent(f_service_) <<
+ "public static class AsyncProcessor<I extends AsyncIface> extends " << extends_processor << " {" << endl;
indent_up();
- indent(f_service_) << "private static final Logger LOGGER = "
- "LoggerFactory.getLogger(AsyncProcessor.class.getName());" << endl;
+ indent(f_service_) << "private static final Logger LOGGER = LoggerFactory.getLogger(AsyncProcessor.class.getName());" << endl;
indent(f_service_) << "public AsyncProcessor(I iface) {" << endl;
- indent(f_service_) << " super(iface, getProcessMap(new HashMap<String, "
- "org.apache.thrift.AsyncProcessFunction<I, ? extends "
- "org.apache.thrift.TBase, ?>>()));" << endl;
+ indent(f_service_) << " super(iface, getProcessMap(new HashMap<String, org.apache.thrift.AsyncProcessFunction<I, ? extends org.apache.thrift.TBase, ?>>()));" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "protected AsyncProcessor(I iface, Map<String, "
- "org.apache.thrift.AsyncProcessFunction<I, ? extends "
- "org.apache.thrift.TBase, ?>> processMap) {" << endl;
+ indent(f_service_) << "protected AsyncProcessor(I iface, Map<String, org.apache.thrift.AsyncProcessFunction<I, ? extends org.apache.thrift.TBase, ?>> processMap) {" << endl;
indent(f_service_) << " super(iface, getProcessMap(processMap));" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "private static <I extends AsyncIface> Map<String, "
- "org.apache.thrift.AsyncProcessFunction<I, ? extends "
- "org.apache.thrift.TBase,?>> getProcessMap(Map<String, "
- "org.apache.thrift.AsyncProcessFunction<I, ? extends "
- "org.apache.thrift.TBase, ?>> processMap) {" << endl;
+
+ indent(f_service_) << "private static <I extends AsyncIface> Map<String, org.apache.thrift.AsyncProcessFunction<I, ? extends org.apache.thrift.TBase,?>> getProcessMap(Map<String, org.apache.thrift.AsyncProcessFunction<I, ? extends org.apache.thrift.TBase, ?>> processMap) {" << endl;
indent_up();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent(f_service_) << "processMap.put(\"" << (*f_iter)->get_name() << "\", new "
- << (*f_iter)->get_name() << "());" << endl;
+ indent(f_service_) << "processMap.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl;
}
indent(f_service_) << "return processMap;" << endl;
indent_down();
@@ -3088,6 +2981,7 @@
indent(f_service_) << "}" << endl << endl;
}
+
/**
* Generates a struct and helpers for a function.
*
@@ -3114,26 +3008,29 @@
generate_java_struct_definition(f_service_, &result, false, true, true);
}
+
+
/**
* Generates a process function definition.
*
* @param tfunction The function to write a dispatcher for
*/
-void t_java_generator::generate_process_async_function(t_service* tservice, t_function* tfunction) {
+void t_java_generator::generate_process_async_function(t_service* tservice,
+ t_function* tfunction) {
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
- if (tfunction->is_oneway()) {
- resultname = "org.apache.thrift.TBase";
- }
+ if (tfunction->is_oneway()) {
+ resultname = "org.apache.thrift.TBase";
+ }
- string resulttype = type_name(tfunction->get_returntype(), true);
+ string resulttype = type_name(tfunction->get_returntype(),true);
- (void)tservice;
+
+ (void) tservice;
// Open class
- indent(f_service_) << "public static class " << tfunction->get_name()
- << "<I extends AsyncIface> extends org.apache.thrift.AsyncProcessFunction<I, "
- << argsname << ", " << resulttype << "> {" << endl;
+ indent(f_service_) <<
+ "public static class " << tfunction->get_name() << "<I extends AsyncIface> extends org.apache.thrift.AsyncProcessFunction<I, " << argsname << ", "<<resulttype<<"> {" << endl;
indent_up();
indent(f_service_) << "public " << tfunction->get_name() << "() {" << endl;
@@ -3144,101 +3041,90 @@
indent(f_service_) << " return new " << argsname << "();" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "public AsyncMethodCallback<" << resulttype
- << "> getResultHandler(final AsyncFrameBuffer fb, final int seqid) {" << endl;
+ indent(f_service_) << "public AsyncMethodCallback<"<<resulttype<<"> getResultHandler(final AsyncFrameBuffer fb, final int seqid) {" << endl;
indent_up();
- indent(f_service_) << "final org.apache.thrift.AsyncProcessFunction fcall = this;" << endl;
- indent(f_service_) << "return new AsyncMethodCallback<" << resulttype << ">() { " << endl;
+ indent(f_service_) << "final org.apache.thrift.AsyncProcessFunction fcall = this;"<<endl;
+ indent(f_service_) << "return new AsyncMethodCallback<"<<resulttype<<">() { " << endl;
indent_up();
- indent(f_service_) << "public void onComplete(" << resulttype << " o) {" << endl;
+ indent(f_service_) << "public void onComplete(" << resulttype <<" o) {" << endl;
indent_up();
if (!tfunction->is_oneway()) {
- indent(f_service_) << resultname << " result = new " << resultname << "();" << endl;
+ indent(f_service_) <<resultname<<" result = new "<<resultname<<"();"<<endl;
if (!tfunction->get_returntype()->is_void()) {
- indent(f_service_) << "result.success = o;" << endl;
+ indent(f_service_) << "result.success = o;"<<endl;
// Set isset on success field
if (!type_can_be_null(tfunction->get_returntype())) {
- indent(f_service_) << "result.set" << get_cap_name("success") << get_cap_name("isSet")
- << "(true);" << endl;
+ indent(f_service_) << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl;
}
}
- indent(f_service_) << "try {" << endl;
- indent(f_service_)
- << " fcall.sendResponse(fb,result, org.apache.thrift.protocol.TMessageType.REPLY,seqid);"
- << endl;
- indent(f_service_) << " return;" << endl;
- indent(f_service_) << "} catch (Exception e) {" << endl;
- indent(f_service_) << " LOGGER.error(\"Exception writing to internal frame buffer\", e);"
- << endl;
- indent(f_service_) << "}" << endl;
- indent(f_service_) << "fb.close();" << endl;
+ indent(f_service_) << "try {"<<endl;
+ indent(f_service_) << " fcall.sendResponse(fb,result, org.apache.thrift.protocol.TMessageType.REPLY,seqid);"<<endl;
+ indent(f_service_) << " return;"<<endl;
+ indent(f_service_) << "} catch (Exception e) {"<<endl;
+ indent(f_service_) << " LOGGER.error(\"Exception writing to internal frame buffer\", e);"<<endl;
+ indent(f_service_) << "}"<<endl;
+ indent(f_service_) << "fb.close();"<<endl;
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) << "}" <<endl;
- indent(f_service_) << "public void onError(Exception e) {" << endl;
+ indent(f_service_) << "public void onError(Exception e) {"<<endl;
indent_up();
+
+
if (!tfunction->is_oneway()) {
- indent(f_service_) << "byte msgType = org.apache.thrift.protocol.TMessageType.REPLY;" << endl;
- indent(f_service_) << "org.apache.thrift.TBase msg;" << endl;
- indent(f_service_) << resultname << " result = new " << resultname << "();" << endl;
+ indent(f_service_) <<"byte msgType = org.apache.thrift.protocol.TMessageType.REPLY;"<<endl;
+ indent(f_service_) <<"org.apache.thrift.TBase msg;"<<endl;
+ indent(f_service_) <<resultname<<" result = new "<<resultname<<"();"<<endl;
- t_struct* xs = tfunction->get_xceptions();
- const std::vector<t_field*>& xceptions = xs->get_members();
- vector<t_field*>::const_iterator x_iter;
- if (xceptions.size() > 0) {
- for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- if (x_iter != xceptions.begin())
- indent(f_service_) << "else ";
- indent(f_service_) << "if (e instanceof " << type_name((*x_iter)->get_type(), false, false)
- << ") {" << endl;
- indent(f_service_) << indent() << "result." << (*x_iter)->get_name() << " = ("
- << type_name((*x_iter)->get_type(), false, false) << ") e;" << endl;
- indent(f_service_) << indent() << "result.set" << get_cap_name((*x_iter)->get_name())
- << get_cap_name("isSet") << "(true);" << endl;
- indent(f_service_) << indent() << "msg = result;" << endl;
+ t_struct* xs = tfunction->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ if (xceptions.size() > 0) {
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ if (x_iter != xceptions.begin()) indent(f_service_) << "else ";
+ indent(f_service_) << "if (e instanceof " << type_name((*x_iter)->get_type(), false, false)<<") {" << endl;
+ indent(f_service_) << indent() << "result." << (*x_iter)->get_name() << " = (" << type_name((*x_iter)->get_type(), false, false) << ") e;" << endl;
+ indent(f_service_) << indent() << "result.set" << get_cap_name((*x_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl;
+ indent(f_service_) << indent() << "msg = result;"<<endl;
- indent(f_service_) << "}" << endl;
- }
- indent(f_service_) << " else " << endl;
- }
+ indent(f_service_) << "}"<<endl;
+ }
+ indent(f_service_) << " else "<<endl;
+ }
- indent(f_service_) << "{" << endl;
- indent_up();
- indent(f_service_) << "msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION;" << endl;
- indent(f_service_) << "msg = (org.apache.thrift.TBase)new "
- "org.apache.thrift.TApplicationException(org.apache.thrift."
- "TApplicationException.INTERNAL_ERROR, e.getMessage());" << endl;
- indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) << "{"<<endl;
+ indent_up();
+ indent(f_service_) << "msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION;"<<endl;
+ indent(f_service_) << "msg = (org.apache.thrift.TBase)new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage());"<<endl;
+ indent_down();
+ indent(f_service_) << "}"<<endl;
- indent(f_service_) << "try {" << endl;
- indent(f_service_) << " fcall.sendResponse(fb,msg,msgType,seqid);" << endl;
- indent(f_service_) << " return;" << endl;
- indent(f_service_) << "} catch (Exception ex) {" << endl;
- indent(f_service_) << " LOGGER.error(\"Exception writing to internal frame buffer\", ex);"
- << endl;
- indent(f_service_) << "}" << endl;
- indent(f_service_) << "fb.close();" << endl;
+
+ indent(f_service_) << "try {"<<endl;
+ indent(f_service_) << " fcall.sendResponse(fb,msg,msgType,seqid);"<<endl;
+ indent(f_service_) << " return;"<<endl;
+ indent(f_service_) << "} catch (Exception ex) {"<<endl;
+ indent(f_service_) << " LOGGER.error(\"Exception writing to internal frame buffer\", ex);"<<endl;
+ indent(f_service_) << "}"<<endl;
+ indent(f_service_) << "fb.close();"<<endl;
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) << "}" <<endl;
indent_down();
- indent(f_service_) << "};" << endl;
+ indent(f_service_) << "};" <<endl;
indent_down();
indent(f_service_) << "}" << endl << endl;
indent(f_service_) << "protected boolean isOneway() {" << endl;
- indent(f_service_) << " return " << ((tfunction->is_oneway()) ? "true" : "false") << ";" << endl;
+ indent(f_service_) << " return " << ((tfunction->is_oneway())?"true":"false") << ";" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "public void start(I iface, " << argsname
- << " args, org.apache.thrift.async.AsyncMethodCallback<" << resulttype
- << "> resultHandler) throws TException {" << endl;
+ indent(f_service_) << "public void start(I iface, " << argsname << " args, org.apache.thrift.async.AsyncMethodCallback<"<<resulttype<<"> resultHandler) throws TException {" << endl;
indent_up();
// Generate the function call
@@ -3258,10 +3144,11 @@
f_service_ << "args." << (*f_iter)->get_name();
}
if (!first)
- f_service_ << ",";
+ f_service_ << ",";
f_service_ << "resultHandler";
f_service_ << ");" << endl;
+
indent_down();
indent(f_service_) << "}";
@@ -3278,18 +3165,18 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_java_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
+void t_java_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
if (tfunction->is_oneway()) {
resultname = "org.apache.thrift.TBase";
}
- (void)tservice;
+ (void) tservice;
// Open class
- indent(f_service_) << "public static class " << tfunction->get_name()
- << "<I extends Iface> extends org.apache.thrift.ProcessFunction<I, "
- << argsname << "> {" << endl;
+ indent(f_service_) <<
+ "public static class " << tfunction->get_name() << "<I extends Iface> extends org.apache.thrift.ProcessFunction<I, " << argsname << "> {" << endl;
indent_up();
indent(f_service_) << "public " << tfunction->get_name() << "() {" << endl;
@@ -3301,11 +3188,10 @@
indent(f_service_) << "}" << endl << endl;
indent(f_service_) << "protected boolean isOneway() {" << endl;
- indent(f_service_) << " return " << ((tfunction->is_oneway()) ? "true" : "false") << ";" << endl;
+ indent(f_service_) << " return " << ((tfunction->is_oneway())?"true":"false") << ";" << endl;
indent(f_service_) << "}" << endl << endl;
- indent(f_service_) << "public " << resultname << " getResult(I iface, " << argsname
- << " args) throws org.apache.thrift.TException {" << endl;
+ indent(f_service_) << "public " << resultname << " getResult(I iface, " << argsname << " args) throws org.apache.thrift.TException {" << endl;
indent_up();
if (!tfunction->is_oneway()) {
indent(f_service_) << resultname << " result = new " << resultname << "();" << endl;
@@ -3343,22 +3229,19 @@
f_service_ << ");" << endl;
// Set isset on success field
- if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()
- && !type_can_be_null(tfunction->get_returntype())) {
- indent(f_service_) << "result.set" << get_cap_name("success") << get_cap_name("isSet")
- << "(true);" << endl;
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void() && !type_can_be_null(tfunction->get_returntype())) {
+ indent(f_service_) << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl;
}
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
f_service_ << indent() << "}";
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " "
- << (*x_iter)->get_name() << ") {" << endl;
+ f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent() << "}";
} else {
@@ -3384,6 +3267,7 @@
f_service_ << indent() << "}" << endl << endl;
}
+
/**
* Deserializes a field of any type.
*
@@ -3392,8 +3276,7 @@
*/
void t_java_generator::generate_deserialize_field(ofstream& out,
t_field* tfield,
- string prefix,
- bool has_metadata) {
+ string prefix, bool has_metadata) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
@@ -3403,54 +3286,54 @@
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ name);
} else if (type->is_container()) {
generate_deserialize_container(out, type, name, has_metadata);
} else if (type->is_base_type()) {
indent(out) << name << " = iprot.";
+
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:
- if (((t_base_type*)type)->is_binary()) {
- out << "readBinary();";
- } else {
- out << "readString();";
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "readBool();";
- break;
- case t_base_type::TYPE_BYTE:
- out << "readByte();";
- break;
- case t_base_type::TYPE_I16:
- out << "readI16();";
- break;
- case t_base_type::TYPE_I32:
- out << "readI32();";
- break;
- case t_base_type::TYPE_I64:
- out << "readI64();";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "readDouble();";
- break;
- default:
- throw "compiler error: no Java name for base type " + t_base_type::t_base_name(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:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "readBinary();";
+ } else {
+ out << "readString();";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "readBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "readByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "readI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "readI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "readI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "readDouble();";
+ break;
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
}
out << endl;
} else if (type->is_enum()) {
- indent(out) << name << " = "
- << type_name(tfield->get_type(), true, false, false, true)
- + ".findByValue(iprot.readI32());" << endl;
+ indent(out) << name << " = " << type_name(tfield->get_type(), true, false, false, true) + ".findByValue(iprot.readI32());" << endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
@@ -3461,16 +3344,16 @@
t_struct* tstruct,
string prefix) {
- if (reuse_objects_) {
- indent(out) << "if (" << prefix << " == null) {" << endl;
- indent_up();
- }
- indent(out) << prefix << " = new " << type_name(tstruct) << "();" << endl;
- if (reuse_objects_) {
- indent_down();
- indent(out) << "}" << endl;
- }
- indent(out) << prefix << ".read(iprot);" << endl;
+ if (reuse_objects_) {
+ indent(out) << "if (" << prefix << " == null) {" << endl;
+ indent_up();
+ }
+ indent(out) << prefix << " = new " << type_name(tstruct) << "();" << endl;
+ if (reuse_objects_) {
+ indent_down();
+ indent(out) << "}" << endl;
+ }
+ indent(out) << prefix << ".read(iprot);" << endl;
}
/**
@@ -3478,8 +3361,7 @@
*/
void t_java_generator::generate_deserialize_container(ofstream& out,
t_type* ttype,
- string prefix,
- bool has_metadata) {
+ string prefix, bool has_metadata) {
scope_up(out);
@@ -3496,33 +3378,23 @@
if (has_metadata) {
// Declare variables, read header
if (ttype->is_map()) {
- indent(out) << "org.apache.thrift.protocol.TMap " << obj << " = iprot.readMapBegin();"
- << endl;
+ indent(out) << "org.apache.thrift.protocol.TMap " << obj << " = iprot.readMapBegin();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "org.apache.thrift.protocol.TSet " << obj << " = iprot.readSetBegin();"
- << endl;
+ indent(out) << "org.apache.thrift.protocol.TSet " << obj << " = iprot.readSetBegin();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "org.apache.thrift.protocol.TList " << obj << " = iprot.readListBegin();"
- << endl;
+ indent(out) << "org.apache.thrift.protocol.TList " << obj << " = iprot.readListBegin();" << endl;
}
} else {
// Declare variables, read header
if (ttype->is_map()) {
- indent(out) << "org.apache.thrift.protocol.TMap " << obj
- << " = new org.apache.thrift.protocol.TMap("
- << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "iprot.readI32());" << endl;
+ indent(out) << "org.apache.thrift.protocol.TMap " << obj << " = new org.apache.thrift.protocol.TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << "iprot.readI32());" << endl;
} else if (ttype->is_set()) {
- indent(out) << "org.apache.thrift.protocol.TSet " << obj
- << " = new org.apache.thrift.protocol.TSet("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());"
- << endl;
+ indent(out) << "org.apache.thrift.protocol.TSet " << obj << " = new org.apache.thrift.protocol.TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());" << endl;
} else if (ttype->is_list()) {
- indent(out) << "org.apache.thrift.protocol.TList " << obj
- << " = new org.apache.thrift.protocol.TList("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());"
- << endl;
+ indent(out) << "org.apache.thrift.protocol.TList " << obj << " = new org.apache.thrift.protocol.TList(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());" << endl;
}
}
@@ -3531,15 +3403,18 @@
indent_up();
}
- out << indent() << prefix << " = new " << type_name(ttype, false, true);
+ out <<
+ indent() << prefix << " = new " << type_name(ttype, false, true);
// size the collection correctly
if (sorted_containers_ && (ttype->is_map() || ttype->is_set())) {
// TreeSet and TreeMap don't have any constructor which takes a capactity as an argument
out << "();" << endl;
} else {
- out << "(" << (ttype->is_list() ? "" : "2*") << obj << ".size"
- << ");" << endl;
+ out << "("
+ << (ttype->is_list() ? "" : "2*" )
+ << obj << ".size"
+ << ");" << endl;
}
if (reuse_objects_) {
@@ -3570,14 +3445,14 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
void t_java_generator::generate_deserialize_map_element(ofstream& out,
t_map* tmap,
string prefix,
- string obj,
- bool has_metadata) {
+ string obj, bool has_metadata) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
@@ -3587,10 +3462,11 @@
indent(out) << declare_field(&fval, reuse_objects_, false) << endl;
// For loop iterates over elements
- string i = tmp("_i");
- indent(out) << "for (int " << i << " = 0; " << i << " < " << obj << ".size"
- << "; "
- << "++" << i << ")" << endl;
+ string i = tmp("_i");
+ indent(out) <<
+ "for (int " << i << " = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
scope_up(out);
@@ -3606,23 +3482,24 @@
void t_java_generator::generate_deserialize_set_element(ofstream& out,
t_set* tset,
string prefix,
- string obj,
- bool has_metadata) {
+ string obj, bool has_metadata) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
indent(out) << declare_field(&felem, reuse_objects_, false) << endl;
// For loop iterates over elements
- string i = tmp("_i");
- indent(out) << "for (int " << i << " = 0; " << i << " < " << obj << ".size"
- << "; "
- << "++" << i << ")" << endl;
+ string i = tmp("_i");
+ indent(out) <<
+ "for (int " << i << " = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
scope_up(out);
generate_deserialize_field(out, &felem, "", has_metadata);
indent(out) << prefix << ".add(" << elem << ");" << endl;
+
}
/**
@@ -3631,18 +3508,18 @@
void t_java_generator::generate_deserialize_list_element(ofstream& out,
t_list* tlist,
string prefix,
- string obj,
- bool has_metadata) {
+ string obj, bool has_metadata) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
indent(out) << declare_field(&felem, reuse_objects_, false) << endl;
// For loop iterates over elements
- string i = tmp("_i");
- indent(out) << "for (int " << i << " = 0; " << i << " < " << obj << ".size"
- << "; "
- << "++" << i << ")" << endl;
+ string i = tmp("_i");
+ indent(out) <<
+ "for (int " << i << " = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
scope_up(out);
generate_deserialize_field(out, &felem, "", has_metadata);
@@ -3658,8 +3535,7 @@
*/
void t_java_generator::generate_serialize_field(ofstream& out,
t_field* tfield,
- string prefix,
- bool has_metadata) {
+ string prefix, bool has_metadata) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
@@ -3668,10 +3544,14 @@
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name(), has_metadata);
- } else if (type->is_enum()) {
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name(), has_metadata);
+ } else if (type->is_enum()){
indent(out) << "oprot.writeI32(" << prefix + tfield->get_name() << ".getValue());" << endl;
} else if (type->is_base_type()) {
string name = prefix + tfield->get_name();
@@ -3680,36 +3560,36 @@
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:
- if (((t_base_type*)type)->is_binary()) {
- out << "writeBinary(" << name << ");";
- } else {
- out << "writeString(" << name << ");";
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "writeBool(" << name << ");";
- break;
- case t_base_type::TYPE_BYTE:
- out << "writeByte(" << name << ");";
- break;
- case t_base_type::TYPE_I16:
- out << "writeI16(" << name << ");";
- break;
- case t_base_type::TYPE_I32:
- out << "writeI32(" << name << ");";
- break;
- case t_base_type::TYPE_I64:
- out << "writeI64(" << name << ");";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "writeDouble(" << name << ");";
- break;
- default:
- throw "compiler error: no Java name for base type " + t_base_type::t_base_name(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:
+ if (((t_base_type*)type)->is_binary()) {
+ out << "writeBinary(" << name << ");";
+ } else {
+ out << "writeString(" << name << ");";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "writeBool(" << name << ");";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "writeByte(" << name << ");";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "writeI16(" << name << ");";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "writeI32(" << name << ");";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "writeI64(" << name << ");";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "writeDouble(" << name << ");";
+ break;
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "writeI32(struct." << name << ");";
@@ -3729,8 +3609,10 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_java_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
+void t_java_generator::generate_serialize_struct(ofstream& out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
out << indent() << prefix << ".write(oprot);" << endl;
}
@@ -3742,24 +3624,26 @@
*/
void t_java_generator::generate_serialize_container(ofstream& out,
t_type* ttype,
- string prefix,
- bool has_metadata) {
+ string prefix, bool has_metadata) {
scope_up(out);
if (has_metadata) {
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapBegin(new org.apache.thrift.protocol.TMap("
- << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix << ".size()));"
- << endl;
+ indent(out) <<
+ "oprot.writeMapBegin(new org.apache.thrift.protocol.TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".size()));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetBegin(new org.apache.thrift.protocol.TSet("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " << prefix
- << ".size()));" << endl;
+ indent(out) <<
+ "oprot.writeSetBegin(new org.apache.thrift.protocol.TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListBegin(new org.apache.thrift.protocol.TList("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix
- << ".size()));" << endl;
+ indent(out) <<
+ "oprot.writeListBegin(new org.apache.thrift.protocol.TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
}
} else {
indent(out) << "oprot.writeI32(" << prefix << ".size());" << endl;
@@ -3767,15 +3651,24 @@
string iter = tmp("_iter");
if (ttype->is_map()) {
- indent(out) << "for (Map.Entry<" << type_name(((t_map*)ttype)->get_key_type(), true, false)
- << ", " << type_name(((t_map*)ttype)->get_val_type(), true, false) << "> " << iter
- << " : " << prefix << ".entrySet())";
+ indent(out) <<
+ "for (Map.Entry<" <<
+ type_name(((t_map*)ttype)->get_key_type(), true, false) << ", " <<
+ type_name(((t_map*)ttype)->get_val_type(), true, false) << "> " << iter <<
+ " : " <<
+ prefix << ".entrySet())";
} else if (ttype->is_set()) {
- indent(out) << "for (" << type_name(((t_set*)ttype)->get_elem_type()) << " " << iter << " : "
- << prefix << ")";
+ indent(out) <<
+ "for (" <<
+ type_name(((t_set*)ttype)->get_elem_type()) << " " << iter <<
+ " : " <<
+ prefix << ")";
} else if (ttype->is_list()) {
- indent(out) << "for (" << type_name(((t_list*)ttype)->get_elem_type()) << " " << iter << " : "
- << prefix << ")";
+ indent(out) <<
+ "for (" <<
+ type_name(((t_list*)ttype)->get_elem_type()) << " " << iter <<
+ " : " <<
+ prefix << ")";
}
out << endl;
@@ -3808,9 +3701,8 @@
void t_java_generator::generate_serialize_map_element(ofstream& out,
t_map* tmap,
string iter,
- string map,
- bool has_metadata) {
- (void)map;
+ string map, bool has_metadata) {
+ (void) map;
t_field kfield(tmap->get_key_type(), iter + ".getKey()");
generate_serialize_field(out, &kfield, "", has_metadata);
t_field vfield(tmap->get_val_type(), iter + ".getValue()");
@@ -3846,11 +3738,7 @@
* @param container Is the type going inside a container?
* @return Java type name, i.e. HashMap<Key,Value>
*/
-string t_java_generator::type_name(t_type* ttype,
- bool in_container,
- bool in_init,
- bool skip_generic,
- bool force_namespace) {
+string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_init, bool skip_generic, bool force_namespace) {
// In Java typedefs are just resolved to their real type
ttype = get_true_type(ttype);
string prefix;
@@ -3858,7 +3746,7 @@
if (ttype->is_base_type()) {
return base_type_name((t_base_type*)ttype, in_container);
} else if (ttype->is_map()) {
- t_map* tmap = (t_map*)ttype;
+ t_map* tmap = (t_map*) ttype;
if (in_init) {
if (sorted_containers_) {
prefix = "TreeMap";
@@ -3868,10 +3756,11 @@
} else {
prefix = "Map";
}
- return prefix + (skip_generic ? "" : "<" + type_name(tmap->get_key_type(), true) + ","
- + type_name(tmap->get_val_type(), true) + ">");
+ return prefix + (skip_generic ? "" : "<" +
+ type_name(tmap->get_key_type(), true) + "," +
+ type_name(tmap->get_val_type(), true) + ">");
} else if (ttype->is_set()) {
- t_set* tset = (t_set*)ttype;
+ t_set* tset = (t_set*) ttype;
if (in_init) {
if (sorted_containers_) {
prefix = "TreeSet";
@@ -3883,7 +3772,7 @@
}
return prefix + (skip_generic ? "" : "<" + type_name(tset->get_elem_type(), true) + ">");
} else if (ttype->is_list()) {
- t_list* tlist = (t_list*)ttype;
+ t_list* tlist = (t_list*) ttype;
if (in_init) {
prefix = "ArrayList";
} else {
@@ -3910,32 +3799,33 @@
* @param tbase The base type
* @param container Is it going in a Java container?
*/
-string t_java_generator::base_type_name(t_base_type* type, bool in_container) {
+string t_java_generator::base_type_name(t_base_type* type,
+ bool in_container) {
t_base_type::t_base tbase = type->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- return (in_container ? "Void" : "void");
- case t_base_type::TYPE_STRING:
- if (type->is_binary()) {
- return "ByteBuffer";
- } else {
- return "String";
- }
- case t_base_type::TYPE_BOOL:
- return (in_container ? "Boolean" : "boolean");
- case t_base_type::TYPE_BYTE:
- return (in_container ? "Byte" : "byte");
- case t_base_type::TYPE_I16:
- return (in_container ? "Short" : "short");
- case t_base_type::TYPE_I32:
- return (in_container ? "Integer" : "int");
- case t_base_type::TYPE_I64:
- return (in_container ? "Long" : "long");
- case t_base_type::TYPE_DOUBLE:
- return (in_container ? "Double" : "double");
- default:
- throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
+ case t_base_type::TYPE_VOID:
+ return (in_container ? "Void" : "void");
+ case t_base_type::TYPE_STRING:
+ if (type->is_binary()) {
+ return "ByteBuffer";
+ } else {
+ return "String";
+ }
+ case t_base_type::TYPE_BOOL:
+ return (in_container ? "Boolean" : "boolean");
+ case t_base_type::TYPE_BYTE:
+ return (in_container ? "Byte" : "byte");
+ case t_base_type::TYPE_I16:
+ return (in_container ? "Short" : "short");
+ case t_base_type::TYPE_I32:
+ return (in_container ? "Integer" : "int");
+ case t_base_type::TYPE_I64:
+ return (in_container ? "Long" : "long");
+ case t_base_type::TYPE_DOUBLE:
+ return (in_container ? "Double" : "double");
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
}
}
@@ -3956,31 +3846,30 @@
} else if (ttype->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
switch (tbase) {
- case t_base_type::TYPE_VOID:
- throw "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- result += " = null";
- break;
- case t_base_type::TYPE_BOOL:
- result += " = false";
- break;
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- result += " = 0";
- break;
- case t_base_type::TYPE_DOUBLE:
- result += " = (double)0";
- break;
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ result += " = null";
+ break;
+ case t_base_type::TYPE_BOOL:
+ result += " = false";
+ break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ result += " = 0";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ result += " = (double)0";
+ break;
}
} else if (ttype->is_enum()) {
result += " = 0";
} else if (ttype->is_container()) {
result += " = new " + type_name(ttype, false, true) + "()";
} else {
- result += " = new " + type_name(ttype, false, true) + "()";
- ;
+ result += " = new " + type_name(ttype, false, true) + "()";;
}
}
result += ";";
@@ -4001,11 +3890,12 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_java_generator::function_signature(t_function* tfunction, string prefix) {
+string t_java_generator::function_signature(t_function* tfunction,
+ string prefix) {
t_type* ttype = tfunction->get_returntype();
std::string fn_name = get_rpc_method_name(tfunction->get_name());
- std::string result = type_name(ttype) + " " + prefix + fn_name + "("
- + argument_list(tfunction->get_arglist()) + ") throws ";
+ std::string result =
+ type_name(ttype) + " " + prefix + fn_name + "(" + argument_list(tfunction->get_arglist()) + ") throws ";
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
@@ -4022,9 +3912,7 @@
* @params tfunction Function definition
* @return String of rendered function definition
*/
-string t_java_generator::function_signature_async(t_function* tfunction,
- bool use_base_method,
- string prefix) {
+string t_java_generator::function_signature_async(t_function* tfunction, bool use_base_method, string prefix) {
std::string arglist = async_function_call_arglist(tfunction, use_base_method, true);
std::string ret_type = "";
@@ -4039,10 +3927,8 @@
return result;
}
-string t_java_generator::async_function_call_arglist(t_function* tfunc,
- bool use_base_method,
- bool include_types) {
- (void)use_base_method;
+string t_java_generator::async_function_call_arglist(t_function* tfunc, bool use_base_method, bool include_types) {
+ (void) use_base_method;
std::string arglist = "";
if (tfunc->get_arglist()->get_members().size() > 0) {
arglist = argument_list(tfunc->get_arglist(), include_types) + ", ";
@@ -4079,12 +3965,9 @@
return result;
}
-string t_java_generator::async_argument_list(t_function* tfunct,
- t_struct* tstruct,
- t_type* ttype,
- bool include_types) {
- (void)tfunct;
- (void)ttype;
+string t_java_generator::async_argument_list(t_function* tfunct, t_struct* tstruct, t_type* ttype, bool include_types) {
+ (void) tfunct;
+ (void) ttype;
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -4119,22 +4002,22 @@
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 "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- return "org.apache.thrift.protocol.TType.STRING";
- case t_base_type::TYPE_BOOL:
- return "org.apache.thrift.protocol.TType.BOOL";
- case t_base_type::TYPE_BYTE:
- return "org.apache.thrift.protocol.TType.BYTE";
- case t_base_type::TYPE_I16:
- return "org.apache.thrift.protocol.TType.I16";
- case t_base_type::TYPE_I32:
- return "org.apache.thrift.protocol.TType.I32";
- case t_base_type::TYPE_I64:
- return "org.apache.thrift.protocol.TType.I64";
- case t_base_type::TYPE_DOUBLE:
- return "org.apache.thrift.protocol.TType.DOUBLE";
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "org.apache.thrift.protocol.TType.STRING";
+ case t_base_type::TYPE_BOOL:
+ return "org.apache.thrift.protocol.TType.BOOL";
+ case t_base_type::TYPE_BYTE:
+ return "org.apache.thrift.protocol.TType.BYTE";
+ case t_base_type::TYPE_I16:
+ return "org.apache.thrift.protocol.TType.I16";
+ case t_base_type::TYPE_I32:
+ return "org.apache.thrift.protocol.TType.I32";
+ case t_base_type::TYPE_I64:
+ return "org.apache.thrift.protocol.TType.I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "org.apache.thrift.protocol.TType.DOUBLE";
}
} else if (type->is_enum()) {
return "org.apache.thrift.protocol.TType.I32";
@@ -4157,9 +4040,9 @@
* @param fromName The name which shall become a valid Java source file name
* @return The produced identifier
*/
-std::string t_java_generator::make_valid_java_filename(std::string const& fromName) {
- // if any further rules apply to source file names in Java, modify as necessary
- return make_valid_java_identifier(fromName);
+std::string t_java_generator::make_valid_java_filename( std::string const & fromName) {
+ // if any further rules apply to source file names in Java, modify as necessary
+ return make_valid_java_identifier(fromName);
}
/**
@@ -4168,52 +4051,56 @@
* @param fromName The name which shall become a valid Java identifier
* @return The produced identifier
*/
-std::string t_java_generator::make_valid_java_identifier(std::string const& fromName) {
- std::string str = fromName;
- if (str.empty()) {
- return str;
- }
-
- // tests rely on this
- assert(('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
-
- // if the first letter is a number, we add an additional underscore in front of it
- char c = str.at(0);
- if (('0' <= c) && (c <= '9')) {
- str = "_" + str;
- }
-
- // following chars: letter, number or underscore
- for (size_t i = 0; i < str.size(); ++i) {
- c = str.at(i);
- if ((('A' > c) || (c > 'Z')) && (('a' > c) || (c > 'z')) && (('0' > c) || (c > '9'))
- && ('_' != c)) {
- str.replace(i, 1, "_");
+std::string t_java_generator::make_valid_java_identifier( std::string const & fromName) {
+ std::string str = fromName;
+ if( str.empty()) {
+ return str;
}
- }
- return str;
+ // tests rely on this
+ assert( ('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
+
+ // if the first letter is a number, we add an additional underscore in front of it
+ char c = str.at(0);
+ if( ('0' <= c) && (c <= '9')) {
+ str = "_" + str;
+ }
+
+ // following chars: letter, number or underscore
+ for( size_t i = 0; i < str.size(); ++i) {
+ c = str.at(i);
+ if( (('A' > c) || (c > 'Z')) &&
+ (('a' > c) || (c > 'z')) &&
+ (('0' > c) || (c > '9')) &&
+ ('_' != c) ) {
+ str.replace( i, 1, "_");
+ }
+ }
+
+ return str;
}
+
std::string t_java_generator::as_camel_case(std::string name, bool ucfirst) {
std::string new_name;
size_t i = 0;
for (i = 0; i < name.size(); i++) {
- if (name[i] != '_')
- break;
+ if (name[i] != '_') break;
}
if (ucfirst) {
new_name += toupper(name[i++]);
- } else {
+ }
+ else {
new_name += tolower(name[i++]);
}
for (; i < name.size(); i++) {
if (name[i] == '_') {
- if (i < name.size() - 1) {
+ if (i < name.size()-1) {
i++;
new_name += toupper(name[i]);
}
- } else {
+ }
+ else {
new_name += name[i];
}
}
@@ -4232,7 +4119,7 @@
* Applies the correct style to a string based on the value of nocamel_style_
* and/or fullcamel_style_
*/
-std::string t_java_generator::get_cap_name(std::string name) {
+std::string t_java_generator::get_cap_name(std::string name){
if (nocamel_style_) {
return "_" + name;
} else if (fullcamel_style_) {
@@ -4265,11 +4152,8 @@
return constant_name;
}
-void t_java_generator::generate_deep_copy_container(ofstream& out,
- std::string source_name_p1,
- std::string source_name_p2,
- std::string result_name,
- t_type* type) {
+void t_java_generator::generate_deep_copy_container(ofstream &out, std::string source_name_p1, std::string source_name_p2,
+ std::string result_name, t_type* type) {
t_container* container = (t_container*)type;
std::string source_name;
@@ -4280,20 +4164,17 @@
bool copy_construct_container;
if (container->is_map()) {
- t_map* tmap = (t_map*)container;
- copy_construct_container = tmap->get_key_type()->is_base_type()
- && tmap->get_val_type()->is_base_type();
+ t_map *tmap = (t_map *)container;
+ copy_construct_container = tmap->get_key_type()->is_base_type() && tmap->get_val_type()->is_base_type();
} else {
- t_type* elem_type = container->is_list() ? ((t_list*)container)->get_elem_type()
- : ((t_set*)container)->get_elem_type();
+ t_type* elem_type = container->is_list() ? ((t_list *) container)->get_elem_type() :
+ ((t_set *) container)->get_elem_type();
copy_construct_container = elem_type->is_base_type();
}
if (copy_construct_container) {
- // deep copy of base types can be done much more efficiently than iterating over all the
- // elements manually
- indent(out) << type_name(type, true, false) << " " << result_name << " = new "
- << type_name(container, false, true) << "(" << source_name << ");" << endl;
+ // deep copy of base types can be done much more efficiently than iterating over all the elements manually
+ indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "(" << source_name << ");" << endl;
return;
}
@@ -4302,66 +4183,47 @@
// unsorted containers accept a capacity value
capacity = source_name + ".size()";
}
- indent(out) << type_name(type, true, false) << " " << result_name << " = new "
- << type_name(container, false, true) << "(" << capacity << ");" << endl;
+ indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "(" << capacity << ");" << endl;
std::string iterator_element_name = source_name_p1 + "_element";
std::string result_element_name = result_name + "_copy";
- if (container->is_map()) {
+ if(container->is_map()) {
t_type* key_type = ((t_map*)container)->get_key_type();
t_type* val_type = ((t_map*)container)->get_val_type();
- indent(out) << "for (Map.Entry<" << type_name(key_type, true, false) << ", "
- << type_name(val_type, true, false) << "> " << iterator_element_name << " : "
- << source_name << ".entrySet()) {" << endl;
+ indent(out) <<
+ "for (Map.Entry<" << type_name(key_type, true, false) << ", " << type_name(val_type, true, false) << "> " << iterator_element_name << " : " << source_name << ".entrySet()) {" << endl;
indent_up();
out << endl;
- indent(out) << type_name(key_type, true, false) << " " << iterator_element_name
- << "_key = " << iterator_element_name << ".getKey();" << endl;
- indent(out) << type_name(val_type, true, false) << " " << iterator_element_name
- << "_value = " << iterator_element_name << ".getValue();" << endl;
+ indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = " << iterator_element_name << ".getKey();" << endl;
+ indent(out) << type_name(val_type, true, false) << " " << iterator_element_name << "_value = " << iterator_element_name << ".getValue();" << endl;
out << endl;
if (key_type->is_container()) {
- generate_deep_copy_container(out,
- iterator_element_name + "_key",
- "",
- result_element_name + "_key",
- key_type);
+ generate_deep_copy_container(out, iterator_element_name + "_key", "", result_element_name + "_key", key_type);
} else {
indent(out) << type_name(key_type, true, false) << " " << result_element_name << "_key = ";
- generate_deep_copy_non_container(out,
- iterator_element_name + "_key",
- result_element_name + "_key",
- key_type);
+ generate_deep_copy_non_container(out, iterator_element_name + "_key", result_element_name + "_key", key_type);
out << ";" << endl;
}
out << endl;
if (val_type->is_container()) {
- generate_deep_copy_container(out,
- iterator_element_name + "_value",
- "",
- result_element_name + "_value",
- val_type);
+ generate_deep_copy_container(out, iterator_element_name + "_value", "", result_element_name + "_value", val_type);
} else {
indent(out) << type_name(val_type, true, false) << " " << result_element_name << "_value = ";
- generate_deep_copy_non_container(out,
- iterator_element_name + "_value",
- result_element_name + "_value",
- val_type);
+ generate_deep_copy_non_container(out, iterator_element_name + "_value", result_element_name + "_value", val_type);
out << ";" << endl;
}
out << endl;
- indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name
- << "_value);" << endl;
+ indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name << "_value);" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -4375,8 +4237,8 @@
elem_type = ((t_list*)container)->get_elem_type();
}
- indent(out) << "for (" << type_name(elem_type, true, false) << " " << iterator_element_name
- << " : " << source_name << ") {" << endl;
+ indent(out)
+ << "for (" << type_name(elem_type, true, false) << " " << iterator_element_name << " : " << source_name << ") {" << endl;
indent_up();
@@ -4386,15 +4248,12 @@
indent(out) << result_name << ".add(" << result_element_name << ");" << endl;
} else {
// iterative copy
- if (((t_base_type*)elem_type)->is_binary()) {
+ if(((t_base_type*)elem_type)->is_binary()){
indent(out) << "ByteBuffer temp_binary_element = ";
- generate_deep_copy_non_container(out,
- iterator_element_name,
- "temp_binary_element",
- elem_type);
+ generate_deep_copy_non_container(out, iterator_element_name, "temp_binary_element", elem_type);
out << ";" << endl;
indent(out) << result_name << ".add(temp_binary_element);" << endl;
- } else {
+ } else{
indent(out) << result_name << ".add(";
generate_deep_copy_non_container(out, iterator_element_name, result_name, elem_type);
out << ");" << endl;
@@ -4404,14 +4263,12 @@
indent_down();
indent(out) << "}" << endl;
+
}
}
-void t_java_generator::generate_deep_copy_non_container(ofstream& out,
- std::string source_name,
- std::string dest_name,
- t_type* type) {
- (void)dest_name;
+void t_java_generator::generate_deep_copy_non_container(ofstream& out, std::string source_name, std::string dest_name, t_type* type) {
+ (void) dest_name;
if (type->is_base_type() || type->is_enum() || type->is_typedef()) {
if (((t_base_type*)type)->is_binary()) {
out << "org.apache.thrift.TBaseHelper.copyBinary(" << source_name << ")";
@@ -4438,14 +4295,13 @@
void t_java_generator::generate_isset_set(ofstream& out, t_field* field, string prefix) {
if (!type_can_be_null(field->get_type())) {
- indent(out) << prefix << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet")
- << "(true);" << endl;
+ indent(out) << prefix << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);" << endl;
}
}
void t_java_generator::generate_struct_desc(ofstream& out, t_struct* tstruct) {
- indent(out) << "private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new "
- "org.apache.thrift.protocol.TStruct(\"" << tstruct->get_name() << "\");" << endl;
+ indent(out) <<
+ "private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct(\"" << tstruct->get_name() << "\");" << endl;
}
void t_java_generator::generate_field_descs(ofstream& out, t_struct* tstruct) {
@@ -4453,28 +4309,24 @@
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "private static final org.apache.thrift.protocol.TField "
- << constant_name((*m_iter)->get_name())
- << "_FIELD_DESC = new org.apache.thrift.protocol.TField(\"" << (*m_iter)->get_name()
- << "\", " << type_to_enum((*m_iter)->get_type()) << ", "
- << "(short)" << (*m_iter)->get_key() << ");" << endl;
+ indent(out) <<
+ "private static final org.apache.thrift.protocol.TField " << constant_name((*m_iter)->get_name()) <<
+ "_FIELD_DESC = new org.apache.thrift.protocol.TField(\"" << (*m_iter)->get_name() << "\", " <<
+ type_to_enum((*m_iter)->get_type()) << ", " <<
+ "(short)" << (*m_iter)->get_key() << ");" << endl;
}
}
void t_java_generator::generate_scheme_map(ofstream& out, t_struct* tstruct) {
- indent(out) << "private static final Map<Class<? extends IScheme>, SchemeFactory> schemes = new "
- "HashMap<Class<? extends IScheme>, SchemeFactory>();" << endl;
+ indent(out) << "private static final Map<Class<? extends IScheme>, SchemeFactory> schemes = new HashMap<Class<? extends IScheme>, SchemeFactory>();" << endl;
indent(out) << "static {" << endl;
- indent(out) << " schemes.put(StandardScheme.class, new " << tstruct->get_name()
- << "StandardSchemeFactory());" << endl;
- indent(out) << " schemes.put(TupleScheme.class, new " << tstruct->get_name()
- << "TupleSchemeFactory());" << endl;
+ indent(out) << " schemes.put(StandardScheme.class, new " << tstruct->get_name() << "StandardSchemeFactory());" << endl;
+ indent(out) << " schemes.put(TupleScheme.class, new " << tstruct->get_name() << "TupleSchemeFactory());" << endl;
indent(out) << "}" << endl;
}
void t_java_generator::generate_field_name_constants(ofstream& out, t_struct* tstruct) {
- indent(out) << "/** The set of fields this struct contains, along with convenience methods for "
- "finding and manipulating them. */" << endl;
+ indent(out) << "/** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */" << endl;
indent(out) << "public enum _Fields implements org.apache.thrift.TFieldIdEnum {" << endl;
indent_up();
@@ -4487,15 +4339,12 @@
}
first = false;
generate_java_doc(out, *m_iter);
- indent(out) << constant_name((*m_iter)->get_name()) << "((short)" << (*m_iter)->get_key()
- << ", \"" << (*m_iter)->get_name() << "\")";
+ indent(out) << constant_name((*m_iter)->get_name()) << "((short)" << (*m_iter)->get_key() << ", \"" << (*m_iter)->get_name() << "\")";
}
out << ";" << endl << endl;
- indent(out)
- << "private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();"
- << endl;
+ indent(out) << "private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();" << endl;
out << endl;
indent(out) << "static {" << endl;
@@ -4505,8 +4354,7 @@
indent(out) << "}" << endl << endl;
indent(out) << "/**" << endl;
- indent(out) << " * Find the _Fields constant that matches fieldId, or null if its not found."
- << endl;
+ indent(out) << " * Find the _Fields constant that matches fieldId, or null if its not found." << endl;
indent(out) << " */" << endl;
indent(out) << "public static _Fields findByThriftId(int fieldId) {" << endl;
indent_up();
@@ -4514,8 +4362,7 @@
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "case " << (*m_iter)->get_key() << ": // "
- << constant_name((*m_iter)->get_name()) << endl;
+ indent(out) << "case " << (*m_iter)->get_key() << ": // " << constant_name((*m_iter)->get_name()) << endl;
indent(out) << " return " << constant_name((*m_iter)->get_name()) << ";" << endl;
}
@@ -4534,14 +4381,12 @@
indent(out) << " */" << endl;
indent(out) << "public static _Fields findByThriftIdOrThrow(int fieldId) {" << endl;
indent(out) << " _Fields fields = findByThriftId(fieldId);" << endl;
- indent(out) << " if (fields == null) throw new IllegalArgumentException(\"Field \" + fieldId + "
- "\" doesn't exist!\");" << endl;
+ indent(out) << " if (fields == null) throw new IllegalArgumentException(\"Field \" + fieldId + \" doesn't exist!\");" << endl;
indent(out) << " return fields;" << endl;
indent(out) << "}" << endl << endl;
indent(out) << "/**" << endl;
- indent(out) << " * Find the _Fields constant that matches name, or null if its not found."
- << endl;
+ indent(out) << " * Find the _Fields constant that matches name, or null if its not found." << endl;
indent(out) << " */" << endl;
indent(out) << "public static _Fields findByName(String name) {" << endl;
indent(out) << " return byName.get(name);" << endl;
@@ -4568,8 +4413,7 @@
indent(out) << "}" << endl;
}
-t_java_generator::isset_type t_java_generator::needs_isset(t_struct* tstruct,
- std::string* outPrimitiveType) {
+t_java_generator::isset_type t_java_generator::needs_isset(t_struct* tstruct, std::string *outPrimitiveType) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -4579,17 +4423,17 @@
count++;
}
}
- if (count == 0) {
+ if(count == 0) {
return ISSET_NONE;
- } else if (count <= 64) {
- if (outPrimitiveType != NULL) {
- if (count <= 8)
+ } else if(count <= 64) {
+ if(outPrimitiveType != NULL) {
+ if(count <= 8)
*outPrimitiveType = "byte";
- else if (count <= 16)
+ else if(count <= 16)
*outPrimitiveType = "short";
- else if (count <= 32)
+ else if(count <= 32)
*outPrimitiveType = "int";
- else if (count <= 64)
+ else if(count <= 64)
*outPrimitiveType = "long";
}
return ISSET_PRIMITIVE;
@@ -4624,25 +4468,24 @@
// must be a base type
// means it also needs to be explicitly unset
- indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(false);"
- << endl;
- t_base_type* base_type = (t_base_type*)t;
+ indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(false);" << endl;
+ t_base_type* base_type = (t_base_type*) t;
switch (base_type->get_base()) {
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- indent(out) << "this." << field->get_name() << " = 0;" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- indent(out) << "this." << field->get_name() << " = 0.0;" << endl;
- break;
- case t_base_type::TYPE_BOOL:
- indent(out) << "this." << field->get_name() << " = false;" << endl;
- break;
- default:
- throw "unsupported type: " + base_type->get_name() + " for field " + field->get_name();
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ indent(out) << "this." << field->get_name() << " = 0;" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << "this." << field->get_name() << " = 0.0;" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << "this." << field->get_name() << " = false;" << endl;
+ break;
+ default:
+ throw "unsupported type: " + base_type->get_name() + " for field " + field->get_name();
}
}
indent_down();
@@ -4652,61 +4495,53 @@
// generates java method to serialize (in the Java sense) the object
void t_java_generator::generate_java_struct_write_object(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
- indent(out)
- << "private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {"
- << endl;
+ (void) tstruct;
+ indent(out) << "private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {" << endl;
indent(out) << " try {" << endl;
- indent(out) << " write(new org.apache.thrift.protocol.TCompactProtocol(new "
- "org.apache.thrift.transport.TIOStreamTransport(out)));" << endl;
+ indent(out) << " write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));" << endl;
indent(out) << " } catch (org.apache.thrift.TException te) {" << endl;
- indent(out) << " throw new java.io.IOException(te" << (android_legacy_ ? ".getMessage()" : "")
- << ");" << endl;
+ indent(out) << " throw new java.io.IOException(te" << (android_legacy_? ".getMessage()" : "") << ");" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl << endl;
}
// generates java method to serialize (in the Java sense) the object
void t_java_generator::generate_java_struct_read_object(ofstream& out, t_struct* tstruct) {
- indent(out) << "private void readObject(java.io.ObjectInputStream in) throws "
- "java.io.IOException, ClassNotFoundException {" << endl;
+ indent(out) << "private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {" << endl;
indent(out) << " try {" << endl;
if (!tstruct->is_union()) {
- switch (needs_isset(tstruct)) {
- case ISSET_NONE:
- break;
- case ISSET_PRIMITIVE:
- indent(out) << " // it doesn't seem like you should have to do this, but java "
- "serialization is wacky, and doesn't call the default constructor." << endl;
- indent(out) << " __isset_bitfield = 0;" << endl;
- break;
- case ISSET_BITSET:
- indent(out) << " // it doesn't seem like you should have to do this, but java "
- "serialization is wacky, and doesn't call the default constructor." << endl;
- indent(out) << " __isset_bit_vector = new BitSet(1);" << endl;
- break;
+ switch(needs_isset(tstruct)) {
+ case ISSET_NONE:
+ break;
+ case ISSET_PRIMITIVE:
+ indent(out) << " // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor." << endl;
+ indent(out) << " __isset_bitfield = 0;" << endl;
+ break;
+ case ISSET_BITSET:
+ indent(out) << " // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor." << endl;
+ indent(out) << " __isset_bit_vector = new BitSet(1);" << endl;
+ break;
}
}
- indent(out) << " read(new org.apache.thrift.protocol.TCompactProtocol(new "
- "org.apache.thrift.transport.TIOStreamTransport(in)));" << endl;
+ indent(out) << " read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));" << endl;
indent(out) << " } catch (org.apache.thrift.TException te) {" << endl;
- indent(out) << " throw new java.io.IOException(te" << (android_legacy_ ? ".getMessage()" : "")
- << ");" << endl;
+ indent(out) << " throw new java.io.IOException(te" << (android_legacy_? ".getMessage()" : "") << ");" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl << endl;
}
void t_java_generator::generate_standard_reader(ofstream& out, t_struct* tstruct) {
- out << indent() << "public void read(org.apache.thrift.protocol.TProtocol iprot, "
- << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
+ out <<
+ indent() << "public void read(org.apache.thrift.protocol.TProtocol iprot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// Declare stack tmp variables and read struct header
- out << indent() << "org.apache.thrift.protocol.TField schemeField;" << endl << indent()
- << "iprot.readStructBegin();" << endl;
+ out <<
+ indent() << "org.apache.thrift.protocol.TField schemeField;" << endl <<
+ indent() << "iprot.readStructBegin();" << endl;
// Loop over reading in fields
indent(out) << "while (true)" << endl;
@@ -4729,27 +4564,26 @@
// Generate deserialization code for known cases
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ": // "
- << constant_name((*f_iter)->get_name()) << endl;
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ": // " << constant_name((*f_iter)->get_name()) << endl;
indent_up();
- indent(out) << "if (schemeField.type == " << type_to_enum((*f_iter)->get_type()) << ") {"
- << endl;
+ indent(out) <<
+ "if (schemeField.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
indent_up();
generate_deserialize_field(out, *f_iter, "struct.", true);
- indent(out) << "struct."
- << "set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet")
- << "(true);" << endl;
+ indent(out) << "struct." << "set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl;
indent_down();
- out << indent() << "} else { " << endl << indent()
- << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);" << endl
- << indent() << "}" << endl << indent() << "break;" << endl;
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
indent_down();
}
indent(out) << "default:" << endl;
- indent(out) << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);"
- << endl;
+ indent(out) << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -4760,21 +4594,19 @@
indent_down();
indent(out) << "}" << endl;
- out << indent() << "iprot.readStructEnd();" << endl;
+ out <<
+ indent() << "iprot.readStructEnd();" << endl;
// in non-beans style, check for required fields of primitive type
// (which can be checked here but not in the general validate method)
- if (!bean_style_) {
- out << endl << indent() << "// check for required fields of primitive type, which can't be "
- "checked in the validate method" << endl;
+ if (!bean_style_){
+ out << endl << indent() << "// check for required fields of primitive type, which can't be checked in the validate method" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
- out << indent() << "if (!struct." << generate_isset_check(*f_iter) << ") {" << endl
- << indent()
- << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '"
- << (*f_iter)->get_name()
- << "' was not found in serialized data! Struct: \" + toString());" << endl << indent()
- << "}" << endl;
+ out <<
+ indent() << "if (!struct." << generate_isset_check(*f_iter) << ") {" << endl <<
+ indent() << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' was not found in serialized data! Struct: \" + toString());" << endl <<
+ indent() << "}" << endl;
}
}
}
@@ -4788,8 +4620,9 @@
void t_java_generator::generate_standard_writer(ofstream& out, t_struct* tstruct, bool is_result) {
indent_up();
- out << indent() << "public void write(org.apache.thrift.protocol.TProtocol oprot, "
- << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
+ out <<
+ indent() << "public void write(org.apache.thrift.protocol.TProtocol oprot, " <<
+ tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
@@ -4802,18 +4635,17 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (struct." << (*f_iter)->get_name() << " != null) {" << endl;
+ out <<
+ indent() << "if (struct." << (*f_iter)->get_name() << " != null) {" << endl;
indent_up();
}
bool optional = ((*f_iter)->get_req() == t_field::T_OPTIONAL) || (is_result && !null_allowed);
if (optional) {
- indent(out) << "if ("
- << "struct." << generate_isset_check((*f_iter)) << ") {" << endl;
+ indent(out) << "if (" << "struct." << generate_isset_check((*f_iter)) << ") {" << endl;
indent_up();
}
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "struct.", true);
@@ -4831,19 +4663,17 @@
}
}
// Write the struct map
- out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
- << endl;
+ out <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
out << indent() << "}" << endl << endl;
indent_down();
}
-void t_java_generator::generate_java_struct_standard_scheme(ofstream& out,
- t_struct* tstruct,
- bool is_result) {
- indent(out) << "private static class " << tstruct->get_name()
- << "StandardSchemeFactory implements SchemeFactory {" << endl;
+void t_java_generator::generate_java_struct_standard_scheme(ofstream& out, t_struct* tstruct, bool is_result){
+ indent(out) << "private static class " << tstruct->get_name() << "StandardSchemeFactory implements SchemeFactory {" << endl;
indent_up();
indent(out) << "public " << tstruct->get_name() << "StandardScheme getScheme() {" << endl;
indent_up();
@@ -4853,48 +4683,44 @@
indent_down();
indent(out) << "}" << endl << endl;
- out << indent() << "private static class " << tstruct->get_name()
- << "StandardScheme extends StandardScheme<" << tstruct->get_name() << "> {" << endl << endl;
+ out << indent() << "private static class " << tstruct->get_name() << "StandardScheme extends StandardScheme<" << tstruct->get_name() << "> {" << endl << endl;
indent_up();
generate_standard_reader(out, tstruct);
indent_down();
out << endl;
generate_standard_writer(out, tstruct, is_result);
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
void t_java_generator::generate_java_struct_tuple_reader(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "public void read(org.apache.thrift.protocol.TProtocol prot, "
- << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
+ indent(out) << "public void read(org.apache.thrift.protocol.TProtocol prot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "TTupleProtocol iprot = (TTupleProtocol) prot;" << endl;
int optional_count = 0;
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
optional_count++;
}
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
generate_deserialize_field(out, (*f_iter), "struct.", false);
- indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet")
- << "(true);" << endl;
+ indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl;
}
}
if (optional_count > 0) {
indent(out) << "BitSet incoming = iprot.readBitSet(" << optional_count << ");" << endl;
int i = 0;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
indent(out) << "if (incoming.get(" << i << ")) {" << endl;
indent_up();
generate_deserialize_field(out, (*f_iter), "struct.", false);
- indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet")
- << "(true);" << endl;
+ indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl;
indent_down();
indent(out) << "}" << endl;
i++;
@@ -4907,8 +4733,7 @@
void t_java_generator::generate_java_struct_tuple_writer(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
- indent(out) << "public void write(org.apache.thrift.protocol.TProtocol prot, "
- << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
+ indent(out) << "public void write(org.apache.thrift.protocol.TProtocol prot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "TTupleProtocol oprot = (TTupleProtocol) prot;" << endl;
@@ -4917,8 +4742,7 @@
bool has_optional = false;
int optional_count = 0;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
optional_count++;
has_optional = true;
}
@@ -4930,8 +4754,7 @@
indent(out) << "BitSet optionals = new BitSet();" << endl;
int i = 0;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
indent(out) << "if (struct." << generate_isset_check((*f_iter)) << ") {" << endl;
indent_up();
indent(out) << "optionals.set(" << i << ");" << endl;
@@ -4944,8 +4767,7 @@
indent(out) << "oprot.writeBitSet(optionals, " << optional_count << ");" << endl;
int j = 0;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_OPTIONAL
- || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
+ if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) {
indent(out) << "if (struct." << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
generate_serialize_field(out, (*f_iter), "struct.", false);
@@ -4959,9 +4781,8 @@
indent(out) << "}" << endl;
}
-void t_java_generator::generate_java_struct_tuple_scheme(ofstream& out, t_struct* tstruct) {
- indent(out) << "private static class " << tstruct->get_name()
- << "TupleSchemeFactory implements SchemeFactory {" << endl;
+void t_java_generator::generate_java_struct_tuple_scheme(ofstream& out, t_struct* tstruct){
+ indent(out) << "private static class " << tstruct->get_name() << "TupleSchemeFactory implements SchemeFactory {" << endl;
indent_up();
indent(out) << "public " << tstruct->get_name() << "TupleScheme getScheme() {" << endl;
indent_up();
@@ -4970,8 +4791,7 @@
indent(out) << "}" << endl;
indent_down();
indent(out) << "}" << endl << endl;
- out << indent() << "private static class " << tstruct->get_name()
- << "TupleScheme extends TupleScheme<" << tstruct->get_name() << "> {" << endl << endl;
+ out << indent() << "private static class " << tstruct->get_name() << "TupleScheme extends TupleScheme<" << tstruct->get_name() << "> {" << endl << endl;
indent_up();
generate_java_struct_tuple_writer(out, tstruct);
out << endl;
@@ -4980,28 +4800,25 @@
out << indent() << "}" << endl << endl;
}
-void t_java_generator::generate_javax_generated_annotation(ofstream& out) {
+void t_java_generator::generate_javax_generated_annotation(ofstream& out){
time_t seconds = time(NULL);
- struct tm* now = localtime(&seconds);
- indent(out) << "@Generated(value = \"" << autogen_summary() << "\", date = \""
- << (now->tm_year + 1900) << "-" << (now->tm_mon + 1) << "-" << now->tm_mday << "\")"
- << endl;
+ struct tm *now = localtime(&seconds);
+ indent(out) << "@Generated(value = \"" << autogen_summary()
+ << "\", date = \"" << (now->tm_year + 1900)
+ << "-" << (now->tm_mon + 1) << "-" << now->tm_mday
+ << "\")" << endl;
}
-THRIFT_REGISTER_GENERATOR(
- java,
- "Java",
- " beans: Members will be private, and setter methods will return void.\n"
- " private-members: Members will be private, but setter methods will return 'this' like "
- "usual.\n"
- " nocamel: Do not use CamelCase field accessors with beans.\n"
- " fullcamel: Convert underscored_accessor_or_service_names to camelCase.\n"
- " android: Generated structures are Parcelable.\n"
- " android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and "
- "above).\n"
- " java5: Generate Java 1.5 compliant code (includes android_legacy flag).\n"
- " reuse-objects: Data objects will not be allocated, but existing instances will be used "
- "(read and write).\n"
- " sorted_containers:\n"
- " Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of "
- "set/map.\n")
+THRIFT_REGISTER_GENERATOR(java, "Java",
+" beans: Members will be private, and setter methods will return void.\n"
+" private-members: Members will be private, but setter methods will return 'this' like usual.\n"
+" nocamel: Do not use CamelCase field accessors with beans.\n"
+" fullcamel: Convert underscored_accessor_or_service_names to camelCase.\n"
+" android: Generated structures are Parcelable.\n"
+" android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).\n"
+" java5: Generate Java 1.5 compliant code (includes android_legacy flag).\n"
+" reuse-objects: Data objects will not be allocated, but existing instances will be used (read and write).\n"
+" sorted_containers:\n"
+" Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_javame_generator.cc b/compiler/cpp/src/generate/t_javame_generator.cc
index b4a13fc..eb934ac 100644
--- a/compiler/cpp/src/generate/t_javame_generator.cc
+++ b/compiler/cpp/src/generate/t_javame_generator.cc
@@ -37,20 +37,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Java code generator.
*
*/
class t_javame_generator : public t_oop_generator {
-public:
- t_javame_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_javame_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
out_dir_base_ = "gen-javame";
}
@@ -68,23 +70,15 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_struct(t_struct* tstruct);
- void generate_union(t_struct* tunion);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_struct (t_struct* tstruct);
+ void generate_union (t_struct* tunion);
+ void generate_xception(t_struct* txception);
+ void generate_service (t_service* tservice);
- void print_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval = false);
- std::string render_const_value(std::ofstream& out,
- std::string name,
- t_type* type,
- t_const_value* value);
+ void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
+ std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
/**
* Service-level generation functions
@@ -92,11 +86,7 @@
void generate_java_struct(t_struct* tstruct, bool is_exception);
- void generate_java_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool in_class = false,
- bool is_result = false);
+ void generate_java_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
void generate_java_struct_equality(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_compare_to(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_reader(std::ofstream& out, t_struct* tstruct);
@@ -107,14 +97,8 @@
void generate_java_struct_clear(std::ofstream& out, t_struct* tstruct);
void generate_field_value_meta_data(std::ofstream& out, t_type* type);
std::string get_java_type_string(t_type* type);
- void generate_reflection_setters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
- void generate_reflection_getters(std::ostringstream& out,
- t_type* type,
- std::string field_name,
- std::string cap_name);
+ void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
+ void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
void generate_java_bean_boilerplate(std::ofstream& out, t_struct* tstruct);
@@ -125,12 +109,12 @@
void generate_isset_set(ofstream& out, t_field* field);
std::string isset_field_id(t_field* field);
- void generate_primitive_service_interface(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_helpers(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_primitive_service_interface (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
void generate_java_union(t_struct* tstruct);
void generate_union_constructor(ofstream& out, t_struct* tstruct);
@@ -150,52 +134,69 @@
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_deserialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream& out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream& out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream& out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream& out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string iter,
- std::string map);
+ void generate_serialize_map_element (std::ofstream& out,
+ t_map* tmap,
+ std::string iter,
+ std::string map);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream& out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream& out,
+ t_list* tlist,
+ std::string iter);
- void generate_java_doc(std::ofstream& out, t_field* field);
+ void generate_java_doc (std::ofstream& out,
+ t_field* field);
- void generate_java_doc(std::ofstream& out, t_doc* tdoc);
+ void generate_java_doc (std::ofstream& out,
+ t_doc* tdoc);
- void generate_java_doc(std::ofstream& out, t_function* tdoc);
+ void generate_java_doc (std::ofstream& out,
+ t_function* tdoc);
- void generate_java_docstring_comment(std::ofstream& out, string contents);
+ void generate_java_docstring_comment (std::ofstream &out,
+ string contents);
- void generate_deep_copy_container(std::ofstream& out,
- std::string source_name_p1,
- std::string source_name_p2,
- std::string result_name,
- t_type* type);
- void generate_deep_copy_non_container(std::ofstream& out,
- std::string source_name,
- std::string dest_name,
- t_type* type);
+ void generate_deep_copy_container(std::ofstream& out, std::string source_name_p1, std::string source_name_p2, std::string result_name, t_type* type);
+ void generate_deep_copy_non_container(std::ofstream& out, std::string source_name, std::string dest_name, t_type* type);
bool has_bit_vector(t_struct* tstruct);
@@ -206,13 +207,10 @@
std::string java_package();
std::string java_type_imports();
std::string java_thrift_imports();
- std::string type_name(t_type* ttype,
- bool in_container = false,
- bool in_init = false,
- bool skip_generic = false);
- std::string base_type_name(t_base_type* tbase, bool in_container = false);
- std::string declare_field(t_field* tfield, bool init = false);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false, bool skip_generic=false);
+ std::string base_type_name(t_base_type* tbase, bool in_container=false);
+ std::string declare_field(t_field* tfield, bool init=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct, bool include_types = true);
std::string type_to_enum(t_type* ttype);
std::string get_enum_class_name(t_type* type);
@@ -223,13 +221,18 @@
bool type_can_be_null(t_type* ttype) {
ttype = get_true_type(ttype);
- return ttype->is_container() || ttype->is_struct() || ttype->is_xception() || ttype->is_string()
- || ttype->is_enum();
+ return
+ ttype->is_container() ||
+ ttype->is_struct() ||
+ ttype->is_xception() ||
+ ttype->is_string() ||
+ ttype->is_enum();
}
std::string constant_name(std::string name);
-private:
+ private:
+
/**
* File streams
*/
@@ -237,8 +240,10 @@
std::string package_name_;
std::ofstream f_service_;
std::string package_dir_;
+
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -256,7 +261,7 @@
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
if (dir.size() > 0) {
subdir = subdir + "/" + dir;
@@ -284,8 +289,11 @@
* @return List of imports for Java types that are used in here
*/
string t_javame_generator::java_type_imports() {
- return string() + "import java.util.Hashtable;\n" + "import java.util.Vector;\n"
- + "import java.util.Enumeration;\n\n";
+ return
+ string() +
+ "import java.util.Hashtable;\n" +
+ "import java.util.Vector;\n" +
+ "import java.util.Enumeration;\n\n";
}
/**
@@ -294,15 +302,18 @@
* @return List of imports necessary for thrift
*/
string t_javame_generator::java_thrift_imports() {
- return string() + "import org.apache.thrift.*;\n" + "import org.apache.thrift.meta_data.*;\n"
- + "import org.apache.thrift.transport.*;\n" + "import org.apache.thrift.protocol.*;\n\n";
+ return
+ string() +
+ "import org.apache.thrift.*;\n" +
+ "import org.apache.thrift.meta_data.*;\n" +
+ "import org.apache.thrift.transport.*;\n" +
+ "import org.apache.thrift.protocol.*;\n\n";
}
/**
* Nothing in Java
*/
-void t_javame_generator::close_generator() {
-}
+void t_javame_generator::close_generator() {}
/**
* Generates a typedef. This is not done in Java, since it does
@@ -312,7 +323,7 @@
* @param ttypedef The type definition
*/
void t_javame_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -322,15 +333,18 @@
*/
void t_javame_generator::generate_enum(t_enum* tenum) {
// Make output file
- string f_enum_name = package_dir_ + "/" + (tenum->get_name()) + ".java";
+ string f_enum_name = package_dir_+"/"+(tenum->get_name())+".java";
ofstream f_enum;
f_enum.open(f_enum_name.c_str());
// Comment and package it
- f_enum << autogen_comment() << java_package();
+ f_enum <<
+ autogen_comment() <<
+ java_package();
generate_java_doc(f_enum, tenum);
- indent(f_enum) << "public class " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
+ indent(f_enum) <<
+ "public class " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
scope_up(f_enum);
f_enum << endl;
@@ -339,8 +353,9 @@
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
generate_java_doc(f_enum, *c_iter);
- indent(f_enum) << "public static final " << tenum->get_name() << " " << (*c_iter)->get_name()
- << " = new " << tenum->get_name() << "(" << value << ");" << endl;
+ indent(f_enum) << "public static final " << tenum->get_name() <<
+ " " << (*c_iter)->get_name() << " = new " << tenum->get_name() <<
+ "(" << value << ");" << endl;
}
f_enum << endl;
@@ -348,23 +363,21 @@
indent(f_enum) << "private final int value;" << endl << endl;
indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl;
- indent(f_enum) << " this.value = value;" << endl;
+ indent(f_enum) << " this.value = value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
- indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL."
- << endl;
+ indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " */" << endl;
indent(f_enum) << "public int getValue() {" << endl;
- indent(f_enum) << " return value;" << endl;
+ indent(f_enum) << " return value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
- indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL."
- << endl;
+ indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " * @return null if the value is not found." << endl;
indent(f_enum) << " */" << endl;
- indent(f_enum) << "public static " + tenum->get_name() + " findByValue(int value) { " << endl;
+ indent(f_enum) << "public static "+ tenum->get_name() + " findByValue(int value) { " << endl;
indent_up();
@@ -401,14 +414,19 @@
return;
}
- string f_consts_name = package_dir_ + "/" + program_name_ + "Constants.java";
+ string f_consts_name = package_dir_+ "/" + program_name_ + "Constants.java";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
// Print header
- f_consts << autogen_comment() << java_package() << java_type_imports();
+ f_consts <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports();
- f_consts << "public class " << program_name_ << "Constants {" << endl << endl;
+ f_consts <<
+ "public class " << program_name_ << "Constants {" << endl <<
+ endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
@@ -419,26 +437,25 @@
false);
}
indent_down();
- indent(f_consts) << "}" << endl;
+ indent(f_consts) <<
+ "}" << endl;
f_consts.close();
}
+
/**
* Prints the value of a constant with the given type. Note that type checking
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-void t_javame_generator::print_const_value(std::ofstream& out,
- string name,
- t_type* type,
- t_const_value* value,
- bool in_static,
- bool defval) {
+void t_javame_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
type = get_true_type(type);
indent(out);
if (!defval) {
- out << (in_static ? "" : "public static final ") << type_name(type) << " ";
+ out <<
+ (in_static ? "" : "public static final ") <<
+ type_name(type) << " ";
}
if (type->is_base_type()) {
string v2 = render_const_value(out, name, type, value);
@@ -488,8 +505,8 @@
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string key = render_const_value(out, name, ktype, v_iter->first);
string val = render_const_value(out, name, vtype, v_iter->second);
- indent(out) << name << ".put(" << box_type(ktype, key) << ", " << box_type(vtype, val) << ");"
- << endl;
+ indent(out) << name << ".put(" << box_type(ktype, key) << ", " <<
+ box_type(vtype, val) << ");" << endl;
}
if (!in_static) {
indent_down();
@@ -513,10 +530,11 @@
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string val = render_const_value(out, name, etype, *v_iter);
if (type->is_list()) {
- indent(out) << name << ".addElement(" << box_type(etype, val) << ");" << endl;
+ indent(out) << name << ".addElement(" << box_type(etype, val) << ");" << endl;
} else {
- indent(out) << name << ".put(" << box_type(etype, val) << ", " << box_type(etype, val)
- << ");" << endl;
+ indent(out) << name << ".put(" <<
+ box_type(etype, val) << ", " <<
+ box_type(etype, val) << ");" << endl;
}
}
if (!in_static) {
@@ -529,11 +547,8 @@
}
}
-string t_javame_generator::render_const_value(ofstream& out,
- string name,
- t_type* type,
- t_const_value* value) {
- (void)name;
+string t_javame_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
+ (void) name;
type = get_true_type(type);
std::ostringstream render;
@@ -624,20 +639,28 @@
generate_java_struct(txception, true);
}
+
/**
* Java struct definition.
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct(t_struct* tstruct, bool is_exception) {
+void t_javame_generator::generate_java_struct(t_struct* tstruct,
+ bool is_exception) {
// Make output file
- string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".java";
+ string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
- generate_java_struct_definition(f_struct, tstruct, is_exception);
+ generate_java_struct_definition(f_struct,
+ tstruct,
+ is_exception);
f_struct.close();
}
@@ -648,18 +671,23 @@
*/
void t_javame_generator::generate_java_union(t_struct* tstruct) {
// Make output file
- string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".java";
+ string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
- f_struct << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
+ f_struct <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
generate_java_doc(f_struct, tstruct);
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- indent(f_struct) << "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
- << " extends TUnion ";
+ indent(f_struct) <<
+ "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
+ << " extends TUnion ";
scope_up(f_struct);
@@ -702,8 +730,7 @@
indent(out) << " super(setField, value);" << endl;
indent(out) << "}" << endl << endl;
- indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {"
- << endl;
+ indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {" << endl;
indent(out) << " super(other);" << endl;
indent(out) << "}" << endl;
@@ -715,8 +742,7 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "("
- << type_name((*m_iter)->get_type()) << " value) {" << endl;
+ indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(" << type_name((*m_iter)->get_type()) << " value) {" << endl;
indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl;
indent(out) << " return x;" << endl;
@@ -739,24 +765,19 @@
t_field* field = (*m_iter);
generate_java_doc(out, field);
- indent(out) << "public " << type_name(field->get_type()) << " get"
- << get_cap_name(field->get_name()) << "() {" << endl;
- indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {"
- << endl;
- indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();"
- << endl;
+ indent(out) << "public " << type_name(field->get_type()) << " get" << get_cap_name(field->get_name()) << "() {" << endl;
+ indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
+ indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();" << endl;
indent(out) << " } else {" << endl;
indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
- << "' because union is currently set to \" + getFieldDesc(getSetField()).name);"
- << endl;
+ << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl;
out << endl;
generate_java_doc(out, field);
- indent(out) << "public void set" << get_cap_name(field->get_name()) << "("
- << type_name(field->get_type()) << " value) {" << endl;
+ indent(out) << "public void set" << get_cap_name(field->get_name()) << "(" << type_name(field->get_type()) << " value) {" << endl;
if (type_can_be_null(field->get_type())) {
indent(out) << " if (value == null) throw new NullPointerException();" << endl;
}
@@ -780,9 +801,7 @@
}
void t_javame_generator::generate_check_type(ofstream& out, t_struct* tstruct) {
- indent(out)
- << "protected void checkType(_Fields setField, Object value) throws ClassCastException {"
- << endl;
+ indent(out) << "protected void checkType(_Fields setField, Object value) throws ClassCastException {" << endl;
indent_up();
indent(out) << "switch (setField) {" << endl;
@@ -795,13 +814,12 @@
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
- indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true)
- << ") {" << endl;
+ indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true) << ") {" << endl;
indent(out) << " break;" << endl;
indent(out) << " }" << endl;
indent(out) << " throw new ClassCastException(\"Was expecting value of type "
- << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
- << "', but got \" + value.getClass().getSimpleName());" << endl;
+ << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
+ << "', but got \" + value.getClass().getSimpleName());" << endl;
// do the real check here
}
@@ -816,8 +834,7 @@
}
void t_javame_generator::generate_read_value(ofstream& out, t_struct* tstruct) {
- indent(out) << "protected Object readValue(TProtocol iprot, TField field) throws TException {"
- << endl;
+ indent(out) << "protected Object readValue(TProtocol iprot, TField field) throws TException {" << endl;
indent_up();
@@ -835,11 +852,9 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {"
- << endl;
+ indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";"
- << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl;
generate_deserialize_field(out, field, "");
indent(out) << "return " << field->get_name() << ";" << endl;
indent_down();
@@ -851,8 +866,7 @@
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any "
- "of the case statements!\");" << endl;
+ indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -885,22 +899,23 @@
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
- indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << " = ("
- << type_name(field->get_type(), true, false) << ")value_;" << endl;
+ indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name()
+ << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl;
generate_serialize_field(out, field, "");
indent(out) << "return;" << endl;
indent_down();
}
indent(out) << "default:" << endl;
- indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + "
- "setField_);" << endl;
+ indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
+
+
indent(out) << "}" << endl;
}
@@ -931,7 +946,7 @@
}
void t_javame_generator::generate_get_struct_desc(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
+ (void) tstruct;
indent(out) << "protected TStruct getStructDesc() {" << endl;
indent(out) << " return STRUCT_DESC;" << endl;
indent(out) << "}" << endl;
@@ -950,17 +965,14 @@
out << endl;
indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl;
- indent(out) << " return other != null && getSetField() == other.getSetField() && "
- "getFieldValue().equals(other.getFieldValue());" << endl;
+ indent(out) << " return other != null && getSetField() == other.getSetField() && getFieldValue().equals(other.getFieldValue());" << endl;
indent(out) << "}" << endl;
out << endl;
indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
- indent(out) << " int lastComparison = TBaseHelper.compareTo(getSetField(), other.getSetField());"
- << endl;
+ indent(out) << " int lastComparison = TBaseHelper.compareTo(getSetField(), other.getSetField());" << endl;
indent(out) << " if (lastComparison == 0) {" << endl;
- indent(out) << " return TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());"
- << endl;
+ indent(out) << " return TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());" << endl;
indent(out) << " }" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
@@ -968,11 +980,9 @@
}
void t_javame_generator::generate_union_hashcode(ofstream& out, t_struct* tstruct) {
- (void)tstruct;
+ (void) tstruct;
indent(out) << "/**" << endl;
- indent(out)
- << " * If you'd like this to perform more respectably, use the hashcode generator option."
- << endl;
+ indent(out) << " * If you'd like this to perform more respectably, use the hashcode generator option." << endl;
indent(out) << " */" << endl;
indent(out) << "public int hashCode() {" << endl;
indent(out) << " return 0;" << endl;
@@ -989,17 +999,18 @@
* @param in_class If inside a class, needs to be static class
* @param is_result If this is a result it needs a different writer
*/
-void t_javame_generator::generate_java_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool in_class,
- bool is_result) {
+void t_javame_generator::generate_java_struct_definition(ofstream &out,
+ t_struct* tstruct,
+ bool is_exception,
+ bool in_class,
+ bool is_result) {
generate_java_doc(out, tstruct);
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
- indent(out) << "public " << (is_final ? "final " : "") << (in_class ? "static " : "") << "class "
- << tstruct->get_name() << " ";
+ indent(out) <<
+ "public " << (is_final ? "final " : "") <<
+ (in_class ? "static " : "") << "class " << tstruct->get_name() << " ";
if (is_exception) {
out << "extends Exception ";
@@ -1034,8 +1045,8 @@
int i = 0;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (!type_can_be_null((*m_iter)->get_type())) {
- indent(out) << "private static final int " << isset_field_id(*m_iter) << " = " << i << ";"
- << endl;
+ indent(out) << "private static final int " << isset_field_id(*m_iter)
+ << " = " << i << ";" << endl;
i++;
}
}
@@ -1050,17 +1061,13 @@
bool all_optional_members = true;
// Default constructor
- indent(out) << "public " << tstruct->get_name() << "() {" << endl;
+ indent(out) <<
+ "public " << tstruct->get_name() << "() {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL) {
- print_const_value(out,
- "this." + (*m_iter)->get_name(),
- t,
- (*m_iter)->get_value(),
- true,
- true);
+ print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
}
if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
all_optional_members = false;
@@ -1071,7 +1078,8 @@
if (!members.empty() && !all_optional_members) {
// Full constructor for all fields
- indent(out) << "public " << tstruct->get_name() << "(" << endl;
+ indent(out) <<
+ "public " << tstruct->get_name() << "(" << endl;
indent_up();
bool first = true;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -1080,7 +1088,8 @@
out << "," << endl;
}
first = false;
- indent(out) << type_name((*m_iter)->get_type()) << " " << (*m_iter)->get_name();
+ indent(out) << type_name((*m_iter)->get_type()) << " " <<
+ (*m_iter)->get_name();
}
}
out << ")" << endl;
@@ -1090,8 +1099,8 @@
indent(out) << "this();" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
- indent(out) << "this." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << ";"
- << endl;
+ indent(out) << "this." << (*m_iter)->get_name() << " = " <<
+ (*m_iter)->get_name() << ";" << endl;
generate_isset_set(out, (*m_iter));
}
}
@@ -1103,13 +1112,11 @@
indent(out) << "/**" << endl;
indent(out) << " * Performs a deep copy on <i>other</i>." << endl;
indent(out) << " */" << endl;
- indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {"
- << endl;
+ indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {" << endl;
indent_up();
if (has_bit_vector(tstruct)) {
- indent(out) << "System.arraycopy(other.__isset_vector, 0, __isset_vector, 0, "
- "other.__isset_vector.length);" << endl;
+ indent(out) << "System.arraycopy(other.__isset_vector, 0, __isset_vector, 0, other.__isset_vector.length);" << endl;
}
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -1171,19 +1178,25 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct_equality(ofstream& out, t_struct* tstruct) {
+void t_javame_generator::generate_java_struct_equality(ofstream& out,
+ t_struct* tstruct) {
out << indent() << "public boolean equals(Object that) {" << endl;
indent_up();
- out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl
- << indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl << indent()
- << " return this.equals((" << tstruct->get_name() << ")that);" << endl << indent()
- << "return false;" << endl;
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl <<
+ indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl <<
+ indent() << " return this.equals((" << tstruct->get_name() << ")that);" << endl <<
+ indent() << "return false;" << endl;
scope_down(out);
out << endl;
- out << indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
+ out <<
+ indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
indent_up();
- out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (that == null)" << endl <<
+ indent() << " return false;" << endl;
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -1206,24 +1219,31 @@
that_present += " && that." + generate_isset_check(*m_iter);
}
- out << indent() << "boolean this_present_" << name << " = " << this_present << ";" << endl
- << indent() << "boolean that_present_" << name << " = " << that_present << ";" << endl
- << indent() << "if ("
- << "this_present_" << name << " || that_present_" << name << ") {" << endl;
+ out <<
+ indent() << "boolean this_present_" << name << " = "
+ << this_present << ";" << endl <<
+ indent() << "boolean that_present_" << name << " = "
+ << that_present << ";" << endl <<
+ indent() << "if (" << "this_present_" << name
+ << " || that_present_" << name << ") {" << endl;
indent_up();
- out << indent() << "if (!("
- << "this_present_" << name << " && that_present_" << name << "))" << endl << indent()
- << " return false;" << endl;
+ out <<
+ indent() << "if (!(" << "this_present_" << name
+ << " && that_present_" << name << "))" << endl <<
+ indent() << " return false;" << endl;
if (t->is_base_type() && ((t_base_type*)t)->is_binary()) {
- unequal = "TBaseHelper.compareTo(this." + name + ", that." + name + ") != 0";
+ unequal = "TBaseHelper.compareTo(this." + name + ", that." +
+ name + ") != 0";
} else if (can_be_null) {
unequal = "!this." + name + ".equals(that." + name + ")";
} else {
unequal = "this." + name + " != that." + name;
}
- out << indent() << "if (" << unequal << ")" << endl << indent() << " return false;" << endl;
+ out <<
+ indent() << "if (" << unequal << ")" << endl <<
+ indent() << " return false;" << endl;
scope_down(out);
}
@@ -1245,8 +1265,7 @@
indent_up();
indent(out) << "if (!getClass().equals(otherObject.getClass())) {" << endl;
- indent(out) << " return getClass().getName().compareTo(otherObject.getClass().getName());"
- << endl;
+ indent(out) << " return getClass().getName().compareTo(otherObject.getClass().getName());" << endl;
indent(out) << "}" << endl;
out << endl;
indent(out) << type_name(tstruct) << " other = (" << type_name(tstruct) << ")otherObject;";
@@ -1258,19 +1277,16 @@
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = *m_iter;
- indent(out) << "lastComparison = TBaseHelper.compareTo(" << generate_isset_check(field)
- << ", other." << generate_isset_check(field) << ");" << endl;
+ indent(out) << "lastComparison = TBaseHelper.compareTo(" << generate_isset_check(field) << ", other." << generate_isset_check(field) << ");" << endl;
indent(out) << "if (lastComparison != 0) {" << endl;
indent(out) << " return lastComparison;" << endl;
indent(out) << "}" << endl;
indent(out) << "if (" << generate_isset_check(field) << ") {" << endl;
if (field->get_type()->is_struct() || field->get_type()->is_xception()) {
- indent(out) << " lastComparison = this." << field->get_name() << ".compareTo(other."
- << field->get_name() << ");" << endl;
+ indent(out) << " lastComparison = this." << field->get_name() << ".compareTo(other." << field->get_name() << ");" << endl;
} else {
- indent(out) << " lastComparison = TBaseHelper.compareTo(this." << field->get_name()
- << ", other." << field->get_name() << ");" << endl;
+ indent(out) << " lastComparison = TBaseHelper.compareTo(this." << field->get_name() << ", other." << field->get_name() << ");" << endl;
}
indent(out) << " if (lastComparison != 0) {" << endl;
@@ -1290,75 +1306,93 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct_reader(ofstream& out, t_struct* tstruct) {
- out << indent() << "public void read(TProtocol iprot) throws TException {" << endl;
+void t_javame_generator::generate_java_struct_reader(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void read(TProtocol iprot) throws TException {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// Declare stack tmp variables and read struct header
- out << indent() << "TField field;" << endl << indent() << "iprot.readStructBegin();" << endl;
+ out <<
+ indent() << "TField field;" << endl <<
+ indent() << "iprot.readStructBegin();" << endl;
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
- scope_up(out);
+ indent(out) <<
+ "while (true)" << endl;
+ scope_up(out);
- // Read beginning field marker
- indent(out) << "field = iprot.readFieldBegin();" << endl;
+ // Read beginning field marker
+ indent(out) <<
+ "field = iprot.readFieldBegin();" << endl;
- // Check for field STOP marker and break
- indent(out) << "if (field.type == TType.STOP) { " << endl;
- indent_up();
- indent(out) << "break;" << endl;
- indent_down();
- indent(out) << "}" << endl;
-
- // Switch statement on the field we are reading
- indent(out) << "switch (field.id) {" << endl;
-
- indent_up();
-
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ": // "
- << constant_name((*f_iter)->get_name()) << endl;
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if (field.type == TType.STOP) { " << endl;
indent_up();
- indent(out) << "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent(out) <<
+ "break;" << endl;
+ indent_down();
+ indent(out) <<
+ "}" << endl;
+
+ // Switch statement on the field we are reading
+ indent(out) << "switch (field.id) {" << endl;
+
indent_up();
- generate_deserialize_field(out, *f_iter, "this.");
- generate_isset_set(out, *f_iter);
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ": // " << constant_name((*f_iter)->get_name()) << endl;
+ indent_up();
+ indent(out) <<
+ "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+
+ generate_deserialize_field(out, *f_iter, "this.");
+ generate_isset_set(out, *f_iter);
+ indent_down();
+ out <<
+ indent() << "} else { " << endl <<
+ indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
+ }
+
+ indent(out) << "default:" << endl;
+ indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
+
indent_down();
- out << indent() << "} else { " << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
- << endl << indent() << "}" << endl << indent() << "break;" << endl;
+ indent(out) << "}" << endl;
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd();" << endl;
+
indent_down();
- }
+ indent(out) << "}" << endl;
- indent(out) << "default:" << endl;
- indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
+ out <<
+ indent() << "iprot.readStructEnd();" << endl;
+
+ // performs various checks (e.g. check that all required fields are set)
+ indent(out) << "validate();" << endl;
indent_down();
- indent(out) << "}" << endl;
-
- // Read field end marker
- indent(out) << "iprot.readFieldEnd();" << endl;
-
- indent_down();
- indent(out) << "}" << endl;
-
- out << indent() << "iprot.readStructEnd();" << endl;
-
- // performs various checks (e.g. check that all required fields are set)
- indent(out) << "validate();" << endl;
-
- indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
// generates java method to perform various checks
// (e.g. check that all required fields are set)
-void t_javame_generator::generate_java_validator(ofstream& out, t_struct* tstruct) {
+void t_javame_generator::generate_java_validator(ofstream& out,
+ t_struct* tstruct){
indent(out) << "public void validate() throws TException {" << endl;
indent_up();
@@ -1368,9 +1402,10 @@
out << indent() << "// check for required fields" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
- out << indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl << indent()
- << " throw new TProtocolException(\"Required field '" << (*f_iter)->get_name()
- << "' is unset! Struct:\" + toString());" << endl << indent() << "}" << endl << endl;
+ out <<
+ indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl <<
+ indent() << " throw new TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' is unset! Struct:\" + toString());" << endl <<
+ indent() << "}" << endl << endl;
}
}
@@ -1383,8 +1418,10 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public void write(TProtocol oprot) throws TException {" << endl;
+void t_javame_generator::generate_java_struct_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void write(TProtocol oprot) throws TException {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -1399,7 +1436,8 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool null_allowed = type_can_be_null((*f_iter)->get_type());
if (null_allowed) {
- out << indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
+ out <<
+ indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
indent_up();
}
bool optional = (*f_iter)->get_req() == t_field::T_OPTIONAL;
@@ -1408,14 +1446,14 @@
indent_up();
}
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
if (optional) {
indent_down();
@@ -1427,11 +1465,14 @@
}
}
// Write the struct map
- out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
- << endl;
+ out <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -1442,8 +1483,10 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct_result_writer(ofstream& out, t_struct* tstruct) {
- out << indent() << "public void write(TProtocol oprot) throws TException {" << endl;
+void t_javame_generator::generate_java_struct_result_writer(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "public void write(TProtocol oprot) throws TException {" << endl;
indent_up();
string name = tstruct->get_name();
@@ -1456,7 +1499,9 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
first = false;
- out << endl << indent() << "if ";
+ out <<
+ endl <<
+ indent() << "if ";
} else {
out << " else if ";
}
@@ -1465,38 +1510,38 @@
indent_up();
- indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
- << "_FIELD_DESC);" << endl;
+ indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd();" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd();" << endl;
indent_down();
indent(out) << "}";
}
// Write the struct map
- out << endl << indent() << "oprot.writeFieldStop();" << endl << indent()
- << "oprot.writeStructEnd();" << endl;
+ out <<
+ endl <<
+ indent() << "oprot.writeFieldStop();" << endl <<
+ indent() << "oprot.writeStructEnd();" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
-void t_javame_generator::generate_reflection_getters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
+void t_javame_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
indent(out) << "case " << constant_name(field_name) << ":" << endl;
indent_up();
if (type->is_base_type() && !type->is_string()) {
t_base_type* base_type = (t_base_type*)type;
- indent(out) << "return new " << type_name(type, true, false) << "("
- << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
+ indent(out) << "return new " << type_name(type, true, false) << "(" << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
} else {
indent(out) << "return get" << cap_name << "();" << endl << endl;
}
@@ -1504,10 +1549,7 @@
indent_down();
}
-void t_javame_generator::generate_reflection_setters(ostringstream& out,
- t_type* type,
- string field_name,
- string cap_name) {
+void t_javame_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) {
indent(out) << "case " << constant_name(field_name) << ":" << endl;
indent_up();
indent(out) << "if (value == null) {" << endl;
@@ -1520,9 +1562,8 @@
indent_down();
}
-void t_javame_generator::generate_generic_field_getters_setters(std::ofstream& out,
- t_struct* tstruct) {
- (void)out;
+void t_javame_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) {
+ (void) out;
std::ostringstream getter_stream;
std::ostringstream setter_stream;
@@ -1540,6 +1581,7 @@
generate_reflection_getters(getter_stream, type, field_name, cap_name);
indent_down();
}
+
}
/**
@@ -1548,7 +1590,8 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_bean_boilerplate(ofstream& out, t_struct* tstruct) {
+void t_javame_generator::generate_java_bean_boilerplate(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1563,8 +1606,8 @@
out << get_cap_name("size() {") << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? 0 : "
- << "this." << field_name << ".size();" << endl;
+ indent(out) << "return (this." << field_name << " == null) ? 0 : " <<
+ "this." << field_name << ".size();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
}
@@ -1583,8 +1626,8 @@
out << get_cap_name("Enumeration() {") << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? null : "
- << "this." << field_name << ".elements();" << endl;
+ indent(out) << "return (this." << field_name << " == null) ? null : " <<
+ "this." << field_name << ".elements();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
@@ -1596,16 +1639,17 @@
indent_up();
indent(out) << "if (this." << field_name << " == null) {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
- << endl;
+ indent(out) << "this." << field_name << " = new " << type_name(type, false, true) <<
+ "();" << endl;
indent_down();
indent(out) << "}" << endl;
if (type->is_set()) {
- indent(out) << "this." << field_name << ".put(" << box_type(element_type, "elem") << ", "
- << box_type(element_type, "elem") << ");" << endl;
+ indent(out) << "this." << field_name << ".put(" <<
+ box_type(element_type, "elem") << ", " <<
+ box_type(element_type, "elem") << ");" << endl;
} else {
- indent(out) << "this." << field_name << ".addElement(" << box_type(element_type, "elem")
- << ");" << endl;
+ indent(out) << "this." << field_name << ".addElement(" <<
+ box_type(element_type, "elem") << ");" << endl;
}
indent_down();
indent(out) << "}" << endl << endl;
@@ -1616,14 +1660,15 @@
t_type* val_type = ((t_map*)type)->get_val_type();
indent(out);
- out << "public void putTo" << cap_name << "(" << type_name(key_type, true) << " key, "
- << type_name(val_type, true) << " val) {" << endl;
+ out << "public void putTo"
+ << cap_name << "(" << type_name(key_type, true) << " key, "
+ << type_name(val_type, true) << " val) {" << endl;
indent_up();
indent(out) << "if (this." << field_name << " == null) {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
- << endl;
+ indent(out) << "this." << field_name << " = new " <<
+ type_name(type, false, true) << "();" << endl;
indent_down();
indent(out) << "}" << endl;
indent(out) << "this." << field_name << ".put(key, val);" << endl;
@@ -1634,7 +1679,8 @@
// Simple getter
generate_java_doc(out, field);
indent(out) << "public " << type_name(type);
- if (type->is_base_type() && ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
+ if (type->is_base_type() &&
+ ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
out << " is";
} else {
out << " get";
@@ -1651,7 +1697,8 @@
out << "void";
out << " set" << cap_name << "(" << type_name(type) << " " << field_name << ") {" << endl;
indent_up();
- indent(out) << "this." << field_name << " = " << field_name << ";" << endl;
+ indent(out) << "this." << field_name << " = " << field_name << ";" <<
+ endl;
generate_isset_set(out, field);
indent_down();
@@ -1669,8 +1716,7 @@
indent(out) << "}" << endl << endl;
// isSet method
- indent(out) << "/** Returns true if field " << field_name
- << " is set (has been assigned a value) and false otherwise */" << endl;
+ indent(out) << "/** Returns true if field " << field_name << " is set (has been assigned a value) and false otherwise */" << endl;
indent(out) << "public boolean is" << get_cap_name("set") << cap_name << "() {" << endl;
indent_up();
if (type_can_be_null(type)) {
@@ -1681,8 +1727,7 @@
indent_down();
indent(out) << "}" << endl << endl;
- indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {"
- << endl;
+ indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {" << endl;
indent_up();
if (type_can_be_null(type)) {
indent(out) << "if (!value) {" << endl;
@@ -1701,12 +1746,13 @@
*
* @param tstruct The struct definition
*/
-void t_javame_generator::generate_java_struct_tostring(ofstream& out, t_struct* tstruct) {
+void t_javame_generator::generate_java_struct_tostring(ofstream& out,
+ t_struct* tstruct) {
out << indent() << "public String toString() {" << endl;
indent_up();
- out << indent() << "StringBuffer sb = new StringBuffer(\"" << tstruct->get_name() << "(\");"
- << endl;
+ out <<
+ indent() << "StringBuffer sb = new StringBuffer(\"" << tstruct->get_name() << "(\");" << endl;
out << indent() << "boolean first = true;" << endl << endl;
const vector<t_field*>& fields = tstruct->get_members();
@@ -1714,7 +1760,7 @@
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
- if (could_be_unset) {
+ if(could_be_unset) {
indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
indent_up();
}
@@ -1745,16 +1791,19 @@
}
indent(out) << "first = false;" << endl;
- if (could_be_unset) {
+ if(could_be_unset) {
indent_down();
indent(out) << "}" << endl;
}
first = false;
}
- out << indent() << "sb.append(\")\");" << endl << indent() << "return sb.toString();" << endl;
+ out <<
+ indent() << "sb.append(\")\");" << endl <<
+ indent() << "return sb.toString();" << endl;
indent_down();
- indent(out) << "}" << endl << endl;
+ indent(out) << "}" << endl <<
+ endl;
}
/**
@@ -1762,7 +1811,7 @@
* (e.g. for the type struct it returns "TType.STRUCT")
*/
std::string t_javame_generator::get_java_type_string(t_type* type) {
- if (type->is_list()) {
+ if (type->is_list()){
return "TType.LIST";
} else if (type->is_map()) {
return "TType.MAP";
@@ -1776,58 +1825,37 @@
return get_java_type_string(((t_typedef*)type)->get_type());
} else if (type->is_base_type()) {
switch (((t_base_type*)type)->get_base()) {
- case t_base_type::TYPE_VOID:
- return "TType.VOID";
- break;
- case t_base_type::TYPE_STRING:
- return "TType.STRING";
- break;
- case t_base_type::TYPE_BOOL:
- return "TType.BOOL";
- break;
- case t_base_type::TYPE_BYTE:
- return "TType.BYTE";
- break;
- case t_base_type::TYPE_I16:
- return "TType.I16";
- break;
- case t_base_type::TYPE_I32:
- return "TType.I32";
- break;
- case t_base_type::TYPE_I64:
- return "TType.I64";
- break;
- case t_base_type::TYPE_DOUBLE:
- return "TType.DOUBLE";
- break;
- default:
- throw std::runtime_error("Unknown thrift type \"" + type->get_name()
- + "\" passed to t_javame_generator::get_java_type_string!");
- break; // This should never happen!
+ case t_base_type::TYPE_VOID : return "TType.VOID"; break;
+ case t_base_type::TYPE_STRING : return "TType.STRING"; break;
+ case t_base_type::TYPE_BOOL : return "TType.BOOL"; break;
+ case t_base_type::TYPE_BYTE : return "TType.BYTE"; break;
+ case t_base_type::TYPE_I16 : return "TType.I16"; break;
+ case t_base_type::TYPE_I32 : return "TType.I32"; break;
+ case t_base_type::TYPE_I64 : return "TType.I64"; break;
+ case t_base_type::TYPE_DOUBLE : return "TType.DOUBLE"; break;
+ default : throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_javame_generator::get_java_type_string!"); break; // This should never happen!
}
} else {
- throw std::runtime_error(
- "Unknown thrift type \"" + type->get_name()
- + "\" passed to t_javame_generator::get_java_type_string!"); // This should never happen!
+ throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_javame_generator::get_java_type_string!"); // This should never happen!
}
}
-void t_javame_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type) {
+void t_javame_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){
out << endl;
indent_up();
indent_up();
- if (type->is_struct()) {
+ if (type->is_struct()){
indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type) << ".class";
- } else if (type->is_container()) {
- if (type->is_list()) {
+ } else if (type->is_container()){
+ if (type->is_list()){
indent(out) << "new ListMetaData(TType.LIST, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else if (type->is_set()) {
+ } else if (type->is_set()){
indent(out) << "new SetMetaData(TType.SET, ";
t_type* elem_type = ((t_list*)type)->get_elem_type();
generate_field_value_meta_data(out, elem_type);
- } else { // map
+ } else{ // map
indent(out) << "new MapMetaData(TType.MAP, ";
t_type* key_type = ((t_map*)type)->get_key_type();
t_type* val_type = ((t_map*)type)->get_val_type();
@@ -1848,6 +1876,7 @@
indent_down();
}
+
/**
* Generates a thrift service. In C++, this comprises an entirely separate
* header and source file. The header file defines the methods and includes
@@ -1858,12 +1887,18 @@
*/
void t_javame_generator::generate_service(t_service* tservice) {
// Make output file
- string f_service_name = package_dir_ + "/" + service_name_ + ".java";
+ string f_service_name = package_dir_+"/"+service_name_+".java";
f_service_.open(f_service_name.c_str());
- f_service_ << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
+ f_service_ <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
- f_service_ << "public class " << service_name_ << " {" << endl << endl;
+ f_service_ <<
+ "public class " << service_name_ << " {" << endl <<
+ endl;
indent_up();
// Generate the three main parts of the service
@@ -1873,7 +1908,8 @@
generate_service_helpers(tservice);
indent_down();
- f_service_ << "}" << endl;
+ f_service_ <<
+ "}" << endl;
f_service_.close();
}
@@ -1883,8 +1919,8 @@
* @param tservice The service to generate a header definition for
*/
void t_javame_generator::generate_primitive_service_interface(t_service* tservice) {
- f_service_ << indent() << "public interface Iface extends " << service_name_ << "Iface { }"
- << endl << endl;
+ f_service_ << indent() << "public interface Iface extends " <<
+ service_name_ << "Iface { }" << endl << endl;
string f_interface_name = package_dir_ + "/" + service_name_ + "Iface.java";
std::ofstream f_iface;
@@ -1895,10 +1931,14 @@
extends_iface = " extends " + type_name(tservice->get_extends()) + "Iface";
}
- f_iface << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
+ f_iface <<
+ autogen_comment() <<
+ java_package() <<
+ java_type_imports() <<
+ java_thrift_imports();
generate_java_doc(f_iface, tservice);
- f_iface << "public interface " << service_name_ << "Iface" << extends_iface << " {" << endl
- << endl;
+ f_iface <<
+ "public interface " << service_name_ << "Iface" << extends_iface << " {" << endl << endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
@@ -1922,7 +1962,8 @@
}
generate_java_doc(f_service_, tservice);
- f_service_ << indent() << "public interface Iface" << extends_iface << " {" << endl << endl;
+ f_service_ << indent() << "public interface Iface" << extends_iface <<
+ " {" << endl << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
@@ -1962,42 +2003,56 @@
extends_client = " extends " + extends + ".Client";
}
- indent(f_service_) << "public static class Client" << extends_client
- << " implements TServiceClient, Iface {" << endl;
+ indent(f_service_) <<
+ "public static class Client" << extends_client << " implements TServiceClient, Iface {" << endl;
indent_up();
- indent(f_service_) << "public Client(TProtocol prot)" << endl;
+ indent(f_service_) <<
+ "public Client(TProtocol prot)" << endl;
scope_up(f_service_);
- indent(f_service_) << "this(prot, prot);" << endl;
+ indent(f_service_) <<
+ "this(prot, prot);" << endl;
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public Client(TProtocol iprot, TProtocol oprot)" << endl;
+ indent(f_service_) <<
+ "public Client(TProtocol iprot, TProtocol oprot)" << endl;
scope_up(f_service_);
if (extends.empty()) {
- f_service_ << indent() << "iprot_ = iprot;" << endl << indent() << "oprot_ = oprot;" << endl;
+ f_service_ <<
+ indent() << "iprot_ = iprot;" << endl <<
+ indent() << "oprot_ = oprot;" << endl;
} else {
- f_service_ << indent() << "super(iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "super(iprot, oprot);" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected TProtocol iprot_;" << endl << indent()
- << "protected TProtocol oprot_;" << endl << endl << indent()
- << "protected int seqid_;" << endl << endl;
+ f_service_ <<
+ indent() << "protected TProtocol iprot_;" << endl <<
+ indent() << "protected TProtocol oprot_;" << endl <<
+ endl <<
+ indent() << "protected int seqid_;" << endl <<
+ endl;
- indent(f_service_) << "public TProtocol getInputProtocol()" << endl;
+ indent(f_service_) <<
+ "public TProtocol getInputProtocol()" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.iprot_;" << endl;
+ indent(f_service_) <<
+ "return this.iprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public TProtocol getOutputProtocol()" << endl;
+ indent(f_service_) <<
+ "public TProtocol getOutputProtocol()" << endl;
scope_up(f_service_);
- indent(f_service_) << "return this.oprot_;" << endl;
+ indent(f_service_) <<
+ "return this.oprot_;" << endl;
scope_down(f_service_);
f_service_ << endl;
+
}
// Generate client method implementations
@@ -2007,9 +2062,11 @@
string funname = (*f_iter)->get_name();
// Open function
- indent(f_service_) << "public " << function_signature(*f_iter) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(*f_iter) << endl;
scope_up(f_service_);
- indent(f_service_) << "send_" << funname << "(";
+ indent(f_service_) <<
+ "send_" << funname << "(";
// Get the struct of function call params
t_struct* arg_struct = (*f_iter)->get_arglist();
@@ -2033,7 +2090,8 @@
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "recv_" << funname << "();" << endl;
+ f_service_ <<
+ "recv_" << funname << "();" << endl;
}
scope_down(f_service_);
f_service_ << endl;
@@ -2045,23 +2103,26 @@
string argsname = (*f_iter)->get_name() + "_args";
// Open function
- indent(f_service_) << "public " << function_signature(&send_function) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(&send_function) << endl;
scope_up(f_service_);
// Serialize the request
- f_service_ << indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", "
- << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
- << ", ++seqid_));" << endl << indent() << argsname << " args = new " << argsname
- << "();" << endl;
+ f_service_ <<
+ indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", " <<
+ ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL") <<
+ ", ++seqid_));" << endl <<
+ indent() << argsname << " args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "("
- << (*fld_iter)->get_name() << ");" << endl;
+ f_service_ <<
+ indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl;
}
- f_service_ << indent() << "args.write(oprot_);" << endl << indent()
- << "oprot_.writeMessageEnd();" << endl << indent()
- << "oprot_.getTransport().flush();" << endl;
+ f_service_ <<
+ indent() << "args.write(oprot_);" << endl <<
+ indent() << "oprot_.writeMessageEnd();" << endl <<
+ indent() << "oprot_.getTransport().flush();" << endl;
scope_down(f_service_);
f_service_ << endl;
@@ -2075,43 +2136,49 @@
&noargs,
(*f_iter)->get_xceptions());
// Open function
- indent(f_service_) << "public " << function_signature(&recv_function) << endl;
+ indent(f_service_) <<
+ "public " << function_signature(&recv_function) << endl;
scope_up(f_service_);
- f_service_ << indent() << "TMessage msg = iprot_.readMessageBegin();" << endl << indent()
- << "if (msg.type == TMessageType.EXCEPTION) {" << endl << indent()
- << " TApplicationException x = TApplicationException.read(iprot_);" << endl
- << indent() << " iprot_.readMessageEnd();" << endl << indent() << " throw x;"
- << endl << indent() << "}" << endl << indent() << "if (msg.seqid != seqid_) {"
- << endl << indent()
- << " throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, \""
- << (*f_iter)->get_name() << " failed: out of sequence response\");" << endl
- << indent() << "}" << endl << indent() << resultname << " result = new "
- << resultname << "();" << endl << indent() << "result.read(iprot_);" << endl
- << indent() << "iprot_.readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "TMessage msg = iprot_.readMessageBegin();" << endl <<
+ indent() << "if (msg.type == TMessageType.EXCEPTION) {" << endl <<
+ indent() << " TApplicationException x = TApplicationException.read(iprot_);" << endl <<
+ indent() << " iprot_.readMessageEnd();" << endl <<
+ indent() << " throw x;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (msg.seqid != seqid_) {" << endl <<
+ indent() << " throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, \"" << (*f_iter)->get_name() << " failed: out of sequence response\");" << endl <<
+ indent() << "}" << endl <<
+ indent() << resultname << " result = new " << resultname << "();" << endl <<
+ indent() << "result.read(iprot_);" << endl <<
+ indent() << "iprot_.readMessageEnd();" << endl;
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (result." << generate_isset_check("success") << ") {" << endl
- << indent() << " return result.success;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << generate_isset_check("success") << ") {" << endl <<
+ indent() << " return result.success;" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl
- << indent() << " throw result." << (*x_iter)->get_name() << ";" << endl
- << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl <<
+ indent() << " throw result." << (*x_iter)->get_name() << ";" << endl <<
+ indent() << "}" << endl;
}
// If you get here it's an exception, unless a void function
if ((*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "return;" << endl;
+ indent(f_service_) <<
+ "return;" << endl;
} else {
- f_service_ << indent()
- << "throw new TApplicationException(TApplicationException.MISSING_RESULT, \""
- << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
+ f_service_ <<
+ indent() << "throw new TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
// Close function
@@ -2121,7 +2188,8 @@
}
indent_down();
- indent(f_service_) << "}" << endl;
+ indent(f_service_) <<
+ "}" << endl;
}
/**
@@ -2143,64 +2211,72 @@
}
// Generate the header portion
- indent(f_service_) << "public static class Processor" << extends_processor
- << " implements TProcessor {" << endl;
+ indent(f_service_) <<
+ "public static class Processor" << extends_processor << " implements TProcessor {" << endl;
indent_up();
- indent(f_service_) << "public Processor(Iface iface)" << endl;
+ indent(f_service_) <<
+ "public Processor(Iface iface)" << endl;
scope_up(f_service_);
if (!extends.empty()) {
- f_service_ << indent() << "super(iface);" << endl;
+ f_service_ <<
+ indent() << "super(iface);" << endl;
}
- f_service_ << indent() << "iface_ = iface;" << endl;
+ f_service_ <<
+ indent() << "iface_ = iface;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "processMap_.put(\"" << (*f_iter)->get_name() << "\", new "
- << (*f_iter)->get_name() << "());" << endl;
+ f_service_ <<
+ indent() << "processMap_.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl;
}
scope_down(f_service_);
f_service_ << endl;
if (extends.empty()) {
- f_service_
- << indent() << "protected static interface ProcessFunction {" << endl << indent()
- << " public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException;"
- << endl << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "protected static interface ProcessFunction {" << endl <<
+ indent() << " public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException;" << endl <<
+ indent() << "}" << endl <<
+ endl;
}
- f_service_ << indent() << "private Iface iface_;" << endl;
+ f_service_ <<
+ indent() << "private Iface iface_;" << endl;
if (extends.empty()) {
- f_service_ << indent() << "protected final Hashtable processMap_ = new Hashtable();" << endl;
+ f_service_ <<
+ indent() << "protected final Hashtable processMap_ = new Hashtable();" << endl;
}
f_service_ << endl;
// Generate the server implementation
- indent(f_service_) << "public boolean process(TProtocol iprot, TProtocol oprot) throws TException"
- << endl;
+ indent(f_service_) <<
+ "public boolean process(TProtocol iprot, TProtocol oprot) throws TException" << endl;
scope_up(f_service_);
- f_service_ << indent() << "TMessage msg = iprot.readMessageBegin();" << endl;
+ f_service_ <<
+ indent() << "TMessage msg = iprot.readMessageBegin();" << endl;
// TODO(mcslee): validate message, was the seqid etc. legit?
- f_service_
- << indent() << "ProcessFunction fn = (ProcessFunction)processMap_.get(msg.name);" << endl
- << indent() << "if (fn == null) {" << endl << indent()
- << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl << indent()
- << " iprot.readMessageEnd();" << endl << indent()
- << " TApplicationException x = new "
- "TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid method name: "
- "'\"+msg.name+\"'\");" << endl << indent()
- << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));"
- << endl << indent() << " x.write(oprot);" << endl << indent() << " oprot.writeMessageEnd();"
- << endl << indent() << " oprot.getTransport().flush();" << endl << indent()
- << " return true;" << endl << indent() << "}" << endl << indent()
- << "fn.process(msg.seqid, iprot, oprot);" << endl;
+ f_service_ <<
+ indent() << "ProcessFunction fn = (ProcessFunction)processMap_.get(msg.name);" << endl <<
+ indent() << "if (fn == null) {" << endl <<
+ indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl <<
+ indent() << " iprot.readMessageEnd();" << endl <<
+ indent() << " TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid method name: '\"+msg.name+\"'\");" << endl <<
+ indent() << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));" << endl <<
+ indent() << " x.write(oprot);" << endl <<
+ indent() << " oprot.writeMessageEnd();" << endl <<
+ indent() << " oprot.getTransport().flush();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "fn.process(msg.seqid, iprot, oprot);" << endl;
- f_service_ << indent() << "return true;" << endl;
+ f_service_ <<
+ indent() << "return true;" << endl;
scope_down(f_service_);
f_service_ << endl;
@@ -2211,7 +2287,9 @@
}
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) <<
+ "}" << endl <<
+ endl;
}
/**
@@ -2245,40 +2323,44 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_javame_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_javame_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open class
- indent(f_service_) << "private class " << tfunction->get_name() << " implements ProcessFunction {"
- << endl;
+ indent(f_service_) <<
+ "private class " << tfunction->get_name() << " implements ProcessFunction {" << endl;
indent_up();
// Open function
- indent(f_service_)
- << "public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException"
- << endl;
+ indent(f_service_) <<
+ "public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException" << endl;
scope_up(f_service_);
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
- f_service_ << indent() << argsname << " args = new " << argsname << "();" << endl << indent()
- << "try {" << endl;
+ f_service_ <<
+ indent() << argsname << " args = new " << argsname << "();" << endl <<
+ indent() << "try {" << endl;
indent_up();
- f_service_ << indent() << "args.read(iprot);" << endl;
+ f_service_ <<
+ indent() << "args.read(iprot);" << endl;
indent_down();
- f_service_ << indent() << "} catch (TProtocolException e) {" << endl;
+ f_service_ <<
+ indent() << "} catch (TProtocolException e) {" << endl;
indent_up();
- f_service_ << indent() << "iprot.readMessageEnd();" << endl << indent()
- << "TApplicationException x = new "
- "TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());"
- << endl << indent() << "oprot.writeMessageBegin(new TMessage(\""
- << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl << indent()
- << "x.write(oprot);" << endl << indent() << "oprot.writeMessageEnd();" << endl
- << indent() << "oprot.getTransport().flush();" << endl << indent() << "return;"
- << endl;
+ f_service_ <<
+ indent() << "iprot.readMessageEnd();" << endl <<
+ indent() << "TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());" << endl <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl <<
+ indent() << "return;" << endl;
indent_down();
f_service_ << indent() << "}" << endl;
- f_service_ << indent() << "iprot.readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "iprot.readMessageEnd();" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -2286,12 +2368,14 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << resultname << " result = new " << resultname << "();" << endl;
+ f_service_ <<
+ indent() << resultname << " result = new " << resultname << "();" << endl;
}
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "try {" << endl;
+ f_service_ <<
+ indent() << "try {" << endl;
indent_up();
}
@@ -2304,7 +2388,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result.success = ";
}
- f_service_ << "iface_." << tfunction->get_name() << "(";
+ f_service_ <<
+ "iface_." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -2317,22 +2402,20 @@
f_service_ << ");" << endl;
// Set isset on success field
- if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()
- && !type_can_be_null(tfunction->get_returntype())) {
- f_service_ << indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet")
- << "(true);" << endl;
+ if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void() && !type_can_be_null(tfunction->get_returntype())) {
+ f_service_ <<
+ indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl;
}
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
f_service_ << indent() << "}";
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " "
- << (*x_iter)->get_name() << ") {" << endl;
+ f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent() << "}";
} else {
@@ -2341,33 +2424,36 @@
}
f_service_ << " catch (Throwable th) {" << endl;
indent_up();
- f_service_ << indent() << "TApplicationException x = new "
- "TApplicationException(TApplicationException.INTERNAL_ERROR, "
- "\"Internal error processing " << tfunction->get_name() << "\");"
- << endl << indent() << "oprot.writeMessageBegin(new TMessage(\""
- << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl
- << indent() << "x.write(oprot);" << endl << indent() << "oprot.writeMessageEnd();"
- << endl << indent() << "oprot.getTransport().flush();" << endl << indent()
- << "return;" << endl;
+ f_service_ <<
+ indent() << "TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, \"Internal error processing " << tfunction->get_name() << "\");" << endl <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+ indent() << "x.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl <<
+ indent() << "return;" << endl;
indent_down();
f_service_ << indent() << "}" << endl;
}
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
scope_down(f_service_);
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
return;
}
- f_service_ << indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid));" << endl << indent() << "result.write(oprot);"
- << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
- << "oprot.getTransport().flush();" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid));" << endl <<
+ indent() << "result.write(oprot);" << endl <<
+ indent() << "oprot.writeMessageEnd();" << endl <<
+ indent() << "oprot.getTransport().flush();" << endl;
// Close function
scope_down(f_service_);
@@ -2375,7 +2461,9 @@
// Close class
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -2384,17 +2472,22 @@
* @param tfield The field
* @param prefix The variable name or container for this field
*/
-void t_javame_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_javame_generator::generate_deserialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
@@ -2402,46 +2495,44 @@
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:
- if (!((t_base_type*)type)->is_binary()) {
- out << "readString();";
- } else {
- out << "readBinary();";
- }
- break;
- case t_base_type::TYPE_BOOL:
- out << "readBool();";
- break;
- case t_base_type::TYPE_BYTE:
- out << "readByte();";
- break;
- case t_base_type::TYPE_I16:
- out << "readI16();";
- break;
- case t_base_type::TYPE_I32:
- out << "readI32();";
- break;
- case t_base_type::TYPE_I64:
- out << "readI64();";
- break;
- case t_base_type::TYPE_DOUBLE:
- out << "readDouble();";
- break;
- default:
- throw "compiler error: no Java name for base type " + t_base_type::t_base_name(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:
+ if (!((t_base_type*)type)->is_binary()) {
+ out << "readString();";
+ } else {
+ out << "readBinary();";
+ }
+ break;
+ case t_base_type::TYPE_BOOL:
+ out << "readBool();";
+ break;
+ case t_base_type::TYPE_BYTE:
+ out << "readByte();";
+ break;
+ case t_base_type::TYPE_I16:
+ out << "readI16();";
+ break;
+ case t_base_type::TYPE_I32:
+ out << "readI32();";
+ break;
+ case t_base_type::TYPE_I64:
+ out << "readI64();";
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ out << "readDouble();";
+ break;
+ default:
+ throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
}
out << endl;
} else if (type->is_enum()) {
- indent(out) << name << " = "
- << type_name(tfield->get_type(), true, false) + ".findByValue(iprot.readI32());"
- << endl;
+ indent(out) << name << " = " << type_name(tfield->get_type(), true, false) + ".findByValue(iprot.readI32());" << endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type_name(type).c_str());
+ tfield->get_name().c_str(), type_name(type).c_str());
}
}
@@ -2449,18 +2540,19 @@
* Generates an unserializer for a struct, invokes read()
*/
void t_javame_generator::generate_deserialize_struct(ofstream& out,
- t_struct* tstruct,
- string prefix) {
- out << indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << indent()
- << prefix << ".read(iprot);" << endl;
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = new " << type_name(tstruct) << "();" << endl <<
+ indent() << prefix << ".read(iprot);" << endl;
}
/**
* Deserializes a container by reading its size and then iterating
*/
void t_javame_generator::generate_deserialize_container(ofstream& out,
- t_type* ttype,
- string prefix) {
+ t_type* ttype,
+ string prefix) {
scope_up(out);
string obj;
@@ -2482,28 +2574,32 @@
indent(out) << "TList " << obj << " = iprot.readListBegin();" << endl;
}
- indent(out) << prefix << " = new " << type_name(ttype, false, true)
- // size the collection correctly
- << "(" << (ttype->is_list() ? "" : "2*") << obj << ".size"
- << ");" << endl;
+ indent(out)
+ << prefix << " = new " << type_name(ttype, false, true)
+ // size the collection correctly
+ << "("
+ << (ttype->is_list() ? "" : "2*" )
+ << obj << ".size"
+ << ");" << endl;
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for (int " << i << " = 0; " << i << " < " << obj << ".size"
- << "; "
- << "++" << i << ")" << endl;
+ indent(out) <<
+ "for (int " << i << " = 0; " <<
+ i << " < " << obj << ".size" << "; " <<
+ "++" << i << ")" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- scope_down(out);
+ scope_down(out);
// Read container end
if (ttype->is_map()) {
@@ -2517,89 +2613,110 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
void t_javame_generator::generate_deserialize_map_element(ofstream& out,
- t_map* tmap,
- string prefix) {
+ t_map* tmap,
+ string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey) << endl;
- indent(out) << declare_field(&fval) << endl;
+ indent(out) <<
+ declare_field(&fkey) << endl;
+ indent(out) <<
+ declare_field(&fval) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << ".put(" << box_type(tmap->get_key_type(), key) << ", "
- << box_type(tmap->get_val_type(), val) << ");" << endl;
+ indent(out) <<
+ prefix << ".put(" <<
+ box_type(tmap->get_key_type(), key) << ", " <<
+ box_type(tmap->get_val_type(), val) << ");" << endl;
}
/**
* Deserializes a set element
*/
void t_javame_generator::generate_deserialize_set_element(ofstream& out,
- t_set* tset,
- string prefix) {
+ t_set* tset,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".put(" << box_type(tset->get_elem_type(), elem) << ", "
- << box_type(tset->get_elem_type(), elem) << ");" << endl;
+ indent(out) <<
+ prefix << ".put(" <<
+ box_type(tset->get_elem_type(), elem) << ", " <<
+ box_type(tset->get_elem_type(), elem) << ");" << endl;
}
/**
* Deserializes a list element
*/
void t_javame_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix) {
+ t_list* tlist,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << declare_field(&felem) << endl;
+ indent(out) <<
+ declare_field(&felem) << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".addElement(" << box_type(tlist->get_elem_type(), elem) << ");" << endl;
+ indent(out) <<
+ prefix << ".addElement(" <<
+ box_type(tlist->get_elem_type(), elem) << ");" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_javame_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_javame_generator::generate_serialize_field(ofstream& out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
- } else if (type->is_enum()) {
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
+ } else if (type->is_enum()){
indent(out) << "oprot.writeI32(" << prefix + tfield->get_name() << ".getValue());" << endl;
} else if (type->is_base_type()) {
string name = prefix + tfield->get_name();
- indent(out) << "oprot.";
+ indent(out) <<
+ "oprot.";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
@@ -2648,10 +2765,11 @@
* @param prefix String prefix to attach to all fields
*/
void t_javame_generator::generate_serialize_struct(ofstream& out,
- t_struct* tstruct,
- string prefix) {
- (void)tstruct;
- out << indent() << prefix << ".write(oprot);" << endl;
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
+ out <<
+ indent() << prefix << ".write(oprot);" << endl;
}
/**
@@ -2660,47 +2778,56 @@
* @param ttype The type of container
* @param prefix String prefix for fields
*/
-void t_javame_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_javame_generator::generate_serialize_container(ofstream& out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix
- << ".size()));" << endl;
+ indent(out) <<
+ "oprot.writeMapBegin(new TMap(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ prefix << ".size()));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", " << prefix << ".size()));" << endl;
+ indent(out) <<
+ "oprot.writeSetBegin(new TSet(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListBegin(new TList("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".size()));"
- << endl;
+ indent(out) <<
+ "oprot.writeListBegin(new TList(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".size()));" << endl;
}
string iter = tmp("_iter");
if (ttype->is_map()) {
string enumer = iter + "_enum";
string key_type = type_name(((t_map*)ttype)->get_key_type(), true, false);
- indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer
- << ".hasMoreElements(); ) ";
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer << ".hasMoreElements(); ) ";
scope_up(out);
- indent(out) << key_type << " " << iter << " = (" << key_type << ")" << enumer
- << ".nextElement();" << endl;
+ indent(out)
+ << key_type << " " << iter << " = (" << key_type << ")" << enumer << ".nextElement();" << endl;
} else if (ttype->is_set()) {
string enumer = iter + "_enum";
string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
- indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer
- << ".hasMoreElements(); ) ";
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer << ".hasMoreElements(); ) ";
scope_up(out);
- indent(out) << ele_type << " " << iter << " = (" << ele_type << ")" << enumer
- << ".nextElement();" << endl;
+ indent(out)
+ << ele_type << " " << iter << " = ("
+ << ele_type << ")" << enumer << ".nextElement();" << endl;
} else if (ttype->is_list()) {
string enumer = iter + "_enum";
- indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".elements(); " << enumer
- << ".hasMoreElements(); ) ";
+ indent(out) <<
+ "for (Enumeration " << enumer << " = " << prefix << ".elements(); " << enumer << ".hasMoreElements(); ) ";
scope_up(out);
string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
- indent(out) << ele_type << " " << iter << " = (" << ele_type << ")" << enumer
- << ".nextElement();" << endl;
+ indent(out)
+ << ele_type << " " << iter << " = ("
+ << ele_type << ")" << enumer << ".nextElement();" << endl;
}
if (ttype->is_map()) {
@@ -2713,11 +2840,14 @@
scope_down(out);
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapEnd();" << endl;
+ indent(out) <<
+ "oprot.writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetEnd();" << endl;
+ indent(out) <<
+ "oprot.writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListEnd();" << endl;
+ indent(out) <<
+ "oprot.writeListEnd();" << endl;
}
scope_down(out);
@@ -2727,9 +2857,9 @@
* Serializes the members of a map.
*/
void t_javame_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string iter,
- string map) {
+ t_map* tmap,
+ string iter,
+ string map) {
t_field kfield(tmap->get_key_type(), iter);
generate_serialize_field(out, &kfield, "");
string val_type = type_name(tmap->get_val_type(), true, false);
@@ -2740,7 +2870,9 @@
/**
* Serializes the members of a set.
*/
-void t_javame_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_javame_generator::generate_serialize_set_element(ofstream& out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2749,8 +2881,8 @@
* Serializes the members of a list.
*/
void t_javame_generator::generate_serialize_list_element(ofstream& out,
- t_list* tlist,
- string iter) {
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2762,12 +2894,9 @@
* @param container Is the type going inside a container?
* @return Java type name, i.e. Vector
*/
-string t_javame_generator::type_name(t_type* ttype,
- bool in_container,
- bool in_init,
- bool skip_generic) {
- (void)in_init;
- (void)skip_generic;
+string t_javame_generator::type_name(t_type* ttype, bool in_container, bool in_init, bool skip_generic) {
+ (void) in_init;
+ (void) skip_generic;
// In Java typedefs are just resolved to their real type
ttype = get_true_type(ttype);
string prefix;
@@ -2800,7 +2929,8 @@
* @param tbase The base type
* @param container Is it going in a Java container?
*/
-string t_javame_generator::base_type_name(t_base_type* type, bool in_container) {
+string t_javame_generator::base_type_name(t_base_type* type,
+ bool in_container) {
t_base_type::t_base tbase = type->get_base();
switch (tbase) {
@@ -2862,15 +2992,14 @@
case t_base_type::TYPE_DOUBLE:
result += " = (double)0";
break;
- }
+ }
} else if (ttype->is_enum()) {
result += " = 0";
} else if (ttype->is_container()) {
result += " = new " + type_name(ttype, false, true) + "()";
} else {
- result += " = new " + type_name(ttype, false, true) + "()";
- ;
+ result += " = new " + type_name(ttype, false, true) + "()";;
}
}
return result + ";";
@@ -2882,10 +3011,11 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_javame_generator::function_signature(t_function* tfunction, string prefix) {
+string t_javame_generator::function_signature(t_function* tfunction,
+ string prefix) {
t_type* ttype = tfunction->get_returntype();
- std::string result = type_name(ttype) + " " + prefix + tfunction->get_name() + "("
- + argument_list(tfunction->get_arglist()) + ") throws ";
+ std::string result =
+ type_name(ttype) + " " + prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ") throws ";
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
@@ -2963,7 +3093,7 @@
/**
* Applies the correct style to a string based on the value of nocamel_style_
*/
-std::string t_javame_generator::get_cap_name(std::string name) {
+std::string t_javame_generator::get_cap_name(std::string name){
name[0] = toupper(name[0]);
return name;
}
@@ -2990,11 +3120,15 @@
return constant_name;
}
-void t_javame_generator::generate_java_docstring_comment(ofstream& out, string contents) {
- generate_docstring_comment(out, "/**\n", " * ", contents, " */\n");
+void t_javame_generator::generate_java_docstring_comment(ofstream &out, string contents) {
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", contents,
+ " */\n");
}
-void t_javame_generator::generate_java_doc(ofstream& out, t_field* field) {
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_field* field) {
if (field->get_type()->is_enum()) {
string combined_message = field->get_doc() + "\n@see " + get_enum_class_name(field->get_type());
generate_java_docstring_comment(out, combined_message);
@@ -3006,7 +3140,8 @@
/**
* Emits a JavaDoc comment if the provided object has a doc in Thrift
*/
-void t_javame_generator::generate_java_doc(ofstream& out, t_doc* tdoc) {
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_doc* tdoc) {
if (tdoc->has_doc()) {
generate_java_docstring_comment(out, tdoc->get_doc());
}
@@ -3015,7 +3150,8 @@
/**
* Emits a JavaDoc comment if the provided function object has a doc in Thrift
*/
-void t_javame_generator::generate_java_doc(ofstream& out, t_function* tfunction) {
+void t_javame_generator::generate_java_doc(ofstream &out,
+ t_function* tfunction) {
if (tfunction->has_doc()) {
stringstream ss;
ss << tfunction->get_doc();
@@ -3028,85 +3164,66 @@
ss << " " << p->get_doc();
}
}
- generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
}
}
-void t_javame_generator::generate_deep_copy_container(ofstream& out,
- std::string source_name_p1,
- std::string source_name_p2,
- std::string result_name,
- t_type* type) {
+void t_javame_generator::generate_deep_copy_container(ofstream &out, std::string source_name_p1, std::string source_name_p2,
+ std::string result_name, t_type* type) {
t_container* container = (t_container*)type;
std::string source_name;
if (source_name_p2 == "")
- source_name = source_name_p1;
+ source_name = source_name_p1;
else
- source_name = source_name_p1 + "." + source_name_p2;
+ source_name = source_name_p1 + "." + source_name_p2;
- indent(out) << type_name(type, true, false) << " " << result_name << " = new "
- << type_name(container, false, true) << "();" << endl;
+ indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "();" << endl;
std::string iterator_element_name = source_name_p1 + "_element";
std::string enumeration_name = source_name_p1 + "_enum";
std::string result_element_name = result_name + "_copy";
- if (container->is_map()) {
+ if(container->is_map()) {
t_type* key_type = ((t_map*)container)->get_key_type();
t_type* val_type = ((t_map*)container)->get_val_type();
- indent(out) << "for (Enumeration " << enumeration_name << " = " << source_name << ".keys(); "
- << enumeration_name << ".hasMoreElements(); ) {" << endl;
+ indent(out) <<
+ "for (Enumeration " << enumeration_name << " = " << source_name << ".keys(); " << enumeration_name << ".hasMoreElements(); ) {" << endl;
indent_up();
out << endl;
- indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = ("
- << type_name(key_type, true, false) << ")" << enumeration_name << ".nextElement();"
- << endl;
+ indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = (" << type_name(key_type, true, false) << ")" << enumeration_name << ".nextElement();" << endl;
indent(out) << type_name(val_type, true, false) << " " << iterator_element_name << "_value = ("
- << type_name(val_type, true, false) << ")" << source_name << ".get("
- << iterator_element_name << "_key);" << endl;
+ << type_name(val_type, true, false) << ")" << source_name << ".get(" << iterator_element_name << "_key);" << endl;
out << endl;
if (key_type->is_container()) {
- generate_deep_copy_container(out,
- iterator_element_name + "_key",
- "",
- result_element_name + "_key",
- key_type);
+ generate_deep_copy_container(out, iterator_element_name + "_key", "", result_element_name + "_key", key_type);
} else {
indent(out) << type_name(key_type, true, false) << " " << result_element_name << "_key = ";
- generate_deep_copy_non_container(out,
- iterator_element_name + "_key",
- result_element_name + "_key",
- key_type);
+ generate_deep_copy_non_container(out, iterator_element_name + "_key", result_element_name + "_key", key_type);
out << ";" << endl;
}
out << endl;
if (val_type->is_container()) {
- generate_deep_copy_container(out,
- iterator_element_name + "_value",
- "",
- result_element_name + "_value",
- val_type);
+ generate_deep_copy_container(out, iterator_element_name + "_value", "", result_element_name + "_value", val_type);
} else {
indent(out) << type_name(val_type, true, false) << " " << result_element_name << "_value = ";
- generate_deep_copy_non_container(out,
- iterator_element_name + "_value",
- result_element_name + "_value",
- val_type);
+ generate_deep_copy_non_container(out, iterator_element_name + "_value", result_element_name + "_value", val_type);
out << ";" << endl;
}
out << endl;
- indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name
- << "_value);" << endl;
+ indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name << "_value);" << endl;
indent_down();
indent(out) << "}" << endl;
@@ -3120,36 +3237,33 @@
elem_type = ((t_list*)container)->get_elem_type();
}
- indent(out) << "for (Enumeration " << enumeration_name << " = " << source_name
- << ".elements(); " << enumeration_name << ".hasMoreElements(); ) {" << endl;
+ indent(out)
+ << "for (Enumeration " << enumeration_name << " = " << source_name << ".elements(); "
+ << enumeration_name << ".hasMoreElements(); ) {" << endl;
indent_up();
- indent(out) << type_name(elem_type, true, false) << " " << iterator_element_name << " = ("
- << type_name(elem_type, true, false) << ")" << enumeration_name << ".nextElement();"
- << endl;
+ indent(out)
+ << type_name(elem_type, true, false) << " " << iterator_element_name << " = ("
+ << type_name(elem_type, true, false) << ")" << enumeration_name << ".nextElement();" << endl;
if (elem_type->is_container()) {
// recursive deep copy
generate_deep_copy_container(out, iterator_element_name, "", result_element_name, elem_type);
if (elem_type->is_list()) {
- indent(out) << result_name << ".addElement(" << result_element_name << ");" << endl;
+ indent(out) << result_name << ".addElement(" << result_element_name << ");" << endl;
} else {
- indent(out) << result_name << ".put(" << result_element_name << ", " << result_element_name
- << ");" << endl;
+ indent(out) << result_name << ".put(" << result_element_name << ", " << result_element_name << ");" << endl;
}
} else {
// iterative copy
- if (((t_base_type*)elem_type)->is_binary()) {
+ if(((t_base_type*)elem_type)->is_binary()){
indent(out) << type_name(elem_type, true, false) << " temp_binary_element = ";
- generate_deep_copy_non_container(out,
- iterator_element_name,
- "temp_binary_element",
- elem_type);
+ generate_deep_copy_non_container(out, iterator_element_name, "temp_binary_element", elem_type);
out << ";" << endl;
- if (elem_type->is_list()) {
- indent(out) << result_name << ".addElement(temp_binary_element);" << endl;
- } else {
- indent(out) << result_name << ".put(temp_binary_element, temp_binary_element);" << endl;
- }
- } else {
+ if (elem_type->is_list()) {
+ indent(out) << result_name << ".addElement(temp_binary_element);" << endl;
+ } else {
+ indent(out) << result_name << ".put(temp_binary_element, temp_binary_element);" << endl;
+ }
+ } else{
indent(out) << result_name << ".addElement(";
generate_deep_copy_non_container(out, iterator_element_name, result_name, elem_type);
out << ");" << endl;
@@ -3159,19 +3273,16 @@
indent_down();
indent(out) << "}" << endl;
+
}
}
-void t_javame_generator::generate_deep_copy_non_container(ofstream& out,
- std::string source_name,
- std::string dest_name,
- t_type* type) {
+void t_javame_generator::generate_deep_copy_non_container(ofstream& out, std::string source_name, std::string dest_name, t_type* type) {
if (type->is_base_type() || type->is_enum() || type->is_typedef()) {
// binary fields need to be copied with System.arraycopy
- if (((t_base_type*)type)->is_binary()) {
+ if (((t_base_type*)type)->is_binary()){
out << "new byte[" << source_name << ".length];" << endl;
- indent(out) << "System.arraycopy(" << source_name << ", 0, " << dest_name << ", 0, "
- << source_name << ".length)";
+ indent(out) << "System.arraycopy(" << source_name << ", 0, " << dest_name << ", 0, " << source_name << ".length)";
}
// everything else can be copied directly
else
@@ -3195,8 +3306,7 @@
void t_javame_generator::generate_isset_set(ofstream& out, t_field* field) {
if (!type_can_be_null(field->get_type())) {
- indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);"
- << endl;
+ indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);" << endl;
}
}
@@ -3210,8 +3320,8 @@
}
void t_javame_generator::generate_struct_desc(ofstream& out, t_struct* tstruct) {
- indent(out) << "private static final TStruct STRUCT_DESC = new TStruct(\"" << tstruct->get_name()
- << "\");" << endl;
+ indent(out) <<
+ "private static final TStruct STRUCT_DESC = new TStruct(\"" << tstruct->get_name() << "\");" << endl;
}
void t_javame_generator::generate_field_descs(ofstream& out, t_struct* tstruct) {
@@ -3219,10 +3329,11 @@
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- indent(out) << "private static final TField " << constant_name((*m_iter)->get_name())
- << "_FIELD_DESC = new TField(\"" << (*m_iter)->get_name() << "\", "
- << type_to_enum((*m_iter)->get_type()) << ", "
- << "(short)" << (*m_iter)->get_key() << ");" << endl;
+ indent(out) <<
+ "private static final TField " << constant_name((*m_iter)->get_name()) <<
+ "_FIELD_DESC = new TField(\"" << (*m_iter)->get_name() << "\", " <<
+ type_to_enum((*m_iter)->get_type()) << ", " <<
+ "(short)" << (*m_iter)->get_key() << ");" << endl;
}
}
@@ -3248,35 +3359,29 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL) {
- print_const_value(out,
- "this." + (*m_iter)->get_name(),
- t,
- (*m_iter)->get_value(),
- true,
- true);
+ print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
} else {
if (type_can_be_null(t)) {
indent(out) << "this." << (*m_iter)->get_name() << " = null;" << endl;
} else {
// must be a base type
// means it also needs to be explicitly unset
- indent(out) << "set" << get_cap_name((*m_iter)->get_name()) << get_cap_name("isSet")
- << "(false);" << endl;
+ indent(out) << "set" << get_cap_name((*m_iter)->get_name()) << get_cap_name("isSet") << "(false);" << endl;
switch (((t_base_type*)t)->get_base()) {
- case t_base_type::TYPE_BYTE:
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- indent(out) << "this." << (*m_iter)->get_name() << " = 0;" << endl;
- break;
- case t_base_type::TYPE_DOUBLE:
- indent(out) << "this." << (*m_iter)->get_name() << " = 0.0;" << endl;
- break;
- case t_base_type::TYPE_BOOL:
- indent(out) << "this." << (*m_iter)->get_name() << " = false;" << endl;
- break;
- default: // prevent gcc compiler warning
- break;
+ case t_base_type::TYPE_BYTE:
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ indent(out) << "this." << (*m_iter)->get_name() << " = 0;" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << "this." << (*m_iter)->get_name() << " = 0.0;" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << "this." << (*m_iter)->get_name() << " = false;" << endl;
+ break;
+ default: // prevent gcc compiler warning
+ break;
}
}
}
@@ -3287,3 +3392,4 @@
}
THRIFT_REGISTER_GENERATOR(javame, "Java ME", "")
+
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 0fdde4d..d753bbb 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -37,7 +37,7 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
#include "t_oop_generator.h"
@@ -45,40 +45,40 @@
* JS code generator.
*/
class t_js_generator : public t_oop_generator {
-public:
+ public:
t_js_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string) :
+ t_oop_generator(program) {
+ (void) option_string;
- std::map<std::string, std::string>::const_iterator iter;
+ std::map<std::string, std::string>::const_iterator iter;
- iter = parsed_options.find("node");
- gen_node_ = (iter != parsed_options.end());
+ iter = parsed_options.find("node");
+ gen_node_ = (iter != parsed_options.end());
- iter = parsed_options.find("jquery");
- gen_jquery_ = (iter != parsed_options.end());
+ iter = parsed_options.find("jquery");
+ gen_jquery_ = (iter != parsed_options.end());
- if (!gen_node_) {
- iter = parsed_options.find("ts");
- gen_ts_ = (iter != parsed_options.end());
- } else {
- gen_ts_ = false;
- }
+ if (!gen_node_) {
+ iter = parsed_options.find("ts");
+ gen_ts_ = (iter != parsed_options.end());
+ }
+ else {
+ gen_ts_ = false;
+ }
- if (gen_node_ && gen_jquery_) {
- throw "Invalid switch: [-gen js:node,jquery] options not compatible, try: [-gen js:node -gen "
- "js:jquery]";
- }
+ if (gen_node_ && gen_jquery_) {
+ throw "Invalid switch: [-gen js:node,jquery] options not compatible, try: [-gen js:node -gen js:jquery]";
+ }
- if (gen_node_) {
- out_dir_base_ = "gen-nodejs";
- } else {
- out_dir_base_ = "gen-js";
- }
+ if (gen_node_) {
+ out_dir_base_ = "gen-nodejs";
+ } else {
+ out_dir_base_ = "gen-js";
+ }
- escape_['\''] = "\\'";
+ escape_['\''] = "\\'";
}
/**
@@ -92,26 +92,24 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_recv_throw(std::string var);
std::string render_recv_return(std::string var);
std::string render_const_value(t_type* type, t_const_value* value);
+
/**
* Structs!
*/
void generate_js_struct(t_struct* tstruct, bool is_exception);
- void generate_js_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_exported = true);
+ void generate_js_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_exported=true);
void generate_js_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_js_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_js_function_helpers(t_function* tfunction);
@@ -119,48 +117,66 @@
/**
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_rest(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_processor(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_rest (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_processor (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inclass = false);
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
/**
* Helper rendering functions
@@ -168,37 +184,38 @@
std::string js_includes();
std::string render_includes();
- std::string declare_field(t_field* tfield, bool init = false, bool obj = false);
- std::string function_signature(t_function* tfunction,
- std::string prefix = "",
- bool include_callback = false);
- std::string argument_list(t_struct* tstruct, bool include_callback = false);
+ std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="", bool include_callback=false);
+ std::string argument_list(t_struct* tstruct, bool include_callback=false);
std::string type_to_enum(t_type* ttype);
std::string autogen_comment() {
- return std::string("//\n") + "// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + "//\n" + "// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
- + "//\n";
+ return
+ std::string("//\n") +
+ "// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "//\n" +
+ "// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "//\n";
}
std::vector<std::string> js_namespace_pieces(t_program* p) {
- std::string ns = p->get_namespace("js");
+ std::string ns = p->get_namespace("js");
- std::string::size_type loc;
- std::vector<std::string> pieces;
+ std::string::size_type loc;
+ std::vector<std::string> pieces;
- if (ns.size() > 0) {
- while ((loc = ns.find(".")) != std::string::npos) {
- pieces.push_back(ns.substr(0, loc));
- ns = ns.substr(loc + 1);
+ if (ns.size() > 0) {
+ while ((loc = ns.find(".")) != std::string::npos) {
+ pieces.push_back(ns.substr(0, loc));
+ ns = ns.substr(loc+1);
+ }
}
- }
- if (ns.size() > 0) {
- pieces.push_back(ns);
- }
+ if (ns.size() > 0) {
+ pieces.push_back(ns);
+ }
- return pieces;
+ return pieces;
}
std::string js_type_namespace(t_program* p) {
@@ -219,12 +236,13 @@
}
std::string js_namespace(t_program* p) {
- std::string ns = p->get_namespace("js");
- if (ns.size() > 0) {
- ns += ".";
- }
+ std::string ns = p->get_namespace("js");
+ if (ns.size() > 0) {
+ ns += ".";
+ }
- return ns;
+
+ return ns;
}
/**
@@ -239,20 +257,26 @@
* Returns the normal indentation + " " if a module was defined.
* @return string
*/
- string ts_indent() { return indent() + (!ts_module_.empty() ? " " : ""); }
+ string ts_indent() {
+ return indent() + (!ts_module_.empty() ? " " : "");
+ }
/**
* Returns "declare " if no module was defined.
* @return string
*/
- string ts_declare() { return (ts_module_.empty() ? "declare " : ""); }
+ string ts_declare() {
+ return (ts_module_.empty() ? "declare " : "");
+ }
/**
* Returns "?" if the given field is optional.
* @param t_field The field to check
* @return string
*/
- string ts_get_req(t_field* field) { return (field->get_req() == t_field::T_OPTIONAL ? "?" : ""); }
+ string ts_get_req(t_field* field) {
+ return (field->get_req() == t_field::T_OPTIONAL ? "?" : "");
+ }
/**
* Returns the documentation, if the provided documentable object has one.
@@ -275,7 +299,8 @@
return result;
}
-private:
+ private:
+
/**
* True if we should generate NodeJS-friendly RPC services.
*/
@@ -305,6 +330,7 @@
std::ofstream f_service_ts_;
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -318,7 +344,7 @@
string outdir = get_out_dir();
// Make output file(s)
- string f_types_name = outdir + program_->get_name() + "_types.js";
+ string f_types_name = outdir+program_->get_name()+"_types.js";
f_types_.open(f_types_name.c_str());
if (gen_ts_) {
@@ -327,7 +353,10 @@
}
// Print header
- f_types_ << autogen_comment() << js_includes() << endl << render_includes() << endl;
+ f_types_ <<
+ autogen_comment() <<
+ js_includes() << endl <<
+ render_includes() << endl;
if (gen_ts_) {
f_types_ts_ << autogen_comment() << endl;
@@ -339,21 +368,22 @@
string pns;
- // setup the namespace
+ //setup the namespace
// TODO should the namespace just be in the directory structure for node?
- vector<string> ns_pieces = js_namespace_pieces(program_);
- if (ns_pieces.size() > 0) {
- for (size_t i = 0; i < ns_pieces.size(); ++i) {
+ vector<string> ns_pieces = js_namespace_pieces( program_ );
+ if( ns_pieces.size() > 0){
+ for(size_t i = 0; i < ns_pieces.size(); ++i) {
pns += ((i == 0) ? "" : ".") + ns_pieces[i];
f_types_ << "if (typeof " << pns << " === 'undefined') {" << endl;
- f_types_ << " " << pns << " = {};" << endl;
- f_types_ << "}" << endl;
+ f_types_ << " " << pns << " = {};" << endl;
+ f_types_ << "}" << endl;
}
if (gen_ts_) {
ts_module_ = pns;
f_types_ts_ << "declare module " << ts_module_ << " {";
}
}
+
}
/**
@@ -361,10 +391,9 @@
*/
string t_js_generator::js_includes() {
if (gen_node_) {
- return string(
- "var thrift = require('thrift');\n"
- "var Thrift = thrift.Thrift;\n"
- "var Q = thrift.Q;\n");
+ return string("var thrift = require('thrift');\n"
+ "var Thrift = thrift.Thrift;\n"
+ "var Q = thrift.Q;\n");
}
return "";
@@ -379,8 +408,7 @@
if (gen_node_) {
const vector<t_program*>& includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
- result += "var " + includes[i]->get_name() + "_ttypes = require('./" + includes[i]->get_name()
- + "_types')\n";
+ result += "var " + includes[i]->get_name() + "_ttypes = require('./" + includes[i]->get_name() + "_types')\n";
}
if (includes.size() > 0) {
result += "\n";
@@ -412,7 +440,7 @@
* @param ttypedef The type definition
*/
void t_js_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -422,11 +450,12 @@
* @param tenum The enumeration
*/
void t_js_generator::generate_enum(t_enum* tenum) {
- f_types_ << js_type_namespace(tenum->get_program()) << tenum->get_name() << " = {" << endl;
+ f_types_ << js_type_namespace(tenum->get_program())<<tenum->get_name()<<" = {"<<endl;
if (gen_ts_) {
- f_types_ts_ << ts_print_doc(tenum) << ts_indent() << ts_declare() << "enum "
- << tenum->get_name() << " {" << endl;
+ f_types_ts_ <<
+ ts_print_doc(tenum) <<
+ ts_indent() << ts_declare() << "enum " << tenum->get_name() << " {" << endl;
}
indent_up();
@@ -437,19 +466,19 @@
int value = (*c_iter)->get_value();
if (gen_ts_) {
f_types_ts_ << ts_indent() << "'" << (*c_iter)->get_name() << "' = " << value << "," << endl;
- // add 'value: key' in addition to 'key: value' for TypeScript enums
+ //add 'value: key' in addition to 'key: value' for TypeScript enums
f_types_ << indent() << "'" << value << "' : '" << (*c_iter)->get_name() << "'," << endl;
}
f_types_ << indent() << "'" << (*c_iter)->get_name() << "' : " << value;
- if (c_iter != constants.end() - 1) {
- f_types_ << ",";
+ if (c_iter != constants.end()-1) {
+ f_types_ << ",";
}
f_types_ << endl;
}
indent_down();
- f_types_ << "};" << endl;
+ f_types_ << "};"<<endl;
if (gen_ts_) {
f_types_ts_ << ts_indent() << "}" << endl;
@@ -464,12 +493,13 @@
string name = tconst->get_name();
t_const_value* value = tconst->get_value();
- f_types_ << js_type_namespace(program_) << name << " = ";
+ f_types_ << js_type_namespace(program_) << name << " = ";
f_types_ << render_const_value(type, value) << ";" << endl;
if (gen_ts_) {
- f_types_ts_ << ts_print_doc(tconst) << ts_indent() << ts_declare() << "var " << name << ": "
- << ts_get_type(type) << ";" << endl;
+ f_types_ts_ <<
+ ts_print_doc(tconst) <<
+ ts_indent() << ts_declare() << "var " << name << ": " << ts_get_type(type) << ";" << endl;
}
}
@@ -544,13 +574,13 @@
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
- if (v_iter != val.begin())
- out << "," << endl;
+ if (v_iter != val.begin())
+ out << "," << endl;
- out << render_const_value(ktype, v_iter->first);
+ out << render_const_value(ktype, v_iter->first);
- out << " : ";
- out << render_const_value(vtype, v_iter->second);
+ out << " : ";
+ out << render_const_value(vtype, v_iter->second);
}
out << endl << "}";
@@ -594,7 +624,8 @@
/**
* Structs can be normal or exceptions.
*/
-void t_js_generator::generate_js_struct(t_struct* tstruct, bool is_exception) {
+void t_js_generator::generate_js_struct(t_struct* tstruct,
+ bool is_exception) {
generate_js_struct_definition(f_types_, tstruct, is_exception);
}
@@ -606,52 +637,49 @@
* @param tstruct The struct definition
*/
void t_js_generator::generate_js_struct_definition(ofstream& out,
- t_struct* tstruct,
- bool is_exception,
- bool is_exported) {
+ t_struct* tstruct,
+ bool is_exception,
+ bool is_exported) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
if (gen_node_) {
if (is_exported) {
- out << js_namespace(tstruct->get_program()) << tstruct->get_name() << " = "
- << "module.exports." << tstruct->get_name() << " = function(args) {" << endl;
+ out << js_namespace(tstruct->get_program()) << tstruct->get_name() << " = " <<
+ "module.exports." << tstruct->get_name() << " = function(args) {" << endl;
} else {
- out << js_namespace(tstruct->get_program()) << tstruct->get_name() << " = function(args) {"
- << endl;
+ out << js_namespace(tstruct->get_program()) << tstruct->get_name() << " = function(args) {" << endl;
}
} else {
- out << js_namespace(tstruct->get_program()) << tstruct->get_name() << " = function(args) {"
- << endl;
+ out << js_namespace(tstruct->get_program()) << tstruct->get_name() <<" = function(args) {" << endl;
if (gen_ts_) {
- f_types_ts_ << ts_print_doc(tstruct) << ts_indent() << ts_declare() << "class "
- << tstruct->get_name() << (is_exception ? " extends Thrift.TException" : "")
- << " {" << endl;
+ f_types_ts_ <<
+ ts_print_doc(tstruct) <<
+ ts_indent() << ts_declare() << "class " << tstruct->get_name() << (is_exception ? " extends Thrift.TException" : "" ) << " {" << endl;
}
}
indent_up();
if (gen_node_ && is_exception) {
- out << indent() << "Thrift.TException.call(this, \"" << js_namespace(tstruct->get_program())
- << tstruct->get_name() << "\")" << endl;
- out << indent() << "this.name = \"" << js_namespace(tstruct->get_program())
- << tstruct->get_name() << "\"" << endl;
+ out << indent() << "Thrift.TException.call(this, \"" <<
+ js_namespace(tstruct->get_program()) << tstruct->get_name() << "\")" << endl;
+ out << indent() << "this.name = \"" <<
+ js_namespace(tstruct->get_program()) << tstruct->get_name() << "\"" << endl;
}
- // members with arguments
+ //members with arguments
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- string dval = declare_field(*m_iter, false, true);
+ string dval = declare_field(*m_iter,false,true);
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL && !(t->is_struct() || t->is_xception())) {
- dval = render_const_value((*m_iter)->get_type(), (*m_iter)->get_value());
- out << indent() << "this." << (*m_iter)->get_name() << " = " << dval << ";" << endl;
+ dval = render_const_value((*m_iter)->get_type(), (*m_iter)->get_value());
+ out << indent() << "this." << (*m_iter)->get_name() << " = " << dval << ";" << endl;
} else {
- out << indent() << dval << ";" << endl;
+ out << indent() << dval << ";" << endl;
}
if (gen_ts_) {
- f_types_ts_ << ts_indent() << (*m_iter)->get_name() << ": "
- << ts_get_type((*m_iter)->get_type()) << ";" << endl;
+ f_types_ts_ << ts_indent() << (*m_iter)->get_name() << ": " << ts_get_type((*m_iter)->get_type()) << ";" << endl;
}
}
@@ -661,47 +689,44 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
- indent(out) << "this." << (*m_iter)->get_name() << " = "
- << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
+ indent(out) << "this." << (*m_iter)->get_name() << " = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
}
}
// Early returns for exceptions
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- if (t->is_xception()) {
- out << indent() << "if (args instanceof " << js_type_namespace(t->get_program())
- << t->get_name() << ") {" << endl << indent() << indent() << "this."
- << (*m_iter)->get_name() << " = args;" << endl << indent() << indent() << "return;"
- << endl << indent() << "}" << endl;
- }
+ t_type* t = get_true_type((*m_iter)->get_type());
+ if (t->is_xception()) {
+ out << indent() << "if (args instanceof " << js_type_namespace(t->get_program()) << t->get_name() << ") {" << endl
+ << indent() << indent() << "this." << (*m_iter)->get_name() << " = args;" << endl
+ << indent() << indent() << "return;" << endl
+ << indent() << "}" << endl;
+ }
}
- out << indent() << "if (args) {" << endl;
+ out << indent() << "if (args) {" << endl;
if (gen_ts_) {
f_types_ts_ << endl << ts_indent() << "constructor(args?: { ";
}
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- out << indent() << indent() << "if (args." << (*m_iter)->get_name() << " !== undefined) {"
- << endl << indent() << indent() << indent() << "this." << (*m_iter)->get_name()
- << " = args." << (*m_iter)->get_name() << ";" << endl;
- if (!(*m_iter)->get_req()) {
- out << indent() << indent() << "} else {" << endl << indent() << indent() << indent()
- << "throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.UNKNOWN, "
- "'Required field " << (*m_iter)->get_name() << " is unset!');" << endl;
- }
- out << indent() << indent() << "}" << endl;
- if (gen_ts_) {
- f_types_ts_ << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": "
- << ts_get_type((*m_iter)->get_type()) << "; ";
- }
+ out << indent() << indent() << "if (args." << (*m_iter)->get_name() << " !== undefined) {" << endl
+ << indent() << indent() << indent() << "this." << (*m_iter)->get_name() << " = args." << (*m_iter)->get_name() << ";" << endl;
+ if (!(*m_iter)->get_req()) {
+ out << indent() << indent() << "} else {" << endl
+ << indent() << indent() << indent() << "throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.UNKNOWN, 'Required field " << (*m_iter)->get_name() << " is unset!');" << endl;
+ }
+ out << indent() << indent() << "}" << endl;
+ if (gen_ts_) {
+ f_types_ts_ << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": " << ts_get_type((*m_iter)->get_type()) << "; ";
+ }
}
- out << indent() << "}" << endl;
+ out << indent() << "}" << endl;
if (gen_ts_) {
f_types_ts_ << "});" << endl;
}
+
}
indent_down();
@@ -711,34 +736,36 @@
}
if (is_exception) {
- out << "Thrift.inherits(" << js_namespace(tstruct->get_program()) << tstruct->get_name()
- << ", Thrift.TException);" << endl;
- out << js_namespace(tstruct->get_program()) << tstruct->get_name() << ".prototype.name = '"
- << tstruct->get_name() << "';" << endl;
+ out << "Thrift.inherits(" <<
+ js_namespace(tstruct->get_program()) <<
+ tstruct->get_name() << ", Thrift.TException);" << endl;
+ out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype.name = '" << tstruct->get_name() << "';" << endl;
} else {
- // init prototype
- out << js_namespace(tstruct->get_program()) << tstruct->get_name() << ".prototype = {};"
- << endl;
+ //init prototype
+ out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {};" << endl;
}
+
generate_js_struct_reader(out, tstruct);
generate_js_struct_writer(out, tstruct);
+
}
/**
* Generates the read() method for a struct
*/
-void t_js_generator::generate_js_struct_reader(ofstream& out, t_struct* tstruct) {
+void t_js_generator::generate_js_struct_reader(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out << js_namespace(tstruct->get_program()) << tstruct->get_name()
- << ".prototype.read = function(input) {" << endl;
+ out << js_namespace(tstruct->get_program())<<tstruct->get_name() << ".prototype.read = function(input) {"<<endl;
indent_up();
indent(out) << "input.readStructBegin();" << endl;
+
// Loop over reading in fields
indent(out) << "while (true)" << endl;
@@ -747,7 +774,8 @@
indent(out) << "var ret = input.readFieldBegin();" << endl;
indent(out) << "var fname = ret.fname;" << endl;
indent(out) << "var ftype = ret.ftype;" << endl;
- indent(out) << "var fid = ret.fid;" << endl;
+ indent(out) << "var fid = ret.fid;" << endl;
+
// Check for field STOP marker and break
indent(out) << "if (ftype == Thrift.Type.STOP) {" << endl;
@@ -773,19 +801,22 @@
indent(out) << "} else {" << endl;
- indent(out) << " input.skip(ftype);" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
- out << indent() << "}" << endl << indent() << "break;" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+
}
if (fields.size() == 1) {
// pseudo case to make jslint happy
- indent(out) << "case 0:" << endl;
- indent(out) << " input.skip(ftype);" << endl;
- indent(out) << " break;" << endl;
+ indent(out) << "case 0:" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
+ indent(out) << " break;" << endl;
}
// In the default case we skip the field
- indent(out) << "default:" << endl;
- indent(out) << " input.skip(ftype);" << endl;
+ indent(out) << "default:" << endl;
+ indent(out) << " input.skip(ftype);" << endl;
scope_down(out);
} else {
@@ -807,43 +838,50 @@
/**
* Generates the write() method for a struct
*/
-void t_js_generator::generate_js_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_js_generator::generate_js_struct_writer(ofstream& out,
+ t_struct* tstruct) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out << js_namespace(tstruct->get_program()) << tstruct->get_name()
- << ".prototype.write = function(output) {" << endl;
+ out << js_namespace(tstruct->get_program())<< tstruct->get_name() << ".prototype.write = function(output) {"<<endl;
indent_up();
indent(out) << "output.writeStructBegin('" << name << "');" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- out << indent() << "if (this." << (*f_iter)->get_name() << " !== null && this."
- << (*f_iter)->get_name() << " !== undefined) {" << endl;
+ out << indent() << "if (this." << (*f_iter)->get_name() << " !== null && this." << (*f_iter)->get_name() << " !== undefined) {" << endl;
indent_up();
- indent(out) << "output.writeFieldBegin("
- << "'" << (*f_iter)->get_name() << "', " << type_to_enum((*f_iter)->get_type())
- << ", " << (*f_iter)->get_key() << ");" << endl;
+ indent(out) <<
+ "output.writeFieldBegin(" <<
+ "'" << (*f_iter)->get_name() << "', " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ");" << endl;
+
// Write field contents
generate_serialize_field(out, *f_iter, "this.");
- indent(out) << "output.writeFieldEnd();" << endl;
+ indent(out) <<
+ "output.writeFieldEnd();" << endl;
indent_down();
indent(out) << "}" << endl;
}
- out << indent() << "output.writeFieldStop();" << endl << indent() << "output.writeStructEnd();"
- << endl;
- out << indent() << "return;" << endl;
+ out <<
+ indent() << "output.writeFieldStop();" << endl <<
+ indent() << "output.writeStructEnd();" << endl;
+
+ out <<indent() << "return;" << endl;
indent_down();
- out << indent() << "};" << endl << endl;
+ out <<
+ indent() << "};" << endl <<
+ endl;
}
/**
@@ -852,55 +890,59 @@
* @param tservice The service definition
*/
void t_js_generator::generate_service(t_service* tservice) {
- string f_service_name = get_out_dir() + service_name_ + ".js";
- f_service_.open(f_service_name.c_str());
+ string f_service_name = get_out_dir()+service_name_+".js";
+ f_service_.open(f_service_name.c_str());
- if (gen_ts_) {
- string f_service_ts_name = get_out_dir() + service_name_ + ".d.ts";
- f_service_ts_.open(f_service_ts_name.c_str());
- }
-
- f_service_ << autogen_comment() << js_includes() << endl << render_includes() << endl;
-
- if (gen_ts_) {
- if (tservice->get_extends() != NULL) {
- f_service_ts_ << "/// <reference path=\"" << tservice->get_extends()->get_name()
- << ".d.ts\" />" << endl;
- }
- f_service_ts_ << autogen_comment() << endl;
- if (!ts_module_.empty()) {
- f_service_ts_ << "declare module " << ts_module_ << " {";
- }
- }
-
- if (gen_node_) {
- if (tservice->get_extends() != NULL) {
- f_service_ << "var " << tservice->get_extends()->get_name() << " = require('./"
- << tservice->get_extends()->get_name() << "')" << endl << "var "
- << tservice->get_extends()->get_name()
- << "Client = " << tservice->get_extends()->get_name() << ".Client" << endl
- << "var " << tservice->get_extends()->get_name()
- << "Processor = " << tservice->get_extends()->get_name() << ".Processor" << endl;
+ if (gen_ts_) {
+ string f_service_ts_name = get_out_dir() + service_name_ + ".d.ts";
+ f_service_ts_.open(f_service_ts_name.c_str());
}
- f_service_ << "var ttypes = require('./" + program_->get_name() + "_types');" << endl;
- }
+ f_service_ <<
+ autogen_comment() <<
+ js_includes() << endl <<
+ render_includes() << endl;
- generate_service_helpers(tservice);
- generate_service_interface(tservice);
- generate_service_client(tservice);
-
- if (gen_node_) {
- generate_service_processor(tservice);
- }
-
- f_service_.close();
- if (gen_ts_) {
- if (!ts_module_.empty()) {
- f_service_ts_ << "}";
+ if (gen_ts_) {
+ if (tservice->get_extends() != NULL) {
+ f_service_ts_ << "/// <reference path=\"" << tservice->get_extends()->get_name() << ".d.ts\" />" << endl;
+ }
+ f_service_ts_ << autogen_comment() << endl;
+ if (!ts_module_.empty()) {
+ f_service_ts_ << "declare module " << ts_module_ << " {";
+ }
}
- f_service_ts_.close();
- }
+
+ if (gen_node_) {
+ if (tservice->get_extends() != NULL) {
+ f_service_ <<
+ "var " << tservice->get_extends()->get_name() <<
+ " = require('./" << tservice->get_extends()->get_name() << "')" << endl <<
+ "var " << tservice->get_extends()->get_name() << "Client = " <<
+ tservice->get_extends()->get_name() << ".Client" << endl <<
+ "var " << tservice->get_extends()->get_name() << "Processor = " <<
+ tservice->get_extends()->get_name() << ".Processor" << endl;
+ }
+
+ f_service_ <<
+ "var ttypes = require('./" + program_->get_name() + "_types');" << endl;
+ }
+
+ generate_service_helpers(tservice);
+ generate_service_interface(tservice);
+ generate_service_client(tservice);
+
+ if (gen_node_) {
+ generate_service_processor(tservice);
+ }
+
+ f_service_.close();
+ if (gen_ts_) {
+ if (!ts_module_.empty()) {
+ f_service_ts_ << "}";
+ }
+ f_service_ts_.close();
+ }
}
/**
@@ -909,50 +951,52 @@
* @param tservice The service to generate a server for.
*/
void t_js_generator::generate_service_processor(t_service* tservice) {
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
- f_service_ << js_namespace(tservice->get_program()) << service_name_ << "Processor = "
- << "exports.Processor = function(handler) ";
+ f_service_ <<
+ js_namespace(tservice->get_program()) << service_name_ << "Processor = " <<
+ "exports.Processor = function(handler) ";
- scope_up(f_service_);
+ scope_up(f_service_);
- f_service_ << indent() << "this._handler = handler" << endl;
+ f_service_ << indent() << "this._handler = handler" << endl;
- scope_down(f_service_);
+ scope_down(f_service_);
- if (tservice->get_extends() != NULL) {
- indent(f_service_) << "Thrift.inherits(" << js_namespace(tservice->get_program())
- << service_name_ << "Processor, " << tservice->get_extends()->get_name()
- << "Processor)" << endl;
- }
+ if (tservice->get_extends() != NULL) {
+ indent(f_service_) << "Thrift.inherits(" <<
+ js_namespace(tservice->get_program()) <<
+ service_name_ << "Processor, " <<
+ tservice->get_extends()->get_name() << "Processor)" << endl;
+ }
- // Generate the server implementation
- indent(f_service_) << js_namespace(tservice->get_program()) << service_name_
- << "Processor.prototype.process = function(input, output) ";
+ // Generate the server implementation
+ indent(f_service_) <<
+ js_namespace(tservice->get_program()) << service_name_ << "Processor.prototype.process = function(input, output) ";
- scope_up(f_service_);
+ scope_up(f_service_);
- f_service_ << indent() << "var r = input.readMessageBegin();" << endl << indent()
- << "if (this['process_' + r.fname]) {" << endl << indent()
- << " return this['process_' + r.fname].call(this, r.rseqid, input, output);" << endl
- << indent() << "} else {" << endl << indent() << " input.skip(Thrift.Type.STRUCT);"
- << endl << indent() << " input.readMessageEnd();" << endl << indent()
- << " var x = new "
- "Thrift.TApplicationException(Thrift.TApplicationExceptionType.UNKNOWN_METHOD, "
- "'Unknown function ' + r.fname);" << endl << indent()
- << " output.writeMessageBegin(r.fname, Thrift.MessageType.EXCEPTION, r.rseqid);"
- << endl << indent() << " x.write(output);" << endl << indent()
- << " output.writeMessageEnd();" << endl << indent() << " output.flush();" << endl
- << indent() << "}" << endl;
+ f_service_ << indent() << "var r = input.readMessageBegin();" << endl
+ << indent() << "if (this['process_' + r.fname]) {" << endl
+ << indent() << " return this['process_' + r.fname].call(this, r.rseqid, input, output);" << endl
+ << indent() << "} else {" << endl
+ << indent() << " input.skip(Thrift.Type.STRUCT);" << endl
+ << indent() << " input.readMessageEnd();" << endl
+ << indent() << " var x = new Thrift.TApplicationException(Thrift.TApplicationExceptionType.UNKNOWN_METHOD, 'Unknown function ' + r.fname);" << endl
+ << indent() << " output.writeMessageBegin(r.fname, Thrift.MessageType.EXCEPTION, r.rseqid);" << endl
+ << indent() << " x.write(output);" << endl
+ << indent() << " output.writeMessageEnd();" << endl
+ << indent() << " output.flush();" << endl
+ << indent() << "}" << endl;
- scope_down(f_service_);
- f_service_ << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
- // Generate the process subfunctions
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- generate_process_function(tservice, *f_iter);
- }
+ // Generate the process subfunctions
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_process_function(tservice, *f_iter);
+ }
}
/**
@@ -960,99 +1004,105 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_js_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- indent(f_service_) << js_namespace(tservice->get_program()) << service_name_
- << "Processor.prototype.process_" + tfunction->get_name()
- + " = function(seqid, input, output) ";
+void t_js_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ indent(f_service_) <<
+ js_namespace(tservice->get_program()) << service_name_ << "Processor.prototype.process_" + tfunction->get_name() + " = function(seqid, input, output) ";
- scope_up(f_service_);
+ scope_up(f_service_);
- string argsname = js_namespace(program_) + service_name_ + "_" + tfunction->get_name() + "_args";
- string resultname = js_namespace(program_) + service_name_ + "_" + tfunction->get_name()
- + "_result";
+ string argsname = js_namespace(program_)+ service_name_ + "_" + tfunction->get_name() + "_args";
+ string resultname = js_namespace(program_)+ service_name_ + "_" + tfunction->get_name() + "_result";
- f_service_ << indent() << "var args = new " << argsname << "();" << endl << indent()
- << "args.read(input);" << endl << indent() << "input.readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "var args = new " << argsname << "();" << endl <<
+ indent() << "args.read(input);" << endl <<
+ indent() << "input.readMessageEnd();" << endl;
- // Generate the function call
- t_struct* arg_struct = tfunction->get_arglist();
- const std::vector<t_field*>& fields = arg_struct->get_members();
- vector<t_field*>::const_iterator f_iter;
+ // Generate the function call
+ t_struct* arg_struct = tfunction->get_arglist();
+ const std::vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator f_iter;
- // Shortcut out here for oneway functions
- if (tfunction->is_oneway()) {
- indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
+ // Shortcut out here for oneway functions
+ if (tfunction->is_oneway()) {
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
- bool first = true;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
+ bool first = true;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "args." << (*f_iter)->get_name();
}
- f_service_ << "args." << (*f_iter)->get_name();
+
+ f_service_ << ")" << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
+ return;
+ }
+
+ f_service_ <<
+ indent() << "if (this._handler." << tfunction->get_name() << ".length === " << fields.size() <<") {" << endl;
+ indent_up();
+ indent(f_service_) << "Q.fcall(this._handler." << tfunction->get_name();
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << ", args." << (*f_iter)->get_name();
}
f_service_ << ")" << endl;
+ indent_up();
+ indent(f_service_) << ".then(function(result) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "({success: result});" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "}, function (err) {" << endl;
+ indent_up();
+ f_service_ <<
+ indent() << "var result = new " << resultname << "(err);" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+ indent_down();
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent_down();
+ indent(f_service_) << "} else {" << endl;
+ indent_up();
+ indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ f_service_ << "args." << (*f_iter)->get_name() << ", ";
+ }
+
+ f_service_ << " function (err, result) {" << endl;
+ indent_up();
+
+ f_service_ <<
+ indent() << "var result = new " << resultname << "((err != null ? err : {success: result}));" << endl <<
+ indent() << "output.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", Thrift.MessageType.REPLY, seqid);" << endl <<
+ indent() << "result.write(output);" << endl <<
+ indent() << "output.writeMessageEnd();" << endl <<
+ indent() << "output.flush();" << endl;
+
+ indent_down();
+ indent(f_service_) << "});" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
scope_down(f_service_);
f_service_ << endl;
- return;
- }
-
- f_service_ << indent() << "if (this._handler." << tfunction->get_name()
- << ".length === " << fields.size() << ") {" << endl;
- indent_up();
- indent(f_service_) << "Q.fcall(this._handler." << tfunction->get_name();
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- f_service_ << ", args." << (*f_iter)->get_name();
- }
-
- f_service_ << ")" << endl;
- indent_up();
- indent(f_service_) << ".then(function(result) {" << endl;
- indent_up();
- f_service_ << indent() << "var result = new " << resultname << "({success: result});" << endl
- << indent() << "output.writeMessageBegin(\"" << tfunction->get_name()
- << "\", Thrift.MessageType.REPLY, seqid);" << endl << indent()
- << "result.write(output);" << endl << indent() << "output.writeMessageEnd();" << endl
- << indent() << "output.flush();" << endl;
- indent_down();
- indent(f_service_) << "}, function (err) {" << endl;
- indent_up();
- f_service_ << indent() << "var result = new " << resultname << "(err);" << endl << indent()
- << "output.writeMessageBegin(\"" << tfunction->get_name()
- << "\", Thrift.MessageType.REPLY, seqid);" << endl << indent()
- << "result.write(output);" << endl << indent() << "output.writeMessageEnd();" << endl
- << indent() << "output.flush();" << endl;
- indent_down();
- indent(f_service_) << "});" << endl;
- indent_down();
- indent_down();
- indent(f_service_) << "} else {" << endl;
- indent_up();
- indent(f_service_) << "this._handler." << tfunction->get_name() << "(";
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- f_service_ << "args." << (*f_iter)->get_name() << ", ";
- }
-
- f_service_ << " function (err, result) {" << endl;
- indent_up();
-
- f_service_ << indent() << "var result = new " << resultname
- << "((err != null ? err : {success: result}));" << endl << indent()
- << "output.writeMessageBegin(\"" << tfunction->get_name()
- << "\", Thrift.MessageType.REPLY, seqid);" << endl << indent()
- << "result.write(output);" << endl << indent() << "output.writeMessageEnd();" << endl
- << indent() << "output.flush();" << endl;
-
- indent_down();
- indent(f_service_) << "});" << endl;
- indent_down();
- indent(f_service_) << "}" << endl;
- scope_down(f_service_);
- f_service_ << endl;
}
/**
@@ -1061,25 +1111,26 @@
* @param tservice The service to generate a header definition for
*/
void t_js_generator::generate_service_helpers(t_service* tservice) {
- // Do not generate TS definitions for helper functions
- bool gen_ts_tmp = gen_ts_;
- gen_ts_ = false;
+ //Do not generate TS definitions for helper functions
+ bool gen_ts_tmp = gen_ts_;
+ gen_ts_ = false;
- vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
- f_service_ << "//HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
+ f_service_ <<
+ "//HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
- for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- t_struct* ts = (*f_iter)->get_arglist();
- string name = ts->get_name();
- ts->set_name(service_name_ + "_" + name);
- generate_js_struct_definition(f_service_, ts, false, false);
- generate_js_function_helpers(*f_iter);
- ts->set_name(name);
- }
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_struct* ts = (*f_iter)->get_arglist();
+ string name = ts->get_name();
+ ts->set_name(service_name_ + "_" + name);
+ generate_js_struct_definition(f_service_, ts, false, false);
+ generate_js_function_helpers(*f_iter);
+ ts->set_name(name);
+ }
- gen_ts_ = gen_ts_tmp;
+ gen_ts_ = gen_ts_tmp;
}
/**
@@ -1088,20 +1139,20 @@
* @param tfunction The function
*/
void t_js_generator::generate_js_function_helpers(t_function* tfunction) {
- t_struct result(program_, service_name_ + "_" + tfunction->get_name() + "_result");
- t_field success(tfunction->get_returntype(), "success", 0);
- if (!tfunction->get_returntype()->is_void()) {
- result.append(&success);
- }
+ t_struct result(program_, service_name_ + "_" + tfunction->get_name() + "_result");
+ t_field success(tfunction->get_returntype(), "success", 0);
+ if (!tfunction->get_returntype()->is_void()) {
+ result.append(&success);
+ }
- t_struct* xs = tfunction->get_xceptions();
- const vector<t_field*>& fields = xs->get_members();
- vector<t_field*>::const_iterator f_iter;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- result.append(*f_iter);
- }
+ t_struct* xs = tfunction->get_xceptions();
+ const vector<t_field*>& fields = xs->get_members();
+ vector<t_field*>::const_iterator f_iter;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ result.append(*f_iter);
+ }
- generate_js_struct_definition(f_service_, &result, false, false);
+ generate_js_struct_definition(f_service_, &result, false, false);
}
/**
@@ -1110,14 +1161,14 @@
* @param tservice The service to generate a header definition for
*/
void t_js_generator::generate_service_interface(t_service* tservice) {
- (void)tservice;
+ (void) tservice;
}
/**
* Generates a REST interface
*/
void t_js_generator::generate_service_rest(t_service* tservice) {
- (void)tservice;
+ (void) tservice;
}
/**
@@ -1127,14 +1178,16 @@
*/
void t_js_generator::generate_service_client(t_service* tservice) {
if (gen_node_) {
- f_service_ << js_namespace(tservice->get_program()) << service_name_ << "Client = "
- << "exports.Client = function(output, pClass) {" << endl;
+ f_service_ <<
+ js_namespace(tservice->get_program()) << service_name_ << "Client = " <<
+ "exports.Client = function(output, pClass) {"<<endl;
} else {
- f_service_ << js_namespace(tservice->get_program()) << service_name_
- << "Client = function(input, output) {" << endl;
+ f_service_ <<
+ js_namespace(tservice->get_program()) << service_name_ << "Client = function(input, output) {"<<endl;
if (gen_ts_) {
- f_service_ts_ << ts_print_doc(tservice) << ts_indent() << ts_declare() << "class "
- << service_name_ << "Client ";
+ f_service_ts_ <<
+ ts_print_doc(tservice) <<
+ ts_indent() << ts_declare() << "class " << service_name_ << "Client ";
if (tservice->get_extends() != NULL) {
f_service_ts_ << "extends " << tservice->get_extends()->get_name() << "Client ";
}
@@ -1144,45 +1197,50 @@
indent_up();
+
if (gen_node_) {
- f_service_ << indent() << " this.output = output;" << endl << indent()
- << " this.pClass = pClass;" << endl << indent() << " this._seqid = 0;" << endl
- << indent() << " this._reqs = {};" << endl;
+ f_service_ <<
+ indent() << " this.output = output;" << endl <<
+ indent() << " this.pClass = pClass;" << endl <<
+ indent() << " this._seqid = 0;" << endl <<
+ indent() << " this._reqs = {};" << endl;
} else {
- f_service_ << indent() << " this.input = input;" << endl << indent()
- << " this.output = (!output) ? input : output;" << endl << indent()
- << " this.seqid = 0;" << endl;
+ f_service_ <<
+ indent() << " this.input = input;" << endl <<
+ indent() << " this.output = (!output) ? input : output;" << endl <<
+ indent() << " this.seqid = 0;" << endl;
if (gen_ts_) {
- f_service_ts_ << ts_indent() << "input: Thrift.TJSONProtocol;" << endl << ts_indent()
- << "output: Thrift.TJSONProtocol;" << endl << ts_indent() << "seqid: number;"
- << endl << endl << ts_indent()
- << "constructor(input: Thrift.TJSONProtocol, output?: Thrift.TJSONProtocol);"
- << endl;
+ f_service_ts_ <<
+ ts_indent() << "input: Thrift.TJSONProtocol;" << endl <<
+ ts_indent() << "output: Thrift.TJSONProtocol;" << endl <<
+ ts_indent() << "seqid: number;" << endl <<
+ endl <<
+ ts_indent() << "constructor(input: Thrift.TJSONProtocol, output?: Thrift.TJSONProtocol);" << endl;
}
}
+
indent_down();
- f_service_ << indent() << "};" << endl;
+ f_service_ <<
+ indent() << "};" << endl;
+
if (tservice->get_extends() != NULL) {
- indent(f_service_) << "Thrift.inherits(" << js_namespace(tservice->get_program())
- << service_name_ << "Client, "
- << js_namespace(tservice->get_extends()->get_program())
- << tservice->get_extends()->get_name() << "Client);" << endl;
+ indent(f_service_) << "Thrift.inherits(" <<
+ js_namespace(tservice->get_program()) <<
+ service_name_ << "Client, " <<
+ js_namespace(tservice->get_extends()->get_program()) <<
+ tservice->get_extends()->get_name() << "Client);" << endl;
} else {
- // init prototype
- indent(f_service_) << js_namespace(tservice->get_program()) << service_name_
- << "Client.prototype = {};" << endl;
+ //init prototype
+ indent(f_service_) << js_namespace(tservice->get_program())<<service_name_ << "Client.prototype = {};"<<endl;
}
// utils for multiplexed services
if (gen_node_) {
- indent(f_service_) << js_namespace(tservice->get_program()) << service_name_
- << "Client.prototype.seqid = function() { return this._seqid; }" << endl
- << js_namespace(tservice->get_program()) << service_name_
- << "Client.prototype.new_seqid = function() { return this._seqid += 1; }"
- << endl;
+ indent(f_service_) << js_namespace(tservice->get_program())<<service_name_ << "Client.prototype.seqid = function() { return this._seqid; }" << endl <<
+ js_namespace(tservice->get_program())<<service_name_ << "Client.prototype.new_seqid = function() { return this._seqid += 1; }" << endl;
}
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
@@ -1195,25 +1253,29 @@
string arglist = argument_list(arg_struct);
// Open function
- f_service_ << js_namespace(tservice->get_program()) << service_name_ << "Client.prototype."
- << function_signature(*f_iter, "", true) << " {" << endl;
+ f_service_ << js_namespace(tservice->get_program())<<service_name_<<"Client.prototype." <<
+ function_signature(*f_iter, "", true) << " {" << endl;
indent_up();
if (gen_ts_) {
- f_service_ts_ << ts_print_doc(*f_iter) <<
- // function definition without callback
- ts_indent() << ts_function_signature(*f_iter, false) << endl << ts_print_doc(*f_iter) <<
- // overload with callback
- ts_indent() << ts_function_signature(*f_iter, true) << endl;
+ f_service_ts_ <<
+ ts_print_doc(*f_iter) <<
+ //function definition without callback
+ ts_indent() << ts_function_signature(*f_iter, false) << endl <<
+ ts_print_doc(*f_iter) <<
+ //overload with callback
+ ts_indent() << ts_function_signature(*f_iter, true) << endl;
}
- if (gen_node_) { // Node.js output ./gen-nodejs
- f_service_ << indent() << "this._seqid = this.new_seqid();" << endl << indent()
- << "if (callback === undefined) {" << endl;
+ if (gen_node_) { //Node.js output ./gen-nodejs
+ f_service_ <<
+ indent() << "this._seqid = this.new_seqid();" << endl <<
+ indent() << "if (callback === undefined) {" << endl;
indent_up();
- f_service_ << indent() << "var _defer = Q.defer();" << endl << indent()
- << "this._reqs[this.seqid()] = function(error, result) {" << endl;
+ f_service_ <<
+ indent() << "var _defer = Q.defer();" << endl <<
+ indent() << "this._reqs[this.seqid()] = function(error, result) {" << endl;
indent_up();
indent(f_service_) << "if (error) {" << endl;
indent_up();
@@ -1226,17 +1288,20 @@
indent(f_service_) << "}" << endl;
indent_down();
indent(f_service_) << "};" << endl;
- f_service_ << indent() << "this.send_" << funname << "(" << arglist << ");" << endl
- << indent() << "return _defer.promise;" << endl;
+ f_service_ <<
+ indent() << "this.send_" << funname << "(" << arglist << ");" << endl <<
+ indent() << "return _defer.promise;" << endl;
indent_down();
indent(f_service_) << "} else {" << endl;
indent_up();
- f_service_ << indent() << "this._reqs[this.seqid()] = callback;" << endl << indent()
- << "this.send_" << funname << "(" << arglist << ");" << endl;
+ f_service_ <<
+ indent() << "this._reqs[this.seqid()] = callback;" << endl <<
+ indent() << "this.send_" << funname << "(" << arglist << ");" << endl;
indent_down();
indent(f_service_) << "}" << endl;
- } else if (gen_jquery_) { // jQuery output ./gen-js
- f_service_ << indent() << "if (callback === undefined) {" << endl;
+ }
+ else if (gen_jquery_) { //jQuery output ./gen-js
+ f_service_ << indent() << "if (callback === undefined) {" << endl;
indent_up();
f_service_ << indent() << "this.send_" << funname << "(" << arglist << ");" << endl;
if (!(*f_iter)->is_oneway()) {
@@ -1249,20 +1314,19 @@
indent_down();
f_service_ << indent() << "} else {" << endl;
indent_up();
- f_service_ << indent() << "var postData = this.send_" << funname << "(" << arglist
- << (arglist.empty() ? "" : ", ") << "true);" << endl;
+ f_service_ << indent() << "var postData = this.send_" << funname <<
+ "(" << arglist << (arglist.empty() ? "" : ", ") << "true);" << endl;
f_service_ << indent() << "return this.output.getTransport()" << endl;
indent_up();
- f_service_ << indent() << ".jqRequest(this, postData, arguments, this.recv_" << funname
- << ");" << endl;
+ f_service_ << indent() << ".jqRequest(this, postData, arguments, this.recv_" << funname << ");" << endl;
indent_down();
indent_down();
f_service_ << indent() << "}" << endl;
- } else { // Standard JavaScript ./gen-js
- f_service_ << indent() << "this.send_" << funname << "(" << arglist
- << (arglist.empty() ? "" : ", ") << "callback); " << endl;
+ } else { //Standard JavaScript ./gen-js
+ f_service_ << indent() <<
+ "this.send_" << funname << "(" << arglist << (arglist.empty() ? "" : ", ") << "callback); " << endl;
if (!(*f_iter)->is_oneway()) {
- f_service_ << indent() << "if (!callback) {" << endl;
+ f_service_ << indent() << "if (!callback) {" << endl;
f_service_ << indent();
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << " return ";
@@ -1276,45 +1340,52 @@
f_service_ << "};" << endl << endl;
+
// Send function
- f_service_ << js_namespace(tservice->get_program()) << service_name_ << "Client.prototype.send_"
- << function_signature(*f_iter, "", !gen_node_) << " {" << endl;
+ f_service_ << js_namespace(tservice->get_program())<<service_name_ <<
+ "Client.prototype.send_" << function_signature(*f_iter, "", !gen_node_) << " {" << endl;
indent_up();
std::string outputVar;
if (gen_node_) {
- f_service_ << indent() << "var output = new this.pClass(this.output);" << endl;
+ f_service_ <<
+ indent() << "var output = new this.pClass(this.output);" << endl;
outputVar = "output";
} else {
outputVar = "this.output";
}
- std::string argsname = js_namespace(program_) + service_name_ + "_" + (*f_iter)->get_name()
- + "_args";
+ std::string argsname = js_namespace(program_)+ service_name_ + "_" + (*f_iter)->get_name() + "_args";
- std::string messageType = (*f_iter)->is_oneway() ? "Thrift.MessageType.ONEWAY"
- : "Thrift.MessageType.CALL";
+ std::string messageType
+ = (*f_iter)->is_oneway() ? "Thrift.MessageType.ONEWAY" : "Thrift.MessageType.CALL";
// Serialize the request header
if (gen_node_) {
- f_service_ << indent() << outputVar << ".writeMessageBegin('" << (*f_iter)->get_name()
- << "', " << messageType << ", this.seqid());" << endl;
- } else {
- f_service_ << indent() << outputVar << ".writeMessageBegin('" << (*f_iter)->get_name()
- << "', " << messageType << ", this.seqid);" << endl;
+ f_service_ <<
+ indent() << outputVar << ".writeMessageBegin('" << (*f_iter)->get_name()
+ << "', " << messageType << ", this.seqid());" << endl;
+ }
+ else {
+ f_service_ <<
+ indent() << outputVar << ".writeMessageBegin('"
+ << (*f_iter)->get_name() << "', "
+ << messageType << ", this.seqid);" << endl;
}
- f_service_ << indent() << "var args = new " << argsname << "();" << endl;
+ f_service_ <<
+ indent() << "var args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = "
- << (*fld_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << ";" << endl;
}
// Write to the stream
- f_service_ << indent() << "args.write(" << outputVar << ");" << endl << indent() << outputVar
- << ".writeMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "args.write(" << outputVar << ");" << endl <<
+ indent() << outputVar << ".writeMessageEnd();" << endl;
if (gen_node_) {
f_service_ << indent() << "return this.output.flush();" << endl;
@@ -1343,15 +1414,15 @@
f_service_ << "};" << endl;
+
if (!(*f_iter)->is_oneway()) {
- std::string resultname = js_namespace(tservice->get_program()) + service_name_ + "_"
- + (*f_iter)->get_name() + "_result";
+ std::string resultname = js_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_result";
if (gen_node_) {
// Open function
- f_service_ << endl << js_namespace(tservice->get_program()) << service_name_
- << "Client.prototype.recv_" << (*f_iter)->get_name()
- << " = function(input,mtype,rseqid) {" << endl;
+ f_service_ <<
+ endl << js_namespace(tservice->get_program())<<service_name_ <<
+ "Client.prototype.recv_" << (*f_iter)->get_name() << " = function(input,mtype,rseqid) {" << endl;
} else {
t_struct noargs(program_);
@@ -1359,8 +1430,9 @@
string("recv_") + (*f_iter)->get_name(),
&noargs);
// Open function
- f_service_ << endl << js_namespace(tservice->get_program()) << service_name_
- << "Client.prototype." << function_signature(&recv_function) << " {" << endl;
+ f_service_ <<
+ endl << js_namespace(tservice->get_program())<<service_name_ <<
+ "Client.prototype." << function_signature(&recv_function) << " {" << endl;
}
indent_up();
@@ -1373,58 +1445,73 @@
}
if (gen_node_) {
- f_service_ << indent() << "var callback = this._reqs[rseqid] || function() {};" << endl
- << indent() << "delete this._reqs[rseqid];" << endl;
+ f_service_ <<
+ indent() << "var callback = this._reqs[rseqid] || function() {};" << endl <<
+ indent() << "delete this._reqs[rseqid];" << endl;
} else {
- f_service_ << indent() << "var ret = this.input.readMessageBegin();" << endl << indent()
- << "var fname = ret.fname;" << endl << indent() << "var mtype = ret.mtype;"
- << endl << indent() << "var rseqid = ret.rseqid;" << endl;
+ f_service_ <<
+ indent() << "var ret = this.input.readMessageBegin();" << endl <<
+ indent() << "var fname = ret.fname;" << endl <<
+ indent() << "var mtype = ret.mtype;" << endl <<
+ indent() << "var rseqid = ret.rseqid;" <<endl;
}
- f_service_ << indent() << "if (mtype == Thrift.MessageType.EXCEPTION) {" << endl << indent()
- << " var x = new Thrift.TApplicationException();" << endl << indent()
- << " x.read(" << inputVar << ");" << endl << indent() << " " << inputVar
- << ".readMessageEnd();" << endl << indent() << " " << render_recv_throw("x")
- << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (mtype == Thrift.MessageType.EXCEPTION) {" << endl <<
+ indent() << " var x = new Thrift.TApplicationException();" << endl <<
+ indent() << " x.read(" << inputVar << ");" << endl <<
+ indent() << " " << inputVar << ".readMessageEnd();" << endl <<
+ indent() << " " << render_recv_throw("x") << endl <<
+ indent() << "}" << endl;
- f_service_ << indent() << "var result = new " << resultname << "();" << endl << indent()
- << "result.read(" << inputVar << ");" << endl;
- f_service_ << indent() << inputVar << ".readMessageEnd();" << endl << endl;
+ f_service_ <<
+ indent() << "var result = new " << resultname << "();" << endl <<
+ indent() << "result.read(" << inputVar << ");" << endl;
+
+
+ f_service_ <<
+ indent() << inputVar << ".readMessageEnd();" << endl <<
+ endl;
+
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if (null !== result." << (*x_iter)->get_name() << ") {" << endl
- << indent() << " " << render_recv_throw("result." + (*x_iter)->get_name())
- << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (null !== result." << (*x_iter)->get_name() << ") {" << endl <<
+ indent() << " " << render_recv_throw("result." + (*x_iter)->get_name()) << endl <<
+ indent() << "}" << endl;
}
// Careful, only return result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (null !== result.success) {" << endl << indent() << " "
- << render_recv_return("result.success") << endl << indent() << "}" << endl;
- f_service_ << indent()
- << render_recv_throw("'" + (*f_iter)->get_name() + " failed: unknown result'")
- << endl;
+ f_service_ <<
+ indent() << "if (null !== result.success) {" << endl <<
+ indent() << " " << render_recv_return("result.success") << endl <<
+ indent() << "}" << endl;
+ f_service_ <<
+ indent() << render_recv_throw("'" + (*f_iter)->get_name() + " failed: unknown result'") << endl;
} else {
- if (gen_node_) {
- indent(f_service_) << "callback(null)" << endl;
- } else {
- indent(f_service_) << "return;" << endl;
- }
+ if (gen_node_) {
+ indent(f_service_) << "callback(null)" << endl;
+ } else {
+ indent(f_service_) << "return;" << endl;
+ }
}
// Close function
indent_down();
- f_service_ << "};" << endl;
+ f_service_ << "};"<<endl;
+
}
}
if (gen_ts_) {
- f_service_ts_ << ts_indent() << "}" << endl;
+ f_service_ts_ << ts_indent() << "}" << endl;
}
+
}
std::string t_js_generator::render_recv_throw(std::string var) {
@@ -1446,21 +1533,24 @@
/**
* Deserializes a field of any type.
*/
-void t_js_generator::generate_deserialize_field(ofstream& out,
- t_field* tfield,
- string prefix,
- bool inclass) {
- (void)inclass;
+void t_js_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix,
+ bool inclass) {
+ (void) inclass;
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
- string name = prefix + tfield->get_name();
+ string name = prefix+tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
@@ -1470,7 +1560,8 @@
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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
out << (((t_base_type*)type)->is_binary() ? "readBinary()" : "readString()");
@@ -1501,14 +1592,13 @@
}
if (!gen_node_) {
- out << ".value";
+ out << ".value";
}
out << ";" << endl;
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type->get_name().c_str());
+ tfield->get_name().c_str(), type->get_name().c_str());
}
}
@@ -1518,12 +1608,18 @@
* buffer for deserialization, and that there is a variable protocol which
* is a reference to a TProtocol serialization object.
*/
-void t_js_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- out << indent() << prefix << " = new " << js_type_namespace(tstruct->get_program())
- << tstruct->get_name() << "();" << endl << indent() << prefix << ".read(input);" << endl;
+void t_js_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = new " << js_type_namespace(tstruct->get_program())<<tstruct->get_name() << "();" << endl <<
+ indent() << prefix << ".read(input);" << endl;
+
}
-void t_js_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_js_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
string size = tmp("_size");
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
@@ -1538,41 +1634,55 @@
out << indent() << "var " << size << " = 0;" << endl;
out << indent() << "var " << rtmp3 << ";" << endl;
+
// Declare variables, read header
if (ttype->is_map()) {
- out << indent() << prefix << " = {};" << endl << indent() << "var " << ktype << " = 0;" << endl
- << indent() << "var " << vtype << " = 0;" << endl;
+ out <<
+ indent() << prefix << " = {};" << endl <<
+ indent() << "var " << ktype << " = 0;" << endl <<
+ indent() << "var " << vtype << " = 0;" << endl;
- out << indent() << rtmp3 << " = input.readMapBegin();" << endl;
- out << indent() << ktype << " = " << rtmp3 << ".ktype;" << endl;
- out << indent() << vtype << " = " << rtmp3 << ".vtype;" << endl;
- out << indent() << size << " = " << rtmp3 << ".size;" << endl;
+ out << indent() << rtmp3 << " = input.readMapBegin();" << endl;
+ out << indent() << ktype << " = " << rtmp3 << ".ktype;" << endl;
+ out << indent() << vtype << " = " << rtmp3 << ".vtype;" << endl;
+ out << indent() << size << " = " << rtmp3 << ".size;" << endl;
+
} else if (ttype->is_set()) {
- out << indent() << prefix << " = [];" << endl << indent() << "var " << etype << " = 0;" << endl
- << indent() << rtmp3 << " = input.readSetBegin();" << endl << indent() << etype << " = "
- << rtmp3 << ".etype;" << endl << indent() << size << " = " << rtmp3 << ".size;" << endl;
+ out <<
+ indent() << prefix << " = [];" << endl <<
+ indent() << "var " << etype << " = 0;" << endl <<
+ indent() << rtmp3 << " = input.readSetBegin();" << endl <<
+ indent() << etype << " = " << rtmp3 << ".etype;"<<endl<<
+ indent() << size << " = " << rtmp3 << ".size;"<<endl;
} else if (ttype->is_list()) {
- out << indent() << prefix << " = [];" << endl << indent() << "var " << etype << " = 0;" << endl
- << indent() << rtmp3 << " = input.readListBegin();" << endl << indent() << etype << " = "
- << rtmp3 << ".etype;" << endl << indent() << size << " = " << rtmp3 << ".size;" << endl;
+ out <<
+ indent() << prefix << " = [];" << endl <<
+ indent() << "var " << etype << " = 0;" << endl <<
+ indent() << rtmp3 << " = input.readListBegin();" << endl <<
+ indent() << etype << " = " << rtmp3 << ".etype;"<<endl<<
+ indent() << size << " = " << rtmp3 << ".size;"<<endl;
}
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for (var " << i << " = 0; " << i << " < " << size << "; ++" << i << ")" << endl;
+ indent(out) <<
+ "for (var " <<
+ i << " = 0; " << i << " < " << size << "; ++" << i << ")" << endl;
scope_up(out);
if (ttype->is_map()) {
if (!gen_node_) {
- out << indent() << "if (" << i << " > 0 ) {" << endl << indent()
- << " if (input.rstack.length > input.rpos[input.rpos.length -1] + 1) {" << endl
- << indent() << " input.rstack.pop();" << endl << indent() << " }" << endl << indent()
- << "}" << endl;
+ out <<
+ indent() << "if (" << i << " > 0 ) {" << endl <<
+ indent() << " if (input.rstack.length > input.rpos[input.rpos.length -1] + 1) {" << endl <<
+ indent() << " input.rstack.pop();" << endl <<
+ indent() << " }" << endl <<
+ indent() << "}" << endl;
}
generate_deserialize_map_element(out, (t_map*)ttype, prefix);
@@ -1584,6 +1694,7 @@
scope_down(out);
+
// Read container end
if (ttype->is_map()) {
indent(out) << "input.readMapEnd();" << endl;
@@ -1594,72 +1705,92 @@
}
}
+
/**
* Generates code to deserialize a map
*/
-void t_js_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_js_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("key");
string val = tmp("val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey, false, false) << ";" << endl;
- indent(out) << declare_field(&fval, false, false) << ";" << endl;
+ indent(out) <<
+ declare_field(&fkey, false, false) << ";" << endl;
+ indent(out) <<
+ declare_field(&fval, false, false) << ";" << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << ";" << endl;
}
-void t_js_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_js_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << "var " << elem << " = null;" << endl;
+ indent(out) <<
+ "var " << elem << " = null;" << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".push(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".push(" << elem << ");" << endl;
}
-void t_js_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix) {
+void t_js_generator::generate_deserialize_list_element(ofstream &out,
+ t_list* tlist,
+ string prefix) {
string elem = tmp("elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << "var " << elem << " = null;" << endl;
+ indent(out) <<
+ "var " << elem << " = null;" << endl;
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".push(" << elem << ");" << endl;
+ indent(out) <<
+ prefix << ".push(" << elem << ");" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_js_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_js_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix +tfield->get_name() );
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
string name = tfield->get_name();
- // Hack for when prefix is defined (always a hash ref)
- if (!prefix.empty())
+ //Hack for when prefix is defined (always a hash ref)
+ if(!prefix.empty())
name = prefix + tfield->get_name();
indent(out) << "output.";
@@ -1668,7 +1799,8 @@
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
out << (((t_base_type*)type)->is_binary() ? "writeBinary(" : "writeString(") << name << ")";
@@ -1713,70 +1845,88 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_js_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
+void t_js_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
indent(out) << prefix << ".write(output);" << endl;
}
/**
* Writes out a container
*/
-void t_js_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_js_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
if (ttype->is_map()) {
- indent(out) << "output.writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "Thrift.objectLength(" << prefix << "));" << endl;
+ indent(out) <<
+ "output.writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "Thrift.objectLength(" << prefix << "));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "output.writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", "
- << prefix << ".length);" << endl;
+ indent(out) <<
+ "output.writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length);" << endl;
} else if (ttype->is_list()) {
- indent(out) << "output.writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ", " << prefix << ".length);" << endl;
+ indent(out) <<
+ "output.writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ prefix << ".length);" << endl;
+
}
if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
- indent(out) << "for (var " << kiter << " in " << prefix << ")" << endl;
+ indent(out) << "for (var "<<kiter<<" in "<<prefix<<")" << endl;
scope_up(out);
- indent(out) << "if (" << prefix << ".hasOwnProperty(" << kiter << "))" << endl;
+ indent(out) << "if ("<<prefix<<".hasOwnProperty("<<kiter<<"))" <<endl;
scope_up(out);
- indent(out) << "var " << viter << " = " << prefix << "[" << kiter << "];" << endl;
+ indent(out) << "var "<<viter<<" = "<<prefix<<"["<<kiter<<"];"<<endl;
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
scope_down(out);
scope_down(out);
+
} else if (ttype->is_set()) {
string iter = tmp("iter");
- indent(out) << "for (var " << iter << " in " << prefix << ")" << endl;
+ indent(out) <<
+ "for (var "<<iter<<" in " << prefix << ")" << endl;
scope_up(out);
- indent(out) << "if (" << prefix << ".hasOwnProperty(" << iter << "))" << endl;
+ indent(out) << "if ("<<prefix<<".hasOwnProperty("<<iter<<"))" <<endl;
scope_up(out);
- indent(out) << iter << " = " << prefix << "[" << iter << "];" << endl;
+ indent(out) << iter << " = " << prefix << "[" << iter << "];"<< endl;
generate_serialize_set_element(out, (t_set*)ttype, iter);
scope_down(out);
scope_down(out);
+
} else if (ttype->is_list()) {
string iter = tmp("iter");
- indent(out) << "for (var " << iter << " in " << prefix << ")" << endl;
+ indent(out) <<
+ "for (var "<<iter<<" in "<< prefix << ")" << endl;
scope_up(out);
- indent(out) << "if (" << prefix << ".hasOwnProperty(" << iter << "))" << endl;
+ indent(out) << "if ("<<prefix<<".hasOwnProperty("<<iter<<"))" <<endl;
scope_up(out);
- indent(out) << iter << " = " << prefix << "[" << iter << "];" << endl;
+ indent(out) << iter << " = " << prefix << "[" << iter << "];"<< endl;
generate_serialize_list_element(out, (t_list*)ttype, iter);
scope_down(out);
scope_down(out);
}
if (ttype->is_map()) {
- indent(out) << "output.writeMapEnd();" << endl;
+ indent(out) <<
+ "output.writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "output.writeSetEnd();" << endl;
+ indent(out) <<
+ "output.writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "output.writeListEnd();" << endl;
+ indent(out) <<
+ "output.writeListEnd();" << endl;
}
}
@@ -1784,10 +1934,10 @@
* Serializes the members of a map.
*
*/
-void t_js_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string kiter,
- string viter) {
+void t_js_generator::generate_serialize_map_element(ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter) {
t_field kfield(tmap->get_key_type(), kiter);
generate_serialize_field(out, &kfield);
@@ -1798,7 +1948,9 @@
/**
* Serializes the members of a set.
*/
-void t_js_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_js_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
@@ -1806,7 +1958,9 @@
/**
* Serializes the members of a list.
*/
-void t_js_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_js_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
@@ -1819,8 +1973,8 @@
string t_js_generator::declare_field(t_field* tfield, bool init, bool obj) {
string result = "this." + tfield->get_name();
- if (!obj) {
- result = "var " + tfield->get_name();
+ if(!obj){
+ result = "var " + tfield->get_name();
}
if (init) {
@@ -1844,13 +1998,13 @@
}
} else if (type->is_enum()) {
result += " = null";
- } else if (type->is_map()) {
+ } else if (type->is_map()){
result += " = null";
} else if (type->is_container()) {
result += " = null";
} else if (type->is_struct() || type->is_xception()) {
if (obj) {
- result += " = new " + js_type_namespace(type->get_program()) + type->get_name() + "()";
+ result += " = new " +js_type_namespace(type->get_program()) + type->get_name() + "()";
} else {
result += " = null";
}
@@ -1868,12 +2022,12 @@
* @return String of rendered function definition
*/
string t_js_generator::function_signature(t_function* tfunction,
- string prefix,
- bool include_callback) {
+ string prefix,
+ bool include_callback) {
string str;
- str = prefix + tfunction->get_name() + " = function(";
+ str = prefix + tfunction->get_name() + " = function(";
str += argument_list(tfunction->get_arglist(), include_callback);
@@ -1884,7 +2038,8 @@
/**
* Renders a field list
*/
-string t_js_generator::argument_list(t_struct* tstruct, bool include_callback) {
+string t_js_generator::argument_list(t_struct* tstruct,
+ bool include_callback) {
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@@ -1963,23 +2118,23 @@
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
- case t_base_type::TYPE_STRING:
- ts_type = "string";
- break;
- case t_base_type::TYPE_BOOL:
- ts_type = "boolean";
- break;
- case t_base_type::TYPE_BYTE:
- ts_type = "any";
- break;
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_DOUBLE:
- ts_type = "number";
- break;
- case t_base_type::TYPE_VOID:
- ts_type = "void";
+ case t_base_type::TYPE_STRING:
+ ts_type = "string";
+ break;
+ case t_base_type::TYPE_BOOL:
+ ts_type = "boolean";
+ break;
+ case t_base_type::TYPE_BYTE:
+ ts_type = "any";
+ break;
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_DOUBLE:
+ ts_type = "number";
+ break;
+ case t_base_type::TYPE_VOID:
+ ts_type = "void";
}
} else if (type->is_enum() || type->is_struct() || type->is_xception()) {
ts_type = type->get_name();
@@ -2044,8 +2199,8 @@
return str;
}
-THRIFT_REGISTER_GENERATOR(js,
- "Javascript",
- " jquery: Generate jQuery compatible code.\n"
- " node: Generate node.js compatible code.\n"
- " ts: Generate TypeScript definition files.\n")
+
+THRIFT_REGISTER_GENERATOR(js, "Javascript",
+" jquery: Generate jQuery compatible code.\n"
+" node: Generate node.js compatible code.\n"
+" ts: Generate TypeScript definition files.\n")
diff --git a/compiler/cpp/src/generate/t_json_generator.cc b/compiler/cpp/src/generate/t_json_generator.cc
index dee1de2..290ffaa 100644
--- a/compiler/cpp/src/generate/t_json_generator.cc
+++ b/compiler/cpp/src/generate/t_json_generator.cc
@@ -45,12 +45,14 @@
class t_json_generator : public t_generator {
public:
- t_json_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ t_json_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-json";
}
@@ -67,8 +69,8 @@
void generate_enum(t_enum* tenum);
void generate_program();
void generate_consts(vector<t_const*>);
- void generate_function(t_function* tfunc);
- void generate_field(t_field* field);
+ void generate_function(t_function * tfunc);
+ void generate_field(t_field * field);
void generate_service(t_service* tservice);
void generate_struct(t_struct* tstruct);
@@ -98,7 +100,7 @@
string f_json_name = get_out_dir() + program_->get_name() + ".json";
f_json_.open(f_json_name.c_str());
- // Merge all included programs into this one so we can output one big file.
+ //Merge all included programs into this one so we can output one big file.
merge_includes(program_);
}
@@ -106,81 +108,60 @@
std::ostringstream ss;
for (std::string::const_iterator iter = input.begin(); iter != input.end(); iter++) {
switch (*iter) {
- case '\\':
- ss << "\\\\";
- break;
- case '"':
- ss << "\\\"";
- break;
- case '/':
- ss << "\\/";
- break;
- case '\b':
- ss << "\\b";
- break;
- case '\f':
- ss << "\\f";
- break;
- case '\n':
- ss << "\\n";
- break;
- case '\r':
- ss << "\\r";
- break;
- case '\t':
- ss << "\\t";
- break;
- default:
- ss << *iter;
- break;
+ case '\\': ss << "\\\\"; break;
+ case '"': ss << "\\\""; break;
+ case '/': ss << "\\/"; break;
+ case '\b': ss << "\\b"; break;
+ case '\f': ss << "\\f"; break;
+ case '\n': ss << "\\n"; break;
+ case '\r': ss << "\\r"; break;
+ case '\t': ss << "\\t"; break;
+ default: ss << *iter; break;
}
}
return ss.str();
}
-void t_json_generator::start_object() {
+void t_json_generator::start_object(){
f_json_ << "{";
_commaNeeded.push(false);
}
-void t_json_generator::start_array() {
+void t_json_generator::start_array(){
f_json_ << "[";
_commaNeeded.push(false);
}
-void t_json_generator::write_comma_if_needed() {
- if (_commaNeeded.top())
- f_json_ << ",";
+void t_json_generator::write_comma_if_needed(){
+ if (_commaNeeded.top()) f_json_ << ",";
}
-void t_json_generator::indicate_comma_needed() {
+void t_json_generator::indicate_comma_needed(){
_commaNeeded.pop();
_commaNeeded.push(true);
}
-void t_json_generator::write_key(string key, string val) {
+void t_json_generator::write_key(string key, string val){
write_comma_if_needed();
f_json_ << quot << key << quot << ":" << quot << escapeJsonString(val) << quot;
indicate_comma_needed();
}
-void t_json_generator::write_key_int(string key, int val) {
+void t_json_generator::write_key_int(string key, int val){
write_comma_if_needed();
f_json_ << quot << key << quot << ":" << quot << val << quot;
indicate_comma_needed();
}
-void t_json_generator::end_object(bool newLine) {
+void t_json_generator::end_object(bool newLine){
f_json_ << "}";
- if (newLine)
- f_json_ << endl;
+ if (newLine) f_json_ << endl;
_commaNeeded.pop();
}
-void t_json_generator::end_array(bool newLine) {
+void t_json_generator::end_array(bool newLine){
f_json_ << "]";
- if (newLine)
- f_json_ << endl;
+ if (newLine) f_json_ << endl;
_commaNeeded.pop();
}
@@ -188,35 +169,35 @@
f_json_.close();
}
-void t_json_generator::merge_includes(t_program* program) {
+void t_json_generator::merge_includes(t_program * program) {
vector<t_program*> includes = program->get_includes();
vector<t_program*>::iterator inc_iter;
- for (inc_iter = includes.begin(); inc_iter != includes.end(); ++inc_iter) {
+ for (inc_iter = includes.begin(); inc_iter != includes.end(); ++inc_iter){
t_program* include = *inc_iter;
- // recurse in case we get crazy
+ //recurse in case we get crazy
merge_includes(include);
- // merge enums
+ //merge enums
vector<t_enum*> enums = include->get_enums();
vector<t_enum*>::iterator en_iter;
for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
program->add_enum(*en_iter);
}
- // merge typedefs
+ //merge typedefs
vector<t_typedef*> typedefs = include->get_typedefs();
vector<t_typedef*>::iterator td_iter;
for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
program->add_typedef(*td_iter);
}
- // merge structs
+ //merge structs
vector<t_struct*> objects = include->get_objects();
vector<t_struct*>::iterator o_iter;
for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
program->add_struct(*o_iter);
}
- // merge constants
+ //merge constants
vector<t_const*> consts = include->get_consts();
vector<t_const*>::iterator c_iter;
- for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter){
program->add_const(*c_iter);
}
@@ -235,8 +216,7 @@
start_object();
write_key("name", program_->get_name());
- if (program_->has_doc())
- write_key("doc", program_->get_doc());
+ if (program_->has_doc()) write_key("doc", program_->get_doc());
// Generate enums
vector<t_enum*> enums = program_->get_enums();
@@ -274,7 +254,8 @@
write_comma_if_needed();
if ((*o_iter)->is_xception()) {
generate_xception(*o_iter);
- } else {
+ }
+ else {
generate_struct(*o_iter);
}
indicate_comma_needed();
@@ -303,16 +284,15 @@
close_generator();
}
-void t_json_generator::generate_typedef(t_typedef* ttypedef) {
+void t_json_generator::generate_typedef(t_typedef* ttypedef){
start_object();
write_key("name", ttypedef->get_name());
write_key("type", get_type_name(ttypedef->get_true_type()));
- if (ttypedef->has_doc())
- write_key("doc", ttypedef->get_doc());
+ if (ttypedef->has_doc()) write_key("doc", ttypedef->get_doc());
end_object(true);
}
-void t_json_generator::generate_consts(vector<t_const*> consts) {
+void t_json_generator::generate_consts(vector<t_const*> consts){
vector<t_const*>::iterator c_iter;
f_json_ << ",\"constants\":";
start_array();
@@ -324,8 +304,7 @@
t_const* con = (*c_iter);
write_key("name", con->get_name());
write_key("type", get_type_name(con->get_type()));
- if (con->has_doc())
- write_key("doc", con->get_doc());
+ if (con->has_doc()) write_key("doc", con->get_doc());
write_key("value", get_const_value(con->get_value()));
end_object(true);
}
@@ -335,8 +314,7 @@
void t_json_generator::generate_enum(t_enum* tenum) {
start_object();
write_key("name", tenum->get_name());
- if (tenum->has_doc())
- write_key("doc", tenum->get_doc());
+ if (tenum->has_doc()) write_key("doc", tenum->get_doc());
f_json_ << ",\"members\":";
start_array();
vector<t_enum_value*> values = tenum->get_constants();
@@ -347,8 +325,7 @@
start_object();
write_key("name", val->get_name());
write_key_int("value", val->get_value());
- if (val->has_doc())
- write_key("doc", val->get_doc());
+ if (val->has_doc()) write_key("doc", val->get_doc());
end_object(false);
indicate_comma_needed();
}
@@ -356,13 +333,11 @@
end_object(true);
}
-void t_json_generator::generate_struct(t_struct* tstruct) {
+void t_json_generator::generate_struct(t_struct* tstruct){
start_object();
write_key("name", tstruct->get_name());
- if (tstruct->has_doc())
- write_key("doc", tstruct->get_doc());
- if (tstruct->is_xception())
- write_key("isException", "true");
+ if (tstruct->has_doc()) write_key("doc", tstruct->get_doc());
+ if (tstruct->is_xception()) write_key("isException", "true");
vector<t_field*> members = tstruct->get_members();
vector<t_field*>::iterator mem_iter = members.begin();
f_json_ << ",\"fields\":";
@@ -374,13 +349,11 @@
end_object(true);
}
-void t_json_generator::generate_service(t_service* tservice) {
+void t_json_generator::generate_service(t_service* tservice){
start_object();
write_key("name", tservice->get_name());
- if (tservice->get_extends())
- write_key("extendsType", tservice->get_extends()->get_name());
- if (tservice->has_doc())
- write_key("doc", tservice->get_doc());
+ if (tservice->get_extends()) write_key("extendsType", tservice->get_extends()->get_name());
+ if (tservice->has_doc()) write_key("doc", tservice->get_doc());
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator fn_iter = functions.begin();
f_json_ << ",\"functions\":";
@@ -395,14 +368,12 @@
end_object(true);
}
-void t_json_generator::generate_function(t_function* tfunc) {
+void t_json_generator::generate_function(t_function* tfunc){
start_object();
write_key("name", tfunc->get_name());
write_key("returnType", get_type_name(tfunc->get_returntype()));
- if (tfunc->is_oneway())
- write_key("oneWay", "true");
- if (tfunc->has_doc())
- write_key("doc", tfunc->get_doc());
+ if (tfunc->is_oneway()) write_key("oneWay", "true");
+ if (tfunc->has_doc()) write_key("doc", tfunc->get_doc());
vector<t_field*> members = tfunc->get_arglist()->get_members();
vector<t_field*>::iterator mem_iter = members.begin();
f_json_ << ",\"arguments\":";
@@ -423,21 +394,20 @@
end_object(false);
}
-void t_json_generator::generate_field(t_field* field) {
+void t_json_generator::generate_field(t_field * field){
write_comma_if_needed();
start_object();
write_key_int("index", field->get_key());
write_key("name", field->get_name());
write_key("type", get_type_name(field->get_type()));
- if (field->has_doc())
- write_key("doc", field->get_doc());
+ if (field->has_doc()) write_key("doc", field->get_doc());
switch (field->get_req()) {
- case t_field::T_REQUIRED:
- write_key("required", "true");
- break;
- default:
- write_key("required", "false");
- break;
+ case t_field::T_REQUIRED:
+ write_key("required", "true");
+ break;
+ default:
+ write_key("required", "false");
+ break;
}
if (field->get_value())
write_key("default", get_const_value(field->get_value()));
@@ -445,7 +415,7 @@
end_object(false);
indicate_comma_needed();
}
-string t_json_generator::get_const_value(t_const_value* tvalue) {
+string t_json_generator::get_const_value(t_const_value* tvalue){
switch (tvalue->get_type()) {
case t_const_value::CV_INTEGER:
@@ -454,15 +424,14 @@
return tvalue->get_string();
case t_const_value::CV_STRING:
return tvalue->get_string();
- case t_const_value::CV_LIST: {
+ case t_const_value::CV_LIST:
+ {
string list = "[";
- vector<t_const_value*> list_elems = tvalue->get_list();
- ;
+ vector<t_const_value*> list_elems = tvalue->get_list();;
vector<t_const_value*>::iterator list_iter;
bool first = true;
for (list_iter = list_elems.begin(); list_iter != list_elems.end(); list_iter++) {
- if (!first)
- list += ",";
+ if (!first)list += ",";
first = false;
list += get_const_value(*list_iter);
}
@@ -476,8 +445,7 @@
string map = "[";
bool first = true;
for (map_iter = map_elems.begin(); map_iter != map_elems.end(); map_iter++) {
- if (!first)
- map += ",";
+ if (!first) map += ",";
first = false;
map += get_const_value(map_iter->first) + ":";
map += get_const_value(map_iter->second);
@@ -486,17 +454,20 @@
}
return "UNKNOWN";
}
-string t_json_generator::get_type_name(t_type* ttype) {
+string t_json_generator::get_type_name(t_type* ttype){
if (ttype->is_container()) {
if (ttype->is_list()) {
return "list<" + get_type_name(((t_list*)ttype)->get_elem_type()) + ">";
- } else if (ttype->is_set()) {
- return "set<" + get_type_name(((t_set*)ttype)->get_elem_type()) + ">";
- } else if (ttype->is_map()) {
- return "map<" + get_type_name(((t_map*)ttype)->get_key_type()) + +","
- + get_type_name(((t_map*)ttype)->get_val_type()) + ">";
}
- } else if (ttype->is_base_type()) {
+ else if (ttype->is_set()) {
+ return "set<" + get_type_name(((t_set*)ttype)->get_elem_type()) + ">";
+ }
+ else if (ttype->is_map()) {
+ return "map<" + get_type_name(((t_map*)ttype)->get_key_type()) +
+ +"," + get_type_name(((t_map*)ttype)->get_val_type()) + ">";
+ }
+ }
+ else if (ttype->is_base_type()) {
return (((t_base_type*)ttype)->is_binary() ? "binary" : ttype->get_name());
}
return ttype->get_name();
diff --git a/compiler/cpp/src/generate/t_lua_generator.cc b/compiler/cpp/src/generate/t_lua_generator.cc
index 5703ecb..7303c21 100644
--- a/compiler/cpp/src/generate/t_lua_generator.cc
+++ b/compiler/cpp/src/generate/t_lua_generator.cc
@@ -26,19 +26,21 @@
using std::vector;
using std::map;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* LUA code generator.
*
*/
class t_lua_generator : public t_oop_generator {
-public:
- t_lua_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_lua_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("omit_requires");
@@ -56,16 +58,17 @@
/**
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
-private:
+ private:
+
/**
* True iff we should generate lua require statements.
*/
@@ -74,71 +77,81 @@
/**
* Struct-level generation functions
*/
- void generate_lua_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false);
+ void generate_lua_struct_definition(
+ std::ofstream& out, t_struct* tstruct, bool is_xception=false);
void generate_lua_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_lua_struct_writer(std::ofstream& out, t_struct* tstruct);
/**
* Service-level generation functions
*/
- void generate_service_client(std::ofstream& out, t_service* tservice);
- void generate_service_interface(std::ofstream& out, t_service* tservice);
- void generate_service_processor(std::ofstream& out, t_service* tservice);
- void generate_process_function(std::ofstream& out, t_service* tservice, t_function* tfunction);
- void generate_service_helpers(ofstream& out, t_service* tservice);
- void generate_function_helpers(ofstream& out, t_function* tfunction);
+ void generate_service_client (std::ofstream& out, t_service* tservice);
+ void generate_service_interface (std::ofstream& out, t_service* tservice);
+ void generate_service_processor (std::ofstream& out, t_service* tservice);
+ void generate_process_function (std::ofstream& out, t_service* tservice,
+ t_function* tfunction);
+ void generate_service_helpers (ofstream &out, t_service* tservice);
+ void generate_function_helpers (ofstream &out, t_function* tfunction);
/**
* Deserialization (Read)
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_deserialize_field(
+ std::ofstream &out, t_field *tfield, std::string prefix="");
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct(
+ std::ofstream &out, t_struct *tstruct, std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container(
+ std::ofstream &out, t_type *ttype, std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element(
+ std::ofstream &out, t_set *tset, std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element(
+ std::ofstream &out, t_map *tmap, std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element(
+ std::ofstream &out, t_list *tlist, std::string prefix="");
/**
* Serialization (Write)
*/
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field(
+ std::ofstream &out, t_field *tfield, std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct(
+ std::ofstream &out, t_struct *tstruct, std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container(
+ std::ofstream &out, t_type *ttype, std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element(
+ std::ofstream &out, t_map *tmap, std::string kiter, std::string viter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element(
+ std::ofstream &out, t_set *tmap, std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element(
+ std::ofstream &out, t_list *tlist, std::string iter);
/**
* Helper rendering functions
*/
std::string lua_includes();
- std::string function_signature(t_function* tfunction, std::string prefix = "");
- std::string argument_list(t_struct* tstruct, std::string prefix = "");
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string argument_list(t_struct* tstruct, std::string prefix="");
std::string type_to_enum(t_type* ttype);
static std::string get_namespace(const t_program* program);
std::string autogen_comment() {
- return std::string("--\n") + "-- Autogenerated by Thrift\n" + "--\n"
- + "-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "-- @"
- "generated\n"
- + "--\n";
+ return
+ std::string("--\n") +
+ "-- Autogenerated by Thrift\n" +
+ "--\n" +
+ "-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "-- @""generated\n" +
+ "--\n";
}
/**
@@ -149,6 +162,7 @@
std::ofstream f_service_;
};
+
/**
* Init and close methods
*/
@@ -182,8 +196,10 @@
* Generate a typedef (essentially a constant)
*/
void t_lua_generator::generate_typedef(t_typedef* ttypedef) {
- f_types_ << endl << endl << indent() << ttypedef->get_symbolic() << " = "
- << ttypedef->get_type()->get_name();
+ f_types_
+ << endl << endl << indent()
+ << ttypedef->get_symbolic() << " = "
+ << ttypedef->get_type()->get_name();
}
/**
@@ -222,7 +238,8 @@
/**
* Prints the value of a constant with the given type.
*/
-string t_lua_generator::render_const_value(t_type* type, t_const_value* value) {
+string t_lua_generator::render_const_value(
+ t_type* type, t_const_value* value) {
std::ostringstream out;
type = get_true_type(type);
@@ -251,7 +268,8 @@
}
break;
default:
- throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
+ throw "compiler error: no const of base type "
+ + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << value->get_integer();
@@ -271,7 +289,8 @@
}
}
if (field_type == NULL) {
- throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+ throw "type error: " + type->get_name() + " has no field "
+ + v_iter->first->get_string();
}
indent(out);
@@ -296,8 +315,9 @@
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end();) {
- indent(out) << "[" << render_const_value(ktype, v_iter->first)
- << "] = " << render_const_value(vtype, v_iter->second);
+ indent(out)
+ << "[" << render_const_value(ktype, v_iter->first) << "] = "
+ << render_const_value(vtype, v_iter->second);
++v_iter;
if (v_iter != val.end()) {
out << ",";
@@ -351,15 +371,16 @@
/**
* Generate a thrift struct or exception (lua table)
*/
-void t_lua_generator::generate_lua_struct_definition(ofstream& out,
- t_struct* tstruct,
+void t_lua_generator::generate_lua_struct_definition(ofstream &out,
+ t_struct *tstruct,
bool is_exception) {
vector<t_field*>::const_iterator m_iter;
const vector<t_field*>& members = tstruct->get_members();
indent(out) << endl << endl << tstruct->get_name();
if (is_exception) {
- out << " = TException:new{" << endl << indent() << " __type = '" << tstruct->get_name() << "'";
+ out << " = TException:new{" << endl <<
+ indent() << " __type = '" << tstruct->get_name() << "'";
if (members.size() > 0) {
out << ",";
}
@@ -387,51 +408,56 @@
/**
* Generate a struct/exception reader
*/
-void t_lua_generator::generate_lua_struct_reader(ofstream& out, t_struct* tstruct) {
+void t_lua_generator::generate_lua_struct_reader(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// function
- indent(out) << endl << endl << "function " << tstruct->get_name() << ":read(iprot)" << endl;
+ indent(out) << endl << endl
+ << "function " << tstruct->get_name() << ":read(iprot)" << endl;
indent_up();
indent(out) << "iprot:readStructBegin()" << endl;
// while: Read in fields
indent(out) << "while true do" << endl;
- indent_up();
-
- // if: Check what to read
- indent(out) << "local fname, ftype, fid = iprot:readFieldBegin()" << endl;
- indent(out) << "if ftype == TType.STOP then" << endl;
- indent_up();
- indent(out) << "break" << endl;
-
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent_down();
- indent(out) << "elseif fid == " << (*f_iter)->get_key() << " then" << endl;
- indent_up();
- indent(out) << "if ftype == " << type_to_enum((*f_iter)->get_type()) << " then" << endl;
indent_up();
- // Read field contents
- generate_deserialize_field(out, *f_iter, "self.");
+ // if: Check what to read
+ indent(out) << "local fname, ftype, fid = iprot:readFieldBegin()" << endl;
+ indent(out) << "if ftype == TType.STOP then" << endl;
+ indent_up();
+ indent(out) << "break" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent_down();
+ indent(out)
+ << "elseif fid == " << (*f_iter)->get_key() << " then" << endl;
+ indent_up();
+ indent(out)
+ << "if ftype == " << type_to_enum((*f_iter)->get_type())
+ << " then" << endl;
+ indent_up();
+
+ // Read field contents
+ generate_deserialize_field(out, *f_iter, "self.");
+
+ indent_down();
+ indent(out) << "else" << endl;
+ indent(out) << " iprot:skip(ftype)" << endl;
+ indent(out) << "end" << endl;
+ }
+
+ // end if
indent_down();
indent(out) << "else" << endl;
indent(out) << " iprot:skip(ftype)" << endl;
indent(out) << "end" << endl;
- }
-
- // end if
- indent_down();
- indent(out) << "else" << endl;
- indent(out) << " iprot:skip(ftype)" << endl;
- indent(out) << "end" << endl;
- indent(out) << "iprot:readFieldEnd()" << endl;
+ indent(out) << "iprot:readFieldEnd()" << endl;
// end while
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
indent(out) << "iprot:readStructEnd()" << endl;
@@ -444,31 +470,36 @@
/**
* Generate a struct/exception writer
*/
-void t_lua_generator::generate_lua_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_lua_generator::generate_lua_struct_writer(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
// function
- indent(out) << endl << endl << "function " << tstruct->get_name() << ":write(oprot)" << endl;
+ indent(out) << endl << endl
+ << "function " << tstruct->get_name() << ":write(oprot)" << endl;
indent_up();
- indent(out) << "oprot:writeStructBegin('" << tstruct->get_name() << "')" << endl;
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "if self." << (*f_iter)->get_name() << " then" << endl;
- indent_up();
- indent(out) << "oprot:writeFieldBegin('" << (*f_iter)->get_name() << "', "
- << type_to_enum((*f_iter)->get_type()) << ", " << (*f_iter)->get_key() << ")"
- << endl;
+ indent(out)
+ << "oprot:writeStructBegin('" << tstruct->get_name() << "')" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) << "if self." << (*f_iter)->get_name() << " then" << endl;
+ indent_up();
+ indent(out)
+ << "oprot:writeFieldBegin('" << (*f_iter)->get_name() << "', "
+ << type_to_enum((*f_iter)->get_type()) << ", "
+ << (*f_iter)->get_key() << ")" << endl;
- // Write field contents
- generate_serialize_field(out, *f_iter, "self.");
+ // Write field contents
+ generate_serialize_field(out, *f_iter, "self.");
- indent(out) << "oprot:writeFieldEnd()" << endl;
- indent_down();
- indent(out) << "end" << endl;
- }
- indent(out) << "oprot:writeFieldStop()" << endl;
- indent(out) << "oprot:writeStructEnd()" << endl;
+ indent(out)
+ << "oprot:writeFieldEnd()" << endl;
+ indent_down();
+ indent(out) << "end" << endl;
+ }
+ indent(out) << "oprot:writeFieldStop()" << endl;
+ indent(out) << "oprot:writeStructEnd()" << endl;
// end function
indent_down();
@@ -494,8 +525,9 @@
f_service_ << endl << "require '" << cur_ns << "ttypes'" << endl;
if (tservice->get_extends() != NULL) {
- f_service_ << "require '" << get_namespace(tservice->get_extends()->get_program())
- << tservice->get_extends()->get_name() << "'" << endl;
+ f_service_
+ << "require '" << get_namespace(tservice->get_extends()->get_program())
+ << tservice->get_extends()->get_name() << "'" << endl;
}
}
@@ -510,7 +542,8 @@
f_service_.close();
}
-void t_lua_generator::generate_service_interface(ofstream& out, t_service* tservice) {
+void t_lua_generator::generate_service_interface(ofstream &out,
+ t_service* tservice) {
string classname = tservice->get_name() + "Iface";
t_service* extends_s = tservice->get_extends();
@@ -521,10 +554,13 @@
} else {
out << "__TObject:new{" << endl;
}
- out << " __type = '" << classname << "'" << endl << "}" << endl << endl;
+ out
+ << " __type = '" << classname << "'" << endl
+ << "}" << endl << endl;
}
-void t_lua_generator::generate_service_client(ofstream& out, t_service* tservice) {
+void t_lua_generator::generate_service_client(ofstream &out,
+ t_service* tservice) {
string classname = tservice->get_name() + "Client";
t_service* extends_s = tservice->get_extends();
@@ -535,7 +571,10 @@
} else {
out << "__TClient";
}
- out << ", {" << endl << " __type = '" << classname << "'" << endl << "})" << endl;
+ out
+ <<", {" << endl
+ << " __type = '" << classname << "'" << endl
+ << "})" << endl;
// Send/Recv functions
vector<t_function*> functions = tservice->get_functions();
@@ -546,113 +585,141 @@
// Wrapper function
indent(out) << endl << "function " << classname << ":" << sig << endl;
- indent_up();
+ indent_up();
- indent(out) << "self:send_" << sig << endl << indent();
- if (!(*f_iter)->is_oneway()) {
- if (!(*f_iter)->get_returntype()->is_void()) {
- out << "return ";
+ indent(out) << "self:send_" << sig << endl << indent();
+ if (!(*f_iter)->is_oneway()) {
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ out << "return ";
+ }
+ out << "self:recv_" << sig << endl;
}
- out << "self:recv_" << sig << endl;
- }
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
// Send function
indent(out) << endl << "function " << classname << ":send_" << sig << endl;
- indent_up();
+ indent_up();
- indent(out) << "self.oprot:writeMessageBegin('" << funcname << "', "
- << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
- << ", self._seqid)" << endl;
- indent(out) << "local args = " << funcname << "_args:new{}" << endl;
+ indent(out) << "self.oprot:writeMessageBegin('" << funcname << "', "
+ << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
+ << ", self._seqid)" << endl;
+ indent(out) << "local args = " << funcname << "_args:new{}" << endl;
- // Set the args
- const vector<t_field*>& args = (*f_iter)->get_arglist()->get_members();
- vector<t_field*>::const_iterator fld_iter;
- for (fld_iter = args.begin(); fld_iter != args.end(); ++fld_iter) {
- std::string argname = (*fld_iter)->get_name();
- indent(out) << "args." << argname << " = " << argname << endl;
- }
+ // Set the args
+ const vector<t_field*>& args = (*f_iter)->get_arglist()->get_members();
+ vector<t_field*>::const_iterator fld_iter;
+ for (fld_iter = args.begin(); fld_iter != args.end(); ++fld_iter) {
+ std::string argname = (*fld_iter)->get_name();
+ indent(out) << "args." << argname << " = " << argname << endl;
+ }
- indent(out) << "args:write(self.oprot)" << endl;
- indent(out) << "self.oprot:writeMessageEnd()" << endl;
- indent(out) << "self.oprot.trans:flush()" << endl;
+ indent(out) << "args:write(self.oprot)" << endl;
+ indent(out) << "self.oprot:writeMessageEnd()" << endl;
+ indent(out) << "self.oprot.trans:flush()" << endl;
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
// Recv function
if (!(*f_iter)->is_oneway()) {
- indent(out) << endl << "function " << classname << ":recv_" << sig << endl;
- indent_up();
+ indent(out)
+ << endl << "function " << classname << ":recv_" << sig << endl;
+ indent_up();
- out << indent() << "local fname, mtype, rseqid = self.iprot:"
- << "readMessageBegin()" << endl << indent() << "if mtype == TMessageType.EXCEPTION then"
- << endl << indent() << " local x = TApplicationException:new{}" << endl << indent()
- << " x:read(self.iprot)" << endl << indent() << " self.iprot:readMessageEnd()" << endl
- << indent() << " error(x)" << endl << indent() << "end" << endl << indent()
- << "local result = " << funcname << "_result:new{}" << endl << indent()
- << "result:read(self.iprot)" << endl << indent() << "self.iprot:readMessageEnd()" << endl;
+ out <<
+ indent() << "local fname, mtype, rseqid = self.iprot:"
+ << "readMessageBegin()"<< endl <<
+ indent() << "if mtype == TMessageType.EXCEPTION then" << endl <<
+ indent() << " local x = TApplicationException:new{}" << endl <<
+ indent() << " x:read(self.iprot)" << endl <<
+ indent() << " self.iprot:readMessageEnd()" << endl <<
+ indent() << " error(x)" << endl <<
+ indent() << "end" << endl <<
+ indent() << "local result = " << funcname << "_result:new{}"
+ << endl <<
+ indent() << "result:read(self.iprot)" << endl <<
+ indent() << "self.iprot:readMessageEnd()" << endl;
- // Return the result if it's not a void function
- if (!(*f_iter)->get_returntype()->is_void()) {
- out << indent() << "if result.success then" << endl << indent() << " return result.success"
- << endl;
+ // Return the result if it's not a void function
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ out <<
+ indent() << "if result.success then" << endl <<
+ indent() << " return result.success" << endl;
- // Throw custom exceptions
- const std::vector<t_field*>& xf = (*f_iter)->get_xceptions()->get_members();
- vector<t_field*>::const_iterator x_iter;
- for (x_iter = xf.begin(); x_iter != xf.end(); ++x_iter) {
- out << indent() << "elseif result." << (*x_iter)->get_name() << " then" << endl
- << indent() << " error(result." << (*x_iter)->get_name() << ")" << endl;
+ // Throw custom exceptions
+ const std::vector<t_field*>& xf =
+ (*f_iter)->get_xceptions()->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xf.begin(); x_iter != xf.end(); ++x_iter) {
+ out <<
+ indent() << "elseif result." << (*x_iter)->get_name() << " then"
+ << endl <<
+ indent() << " error(result." << (*x_iter)->get_name() << ")"
+ << endl;
+ }
+
+ out <<
+ indent() << "end" << endl <<
+ indent() << "error(TApplicationException:new{errorCode = "
+ << "TApplicationException.MISSING_RESULT})" << endl;
}
- out << indent() << "end" << endl << indent()
- << "error(TApplicationException:new{errorCode = "
- << "TApplicationException.MISSING_RESULT})" << endl;
- }
-
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
}
}
}
-void t_lua_generator::generate_service_processor(ofstream& out, t_service* tservice) {
+void t_lua_generator::generate_service_processor(ofstream &out,
+ t_service* tservice) {
string classname = tservice->get_name() + "Processor";
t_service* extends_s = tservice->get_extends();
// Define processor table
- out << endl << classname << " = __TObject.new(";
+ out << endl
+ << classname << " = __TObject.new(";
if (extends_s != NULL) {
out << extends_s << "Processor" << endl;
} else {
out << "__TProcessor" << endl;
}
- out << ", {" << endl << " __type = '" << classname << "'" << endl << "})" << endl;
+ out
+ << ", {" << endl
+ << " __type = '" << classname << "'" << endl
+ << "})" << endl;
// Process function
- indent(out) << endl << "function " << classname << ":process(iprot, oprot, server_ctx)" << endl;
- indent_up();
+ indent(out) << endl << "function " << classname
+ << ":process(iprot, oprot, server_ctx)" << endl;
+ indent_up();
- indent(out) << "local name, mtype, seqid = iprot:readMessageBegin()" << endl;
- indent(out) << "local func_name = 'process_' .. name" << endl;
- indent(out) << "if not self[func_name] or ttype(self[func_name]) ~= 'function' then";
- indent_up();
- out << endl << indent() << "iprot:skip(TType.STRUCT)" << endl << indent()
- << "iprot:readMessageEnd()" << endl << indent() << "x = TApplicationException:new{" << endl
- << indent() << " errorCode = TApplicationException.UNKNOWN_METHOD" << endl << indent() << "}"
- << endl << indent() << "oprot:writeMessageBegin(name, TMessageType.EXCEPTION, "
- << "seqid)" << endl << indent() << "x:write(oprot)" << endl << indent()
- << "oprot:writeMessageEnd()" << endl << indent() << "oprot.trans:flush()" << endl;
- indent_down();
- indent(out) << "else" << endl << indent()
- << " self[func_name](self, seqid, iprot, oprot, server_ctx)" << endl << indent()
- << "end" << endl;
+ indent(out)
+ << "local name, mtype, seqid = iprot:readMessageBegin()" << endl;
+ indent(out)
+ << "local func_name = 'process_' .. name" << endl;
+ indent(out)
+ << "if not self[func_name] or ttype(self[func_name]) ~= 'function' then";
+ indent_up();
+ out << endl <<
+ indent() << "iprot:skip(TType.STRUCT)" << endl <<
+ indent() << "iprot:readMessageEnd()" << endl <<
+ indent() << "x = TApplicationException:new{" << endl <<
+ indent() << " errorCode = TApplicationException.UNKNOWN_METHOD" << endl
+ << indent() << "}" << endl <<
+ indent() << "oprot:writeMessageBegin(name, TMessageType.EXCEPTION, "
+ << "seqid)" << endl <<
+ indent() << "x:write(oprot)" << endl <<
+ indent() << "oprot:writeMessageEnd()" << endl <<
+ indent() << "oprot.trans:flush()" << endl;
+ indent_down();
+ indent(out) << "else" << endl <<
+ indent() << " self[func_name](self, seqid, iprot, oprot, server_ctx)"
+ << endl
+ << indent() << "end" << endl;
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
// Generate the process subfunctions
@@ -663,7 +730,7 @@
}
}
-void t_lua_generator::generate_process_function(ofstream& out,
+void t_lua_generator::generate_process_function(ofstream &out,
t_service* tservice,
t_function* tfunction) {
string classname = tservice->get_name() + "Processor";
@@ -672,49 +739,62 @@
string fn_name = tfunction->get_name();
indent(out) << endl << "function " << classname << ":process_" << fn_name
- << "(seqid, iprot, oprot, server_ctx)" << endl;
- indent_up();
+ << "(seqid, iprot, oprot, server_ctx)" << endl;
+ indent_up();
- // Read the request
- out << indent() << "local args = " << argsname << ":new{}" << endl << indent()
- << "local reply_type = TMessageType.REPLY" << endl << indent() << "args:read(iprot)" << endl
- << indent() << "iprot:readMessageEnd()" << endl << indent() << "local result = " << resultname
- << ":new{}" << endl << indent() << "local status, res = pcall(self.handler." << fn_name
- << ", self.handler";
+ // Read the request
+ out <<
+ indent() << "local args = " << argsname << ":new{}" << endl <<
+ indent() << "local reply_type = TMessageType.REPLY" << endl <<
+ indent() << "args:read(iprot)" << endl <<
+ indent() << "iprot:readMessageEnd()" << endl <<
+ indent() << "local result = " << resultname << ":new{}" << endl <<
+ indent() << "local status, res = pcall(self.handler." << fn_name
+ << ", self.handler";
- // Print arguments
- t_struct* args = tfunction->get_arglist();
- if (args->get_members().size() > 0) {
- out << ", " << argument_list(args, "args.");
- }
-
- // Check for errors
- out << ")" << endl << indent() << "if not status then" << endl << indent()
- << " reply_type = TMessageType.EXCEPTION" << endl << indent()
- << " result = TApplicationException:new{message = res}" << endl;
-
- // Handle custom exceptions
- const std::vector<t_field*>& xf = tfunction->get_xceptions()->get_members();
- if (xf.size() > 0) {
- vector<t_field*>::const_iterator x_iter;
- for (x_iter = xf.begin(); x_iter != xf.end(); ++x_iter) {
- out << indent() << "elseif ttype(res) == '" << (*x_iter)->get_type()->get_name() << "' then"
- << endl << indent() << " result." << (*x_iter)->get_name() << " = res" << endl;
+ // Print arguments
+ t_struct *args = tfunction->get_arglist();
+ if (args->get_members().size() > 0) {
+ out << ", " << argument_list(args, "args.");
}
- }
- // Set the result and write the reply
- out << indent() << "else" << endl << indent() << " result.success = res" << endl << indent()
- << "end" << endl << indent() << "oprot:writeMessageBegin('" << fn_name << "', reply_type, "
- << "seqid)" << endl << indent() << "result:write(oprot)" << endl << indent()
- << "oprot:writeMessageEnd()" << endl << indent() << "oprot.trans:flush()" << endl;
+ // Check for errors
+ out << ")" << endl <<
+ indent() << "if not status then" << endl <<
+ indent() << " reply_type = TMessageType.EXCEPTION" << endl <<
+ indent() << " result = TApplicationException:new{message = res}"
+ << endl;
- indent_down();
+ // Handle custom exceptions
+ const std::vector<t_field*>& xf = tfunction->get_xceptions()->get_members();
+ if (xf.size() > 0) {
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xf.begin(); x_iter != xf.end(); ++x_iter) {
+ out <<
+ indent() << "elseif ttype(res) == '"
+ << (*x_iter)->get_type()->get_name() << "' then" << endl <<
+ indent() << " result." << (*x_iter)->get_name() << " = res" << endl;
+ }
+ }
+
+ // Set the result and write the reply
+ out <<
+ indent() << "else" << endl <<
+ indent() << " result.success = res" << endl <<
+ indent() << "end" << endl <<
+ indent() << "oprot:writeMessageBegin('" << fn_name << "', reply_type, "
+ << "seqid)" << endl <<
+ indent() << "result:write(oprot)" << endl <<
+ indent() << "oprot:writeMessageEnd()" << endl <<
+ indent() << "oprot.trans:flush()" << endl;
+
+ indent_down();
indent(out) << "end" << endl;
}
// Service helpers
-void t_lua_generator::generate_service_helpers(ofstream& out, t_service* tservice) {
+void t_lua_generator::generate_service_helpers(ofstream &out,
+ t_service* tservice) {
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
@@ -726,7 +806,8 @@
}
}
-void t_lua_generator::generate_function_helpers(ofstream& out, t_function* tfunction) {
+void t_lua_generator::generate_function_helpers(ofstream &out,
+ t_function *tfunction) {
if (!tfunction->is_oneway()) {
t_struct result(program_, tfunction->get_name() + "_result");
t_field success(tfunction->get_returntype(), "success", 0);
@@ -747,11 +828,14 @@
/**
* Deserialize (Read)
*/
-void t_lua_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_lua_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
@@ -761,13 +845,15 @@
} else if (type->is_container()) {
generate_deserialize_container(out, type, name);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << name << " = iprot:";
+ indent(out) <<
+ name << " = 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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
out << "readString()";
@@ -791,7 +877,8 @@
out << "readDouble()";
break;
default:
- throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
+ throw "compiler error: no PHP name for base type " +
+ t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "readI32()";
@@ -800,17 +887,21 @@
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type->get_name().c_str());
+ tfield->get_name().c_str(), type->get_name().c_str());
}
}
-void t_lua_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- indent(out) << prefix << " = " << tstruct->get_name() << ":new{}" << endl << indent() << prefix
- << ":read(iprot)" << endl;
+void t_lua_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ indent(out)
+ << prefix << " = " << tstruct->get_name() << ":new{}" << endl
+ << indent() << prefix << ":read(iprot)" << endl;
}
-void t_lua_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_lua_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
string size = tmp("_size");
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
@@ -824,27 +915,29 @@
// Declare variables, read header
indent(out) << prefix << " = {}" << endl;
if (ttype->is_map()) {
- indent(out) << "local " << ktype << ", " << vtype << ", " << size << " = iprot:readMapBegin() "
- << endl;
+ indent(out) << "local " << ktype << ", " << vtype << ", " << size
+ << " = iprot:readMapBegin() " << endl;
} else if (ttype->is_set()) {
- indent(out) << "local " << etype << ", " << size << " = iprot:readSetBegin()" << endl;
+ indent(out) << "local " << etype << ", " << size
+ << " = iprot:readSetBegin()" << endl;
} else if (ttype->is_list()) {
- indent(out) << "local " << etype << ", " << size << " = iprot:readListBegin()" << endl;
+ indent(out) << "local " << etype << ", " << size
+ << " = iprot:readListBegin()" << endl;
}
// Deserialize
indent(out) << "for _i=1," << size << " do" << endl;
- indent_up();
+ indent_up();
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- indent_down();
+ indent_down();
indent(out) << "end" << endl;
// Read container end
@@ -857,7 +950,9 @@
}
}
-void t_lua_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_lua_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
// A map is represented by a table indexable by any lua type
string key = tmp("_key");
string val = tmp("_val");
@@ -870,17 +965,20 @@
indent(out) << prefix << "[" << key << "] = " << val << endl;
}
-void t_lua_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_lua_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
// A set is represented by a table indexed by the value
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
generate_deserialize_field(out, &felem);
- indent(out) << prefix << "[" << elem << "] = " << elem << endl;
+ indent(out) <<
+ prefix << "[" << elem << "] = " << elem << endl;
}
-void t_lua_generator::generate_deserialize_list_element(ofstream& out,
+void t_lua_generator::generate_deserialize_list_element(ofstream &out,
t_list* tlist,
string prefix) {
// A list is represented by a table indexed by integer values
@@ -896,7 +994,9 @@
/**
* Serialize (Write)
*/
-void t_lua_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_lua_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
string name = prefix + tfield->get_name();
@@ -916,7 +1016,8 @@
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
out << "writeString(" << name << ")";
@@ -940,7 +1041,8 @@
out << "writeDouble(" << name << ")";
break;
default:
- throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
+ throw "compiler error: no PHP name for base type " +
+ t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
out << "writeI32(" << name << ")";
@@ -953,48 +1055,61 @@
}
}
-void t_lua_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
+void t_lua_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
indent(out) << prefix << ":write(oprot)" << endl;
}
-void t_lua_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_lua_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
// Begin writing
if (ttype->is_map()) {
- indent(out) << "oprot:writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "string.len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot:writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "string.len(" << prefix << "))" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot:writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", "
- << "string.len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot:writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "string.len(" << prefix << "))" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot:writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ", "
- << "string.len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot:writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "string.len(" << prefix << "))" << endl;
}
// Serialize
if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
- indent(out) << "for " << kiter << "," << viter << " in pairs(" << prefix << ") do" << endl;
- indent_up();
- generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
- indent_down();
+ indent(out)
+ << "for " << kiter << "," << viter << " in pairs(" << prefix << ") do"
+ << endl;
+ indent_up();
+ generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
+ indent_down();
indent(out) << "end" << endl;
} else if (ttype->is_set()) {
string iter = tmp("iter");
- indent(out) << "for " << iter << ",_ in pairs(" << prefix << ") do" << endl;
- indent_up();
- generate_serialize_set_element(out, (t_set*)ttype, iter);
- indent_down();
+ indent(out) <<
+ "for " << iter << ",_ in pairs(" << prefix << ") do" << endl;
+ indent_up();
+ generate_serialize_set_element(out, (t_set*)ttype, iter);
+ indent_down();
indent(out) << "end" << endl;
} else if (ttype->is_list()) {
string iter = tmp("iter");
- indent(out) << "for _," << iter << " in ipairs(" << prefix << ") do" << endl;
- indent_up();
- generate_serialize_list_element(out, (t_list*)ttype, iter);
- indent_down();
+ indent(out) <<
+ "for _," << iter << " in ipairs(" << prefix << ") do" << endl;
+ indent_up();
+ generate_serialize_list_element(out, (t_list*)ttype, iter);
+ indent_down();
indent(out) << "end" << endl;
}
@@ -1008,7 +1123,7 @@
}
}
-void t_lua_generator::generate_serialize_map_element(ofstream& out,
+void t_lua_generator::generate_serialize_map_element(ofstream &out,
t_map* tmap,
string kiter,
string viter) {
@@ -1019,12 +1134,16 @@
generate_serialize_field(out, &vfield, "");
}
-void t_lua_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_lua_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
-void t_lua_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_lua_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -1048,9 +1167,11 @@
return real_module + "_";
}
-string t_lua_generator::function_signature(t_function* tfunction, string prefix) {
- (void)prefix;
- std::string ret = tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ")";
+string t_lua_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ (void) prefix;
+ std::string ret = tfunction->get_name() + "(" +
+ argument_list(tfunction->get_arglist()) + ")";
return ret;
}
@@ -1074,22 +1195,22 @@
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 "NO T_VOID CONSTRUCT";
- case t_base_type::TYPE_STRING:
- return "TType.STRING";
- case t_base_type::TYPE_BOOL:
- return "TType.BOOL";
- case t_base_type::TYPE_BYTE:
- return "TType.BYTE";
- case t_base_type::TYPE_I16:
- return "TType.I16";
- case t_base_type::TYPE_I32:
- return "TType.I32";
- case t_base_type::TYPE_I64:
- return "TType.I64";
- case t_base_type::TYPE_DOUBLE:
- return "TType.DOUBLE";
+ case t_base_type::TYPE_VOID:
+ throw "NO T_VOID CONSTRUCT";
+ case t_base_type::TYPE_STRING:
+ return "TType.STRING";
+ case t_base_type::TYPE_BOOL:
+ return "TType.BOOL";
+ case t_base_type::TYPE_BYTE:
+ return "TType.BYTE";
+ case t_base_type::TYPE_I16:
+ return "TType.I16";
+ case t_base_type::TYPE_I32:
+ return "TType.I32";
+ case t_base_type::TYPE_I64:
+ return "TType.I64";
+ case t_base_type::TYPE_DOUBLE:
+ return "TType.DOUBLE";
}
} else if (type->is_enum()) {
return "TType.I32";
diff --git a/compiler/cpp/src/generate/t_ocaml_generator.cc b/compiler/cpp/src/generate/t_ocaml_generator.cc
index 75bc12d..6607f6e 100644
--- a/compiler/cpp/src/generate/t_ocaml_generator.cc
+++ b/compiler/cpp/src/generate/t_ocaml_generator.cc
@@ -38,20 +38,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* OCaml code generator.
*
*/
class t_ocaml_generator : public t_oop_generator {
-public:
- t_ocaml_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_ocaml_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-ocaml";
}
@@ -65,77 +67,92 @@
/**
* Program-level generation functions
*/
- void generate_program();
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_program ();
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
- bool struct_member_persistent(t_field* tmember);
- bool struct_member_omitable(t_field* tmember);
- bool struct_member_default_cheaply_comparable(t_field* tmember);
- std::string struct_member_copy_of(t_type* type, string what);
+ bool struct_member_persistent(t_field *tmember);
+ bool struct_member_omitable(t_field *tmember);
+ bool struct_member_default_cheaply_comparable(t_field *tmember);
+ std::string struct_member_copy_of(t_type *type, string what);
/**
* Struct generation code
*/
void generate_ocaml_struct(t_struct* tstruct, bool is_exception);
- void generate_ocaml_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false);
+ void generate_ocaml_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
void generate_ocaml_struct_member(std::ofstream& out, string tname, t_field* tmember);
void generate_ocaml_struct_sig(std::ofstream& out, t_struct* tstruct, bool is_exception);
void generate_ocaml_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_ocaml_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_ocaml_function_helpers(t_function* tfunction);
- void generate_ocaml_method_copy(std::ofstream& out, const vector<t_field*>& members);
- void generate_ocaml_member_copy(std::ofstream& out, t_field* member);
+ void generate_ocaml_method_copy(std::ofstream& out, const vector<t_field *>& members);
+ void generate_ocaml_member_copy(std::ofstream& out, t_field *member);
/**
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out, t_field* tfield, std::string prefix);
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct);
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct);
- void generate_deserialize_container(std::ofstream& out, t_type* ttype);
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype);
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset);
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset);
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
- void generate_deserialize_type(std::ofstream& out, t_type* type);
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string name = "");
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
+ void generate_deserialize_type (std::ofstream &out,
+ t_type* type);
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string name= "");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
+
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
/**
* Helper rendering functions
@@ -144,13 +161,15 @@
std::string ocaml_autogen_comment();
std::string ocaml_imports();
std::string type_name(t_type* ttype);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
- std::string function_type(t_function* tfunc, bool method = false, bool options = false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
+ std::string function_type(t_function* tfunc, bool method=false, bool options = false);
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string render_ocaml_type(t_type* type);
-private:
+
+ private:
+
/**
* File streams
*/
@@ -161,8 +180,10 @@
std::ofstream f_types_i_;
std::ofstream f_service_i_;
+
};
+
/*
* This is necessary because we want typedefs to appear later,
* after all the types have been declared.
@@ -215,6 +236,7 @@
close_generator();
}
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -226,27 +248,38 @@
MKDIR(get_out_dir().c_str());
// Make output file
- string f_types_name = get_out_dir() + program_name_ + "_types.ml";
+ string f_types_name = get_out_dir()+program_name_+"_types.ml";
f_types_.open(f_types_name.c_str());
- string f_types_i_name = get_out_dir() + program_name_ + "_types.mli";
+ string f_types_i_name = get_out_dir()+program_name_+"_types.mli";
f_types_i_.open(f_types_i_name.c_str());
- string f_consts_name = get_out_dir() + program_name_ + "_consts.ml";
+ string f_consts_name = get_out_dir()+program_name_+"_consts.ml";
f_consts_.open(f_consts_name.c_str());
// Print header
- f_types_ << ocaml_autogen_comment() << endl << ocaml_imports() << endl;
- f_types_i_ << ocaml_autogen_comment() << endl << ocaml_imports() << endl;
- f_consts_ << ocaml_autogen_comment() << endl << ocaml_imports() << endl << "open "
- << capitalize(program_name_) << "_types" << endl;
+ f_types_ <<
+ ocaml_autogen_comment() << endl <<
+ ocaml_imports() << endl;
+ f_types_i_ <<
+ ocaml_autogen_comment() << endl <<
+ ocaml_imports() << endl;
+ f_consts_ <<
+ ocaml_autogen_comment() << endl <<
+ ocaml_imports() << endl <<
+ "open " << capitalize(program_name_)<<"_types"<< endl;
}
+
/**
* Autogen'd comment
*/
string t_ocaml_generator::ocaml_autogen_comment() {
- return std::string("(*\n") + " Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + "\n"
- + " DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING\n" + "*)\n";
+ return
+ std::string("(*\n") +
+ " Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "\n" +
+ " DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING\n" +
+ "*)\n";
}
/**
@@ -270,10 +303,10 @@
* @param ttypedef The type definition
*/
void t_ocaml_generator::generate_typedef(t_typedef* ttypedef) {
- f_types_ << indent() << "type " << decapitalize(ttypedef->get_symbolic()) << " = "
- << render_ocaml_type(ttypedef->get_type()) << endl << endl;
- f_types_i_ << indent() << "type " << decapitalize(ttypedef->get_symbolic()) << " = "
- << render_ocaml_type(ttypedef->get_type()) << endl << endl;
+ f_types_ <<
+ indent() << "type "<< decapitalize(ttypedef->get_symbolic()) << " = " << render_ocaml_type(ttypedef->get_type()) << endl << endl;
+ f_types_i_ <<
+ indent() << "type "<< decapitalize(ttypedef->get_symbolic()) << " = " << render_ocaml_type(ttypedef->get_type()) << endl << endl;
}
/**
@@ -283,10 +316,8 @@
* @param tenum The enumeration
*/
void t_ocaml_generator::generate_enum(t_enum* tenum) {
- indent(f_types_) << "module " << capitalize(tenum->get_name()) << " = " << endl << "struct"
- << endl;
- indent(f_types_i_) << "module " << capitalize(tenum->get_name()) << " : " << endl << "sig"
- << endl;
+ indent(f_types_) << "module " << capitalize(tenum->get_name()) << " = " << endl << "struct" << endl;
+ indent(f_types_i_) << "module " << capitalize(tenum->get_name()) << " : " << endl << "sig" << endl;
indent_up();
indent(f_types_) << "type t = " << endl;
indent(f_types_i_) << "type t = " << endl;
@@ -313,7 +344,7 @@
indent(f_types_) << "let of_i = function" << endl;
indent(f_types_i_) << "val of_i : Int32.t -> t" << endl;
indent_up();
- for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
string name = capitalize((*c_iter)->get_name());
indent(f_types_) << "| " << value << "l -> " << name << endl;
@@ -409,7 +440,7 @@
}
string fname = v_iter->first->get_string();
out << indent();
- out << ct << "#set_" << fname << " ";
+ out << ct <<"#set_" << fname << " ";
out << render_const_value(field_type, v_iter->second);
out << ";" << endl;
}
@@ -488,81 +519,82 @@
/**
* Generates an OCaml struct
*/
-void t_ocaml_generator::generate_ocaml_struct(t_struct* tstruct, bool is_exception) {
+void t_ocaml_generator::generate_ocaml_struct(t_struct* tstruct,
+ bool is_exception) {
generate_ocaml_struct_definition(f_types_, tstruct, is_exception);
- generate_ocaml_struct_sig(f_types_i_, tstruct, is_exception);
+ generate_ocaml_struct_sig(f_types_i_,tstruct,is_exception);
}
-void t_ocaml_generator::generate_ocaml_method_copy(ofstream& out, const vector<t_field*>& members) {
- vector<t_field*>::const_iterator m_iter;
+void t_ocaml_generator::generate_ocaml_method_copy(ofstream& out,
+ const vector<t_field *>& members) {
+ vector<t_field*>::const_iterator m_iter;
- /* Create a copy of the current object */
- indent(out) << "method copy =" << endl;
- indent_up();
- indent_up();
- indent(out) << "let _new = Oo.copy self in" << endl;
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter)
- generate_ocaml_member_copy(out, *m_iter);
+ /* Create a copy of the current object */
+ indent(out) << "method copy =" << endl;
+ indent_up(); indent_up();
+ indent(out) << "let _new = Oo.copy self in" << endl;
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter)
+ generate_ocaml_member_copy(out, *m_iter);
- indent_down();
- indent(out) << "_new" << endl;
- indent_down();
+ indent_down();
+ indent(out) << "_new" << endl;
+ indent_down();
}
-string t_ocaml_generator::struct_member_copy_of(t_type* type, string what) {
- if (type->is_struct() || type->is_xception()) {
- return what + string("#copy");
- }
- if (type->is_map()) {
- string copy_of_k = struct_member_copy_of(((t_map*)type)->get_key_type(), "k");
- string copy_of_v = struct_member_copy_of(((t_map*)type)->get_val_type(), "v");
+string t_ocaml_generator::struct_member_copy_of(t_type *type, string what) {
+ if (type->is_struct() || type->is_xception()) {
+ return what + string ("#copy");
+ } if (type->is_map()) {
+ string copy_of_k = struct_member_copy_of(((t_map *)type)->get_key_type(), "k");
+ string copy_of_v = struct_member_copy_of(((t_map *)type)->get_val_type(), "v");
- if (copy_of_k == "k" && copy_of_v == "v") {
- return string("(Hashtbl.copy ") + what + string(")");
- } else {
- return string(
- "((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v "
- "-> Hashtbl.add nh ") + copy_of_k + string(" ") + copy_of_v + string(") oh; nh) ")
- + what + ")";
- }
- }
- if (type->is_set()) {
- string copy_of = struct_member_copy_of(((t_set*)type)->get_elem_type(), "k");
+ if(copy_of_k == "k" && copy_of_v == "v") {
+ return string ("(Hashtbl.copy ") + what + string(")");
+ } else {
+ return string ("((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v -> Hashtbl.add nh ")
+ + copy_of_k + string(" ") + copy_of_v
+ + string(") oh; nh) ")
+ + what + ")";
+ }
+ } if (type->is_set()) {
+ string copy_of = struct_member_copy_of(((t_set *)type)->get_elem_type(), "k");
- if (copy_of == "k") {
- return string("(Hashtbl.copy ") + what + string(")");
- } else {
- return string(
- "((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v "
- "-> Hashtbl.add nh ") + copy_of + string(" true") + string(") oh; nh) ") + what
- + ")";
+ if(copy_of == "k") {
+ return string ("(Hashtbl.copy ") + what + string(")");
+ } else {
+ return string ("((fun oh -> let nh = Hashtbl.create (Hashtbl.length oh) in Hashtbl.iter (fun k v -> Hashtbl.add nh ")
+ + copy_of + string(" true")
+ + string(") oh; nh) ")
+ + what + ")";
+ }
+ } if (type->is_list()) {
+ string copy_of = struct_member_copy_of(((t_list *)type)->get_elem_type(), "x");
+ if(copy_of != "x") {
+ return string("(List.map (fun x -> ")
+ + copy_of + string (") ")
+ + what + string(")");
+ } else {
+ return what;
+ }
}
- }
- if (type->is_list()) {
- string copy_of = struct_member_copy_of(((t_list*)type)->get_elem_type(), "x");
- if (copy_of != "x") {
- return string("(List.map (fun x -> ") + copy_of + string(") ") + what + string(")");
- } else {
- return what;
- }
- }
- return what;
+ return what;
}
-void t_ocaml_generator::generate_ocaml_member_copy(ofstream& out, t_field* tmember) {
- string mname = decapitalize(tmember->get_name());
- t_type* type = get_true_type(tmember->get_type());
+void t_ocaml_generator::generate_ocaml_member_copy(ofstream& out,
+ t_field *tmember) {
+ string mname = decapitalize(tmember->get_name());
+ t_type* type = get_true_type(tmember->get_type());
- string grab_field = string("self#grab_") + mname;
- string copy_of = struct_member_copy_of(type, grab_field);
- if (copy_of != grab_field) {
- indent(out);
- if (!struct_member_persistent(tmember)) {
- out << "if _" << mname << " <> None then" << endl;
- indent(out) << " ";
+ string grab_field = string("self#grab_") + mname;
+ string copy_of = struct_member_copy_of(type, grab_field);
+ if(copy_of != grab_field) {
+ indent(out);
+ if(!struct_member_persistent(tmember)) {
+ out << "if _" << mname << " <> None then" << endl;
+ indent(out) << " ";
+ }
+ out << "_new#set_" << mname << " " << copy_of << ";" << endl;
}
- out << "_new#set_" << mname << " " << copy_of << ";" << endl;
- }
}
/**
@@ -592,8 +624,8 @@
indent_down();
indent(out) << "end" << endl;
- if (is_exception) {
- indent(out) << "exception " << capitalize(tname) << " of " << tname << endl;
+ if(is_exception){
+ indent(out) << "exception " << capitalize(tname) <<" of " << tname << endl;
}
generate_ocaml_struct_reader(out, tstruct);
@@ -612,41 +644,39 @@
string mname = decapitalize(tmember->get_name());
indent(out) << "val mutable _" << mname << " : " << render_ocaml_type(tmember->get_type());
- t_const_value* val = tmember->get_value();
- if (val) {
- if (struct_member_persistent(tmember))
- out << " = " << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
+ t_const_value *val = tmember->get_value();
+ if(val) {
+ if(struct_member_persistent(tmember))
+ out << " = " << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
else
- out << " option = Some " << render_const_value(tmember->get_type(), tmember->get_value())
- << endl;
+ out << " option = Some " << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
} else {
// assert(!struct_member_persistent(tmember))
out << " option = None" << endl;
}
- if (struct_member_persistent(tmember)) {
- indent(out) << "method get_" << mname << " = Some _" << mname << endl;
- indent(out) << "method grab_" << mname << " = _" << mname << endl;
- indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- " << x << endl;
+ if(struct_member_persistent(tmember)) {
+ indent(out) << "method get_" << mname << " = Some _" << mname << endl;
+ indent(out) << "method grab_" << mname << " = _" << mname << endl;
+ indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- " << x << endl;
} else {
- indent(out) << "method get_" << mname << " = _" << mname << endl;
- indent(out) << "method grab_" << mname << " = match _" << mname
- << " with None->raise (Field_empty \"" << tname << "." << mname << "\") | Some "
- << x << " -> " << x << endl;
- indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- Some " << x
- << endl;
- indent(out) << "method unset_" << mname << " = _" << mname << " <- None" << endl;
+ indent(out) << "method get_" << mname << " = _" << mname << endl;
+ indent(out) << "method grab_" << mname << " = match _"<<mname<<" with None->raise (Field_empty \""<<tname<<"."<<mname<<"\") | Some " << x <<" -> " << x << endl;
+ indent(out) << "method set_" << mname << " " << x << " = _" << mname << " <- Some " << x << endl;
+ indent(out) << "method unset_" << mname << " = _" << mname << " <- None" << endl;
}
indent(out) << "method reset_" << mname << " = _" << mname << " <- ";
- if (val) {
- if (struct_member_persistent(tmember))
- out << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
+ if(val) {
+ if(struct_member_persistent(tmember))
+ out << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
else
- out << "Some " << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
+ out << "Some " << render_const_value(tmember->get_type(), tmember->get_value()) << endl;
} else {
out << "None" << endl;
}
+
+
}
/**
@@ -654,8 +684,8 @@
*
* @param tmember Member definition
*/
-bool t_ocaml_generator::struct_member_persistent(t_field* tmember) {
- t_const_value* val = tmember->get_value();
+bool t_ocaml_generator::struct_member_persistent(t_field *tmember) {
+ t_const_value *val = tmember->get_value();
return (val ? true : false);
}
@@ -664,7 +694,7 @@
*
* @param tmember Member definition
*/
-bool t_ocaml_generator::struct_member_omitable(t_field* tmember) {
+bool t_ocaml_generator::struct_member_omitable(t_field *tmember) {
return (tmember->get_req() != t_field::T_REQUIRED);
}
@@ -674,29 +704,29 @@
*
* @param tmember Member definition
*/
-bool t_ocaml_generator::struct_member_default_cheaply_comparable(t_field* tmember) {
+bool t_ocaml_generator::struct_member_default_cheaply_comparable(t_field *tmember) {
t_type* type = get_true_type(tmember->get_type());
- t_const_value* val = tmember->get_value();
- if (!val) {
+ t_const_value *val = tmember->get_value();
+ if(!val) {
return false;
- } else if (type->is_base_type()) {
+ } else if(type->is_base_type()) {
// Base types are generally cheaply compared for structural equivalence.
- switch (((t_base_type*)type)->get_base()) {
+ switch(((t_base_type*)type)->get_base()) {
case t_base_type::TYPE_DOUBLE:
- if (val->get_double() == 0.0)
- return true;
- else
- return false;
+ if(val->get_double() == 0.0)
+ return true;
+ else
+ return false;
default:
- return true;
+ return true;
}
- } else if (type->is_list()) {
+ } else if(type->is_list()) {
// Empty lists are cheaply compared for structural equivalence.
// Is empty list?
- if (val->get_list().size() == 0)
- return true;
+ if(val->get_list().size() == 0)
+ return true;
else
- return false;
+ return false;
} else {
return false;
}
@@ -726,7 +756,7 @@
indent(out) << "method get_" << mname << " : " << type << " option" << endl;
indent(out) << "method grab_" << mname << " : " << type << endl;
indent(out) << "method set_" << mname << " : " << type << " -> unit" << endl;
- if (!struct_member_persistent(*m_iter))
+ if(!struct_member_persistent(*m_iter))
indent(out) << "method unset_" << mname << " : unit" << endl;
indent(out) << "method reset_" << mname << " : unit" << endl;
}
@@ -736,8 +766,8 @@
indent_down();
indent(out) << "end" << endl;
- if (is_exception) {
- indent(out) << "exception " << capitalize(tname) << " of " << tname << endl;
+ if(is_exception){
+ indent(out) << "exception " << capitalize(tname) <<" of " << tname << endl;
}
indent(out) << "val read_" << tname << " : Protocol.t -> " << tname << endl;
@@ -753,83 +783,96 @@
string str = tmp("_str");
string t = tmp("_t");
string id = tmp("_id");
- indent(out) << "let rec read_" << sname << " (iprot : Protocol.t) =" << endl;
+ indent(out) <<
+ "let rec read_" << sname << " (iprot : Protocol.t) =" << endl;
indent_up();
indent(out) << "let " << str << " = new " << sname << " in" << endl;
indent_up();
- indent(out) << "ignore(iprot#readStructBegin);" << endl;
+ indent(out) <<
+ "ignore(iprot#readStructBegin);" << endl;
// Loop over reading in fields
- indent(out) << "(try while true do" << endl;
+ indent(out) <<
+ "(try while true do" << endl;
indent_up();
indent_up();
// Read beginning field marker
- indent(out) << "let (_," << t << "," << id << ") = iprot#readFieldBegin in" << endl;
+ indent(out) <<
+ "let (_," << t <<","<<id<<") = iprot#readFieldBegin in" << endl;
// Check for field STOP marker and break
- indent(out) << "if " << t << " = Protocol.T_STOP then" << endl;
+ indent(out) <<
+ "if " << t <<" = Protocol.T_STOP then" << endl;
indent_up();
- indent(out) << "raise Break" << endl;
- indent_down();
- indent(out) << "else ();" << endl;
-
- indent(out) << "(match " << id << " with " << endl;
- indent_up();
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "| " << (*f_iter)->get_key() << " -> (";
- out << "if " << t << " = " << type_to_enum((*f_iter)->get_type()) << " then" << endl;
- indent_up();
- indent_up();
- generate_deserialize_field(out, *f_iter, str);
+ indent(out) <<
+ "raise Break" << endl;
indent_down();
- out << indent() << "else" << endl << indent() << " iprot#skip " << t << ")" << endl;
+ indent(out) << "else ();" << endl;
+
+ indent(out) << "(match " << id<<" with " << endl;
+ indent_up();
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) << "| " << (*f_iter)->get_key() << " -> (";
+ out << "if " << t <<" = " << type_to_enum((*f_iter)->get_type()) << " then" << endl;
+ indent_up();
+ indent_up();
+ generate_deserialize_field(out, *f_iter,str);
+ indent_down();
+ out <<
+ indent() << "else" << endl <<
+ indent() << " iprot#skip "<< t << ")" << endl;
+ indent_down();
+ }
+
+ // In the default case we skip the field
+ out <<
+ indent() << "| _ -> " << "iprot#skip "<<t<<");" << endl;
indent_down();
- }
+ // Read field end marker
+ indent(out) << "iprot#readFieldEnd;" << endl;
+ indent_down();
+ indent(out) << "done; ()" << endl;
+ indent_down();
+ indent(out) << "with Break -> ());" << endl;
- // In the default case we skip the field
- out << indent() << "| _ -> "
- << "iprot#skip " << t << ");" << endl;
- indent_down();
- // Read field end marker
- indent(out) << "iprot#readFieldEnd;" << endl;
- indent_down();
- indent(out) << "done; ()" << endl;
- indent_down();
- indent(out) << "with Break -> ());" << endl;
+ indent(out) <<
+ "iprot#readStructEnd;" << endl;
- indent(out) << "iprot#readStructEnd;" << endl;
-
- indent(out) << str << endl << endl;
- indent_down();
- indent_down();
+ indent(out) << str << endl << endl;
+ indent_down();
+ indent_down();
}
-void t_ocaml_generator::generate_ocaml_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_ocaml_generator::generate_ocaml_struct_writer(ofstream& out,
+ t_struct* tstruct) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
string str = tmp("_str");
string f = tmp("_f");
- indent(out) << "method write (oprot : Protocol.t) =" << endl;
+ indent(out) <<
+ "method write (oprot : Protocol.t) =" << endl;
indent_up();
- indent(out) << "oprot#writeStructBegin \"" << name << "\";" << endl;
+ indent(out) <<
+ "oprot#writeStructBegin \""<<name<<"\";" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- t_field* tmember = (*f_iter);
- string mname = "_" + decapitalize(tmember->get_name());
+ t_field *tmember = (*f_iter);
+ string mname = "_"+decapitalize(tmember->get_name());
string _v;
- if (struct_member_persistent(tmember)) {
+ if(struct_member_persistent(tmember)) {
- if (struct_member_omitable(tmember) && struct_member_default_cheaply_comparable(tmember)) {
+ if(struct_member_omitable(tmember)
+ && struct_member_default_cheaply_comparable(tmember)) {
_v = "_v";
// Avoid redundant encoding of members having default values.
indent(out) << "(match " << mname << " with "
- << render_const_value(tmember->get_type(), tmember->get_value()) << " -> () | "
- << _v << " -> " << endl;
+ << render_const_value(tmember->get_type(), tmember->get_value())
+ << " -> () | " << _v << " -> " << endl;
} else {
_v = mname;
indent(out) << "(" << endl;
@@ -839,19 +882,20 @@
indent(out) << "(match " << mname << " with ";
- if (struct_member_omitable(tmember)) {
+ if(struct_member_omitable(tmember)) {
out << "None -> ()";
- if (struct_member_default_cheaply_comparable(tmember)) {
+ if(struct_member_default_cheaply_comparable(tmember)) {
// Avoid redundant encoding of members having default values.
- out << " | Some " << render_const_value(tmember->get_type(), tmember->get_value())
+ out << " | Some "
+ << render_const_value(tmember->get_type(), tmember->get_value())
<< " -> ()";
}
out << " | Some _v -> " << endl;
} else {
out << endl;
- indent(out) << "| None -> raise (Field_empty \"" << type_name(tstruct) << "." << mname
- << "\")" << endl;
+ indent(out) << "| None -> raise (Field_empty \""
+ << type_name(tstruct) << "." << mname << "\")" << endl;
indent(out) << "| Some _v -> " << endl;
}
@@ -859,8 +903,9 @@
}
indent_up();
// Write field header
- indent(out) << "oprot#writeFieldBegin(\"" << tmember->get_name() << "\","
- << type_to_enum(tmember->get_type()) << "," << tmember->get_key() << ");" << endl;
+ indent(out) << "oprot#writeFieldBegin(\""<< tmember->get_name()<<"\","
+ << type_to_enum(tmember->get_type()) << ","
+ << tmember->get_key()<<");" << endl;
// Write field contents
generate_serialize_field(out, tmember, _v);
@@ -873,7 +918,9 @@
}
// Write the struct map
- out << indent() << "oprot#writeFieldStop;" << endl << indent() << "oprot#writeStructEnd" << endl;
+ out <<
+ indent() << "oprot#writeFieldStop;" << endl <<
+ indent() << "oprot#writeStructEnd" << endl;
indent_down();
}
@@ -884,13 +931,17 @@
* @param tservice The service definition
*/
void t_ocaml_generator::generate_service(t_service* tservice) {
- string f_service_name = get_out_dir() + capitalize(service_name_) + ".ml";
+ string f_service_name = get_out_dir()+capitalize(service_name_)+".ml";
f_service_.open(f_service_name.c_str());
- string f_service_i_name = get_out_dir() + capitalize(service_name_) + ".mli";
+ string f_service_i_name = get_out_dir()+capitalize(service_name_)+".mli";
f_service_i_.open(f_service_i_name.c_str());
- f_service_ << ocaml_autogen_comment() << endl << ocaml_imports() << endl;
- f_service_i_ << ocaml_autogen_comment() << endl << ocaml_imports() << endl;
+ f_service_ <<
+ ocaml_autogen_comment() << endl <<
+ ocaml_imports() << endl;
+ f_service_i_ <<
+ ocaml_autogen_comment() << endl <<
+ ocaml_imports() << endl;
/* if (tservice->get_extends() != NULL) {
f_service_ <<
@@ -899,9 +950,13 @@
"open " << capitalize(tservice->get_extends()->get_name()) << endl;
}
*/
- f_service_ << "open " << capitalize(program_name_) << "_types" << endl << endl;
+ f_service_ <<
+ "open " << capitalize(program_name_) << "_types" << endl <<
+ endl;
- f_service_i_ << "open " << capitalize(program_name_) << "_types" << endl << endl;
+ f_service_i_ <<
+ "open " << capitalize(program_name_) << "_types" << endl <<
+ endl;
// Generate the three main parts of the service
generate_service_helpers(tservice);
@@ -909,6 +964,7 @@
generate_service_client(tservice);
generate_service_server(tservice);
+
// Close service file
f_service_.close();
f_service_i_.close();
@@ -923,7 +979,8 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- indent(f_service_) << "(* HELPER FUNCTIONS AND STRUCTURES *)" << endl << endl;
+ indent(f_service_) <<
+ "(* HELPER FUNCTIONS AND STRUCTURES *)" << endl << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
@@ -959,8 +1016,10 @@
* @param tservice The service to generate a header definition for
*/
void t_ocaml_generator::generate_service_interface(t_service* tservice) {
- f_service_ << indent() << "class virtual iface =" << endl << "object (self)" << endl;
- f_service_i_ << indent() << "class virtual iface :" << endl << "object" << endl;
+ f_service_ <<
+ indent() << "class virtual iface =" << endl << "object (self)" << endl;
+ f_service_i_ <<
+ indent() << "class virtual iface :" << endl << "object" << endl;
indent_up();
@@ -973,11 +1032,11 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- string ft = function_type(*f_iter, true, true);
- f_service_ << indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : "
- << ft << endl;
- f_service_i_ << indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : "
- << ft << endl;
+ string ft = function_type(*f_iter,true,true);
+ f_service_ <<
+ indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : " << ft << endl;
+ f_service_i_ <<
+ indent() << "method virtual " << decapitalize((*f_iter)->get_name()) << " : " << ft << endl;
}
indent_down();
indent(f_service_) << "end" << endl << endl;
@@ -985,19 +1044,20 @@
}
/**
- * Generates a service client definition. Note that in OCaml, the client doesn't implement iface.
- *This is because
+ * Generates a service client definition. Note that in OCaml, the client doesn't implement iface. This is because
* The client does not (and should not have to) deal with arguments being None.
*
* @param tservice The service to generate a server for.
*/
void t_ocaml_generator::generate_service_client(t_service* tservice) {
string extends = "";
- indent(f_service_) << "class client (iprot : Protocol.t) (oprot : Protocol.t) =" << endl
- << "object (self)" << endl;
- indent(f_service_i_) << "class client : Protocol.t -> Protocol.t -> " << endl << "object" << endl;
+ indent(f_service_) <<
+ "class client (iprot : Protocol.t) (oprot : Protocol.t) =" << endl << "object (self)" << endl;
+ indent(f_service_i_) <<
+ "class client : Protocol.t -> Protocol.t -> " << endl << "object" << endl;
indent_up();
+
if (tservice->get_extends() != NULL) {
extends = type_name(tservice->get_extends());
indent(f_service_) << "inherit " << extends << ".client iprot oprot as super" << endl;
@@ -1005,6 +1065,7 @@
}
indent(f_service_) << "val mutable seqid = 0" << endl;
+
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::const_iterator f_iter;
@@ -1015,11 +1076,14 @@
string funname = (*f_iter)->get_name();
// Open function
- indent(f_service_) << "method " << function_signature(*f_iter) << " = " << endl;
- indent(f_service_i_) << "method " << decapitalize((*f_iter)->get_name()) << " : "
- << function_type(*f_iter, true, false) << endl;
+ indent(f_service_) <<
+ "method " << function_signature(*f_iter) << " = " << endl;
+ indent(f_service_i_) <<
+ "method " << decapitalize((*f_iter)->get_name()) << " : " << function_type(*f_iter,true,false) << endl;
indent_up();
- indent(f_service_) << "self#send_" << funname;
+ indent(f_service_) <<
+ "self#send_" << funname;
+
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
f_service_ << " " << decapitalize((*fld_iter)->get_name());
@@ -1028,31 +1092,37 @@
if (!(*f_iter)->is_oneway()) {
f_service_ << indent();
- f_service_ << "self#recv_" << funname << endl;
+ f_service_ <<
+ "self#recv_" << funname << endl;
}
indent_down();
- indent(f_service_) << "method private send_" << function_signature(*f_iter) << " = " << endl;
+ indent(f_service_) <<
+ "method private send_" << function_signature(*f_iter) << " = " << endl;
indent_up();
std::string argsname = decapitalize((*f_iter)->get_name() + "_args");
// Serialize the request header
- f_service_ << indent() << "oprot#writeMessageBegin (\"" << (*f_iter)->get_name() << "\", "
- << ((*f_iter)->is_oneway() ? "Protocol.ONEWAY" : "Protocol.CALL") << ", seqid);"
- << endl;
+ f_service_ <<
+ indent() << "oprot#writeMessageBegin (\"" << (*f_iter)->get_name() << "\", "
+ << ((*f_iter)->is_oneway() ? "Protocol.ONEWAY" : "Protocol.CALL")
+ << ", seqid);" << endl;
- f_service_ << indent() << "let args = new " << argsname << " in" << endl;
+ f_service_ <<
+ indent() << "let args = new " << argsname << " in" << endl;
indent_up();
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args#set_" << (*fld_iter)->get_name() << " "
- << (*fld_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "args#set_" << (*fld_iter)->get_name() << " " << (*fld_iter)->get_name() << ";" << endl;
}
// Write to the stream
- f_service_ << indent() << "args#write oprot;" << endl << indent() << "oprot#writeMessageEnd;"
- << endl << indent() << "oprot#getTransport#flush" << endl;
+ f_service_ <<
+ indent() << "args#write oprot;" << endl <<
+ indent() << "oprot#writeMessageEnd;" << endl <<
+ indent() << "oprot#getTransport#flush" << endl;
indent_down();
indent_down();
@@ -1065,21 +1135,25 @@
string("recv_") + (*f_iter)->get_name(),
&noargs);
// Open function
- f_service_ << indent() << "method private " << function_signature(&recv_function) << " ="
- << endl;
+ f_service_ <<
+ indent() << "method private " << function_signature(&recv_function) << " =" << endl;
indent_up();
// TODO(mcslee): Validate message reply here, seq ids etc.
- f_service_ << indent() << "let (fname, mtype, rseqid) = iprot#readMessageBegin in" << endl;
+ f_service_ <<
+ indent() << "let (fname, mtype, rseqid) = iprot#readMessageBegin in" << endl;
indent_up();
- f_service_ << indent() << "(if mtype = Protocol.EXCEPTION then" << endl << indent()
- << " let x = Application_Exn.read iprot in" << endl;
+ f_service_ <<
+ indent() << "(if mtype = Protocol.EXCEPTION then" << endl <<
+ indent() << " let x = Application_Exn.read iprot in" << endl;
indent_up();
- f_service_ << indent() << " (iprot#readMessageEnd;" << indent()
- << " raise (Application_Exn.E x))" << endl;
+ f_service_ <<
+ indent() << " (iprot#readMessageEnd;" <<
+ indent() << " raise (Application_Exn.E x))" << endl;
indent_down();
- f_service_ << indent() << "else ());" << endl;
+ f_service_ <<
+ indent() << "else ());" << endl;
string res = "_";
t_struct* xs = (*f_iter)->get_xceptions();
@@ -1088,32 +1162,34 @@
if (!(*f_iter)->get_returntype()->is_void() || xceptions.size() > 0) {
res = "result";
}
- f_service_ << indent() << "let " << res << " = read_" << resultname << " iprot in" << endl;
+ f_service_ <<
+ indent() << "let "<<res<<" = read_" << resultname << " iprot in" << endl;
indent_up();
- f_service_ << indent() << "iprot#readMessageEnd;" << endl;
+ f_service_ <<
+ indent() << "iprot#readMessageEnd;" << endl;
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "match result#get_success with Some v -> v | None -> (" << endl;
+ f_service_ <<
+ indent() << "match result#get_success with Some v -> v | None -> (" << endl;
indent_up();
}
+
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "(match result#get_" << (*x_iter)->get_name()
- << " with None -> () | Some _v ->" << endl;
- indent(f_service_) << " raise (" << capitalize(type_name((*x_iter)->get_type()))
- << " _v));" << endl;
+ f_service_ <<
+ indent() << "(match result#get_" << (*x_iter)->get_name() << " with None -> () | Some _v ->" << endl;
+ indent(f_service_) << " raise (" << capitalize(type_name((*x_iter)->get_type())) << " _v));" << endl;
}
// Careful, only return _result if not a void function
if ((*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "()" << endl;
+ indent(f_service_) <<
+ "()" << endl;
} else {
- f_service_
- << indent()
- << "raise (Application_Exn.E (Application_Exn.create Application_Exn.MISSING_RESULT \""
- << (*f_iter)->get_name() << " failed: unknown result\")))" << endl;
+ f_service_ <<
+ indent() << "raise (Application_Exn.E (Application_Exn.create Application_Exn.MISSING_RESULT \"" << (*f_iter)->get_name() << " failed: unknown result\")))" << endl;
indent_down();
}
@@ -1139,51 +1215,62 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
+
// Generate the header portion
- indent(f_service_) << "class processor (handler : iface) =" << endl << indent() << "object (self)"
- << endl;
- indent(f_service_i_) << "class processor : iface ->" << endl << indent() << "object" << endl;
+ indent(f_service_) <<
+ "class processor (handler : iface) =" << endl << indent() << "object (self)" << endl;
+ indent(f_service_i_) <<
+ "class processor : iface ->" << endl << indent() << "object" << endl;
indent_up();
- f_service_ << indent() << "inherit Processor.t" << endl << endl;
- f_service_i_ << indent() << "inherit Processor.t" << endl << endl;
+ f_service_ <<
+ indent() << "inherit Processor.t" << endl <<
+ endl;
+ f_service_i_ <<
+ indent() << "inherit Processor.t" << endl <<
+ endl;
string extends = "";
if (tservice->get_extends() != NULL) {
extends = type_name(tservice->get_extends());
- indent(f_service_) << "inherit " + extends + ".processor (handler :> " + extends + ".iface)"
- << endl;
+ indent(f_service_) << "inherit " + extends + ".processor (handler :> " + extends + ".iface)" << endl;
indent(f_service_i_) << "inherit " + extends + ".processor" << endl;
}
if (extends.empty()) {
indent(f_service_) << "val processMap = Hashtbl.create " << functions.size() << endl;
}
- indent(f_service_i_)
- << "val processMap : (string, int * Protocol.t * Protocol.t -> unit) Hashtbl.t" << endl;
+ indent(f_service_i_) << "val processMap : (string, int * Protocol.t * Protocol.t -> unit) Hashtbl.t" << endl;
// Generate the server implementation
- indent(f_service_) << "method process iprot oprot =" << endl;
- indent(f_service_i_) << "method process : Protocol.t -> Protocol.t -> bool" << endl;
+ indent(f_service_) <<
+ "method process iprot oprot =" << endl;
+ indent(f_service_i_) <<
+ "method process : Protocol.t -> Protocol.t -> bool" << endl;
indent_up();
- f_service_ << indent() << "let (name, typ, seqid) = iprot#readMessageBegin in" << endl;
+ f_service_ <<
+ indent() << "let (name, typ, seqid) = iprot#readMessageBegin in" << endl;
indent_up();
// TODO(mcslee): validate message
// HOT: dictionary function lookup
- f_service_ << indent() << "if Hashtbl.mem processMap name then" << endl << indent()
- << " (Hashtbl.find processMap name) (seqid, iprot, oprot)" << endl << indent()
- << "else (" << endl << indent() << " iprot#skip(Protocol.T_STRUCT);" << endl
- << indent() << " iprot#readMessageEnd;" << endl << indent()
- << " let x = Application_Exn.create Application_Exn.UNKNOWN_METHOD (\"Unknown "
- "function \"^name) in" << endl << indent()
- << " oprot#writeMessageBegin(name, Protocol.EXCEPTION, seqid);" << endl << indent()
- << " x#write oprot;" << endl << indent() << " oprot#writeMessageEnd;" << endl
- << indent() << " oprot#getTransport#flush" << endl << indent() << ");" << endl;
+ f_service_ <<
+ indent() << "if Hashtbl.mem processMap name then" << endl <<
+ indent() << " (Hashtbl.find processMap name) (seqid, iprot, oprot)" << endl <<
+ indent() << "else (" << endl <<
+ indent() << " iprot#skip(Protocol.T_STRUCT);" << endl <<
+ indent() << " iprot#readMessageEnd;" << endl <<
+ indent() << " let x = Application_Exn.create Application_Exn.UNKNOWN_METHOD (\"Unknown function \"^name) in" << endl <<
+ indent() << " oprot#writeMessageBegin(name, Protocol.EXCEPTION, seqid);" << endl <<
+ indent() << " x#write oprot;" << endl <<
+ indent() << " oprot#writeMessageEnd;" << endl <<
+ indent() << " oprot#getTransport#flush" << endl <<
+ indent() << ");" << endl;
// Read end of args field, the T_STOP, and the struct close
- f_service_ << indent() << "true" << endl;
+ f_service_ <<
+ indent() << "true" << endl;
indent_down();
indent_down();
// Generate the process subfunctions
@@ -1194,8 +1281,8 @@
indent(f_service_) << "initializer" << endl;
indent_up();
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "Hashtbl.add processMap \"" << (*f_iter)->get_name()
- << "\" self#process_" << (*f_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "Hashtbl.add processMap \"" << (*f_iter)->get_name() << "\" self#process_" << (*f_iter)->get_name() << ";" << endl;
}
indent_down();
@@ -1209,11 +1296,13 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_ocaml_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_ocaml_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open function
- indent(f_service_) << "method private process_" << tfunction->get_name()
- << " (seqid, iprot, oprot) =" << endl;
+ indent(f_service_) <<
+ "method private process_" << tfunction->get_name() <<
+ " (seqid, iprot, oprot) =" << endl;
indent_up();
string argsname = decapitalize(tfunction->get_name()) + "_args";
@@ -1225,13 +1314,15 @@
vector<t_field*>::const_iterator f_iter;
string args = "args";
- if (fields.size() == 0) {
- args = "_";
+ if(fields.size() == 0){
+ args="_";
}
- f_service_ << indent() << "let " << args << " = read_" << argsname << " iprot in" << endl;
+ f_service_ <<
+ indent() << "let "<<args<<" = read_" << argsname << " iprot in" << endl;
indent_up();
- f_service_ << indent() << "iprot#readMessageEnd;" << endl;
+ f_service_ <<
+ indent() << "iprot#readMessageEnd;" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -1239,38 +1330,45 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "let result = new " << resultname << " in" << endl;
+ f_service_ <<
+ indent() << "let result = new " << resultname << " in" << endl;
indent_up();
}
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "(try" << endl;
+ f_service_ <<
+ indent() << "(try" << endl;
indent_up();
}
+
+
+
f_service_ << indent();
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result#set_success ";
}
- f_service_ << "(handler#" << tfunction->get_name();
+ f_service_ <<
+ "(handler#" << tfunction->get_name();
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- f_service_ << " args#get_" << (*f_iter)->get_name();
+ f_service_ << " args#get_" << (*f_iter)->get_name();
}
f_service_ << ");" << endl;
+
if (xceptions.size() > 0) {
indent_down();
- indent(f_service_) << "with" << endl;
+ indent(f_service_) << "with" <<endl;
indent_up();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "| " << capitalize(type_name((*x_iter)->get_type())) << " "
- << (*x_iter)->get_name() << " -> " << endl;
+ f_service_ <<
+ indent() << "| " << capitalize(type_name((*x_iter)->get_type())) << " " << (*x_iter)->get_name() << " -> " << endl;
indent_up();
indent_up();
- if (!tfunction->is_oneway()) {
- f_service_ << indent() << "result#set_" << (*x_iter)->get_name() << " "
- << (*x_iter)->get_name() << endl;
+ if(!tfunction->is_oneway()){
+ f_service_ <<
+ indent() << "result#set_" << (*x_iter)->get_name() << " " << (*x_iter)->get_name() << endl;
} else {
indent(f_service_) << "()";
}
@@ -1281,18 +1379,22 @@
f_service_ << indent() << ");" << endl;
}
+
+
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "()" << endl;
+ f_service_ <<
+ indent() << "()" << endl;
indent_down();
indent_down();
return;
}
- f_service_ << indent() << "oprot#writeMessageBegin (\"" << tfunction->get_name()
- << "\", Protocol.REPLY, seqid);" << endl << indent() << "result#write oprot;" << endl
- << indent() << "oprot#writeMessageEnd;" << endl << indent()
- << "oprot#getTransport#flush" << endl;
+ f_service_ <<
+ indent() << "oprot#writeMessageBegin (\"" << tfunction->get_name() << "\", Protocol.REPLY, seqid);" << endl <<
+ indent() << "result#write oprot;" << endl <<
+ indent() << "oprot#writeMessageEnd;" << endl <<
+ indent() << "oprot#getTransport#flush" << endl;
// Close function
indent_down();
@@ -1303,27 +1405,34 @@
/**
* Deserializes a field of any type.
*/
-void t_ocaml_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_ocaml_generator::generate_deserialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix){
t_type* type = tfield->get_type();
+
string name = decapitalize(tfield->get_name());
- indent(out) << prefix << "#set_" << name << " ";
- generate_deserialize_type(out, type);
+ indent(out) << prefix << "#set_"<<name << " ";
+ generate_deserialize_type(out,type);
out << endl;
}
+
/**
* Deserializes a field of any type.
*/
-void t_ocaml_generator::generate_deserialize_type(ofstream& out, t_type* type) {
+void t_ocaml_generator::generate_deserialize_type(ofstream &out,
+ t_type* type){
type = get_true_type(type);
if (type->is_void()) {
throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE";
}
+
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type);
+ generate_deserialize_struct(out,
+ (t_struct*)type);
} else if (type->is_container()) {
generate_deserialize_container(out, type);
} else if (type->is_base_type()) {
@@ -1359,30 +1468,35 @@
}
} else if (type->is_enum()) {
string ename = capitalize(type->get_name());
- out << "(" << ename << ".of_i iprot#readI32)";
+ out << "(" <<ename << ".of_i iprot#readI32)";
} else {
- printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n", type->get_name().c_str());
+ printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n",
+ type->get_name().c_str());
}
}
+
/**
* Generates an unserializer for a struct, calling read()
*/
-void t_ocaml_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct) {
+void t_ocaml_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct) {
string prefix = "";
t_program* program = tstruct->get_program();
if (program != NULL && program != program_) {
- prefix = capitalize(program->get_name()) + "_types.";
+ prefix = capitalize(program->get_name()) + "_types.";
}
string name = decapitalize(tstruct->get_name());
out << "(" << prefix << "read_" << name << " iprot)";
+
}
/**
* Serialize a container by writing out the header followed by
* data and then a footer.
*/
-void t_ocaml_generator::generate_deserialize_container(ofstream& out, t_type* ttype) {
+void t_ocaml_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype) {
string size = tmp("_size");
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
@@ -1398,88 +1512,99 @@
indent_up();
// Declare variables, read header
if (ttype->is_map()) {
- indent(out) << "(let (" << ktype << "," << vtype << "," << size << ") = iprot#readMapBegin in"
- << endl;
- indent(out) << "let " << con << " = Hashtbl.create " << size << " in" << endl;
+ indent(out) << "(let ("<<ktype<<","<<vtype<<","<<size<<") = iprot#readMapBegin in" << endl;
+ indent(out) << "let "<<con<<" = Hashtbl.create "<<size<<" in" << endl;
indent_up();
- indent(out) << "for i = 1 to " << size << " do" << endl;
+ indent(out) << "for i = 1 to "<<size<<" do" <<endl;
indent_up();
indent(out) << "let _k = ";
- generate_deserialize_type(out, ((t_map*)ttype)->get_key_type());
+ generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
out << " in" << endl;
indent(out) << "let _v = ";
- generate_deserialize_type(out, ((t_map*)ttype)->get_val_type());
+ generate_deserialize_type(out,((t_map*)ttype)->get_val_type());
out << " in" << endl;
indent_up();
- indent(out) << "Hashtbl.add " << con << " _k _v" << endl;
+ indent(out) << "Hashtbl.add "<<con<< " _k _v" << endl;
indent_down();
indent_down();
- indent(out) << "done; iprot#readMapEnd; " << con << ")";
+ indent(out) << "done; iprot#readMapEnd; "<<con<<")";
indent_down();
} else if (ttype->is_set()) {
- indent(out) << "(let (" << etype << "," << size << ") = iprot#readSetBegin in" << endl;
- indent(out) << "let " << con << " = Hashtbl.create " << size << " in" << endl;
+ indent(out) << "(let ("<<etype<<","<<size<<") = iprot#readSetBegin in" << endl;
+ indent(out) << "let "<<con<<" = Hashtbl.create "<<size<<" in" << endl;
indent_up();
- indent(out) << "for i = 1 to " << size << " do" << endl;
+ indent(out) << "for i = 1 to "<<size<<" do" <<endl;
indent_up();
- indent(out) << "Hashtbl.add " << con << " ";
- generate_deserialize_type(out, ((t_set*)ttype)->get_elem_type());
+ indent(out) << "Hashtbl.add "<<con<<" ";
+ generate_deserialize_type(out,((t_set*)ttype)->get_elem_type());
out << " true" << endl;
indent_down();
- indent(out) << "done; iprot#readSetEnd; " << con << ")";
+ indent(out) << "done; iprot#readSetEnd; "<<con<<")";
indent_down();
} else if (ttype->is_list()) {
- indent(out) << "(let (" << etype << "," << size << ") = iprot#readListBegin in" << endl;
+ indent(out) << "(let ("<<etype<<","<<size<<") = iprot#readListBegin in" << endl;
indent_up();
- indent(out) << "let " << con << " = (Array.to_list (Array.init " << size << " (fun _ -> ";
- generate_deserialize_type(out, ((t_list*)ttype)->get_elem_type());
+ indent(out) << "let "<<con<<" = (Array.to_list (Array.init "<<size<<" (fun _ -> ";
+ generate_deserialize_type(out,((t_list*)ttype)->get_elem_type());
out << "))) in" << endl;
indent_up();
- indent(out) << "iprot#readListEnd; " << con << ")";
+ indent(out) << "iprot#readListEnd; "<<con<<")";
indent_down();
indent_down();
}
indent_down();
}
+
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_ocaml_generator::generate_serialize_field(ofstream& out, t_field* tfield, string name) {
+void t_ocaml_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string name) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ tfield->get_name();
}
- if (name.length() == 0) {
+ if(name.length() == 0){
name = decapitalize(tfield->get_name());
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, name);
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ name);
} else if (type->is_container()) {
- generate_serialize_container(out, type, name);
+ generate_serialize_container(out,
+ type,
+ name);
} else if (type->is_base_type() || type->is_enum()) {
- indent(out) << "oprot#";
+
+ indent(out) <<
+ "oprot#";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
out << "writeString(" << name << ")";
break;
case t_base_type::TYPE_BOOL:
out << "writeBool(" << name << ")";
- break;
+ break;
case t_base_type::TYPE_BYTE:
out << "writeByte(" << name << ")";
break;
@@ -1500,7 +1625,7 @@
}
} else if (type->is_enum()) {
string ename = capitalize(type->get_name());
- out << "writeI32(" << ename << ".to_i " << name << ")";
+ out << "writeI32("<<ename<<".to_i " << name << ")";
}
} else {
@@ -1517,55 +1642,63 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_ocaml_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
+void t_ocaml_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
indent(out) << prefix << "#write(oprot)";
}
-void t_ocaml_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_ocaml_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
if (ttype->is_map()) {
- indent(out) << "oprot#writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ",";
+ indent(out) << "oprot#writeMapBegin("<< type_to_enum(((t_map*)ttype)->get_key_type()) << ",";
out << type_to_enum(((t_map*)ttype)->get_val_type()) << ",";
out << "Hashtbl.length " << prefix << ");" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot#writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ",";
+ indent(out) <<
+ "oprot#writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ",";
out << "Hashtbl.length " << prefix << ");" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot#writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ",";
+ indent(out) <<
+ "oprot#writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type()) << ",";
out << "List.length " << prefix << ");" << endl;
}
if (ttype->is_map()) {
string kiter = tmp("_kiter");
string viter = tmp("_viter");
- indent(out) << "Hashtbl.iter (fun " << kiter << " -> fun " << viter << " -> " << endl;
+ indent(out) << "Hashtbl.iter (fun "<<kiter<<" -> fun " << viter << " -> " << endl;
indent_up();
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
indent_down();
indent(out) << ") " << prefix << ";" << endl;
} else if (ttype->is_set()) {
string iter = tmp("_iter");
- indent(out) << "Hashtbl.iter (fun " << iter << " -> fun _ -> ";
+ indent(out) << "Hashtbl.iter (fun "<<iter<<" -> fun _ -> ";
indent_up();
generate_serialize_set_element(out, (t_set*)ttype, iter);
indent_down();
indent(out) << ") " << prefix << ";" << endl;
} else if (ttype->is_list()) {
string iter = tmp("_iter");
- indent(out) << "List.iter (fun " << iter << " -> ";
+ indent(out) << "List.iter (fun "<<iter<<" -> ";
indent_up();
generate_serialize_list_element(out, (t_list*)ttype, iter);
indent_down();
- indent(out) << ") " << prefix << ";" << endl;
+ indent(out) << ") " << prefix << ";" << endl;
}
if (ttype->is_map()) {
- indent(out) << "oprot#writeMapEnd";
+ indent(out) <<
+ "oprot#writeMapEnd";
} else if (ttype->is_set()) {
- indent(out) << "oprot#writeSetEnd";
+ indent(out) <<
+ "oprot#writeSetEnd";
} else if (ttype->is_list()) {
- indent(out) << "oprot#writeListEnd";
+ indent(out) <<
+ "oprot#writeListEnd";
}
}
@@ -1573,10 +1706,10 @@
* Serializes the members of a map.
*
*/
-void t_ocaml_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string kiter,
- string viter) {
+void t_ocaml_generator::generate_serialize_map_element(ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter) {
t_field kfield(tmap->get_key_type(), kiter);
generate_serialize_field(out, &kfield);
@@ -1587,7 +1720,9 @@
/**
* Serializes the members of a set.
*/
-void t_ocaml_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_ocaml_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
@@ -1595,34 +1730,40 @@
/**
* Serializes the members of a list.
*/
-void t_ocaml_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_ocaml_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
+
+
/**
* Renders a function signature of the form 'name args'
*
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_ocaml_generator::function_signature(t_function* tfunction, string prefix) {
- return prefix + decapitalize(tfunction->get_name()) + " "
- + argument_list(tfunction->get_arglist());
+string t_ocaml_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ return
+ prefix + decapitalize(tfunction->get_name()) +
+ " " + argument_list(tfunction->get_arglist());
}
-string t_ocaml_generator::function_type(t_function* tfunc, bool method, bool options) {
- string result = "";
+string t_ocaml_generator::function_type(t_function* tfunc, bool method, bool options){
+ string result="";
const vector<t_field*>& fields = tfunc->get_arglist()->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
result += render_ocaml_type((*f_iter)->get_type());
- if (options)
+ if(options)
result += " option";
result += " -> ";
}
- if (fields.empty() && !method) {
+ if(fields.empty() && !method){
result += "unit -> ";
}
result += render_ocaml_type(tfunc->get_returntype());
@@ -1659,7 +1800,7 @@
}
string name = ttype->get_name();
- if (ttype->is_service()) {
+ if(ttype->is_service()){
name = capitalize(name);
} else {
name = decapitalize(name);
@@ -1735,22 +1876,24 @@
return "float";
}
} else if (type->is_enum()) {
- return capitalize(((t_enum*)type)->get_name()) + ".t";
+ return capitalize(((t_enum*)type)->get_name())+".t";
} else if (type->is_struct() || type->is_xception()) {
return type_name((t_struct*)type);
} else if (type->is_map()) {
t_type* ktype = ((t_map*)type)->get_key_type();
t_type* vtype = ((t_map*)type)->get_val_type();
- return "(" + render_ocaml_type(ktype) + "," + render_ocaml_type(vtype) + ") Hashtbl.t";
+ return "("+render_ocaml_type(ktype)+","+render_ocaml_type(vtype)+") Hashtbl.t";
} else if (type->is_set()) {
t_type* etype = ((t_set*)type)->get_elem_type();
- return "(" + render_ocaml_type(etype) + ",bool) Hashtbl.t";
+ return "("+render_ocaml_type(etype)+",bool) Hashtbl.t";
} else if (type->is_list()) {
t_type* etype = ((t_list*)type)->get_elem_type();
- return render_ocaml_type(etype) + " list";
+ return render_ocaml_type(etype)+" list";
}
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
THRIFT_REGISTER_GENERATOR(ocaml, "OCaml", "")
+
diff --git a/compiler/cpp/src/generate/t_oop_generator.h b/compiler/cpp/src/generate/t_oop_generator.h
index 925d108..8d27f38 100644
--- a/compiler/cpp/src/generate/t_oop_generator.h
+++ b/compiler/cpp/src/generate/t_oop_generator.h
@@ -35,8 +35,9 @@
*
*/
class t_oop_generator : public t_generator {
-public:
- t_oop_generator(t_program* program) : t_generator(program) {}
+ public:
+ t_oop_generator(t_program* program) :
+ t_generator(program) {}
/**
* Scoping, using curly braces!
@@ -53,7 +54,7 @@
}
std::string upcase_string(std::string original) {
- std::transform(original.begin(), original.end(), original.begin(), (int (*)(int))toupper);
+ std::transform(original.begin(), original.end(), original.begin(), (int(*)(int)) toupper);
return original;
}
@@ -64,13 +65,18 @@
* @return C-style comment mentioning that this file is autogenerated.
*/
virtual std::string autogen_comment() {
- return std::string("/**\n") + " * " + autogen_summary() + "\n" + " *\n"
- + " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
- + " * @generated\n" + " */\n";
+ return
+ std::string("/**\n") +
+ " * " + autogen_summary() + "\n" +
+ " *\n" +
+ " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ " * @generated\n" +
+ " */\n";
}
virtual std::string autogen_summary() {
- return std::string("Autogenerated by Thrift Compiler (") + THRIFT_VERSION + ")";
+ return
+ std::string("Autogenerated by Thrift Compiler (") + THRIFT_VERSION + ")";
}
virtual std::string get_enum_class_name(t_type* type) {
@@ -82,14 +88,17 @@
return package + type->get_name();
}
- virtual void generate_java_docstring_comment(std::ofstream& out, std::string contents) {
- generate_docstring_comment(out, "/**\n", " * ", contents, " */\n");
+ virtual void generate_java_docstring_comment(std::ofstream &out, std::string contents) {
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", contents,
+ " */\n");
}
- virtual void generate_java_doc(std::ofstream& out, t_field* field) {
+ virtual void generate_java_doc(std::ofstream &out,
+ t_field* field) {
if (field->get_type()->is_enum()) {
- std::string combined_message = field->get_doc() + "\n@see "
- + get_enum_class_name(field->get_type());
+ std::string combined_message = field->get_doc() + "\n@see " + get_enum_class_name(field->get_type());
generate_java_docstring_comment(out, combined_message);
} else {
generate_java_doc(out, (t_doc*)field);
@@ -99,7 +108,8 @@
/**
* Emits a JavaDoc comment if the provided object has a doc in Thrift
*/
- virtual void generate_java_doc(std::ofstream& out, t_doc* tdoc) {
+ virtual void generate_java_doc(std::ofstream &out,
+ t_doc* tdoc) {
if (tdoc->has_doc()) {
generate_java_docstring_comment(out, tdoc->get_doc());
}
@@ -108,7 +118,8 @@
/**
* Emits a JavaDoc comment if the provided function object has a doc in Thrift
*/
- virtual void generate_java_doc(std::ofstream& out, t_function* tfunction) {
+ virtual void generate_java_doc(std::ofstream &out,
+ t_function* tfunction) {
if (tfunction->has_doc()) {
std::stringstream ss;
ss << tfunction->get_doc();
@@ -121,9 +132,13 @@
ss << " " << p->get_doc();
}
}
- generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
}
}
};
#endif
+
diff --git a/compiler/cpp/src/generate/t_perl_generator.cc b/compiler/cpp/src/generate/t_perl_generator.cc
index 6c823c0..5a5c080 100644
--- a/compiler/cpp/src/generate/t_perl_generator.cc
+++ b/compiler/cpp/src/generate/t_perl_generator.cc
@@ -37,20 +37,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* PERL code generator.
*
*/
class t_perl_generator : public t_oop_generator {
-public:
- t_perl_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_perl_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-perl";
escape_['$'] = "\\$";
escape_['@'] = "\\@";
@@ -67,12 +69,12 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
@@ -81,9 +83,7 @@
*/
void generate_perl_struct(t_struct* tstruct, bool is_exception);
- void generate_perl_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false);
+ void generate_perl_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
void generate_perl_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_perl_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_perl_function_helpers(t_function* tfunction);
@@ -92,62 +92,84 @@
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_rest(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_processor(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_rest (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_processor (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inclass = false);
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
/**
* Helper rendering functions
*/
std::string perl_includes();
- std::string declare_field(t_field* tfield, bool init = false, bool obj = false);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string autogen_comment() {
- return std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n";
+ return
+ std::string("#\n") +
+ "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "#\n" +
+ "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "#\n";
}
void perl_namespace_dirs(t_program* p, std::list<std::string>& dirs) {
@@ -157,7 +179,7 @@
if (ns.size() > 0) {
while ((loc = ns.find(".")) != std::string::npos) {
dirs.push_back(ns.substr(0, loc));
- ns = ns.substr(loc + 1);
+ ns = ns.substr(loc+1);
}
}
@@ -175,7 +197,7 @@
while ((loc = ns.find(".")) != std::string::npos) {
result += ns.substr(0, loc);
result += "::";
- ns = ns.substr(loc + 1);
+ ns = ns.substr(loc+1);
}
if (ns.size() > 0) {
@@ -197,7 +219,8 @@
return outdir;
}
-private:
+ private:
+
/**
* File streams
*/
@@ -205,8 +228,10 @@
std::ofstream f_consts_;
std::ofstream f_helpers_;
std::ofstream f_service_;
+
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -222,22 +247,27 @@
perl_namespace_dirs(program_, dirs);
std::list<std::string>::iterator it;
for (it = dirs.begin(); it != dirs.end(); it++) {
- outdir += *it + "/";
- MKDIR(outdir.c_str());
+ outdir += *it + "/";
+ MKDIR(outdir.c_str());
}
// Make output file
- string f_types_name = outdir + "Types.pm";
+ string f_types_name = outdir+"Types.pm";
f_types_.open(f_types_name.c_str());
- string f_consts_name = outdir + "Constants.pm";
+ string f_consts_name = outdir+"Constants.pm";
f_consts_.open(f_consts_name.c_str());
// Print header
- f_types_ << autogen_comment() << perl_includes();
+ f_types_ <<
+ autogen_comment() <<
+ perl_includes();
// Print header
- f_consts_ << autogen_comment() << "package " << perl_namespace(program_) << "Constants;" << endl
- << perl_includes() << endl;
+ f_consts_ <<
+ autogen_comment() <<
+ "package "<< perl_namespace(program_) <<"Constants;"<<endl<<
+ perl_includes() <<
+ endl;
}
/**
@@ -246,7 +276,7 @@
string t_perl_generator::perl_includes() {
string inc;
- inc = "require 5.6.0;\n";
+ inc = "require 5.6.0;\n";
inc += "use strict;\n";
inc += "use warnings;\n";
inc += "use Thrift;\n\n";
@@ -272,7 +302,7 @@
* @param ttypedef The type definition
*/
void t_perl_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -282,13 +312,13 @@
* @param tenum The enumeration
*/
void t_perl_generator::generate_enum(t_enum* tenum) {
- f_types_ << "package " << perl_namespace(program_) << tenum->get_name() << ";" << endl;
+ f_types_ << "package " << perl_namespace(program_) <<tenum->get_name()<<";"<<endl;
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
- f_types_ << "use constant " << (*c_iter)->get_name() << " => " << value << ";" << endl;
+ f_types_ << "use constant "<< (*c_iter)->get_name() << " => " << value << ";" << endl;
}
}
@@ -425,7 +455,8 @@
/**
* Structs can be normal or exceptions.
*/
-void t_perl_generator::generate_perl_struct(t_struct* tstruct, bool is_exception) {
+void t_perl_generator::generate_perl_struct(t_struct* tstruct,
+ bool is_exception) {
generate_perl_struct_definition(f_types_, tstruct, is_exception);
}
@@ -442,32 +473,36 @@
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
- out << "package " << perl_namespace(tstruct->get_program()) << tstruct->get_name() << ";\n";
+ out <<
+ "package " << perl_namespace(tstruct->get_program()) << tstruct->get_name() <<";\n";
if (is_exception) {
out << "use base qw(Thrift::TException);\n";
}
- // Create simple acessor methods
+ //Create simple acessor methods
out << "use base qw(Class::Accessor);\n";
if (members.size() > 0) {
- out << perl_namespace(tstruct->get_program()) << tstruct->get_name() << "->mk_accessors( qw( ";
- for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- t_type* t = get_true_type((*m_iter)->get_type());
- if (!t->is_xception()) {
- out << (*m_iter)->get_name() << " ";
+ out << perl_namespace(tstruct->get_program()) << tstruct->get_name() <<"->mk_accessors( qw( ";
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_type* t = get_true_type((*m_iter)->get_type());
+ if (!t->is_xception()) {
+ out << (*m_iter)->get_name() << " ";
+ }
}
- }
- out << ") );\n";
+ out << ") );\n";
}
out << endl;
// new()
indent_up();
- out << "sub new {" << endl << indent() << "my $classname = shift;" << endl << indent()
- << "my $self = {};" << endl << indent() << "my $vals = shift || {};" << endl;
+ out <<
+ "sub new {" << endl <<
+ indent() << "my $classname = shift;" << endl <<
+ indent() << "my $self = {};" << endl <<
+ indent() << "my $vals = shift || {};" << endl;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
string dval = "undef";
@@ -475,7 +510,8 @@
if ((*m_iter)->get_value() != NULL && !(t->is_struct() || t->is_xception())) {
dval = render_const_value((*m_iter)->get_type(), (*m_iter)->get_value());
}
- out << indent() << "$self->{" << (*m_iter)->get_name() << "} = " << dval << ";" << endl;
+ out <<
+ indent() << "$self->{" << (*m_iter)->get_name() << "} = " << dval << ";" << endl;
}
// Generate constructor from array
@@ -484,50 +520,61 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
- indent(out) << "$self->{" << (*m_iter)->get_name()
- << "} = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
+ indent(out) << "$self->{" << (*m_iter)->get_name() << "} = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
}
}
out << indent() << "if (UNIVERSAL::isa($vals,'HASH')) {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- out << indent() << "if (defined $vals->{" << (*m_iter)->get_name() << "}) {" << endl
- << indent() << " $self->{" << (*m_iter)->get_name() << "} = $vals->{"
- << (*m_iter)->get_name() << "};" << endl << indent() << "}" << endl;
+ out <<
+ indent() << "if (defined $vals->{" << (*m_iter)->get_name() << "}) {" << endl <<
+ indent() << " $self->{" << (*m_iter)->get_name() << "} = $vals->{" << (*m_iter)->get_name() << "};" << endl <<
+ indent() << "}" << endl;
}
indent_down();
- out << indent() << "}" << endl;
+ out <<
+ indent() << "}" << endl;
+
}
out << indent() << "return bless ($self, $classname);" << endl;
indent_down();
out << "}\n\n";
- out << "sub getName {" << endl << indent() << " return '" << tstruct->get_name() << "';" << endl
- << indent() << "}" << endl << endl;
+ out <<
+ "sub getName {" << endl <<
+ indent() << " return '" << tstruct->get_name() << "';" << endl <<
+ indent() << "}" << endl <<
+ endl;
generate_perl_struct_reader(out, tstruct);
generate_perl_struct_writer(out, tstruct);
+
}
/**
* Generates the read() method for a struct
*/
-void t_perl_generator::generate_perl_struct_reader(ofstream& out, t_struct* tstruct) {
+void t_perl_generator::generate_perl_struct_reader(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out << "sub read {" << endl;
+ out << "sub read {" <<endl;
indent_up();
- out << indent() << "my ($self, $input) = @_;" << endl << indent() << "my $xfer = 0;" << endl
- << indent() << "my $fname;" << endl << indent() << "my $ftype = 0;" << endl << indent()
- << "my $fid = 0;" << endl;
+ out <<
+ indent() << "my ($self, $input) = @_;" << endl <<
+ indent() << "my $xfer = 0;" << endl <<
+ indent() << "my $fname;" << endl <<
+ indent() << "my $ftype = 0;" << endl <<
+ indent() << "my $fid = 0;" << endl;
indent(out) << "$xfer += $input->readStructBegin(\\$fname);" << endl;
+
// Loop over reading in fields
indent(out) << "while (1) " << endl;
@@ -559,13 +606,16 @@
indent(out) << "} else {" << endl;
- indent(out) << " $xfer += $input->skip($ftype);" << endl;
+ indent(out) << " $xfer += $input->skip($ftype);" << endl;
- out << indent() << "}" << endl << indent() << "last; };" << endl;
+ out <<
+ indent() << "}" << endl <<
+ indent() << "last; };" << endl;
+
}
// In the default case we skip the field
- indent(out) << " $xfer += $input->skip($ftype);" << endl;
+ indent(out) << " $xfer += $input->skip($ftype);" << endl;
scope_down(out);
@@ -584,7 +634,8 @@
/**
* Generates the write() method for a struct
*/
-void t_perl_generator::generate_perl_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_perl_generator::generate_perl_struct_writer(ofstream& out,
+ t_struct* tstruct) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
@@ -601,26 +652,34 @@
out << indent() << "if (defined $self->{" << (*f_iter)->get_name() << "}) {" << endl;
indent_up();
- indent(out) << "$xfer += $output->writeFieldBegin("
- << "'" << (*f_iter)->get_name() << "', " << type_to_enum((*f_iter)->get_type())
- << ", " << (*f_iter)->get_key() << ");" << endl;
+ indent(out) <<
+ "$xfer += $output->writeFieldBegin(" <<
+ "'" << (*f_iter)->get_name() << "', " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ");" << endl;
+
// Write field contents
generate_serialize_field(out, *f_iter, "self->");
- indent(out) << "$xfer += $output->writeFieldEnd();" << endl;
+ indent(out) <<
+ "$xfer += $output->writeFieldEnd();" << endl;
indent_down();
indent(out) << "}" << endl;
}
- out << indent() << "$xfer += $output->writeFieldStop();" << endl << indent()
- << "$xfer += $output->writeStructEnd();" << endl;
- out << indent() << "return $xfer;" << endl;
+ out <<
+ indent() << "$xfer += $output->writeFieldStop();" << endl <<
+ indent() << "$xfer += $output->writeStructEnd();" << endl;
+
+ out <<indent() << "return $xfer;" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
@@ -629,22 +688,25 @@
* @param tservice The service definition
*/
void t_perl_generator::generate_service(t_service* tservice) {
- string f_service_name = get_namespace_out_dir() + service_name_ + ".pm";
+ string f_service_name = get_namespace_out_dir()+service_name_+".pm";
f_service_.open(f_service_name.c_str());
f_service_ <<
- /// "package "<<service_name_<<";"<<endl<<
- autogen_comment() << perl_includes();
+ /// "package "<<service_name_<<";"<<endl<<
+ autogen_comment() <<
+ perl_includes();
- f_service_ << "use " << perl_namespace(tservice->get_program()) << "Types;" << endl;
+ f_service_ <<
+ "use " << perl_namespace(tservice->get_program()) << "Types;" << endl;
t_service* extends_s = tservice->get_extends();
if (extends_s != NULL) {
- f_service_ << "use " << perl_namespace(extends_s->get_program()) << extends_s->get_name() << ";"
- << endl;
+ f_service_ <<
+ "use " << perl_namespace(extends_s->get_program()) << extends_s->get_name() << ";" << endl;
}
- f_service_ << endl;
+ f_service_ <<
+ endl;
// Generate the three main parts of the service (well, two for now in PERL)
generate_service_helpers(tservice);
@@ -679,59 +741,74 @@
indent_up();
// Generate the header portion
- f_service_ << "package " << perl_namespace(program_) << service_name_ << "Processor;" << endl
- << endl << "use strict;" << endl << extends_processor << endl << endl;
+ f_service_ <<
+ "package " << perl_namespace(program_) << service_name_ << "Processor;" << endl << endl <<
+ "use strict;" << endl <<
+ extends_processor << endl << endl;
+
if (extends.empty()) {
f_service_ << "sub new {" << endl;
indent_up();
- f_service_ << indent() << "my ($classname, $handler) = @_;" << endl << indent()
- << "my $self = {};" << endl;
+ f_service_ <<
+ indent() << "my ($classname, $handler) = @_;"<< endl <<
+ indent() << "my $self = {};" << endl;
- f_service_ << indent() << "$self->{handler} = $handler;" << endl;
+ f_service_ <<
+ indent() << "$self->{handler} = $handler;" << endl;
- f_service_ << indent() << "return bless ($self, $classname);" << endl;
+ f_service_ <<
+ indent() << "return bless ($self, $classname);"<<endl;
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
// Generate the server implementation
f_service_ << "sub process {" << endl;
indent_up();
- f_service_ << indent() << "my ($self, $input, $output) = @_;" << endl;
+ f_service_ <<
+ indent() << "my ($self, $input, $output) = @_;" << endl;
- f_service_ << indent() << "my $rseqid = 0;" << endl << indent() << "my $fname = undef;" << endl
- << indent() << "my $mtype = 0;" << endl << endl;
+ f_service_ <<
+ indent() << "my $rseqid = 0;" << endl <<
+ indent() << "my $fname = undef;" << endl <<
+ indent() << "my $mtype = 0;" << endl << endl;
- f_service_ << indent() << "$input->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);" << endl;
+ f_service_ <<
+ indent() << "$input->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);" << endl;
// HOT: check for method implementation
- f_service_ << indent() << "my $methodname = 'process_'.$fname;" << endl << indent()
- << "if (!$self->can($methodname)) {" << endl;
+ f_service_ <<
+ indent() << "my $methodname = 'process_'.$fname;" << endl <<
+ indent() << "if (!$self->can($methodname)) {" << endl;
indent_up();
- f_service_ << indent() << "$input->skip(TType::STRUCT);" << endl << indent()
- << "$input->readMessageEnd();" << endl << indent()
- << "my $x = new TApplicationException('Function '.$fname.' not implemented.', "
- "TApplicationException::UNKNOWN_METHOD);" << endl << indent()
- << "$output->writeMessageBegin($fname, TMessageType::EXCEPTION, $rseqid);" << endl
- << indent() << "$x->write($output);" << endl << indent()
- << "$output->writeMessageEnd();" << endl << indent()
- << "$output->getTransport()->flush();" << endl << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "$input->skip(TType::STRUCT);" << endl <<
+ indent() << "$input->readMessageEnd();" << endl <<
+ indent() << "my $x = new TApplicationException('Function '.$fname.' not implemented.', TApplicationException::UNKNOWN_METHOD);" << endl <<
+ indent() << "$output->writeMessageBegin($fname, TMessageType::EXCEPTION, $rseqid);" << endl <<
+ indent() << "$x->write($output);" << endl <<
+ indent() << "$output->writeMessageEnd();" << endl <<
+ indent() << "$output->getTransport()->flush();" << endl <<
+ indent() << "return;" << endl;
indent_down();
- f_service_ << indent() << "}" << endl << indent()
- << "$self->$methodname($rseqid, $input, $output);" << endl << indent() << "return 1;"
- << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "$self->$methodname($rseqid, $input, $output);" << endl <<
+ indent() << "return 1;" << endl;
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl <<endl;
// Generate the process subfunctions
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
@@ -744,23 +821,26 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_perl_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
+void t_perl_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
// Open function
- f_service_ << "sub process_" << tfunction->get_name() << " {" << endl;
+ f_service_ <<
+ "sub process_" << tfunction->get_name() << " {"<<endl;
indent_up();
- f_service_ << indent() << "my ($self, $seqid, $input, $output) = @_;" << endl;
+ f_service_ <<
+ indent() << "my ($self, $seqid, $input, $output) = @_;" << endl;
- string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_"
- + tfunction->get_name() + "_args";
- string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_"
- + tfunction->get_name() + "_result";
+ string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_args";
+ string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_result";
- f_service_ << indent() << "my $args = new " << argsname << "();" << endl << indent()
- << "$args->read($input);" << endl;
+ f_service_ <<
+ indent() << "my $args = new " << argsname << "();" << endl <<
+ indent() << "$args->read($input);" << endl;
- f_service_ << indent() << "$input->readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "$input->readMessageEnd();" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -768,12 +848,14 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "my $result = new " << resultname << "();" << endl;
+ f_service_ <<
+ indent() << "my $result = new " << resultname << "();" << endl;
}
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "eval {" << endl;
+ f_service_ <<
+ indent() << "eval {" << endl;
indent_up();
}
@@ -786,7 +868,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "$result->{success} = ";
}
- f_service_ << "$self->{handler}->" << tfunction->get_name() << "(";
+ f_service_ <<
+ "$self->{handler}->" << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -801,13 +884,16 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "}; if( UNIVERSAL::isa($@,'"
- << perl_namespace((*x_iter)->get_type()->get_program())
- << (*x_iter)->get_type()->get_name() << "') ){ " << endl;
+ f_service_ <<
+ indent() << "}; if( UNIVERSAL::isa($@,'" <<
+ perl_namespace((*x_iter)->get_type()->get_program()) <<
+ (*x_iter)->get_type()->get_name() <<
+ "') ){ " << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "$result->{" << (*x_iter)->get_name() << "} = $@;" << endl;
+ f_service_ <<
+ indent() << "$result->{" << (*x_iter)->get_name() << "} = $@;" << endl;
indent_down();
f_service_ << indent();
}
@@ -817,20 +903,24 @@
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
indent_down();
- f_service_ << "}" << endl;
+ f_service_ <<
+ "}" << endl;
return;
}
// Serialize the request header
- f_service_ << indent() << "$output->writeMessageBegin('" << tfunction->get_name()
- << "', TMessageType::REPLY, $seqid);" << endl << indent() << "$result->write($output);"
- << endl << indent() << "$output->writeMessageEnd();" << endl << indent()
- << "$output->getTransport()->flush();" << endl;
+ f_service_ <<
+ indent() << "$output->writeMessageBegin('" << tfunction->get_name() << "', TMessageType::REPLY, $seqid);" << endl <<
+ indent() << "$result->write($output);" << endl <<
+ indent() << "$output->writeMessageEnd();" << endl <<
+ indent() << "$output->getTransport()->flush();" << endl;
// Close function
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
/**
@@ -842,7 +932,8 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- f_service_ << "# HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
+ f_service_ <<
+ "# HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
@@ -885,21 +976,24 @@
string extends_if = "";
t_service* extends_s = tservice->get_extends();
if (extends_s != NULL) {
- extends_if = "use base qw(" + perl_namespace(extends_s->get_program()) + extends_s->get_name()
- + "If);";
+ extends_if = "use base qw(" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "If);";
}
- f_service_ << "package " << perl_namespace(program_) << service_name_ << "If;" << endl << endl
- << "use strict;" << endl << extends_if << endl << endl;
+ f_service_ <<
+ "package " << perl_namespace(program_) << service_name_ << "If;" << endl << endl <<
+ "use strict;" << endl <<
+ extends_if << endl << endl;
+
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << "sub " << function_signature(*f_iter) << endl << " die 'implement interface';\n}"
- << endl << endl;
+ f_service_ <<
+ "sub " << function_signature(*f_iter) <<endl<< " die 'implement interface';\n}" << endl << endl;
}
indent_down();
+
}
/**
@@ -910,54 +1004,65 @@
string extends_if = "";
t_service* extends_s = tservice->get_extends();
if (extends_s != NULL) {
- extends = extends_s->get_name();
- extends_if = "use base qw(" + perl_namespace(extends_s->get_program()) + extends_s->get_name()
- + "Rest);";
+ extends = extends_s->get_name();
+ extends_if = "use base qw(" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "Rest);";
}
- f_service_ << "package " << perl_namespace(program_) << service_name_ << "Rest;" << endl << endl
- << "use strict;" << endl << extends_if << endl << endl;
+ f_service_ <<
+ "package " << perl_namespace(program_) << service_name_ << "Rest;" << endl << endl <<
+ "use strict;" << endl <<
+ extends_if << endl << endl;
+
if (extends.empty()) {
f_service_ << "sub new {" << endl;
indent_up();
- f_service_ << indent() << "my ($classname, $impl) = @_;" << endl << indent()
- << "my $self ={ impl => $impl };" << endl << endl << indent()
- << "return bless($self,$classname);" << endl;
+ f_service_ <<
+ indent() << "my ($classname, $impl) = @_;" << endl <<
+ indent() << "my $self ={ impl => $impl };" << endl << endl <<
+ indent() << "return bless($self,$classname);" << endl;
+
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << "sub " << (*f_iter)->get_name() << "{" << endl;
+ f_service_ <<
+ "sub " << (*f_iter)->get_name() <<
+ "{" <<endl;
indent_up();
- f_service_ << indent() << "my ($self, $request) = @_;" << endl << endl;
+ f_service_ <<
+ indent() << "my ($self, $request) = @_;" << endl << endl;
+
const vector<t_field*>& args = (*f_iter)->get_arglist()->get_members();
vector<t_field*>::const_iterator a_iter;
for (a_iter = args.begin(); a_iter != args.end(); ++a_iter) {
t_type* atype = get_true_type((*a_iter)->get_type());
string req = "$request->{'" + (*a_iter)->get_name() + "'}";
- f_service_ << indent() << "my $" << (*a_iter)->get_name() << " = (" << req << ") ? " << req
- << " : undef;" << endl;
- if (atype->is_string() && ((t_base_type*)atype)->is_string_list()) {
- f_service_ << indent() << "my @" << (*a_iter)->get_name() << " = split(/,/, $"
- << (*a_iter)->get_name() << ");" << endl << indent() << "$"
- << (*a_iter)->get_name() << " = \\@" << (*a_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "my $" << (*a_iter)->get_name() << " = (" << req << ") ? " << req << " : undef;" << endl;
+ if (atype->is_string() &&
+ ((t_base_type*)atype)->is_string_list()) {
+ f_service_ <<
+ indent() << "my @" << (*a_iter)->get_name() << " = split(/,/, $" << (*a_iter)->get_name() << ");" << endl <<
+ indent() << "$"<<(*a_iter)->get_name() <<" = \\@"<<(*a_iter)->get_name()<<endl;
}
}
- f_service_ << indent() << "return $self->{impl}->" << (*f_iter)->get_name() << "("
- << argument_list((*f_iter)->get_arglist()) << ");" << endl;
+ f_service_ <<
+ indent() << "return $self->{impl}->" << (*f_iter)->get_name() << "(" << argument_list((*f_iter)->get_arglist()) << ");" << endl;
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) << "}" << endl <<endl;
}
+
}
/**
@@ -974,31 +1079,37 @@
extends_client = "use base qw(" + extends + "Client);";
}
- f_service_ << "package " << perl_namespace(program_) << service_name_ << "Client;" << endl << endl
- << extends_client << endl << "use base qw(" << perl_namespace(program_)
- << service_name_ << "If);" << endl;
+ f_service_ <<
+ "package " << perl_namespace(program_) << service_name_ << "Client;" << endl << endl <<
+ extends_client << endl <<
+ "use base qw(" << perl_namespace(program_) << service_name_ << "If);" << endl;
// Constructor function
- f_service_ << "sub new {" << endl;
+ f_service_ << "sub new {"<<endl;
indent_up();
- f_service_ << indent() << "my ($classname, $input, $output) = @_;" << endl << indent()
- << "my $self = {};" << endl;
+ f_service_ <<
+ indent() << "my ($classname, $input, $output) = @_;" << endl <<
+ indent() << "my $self = {};" <<endl;
if (!extends.empty()) {
- f_service_ << indent() << "$self = $classname->SUPER::new($input, $output);" << endl;
+ f_service_ <<
+ indent() << "$self = $classname->SUPER::new($input, $output);" << endl;
} else {
- f_service_ << indent() << "$self->{input} = $input;" << endl << indent()
- << "$self->{output} = defined $output ? $output : $input;" << endl << indent()
- << "$self->{seqid} = 0;" << endl;
+ f_service_ <<
+ indent() << "$self->{input} = $input;" << endl <<
+ indent() << "$self->{output} = defined $output ? $output : $input;" << endl <<
+ indent() << "$self->{seqid} = 0;" << endl;
}
- f_service_ << indent() << "return bless($self,$classname);" << endl;
+ f_service_ <<
+ indent() << "return bless($self,$classname);"<<endl;
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
@@ -1014,7 +1125,8 @@
indent_up();
- indent(f_service_) << indent() << "$self->send_" << funname << "(";
+ indent(f_service_) << indent() <<
+ "$self->send_" << funname << "(";
bool first = true;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
@@ -1032,132 +1144,161 @@
if (!(*f_iter)->get_returntype()->is_void()) {
f_service_ << "return ";
}
- f_service_ << "$self->recv_" << funname << "();" << endl;
+ f_service_ <<
+ "$self->recv_" << funname << "();" << endl;
}
indent_down();
f_service_ << "}" << endl << endl;
- f_service_ << "sub send_" << function_signature(*f_iter) << endl;
+ f_service_ <<
+ "sub send_" << function_signature(*f_iter) << endl;
indent_up();
- std::string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_"
- + (*f_iter)->get_name() + "_args";
+ std::string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_args";
// Serialize the request header
- f_service_ << indent() << "$self->{output}->writeMessageBegin('" << (*f_iter)->get_name()
- << "', " << ((*f_iter)->is_oneway() ? "TMessageType::ONEWAY" : "TMessageType::CALL")
- << ", $self->{seqid});" << endl;
+ f_service_ <<
+ indent() << "$self->{output}->writeMessageBegin('" << (*f_iter)->get_name() << "', " <<
+ ((*f_iter)->is_oneway() ? "TMessageType::ONEWAY" : "TMessageType::CALL") <<
+ ", $self->{seqid});" << endl;
- f_service_ << indent() << "my $args = new " << argsname << "();" << endl;
+ f_service_ <<
+ indent() << "my $args = new " << argsname << "();" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "$args->{" << (*fld_iter)->get_name() << "} = $"
- << (*fld_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "$args->{" << (*fld_iter)->get_name() << "} = $" << (*fld_iter)->get_name() << ";" << endl;
}
// Write to the stream
- f_service_ << indent() << "$args->write($self->{output});" << endl << indent()
- << "$self->{output}->writeMessageEnd();" << endl << indent()
- << "$self->{output}->getTransport()->flush();" << endl;
+ f_service_ <<
+ indent() << "$args->write($self->{output});" << endl <<
+ indent() << "$self->{output}->writeMessageEnd();" << endl <<
+ indent() << "$self->{output}->getTransport()->flush();" << endl;
+
indent_down();
f_service_ << "}" << endl;
+
if (!(*f_iter)->is_oneway()) {
- std::string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_"
- + (*f_iter)->get_name() + "_result";
+ std::string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_result";
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
string("recv_") + (*f_iter)->get_name(),
&noargs);
// Open function
- f_service_ << endl << "sub " << function_signature(&recv_function) << endl;
+ f_service_ <<
+ endl <<
+ "sub " << function_signature(&recv_function) << endl;
indent_up();
- f_service_ << indent() << "my $rseqid = 0;" << endl << indent() << "my $fname;" << endl
- << indent() << "my $mtype = 0;" << endl << endl;
+ f_service_ <<
+ indent() << "my $rseqid = 0;" << endl <<
+ indent() << "my $fname;" << endl <<
+ indent() << "my $mtype = 0;" << endl <<
+ endl;
- f_service_ << indent() << "$self->{input}->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);"
- << endl << indent() << "if ($mtype == TMessageType::EXCEPTION) {" << endl
- << indent() << " my $x = new TApplicationException();" << endl << indent()
- << " $x->read($self->{input});" << endl << indent()
- << " $self->{input}->readMessageEnd();" << endl << indent() << " die $x;" << endl
- << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "$self->{input}->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);" << endl <<
+ indent() << "if ($mtype == TMessageType::EXCEPTION) {" << endl <<
+ indent() << " my $x = new TApplicationException();" << endl <<
+ indent() << " $x->read($self->{input});" << endl <<
+ indent() << " $self->{input}->readMessageEnd();" << endl <<
+ indent() << " die $x;" << endl <<
+ indent() << "}" << endl;
- f_service_ << indent() << "my $result = new " << resultname << "();" << endl << indent()
- << "$result->read($self->{input});" << endl;
- f_service_ << indent() << "$self->{input}->readMessageEnd();" << endl << endl;
+ f_service_ <<
+ indent() << "my $result = new " << resultname << "();" << endl <<
+ indent() << "$result->read($self->{input});" << endl;
+
+
+ f_service_ <<
+ indent() << "$self->{input}->readMessageEnd();" << endl <<
+ endl;
+
// Careful, only return result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if (defined $result->{success} ) {" << endl << indent()
- << " return $result->{success};" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (defined $result->{success} ) {" << endl <<
+ indent() << " return $result->{success};" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if (defined $result->{" << (*x_iter)->get_name() << "}) {"
- << endl << indent() << " die $result->{" << (*x_iter)->get_name() << "};"
- << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if (defined $result->{" << (*x_iter)->get_name() << "}) {" << endl <<
+ indent() << " die $result->{" << (*x_iter)->get_name() << "};" << endl <<
+ indent() << "}" << endl;
}
// Careful, only return _result if not a void function
if ((*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "return;" << endl;
+ indent(f_service_) <<
+ "return;" << endl;
} else {
- f_service_ << indent() << "die \"" << (*f_iter)->get_name() << " failed: unknown result\";"
- << endl;
+ f_service_ <<
+ indent() << "die \"" << (*f_iter)->get_name() << " failed: unknown result\";" << endl;
}
// Close function
indent_down();
- f_service_ << "}" << endl;
+ f_service_ << "}"<<endl;
+
}
}
+
}
/**
* Deserializes a field of any type.
*/
-void t_perl_generator::generate_deserialize_field(ofstream& out,
+void t_perl_generator::generate_deserialize_field(ofstream &out,
t_field* tfield,
string prefix,
bool inclass) {
- (void)inclass;
+ (void) inclass;
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = tfield->get_name();
- // Hack for when prefix is defined (always a hash ref)
+ //Hack for when prefix is defined (always a hash ref)
if (!prefix.empty()) {
name = prefix + "{" + tfield->get_name() + "}";
}
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
- indent(out) << "$xfer += $input->";
+ indent(out) <<
+ "$xfer += $input->";
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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
out << "readString(\\$" << name << ");";
@@ -1190,8 +1331,7 @@
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type->get_name().c_str());
+ tfield->get_name().c_str(), type->get_name().c_str());
}
}
@@ -1201,15 +1341,17 @@
* buffer for deserialization, and that there is a variable protocol which
* is a reference to a TProtocol serialization object.
*/
-void t_perl_generator::generate_deserialize_struct(ofstream& out,
+void t_perl_generator::generate_deserialize_struct(ofstream &out,
t_struct* tstruct,
string prefix) {
- out << indent() << "$" << prefix << " = new " << perl_namespace(tstruct->get_program())
- << tstruct->get_name() << "();" << endl << indent() << "$xfer += $" << prefix
- << "->read($input);" << endl;
+ out <<
+ indent() << "$" << prefix << " = new " << perl_namespace(tstruct->get_program()) << tstruct->get_name() << "();" << endl <<
+ indent() << "$xfer += $" << prefix << "->read($input);" << endl;
}
-void t_perl_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_perl_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
string size = tmp("_size");
@@ -1222,33 +1364,43 @@
t_field fvtype(g_type_byte, vtype);
t_field fetype(g_type_byte, etype);
- out << indent() << "my $" << size << " = 0;" << endl;
+ out <<
+ indent() << "my $" << size << " = 0;" << endl;
// Declare variables, read header
if (ttype->is_map()) {
- out << indent() << "$" << prefix << " = {};" << endl << indent() << "my $" << ktype << " = 0;"
- << endl << indent() << "my $" << vtype << " = 0;" << endl;
+ out <<
+ indent() << "$" << prefix << " = {};" << endl <<
+ indent() << "my $" << ktype << " = 0;" << endl <<
+ indent() << "my $" << vtype << " = 0;" << endl;
- out << indent() << "$xfer += $input->readMapBegin("
- << "\\$" << ktype << ", \\$" << vtype << ", \\$" << size << ");" << endl;
+ out <<
+ indent() << "$xfer += $input->readMapBegin(" <<
+ "\\$" << ktype << ", \\$" << vtype << ", \\$" << size << ");" << endl;
} else if (ttype->is_set()) {
- out << indent() << "$" << prefix << " = {};" << endl << indent() << "my $" << etype << " = 0;"
- << endl << indent() << "$xfer += $input->readSetBegin("
- << "\\$" << etype << ", \\$" << size << ");" << endl;
+ out <<
+ indent() << "$" << prefix << " = {};" << endl <<
+ indent() << "my $" << etype << " = 0;" << endl <<
+ indent() << "$xfer += $input->readSetBegin(" <<
+ "\\$" << etype << ", \\$" << size << ");" << endl;
} else if (ttype->is_list()) {
- out << indent() << "$" << prefix << " = [];" << endl << indent() << "my $" << etype << " = 0;"
- << endl << indent() << "$xfer += $input->readListBegin("
- << "\\$" << etype << ", \\$" << size << ");" << endl;
+ out <<
+ indent() << "$" << prefix << " = [];" << endl <<
+ indent() << "my $" << etype << " = 0;" << endl <<
+ indent() << "$xfer += $input->readListBegin(" <<
+ "\\$" << etype << ", \\$" << size << ");" << endl;
+
}
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for (my $" << i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")"
- << endl;
+ indent(out) <<
+ "for (my $" <<
+ i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")" << endl;
scope_up(out);
@@ -1262,6 +1414,7 @@
scope_down(out);
+
// Read container end
if (ttype->is_map()) {
indent(out) << "$xfer += $input->readMapEnd();" << endl;
@@ -1274,81 +1427,103 @@
scope_down(out);
}
+
/**
* Generates code to deserialize a map
*/
-void t_perl_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_perl_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("key");
string val = tmp("val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey, true, true) << endl;
- indent(out) << declare_field(&fval, true, true) << endl;
+ indent(out) <<
+ declare_field(&fkey, true, true) << endl;
+ indent(out) <<
+ declare_field(&fval, true, true) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << "$" << prefix << "->{$" << key << "} = $" << val << ";" << endl;
+ indent(out) <<
+ "$" << prefix << "->{$" << key << "} = $" << val << ";" << endl;
}
-void t_perl_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_perl_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << "my $" << elem << " = undef;" << endl;
+ indent(out) <<
+ "my $" << elem << " = undef;" << endl;
generate_deserialize_field(out, &felem);
- indent(out) << "$" << prefix << "->{$" << elem << "} = 1;" << endl;
+ indent(out) <<
+ "$" << prefix << "->{$" << elem << "} = 1;" << endl;
}
-void t_perl_generator::generate_deserialize_list_element(ofstream& out,
+void t_perl_generator::generate_deserialize_list_element(ofstream &out,
t_list* tlist,
string prefix) {
string elem = tmp("elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << "my $" << elem << " = undef;" << endl;
+ indent(out) <<
+ "my $" << elem << " = undef;" << endl;
generate_deserialize_field(out, &felem);
- indent(out) << "push(@{$" << prefix << "},$" << elem << ");" << endl;
+ indent(out) <<
+ "push(@{$" << prefix << "},$" << elem << ");" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_perl_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_perl_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + "{" + tfield->get_name() + "}");
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + "{"+tfield->get_name()+"}" );
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + "{" + tfield->get_name() + "}");
+ generate_serialize_container(out,
+ type,
+ prefix + "{" + tfield->get_name()+"}");
} else if (type->is_base_type() || type->is_enum()) {
string name = tfield->get_name();
- // Hack for when prefix is defined (always a hash ref)
- if (!prefix.empty())
+ //Hack for when prefix is defined (always a hash ref)
+ if(!prefix.empty())
name = prefix + "{" + tfield->get_name() + "}";
- indent(out) << "$xfer += $output->";
+ indent(out) <<
+ "$xfer += $output->";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
out << "writeString($" << name << ");";
@@ -1393,32 +1568,41 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_perl_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
- indent(out) << "$xfer += $" << prefix << "->write($output);" << endl;
+void t_perl_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
+ indent(out) <<
+ "$xfer += $" << prefix << "->write($output);" << endl;
}
/**
* Writes out a container
*/
-void t_perl_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_perl_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
- indent(out) << "$xfer += $output->writeMapBegin("
- << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "scalar(keys %{$" << prefix << "}));" << endl;
+ indent(out) <<
+ "$xfer += $output->writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "scalar(keys %{$" << prefix << "}));" << endl;
} else if (ttype->is_set()) {
- indent(out) << "$xfer += $output->writeSetBegin("
- << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", "
- << "scalar(@{$" << prefix << "}));" << endl;
+ indent(out) <<
+ "$xfer += $output->writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "scalar(@{$" << prefix << "}));" << endl;
} else if (ttype->is_list()) {
- indent(out) << "$xfer += $output->writeListBegin("
- << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", "
- << "scalar(@{$" << prefix << "}));" << endl;
+ indent(out) <<
+ "$xfer += $output->writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "scalar(@{$" << prefix << "}));" << endl;
+
}
scope_up(out);
@@ -1426,8 +1610,8 @@
if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
- indent(out) << "while( my ($" << kiter << ",$" << viter << ") = each %{$" << prefix << "}) "
- << endl;
+ indent(out) <<
+ "while( my ($"<<kiter<<",$"<<viter<<") = each %{$" << prefix << "}) " << endl;
scope_up(out);
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
@@ -1435,14 +1619,17 @@
} else if (ttype->is_set()) {
string iter = tmp("iter");
- indent(out) << "foreach my $" << iter << " (@{$" << prefix << "})" << endl;
+ indent(out) <<
+ "foreach my $"<<iter<<" (@{$" << prefix << "})" << endl;
scope_up(out);
generate_serialize_set_element(out, (t_set*)ttype, iter);
scope_down(out);
+
} else if (ttype->is_list()) {
string iter = tmp("iter");
- indent(out) << "foreach my $" << iter << " (@{$" << prefix << "}) " << endl;
+ indent(out) <<
+ "foreach my $"<<iter<<" (@{$" << prefix << "}) " << endl;
scope_up(out);
generate_serialize_list_element(out, (t_list*)ttype, iter);
scope_down(out);
@@ -1451,11 +1638,14 @@
scope_down(out);
if (ttype->is_map()) {
- indent(out) << "$xfer += $output->writeMapEnd();" << endl;
+ indent(out) <<
+ "$xfer += $output->writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "$xfer += $output->writeSetEnd();" << endl;
+ indent(out) <<
+ "$xfer += $output->writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "$xfer += $output->writeListEnd();" << endl;
+ indent(out) <<
+ "$xfer += $output->writeListEnd();" << endl;
}
scope_down(out);
@@ -1465,7 +1655,7 @@
* Serializes the members of a map.
*
*/
-void t_perl_generator::generate_serialize_map_element(ofstream& out,
+void t_perl_generator::generate_serialize_map_element(ofstream &out,
t_map* tmap,
string kiter,
string viter) {
@@ -1479,7 +1669,9 @@
/**
* Serializes the members of a set.
*/
-void t_perl_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_perl_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
@@ -1487,7 +1679,9 @@
/**
* Serializes the members of a list.
*/
-void t_perl_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_perl_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield);
}
@@ -1522,8 +1716,7 @@
result += " = 0.0";
break;
default:
- throw "compiler error: no PERL initializer for base type "
- + t_base_type::t_base_name(tbase);
+ throw "compiler error: no PERL initializer for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
result += " = 0";
@@ -1546,15 +1739,16 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_perl_generator::function_signature(t_function* tfunction, string prefix) {
+string t_perl_generator::function_signature(t_function* tfunction,
+ string prefix) {
string str;
- str = prefix + tfunction->get_name() + "{\n";
+ str = prefix + tfunction->get_name() + "{\n";
str += " my $self = shift;\n";
- // Need to create perl function arg inputs
- const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
+ //Need to create perl function arg inputs
+ const vector<t_field*> &fields = tfunction->get_arglist()->get_members();
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1587,7 +1781,7 @@
/**
* Converts the parse type to a C++ enum string for the given type.
*/
-string t_perl_generator::type_to_enum(t_type* type) {
+string t_perl_generator ::type_to_enum(t_type* type) {
type = get_true_type(type);
if (type->is_base_type()) {
@@ -1626,3 +1820,4 @@
}
THRIFT_REGISTER_GENERATOR(perl, "Perl", "")
+
diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc
index 827af9f..ae57ec3 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -35,24 +35,27 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
-#define NSGLOBAL (nsglobal_.size() ? nsglobal_ : "")
-#define NSGLOBAL_A ("\\" + NSGLOBAL)
-#define NSGLOBAL_B (NSGLOBAL + "\\")
+#define NSGLOBAL (nsglobal_.size() ? nsglobal_ : "")
+#define NSGLOBAL_A ("\\" + NSGLOBAL )
+#define NSGLOBAL_B ( NSGLOBAL + "\\")
#define NSGLOBAL_AB ("\\" + NSGLOBAL + "\\")
+
/**
* PHP code generator.
*
*/
class t_php_generator : public t_oop_generator {
-public:
- t_php_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_php_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("inlined");
@@ -74,9 +77,10 @@
json_serializable_ = (iter != parsed_options.end());
iter = parsed_options.find("nsglobal");
- if (iter != parsed_options.end()) {
+ if(iter != parsed_options.end()) {
nsglobal_ = iter->second;
- } else {
+ }
+ else {
nsglobal_ = ""; // by default global namespace is empty
}
@@ -101,13 +105,13 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_consts(vector<t_const*> consts);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_consts (vector<t_const*> consts);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
@@ -116,17 +120,11 @@
*/
void generate_php_struct(t_struct* tstruct, bool is_exception);
- void generate_php_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_result = false);
+ void generate_php_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
void generate_php_struct_reader(std::ofstream& out, t_struct* tstruct, bool is_result);
void generate_php_struct_writer(std::ofstream& out, t_struct* tstruct, bool is_result);
void generate_php_function_helpers(t_function* tfunction);
- void generate_php_struct_required_validator(ofstream& out,
- t_struct* tstruct,
- std::string method_name,
- bool write_mode);
+ void generate_php_struct_required_validator(ofstream& out, t_struct* tstruct, std::string method_name, bool write_mode);
void generate_php_struct_read_validator(ofstream& out, t_struct* tstruct);
void generate_php_struct_write_validator(ofstream& out, t_struct* tstruct);
void generate_php_struct_json_serialize(ofstream& out, t_struct* tstruct, bool is_result);
@@ -134,71 +132,93 @@
bool needs_php_read_validator(t_struct* tstruct, bool is_result);
int get_php_num_required_fields(const vector<t_field*>& fields, bool write_mode);
- void generate_php_type_spec(std::ofstream& out, t_type* t);
- void generate_php_struct_spec(std::ofstream& out, t_struct* tstruct);
+ void generate_php_type_spec(std::ofstream &out, t_type* t);
+ void generate_php_struct_spec(std::ofstream &out, t_struct* tstruct);
/**
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_rest(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_processor(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_rest (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_processor (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inclass = false);
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
- void generate_php_doc(std::ofstream& out, t_doc* tdoc);
+ void generate_php_doc (std::ofstream& out,
+ t_doc* tdoc);
- void generate_php_doc(std::ofstream& out, t_field* tfield);
+ void generate_php_doc (std::ofstream& out,
+ t_field* tfield);
- void generate_php_doc(std::ofstream& out, t_function* tfunction);
+ void generate_php_doc (std::ofstream& out,
+ t_function* tfunction);
- void generate_php_docstring_comment(std::ofstream& out, string contents);
+ void generate_php_docstring_comment (std::ofstream &out,
+ string contents);
/**
* Helper rendering functions
*/
std::string php_includes();
- std::string declare_field(t_field* tfield, bool init = false, bool obj = false);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct, bool addTypeHints = true);
std::string type_to_cast(t_type* ttype);
std::string type_to_enum(t_type* ttype);
@@ -206,7 +226,7 @@
std::string php_namespace_base(const t_program* p) {
std::string ns = p->get_namespace("php");
- const char* delimiter = "\\";
+ const char * delimiter = "\\";
size_t position = ns.find('.');
while (position != string::npos) {
ns.replace(position, 1, delimiter);
@@ -215,37 +235,39 @@
return ns;
}
- // general use namespace prefixing: \my\namespace\ or my_namespace_
+ //general use namespace prefixing: \my\namespace\ or my_namespace_
string php_namespace(const t_program* p) {
string ns = php_namespace_base(p);
return (nsglobal_.size() ? NSGLOBAL_AB : NSGLOBAL_B) + (ns.size() ? (ns + "\\") : "");
}
- // setting the namespace of a file: my\namespace
+ //setting the namespace of a file: my\namespace
string php_namespace_suffix(const t_program* p) {
string ns = php_namespace_base(p);
return (nsglobal_.size() ? NSGLOBAL_B : NSGLOBAL) + ns;
}
- // add a directory to allready existing namespace
+ //add a directory to allready existing namespace
string php_namespace_directory(string directory, bool end = true) {
- (void)directory;
- if (end) {
+ (void) directory;
+ if(end) {
return ";";
} else {
return "";
}
}
- // writing an autload identifier into globa;ls: my\namespace\ or my_namespace_
+ //writing an autload identifier into globa;ls: my\namespace\ or my_namespace_
string php_namespace_autoload(const t_program* p) {
std::string ns = php_namespace_base(p);
return (nsglobal_.size() ? NSGLOBAL_B : NSGLOBAL) + (ns.size() ? (ns + "\\") : "");
}
- // declaring a type: typename or my_namespace_typename
- string php_namespace_declaration(t_type* t) { return t->get_name(); }
+ //declaring a type: typename or my_namespace_typename
+ string php_namespace_declaration(t_type* t) {
+ return t->get_name();
+ }
std::string php_path(t_program* p) {
std::string ns = p->get_namespace("php.path");
@@ -269,7 +291,8 @@
* @param str
* @return stirng
*/
- string classify(string str) {
+ string classify(string str)
+ {
string classe = "";
vector<string> x = split(str, '_');
@@ -288,18 +311,18 @@
* @param elems
* @return
*/
- vector<string>& split(const string& s, char delim, vector<string>& elems) {
+ vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
- while (getline(ss, item, delim)) {
+ while(getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
- vector<string> split(const string& s, char delim) {
+ vector<string> split(const string &s, char delim) {
vector<string> elems;
return split(s, delim, elems);
@@ -310,20 +333,22 @@
* @param str
* @return
*/
- string capitalize(string str) {
+ string capitalize(string str)
+ {
string::iterator it(str.begin());
if (it != str.end())
- str[0] = toupper((unsigned char)str[0]);
+ str[0] = toupper((unsigned char)str[0]);
- // while(++it != str.end())
- // {
- // *it = tolower((unsigned char)*it);
- // }
+// while(++it != str.end())
+// {
+// *it = tolower((unsigned char)*it);
+// }
return str;
}
-private:
+ private:
+
/**
* File streams
*/
@@ -368,10 +393,12 @@
std::string nsglobal_;
};
+
bool t_php_generator::is_valid_namespace(const std::string& sub_namespace) {
return sub_namespace == "path";
}
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -392,12 +419,13 @@
}
// Make output file
- string f_types_name = package_dir_ + "Types.php";
+ string f_types_name = package_dir_+"Types.php";
f_types_.open(f_types_name.c_str());
// Print header
- f_types_ << "<?php" << endl;
- f_types_ << "namespace " << php_namespace_suffix(get_program()) << ";" << endl << endl;
+ f_types_ <<
+ "<?php" << endl;
+ f_types_ << "namespace " << php_namespace_suffix(get_program()) << ";" << endl << endl;
f_types_ << autogen_comment() << php_includes();
f_types_ << endl;
@@ -407,18 +435,20 @@
* Prints standard php includes
*/
string t_php_generator::php_includes() {
- string includes = "use Thrift\\Base\\TBase;\n"
- "use Thrift\\Type\\TType;\n"
- "use Thrift\\Type\\TMessageType;\n"
- "use Thrift\\Exception\\TException;\n"
- "use Thrift\\Exception\\TProtocolException;\n"
- "use Thrift\\Protocol\\TProtocol;\n"
- "use Thrift\\Protocol\\TBinaryProtocolAccelerated;\n"
- "use Thrift\\Exception\\TApplicationException;\n";
+ string includes =
+ "use Thrift\\Base\\TBase;\n"
+ "use Thrift\\Type\\TType;\n"
+ "use Thrift\\Type\\TMessageType;\n"
+ "use Thrift\\Exception\\TException;\n"
+ "use Thrift\\Exception\\TProtocolException;\n"
+ "use Thrift\\Protocol\\TProtocol;\n"
+ "use Thrift\\Protocol\\TBinaryProtocolAccelerated;\n"
+ "use Thrift\\Exception\\TApplicationException;\n";
if (json_serializable_) {
- includes += "use JsonSerializable;\n"
- "use stdClass;\n";
+ includes +=
+ "use JsonSerializable;\n"
+ "use stdClass;\n";
}
return includes + "\n";
@@ -429,7 +459,7 @@
*/
void t_php_generator::close_generator() {
// Close types file
- f_types_ << endl;
+ f_types_ << endl;
f_types_.close();
}
@@ -439,7 +469,7 @@
* @param ttypedef The type definition
*/
void t_php_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -456,21 +486,26 @@
// code but you can't do things like an 'extract' on it, which is a bit of
// a downer.
generate_php_doc(f_types_, tenum);
- f_types_ << "final class " << tenum->get_name() << " {" << endl;
+ f_types_ <<
+ "final class " << tenum->get_name() << " {" << endl;
indent_up();
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
generate_php_doc(f_types_, *c_iter);
- indent(f_types_) << "const " << (*c_iter)->get_name() << " = " << value << ";" << endl;
+ indent(f_types_) <<
+ "const " << (*c_iter)->get_name() << " = " << value << ";" << endl;
}
- indent(f_types_) << "static public $__names = array(" << endl;
+ indent(f_types_) <<
+ "static public $__names = array(" << endl;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
- indent(f_types_) << " " << value << " => '" << (*c_iter)->get_name() << "'," << endl;
+ indent(f_types_) <<
+ " " << value << " => '" << (*c_iter)->get_name() << "'," << endl;
}
- indent(f_types_) << ");" << endl;
+ indent(f_types_) <<
+ ");" << endl;
indent_down();
f_types_ << "}" << endl << endl;
@@ -482,40 +517,41 @@
* Override the one from t_generator
*/
void t_php_generator::generate_consts(vector<t_const*> consts) {
- vector<t_const*>::iterator c_iter;
+ vector<t_const*>::iterator c_iter;
- // Create class only if needed
- if (consts.size() > 0) {
- f_types_ << "final class Constant extends \\Thrift\\Type\\TConstant {" << endl;
- indent_up();
+ // Create class only if needed
+ if(consts.size() > 0)
+ {
+ f_types_ << "final class Constant extends \\Thrift\\Type\\TConstant {" << endl;
+ indent_up();
- // Create static property
- for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- string name = (*c_iter)->get_name();
+ // Create static property
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ string name = (*c_iter)->get_name();
- indent(f_types_) << "static protected $" << name << ";" << endl;
+ indent(f_types_) << "static protected $" << name << ";" << endl;
+ }
+
+ // Create init function
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ string name = (*c_iter)->get_name();
+
+ f_types_ << endl;
+
+ indent(f_types_) << "static protected function init_" << name << "() {" << endl;
+ indent_up();
+
+ indent(f_types_) << "return ";
+ generate_const(*c_iter);
+ f_types_ << ";" << endl;
+
+ indent_down();
+ indent(f_types_) << "}" << endl;
+ }
+
+ indent_down();
+ f_types_ << "}" << endl << endl;
}
-
- // Create init function
- for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- string name = (*c_iter)->get_name();
-
- f_types_ << endl;
-
- indent(f_types_) << "static protected function init_" << name << "() {" << endl;
- indent_up();
-
- indent(f_types_) << "return ";
- generate_const(*c_iter);
- f_types_ << ";" << endl;
-
- indent_down();
- indent(f_types_) << "}" << endl;
- }
-
- indent_down();
- f_types_ << "}" << endl << endl;
- }
}
/**
@@ -650,19 +686,20 @@
/**
* Structs can be normal or exceptions.
*/
-void t_php_generator::generate_php_struct(t_struct* tstruct, bool is_exception) {
+void t_php_generator::generate_php_struct(t_struct* tstruct,
+ bool is_exception) {
generate_php_struct_definition(f_types_, tstruct, is_exception);
}
-void t_php_generator::generate_php_type_spec(ofstream& out, t_type* t) {
+void t_php_generator::generate_php_type_spec(ofstream& out,
+ t_type* t) {
t = get_true_type(t);
indent(out) << "'type' => " << type_to_enum(t) << "," << endl;
if (t->is_base_type() || t->is_enum()) {
// Noop, type is all we need
} else if (t->is_struct() || t->is_xception()) {
- indent(out) << "'class' => '" << php_namespace(t->get_program()) << t->get_name() << "',"
- << endl;
+ indent(out) << "'class' => '" << php_namespace(t->get_program()) << t->get_name() <<"'," << endl;
} else if (t->is_map()) {
t_type* ktype = get_true_type(((t_map*)t)->get_key_type());
t_type* vtype = get_true_type(((t_map*)t)->get_val_type());
@@ -685,7 +722,7 @@
} else {
etype = get_true_type(((t_set*)t)->get_elem_type());
}
- indent(out) << "'etype' => " << type_to_enum(etype) << "," << endl;
+ indent(out) << "'etype' => " << type_to_enum(etype) <<"," << endl;
indent(out) << "'elem' => array(" << endl;
indent_up();
generate_php_type_spec(out, etype);
@@ -694,13 +731,15 @@
} else {
throw "compiler error: no type for php struct spec field";
}
+
}
/**
* Generates the struct specification structure, which fully qualifies enough
* type information to generalize serialization routines.
*/
-void t_php_generator::generate_php_struct_spec(ofstream& out, t_struct* tstruct) {
+void t_php_generator::generate_php_struct_spec(ofstream& out,
+ t_struct* tstruct) {
indent(out) << "if (!isset(self::$_TSPEC)) {" << endl;
indent_up();
@@ -713,7 +752,8 @@
t_type* t = get_true_type((*m_iter)->get_type());
indent(out) << (*m_iter)->get_key() << " => array(" << endl;
indent_up();
- out << indent() << "'var' => '" << (*m_iter)->get_name() << "'," << endl;
+ out <<
+ indent() << "'var' => '" << (*m_iter)->get_name() << "'," << endl;
generate_php_type_spec(out, t);
indent(out) << ")," << endl;
indent_down();
@@ -740,18 +780,18 @@
vector<t_field*>::const_iterator m_iter;
generate_php_doc(out, tstruct);
- out << "class " << php_namespace_declaration(tstruct);
+ out <<
+ "class " << php_namespace_declaration(tstruct);
if (is_exception) {
- out << " extends "
- << "TException";
+ out << " extends " << "TException";
} else if (oop_) {
- out << " extends "
- << "TBase";
+ out << " extends " << "TBase";
}
if (json_serializable_) {
out << " implements JsonSerializable";
}
- out << " {" << endl;
+ out <<
+ " {" << endl;
indent_up();
indent(out) << "static $_TSPEC;" << endl << endl;
@@ -763,14 +803,16 @@
dval = render_const_value((*m_iter)->get_type(), (*m_iter)->get_value());
}
generate_php_doc(out, *m_iter);
- indent(out) << "public $" << (*m_iter)->get_name() << " = " << dval << ";" << endl;
+ indent(out) <<
+ "public $" << (*m_iter)->get_name() << " = " << dval << ";" << endl;
}
out << endl;
// Generate constructor from array
string param = (members.size() > 0) ? "$vals=null" : "";
- out << indent() << "public function __construct(" << param << ") {" << endl;
+ out <<
+ indent() << "public function __construct(" << param << ") {" << endl;
indent_up();
generate_php_struct_spec(out, tstruct);
@@ -779,29 +821,34 @@
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* t = get_true_type((*m_iter)->get_type());
if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
- indent(out) << "$this->" << (*m_iter)->get_name() << " = "
- << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
+ indent(out) << "$this->" << (*m_iter)->get_name() << " = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
}
}
- out << indent() << "if (is_array($vals)) {" << endl;
+ out <<
+ indent() << "if (is_array($vals)) {" << endl;
indent_up();
if (oop_) {
out << indent() << "parent::__construct(self::$_TSPEC, $vals);" << endl;
} else {
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;
+ 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;
+ out <<
+ indent() << "}" << endl;
}
scope_down(out);
out << endl;
- out << indent() << "public function getName() {" << endl << indent() << " return '"
- << tstruct->get_name() << "';" << endl << indent() << "}" << endl << endl;
+ out <<
+ indent() << "public function getName() {" << endl <<
+ indent() << " return '" << tstruct->get_name() << "';" << endl <<
+ indent() << "}" << endl <<
+ endl;
generate_php_struct_reader(out, tstruct, is_result);
generate_php_struct_writer(out, tstruct, is_result);
@@ -816,135 +863,161 @@
}
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
* Generates the read() method for a struct
*/
-void t_php_generator::generate_php_struct_reader(ofstream& out, t_struct* tstruct, bool is_result) {
+void t_php_generator::generate_php_struct_reader(ofstream& out,
+ t_struct* tstruct,
+ bool is_result) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- indent(out) << "public function read($input)" << endl;
+ indent(out) <<
+ "public function read($input)" << endl;
scope_up(out);
if (oop_) {
if (needs_php_read_validator(tstruct, is_result)) {
- indent(out) << "$tmp = $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);"
- << endl;
+ indent(out) << "$tmp = $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);" << endl;
indent(out) << "$this->_validateForRead();" << endl;
indent(out) << "return $tmp;" << endl;
} else {
- indent(out) << "return $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);"
- << endl;
+ indent(out) << "return $this->_read('" << tstruct->get_name() << "', self::$_TSPEC, $input);" << endl;
}
scope_down(out);
out << endl;
return;
}
- out << indent() << "$xfer = 0;" << endl << indent() << "$fname = null;" << endl << indent()
- << "$ftype = 0;" << endl << indent() << "$fid = 0;" << endl;
+ out <<
+ indent() << "$xfer = 0;" << endl <<
+ indent() << "$fname = null;" << endl <<
+ indent() << "$ftype = 0;" << endl <<
+ indent() << "$fid = 0;" << endl;
// Declare stack tmp variables
if (!binary_inline_) {
- indent(out) << "$xfer += $input->readStructBegin($fname);" << endl;
+ indent(out) <<
+ "$xfer += $input->readStructBegin($fname);" << endl;
}
// Loop over reading in fields
- indent(out) << "while (true)" << endl;
+ indent(out) <<
+ "while (true)" << endl;
- scope_up(out);
+ scope_up(out);
- // Read beginning field marker
- if (binary_inline_) {
- t_field fftype(g_type_byte, "ftype");
- t_field ffid(g_type_i16, "fid");
- generate_deserialize_field(out, &fftype);
- out << indent() << "if ($ftype == "
- << "TType::STOP) {" << endl << indent() << " break;" << endl << indent() << "}" << endl;
- generate_deserialize_field(out, &ffid);
- } else {
- indent(out) << "$xfer += $input->readFieldBegin($fname, $ftype, $fid);" << endl;
- // Check for field STOP marker and break
- indent(out) << "if ($ftype == "
- << "TType::STOP) {" << endl;
- indent_up();
- indent(out) << "break;" << endl;
- indent_down();
- indent(out) << "}" << endl;
- }
-
- // Switch statement on the field we are reading
- indent(out) << "switch ($fid)" << endl;
-
- scope_up(out);
-
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- indent(out) << "case " << (*f_iter)->get_key() << ":" << endl;
- indent_up();
- indent(out) << "if ($ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
- indent_up();
- generate_deserialize_field(out, *f_iter, "this->");
- indent_down();
- out << indent() << "} else {" << endl;
+ // Read beginning field marker
if (binary_inline_) {
- indent(out) << " $xfer += "
- << "TProtocol::skipBinary($input, $ftype);" << endl;
+ t_field fftype(g_type_byte, "ftype");
+ t_field ffid(g_type_i16, "fid");
+ generate_deserialize_field(out, &fftype);
+ out <<
+ indent() << "if ($ftype == " << "TType::STOP) {" << endl <<
+ indent() << " break;" << endl <<
+ indent() << "}" << endl;
+ generate_deserialize_field(out, &ffid);
} else {
- indent(out) << " $xfer += $input->skip($ftype);" << endl;
+ indent(out) <<
+ "$xfer += $input->readFieldBegin($fname, $ftype, $fid);" << endl;
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if ($ftype == " << "TType::STOP) {" << endl;
+ indent_up();
+ indent(out) <<
+ "break;" << endl;
+ indent_down();
+ indent(out) <<
+ "}" << endl;
}
- out << indent() << "}" << endl << indent() << "break;" << endl;
- indent_down();
- }
- // In the default case we skip the field
- indent(out) << "default:" << endl;
- if (binary_inline_) {
- indent(out) << " $xfer += "
- << "TProtocol::skipBinary($input, $ftype);" << endl;
- } else {
- indent(out) << " $xfer += $input->skip($ftype);" << endl;
- }
- indent(out) << " break;" << endl;
+ // Switch statement on the field we are reading
+ indent(out) <<
+ "switch ($fid)" << endl;
- scope_down(out);
+ scope_up(out);
+
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ indent(out) <<
+ "case " << (*f_iter)->get_key() << ":" << endl;
+ indent_up();
+ indent(out) << "if ($ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+ indent_up();
+ generate_deserialize_field(out, *f_iter, "this->");
+ indent_down();
+ out <<
+ indent() << "} else {" << endl;
+ if (binary_inline_) {
+ indent(out) << " $xfer += " << "TProtocol::skipBinary($input, $ftype);" << endl;
+ } else {
+ indent(out) << " $xfer += $input->skip($ftype);" << endl;
+ }
+ out <<
+ indent() << "}" << endl <<
+ indent() << "break;" << endl;
+ indent_down();
+ }
+
+ // In the default case we skip the field
+ indent(out) << "default:" << endl;
+ if (binary_inline_) {
+ indent(out) << " $xfer += " << "TProtocol::skipBinary($input, $ftype);" << endl;
+ } else {
+ indent(out) << " $xfer += $input->skip($ftype);" << endl;
+ }
+ indent(out) << " break;" << endl;
+
+ scope_down(out);
+
+ if (!binary_inline_) {
+ // Read field end marker
+ indent(out) <<
+ "$xfer += $input->readFieldEnd();" << endl;
+ }
+
+ scope_down(out);
if (!binary_inline_) {
- // Read field end marker
- indent(out) << "$xfer += $input->readFieldEnd();" << endl;
- }
-
- scope_down(out);
-
- if (!binary_inline_) {
- indent(out) << "$xfer += $input->readStructEnd();" << endl;
+ indent(out) <<
+ "$xfer += $input->readStructEnd();" << endl;
}
if (needs_php_read_validator(tstruct, is_result)) {
- indent(out) << "$this->_validateForRead();" << endl;
+ indent(out) <<
+ "$this->_validateForRead();" << endl;
}
- indent(out) << "return $xfer;" << endl;
+ indent(out) <<
+ "return $xfer;" << endl;
indent_down();
- out << indent() << "}" << endl << endl;
+ out <<
+ indent() << "}" << endl <<
+ endl;
}
/**
* Generates the write() method for a struct
*/
-void t_php_generator::generate_php_struct_writer(ofstream& out, t_struct* tstruct, bool is_result) {
+void t_php_generator::generate_php_struct_writer(ofstream& out,
+ t_struct* tstruct,
+ bool is_result) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
if (binary_inline_) {
- indent(out) << "public function write(&$output) {" << endl;
+ indent(out) <<
+ "public function write(&$output) {" << endl;
} else {
- indent(out) << "public function write($output) {" << endl;
+ indent(out) <<
+ "public function write($output) {" << endl;
}
indent_up();
@@ -953,21 +1026,23 @@
}
if (oop_) {
- indent(out) << "return $this->_write('" << tstruct->get_name() << "', self::$_TSPEC, $output);"
- << endl;
+ indent(out) << "return $this->_write('" << tstruct->get_name() << "', self::$_TSPEC, $output);" << endl;
scope_down(out);
out << endl;
return;
}
- indent(out) << "$xfer = 0;" << endl;
+ indent(out) <<
+ "$xfer = 0;" << endl;
if (!binary_inline_) {
- indent(out) << "$xfer += $output->writeStructBegin('" << name << "');" << endl;
+ indent(out) <<
+ "$xfer += $output->writeStructBegin('" << name << "');" << endl;
}
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- out << indent() << "if ($this->" << (*f_iter)->get_name() << " !== null) {" << endl;
+ out <<
+ indent() << "if ($this->" << (*f_iter)->get_name() << " !== null) {" << endl;
indent_up();
t_type* type = get_true_type((*f_iter)->get_type());
@@ -978,23 +1053,25 @@
expect = "object";
}
if (!expect.empty()) {
- out << indent() << "if (!is_" << expect << "($this->" << (*f_iter)->get_name() << ")) {"
- << endl;
+ out <<
+ indent() << "if (!is_" << expect << "($this->" << (*f_iter)->get_name() << ")) {" << endl;
indent_up();
- out << indent() << "throw new "
- << "TProtocolException('Bad type in structure.', "
- << "TProtocolException::INVALID_DATA);" << endl;
+ out <<
+ indent() << "throw new " << "TProtocolException('Bad type in structure.', " << "TProtocolException::INVALID_DATA);" << endl;
scope_down(out);
}
// Write field header
if (binary_inline_) {
- out << indent() << "$output .= pack('c', " << type_to_enum((*f_iter)->get_type()) << ");"
- << endl << indent() << "$output .= pack('n', " << (*f_iter)->get_key() << ");" << endl;
+ out <<
+ indent() << "$output .= pack('c', " << type_to_enum((*f_iter)->get_type()) << ");" << endl <<
+ indent() << "$output .= pack('n', " << (*f_iter)->get_key() << ");" << endl;
} else {
- indent(out) << "$xfer += $output->writeFieldBegin("
- << "'" << (*f_iter)->get_name() << "', " << type_to_enum((*f_iter)->get_type())
- << ", " << (*f_iter)->get_key() << ");" << endl;
+ indent(out) <<
+ "$xfer += $output->writeFieldBegin(" <<
+ "'" << (*f_iter)->get_name() << "', " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ");" << endl;
}
// Write field contents
@@ -1002,32 +1079,38 @@
// Write field closer
if (!binary_inline_) {
- indent(out) << "$xfer += $output->writeFieldEnd();" << endl;
+ indent(out) <<
+ "$xfer += $output->writeFieldEnd();" << endl;
}
indent_down();
- indent(out) << "}" << endl;
+ indent(out) <<
+ "}" << endl;
}
if (binary_inline_) {
- out << indent() << "$output .= pack('c', "
- << "TType::STOP);" << endl;
+ out <<
+ indent() << "$output .= pack('c', " << "TType::STOP);" << endl;
} else {
- out << indent() << "$xfer += $output->writeFieldStop();" << endl << indent()
- << "$xfer += $output->writeStructEnd();" << endl;
+ out <<
+ indent() << "$xfer += $output->writeFieldStop();" << endl <<
+ indent() << "$xfer += $output->writeStructEnd();" << endl;
}
- out << indent() << "return $xfer;" << endl;
+ out <<
+ indent() << "return $xfer;" << endl;
indent_down();
out << indent() << "}" << endl << endl;
}
-void t_php_generator::generate_php_struct_read_validator(ofstream& out, t_struct* tstruct) {
+void t_php_generator::generate_php_struct_read_validator(ofstream& out,
+ t_struct* tstruct) {
generate_php_struct_required_validator(out, tstruct, "_validateForRead", false);
}
-void t_php_generator::generate_php_struct_write_validator(ofstream& out, t_struct* tstruct) {
+void t_php_generator::generate_php_struct_write_validator(ofstream& out,
+ t_struct* tstruct) {
generate_php_struct_required_validator(out, tstruct, "_validateForWrite", true);
}
@@ -1035,7 +1118,8 @@
t_struct* tstruct,
std::string method_name,
bool write_mode) {
- indent(out) << "private function " << method_name << "() {" << endl;
+ indent(out) <<
+ "private function " << method_name << "() {" << endl;
indent_up();
const vector<t_field*>& fields = tstruct->get_members();
@@ -1045,14 +1129,17 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = (*f_iter);
- if (field->get_req() == t_field::T_REQUIRED
- || (field->get_req() == t_field::T_OPT_IN_REQ_OUT && write_mode)) {
- indent(out) << "if ($this->" << field->get_name() << " === null) {" << endl;
+ if (field->get_req() == t_field::T_REQUIRED ||
+ (field->get_req() == t_field::T_OPT_IN_REQ_OUT && write_mode)) {
+ indent(out) <<
+ "if ($this->" << field->get_name() << " === null) {" << endl;
indent_up();
- indent(out) << "throw new TProtocolException('Required field " << tstruct->get_name() << "."
- << field->get_name() << " is unset!');" << endl;
+ indent(out) <<
+ "throw new TProtocolException('Required field " <<
+ tstruct->get_name() << "." << field->get_name() << " is unset!');" << endl;
indent_down();
- indent(out) << "}" << endl;
+ indent(out) <<
+ "}" << endl;
}
}
}
@@ -1061,10 +1148,13 @@
indent(out) << "}" << endl << endl;
}
+
+
void t_php_generator::generate_php_struct_json_serialize(ofstream& out,
t_struct* tstruct,
bool is_result) {
- indent(out) << "public function jsonSerialize() {" << endl;
+ indent(out) <<
+ "public function jsonSerialize() {" << endl;
indent_up();
if (needs_php_write_validator(tstruct, is_result)) {
@@ -1099,14 +1189,15 @@
indent(out) << "}" << endl << endl;
}
-int t_php_generator::get_php_num_required_fields(const vector<t_field*>& fields, bool write_mode) {
+int t_php_generator::get_php_num_required_fields(const vector<t_field*>& fields,
+ bool write_mode) {
int num_req = 0;
if (fields.size() > 0) {
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if ((*f_iter)->get_req() == t_field::T_REQUIRED
- || ((*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT && write_mode)) {
+ if ((*f_iter)->get_req() == t_field::T_REQUIRED ||
+ ((*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT && write_mode)) {
++num_req;
}
}
@@ -1115,13 +1206,16 @@
}
bool t_php_generator::needs_php_write_validator(t_struct* tstruct, bool is_result) {
- return (validate_ && !is_result && !tstruct->is_union()
- && get_php_num_required_fields(tstruct->get_members(), true) > 0);
+ return (validate_ &&
+ !is_result &&
+ !tstruct->is_union() &&
+ get_php_num_required_fields(tstruct->get_members(), true) > 0);
}
bool t_php_generator::needs_php_read_validator(t_struct* tstruct, bool is_result) {
- return (validate_ && !is_result
- && (get_php_num_required_fields(tstruct->get_members(), false) > 0));
+ return (validate_ &&
+ !is_result &&
+ (get_php_num_required_fields(tstruct->get_members(), false) > 0));
}
/**
@@ -1130,14 +1224,16 @@
* @param tservice The service definition
*/
void t_php_generator::generate_service(t_service* tservice) {
- string f_service_name = package_dir_ + service_name_ + ".php";
+ string f_service_name = package_dir_+service_name_+".php";
f_service_.open(f_service_name.c_str());
f_service_ << "<?php" << endl;
f_service_ << "namespace " << php_namespace_suffix(tservice->get_program()) << ";" << endl;
- f_service_ << autogen_comment() << php_includes();
+ f_service_ << autogen_comment() <<
+ php_includes();
- f_service_ << endl;
+ f_service_ <<
+ endl;
// Generate the three main parts of the service (well, two for now in PHP)
generate_service_interface(tservice);
@@ -1169,32 +1265,42 @@
string extends_processor = "";
if (tservice->get_extends() != NULL) {
extends = tservice->get_extends()->get_name();
- extends_processor = " extends " + php_namespace(tservice->get_extends()->get_program())
- + extends + "Processor";
+ extends_processor = " extends " + php_namespace(tservice->get_extends()->get_program()) + extends + "Processor";
}
// Generate the header portion
- f_service_ << "class " << service_name_ << "Processor" << extends_processor << " {" << endl;
+ f_service_ <<
+ "class " << service_name_ << "Processor" << extends_processor << " {" << endl;
indent_up();
if (extends.empty()) {
- f_service_ << indent() << "protected $handler_ = null;" << endl;
+ f_service_ <<
+ indent() << "protected $handler_ = null;" << endl;
}
- f_service_ << indent() << "public function __construct($handler) {" << endl;
+ f_service_ <<
+ indent() << "public function __construct($handler) {" << endl;
if (extends.empty()) {
- f_service_ << indent() << " $this->handler_ = $handler;" << endl;
+ f_service_ <<
+ indent() << " $this->handler_ = $handler;" << endl;
} else {
- f_service_ << indent() << " parent::__construct($handler);" << endl;
+ f_service_ <<
+ indent() << " parent::__construct($handler);" << endl;
}
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
// Generate the server implementation
- indent(f_service_) << "public function process($input, $output) {" << endl;
+ indent(f_service_) <<
+ "public function process($input, $output) {" << endl;
indent_up();
- f_service_ << indent() << "$rseqid = 0;" << endl << indent() << "$fname = null;" << endl
- << indent() << "$mtype = 0;" << endl << endl;
+ f_service_ <<
+ indent() << "$rseqid = 0;" << endl <<
+ indent() << "$fname = null;" << endl <<
+ indent() << "$mtype = 0;" << endl <<
+ endl;
if (binary_inline_) {
t_field ffname(g_type_string, "fname");
@@ -1204,32 +1310,36 @@
generate_deserialize_field(f_service_, &fmtype, "", true);
generate_deserialize_field(f_service_, &fseqid, "", true);
} else {
- f_service_ << indent() << "$input->readMessageBegin($fname, $mtype, $rseqid);" << endl;
+ f_service_ <<
+ indent() << "$input->readMessageBegin($fname, $mtype, $rseqid);" << endl;
}
// HOT: check for method implementation
- f_service_ << indent() << "$methodname = 'process_'.$fname;" << endl << indent()
- << "if (!method_exists($this, $methodname)) {" << endl;
+ f_service_ <<
+ indent() << "$methodname = 'process_'.$fname;" << endl <<
+ indent() << "if (!method_exists($this, $methodname)) {" << endl;
if (binary_inline_) {
- f_service_ << indent() << " throw new \\Exception('Function '.$fname.' not implemented.');"
- << endl;
+ f_service_ <<
+ indent() << " throw new \\Exception('Function '.$fname.' not implemented.');" << endl;
} else {
- f_service_ << indent() << " $input->skip("
- << "TType::STRUCT);" << endl << indent() << " $input->readMessageEnd();" << endl
- << indent() << " $x = new "
- << "TApplicationException('Function '.$fname.' not implemented.', "
- << "TApplicationException::UNKNOWN_METHOD);" << endl << indent()
- << " $output->writeMessageBegin($fname, "
- << "TMessageType::EXCEPTION, $rseqid);" << endl << indent()
- << " $x->write($output);" << endl << indent() << " $output->writeMessageEnd();"
- << endl << indent() << " $output->getTransport()->flush();" << endl << indent()
- << " return;" << endl;
+ f_service_ <<
+ indent() << " $input->skip(" << "TType::STRUCT);" << endl <<
+ indent() << " $input->readMessageEnd();" << endl <<
+ indent() << " $x = new " << "TApplicationException('Function '.$fname.' not implemented.', " << "TApplicationException::UNKNOWN_METHOD);" << endl <<
+ indent() << " $output->writeMessageBegin($fname, " << "TMessageType::EXCEPTION, $rseqid);" << endl <<
+ indent() << " $x->write($output);" << endl <<
+ indent() << " $output->writeMessageEnd();" << endl <<
+ indent() << " $output->getTransport()->flush();" << endl <<
+ indent() << " return;" << endl;
}
- f_service_ << indent() << "}" << endl << indent()
- << "$this->$methodname($rseqid, $input, $output);" << endl << indent()
- << "return true;" << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "$this->$methodname($rseqid, $input, $output);" << endl <<
+ indent() << "return true;" << endl;
indent_down();
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
// Generate the process subfunctions
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
@@ -1245,21 +1355,23 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_php_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
+void t_php_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
// Open function
- indent(f_service_) << "protected function process_" << tfunction->get_name()
- << "($seqid, $input, $output) {" << endl;
+ indent(f_service_) <<
+ "protected function process_" << tfunction->get_name() <<
+ "($seqid, $input, $output) {" << endl;
indent_up();
- string argsname = php_namespace(tservice->get_program()) + service_name_ + "_"
- + tfunction->get_name() + "_args";
- string resultname = php_namespace(tservice->get_program()) + service_name_ + "_"
- + tfunction->get_name() + "_result";
+ string argsname = php_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_args";
+ string resultname = php_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_result";
- f_service_ << indent() << "$args = new " << argsname << "();" << endl << indent()
- << "$args->read($input);" << endl;
+ f_service_ <<
+ indent() << "$args = new " << argsname << "();" << endl <<
+ indent() << "$args->read($input);" << endl;
if (!binary_inline_) {
- f_service_ << indent() << "$input->readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "$input->readMessageEnd();" << endl;
}
t_struct* xs = tfunction->get_xceptions();
@@ -1268,12 +1380,14 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "$result = new " << resultname << "();" << endl;
+ f_service_ <<
+ indent() << "$result = new " << resultname << "();" << endl;
}
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "try {" << endl;
+ f_service_ <<
+ indent() << "try {" << endl;
indent_up();
}
@@ -1286,7 +1400,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "$result->success = ";
}
- f_service_ << "$this->handler_->" << tfunction->get_name() << "(";
+ f_service_ <<
+ "$this->handler_->" << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1301,14 +1416,12 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "} catch ("
- << php_namespace(get_true_type((*x_iter)->get_type())->get_program())
- << (*x_iter)->get_type()->get_name() << " $" << (*x_iter)->get_name() << ") {"
- << endl;
+ f_service_ <<
+ indent() << "} catch (" << php_namespace(get_true_type((*x_iter)->get_type())->get_program()) << (*x_iter)->get_type()->get_name() << " $" << (*x_iter)->get_name() << ") {" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "$result->" << (*x_iter)->get_name() << " = $"
- << (*x_iter)->get_name() << ";" << endl;
+ f_service_ <<
+ indent() << "$result->" << (*x_iter)->get_name() << " = $" << (*x_iter)->get_name() << ";" << endl;
indent_down();
f_service_ << indent();
}
@@ -1318,47 +1431,53 @@
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return;" << endl;
+ f_service_ <<
+ indent() << "return;" << endl;
indent_down();
- f_service_ << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "}" << endl;
return;
}
- f_service_ << indent() << "$bin_accel = ($output instanceof "
- << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');"
- << endl;
+ f_service_ <<
+ indent() << "$bin_accel = ($output instanceof " << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');" << endl;
- f_service_ << indent() << "if ($bin_accel)" << endl;
+ f_service_ <<
+ indent() << "if ($bin_accel)" << endl;
scope_up(f_service_);
- f_service_ << indent() << "thrift_protocol_write_binary($output, '" << tfunction->get_name()
- << "', "
- << "TMessageType::REPLY, $result, $seqid, $output->isStrictWrite());" << endl;
+ f_service_ <<
+ indent() << "thrift_protocol_write_binary($output, '" << tfunction->get_name() << "', " << "TMessageType::REPLY, $result, $seqid, $output->isStrictWrite());" << endl;
scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
scope_up(f_service_);
// Serialize the request header
if (binary_inline_) {
- f_service_ << indent() << "$buff = pack('N', (0x80010000 | "
- << "TMessageType::REPLY)); " << endl << indent() << "$buff .= pack('N', strlen('"
- << tfunction->get_name() << "'));" << endl << indent() << "$buff .= '"
- << tfunction->get_name() << "';" << endl << indent() << "$buff .= pack('N', $seqid);"
- << endl << indent() << "$result->write($buff);" << endl << indent()
- << "$output->write($buff);" << endl << indent() << "$output->flush();" << endl;
+ f_service_ <<
+ indent() << "$buff = pack('N', (0x80010000 | " << "TMessageType::REPLY)); " << endl <<
+ indent() << "$buff .= pack('N', strlen('" << tfunction->get_name() << "'));" << endl <<
+ indent() << "$buff .= '" << tfunction->get_name() << "';" << endl <<
+ indent() << "$buff .= pack('N', $seqid);" << endl <<
+ indent() << "$result->write($buff);" << endl <<
+ indent() << "$output->write($buff);" << endl <<
+ indent() << "$output->flush();" << endl;
} else {
- f_service_ << indent() << "$output->writeMessageBegin('" << tfunction->get_name() << "', "
- << "TMessageType::REPLY, $seqid);" << endl << indent() << "$result->write($output);"
- << endl << indent() << "$output->writeMessageEnd();" << endl << indent()
- << "$output->getTransport()->flush();" << endl;
+ f_service_ <<
+ indent() << "$output->writeMessageBegin('" << tfunction->get_name() << "', " << "TMessageType::REPLY, $seqid);" << endl <<
+ indent() << "$result->write($output);" << endl <<
+ indent() << "$output->writeMessageEnd();" << endl <<
+ indent() << "$output->getTransport()->flush();" << endl;
}
scope_down(f_service_);
// Close function
indent_down();
- f_service_ << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "}" << endl;
}
/**
@@ -1370,7 +1489,8 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- f_service_ << "// HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
+ f_service_ <<
+ "// HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
@@ -1415,23 +1535,23 @@
string extends = "";
string extends_if = "";
if (tservice->get_extends() != NULL) {
- extends = " extends " + php_namespace(tservice->get_extends()->get_program())
- + tservice->get_extends()->get_name();
- extends_if = " extends " + php_namespace(tservice->get_extends()->get_program())
- + tservice->get_extends()->get_name() + "If";
+ extends = " extends " + php_namespace(tservice->get_extends()->get_program()) + tservice->get_extends()->get_name();
+ extends_if = " extends " + php_namespace(tservice->get_extends()->get_program()) + tservice->get_extends()->get_name() + "If";
}
generate_php_doc(f_service_, tservice);
- f_service_ << "interface " << php_namespace_declaration(tservice) << "If" << extends_if << " {"
- << endl;
+ f_service_ <<
+ "interface " << php_namespace_declaration(tservice) << "If" << extends_if << " {" << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
generate_php_doc(f_service_, *f_iter);
- indent(f_service_) << "public function " << function_signature(*f_iter) << ";" << endl;
+ indent(f_service_) <<
+ "public function " << function_signature(*f_iter) << ";" << endl;
}
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
/**
@@ -1441,25 +1561,30 @@
string extends = "";
string extends_if = "";
if (tservice->get_extends() != NULL) {
- extends = " extends " + php_namespace(tservice->get_extends()->get_program())
- + tservice->get_extends()->get_name();
- extends_if = " extends " + php_namespace(tservice->get_extends()->get_program())
- + tservice->get_extends()->get_name() + "Rest";
+ extends = " extends " + php_namespace(tservice->get_extends()->get_program()) + tservice->get_extends()->get_name();
+ extends_if = " extends " + php_namespace(tservice->get_extends()->get_program()) + tservice->get_extends()->get_name() + "Rest";
}
- f_service_ << "class " << service_name_ << "Rest" << extends_if << " {" << endl;
+ f_service_ <<
+ "class " << service_name_ << "Rest" << extends_if << " {" << endl;
indent_up();
if (extends.empty()) {
- f_service_ << indent() << "protected $impl_;" << endl << endl;
+ f_service_ <<
+ indent() << "protected $impl_;" << endl <<
+ endl;
}
- f_service_ << indent() << "public function __construct($impl) {" << endl << indent()
- << " $this->impl_ = $impl;" << endl << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "public function __construct($impl) {" << endl <<
+ indent() << " $this->impl_ = $impl;" << endl <<
+ indent() << "}" << endl <<
+ endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- indent(f_service_) << "public function " << (*f_iter)->get_name() << "($request) {" << endl;
+ indent(f_service_) <<
+ "public function " << (*f_iter)->get_name() << "($request) {" << endl;
indent_up();
const vector<t_field*>& args = (*f_iter)->get_arglist()->get_members();
vector<t_field*>::const_iterator a_iter;
@@ -1468,35 +1593,39 @@
string cast = type_to_cast(atype);
string req = "$request['" + (*a_iter)->get_name() + "']";
if (atype->is_bool()) {
- f_service_ << indent() << "$" << (*a_iter)->get_name() << " = " << cast << "(!empty(" << req
- << ") && (" << req << " !== 'false'));" << endl;
+ f_service_ <<
+ indent() << "$" << (*a_iter)->get_name() << " = " << cast << "(!empty(" << req << ") && (" << req << " !== 'false'));" << endl;
} else {
- f_service_ << indent() << "$" << (*a_iter)->get_name() << " = isset(" << req << ") ? "
- << cast << req << " : null;" << endl;
+ f_service_ <<
+ indent() << "$" << (*a_iter)->get_name() << " = isset(" << req << ") ? " << cast << req << " : null;" << endl;
}
- if (atype->is_string() && ((t_base_type*)atype)->is_string_list()) {
- f_service_ << indent() << "$" << (*a_iter)->get_name() << " = explode(',', $"
- << (*a_iter)->get_name() << ");" << endl;
+ if (atype->is_string() &&
+ ((t_base_type*)atype)->is_string_list()) {
+ f_service_ <<
+ indent() << "$" << (*a_iter)->get_name() << " = explode(',', $" << (*a_iter)->get_name() << ");" << endl;
} else if (atype->is_map() || atype->is_list()) {
- f_service_ << indent() << "$" << (*a_iter)->get_name() << " = json_decode($"
- << (*a_iter)->get_name() << ", true);" << endl;
+ f_service_ <<
+ indent() << "$" << (*a_iter)->get_name() << " = json_decode($" << (*a_iter)->get_name() << ", true);" << endl;
} else if (atype->is_set()) {
- f_service_ << indent() << "$" << (*a_iter)->get_name() << " = array_fill_keys(json_decode($"
- << (*a_iter)->get_name() << ", true), 1);" << endl;
+ f_service_ <<
+ indent() << "$" << (*a_iter)->get_name() << " = array_fill_keys(json_decode($" << (*a_iter)->get_name() << ", true), 1);" << endl;
} else if (atype->is_struct() || atype->is_xception()) {
- f_service_ << indent() << "if ($" << (*a_iter)->get_name() << " !== null) {" << endl
- << indent() << " $" << (*a_iter)->get_name() << " = new "
- << php_namespace(atype->get_program()) << atype->get_name() << "(json_decode($"
- << (*a_iter)->get_name() << ", true));" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if ($" << (*a_iter)->get_name() << " !== null) {" << endl <<
+ indent() << " $" << (*a_iter)->get_name() << " = new " << php_namespace(atype->get_program()) << atype->get_name() << "(json_decode($" << (*a_iter)->get_name() << ", true));" << endl <<
+ indent() << "}" << endl;
}
}
- f_service_ << indent() << "return $this->impl_->" << (*f_iter)->get_name() << "("
- << argument_list((*f_iter)->get_arglist(), false) << ");" << endl;
+ f_service_ <<
+ indent() << "return $this->impl_->" << (*f_iter)->get_name() << "(" << argument_list((*f_iter)->get_arglist(), false) << ");" << endl;
indent_down();
- indent(f_service_) << "}" << endl << endl;
+ indent(f_service_) <<
+ "}" << endl <<
+ endl;
}
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
/**
@@ -1509,31 +1638,37 @@
string extends_client = "";
if (tservice->get_extends() != NULL) {
extends = tservice->get_extends()->get_name();
- extends_client = " extends " + php_namespace(tservice->get_extends()->get_program()) + extends
- + "Client";
+ extends_client = " extends " + php_namespace(tservice->get_extends()->get_program()) + extends + "Client";
}
- f_service_ << "class " << php_namespace_declaration(tservice) << "Client" << extends_client
- << " implements " << php_namespace(tservice->get_program()) << service_name_ << "If {"
- << endl;
+ f_service_ <<
+ "class " << php_namespace_declaration(tservice) << "Client" << extends_client << " implements " << php_namespace(tservice->get_program()) << service_name_ << "If {" << endl;
indent_up();
// Private members
if (extends.empty()) {
- f_service_ << indent() << "protected $input_ = null;" << endl << indent()
- << "protected $output_ = null;" << endl << endl;
- f_service_ << indent() << "protected $seqid_ = 0;" << endl << endl;
+ f_service_ <<
+ indent() << "protected $input_ = null;" << endl <<
+ indent() << "protected $output_ = null;" << endl <<
+ endl;
+ f_service_ <<
+ indent() << "protected $seqid_ = 0;" << endl <<
+ endl;
}
// Constructor function
- f_service_ << indent() << "public function __construct($input, $output=null) {" << endl;
+ f_service_ <<
+ indent() << "public function __construct($input, $output=null) {" << endl;
if (!extends.empty()) {
- f_service_ << indent() << " parent::__construct($input, $output);" << endl;
+ f_service_ <<
+ indent() << " parent::__construct($input, $output);" << endl;
} else {
- f_service_ << indent() << " $this->input_ = $input;" << endl << indent()
- << " $this->output_ = $output ? $output : $input;" << endl;
+ f_service_ <<
+ indent() << " $this->input_ = $input;" << endl <<
+ indent() << " $this->output_ = $output ? $output : $input;" << endl;
}
- f_service_ << indent() << "}" << endl << endl;
+ f_service_ <<
+ indent() << "}" << endl << endl;
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
@@ -1545,192 +1680,220 @@
string funname = (*f_iter)->get_name();
// Open function
- indent(f_service_) << "public function " << function_signature(*f_iter) << endl;
+ indent(f_service_) <<
+ "public function " << function_signature(*f_iter) << endl;
scope_up(f_service_);
- indent(f_service_) << "$this->send_" << funname << "(";
+ indent(f_service_) <<
+ "$this->send_" << funname << "(";
- bool first = true;
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
+ bool first = true;
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << "$" << (*fld_iter)->get_name();
}
- f_service_ << "$" << (*fld_iter)->get_name();
- }
- f_service_ << ");" << endl;
+ f_service_ << ");" << endl;
- if (!(*f_iter)->is_oneway()) {
- f_service_ << indent();
- if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << "return ";
+ if (!(*f_iter)->is_oneway()) {
+ f_service_ << indent();
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ << "return ";
+ }
+ f_service_ <<
+ "$this->recv_" << funname << "();" << endl;
}
- f_service_ << "$this->recv_" << funname << "();" << endl;
- }
scope_down(f_service_);
f_service_ << endl;
- indent(f_service_) << "public function send_" << function_signature(*f_iter) << endl;
+ indent(f_service_) <<
+ "public function send_" << function_signature(*f_iter) << endl;
scope_up(f_service_);
- std::string argsname = php_namespace(tservice->get_program()) + service_name_ + "_"
- + (*f_iter)->get_name() + "_args";
+ std::string argsname = php_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_args";
- f_service_ << indent() << "$args = new " << argsname << "();" << endl;
+ f_service_ <<
+ indent() << "$args = new " << argsname << "();" << endl;
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "$args->" << (*fld_iter)->get_name() << " = $"
- << (*fld_iter)->get_name() << ";" << endl;
- }
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ <<
+ indent() << "$args->" << (*fld_iter)->get_name() << " = $" << (*fld_iter)->get_name() << ";" << endl;
+ }
- f_service_ << indent() << "$bin_accel = ($this->output_ instanceof "
- << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');"
- << endl;
+ f_service_ <<
+ indent() << "$bin_accel = ($this->output_ instanceof " << "TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary');" << endl;
- f_service_ << indent() << "if ($bin_accel)" << endl;
- scope_up(f_service_);
+ f_service_ <<
+ indent() << "if ($bin_accel)" << endl;
+ scope_up(f_service_);
- string messageType = (*f_iter)->is_oneway() ? "TMessageType::ONEWAY" : "TMessageType::CALL";
+ string messageType = (*f_iter)->is_oneway() ? "TMessageType::ONEWAY" : "TMessageType::CALL";
- f_service_ << indent() << "thrift_protocol_write_binary($this->output_, '"
- << (*f_iter)->get_name() << "', " << messageType
- << ", $args, $this->seqid_, $this->output_->isStrictWrite());" << endl;
+ f_service_ <<
+ indent() << "thrift_protocol_write_binary($this->output_, '"
+ << (*f_iter)->get_name() << "', " << messageType
+ << ", $args, $this->seqid_, $this->output_->isStrictWrite());" << endl;
- scope_down(f_service_);
- f_service_ << indent() << "else" << endl;
- scope_up(f_service_);
+ scope_down(f_service_);
+ f_service_ <<
+ indent() << "else" << endl;
+ scope_up(f_service_);
- // Serialize the request header
- if (binary_inline_) {
- f_service_ << indent() << "$buff = pack('N', (0x80010000 | " << messageType << "));" << endl
- << indent() << "$buff .= pack('N', strlen('" << funname << "'));" << endl
- << indent() << "$buff .= '" << funname << "';" << endl << indent()
- << "$buff .= pack('N', $this->seqid_);" << endl;
- } else {
- f_service_ << indent() << "$this->output_->writeMessageBegin('" << (*f_iter)->get_name()
- << "', " << messageType << ", $this->seqid_);" << endl;
- }
+ // Serialize the request header
+ if (binary_inline_) {
+ f_service_ <<
+ indent() << "$buff = pack('N', (0x80010000 | " << messageType
+ << "));" << endl <<
+ indent() << "$buff .= pack('N', strlen('" << funname << "'));" << endl <<
+ indent() << "$buff .= '" << funname << "';" << endl <<
+ indent() << "$buff .= pack('N', $this->seqid_);" << endl;
+ } else {
+ f_service_ <<
+ indent() << "$this->output_->writeMessageBegin('" << (*f_iter)->get_name() << "', "
+ << messageType << ", $this->seqid_);" << endl;
+ }
- // Write to the stream
- if (binary_inline_) {
- f_service_ << indent() << "$args->write($buff);" << endl << indent()
- << "$this->output_->write($buff);" << endl << indent()
- << "$this->output_->flush();" << endl;
- } else {
- f_service_ << indent() << "$args->write($this->output_);" << endl << indent()
- << "$this->output_->writeMessageEnd();" << endl << indent()
- << "$this->output_->getTransport()->flush();" << endl;
- }
+ // Write to the stream
+ if (binary_inline_) {
+ f_service_ <<
+ indent() << "$args->write($buff);" << endl <<
+ indent() << "$this->output_->write($buff);" << endl <<
+ indent() << "$this->output_->flush();" << endl;
+ } else {
+ f_service_ <<
+ indent() << "$args->write($this->output_);" << endl <<
+ indent() << "$this->output_->writeMessageEnd();" << endl <<
+ indent() << "$this->output_->getTransport()->flush();" << endl;
+ }
scope_down(f_service_);
scope_down(f_service_);
+
if (!(*f_iter)->is_oneway()) {
- std::string resultname = php_namespace(tservice->get_program()) + service_name_ + "_"
- + (*f_iter)->get_name() + "_result";
+ std::string resultname = php_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_result";
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
string("recv_") + (*f_iter)->get_name(),
&noargs);
// Open function
- f_service_ << endl << indent() << "public function " << function_signature(&recv_function)
- << endl;
+ f_service_ <<
+ endl <<
+ indent() << "public function " << function_signature(&recv_function) << endl;
scope_up(f_service_);
- f_service_ << indent() << "$bin_accel = ($this->input_ instanceof "
- << "TBinaryProtocolAccelerated)"
+ f_service_ <<
+ indent() << "$bin_accel = ($this->input_ instanceof " << "TBinaryProtocolAccelerated)"
<< " && function_exists('thrift_protocol_read_binary');" << endl;
- f_service_ << indent()
- << "if ($bin_accel) $result = thrift_protocol_read_binary($this->input_, '"
- << resultname << "', $this->input_->isStrictRead());" << endl;
- f_service_ << indent() << "else" << endl;
+ f_service_ <<
+ indent() << "if ($bin_accel) $result = thrift_protocol_read_binary($this->input_, '" << resultname << "', $this->input_->isStrictRead());" << endl;
+ f_service_ <<
+ indent() << "else" << endl;
scope_up(f_service_);
- f_service_ << indent() << "$rseqid = 0;" << endl << indent() << "$fname = null;" << endl
- << indent() << "$mtype = 0;" << endl << endl;
+ f_service_ <<
+ indent() << "$rseqid = 0;" << endl <<
+ indent() << "$fname = null;" << endl <<
+ indent() << "$mtype = 0;" << endl <<
+ endl;
if (binary_inline_) {
t_field ffname(g_type_string, "fname");
t_field fseqid(g_type_i32, "rseqid");
- f_service_ << indent() << "$ver = unpack('N', $this->input_->readAll(4));" << endl
- << indent() << "$ver = $ver[1];" << endl << indent() << "$mtype = $ver & 0xff;"
- << endl << indent() << "$ver = $ver & 0xffff0000;" << endl << indent()
- << "if ($ver != 0x80010000) throw new "
- << "TProtocolException('Bad version identifier: '.$ver, "
- << "TProtocolException::BAD_VERSION);" << endl;
+ f_service_ <<
+ indent() << "$ver = unpack('N', $this->input_->readAll(4));" << endl <<
+ indent() << "$ver = $ver[1];" << endl <<
+ indent() << "$mtype = $ver & 0xff;" << endl <<
+ indent() << "$ver = $ver & 0xffff0000;" << endl <<
+ indent() << "if ($ver != 0x80010000) throw new " << "TProtocolException('Bad version identifier: '.$ver, " << "TProtocolException::BAD_VERSION);" << endl;
generate_deserialize_field(f_service_, &ffname, "", true);
generate_deserialize_field(f_service_, &fseqid, "", true);
} else {
- f_service_ << indent() << "$this->input_->readMessageBegin($fname, $mtype, $rseqid);"
- << endl << indent() << "if ($mtype == "
- << "TMessageType::EXCEPTION) {" << endl << indent() << " $x = new "
- << "TApplicationException();" << endl << indent() << " $x->read($this->input_);"
- << endl << indent() << " $this->input_->readMessageEnd();" << endl << indent()
- << " throw $x;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "$this->input_->readMessageBegin($fname, $mtype, $rseqid);" << endl <<
+ indent() << "if ($mtype == " << "TMessageType::EXCEPTION) {" << endl <<
+ indent() << " $x = new " << "TApplicationException();" << endl <<
+ indent() << " $x->read($this->input_);" << endl <<
+ indent() << " $this->input_->readMessageEnd();" << endl <<
+ indent() << " throw $x;" << endl <<
+ indent() << "}" << endl;
}
- f_service_ << indent() << "$result = new " << resultname << "();" << endl << indent()
- << "$result->read($this->input_);" << endl;
+ f_service_ <<
+ indent() << "$result = new " << resultname << "();" << endl <<
+ indent() << "$result->read($this->input_);" << endl;
if (!binary_inline_) {
- f_service_ << indent() << "$this->input_->readMessageEnd();" << endl;
+ f_service_ <<
+ indent() << "$this->input_->readMessageEnd();" << endl;
}
scope_down(f_service_);
// Careful, only return result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if ($result->success !== null) {" << endl << indent()
- << " return $result->success;" << endl << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if ($result->success !== null) {" << endl <<
+ indent() << " return $result->success;" << endl <<
+ indent() << "}" << endl;
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if ($result->" << (*x_iter)->get_name() << " !== null) {" << endl
- << indent() << " throw $result->" << (*x_iter)->get_name() << ";" << endl
- << indent() << "}" << endl;
+ f_service_ <<
+ indent() << "if ($result->" << (*x_iter)->get_name() << " !== null) {" << endl <<
+ indent() << " throw $result->" << (*x_iter)->get_name() << ";" << endl <<
+ indent() << "}" << endl;
}
// Careful, only return _result if not a void function
if ((*f_iter)->get_returntype()->is_void()) {
- indent(f_service_) << "return;" << endl;
+ indent(f_service_) <<
+ "return;" << endl;
} else {
- f_service_ << indent() << "throw new \\Exception(\"" << (*f_iter)->get_name()
- << " failed: unknown result\");" << endl;
+ f_service_ <<
+ indent() << "throw new \\Exception(\"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
- // Close function
- scope_down(f_service_);
- f_service_ << endl;
+ // Close function
+ scope_down(f_service_);
+ f_service_ << endl;
+
}
}
indent_down();
- f_service_ << "}" << endl << endl;
+ f_service_ <<
+ "}" << endl << endl;
}
/**
* Deserializes a field of any type.
*/
-void t_php_generator::generate_deserialize_field(ofstream& out,
+void t_php_generator::generate_deserialize_field(ofstream &out,
t_field* tfield,
string prefix,
bool inclass) {
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ generate_deserialize_struct(out,
+ (t_struct*)type,
+ name);
} else {
if (type->is_container()) {
@@ -1744,67 +1907,85 @@
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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
- out << indent() << "$len = unpack('N', " << itrans << "->readAll(4));" << endl
- << indent() << "$len = $len[1];" << endl << indent() << "if ($len > 0x7fffffff) {"
- << endl << indent() << " $len = 0 - (($len - 1) ^ 0xffffffff);" << endl << indent()
- << "}" << endl << indent() << "$" << name << " = " << itrans << "->readAll($len);"
- << endl;
+ out <<
+ indent() << "$len = unpack('N', " << itrans << "->readAll(4));" << endl <<
+ indent() << "$len = $len[1];" << endl <<
+ indent() << "if ($len > 0x7fffffff) {" << endl <<
+ indent() << " $len = 0 - (($len - 1) ^ 0xffffffff);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "$" << name << " = " << itrans << "->readAll($len);" << endl;
break;
case t_base_type::TYPE_BOOL:
- out << indent() << "$" << name << " = unpack('c', " << itrans << "->readAll(1));"
- << endl << indent() << "$" << name << " = (bool)$" << name << "[1];" << endl;
+ out <<
+ indent() << "$" << name << " = unpack('c', " << itrans << "->readAll(1));" << endl <<
+ indent() << "$" << name << " = (bool)$" << name << "[1];" << endl;
break;
case t_base_type::TYPE_BYTE:
- out << indent() << "$" << name << " = unpack('c', " << itrans << "->readAll(1));"
- << endl << indent() << "$" << name << " = $" << name << "[1];" << endl;
+ out <<
+ indent() << "$" << name << " = unpack('c', " << itrans << "->readAll(1));" << endl <<
+ indent() << "$" << name << " = $" << name << "[1];" << endl;
break;
case t_base_type::TYPE_I16:
- out << indent() << "$val = unpack('n', " << itrans << "->readAll(2));" << endl
- << indent() << "$val = $val[1];" << endl << indent() << "if ($val > 0x7fff) {"
- << endl << indent() << " $val = 0 - (($val - 1) ^ 0xffff);" << endl << indent()
- << "}" << endl << indent() << "$" << name << " = $val;" << endl;
+ out <<
+ indent() << "$val = unpack('n', " << itrans << "->readAll(2));" << endl <<
+ indent() << "$val = $val[1];" << endl <<
+ indent() << "if ($val > 0x7fff) {" << endl <<
+ indent() << " $val = 0 - (($val - 1) ^ 0xffff);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "$" << name << " = $val;" << endl;
break;
case t_base_type::TYPE_I32:
- out << indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl
- << indent() << "$val = $val[1];" << endl << indent() << "if ($val > 0x7fffffff) {"
- << endl << indent() << " $val = 0 - (($val - 1) ^ 0xffffffff);" << endl << indent()
- << "}" << endl << indent() << "$" << name << " = $val;" << endl;
+ out <<
+ indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl <<
+ indent() << "$val = $val[1];" << endl <<
+ indent() << "if ($val > 0x7fffffff) {" << endl <<
+ indent() << " $val = 0 - (($val - 1) ^ 0xffffffff);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "$" << name << " = $val;" << endl;
break;
case t_base_type::TYPE_I64:
- out << indent() << "$arr = unpack('N2', " << itrans << "->readAll(8));" << endl
- << indent() << "if ($arr[1] & 0x80000000) {" << endl << indent()
- << " $arr[1] = $arr[1] ^ 0xFFFFFFFF;" << endl << indent()
- << " $arr[2] = $arr[2] ^ 0xFFFFFFFF;" << endl << indent() << " $" << name
- << " = 0 - $arr[1]*4294967296 - $arr[2] - 1;" << endl << indent() << "} else {"
- << endl << indent() << " $" << name << " = $arr[1]*4294967296 + $arr[2];" << endl
- << indent() << "}" << endl;
+ out <<
+ indent() << "$arr = unpack('N2', " << itrans << "->readAll(8));" << endl <<
+ indent() << "if ($arr[1] & 0x80000000) {" << endl <<
+ indent() << " $arr[1] = $arr[1] ^ 0xFFFFFFFF;" << endl <<
+ indent() << " $arr[2] = $arr[2] ^ 0xFFFFFFFF;" << endl <<
+ indent() << " $" << name << " = 0 - $arr[1]*4294967296 - $arr[2] - 1;" << endl <<
+ indent() << "} else {" << endl <<
+ indent() << " $" << name << " = $arr[1]*4294967296 + $arr[2];" << endl <<
+ indent() << "}" << endl;
break;
case t_base_type::TYPE_DOUBLE:
- out << indent() << "$arr = unpack('d', strrev(" << itrans << "->readAll(8)));" << endl
- << indent() << "$" << name << " = $arr[1];" << endl;
+ out <<
+ indent() << "$arr = unpack('d', strrev(" << itrans << "->readAll(8)));" << endl <<
+ indent() << "$" << name << " = $arr[1];" << endl;
break;
default:
- throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase)
- + tfield->get_name();
+ throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase) + tfield->get_name();
}
} else if (type->is_enum()) {
- out << indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl << indent()
- << "$val = $val[1];" << endl << indent() << "if ($val > 0x7fffffff) {" << endl
- << indent() << " $val = 0 - (($val - 1) ^ 0xffffffff);" << endl << indent() << "}"
- << endl << indent() << "$" << name << " = $val;" << endl;
+ out <<
+ indent() << "$val = unpack('N', " << itrans << "->readAll(4));" << endl <<
+ indent() << "$val = $val[1];" << endl <<
+ indent() << "if ($val > 0x7fffffff) {" << endl <<
+ indent() << " $val = 0 - (($val - 1) ^ 0xffffffff);" << endl <<
+ indent() << "}" << endl <<
+ indent() << "$" << name << " = $val;" << endl;
}
} else {
- indent(out) << "$xfer += $input->";
+ indent(out) <<
+ "$xfer += $input->";
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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
out << "readString($" << name << ");";
@@ -1837,8 +2018,7 @@
}
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type->get_name().c_str());
+ tfield->get_name().c_str(), type->get_name().c_str());
}
}
}
@@ -1849,13 +2029,17 @@
* buffer for deserialization, and that there is a variable protocol which
* is a reference to a TProtocol serialization object.
*/
-void t_php_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- out << indent() << "$" << prefix << " = new " << php_namespace(tstruct->get_program())
- << tstruct->get_name() << "();" << endl << indent() << "$xfer += $" << prefix
- << "->read($input);" << endl;
+void t_php_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << "$" << prefix << " = new " << php_namespace(tstruct->get_program()) << tstruct->get_name() << "();" << endl <<
+ indent() << "$xfer += $" << prefix << "->read($input);" << endl;
}
-void t_php_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_php_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
string size = tmp("_size");
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
@@ -1866,56 +2050,63 @@
t_field fvtype(g_type_byte, vtype);
t_field fetype(g_type_byte, etype);
- out << indent() << "$" << prefix << " = array();" << endl << indent() << "$" << size << " = 0;"
- << endl;
+ out <<
+ indent() << "$" << prefix << " = array();" << endl <<
+ indent() << "$" << size << " = 0;" << endl;
// Declare variables, read header
if (ttype->is_map()) {
- out << indent() << "$" << ktype << " = 0;" << endl << indent() << "$" << vtype << " = 0;"
- << endl;
+ out <<
+ indent() << "$" << ktype << " = 0;" << endl <<
+ indent() << "$" << vtype << " = 0;" << endl;
if (binary_inline_) {
generate_deserialize_field(out, &fktype);
generate_deserialize_field(out, &fvtype);
generate_deserialize_field(out, &fsize);
} else {
- out << indent() << "$xfer += $input->readMapBegin("
- << "$" << ktype << ", $" << vtype << ", $" << size << ");" << endl;
+ out <<
+ indent() << "$xfer += $input->readMapBegin(" <<
+ "$" << ktype << ", $" << vtype << ", $" << size << ");" << endl;
}
} else if (ttype->is_set()) {
if (binary_inline_) {
generate_deserialize_field(out, &fetype);
generate_deserialize_field(out, &fsize);
} else {
- out << indent() << "$" << etype << " = 0;" << endl << indent()
- << "$xfer += $input->readSetBegin("
- << "$" << etype << ", $" << size << ");" << endl;
+ out <<
+ indent() << "$" << etype << " = 0;" << endl <<
+ indent() << "$xfer += $input->readSetBegin(" <<
+ "$" << etype << ", $" << size << ");" << endl;
}
} else if (ttype->is_list()) {
if (binary_inline_) {
generate_deserialize_field(out, &fetype);
generate_deserialize_field(out, &fsize);
} else {
- out << indent() << "$" << etype << " = 0;" << endl << indent()
- << "$xfer += $input->readListBegin("
- << "$" << etype << ", $" << size << ");" << endl;
+ out <<
+ indent() << "$" << etype << " = 0;" << endl <<
+ indent() << "$xfer += $input->readListBegin(" <<
+ "$" << etype << ", $" << size << ");" << endl;
}
}
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for ($" << i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")" << endl;
+ indent(out) <<
+ "for ($" <<
+ i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")" << endl;
- scope_up(out);
+ scope_up(out);
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- scope_down(out);
+ scope_down(out);
if (!binary_inline_) {
// Read container end
@@ -1929,29 +2120,38 @@
}
}
+
/**
* Generates code to deserialize a map
*/
-void t_php_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_php_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("key");
string val = tmp("val");
t_field fkey(tmap->get_key_type(), key);
t_field fval(tmap->get_val_type(), val);
- indent(out) << declare_field(&fkey, true, true) << endl;
- indent(out) << declare_field(&fval, true, true) << endl;
+ indent(out) <<
+ declare_field(&fkey, true, true) << endl;
+ indent(out) <<
+ declare_field(&fval, true, true) << endl;
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << "$" << prefix << "[$" << key << "] = $" << val << ";" << endl;
+ indent(out) <<
+ "$" << prefix << "[$" << key << "] = $" << val << ";" << endl;
}
-void t_php_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_php_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("elem");
t_field felem(tset->get_elem_type(), elem);
- indent(out) << "$" << elem << " = null;" << endl;
+ indent(out) <<
+ "$" << elem << " = null;" << endl;
generate_deserialize_field(out, &felem);
@@ -1962,37 +2162,47 @@
indent(out) << "}" << endl;
}
-void t_php_generator::generate_deserialize_list_element(ofstream& out,
+void t_php_generator::generate_deserialize_list_element(ofstream &out,
t_list* tlist,
string prefix) {
string elem = tmp("elem");
t_field felem(tlist->get_elem_type(), elem);
- indent(out) << "$" << elem << " = null;" << endl;
+ indent(out) <<
+ "$" << elem << " = null;" << endl;
generate_deserialize_field(out, &felem);
- indent(out) << "$" << prefix << " []= $" << elem << ";" << endl;
+ indent(out) <<
+ "$" << prefix << " []= $" << elem << ";" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_php_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_php_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
string name = prefix + tfield->get_name();
@@ -2002,46 +2212,56 @@
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
- out << indent() << "$output .= pack('N', strlen($" << name << "));" << endl << indent()
- << "$output .= $" << name << ";" << endl;
+ out <<
+ indent() << "$output .= pack('N', strlen($" << name << "));" << endl <<
+ indent() << "$output .= $" << name << ";" << endl;
break;
case t_base_type::TYPE_BOOL:
- out << indent() << "$output .= pack('c', $" << name << " ? 1 : 0);" << endl;
+ out <<
+ indent() << "$output .= pack('c', $" << name << " ? 1 : 0);" << endl;
break;
case t_base_type::TYPE_BYTE:
- out << indent() << "$output .= pack('c', $" << name << ");" << endl;
+ out <<
+ indent() << "$output .= pack('c', $" << name << ");" << endl;
break;
case t_base_type::TYPE_I16:
- out << indent() << "$output .= pack('n', $" << name << ");" << endl;
+ out <<
+ indent() << "$output .= pack('n', $" << name << ");" << endl;
break;
case t_base_type::TYPE_I32:
- out << indent() << "$output .= pack('N', $" << name << ");" << endl;
+ out <<
+ indent() << "$output .= pack('N', $" << name << ");" << endl;
break;
case t_base_type::TYPE_I64:
- out << indent() << "$output .= pack('N2', $" << name << " >> 32, $" << name
- << " & 0xFFFFFFFF);" << endl;
+ out <<
+ indent() << "$output .= pack('N2', $" << name << " >> 32, $" << name << " & 0xFFFFFFFF);" << endl;
break;
case t_base_type::TYPE_DOUBLE:
- out << indent() << "$output .= strrev(pack('d', $" << name << "));" << endl;
+ out <<
+ indent() << "$output .= strrev(pack('d', $" << name << "));" << endl;
break;
default:
throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
- out << indent() << "$output .= pack('N', $" << name << ");" << endl;
+ out <<
+ indent() << "$output .= pack('N', $" << name << ");" << endl;
}
} else {
- indent(out) << "$xfer += $output->";
+ indent(out) <<
+ "$xfer += $output->";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
out << "writeString($" << name << ");";
@@ -2086,49 +2306,58 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_php_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
- indent(out) << "$xfer += $" << prefix << "->write($output);" << endl;
+void t_php_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
+ indent(out) <<
+ "$xfer += $" << prefix << "->write($output);" << endl;
}
/**
* Writes out a container
*/
-void t_php_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_php_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
scope_up(out);
if (ttype->is_map()) {
if (binary_inline_) {
- out << indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_key_type())
- << ");" << endl << indent() << "$output .= pack('c', "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ");" << endl << indent()
- << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
+ out <<
+ indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_key_type()) << ");" << endl <<
+ indent() << "$output .= pack('c', " << type_to_enum(((t_map*)ttype)->get_val_type()) << ");" << endl <<
+ indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
} else {
- indent(out) << "$output->writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type())
- << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "count($" << prefix << "));" << endl;
+ indent(out) <<
+ "$output->writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "count($" << prefix << "));" << endl;
}
} else if (ttype->is_set()) {
if (binary_inline_) {
- out << indent() << "$output .= pack('c', " << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ");" << endl << indent() << "$output .= strrev(pack('l', count($" << prefix << ")));"
- << endl;
+ out <<
+ indent() << "$output .= pack('c', " << type_to_enum(((t_set*)ttype)->get_elem_type()) << ");" << endl <<
+ indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
} else {
- indent(out) << "$output->writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type())
- << ", "
- << "count($" << prefix << "));" << endl;
+ indent(out) <<
+ "$output->writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "count($" << prefix << "));" << endl;
}
} else if (ttype->is_list()) {
if (binary_inline_) {
- out << indent() << "$output .= pack('c', " << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ");" << endl << indent() << "$output .= strrev(pack('l', count($" << prefix << ")));"
- << endl;
+ out <<
+ indent() << "$output .= pack('c', " << type_to_enum(((t_list*)ttype)->get_elem_type()) << ");" << endl <<
+ indent() << "$output .= strrev(pack('l', count($" << prefix << ")));" << endl;
} else {
- indent(out) << "$output->writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ", "
- << "count($" << prefix << "));" << endl;
+ indent(out) <<
+ "$output->writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "count($" << prefix << "));" << endl;
}
}
@@ -2137,15 +2366,17 @@
if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
- indent(out) << "foreach ($" << prefix << " as "
- << "$" << kiter << " => $" << viter << ")" << endl;
+ indent(out) <<
+ "foreach ($" << prefix << " as " <<
+ "$" << kiter << " => $" << viter << ")" << endl;
scope_up(out);
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
scope_down(out);
} else if (ttype->is_set()) {
string iter = tmp("iter");
string iter_val = tmp("iter");
- indent(out) << "foreach ($" << prefix << " as $" << iter << " => $" << iter_val << ")" << endl;
+ indent(out) <<
+ "foreach ($" << prefix << " as $" << iter << " => $" << iter_val << ")" << endl;
scope_up(out);
indent(out) << "if (is_scalar($" << iter_val << ")) {" << endl;
generate_serialize_set_element(out, (t_set*)ttype, iter);
@@ -2155,7 +2386,8 @@
scope_down(out);
} else if (ttype->is_list()) {
string iter = tmp("iter");
- indent(out) << "foreach ($" << prefix << " as $" << iter << ")" << endl;
+ indent(out) <<
+ "foreach ($" << prefix << " as $" << iter << ")" << endl;
scope_up(out);
generate_serialize_list_element(out, (t_list*)ttype, iter);
scope_down(out);
@@ -2165,11 +2397,14 @@
if (!binary_inline_) {
if (ttype->is_map()) {
- indent(out) << "$output->writeMapEnd();" << endl;
+ indent(out) <<
+ "$output->writeMapEnd();" << endl;
} else if (ttype->is_set()) {
- indent(out) << "$output->writeSetEnd();" << endl;
+ indent(out) <<
+ "$output->writeSetEnd();" << endl;
} else if (ttype->is_list()) {
- indent(out) << "$output->writeListEnd();" << endl;
+ indent(out) <<
+ "$output->writeListEnd();" << endl;
}
}
@@ -2180,7 +2415,7 @@
* Serializes the members of a map.
*
*/
-void t_php_generator::generate_serialize_map_element(ofstream& out,
+void t_php_generator::generate_serialize_map_element(ofstream &out,
t_map* tmap,
string kiter,
string viter) {
@@ -2194,7 +2429,9 @@
/**
* Serializes the members of a set.
*/
-void t_php_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_php_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2202,7 +2439,9 @@
/**
* Serializes the members of a list.
*/
-void t_php_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_php_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2210,14 +2449,17 @@
/**
* Emits a PHPDoc comment for the given contents
*/
-void t_php_generator::generate_php_docstring_comment(ofstream& out, string contents) {
- generate_docstring_comment(out, "/**\n", " * ", contents, " */\n");
+void t_php_generator::generate_php_docstring_comment(ofstream &out, string contents) {
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", contents,
+ " */\n");
}
/**
* Emits a PHPDoc comment if the provided object has a doc in Thrift
*/
-void t_php_generator::generate_php_doc(ofstream& out, t_doc* tdoc) {
+void t_php_generator::generate_php_doc(ofstream &out, t_doc* tdoc) {
if (tdoc->has_doc()) {
generate_php_docstring_comment(out, tdoc->get_doc());
}
@@ -2226,7 +2468,7 @@
/**
* Emits a PHPDoc comment for a field
*/
-void t_php_generator::generate_php_doc(ofstream& out, t_field* field) {
+void t_php_generator::generate_php_doc(ofstream &out, t_field* field) {
stringstream ss;
// prepend free-style doc if available
@@ -2244,7 +2486,7 @@
/**
* Emits a PHPDoc comment for a function
*/
-void t_php_generator::generate_php_doc(ofstream& out, t_function* function) {
+void t_php_generator::generate_php_doc(ofstream &out, t_function* function) {
stringstream ss;
if (function->has_doc()) {
ss << function->get_doc() << endl;
@@ -2284,9 +2526,13 @@
ss << endl;
}
- generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
+ generate_docstring_comment(out,
+ "/**\n",
+ " * ", ss.str(),
+ " */\n");
}
+
/**
* Declares a field, which may include initialization as necessary.
*
@@ -2340,8 +2586,11 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_php_generator::function_signature(t_function* tfunction, string prefix) {
- return prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ")";
+string t_php_generator::function_signature(t_function* tfunction,
+ string prefix) {
+ return
+ prefix + tfunction->get_name() +
+ "(" + argument_list(tfunction->get_arglist()) + ")";
}
/**
@@ -2362,15 +2611,17 @@
t_type* type = (*f_iter)->get_type();
- // Set type name
- if (addTypeHints) {
- if (type->is_struct()) {
- string className = php_namespace(type->get_program())
- + php_namespace_directory("Definition", false)
- + classify(type->get_name());
+ //Set type name
+ if (addTypeHints)
+ {
+ if (type->is_struct())
+ {
+ string className = php_namespace(type->get_program()) + php_namespace_directory("Definition", false) + classify(type->get_name());
result += className + " ";
- } else if (type->is_container()) {
+ }
+ else if (type->is_container())
+ {
result += "array ";
}
}
@@ -2410,7 +2661,7 @@
/**
* Converts the parse type to a C++ enum string for the given type.
*/
-string t_php_generator::type_to_enum(t_type* type) {
+string t_php_generator ::type_to_enum(t_type* type) {
type = get_true_type(type);
if (type->is_base_type()) {
@@ -2451,7 +2702,7 @@
/**
* Converts the parse type to a PHPDoc string for the given type.
*/
-string t_php_generator::type_to_phpdoc(t_type* type) {
+string t_php_generator ::type_to_phpdoc(t_type* type) {
type = get_true_type(type);
if (type->is_base_type()) {
@@ -2501,13 +2752,13 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
-THRIFT_REGISTER_GENERATOR(
- php,
- "PHP",
- " inlined: Generate PHP inlined files\n"
- " server: Generate PHP server stubs\n"
- " oop: Generate PHP with object oriented subclasses\n"
- " rest: Generate PHP REST processors\n"
- " nsglobal=NAME: Set global namespace\n"
- " validate: Generate PHP validator methods\n"
- " json: Generate JsonSerializable classes (requires PHP >= 5.4)\n")
+THRIFT_REGISTER_GENERATOR(php, "PHP",
+" inlined: Generate PHP inlined files\n"
+" server: Generate PHP server stubs\n"
+" oop: Generate PHP with object oriented subclasses\n"
+" rest: Generate PHP REST processors\n"
+" nsglobal=NAME: Set global namespace\n"
+" validate: Generate PHP validator methods\n"
+" json: Generate JsonSerializable classes (requires PHP >= 5.4)\n"
+)
+
diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc
index 50af776..924bcc7 100644
--- a/compiler/cpp/src/generate/t_py_generator.cc
+++ b/compiler/cpp/src/generate/t_py_generator.cc
@@ -38,19 +38,21 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Python code generator.
*
*/
class t_py_generator : public t_generator {
-public:
- t_py_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)option_string;
+ public:
+ t_py_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("new_style");
@@ -121,12 +123,12 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
@@ -135,10 +137,7 @@
*/
void generate_py_struct(t_struct* tstruct, bool is_exception);
- void generate_py_struct_definition(std::ofstream& out,
- t_struct* tstruct,
- bool is_xception = false,
- bool is_result = false);
+ void generate_py_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
void generate_py_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_py_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_py_struct_required_validator(std::ofstream& out, t_struct* tstruct);
@@ -148,59 +147,80 @@
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_remote(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_remote (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(std::ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inclass = false);
+ void generate_deserialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
- void generate_deserialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(std::ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (std::ofstream &out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(std::ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(std::ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(std::ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (std::ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(std::ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (std::ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(std::ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (std::ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(std::ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element (std::ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_set_element(std::ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (std::ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(std::ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (std::ofstream &out,
+ t_list* tlist,
+ std::string iter);
- void generate_python_docstring(std::ofstream& out, t_struct* tstruct);
+ void generate_python_docstring (std::ofstream& out,
+ t_struct* tstruct);
- void generate_python_docstring(std::ofstream& out, t_function* tfunction);
+ void generate_python_docstring (std::ofstream& out,
+ t_function* tfunction);
- void generate_python_docstring(std::ofstream& out,
- t_doc* tdoc,
- t_struct* tstruct,
- const char* subheader);
+ void generate_python_docstring (std::ofstream& out,
+ t_doc* tdoc,
+ t_struct* tstruct,
+ const char* subheader);
- void generate_python_docstring(std::ofstream& out, t_doc* tdoc);
+ void generate_python_docstring (std::ofstream& out,
+ t_doc* tdoc);
/**
* Helper rendering functions
@@ -213,10 +233,11 @@
std::string declare_argument(t_field* tfield);
std::string render_field_default_value(t_field* tfield);
std::string type_name(t_type* ttype);
- std::string function_signature(t_function* tfunction, bool interface = false);
+ std::string function_signature(t_function* tfunction,
+ bool interface=false);
std::string argument_list(t_struct* tstruct,
- std::vector<std::string>* pre = NULL,
- std::vector<std::string>* post = NULL);
+ std::vector<std::string> *pre=NULL,
+ std::vector<std::string> *post=NULL);
std::string type_to_enum(t_type* ttype);
std::string type_to_spec_args(t_type* ttype);
@@ -225,9 +246,9 @@
}
static std::string get_real_py_module(const t_program* program, bool gen_twisted) {
- if (gen_twisted) {
+ if(gen_twisted) {
std::string twisted_module = program->get_namespace("py.twisted");
- if (!twisted_module.empty()) {
+ if(!twisted_module.empty()){
return twisted_module;
}
}
@@ -239,15 +260,16 @@
return real_module;
}
-private:
+ private:
+
/**
* True if we should generate new-style classes.
*/
bool gen_newstyle_;
- /**
- * True if we should generate dynamic style classes.
- */
+ /**
+ * True if we should generate dynamic style classes.
+ */
bool gen_dynamic_;
bool gen_dynbase_;
@@ -285,8 +307,10 @@
std::string package_dir_;
std::string module_;
+
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -301,7 +325,7 @@
while (true) {
// TODO: Do better error checking here.
MKDIR(package_dir_.c_str());
- std::ofstream init_py((package_dir_ + "/__init__.py").c_str(), std::ios_base::app);
+ std::ofstream init_py((package_dir_+"/__init__.py").c_str(), std::ios_base::app);
init_py.close();
if (module.empty()) {
break;
@@ -314,21 +338,22 @@
} else {
package_dir_ += "/";
package_dir_ += module.substr(0, pos);
- module.erase(0, pos + 1);
+ module.erase(0, pos+1);
}
}
// Make output file
- string f_types_name = package_dir_ + "/" + "ttypes.py";
+ string f_types_name = package_dir_+"/"+"ttypes.py";
f_types_.open(f_types_name.c_str());
- string f_consts_name = package_dir_ + "/" + "constants.py";
+ string f_consts_name = package_dir_+"/"+"constants.py";
f_consts_.open(f_consts_name.c_str());
- string f_init_name = package_dir_ + "/__init__.py";
+ string f_init_name = package_dir_+"/__init__.py";
ofstream f_init;
f_init.open(f_init_name.c_str());
- f_init << "__all__ = ['ttypes', 'constants'";
+ f_init <<
+ "__all__ = ['ttypes', 'constants'";
vector<t_service*> services = program_->get_services();
vector<t_service*>::iterator sv_iter;
for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
@@ -338,11 +363,18 @@
f_init.close();
// Print header
- f_types_ << py_autogen_comment() << endl << py_imports() << endl << render_includes() << endl
- << render_fastbinary_includes() << endl << endl;
+ f_types_ <<
+ py_autogen_comment() << endl <<
+ py_imports() << endl <<
+ render_includes() << endl <<
+ render_fastbinary_includes() <<
+ endl << endl;
- f_consts_ << py_autogen_comment() << endl << py_imports() << endl << "from ttypes import *"
- << endl << endl;
+ f_consts_ <<
+ py_autogen_comment() << endl <<
+ py_imports() << endl <<
+ "from ttypes import *" << endl <<
+ endl;
}
/**
@@ -368,12 +400,13 @@
if (gen_dynamic_) {
hdr += std::string(import_dynbase_);
} else {
- hdr += "from thrift.transport import TTransport\n"
- "from thrift.protocol import TBinaryProtocol, TProtocol\n"
- "try:\n"
- " from thrift.protocol import fastbinary\n"
- "except:\n"
- " fastbinary = None\n";
+ hdr +=
+ "from thrift.transport import TTransport\n"
+ "from thrift.protocol import TBinaryProtocol, TProtocol\n"
+ "try:\n"
+ " from thrift.protocol import fastbinary\n"
+ "except:\n"
+ " fastbinary = None\n";
}
return hdr;
}
@@ -382,16 +415,22 @@
* Autogen'd comment
*/
string t_py_generator::py_autogen_comment() {
- return std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n"
- + "# options string: " + copy_options_ + "\n" + "#\n";
+ return
+ std::string("#\n") +
+ "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "#\n" +
+ "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "#\n" +
+ "# options string: " + copy_options_ + "\n" +
+ "#\n";
}
/**
* Prints standard thrift imports
*/
string t_py_generator::py_imports() {
- return string("from thrift.Thrift import TType, TMessageType, TException, TApplicationException");
+ return
+ string("from thrift.Thrift import TType, TMessageType, TException, TApplicationException");
}
/**
@@ -409,7 +448,7 @@
* @param ttypedef The type definition
*/
void t_py_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -421,8 +460,11 @@
void t_py_generator::generate_enum(t_enum* tenum) {
std::ostringstream to_string_mapping, from_string_mapping;
- f_types_ << "class " << tenum->get_name() << (gen_newstyle_ ? "(object)" : "")
- << (gen_dynamic_ ? "(" + gen_dynbaseclass_ + ")" : "") << ":" << endl;
+ f_types_ <<
+ "class " << tenum->get_name() <<
+ (gen_newstyle_ ? "(object)" : "") <<
+ (gen_dynamic_ ? "(" + gen_dynbaseclass_ + ")" : "") <<
+ ":" << endl;
indent_up();
generate_python_docstring(f_types_, tenum);
@@ -436,10 +478,12 @@
indent(f_types_) << (*c_iter)->get_name() << " = " << value << endl;
// Dictionaries to/from string names of enums
- to_string_mapping << indent() << indent() << value << ": \""
- << escape_string((*c_iter)->get_name()) << "\"," << endl;
- from_string_mapping << indent() << indent() << '"' << escape_string((*c_iter)->get_name())
- << "\": " << value << ',' << endl;
+ to_string_mapping <<
+ indent() << indent() << value << ": \"" <<
+ escape_string((*c_iter)->get_name()) << "\"," << endl;
+ from_string_mapping <<
+ indent() << indent() << '"' << escape_string((*c_iter)->get_name()) <<
+ "\": " << value << ',' << endl;
}
to_string_mapping << indent() << "}" << endl;
from_string_mapping << indent() << "}" << endl;
@@ -589,7 +633,8 @@
/**
* Generates a python struct
*/
-void t_py_generator::generate_py_struct(t_struct* tstruct, bool is_exception) {
+void t_py_generator::generate_py_struct(t_struct* tstruct,
+ bool is_exception) {
generate_py_struct_definition(f_types_, tstruct, is_exception);
}
@@ -602,12 +647,13 @@
t_struct* tstruct,
bool is_exception,
bool is_result) {
- (void)is_result;
+ (void) is_result;
const vector<t_field*>& members = tstruct->get_members();
const vector<t_field*>& sorted_members = tstruct->get_sorted_members();
vector<t_field*>::const_iterator m_iter;
- out << std::endl << "class " << tstruct->get_name();
+ out << std::endl <<
+ "class " << tstruct->get_name();
if (is_exception) {
if (gen_dynamic_) {
out << "(" << gen_dynbaseclass_exc_ << ")";
@@ -651,10 +697,11 @@
indent(out) << "__slots__ = [ " << endl;
indent_up();
for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) {
- indent(out) << "'" << (*m_iter)->get_name() << "'," << endl;
+ indent(out) << "'" << (*m_iter)->get_name() << "'," << endl;
}
indent_down();
indent(out) << " ]" << endl << endl;
+
}
// TODO(dreiss): Look into generating an empty tuple instead of None
@@ -672,15 +719,16 @@
indent(out) << "None, # " << sorted_keys_pos << endl;
}
- indent(out) << "(" << (*m_iter)->get_key() << ", " << type_to_enum((*m_iter)->get_type())
- << ", "
- << "'" << (*m_iter)->get_name() << "'"
- << ", " << type_to_spec_args((*m_iter)->get_type()) << ", "
- << render_field_default_value(*m_iter) << ", "
- << "),"
- << " # " << sorted_keys_pos << endl;
+ indent(out) << "(" << (*m_iter)->get_key() << ", "
+ << type_to_enum((*m_iter)->get_type()) << ", "
+ << "'" << (*m_iter)->get_name() << "'" << ", "
+ << type_to_spec_args((*m_iter)->get_type()) << ", "
+ << render_field_default_value(*m_iter) << ", "
+ << "),"
+ << " # " << sorted_keys_pos
+ << endl;
- sorted_keys_pos++;
+ sorted_keys_pos ++;
}
indent_down();
@@ -689,8 +737,10 @@
indent(out) << "thrift_spec = None" << endl;
}
+
if (members.size() > 0) {
- out << indent() << "def __init__(self,";
+ out <<
+ indent() << "def __init__(self,";
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
// This fills in default values, as opposed to nulls
@@ -705,12 +755,14 @@
// Initialize fields
t_type* type = (*m_iter)->get_type();
if (!type->is_base_type() && !type->is_enum() && (*m_iter)->get_value() != NULL) {
- indent(out) << "if " << (*m_iter)->get_name() << " is "
- << "self.thrift_spec[" << (*m_iter)->get_key() << "][4]:" << endl;
- indent(out) << " " << (*m_iter)->get_name() << " = " << render_field_default_value(*m_iter)
- << endl;
+ indent(out) <<
+ "if " << (*m_iter)->get_name() << " is " << "self.thrift_spec[" <<
+ (*m_iter)->get_key() << "][4]:" << endl;
+ indent(out) << " " << (*m_iter)->get_name() << " = " <<
+ render_field_default_value(*m_iter) << endl;
}
- indent(out) << "self." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << endl;
+ indent(out) <<
+ "self." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << endl;
}
indent_down();
@@ -727,13 +779,15 @@
// because when raised exceptions are printed to the console, __repr__
// isn't used. See python bug #5882
if (is_exception) {
- out << indent() << "def __str__(self):" << endl << indent() << " return repr(self)" << endl
- << endl;
+ out <<
+ indent() << "def __str__(self):" << endl <<
+ indent() << " return repr(self)" << endl <<
+ endl;
}
out << indent() << "def __hash__(self):" << endl;
indent_up();
- indent(out) << "value = 17" << endl; // PYTHONHASHSEED would be better, but requires Python 3.2.3
+ indent(out) << "value = 17" << endl; // PYTHONHASHSEED would be better, but requires Python 3.2.3
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
indent(out) << "value = (value * 31) ^ hash(self." << (*m_iter)->get_name() + ")" << endl;
}
@@ -741,46 +795,61 @@
indent_down();
out << endl;
+
if (!gen_slots_) {
// Printing utilities so that on the command line thrift
// structs look pretty like dictionaries
- out << indent() << "def __repr__(self):" << endl << indent() << " L = ['%s=%r' % (key, value)"
- << endl << indent() << " for key, value in self.__dict__.iteritems()]" << endl
- << indent() << " return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl
- << endl;
+ out <<
+ indent() << "def __repr__(self):" << endl <<
+ indent() << " L = ['%s=%r' % (key, value)" << endl <<
+ indent() << " for key, value in self.__dict__.iteritems()]" << endl <<
+ indent() << " return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl <<
+ endl;
// Equality and inequality methods that compare by value
- out << indent() << "def __eq__(self, other):" << endl;
+ out <<
+ indent() << "def __eq__(self, other):" << endl;
indent_up();
- out << indent() << "return isinstance(other, self.__class__) and "
- "self.__dict__ == other.__dict__" << endl;
+ out <<
+ indent() << "return isinstance(other, self.__class__) and "
+ "self.__dict__ == other.__dict__" << endl;
indent_down();
out << endl;
- out << indent() << "def __ne__(self, other):" << endl;
+ out <<
+ indent() << "def __ne__(self, other):" << endl;
indent_up();
- out << indent() << "return not (self == other)" << endl;
+ out <<
+ indent() << "return not (self == other)" << endl;
indent_down();
} else if (!gen_dynamic_) {
// no base class available to implement __eq__ and __repr__ and __ne__ for us
// so we must provide one that uses __slots__
- out << indent() << "def __repr__(self):" << endl << indent()
- << " L = ['%s=%r' % (key, getattr(self, key))" << endl << indent()
- << " for key in self.__slots__]" << endl << indent()
- << " return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl << endl;
+ out <<
+ indent() << "def __repr__(self):" << endl <<
+ indent() << " L = ['%s=%r' % (key, getattr(self, key))" << endl <<
+ indent() << " for key in self.__slots__]" << endl <<
+ indent() << " return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl <<
+ endl;
// Equality method that compares each attribute by value and type, walking __slots__
- out << indent() << "def __eq__(self, other):" << endl << indent()
- << " if not isinstance(other, self.__class__):" << endl << indent() << " return False"
- << endl << indent() << " for attr in self.__slots__:" << endl << indent()
- << " my_val = getattr(self, attr)" << endl << indent()
- << " other_val = getattr(other, attr)" << endl << indent()
- << " if my_val != other_val:" << endl << indent() << " return False" << endl
- << indent() << " return True" << endl << endl;
+ out <<
+ indent() << "def __eq__(self, other):" << endl <<
+ indent() << " if not isinstance(other, self.__class__):" << endl <<
+ indent() << " return False" << endl <<
+ indent() << " for attr in self.__slots__:" << endl <<
+ indent() << " my_val = getattr(self, attr)" << endl <<
+ indent() << " other_val = getattr(other, attr)" << endl <<
+ indent() << " if my_val != other_val:" << endl <<
+ indent() << " return False" << endl <<
+ indent() << " return True" << endl <<
+ endl;
- out << indent() << "def __ne__(self, other):" << endl << indent()
- << " return not (self == other)" << endl << endl;
+ out <<
+ indent() << "def __ne__(self, other):" << endl <<
+ indent() << " return not (self == other)" << endl <<
+ endl;
}
indent_down();
}
@@ -788,75 +857,93 @@
/**
* Generates the read method for a struct
*/
-void t_py_generator::generate_py_struct_reader(ofstream& out, t_struct* tstruct) {
+void t_py_generator::generate_py_struct_reader(ofstream& out,
+ t_struct* tstruct) {
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- indent(out) << "def read(self, iprot):" << endl;
+ indent(out) <<
+ "def read(self, iprot):" << endl;
indent_up();
- indent(out) << "if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
- "and isinstance(iprot.trans, TTransport.CReadableTransport) "
- "and self.thrift_spec is not None "
- "and fastbinary is not None:" << endl;
+ indent(out) <<
+ "if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
+ "and isinstance(iprot.trans, TTransport.CReadableTransport) "
+ "and self.thrift_spec is not None "
+ "and fastbinary is not None:" << endl;
indent_up();
- indent(out) << "fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))"
- << endl;
- indent(out) << "return" << endl;
+ indent(out) <<
+ "fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))" << endl;
+ indent(out) <<
+ "return" << endl;
indent_down();
- indent(out) << "iprot.readStructBegin()" << endl;
+ indent(out) <<
+ "iprot.readStructBegin()" << endl;
// Loop over reading in fields
- indent(out) << "while True:" << endl;
- indent_up();
+ indent(out) <<
+ "while True:" << endl;
+ indent_up();
- // Read beginning field marker
- indent(out) << "(fname, ftype, fid) = iprot.readFieldBegin()" << endl;
+ // Read beginning field marker
+ indent(out) <<
+ "(fname, ftype, fid) = iprot.readFieldBegin()" << endl;
- // Check for field STOP marker and break
- indent(out) << "if ftype == TType.STOP:" << endl;
- indent_up();
- indent(out) << "break" << endl;
- indent_down();
+ // Check for field STOP marker and break
+ indent(out) <<
+ "if ftype == TType.STOP:" << endl;
+ indent_up();
+ indent(out) <<
+ "break" << endl;
+ indent_down();
- // Switch statement on the field we are reading
- bool first = true;
+ // Switch statement on the field we are reading
+ bool first = true;
- // Generate deserialization code for known cases
- for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- if (first) {
- first = false;
- out << indent() << "if ";
- } else {
- out << indent() << "elif ";
+ // Generate deserialization code for known cases
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ if (first) {
+ first = false;
+ out <<
+ indent() << "if ";
+ } else {
+ out <<
+ indent() << "elif ";
+ }
+ out << "fid == " << (*f_iter)->get_key() << ":" << endl;
+ indent_up();
+ indent(out) << "if ftype == " << type_to_enum((*f_iter)->get_type()) << ":" << endl;
+ indent_up();
+ generate_deserialize_field(out, *f_iter, "self.");
+ indent_down();
+ out <<
+ indent() << "else:" << endl <<
+ indent() << " iprot.skip(ftype)" << endl;
+ indent_down();
}
- out << "fid == " << (*f_iter)->get_key() << ":" << endl;
- indent_up();
- indent(out) << "if ftype == " << type_to_enum((*f_iter)->get_type()) << ":" << endl;
- indent_up();
- generate_deserialize_field(out, *f_iter, "self.");
+
+ // In the default case we skip the field
+ out <<
+ indent() << "else:" << endl <<
+ indent() << " iprot.skip(ftype)" << endl;
+
+ // Read field end marker
+ indent(out) <<
+ "iprot.readFieldEnd()" << endl;
+
indent_down();
- out << indent() << "else:" << endl << indent() << " iprot.skip(ftype)" << endl;
+
+ indent(out) <<
+ "iprot.readStructEnd()" << endl;
+
indent_down();
- }
-
- // In the default case we skip the field
- out << indent() << "else:" << endl << indent() << " iprot.skip(ftype)" << endl;
-
- // Read field end marker
- indent(out) << "iprot.readFieldEnd()" << endl;
-
- indent_down();
-
- indent(out) << "iprot.readStructEnd()" << endl;
-
- indent_down();
out << endl;
}
-void t_py_generator::generate_py_struct_writer(ofstream& out, t_struct* tstruct) {
+void t_py_generator::generate_py_struct_writer(ofstream& out,
+ t_struct* tstruct) {
string name = tstruct->get_name();
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator f_iter;
@@ -864,48 +951,57 @@
indent(out) << "def write(self, oprot):" << endl;
indent_up();
- indent(out) << "if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
- "and self.thrift_spec is not None "
- "and fastbinary is not None:" << endl;
+ indent(out) <<
+ "if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
+ "and self.thrift_spec is not None "
+ "and fastbinary is not None:" << endl;
indent_up();
- indent(out)
- << "oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))"
- << endl;
- indent(out) << "return" << endl;
+ indent(out) <<
+ "oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))" << endl;
+ indent(out) <<
+ "return" << endl;
indent_down();
- indent(out) << "oprot.writeStructBegin('" << name << "')" << endl;
+ indent(out) <<
+ "oprot.writeStructBegin('" << name << "')" << endl;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
// Write field header
- indent(out) << "if self." << (*f_iter)->get_name() << " is not None:" << endl;
+ indent(out) <<
+ "if self." << (*f_iter)->get_name() << " is not None:" << endl;
indent_up();
- indent(out) << "oprot.writeFieldBegin("
- << "'" << (*f_iter)->get_name() << "', " << type_to_enum((*f_iter)->get_type())
- << ", " << (*f_iter)->get_key() << ")" << endl;
+ indent(out) <<
+ "oprot.writeFieldBegin(" <<
+ "'" << (*f_iter)->get_name() << "', " <<
+ type_to_enum((*f_iter)->get_type()) << ", " <<
+ (*f_iter)->get_key() << ")" << endl;
// Write field contents
generate_serialize_field(out, *f_iter, "self.");
// Write field closer
- indent(out) << "oprot.writeFieldEnd()" << endl;
+ indent(out) <<
+ "oprot.writeFieldEnd()" << endl;
indent_down();
}
// Write the struct map
- out << indent() << "oprot.writeFieldStop()" << endl << indent() << "oprot.writeStructEnd()"
- << endl;
+ out <<
+ indent() << "oprot.writeFieldStop()" << endl <<
+ indent() << "oprot.writeStructEnd()" << endl;
out << endl;
indent_down();
generate_py_struct_required_validator(out, tstruct);
- out << endl;
+ out <<
+ endl;
}
-void t_py_generator::generate_py_struct_required_validator(ofstream& out, t_struct* tstruct) {
+void t_py_generator::generate_py_struct_required_validator(ofstream& out,
+ t_struct* tstruct) {
indent(out) << "def validate(self):" << endl;
indent_up();
@@ -918,8 +1014,8 @@
t_field* field = (*f_iter);
if (field->get_req() == t_field::T_REQUIRED) {
indent(out) << "if self." << field->get_name() << " is None:" << endl;
- indent(out) << " raise TProtocol.TProtocolException(message='Required field "
- << field->get_name() << " is unset!')" << endl;
+ indent(out) << " raise TProtocol.TProtocolException(message='Required field " <<
+ field->get_name() << " is unset!')" << endl;
}
}
}
@@ -934,24 +1030,29 @@
* @param tservice The service definition
*/
void t_py_generator::generate_service(t_service* tservice) {
- string f_service_name = package_dir_ + "/" + service_name_ + ".py";
+ string f_service_name = package_dir_+"/"+service_name_+".py";
f_service_.open(f_service_name.c_str());
- f_service_ << py_autogen_comment() << endl << py_imports() << endl;
+ f_service_ <<
+ py_autogen_comment() << endl <<
+ py_imports() << endl;
if (tservice->get_extends() != NULL) {
- f_service_ << "import "
- << get_real_py_module(tservice->get_extends()->get_program(), gen_twisted_) << "."
- << tservice->get_extends()->get_name() << endl;
+ f_service_ <<
+ "import " << get_real_py_module(tservice->get_extends()->get_program(), gen_twisted_) <<
+ "." << tservice->get_extends()->get_name() << endl;
}
- f_service_ << "from ttypes import *" << endl << "from thrift.Thrift import TProcessor" << endl
- << render_fastbinary_includes() << endl;
+ f_service_ <<
+ "from ttypes import *" << endl <<
+ "from thrift.Thrift import TProcessor" << endl <<
+ render_fastbinary_includes() << endl;
if (gen_twisted_) {
- f_service_ << "from zope.interface import Interface, implements" << endl
- << "from twisted.internet import defer" << endl
- << "from thrift.transport import TTwisted" << endl;
+ f_service_ <<
+ "from zope.interface import Interface, implements" << endl <<
+ "from twisted.internet import defer" << endl <<
+ "from thrift.transport import TTwisted" << endl;
} else if (gen_tornado_) {
f_service_ << "from tornado import gen" << endl;
f_service_ << "from tornado import concurrent" << endl;
@@ -980,7 +1081,8 @@
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::iterator f_iter;
- f_service_ << "# HELPER FUNCTIONS AND STRUCTURES" << endl;
+ f_service_ <<
+ "# HELPER FUNCTIONS AND STRUCTURES" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
@@ -1031,25 +1133,30 @@
}
}
- f_service_ << "class Iface" << extends_if << ":" << endl;
+ f_service_ <<
+ "class Iface" << extends_if << ":" << endl;
indent_up();
generate_python_docstring(f_service_, tservice);
vector<t_function*> functions = tservice->get_functions();
if (functions.empty()) {
- f_service_ << indent() << "pass" << endl;
+ f_service_ <<
+ indent() << "pass" << endl;
} else {
vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "def " << function_signature(*f_iter, true) << ":" << endl;
+ f_service_ <<
+ indent() << "def " << function_signature(*f_iter, true) << ":" << endl;
indent_up();
generate_python_docstring(f_service_, (*f_iter));
- f_service_ << indent() << "pass" << endl << endl;
+ f_service_ <<
+ indent() << "pass" << endl << endl;
indent_down();
}
}
indent_down();
- f_service_ << endl;
+ f_service_ <<
+ endl;
}
/**
@@ -1074,76 +1181,96 @@
}
if (gen_twisted_) {
- f_service_ << "class Client" << extends_client << ":" << endl << " implements(Iface)" << endl
- << endl;
+ f_service_ <<
+ "class Client" << extends_client << ":" << endl <<
+ " implements(Iface)" << endl << endl;
} else {
- f_service_ << "class Client(" << extends_client << "Iface):" << endl;
+ f_service_ <<
+ "class Client(" << extends_client << "Iface):" << endl;
}
indent_up();
generate_python_docstring(f_service_, tservice);
// Constructor function
if (gen_twisted_) {
- f_service_ << indent() << "def __init__(self, transport, oprot_factory):" << endl;
+ f_service_ <<
+ indent() << "def __init__(self, transport, oprot_factory):" << endl;
} else if (gen_tornado_) {
- f_service_ << indent()
- << "def __init__(self, transport, iprot_factory, oprot_factory=None):" << endl;
+ f_service_ <<
+ indent() << "def __init__(self, transport, iprot_factory, oprot_factory=None):" << endl;
} else {
- f_service_ << indent() << "def __init__(self, iprot, oprot=None):" << endl;
+ f_service_ <<
+ indent() << "def __init__(self, iprot, oprot=None):" << endl;
}
if (extends.empty()) {
if (gen_twisted_) {
- f_service_ << indent() << " self._transport = transport" << endl << indent()
- << " self._oprot_factory = oprot_factory" << endl << indent()
- << " self._seqid = 0" << endl << indent() << " self._reqs = {}" << endl << endl;
+ f_service_ <<
+ indent() << " self._transport = transport" << endl <<
+ indent() << " self._oprot_factory = oprot_factory" << endl <<
+ indent() << " self._seqid = 0" << endl <<
+ indent() << " self._reqs = {}" << endl <<
+ endl;
} else if (gen_tornado_) {
- f_service_ << indent() << " self._transport = transport" << endl << indent()
- << " self._iprot_factory = iprot_factory" << endl << indent()
- << " self._oprot_factory = (oprot_factory if oprot_factory is not None" << endl
- << indent() << " else iprot_factory)" << endl << indent()
- << " self._seqid = 0" << endl << indent() << " self._reqs = {}" << endl
- << indent() << " self._transport.io_loop.spawn_callback(self._start_receiving)"
- << endl << endl;
+ f_service_ <<
+ indent() << " self._transport = transport" << endl <<
+ indent() << " self._iprot_factory = iprot_factory" << endl <<
+ indent() << " self._oprot_factory = (oprot_factory if oprot_factory is not None" << endl <<
+ indent() << " else iprot_factory)" << endl <<
+ indent() << " self._seqid = 0" << endl <<
+ indent() << " self._reqs = {}" << endl <<
+ indent() << " self._transport.io_loop.spawn_callback(self._start_receiving)" << endl <<
+ endl;
} else {
- f_service_ << indent() << " self._iprot = self._oprot = iprot" << endl << indent()
- << " if oprot is not None:" << endl << indent() << " self._oprot = oprot"
- << endl << indent() << " self._seqid = 0" << endl << endl;
+ f_service_ <<
+ indent() << " self._iprot = self._oprot = iprot" << endl <<
+ indent() << " if oprot is not None:" << endl <<
+ indent() << " self._oprot = oprot" << endl <<
+ indent() << " self._seqid = 0" << endl <<
+ endl;
}
} else {
if (gen_twisted_) {
- f_service_ << indent() << " " << extends
- << ".Client.__init__(self, transport, oprot_factory)" << endl << endl;
+ f_service_ <<
+ indent() << " " << extends << ".Client.__init__(self, transport, oprot_factory)" << endl <<
+ endl;
} else if (gen_tornado_) {
- f_service_ << indent() << " " << extends
- << ".Client.__init__(self, transport, iprot_factory, oprot_factory)" << endl
- << endl;
+ f_service_ <<
+ indent() << " " << extends << ".Client.__init__(self, transport, iprot_factory, oprot_factory)" << endl <<
+ endl;
} else {
- f_service_ << indent() << " " << extends << ".Client.__init__(self, iprot, oprot)" << endl
- << endl;
+ f_service_ <<
+ indent() << " " << extends << ".Client.__init__(self, iprot, oprot)" << endl <<
+ endl;
}
}
if (gen_tornado_ && extends.empty()) {
- f_service_ << indent() << "@gen.engine" << endl << indent()
- << "def _start_receiving(self):" << endl << indent() << " while True:" << endl
- << indent() << " try:" << endl << indent()
- << " frame = yield self._transport.readFrame()" << endl << indent()
- << " except TTransport.TTransportException as e:" << endl << indent()
- << " for future in self._reqs.itervalues():" << endl << indent()
- << " future.set_exception(e)" << endl << indent() << " self._reqs = {}"
- << endl << indent() << " return" << endl << indent()
- << " tr = TTransport.TMemoryBuffer(frame)" << endl << indent()
- << " iprot = self._iprot_factory.getProtocol(tr)" << endl << indent()
- << " (fname, mtype, rseqid) = iprot.readMessageBegin()" << endl << indent()
- << " future = self._reqs.pop(rseqid, None)" << endl << indent()
- << " if not future:" << endl << indent()
- << " # future has already been discarded" << endl << indent()
- << " continue" << endl << indent()
- << " method = getattr(self, 'recv_' + fname)" << endl << indent()
- << " try:" << endl << indent() << " result = method(iprot, mtype, rseqid)"
- << endl << indent() << " except Exception as e:" << endl << indent()
- << " future.set_exception(e)" << endl << indent() << " else:" << endl
- << indent() << " future.set_result(result)" << endl << endl;
+ f_service_ <<
+ indent() << "@gen.engine" << endl <<
+ indent() << "def _start_receiving(self):" << endl <<
+ indent() << " while True:" << endl <<
+ indent() << " try:" << endl <<
+ indent() << " frame = yield self._transport.readFrame()" << endl <<
+ indent() << " except TTransport.TTransportException as e:" << endl <<
+ indent() << " for future in self._reqs.itervalues():" << endl <<
+ indent() << " future.set_exception(e)" << endl <<
+ indent() << " self._reqs = {}" << endl <<
+ indent() << " return" << endl <<
+ indent() << " tr = TTransport.TMemoryBuffer(frame)" << endl <<
+ indent() << " iprot = self._iprot_factory.getProtocol(tr)" << endl <<
+ indent() << " (fname, mtype, rseqid) = iprot.readMessageBegin()" << endl <<
+ indent() << " future = self._reqs.pop(rseqid, None)" << endl <<
+ indent() << " if not future:" << endl <<
+ indent() << " # future has already been discarded" << endl <<
+ indent() << " continue" << endl <<
+ indent() << " method = getattr(self, 'recv_' + fname)" << endl <<
+ indent() << " try:" << endl <<
+ indent() << " result = method(iprot, mtype, rseqid)" << endl <<
+ indent() << " except Exception as e:" << endl <<
+ indent() << " future.set_exception(e)" << endl <<
+ indent() << " else:" << endl <<
+ indent() << " future.set_result(result)" << endl <<
+ endl;
}
// Generate client method implementations
@@ -1156,7 +1283,8 @@
string funname = (*f_iter)->get_name();
// Open function
- indent(f_service_) << "def " << function_signature(*f_iter, false) << ":" << endl;
+ indent(f_service_) <<
+ "def " << function_signature(*f_iter, false) << ":" << endl;
indent_up();
generate_python_docstring(f_service_, (*f_iter));
if (gen_twisted_) {
@@ -1167,18 +1295,20 @@
} else if (gen_tornado_) {
indent(f_service_) << "self._seqid += 1" << endl;
if (!(*f_iter)->is_oneway()) {
- indent(f_service_) << "future = self._reqs[self._seqid] = concurrent.Future()" << endl;
+ indent(f_service_) <<
+ "future = self._reqs[self._seqid] = concurrent.Future()" << endl;
}
- indent(f_service_) << "self.send_" << funname << "(";
+ indent(f_service_) <<
+ "self.send_" << funname << "(";
} else {
- indent(f_service_) << "self.send_" << funname << "(";
+ indent(f_service_) <<
+ "self.send_" << funname << "(";
}
bool first = true;
if (gen_twisted_) {
- // we need a leading comma if there are args, since it's called as maybeDeferred(funcname,
- // arg)
+ // we need a leading comma if there are args, since it's called as maybeDeferred(funcname, arg)
first = false;
}
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
@@ -1210,40 +1340,52 @@
if (gen_twisted_) {
// This block injects the body of the send_<> method for twisted (and a cb/eb pair)
indent_up();
- indent(f_service_) << "d.addCallbacks(" << endl;
+ indent(f_service_) <<
+ "d.addCallbacks(" << endl;
indent_up();
- f_service_ << indent() << "callback=self.cb_send_" << funname << "," << endl << indent()
- << "callbackArgs=(seqid,)," << endl << indent() << "errback=self.eb_send_"
- << funname << "," << endl << indent() << "errbackArgs=(seqid,))" << endl;
+ f_service_ <<
+ indent() << "callback=self.cb_send_" << funname << "," << endl <<
+ indent() << "callbackArgs=(seqid,)," << endl <<
+ indent() << "errback=self.eb_send_" << funname << "," << endl <<
+ indent() << "errbackArgs=(seqid,))" << endl;
indent_down();
- indent(f_service_) << "return d" << endl;
+ indent(f_service_) <<
+ "return d" << endl;
indent_down();
f_service_ << endl;
- indent(f_service_) << "def cb_send_" << funname << "(self, _, seqid):" << endl;
+ indent(f_service_) <<
+ "def cb_send_" << funname << "(self, _, seqid):" << endl;
indent_up();
if ((*f_iter)->is_oneway()) {
// if one-way, fire the deferred & remove it from _reqs
- f_service_ << indent() << "d = self._reqs.pop(seqid)" << endl << indent()
- << "d.callback(None)" << endl << indent() << "return d" << endl;
+ f_service_ << indent() <<
+ "d = self._reqs.pop(seqid)" << endl << indent() <<
+ "d.callback(None)" << endl << indent() <<
+ "return d" << endl;
} else {
- f_service_ << indent() << "return self._reqs[seqid]" << endl;
+ f_service_ << indent() <<
+ "return self._reqs[seqid]" << endl;
}
indent_down();
f_service_ << endl;
// add an errback to fail the request if the call to send_<> raised an exception
- indent(f_service_) << "def eb_send_" << funname << "(self, f, seqid):" << endl;
+ indent(f_service_) <<
+ "def eb_send_" << funname << "(self, f, seqid):" << endl;
indent_up();
- f_service_ << indent() << "d = self._reqs.pop(seqid)" << endl << indent() << "d.errback(f)"
- << endl << indent() << "return d" << endl;
+ f_service_ <<
+ indent() << "d = self._reqs.pop(seqid)" << endl <<
+ indent() << "d.errback(f)" << endl <<
+ indent() << "return d" << endl;
indent_down();
}
f_service_ << endl;
- indent(f_service_) << "def send_" << function_signature(*f_iter, false) << ":" << endl;
+ indent(f_service_) <<
+ "def send_" << function_signature(*f_iter, false) << ":" << endl;
indent_up();
std::string argsname = (*f_iter)->get_name() + "_args";
@@ -1251,29 +1393,35 @@
// Serialize the request header
if (gen_twisted_ || gen_tornado_) {
- f_service_ << indent() << "oprot = self._oprot_factory.getProtocol(self._transport)" << endl
- << indent() << "oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', "
+ f_service_ <<
+ indent() << "oprot = self._oprot_factory.getProtocol(self._transport)" << endl <<
+ indent() << "oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', "
<< messageType << ", self._seqid)" << endl;
} else {
- f_service_ << indent() << "self._oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', "
+ f_service_ <<
+ indent() << "self._oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', "
<< messageType << ", self._seqid)" << endl;
}
- f_service_ << indent() << "args = " << argsname << "()" << endl;
+ f_service_ <<
+ indent() << "args = " << argsname << "()" << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = "
- << (*fld_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << endl;
}
// Write to the stream
if (gen_twisted_ || gen_tornado_) {
- f_service_ << indent() << "args.write(oprot)" << endl << indent() << "oprot.writeMessageEnd()"
- << endl << indent() << "oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "args.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
} else {
- f_service_ << indent() << "args.write(self._oprot)" << endl << indent()
- << "self._oprot.writeMessageEnd()" << endl << indent()
- << "self._oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "args.write(self._oprot)" << endl <<
+ indent() << "self._oprot.writeMessageEnd()" << endl <<
+ indent() << "self._oprot.trans.flush()" << endl;
}
indent_down();
@@ -1281,84 +1429,100 @@
if (!(*f_iter)->is_oneway()) {
std::string resultname = (*f_iter)->get_name() + "_result";
// Open function
- f_service_ << endl;
+ f_service_ <<
+ endl;
if (gen_twisted_ || gen_tornado_) {
- f_service_ << indent() << "def recv_" << (*f_iter)->get_name()
- << "(self, iprot, mtype, rseqid):" << endl;
+ f_service_ <<
+ indent() << "def recv_" << (*f_iter)->get_name() <<
+ "(self, iprot, mtype, rseqid):" << endl;
} else {
t_struct noargs(program_);
t_function recv_function((*f_iter)->get_returntype(),
- string("recv_") + (*f_iter)->get_name(),
- &noargs);
- f_service_ << indent() << "def " << function_signature(&recv_function) << ":" << endl;
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs);
+ f_service_ <<
+ indent() << "def " << function_signature(&recv_function) << ":" << endl;
}
indent_up();
// TODO(mcslee): Validate message reply here, seq ids etc.
if (gen_twisted_) {
- f_service_ << indent() << "d = self._reqs.pop(rseqid)" << endl;
+ f_service_ <<
+ indent() << "d = self._reqs.pop(rseqid)" << endl;
} else if (gen_tornado_) {
} else {
- f_service_ << indent() << "iprot = self._iprot" << endl << indent()
- << "(fname, mtype, rseqid) = iprot.readMessageBegin()" << endl;
+ f_service_ <<
+ indent() << "iprot = self._iprot" << endl <<
+ indent() << "(fname, mtype, rseqid) = iprot.readMessageBegin()" << endl;
}
- f_service_ << indent() << "if mtype == TMessageType.EXCEPTION:" << endl << indent()
- << " x = TApplicationException()" << endl;
+ f_service_ <<
+ indent() << "if mtype == TMessageType.EXCEPTION:" << endl <<
+ indent() << " x = TApplicationException()" << endl;
if (gen_twisted_) {
- f_service_ << indent() << " x.read(iprot)" << endl << indent()
- << " iprot.readMessageEnd()" << endl << indent() << " return d.errback(x)"
- << endl << indent() << "result = " << resultname << "()" << endl << indent()
- << "result.read(iprot)" << endl << indent() << "iprot.readMessageEnd()" << endl;
+ f_service_ <<
+ indent() << " x.read(iprot)" << endl <<
+ indent() << " iprot.readMessageEnd()" << endl <<
+ indent() << " return d.errback(x)" << endl <<
+ indent() << "result = " << resultname << "()" << endl <<
+ indent() << "result.read(iprot)" << endl <<
+ indent() << "iprot.readMessageEnd()" << endl;
} else {
- f_service_ << indent() << " x.read(iprot)" << endl << indent()
- << " iprot.readMessageEnd()" << endl << indent() << " raise x" << endl
- << indent() << "result = " << resultname << "()" << endl << indent()
- << "result.read(iprot)" << endl << indent() << "iprot.readMessageEnd()" << endl;
+ f_service_ <<
+ indent() << " x.read(iprot)" << endl <<
+ indent() << " iprot.readMessageEnd()" << endl <<
+ indent() << " raise x" << endl <<
+ indent() << "result = " << resultname << "()" << endl <<
+ indent() << "result.read(iprot)" << endl <<
+ indent() << "iprot.readMessageEnd()" << endl;
}
// Careful, only return _result if not a void function
if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << indent() << "if result.success is not None:" << endl;
- if (gen_twisted_) {
- f_service_ << indent() << " return d.callback(result.success)" << endl;
- } else {
- f_service_ << indent() << " return result.success" << endl;
- }
+ f_service_ <<
+ indent() << "if result.success is not None:" << endl;
+ if (gen_twisted_) {
+ f_service_ <<
+ indent() << " return d.callback(result.success)" << endl;
+ } else {
+ f_service_ <<
+ indent() << " return result.success" << endl;
+ }
}
t_struct* xs = (*f_iter)->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "if result." << (*x_iter)->get_name() << " is not None:" << endl;
- if (gen_twisted_) {
- f_service_ << indent() << " return d.errback(result." << (*x_iter)->get_name() << ")"
- << endl;
- } else {
- f_service_ << indent() << " raise result." << (*x_iter)->get_name() << "" << endl;
- }
+ f_service_ <<
+ indent() << "if result." << (*x_iter)->get_name() << " is not None:" << endl;
+ if (gen_twisted_) {
+ f_service_ <<
+ indent() << " return d.errback(result." << (*x_iter)->get_name() << ")" << endl;
+ } else {
+ f_service_ <<
+ indent() << " raise result." << (*x_iter)->get_name() << "" << endl;
+ }
}
// Careful, only return _result if not a void function
if ((*f_iter)->get_returntype()->is_void()) {
if (gen_twisted_) {
- f_service_ << indent() << "return d.callback(None)" << endl;
+ f_service_ <<
+ indent() << "return d.callback(None)" << endl;
} else {
- f_service_ << indent() << "return" << endl;
+ f_service_ <<
+ indent() << "return" << endl;
}
} else {
if (gen_twisted_) {
- f_service_
- << indent()
- << "return d.errback(TApplicationException(TApplicationException.MISSING_RESULT, \""
- << (*f_iter)->get_name() << " failed: unknown result\"))" << endl;
+ f_service_ <<
+ indent() << "return d.errback(TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\"))" << endl;
} else {
- f_service_ << indent()
- << "raise TApplicationException(TApplicationException.MISSING_RESULT, \""
- << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
+ f_service_ <<
+ indent() << "raise TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
}
@@ -1369,7 +1533,8 @@
}
indent_down();
- f_service_ << endl;
+ f_service_ <<
+ endl;
}
/**
@@ -1379,37 +1544,46 @@
*/
void t_py_generator::generate_service_remote(t_service* tservice) {
vector<t_function*> functions = tservice->get_functions();
- // Get all function from parents
+ //Get all function from parents
t_service* parent = tservice->get_extends();
- while (parent != NULL) {
+ while(parent != NULL) {
vector<t_function*> p_functions = parent->get_functions();
functions.insert(functions.end(), p_functions.begin(), p_functions.end());
parent = parent->get_extends();
}
vector<t_function*>::iterator f_iter;
- string f_remote_name = package_dir_ + "/" + service_name_ + "-remote";
+ string f_remote_name = package_dir_+"/"+service_name_+"-remote";
ofstream f_remote;
f_remote.open(f_remote_name.c_str());
- f_remote << "#!/usr/bin/env python" << endl << py_autogen_comment() << endl << "import sys"
- << endl << "import pprint" << endl << "from urlparse import urlparse" << endl
- << "from thrift.transport import TTransport" << endl
- << "from thrift.transport import TSocket" << endl
- << "from thrift.transport import TSSLSocket" << endl
- << "from thrift.transport import THttpClient" << endl
- << "from thrift.protocol import TBinaryProtocol" << endl << endl;
+ f_remote <<
+ "#!/usr/bin/env python" << endl <<
+ py_autogen_comment() << endl <<
+ "import sys" << endl <<
+ "import pprint" << endl <<
+ "from urlparse import urlparse" << endl <<
+ "from thrift.transport import TTransport" << endl <<
+ "from thrift.transport import TSocket" << endl <<
+ "from thrift.transport import TSSLSocket" << endl <<
+ "from thrift.transport import THttpClient" << endl <<
+ "from thrift.protocol import TBinaryProtocol" << endl <<
+ endl;
- f_remote << "from " << module_ << " import " << service_name_ << endl << "from " << module_
- << ".ttypes import *" << endl << endl;
+ f_remote <<
+ "from " << module_ << " import " << service_name_ << endl <<
+ "from " << module_ << ".ttypes import *" << endl <<
+ endl;
- f_remote << "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl << " print('')" << endl
- << " print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] [-s[sl]] "
- "function [arg1 [arg2...]]')" << endl << " print('')" << endl
- << " print('Functions:')" << endl;
+ f_remote <<
+ "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl <<
+ " print('')" << endl <<
+ " print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] [-s[sl]] function [arg1 [arg2...]]')" << endl <<
+ " print('')" << endl <<
+ " print('Functions:')" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_remote << " print(' " << (*f_iter)->get_returntype()->get_name() << " "
- << (*f_iter)->get_name() << "(";
+ f_remote <<
+ " print(' " << (*f_iter)->get_returntype()->get_name() << " " << (*f_iter)->get_name() << "(";
t_struct* arg_struct = (*f_iter)->get_arglist();
const std::vector<t_field*>& args = arg_struct->get_members();
vector<t_field*>::const_iterator a_iter;
@@ -1421,37 +1595,70 @@
} else {
f_remote << ", ";
}
- f_remote << args[i]->get_type()->get_name() << " " << args[i]->get_name();
+ f_remote <<
+ args[i]->get_type()->get_name() << " " << args[i]->get_name();
}
f_remote << ")')" << endl;
}
- f_remote << " print('')" << endl << " sys.exit(0)" << endl << endl;
+ f_remote <<
+ " print('')" << endl <<
+ " sys.exit(0)" << endl <<
+ endl;
- f_remote << "pp = pprint.PrettyPrinter(indent = 2)" << endl << "host = 'localhost'" << endl
- << "port = 9090" << endl << "uri = ''" << endl << "framed = False" << endl
- << "ssl = False" << endl << "http = False" << endl << "argi = 1" << endl << endl
- << "if sys.argv[argi] == '-h':" << endl << " parts = sys.argv[argi+1].split(':')"
- << endl << " host = parts[0]" << endl << " if len(parts) > 1:" << endl
- << " port = int(parts[1])" << endl << " argi += 2" << endl << endl
- << "if sys.argv[argi] == '-u':" << endl << " url = urlparse(sys.argv[argi+1])" << endl
- << " parts = url[1].split(':')" << endl << " host = parts[0]" << endl
- << " if len(parts) > 1:" << endl << " port = int(parts[1])" << endl
- << " else:" << endl << " port = 80" << endl << " uri = url[2]" << endl
- << " if url[4]:" << endl << " uri += '?%s' % url[4]" << endl << " http = True"
- << endl << " argi += 2" << endl << endl
- << "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl
- << " framed = True" << endl << " argi += 1" << endl << endl
- << "if sys.argv[argi] == '-s' or sys.argv[argi] == '-ssl':" << endl << " ssl = True"
- << endl << " argi += 1" << endl << endl << "cmd = sys.argv[argi]" << endl
- << "args = sys.argv[argi+1:]" << endl << endl << "if http:" << endl
- << " transport = THttpClient.THttpClient(host, port, uri)" << endl << "else:" << endl
- << " socket = TSSLSocket.TSSLSocket(host, port, validate=False) if ssl else "
- "TSocket.TSocket(host, port)" << endl << " if framed:" << endl
- << " transport = TTransport.TFramedTransport(socket)" << endl << " else:" << endl
- << " transport = TTransport.TBufferedTransport(socket)" << endl
- << "protocol = TBinaryProtocol.TBinaryProtocol(transport)" << endl
- << "client = " << service_name_ << ".Client(protocol)" << endl << "transport.open()"
- << endl << endl;
+ f_remote <<
+ "pp = pprint.PrettyPrinter(indent = 2)" << endl <<
+ "host = 'localhost'" << endl <<
+ "port = 9090" << endl <<
+ "uri = ''" << endl <<
+ "framed = False" << endl <<
+ "ssl = False" << endl <<
+ "http = False" << endl <<
+ "argi = 1" << endl <<
+ endl <<
+ "if sys.argv[argi] == '-h':" << endl <<
+ " parts = sys.argv[argi+1].split(':')" << endl <<
+ " host = parts[0]" << endl <<
+ " if len(parts) > 1:" << endl <<
+ " port = int(parts[1])" << endl <<
+ " argi += 2" << endl <<
+ endl <<
+ "if sys.argv[argi] == '-u':" << endl <<
+ " url = urlparse(sys.argv[argi+1])" << endl <<
+ " parts = url[1].split(':')" << endl <<
+ " host = parts[0]" << endl <<
+ " if len(parts) > 1:" << endl <<
+ " port = int(parts[1])" << endl <<
+ " else:" << endl <<
+ " port = 80" << endl <<
+ " uri = url[2]" << endl <<
+ " if url[4]:" << endl <<
+ " uri += '?%s' % url[4]" << endl <<
+ " http = True" << endl <<
+ " argi += 2" << endl <<
+ endl <<
+ "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl <<
+ " framed = True" << endl <<
+ " argi += 1" << endl <<
+ endl <<
+ "if sys.argv[argi] == '-s' or sys.argv[argi] == '-ssl':" << endl <<
+ " ssl = True" << endl <<
+ " argi += 1" << endl <<
+ endl <<
+ "cmd = sys.argv[argi]" << endl <<
+ "args = sys.argv[argi+1:]" << endl <<
+ endl <<
+ "if http:" << endl <<
+ " transport = THttpClient.THttpClient(host, port, uri)" << endl <<
+ "else:" << endl <<
+ " socket = TSSLSocket.TSSLSocket(host, port, validate=False) if ssl else TSocket.TSocket(host, port)" << endl <<
+ " if framed:" << endl <<
+ " transport = TTransport.TFramedTransport(socket)" << endl <<
+ " else:" << endl <<
+ " transport = TTransport.TBufferedTransport(socket)" << endl <<
+ "protocol = TBinaryProtocol.TBinaryProtocol(transport)" << endl <<
+ "client = " << service_name_ << ".Client(protocol)" << endl <<
+ "transport.open()" << endl <<
+ endl;
// Generate the dispatch methods
bool first = true;
@@ -1468,10 +1675,12 @@
vector<t_field*>::const_iterator a_iter;
int num_args = args.size();
- f_remote << "if cmd == '" << (*f_iter)->get_name() << "':" << endl
- << " if len(args) != " << num_args << ":" << endl << " print('"
- << (*f_iter)->get_name() << " requires " << num_args << " args')" << endl
- << " sys.exit(1)" << endl << " pp.pprint(client." << (*f_iter)->get_name() << "(";
+ f_remote <<
+ "if cmd == '" << (*f_iter)->get_name() << "':" << endl <<
+ " if len(args) != " << num_args << ":" << endl <<
+ " print('" << (*f_iter)->get_name() << " requires " << num_args << " args')" << endl <<
+ " sys.exit(1)" << endl <<
+ " pp.pprint(client." << (*f_iter)->get_name() << "(";
for (int i = 0; i < num_args; ++i) {
if (args[i]->get_type()->is_string()) {
f_remote << "args[" << i << "],";
@@ -1500,11 +1709,16 @@
// Make file executable, love that bitwise OR action
chmod(f_remote_name.c_str(),
- S_IRUSR | S_IWUSR | S_IXUSR
+ S_IRUSR
+ | S_IWUSR
+ | S_IXUSR
#ifndef _WIN32
- | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
+ | S_IRGRP
+ | S_IXGRP
+ | S_IROTH
+ | S_IXOTH
#endif
- );
+ );
#endif // _MSC_VER
}
@@ -1528,71 +1742,88 @@
// Generate the header portion
if (gen_twisted_) {
- f_service_ << "class Processor(" << extends_processor << "TProcessor):" << endl
- << " implements(Iface)" << endl << endl;
+ f_service_ <<
+ "class Processor(" << extends_processor << "TProcessor):" << endl <<
+ " implements(Iface)" << endl << endl;
} else {
- f_service_ << "class Processor(" << extends_processor << "Iface, TProcessor):" << endl;
+ f_service_ <<
+ "class Processor(" << extends_processor << "Iface, TProcessor):" << endl;
}
indent_up();
- indent(f_service_) << "def __init__(self, handler):" << endl;
+ indent(f_service_) <<
+ "def __init__(self, handler):" << endl;
indent_up();
if (extends.empty()) {
if (gen_twisted_) {
- f_service_ << indent() << "self._handler = Iface(handler)" << endl;
+ f_service_ <<
+ indent() << "self._handler = Iface(handler)" << endl;
} else {
- f_service_ << indent() << "self._handler = handler" << endl;
+ f_service_ <<
+ indent() << "self._handler = handler" << endl;
}
- f_service_ << indent() << "self._processMap = {}" << endl;
+ f_service_ <<
+ indent() << "self._processMap = {}" << endl;
} else {
if (gen_twisted_) {
- f_service_ << indent() << extends << ".Processor.__init__(self, Iface(handler))" << endl;
+ f_service_ <<
+ indent() << extends << ".Processor.__init__(self, Iface(handler))" << endl;
} else {
- f_service_ << indent() << extends << ".Processor.__init__(self, handler)" << endl;
+ f_service_ <<
+ indent() << extends << ".Processor.__init__(self, handler)" << endl;
}
}
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
- f_service_ << indent() << "self._processMap[\"" << (*f_iter)->get_name()
- << "\"] = Processor.process_" << (*f_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "self._processMap[\"" << (*f_iter)->get_name() << "\"] = Processor.process_" << (*f_iter)->get_name() << endl;
}
indent_down();
f_service_ << endl;
// Generate the server implementation
- f_service_ << indent() << "def process(self, iprot, oprot):" << endl;
+ f_service_ <<
+ indent() << "def process(self, iprot, oprot):" << endl;
indent_up();
- f_service_ << indent() << "(name, type, seqid) = iprot.readMessageBegin()" << endl;
+ f_service_ <<
+ indent() << "(name, type, seqid) = iprot.readMessageBegin()" << endl;
// TODO(mcslee): validate message
// HOT: dictionary function lookup
- f_service_ << indent() << "if name not in self._processMap:" << endl << indent()
- << " iprot.skip(TType.STRUCT)" << endl << indent() << " iprot.readMessageEnd()"
- << endl << indent()
- << " x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown "
- "function %s' % (name))" << endl << indent()
- << " oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)" << endl << indent()
- << " x.write(oprot)" << endl << indent() << " oprot.writeMessageEnd()" << endl
- << indent() << " oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "if name not in self._processMap:" << endl <<
+ indent() << " iprot.skip(TType.STRUCT)" << endl <<
+ indent() << " iprot.readMessageEnd()" << endl <<
+ indent() << " x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name))" << endl <<
+ indent() << " oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)" << endl <<
+ indent() << " x.write(oprot)" << endl <<
+ indent() << " oprot.writeMessageEnd()" << endl <<
+ indent() << " oprot.trans.flush()" << endl;
if (gen_twisted_) {
- f_service_ << indent() << " return defer.succeed(None)" << endl;
+ f_service_ <<
+ indent() << " return defer.succeed(None)" << endl;
} else {
- f_service_ << indent() << " return" << endl;
+ f_service_ <<
+ indent() << " return" << endl;
}
- f_service_ << indent() << "else:" << endl;
+ f_service_ <<
+ indent() << "else:" << endl;
if (gen_twisted_ || gen_tornado_) {
- f_service_ << indent() << " return self._processMap[name](self, seqid, iprot, oprot)" << endl;
+ f_service_ <<
+ indent() << " return self._processMap[name](self, seqid, iprot, oprot)" << endl;
} else {
- f_service_ << indent() << " self._processMap[name](self, seqid, iprot, oprot)" << endl;
+ f_service_ <<
+ indent() << " self._processMap[name](self, seqid, iprot, oprot)" << endl;
// Read end of args field, the T_STOP, and the struct close
- f_service_ << indent() << "return True" << endl;
+ f_service_ <<
+ indent() << "return True" << endl;
}
indent_down();
@@ -1612,15 +1843,19 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_py_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_py_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open function
if (gen_tornado_) {
- f_service_ << indent() << "@gen.coroutine" << endl << indent() << "def process_"
- << tfunction->get_name() << "(self, seqid, iprot, oprot):" << endl;
+ f_service_ <<
+ indent() << "@gen.coroutine" << endl <<
+ indent() << "def process_" << tfunction->get_name() <<
+ "(self, seqid, iprot, oprot):" << endl;
} else {
- f_service_ << indent() << "def process_" << tfunction->get_name()
- << "(self, seqid, iprot, oprot):" << endl;
+ f_service_ <<
+ indent() << "def process_" << tfunction->get_name() <<
+ "(self, seqid, iprot, oprot):" << endl;
}
indent_up();
@@ -1628,8 +1863,10 @@
string argsname = tfunction->get_name() + "_args";
string resultname = tfunction->get_name() + "_result";
- f_service_ << indent() << "args = " << argsname << "()" << endl << indent() << "args.read(iprot)"
- << endl << indent() << "iprot.readMessageEnd()" << endl;
+ f_service_ <<
+ indent() << "args = " << argsname << "()" << endl <<
+ indent() << "args.read(iprot)" << endl <<
+ indent() << "iprot.readMessageEnd()" << endl;
t_struct* xs = tfunction->get_xceptions();
const std::vector<t_field*>& xceptions = xs->get_members();
@@ -1637,7 +1874,8 @@
// Declare result for non oneway function
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "result = " << resultname << "()" << endl;
+ f_service_ <<
+ indent() << "result = " << resultname << "()" << endl;
}
if (gen_twisted_) {
@@ -1646,8 +1884,9 @@
const std::vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator f_iter;
- f_service_ << indent() << "d = defer.maybeDeferred(self._handler." << tfunction->get_name()
- << ", ";
+ f_service_ <<
+ indent() << "d = defer.maybeDeferred(self._handler." <<
+ tfunction->get_name() << ", ";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1661,61 +1900,76 @@
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return d" << endl;
+ f_service_ <<
+ indent() << "return d" << endl;
indent_down();
f_service_ << endl;
return;
}
- f_service_ << indent() << "d.addCallback(self.write_results_success_" << tfunction->get_name()
- << ", result, seqid, oprot)" << endl;
+ f_service_ <<
+ indent() <<
+ "d.addCallback(self.write_results_success_" <<
+ tfunction->get_name() << ", result, seqid, oprot)" << endl;
if (xceptions.size() > 0) {
- f_service_ << indent() << "d.addErrback(self.write_results_exception_"
- << tfunction->get_name() << ", result, seqid, oprot)" << endl;
+ f_service_ <<
+ indent() <<
+ "d.addErrback(self.write_results_exception_" <<
+ tfunction->get_name() << ", result, seqid, oprot)" << endl;
}
- f_service_ << indent() << "return d" << endl;
+ f_service_ <<
+ indent() << "return d" << endl;
indent_down();
f_service_ << endl;
- indent(f_service_) << "def write_results_success_" << tfunction->get_name()
- << "(self, success, result, seqid, oprot):" << endl;
+ indent(f_service_) <<
+ "def write_results_success_" << tfunction->get_name() <<
+ "(self, success, result, seqid, oprot):" << endl;
indent_up();
- f_service_ << indent() << "result.success = success" << endl << indent()
- << "oprot.writeMessageBegin(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid)" << endl << indent() << "result.write(oprot)"
- << endl << indent() << "oprot.writeMessageEnd()" << endl << indent()
- << "oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "result.success = success" << endl <<
+ indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", TMessageType.REPLY, seqid)" << endl <<
+ indent() << "result.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
indent_down();
f_service_ << endl;
// Try block for a function with exceptions
if (!tfunction->is_oneway() && xceptions.size() > 0) {
- indent(f_service_) << "def write_results_exception_" << tfunction->get_name()
- << "(self, error, result, seqid, oprot):" << endl;
+ indent(f_service_) <<
+ "def write_results_exception_" << tfunction->get_name() <<
+ "(self, error, result, seqid, oprot):" << endl;
indent_up();
- f_service_ << indent() << "try:" << endl;
+ f_service_ <<
+ indent() << "try:" << endl;
// Kinda absurd
- f_service_ << indent() << " error.raiseException()" << endl;
+ f_service_ <<
+ indent() << " error.raiseException()" << endl;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << ", "
- << (*x_iter)->get_name() << ":" << endl;
+ f_service_ <<
+ indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
indent_down();
} else {
- f_service_ << indent() << "pass" << endl;
+ f_service_ <<
+ indent() << "pass" << endl;
}
}
- f_service_ << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid)" << endl << indent() << "result.write(oprot)"
- << endl << indent() << "oprot.writeMessageEnd()" << endl << indent()
- << "oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() <<
+ "\", TMessageType.REPLY, seqid)" << endl <<
+ indent() << "result.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
indent_down();
f_service_ << endl;
}
@@ -1751,14 +2005,16 @@
vector<t_field*>::const_iterator f_iter;
if (xceptions.size() > 0) {
- f_service_ << indent() << "try:" << endl;
+ f_service_ <<
+ indent() << "try:" << endl;
indent_up();
}
f_service_ << indent();
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result.success = ";
}
- f_service_ << "yield gen.maybe_future(self._handler." << tfunction->get_name() << "(";
+ f_service_ <<
+ "yield gen.maybe_future(self._handler." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1773,34 +2029,37 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << ", "
- << (*x_iter)->get_name() << ":" << endl;
+ f_service_ <<
+ indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
indent_down();
} else {
- f_service_ << indent() << "pass" << endl;
+ f_service_ <<
+ indent() << "pass" << endl;
}
}
}
if (!tfunction->is_oneway()) {
- f_service_ << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid)" << endl << indent() << "result.write(oprot)"
- << endl << indent() << "oprot.writeMessageEnd()" << endl << indent()
- << "oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid)" << endl <<
+ indent() << "result.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
}
// Close function
indent_down();
f_service_ << endl;
- } else { // py
+ } else { // py
// Try block for a function with exceptions
if (xceptions.size() > 0) {
- f_service_ << indent() << "try:" << endl;
+ f_service_ <<
+ indent() << "try:" << endl;
indent_up();
}
@@ -1813,7 +2072,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result.success = ";
}
- f_service_ << "self._handler." << tfunction->get_name() << "(";
+ f_service_ <<
+ "self._handler." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1828,31 +2088,34 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
indent_down();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << ", "
- << (*x_iter)->get_name() << ":" << endl;
+ f_service_ <<
+ indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
if (!tfunction->is_oneway()) {
indent_up();
- f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
- << (*x_iter)->get_name() << endl;
+ f_service_ <<
+ indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
indent_down();
} else {
- f_service_ << indent() << "pass" << endl;
+ f_service_ <<
+ indent() << "pass" << endl;
}
}
}
// Shortcut out here for oneway functions
if (tfunction->is_oneway()) {
- f_service_ << indent() << "return" << endl;
+ f_service_ <<
+ indent() << "return" << endl;
indent_down();
f_service_ << endl;
return;
}
- f_service_ << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name()
- << "\", TMessageType.REPLY, seqid)" << endl << indent() << "result.write(oprot)"
- << endl << indent() << "oprot.writeMessageEnd()" << endl << indent()
- << "oprot.trans.flush()" << endl;
+ f_service_ <<
+ indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid)" << endl <<
+ indent() << "result.write(oprot)" << endl <<
+ indent() << "oprot.writeMessageEnd()" << endl <<
+ indent() << "oprot.trans.flush()" << endl;
// Close function
indent_down();
@@ -1863,31 +2126,36 @@
/**
* Deserializes a field of any type.
*/
-void t_py_generator::generate_deserialize_field(ofstream& out,
+void t_py_generator::generate_deserialize_field(ofstream &out,
t_field* tfield,
string prefix,
bool inclass) {
- (void)inclass;
+ (void) inclass;
t_type* type = get_true_type(tfield->get_type());
if (type->is_void()) {
- throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
string name = prefix + tfield->get_name();
if (type->is_struct() || type->is_xception()) {
- generate_deserialize_struct(out, (t_struct*)type, name);
+ 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()) {
- indent(out) << name << " = iprot.";
+ indent(out) <<
+ name << " = 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;
+ throw "compiler error: cannot serialize void field in a struct: " +
+ name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary() || !gen_utf8strings_) {
@@ -1924,24 +2192,28 @@
} else {
printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
- tfield->get_name().c_str(),
- type->get_name().c_str());
+ tfield->get_name().c_str(), type->get_name().c_str());
}
}
/**
* Generates an unserializer for a struct, calling read()
*/
-void t_py_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- out << indent() << prefix << " = " << type_name(tstruct) << "()" << endl << indent() << prefix
- << ".read(iprot)" << endl;
+void t_py_generator::generate_deserialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ out <<
+ indent() << prefix << " = " << type_name(tstruct) << "()" << endl <<
+ indent() << prefix << ".read(iprot)" << endl;
}
/**
* Serialize a container by writing out the header followed by
* data and then a footer.
*/
-void t_py_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_py_generator::generate_deserialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
string size = tmp("_size");
string ktype = tmp("_ktype");
string vtype = tmp("_vtype");
@@ -1954,31 +2226,35 @@
// Declare variables, read header
if (ttype->is_map()) {
- out << indent() << prefix << " = {}" << endl << indent() << "(" << ktype << ", " << vtype
- << ", " << size << " ) = iprot.readMapBegin()" << endl;
+ out <<
+ indent() << prefix << " = {}" << endl <<
+ indent() << "(" << ktype << ", " << vtype << ", " << size << " ) = iprot.readMapBegin()" << endl;
} else if (ttype->is_set()) {
- out << indent() << prefix << " = set()" << endl << indent() << "(" << etype << ", " << size
- << ") = iprot.readSetBegin()" << endl;
+ out <<
+ indent() << prefix << " = set()" << endl <<
+ indent() << "(" << etype << ", " << size << ") = iprot.readSetBegin()" << endl;
} else if (ttype->is_list()) {
- out << indent() << prefix << " = []" << endl << indent() << "(" << etype << ", " << size
- << ") = iprot.readListBegin()" << endl;
+ out <<
+ indent() << prefix << " = []" << endl <<
+ indent() << "(" << etype << ", " << size << ") = iprot.readListBegin()" << endl;
}
// For loop iterates over elements
string i = tmp("_i");
- indent(out) << "for " << i << " in xrange(" << size << "):" << endl;
+ indent(out) <<
+ "for " << i << " in xrange(" << size << "):" << endl;
- indent_up();
+ indent_up();
- if (ttype->is_map()) {
- generate_deserialize_map_element(out, (t_map*)ttype, prefix);
- } else if (ttype->is_set()) {
- generate_deserialize_set_element(out, (t_set*)ttype, prefix);
- } else if (ttype->is_list()) {
- generate_deserialize_list_element(out, (t_list*)ttype, prefix);
- }
+ if (ttype->is_map()) {
+ generate_deserialize_map_element(out, (t_map*)ttype, prefix);
+ } else if (ttype->is_set()) {
+ generate_deserialize_set_element(out, (t_set*)ttype, prefix);
+ } else if (ttype->is_list()) {
+ generate_deserialize_list_element(out, (t_list*)ttype, prefix);
+ }
- indent_down();
+ indent_down();
// Read container end
if (ttype->is_map()) {
@@ -1990,10 +2266,13 @@
}
}
+
/**
* Generates code to deserialize a map
*/
-void t_py_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
+void t_py_generator::generate_deserialize_map_element(ofstream &out,
+ t_map* tmap,
+ string prefix) {
string key = tmp("_key");
string val = tmp("_val");
t_field fkey(tmap->get_key_type(), key);
@@ -2002,64 +2281,79 @@
generate_deserialize_field(out, &fkey);
generate_deserialize_field(out, &fval);
- indent(out) << prefix << "[" << key << "] = " << val << endl;
+ indent(out) <<
+ prefix << "[" << key << "] = " << val << endl;
}
/**
* Write a set element
*/
-void t_py_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
+void t_py_generator::generate_deserialize_set_element(ofstream &out,
+ t_set* tset,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tset->get_elem_type(), elem);
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".add(" << elem << ")" << endl;
+ indent(out) <<
+ prefix << ".add(" << elem << ")" << endl;
}
/**
* Write a list element
*/
-void t_py_generator::generate_deserialize_list_element(ofstream& out,
- t_list* tlist,
- string prefix) {
+void t_py_generator::generate_deserialize_list_element(ofstream &out,
+ t_list* tlist,
+ string prefix) {
string elem = tmp("_elem");
t_field felem(tlist->get_elem_type(), elem);
generate_deserialize_field(out, &felem);
- indent(out) << prefix << ".append(" << elem << ")" << endl;
+ indent(out) <<
+ prefix << ".append(" << elem << ")" << endl;
}
+
/**
* Serializes a field of any type.
*
* @param tfield The field to serialize
* @param prefix Name to prepend to field name
*/
-void t_py_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
+void t_py_generator::generate_serialize_field(ofstream &out,
+ t_field* tfield,
+ string prefix) {
t_type* type = get_true_type(tfield->get_type());
// Do nothing for void types
if (type->is_void()) {
- throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
+ throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+ prefix + tfield->get_name();
}
if (type->is_struct() || type->is_xception()) {
- generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
+ generate_serialize_struct(out,
+ (t_struct*)type,
+ prefix + tfield->get_name());
} else if (type->is_container()) {
- generate_serialize_container(out, type, prefix + tfield->get_name());
+ generate_serialize_container(out,
+ type,
+ prefix + tfield->get_name());
} else if (type->is_base_type() || type->is_enum()) {
string name = prefix + tfield->get_name();
- indent(out) << "oprot.";
+ indent(out) <<
+ "oprot.";
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;
+ throw
+ "compiler error: cannot serialize void field in a struct: " + name;
break;
case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary() || !gen_utf8strings_) {
@@ -2107,52 +2401,67 @@
* @param tstruct The struct to serialize
* @param prefix String prefix to attach to all fields
*/
-void t_py_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
- (void)tstruct;
+void t_py_generator::generate_serialize_struct(ofstream &out,
+ t_struct* tstruct,
+ string prefix) {
+ (void) tstruct;
indent(out) << prefix << ".write(oprot)" << endl;
}
-void t_py_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
+void t_py_generator::generate_serialize_container(ofstream &out,
+ t_type* ttype,
+ string prefix) {
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ", "
- << type_to_enum(((t_map*)ttype)->get_val_type()) << ", "
- << "len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot.writeMapBegin(" <<
+ type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
+ type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
+ "len(" << prefix << "))" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", "
- << "len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot.writeSetBegin(" <<
+ type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
+ "len(" << prefix << "))" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type())
- << ", "
- << "len(" << prefix << "))" << endl;
+ indent(out) <<
+ "oprot.writeListBegin(" <<
+ type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
+ "len(" << prefix << "))" << endl;
}
if (ttype->is_map()) {
string kiter = tmp("kiter");
string viter = tmp("viter");
- indent(out) << "for " << kiter << "," << viter << " in " << prefix << ".items():" << endl;
+ indent(out) <<
+ "for " << kiter << "," << viter << " in " << prefix << ".items():" << endl;
indent_up();
generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
indent_down();
} else if (ttype->is_set()) {
string iter = tmp("iter");
- indent(out) << "for " << iter << " in " << prefix << ":" << endl;
+ indent(out) <<
+ "for " << iter << " in " << prefix << ":" << endl;
indent_up();
generate_serialize_set_element(out, (t_set*)ttype, iter);
indent_down();
} else if (ttype->is_list()) {
string iter = tmp("iter");
- indent(out) << "for " << iter << " in " << prefix << ":" << endl;
+ indent(out) <<
+ "for " << iter << " in " << prefix << ":" << endl;
indent_up();
generate_serialize_list_element(out, (t_list*)ttype, iter);
indent_down();
}
if (ttype->is_map()) {
- indent(out) << "oprot.writeMapEnd()" << endl;
+ indent(out) <<
+ "oprot.writeMapEnd()" << endl;
} else if (ttype->is_set()) {
- indent(out) << "oprot.writeSetEnd()" << endl;
+ indent(out) <<
+ "oprot.writeSetEnd()" << endl;
} else if (ttype->is_list()) {
- indent(out) << "oprot.writeListEnd()" << endl;
+ indent(out) <<
+ "oprot.writeListEnd()" << endl;
}
}
@@ -2160,10 +2469,10 @@
* Serializes the members of a map.
*
*/
-void t_py_generator::generate_serialize_map_element(ofstream& out,
- t_map* tmap,
- string kiter,
- string viter) {
+void t_py_generator::generate_serialize_map_element(ofstream &out,
+ t_map* tmap,
+ string kiter,
+ string viter) {
t_field kfield(tmap->get_key_type(), kiter);
generate_serialize_field(out, &kfield, "");
@@ -2174,7 +2483,9 @@
/**
* Serializes the members of a set.
*/
-void t_py_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
+void t_py_generator::generate_serialize_set_element(ofstream &out,
+ t_set* tset,
+ string iter) {
t_field efield(tset->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2182,7 +2493,9 @@
/**
* Serializes the members of a list.
*/
-void t_py_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
+void t_py_generator::generate_serialize_list_element(ofstream &out,
+ t_list* tlist,
+ string iter) {
t_field efield(tlist->get_elem_type(), iter);
generate_serialize_field(out, &efield, "");
}
@@ -2190,14 +2503,16 @@
/**
* Generates the docstring for a given struct.
*/
-void t_py_generator::generate_python_docstring(ofstream& out, t_struct* tstruct) {
+void t_py_generator::generate_python_docstring(ofstream& out,
+ t_struct* tstruct) {
generate_python_docstring(out, tstruct, tstruct, "Attributes");
}
/**
* Generates the docstring for a given function.
*/
-void t_py_generator::generate_python_docstring(ofstream& out, t_function* tfunction) {
+void t_py_generator::generate_python_docstring(ofstream& out,
+ t_function* tfunction) {
generate_python_docstring(out, tfunction, tfunction->get_arglist(), "Parameters");
}
@@ -2205,7 +2520,7 @@
* Generates the docstring for a struct or function.
*/
void t_py_generator::generate_python_docstring(ofstream& out,
- t_doc* tdoc,
+ t_doc* tdoc,
t_struct* tstruct,
const char* subheader) {
bool has_doc = false;
@@ -2235,16 +2550,23 @@
}
if (has_doc) {
- generate_docstring_comment(out, "\"\"\"\n", "", ss.str(), "\"\"\"\n");
+ generate_docstring_comment(out,
+ "\"\"\"\n",
+ "", ss.str(),
+ "\"\"\"\n");
}
}
/**
* Generates the docstring for a generic object.
*/
-void t_py_generator::generate_python_docstring(ofstream& out, t_doc* tdoc) {
+void t_py_generator::generate_python_docstring(ofstream& out,
+ t_doc* tdoc) {
if (tdoc->has_doc()) {
- generate_docstring_comment(out, "\"\"\"\n", "", tdoc->get_doc(), "\"\"\"\n");
+ generate_docstring_comment(out,
+ "\"\"\"\n",
+ "", tdoc->get_doc(),
+ "\"\"\"\n");
}
}
@@ -2257,7 +2579,8 @@
std::ostringstream result;
result << tfield->get_name() << "=";
if (tfield->get_value() != NULL) {
- result << "thrift_spec[" << tfield->get_key() << "][4]";
+ result << "thrift_spec[" <<
+ tfield->get_key() << "][4]";
} else {
result << "None";
}
@@ -2284,7 +2607,8 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_py_generator::function_signature(t_function* tfunction, bool interface) {
+string t_py_generator::function_signature(t_function* tfunction,
+ bool interface) {
vector<string> pre;
vector<string> post;
string signature = tfunction->get_name() + "(";
@@ -2300,7 +2624,7 @@
/**
* Renders a field list
*/
-string t_py_generator::argument_list(t_struct* tstruct, vector<string>* pre, vector<string>* post) {
+string t_py_generator::argument_list(t_struct* tstruct, vector<string> *pre, vector<string> *post) {
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@@ -2401,33 +2725,38 @@
} else if (ttype->is_struct() || ttype->is_xception()) {
return "(" + type_name(ttype) + ", " + type_name(ttype) + ".thrift_spec)";
} else if (ttype->is_map()) {
- return "(" + type_to_enum(((t_map*)ttype)->get_key_type()) + ","
- + type_to_spec_args(((t_map*)ttype)->get_key_type()) + ","
- + type_to_enum(((t_map*)ttype)->get_val_type()) + ","
- + type_to_spec_args(((t_map*)ttype)->get_val_type()) + ")";
+ return "(" +
+ type_to_enum(((t_map*)ttype)->get_key_type()) + "," +
+ type_to_spec_args(((t_map*)ttype)->get_key_type()) + "," +
+ type_to_enum(((t_map*)ttype)->get_val_type()) + "," +
+ type_to_spec_args(((t_map*)ttype)->get_val_type()) +
+ ")";
} else if (ttype->is_set()) {
- return "(" + type_to_enum(((t_set*)ttype)->get_elem_type()) + ","
- + type_to_spec_args(((t_set*)ttype)->get_elem_type()) + ")";
+ return "(" +
+ type_to_enum(((t_set*)ttype)->get_elem_type()) + "," +
+ type_to_spec_args(((t_set*)ttype)->get_elem_type()) +
+ ")";
} else if (ttype->is_list()) {
- return "(" + type_to_enum(((t_list*)ttype)->get_elem_type()) + ","
- + type_to_spec_args(((t_list*)ttype)->get_elem_type()) + ")";
+ return "(" +
+ type_to_enum(((t_list*)ttype)->get_elem_type()) + "," +
+ type_to_spec_args(((t_list*)ttype)->get_elem_type()) +
+ ")";
}
throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name();
}
-THRIFT_REGISTER_GENERATOR(
- py,
- "Python",
- " new_style: Generate new-style classes.\n"
- " twisted: Generate Twisted-friendly RPC services.\n"
- " tornado: Generate code for use with Tornado.\n"
- " utf8strings: Encode/decode strings using utf8 in the generated code.\n"
- " slots: Generate code using slots for instance members.\n"
- " dynamic: Generate dynamic code, less code generated but slower.\n"
- " dynbase=CLS Derive generated classes from class CLS instead of TBase.\n"
- " dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.\n"
- " dynimport='from foo.bar import CLS'\n"
- " Add an import line to generated code to find the dynbase class.\n")
+
+THRIFT_REGISTER_GENERATOR(py, "Python",
+" new_style: Generate new-style classes.\n" \
+" twisted: Generate Twisted-friendly RPC services.\n" \
+" tornado: Generate code for use with Tornado.\n" \
+" utf8strings: Encode/decode strings using utf8 in the generated code.\n" \
+" slots: Generate code using slots for instance members.\n" \
+" dynamic: Generate dynamic code, less code generated but slower.\n" \
+" dynbase=CLS Derive generated classes from class CLS instead of TBase.\n" \
+" dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.\n" \
+" dynimport='from foo.bar import CLS'\n" \
+" Add an import line to generated code to find the dynbase class.\n")
diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc
index 49708c7..0a15500 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -43,26 +43,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* A subclass of std::ofstream that includes indenting functionality.
*/
class t_rb_ofstream : public std::ofstream {
-private:
- int indent_;
+ private:
+ int indent_;
-public:
- t_rb_ofstream() : std::ofstream(), indent_(0) {}
- explicit t_rb_ofstream(const char* filename,
- ios_base::openmode mode = ios_base::out,
- int indent = 0)
- : std::ofstream(filename, mode), indent_(indent) {}
+ public:
+ t_rb_ofstream() : std::ofstream(), indent_(0) { }
+ explicit t_rb_ofstream(const char* filename, ios_base::openmode mode = ios_base::out, int indent = 0) :
+ std::ofstream(filename, mode), indent_(indent) { }
t_rb_ofstream& indent() {
- for (int i = 0; i < indent_; ++i) {
- *this << " ";
- }
+ for (int i = 0; i < indent_; ++i) { *this << " "; }
return *this;
}
@@ -75,12 +71,14 @@
*
*/
class t_rb_generator : public t_oop_generator {
-public:
- t_rb_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)option_string;
+ public:
+ t_rb_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) option_string;
out_dir_base_ = "gen-rb";
require_rubygems_ = (parsed_options.find("rubygems") != parsed_options.end());
@@ -98,13 +96,13 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_union(t_struct* tunion);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_union (t_struct* tunion);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
t_rb_ofstream& render_const_value(t_rb_ofstream& out, t_type* type, t_const_value* value);
@@ -119,62 +117,77 @@
void generate_rb_function_helpers(t_function* tfunction);
void generate_rb_simple_constructor(t_rb_ofstream& out, t_struct* tstruct);
void generate_rb_simple_exception_constructor(t_rb_ofstream& out, t_struct* tstruct);
- void generate_field_constants(t_rb_ofstream& out, t_struct* tstruct);
- void generate_field_constructors(t_rb_ofstream& out, t_struct* tstruct);
- void generate_field_defns(t_rb_ofstream& out, t_struct* tstruct);
- void generate_field_data(t_rb_ofstream& out,
- t_type* field_type,
- const std::string& field_name,
- t_const_value* field_value,
- bool optional);
+ void generate_field_constants (t_rb_ofstream& out, t_struct* tstruct);
+ void generate_field_constructors (t_rb_ofstream& out, t_struct* tstruct);
+ void generate_field_defns (t_rb_ofstream& out, t_struct* tstruct);
+ void generate_field_data (t_rb_ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value, bool optional);
/**
* Service-level generation functions
*/
- void generate_service_helpers(t_service* tservice);
- void generate_service_interface(t_service* tservice);
- void generate_service_client(t_service* tservice);
- void generate_service_server(t_service* tservice);
- void generate_process_function(t_service* tservice, t_function* tfunction);
+ void generate_service_helpers (t_service* tservice);
+ void generate_service_interface (t_service* tservice);
+ void generate_service_client (t_service* tservice);
+ void generate_service_server (t_service* tservice);
+ void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
- void generate_deserialize_field(t_rb_ofstream& out,
- t_field* tfield,
- std::string prefix = "",
- bool inclass = false);
+ void generate_deserialize_field (t_rb_ofstream &out,
+ t_field* tfield,
+ std::string prefix="",
+ bool inclass=false);
- void generate_deserialize_struct(t_rb_ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_deserialize_struct (t_rb_ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_deserialize_container(t_rb_ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_deserialize_container (t_rb_ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_deserialize_set_element(t_rb_ofstream& out, t_set* tset, std::string prefix = "");
+ void generate_deserialize_set_element (t_rb_ofstream &out,
+ t_set* tset,
+ std::string prefix="");
- void generate_deserialize_map_element(t_rb_ofstream& out, t_map* tmap, std::string prefix = "");
+ void generate_deserialize_map_element (t_rb_ofstream &out,
+ t_map* tmap,
+ std::string prefix="");
- void generate_deserialize_list_element(t_rb_ofstream& out,
- t_list* tlist,
- std::string prefix = "");
+ void generate_deserialize_list_element (t_rb_ofstream &out,
+ t_list* tlist,
+ std::string prefix="");
- void generate_serialize_field(t_rb_ofstream& out, t_field* tfield, std::string prefix = "");
+ void generate_serialize_field (t_rb_ofstream &out,
+ t_field* tfield,
+ std::string prefix="");
- void generate_serialize_struct(t_rb_ofstream& out, t_struct* tstruct, std::string prefix = "");
+ void generate_serialize_struct (t_rb_ofstream &out,
+ t_struct* tstruct,
+ std::string prefix="");
- void generate_serialize_container(t_rb_ofstream& out, t_type* ttype, std::string prefix = "");
+ void generate_serialize_container (t_rb_ofstream &out,
+ t_type* ttype,
+ std::string prefix="");
- void generate_serialize_map_element(t_rb_ofstream& out,
- t_map* tmap,
- std::string kiter,
- std::string viter);
+ void generate_serialize_map_element (t_rb_ofstream &out,
+ t_map* tmap,
+ std::string kiter,
+ std::string viter);
- void generate_serialize_set_element(t_rb_ofstream& out, t_set* tmap, std::string iter);
+ void generate_serialize_set_element (t_rb_ofstream &out,
+ t_set* tmap,
+ std::string iter);
- void generate_serialize_list_element(t_rb_ofstream& out, t_list* tlist, std::string iter);
+ void generate_serialize_list_element (t_rb_ofstream &out,
+ t_list* tlist,
+ std::string iter);
- void generate_rdoc(t_rb_ofstream& out, t_doc* tdoc);
+ void generate_rdoc (t_rb_ofstream& out,
+ t_doc* tdoc);
/**
* Helper rendering functions
@@ -186,11 +199,12 @@
std::string declare_field(t_field* tfield);
std::string type_name(t_type* ttype);
std::string full_type_name(t_type* ttype);
- std::string function_signature(t_function* tfunction, std::string prefix = "");
+ std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string rb_namespace_to_path_prefix(std::string rb_namespace);
+
std::vector<std::string> ruby_modules(t_program* p) {
std::string ns = p->get_namespace("rb");
std::vector<std::string> modules;
@@ -215,7 +229,8 @@
void begin_namespace(t_rb_ofstream&, std::vector<std::string>);
void end_namespace(t_rb_ofstream&, std::vector<std::string>);
-private:
+ private:
+
/**
* File streams
*/
@@ -234,6 +249,7 @@
bool namespaced_;
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -255,26 +271,31 @@
while ((loc = dir.find("/")) != string::npos) {
subdir = subdir + dir.substr(0, loc) + "/";
MKDIR(subdir.c_str());
- dir = dir.substr(loc + 1);
+ dir = dir.substr(loc+1);
}
}
namespace_dir_ = subdir;
// Make output file
- string f_types_name = namespace_dir_ + underscore(program_name_) + "_types.rb";
+ string f_types_name = namespace_dir_+underscore(program_name_)+"_types.rb";
f_types_.open(f_types_name.c_str());
- string f_consts_name = namespace_dir_ + underscore(program_name_) + "_constants.rb";
+ string f_consts_name = namespace_dir_+underscore(program_name_)+"_constants.rb";
f_consts_.open(f_consts_name.c_str());
// Print header
- f_types_ << rb_autogen_comment() << endl << render_require_thrift() << render_includes() << endl;
- begin_namespace(f_types_, ruby_modules(program_));
+ f_types_ <<
+ rb_autogen_comment() << endl << render_require_thrift() <<
+ render_includes() << endl;
+ begin_namespace(f_types_, ruby_modules(program_));
- f_consts_ << rb_autogen_comment() << endl << render_require_thrift() << "require '"
- << require_prefix_ << underscore(program_name_) << "_types'" << endl << endl;
- begin_namespace(f_consts_, ruby_modules(program_));
+ f_consts_ <<
+ rb_autogen_comment() << endl << render_require_thrift() <<
+ "require '" << require_prefix_ << underscore(program_name_) << "_types'" << endl <<
+ endl;
+ begin_namespace(f_consts_, ruby_modules(program_));
+
}
/**
@@ -296,8 +317,7 @@
string result = "";
for (size_t i = 0; i < includes.size(); ++i) {
t_program* included = includes[i];
- std::string included_require_prefix
- = rb_namespace_to_path_prefix(included->get_namespace("rb"));
+ std::string included_require_prefix = rb_namespace_to_path_prefix(included->get_namespace("rb"));
std::string included_name = included->get_name();
result += "require '" + included_require_prefix + underscore(included_name) + "_types'\n";
}
@@ -311,8 +331,12 @@
* Autogen'd comment
*/
string t_rb_generator::rb_autogen_comment() {
- return std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
- + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n";
+ return
+ std::string("#\n") +
+ "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "#\n" +
+ "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "#\n";
}
/**
@@ -332,7 +356,7 @@
* @param ttypedef The type definition
*/
void t_rb_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
/**
@@ -342,7 +366,8 @@
* @param tenum The enumeration
*/
void t_rb_generator::generate_enum(t_enum* tenum) {
- f_types_.indent() << "module " << capitalize(tenum->get_name()) << endl;
+ f_types_.indent() <<
+ "module " << capitalize(tenum->get_name()) << endl;
f_types_.indent_up();
vector<t_enum_value*> constants = tenum->get_constants();
@@ -359,14 +384,12 @@
f_types_.indent() << name << " = " << value << endl;
}
- // Create a hash mapping values back to their names (as strings) since ruby has no native enum
- // type
+ // Create a hash mapping values back to their names (as strings) since ruby has no native enum type
f_types_.indent() << "VALUE_MAP = {";
- for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+ for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
// Populate the hash
int value = (*c_iter)->get_value();
- if (c_iter != constants.begin())
- f_types_ << ", ";
+ if (c_iter != constants.begin()) f_types_ << ", ";
f_types_ << value << " => \"" << capitalize((*c_iter)->get_name()) << "\"";
}
f_types_ << "}" << endl;
@@ -375,14 +398,14 @@
f_types_.indent() << "VALID_VALUES = Set.new([";
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
// Populate the set
- if (c_iter != constants.begin())
- f_types_ << ", ";
+ if (c_iter != constants.begin()) f_types_ << ", ";
f_types_ << capitalize((*c_iter)->get_name());
}
f_types_ << "]).freeze" << endl;
f_types_.indent_down();
- f_types_.indent() << "end" << endl << endl;
+ f_types_.indent() <<
+ "end" << endl << endl;
}
/**
@@ -404,9 +427,7 @@
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
-t_rb_ofstream& t_rb_generator::render_const_value(t_rb_ofstream& out,
- t_type* type,
- t_const_value* value) {
+t_rb_ofstream& t_rb_generator::render_const_value(t_rb_ofstream& out, t_type* type, t_const_value* value) {
type = get_true_type(type);
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
@@ -527,9 +548,7 @@
/**
* Generates a ruby struct
*/
-void t_rb_generator::generate_rb_struct(t_rb_ofstream& out,
- t_struct* tstruct,
- bool is_exception = false) {
+void t_rb_generator::generate_rb_struct(t_rb_ofstream& out, t_struct* tstruct, bool is_exception = false) {
generate_rdoc(out, tstruct);
out.indent() << "class " << type_name(tstruct);
if (is_exception) {
@@ -554,13 +573,12 @@
out.indent() << "end" << endl << endl;
}
+
/**
* Generates a ruby union
*/
-void t_rb_generator::generate_rb_union(t_rb_ofstream& out,
- t_struct* tstruct,
- bool is_exception = false) {
- (void)is_exception;
+void t_rb_generator::generate_rb_union(t_rb_ofstream& out, t_struct* tstruct, bool is_exception = false) {
+ (void) is_exception;
generate_rdoc(out, tstruct);
out.indent() << "class " << type_name(tstruct) << " < ::Thrift::Union" << endl;
@@ -604,8 +622,7 @@
out << endl;
}
-void t_rb_generator::generate_rb_simple_exception_constructor(t_rb_ofstream& out,
- t_struct* tstruct) {
+void t_rb_generator::generate_rb_simple_exception_constructor(t_rb_ofstream& out, t_struct* tstruct) {
const vector<t_field*>& members = tstruct->get_members();
if (members.size() == 1) {
@@ -657,24 +674,19 @@
out.indent() << upcase_string((*f_iter)->get_name()) << " => ";
- generate_field_data(out,
- (*f_iter)->get_type(),
- (*f_iter)->get_name(),
- (*f_iter)->get_value(),
- (*f_iter)->get_req() == t_field::T_OPTIONAL);
+ generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value(),
+ (*f_iter)->get_req() == t_field::T_OPTIONAL);
}
out.indent_down();
out << endl;
out.indent() << "}" << endl << endl;
out.indent() << "def struct_fields; FIELDS; end" << endl << endl;
+
}
-void t_rb_generator::generate_field_data(t_rb_ofstream& out,
- t_type* field_type,
- const std::string& field_name = "",
- t_const_value* field_value = NULL,
- bool optional = false) {
+void t_rb_generator::generate_field_data(t_rb_ofstream& out, t_type* field_type,
+ const std::string& field_name = "", t_const_value* field_value = NULL, bool optional = false) {
field_type = get_true_type(field_type);
// Begin this field's defn
@@ -710,7 +722,7 @@
}
}
- if (optional) {
+ if(optional) {
out << ", :optional => true";
}
@@ -730,31 +742,33 @@
}
void t_rb_generator::end_namespace(t_rb_ofstream& out, vector<std::string> modules) {
- for (vector<std::string>::reverse_iterator m_iter = modules.rbegin(); m_iter != modules.rend();
- ++m_iter) {
+ for (vector<std::string>::reverse_iterator m_iter = modules.rbegin(); m_iter != modules.rend(); ++m_iter) {
out.indent_down();
out.indent() << "end" << endl;
}
}
+
/**
* Generates a thrift service.
*
* @param tservice The service definition
*/
void t_rb_generator::generate_service(t_service* tservice) {
- string f_service_name = namespace_dir_ + underscore(service_name_) + ".rb";
+ string f_service_name = namespace_dir_+underscore(service_name_)+".rb";
f_service_.open(f_service_name.c_str());
- f_service_ << rb_autogen_comment() << endl << render_require_thrift();
+ f_service_ <<
+ rb_autogen_comment() << endl << render_require_thrift();
if (tservice->get_extends() != NULL) {
- f_service_ << "require '" << require_prefix_ << underscore(tservice->get_extends()->get_name())
- << "'" << endl;
+ f_service_ <<
+ "require '" << require_prefix_ << underscore(tservice->get_extends()->get_name()) << "'" << endl;
}
- f_service_ << "require '" << require_prefix_ << underscore(program_name_) << "_types'" << endl
- << endl;
+ f_service_ <<
+ "require '" << require_prefix_ << underscore(program_name_) << "_types'" << endl <<
+ endl;
begin_namespace(f_service_, ruby_modules(tservice->get_program()));
@@ -844,43 +858,46 @@
// Open function
f_service_.indent() << "def " << function_signature(*f_iter) << endl;
f_service_.indent_up();
- f_service_.indent() << "send_" << funname << "(";
+ f_service_.indent() <<
+ "send_" << funname << "(";
- bool first = true;
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- if (first) {
- first = false;
- } else {
- f_service_ << ", ";
+ bool first = true;
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << (*fld_iter)->get_name();
}
- f_service_ << (*fld_iter)->get_name();
- }
- f_service_ << ")" << endl;
+ f_service_ << ")" << endl;
- if (!(*f_iter)->is_oneway()) {
- f_service_.indent();
- if (!(*f_iter)->get_returntype()->is_void()) {
- f_service_ << "return ";
+ if (!(*f_iter)->is_oneway()) {
+ f_service_.indent();
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ << "return ";
+ }
+ f_service_ <<
+ "recv_" << funname << "()" << endl;
}
- f_service_ << "recv_" << funname << "()" << endl;
- }
f_service_.indent_down();
f_service_.indent() << "end" << endl;
f_service_ << endl;
- f_service_.indent() << "def send_" << function_signature(*f_iter) << endl;
+ f_service_.indent() <<
+ "def send_" << function_signature(*f_iter) << endl;
f_service_.indent_up();
- std::string argsname = capitalize((*f_iter)->get_name() + "_args");
- std::string messageSendProc = (*f_iter)->is_oneway() ? "send_oneway_message" : "send_message";
+ std::string argsname = capitalize((*f_iter)->get_name() + "_args");
+ std::string messageSendProc = (*f_iter)->is_oneway() ? "send_oneway_message" : "send_message";
- f_service_.indent() << messageSendProc << "('" << funname << "', " << argsname;
+ f_service_.indent() << messageSendProc << "('" << funname << "', " << argsname;
- for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- f_service_ << ", :" << (*fld_iter)->get_name() << " => " << (*fld_iter)->get_name();
- }
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ << ", :" << (*fld_iter)->get_name() << " => " << (*fld_iter)->get_name();
+ }
- f_service_ << ")" << endl;
+ f_service_ << ")" << endl;
f_service_.indent_down();
f_service_.indent() << "end" << endl;
@@ -910,18 +927,19 @@
const std::vector<t_field*>& xceptions = xs->get_members();
vector<t_field*>::const_iterator x_iter;
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_.indent() << "raise result." << (*x_iter)->get_name() << " unless result."
- << (*x_iter)->get_name() << ".nil?" << endl;
+ f_service_.indent() <<
+ "raise result." << (*x_iter)->get_name() <<
+ " unless result." << (*x_iter)->get_name() << ".nil?" << endl;
}
// Careful, only return _result if not a void function
if ((*f_iter)->get_returntype()->is_void()) {
- f_service_.indent() << "return" << endl;
+ f_service_.indent() <<
+ "return" << endl;
} else {
- f_service_.indent() << "raise "
- "::Thrift::ApplicationException.new(::Thrift::ApplicationException::"
- "MISSING_RESULT, '" << (*f_iter)->get_name()
- << " failed: unknown result')" << endl;
+ f_service_.indent() <<
+ "raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, '" <<
+ (*f_iter)->get_name() << " failed: unknown result')" << endl;
}
// Close function
@@ -952,7 +970,8 @@
}
// Generate the header portion
- f_service_.indent() << "class Processor" << extends_processor << endl;
+ f_service_.indent() <<
+ "class Processor" << extends_processor << endl;
f_service_.indent_up();
f_service_.indent() << "include ::Thrift::Processor" << endl << endl;
@@ -971,10 +990,13 @@
*
* @param tfunction The function to write a dispatcher for
*/
-void t_rb_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
- (void)tservice;
+void t_rb_generator::generate_process_function(t_service* tservice,
+ t_function* tfunction) {
+ (void) tservice;
// Open function
- f_service_.indent() << "def process_" << tfunction->get_name() << "(seqid, iprot, oprot)" << endl;
+ f_service_.indent() <<
+ "def process_" << tfunction->get_name() <<
+ "(seqid, iprot, oprot)" << endl;
f_service_.indent_up();
string argsname = capitalize(tfunction->get_name()) + "_args";
@@ -1006,7 +1028,8 @@
if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
f_service_ << "result.success = ";
}
- f_service_ << "@handler." << tfunction->get_name() << "(";
+ f_service_ <<
+ "@handler." << tfunction->get_name() << "(";
bool first = true;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if (first) {
@@ -1021,12 +1044,11 @@
if (!tfunction->is_oneway() && xceptions.size() > 0) {
f_service_.indent_down();
for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
- f_service_.indent() << "rescue " << full_type_name((*x_iter)->get_type()) << " => "
- << (*x_iter)->get_name() << endl;
+ f_service_.indent() << "rescue " << full_type_name((*x_iter)->get_type()) << " => " <<
+ (*x_iter)->get_name() << endl;
if (!tfunction->is_oneway()) {
f_service_.indent_up();
- f_service_.indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name()
- << endl;
+ f_service_.indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
f_service_.indent_down();
}
}
@@ -1041,8 +1063,7 @@
return;
}
- f_service_.indent() << "write_result(result, oprot, '" << tfunction->get_name() << "', seqid)"
- << endl;
+ f_service_.indent() << "write_result(result, oprot, '" << tfunction->get_name() << "', seqid)" << endl;
// Close function
f_service_.indent_down();
@@ -1055,9 +1076,12 @@
* @param tfunction Function definition
* @return String of rendered function definition
*/
-string t_rb_generator::function_signature(t_function* tfunction, string prefix) {
+string t_rb_generator::function_signature(t_function* tfunction,
+ string prefix) {
// TODO(mcslee): Nitpicky, no ',' if argument_list is empty
- return prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ")";
+ return
+ prefix + tfunction->get_name() +
+ "(" + argument_list(tfunction->get_arglist()) + ")";
}
/**
@@ -1094,7 +1118,8 @@
string t_rb_generator::full_type_name(t_type* ttype) {
string prefix = "::";
vector<std::string> modules = ruby_modules(ttype->get_program());
- for (vector<std::string>::iterator m_iter = modules.begin(); m_iter != modules.end(); ++m_iter) {
+ for (vector<std::string>::iterator m_iter = modules.begin();
+ m_iter != modules.end(); ++m_iter) {
prefix += *m_iter + "::";
}
return prefix + type_name(ttype);
@@ -1149,7 +1174,7 @@
while ((loc = namespaces_left.find(".")) != string::npos) {
path_prefix = path_prefix + underscore(namespaces_left.substr(0, loc)) + "/";
- namespaces_left = namespaces_left.substr(loc + 1);
+ namespaces_left = namespaces_left.substr(loc+1);
}
if (namespaces_left.size() > 0) {
path_prefix = path_prefix + underscore(namespaces_left) + "/";
@@ -1157,14 +1182,17 @@
return path_prefix;
}
+
void t_rb_generator::generate_rdoc(t_rb_ofstream& out, t_doc* tdoc) {
if (tdoc->has_doc()) {
out.indent();
- generate_docstring_comment(out, "", "# ", tdoc->get_doc(), "");
+ generate_docstring_comment(out,
+ "", "# ", tdoc->get_doc(), "");
}
}
-void t_rb_generator::generate_rb_struct_required_validator(t_rb_ofstream& out, t_struct* tstruct) {
+void t_rb_generator::generate_rb_struct_required_validator(t_rb_ofstream& out,
+ t_struct* tstruct) {
out.indent() << "def validate" << endl;
out.indent_up();
@@ -1174,8 +1202,9 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = (*f_iter);
if (field->get_req() == t_field::T_REQUIRED) {
- out.indent() << "raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, "
- "'Required field " << field->get_name() << " is unset!')";
+ out.indent() <<
+ "raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field " <<
+ field->get_name() << " is unset!')";
if (field->get_type()->is_bool()) {
out << " if @" << field->get_name() << ".nil?";
} else {
@@ -1189,13 +1218,14 @@
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
t_field* field = (*f_iter);
- if (field->get_type()->is_enum()) {
- out.indent() << "unless @" << field->get_name() << ".nil? || "
- << full_type_name(field->get_type()) << "::VALID_VALUES.include?(@"
- << field->get_name() << ")" << endl;
+ if (field->get_type()->is_enum()){
+ out.indent() << "unless @" << field->get_name() << ".nil? || " <<
+ full_type_name(field->get_type()) << "::VALID_VALUES.include?(@" <<
+ field->get_name() << ")" << endl;
out.indent_up();
- out.indent() << "raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, "
- "'Invalid value of field " << field->get_name() << "!')" << endl;
+ out.indent() <<
+ "raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Invalid value of field " <<
+ field->get_name() << "!')" << endl;
out.indent_down();
out.indent() << "end" << endl;
}
@@ -1205,28 +1235,27 @@
out.indent() << "end" << endl << endl;
}
-void t_rb_generator::generate_rb_union_validator(t_rb_ofstream& out, t_struct* tstruct) {
+void t_rb_generator::generate_rb_union_validator(t_rb_ofstream& out,
+ t_struct* tstruct) {
out.indent() << "def validate" << endl;
out.indent_up();
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator f_iter;
- out.indent()
- << "raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?"
- << endl;
+ out.indent() <<
+ "raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?" << endl;
// if field is an enum, check that its value is valid
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
const t_field* field = (*f_iter);
- if (field->get_type()->is_enum()) {
+ if (field->get_type()->is_enum()){
out.indent() << "if get_set_field == :" << field->get_name() << endl;
- out.indent() << " raise "
- "::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, "
- "'Invalid value of field " << field->get_name() << "!') unless "
- << full_type_name(field->get_type()) << "::VALID_VALUES.include?(get_value)"
- << endl;
+ out.indent() <<
+ " raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Invalid value of field " <<
+ field->get_name() << "!') unless " << full_type_name(field->get_type()) <<
+ "::VALID_VALUES.include?(get_value)" << endl;
out.indent() << "end" << endl;
}
}
@@ -1235,8 +1264,6 @@
out.indent() << "end" << endl << endl;
}
-THRIFT_REGISTER_GENERATOR(
- rb,
- "Ruby",
- " rubygems: Add a \"require 'rubygems'\" line to the top of each generated file.\n"
- " namespaced: Generate files in idiomatic namespaced directories.\n")
+THRIFT_REGISTER_GENERATOR(rb, "Ruby",
+" rubygems: Add a \"require 'rubygems'\" line to the top of each generated file.\n"
+" namespaced: Generate files in idiomatic namespaced directories.\n")
diff --git a/compiler/cpp/src/generate/t_st_generator.cc b/compiler/cpp/src/generate/t_st_generator.cc
index 9235f68..32ffd88 100644
--- a/compiler/cpp/src/generate/t_st_generator.cc
+++ b/compiler/cpp/src/generate/t_st_generator.cc
@@ -43,20 +43,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* Smalltalk code generator.
*
*/
class t_st_generator : public t_oop_generator {
-public:
- t_st_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_oop_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_st_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-st";
}
@@ -71,14 +73,15 @@
* Program-level generation functions
*/
- void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum);
- void generate_const(t_const* tconst);
- void generate_struct(t_struct* tstruct);
- void generate_xception(t_struct* txception);
- void generate_service(t_service* tservice);
- void generate_class_side_definition();
- void generate_force_consts();
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+ void generate_class_side_definition ();
+ void generate_force_consts ();
+
std::string render_const_value(t_type* type, t_const_value* value);
@@ -86,30 +89,30 @@
* Struct generation code
*/
- void generate_st_struct(std::ofstream& out, t_struct* tstruct, bool is_exception);
- void generate_accessors(std::ofstream& out, t_struct* tstruct);
+ void generate_st_struct (std::ofstream& out, t_struct* tstruct, bool is_exception);
+ void generate_accessors (std::ofstream& out, t_struct* tstruct);
/**
* Service-level generation functions
*/
- void generate_service_client(t_service* tservice);
+ void generate_service_client (t_service* tservice);
- void generate_send_method(t_function* tfunction);
- void generate_recv_method(t_function* tfunction);
+ void generate_send_method (t_function* tfunction);
+ void generate_recv_method (t_function* tfunction);
- std::string map_reader(t_map* tmap);
- std::string list_reader(t_list* tlist);
- std::string set_reader(t_set* tset);
- std::string struct_reader(t_struct* tstruct, std::string clsName);
+ std::string map_reader (t_map *tmap);
+ std::string list_reader (t_list *tlist);
+ std::string set_reader (t_set *tset);
+ std::string struct_reader (t_struct *tstruct, std::string clsName);
- std::string map_writer(t_map* tmap, std::string name);
- std::string list_writer(t_list* tlist, std::string name);
- std::string set_writer(t_set* tset, std::string name);
- std::string struct_writer(t_struct* tstruct, std::string fname);
+ std::string map_writer (t_map *tmap, std::string name);
+ std::string list_writer (t_list *tlist, std::string name);
+ std::string set_writer (t_set *tset, std::string name);
+ std::string struct_writer (t_struct *tstruct, std::string fname);
- std::string write_val(t_type* t, std::string fname);
- std::string read_val(t_type* t);
+ std::string write_val (t_type *t, std::string fname);
+ std::string read_val (t_type *t);
/**
* Helper rendering functions
@@ -117,15 +120,15 @@
std::string st_autogen_comment();
- void st_class_def(std::ofstream& out, std::string name);
- void st_method(std::ofstream& out, std::string cls, std::string name);
- void st_method(std::ofstream& out, std::string cls, std::string name, std::string category);
- void st_close_method(std::ofstream& out);
- void st_class_method(std::ofstream& out, std::string cls, std::string name);
- void st_class_method(std::ofstream& out, std::string cls, std::string name, std::string category);
- void st_setter(std::ofstream& out, std::string cls, std::string name, std::string type);
- void st_getter(std::ofstream& out, std::string cls, std::string name);
- void st_accessors(std::ofstream& out, std::string cls, std::string name, std::string type);
+ void st_class_def(std::ofstream &out, std::string name);
+ void st_method(std::ofstream &out, std::string cls, std::string name);
+ void st_method(std::ofstream &out, std::string cls, std::string name, std::string category);
+ void st_close_method(std::ofstream &out);
+ void st_class_method(std::ofstream &out, std::string cls, std::string name);
+ void st_class_method(std::ofstream &out, std::string cls, std::string name, std::string category);
+ void st_setter(std::ofstream &out, std::string cls, std::string name, std::string type);
+ void st_getter(std::ofstream &out, std::string cls, std::string name);
+ void st_accessors(std::ofstream &out, std::string cls, std::string name, std::string type);
std::string class_name();
static bool is_valid_namespace(const std::string& sub_namespace);
@@ -144,14 +147,17 @@
std::string temp_name();
std::string generated_category();
-private:
+ private:
+
/**
* File streams
*/
int temporary_var;
std::ofstream f_;
+
};
+
/**
* Prepares for file generation by opening up the necessary file output
* streams.
@@ -165,7 +171,7 @@
temporary_var = 0;
// Make output file
- string f_name = get_out_dir() + "/" + program_name_ + ".st";
+ string f_name = get_out_dir()+"/"+program_name_+".st";
f_.open(f_name.c_str());
// Print header
@@ -174,7 +180,7 @@
st_class_def(f_, program_name_);
generate_class_side_definition();
- // Generate enums
+ //Generate enums
vector<t_enum*> enums = program_->get_enums();
vector<t_enum*>::iterator en_iter;
for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
@@ -205,16 +211,21 @@
* Autogen'd comment
*/
string t_st_generator::st_autogen_comment() {
- return std::string("'") + "Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + "\n"
- + "DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "'!\n";
+ return
+ std::string("'") +
+ "Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" +
+ "\n" +
+ "DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
+ "'!\n";
}
void t_st_generator::generate_force_consts() {
- f_ << prefix(class_name()) << " enums keysAndValuesDo: [:k :v | " << prefix(class_name())
- << " enums at: k put: v value].!" << endl;
+ f_ << prefix(class_name()) << " enums keysAndValuesDo: [:k :v | " <<
+ prefix(class_name()) << " enums at: k put: v value].!" << endl;
- f_ << prefix(class_name()) << " constants keysAndValuesDo: [:k :v | " << prefix(class_name())
- << " constants at: k put: v value].!" << endl;
+ f_ << prefix(class_name()) << " constants keysAndValuesDo: [:k :v | " <<
+ prefix(class_name()) << " constants at: k put: v value].!" << endl;
+
}
void t_st_generator::close_generator() {
@@ -240,76 +251,72 @@
* @param ttypedef The type definition
*/
void t_st_generator::generate_typedef(t_typedef* ttypedef) {
- (void)ttypedef;
+ (void) ttypedef;
}
-void t_st_generator::st_class_def(std::ofstream& out, string name) {
+void t_st_generator::st_class_def(std::ofstream &out, string name) {
out << "Object subclass: #" << prefix(name) << endl;
indent_up();
- out << indent() << "instanceVariableNames: ''" << endl << indent() << "classVariableNames: ''"
- << endl << indent() << "poolDictionaries: ''" << endl << indent() << "category: '"
- << generated_category() << "'!" << endl << endl;
+ out << indent() << "instanceVariableNames: ''" << endl <<
+ indent() << "classVariableNames: ''" << endl <<
+ indent() << "poolDictionaries: ''" << endl <<
+ indent() << "category: '" << generated_category() << "'!" << endl << endl;
}
-void t_st_generator::st_method(std::ofstream& out, string cls, string name) {
+void t_st_generator::st_method(std::ofstream &out, string cls, string name) {
st_method(out, cls, name, "as yet uncategorized");
}
-void t_st_generator::st_class_method(std::ofstream& out, string cls, string name) {
+void t_st_generator::st_class_method(std::ofstream &out, string cls, string name) {
st_method(out, cls + " class", name);
}
-void t_st_generator::st_class_method(std::ofstream& out, string cls, string name, string category) {
+void t_st_generator::st_class_method(std::ofstream &out, string cls, string name, string category) {
st_method(out, cls, name, category);
}
-void t_st_generator::st_method(std::ofstream& out, string cls, string name, string category) {
+void t_st_generator::st_method(std::ofstream &out, string cls, string name, string category) {
char timestr[50];
time_t rawtime;
- struct tm* tinfo;
+ struct tm *tinfo;
time(&rawtime);
tinfo = localtime(&rawtime);
strftime(timestr, 50, "%m/%d/%Y %H:%M", tinfo);
- out << "!" << prefix(cls) << " methodsFor: '" + category + "' stamp: 'thrift " << timestr
- << "'!\n" << name << endl;
+ out << "!" << prefix(cls) <<
+ " methodsFor: '"+category+"' stamp: 'thrift " << timestr << "'!\n" <<
+ name << endl;
indent_up();
out << indent();
}
-void t_st_generator::st_close_method(std::ofstream& out) {
+void t_st_generator::st_close_method(std::ofstream &out) {
out << "! !" << endl << endl;
indent_down();
}
-void t_st_generator::st_setter(std::ofstream& out,
- string cls,
- string name,
- string type = "anObject") {
+void t_st_generator::st_setter(std::ofstream &out, string cls, string name, string type = "anObject") {
st_method(out, cls, name + ": " + type);
out << name << " := " + type;
st_close_method(out);
}
-void t_st_generator::st_getter(std::ofstream& out, string cls, string name) {
+void t_st_generator::st_getter(std::ofstream &out, string cls, string name) {
st_method(out, cls, name + "");
out << "^ " << name;
st_close_method(out);
}
-void t_st_generator::st_accessors(std::ofstream& out,
- string cls,
- string name,
- string type = "anObject") {
+void t_st_generator::st_accessors(std::ofstream &out, string cls, string name, string type = "anObject") {
st_setter(out, cls, name, type);
st_getter(out, cls, name);
}
void t_st_generator::generate_class_side_definition() {
- f_ << prefix(class_name()) << " class" << endl << "\tinstanceVariableNames: 'constants enums'!"
- << endl << endl;
+ f_ << prefix(class_name()) << " class" << endl <<
+ "\tinstanceVariableNames: 'constants enums'!" << endl << endl;
st_accessors(f_, class_name() + " class", "enums");
st_accessors(f_, class_name() + " class", "constants");
@@ -329,8 +336,8 @@
void t_st_generator::generate_enum(t_enum* tenum) {
string cls_name = program_name_ + capitalize(tenum->get_name());
- f_ << prefix(class_name()) << " enums at: '" << tenum->get_name() << "' put: ["
- << "(Dictionary new " << endl;
+ f_ << prefix(class_name()) << " enums at: '" << tenum->get_name() << "' put: [" <<
+ "(Dictionary new " << endl;
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
@@ -350,8 +357,8 @@
string name = tconst->get_name();
t_const_value* value = tconst->get_value();
- f_ << prefix(class_name()) << " constants at: '" << name << "' put: ["
- << render_const_value(type, value) << "]!" << endl << endl;
+ f_ << prefix(class_name()) << " constants at: '" << name << "' put: [" <<
+ render_const_value(type, value) << "]!" << endl << endl;
}
/**
@@ -409,8 +416,8 @@
throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
}
- out << indent() << v_iter->first->get_string() << ": "
- << render_const_value(field_type, v_iter->second) << ";" << endl;
+ out << indent() << v_iter->first->get_string() << ": " <<
+ render_const_value(field_type, v_iter->second) << ";" << endl;
}
out << indent() << "yourself)";
@@ -483,9 +490,7 @@
/**
* Generates a smalltalk class to represent a struct
*/
-void t_st_generator::generate_st_struct(std::ofstream& out,
- t_struct* tstruct,
- bool is_exception = false) {
+void t_st_generator::generate_st_struct(std::ofstream& out, t_struct* tstruct, bool is_exception = false) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -494,31 +499,27 @@
else
out << "Object";
- out << " subclass: #" << prefix(type_name(tstruct)) << endl << "\tinstanceVariableNames: '";
+ out << " subclass: #" << prefix(type_name(tstruct)) << endl <<
+ "\tinstanceVariableNames: '";
if (members.size() > 0) {
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- if (m_iter != members.begin())
- out << " ";
+ if (m_iter != members.begin()) out << " ";
out << camelcase((*m_iter)->get_name());
}
}
- out << "'\n"
- << "\tclassVariableNames: ''\n"
- << "\tpoolDictionaries: ''\n"
- << "\tcategory: '" << generated_category() << "'!\n\n";
+ out << "'\n" <<
+ "\tclassVariableNames: ''\n" <<
+ "\tpoolDictionaries: ''\n" <<
+ "\tcategory: '" << generated_category() << "'!\n\n";
generate_accessors(out, tstruct);
}
bool t_st_generator::is_vowel(char c) {
- switch (tolower(c)) {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
+ switch(tolower(c)) {
+ case 'a': case 'e': case 'i': case 'o': case 'u':
return true;
}
return false;
@@ -568,30 +569,30 @@
return out.str();
}
-string t_st_generator::map_writer(t_map* tmap, string fname) {
+string t_st_generator::map_writer(t_map *tmap, string fname) {
std::ostringstream out;
string key = temp_name();
string val = temp_name();
- out << "[oprot writeMapBegin: (TMap new keyType: " << type_to_enum(tmap->get_key_type())
- << "; valueType: " << type_to_enum(tmap->get_val_type()) << "; size: " << fname << " size)."
- << endl;
+ out << "[oprot writeMapBegin: (TMap new keyType: " << type_to_enum(tmap->get_key_type()) <<
+ "; valueType: " << type_to_enum(tmap->get_val_type()) << "; size: " << fname << " size)." << endl;
indent_up();
out << indent() << fname << " keysAndValuesDo: [:" << key << " :" << val << " |" << endl;
indent_up();
- out << indent() << write_val(tmap->get_key_type(), key) << "." << endl << indent()
- << write_val(tmap->get_val_type(), val);
+ out << indent() << write_val(tmap->get_key_type(), key) << "." << endl <<
+ indent() << write_val(tmap->get_val_type(), val);
indent_down();
- out << "]." << endl << indent() << "oprot writeMapEnd] value";
+ out << "]." << endl <<
+ indent() << "oprot writeMapEnd] value";
indent_down();
return out.str();
}
-string t_st_generator::map_reader(t_map* tmap) {
+string t_st_generator::map_reader(t_map *tmap) {
std::ostringstream out;
string desc = temp_name();
string val = temp_name();
@@ -599,26 +600,29 @@
out << "[|" << desc << " " << val << "| " << endl;
indent_up();
- out << indent() << desc << " := iprot readMapBegin." << endl << indent() << val
- << " := Dictionary new." << endl << indent() << desc << " size timesRepeat: [" << endl;
+ out << indent() << desc << " := iprot readMapBegin." << endl <<
+ indent() << val << " := Dictionary new." << endl <<
+ indent() << desc << " size timesRepeat: [" << endl;
indent_up();
- out << indent() << val << " at: " << read_val(tmap->get_key_type())
- << " put: " << read_val(tmap->get_val_type());
+ out << indent() << val << " at: " << read_val(tmap->get_key_type()) <<
+ " put: " << read_val(tmap->get_val_type());
indent_down();
- out << "]." << endl << indent() << "iprot readMapEnd." << endl << indent() << val << "] value";
+ out << "]." << endl <<
+ indent() << "iprot readMapEnd." << endl <<
+ indent() << val << "] value";
indent_down();
return out.str();
}
-string t_st_generator::list_writer(t_list* tlist, string fname) {
+string t_st_generator::list_writer(t_list *tlist, string fname) {
std::ostringstream out;
string val = temp_name();
- out << "[oprot writeListBegin: (TList new elemType: " << type_to_enum(tlist->get_elem_type())
- << "; size: " << fname << " size)." << endl;
+ out << "[oprot writeListBegin: (TList new elemType: " <<
+ type_to_enum(tlist->get_elem_type()) << "; size: " << fname << " size)." << endl;
indent_up();
out << indent() << fname << " do: [:" << val << "|" << endl;
@@ -627,13 +631,14 @@
out << indent() << write_val(tlist->get_elem_type(), val) << endl;
indent_down();
- out << "]." << endl << indent() << "oprot writeListEnd] value";
+ out << "]." << endl <<
+ indent() << "oprot writeListEnd] value";
indent_down();
return out.str();
}
-string t_st_generator::list_reader(t_list* tlist) {
+string t_st_generator::list_reader(t_list *tlist) {
std::ostringstream out;
string desc = temp_name();
string val = temp_name();
@@ -641,25 +646,27 @@
out << "[|" << desc << " " << val << "| " << desc << " := iprot readListBegin." << endl;
indent_up();
- out << indent() << val << " := OrderedCollection new." << endl << indent() << desc
- << " size timesRepeat: [" << endl;
+ out << indent() << val << " := OrderedCollection new." << endl <<
+ indent() << desc << " size timesRepeat: [" << endl;
indent_up();
out << indent() << val << " add: " << read_val(tlist->get_elem_type());
indent_down();
- out << "]." << endl << indent() << "iprot readListEnd." << endl << indent() << val << "] value";
+ out << "]." << endl <<
+ indent() << "iprot readListEnd." << endl <<
+ indent() << val << "] value";
indent_down();
return out.str();
}
-string t_st_generator::set_writer(t_set* tset, string fname) {
+string t_st_generator::set_writer(t_set *tset, string fname) {
std::ostringstream out;
string val = temp_name();
- out << "[oprot writeSetBegin: (TSet new elemType: " << type_to_enum(tset->get_elem_type())
- << "; size: " << fname << " size)." << endl;
+ out << "[oprot writeSetBegin: (TSet new elemType: " << type_to_enum(tset->get_elem_type()) <<
+ "; size: " << fname << " size)." << endl;
indent_up();
out << indent() << fname << " do: [:" << val << "|" << endl;
@@ -668,13 +675,14 @@
out << indent() << write_val(tset->get_elem_type(), val) << endl;
indent_down();
- out << "]." << endl << indent() << "oprot writeSetEnd] value";
+ out << "]." << endl <<
+ indent() << "oprot writeSetEnd] value";
indent_down();
return out.str();
}
-string t_st_generator::set_reader(t_set* tset) {
+string t_st_generator::set_reader(t_set *tset) {
std::ostringstream out;
string desc = temp_name();
string val = temp_name();
@@ -682,26 +690,28 @@
out << "[|" << desc << " " << val << "| " << desc << " := iprot readSetBegin." << endl;
indent_up();
- out << indent() << val << " := Set new." << endl << indent() << desc << " size timesRepeat: ["
- << endl;
+ out << indent() << val << " := Set new." << endl <<
+ indent() << desc << " size timesRepeat: [" << endl;
indent_up();
out << indent() << val << " add: " << read_val(tset->get_elem_type());
indent_down();
- out << "]." << endl << indent() << "iprot readSetEnd." << endl << indent() << val << "] value";
+ out << "]." << endl <<
+ indent() << "iprot readSetEnd." << endl <<
+ indent() << val << "] value";
indent_down();
return out.str();
}
-string t_st_generator::struct_writer(t_struct* tstruct, string sname) {
+string t_st_generator::struct_writer(t_struct *tstruct, string sname) {
std::ostringstream out;
const vector<t_field*>& fields = tstruct->get_sorted_members();
vector<t_field*>::const_iterator fld_iter;
- out << "[oprot writeStructBegin: "
- << "(TStruct new name: '" + tstruct->get_name() + "')." << endl;
+ out << "[oprot writeStructBegin: " <<
+ "(TStruct new name: '" + tstruct->get_name() +"')." << endl;
indent_up();
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
@@ -714,12 +724,12 @@
indent_up();
}
- out << indent() << "oprot writeFieldBegin: (TField new name: '" << fname
- << "'; type: " << type_to_enum((*fld_iter)->get_type())
- << "; id: " << (*fld_iter)->get_key() << ")." << endl;
+ out << indent() << "oprot writeFieldBegin: (TField new name: '" << fname <<
+ "'; type: " << type_to_enum((*fld_iter)->get_type()) <<
+ "; id: " << (*fld_iter)->get_key() << ")." << endl;
- out << indent() << write_val((*fld_iter)->get_type(), accessor) << "." << endl << indent()
- << "oprot writeFieldEnd";
+ out << indent() << write_val((*fld_iter)->get_type(), accessor) << "." << endl <<
+ indent() << "oprot writeFieldEnd";
if (optional) {
out << "]";
@@ -735,7 +745,7 @@
return out.str();
}
-string t_st_generator::struct_reader(t_struct* tstruct, string clsName = "") {
+string t_st_generator::struct_reader(t_struct *tstruct, string clsName = "") {
std::ostringstream out;
const vector<t_field*>& fields = tstruct->get_members();
vector<t_field*>::const_iterator fld_iter;
@@ -750,21 +760,23 @@
out << "[|" << desc << " " << val << "|" << endl;
indent_up();
- // This is nasty, but without it we'll break things by prefixing TResult.
+ //This is nasty, but without it we'll break things by prefixing TResult.
string name = ((capitalize(clsName) == "TResult") ? capitalize(clsName) : prefix(clsName));
out << indent() << val << " := " << name << " new." << endl;
- out << indent() << "iprot readStructBegin." << endl << indent() << "[" << desc
- << " := iprot readFieldBegin." << endl << indent() << desc
- << " type = TType stop] whileFalse: [|" << found << "|" << endl;
+ out << indent() << "iprot readStructBegin." << endl <<
+ indent() << "[" << desc << " := iprot readFieldBegin." << endl <<
+ indent() << desc << " type = TType stop] whileFalse: [|" << found << "|" << endl;
indent_up();
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
- out << indent() << desc << " id = " << (*fld_iter)->get_key() << " ifTrue: [" << endl;
+ out << indent() << desc << " id = " << (*fld_iter)->get_key() <<
+ " ifTrue: [" << endl;
indent_up();
- out << indent() << found << " := true." << endl << indent() << val << " "
- << camelcase((*fld_iter)->get_name()) << ": " << read_val((*fld_iter)->get_type());
+ out << indent() << found << " := true." << endl <<
+ indent() << val << " " << camelcase((*fld_iter)->get_name()) << ": " <<
+ read_val((*fld_iter)->get_type());
indent_down();
out << "]." << endl;
@@ -773,18 +785,19 @@
out << indent() << found << " ifNil: [iprot skip: " << desc << " type]]." << endl;
indent_down();
- out << indent() << "oprot readStructEnd." << endl << indent() << val << "] value";
+ out << indent() << "oprot readStructEnd." << endl <<
+ indent() << val << "] value";
indent_down();
return out.str();
}
-string t_st_generator::write_val(t_type* t, string fname) {
+string t_st_generator::write_val(t_type *t, string fname) {
t = get_true_type(t);
if (t->is_base_type()) {
- t_base_type::t_base tbase = ((t_base_type*)t)->get_base();
- switch (tbase) {
+ t_base_type::t_base tbase = ((t_base_type*) t)->get_base();
+ switch(tbase) {
case t_base_type::TYPE_DOUBLE:
return "iprot writeDouble: " + fname + " asFloat";
break;
@@ -797,13 +810,13 @@
return "iprot write" + capitalize(type_name(t)) + ": " + fname;
}
} else if (t->is_map()) {
- return map_writer((t_map*)t, fname);
+ return map_writer((t_map*) t, fname);
} else if (t->is_struct() || t->is_xception()) {
- return struct_writer((t_struct*)t, fname);
+ return struct_writer((t_struct*) t, fname);
} else if (t->is_list()) {
- return list_writer((t_list*)t, fname);
+ return list_writer((t_list*) t, fname);
} else if (t->is_set()) {
- return set_writer((t_set*)t, fname);
+ return set_writer((t_set*) t, fname);
} else if (t->is_enum()) {
return "iprot writeI32: " + fname;
} else {
@@ -811,19 +824,19 @@
}
}
-string t_st_generator::read_val(t_type* t) {
+string t_st_generator::read_val(t_type *t) {
t = get_true_type(t);
if (t->is_base_type()) {
return "iprot read" + capitalize(type_name(t));
} else if (t->is_map()) {
- return map_reader((t_map*)t);
+ return map_reader((t_map*) t);
} else if (t->is_struct() || t->is_xception()) {
- return struct_reader((t_struct*)t);
+ return struct_reader((t_struct*) t);
} else if (t->is_list()) {
- return list_reader((t_list*)t);
+ return list_reader((t_list*) t);
} else if (t->is_set()) {
- return set_reader((t_set*)t);
+ return set_reader((t_set*) t);
} else if (t->is_enum()) {
return "iprot readI32";
} else {
@@ -845,23 +858,23 @@
f_ << indent() << "(TCallMessage new" << endl;
indent_up();
- f_ << indent() << "name: '" << funname << "'; " << endl << indent() << "seqid: self nextSeqid)."
- << endl;
+ f_ << indent() << "name: '" << funname << "'; " << endl <<
+ indent() << "seqid: self nextSeqid)." << endl;
indent_down();
indent_down();
- f_ << indent() << "oprot writeStructBegin: "
- << "(TStruct new name: '" + capitalize(camelcase(funname)) + "_args')." << endl;
+ f_ << indent() << "oprot writeStructBegin: " <<
+ "(TStruct new name: '" + capitalize(camelcase(funname)) + "_args')." << endl;
for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
string fname = camelcase((*fld_iter)->get_name());
- f_ << indent() << "oprot writeFieldBegin: (TField new name: '" << fname
- << "'; type: " << type_to_enum((*fld_iter)->get_type()) << "; id: " << (*fld_iter)->get_key()
- << ")." << endl;
+ f_ << indent() << "oprot writeFieldBegin: (TField new name: '" << fname <<
+ "'; type: " << type_to_enum((*fld_iter)->get_type()) <<
+ "; id: " << (*fld_iter)->get_key() << ")." << endl;
- f_ << indent() << write_val((*fld_iter)->get_type(), fname) << "." << endl << indent()
- << "oprot writeFieldEnd." << endl;
+ f_ << indent() << write_val((*fld_iter)->get_type(), fname) << "." << endl <<
+ indent() << "oprot writeFieldEnd." << endl;
}
f_ << indent() << "oprot writeFieldStop; writeStructEnd; writeMessageEnd." << endl;
@@ -884,16 +897,19 @@
vector<t_field*>::const_iterator f_iter;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
// duplicate the field, but call it "exception"... we don't need a dynamic name
- t_field* exception = new t_field((*f_iter)->get_type(), "exception", (*f_iter)->get_key());
+ t_field *exception = new t_field((*f_iter)->get_type(), "exception", (*f_iter)->get_key());
result.append(exception);
}
st_method(f_, client_class_name(), "recv" + capitalize(funname));
- f_ << "| f msg res | " << endl << indent() << "msg := oprot readMessageBegin." << endl << indent()
- << "self validateRemoteMessage: msg." << endl << indent()
- << "res := " << struct_reader(&result) << "." << endl << indent() << "oprot readMessageEnd."
- << endl << indent() << "oprot transport flush." << endl << indent()
- << "res exception ifNotNil: [res exception signal]." << endl << indent() << "^ res";
+ f_ << "| f msg res | " << endl <<
+ indent() << "msg := oprot readMessageBegin." << endl <<
+ indent() << "self validateRemoteMessage: msg." << endl <<
+ indent() << "res := " << struct_reader(&result) << "." << endl <<
+ indent() << "oprot readMessageEnd." << endl <<
+ indent() << "oprot transport flush." << endl <<
+ indent() << "res exception ifNotNil: [res exception signal]." << endl <<
+ indent() << "^ res";
st_close_method(f_);
}
@@ -932,19 +948,19 @@
extends_client = extends + "Client";
}
- f_ << extends_client << " subclass: #" << prefix(client_class_name()) << endl
- << "\tinstanceVariableNames: ''\n"
- << "\tclassVariableNames: ''\n"
- << "\tpoolDictionaries: ''\n"
- << "\tcategory: '" << generated_category() << "'!\n\n";
+ f_ << extends_client << " subclass: #" << prefix(client_class_name()) << endl <<
+ "\tinstanceVariableNames: ''\n" <<
+ "\tclassVariableNames: ''\n" <<
+ "\tpoolDictionaries: ''\n" <<
+ "\tcategory: '" << generated_category() << "'!\n\n";
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string funname = camelcase((*f_iter)->get_name());
string signature = function_signature(*f_iter);
st_method(f_, client_class_name(), signature);
- f_ << function_types_comment(*f_iter) << endl << indent() << "self send"
- << capitalize(signature) << "." << endl;
+ f_ << function_types_comment(*f_iter) << endl <<
+ indent() << "self send" << capitalize(signature) << "." << endl;
if (!(*f_iter)->is_oneway()) {
f_ << indent() << "^ self recv" << capitalize(funname) << " success " << endl;
@@ -1046,4 +1062,6 @@
throw "INVALID TYPE IN type_to_enum: " + type->get_name();
}
+
THRIFT_REGISTER_GENERATOR(st, "Smalltalk", "")
+
diff --git a/compiler/cpp/src/generate/t_xsd_generator.cc b/compiler/cpp/src/generate/t_xsd_generator.cc
index ed76bec..7135f6a 100644
--- a/compiler/cpp/src/generate/t_xsd_generator.cc
+++ b/compiler/cpp/src/generate/t_xsd_generator.cc
@@ -35,20 +35,22 @@
using std::stringstream;
using std::vector;
-static const string endl = "\n"; // avoid ostream << std::endl flushes
+static const string endl = "\n"; // avoid ostream << std::endl flushes
/**
* XSD generator, creates an XSD for the base types etc.
*
*/
class t_xsd_generator : public t_generator {
-public:
- t_xsd_generator(t_program* program,
- const std::map<std::string, std::string>& parsed_options,
- const std::string& option_string)
- : t_generator(program) {
- (void)parsed_options;
- (void)option_string;
+ public:
+ t_xsd_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_generator(program)
+ {
+ (void) parsed_options;
+ (void) option_string;
out_dir_base_ = "gen-xsd";
}
@@ -66,23 +68,24 @@
*/
void generate_typedef(t_typedef* ttypedef);
- void generate_enum(t_enum* tenum) { (void)tenum; }
+ void generate_enum(t_enum* tenum) {
+ (void) tenum;
+ }
void generate_service(t_service* tservice);
void generate_struct(t_struct* tstruct);
-private:
- void generate_element(std::ostream& out,
- std::string name,
- t_type* ttype,
- t_struct* attrs = NULL,
- bool optional = false,
- bool nillable = false,
- bool list_element = false);
+ private:
- std::string ns(std::string in, std::string ns) { return ns + ":" + in; }
+ void generate_element(std::ostream& out, std::string name, t_type* ttype, t_struct* attrs=NULL, bool optional=false, bool nillable=false, bool list_element=false);
- std::string xsd(std::string in) { return ns(in, "xsd"); }
+ std::string ns(std::string in, std::string ns) {
+ return ns + ":" + in;
+ }
+
+ std::string xsd(std::string in) {
+ return ns(in, "xsd");
+ }
std::string type_name(t_type* ttype);
std::string base_type_name(t_base_type::t_base tbase);
@@ -97,17 +100,21 @@
* Output string stream
*/
std::ostringstream s_xsd_types_;
+
};
+
void t_xsd_generator::init_generator() {
// Make output directory
MKDIR(get_out_dir().c_str());
// Make output file
- string f_php_name = get_out_dir() + program_->get_name() + "_xsd.php";
+ string f_php_name = get_out_dir()+program_->get_name()+"_xsd.php";
f_php_.open(f_php_name.c_str());
- f_php_ << "<?php" << endl;
+ f_php_ <<
+ "<?php" << endl;
+
}
void t_xsd_generator::close_generator() {
@@ -116,25 +123,29 @@
}
void t_xsd_generator::generate_typedef(t_typedef* ttypedef) {
- indent(s_xsd_types_) << "<xsd:simpleType name=\"" << ttypedef->get_name() << "\">" << endl;
+ indent(s_xsd_types_) <<
+ "<xsd:simpleType name=\"" << ttypedef->get_name() << "\">" << endl;
indent_up();
if (ttypedef->get_type()->is_string() && ((t_base_type*)ttypedef->get_type())->is_string_enum()) {
- indent(s_xsd_types_) << "<xsd:restriction base=\"" << type_name(ttypedef->get_type()) << "\">"
- << endl;
+ indent(s_xsd_types_) <<
+ "<xsd:restriction base=\"" << type_name(ttypedef->get_type()) << "\">" << endl;
indent_up();
const vector<string>& values = ((t_base_type*)ttypedef->get_type())->get_string_enum_vals();
vector<string>::const_iterator v_iter;
for (v_iter = values.begin(); v_iter != values.end(); ++v_iter) {
- indent(s_xsd_types_) << "<xsd:enumeration value=\"" << (*v_iter) << "\" />" << endl;
+ indent(s_xsd_types_) <<
+ "<xsd:enumeration value=\"" << (*v_iter) << "\" />" << endl;
}
indent_down();
- indent(s_xsd_types_) << "</xsd:restriction>" << endl;
+ indent(s_xsd_types_) <<
+ "</xsd:restriction>" << endl;
} else {
- indent(s_xsd_types_) << "<xsd:restriction base=\"" << type_name(ttypedef->get_type()) << "\" />"
- << endl;
+ indent(s_xsd_types_) <<
+ "<xsd:restriction base=\"" << type_name(ttypedef->get_type()) << "\" />" << endl;
}
indent_down();
- indent(s_xsd_types_) << "</xsd:simpleType>" << endl << endl;
+ indent(s_xsd_types_) <<
+ "</xsd:simpleType>" << endl << endl;
}
void t_xsd_generator::generate_struct(t_struct* tstruct) {
@@ -152,12 +163,7 @@
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
- generate_element(s_xsd_types_,
- (*m_iter)->get_name(),
- (*m_iter)->get_type(),
- (*m_iter)->get_xsd_attrs(),
- (*m_iter)->get_xsd_optional() || xsd_all,
- (*m_iter)->get_xsd_nillable());
+ generate_element(s_xsd_types_, (*m_iter)->get_name(), (*m_iter)->get_type(), (*m_iter)->get_xsd_attrs(), (*m_iter)->get_xsd_optional() || xsd_all, (*m_iter)->get_xsd_nillable());
}
indent_down();
@@ -167,7 +173,9 @@
indent(s_xsd_types_) << "</xsd:sequence>" << endl;
}
indent_down();
- indent(s_xsd_types_) << "</xsd:complexType>" << endl << endl;
+ indent(s_xsd_types_) <<
+ "</xsd:complexType>" << endl <<
+ endl;
}
void t_xsd_generator::generate_element(ostream& out,
@@ -183,12 +191,15 @@
string snillable = nillable ? " nillable=\"true\"" : "";
if (ttype->is_void() || ttype->is_list()) {
- indent(out) << "<xsd:element name=\"" << name << "\"" << soptional << snillable << ">" << endl;
+ indent(out) <<
+ "<xsd:element name=\"" << name << "\"" << soptional << snillable << ">" << endl;
indent_up();
if (attrs == NULL && ttype->is_void()) {
- indent(out) << "<xsd:complexType />" << endl;
+ indent(out) <<
+ "<xsd:complexType />" << endl;
} else {
- indent(out) << "<xsd:complexType>" << endl;
+ indent(out) <<
+ "<xsd:complexType>" << endl;
indent_up();
if (ttype->is_list()) {
indent(out) << "<xsd:sequence minOccurs=\"0\" maxOccurs=\"unbounded\">" << endl;
@@ -200,8 +211,7 @@
} else {
subname = type_name(subtype);
}
- f_php_ << "$GLOBALS['" << program_->get_name() << "_xsd_elt_" << name << "'] = '" << subname
- << "';" << endl;
+ f_php_ << "$GLOBALS['" << program_->get_name() << "_xsd_elt_" << name << "'] = '" << subname << "';" << endl;
generate_element(out, subname, subtype, NULL, false, false, true);
indent_down();
indent(out) << "</xsd:sequence>" << endl;
@@ -211,24 +221,23 @@
const vector<t_field*>& members = attrs->get_members();
vector<t_field*>::const_iterator a_iter;
for (a_iter = members.begin(); a_iter != members.end(); ++a_iter) {
- indent(out) << "<xsd:attribute name=\"" << (*a_iter)->get_name() << "\" type=\""
- << type_name((*a_iter)->get_type()) << "\" />" << endl;
+ indent(out) << "<xsd:attribute name=\"" << (*a_iter)->get_name() << "\" type=\"" << type_name((*a_iter)->get_type()) << "\" />" << endl;
}
}
indent_down();
- indent(out) << "</xsd:complexType>" << endl;
+ indent(out) <<
+ "</xsd:complexType>" << endl;
}
indent_down();
- indent(out) << "</xsd:element>" << endl;
+ indent(out) <<
+ "</xsd:element>" << endl;
} else {
if (attrs == NULL) {
- indent(out) << "<xsd:element name=\"" << name << "\""
- << " type=\"" << type_name(ttype) << "\"" << soptional << snillable << " />"
- << endl;
+ indent(out) <<
+ "<xsd:element name=\"" << name << "\"" << " type=\"" << type_name(ttype) << "\"" << soptional << snillable << " />" << endl;
} else {
// Wow, all this work for a SIMPLE TYPE with attributes?!?!?!
- indent(out) << "<xsd:element name=\"" << name << "\"" << soptional << snillable << ">"
- << endl;
+ indent(out) << "<xsd:element name=\"" << name << "\"" << soptional << snillable << ">" << endl;
indent_up();
indent(out) << "<xsd:complexType>" << endl;
indent_up();
@@ -239,8 +248,7 @@
const vector<t_field*>& members = attrs->get_members();
vector<t_field*>::const_iterator a_iter;
for (a_iter = members.begin(); a_iter != members.end(); ++a_iter) {
- indent(out) << "<xsd:attribute name=\"" << (*a_iter)->get_name() << "\" type=\""
- << type_name((*a_iter)->get_type()) << "\" />" << endl;
+ indent(out) << "<xsd:attribute name=\"" << (*a_iter)->get_name() << "\" type=\"" << type_name((*a_iter)->get_type()) << "\" />" << endl;
}
indent_down();
indent(out) << "</xsd:extension>" << endl;
@@ -256,19 +264,22 @@
void t_xsd_generator::generate_service(t_service* tservice) {
// Make output file
- string f_xsd_name = get_out_dir() + tservice->get_name() + ".xsd";
+ string f_xsd_name = get_out_dir()+tservice->get_name()+".xsd";
f_xsd_.open(f_xsd_name.c_str());
string ns = program_->get_namespace("xsd");
if (ns.size() > 0) {
- ns = " targetNamespace=\"" + ns + "\" xmlns=\"" + ns + "\" "
- + "elementFormDefault=\"qualified\"";
+ ns = " targetNamespace=\"" + ns + "\" xmlns=\"" + ns + "\" " +
+ "elementFormDefault=\"qualified\"";
}
// Print the XSD header
- f_xsd_ << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << endl
- << "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" << ns << ">" << endl
- << endl << "<!-- Yo yo yo, this XSD woz be generated by Thrift. -->" << endl << endl;
+ f_xsd_ <<
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << endl <<
+ "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" << ns << ">" << endl <<
+ endl <<
+ "<!-- Yo yo yo, this XSD woz be generated by Thrift. -->" << endl <<
+ endl;
// Print out the type definitions
indent(f_xsd_) << s_xsd_types_.str();
@@ -353,3 +364,4 @@
}
THRIFT_REGISTER_GENERATOR(xsd, "XSD", "")
+
diff --git a/compiler/cpp/src/globals.h b/compiler/cpp/src/globals.h
index 93d11aa..cf924f1 100644
--- a/compiler/cpp/src/globals.h
+++ b/compiler/cpp/src/globals.h
@@ -48,7 +48,10 @@
* Parsing mode, two passes up in this gin rummy!
*/
-enum PARSE_MODE { INCLUDES = 1, PROGRAM = 2 };
+enum PARSE_MODE {
+ INCLUDES = 1,
+ PROGRAM = 2
+};
/**
* Strictness level
@@ -116,18 +119,19 @@
*/
enum PROGDOCTEXT_STATUS {
INVALID = 0,
- STILL_CANDIDATE = 1, // the text may or may not be the program doctext
- ALREADY_PROCESSED = 2, // doctext has been used and is no longer available
- ABSOLUTELY_SURE = 3, // this is the program doctext
- NO_PROGRAM_DOCTEXT = 4 // there is no program doctext
+ STILL_CANDIDATE = 1, // the text may or may not be the program doctext
+ ALREADY_PROCESSED = 2, // doctext has been used and is no longer available
+ ABSOLUTELY_SURE = 3, // this is the program doctext
+ NO_PROGRAM_DOCTEXT = 4 // there is no program doctext
};
+
/**
* The program level doctext. Stored seperately to make parsing easier.
*/
extern char* g_program_doctext_candidate;
-extern int g_program_doctext_lineno;
-extern PROGDOCTEXT_STATUS g_program_doctext_status;
+extern int g_program_doctext_lineno;
+extern PROGDOCTEXT_STATUS g_program_doctext_status;
/**
* Whether or not negative field keys are accepted.
diff --git a/compiler/cpp/src/logging.h b/compiler/cpp/src/logging.h
index 3f1fce8..097469d 100644
--- a/compiler/cpp/src/logging.h
+++ b/compiler/cpp/src/logging.h
@@ -42,4 +42,5 @@
*/
void failure(const char* fmt, ...);
+
#endif
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
old mode 100644
new mode 100755
index 57cd460..ed57596
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -40,7 +40,7 @@
#include <limits.h>
#ifdef _WIN32
-#include <windows.h> /* for GetFullPathName */
+# include <windows.h> /* for GetFullPathName */
#endif
// Careful: must include globals first for extern definitions
@@ -150,8 +150,8 @@
* The First doctext comment
*/
char* g_program_doctext_candidate;
-int g_program_doctext_lineno = 0;
-PROGDOCTEXT_STATUS g_program_doctext_status = INVALID;
+int g_program_doctext_lineno = 0;
+PROGDOCTEXT_STATUS g_program_doctext_status = INVALID;
/**
* Whether or not negative field keys are accepted.
@@ -172,12 +172,12 @@
* Win32 doesn't have realpath, so use fallback implementation in that case,
* otherwise this just calls through to realpath
*/
-char* saferealpath(const char* path, char* resolved_path) {
+char *saferealpath(const char *path, char *resolved_path) {
#ifdef _WIN32
char buf[MAX_PATH];
char* basename;
DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
- if (len == 0 || len > MAX_PATH - 1) {
+ if (len == 0 || len > MAX_PATH - 1){
strcpy(resolved_path, path);
} else {
strcpy(resolved_path, buf);
@@ -197,17 +197,14 @@
#endif
}
-bool check_is_directory(const char* dir_name) {
+bool check_is_directory(const char *dir_name) {
#ifdef _WIN32
DWORD attributes = ::GetFileAttributesA(dir_name);
- if (attributes == INVALID_FILE_ATTRIBUTES) {
- fprintf(stderr,
- "Output directory %s is unusable: GetLastError() = %ld\n",
- dir_name,
- GetLastError());
+ if(attributes == INVALID_FILE_ATTRIBUTES) {
+ fprintf(stderr, "Output directory %s is unusable: GetLastError() = %ld\n", dir_name, GetLastError());
return false;
}
- if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
+ if((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
return false;
}
@@ -218,7 +215,7 @@
fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
return false;
}
- if (!S_ISDIR(sb.st_mode)) {
+ if (! S_ISDIR(sb.st_mode)) {
fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
return false;
}
@@ -236,7 +233,11 @@
*/
void yyerror(const char* fmt, ...) {
va_list args;
- fprintf(stderr, "[ERROR:%s:%d] (last token was '%s')\n", g_curpath.c_str(), yylineno, yytext);
+ fprintf(stderr,
+ "[ERROR:%s:%d] (last token was '%s')\n",
+ g_curpath.c_str(),
+ yylineno,
+ yytext);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
@@ -315,7 +316,7 @@
string program_name(string filename) {
string::size_type slash = filename.rfind("/");
if (slash != string::npos) {
- filename = filename.substr(slash + 1);
+ filename = filename.substr(slash+1);
}
string::size_type dot = filename.rfind(".");
if (dot != string::npos) {
@@ -399,25 +400,25 @@
* Reset program doctext information after processing a file
*/
void reset_program_doctext_info() {
- if (g_program_doctext_candidate != NULL) {
+ if(g_program_doctext_candidate != NULL) {
free(g_program_doctext_candidate);
g_program_doctext_candidate = NULL;
}
g_program_doctext_lineno = 0;
g_program_doctext_status = INVALID;
- pdebug("%s", "program doctext set to INVALID");
+ pdebug("%s","program doctext set to INVALID");
}
/**
* We are sure the program doctext candidate is really the program doctext.
*/
void declare_valid_program_doctext() {
- if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
+ if((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
g_program_doctext_status = ABSOLUTELY_SURE;
- pdebug("%s", "program doctext set to ABSOLUTELY_SURE");
+ pdebug("%s","program doctext set to ABSOLUTELY_SURE");
} else {
g_program_doctext_status = NO_PROGRAM_DOCTEXT;
- pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT");
+ pdebug("%s","program doctext set to NO_PROGRAM_DOCTEXT");
}
}
@@ -430,14 +431,16 @@
char* clean_up_doctext(char* doctext) {
// Convert to C++ string, and remove Windows's carriage returns.
string docstring = doctext;
- docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end());
+ docstring.erase(
+ remove(docstring.begin(), docstring.end(), '\r'),
+ docstring.end());
// Separate into lines.
vector<string> lines;
string::size_type pos = string::npos;
string::size_type last;
while (true) {
- last = (pos == string::npos) ? 0 : pos + 1;
+ last = (pos == string::npos) ? 0 : pos+1;
pos = docstring.find('\n', last);
if (pos == string::npos) {
// First bit of cleaning. If the last line is only whitespace, drop it.
@@ -447,7 +450,7 @@
}
break;
}
- lines.push_back(docstring.substr(last, pos - last));
+ lines.push_back(docstring.substr(last, pos-last));
}
// A very profound docstring.
@@ -465,7 +468,7 @@
bool found_prefix = false;
string::size_type prefix_len = 0;
vector<string>::iterator l_iter;
- for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
+ for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
if (l_iter->empty()) {
continue;
}
@@ -484,7 +487,9 @@
// Whitespace-only line. Truncate it.
l_iter->clear();
}
- } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) {
+ } else if (l_iter->size() > pos
+ && l_iter->at(pos) == '*'
+ && pos == prefix_len) {
// Business as usual.
} else if (pos == string::npos) {
// Whitespace-only line. Let's truncate it for them.
@@ -500,26 +505,27 @@
if (have_prefix) {
// Get the star too.
prefix_len++;
- for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
+ for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
l_iter->erase(0, prefix_len);
}
}
// Now delete the minimum amount of leading whitespace from each line.
prefix_len = string::npos;
- for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
+ for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
if (l_iter->empty()) {
continue;
}
pos = l_iter->find_first_not_of(" \t");
- if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) {
+ if (pos != string::npos
+ && (prefix_len == string::npos || pos < prefix_len)) {
prefix_len = pos;
}
}
// If our prefix survived, delete it from every line.
if (prefix_len != string::npos) {
- for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
+ for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) {
l_iter->erase(0, prefix_len);
}
}
@@ -527,8 +533,8 @@
// Remove trailing whitespace from every line.
for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
pos = l_iter->find_last_not_of(" \t");
- if (pos != string::npos && pos != l_iter->length() - 1) {
- l_iter->erase(pos + 1);
+ if (pos != string::npos && pos != l_iter->length()-1) {
+ l_iter->erase(pos+1);
}
}
@@ -545,11 +551,11 @@
docstring += '\n';
}
- // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
- if (docstring.length() <= strlen(doctext)) {
+ //assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
+ if(docstring.length() <= strlen(doctext)) {
strcpy(doctext, docstring.c_str());
} else {
- free(doctext); // too short
+ free(doctext); // too short
doctext = strdup(docstring.c_str());
}
return doctext;
@@ -655,18 +661,20 @@
*/
}
+
/**
* Emits a warning on list<byte>, binary type is typically a much better choice.
*/
void check_for_list_of_bytes(t_type* list_elem_type) {
- if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
+ if((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
t_base_type* tbase = (t_base_type*)list_elem_type;
- if (tbase->get_base() == t_base_type::TYPE_BYTE) {
- pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
+ if(tbase->get_base() == t_base_type::TYPE_BYTE) {
+ pwarning(1,"Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
}
}
}
+
/**
* Prints the version number
*/
@@ -693,7 +701,7 @@
fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
fprintf(stderr, " (default: current directory)\n");
fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
- fprintf(stderr, " (no gen-* folder will be created)\n");
+ fprintf(stderr," (no gen-* folder will be created)\n");
fprintf(stderr, " -I dir Add a directory to the list of directories\n");
fprintf(stderr, " searched for include directives\n");
fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
@@ -701,8 +709,7 @@
fprintf(stderr, " -v[erbose] Verbose mode\n");
fprintf(stderr, " -r[ecurse] Also generate included files\n");
fprintf(stderr, " -debug Parse debug trace to stdout\n");
- fprintf(stderr,
- " --allow-neg-keys Allow negative field keys (Used to "
+ fprintf(stderr, " --allow-neg-keys Allow negative field keys (Used to "
"preserve protocol\n");
fprintf(stderr, " compatibility with older .thrift files)\n");
fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
@@ -716,10 +723,9 @@
t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
t_generator_registry::gen_map_t::iterator iter;
for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
- fprintf(stderr,
- " %s (%s):\n",
- iter->second->get_short_name().c_str(),
- iter->second->get_long_name().c_str());
+ fprintf(stderr, " %s (%s):\n",
+ iter->second->get_short_name().c_str(),
+ iter->second->get_long_name().c_str());
fprintf(stderr, "%s", iter->second->get_documentation().c_str());
}
exit(1);
@@ -772,8 +778,8 @@
}
break;
case t_base_type::TYPE_DOUBLE:
- if (value->get_type() != t_const_value::CV_INTEGER
- && value->get_type() != t_const_value::CV_DOUBLE) {
+ if (value->get_type() != t_const_value::CV_INTEGER &&
+ value->get_type() != t_const_value::CV_DOUBLE) {
throw "type error: const \"" + name + "\" was declared as double";
}
break;
@@ -799,9 +805,9 @@
}
}
if (!found) {
- throw "type error: const " + name + " was declared as type " + type->get_name()
- + " which is an enum, but " + value->get_identifier()
- + " is not a valid value for that enum";
+ throw "type error: const " + name + " was declared as type "
+ + type->get_name() + " which is an enum, but "
+ + value->get_identifier() + " is not a valid value for that enum";
}
} else if (type->is_struct() || type->is_xception()) {
if (value->get_type() != t_const_value::CV_MAP) {
@@ -857,8 +863,8 @@
* It's easier to do it this way instead of rewriting the whole grammar etc.
*/
void validate_simple_identifier(const char* identifier) {
- string name(identifier);
- if (name.find(".") != string::npos) {
+ string name( identifier);
+ if( name.find(".") != string::npos) {
yyerror("Identifier %s can't have a dot.", identifier);
exit(1);
}
@@ -898,9 +904,9 @@
bool skip_utf8_bom(FILE* f) {
// pretty straightforward, but works
- if (fgetc(f) == 0xEF) {
- if (fgetc(f) == 0xBB) {
- if (fgetc(f) == 0xBF) {
+ if( fgetc(f) == 0xEF) {
+ if( fgetc(f) == 0xBB) {
+ if( fgetc(f) == 0xBF) {
return true;
}
}
@@ -927,7 +933,7 @@
if (yyin == 0) {
failure("Could not open input file: \"%s\"", path.c_str());
}
- if (skip_utf8_bom(yyin))
+ if( skip_utf8_bom( yyin))
pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
// Create new scope and scan for includes
@@ -969,7 +975,7 @@
if (yyin == 0) {
failure("Could not open input file: \"%s\"", path.c_str());
}
- if (skip_utf8_bom(yyin))
+ if( skip_utf8_bom( yyin))
pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
pverbose("Parsing %s for types\n", path.c_str());
@@ -1004,7 +1010,7 @@
pverbose("Program: %s\n", program->get_path().c_str());
// Compute fingerprints. - not anymore, we do it on the fly now
- // generate_all_fingerprints(program);
+ //generate_all_fingerprints(program);
if (dump_docs) {
dump_docstrings(program);
@@ -1022,11 +1028,13 @@
delete generator;
}
}
+
} catch (string s) {
printf("Error: %s\n", s.c_str());
} catch (const char* exc) {
printf("Error: %s\n", exc);
}
+
}
/**
@@ -1054,7 +1062,7 @@
g_curpath = "arguments";
// Hacky parameter handling... I didn't feel like using a library sorry!
- for (i = 1; i < argc - 1; i++) {
+ for (i = 1; i < argc-1; i++) {
char* arg;
arg = strtok(argv[i], " ");
@@ -1076,9 +1084,9 @@
} else if (strcmp(arg, "-strict") == 0) {
g_strict = 255;
g_warn = 2;
- } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
+ } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
g_verbose = 1;
- } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
+ } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
gen_recurse = true;
} else if (strcmp(arg, "-allow-neg-keys") == 0) {
g_allow_neg_field_keys = true;
@@ -1110,9 +1118,10 @@
out_path = arg;
#ifdef _WIN32
- // strip out trailing \ on Windows
- int last = out_path.length() - 1;
- if (out_path[last] == '\\') {
+ //strip out trailing \ on Windows
+ int last = out_path.length()-1;
+ if (out_path[last] == '\\')
+ {
out_path.erase(last);
}
#endif
@@ -1129,12 +1138,12 @@
}
// display help
- if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
+ if ((strcmp(argv[argc-1], "-help") == 0) || (strcmp(argv[argc-1], "--help") == 0)) {
help();
}
// if you're asking for version, you have a right not to pass a file
- if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
+ if ((strcmp(argv[argc-1], "-version") == 0) || (strcmp(argv[argc-1], "--version") == 0)) {
version();
exit(0);
}
@@ -1175,17 +1184,17 @@
program->set_include_prefix(include_prefix);
// Initialize global types
- g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
+ g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
((t_base_type*)g_type_binary)->set_binary(true);
- g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
+ g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
((t_base_type*)g_type_slist)->set_string_list(true);
- g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
- g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
- g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
- g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
- g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
+ g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
+ g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
+ g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
+ g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
+ g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
// Parse it!
diff --git a/compiler/cpp/src/main.h b/compiler/cpp/src/main.h
index a4f81b3..9b0f3f3 100644
--- a/compiler/cpp/src/main.h
+++ b/compiler/cpp/src/main.h
@@ -29,7 +29,9 @@
* Defined in the flex library
*/
-extern "C" { int yylex(void); }
+extern "C" {
+ int yylex(void);
+}
int yyparse(void);
@@ -97,8 +99,8 @@
* Flex utilities
*/
-extern int yylineno;
-extern char yytext[];
+extern int yylineno;
+extern char yytext[];
extern FILE* yyin;
#endif
diff --git a/compiler/cpp/src/md5.h b/compiler/cpp/src/md5.h
index 3fbe498..3baa4dc 100644
--- a/compiler/cpp/src/md5.h
+++ b/compiler/cpp/src/md5.h
@@ -27,7 +27,7 @@
This code implements the MD5 Algorithm defined in RFC 1321, whose
text is available at
- http://www.ietf.org/rfc/rfc1321.txt
+ http://www.ietf.org/rfc/rfc1321.txt
The code is derived from the text of the RFC, including the test suite
(section A.5) but excluding the rest of Appendix A. It does not include
any code or documentation that is identified in the RFC as being
@@ -38,17 +38,17 @@
that follows (in reverse chronological order):
2002-04-13 lpd Removed support for non-ANSI compilers; removed
- references to Ghostscript; clarified derivation from RFC 1321;
- now handles byte order either statically or dynamically.
+ references to Ghostscript; clarified derivation from RFC 1321;
+ now handles byte order either statically or dynamically.
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
- added conditionalization for C++ compilation from Martin
- Purschke <purschke@bnl.gov>.
+ added conditionalization for C++ compilation from Martin
+ Purschke <purschke@bnl.gov>.
1999-05-03 lpd Original version.
*/
#ifndef md5_INCLUDED
-#define md5_INCLUDED
+# define md5_INCLUDED
/*
* This package supports both compile-time and run-time determination of CPU
@@ -61,30 +61,31 @@
*/
typedef unsigned char md5_byte_t; /* 8-bit byte */
-typedef unsigned int md5_word_t; /* 32-bit word */
+typedef unsigned int md5_word_t; /* 32-bit word */
/* Define the state of the MD5 Algorithm. */
typedef struct md5_state_s {
- md5_word_t count[2]; /* message length in bits, lsw first */
- md5_word_t abcd[4]; /* digest buffer */
- md5_byte_t buf[64]; /* accumulate block */
+ md5_word_t count[2]; /* message length in bits, lsw first */
+ md5_word_t abcd[4]; /* digest buffer */
+ md5_byte_t buf[64]; /* accumulate block */
} md5_state_t;
#ifdef __cplusplus
-extern "C" {
+extern "C"
+{
#endif
/* Initialize the algorithm. */
-void md5_init(md5_state_t* pms);
+void md5_init(md5_state_t *pms);
/* Append a string to the message. */
-void md5_append(md5_state_t* pms, const md5_byte_t* data, int nbytes);
+void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
/* Finish the message and return the digest. */
-void md5_finish(md5_state_t* pms, md5_byte_t digest[16]);
+void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
#ifdef __cplusplus
-} /* end extern "C" */
+} /* end extern "C" */
#endif
#endif /* md5_INCLUDED */
diff --git a/compiler/cpp/src/parse/parse.cc b/compiler/cpp/src/parse/parse.cc
index 7f94bab..0a0c2c0 100644
--- a/compiler/cpp/src/parse/parse.cc
+++ b/compiler/cpp/src/parse/parse.cc
@@ -24,7 +24,7 @@
#include "main.h"
void t_type::generate_fingerprint() {
- if (!has_fingerprint()) {
+ if (! has_fingerprint()) {
pdebug("generating fingerprint for %s", get_name().c_str());
std::string material = get_fingerprint_material();
md5_state_t ctx;
diff --git a/compiler/cpp/src/parse/t_base_type.h b/compiler/cpp/src/parse/t_base_type.h
index 34ff961..d76772b 100644
--- a/compiler/cpp/src/parse/t_base_type.h
+++ b/compiler/cpp/src/parse/t_base_type.h
@@ -29,7 +29,7 @@
*
*/
class t_base_type : public t_type {
-public:
+ public:
/**
* Enumeration of thrift base types
*/
@@ -44,34 +44,64 @@
TYPE_DOUBLE
};
- t_base_type(std::string name, t_base base)
- : t_type(name), base_(base), string_list_(false), binary_(false), string_enum_(false) {}
+ t_base_type(std::string name, t_base base) :
+ t_type(name),
+ base_(base),
+ string_list_(false),
+ binary_(false),
+ string_enum_(false){}
- t_base get_base() const { return base_; }
+ t_base get_base() const {
+ return base_;
+ }
- bool is_void() const { return base_ == TYPE_VOID; }
+ bool is_void() const {
+ return base_ == TYPE_VOID;
+ }
- bool is_string() const { return base_ == TYPE_STRING; }
+ bool is_string() const {
+ return base_ == TYPE_STRING;
+ }
- bool is_bool() const { return base_ == TYPE_BOOL; }
+ bool is_bool() const {
+ return base_ == TYPE_BOOL;
+ }
- void set_string_list(bool val) { string_list_ = val; }
+ void set_string_list(bool val) {
+ string_list_ = val;
+ }
- bool is_string_list() const { return (base_ == TYPE_STRING) && string_list_; }
+ bool is_string_list() const {
+ return (base_ == TYPE_STRING) && string_list_;
+ }
- void set_binary(bool val) { binary_ = val; }
+ void set_binary(bool val) {
+ binary_ = val;
+ }
- bool is_binary() const { return (base_ == TYPE_STRING) && binary_; }
+ bool is_binary() const {
+ return (base_ == TYPE_STRING) && binary_;
+ }
- void set_string_enum(bool val) { string_enum_ = val; }
+ void set_string_enum(bool val) {
+ string_enum_ = val;
+ }
- bool is_string_enum() const { return base_ == TYPE_STRING && string_enum_; }
+ bool is_string_enum() const {
+ return base_ == TYPE_STRING && string_enum_;
+ }
- void add_string_enum_val(std::string val) { string_enum_vals_.push_back(val); }
+ void add_string_enum_val(std::string val) {
+ string_enum_vals_.push_back(val);
+ }
- const std::vector<std::string>& get_string_enum_vals() const { return string_enum_vals_; }
+ const std::vector<std::string>& get_string_enum_vals() const {
+ return string_enum_vals_;
+ }
- bool is_base_type() const { return true; }
+ bool is_base_type() const {
+ return true;
+ }
virtual std::string get_fingerprint_material() const {
std::string rv = t_base_name(base_);
@@ -83,37 +113,19 @@
static std::string t_base_name(t_base tbase) {
switch (tbase) {
- case TYPE_VOID:
- return "void";
- break;
- case TYPE_STRING:
- return "string";
- break;
- case TYPE_BOOL:
- return "bool";
- break;
- case TYPE_BYTE:
- return "byte";
- break;
- case TYPE_I16:
- return "i16";
- break;
- case TYPE_I32:
- return "i32";
- break;
- case TYPE_I64:
- return "i64";
- break;
- case TYPE_DOUBLE:
- return "double";
- break;
- default:
- return "(unknown)";
- break;
+ case TYPE_VOID : return "void"; break;
+ case TYPE_STRING : return "string"; break;
+ case TYPE_BOOL : return "bool"; break;
+ case TYPE_BYTE : return "byte"; break;
+ case TYPE_I16 : return "i16"; break;
+ case TYPE_I32 : return "i32"; break;
+ case TYPE_I64 : return "i64"; break;
+ case TYPE_DOUBLE : return "double"; break;
+ default : return "(unknown)"; break;
}
}
-private:
+ private:
t_base base_;
bool string_list_;
diff --git a/compiler/cpp/src/parse/t_const.h b/compiler/cpp/src/parse/t_const.h
index 0f64bb1..7fd81bd 100644
--- a/compiler/cpp/src/parse/t_const.h
+++ b/compiler/cpp/src/parse/t_const.h
@@ -31,20 +31,29 @@
*
*/
class t_const : public t_doc {
-public:
- t_const(t_type* type, std::string name, t_const_value* value)
- : type_(type), name_(name), value_(value) {}
+ public:
+ t_const(t_type* type, std::string name, t_const_value* value) :
+ type_(type),
+ name_(name),
+ value_(value) {}
- t_type* get_type() const { return type_; }
+ t_type* get_type() const {
+ return type_;
+ }
- std::string get_name() const { return name_; }
+ std::string get_name() const {
+ return name_;
+ }
- t_const_value* get_value() const { return value_; }
+ t_const_value* get_value() const {
+ return value_;
+ }
-private:
+ private:
t_type* type_;
std::string name_;
t_const_value* value_;
};
#endif
+
diff --git a/compiler/cpp/src/parse/t_const_value.h b/compiler/cpp/src/parse/t_const_value.h
index 7e6e3f6..ff422ae 100644
--- a/compiler/cpp/src/parse/t_const_value.h
+++ b/compiler/cpp/src/parse/t_const_value.h
@@ -32,21 +32,35 @@
*
*/
class t_const_value {
-public:
- enum t_const_value_type { CV_INTEGER, CV_DOUBLE, CV_STRING, CV_MAP, CV_LIST, CV_IDENTIFIER };
+ public:
+
+ enum t_const_value_type {
+ CV_INTEGER,
+ CV_DOUBLE,
+ CV_STRING,
+ CV_MAP,
+ CV_LIST,
+ CV_IDENTIFIER
+ };
t_const_value() {}
- t_const_value(int64_t val) { set_integer(val); }
+ t_const_value(int64_t val) {
+ set_integer(val);
+ }
- t_const_value(std::string val) { set_string(val); }
+ t_const_value(std::string val) {
+ set_string(val);
+ }
void set_string(std::string val) {
valType_ = CV_STRING;
stringVal_ = val;
}
- std::string get_string() const { return stringVal_; }
+ std::string get_string() const {
+ return stringVal_;
+ }
void set_integer(int64_t val) {
valType_ = CV_INTEGER;
@@ -61,12 +75,13 @@
std::string identifier = get_identifier();
std::string::size_type dot = identifier.rfind('.');
if (dot != std::string::npos) {
- identifier = identifier.substr(dot + 1);
+ identifier = identifier.substr(dot+1);
}
t_enum_value* val = enum_->get_constant_by_name(identifier);
if (val == NULL) {
- throw "Unable to find enum value \"" + identifier + "\" in enum \"" + enum_->get_name()
- + "\"";
+ throw
+ "Unable to find enum value \"" + identifier +
+ "\" in enum \"" + enum_->get_name() + "\"";
}
return val->get_value();
} else {
@@ -79,26 +94,42 @@
doubleVal_ = val;
}
- double get_double() const { return doubleVal_; }
+ double get_double() const {
+ return doubleVal_;
+ }
- void set_map() { valType_ = CV_MAP; }
+ void set_map() {
+ valType_ = CV_MAP;
+ }
- void add_map(t_const_value* key, t_const_value* val) { mapVal_[key] = val; }
+ void add_map(t_const_value* key, t_const_value* val) {
+ mapVal_[key] = val;
+ }
- const std::map<t_const_value*, t_const_value*>& get_map() const { return mapVal_; }
+ const std::map<t_const_value*, t_const_value*>& get_map() const {
+ return mapVal_;
+ }
- void set_list() { valType_ = CV_LIST; }
+ void set_list() {
+ valType_ = CV_LIST;
+ }
- void add_list(t_const_value* val) { listVal_.push_back(val); }
+ void add_list(t_const_value* val) {
+ listVal_.push_back(val);
+ }
- const std::vector<t_const_value*>& get_list() const { return listVal_; }
+ const std::vector<t_const_value*>& get_list() const {
+ return listVal_;
+ }
void set_identifier(std::string val) {
valType_ = CV_IDENTIFIER;
identifierVal_ = val;
}
- std::string get_identifier() const { return identifierVal_; }
+ std::string get_identifier() const {
+ return identifierVal_;
+ }
std::string get_identifier_name() const {
std::string ret = get_identifier();
@@ -106,10 +137,10 @@
if (s == std::string::npos) {
throw "error: identifier " + ret + " is unqualified!";
}
- ret = ret.substr(s + 1);
+ ret = ret.substr(s+1);
s = ret.find('.');
if (s != std::string::npos) {
- ret = ret.substr(s + 1);
+ ret = ret.substr(s+1);
}
return ret;
}
@@ -120,18 +151,22 @@
if (s == std::string::npos) {
throw "error: identifier " + ret + " is unqualified!";
}
- size_t s2 = ret.find('.', s + 1);
+ size_t s2 = ret.find('.', s+1);
if (s2 != std::string::npos) {
- ret = ret.substr(s + 1);
+ ret = ret.substr(s+1);
}
return ret;
}
- void set_enum(t_enum* tenum) { enum_ = tenum; }
+ void set_enum(t_enum* tenum) {
+ enum_ = tenum;
+ }
- t_const_value_type get_type() const { return valType_; }
+ t_const_value_type get_type() const {
+ return valType_;
+ }
-private:
+ private:
std::map<t_const_value*, t_const_value*> mapVal_;
std::vector<t_const_value*> listVal_;
std::string stringVal_;
@@ -141,6 +176,8 @@
t_enum* enum_;
t_const_value_type valType_;
+
};
#endif
+
diff --git a/compiler/cpp/src/parse/t_container.h b/compiler/cpp/src/parse/t_container.h
index 0d992b7..6753493 100644
--- a/compiler/cpp/src/parse/t_container.h
+++ b/compiler/cpp/src/parse/t_container.h
@@ -23,8 +23,10 @@
#include "t_type.h"
class t_container : public t_type {
-public:
- t_container() : cpp_name_(), has_cpp_name_(false) {}
+ public:
+ t_container() :
+ cpp_name_(),
+ has_cpp_name_(false) {}
virtual ~t_container() {}
@@ -33,15 +35,22 @@
has_cpp_name_ = true;
}
- bool has_cpp_name() { return has_cpp_name_; }
+ bool has_cpp_name() {
+ return has_cpp_name_;
+ }
- std::string get_cpp_name() { return cpp_name_; }
+ std::string get_cpp_name() {
+ return cpp_name_;
+ }
- bool is_container() const { return true; }
+ bool is_container() const {
+ return true;
+ }
-private:
+ private:
std::string cpp_name_;
bool has_cpp_name_;
+
};
#endif
diff --git a/compiler/cpp/src/parse/t_doc.h b/compiler/cpp/src/parse/t_doc.h
index 9d310b7..2c63b5a 100644
--- a/compiler/cpp/src/parse/t_doc.h
+++ b/compiler/cpp/src/parse/t_doc.h
@@ -29,26 +29,30 @@
*/
class t_doc {
-public:
+ public:
t_doc() : has_doc_(false) {}
void set_doc(const std::string& doc) {
doc_ = doc;
has_doc_ = true;
- if ((g_program_doctext_lineno == g_doctext_lineno)
- && (g_program_doctext_status == STILL_CANDIDATE)) {
+ if( (g_program_doctext_lineno == g_doctext_lineno) && (g_program_doctext_status == STILL_CANDIDATE)) {
g_program_doctext_status = ALREADY_PROCESSED;
- pdebug("%s", "program doctext set to ALREADY_PROCESSED");
+ pdebug("%s","program doctext set to ALREADY_PROCESSED");
}
}
- const std::string& get_doc() const { return doc_; }
+ const std::string& get_doc() const {
+ return doc_;
+ }
- bool has_doc() { return has_doc_; }
+ bool has_doc() {
+ return has_doc_;
+ }
-private:
+ private:
std::string doc_;
bool has_doc_;
+
};
#endif
diff --git a/compiler/cpp/src/parse/t_enum.h b/compiler/cpp/src/parse/t_enum.h
index 59941e3..94cb26e 100644
--- a/compiler/cpp/src/parse/t_enum.h
+++ b/compiler/cpp/src/parse/t_enum.h
@@ -28,14 +28,21 @@
*
*/
class t_enum : public t_type {
-public:
- t_enum(t_program* program) : t_type(program) {}
+ public:
+ t_enum(t_program* program) :
+ t_type(program) {}
- void set_name(const std::string& name) { name_ = name; }
+ void set_name(const std::string& name) {
+ name_ = name;
+ }
- void append(t_enum_value* constant) { constants_.push_back(constant); }
+ void append(t_enum_value* constant) {
+ constants_.push_back(constant);
+ }
- const std::vector<t_enum_value*>& get_constants() { return constants_; }
+ const std::vector<t_enum_value*>& get_constants() {
+ return constants_;
+ }
t_enum_value* get_constant_by_name(const std::string name) {
const std::vector<t_enum_value*>& enum_values = get_constants();
@@ -65,7 +72,8 @@
t_enum_value* min_value;
if (enum_values.size() == 0) {
min_value = NULL;
- } else {
+ }
+ else {
int min_value_value;
min_value = enum_values.front();
min_value_value = min_value->get_value();
@@ -85,7 +93,8 @@
t_enum_value* max_value;
if (enum_values.size() == 0) {
max_value = NULL;
- } else {
+ }
+ else {
int max_value_value;
max_value = enum_values.back();
max_value_value = max_value->get_value();
@@ -99,11 +108,16 @@
return max_value;
}
- bool is_enum() const { return true; }
+ bool is_enum() const {
+ return true;
+ }
- virtual std::string get_fingerprint_material() const { return "enum"; }
+ virtual std::string get_fingerprint_material() const {
+ return "enum";
+ }
-private:
+
+ private:
std::vector<t_enum_value*> constants_;
};
diff --git a/compiler/cpp/src/parse/t_enum_value.h b/compiler/cpp/src/parse/t_enum_value.h
index 5979f06..26798d7 100644
--- a/compiler/cpp/src/parse/t_enum_value.h
+++ b/compiler/cpp/src/parse/t_enum_value.h
@@ -30,18 +30,24 @@
*
*/
class t_enum_value : public t_doc {
-public:
- t_enum_value(std::string name, int value) : name_(name), value_(value) {}
+ public:
+ t_enum_value(std::string name, int value) :
+ name_(name),
+ value_(value) {}
~t_enum_value() {}
- const std::string& get_name() const { return name_; }
+ const std::string& get_name() const {
+ return name_;
+ }
- int get_value() const { return value_; }
+ int get_value() const {
+ return value_;
+ }
std::map<std::string, std::string> annotations_;
-private:
+ private:
std::string name_;
int value_;
};
diff --git a/compiler/cpp/src/parse/t_field.h b/compiler/cpp/src/parse/t_field.h
index c4e30e3..c05fdf3 100644
--- a/compiler/cpp/src/parse/t_field.h
+++ b/compiler/cpp/src/parse/t_field.h
@@ -34,65 +34,96 @@
*
*/
class t_field : public t_doc {
-public:
- t_field(t_type* type, std::string name)
- : type_(type),
- name_(name),
- key_(0),
- value_(NULL),
- xsd_optional_(false),
- xsd_nillable_(false),
- xsd_attrs_(NULL),
- reference_(false) {}
+ public:
+ t_field(t_type* type, std::string name) :
+ type_(type),
+ name_(name),
+ key_(0),
+ value_(NULL),
+ xsd_optional_(false),
+ xsd_nillable_(false),
+ xsd_attrs_(NULL),
+ reference_(false) {}
- t_field(t_type* type, std::string name, int32_t key)
- : type_(type),
- name_(name),
- key_(key),
- req_(T_OPT_IN_REQ_OUT),
- value_(NULL),
- xsd_optional_(false),
- xsd_nillable_(false),
- xsd_attrs_(NULL),
- reference_(false) {}
+ t_field(t_type* type, std::string name, int32_t key) :
+ type_(type),
+ name_(name),
+ key_(key),
+ req_(T_OPT_IN_REQ_OUT),
+ value_(NULL),
+ xsd_optional_(false),
+ xsd_nillable_(false),
+ xsd_attrs_(NULL),
+ reference_(false) {}
~t_field() {}
- t_type* get_type() const { return type_; }
+ t_type* get_type() const {
+ return type_;
+ }
- const std::string& get_name() const { return name_; }
+ const std::string& get_name() const {
+ return name_;
+ }
- int32_t get_key() const { return key_; }
+ int32_t get_key() const {
+ return key_;
+ }
- enum e_req { T_REQUIRED, T_OPTIONAL, T_OPT_IN_REQ_OUT };
+ enum e_req {
+ T_REQUIRED,
+ T_OPTIONAL,
+ T_OPT_IN_REQ_OUT
+ };
- void set_req(e_req req) { req_ = req; }
+ void set_req(e_req req) {
+ req_ = req;
+ }
- e_req get_req() const { return req_; }
+ e_req get_req() const {
+ return req_;
+ }
- void set_value(t_const_value* value) { value_ = value; }
+ void set_value(t_const_value* value) {
+ value_ = value;
+ }
- t_const_value* get_value() { return value_; }
+ t_const_value* get_value() {
+ return value_;
+ }
- void set_xsd_optional(bool xsd_optional) { xsd_optional_ = xsd_optional; }
+ void set_xsd_optional(bool xsd_optional) {
+ xsd_optional_ = xsd_optional;
+ }
- bool get_xsd_optional() const { return xsd_optional_; }
+ bool get_xsd_optional() const {
+ return xsd_optional_;
+ }
- void set_xsd_nillable(bool xsd_nillable) { xsd_nillable_ = xsd_nillable; }
+ void set_xsd_nillable(bool xsd_nillable) {
+ xsd_nillable_ = xsd_nillable;
+ }
- bool get_xsd_nillable() const { return xsd_nillable_; }
+ bool get_xsd_nillable() const {
+ return xsd_nillable_;
+ }
- void set_xsd_attrs(t_struct* xsd_attrs) { xsd_attrs_ = xsd_attrs; }
+ void set_xsd_attrs(t_struct* xsd_attrs) {
+ xsd_attrs_ = xsd_attrs;
+ }
- t_struct* get_xsd_attrs() { return xsd_attrs_; }
+ t_struct* get_xsd_attrs() {
+ return xsd_attrs_;
+ }
// This is not the same function as t_type::get_fingerprint_material,
// but it does the same thing.
std::string get_fingerprint_material() const {
std::ostringstream keystm;
keystm << key_;
- return keystm.str() + ":" + ((req_ == T_OPTIONAL) ? "opt-" : "")
- + type_->get_fingerprint_material();
+ return keystm.str() + ":" +
+ ((req_ == T_OPTIONAL) ? "opt-" : "") +
+ type_->get_fingerprint_material();
}
/**
@@ -101,18 +132,22 @@
* The arguments are (const) references to const pointers to const t_fields.
*/
struct key_compare {
- bool operator()(t_field const* const& a, t_field const* const& b) {
+ bool operator()(t_field const * const & a, t_field const * const & b) {
return a->get_key() < b->get_key();
}
};
std::map<std::string, std::string> annotations_;
- bool get_reference() { return reference_; }
+ bool get_reference() {
+ return reference_;
+ }
- void set_reference(bool reference) { reference_ = reference; }
+ void set_reference(bool reference) {
+ reference_ = reference;
+ }
-private:
+ private:
t_type* type_;
std::string name_;
int32_t key_;
diff --git a/compiler/cpp/src/parse/t_function.h b/compiler/cpp/src/parse/t_function.h
index 96886f3..ae8c2f6 100644
--- a/compiler/cpp/src/parse/t_function.h
+++ b/compiler/cpp/src/parse/t_function.h
@@ -32,11 +32,17 @@
*
*/
class t_function : public t_doc {
-public:
- t_function(t_type* returntype, std::string name, t_struct* arglist, bool oneway = false)
- : returntype_(returntype), name_(name), arglist_(arglist), oneway_(oneway) {
+ public:
+ t_function(t_type* returntype,
+ std::string name,
+ t_struct* arglist,
+ bool oneway=false) :
+ returntype_(returntype),
+ name_(name),
+ arglist_(arglist),
+ oneway_(oneway) {
xceptions_ = new t_struct(NULL);
- if (oneway_ && (!returntype_->is_void())) {
+ if (oneway_ && (! returntype_->is_void())) {
pwarning(1, "Oneway methods should return void.\n");
}
}
@@ -45,35 +51,46 @@
std::string name,
t_struct* arglist,
t_struct* xceptions,
- bool oneway = false)
- : returntype_(returntype),
- name_(name),
- arglist_(arglist),
- xceptions_(xceptions),
- oneway_(oneway) {
+ bool oneway=false) :
+ returntype_(returntype),
+ name_(name),
+ arglist_(arglist),
+ xceptions_(xceptions),
+ oneway_(oneway)
+ {
if (oneway_ && !xceptions_->get_members().empty()) {
throw std::string("Oneway methods can't throw exceptions.");
}
- if (oneway_ && (!returntype_->is_void())) {
+ if (oneway_ && (! returntype_->is_void())) {
pwarning(1, "Oneway methods should return void.\n");
}
}
~t_function() {}
- t_type* get_returntype() const { return returntype_; }
+ t_type* get_returntype() const {
+ return returntype_;
+ }
- const std::string& get_name() const { return name_; }
+ const std::string& get_name() const {
+ return name_;
+ }
- t_struct* get_arglist() const { return arglist_; }
+ t_struct* get_arglist() const {
+ return arglist_;
+ }
- t_struct* get_xceptions() const { return xceptions_; }
+ t_struct* get_xceptions() const {
+ return xceptions_;
+ }
- bool is_oneway() const { return oneway_; }
+ bool is_oneway() const {
+ return oneway_;
+ }
std::map<std::string, std::string> annotations_;
-private:
+ private:
t_type* returntype_;
std::string name_;
t_struct* arglist_;
diff --git a/compiler/cpp/src/parse/t_list.h b/compiler/cpp/src/parse/t_list.h
index e121d1d..21a9625 100644
--- a/compiler/cpp/src/parse/t_list.h
+++ b/compiler/cpp/src/parse/t_list.h
@@ -27,12 +27,17 @@
*
*/
class t_list : public t_container {
-public:
- t_list(t_type* elem_type) : elem_type_(elem_type) {}
+ public:
+ t_list(t_type* elem_type) :
+ elem_type_(elem_type) {}
- t_type* get_elem_type() const { return elem_type_; }
+ t_type* get_elem_type() const {
+ return elem_type_;
+ }
- bool is_list() const { return true; }
+ bool is_list() const {
+ return true;
+ }
virtual std::string get_fingerprint_material() const {
return "list<" + elem_type_->get_fingerprint_material() + ">";
@@ -43,8 +48,9 @@
elem_type_->generate_fingerprint();
}
-private:
+ private:
t_type* elem_type_;
};
#endif
+
diff --git a/compiler/cpp/src/parse/t_map.h b/compiler/cpp/src/parse/t_map.h
index 4795147..c4e358f 100644
--- a/compiler/cpp/src/parse/t_map.h
+++ b/compiler/cpp/src/parse/t_map.h
@@ -28,18 +28,26 @@
*
*/
class t_map : public t_container {
-public:
- t_map(t_type* key_type, t_type* val_type) : key_type_(key_type), val_type_(val_type) {}
+ public:
+ t_map(t_type* key_type, t_type* val_type) :
+ key_type_(key_type),
+ val_type_(val_type) {}
- t_type* get_key_type() const { return key_type_; }
+ t_type* get_key_type() const {
+ return key_type_;
+ }
- t_type* get_val_type() const { return val_type_; }
+ t_type* get_val_type() const {
+ return val_type_;
+ }
- bool is_map() const { return true; }
+ bool is_map() const {
+ return true;
+ }
virtual std::string get_fingerprint_material() const {
- return "map<" + key_type_->get_fingerprint_material() + ","
- + val_type_->get_fingerprint_material() + ">";
+ return "map<" + key_type_->get_fingerprint_material() +
+ "," + val_type_->get_fingerprint_material() + ">";
}
virtual void generate_fingerprint() {
@@ -48,7 +56,7 @@
val_type_->generate_fingerprint();
}
-private:
+ private:
t_type* key_type_;
t_type* val_type_;
};
diff --git a/compiler/cpp/src/parse/t_program.h b/compiler/cpp/src/parse/t_program.h
index 51157aa..d18cc5d 100644
--- a/compiler/cpp/src/parse/t_program.h
+++ b/compiler/cpp/src/parse/t_program.h
@@ -56,22 +56,30 @@
*
*/
class t_program : public t_doc {
-public:
- t_program(std::string path, std::string name)
- : path_(path), name_(name), out_path_("./"), out_path_is_absolute_(false) {
+ public:
+ t_program(std::string path, std::string name) :
+ path_(path),
+ name_(name),
+ out_path_("./"),
+ out_path_is_absolute_(false) {
scope_ = new t_scope();
}
- t_program(std::string path) : path_(path), out_path_("./"), out_path_is_absolute_(false) {
+ t_program(std::string path) :
+ path_(path),
+ out_path_("./"),
+ out_path_is_absolute_(false) {
name_ = program_name(path);
scope_ = new t_scope();
}
- ~t_program() {
- if (scope_) {
- delete scope_;
- scope_ = NULL;
- }
+ ~t_program()
+ {
+ if(scope_)
+ {
+ delete scope_;
+ scope_ = NULL;
+ }
}
// Path accessor
@@ -93,27 +101,23 @@
const std::string& get_include_prefix() const { return include_prefix_; }
// Accessors for program elements
- const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
- const std::vector<t_enum*>& get_enums() const { return enums_; }
- const std::vector<t_const*>& get_consts() const { return consts_; }
- const std::vector<t_struct*>& get_structs() const { return structs_; }
- const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
- const std::vector<t_struct*>& get_objects() const { return objects_; }
- const std::vector<t_service*>& get_services() const { return services_; }
+ const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
+ const std::vector<t_enum*>& get_enums() const { return enums_; }
+ const std::vector<t_const*>& get_consts() const { return consts_; }
+ const std::vector<t_struct*>& get_structs() const { return structs_; }
+ const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
+ const std::vector<t_struct*>& get_objects() const { return objects_; }
+ const std::vector<t_service*>& get_services() const { return services_; }
// Program elements
- void add_typedef(t_typedef* td) { typedefs_.push_back(td); }
- void add_enum(t_enum* te) { enums_.push_back(te); }
- void add_const(t_const* tc) { consts_.push_back(tc); }
- void add_struct(t_struct* ts) {
- objects_.push_back(ts);
- structs_.push_back(ts);
- }
- void add_xception(t_struct* tx) {
- objects_.push_back(tx);
- xceptions_.push_back(tx);
- }
- void add_service(t_service* ts) { services_.push_back(ts); }
+ void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
+ void add_enum (t_enum* te) { enums_.push_back(te); }
+ void add_const (t_const* tc) { consts_.push_back(tc); }
+ void add_struct (t_struct* ts) { objects_.push_back(ts);
+ structs_.push_back(ts); }
+ void add_xception (t_struct* tx) { objects_.push_back(tx);
+ xceptions_.push_back(tx); }
+ void add_service (t_service* ts) { services_.push_back(ts); }
// Programs to include
const std::vector<t_program*>& get_includes() const { return includes_; }
@@ -134,9 +138,10 @@
* @param t the type to test for collisions
* @return true if a certain collision was found, otherwise false
*/
- bool is_unique_typename(t_type* t) {
+ bool is_unique_typename(t_type * t) {
int occurances = program_typename_count(this, t);
- for (std::vector<t_program*>::iterator it = includes_.begin(); it != includes_.end(); ++it) {
+ for (std::vector<t_program*>::iterator it = includes_.begin();
+ it != includes_.end(); ++it) {
occurances += program_typename_count(*it, t);
}
return 0 == occurances;
@@ -148,7 +153,7 @@
* @param t the type to test for collisions
* @return the number of certain typename collisions
*/
- int program_typename_count(t_program* prog, t_type* t) {
+ int program_typename_count(t_program * prog, t_type * t) {
int occurances = 0;
occurances += collection_typename_count(prog, prog->typedefs_, t);
occurances += collection_typename_count(prog, prog->enums_, t);
@@ -165,7 +170,7 @@
* @return the number of certain typename collisions
*/
template <class T>
- int collection_typename_count(t_program* prog, T type_collection, t_type* t) {
+ int collection_typename_count(t_program * prog, T type_collection, t_type * t) {
int occurances = 0;
for (typename T::iterator it = type_collection.begin(); it != type_collection.end(); ++it)
if (t != *it && 0 == t->get_name().compare((*it)->get_name()) && is_common_namespace(prog, t))
@@ -185,67 +190,54 @@
* @param t the type containing the typename match
* @return true if a collision within namespaces is found, otherwise false
*/
- bool is_common_namespace(t_program* prog, t_type* t) {
- // Case 1: Typenames are in the same program [collision]
+ bool is_common_namespace(t_program * prog, t_type * t) {
+ //Case 1: Typenames are in the same program [collision]
if (prog == t->get_program()) {
- pwarning(1,
- "Duplicate typename %s found in %s",
- t->get_name().c_str(),
- t->get_program()->get_name().c_str());
+ pwarning(1, "Duplicate typename %s found in %s",
+ t->get_name().c_str(), t->get_program()->get_name().c_str());
return true;
}
- // Case 2: Both programs have identical namespace scope/name declarations [collision]
+ //Case 2: Both programs have identical namespace scope/name declarations [collision]
bool match = true;
for (std::map<std::string, std::string>::iterator it = prog->namespaces_.begin();
- it != prog->namespaces_.end();
- ++it) {
+ it != prog->namespaces_.end(); ++it) {
if (0 == it->second.compare(t->get_program()->get_namespace(it->first))) {
- pwarning(1,
- "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
+ pwarning(1, "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
t->get_name().c_str(),
- t->get_program()->get_name().c_str(),
- it->first.c_str(),
- it->second.c_str(),
- prog->get_name().c_str(),
- it->first.c_str(),
- it->second.c_str());
+ t->get_program()->get_name().c_str(), it->first.c_str(), it->second.c_str(),
+ prog->get_name().c_str(), it->first.c_str(), it->second.c_str());
} else {
match = false;
}
}
for (std::map<std::string, std::string>::iterator it = t->get_program()->namespaces_.begin();
- it != t->get_program()->namespaces_.end();
- ++it) {
+ it != t->get_program()->namespaces_.end(); ++it) {
if (0 == it->second.compare(prog->get_namespace(it->first))) {
- pwarning(1,
- "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
+ pwarning(1, "Duplicate typename %s found in %s,%s,%s and %s,%s,%s [file,scope,ns]",
t->get_name().c_str(),
- t->get_program()->get_name().c_str(),
- it->first.c_str(),
- it->second.c_str(),
- prog->get_name().c_str(),
- it->first.c_str(),
- it->second.c_str());
+ t->get_program()->get_name().c_str(), it->first.c_str(), it->second.c_str(),
+ prog->get_name().c_str(), it->first.c_str(), it->second.c_str());
} else {
match = false;
}
}
if (0 == prog->namespaces_.size() && 0 == t->get_program()->namespaces_.size()) {
- pwarning(1,
- "Duplicate typename %s found in %s and %s",
- t->get_name().c_str(),
- t->get_program()->get_name().c_str(),
- prog->get_name().c_str());
+ pwarning(1, "Duplicate typename %s found in %s and %s",
+ t->get_name().c_str(), t->get_program()->get_name().c_str(), prog->get_name().c_str());
}
return match;
}
// Scoping and namespacing
- void set_namespace(std::string name) { namespace_ = name; }
+ void set_namespace(std::string name) {
+ namespace_ = name;
+ }
// Scope accessor
- t_scope* scope() const { return scope_; }
+ t_scope* scope() const {
+ return scope_;
+ }
// Includes
@@ -264,7 +256,9 @@
includes_.push_back(program);
}
- std::vector<t_program*>& get_includes() { return includes_; }
+ std::vector<t_program*>& get_includes() {
+ return includes_;
+ }
void set_include_prefix(std::string include_prefix) {
include_prefix_ = include_prefix;
@@ -283,7 +277,7 @@
std::string base_language = language.substr(0, sub_index);
std::string sub_namespace;
- if (base_language == "smalltalk") {
+ if(base_language == "smalltalk") {
pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead");
base_language = "st";
}
@@ -291,17 +285,16 @@
t_generator_registry::gen_map_t my_copy = t_generator_registry::get_generator_map();
t_generator_registry::gen_map_t::iterator it;
- it = my_copy.find(base_language);
+ it=my_copy.find(base_language);
if (it == my_copy.end()) {
std::string warning = "No generator named '" + base_language + "' could be found!";
pwarning(1, warning.c_str());
} else {
if (sub_index != std::string::npos) {
- std::string sub_namespace = language.substr(sub_index + 1);
- if (!it->second->is_valid_namespace(sub_namespace)) {
- std::string warning = base_language + " generator does not accept '" + sub_namespace
- + "' as sub-namespace!";
+ std::string sub_namespace = language.substr(sub_index+1);
+ if ( ! it->second->is_valid_namespace(sub_namespace)) {
+ std::string warning = base_language + " generator does not accept '" + sub_namespace + "' as sub-namespace!";
pwarning(1, warning.c_str());
}
}
@@ -313,8 +306,8 @@
std::string get_namespace(std::string language) const {
std::map<std::string, std::string>::const_iterator iter;
- if ((iter = namespaces_.find(language)) != namespaces_.end()
- || (iter = namespaces_.find("*")) != namespaces_.end()) {
+ if ((iter = namespaces_.find(language)) != namespaces_.end() ||
+ (iter = namespaces_.find("*" )) != namespaces_.end()) {
return iter->second;
}
return std::string();
@@ -322,15 +315,24 @@
// Language specific namespace / packaging
- void add_cpp_include(std::string path) { cpp_includes_.push_back(path); }
+ void add_cpp_include(std::string path) {
+ cpp_includes_.push_back(path);
+ }
- const std::vector<std::string>& get_cpp_includes() { return cpp_includes_; }
+ const std::vector<std::string>& get_cpp_includes() {
+ return cpp_includes_;
+ }
- void add_c_include(std::string path) { c_includes_.push_back(path); }
+ void add_c_include(std::string path) {
+ c_includes_.push_back(path);
+ }
- const std::vector<std::string>& get_c_includes() { return c_includes_; }
+ const std::vector<std::string>& get_c_includes() {
+ return c_includes_;
+ }
-private:
+ private:
+
// File path
std::string path_;
@@ -357,11 +359,11 @@
// Components to generate code for
std::vector<t_typedef*> typedefs_;
- std::vector<t_enum*> enums_;
- std::vector<t_const*> consts_;
- std::vector<t_struct*> objects_;
- std::vector<t_struct*> structs_;
- std::vector<t_struct*> xceptions_;
+ std::vector<t_enum*> enums_;
+ std::vector<t_const*> consts_;
+ std::vector<t_struct*> objects_;
+ std::vector<t_struct*> structs_;
+ std::vector<t_struct*> xceptions_;
std::vector<t_service*> services_;
// Dynamic namespaces
@@ -372,6 +374,7 @@
// C extra includes
std::vector<std::string> c_includes_;
+
};
#endif
diff --git a/compiler/cpp/src/parse/t_scope.h b/compiler/cpp/src/parse/t_scope.h
index c108fdd..e73c0f3 100644
--- a/compiler/cpp/src/parse/t_scope.h
+++ b/compiler/cpp/src/parse/t_scope.h
@@ -40,16 +40,24 @@
*
*/
class t_scope {
-public:
+ public:
t_scope() {}
- void add_type(std::string name, t_type* type) { types_[name] = type; }
+ void add_type(std::string name, t_type* type) {
+ types_[name] = type;
+ }
- t_type* get_type(std::string name) { return types_[name]; }
+ t_type* get_type(std::string name) {
+ return types_[name];
+ }
- void add_service(std::string name, t_service* service) { services_[name] = service; }
+ void add_service(std::string name, t_service* service) {
+ services_[name] = service;
+ }
- t_service* get_service(std::string name) { return services_[name]; }
+ t_service* get_service(std::string name) {
+ return services_[name];
+ }
void add_constant(std::string name, t_const* constant) {
if (constants_.find(name) != constants_.end()) {
@@ -59,12 +67,16 @@
}
}
- t_const* get_constant(std::string name) { return constants_[name]; }
+ t_const* get_constant(std::string name) {
+ return constants_[name];
+ }
void print() {
std::map<std::string, t_type*>::iterator iter;
for (iter = types_.begin(); iter != types_.end(); ++iter) {
- printf("%s => %s\n", iter->first.c_str(), iter->second->get_name().c_str());
+ printf("%s => %s\n",
+ iter->first.c_str(),
+ iter->second->get_name().c_str());
}
}
@@ -89,8 +101,7 @@
for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) {
t_field* field = tstruct->get_field_by_name(v_iter->first->get_string());
if (field == NULL) {
- throw "No field named \"" + v_iter->first->get_string()
- + "\" was found in struct of type \"" + tstruct->get_name() + "\"";
+ throw "No field named \"" + v_iter->first->get_string() + "\" was found in struct of type \"" + tstruct->get_name() + "\"";
}
resolve_const_value(v_iter->second, field->get_type());
}
@@ -108,21 +119,21 @@
if (const_type->is_base_type()) {
switch (((t_base_type*)const_type)->get_base()) {
- case t_base_type::TYPE_I16:
- case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
- case t_base_type::TYPE_BOOL:
- case t_base_type::TYPE_BYTE:
- const_val->set_integer(constant->get_value()->get_integer());
- break;
- case t_base_type::TYPE_STRING:
- const_val->set_string(constant->get_value()->get_string());
- break;
- case t_base_type::TYPE_DOUBLE:
- const_val->set_double(constant->get_value()->get_double());
- break;
- case t_base_type::TYPE_VOID:
- throw "Constants cannot be of type VOID";
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ case t_base_type::TYPE_I64:
+ case t_base_type::TYPE_BOOL:
+ case t_base_type::TYPE_BYTE:
+ const_val->set_integer(constant->get_value()->get_integer());
+ break;
+ case t_base_type::TYPE_STRING:
+ const_val->set_string(constant->get_value()->get_string());
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ const_val->set_double(constant->get_value()->get_double());
+ break;
+ case t_base_type::TYPE_VOID:
+ throw "Constants cannot be of type VOID";
}
} else if (const_type->is_map()) {
const std::map<t_const_value*, t_const_value*>& map = constant->get_value()->get_map();
@@ -150,15 +161,15 @@
if (enum_value == NULL) {
std::ostringstream valstm;
valstm << const_val->get_integer();
- throw "Couldn't find a named value in enum " + tenum->get_name() + " for value "
- + valstm.str();
+ throw "Couldn't find a named value in enum " + tenum->get_name() + " for value " + valstm.str();
}
const_val->set_identifier(tenum->get_name() + "." + enum_value->get_name());
const_val->set_enum(tenum);
}
}
-private:
+ private:
+
// Map of names to types
std::map<std::string, t_type*> types_;
@@ -167,6 +178,7 @@
// Map of names to services
std::map<std::string, t_service*> services_;
+
};
#endif
diff --git a/compiler/cpp/src/parse/t_service.h b/compiler/cpp/src/parse/t_service.h
index 091403d..31e6924 100644
--- a/compiler/cpp/src/parse/t_service.h
+++ b/compiler/cpp/src/parse/t_service.h
@@ -30,12 +30,18 @@
*
*/
class t_service : public t_type {
-public:
- t_service(t_program* program) : t_type(program), extends_(NULL) {}
+ public:
+ t_service(t_program* program) :
+ t_type(program),
+ extends_(NULL) {}
- bool is_service() const { return true; }
+ bool is_service() const {
+ return true;
+ }
- void set_extends(t_service* extends) { extends_ = extends; }
+ void set_extends(t_service* extends) {
+ extends_ = extends;
+ }
void add_function(t_function* func) {
std::vector<t_function*>::const_iterator iter;
@@ -47,16 +53,20 @@
functions_.push_back(func);
}
- const std::vector<t_function*>& get_functions() const { return functions_; }
+ const std::vector<t_function*>& get_functions() const {
+ return functions_;
+ }
- t_service* get_extends() { return extends_; }
+ t_service* get_extends() {
+ return extends_;
+ }
virtual std::string get_fingerprint_material() const {
// Services should never be used in fingerprints.
throw "BUG: Can't get fingerprint material for service.";
}
-private:
+ private:
std::vector<t_function*> functions_;
t_service* extends_;
};
diff --git a/compiler/cpp/src/parse/t_set.h b/compiler/cpp/src/parse/t_set.h
index ccfc701..d198357 100644
--- a/compiler/cpp/src/parse/t_set.h
+++ b/compiler/cpp/src/parse/t_set.h
@@ -27,12 +27,17 @@
*
*/
class t_set : public t_container {
-public:
- t_set(t_type* elem_type) : elem_type_(elem_type) {}
+ public:
+ t_set(t_type* elem_type) :
+ elem_type_(elem_type) {}
- t_type* get_elem_type() const { return elem_type_; }
+ t_type* get_elem_type() const {
+ return elem_type_;
+ }
- bool is_set() const { return true; }
+ bool is_set() const {
+ return true;
+ }
virtual std::string get_fingerprint_material() const {
return "set<" + elem_type_->get_fingerprint_material() + ">";
@@ -43,7 +48,7 @@
elem_type_->generate_fingerprint();
}
-private:
+ private:
t_type* elem_type_;
};
diff --git a/compiler/cpp/src/parse/t_struct.h b/compiler/cpp/src/parse/t_struct.h
index 2282676..8dd4c73 100644
--- a/compiler/cpp/src/parse/t_struct.h
+++ b/compiler/cpp/src/parse/t_struct.h
@@ -37,59 +37,58 @@
*
*/
class t_struct : public t_type {
-public:
+ public:
typedef std::vector<t_field*> members_type;
- t_struct(t_program* program)
- : t_type(program),
- is_xception_(false),
- is_union_(false),
- members_validated(false),
- members_with_value(0),
- xsd_all_(false) {}
+ t_struct(t_program* program) :
+ t_type(program),
+ is_xception_(false),
+ is_union_(false),
+ members_validated(false),
+ members_with_value(0),
+ xsd_all_(false) {}
- t_struct(t_program* program, const std::string& name)
- : t_type(program, name),
- is_xception_(false),
- is_union_(false),
- members_validated(false),
- members_with_value(0),
- xsd_all_(false) {}
+ t_struct(t_program* program, const std::string& name) :
+ t_type(program, name),
+ is_xception_(false),
+ is_union_(false),
+ members_validated(false),
+ members_with_value(0),
+ xsd_all_(false) {}
void set_name(const std::string& name) {
name_ = name;
validate_union_members();
}
- void set_xception(bool is_xception) { is_xception_ = is_xception; }
+ void set_xception(bool is_xception) {
+ is_xception_ = is_xception;
+ }
- void validate_union_member(t_field* field) {
- if (is_union_ && (!name_.empty())) {
+ void validate_union_member( t_field * field) {
+ if( is_union_ && (! name_.empty())) {
// unions can't have required fields
- if (field->get_req() == t_field::T_REQUIRED) {
- pwarning(1,
- "Required field %s of union %s set to optional.\n",
- field->get_name().c_str(),
- name_.c_str());
- field->set_req(t_field::T_OPTIONAL);
+ if( field->get_req() == t_field::T_REQUIRED) {
+ pwarning( 1, "Required field %s of union %s set to optional.\n", field->get_name().c_str(), name_.c_str());
+ field->set_req( t_field::T_OPTIONAL);
}
// unions may have up to one member defaulted, but not more
- if (field->get_value() != NULL) {
- if (1 < ++members_with_value) {
- throw "Error: Field " + field->get_name() + " provides another default value for union "
- + name_;
+ if( field->get_value() != NULL) {
+ if( 1 < ++members_with_value) {
+ throw "Error: Field "+field->get_name()+" provides another default value for union "+name_;
}
}
}
+
}
void validate_union_members() {
- if (is_union_ && (!name_.empty()) && (!members_validated)) {
+ if( is_union_ && (! name_.empty()) && (!members_validated)) {
members_type::const_iterator m_iter;
for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
- validate_union_member(*m_iter);
+ validate_union_member( *m_iter);
}
members_validated = true;
}
@@ -100,16 +99,19 @@
validate_union_members();
}
- void set_xsd_all(bool xsd_all) { xsd_all_ = xsd_all; }
+ void set_xsd_all(bool xsd_all) {
+ xsd_all_ = xsd_all;
+ }
- bool get_xsd_all() const { return xsd_all_; }
+ bool get_xsd_all() const {
+ return xsd_all_;
+ }
bool append(t_field* elem) {
typedef members_type::iterator iter_type;
- std::pair<iter_type, iter_type> bounds = std::equal_range(members_in_id_order_.begin(),
- members_in_id_order_.end(),
- elem,
- t_field::key_compare());
+ std::pair<iter_type, iter_type> bounds = std::equal_range(
+ members_in_id_order_.begin(), members_in_id_order_.end(), elem, t_field::key_compare()
+ );
if (bounds.first != bounds.second) {
return false;
}
@@ -119,19 +121,29 @@
}
members_.push_back(elem);
members_in_id_order_.insert(bounds.second, elem);
- validate_union_member(elem);
+ validate_union_member( elem);
return true;
}
- const members_type& get_members() { return members_; }
+ const members_type& get_members() {
+ return members_;
+ }
- const members_type& get_sorted_members() { return members_in_id_order_; }
+ const members_type& get_sorted_members() {
+ return members_in_id_order_;
+ }
- bool is_struct() const { return !is_xception_; }
+ bool is_struct() const {
+ return !is_xception_;
+ }
- bool is_xception() const { return is_xception_; }
+ bool is_xception() const {
+ return is_xception_;
+ }
- bool is_union() const { return is_union_; }
+ bool is_union() const {
+ return is_union_;
+ }
virtual std::string get_fingerprint_material() const {
std::string rv = "{";
@@ -142,9 +154,9 @@
rv += (*m_iter)->get_fingerprint_material();
rv += ";";
- if (do_reserve) {
+ if( do_reserve) {
estimation = members_in_id_order_.size() * rv.size() + 16;
- rv.reserve(estimation);
+ rv.reserve( estimation);
do_reserve = false;
}
}
@@ -170,13 +182,14 @@
return NULL;
}
-private:
+ private:
+
members_type members_;
members_type members_in_id_order_;
bool is_xception_;
bool is_union_;
bool members_validated;
- int members_with_value;
+ int members_with_value;
bool xsd_all_;
};
diff --git a/compiler/cpp/src/parse/t_type.h b/compiler/cpp/src/parse/t_type.h
index 20409f3..b85f2da 100644
--- a/compiler/cpp/src/parse/t_type.h
+++ b/compiler/cpp/src/parse/t_type.h
@@ -37,30 +37,38 @@
*
*/
class t_type : public t_doc {
-public:
+ public:
virtual ~t_type() {}
- virtual void set_name(const std::string& name) { name_ = name; }
+ virtual void set_name(const std::string& name) {
+ name_ = name;
+ }
- virtual const std::string& get_name() const { return name_; }
+ virtual const std::string& get_name() const {
+ return name_;
+ }
- virtual bool is_void() const { return false; }
+ virtual bool is_void() const { return false; }
virtual bool is_base_type() const { return false; }
- virtual bool is_string() const { return false; }
- virtual bool is_bool() const { return false; }
- virtual bool is_typedef() const { return false; }
- virtual bool is_enum() const { return false; }
- virtual bool is_struct() const { return false; }
- virtual bool is_xception() const { return false; }
+ virtual bool is_string() const { return false; }
+ virtual bool is_bool() const { return false; }
+ virtual bool is_typedef() const { return false; }
+ virtual bool is_enum() const { return false; }
+ virtual bool is_struct() const { return false; }
+ virtual bool is_xception() const { return false; }
virtual bool is_container() const { return false; }
- virtual bool is_list() const { return false; }
- virtual bool is_set() const { return false; }
- virtual bool is_map() const { return false; }
- virtual bool is_service() const { return false; }
+ virtual bool is_list() const { return false; }
+ virtual bool is_set() const { return false; }
+ virtual bool is_map() const { return false; }
+ virtual bool is_service() const { return false; }
- t_program* get_program() { return program_; }
+ t_program* get_program() {
+ return program_;
+ }
- const t_program* get_program() const { return program_; }
+ const t_program* get_program() const {
+ return program_;
+ }
t_type* get_true_type();
@@ -87,9 +95,8 @@
return false;
}
- const uint8_t* get_binary_fingerprint() {
- if (!has_fingerprint()) // lazy fingerprint generation, right now only used with the c++
- // generator
+ const uint8_t* get_binary_fingerprint() {
+ if(! has_fingerprint()) // lazy fingerprint generation, right now only used with the c++ generator
generate_fingerprint();
return fingerprint_;
}
@@ -121,16 +128,30 @@
std::map<std::string, std::string> annotations_;
-protected:
- t_type() : program_(NULL) { memset(fingerprint_, 0, sizeof(fingerprint_)); }
-
- t_type(t_program* program) : program_(program) { memset(fingerprint_, 0, sizeof(fingerprint_)); }
-
- t_type(t_program* program, std::string name) : program_(program), name_(name) {
+ protected:
+ t_type() :
+ program_(NULL)
+ {
memset(fingerprint_, 0, sizeof(fingerprint_));
}
- t_type(std::string name) : program_(NULL), name_(name) {
+ t_type(t_program* program) :
+ program_(program)
+ {
+ memset(fingerprint_, 0, sizeof(fingerprint_));
+ }
+
+ t_type(t_program* program, std::string name) :
+ program_(program),
+ name_(name)
+ {
+ memset(fingerprint_, 0, sizeof(fingerprint_));
+ }
+
+ t_type(std::string name) :
+ program_(NULL),
+ name_(name)
+ {
memset(fingerprint_, 0, sizeof(fingerprint_));
}
@@ -140,6 +161,7 @@
uint8_t fingerprint_[fingerprint_len];
};
+
/**
* Placeholder struct for returning the key and value of an annotation
* during parsing.
diff --git a/compiler/cpp/src/parse/t_typedef.h b/compiler/cpp/src/parse/t_typedef.h
index 1051909..57d8df5 100644
--- a/compiler/cpp/src/parse/t_typedef.h
+++ b/compiler/cpp/src/parse/t_typedef.h
@@ -31,31 +31,41 @@
*
*/
class t_typedef : public t_type {
-public:
- t_typedef(t_program* program, t_type* type, const std::string& symbolic)
- : t_type(program, symbolic), type_(type), symbolic_(symbolic), forward_(false), seen_(false) {}
+ public:
+ t_typedef(t_program* program, t_type* type, const std::string& symbolic) :
+ t_type(program, symbolic),
+ type_(type),
+ symbolic_(symbolic),
+ forward_(false),
+ seen_(false) {}
/**
* This constructor is used to refer to a type that is lazily
* resolved at a later time, like for forward declarations or
* recursive types.
*/
- t_typedef(t_program* program, const std::string& symbolic, bool forward)
- : t_type(program, symbolic),
- type_(NULL),
- symbolic_(symbolic),
- forward_(forward),
- seen_(false) {}
+ t_typedef(t_program* program, const std::string& symbolic, bool forward) :
+ t_type(program, symbolic),
+ type_(NULL),
+ symbolic_(symbolic),
+ forward_(forward),
+ seen_(false) {}
~t_typedef() {}
t_type* get_type() const;
- const std::string& get_symbolic() const { return symbolic_; }
+ const std::string& get_symbolic() const {
+ return symbolic_;
+ }
- bool is_forward_typedef() const { return forward_; }
+ bool is_forward_typedef() const {
+ return forward_;
+ }
- bool is_typedef() const { return true; }
+ bool is_typedef() const {
+ return true;
+ }
virtual std::string get_fingerprint_material() const {
if (!seen_) {
@@ -74,7 +84,7 @@
}
}
-private:
+ private:
t_type* type_;
std::string symbolic_;
bool forward_;
diff --git a/compiler/cpp/src/windows/config.h b/compiler/cpp/src/windows/config.h
index a600080..4495672 100644
--- a/compiler/cpp/src/windows/config.h
+++ b/compiler/cpp/src/windows/config.h
@@ -38,8 +38,9 @@
#define PRIi64 "I64d"
// squelch deprecation warnings
-#pragma warning(disable : 4996)
+#pragma warning(disable:4996)
// squelch bool conversion performance warning
-#pragma warning(disable : 4800)
+#pragma warning(disable:4800)
+
#endif // _THRIFT_WINDOWS_CONFIG_H_