Commit | Line | Data |
---|---|---|
970ed795 EL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2014 Ericsson Telecom AB | |
3 | // All rights reserved. This program and the accompanying materials | |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #include <string.h> | |
9 | #include "../common/memory.h" | |
10 | #include "functionref.h" | |
11 | #include "encdec.h" | |
12 | ||
13 | #include "main.hh" | |
14 | #include "error.h" | |
15 | ||
16 | void defFunctionrefClass(const funcref_def *fdef, output_struct *output) | |
17 | { | |
18 | char *def = NULL, *src = NULL; | |
19 | const char *name = fdef->name; | |
20 | const char *dispname = fdef->dispname; | |
21 | ||
22 | const char *return_type, *fat_string; | |
23 | ||
24 | if (fdef->return_type != NULL) return_type = fdef->return_type; | |
25 | else return_type = "void"; | |
26 | ||
27 | switch (fdef->type) { | |
28 | case FUNCTION: | |
29 | fat_string = "function"; | |
30 | break; | |
31 | case ALTSTEP: | |
32 | fat_string = "altstep"; | |
33 | break; | |
34 | case TESTCASE: | |
35 | fat_string = "testcase"; | |
36 | break; | |
37 | default: | |
38 | fat_string = NULL; | |
39 | FATAL_ERROR("defFunctionrefClass(): invalid type"); | |
40 | } | |
41 | ||
42 | /* class declaration code */ | |
43 | output->header.class_decls = mputprintf(output->header.class_decls, | |
44 | "class %s;\n", name); | |
45 | ||
46 | /* class definition */ | |
47 | def = mputprintf(def, | |
48 | #ifndef NDEBUG | |
49 | "// written by %s in " __FILE__ " at %d\n" | |
50 | #endif | |
51 | "class %s : public Base_Type {\n" | |
52 | "public:\n" | |
53 | #ifndef NDEBUG | |
54 | , __FUNCTION__, __LINE__ | |
55 | #endif | |
56 | , name); | |
57 | ||
58 | switch(fdef->type){ | |
59 | case FUNCTION: | |
60 | def = mputstr(def, "typedef "); | |
61 | /* work-around for GCC versions earlier than 3.4: | |
62 | * parse error occurs within the typedef if the return type is the same | |
63 | * as the function type itself */ | |
64 | if (!strcmp(name, return_type)) def = mputstr(def, "class "); | |
65 | def = mputprintf(def, "%s (*function_pointer)(%s);\n" | |
66 | "typedef void (*start_pointer)(const COMPONENT& " | |
67 | "component_reference", return_type, fdef->formal_par_list); | |
68 | if (fdef->formal_par_list[0] != '\0') def = mputstr(def, ", "); | |
69 | def = mputprintf(def, "%s);\n", fdef->formal_par_list); | |
70 | break; | |
71 | case ALTSTEP: | |
72 | def = mputprintf(def, "typedef void (*standalone_pointer)(%s);\n" | |
73 | "typedef Default_Base* (*activate_pointer)(%s);\n" | |
74 | "typedef alt_status (*function_pointer)(%s);\n", fdef->formal_par_list, | |
75 | fdef->formal_par_list, fdef->formal_par_list); | |
76 | break; | |
77 | case TESTCASE: | |
78 | def = mputprintf(def, "typedef verdicttype (*function_pointer)(%s);\n", | |
79 | fdef->formal_par_list); | |
80 | break; | |
81 | } | |
82 | ||
83 | def = mputprintf(def, | |
84 | "private:\n" | |
85 | "friend class %s_template;\n" | |
86 | "friend boolean operator==(%s::function_pointer value, " | |
87 | "const %s& other_value);\n" | |
88 | "function_pointer referred_function;\n" | |
89 | , name, name, name); | |
90 | ||
91 | /* default constructor */ | |
92 | def = mputprintf(def,"public:\n" | |
93 | "%s();\n", name); | |
94 | src = mputprintf(src,"%s::%s()\n" | |
95 | "{\n" | |
96 | "referred_function = NULL;\n" | |
97 | "}\n\n", name, name); | |
98 | ||
99 | def = mputprintf(def,"%s(function_pointer other_value);\n", name); | |
100 | src = mputprintf(src,"%s::%s(function_pointer other_value)\n" | |
101 | "{\n" | |
102 | "referred_function = other_value;\n" | |
103 | "}\n\n", name, name); | |
104 | ||
105 | /* copy constructor */ | |
106 | def = mputprintf(def,"%s(const %s& other_value);\n", name, name); | |
107 | src = mputprintf(src,"%s::%s(const %s& other_value)\n" | |
108 | ": Base_Type()" /* call the *default* constructor as before */ | |
109 | "{\n" | |
110 | "other_value.must_bound(\"Copying an unbound %s value.\");\n" | |
111 | "referred_function = other_value.referred_function;\n" | |
112 | "}\n\n", name, name, name, dispname); | |
113 | ||
114 | /* operator= */ | |
115 | def =mputprintf(def,"%s& operator=(function_pointer other_value);\n", name); | |
116 | src =mputprintf(src,"%s& %s::operator=(function_pointer other_value)\n" | |
117 | "{\n" | |
118 | "referred_function = other_value;\n" | |
119 | "return *this;\n" | |
120 | "}\n\n", name, name); | |
121 | ||
122 | def = mputprintf(def,"%s& operator=(const %s& other_value);\n", | |
123 | name, name); | |
124 | src = mputprintf(src,"%s& %s::operator=(const %s& other_value)\n" | |
125 | "{\n" | |
126 | "other_value.must_bound(\"Assignment of an unbound value.\");\n" | |
127 | "referred_function = other_value.referred_function;\n" | |
128 | "return *this;\n" | |
129 | "}\n\n", name, name, name); | |
130 | ||
131 | /* operator ==*/ | |
132 | def = mputstr(def,"boolean operator==(function_pointer other_value) " | |
133 | "const;\n"); | |
134 | src = mputprintf(src,"boolean %s::operator==(function_pointer other_value) " | |
135 | "const\n" | |
136 | "{\n" | |
137 | "must_bound(\"Unbound left operand of %s comparison.\");\n" | |
138 | "return referred_function == other_value;\n" | |
139 | "}\n\n", name, dispname); | |
140 | def = mputprintf(def,"boolean operator==(const %s& other_value) const;\n" | |
141 | , name); | |
142 | src = mputprintf(src,"boolean %s::operator==(const %s& other_value) const\n" | |
143 | "{\n" | |
144 | "must_bound(\"Unbound left operand of %s comparison.\");\n" | |
145 | "other_value.must_bound(\"Unbound right operand of %s comparison.\");\n" | |
146 | "return referred_function == other_value.referred_function;\n" | |
147 | "}\n\n", name, name, dispname, dispname); | |
148 | ||
149 | /* operator != */ | |
150 | def = mputprintf(def,"inline boolean operator!=(function_pointer other_value)" | |
151 | " const\n" | |
152 | "{ return !(*this == other_value); }\n" | |
153 | "inline boolean operator!=(const %s& other_value) const\n" | |
154 | "{ return !(*this == other_value); }\n\n", name); | |
155 | ||
156 | switch(fdef->type) { | |
157 | case FUNCTION: | |
158 | def = mputprintf(def,"%s invoke(%s) const;\n" | |
159 | , return_type, fdef->formal_par_list); | |
160 | src = mputprintf(src,"%s %s::invoke(%s) const\n" | |
161 | "{\n" | |
162 | "must_bound(\"Call of unbound function.\");\n" | |
163 | "if(referred_function == " | |
164 | "(%s::function_pointer)Module_List::get_fat_null())\n" | |
165 | "TTCN_error(\"null reference cannot be invoked.\");\n" | |
166 | "%sreferred_function(%s);\n" | |
167 | "}\n\n", return_type, name, fdef->formal_par_list, name | |
168 | , fdef->return_type!= NULL? "return ": "", fdef->actual_par_list); | |
169 | if(fdef->is_startable) { | |
170 | def = mputprintf(def,"void start(const COMPONENT& component_reference%s" | |
171 | "%s) const;\n", strcmp(fdef->formal_par_list,"")?", ":"" | |
172 | , fdef->formal_par_list); | |
173 | src = mputprintf(src,"void %s::start(const COMPONENT& " | |
174 | "component_reference%s%s) const\n{\n" | |
175 | "((%s::start_pointer)Module_List::lookup_start_by_function_address" | |
176 | "((genericfunc_t)referred_function))(component_reference%s%s);\n" | |
177 | "}\n\n", name, strcmp(fdef->formal_par_list,"")?", ":"" | |
178 | , fdef->formal_par_list, name, strcmp(fdef->formal_par_list,"")?", ":"" | |
179 | , fdef->actual_par_list); | |
180 | } | |
181 | break; | |
182 | case ALTSTEP: | |
183 | def = mputprintf(def,"void invoke_standalone(%s) const;\n", | |
184 | fdef->formal_par_list); | |
185 | src = mputprintf(src,"void %s::invoke_standalone(%s) const\n" | |
186 | "{\n" | |
187 | "((%s::standalone_pointer)" | |
188 | "Module_List::lookup_standalone_address_by_altstep_address(" | |
189 | "(genericfunc_t)referred_function))(%s);\n" | |
190 | "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list); | |
191 | ||
192 | def = mputprintf(def,"Default_Base *activate(%s) const;\n" | |
193 | , fdef->formal_par_list); | |
194 | src = mputprintf(src,"Default_Base *%s::activate(%s) const\n" | |
195 | "{\n" | |
196 | "return ((%s::activate_pointer)" | |
197 | "Module_List::lookup_activate_address_by_altstep_address(" | |
198 | "(genericfunc_t)referred_function))(%s);\n" | |
199 | "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list); | |
200 | ||
201 | def = mputprintf(def,"alt_status invoke(%s) const;\n" | |
202 | , fdef->formal_par_list); | |
203 | src = mputprintf(src,"alt_status %s::invoke(%s) const\n" | |
204 | "{\n" | |
205 | "must_bound(\"Call of an unbound altstep.\");\n" | |
206 | "if(referred_function == " | |
207 | "(%s::function_pointer)Module_List::get_fat_null())\n" | |
208 | "TTCN_error(\"null reference cannot be invoked.\");\n" | |
209 | "return referred_function(%s);\n" | |
210 | "}\n", name, fdef->formal_par_list, name, fdef->actual_par_list); | |
211 | ||
212 | break; | |
213 | case TESTCASE: | |
214 | def = mputprintf(def,"verdicttype execute(%s) const;\n", | |
215 | fdef->formal_par_list); | |
216 | src = mputprintf(src,"verdicttype %s::execute(%s) const\n" | |
217 | "{\n" | |
218 | "must_bound(\"Call of unbound testcase.\");\n" | |
219 | "if(referred_function == " | |
220 | "(%s::function_pointer)Module_List::get_fat_null())\n" | |
221 | "TTCN_error(\"null reference cannot be executed.\");\n" | |
222 | "return referred_function(%s);\n" | |
223 | "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list); | |
224 | break; | |
225 | } | |
226 | ||
227 | /* bound check */ | |
228 | def = mputstr(def,"inline boolean is_bound() " | |
229 | "const { return referred_function != NULL; }\n"); | |
230 | /* value check */ | |
231 | def = mputstr(def,"inline boolean is_value() " | |
232 | "const { return referred_function != NULL; }\n"); | |
233 | def = mputstr(def,"inline void clean_up() " | |
234 | "{ referred_function = NULL; }\n"); | |
235 | def = mputstr(def,"inline void must_bound(const char *err_msg) const\n" | |
236 | "{ if (referred_function == NULL) TTCN_error(\"%s\", err_msg); }\n\n"); | |
237 | ||
238 | ||
239 | ||
240 | ||
241 | if (use_runtime_2) { | |
242 | /* functions in alternative runtime */ | |
243 | def = mputstr(def, | |
244 | "boolean is_equal(const Base_Type* other_value) const;\n" | |
245 | "void set_value(const Base_Type* other_value);\n" | |
246 | "Base_Type* clone() const;\n" | |
247 | "const TTCN_Typedescriptor_t* get_descriptor() const;\n"); | |
248 | src = mputprintf(src, | |
249 | "boolean %s::is_equal(const Base_Type* other_value) const " | |
250 | "{ return *this == *(static_cast<const %s*>(other_value)); }\n" | |
251 | "void %s::set_value(const Base_Type* other_value) " | |
252 | "{ *this = *(static_cast<const %s*>(other_value)); }\n" | |
253 | "Base_Type* %s::clone() const { return new %s(*this); }\n" | |
254 | "const TTCN_Typedescriptor_t* %s::get_descriptor() const " | |
255 | "{ return &%s_descr_; }\n", | |
256 | name, name, | |
257 | name, name, | |
258 | name, name, | |
259 | name, name); | |
260 | } else { | |
261 | def = mputstr(def, | |
262 | "inline boolean is_present() const { return is_bound(); }\n"); | |
263 | } | |
264 | ||
265 | /* log */ | |
266 | def = mputstr(def,"void log() const;\n"); | |
267 | src = mputprintf(src,"void %s::log() const\n" | |
268 | "{\n" | |
269 | "Module_List::log_%s((genericfunc_t)referred_function);\n" | |
270 | "}\n\n",name, fat_string); | |
271 | ||
272 | /* set_param */ | |
273 | def = mputstr(def,"void set_param(Module_Param& param);\n"); | |
274 | src = mputprintf(src,"void %s::set_param(Module_Param& param)\n" | |
275 | "{\n" | |
276 | " param.error(\"Not supported.\");\n" | |
277 | "}\n\n", name); | |
278 | ||
279 | /* encode_text / decode_text */ | |
280 | def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n"); | |
281 | src = mputprintf(src,"void %s::encode_text(Text_Buf&", name); | |
282 | if (fdef->runs_on_self) { | |
283 | src = mputprintf(src, ") const\n" | |
284 | "{\n" | |
285 | "TTCN_error(\"Values of type %s cannot be sent to " | |
286 | "other test components.\");\n", dispname); | |
287 | } else { | |
288 | src = mputprintf(src, " text_buf) const\n" | |
289 | "{\n" | |
290 | "Module_List::encode_%s(text_buf," | |
291 | "(genericfunc_t)referred_function);\n", fat_string); | |
292 | } | |
293 | src = mputstr(src,"}\n\n"); | |
294 | def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n"); | |
295 | src = mputprintf(src,"void %s::decode_text(Text_Buf&", name); | |
296 | if (fdef->runs_on_self) { | |
297 | src = mputprintf(src, ")\n" | |
298 | "{\n" | |
299 | "TTCN_error(\"Values of type %s cannot be received " | |
300 | "from other test components.\");\n", dispname); | |
301 | } else { | |
302 | src = mputprintf(src, " text_buf)\n" | |
303 | "{\n" | |
304 | "Module_List::decode_%s(text_buf," | |
305 | "(genericfunc_t*)&referred_function);\n", fat_string); | |
306 | } | |
307 | src = mputstr(src,"}\n\n"); | |
308 | def = mputstr(def, "};\n\n"); | |
309 | ||
310 | def = mputprintf(def,"extern boolean operator==(%s::function_pointer value," | |
311 | " const %s& other_value);\n", name, name); | |
312 | src = mputprintf(src,"boolean operator==(%s::function_pointer value, " | |
313 | "const %s& other_value)\n" | |
314 | "{\n" | |
315 | "other_value.must_bound(\"Unbound right operand of %s comparison.\");\n" | |
316 | "return value == other_value.referred_function;\n" | |
317 | "}\n\n", name, name, dispname); | |
318 | def = mputprintf(def,"inline boolean operator!=(%s::function_pointer value," | |
319 | " const %s& other_value)\n" | |
320 | "{ return !(value == other_value); } \n\n", name, name); | |
321 | ||
322 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
323 | Free(def); | |
324 | ||
325 | output->source.methods = mputstr(output->source.methods, src); | |
326 | Free(src); | |
327 | } | |
328 | ||
329 | void defFunctionrefTemplate(const funcref_def *fdef, output_struct *output) | |
330 | { | |
331 | char *def = NULL, *src = NULL; | |
332 | const char *name = fdef->name; | |
333 | const char *dispname = fdef->dispname; | |
334 | char *fat_string = NULL; | |
335 | ||
336 | switch(fdef->type) { | |
337 | case FUNCTION: | |
338 | fat_string = mputstr(fat_string, "function"); | |
339 | break; | |
340 | case ALTSTEP: | |
341 | fat_string = mputstr(fat_string, "altstep"); | |
342 | break; | |
343 | case TESTCASE: | |
344 | fat_string = mputstr(fat_string, "testcase"); | |
345 | break; | |
346 | } | |
347 | ||
348 | /* class declaration */ | |
349 | output->header.class_decls = mputprintf(output->header.class_decls, | |
350 | "class %s_template;\n", name); | |
351 | ||
352 | /* class definition */ | |
353 | def = mputprintf(def,"class %s_template : public Base_Template {\n" | |
354 | "union {\n" | |
355 | "%s::function_pointer single_value;\n" | |
356 | "struct {\n" | |
357 | "unsigned int n_values;\n" | |
358 | "%s_template *list_value;\n" | |
359 | "} value_list;\n" | |
360 | "};\n\n", name, name, name); | |
361 | ||
362 | /* copy template */ | |
363 | def = mputprintf(def," void copy_template(const %s_template& other_value);\n" | |
364 | , name); | |
365 | src = mputprintf(src,"void %s_template::copy_template(const %s_template& " | |
366 | "other_value)\n" | |
367 | "{\n" | |
368 | "switch(other_value.template_selection) {\n" | |
369 | "case SPECIFIC_VALUE:\n" | |
370 | "single_value = other_value.single_value;\n" | |
371 | "break;\n" | |
372 | "case OMIT_VALUE:\n" | |
373 | "case ANY_VALUE:\n" | |
374 | "case ANY_OR_OMIT:\n" | |
375 | "break;\n" | |
376 | "case VALUE_LIST:\n" | |
377 | "case COMPLEMENTED_LIST:\n" | |
378 | "value_list.n_values = other_value.value_list.n_values;\n" | |
379 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
380 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
381 | "value_list.list_value[i] = other_value.value_list.list_value[i];\n" | |
382 | "break;\n" | |
383 | "default:\n" | |
384 | "TTCN_error(\"Copying an unitialized/unsupported %s template.\");\n" | |
385 | "}\n" | |
386 | "set_selection(other_value);\n" | |
387 | "}\n\n", name, name, name, dispname); | |
388 | ||
389 | /* constructors */ | |
390 | def = mputprintf(def,"public:\n" | |
391 | "%s_template();\n", name); | |
392 | src = mputprintf(src,"%s_template::%s_template()\n" | |
393 | "{\n}\n\n", name, name); | |
394 | def = mputprintf(def,"%s_template(template_sel other_value);\n", name); | |
395 | src = mputprintf(src,"%s_template::%s_template(template_sel other_value)\n" | |
396 | " : Base_Template(other_value)\n" | |
397 | "{\n" | |
398 | "check_single_selection(other_value);\n" | |
399 | "}\n\n", name, name); | |
400 | def = mputprintf(def,"%s_template(%s::function_pointer other_value);\n" | |
401 | , name, name); | |
402 | src = mputprintf(src,"%s_template::%s_template(%s::function_pointer " | |
403 | "other_value)\n" | |
404 | " : Base_Template(SPECIFIC_VALUE)\n" | |
405 | "{\n" | |
406 | "single_value = other_value;\n" | |
407 | "}\n\n", name, name, name); | |
408 | def = mputprintf(def,"%s_template(const %s& other_value);\n", name, name); | |
409 | src = mputprintf(src,"%s_template::%s_template(const %s& other_value)\n" | |
410 | " :Base_Template(SPECIFIC_VALUE)\n" | |
411 | "{\n" | |
412 | "other_value.must_bound(\"Creating a template from an unbound %s value." | |
413 | "\");\n" | |
414 | "single_value = other_value.referred_function;\n" | |
415 | "}\n\n", name, name, name, dispname); | |
416 | def = mputprintf(def,"%s_template(const OPTIONAL<%s>& other_value);\n" | |
417 | , name, name); | |
418 | src = mputprintf(src,"%s_template::%s_template(const OPTIONAL<%s>& " | |
419 | "other_value)\n" | |
420 | "{\n" | |
421 | "if(other_value.ispresent()) {\n" | |
422 | "set_selection(SPECIFIC_VALUE);\n" | |
423 | "single_value = ((const %s&)other_value()).referred_function;\n" | |
424 | "} else set_selection(OMIT_VALUE);\n" | |
425 | "}\n\n", name, name, name, name); | |
426 | def = mputprintf(def,"%s_template(const %s_template& other_value);\n" | |
427 | , name, name); | |
428 | src = mputprintf(src,"%s_template::%s_template(const %s_template& " | |
429 | "other_value)\n" | |
430 | " :Base_Template()\n" /* yes, the default constructor */ | |
431 | "{\n" | |
432 | "copy_template(other_value);\n" | |
433 | "}\n\n", name, name, name); | |
434 | ||
435 | /* destructor */ | |
436 | def = mputprintf(def,"~%s_template();\n", name); | |
437 | src = mputprintf(src,"%s_template::~%s_template()\n" | |
438 | "{\n" | |
439 | " clean_up();\n" | |
440 | "}\n\n", name, name); | |
441 | ||
442 | /* clean up */ | |
443 | def = mputstr(def,"void clean_up();\n"); | |
444 | src = mputprintf(src,"void %s_template::clean_up()" | |
445 | "{\n" | |
446 | "if(template_selection == VALUE_LIST ||\n" | |
447 | "template_selection == COMPLEMENTED_LIST)\n" | |
448 | "delete[] value_list.list_value;\n" | |
449 | "template_selection = UNINITIALIZED_TEMPLATE;\n" | |
450 | "}\n\n", name); | |
451 | ||
452 | /* operator = */ | |
453 | def = mputprintf(def,"%s_template& operator=(template_sel other_value);\n" | |
454 | , name); | |
455 | src = mputprintf(src,"%s_template& %s_template::operator=(template_sel " | |
456 | "other_value)\n" | |
457 | "{\n" | |
458 | "check_single_selection(other_value);\n" | |
459 | "clean_up();\n" | |
460 | "set_selection(other_value);\n" | |
461 | "return *this;\n" | |
462 | "}\n\n", name, name); | |
463 | def = mputprintf(def,"%s_template& operator=(%s::function_pointer " | |
464 | "other_value);\n", name, name); | |
465 | src = mputprintf(src,"%s_template& %s_template::operator=" | |
466 | "(%s::function_pointer other_value)\n" | |
467 | "{\n" | |
468 | "clean_up();\n" | |
469 | "set_selection(SPECIFIC_VALUE);\n" | |
470 | "single_value = other_value;\n" | |
471 | "return *this;" | |
472 | "}\n\n", name, name, name); | |
473 | def = mputprintf(def,"%s_template& operator=(const %s& other_value);\n" | |
474 | , name, name); | |
475 | src = mputprintf(src,"%s_template& %s_template::operator=" | |
476 | "(const %s& other_value)\n" | |
477 | "{\n" | |
478 | "other_value.must_bound(\"Assignment of an unbound %s value to a " | |
479 | "template.\");\n" | |
480 | "clean_up();\n" | |
481 | "set_selection(SPECIFIC_VALUE);\n" | |
482 | "single_value = other_value.referred_function;\n" | |
483 | "return *this;\n" | |
484 | "}\n\n", name, name, name, dispname); | |
485 | def = mputprintf(def,"%s_template& operator=(const OPTIONAL<%s>& " | |
486 | "other_value);\n", name, name); | |
487 | src = mputprintf(src,"%s_template& %s_template::operator=(const " | |
488 | "OPTIONAL<%s>& other_value)\n" | |
489 | "{\n" | |
490 | "clean_up();\n" | |
491 | "if(other_value.ispresent()) { \n" | |
492 | "set_selection(SPECIFIC_VALUE);\n" | |
493 | "single_value = ((const %s&)other_value()).referred_function;\n" | |
494 | "} else set_selection(OMIT_VALUE);\n" | |
495 | "return *this;" | |
496 | "}\n\n", name, name, name, name); | |
497 | def = mputprintf(def,"%s_template& operator=(const %s_template& " | |
498 | "other_value);\n", name, name); | |
499 | src = mputprintf(src,"%s_template& %s_template::operator=(const %s_template& " | |
500 | "other_value)\n" | |
501 | "{\n" | |
502 | "if(&other_value != this) {\n" | |
503 | "clean_up();" | |
504 | "copy_template(other_value);\n" | |
505 | "}\n" | |
506 | "return *this;\n" | |
507 | "}\n\n", name ,name, name); | |
508 | ||
509 | /* match functions */ | |
510 | def = mputprintf(def,"boolean match(%s::function_pointer " | |
511 | "other_value) const;\n", name); | |
512 | src = mputprintf(src,"boolean %s_template::match(%s::function_pointer " | |
513 | "other_value) const\n" | |
514 | "{\n" | |
515 | "switch(template_selection) {\n" | |
516 | "case SPECIFIC_VALUE:\n" | |
517 | "return single_value == other_value;\n" | |
518 | "case OMIT_VALUE:\n" | |
519 | "return FALSE;\n" | |
520 | "case ANY_VALUE:\n" | |
521 | "case ANY_OR_OMIT:\n" | |
522 | "return TRUE;\n" | |
523 | "case VALUE_LIST:\n" | |
524 | "case COMPLEMENTED_LIST:\n" | |
525 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
526 | "if(value_list.list_value[i].match(other_value))\n" | |
527 | "return template_selection == VALUE_LIST;\n" | |
528 | "return template_selection == COMPLEMENTED_LIST;\n" | |
529 | "default:\n" | |
530 | "TTCN_error(\"Matching with an unitialized/unsupported %s template." | |
531 | "\");\n" | |
532 | "};\n" | |
533 | "return FALSE;\n" | |
534 | "}\n\n", name, name, dispname); | |
535 | def = mputprintf(def,"boolean match(const %s& other_value) const;\n", name); | |
536 | src = mputprintf(src,"boolean %s_template::match(const %s& other_value) " | |
537 | "const\n" | |
538 | "{\n" | |
539 | " if (!other_value.is_bound()) return FALSE;\n" | |
540 | "return match(other_value.referred_function);\n" | |
541 | "}\n\n", name, name); | |
542 | ||
543 | /* value of function */ | |
544 | def = mputprintf(def,"%s valueof() const;\n", name); | |
545 | src = mputprintf(src,"%s %s_template::valueof() const\n" | |
546 | "{\n" | |
547 | "if(template_selection != SPECIFIC_VALUE || is_ifpresent)\n" | |
548 | "TTCN_error(\"Performing a valueof or send operation on a " | |
549 | "non-specific %s template.\");\n" | |
550 | "return single_value;\n}\n\n", name, name, dispname); | |
551 | ||
552 | /* set type */ | |
553 | def = mputstr(def,"void set_type(template_sel template_type, " | |
554 | "unsigned int list_length);\n"); | |
555 | src = mputprintf(src,"void %s_template::set_type(template_sel template_type, " | |
556 | "unsigned int list_length)\n" | |
557 | "{\n" | |
558 | "if(template_type != VALUE_LIST && " | |
559 | "template_type != COMPLEMENTED_LIST)\n" | |
560 | "TTCN_error(\"Setting an invalid type for an %s template.\");\n" | |
561 | "clean_up();\n" | |
562 | "set_selection(template_type);\n" | |
563 | "value_list.n_values = list_length;\n" | |
564 | "value_list.list_value = new %s_template[list_length];\n" | |
565 | "}\n\n", name, dispname, name); | |
566 | ||
567 | /* list item */ | |
568 | def = mputprintf(def,"%s_template& list_item(unsigned int list_index) " | |
569 | "const;\n", name); | |
570 | src = mputprintf(src,"%s_template& %s_template::list_item(" | |
571 | "unsigned int list_index) const\n" | |
572 | "{\n" | |
573 | "if(template_selection != VALUE_LIST && " | |
574 | "template_selection != COMPLEMENTED_LIST)\n" | |
575 | "TTCN_error(\"Accessing a list element of a non-list template of " | |
576 | "type %s\");\n" | |
577 | "if(list_index >= value_list.n_values)\n" | |
578 | "TTCN_error(\"Index overflow in a value list template of type %s." | |
579 | "\");\n" | |
580 | "return value_list.list_value[list_index];\n" | |
581 | "}\n\n", name, name, dispname, dispname); | |
582 | ||
583 | if (use_runtime_2) { | |
584 | /* functions in alternative runtime */ | |
585 | def = mputstr(def, | |
586 | "void valueofv(Base_Type* value) const;\n" | |
587 | "void set_value(template_sel other_value);\n" | |
588 | "void copy_value(const Base_Type* other_value);\n" | |
589 | "Base_Template* clone() const;\n" | |
590 | "const TTCN_Typedescriptor_t* get_descriptor() const;\n" | |
591 | "boolean matchv(const Base_Type* other_value) const;\n" | |
592 | "void log_matchv(const Base_Type* match_value) const;\n"); | |
593 | src = mputprintf(src, | |
594 | "void %s_template::valueofv(Base_Type* value) const " | |
595 | "{ *(static_cast<%s*>(value)) = valueof(); }\n" | |
596 | "void %s_template::set_value(template_sel other_value) " | |
597 | "{ *this = other_value; }\n" | |
598 | "void %s_template::copy_value(const Base_Type* other_value) " | |
599 | "{ *this = *(static_cast<const %s*>(other_value)); }\n" | |
600 | "Base_Template* %s_template::clone() const " | |
601 | "{ return new %s_template(*this); }\n" | |
602 | "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const " | |
603 | "{ return &%s_descr_; }\n" | |
604 | "boolean %s_template::matchv(const Base_Type* other_value) const " | |
605 | "{ return match(*(static_cast<const %s*>(other_value))); }\n" | |
606 | "void %s_template::log_matchv(const Base_Type* match_value) const " | |
607 | " { log_match(*(static_cast<const %s*>(match_value))); }\n", | |
608 | name, name, | |
609 | name, | |
610 | name, name, | |
611 | name, name, | |
612 | name, name, | |
613 | name, name, | |
614 | name, name); | |
615 | } | |
616 | ||
617 | /* log function */ | |
618 | def = mputstr(def,"void log() const;\n"); | |
619 | src = mputprintf(src,"void %s_template::log() const\n" | |
620 | "{\n" | |
621 | "switch(template_selection) {\n" | |
622 | "case SPECIFIC_VALUE:\n" | |
623 | "Module_List::log_%s((genericfunc_t)single_value);\n" | |
624 | "break;\n" | |
625 | "case COMPLEMENTED_LIST:\n" | |
626 | "TTCN_Logger::log_event_str(\"complement \");\n" | |
627 | "case VALUE_LIST:\n" | |
628 | "TTCN_Logger::log_char('(');\n" | |
629 | "for(unsigned int i = 0; i < value_list.n_values; i++) {\n" | |
630 | "if(i > 0) TTCN_Logger::log_event_str(\", \");\n" | |
631 | "value_list.list_value[i].log();\n" | |
632 | "}\n" | |
633 | "TTCN_Logger::log_char(')');\n" | |
634 | "break;\n" | |
635 | "default:\n" | |
636 | "log_generic();\n" | |
637 | "}\n" | |
638 | "log_ifpresent();\n" | |
639 | "}\n\n", name, fat_string); | |
640 | ||
641 | /* log_match function */ | |
642 | def = mputprintf(def,"void log_match(const %s& match_value) const;\n", name); | |
643 | src = mputprintf(src,"void %s_template::log_match(const %s& match_value) " | |
644 | "const\n" | |
645 | "{\n" | |
646 | "log();\n" | |
647 | "TTCN_Logger::log_event_str(\" with \");\n" | |
648 | "match_value.log();\n" | |
649 | "if(match(match_value)) TTCN_Logger::log_event_str(\" matched\");\n" | |
650 | "else TTCN_Logger::log_event_str(\" unmatched\");\n" | |
651 | "}\n\n", name, name); | |
652 | ||
653 | /* encode_text / decode_text */ | |
654 | def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n"); | |
655 | src = mputprintf(src,"void %s_template::encode_text(Text_Buf&", name); | |
656 | if (fdef->runs_on_self) { | |
657 | src = mputprintf(src, ") const\n" | |
658 | "{\n" | |
659 | "TTCN_error(\"Templates of type %s cannot be sent to " | |
660 | "other test components.\");\n", dispname); | |
661 | } else { | |
662 | src = mputprintf(src, " text_buf) const\n" | |
663 | "{\n" | |
664 | "encode_text_base(text_buf);\n" | |
665 | "switch(template_selection) {\n" | |
666 | "case OMIT_VALUE:\n" | |
667 | "case ANY_VALUE:\n" | |
668 | "case ANY_OR_OMIT:\n" | |
669 | "break;\n" | |
670 | "case SPECIFIC_VALUE:\n" | |
671 | "Module_List::encode_%s(text_buf, (genericfunc_t)single_value);\n" | |
672 | "break;\n" | |
673 | "case VALUE_LIST:\n" | |
674 | "case COMPLEMENTED_LIST:\n" | |
675 | "text_buf.push_int(value_list.n_values);\n" | |
676 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
677 | "value_list.list_value[i].encode_text(text_buf);\n" | |
678 | "break;\n" | |
679 | "default:\n" | |
680 | "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported template " | |
681 | "of type %s.\");\n" | |
682 | "}\n", fat_string, dispname); | |
683 | } | |
684 | src = mputstr(src,"}\n\n"); | |
685 | def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n"); | |
686 | src = mputprintf(src,"void %s_template::decode_text(Text_Buf&", name); | |
687 | if (fdef->runs_on_self) { | |
688 | src = mputprintf(src, ")\n" | |
689 | "{\n" | |
690 | "TTCN_error(\"Templates of type %s cannot be received " | |
691 | "from other test components.\");\n", dispname); | |
692 | } else { | |
693 | src = mputprintf(src, " text_buf)\n" | |
694 | "{\n" | |
695 | "clean_up();\n" | |
696 | "decode_text_base(text_buf);\n" | |
697 | "switch(template_selection) {\n" | |
698 | "case OMIT_VALUE:\n" | |
699 | "case ANY_VALUE:\n" | |
700 | "case ANY_OR_OMIT:\n" | |
701 | "break;\n" | |
702 | "case SPECIFIC_VALUE:\n" | |
703 | "Module_List::decode_%s(text_buf,(genericfunc_t*)&single_value);\n" | |
704 | "break;\n" | |
705 | "case VALUE_LIST:\n" | |
706 | "case COMPLEMENTED_LIST:\n" | |
707 | "value_list.n_values = text_buf.pull_int().get_val();\n" | |
708 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
709 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
710 | "value_list.list_value[i].decode_text(text_buf);\n" | |
711 | "default:\n" | |
712 | "TTCN_error(\"Text decoder: An unknown/unsupported selection was received " | |
713 | "in a template of type %s.\");\n" | |
714 | "}\n", fat_string, name, dispname); | |
715 | } | |
716 | src = mputstr(src,"}\n\n"); | |
717 | ||
718 | /* TTCN-3 ispresent() function */ | |
719 | def = mputstr(def, "boolean is_present() const;\n"); | |
720 | src = mputprintf(src, | |
721 | "boolean %s_template::is_present() const\n" | |
722 | "{\n" | |
723 | "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n" | |
724 | "return !match_omit();\n" | |
725 | "}\n\n", name); | |
726 | ||
727 | /* match_omit() */ | |
728 | def = mputstr(def, "boolean match_omit() const;\n"); | |
729 | src = mputprintf(src, | |
730 | "boolean %s_template::match_omit() const\n" | |
731 | "{\n" | |
732 | "if (is_ifpresent) return TRUE;\n" | |
733 | "switch (template_selection) {\n" | |
734 | "case OMIT_VALUE:\n" | |
735 | "case ANY_OR_OMIT:\n" | |
736 | "return TRUE;\n" | |
737 | "case VALUE_LIST:\n" | |
738 | "case COMPLEMENTED_LIST:\n" | |
739 | "for (unsigned int i=0; i<value_list.n_values; i++)\n" | |
740 | "if (value_list.list_value[i].match_omit())\n" | |
741 | "return template_selection==VALUE_LIST;\n" | |
742 | "return template_selection==COMPLEMENTED_LIST;\n" | |
743 | "default:\n" | |
744 | "return FALSE;\n" | |
745 | "}\n" | |
746 | "return FALSE;\n" | |
747 | "}\n\n", name); | |
748 | ||
749 | /* set_param */ | |
750 | def = mputstr(def,"void set_param(Module_Param& param);\n"); | |
751 | src = mputprintf(src,"void %s_template::set_param(Module_Param& param)\n" | |
752 | "{\n" | |
753 | " param.error(\"Not supported.\");\n" | |
754 | "}\n\n", name); | |
755 | ||
756 | if (!use_runtime_2) { | |
757 | /* check template restriction */ | |
758 | def = mputstr(def, "void check_restriction(template_res t_res, " | |
759 | "const char* t_name=NULL) const;\n"); | |
760 | src = mputprintf(src, | |
761 | "void %s_template::check_restriction" | |
762 | "(template_res t_res, const char* t_name) const\n" | |
763 | "{\n" | |
764 | "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n" | |
765 | "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n" | |
766 | "case TR_VALUE:\n" | |
767 | "if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return;\n" | |
768 | "break;\n" | |
769 | "case TR_OMIT:\n" | |
770 | "if (!is_ifpresent && (template_selection==OMIT_VALUE || " | |
771 | "template_selection==SPECIFIC_VALUE)) return;\n" | |
772 | "break;\n" | |
773 | "case TR_PRESENT:\n" | |
774 | "if (!match_omit()) return;\n" | |
775 | "break;\n" | |
776 | "default:\n" | |
777 | "return;\n" | |
778 | "}\n" | |
779 | "TTCN_error(\"Restriction `%%s' on template of type %%s violated.\", " | |
780 | "get_res_name(t_res), t_name ? t_name : \"%s\");\n" | |
781 | "}\n\n", name, dispname); | |
782 | } | |
783 | ||
784 | def = mputstr(def,"};\n\n"); | |
785 | ||
786 | Free(fat_string); | |
787 | ||
788 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
789 | Free(def); | |
790 | ||
791 | output->source.methods = mputstr(output->source.methods, src); | |
792 | Free(src); | |
793 | } |