David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1 | // Distributed under the Thrift Software License |
| 2 | // |
| 3 | // See accompanying file LICENSE or visit the Thrift site at: |
| 4 | // http://developers.facebook.com/thrift/ |
| 5 | |
David Reiss | 2dfdb2d | 2008-03-27 21:41:27 +0000 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | #include <vector> |
| 10 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sstream> |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 14 | |
David Reiss | 2dfdb2d | 2008-03-27 21:41:27 +0000 | [diff] [blame] | 15 | #include "platform.h" |
| 16 | #include "t_oop_generator.h" |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 17 | using namespace std; |
| 18 | |
David Reiss | 2dfdb2d | 2008-03-27 21:41:27 +0000 | [diff] [blame] | 19 | |
| 20 | class t_csharp_generator : public t_oop_generator |
| 21 | { |
| 22 | public: |
| 23 | t_csharp_generator( |
| 24 | t_program* program, |
| 25 | const std::map<std::string, std::string>& parsed_options, |
| 26 | const std::string& option_string) |
| 27 | : t_oop_generator(program) |
| 28 | { |
| 29 | out_dir_base_ = "gen-csharp"; |
| 30 | } |
| 31 | void init_generator(); |
| 32 | void close_generator(); |
| 33 | |
| 34 | void generate_consts(std::vector<t_const*> consts); |
| 35 | |
| 36 | void generate_typedef (t_typedef* ttypedef); |
| 37 | void generate_enum (t_enum* tenum); |
| 38 | void generate_struct (t_struct* tstruct); |
| 39 | void generate_xception (t_struct* txception); |
| 40 | void generate_service (t_service* tservice); |
| 41 | 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); |
| 42 | std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value); |
| 43 | void print_const_constructor(std::ofstream& out, std::vector<t_const*> consts); |
| 44 | void print_const_def_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value); |
| 45 | |
| 46 | void generate_csharp_struct(t_struct* tstruct, bool is_exception); |
| 47 | void generate_csharp_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false); |
| 48 | void generate_csharp_struct_reader(std::ofstream& out, t_struct* tstruct); |
| 49 | void generate_csharp_struct_result_writer(std::ofstream& out, t_struct* tstruct); |
| 50 | void generate_csharp_struct_writer(std::ofstream& out, t_struct* tstruct); |
| 51 | void generate_csharp_struct_tostring(std::ofstream& out, t_struct* tstruct); |
| 52 | |
| 53 | void generate_function_helpers(t_function* tfunction); |
| 54 | void generate_service_interface (t_service* tservice); |
| 55 | void generate_service_helpers (t_service* tservice); |
| 56 | void generate_service_client (t_service* tservice); |
| 57 | void generate_service_server (t_service* tservice); |
| 58 | void generate_process_function (t_service* tservice, t_function* function); |
| 59 | |
| 60 | void generate_deserialize_field (std::ofstream& out, t_field* tfield, std::string prefix=""); |
| 61 | void generate_deserialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix=""); |
| 62 | void generate_deserialize_container (std::ofstream& out, t_type* ttype, std::string prefix=""); |
| 63 | void generate_deserialize_set_element (std::ofstream& out, t_set* tset, std::string prefix=""); |
| 64 | void generate_deserialize_map_element (std::ofstream& out, t_map* tmap, std::string prefix=""); |
| 65 | void generate_deserialize_list_element (std::ofstream& out, t_list* list, std::string prefix=""); |
| 66 | void generate_serialize_field (std::ofstream& out, t_field* tfield, std::string prefix=""); |
| 67 | void generate_serialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix=""); |
| 68 | void generate_serialize_container (std::ofstream& out, t_type* ttype, std::string prefix=""); |
| 69 | void generate_serialize_map_element (std::ofstream& out, t_map* tmap, std::string iter, std::string map); |
| 70 | void generate_serialize_set_element (std::ofstream& out, t_set* tmap, std::string iter); |
| 71 | void generate_serialize_list_element (std::ofstream& out, t_list* tlist, std::string iter); |
| 72 | |
| 73 | void start_csharp_namespace (std::ofstream& out); |
| 74 | void end_csharp_namespace (std::ofstream& out); |
| 75 | |
| 76 | std::string csharp_type_usings(); |
| 77 | std::string csharp_thrift_usings(); |
| 78 | |
| 79 | std::string type_name(t_type* ttype, bool in_countainer=false, bool in_init=false); |
| 80 | std::string base_type_name(t_base_type* tbase, bool in_container=false); |
| 81 | std::string declare_field(t_field* tfield, bool init=false); |
| 82 | std::string function_signature(t_function* tfunction, std::string prefix=""); |
| 83 | std::string argument_list(t_struct* tstruct); |
| 84 | std::string type_to_enum(t_type* ttype); |
| 85 | |
| 86 | bool type_can_be_null(t_type* ttype) { |
| 87 | while (ttype->is_typedef()) { |
| 88 | ttype = ((t_typedef*)ttype)->get_type(); |
| 89 | } |
| 90 | |
| 91 | return ttype->is_container() || |
| 92 | ttype->is_struct() || |
| 93 | ttype->is_xception() || |
| 94 | ttype->is_string(); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | std::string namespace_name_; |
| 99 | std::ofstream f_service_; |
| 100 | std::string namespace_dir_; |
| 101 | }; |
| 102 | |
| 103 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 104 | void t_csharp_generator::init_generator() { |
| 105 | MKDIR(get_out_dir().c_str()); |
David Reiss | 9d65bf0 | 2008-03-27 21:41:37 +0000 | [diff] [blame] | 106 | namespace_name_ = program_->get_namespace("csharp"); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 107 | |
| 108 | string dir = namespace_name_; |
| 109 | string subdir = get_out_dir().c_str(); |
| 110 | string::size_type loc; |
| 111 | |
| 112 | while ((loc = dir.find(".")) != string::npos) { |
| 113 | subdir = subdir + "/" + dir.substr(0, loc); |
| 114 | MKDIR(subdir.c_str()); |
| 115 | dir = dir.substr(loc + 1); |
| 116 | } |
| 117 | if (dir.size() > 0) { |
| 118 | subdir = subdir + "/" + dir; |
| 119 | MKDIR(subdir.c_str()); |
| 120 | } |
| 121 | |
| 122 | namespace_dir_ = subdir; |
| 123 | } |
| 124 | |
| 125 | void t_csharp_generator::start_csharp_namespace(ofstream& out) { |
| 126 | if (!namespace_name_.empty()) { |
| 127 | out << |
| 128 | "namespace " << namespace_name_ << "\n"; |
| 129 | scope_up(out); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void t_csharp_generator::end_csharp_namespace(ofstream& out) { |
| 134 | if (!namespace_name_.empty()) { |
| 135 | scope_down(out); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | string t_csharp_generator::csharp_type_usings() { |
| 140 | return string() + |
| 141 | "using System;\n" + |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 142 | "using System.Collections;\n" + |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 143 | "using System.Collections.Generic;\n" + |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 144 | "using System.Text;\n" + |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 145 | "using Thrift;\n"; |
| 146 | } |
| 147 | |
| 148 | string t_csharp_generator::csharp_thrift_usings() { |
| 149 | return string() + |
| 150 | "using Thrift.Protocol;\n" + |
| 151 | "using Thrift.Transport;\n"; |
| 152 | } |
| 153 | |
| 154 | void t_csharp_generator::close_generator() { } |
| 155 | void t_csharp_generator::generate_typedef(t_typedef* ttypedef) {} |
| 156 | |
| 157 | void t_csharp_generator::generate_enum(t_enum* tenum) { |
| 158 | string f_enum_name = namespace_dir_+"/" + (tenum->get_name())+".cs"; |
| 159 | ofstream f_enum; |
| 160 | f_enum.open(f_enum_name.c_str()); |
| 161 | |
| 162 | f_enum << |
| 163 | autogen_comment() << endl; |
| 164 | |
| 165 | start_csharp_namespace(f_enum); |
| 166 | |
| 167 | indent(f_enum) << |
| 168 | "public enum " << tenum->get_name() << "\n"; |
| 169 | scope_up(f_enum); |
| 170 | |
| 171 | vector<t_enum_value*> constants = tenum->get_constants(); |
| 172 | vector<t_enum_value*>::iterator c_iter; |
| 173 | int value = -1; |
| 174 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) |
| 175 | { |
| 176 | if ((*c_iter)->has_value()) { |
| 177 | value = (*c_iter)->get_value(); |
| 178 | } else { |
| 179 | ++value; |
| 180 | } |
| 181 | |
| 182 | indent(f_enum) << |
| 183 | (*c_iter)->get_name() << |
| 184 | " = " << value << "," << endl; |
| 185 | } |
| 186 | |
| 187 | scope_down(f_enum); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 188 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 189 | end_csharp_namespace(f_enum); |
| 190 | |
| 191 | f_enum.close(); |
| 192 | } |
| 193 | |
| 194 | void t_csharp_generator::generate_consts(std::vector<t_const*> consts) { |
| 195 | if (consts.empty()){ |
| 196 | return; |
| 197 | } |
| 198 | string f_consts_name = namespace_dir_ + "/Constants.cs"; |
| 199 | ofstream f_consts; |
| 200 | f_consts.open(f_consts_name.c_str()); |
| 201 | |
| 202 | f_consts << |
| 203 | autogen_comment() << |
| 204 | csharp_type_usings() << endl; |
| 205 | |
| 206 | start_csharp_namespace(f_consts); |
| 207 | |
| 208 | indent(f_consts) << |
| 209 | "public class Constants" << endl; |
| 210 | scope_up(f_consts); |
| 211 | |
| 212 | vector<t_const*>::iterator c_iter; |
| 213 | bool need_static_constructor = false; |
| 214 | for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { |
| 215 | if (print_const_value(f_consts, (*c_iter)->get_name(), (*c_iter)->get_type(), (*c_iter)->get_value(), false)) { |
| 216 | need_static_constructor = true; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (need_static_constructor) { |
| 221 | print_const_constructor(f_consts, consts); |
| 222 | } |
| 223 | |
| 224 | scope_down(f_consts); |
| 225 | end_csharp_namespace(f_consts); |
| 226 | f_consts.close(); |
| 227 | } |
| 228 | |
| 229 | void t_csharp_generator::print_const_def_value(std::ofstream& out, string name, t_type* type, t_const_value* value) |
| 230 | { |
| 231 | if (type->is_struct() || type->is_xception()) { |
| 232 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 233 | vector<t_field*>::const_iterator f_iter; |
| 234 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 235 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 236 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 237 | t_type* field_type = NULL; |
| 238 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 239 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 240 | field_type = (*f_iter)->get_type(); |
| 241 | } |
| 242 | } |
| 243 | if (field_type == NULL) { |
| 244 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 245 | } |
| 246 | string val = render_const_value(out, name, field_type, v_iter->second); |
| 247 | indent(out) << name << "." << v_iter->first->get_string() << " = " << val << ";" << endl; |
| 248 | } |
| 249 | } else if (type->is_map()) { |
| 250 | t_type* ktype = ((t_map*)type)->get_key_type(); |
| 251 | t_type* vtype = ((t_map*)type)->get_val_type(); |
| 252 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 253 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 254 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 255 | string key = render_const_value(out, name, ktype, v_iter->first); |
| 256 | string val = render_const_value(out, name, vtype, v_iter->second); |
| 257 | indent(out) << name << "[" << key << "]" << " = " << val << ";" << endl; |
| 258 | } |
| 259 | } else if (type->is_list() || type->is_set()) { |
| 260 | t_type* etype; |
| 261 | if (type->is_list()) { |
| 262 | etype = ((t_list*)type)->get_elem_type(); |
| 263 | } else { |
| 264 | etype = ((t_set*)type)->get_elem_type(); |
| 265 | } |
| 266 | |
| 267 | const vector<t_const_value*>& val = value->get_list(); |
| 268 | vector<t_const_value*>::const_iterator v_iter; |
| 269 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 270 | string val = render_const_value(out, name, etype, *v_iter); |
| 271 | indent(out) << name << ".Add(" << val << ");" << endl; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void t_csharp_generator::print_const_constructor(std::ofstream& out, std::vector<t_const*> consts) { |
| 277 | indent(out) << "static Constants()" << endl; |
| 278 | scope_up(out); |
| 279 | vector<t_const*>::iterator c_iter; |
| 280 | for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { |
| 281 | string name = (*c_iter)->get_name(); |
| 282 | t_type* type = (*c_iter)->get_type(); |
| 283 | t_const_value* value = (*c_iter)->get_value(); |
| 284 | |
| 285 | print_const_def_value(out, name, type, value); |
| 286 | } |
| 287 | scope_down(out); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | //it seems like all that methods that call this are using in_static to be the opposite of what it would imply |
| 292 | 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) { |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 293 | type = get_true_type(type); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 294 | indent(out); |
| 295 | bool need_static_construction = !in_static; |
| 296 | if (!defval || needtype) { |
| 297 | out << |
| 298 | (in_static ? "" : "public static ") << |
| 299 | type_name(type) << " "; |
| 300 | } |
| 301 | if (type->is_base_type()) { |
| 302 | string v2 = render_const_value(out, name, type, value); |
| 303 | out << name << " = " << v2 << ";" << endl; |
| 304 | need_static_construction = false; |
| 305 | } else if (type->is_enum()) { |
| 306 | out << name << " = (" << type_name(type, false, true) << ")" << value->get_integer() << ";" << endl; |
| 307 | need_static_construction = false; |
| 308 | } else if (type->is_struct() || type->is_xception()) { |
| 309 | out << name << " = new " << type_name(type) << "();" << endl; |
| 310 | } else if (type->is_map()) { |
| 311 | out << name << " = new " << type_name(type, true, true) << "();" << endl; |
| 312 | } else if (type->is_list() || type->is_set()) { |
| 313 | out << name << " = new " << type_name(type) << "();" << endl; |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 314 | } else { |
| 315 | throw "compiler error: no const of type " + type->get_name(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (defval && !type->is_base_type() && !type->is_enum()) { |
| 319 | print_const_def_value(out, name, type, value); |
| 320 | } |
| 321 | |
| 322 | return need_static_construction; |
| 323 | } |
| 324 | |
| 325 | std::string t_csharp_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) { |
| 326 | std::ostringstream render; |
| 327 | |
| 328 | if (type->is_base_type()) { |
| 329 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 330 | switch (tbase) { |
| 331 | case t_base_type::TYPE_STRING: |
| 332 | render << "\"" + value->get_string() + "\""; |
| 333 | break; |
| 334 | case t_base_type::TYPE_BOOL: |
| 335 | render << ((value->get_integer() > 0) ? "true" : "false"); |
| 336 | break; |
| 337 | case t_base_type::TYPE_BYTE: |
| 338 | case t_base_type::TYPE_I16: |
| 339 | case t_base_type::TYPE_I32: |
| 340 | case t_base_type::TYPE_I64: |
| 341 | render << value->get_integer(); |
| 342 | break; |
| 343 | case t_base_type::TYPE_DOUBLE: |
| 344 | if (value->get_type() == t_const_value::CV_INTEGER) { |
| 345 | render << value->get_integer(); |
| 346 | } else { |
| 347 | render << value->get_double(); |
| 348 | } |
| 349 | break; |
| 350 | default: |
| 351 | throw "compiler error: no const of base type " + tbase; |
| 352 | } |
| 353 | } else if (type->is_enum()) { |
| 354 | render << "(" << type->get_name() << ")" << value->get_integer(); |
| 355 | } else { |
| 356 | string t = tmp("tmp"); |
| 357 | print_const_value(out, t, type, value, true, true, true); |
| 358 | render << t; |
| 359 | } |
| 360 | |
| 361 | return render.str(); |
| 362 | } |
| 363 | |
| 364 | void t_csharp_generator::generate_struct(t_struct* tstruct) { |
| 365 | generate_csharp_struct(tstruct, false); |
| 366 | } |
| 367 | |
| 368 | void t_csharp_generator::generate_xception(t_struct* txception) { |
| 369 | generate_csharp_struct(txception, true); |
| 370 | } |
| 371 | |
| 372 | void t_csharp_generator::generate_csharp_struct(t_struct* tstruct, bool is_exception) { |
| 373 | string f_struct_name = namespace_dir_ + "/" + (tstruct->get_name()) + ".cs"; |
| 374 | ofstream f_struct; |
| 375 | |
| 376 | f_struct.open(f_struct_name.c_str()); |
| 377 | |
| 378 | f_struct << |
| 379 | autogen_comment() << |
| 380 | csharp_type_usings() << |
| 381 | csharp_thrift_usings(); |
| 382 | |
| 383 | generate_csharp_struct_definition(f_struct, tstruct, is_exception); |
| 384 | |
| 385 | f_struct.close(); |
| 386 | } |
| 387 | |
| 388 | void t_csharp_generator::generate_csharp_struct_definition(ofstream &out, t_struct* tstruct, bool is_exception, bool in_class, bool is_result) { |
| 389 | |
| 390 | if (!in_class) |
| 391 | { |
| 392 | start_csharp_namespace(out); |
| 393 | } |
| 394 | |
| 395 | indent(out) << |
| 396 | "public class " << tstruct->get_name() << " "; |
| 397 | |
| 398 | if (is_exception) { |
| 399 | out << ": Exception "; |
| 400 | } |
| 401 | |
| 402 | out << endl; |
| 403 | |
| 404 | scope_up(out); |
| 405 | |
| 406 | const vector<t_field*>& members = tstruct->get_members(); |
| 407 | vector<t_field*>::const_iterator m_iter; |
| 408 | |
| 409 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 410 | indent(out) << |
| 411 | "public " << declare_field(*m_iter, false) << endl; |
| 412 | } |
| 413 | |
| 414 | if (members.size() > 0) { |
| 415 | out << |
| 416 | endl << |
David Reiss | 46dc629 | 2008-02-06 22:09:58 +0000 | [diff] [blame] | 417 | indent() << "public Isset __isset;" << endl << |
| 418 | indent() << "public struct Isset {" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 419 | indent_up(); |
| 420 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 421 | indent(out) << |
David Reiss | 46dc629 | 2008-02-06 22:09:58 +0000 | [diff] [blame] | 422 | "public bool " << (*m_iter)->get_name() << ";" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | indent_down(); |
| 426 | indent(out) << "}" << endl << endl; |
| 427 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 428 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 429 | indent(out) << |
| 430 | "public " << tstruct->get_name() << "() {" << endl; |
| 431 | indent_up(); |
| 432 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 433 | t_type* t = (*m_iter)->get_type(); |
| 434 | while (t->is_typedef()) { |
| 435 | t = ((t_typedef*)t)->get_type(); |
| 436 | } |
| 437 | if ((*m_iter)->get_value() != NULL) { |
| 438 | print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | indent_down(); |
| 443 | indent(out) << "}" << endl << endl; |
| 444 | |
| 445 | generate_csharp_struct_reader(out, tstruct); |
| 446 | if (is_result) { |
| 447 | generate_csharp_struct_result_writer(out, tstruct); |
| 448 | } else { |
| 449 | generate_csharp_struct_writer(out, tstruct); |
| 450 | } |
| 451 | generate_csharp_struct_tostring(out, tstruct); |
| 452 | scope_down(out); |
| 453 | out << endl; |
| 454 | |
| 455 | if (!in_class) |
| 456 | { |
| 457 | end_csharp_namespace(out); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void t_csharp_generator::generate_csharp_struct_reader(ofstream& out, t_struct* tstruct) { |
| 462 | indent(out) << |
| 463 | "public void Read (TProtocol iprot)" << endl; |
| 464 | scope_up(out); |
| 465 | |
| 466 | const vector<t_field*>& fields = tstruct->get_members(); |
| 467 | vector<t_field*>::const_iterator f_iter; |
| 468 | |
| 469 | indent(out) << |
| 470 | "TField field;" << endl << |
| 471 | indent() << "TStruct struc = iprot.ReadStructBegin();" << endl; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 472 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 473 | indent(out) << |
| 474 | "while (true)" << endl; |
| 475 | scope_up(out); |
| 476 | |
| 477 | indent(out) << |
| 478 | "field = iprot.ReadFieldBegin();" << endl; |
| 479 | |
| 480 | indent(out) << |
| 481 | "if (field.Type == TType.Stop) { " << endl; |
| 482 | indent_up(); |
| 483 | indent(out) << |
| 484 | "break;" << endl; |
| 485 | indent_down(); |
| 486 | indent(out) << |
| 487 | "}" << endl; |
| 488 | |
| 489 | indent(out) << |
| 490 | "switch (field.ID)" << endl; |
| 491 | |
| 492 | scope_up(out); |
| 493 | |
| 494 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 495 | indent(out) << |
| 496 | "case " << (*f_iter)->get_key() << ":" << endl; |
| 497 | indent_up(); |
| 498 | indent(out) << |
| 499 | "if (field.Type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl; |
| 500 | indent_up(); |
| 501 | |
| 502 | generate_deserialize_field(out, *f_iter, "this."); |
| 503 | indent(out) << |
| 504 | "this.__isset." << (*f_iter)->get_name() << " = true;" << endl; |
| 505 | indent_down(); |
| 506 | out << |
| 507 | indent() << "} else { " << endl << |
| 508 | indent() << " TProtocolUtil.Skip(iprot, field.Type);" << endl << |
| 509 | indent() << "}" << endl << |
| 510 | indent() << "break;" << endl; |
| 511 | indent_down(); |
| 512 | } |
| 513 | |
| 514 | indent(out) << |
| 515 | "default: " << endl; |
| 516 | indent_up(); |
| 517 | indent(out) << "TProtocolUtil.Skip(iprot, field.Type);" << endl; |
| 518 | indent(out) << "break;" << endl; |
| 519 | indent_down(); |
| 520 | |
| 521 | scope_down(out); |
| 522 | |
| 523 | indent(out) << |
| 524 | "iprot.ReadFieldEnd();" << endl; |
| 525 | |
| 526 | scope_down(out); |
| 527 | |
| 528 | indent(out) << |
| 529 | "iprot.ReadStructEnd();" << endl; |
| 530 | |
| 531 | indent_down(); |
| 532 | |
| 533 | indent(out) << "}" << endl << endl; |
| 534 | |
| 535 | } |
| 536 | |
| 537 | void t_csharp_generator::generate_csharp_struct_writer(ofstream& out, t_struct* tstruct) { |
| 538 | out << |
| 539 | indent() << "public void Write(TProtocol oprot) {" << endl; |
| 540 | indent_up(); |
| 541 | |
| 542 | string name = tstruct->get_name(); |
| 543 | const vector<t_field*>& fields = tstruct->get_members(); |
| 544 | vector<t_field*>::const_iterator f_iter; |
| 545 | |
| 546 | indent(out) << |
| 547 | "TStruct struc = new TStruct(\"" << name << "\");" << endl; |
| 548 | indent(out) << |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 549 | "oprot.WriteStructBegin(struc);" << endl; |
| 550 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 551 | if (fields.size() > 0) { |
| 552 | indent(out) << "TField field = new TField();" << endl; |
| 553 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 554 | bool null_allowed = type_can_be_null((*f_iter)->get_type()); |
| 555 | if (null_allowed) { |
| 556 | indent(out) << |
| 557 | "if (this." << (*f_iter)->get_name() << " != null) {" << endl; |
| 558 | indent_up(); |
| 559 | } |
| 560 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 561 | indent(out) << |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 562 | "field.Name = \"" << (*f_iter)->get_name() << "\";" << endl; |
| 563 | indent(out) << |
| 564 | "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl; |
| 565 | indent(out) << |
| 566 | "field.ID = " << (*f_iter)->get_key() << ";" << endl; |
| 567 | indent(out) << |
| 568 | "oprot.WriteFieldBegin(field);" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 569 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 570 | generate_serialize_field(out, *f_iter, "this."); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 571 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 572 | indent(out) << |
| 573 | "oprot.WriteFieldEnd();" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 574 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 575 | if (null_allowed) { |
| 576 | indent_down(); |
| 577 | indent(out) << "}" << endl; |
| 578 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | indent(out) << |
| 583 | "oprot.WriteFieldStop();" << endl; |
| 584 | indent(out) << |
| 585 | "oprot.WriteStructEnd();" << endl; |
| 586 | |
| 587 | indent_down(); |
| 588 | |
| 589 | indent(out) << |
| 590 | "}" << endl << endl; |
| 591 | } |
| 592 | |
| 593 | void t_csharp_generator::generate_csharp_struct_result_writer(ofstream& out, t_struct* tstruct) { |
| 594 | indent(out) << |
| 595 | "public void Write(TProtocol oprot) {" << endl; |
| 596 | indent_up(); |
| 597 | |
| 598 | string name = tstruct->get_name(); |
| 599 | const vector<t_field*>& fields = tstruct->get_members(); |
| 600 | vector<t_field*>::const_iterator f_iter; |
| 601 | |
| 602 | indent(out) << |
| 603 | "TStruct struc = new TStruct(\"" << name << "\");" << endl; |
| 604 | indent(out) << |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 605 | "oprot.WriteStructBegin(struc);" << endl; |
| 606 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 607 | if (fields.size() > 0) { |
| 608 | indent(out) << "TField field = new TField();" << endl; |
| 609 | bool first = true; |
| 610 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 611 | if (first) { |
| 612 | first = false; |
| 613 | out << |
| 614 | endl << indent() << "if "; |
| 615 | } else { |
| 616 | out << |
| 617 | " else if "; |
| 618 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 619 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 620 | out << |
| 621 | "(this.__isset." << (*f_iter)->get_name() << ") {" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 622 | indent_up(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 623 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 624 | bool null_allowed = type_can_be_null((*f_iter)->get_type()); |
| 625 | if (null_allowed) { |
| 626 | indent(out) << |
| 627 | "if (this." << (*f_iter)->get_name() << " != null) {" << endl; |
| 628 | indent_up(); |
| 629 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 630 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 631 | indent(out) << |
| 632 | "field.Name = \"" << (*f_iter)->get_name() << "\";" << endl; |
| 633 | indent(out) << |
| 634 | "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl; |
| 635 | indent(out) << |
| 636 | "field.ID = " << (*f_iter)->get_key() << ";" << endl; |
| 637 | indent(out) << |
| 638 | "oprot.WriteFieldBegin(field);" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 639 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 640 | generate_serialize_field(out, *f_iter, "this."); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 641 | |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 642 | indent(out) << |
| 643 | "oprot.WriteFieldEnd();" << endl; |
| 644 | |
| 645 | if (null_allowed) { |
| 646 | indent_down(); |
| 647 | indent(out) << "}" << endl; |
| 648 | } |
| 649 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 650 | indent_down(); |
David Reiss | b79acc9 | 2008-03-18 18:21:58 +0000 | [diff] [blame] | 651 | indent(out) << "}"; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 652 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | out << |
| 656 | endl << |
| 657 | indent() << "oprot.WriteFieldStop();" << endl << |
| 658 | indent() << "oprot.WriteStructEnd();" << endl; |
| 659 | |
| 660 | indent_down(); |
| 661 | |
| 662 | indent(out) << |
| 663 | "}" << endl << endl; |
| 664 | } |
| 665 | |
| 666 | void t_csharp_generator::generate_csharp_struct_tostring(ofstream& out, t_struct* tstruct) { |
| 667 | indent(out) << |
| 668 | "public override string ToString() {" << endl; |
| 669 | indent_up(); |
| 670 | |
| 671 | indent(out) << |
| 672 | "StringBuilder sb = new StringBuilder(\"" << tstruct->get_name() << "(\");" << endl; |
| 673 | |
| 674 | const vector<t_field*>& fields = tstruct->get_members(); |
| 675 | vector<t_field*>::const_iterator f_iter; |
| 676 | |
| 677 | bool first = true; |
| 678 | |
| 679 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 680 | if (first) { |
| 681 | first = false; |
| 682 | indent(out) << |
| 683 | "sb.Append(\"" << (*f_iter)->get_name() << ": \");" << endl; |
| 684 | } else { |
| 685 | indent(out) << |
| 686 | "sb.Append(\"," << (*f_iter)->get_name() << ": \");" << endl; |
| 687 | } |
| 688 | t_type* ttype = (*f_iter)->get_type(); |
| 689 | if (ttype->is_xception() || ttype->is_struct()) { |
| 690 | indent(out) << |
David Reiss | 46dc629 | 2008-02-06 22:09:58 +0000 | [diff] [blame] | 691 | "sb.Append(this." << (*f_iter)->get_name() << "== null ? \"<null>\" : "<< "this." << (*f_iter)->get_name() << ".ToString());" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 692 | } else { |
| 693 | indent(out) << |
| 694 | "sb.Append(this." << (*f_iter)->get_name() << ");" << endl; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | indent(out) << |
| 699 | "sb.Append(\")\");" << endl; |
| 700 | indent(out) << |
| 701 | "return sb.ToString();" << endl; |
| 702 | |
| 703 | indent_down(); |
| 704 | indent(out) << "}" << endl << endl; |
| 705 | } |
| 706 | |
| 707 | void t_csharp_generator::generate_service(t_service* tservice) { |
| 708 | string f_service_name = namespace_dir_ + "/" + service_name_ + ".cs"; |
| 709 | f_service_.open(f_service_name.c_str()); |
| 710 | |
| 711 | f_service_ << |
| 712 | autogen_comment() << |
| 713 | csharp_type_usings() << |
| 714 | csharp_thrift_usings(); |
| 715 | |
| 716 | start_csharp_namespace(f_service_); |
| 717 | |
| 718 | indent(f_service_) << |
| 719 | "public class " << service_name_ << " {" << endl; |
| 720 | indent_up(); |
| 721 | |
| 722 | generate_service_interface(tservice); |
| 723 | generate_service_client(tservice); |
| 724 | generate_service_server(tservice); |
| 725 | generate_service_helpers(tservice); |
| 726 | |
| 727 | indent_down(); |
| 728 | |
| 729 | indent(f_service_) << |
| 730 | "}" << endl; |
| 731 | end_csharp_namespace(f_service_); |
| 732 | f_service_.close(); |
| 733 | } |
| 734 | |
| 735 | void t_csharp_generator::generate_service_interface(t_service* tservice) { |
| 736 | string extends = ""; |
| 737 | string extends_iface = ""; |
| 738 | if (tservice->get_extends() != NULL) { |
| 739 | extends = type_name(tservice->get_extends()); |
| 740 | extends_iface = " : " + extends + ".Iface"; |
| 741 | } |
| 742 | |
| 743 | indent(f_service_) << |
| 744 | "public interface Iface" << extends_iface << " {" << endl; |
| 745 | indent_up(); |
| 746 | vector<t_function*> functions = tservice->get_functions(); |
| 747 | vector<t_function*>::iterator f_iter; |
| 748 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) |
| 749 | { |
| 750 | indent(f_service_) << |
| 751 | function_signature(*f_iter) << ";" << endl; |
| 752 | } |
| 753 | indent_down(); |
| 754 | f_service_ << |
| 755 | indent() << "}" << endl << endl; |
| 756 | } |
| 757 | |
| 758 | void t_csharp_generator::generate_service_helpers(t_service* tservice) { |
| 759 | vector<t_function*> functions = tservice->get_functions(); |
| 760 | vector<t_function*>::iterator f_iter; |
| 761 | |
| 762 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 763 | t_struct* ts = (*f_iter)->get_arglist(); |
| 764 | generate_csharp_struct_definition(f_service_, ts, false, true); |
| 765 | generate_function_helpers(*f_iter); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | void t_csharp_generator::generate_service_client(t_service* tservice) { |
| 770 | string extends = ""; |
| 771 | string extends_client = ""; |
| 772 | if (tservice->get_extends() != NULL) { |
| 773 | extends = type_name(tservice->get_extends()); |
| 774 | extends_client = extends + ".Client, "; |
| 775 | } |
| 776 | |
| 777 | indent(f_service_) << |
| 778 | "public class Client : " << extends_client << "Iface {" << endl; |
| 779 | indent_up(); |
| 780 | indent(f_service_) << |
| 781 | "public Client(TProtocol prot) : this(prot, prot)" << endl; |
| 782 | scope_up(f_service_); |
| 783 | scope_down(f_service_); |
| 784 | f_service_ << endl; |
| 785 | |
| 786 | indent(f_service_) << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 787 | "public Client(TProtocol iprot, TProtocol oprot)"; |
| 788 | if (!extends.empty()) { |
| 789 | f_service_ << " : base(iprot, oprot)"; |
| 790 | } |
| 791 | f_service_ << endl; |
| 792 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 793 | scope_up(f_service_); |
| 794 | if (extends.empty()) { |
| 795 | f_service_ << |
| 796 | indent() << "iprot_ = iprot;" << endl << |
| 797 | indent() << "oprot_ = oprot;" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 798 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 799 | scope_down(f_service_); |
| 800 | |
| 801 | f_service_ << endl; |
| 802 | |
| 803 | if (extends.empty()) { |
| 804 | f_service_ << |
| 805 | indent() << "protected TProtocol iprot_;" << endl << |
| 806 | indent() << "protected TProtocol oprot_;" << endl << |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 807 | indent() << "protected int seqid_;" << endl << endl; |
David Reiss | cdfbeb8 | 2008-03-18 18:22:14 +0000 | [diff] [blame] | 808 | |
| 809 | f_service_ << indent() << "public TProtocol InputProtocol" << endl; |
| 810 | scope_up(f_service_); |
| 811 | indent(f_service_) << "get { return iprot_; }" << endl; |
| 812 | scope_down(f_service_); |
| 813 | |
| 814 | f_service_ << indent() << "public TProtocol OutputProtocol" << endl; |
| 815 | scope_up(f_service_); |
| 816 | indent(f_service_) << "get { return oprot_; }" << endl; |
| 817 | scope_down(f_service_); |
| 818 | f_service_ << endl << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | vector<t_function*> functions = tservice->get_functions(); |
| 822 | vector<t_function*>::const_iterator f_iter; |
| 823 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 824 | string funname = (*f_iter)->get_name(); |
| 825 | |
| 826 | indent(f_service_) << |
| 827 | "public " << function_signature(*f_iter) << endl; |
| 828 | scope_up(f_service_); |
| 829 | indent(f_service_) << |
| 830 | "send_" << funname << "("; |
| 831 | |
| 832 | t_struct* arg_struct = (*f_iter)->get_arglist(); |
| 833 | |
| 834 | const vector<t_field*>& fields = arg_struct->get_members(); |
| 835 | vector<t_field*>::const_iterator fld_iter; |
| 836 | bool first = true; |
| 837 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 838 | if (first) { |
| 839 | first = false; |
| 840 | } else { |
| 841 | f_service_ << ", "; |
| 842 | } |
| 843 | f_service_ << (*fld_iter)->get_name(); |
| 844 | } |
| 845 | f_service_ << ");" << endl; |
| 846 | |
| 847 | if (!(*f_iter)->is_async()) { |
| 848 | f_service_ << indent(); |
| 849 | if (!(*f_iter)->get_returntype()->is_void()) { |
| 850 | f_service_ << "return "; |
| 851 | } |
| 852 | f_service_ << |
| 853 | "recv_" << funname << "();" << endl; |
| 854 | } |
| 855 | scope_down(f_service_); |
| 856 | f_service_ << endl; |
| 857 | |
| 858 | t_function send_function(g_type_void, |
| 859 | string("send_") + (*f_iter)->get_name(), |
| 860 | (*f_iter)->get_arglist()); |
| 861 | |
| 862 | string argsname = (*f_iter)->get_name() + "_args"; |
| 863 | |
| 864 | indent(f_service_) << |
| 865 | "public " << function_signature(&send_function) << endl; |
| 866 | scope_up(f_service_); |
| 867 | |
| 868 | f_service_ << |
| 869 | indent() << "oprot_.WriteMessageBegin(new TMessage(\"" << funname << "\", TMessageType.Call, seqid_));" << endl << |
| 870 | indent() << argsname << " args = new " << argsname << "();" << endl; |
| 871 | |
| 872 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 873 | f_service_ << |
| 874 | indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << ";" << endl; |
| 875 | } |
| 876 | |
| 877 | f_service_ << |
| 878 | indent() << "args.Write(oprot_);" << endl << |
| 879 | indent() << "oprot_.WriteMessageEnd();" << endl << |
| 880 | indent() << "oprot_.Transport.Flush();" << endl; |
| 881 | |
| 882 | scope_down(f_service_); |
| 883 | f_service_ << endl; |
| 884 | |
| 885 | if (!(*f_iter)->is_async()) { |
| 886 | string resultname = (*f_iter)->get_name() + "_result"; |
| 887 | |
| 888 | t_struct noargs(program_); |
| 889 | t_function recv_function((*f_iter)->get_returntype(), |
| 890 | string("recv_") + (*f_iter)->get_name(), |
| 891 | &noargs, |
| 892 | (*f_iter)->get_xceptions()); |
| 893 | indent(f_service_) << |
| 894 | "public " << function_signature(&recv_function) << endl; |
| 895 | scope_up(f_service_); |
| 896 | |
| 897 | f_service_ << |
| 898 | indent() << "TMessage msg = iprot_.ReadMessageBegin();" << endl << |
| 899 | indent() << "if (msg.Type == TMessageType.Exception) {" << endl; |
| 900 | indent_up(); |
| 901 | f_service_ << |
| 902 | indent() << "TApplicationException x = TApplicationException.Read(iprot_);" << endl << |
| 903 | indent() << "iprot_.ReadMessageEnd();" << endl << |
| 904 | indent() << "throw x;" << endl; |
| 905 | indent_down(); |
| 906 | f_service_ << |
| 907 | indent() << "}" << endl << |
| 908 | indent() << resultname << " result = new " << resultname << "();" << endl << |
| 909 | indent() << "result.Read(iprot_);" << endl << |
| 910 | indent() << "iprot_.ReadMessageEnd();" << endl; |
| 911 | |
| 912 | if (!(*f_iter)->get_returntype()->is_void()) { |
| 913 | f_service_ << |
| 914 | indent() << "if (result.__isset.success) {" << endl << |
| 915 | indent() << " return result.success;" << endl << |
| 916 | indent() << "}" << endl; |
| 917 | } |
| 918 | |
| 919 | t_struct *xs = (*f_iter)->get_xceptions(); |
| 920 | |
| 921 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 922 | vector<t_field*>::const_iterator x_iter; |
| 923 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 924 | f_service_ << |
| 925 | indent() << "if (result.__isset." << (*x_iter)->get_name() << ") {" << endl << |
| 926 | indent() << " throw result." << (*x_iter)->get_name() << ";" << endl << |
| 927 | indent() << "}" << endl; |
| 928 | } |
| 929 | |
| 930 | if ((*f_iter)->get_returntype()->is_void()) { |
| 931 | indent(f_service_) << |
| 932 | "return;" << endl; |
| 933 | } else { |
| 934 | f_service_ << |
| 935 | indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl; |
| 936 | } |
| 937 | |
| 938 | scope_down(f_service_); |
| 939 | f_service_ << endl; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | indent_down(); |
| 944 | indent(f_service_) << |
| 945 | "}" << endl; |
| 946 | } |
| 947 | |
| 948 | void t_csharp_generator::generate_service_server(t_service* tservice) { |
| 949 | vector<t_function*> functions = tservice->get_functions(); |
| 950 | vector<t_function*>::iterator f_iter; |
| 951 | |
| 952 | string extends = ""; |
| 953 | string extends_processor = ""; |
| 954 | if (tservice->get_extends() != NULL) { |
| 955 | extends = type_name(tservice->get_extends()); |
| 956 | extends_processor = extends + ".Processor, "; |
| 957 | } |
| 958 | |
| 959 | indent(f_service_) << |
| 960 | "public class Processor : " << extends_processor << "TProcessor {" << endl; |
| 961 | indent_up(); |
| 962 | |
| 963 | indent(f_service_) << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 964 | "public Processor(Iface iface)" ; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 965 | if (!extends.empty()) { |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 966 | f_service_ << " : base(iface)"; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 967 | } |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 968 | f_service_ << endl; |
| 969 | scope_up(f_service_); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 970 | f_service_ << |
| 971 | indent() << "iface_ = iface;" << endl; |
| 972 | |
| 973 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 974 | f_service_ << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 975 | indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = " << (*f_iter)->get_name() << "_Process;" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | scope_down(f_service_); |
| 979 | f_service_ << endl; |
| 980 | |
| 981 | if (extends.empty()) { |
| 982 | f_service_ << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 983 | indent() << "protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | f_service_ << |
| 987 | indent() << "private Iface iface_;" << endl; |
| 988 | |
| 989 | if (extends.empty()) { |
| 990 | f_service_ << |
| 991 | indent() << "protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();" << endl; |
| 992 | } |
| 993 | |
| 994 | f_service_ << endl; |
| 995 | |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 996 | if (extends.empty()) { |
| 997 | indent(f_service_) << |
| 998 | "public bool Process(TProtocol iprot, TProtocol oprot)" << endl; |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | indent(f_service_) << |
| 1003 | "public new bool Process(TProtocol iprot, TProtocol oprot)" << endl; |
| 1004 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1005 | scope_up(f_service_); |
| 1006 | |
| 1007 | f_service_ << |
| 1008 | indent() << "TMessage msg = iprot.ReadMessageBegin();" << endl; |
| 1009 | |
| 1010 | f_service_ << |
| 1011 | indent() << "ProcessFunction fn = processMap_[msg.Name];" << endl << |
| 1012 | indent() << "if (fn == null) {" << endl << |
| 1013 | indent() << " TProtocolUtil.Skip(iprot, TType.Struct);" << endl << |
| 1014 | indent() << " iprot.ReadMessageEnd();" << endl << |
| 1015 | indent() << " TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, \"Invalid method name: '\" + msg.Name + \"'\");" << endl << |
| 1016 | indent() << " oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));" << endl << |
| 1017 | indent() << " x.Write(oprot);" << endl << |
| 1018 | indent() << " oprot.WriteMessageEnd();" << endl << |
| 1019 | indent() << " oprot.Transport.Flush();" << endl << |
| 1020 | indent() << " return true;" << endl << |
| 1021 | indent() << "}" << endl << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 1022 | indent() << "fn(msg.SeqID, iprot, oprot);" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1023 | |
| 1024 | f_service_ << |
| 1025 | indent() << "return true;" << endl; |
| 1026 | |
| 1027 | scope_down(f_service_); |
| 1028 | f_service_ << endl; |
| 1029 | |
| 1030 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) |
| 1031 | { |
| 1032 | generate_process_function(tservice, *f_iter); |
| 1033 | } |
| 1034 | |
| 1035 | indent_down(); |
| 1036 | indent(f_service_) << |
| 1037 | "}" << endl << endl; |
| 1038 | } |
| 1039 | |
| 1040 | void t_csharp_generator::generate_function_helpers(t_function* tfunction) { |
| 1041 | if (tfunction->is_async()) { |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | t_struct result(program_, tfunction->get_name() + "_result"); |
| 1046 | t_field success(tfunction->get_returntype(), "success", 0); |
| 1047 | if (!tfunction->get_returntype()->is_void()) { |
| 1048 | result.append(&success); |
| 1049 | } |
| 1050 | |
| 1051 | t_struct *xs = tfunction->get_xceptions(); |
| 1052 | const vector<t_field*>& fields = xs->get_members(); |
| 1053 | vector<t_field*>::const_iterator f_iter; |
| 1054 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1055 | result.append(*f_iter); |
| 1056 | } |
| 1057 | |
| 1058 | generate_csharp_struct_definition(f_service_, &result, false, true, true); |
| 1059 | } |
| 1060 | |
| 1061 | void t_csharp_generator::generate_process_function(t_service* tservice, t_function* tfunction) { |
| 1062 | indent(f_service_) << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 1063 | "public void " << tfunction->get_name() << "_Process(int seqid, TProtocol iprot, TProtocol oprot)" << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1064 | scope_up(f_service_); |
| 1065 | |
| 1066 | string argsname = tfunction->get_name() + "_args"; |
| 1067 | string resultname = tfunction->get_name() + "_result"; |
| 1068 | |
| 1069 | f_service_ << |
| 1070 | indent() << argsname << " args = new " << argsname << "();" << endl << |
| 1071 | indent() << "args.Read(iprot);" << endl << |
| 1072 | indent() << "iprot.ReadMessageEnd();" << endl; |
| 1073 | |
| 1074 | t_struct* xs = tfunction->get_xceptions(); |
| 1075 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 1076 | vector<t_field*>::const_iterator x_iter; |
| 1077 | |
| 1078 | if (!tfunction->is_async()) { |
| 1079 | f_service_ << |
| 1080 | indent() << resultname << " result = new " << resultname << "();" << endl; |
| 1081 | } |
| 1082 | |
| 1083 | if (xceptions.size() > 0) { |
| 1084 | f_service_ << |
| 1085 | indent() << "try {" << endl; |
| 1086 | indent_up(); |
| 1087 | } |
| 1088 | |
| 1089 | t_struct* arg_struct = tfunction->get_arglist(); |
| 1090 | const std::vector<t_field*>& fields = arg_struct->get_members(); |
| 1091 | vector<t_field*>::const_iterator f_iter; |
| 1092 | |
| 1093 | f_service_ << indent(); |
| 1094 | if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()) { |
| 1095 | f_service_ << "result.success = "; |
| 1096 | } |
| 1097 | f_service_ << |
David Reiss | 8bfba63 | 2008-04-02 22:10:13 +0000 | [diff] [blame] | 1098 | "iface_." << tfunction->get_name() << "("; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1099 | bool first = true; |
| 1100 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1101 | if (first) { |
| 1102 | first = false; |
| 1103 | } else { |
| 1104 | f_service_ << ", "; |
| 1105 | } |
| 1106 | f_service_ << "args." << (*f_iter)->get_name(); |
| 1107 | } |
| 1108 | f_service_ << ");" << endl; |
| 1109 | |
| 1110 | if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()) { |
| 1111 | f_service_ << |
| 1112 | indent() << "result.__isset.success = true;" << endl; |
| 1113 | } |
| 1114 | |
| 1115 | if (!tfunction->is_async() && xceptions.size() > 0) { |
| 1116 | indent_down(); |
| 1117 | f_service_ << indent() << "}"; |
| 1118 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 1119 | f_service_ << " catch (" << (*x_iter)->get_type()->get_name() << " " << (*x_iter)->get_name() << ") {" << endl; |
| 1120 | if (!tfunction->is_async()) { |
| 1121 | indent_up(); |
| 1122 | f_service_ << |
| 1123 | indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl << |
| 1124 | indent() << "result.__isset." << (*x_iter)->get_name() << " = true;" << endl; |
| 1125 | indent_down(); |
| 1126 | f_service_ << indent() << "}"; |
| 1127 | } else { |
| 1128 | f_service_ << "}"; |
| 1129 | } |
| 1130 | } |
| 1131 | f_service_ << endl; |
| 1132 | } |
| 1133 | |
| 1134 | if (tfunction->is_async()) { |
| 1135 | f_service_ << |
| 1136 | indent() << "return;" << endl; |
| 1137 | scope_down(f_service_); |
| 1138 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | f_service_ << |
| 1143 | indent() << "oprot.WriteMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.Reply, seqid)); " << endl << |
| 1144 | indent() << "result.Write(oprot);" << endl << |
| 1145 | indent() << "oprot.WriteMessageEnd();" << endl << |
| 1146 | indent() << "oprot.Transport.Flush();" << endl; |
| 1147 | |
| 1148 | scope_down(f_service_); |
| 1149 | |
| 1150 | f_service_ << endl; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void t_csharp_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) { |
| 1154 | t_type* type = tfield->get_type(); |
| 1155 | while(type->is_typedef()) { |
| 1156 | type = ((t_typedef*)type)->get_type(); |
| 1157 | } |
| 1158 | |
| 1159 | if (type->is_void()) { |
| 1160 | throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); |
| 1161 | } |
| 1162 | |
| 1163 | string name = prefix + tfield->get_name(); |
| 1164 | |
| 1165 | if (type->is_struct() || type->is_xception()) { |
| 1166 | generate_deserialize_struct(out, (t_struct*)type, name); |
| 1167 | } else if (type->is_container()) { |
| 1168 | generate_deserialize_container(out, type, name); |
| 1169 | } else if (type->is_base_type() || type->is_enum()) { |
| 1170 | indent(out) << |
| 1171 | name << " = "; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1172 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1173 | if (type->is_enum()) |
| 1174 | { |
| 1175 | out << "(" << type_name(type, false, true) << ")"; |
| 1176 | } |
| 1177 | |
| 1178 | out << "iprot."; |
| 1179 | |
| 1180 | if (type->is_base_type()) { |
| 1181 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1182 | switch (tbase) { |
| 1183 | case t_base_type::TYPE_VOID: |
| 1184 | throw "compiler error: cannot serialize void field in a struct: " + name; |
| 1185 | break; |
| 1186 | case t_base_type::TYPE_STRING: |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 1187 | if (((t_base_type*)type)->is_binary()) { |
| 1188 | out << "ReadBinary();"; |
| 1189 | } else { |
| 1190 | out << "ReadString();"; |
| 1191 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | case t_base_type::TYPE_BOOL: |
| 1194 | out << "ReadBool();"; |
| 1195 | break; |
| 1196 | case t_base_type::TYPE_BYTE: |
| 1197 | out << "ReadByte();"; |
| 1198 | break; |
| 1199 | case t_base_type::TYPE_I16: |
| 1200 | out << "ReadI16();"; |
| 1201 | break; |
| 1202 | case t_base_type::TYPE_I32: |
| 1203 | out << "ReadI32();"; |
| 1204 | break; |
| 1205 | case t_base_type::TYPE_I64: |
| 1206 | out << "ReadI64();"; |
| 1207 | break; |
| 1208 | case t_base_type::TYPE_DOUBLE: |
| 1209 | out << "ReadDouble();"; |
| 1210 | break; |
| 1211 | default: |
| 1212 | throw "compiler error: no C# name for base type " + tbase; |
| 1213 | } |
| 1214 | } else if (type->is_enum()) { |
| 1215 | out << "ReadI32();"; |
| 1216 | } |
| 1217 | out << endl; |
| 1218 | } else { |
| 1219 | printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", tfield->get_name().c_str(), type_name(type).c_str()); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | void t_csharp_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) { |
| 1224 | out << |
| 1225 | indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << |
| 1226 | indent() << prefix << ".Read(iprot);" << endl; |
| 1227 | } |
| 1228 | |
| 1229 | void t_csharp_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) { |
| 1230 | scope_up(out); |
| 1231 | |
| 1232 | string obj; |
| 1233 | |
| 1234 | if (ttype->is_map()) { |
| 1235 | obj = tmp("_map"); |
| 1236 | } else if (ttype->is_set()) { |
| 1237 | obj = tmp("_set"); |
| 1238 | } else if (ttype->is_list()) { |
| 1239 | obj = tmp("_list"); |
| 1240 | } |
| 1241 | |
| 1242 | indent(out) << |
| 1243 | prefix << " = new " << type_name(ttype, false, true) << "();" <<endl; |
| 1244 | if (ttype->is_map()) { |
| 1245 | out << |
| 1246 | indent() << "TMap " << obj << " = iprot.ReadMapBegin();" << endl; |
| 1247 | } else if (ttype->is_set()) { |
| 1248 | out << |
| 1249 | indent() << "TSet " << obj << " = iprot.ReadSetBegin();" << endl; |
| 1250 | } else if (ttype->is_list()) { |
| 1251 | out << |
| 1252 | indent() << "TList " << obj << " = iprot.ReadListBegin();" << endl; |
| 1253 | } |
| 1254 | |
| 1255 | string i = tmp("_i"); |
| 1256 | indent(out) << |
| 1257 | "for( int " << i << " = 0; " << i << " < " << obj << ".Count" << "; " << "++" << i << ")" << endl; |
| 1258 | scope_up(out); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 1259 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1260 | if (ttype->is_map()) { |
| 1261 | generate_deserialize_map_element(out, (t_map*)ttype, prefix); |
| 1262 | } else if (ttype->is_set()) { |
| 1263 | generate_deserialize_set_element(out, (t_set*)ttype, prefix); |
| 1264 | } else if (ttype->is_list()) { |
| 1265 | generate_deserialize_list_element(out, (t_list*)ttype, prefix); |
| 1266 | } |
| 1267 | |
| 1268 | scope_down(out); |
| 1269 | |
| 1270 | if (ttype->is_map()) { |
| 1271 | indent(out) << "iprot.ReadMapEnd();" << endl; |
| 1272 | } else if (ttype->is_set()) { |
| 1273 | indent(out) << "iprot.ReadSetEnd();" << endl; |
| 1274 | } else if (ttype->is_list()) { |
| 1275 | indent(out) << "iprot.ReadListEnd();" << endl; |
| 1276 | } |
| 1277 | |
| 1278 | scope_down(out); |
| 1279 | } |
| 1280 | |
| 1281 | void t_csharp_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) { |
| 1282 | string key = tmp("_key"); |
| 1283 | string val = tmp("_val"); |
| 1284 | |
| 1285 | t_field fkey(tmap->get_key_type(), key); |
| 1286 | t_field fval(tmap->get_val_type(), val); |
| 1287 | |
| 1288 | indent(out) << |
| 1289 | declare_field(&fkey) << endl; |
| 1290 | indent(out) << |
| 1291 | declare_field(&fval) << endl; |
| 1292 | |
| 1293 | generate_deserialize_field(out, &fkey); |
| 1294 | generate_deserialize_field(out, &fval); |
| 1295 | |
| 1296 | indent(out) << |
| 1297 | prefix << "[" << key << "] = " << val << ";" << endl; |
| 1298 | } |
| 1299 | |
| 1300 | void t_csharp_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) { |
| 1301 | string elem = tmp("_elem"); |
| 1302 | t_field felem(tset->get_elem_type(), elem); |
| 1303 | |
| 1304 | indent(out) << |
| 1305 | declare_field(&felem, true) << endl; |
| 1306 | |
| 1307 | generate_deserialize_field(out, &felem); |
| 1308 | |
| 1309 | indent(out) << |
| 1310 | prefix << ".Add(" << elem << ");" << endl; |
| 1311 | } |
| 1312 | |
| 1313 | void t_csharp_generator::generate_deserialize_list_element(ofstream& out, t_list* tlist, string prefix) { |
| 1314 | string elem = tmp("_elem"); |
| 1315 | t_field felem(tlist->get_elem_type(), elem); |
| 1316 | |
| 1317 | indent(out) << |
| 1318 | declare_field(&felem, true) << endl; |
| 1319 | |
| 1320 | generate_deserialize_field(out, &felem); |
| 1321 | |
| 1322 | indent(out) << |
| 1323 | prefix << ".Add(" << elem << ");" << endl; |
| 1324 | } |
| 1325 | |
| 1326 | void t_csharp_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) { |
| 1327 | t_type* type = tfield->get_type(); |
| 1328 | while (type->is_typedef()) { |
| 1329 | type = ((t_typedef*)type)->get_type(); |
| 1330 | } |
| 1331 | |
| 1332 | if (type->is_void()) { |
| 1333 | throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); |
| 1334 | } |
| 1335 | |
| 1336 | if (type->is_struct() || type->is_xception()) { |
| 1337 | generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name()); |
| 1338 | } else if (type->is_container()) { |
| 1339 | generate_serialize_container(out, type, prefix + tfield->get_name()); |
| 1340 | } else if (type->is_base_type() || type->is_enum()) { |
| 1341 | string name = prefix + tfield->get_name(); |
| 1342 | indent(out) << |
| 1343 | "oprot."; |
| 1344 | |
| 1345 | if (type->is_base_type()) { |
| 1346 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1347 | |
| 1348 | switch(tbase) { |
| 1349 | case t_base_type::TYPE_VOID: |
| 1350 | throw "compiler error: cannot serialize void field in a struct: " + name; |
| 1351 | break; |
| 1352 | case t_base_type::TYPE_STRING: |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 1353 | if (((t_base_type*)type)->is_binary()) { |
| 1354 | out << "WriteBinary("; |
| 1355 | } else { |
| 1356 | out << "WriteString("; |
| 1357 | } |
| 1358 | out << name << ");"; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1359 | break; |
| 1360 | case t_base_type::TYPE_BOOL: |
| 1361 | out << "WriteBool(" << name << ");"; |
| 1362 | break; |
| 1363 | case t_base_type::TYPE_BYTE: |
| 1364 | out << "WriteByte(" << name << ");"; |
| 1365 | break; |
| 1366 | case t_base_type::TYPE_I16: |
| 1367 | out << "WriteI16(" << name << ");"; |
| 1368 | break; |
| 1369 | case t_base_type::TYPE_I32: |
| 1370 | out << "WriteI32(" << name << ");"; |
| 1371 | break; |
| 1372 | case t_base_type::TYPE_I64: |
| 1373 | out << "WriteI64(" << name << ");"; |
| 1374 | break; |
| 1375 | case t_base_type::TYPE_DOUBLE: |
| 1376 | out << "WriteDouble(" << name << ");"; |
| 1377 | break; |
| 1378 | default: |
| 1379 | throw "compiler error: no C# name for base type " + tbase; |
| 1380 | } |
| 1381 | } else if (type->is_enum()) { |
| 1382 | out << "WriteI32((int)" << name << ");"; |
| 1383 | } |
| 1384 | out << endl; |
| 1385 | } else { |
| 1386 | printf("DO NOT KNOW HOW TO SERIALIZE '%s%s' TYPE '%s'\n", |
| 1387 | prefix.c_str(), |
| 1388 | tfield->get_name().c_str(), |
| 1389 | type_name(type).c_str()); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | void t_csharp_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) { |
| 1394 | out << |
| 1395 | indent() << prefix << ".Write(oprot);" << endl; |
| 1396 | } |
| 1397 | |
| 1398 | void t_csharp_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) { |
| 1399 | scope_up(out); |
| 1400 | |
| 1401 | if (ttype->is_map()) { |
| 1402 | indent(out) << |
| 1403 | "oprot.WriteMapBegin(new TMap(" << |
| 1404 | type_to_enum(((t_map*)ttype)->get_key_type()) << ", " << |
| 1405 | type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << |
| 1406 | prefix << ".Count));" << endl; |
| 1407 | } else if (ttype->is_set()) { |
| 1408 | indent(out) << |
| 1409 | "oprot.WriteSetBegin(new TSet(" << |
| 1410 | type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " << |
| 1411 | prefix << ".Count));" << endl; |
| 1412 | } else if (ttype->is_list()) { |
| 1413 | indent(out) << |
| 1414 | "oprot.WriteListBegin(new TList(" << |
| 1415 | type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << |
| 1416 | prefix << ".Count));" << endl; |
| 1417 | } |
| 1418 | |
| 1419 | string iter = tmp("_iter"); |
| 1420 | if (ttype->is_map()) { |
| 1421 | indent(out) << |
| 1422 | "foreach (" << |
| 1423 | type_name(((t_map*)ttype)->get_key_type()) << " " << iter << |
| 1424 | " in " << |
| 1425 | prefix << ".Keys)"; |
| 1426 | } else if (ttype->is_set()) { |
| 1427 | indent(out) << |
| 1428 | "foreach (" << |
| 1429 | type_name(((t_set*)ttype)->get_elem_type()) << " " << iter << |
| 1430 | " in " << |
| 1431 | prefix << ")"; |
| 1432 | } else if (ttype->is_list()) { |
| 1433 | indent(out) << |
| 1434 | "foreach (" << |
| 1435 | type_name(((t_list*)ttype)->get_elem_type()) << " " << iter << |
| 1436 | " in " << |
| 1437 | prefix << ")"; |
| 1438 | } |
| 1439 | |
| 1440 | out << endl; |
| 1441 | scope_up(out); |
| 1442 | |
| 1443 | if (ttype->is_map()) { |
| 1444 | generate_serialize_map_element(out, (t_map*)ttype, iter, prefix); |
| 1445 | } else if (ttype->is_set()) { |
| 1446 | generate_serialize_set_element(out, (t_set*)ttype, iter); |
| 1447 | } else if (ttype->is_list()) { |
| 1448 | generate_serialize_list_element(out, (t_list*)ttype, iter); |
| 1449 | } |
| 1450 | |
| 1451 | if (ttype->is_map()) { |
| 1452 | indent(out) << "oprot.WriteMapEnd();" << endl; |
| 1453 | } else if (ttype->is_set()) { |
| 1454 | indent(out) << "oprot.WriteSetEnd();" << endl; |
| 1455 | } else if (ttype->is_list()) { |
| 1456 | indent(out) << "oprot.WriteListEnd();" << endl; |
| 1457 | } |
| 1458 | |
| 1459 | scope_down(out); |
| 1460 | scope_down(out); |
| 1461 | } |
| 1462 | |
| 1463 | void t_csharp_generator::generate_serialize_map_element(ofstream& out, t_map* tmap, string iter, string map) { |
| 1464 | t_field kfield(tmap->get_key_type(), iter); |
| 1465 | generate_serialize_field(out, &kfield, ""); |
| 1466 | t_field vfield(tmap->get_val_type(), map + "[" + iter + "]"); |
| 1467 | generate_serialize_field(out, &vfield, ""); |
| 1468 | } |
| 1469 | |
| 1470 | void t_csharp_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) { |
| 1471 | t_field efield(tset->get_elem_type(), iter); |
| 1472 | generate_serialize_field(out, &efield, ""); |
| 1473 | } |
| 1474 | |
| 1475 | void t_csharp_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) { |
| 1476 | t_field efield(tlist->get_elem_type(), iter); |
| 1477 | generate_serialize_field(out, &efield, ""); |
| 1478 | } |
| 1479 | |
| 1480 | string t_csharp_generator::type_name(t_type* ttype, bool in_container, bool in_init) { |
| 1481 | while (ttype->is_typedef()) { |
| 1482 | ttype = ((t_typedef*)ttype)->get_type(); |
| 1483 | } |
| 1484 | |
| 1485 | if (ttype->is_base_type()) { |
David Reiss | 46dc629 | 2008-02-06 22:09:58 +0000 | [diff] [blame] | 1486 | return base_type_name((t_base_type*)ttype, in_container); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1487 | } else if (ttype->is_map()) { |
| 1488 | t_map *tmap = (t_map*) ttype; |
| 1489 | return "Dictionary<" + type_name(tmap->get_key_type(), true) + |
| 1490 | ", " + type_name(tmap->get_val_type(), true) + ">"; |
| 1491 | } else if (ttype->is_set()) { |
| 1492 | t_set* tset = (t_set*) ttype; |
| 1493 | return "HashSet<" + type_name(tset->get_elem_type(), true) + ">"; |
| 1494 | } else if (ttype->is_list()) { |
| 1495 | t_list* tlist = (t_list*) ttype; |
| 1496 | return "List<" + type_name(tlist->get_elem_type(), true) + ">"; |
| 1497 | } |
| 1498 | |
| 1499 | t_program* program = ttype->get_program(); |
| 1500 | if (program != NULL && program != program_) { |
David Reiss | 9d65bf0 | 2008-03-27 21:41:37 +0000 | [diff] [blame] | 1501 | string ns = program->get_namespace("csharp"); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1502 | if (!ns.empty()) { |
| 1503 | return ns + "." + ttype->get_name(); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | return ttype->get_name(); |
| 1508 | } |
| 1509 | |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 1510 | string t_csharp_generator::base_type_name(t_base_type* tbase, bool in_container) { |
| 1511 | switch (tbase->get_base()) { |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1512 | case t_base_type::TYPE_VOID: |
| 1513 | return "void"; |
| 1514 | case t_base_type::TYPE_STRING: |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 1515 | if (tbase->is_binary()) { |
| 1516 | return "byte[]"; |
| 1517 | } else { |
| 1518 | return "string"; |
| 1519 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1520 | case t_base_type::TYPE_BOOL: |
| 1521 | return "bool"; |
| 1522 | case t_base_type::TYPE_BYTE: |
| 1523 | return "byte"; |
| 1524 | case t_base_type::TYPE_I16: |
| 1525 | return "short"; |
| 1526 | case t_base_type::TYPE_I32: |
| 1527 | return "int"; |
| 1528 | case t_base_type::TYPE_I64: |
| 1529 | return "long"; |
| 1530 | case t_base_type::TYPE_DOUBLE: |
| 1531 | return "double"; |
| 1532 | default: |
David Reiss | cba5727 | 2008-02-06 22:09:44 +0000 | [diff] [blame] | 1533 | throw "compiler error: no C# name for base type " + tbase->get_base(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | string t_csharp_generator::declare_field(t_field* tfield, bool init) { |
| 1538 | string result = type_name(tfield->get_type()) + " " + tfield->get_name(); |
| 1539 | if (init) { |
| 1540 | t_type* ttype = tfield->get_type(); |
| 1541 | while (ttype->is_typedef()) { |
| 1542 | ttype = ((t_typedef*)ttype)->get_type(); |
| 1543 | } |
| 1544 | if (ttype->is_base_type() && tfield->get_value() != NULL) { |
| 1545 | ofstream dummy; |
| 1546 | result += " = " + render_const_value(dummy, tfield->get_name(), ttype, tfield->get_value()); |
| 1547 | } else if (ttype->is_base_type()) { |
| 1548 | t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base(); |
| 1549 | switch (tbase) { |
| 1550 | case t_base_type::TYPE_VOID: |
| 1551 | throw "NO T_VOID CONSTRUCT"; |
| 1552 | case t_base_type::TYPE_STRING: |
| 1553 | result += " = null"; |
| 1554 | break; |
| 1555 | case t_base_type::TYPE_BOOL: |
| 1556 | result += " = false"; |
| 1557 | break; |
| 1558 | case t_base_type::TYPE_BYTE: |
| 1559 | case t_base_type::TYPE_I16: |
| 1560 | case t_base_type::TYPE_I32: |
| 1561 | case t_base_type::TYPE_I64: |
| 1562 | result += " = 0"; |
| 1563 | break; |
| 1564 | case t_base_type::TYPE_DOUBLE: |
| 1565 | result += " = (double)0"; |
| 1566 | break; |
| 1567 | } |
| 1568 | } else if (ttype->is_enum()) { |
| 1569 | result += " = (" + type_name(ttype, false, true) + ")0"; |
| 1570 | } else if (ttype->is_container()) { |
| 1571 | result += " = new " + type_name(ttype, false, true) + "()"; |
| 1572 | } else { |
| 1573 | result += " = new " + type_name(ttype, false, true) + "()"; |
| 1574 | } |
| 1575 | } |
| 1576 | return result + ";"; |
| 1577 | } |
| 1578 | |
| 1579 | string t_csharp_generator::function_signature(t_function* tfunction, string prefix) { |
| 1580 | t_type* ttype = tfunction->get_returntype(); |
| 1581 | return type_name(ttype) + " " + prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ")"; |
| 1582 | } |
| 1583 | |
| 1584 | string t_csharp_generator::argument_list(t_struct* tstruct) { |
| 1585 | string result = ""; |
| 1586 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1587 | vector<t_field*>::const_iterator f_iter; |
| 1588 | bool first = true; |
| 1589 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1590 | if (first) { |
| 1591 | first = false; |
| 1592 | } else { |
| 1593 | result += ", "; |
| 1594 | } |
| 1595 | result += type_name((*f_iter)->get_type()) + " " + (*f_iter)->get_name(); |
| 1596 | } |
| 1597 | return result; |
| 1598 | } |
| 1599 | |
| 1600 | string t_csharp_generator::type_to_enum(t_type* type) { |
| 1601 | while (type->is_typedef()) { |
| 1602 | type = ((t_typedef*)type)->get_type(); |
| 1603 | } |
| 1604 | |
| 1605 | if (type->is_base_type()) { |
| 1606 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 1607 | switch (tbase) { |
| 1608 | case t_base_type::TYPE_VOID: |
| 1609 | throw "NO T_VOID CONSTRUCT"; |
| 1610 | case t_base_type::TYPE_STRING: |
| 1611 | return "TType.String"; |
| 1612 | case t_base_type::TYPE_BOOL: |
| 1613 | return "TType.Bool"; |
| 1614 | case t_base_type::TYPE_BYTE: |
| 1615 | return "TType.Byte"; |
| 1616 | case t_base_type::TYPE_I16: |
| 1617 | return "TType.I16"; |
| 1618 | case t_base_type::TYPE_I32: |
| 1619 | return "TType.I32"; |
| 1620 | case t_base_type::TYPE_I64: |
| 1621 | return "TType.I64"; |
| 1622 | case t_base_type::TYPE_DOUBLE: |
| 1623 | return "TType.Double"; |
| 1624 | } |
| 1625 | } else if (type->is_enum()) { |
| 1626 | return "TType.I32"; |
| 1627 | } else if (type->is_struct() || type->is_xception()) { |
| 1628 | return "TType.Struct"; |
| 1629 | } else if (type->is_map()) { |
| 1630 | return "TType.Map"; |
| 1631 | } else if (type->is_set()) { |
| 1632 | return "TType.Set"; |
| 1633 | } else if (type->is_list()) { |
| 1634 | return "TType.List"; |
| 1635 | } |
| 1636 | |
| 1637 | throw "INVALID TYPE IN type_to_enum: " + type->get_name(); |
| 1638 | } |
David Reiss | 861869b | 2008-03-27 21:41:23 +0000 | [diff] [blame] | 1639 | |
| 1640 | |
| 1641 | THRIFT_REGISTER_GENERATOR(csharp, "C#", ""); |