blob: 93a39f97e0003709032dc63279aebfda9dc60ed7 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
David Reissea2cba82009-03-30 21:35:00 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
David Reiss2dfdb2d2008-03-27 21:41:27 +000024#include <string>
25#include <fstream>
26#include <iostream>
27#include <vector>
28
David Reiss7f42bcf2008-01-11 20:59:12 +000029#include <stdlib.h>
30#include <sys/stat.h>
31#include <sstream>
David Reiss7f42bcf2008-01-11 20:59:12 +000032
David Reiss2dfdb2d2008-03-27 21:41:27 +000033#include "platform.h"
34#include "t_oop_generator.h"
David Reiss7f42bcf2008-01-11 20:59:12 +000035using namespace std;
36
David Reiss2dfdb2d2008-03-27 21:41:27 +000037
38class t_csharp_generator : public t_oop_generator
39{
40 public:
41 t_csharp_generator(
42 t_program* program,
43 const std::map<std::string, std::string>& parsed_options,
44 const std::string& option_string)
45 : t_oop_generator(program)
46 {
Roger Meier3b771a12010-11-17 22:11:26 +000047 (void) parsed_options;
48 (void) option_string;
David Reiss2dfdb2d2008-03-27 21:41:27 +000049 out_dir_base_ = "gen-csharp";
50 }
51 void init_generator();
52 void close_generator();
53
54 void generate_consts(std::vector<t_const*> consts);
55
56 void generate_typedef (t_typedef* ttypedef);
57 void generate_enum (t_enum* tenum);
58 void generate_struct (t_struct* tstruct);
59 void generate_xception (t_struct* txception);
60 void generate_service (t_service* tservice);
David Reiss63191332009-01-06 19:49:22 +000061 void generate_property(ofstream& out, t_field* tfield, bool isPublic);
Bryan Duxburyc43ec622010-09-02 14:58:51 +000062 void generate_csharp_property(ofstream& out, t_field* tfield, bool isPublic, std::string fieldPrefix = "");
David Reiss2dfdb2d2008-03-27 21:41:27 +000063 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);
64 std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
65 void print_const_constructor(std::ofstream& out, std::vector<t_const*> consts);
66 void print_const_def_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
67
68 void generate_csharp_struct(t_struct* tstruct, bool is_exception);
69 void generate_csharp_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
70 void generate_csharp_struct_reader(std::ofstream& out, t_struct* tstruct);
71 void generate_csharp_struct_result_writer(std::ofstream& out, t_struct* tstruct);
72 void generate_csharp_struct_writer(std::ofstream& out, t_struct* tstruct);
73 void generate_csharp_struct_tostring(std::ofstream& out, t_struct* tstruct);
74
75 void generate_function_helpers(t_function* tfunction);
76 void generate_service_interface (t_service* tservice);
77 void generate_service_helpers (t_service* tservice);
78 void generate_service_client (t_service* tservice);
79 void generate_service_server (t_service* tservice);
80 void generate_process_function (t_service* tservice, t_function* function);
81
82 void generate_deserialize_field (std::ofstream& out, t_field* tfield, std::string prefix="");
83 void generate_deserialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix="");
84 void generate_deserialize_container (std::ofstream& out, t_type* ttype, std::string prefix="");
85 void generate_deserialize_set_element (std::ofstream& out, t_set* tset, std::string prefix="");
86 void generate_deserialize_map_element (std::ofstream& out, t_map* tmap, std::string prefix="");
87 void generate_deserialize_list_element (std::ofstream& out, t_list* list, std::string prefix="");
88 void generate_serialize_field (std::ofstream& out, t_field* tfield, std::string prefix="");
89 void generate_serialize_struct (std::ofstream& out, t_struct* tstruct, std::string prefix="");
90 void generate_serialize_container (std::ofstream& out, t_type* ttype, std::string prefix="");
91 void generate_serialize_map_element (std::ofstream& out, t_map* tmap, std::string iter, std::string map);
92 void generate_serialize_set_element (std::ofstream& out, t_set* tmap, std::string iter);
93 void generate_serialize_list_element (std::ofstream& out, t_list* tlist, std::string iter);
94
95 void start_csharp_namespace (std::ofstream& out);
96 void end_csharp_namespace (std::ofstream& out);
97
98 std::string csharp_type_usings();
99 std::string csharp_thrift_usings();
100
101 std::string type_name(t_type* ttype, bool in_countainer=false, bool in_init=false);
102 std::string base_type_name(t_base_type* tbase, bool in_container=false);
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000103 std::string declare_field(t_field* tfield, bool init=false, std::string prefix="");
David Reiss2dfdb2d2008-03-27 21:41:27 +0000104 std::string function_signature(t_function* tfunction, std::string prefix="");
105 std::string argument_list(t_struct* tstruct);
106 std::string type_to_enum(t_type* ttype);
David Reiss63191332009-01-06 19:49:22 +0000107 std::string prop_name(t_field* tfield);
David Reiss2dfdb2d2008-03-27 21:41:27 +0000108
109 bool type_can_be_null(t_type* ttype) {
110 while (ttype->is_typedef()) {
111 ttype = ((t_typedef*)ttype)->get_type();
112 }
113
114 return ttype->is_container() ||
115 ttype->is_struct() ||
116 ttype->is_xception() ||
117 ttype->is_string();
118 }
119
120 private:
121 std::string namespace_name_;
122 std::ofstream f_service_;
123 std::string namespace_dir_;
124};
125
126
David Reiss7f42bcf2008-01-11 20:59:12 +0000127void t_csharp_generator::init_generator() {
128 MKDIR(get_out_dir().c_str());
David Reiss9d65bf02008-03-27 21:41:37 +0000129 namespace_name_ = program_->get_namespace("csharp");
David Reiss7f42bcf2008-01-11 20:59:12 +0000130
131 string dir = namespace_name_;
132 string subdir = get_out_dir().c_str();
133 string::size_type loc;
134
135 while ((loc = dir.find(".")) != string::npos) {
136 subdir = subdir + "/" + dir.substr(0, loc);
137 MKDIR(subdir.c_str());
138 dir = dir.substr(loc + 1);
139 }
140 if (dir.size() > 0) {
141 subdir = subdir + "/" + dir;
142 MKDIR(subdir.c_str());
143 }
144
145 namespace_dir_ = subdir;
146}
147
148void t_csharp_generator::start_csharp_namespace(ofstream& out) {
149 if (!namespace_name_.empty()) {
150 out <<
151 "namespace " << namespace_name_ << "\n";
152 scope_up(out);
153 }
154}
155
156void t_csharp_generator::end_csharp_namespace(ofstream& out) {
157 if (!namespace_name_.empty()) {
158 scope_down(out);
159 }
160}
161
162string t_csharp_generator::csharp_type_usings() {
163 return string() +
164 "using System;\n" +
David Reiss0c90f6f2008-02-06 22:18:40 +0000165 "using System.Collections;\n" +
David Reiss7f42bcf2008-01-11 20:59:12 +0000166 "using System.Collections.Generic;\n" +
David Reiss0c90f6f2008-02-06 22:18:40 +0000167 "using System.Text;\n" +
David Reiss63191332009-01-06 19:49:22 +0000168 "using System.IO;\n" +
David Reissd831a212009-02-13 03:09:52 +0000169 "using Thrift;\n" +
170 "using Thrift.Collections;\n";
David Reiss7f42bcf2008-01-11 20:59:12 +0000171}
172
173string t_csharp_generator::csharp_thrift_usings() {
174 return string() +
175 "using Thrift.Protocol;\n" +
176 "using Thrift.Transport;\n";
177}
178
179void t_csharp_generator::close_generator() { }
Roger Meier3b771a12010-11-17 22:11:26 +0000180void t_csharp_generator::generate_typedef(t_typedef* ttypedef) {
181 (void) ttypedef;
182}
David Reiss7f42bcf2008-01-11 20:59:12 +0000183
184void t_csharp_generator::generate_enum(t_enum* tenum) {
185 string f_enum_name = namespace_dir_+"/" + (tenum->get_name())+".cs";
186 ofstream f_enum;
187 f_enum.open(f_enum_name.c_str());
188
189 f_enum <<
190 autogen_comment() << endl;
191
192 start_csharp_namespace(f_enum);
193
194 indent(f_enum) <<
195 "public enum " << tenum->get_name() << "\n";
196 scope_up(f_enum);
197
198 vector<t_enum_value*> constants = tenum->get_constants();
199 vector<t_enum_value*>::iterator c_iter;
Bryan Duxburya406b902010-09-27 23:37:44 +0000200 for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
201 int value = (*c_iter)->get_value();
202 indent(f_enum) << (*c_iter)->get_name() << " = " << value << "," << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000203 }
204
205 scope_down(f_enum);
David Reiss0c90f6f2008-02-06 22:18:40 +0000206
David Reiss7f42bcf2008-01-11 20:59:12 +0000207 end_csharp_namespace(f_enum);
208
209 f_enum.close();
210}
211
212void t_csharp_generator::generate_consts(std::vector<t_const*> consts) {
213 if (consts.empty()){
214 return;
215 }
216 string f_consts_name = namespace_dir_ + "/Constants.cs";
217 ofstream f_consts;
218 f_consts.open(f_consts_name.c_str());
219
220 f_consts <<
221 autogen_comment() <<
222 csharp_type_usings() << endl;
223
224 start_csharp_namespace(f_consts);
225
226 indent(f_consts) <<
227 "public class Constants" << endl;
228 scope_up(f_consts);
229
230 vector<t_const*>::iterator c_iter;
231 bool need_static_constructor = false;
232 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
233 if (print_const_value(f_consts, (*c_iter)->get_name(), (*c_iter)->get_type(), (*c_iter)->get_value(), false)) {
234 need_static_constructor = true;
235 }
236 }
237
238 if (need_static_constructor) {
239 print_const_constructor(f_consts, consts);
240 }
241
242 scope_down(f_consts);
243 end_csharp_namespace(f_consts);
244 f_consts.close();
245}
246
247void t_csharp_generator::print_const_def_value(std::ofstream& out, string name, t_type* type, t_const_value* value)
248{
249 if (type->is_struct() || type->is_xception()) {
250 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
251 vector<t_field*>::const_iterator f_iter;
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 t_type* field_type = NULL;
256 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
257 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
258 field_type = (*f_iter)->get_type();
259 }
260 }
261 if (field_type == NULL) {
262 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
263 }
264 string val = render_const_value(out, name, field_type, v_iter->second);
265 indent(out) << name << "." << v_iter->first->get_string() << " = " << val << ";" << endl;
266 }
267 } else if (type->is_map()) {
268 t_type* ktype = ((t_map*)type)->get_key_type();
269 t_type* vtype = ((t_map*)type)->get_val_type();
270 const map<t_const_value*, t_const_value*>& val = value->get_map();
271 map<t_const_value*, t_const_value*>::const_iterator v_iter;
272 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
273 string key = render_const_value(out, name, ktype, v_iter->first);
274 string val = render_const_value(out, name, vtype, v_iter->second);
275 indent(out) << name << "[" << key << "]" << " = " << val << ";" << endl;
276 }
277 } else if (type->is_list() || type->is_set()) {
278 t_type* etype;
279 if (type->is_list()) {
280 etype = ((t_list*)type)->get_elem_type();
281 } else {
282 etype = ((t_set*)type)->get_elem_type();
283 }
284
285 const vector<t_const_value*>& val = value->get_list();
286 vector<t_const_value*>::const_iterator v_iter;
287 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
288 string val = render_const_value(out, name, etype, *v_iter);
289 indent(out) << name << ".Add(" << val << ");" << endl;
290 }
291 }
292}
293
294void t_csharp_generator::print_const_constructor(std::ofstream& out, std::vector<t_const*> consts) {
295 indent(out) << "static Constants()" << endl;
296 scope_up(out);
297 vector<t_const*>::iterator c_iter;
298 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
299 string name = (*c_iter)->get_name();
300 t_type* type = (*c_iter)->get_type();
301 t_const_value* value = (*c_iter)->get_value();
302
303 print_const_def_value(out, name, type, value);
304 }
305 scope_down(out);
306}
307
308
309//it seems like all that methods that call this are using in_static to be the opposite of what it would imply
310bool 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) {
311 indent(out);
312 bool need_static_construction = !in_static;
313 if (!defval || needtype) {
314 out <<
315 (in_static ? "" : "public static ") <<
316 type_name(type) << " ";
317 }
318 if (type->is_base_type()) {
319 string v2 = render_const_value(out, name, type, value);
320 out << name << " = " << v2 << ";" << endl;
321 need_static_construction = false;
322 } else if (type->is_enum()) {
323 out << name << " = (" << type_name(type, false, true) << ")" << value->get_integer() << ";" << endl;
324 need_static_construction = false;
325 } else if (type->is_struct() || type->is_xception()) {
326 out << name << " = new " << type_name(type) << "();" << endl;
327 } else if (type->is_map()) {
328 out << name << " = new " << type_name(type, true, true) << "();" << endl;
329 } else if (type->is_list() || type->is_set()) {
330 out << name << " = new " << type_name(type) << "();" << endl;
331 }
332
333 if (defval && !type->is_base_type() && !type->is_enum()) {
334 print_const_def_value(out, name, type, value);
335 }
336
337 return need_static_construction;
338}
339
340std::string t_csharp_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) {
Roger Meier3b771a12010-11-17 22:11:26 +0000341 (void) name;
David Reiss7f42bcf2008-01-11 20:59:12 +0000342 std::ostringstream render;
343
344 if (type->is_base_type()) {
345 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
346 switch (tbase) {
347 case t_base_type::TYPE_STRING:
David Reiss82e6fc02009-03-26 23:32:36 +0000348 render << '"' << get_escaped_string(value) << '"';
David Reiss7f42bcf2008-01-11 20:59:12 +0000349 break;
350 case t_base_type::TYPE_BOOL:
351 render << ((value->get_integer() > 0) ? "true" : "false");
352 break;
353 case t_base_type::TYPE_BYTE:
354 case t_base_type::TYPE_I16:
355 case t_base_type::TYPE_I32:
356 case t_base_type::TYPE_I64:
357 render << value->get_integer();
358 break;
359 case t_base_type::TYPE_DOUBLE:
360 if (value->get_type() == t_const_value::CV_INTEGER) {
361 render << value->get_integer();
362 } else {
363 render << value->get_double();
364 }
365 break;
366 default:
367 throw "compiler error: no const of base type " + tbase;
368 }
369 } else if (type->is_enum()) {
370 render << "(" << type->get_name() << ")" << value->get_integer();
371 } else {
372 string t = tmp("tmp");
373 print_const_value(out, t, type, value, true, true, true);
374 render << t;
375 }
376
377 return render.str();
378}
379
380void t_csharp_generator::generate_struct(t_struct* tstruct) {
381 generate_csharp_struct(tstruct, false);
382}
383
384void t_csharp_generator::generate_xception(t_struct* txception) {
385 generate_csharp_struct(txception, true);
386}
387
388void t_csharp_generator::generate_csharp_struct(t_struct* tstruct, bool is_exception) {
389 string f_struct_name = namespace_dir_ + "/" + (tstruct->get_name()) + ".cs";
390 ofstream f_struct;
391
392 f_struct.open(f_struct_name.c_str());
393
394 f_struct <<
395 autogen_comment() <<
396 csharp_type_usings() <<
397 csharp_thrift_usings();
398
399 generate_csharp_struct_definition(f_struct, tstruct, is_exception);
400
401 f_struct.close();
402}
403
404void t_csharp_generator::generate_csharp_struct_definition(ofstream &out, t_struct* tstruct, bool is_exception, bool in_class, bool is_result) {
405
David Reissc6544162009-01-06 19:49:33 +0000406 if (!in_class) {
David Reiss7f42bcf2008-01-11 20:59:12 +0000407 start_csharp_namespace(out);
408 }
409
David Reiss63191332009-01-06 19:49:22 +0000410 out << endl;
411 indent(out) << "[Serializable]" << endl;
Bryan Duxbury056bcb62009-02-01 16:56:29 +0000412 bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
413
Bryan Duxbury281d9da2009-07-02 20:15:05 +0000414 indent(out) << "public " << (is_final ? "sealed " : "") << "partial class " << tstruct->get_name() << " : ";
David Reiss7f42bcf2008-01-11 20:59:12 +0000415
416 if (is_exception) {
David Reissc6544162009-01-06 19:49:33 +0000417 out << "Exception, ";
David Reiss7f42bcf2008-01-11 20:59:12 +0000418 }
David Reissc6544162009-01-06 19:49:33 +0000419 out << "TBase";
David Reiss7f42bcf2008-01-11 20:59:12 +0000420
421 out << endl;
422
423 scope_up(out);
424
425 const vector<t_field*>& members = tstruct->get_members();
426 vector<t_field*>::const_iterator m_iter;
427
David Reiss63191332009-01-06 19:49:22 +0000428 //make private members with public Properties
David Reiss7f42bcf2008-01-11 20:59:12 +0000429 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
430 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000431 "private " << declare_field(*m_iter, false, "_") << endl;
David Reiss63191332009-01-06 19:49:22 +0000432 }
433 out << endl;
434
435 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
436 generate_property(out, *m_iter, true);
David Reiss7f42bcf2008-01-11 20:59:12 +0000437 }
438
439 if (members.size() > 0) {
440 out <<
441 endl <<
David Reiss46dc6292008-02-06 22:09:58 +0000442 indent() << "public Isset __isset;" << endl <<
David Reiss63191332009-01-06 19:49:22 +0000443 indent() << "[Serializable]" << endl <<
David Reiss46dc6292008-02-06 22:09:58 +0000444 indent() << "public struct Isset {" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000445 indent_up();
446 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
447 indent(out) <<
David Reiss46dc6292008-02-06 22:09:58 +0000448 "public bool " << (*m_iter)->get_name() << ";" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000449 }
450
451 indent_down();
452 indent(out) << "}" << endl << endl;
453 }
David Reiss0c90f6f2008-02-06 22:18:40 +0000454
David Reiss7f42bcf2008-01-11 20:59:12 +0000455 indent(out) <<
456 "public " << tstruct->get_name() << "() {" << endl;
457 indent_up();
458 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
459 t_type* t = (*m_iter)->get_type();
460 while (t->is_typedef()) {
461 t = ((t_typedef*)t)->get_type();
462 }
463 if ((*m_iter)->get_value() != NULL) {
Roger Meierbe87ab22010-11-10 21:19:36 +0000464 print_const_value(out, "this._" + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true);
David Reiss7f42bcf2008-01-11 20:59:12 +0000465 }
466 }
467
468 indent_down();
469 indent(out) << "}" << endl << endl;
470
471 generate_csharp_struct_reader(out, tstruct);
472 if (is_result) {
473 generate_csharp_struct_result_writer(out, tstruct);
474 } else {
475 generate_csharp_struct_writer(out, tstruct);
476 }
477 generate_csharp_struct_tostring(out, tstruct);
478 scope_down(out);
479 out << endl;
480
481 if (!in_class)
482 {
483 end_csharp_namespace(out);
484 }
485}
486
487void t_csharp_generator::generate_csharp_struct_reader(ofstream& out, t_struct* tstruct) {
488 indent(out) <<
489 "public void Read (TProtocol iprot)" << endl;
490 scope_up(out);
491
492 const vector<t_field*>& fields = tstruct->get_members();
493 vector<t_field*>::const_iterator f_iter;
494
495 indent(out) <<
496 "TField field;" << endl <<
Bryan Duxburyf28f8262009-02-07 17:17:43 +0000497 indent() << "iprot.ReadStructBegin();" << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +0000498
David Reiss7f42bcf2008-01-11 20:59:12 +0000499 indent(out) <<
500 "while (true)" << endl;
501 scope_up(out);
502
503 indent(out) <<
504 "field = iprot.ReadFieldBegin();" << endl;
505
506 indent(out) <<
507 "if (field.Type == TType.Stop) { " << endl;
508 indent_up();
509 indent(out) <<
510 "break;" << endl;
511 indent_down();
512 indent(out) <<
513 "}" << endl;
514
515 indent(out) <<
516 "switch (field.ID)" << endl;
517
518 scope_up(out);
519
520 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
521 indent(out) <<
522 "case " << (*f_iter)->get_key() << ":" << endl;
523 indent_up();
524 indent(out) <<
525 "if (field.Type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
526 indent_up();
527
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000528 generate_deserialize_field(out, *f_iter);
529
David Reiss7f42bcf2008-01-11 20:59:12 +0000530 indent_down();
531 out <<
532 indent() << "} else { " << endl <<
533 indent() << " TProtocolUtil.Skip(iprot, field.Type);" << endl <<
534 indent() << "}" << endl <<
535 indent() << "break;" << endl;
536 indent_down();
537 }
538
539 indent(out) <<
540 "default: " << endl;
541 indent_up();
542 indent(out) << "TProtocolUtil.Skip(iprot, field.Type);" << endl;
543 indent(out) << "break;" << endl;
544 indent_down();
545
546 scope_down(out);
547
548 indent(out) <<
549 "iprot.ReadFieldEnd();" << endl;
550
551 scope_down(out);
552
553 indent(out) <<
554 "iprot.ReadStructEnd();" << endl;
555
556 indent_down();
557
558 indent(out) << "}" << endl << endl;
559
560}
561
562void t_csharp_generator::generate_csharp_struct_writer(ofstream& out, t_struct* tstruct) {
563 out <<
564 indent() << "public void Write(TProtocol oprot) {" << endl;
565 indent_up();
566
567 string name = tstruct->get_name();
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000568 const vector<t_field*>& fields = tstruct->get_sorted_members();
David Reiss7f42bcf2008-01-11 20:59:12 +0000569 vector<t_field*>::const_iterator f_iter;
570
571 indent(out) <<
572 "TStruct struc = new TStruct(\"" << name << "\");" << endl;
573 indent(out) <<
David Reiss7f42bcf2008-01-11 20:59:12 +0000574 "oprot.WriteStructBegin(struc);" << endl;
575
David Reissb79acc92008-03-18 18:21:58 +0000576 if (fields.size() > 0) {
577 indent(out) << "TField field = new TField();" << endl;
578 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
579 bool null_allowed = type_can_be_null((*f_iter)->get_type());
580 if (null_allowed) {
581 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000582 "if (" << prop_name((*f_iter)) << " != null && __isset." << (*f_iter)->get_name() << ") {" << endl;
David Reiss63191332009-01-06 19:49:22 +0000583 indent_up();
584 }
585 else
586 {
587 indent(out) <<
588 "if (__isset." << (*f_iter)->get_name() << ") {" << endl;
David Reissb79acc92008-03-18 18:21:58 +0000589 indent_up();
590 }
591
David Reiss7f42bcf2008-01-11 20:59:12 +0000592 indent(out) <<
David Reissb79acc92008-03-18 18:21:58 +0000593 "field.Name = \"" << (*f_iter)->get_name() << "\";" << endl;
594 indent(out) <<
595 "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
596 indent(out) <<
597 "field.ID = " << (*f_iter)->get_key() << ";" << endl;
598 indent(out) <<
599 "oprot.WriteFieldBegin(field);" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000600
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000601 generate_serialize_field(out, *f_iter);
David Reiss7f42bcf2008-01-11 20:59:12 +0000602
David Reissb79acc92008-03-18 18:21:58 +0000603 indent(out) <<
604 "oprot.WriteFieldEnd();" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000605
David Reiss63191332009-01-06 19:49:22 +0000606 indent_down();
607 indent(out) << "}" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000608 }
609 }
610
611 indent(out) <<
612 "oprot.WriteFieldStop();" << endl;
613 indent(out) <<
614 "oprot.WriteStructEnd();" << endl;
615
616 indent_down();
617
618 indent(out) <<
619 "}" << endl << endl;
620}
621
622void t_csharp_generator::generate_csharp_struct_result_writer(ofstream& out, t_struct* tstruct) {
623 indent(out) <<
624 "public void Write(TProtocol oprot) {" << endl;
625 indent_up();
626
627 string name = tstruct->get_name();
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000628 const vector<t_field*>& fields = tstruct->get_sorted_members();
David Reiss7f42bcf2008-01-11 20:59:12 +0000629 vector<t_field*>::const_iterator f_iter;
630
631 indent(out) <<
632 "TStruct struc = new TStruct(\"" << name << "\");" << endl;
633 indent(out) <<
David Reiss7f42bcf2008-01-11 20:59:12 +0000634 "oprot.WriteStructBegin(struc);" << endl;
635
David Reissb79acc92008-03-18 18:21:58 +0000636 if (fields.size() > 0) {
637 indent(out) << "TField field = new TField();" << endl;
638 bool first = true;
639 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
640 if (first) {
641 first = false;
642 out <<
643 endl << indent() << "if ";
644 } else {
645 out <<
646 " else if ";
647 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000648
David Reissb79acc92008-03-18 18:21:58 +0000649 out <<
650 "(this.__isset." << (*f_iter)->get_name() << ") {" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000651 indent_up();
David Reiss7f42bcf2008-01-11 20:59:12 +0000652
David Reissb79acc92008-03-18 18:21:58 +0000653 bool null_allowed = type_can_be_null((*f_iter)->get_type());
654 if (null_allowed) {
655 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000656 "if (" << prop_name(*f_iter) << " != null) {" << endl;
David Reissb79acc92008-03-18 18:21:58 +0000657 indent_up();
658 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000659
David Reissb79acc92008-03-18 18:21:58 +0000660 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000661 "field.Name = \"" << prop_name(*f_iter) << "\";" << endl;
David Reissb79acc92008-03-18 18:21:58 +0000662 indent(out) <<
663 "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl;
664 indent(out) <<
665 "field.ID = " << (*f_iter)->get_key() << ";" << endl;
666 indent(out) <<
667 "oprot.WriteFieldBegin(field);" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000668
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000669 generate_serialize_field(out, *f_iter);
David Reiss7f42bcf2008-01-11 20:59:12 +0000670
David Reissb79acc92008-03-18 18:21:58 +0000671 indent(out) <<
672 "oprot.WriteFieldEnd();" << endl;
673
674 if (null_allowed) {
675 indent_down();
676 indent(out) << "}" << endl;
677 }
678
David Reiss7f42bcf2008-01-11 20:59:12 +0000679 indent_down();
David Reissb79acc92008-03-18 18:21:58 +0000680 indent(out) << "}";
David Reiss7f42bcf2008-01-11 20:59:12 +0000681 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000682 }
683
684 out <<
685 endl <<
686 indent() << "oprot.WriteFieldStop();" << endl <<
687 indent() << "oprot.WriteStructEnd();" << endl;
688
689 indent_down();
690
691 indent(out) <<
692 "}" << endl << endl;
693}
694
695void t_csharp_generator::generate_csharp_struct_tostring(ofstream& out, t_struct* tstruct) {
696 indent(out) <<
697 "public override string ToString() {" << endl;
698 indent_up();
699
700 indent(out) <<
701 "StringBuilder sb = new StringBuilder(\"" << tstruct->get_name() << "(\");" << endl;
702
703 const vector<t_field*>& fields = tstruct->get_members();
704 vector<t_field*>::const_iterator f_iter;
705
706 bool first = true;
707
708 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
709 if (first) {
710 first = false;
711 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000712 "sb.Append(\"" << prop_name((*f_iter)) << ": \");" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000713 } else {
714 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000715 "sb.Append(\"," << prop_name((*f_iter)) << ": \");" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000716 }
717 t_type* ttype = (*f_iter)->get_type();
718 if (ttype->is_xception() || ttype->is_struct()) {
719 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000720 "sb.Append(" << prop_name((*f_iter)) << "== null ? \"<null>\" : "<< prop_name((*f_iter)) << ".ToString());" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000721 } else {
722 indent(out) <<
Bryan Duxburyc43ec622010-09-02 14:58:51 +0000723 "sb.Append(" << prop_name((*f_iter)) << ");" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000724 }
725 }
726
727 indent(out) <<
728 "sb.Append(\")\");" << endl;
729 indent(out) <<
730 "return sb.ToString();" << endl;
731
732 indent_down();
733 indent(out) << "}" << endl << endl;
734}
735
736void t_csharp_generator::generate_service(t_service* tservice) {
737 string f_service_name = namespace_dir_ + "/" + service_name_ + ".cs";
738 f_service_.open(f_service_name.c_str());
739
740 f_service_ <<
741 autogen_comment() <<
742 csharp_type_usings() <<
743 csharp_thrift_usings();
744
745 start_csharp_namespace(f_service_);
746
747 indent(f_service_) <<
748 "public class " << service_name_ << " {" << endl;
749 indent_up();
750
751 generate_service_interface(tservice);
752 generate_service_client(tservice);
753 generate_service_server(tservice);
754 generate_service_helpers(tservice);
755
756 indent_down();
757
758 indent(f_service_) <<
759 "}" << endl;
760 end_csharp_namespace(f_service_);
761 f_service_.close();
762}
763
764void t_csharp_generator::generate_service_interface(t_service* tservice) {
765 string extends = "";
766 string extends_iface = "";
767 if (tservice->get_extends() != NULL) {
768 extends = type_name(tservice->get_extends());
769 extends_iface = " : " + extends + ".Iface";
770 }
771
772 indent(f_service_) <<
773 "public interface Iface" << extends_iface << " {" << endl;
774 indent_up();
775 vector<t_function*> functions = tservice->get_functions();
776 vector<t_function*>::iterator f_iter;
777 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
778 {
779 indent(f_service_) <<
780 function_signature(*f_iter) << ";" << endl;
781 }
782 indent_down();
783 f_service_ <<
784 indent() << "}" << endl << endl;
785}
786
787void t_csharp_generator::generate_service_helpers(t_service* tservice) {
788 vector<t_function*> functions = tservice->get_functions();
789 vector<t_function*>::iterator f_iter;
790
791 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
792 t_struct* ts = (*f_iter)->get_arglist();
793 generate_csharp_struct_definition(f_service_, ts, false, true);
794 generate_function_helpers(*f_iter);
795 }
796}
797
798void t_csharp_generator::generate_service_client(t_service* tservice) {
799 string extends = "";
800 string extends_client = "";
801 if (tservice->get_extends() != NULL) {
802 extends = type_name(tservice->get_extends());
803 extends_client = extends + ".Client, ";
804 }
805
806 indent(f_service_) <<
807 "public class Client : " << extends_client << "Iface {" << endl;
808 indent_up();
809 indent(f_service_) <<
810 "public Client(TProtocol prot) : this(prot, prot)" << endl;
811 scope_up(f_service_);
812 scope_down(f_service_);
813 f_service_ << endl;
814
815 indent(f_service_) <<
David Reiss8bfba632008-04-02 22:10:13 +0000816 "public Client(TProtocol iprot, TProtocol oprot)";
817 if (!extends.empty()) {
818 f_service_ << " : base(iprot, oprot)";
819 }
820 f_service_ << endl;
821
David Reiss7f42bcf2008-01-11 20:59:12 +0000822 scope_up(f_service_);
823 if (extends.empty()) {
824 f_service_ <<
825 indent() << "iprot_ = iprot;" << endl <<
826 indent() << "oprot_ = oprot;" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000827 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000828 scope_down(f_service_);
829
830 f_service_ << endl;
831
832 if (extends.empty()) {
833 f_service_ <<
834 indent() << "protected TProtocol iprot_;" << endl <<
835 indent() << "protected TProtocol oprot_;" << endl <<
David Reiss7f42bcf2008-01-11 20:59:12 +0000836 indent() << "protected int seqid_;" << endl << endl;
David Reisscdfbeb82008-03-18 18:22:14 +0000837
838 f_service_ << indent() << "public TProtocol InputProtocol" << endl;
839 scope_up(f_service_);
840 indent(f_service_) << "get { return iprot_; }" << endl;
841 scope_down(f_service_);
842
843 f_service_ << indent() << "public TProtocol OutputProtocol" << endl;
844 scope_up(f_service_);
845 indent(f_service_) << "get { return oprot_; }" << endl;
846 scope_down(f_service_);
847 f_service_ << endl << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000848 }
849
850 vector<t_function*> functions = tservice->get_functions();
851 vector<t_function*>::const_iterator f_iter;
852 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
853 string funname = (*f_iter)->get_name();
854
855 indent(f_service_) <<
856 "public " << function_signature(*f_iter) << endl;
857 scope_up(f_service_);
858 indent(f_service_) <<
859 "send_" << funname << "(";
860
861 t_struct* arg_struct = (*f_iter)->get_arglist();
862
863 const vector<t_field*>& fields = arg_struct->get_members();
864 vector<t_field*>::const_iterator fld_iter;
865 bool first = true;
866 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
867 if (first) {
868 first = false;
869 } else {
870 f_service_ << ", ";
871 }
872 f_service_ << (*fld_iter)->get_name();
873 }
874 f_service_ << ");" << endl;
875
David Reiss47329252009-03-24 20:01:02 +0000876 if (!(*f_iter)->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +0000877 f_service_ << indent();
878 if (!(*f_iter)->get_returntype()->is_void()) {
879 f_service_ << "return ";
880 }
881 f_service_ <<
882 "recv_" << funname << "();" << endl;
883 }
884 scope_down(f_service_);
885 f_service_ << endl;
886
887 t_function send_function(g_type_void,
888 string("send_") + (*f_iter)->get_name(),
889 (*f_iter)->get_arglist());
890
891 string argsname = (*f_iter)->get_name() + "_args";
892
893 indent(f_service_) <<
894 "public " << function_signature(&send_function) << endl;
895 scope_up(f_service_);
896
897 f_service_ <<
898 indent() << "oprot_.WriteMessageBegin(new TMessage(\"" << funname << "\", TMessageType.Call, seqid_));" << endl <<
899 indent() << argsname << " args = new " << argsname << "();" << endl;
900
901 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
902 f_service_ <<
David Reiss63191332009-01-06 19:49:22 +0000903 indent() << "args." << prop_name(*fld_iter) << " = " << (*fld_iter)->get_name() << ";" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +0000904 }
905
906 f_service_ <<
907 indent() << "args.Write(oprot_);" << endl <<
908 indent() << "oprot_.WriteMessageEnd();" << endl <<
909 indent() << "oprot_.Transport.Flush();" << endl;
910
911 scope_down(f_service_);
912 f_service_ << endl;
913
David Reiss47329252009-03-24 20:01:02 +0000914 if (!(*f_iter)->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +0000915 string resultname = (*f_iter)->get_name() + "_result";
916
917 t_struct noargs(program_);
918 t_function recv_function((*f_iter)->get_returntype(),
919 string("recv_") + (*f_iter)->get_name(),
920 &noargs,
921 (*f_iter)->get_xceptions());
922 indent(f_service_) <<
923 "public " << function_signature(&recv_function) << endl;
924 scope_up(f_service_);
925
926 f_service_ <<
927 indent() << "TMessage msg = iprot_.ReadMessageBegin();" << endl <<
928 indent() << "if (msg.Type == TMessageType.Exception) {" << endl;
929 indent_up();
930 f_service_ <<
931 indent() << "TApplicationException x = TApplicationException.Read(iprot_);" << endl <<
932 indent() << "iprot_.ReadMessageEnd();" << endl <<
933 indent() << "throw x;" << endl;
934 indent_down();
935 f_service_ <<
936 indent() << "}" << endl <<
937 indent() << resultname << " result = new " << resultname << "();" << endl <<
938 indent() << "result.Read(iprot_);" << endl <<
939 indent() << "iprot_.ReadMessageEnd();" << endl;
940
941 if (!(*f_iter)->get_returntype()->is_void()) {
942 f_service_ <<
943 indent() << "if (result.__isset.success) {" << endl <<
David Reiss63191332009-01-06 19:49:22 +0000944 indent() << " return result.Success;" << endl <<
David Reiss7f42bcf2008-01-11 20:59:12 +0000945 indent() << "}" << endl;
946 }
947
948 t_struct *xs = (*f_iter)->get_xceptions();
949
950 const std::vector<t_field*>& xceptions = xs->get_members();
951 vector<t_field*>::const_iterator x_iter;
952 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
953 f_service_ <<
954 indent() << "if (result.__isset." << (*x_iter)->get_name() << ") {" << endl <<
David Reiss63191332009-01-06 19:49:22 +0000955 indent() << " throw result." << prop_name(*x_iter) << ";" << endl <<
David Reiss7f42bcf2008-01-11 20:59:12 +0000956 indent() << "}" << endl;
957 }
958
959 if ((*f_iter)->get_returntype()->is_void()) {
960 indent(f_service_) <<
961 "return;" << endl;
962 } else {
963 f_service_ <<
964 indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
965 }
966
967 scope_down(f_service_);
968 f_service_ << endl;
969 }
970 }
971
972 indent_down();
973 indent(f_service_) <<
974 "}" << endl;
975}
976
977void t_csharp_generator::generate_service_server(t_service* tservice) {
978 vector<t_function*> functions = tservice->get_functions();
979 vector<t_function*>::iterator f_iter;
980
981 string extends = "";
982 string extends_processor = "";
983 if (tservice->get_extends() != NULL) {
984 extends = type_name(tservice->get_extends());
985 extends_processor = extends + ".Processor, ";
986 }
987
988 indent(f_service_) <<
989 "public class Processor : " << extends_processor << "TProcessor {" << endl;
990 indent_up();
991
992 indent(f_service_) <<
David Reiss8bfba632008-04-02 22:10:13 +0000993 "public Processor(Iface iface)" ;
David Reiss7f42bcf2008-01-11 20:59:12 +0000994 if (!extends.empty()) {
David Reiss8bfba632008-04-02 22:10:13 +0000995 f_service_ << " : base(iface)";
David Reiss7f42bcf2008-01-11 20:59:12 +0000996 }
David Reiss8bfba632008-04-02 22:10:13 +0000997 f_service_ << endl;
998 scope_up(f_service_);
David Reiss7f42bcf2008-01-11 20:59:12 +0000999 f_service_ <<
1000 indent() << "iface_ = iface;" << endl;
1001
1002 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1003 f_service_ <<
David Reiss8bfba632008-04-02 22:10:13 +00001004 indent() << "processMap_[\"" << (*f_iter)->get_name() << "\"] = " << (*f_iter)->get_name() << "_Process;" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001005 }
1006
1007 scope_down(f_service_);
1008 f_service_ << endl;
1009
1010 if (extends.empty()) {
1011 f_service_ <<
David Reiss8bfba632008-04-02 22:10:13 +00001012 indent() << "protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001013 }
1014
1015 f_service_ <<
1016 indent() << "private Iface iface_;" << endl;
1017
1018 if (extends.empty()) {
1019 f_service_ <<
1020 indent() << "protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();" << endl;
1021 }
1022
1023 f_service_ << endl;
1024
David Reiss8bfba632008-04-02 22:10:13 +00001025 if (extends.empty()) {
1026 indent(f_service_) <<
1027 "public bool Process(TProtocol iprot, TProtocol oprot)" << endl;
1028 }
1029 else
1030 {
1031 indent(f_service_) <<
1032 "public new bool Process(TProtocol iprot, TProtocol oprot)" << endl;
1033 }
David Reiss7f42bcf2008-01-11 20:59:12 +00001034 scope_up(f_service_);
1035
David Reiss63191332009-01-06 19:49:22 +00001036 f_service_ << indent() << "try" << endl;
1037 scope_up(f_service_);
1038
David Reiss7f42bcf2008-01-11 20:59:12 +00001039 f_service_ <<
1040 indent() << "TMessage msg = iprot.ReadMessageBegin();" << endl;
1041
1042 f_service_ <<
David Reissb0f31ef2009-01-06 19:49:29 +00001043 indent() << "ProcessFunction fn;" << endl <<
1044 indent() << "processMap_.TryGetValue(msg.Name, out fn);" << endl <<
David Reiss7f42bcf2008-01-11 20:59:12 +00001045 indent() << "if (fn == null) {" << endl <<
1046 indent() << " TProtocolUtil.Skip(iprot, TType.Struct);" << endl <<
1047 indent() << " iprot.ReadMessageEnd();" << endl <<
1048 indent() << " TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, \"Invalid method name: '\" + msg.Name + \"'\");" << endl <<
1049 indent() << " oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));" << endl <<
1050 indent() << " x.Write(oprot);" << endl <<
1051 indent() << " oprot.WriteMessageEnd();" << endl <<
1052 indent() << " oprot.Transport.Flush();" << endl <<
1053 indent() << " return true;" << endl <<
1054 indent() << "}" << endl <<
David Reiss8bfba632008-04-02 22:10:13 +00001055 indent() << "fn(msg.SeqID, iprot, oprot);" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001056
David Reiss63191332009-01-06 19:49:22 +00001057 scope_down(f_service_);
1058
1059 f_service_ <<
1060 indent() << "catch (IOException)" << endl;
1061 scope_up(f_service_);
1062 f_service_ <<
1063 indent() << "return false;" << endl;
1064 scope_down(f_service_);
1065
David Reiss7f42bcf2008-01-11 20:59:12 +00001066 f_service_ <<
1067 indent() << "return true;" << endl;
1068
1069 scope_down(f_service_);
1070 f_service_ << endl;
1071
1072 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
1073 {
1074 generate_process_function(tservice, *f_iter);
1075 }
1076
1077 indent_down();
1078 indent(f_service_) <<
1079 "}" << endl << endl;
1080}
1081
1082void t_csharp_generator::generate_function_helpers(t_function* tfunction) {
David Reiss47329252009-03-24 20:01:02 +00001083 if (tfunction->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001084 return;
1085 }
1086
1087 t_struct result(program_, tfunction->get_name() + "_result");
1088 t_field success(tfunction->get_returntype(), "success", 0);
1089 if (!tfunction->get_returntype()->is_void()) {
1090 result.append(&success);
1091 }
1092
1093 t_struct *xs = tfunction->get_xceptions();
1094 const vector<t_field*>& fields = xs->get_members();
1095 vector<t_field*>::const_iterator f_iter;
1096 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1097 result.append(*f_iter);
1098 }
1099
1100 generate_csharp_struct_definition(f_service_, &result, false, true, true);
1101}
1102
1103void t_csharp_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
Roger Meier3b771a12010-11-17 22:11:26 +00001104 (void) tservice;
David Reiss7f42bcf2008-01-11 20:59:12 +00001105 indent(f_service_) <<
David Reiss8bfba632008-04-02 22:10:13 +00001106 "public void " << tfunction->get_name() << "_Process(int seqid, TProtocol iprot, TProtocol oprot)" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001107 scope_up(f_service_);
1108
1109 string argsname = tfunction->get_name() + "_args";
1110 string resultname = tfunction->get_name() + "_result";
1111
1112 f_service_ <<
1113 indent() << argsname << " args = new " << argsname << "();" << endl <<
1114 indent() << "args.Read(iprot);" << endl <<
1115 indent() << "iprot.ReadMessageEnd();" << endl;
1116
1117 t_struct* xs = tfunction->get_xceptions();
1118 const std::vector<t_field*>& xceptions = xs->get_members();
1119 vector<t_field*>::const_iterator x_iter;
1120
David Reiss47329252009-03-24 20:01:02 +00001121 if (!tfunction->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001122 f_service_ <<
1123 indent() << resultname << " result = new " << resultname << "();" << endl;
1124 }
1125
1126 if (xceptions.size() > 0) {
1127 f_service_ <<
1128 indent() << "try {" << endl;
1129 indent_up();
1130 }
1131
1132 t_struct* arg_struct = tfunction->get_arglist();
1133 const std::vector<t_field*>& fields = arg_struct->get_members();
1134 vector<t_field*>::const_iterator f_iter;
1135
1136 f_service_ << indent();
David Reiss47329252009-03-24 20:01:02 +00001137 if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
David Reiss63191332009-01-06 19:49:22 +00001138 f_service_ << "result.Success = ";
David Reiss7f42bcf2008-01-11 20:59:12 +00001139 }
1140 f_service_ <<
David Reiss8bfba632008-04-02 22:10:13 +00001141 "iface_." << tfunction->get_name() << "(";
David Reiss7f42bcf2008-01-11 20:59:12 +00001142 bool first = true;
1143 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1144 if (first) {
1145 first = false;
1146 } else {
1147 f_service_ << ", ";
1148 }
David Reiss63191332009-01-06 19:49:22 +00001149 f_service_ << "args." << prop_name(*f_iter);
David Reiss7f42bcf2008-01-11 20:59:12 +00001150 }
1151 f_service_ << ");" << endl;
1152
David Reiss47329252009-03-24 20:01:02 +00001153 if (!tfunction->is_oneway() && xceptions.size() > 0) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001154 indent_down();
1155 f_service_ << indent() << "}";
1156 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
Bryan Duxburyf28f8262009-02-07 17:17:43 +00001157 f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl;
David Reiss47329252009-03-24 20:01:02 +00001158 if (!tfunction->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001159 indent_up();
1160 f_service_ <<
David Reiss63191332009-01-06 19:49:22 +00001161 indent() << "result." << prop_name(*x_iter) << " = " << (*x_iter)->get_name() << ";" << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001162 indent_down();
1163 f_service_ << indent() << "}";
1164 } else {
1165 f_service_ << "}";
1166 }
1167 }
1168 f_service_ << endl;
1169 }
1170
David Reiss47329252009-03-24 20:01:02 +00001171 if (tfunction->is_oneway()) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001172 f_service_ <<
1173 indent() << "return;" << endl;
1174 scope_down(f_service_);
1175
David Reiss7f42bcf2008-01-11 20:59:12 +00001176 return;
1177 }
1178
1179 f_service_ <<
1180 indent() << "oprot.WriteMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.Reply, seqid)); " << endl <<
1181 indent() << "result.Write(oprot);" << endl <<
1182 indent() << "oprot.WriteMessageEnd();" << endl <<
1183 indent() << "oprot.Transport.Flush();" << endl;
1184
1185 scope_down(f_service_);
1186
1187 f_service_ << endl;
David Reiss7f42bcf2008-01-11 20:59:12 +00001188}
1189
1190void t_csharp_generator::generate_deserialize_field(ofstream& out, t_field* tfield, string prefix) {
1191 t_type* type = tfield->get_type();
1192 while(type->is_typedef()) {
1193 type = ((t_typedef*)type)->get_type();
1194 }
1195
1196 if (type->is_void()) {
1197 throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
1198 }
1199
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001200 string name = prefix + prop_name(tfield);
David Reiss7f42bcf2008-01-11 20:59:12 +00001201
1202 if (type->is_struct() || type->is_xception()) {
1203 generate_deserialize_struct(out, (t_struct*)type, name);
1204 } else if (type->is_container()) {
1205 generate_deserialize_container(out, type, name);
1206 } else if (type->is_base_type() || type->is_enum()) {
1207 indent(out) <<
1208 name << " = ";
David Reiss0c90f6f2008-02-06 22:18:40 +00001209
David Reiss7f42bcf2008-01-11 20:59:12 +00001210 if (type->is_enum())
1211 {
1212 out << "(" << type_name(type, false, true) << ")";
1213 }
1214
1215 out << "iprot.";
1216
1217 if (type->is_base_type()) {
1218 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1219 switch (tbase) {
1220 case t_base_type::TYPE_VOID:
1221 throw "compiler error: cannot serialize void field in a struct: " + name;
1222 break;
1223 case t_base_type::TYPE_STRING:
David Reisscba57272008-02-06 22:09:44 +00001224 if (((t_base_type*)type)->is_binary()) {
1225 out << "ReadBinary();";
1226 } else {
1227 out << "ReadString();";
1228 }
David Reiss7f42bcf2008-01-11 20:59:12 +00001229 break;
1230 case t_base_type::TYPE_BOOL:
1231 out << "ReadBool();";
1232 break;
1233 case t_base_type::TYPE_BYTE:
1234 out << "ReadByte();";
1235 break;
1236 case t_base_type::TYPE_I16:
1237 out << "ReadI16();";
1238 break;
1239 case t_base_type::TYPE_I32:
1240 out << "ReadI32();";
1241 break;
1242 case t_base_type::TYPE_I64:
1243 out << "ReadI64();";
1244 break;
1245 case t_base_type::TYPE_DOUBLE:
1246 out << "ReadDouble();";
1247 break;
1248 default:
1249 throw "compiler error: no C# name for base type " + tbase;
1250 }
1251 } else if (type->is_enum()) {
1252 out << "ReadI32();";
1253 }
1254 out << endl;
1255 } else {
1256 printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", tfield->get_name().c_str(), type_name(type).c_str());
1257 }
1258}
1259
1260void t_csharp_generator::generate_deserialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
1261 out <<
1262 indent() << prefix << " = new " << type_name(tstruct) << "();" << endl <<
1263 indent() << prefix << ".Read(iprot);" << endl;
1264}
1265
1266void t_csharp_generator::generate_deserialize_container(ofstream& out, t_type* ttype, string prefix) {
1267 scope_up(out);
1268
1269 string obj;
1270
1271 if (ttype->is_map()) {
1272 obj = tmp("_map");
1273 } else if (ttype->is_set()) {
1274 obj = tmp("_set");
1275 } else if (ttype->is_list()) {
1276 obj = tmp("_list");
1277 }
1278
1279 indent(out) <<
1280 prefix << " = new " << type_name(ttype, false, true) << "();" <<endl;
1281 if (ttype->is_map()) {
1282 out <<
1283 indent() << "TMap " << obj << " = iprot.ReadMapBegin();" << endl;
1284 } else if (ttype->is_set()) {
1285 out <<
1286 indent() << "TSet " << obj << " = iprot.ReadSetBegin();" << endl;
1287 } else if (ttype->is_list()) {
1288 out <<
1289 indent() << "TList " << obj << " = iprot.ReadListBegin();" << endl;
1290 }
1291
1292 string i = tmp("_i");
1293 indent(out) <<
1294 "for( int " << i << " = 0; " << i << " < " << obj << ".Count" << "; " << "++" << i << ")" << endl;
1295 scope_up(out);
David Reiss0c90f6f2008-02-06 22:18:40 +00001296
David Reiss7f42bcf2008-01-11 20:59:12 +00001297 if (ttype->is_map()) {
1298 generate_deserialize_map_element(out, (t_map*)ttype, prefix);
1299 } else if (ttype->is_set()) {
1300 generate_deserialize_set_element(out, (t_set*)ttype, prefix);
1301 } else if (ttype->is_list()) {
1302 generate_deserialize_list_element(out, (t_list*)ttype, prefix);
1303 }
1304
1305 scope_down(out);
1306
1307 if (ttype->is_map()) {
1308 indent(out) << "iprot.ReadMapEnd();" << endl;
1309 } else if (ttype->is_set()) {
1310 indent(out) << "iprot.ReadSetEnd();" << endl;
1311 } else if (ttype->is_list()) {
1312 indent(out) << "iprot.ReadListEnd();" << endl;
1313 }
1314
1315 scope_down(out);
1316}
1317
1318void t_csharp_generator::generate_deserialize_map_element(ofstream& out, t_map* tmap, string prefix) {
1319 string key = tmp("_key");
1320 string val = tmp("_val");
1321
1322 t_field fkey(tmap->get_key_type(), key);
1323 t_field fval(tmap->get_val_type(), val);
1324
1325 indent(out) <<
1326 declare_field(&fkey) << endl;
1327 indent(out) <<
1328 declare_field(&fval) << endl;
1329
1330 generate_deserialize_field(out, &fkey);
1331 generate_deserialize_field(out, &fval);
1332
1333 indent(out) <<
1334 prefix << "[" << key << "] = " << val << ";" << endl;
1335}
1336
1337void t_csharp_generator::generate_deserialize_set_element(ofstream& out, t_set* tset, string prefix) {
1338 string elem = tmp("_elem");
1339 t_field felem(tset->get_elem_type(), elem);
1340
1341 indent(out) <<
1342 declare_field(&felem, true) << endl;
1343
1344 generate_deserialize_field(out, &felem);
1345
1346 indent(out) <<
1347 prefix << ".Add(" << elem << ");" << endl;
1348}
1349
1350void t_csharp_generator::generate_deserialize_list_element(ofstream& out, t_list* tlist, string prefix) {
1351 string elem = tmp("_elem");
1352 t_field felem(tlist->get_elem_type(), elem);
1353
1354 indent(out) <<
1355 declare_field(&felem, true) << endl;
1356
1357 generate_deserialize_field(out, &felem);
1358
1359 indent(out) <<
1360 prefix << ".Add(" << elem << ");" << endl;
1361}
1362
1363void t_csharp_generator::generate_serialize_field(ofstream& out, t_field* tfield, string prefix) {
1364 t_type* type = tfield->get_type();
1365 while (type->is_typedef()) {
1366 type = ((t_typedef*)type)->get_type();
1367 }
1368
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001369 string name = prefix + prop_name(tfield);
1370
David Reiss7f42bcf2008-01-11 20:59:12 +00001371 if (type->is_void()) {
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001372 throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
David Reiss7f42bcf2008-01-11 20:59:12 +00001373 }
1374
1375 if (type->is_struct() || type->is_xception()) {
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001376 generate_serialize_struct(out, (t_struct*)type, name);
David Reiss7f42bcf2008-01-11 20:59:12 +00001377 } else if (type->is_container()) {
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001378 generate_serialize_container(out, type, name);
David Reiss7f42bcf2008-01-11 20:59:12 +00001379 } else if (type->is_base_type() || type->is_enum()) {
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001380
David Reiss7f42bcf2008-01-11 20:59:12 +00001381 indent(out) <<
1382 "oprot.";
1383
1384 if (type->is_base_type()) {
1385 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1386
1387 switch(tbase) {
1388 case t_base_type::TYPE_VOID:
1389 throw "compiler error: cannot serialize void field in a struct: " + name;
1390 break;
1391 case t_base_type::TYPE_STRING:
David Reisscba57272008-02-06 22:09:44 +00001392 if (((t_base_type*)type)->is_binary()) {
1393 out << "WriteBinary(";
1394 } else {
1395 out << "WriteString(";
1396 }
1397 out << name << ");";
David Reiss7f42bcf2008-01-11 20:59:12 +00001398 break;
1399 case t_base_type::TYPE_BOOL:
1400 out << "WriteBool(" << name << ");";
1401 break;
1402 case t_base_type::TYPE_BYTE:
1403 out << "WriteByte(" << name << ");";
1404 break;
1405 case t_base_type::TYPE_I16:
1406 out << "WriteI16(" << name << ");";
1407 break;
1408 case t_base_type::TYPE_I32:
1409 out << "WriteI32(" << name << ");";
1410 break;
1411 case t_base_type::TYPE_I64:
1412 out << "WriteI64(" << name << ");";
1413 break;
1414 case t_base_type::TYPE_DOUBLE:
1415 out << "WriteDouble(" << name << ");";
1416 break;
1417 default:
1418 throw "compiler error: no C# name for base type " + tbase;
1419 }
1420 } else if (type->is_enum()) {
1421 out << "WriteI32((int)" << name << ");";
1422 }
1423 out << endl;
1424 } else {
1425 printf("DO NOT KNOW HOW TO SERIALIZE '%s%s' TYPE '%s'\n",
1426 prefix.c_str(),
1427 tfield->get_name().c_str(),
1428 type_name(type).c_str());
1429 }
1430}
1431
1432void t_csharp_generator::generate_serialize_struct(ofstream& out, t_struct* tstruct, string prefix) {
Roger Meier3b771a12010-11-17 22:11:26 +00001433 (void) tstruct;
David Reiss7f42bcf2008-01-11 20:59:12 +00001434 out <<
1435 indent() << prefix << ".Write(oprot);" << endl;
1436}
1437
1438void t_csharp_generator::generate_serialize_container(ofstream& out, t_type* ttype, string prefix) {
1439 scope_up(out);
1440
1441 if (ttype->is_map()) {
1442 indent(out) <<
1443 "oprot.WriteMapBegin(new TMap(" <<
1444 type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
1445 type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
1446 prefix << ".Count));" << endl;
1447 } else if (ttype->is_set()) {
1448 indent(out) <<
1449 "oprot.WriteSetBegin(new TSet(" <<
1450 type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
1451 prefix << ".Count));" << endl;
1452 } else if (ttype->is_list()) {
1453 indent(out) <<
1454 "oprot.WriteListBegin(new TList(" <<
1455 type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
1456 prefix << ".Count));" << endl;
1457 }
1458
1459 string iter = tmp("_iter");
1460 if (ttype->is_map()) {
1461 indent(out) <<
1462 "foreach (" <<
1463 type_name(((t_map*)ttype)->get_key_type()) << " " << iter <<
1464 " in " <<
1465 prefix << ".Keys)";
1466 } else if (ttype->is_set()) {
1467 indent(out) <<
1468 "foreach (" <<
1469 type_name(((t_set*)ttype)->get_elem_type()) << " " << iter <<
1470 " in " <<
1471 prefix << ")";
1472 } else if (ttype->is_list()) {
1473 indent(out) <<
1474 "foreach (" <<
1475 type_name(((t_list*)ttype)->get_elem_type()) << " " << iter <<
1476 " in " <<
1477 prefix << ")";
1478 }
1479
1480 out << endl;
1481 scope_up(out);
1482
1483 if (ttype->is_map()) {
1484 generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
1485 } else if (ttype->is_set()) {
1486 generate_serialize_set_element(out, (t_set*)ttype, iter);
1487 } else if (ttype->is_list()) {
1488 generate_serialize_list_element(out, (t_list*)ttype, iter);
1489 }
1490
Bryan Duxbury137fea62011-03-28 14:52:25 +00001491 scope_down(out);
1492
David Reiss7f42bcf2008-01-11 20:59:12 +00001493 if (ttype->is_map()) {
1494 indent(out) << "oprot.WriteMapEnd();" << endl;
1495 } else if (ttype->is_set()) {
1496 indent(out) << "oprot.WriteSetEnd();" << endl;
1497 } else if (ttype->is_list()) {
1498 indent(out) << "oprot.WriteListEnd();" << endl;
1499 }
1500
1501 scope_down(out);
David Reiss7f42bcf2008-01-11 20:59:12 +00001502}
1503
1504void t_csharp_generator::generate_serialize_map_element(ofstream& out, t_map* tmap, string iter, string map) {
1505 t_field kfield(tmap->get_key_type(), iter);
1506 generate_serialize_field(out, &kfield, "");
1507 t_field vfield(tmap->get_val_type(), map + "[" + iter + "]");
1508 generate_serialize_field(out, &vfield, "");
1509}
1510
1511void t_csharp_generator::generate_serialize_set_element(ofstream& out, t_set* tset, string iter) {
1512 t_field efield(tset->get_elem_type(), iter);
1513 generate_serialize_field(out, &efield, "");
1514}
1515
1516void t_csharp_generator::generate_serialize_list_element(ofstream& out, t_list* tlist, string iter) {
1517 t_field efield(tlist->get_elem_type(), iter);
1518 generate_serialize_field(out, &efield, "");
1519}
1520
David Reiss63191332009-01-06 19:49:22 +00001521void t_csharp_generator::generate_property(ofstream& out, t_field* tfield, bool isPublic) {
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001522 generate_csharp_property(out, tfield, isPublic, "_");
1523}
1524void t_csharp_generator::generate_csharp_property(ofstream& out, t_field* tfield, bool isPublic, std::string fieldPrefix) {
David Reiss63191332009-01-06 19:49:22 +00001525 indent(out) << (isPublic ? "public " : "private ") << type_name(tfield->get_type())
1526 << " " << prop_name(tfield) << endl;
1527 scope_up(out);
1528 indent(out) << "get" << endl;
1529 scope_up(out);
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001530 indent(out) << "return " << fieldPrefix + tfield->get_name() << ";" << endl;
David Reiss63191332009-01-06 19:49:22 +00001531 scope_down(out);
1532 indent(out) << "set" << endl;
1533 scope_up(out);
1534 indent(out) << "__isset." << tfield->get_name() << " = true;" << endl;
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001535 indent(out) << "this." << fieldPrefix + tfield->get_name() << " = value;" << endl;
David Reiss63191332009-01-06 19:49:22 +00001536 scope_down(out);
1537 scope_down(out);
1538 out << endl;
1539}
1540
1541std::string t_csharp_generator::prop_name(t_field* tfield) {
1542 string name (tfield->get_name());
1543 name[0] = toupper(name[0]);
1544 return name;
1545}
1546
David Reiss7f42bcf2008-01-11 20:59:12 +00001547string t_csharp_generator::type_name(t_type* ttype, bool in_container, bool in_init) {
Roger Meier3b771a12010-11-17 22:11:26 +00001548 (void) in_init;
David Reiss7f42bcf2008-01-11 20:59:12 +00001549 while (ttype->is_typedef()) {
1550 ttype = ((t_typedef*)ttype)->get_type();
1551 }
1552
1553 if (ttype->is_base_type()) {
David Reiss46dc6292008-02-06 22:09:58 +00001554 return base_type_name((t_base_type*)ttype, in_container);
David Reiss7f42bcf2008-01-11 20:59:12 +00001555 } else if (ttype->is_map()) {
1556 t_map *tmap = (t_map*) ttype;
1557 return "Dictionary<" + type_name(tmap->get_key_type(), true) +
1558 ", " + type_name(tmap->get_val_type(), true) + ">";
1559 } else if (ttype->is_set()) {
1560 t_set* tset = (t_set*) ttype;
David Reissd831a212009-02-13 03:09:52 +00001561 return "THashSet<" + type_name(tset->get_elem_type(), true) + ">";
David Reiss7f42bcf2008-01-11 20:59:12 +00001562 } else if (ttype->is_list()) {
1563 t_list* tlist = (t_list*) ttype;
1564 return "List<" + type_name(tlist->get_elem_type(), true) + ">";
1565 }
1566
1567 t_program* program = ttype->get_program();
1568 if (program != NULL && program != program_) {
David Reiss9d65bf02008-03-27 21:41:37 +00001569 string ns = program->get_namespace("csharp");
David Reiss7f42bcf2008-01-11 20:59:12 +00001570 if (!ns.empty()) {
1571 return ns + "." + ttype->get_name();
1572 }
1573 }
1574
1575 return ttype->get_name();
1576}
1577
David Reisscba57272008-02-06 22:09:44 +00001578string t_csharp_generator::base_type_name(t_base_type* tbase, bool in_container) {
Roger Meier3b771a12010-11-17 22:11:26 +00001579 (void) in_container;
David Reisscba57272008-02-06 22:09:44 +00001580 switch (tbase->get_base()) {
David Reiss7f42bcf2008-01-11 20:59:12 +00001581 case t_base_type::TYPE_VOID:
1582 return "void";
1583 case t_base_type::TYPE_STRING:
David Reisscba57272008-02-06 22:09:44 +00001584 if (tbase->is_binary()) {
1585 return "byte[]";
1586 } else {
1587 return "string";
1588 }
David Reiss7f42bcf2008-01-11 20:59:12 +00001589 case t_base_type::TYPE_BOOL:
1590 return "bool";
1591 case t_base_type::TYPE_BYTE:
1592 return "byte";
1593 case t_base_type::TYPE_I16:
1594 return "short";
1595 case t_base_type::TYPE_I32:
1596 return "int";
1597 case t_base_type::TYPE_I64:
1598 return "long";
1599 case t_base_type::TYPE_DOUBLE:
1600 return "double";
1601 default:
David Reisscba57272008-02-06 22:09:44 +00001602 throw "compiler error: no C# name for base type " + tbase->get_base();
David Reiss7f42bcf2008-01-11 20:59:12 +00001603 }
1604}
1605
Bryan Duxburyc43ec622010-09-02 14:58:51 +00001606string t_csharp_generator::declare_field(t_field* tfield, bool init, std::string prefix) {
1607 string result = type_name(tfield->get_type()) + " " + prefix + tfield->get_name();
David Reiss7f42bcf2008-01-11 20:59:12 +00001608 if (init) {
1609 t_type* ttype = tfield->get_type();
1610 while (ttype->is_typedef()) {
1611 ttype = ((t_typedef*)ttype)->get_type();
1612 }
1613 if (ttype->is_base_type() && tfield->get_value() != NULL) {
1614 ofstream dummy;
1615 result += " = " + render_const_value(dummy, tfield->get_name(), ttype, tfield->get_value());
1616 } else if (ttype->is_base_type()) {
1617 t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
1618 switch (tbase) {
1619 case t_base_type::TYPE_VOID:
1620 throw "NO T_VOID CONSTRUCT";
1621 case t_base_type::TYPE_STRING:
1622 result += " = null";
1623 break;
1624 case t_base_type::TYPE_BOOL:
1625 result += " = false";
1626 break;
1627 case t_base_type::TYPE_BYTE:
1628 case t_base_type::TYPE_I16:
1629 case t_base_type::TYPE_I32:
1630 case t_base_type::TYPE_I64:
1631 result += " = 0";
1632 break;
1633 case t_base_type::TYPE_DOUBLE:
1634 result += " = (double)0";
1635 break;
1636 }
1637 } else if (ttype->is_enum()) {
1638 result += " = (" + type_name(ttype, false, true) + ")0";
1639 } else if (ttype->is_container()) {
1640 result += " = new " + type_name(ttype, false, true) + "()";
1641 } else {
1642 result += " = new " + type_name(ttype, false, true) + "()";
1643 }
1644 }
1645 return result + ";";
1646}
1647
1648string t_csharp_generator::function_signature(t_function* tfunction, string prefix) {
1649 t_type* ttype = tfunction->get_returntype();
1650 return type_name(ttype) + " " + prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ")";
1651}
1652
1653string t_csharp_generator::argument_list(t_struct* tstruct) {
1654 string result = "";
1655 const vector<t_field*>& fields = tstruct->get_members();
1656 vector<t_field*>::const_iterator f_iter;
1657 bool first = true;
1658 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1659 if (first) {
1660 first = false;
1661 } else {
1662 result += ", ";
1663 }
1664 result += type_name((*f_iter)->get_type()) + " " + (*f_iter)->get_name();
1665 }
1666 return result;
1667}
1668
1669string t_csharp_generator::type_to_enum(t_type* type) {
1670 while (type->is_typedef()) {
1671 type = ((t_typedef*)type)->get_type();
1672 }
1673
1674 if (type->is_base_type()) {
1675 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1676 switch (tbase) {
1677 case t_base_type::TYPE_VOID:
1678 throw "NO T_VOID CONSTRUCT";
1679 case t_base_type::TYPE_STRING:
1680 return "TType.String";
1681 case t_base_type::TYPE_BOOL:
1682 return "TType.Bool";
1683 case t_base_type::TYPE_BYTE:
1684 return "TType.Byte";
1685 case t_base_type::TYPE_I16:
1686 return "TType.I16";
1687 case t_base_type::TYPE_I32:
1688 return "TType.I32";
1689 case t_base_type::TYPE_I64:
1690 return "TType.I64";
1691 case t_base_type::TYPE_DOUBLE:
1692 return "TType.Double";
1693 }
1694 } else if (type->is_enum()) {
1695 return "TType.I32";
1696 } else if (type->is_struct() || type->is_xception()) {
1697 return "TType.Struct";
1698 } else if (type->is_map()) {
1699 return "TType.Map";
1700 } else if (type->is_set()) {
1701 return "TType.Set";
1702 } else if (type->is_list()) {
1703 return "TType.List";
1704 }
1705
1706 throw "INVALID TYPE IN type_to_enum: " + type->get_name();
1707}
David Reiss861869b2008-03-27 21:41:23 +00001708
1709
Roger Meier0069cc42010-10-13 18:10:18 +00001710THRIFT_REGISTER_GENERATOR(csharp, "C#", "")
1711