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