Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
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); | |
3abe9331 | 278 | |
279 | /* get_param */ | |
280 | def = mputstr(def,"Module_Param* get_param(Module_Param_Name& param_name) const;\n"); | |
281 | src = mputprintf(src,"Module_Param* %s::get_param(Module_Param_Name& /* param_name */) const\n" | |
282 | "{\n" | |
283 | " return NULL;\n" | |
284 | "}\n\n", name); | |
970ed795 EL |
285 | |
286 | /* encode_text / decode_text */ | |
287 | def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n"); | |
288 | src = mputprintf(src,"void %s::encode_text(Text_Buf&", name); | |
289 | if (fdef->runs_on_self) { | |
290 | src = mputprintf(src, ") const\n" | |
291 | "{\n" | |
292 | "TTCN_error(\"Values of type %s cannot be sent to " | |
293 | "other test components.\");\n", dispname); | |
294 | } else { | |
295 | src = mputprintf(src, " text_buf) const\n" | |
296 | "{\n" | |
297 | "Module_List::encode_%s(text_buf," | |
298 | "(genericfunc_t)referred_function);\n", fat_string); | |
299 | } | |
300 | src = mputstr(src,"}\n\n"); | |
301 | def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n"); | |
302 | src = mputprintf(src,"void %s::decode_text(Text_Buf&", name); | |
303 | if (fdef->runs_on_self) { | |
304 | src = mputprintf(src, ")\n" | |
305 | "{\n" | |
306 | "TTCN_error(\"Values of type %s cannot be received " | |
307 | "from other test components.\");\n", dispname); | |
308 | } else { | |
309 | src = mputprintf(src, " text_buf)\n" | |
310 | "{\n" | |
311 | "Module_List::decode_%s(text_buf," | |
312 | "(genericfunc_t*)&referred_function);\n", fat_string); | |
313 | } | |
314 | src = mputstr(src,"}\n\n"); | |
315 | def = mputstr(def, "};\n\n"); | |
316 | ||
317 | def = mputprintf(def,"extern boolean operator==(%s::function_pointer value," | |
318 | " const %s& other_value);\n", name, name); | |
319 | src = mputprintf(src,"boolean operator==(%s::function_pointer value, " | |
320 | "const %s& other_value)\n" | |
321 | "{\n" | |
322 | "other_value.must_bound(\"Unbound right operand of %s comparison.\");\n" | |
323 | "return value == other_value.referred_function;\n" | |
324 | "}\n\n", name, name, dispname); | |
325 | def = mputprintf(def,"inline boolean operator!=(%s::function_pointer value," | |
326 | " const %s& other_value)\n" | |
327 | "{ return !(value == other_value); } \n\n", name, name); | |
328 | ||
329 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
330 | Free(def); | |
331 | ||
332 | output->source.methods = mputstr(output->source.methods, src); | |
333 | Free(src); | |
334 | } | |
335 | ||
336 | void defFunctionrefTemplate(const funcref_def *fdef, output_struct *output) | |
337 | { | |
338 | char *def = NULL, *src = NULL; | |
339 | const char *name = fdef->name; | |
340 | const char *dispname = fdef->dispname; | |
341 | char *fat_string = NULL; | |
342 | ||
343 | switch(fdef->type) { | |
344 | case FUNCTION: | |
345 | fat_string = mputstr(fat_string, "function"); | |
346 | break; | |
347 | case ALTSTEP: | |
348 | fat_string = mputstr(fat_string, "altstep"); | |
349 | break; | |
350 | case TESTCASE: | |
351 | fat_string = mputstr(fat_string, "testcase"); | |
352 | break; | |
353 | } | |
354 | ||
355 | /* class declaration */ | |
356 | output->header.class_decls = mputprintf(output->header.class_decls, | |
357 | "class %s_template;\n", name); | |
358 | ||
359 | /* class definition */ | |
360 | def = mputprintf(def,"class %s_template : public Base_Template {\n" | |
361 | "union {\n" | |
362 | "%s::function_pointer single_value;\n" | |
363 | "struct {\n" | |
364 | "unsigned int n_values;\n" | |
365 | "%s_template *list_value;\n" | |
366 | "} value_list;\n" | |
367 | "};\n\n", name, name, name); | |
368 | ||
369 | /* copy template */ | |
370 | def = mputprintf(def," void copy_template(const %s_template& other_value);\n" | |
371 | , name); | |
372 | src = mputprintf(src,"void %s_template::copy_template(const %s_template& " | |
373 | "other_value)\n" | |
374 | "{\n" | |
375 | "switch(other_value.template_selection) {\n" | |
376 | "case SPECIFIC_VALUE:\n" | |
377 | "single_value = other_value.single_value;\n" | |
378 | "break;\n" | |
379 | "case OMIT_VALUE:\n" | |
380 | "case ANY_VALUE:\n" | |
381 | "case ANY_OR_OMIT:\n" | |
382 | "break;\n" | |
383 | "case VALUE_LIST:\n" | |
384 | "case COMPLEMENTED_LIST:\n" | |
385 | "value_list.n_values = other_value.value_list.n_values;\n" | |
386 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
387 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
388 | "value_list.list_value[i] = other_value.value_list.list_value[i];\n" | |
389 | "break;\n" | |
390 | "default:\n" | |
391 | "TTCN_error(\"Copying an unitialized/unsupported %s template.\");\n" | |
392 | "}\n" | |
393 | "set_selection(other_value);\n" | |
394 | "}\n\n", name, name, name, dispname); | |
395 | ||
396 | /* constructors */ | |
397 | def = mputprintf(def,"public:\n" | |
398 | "%s_template();\n", name); | |
399 | src = mputprintf(src,"%s_template::%s_template()\n" | |
400 | "{\n}\n\n", name, name); | |
401 | def = mputprintf(def,"%s_template(template_sel other_value);\n", name); | |
402 | src = mputprintf(src,"%s_template::%s_template(template_sel other_value)\n" | |
403 | " : Base_Template(other_value)\n" | |
404 | "{\n" | |
405 | "check_single_selection(other_value);\n" | |
406 | "}\n\n", name, name); | |
407 | def = mputprintf(def,"%s_template(%s::function_pointer other_value);\n" | |
408 | , name, name); | |
409 | src = mputprintf(src,"%s_template::%s_template(%s::function_pointer " | |
410 | "other_value)\n" | |
411 | " : Base_Template(SPECIFIC_VALUE)\n" | |
412 | "{\n" | |
413 | "single_value = other_value;\n" | |
414 | "}\n\n", name, name, name); | |
415 | def = mputprintf(def,"%s_template(const %s& other_value);\n", name, name); | |
416 | src = mputprintf(src,"%s_template::%s_template(const %s& other_value)\n" | |
417 | " :Base_Template(SPECIFIC_VALUE)\n" | |
418 | "{\n" | |
419 | "other_value.must_bound(\"Creating a template from an unbound %s value." | |
420 | "\");\n" | |
421 | "single_value = other_value.referred_function;\n" | |
422 | "}\n\n", name, name, name, dispname); | |
423 | def = mputprintf(def,"%s_template(const OPTIONAL<%s>& other_value);\n" | |
424 | , name, name); | |
425 | src = mputprintf(src,"%s_template::%s_template(const OPTIONAL<%s>& " | |
426 | "other_value)\n" | |
427 | "{\n" | |
428 | "if(other_value.ispresent()) {\n" | |
429 | "set_selection(SPECIFIC_VALUE);\n" | |
430 | "single_value = ((const %s&)other_value()).referred_function;\n" | |
431 | "} else set_selection(OMIT_VALUE);\n" | |
432 | "}\n\n", name, name, name, name); | |
433 | def = mputprintf(def,"%s_template(const %s_template& other_value);\n" | |
434 | , name, name); | |
435 | src = mputprintf(src,"%s_template::%s_template(const %s_template& " | |
436 | "other_value)\n" | |
437 | " :Base_Template()\n" /* yes, the default constructor */ | |
438 | "{\n" | |
439 | "copy_template(other_value);\n" | |
440 | "}\n\n", name, name, name); | |
441 | ||
442 | /* destructor */ | |
443 | def = mputprintf(def,"~%s_template();\n", name); | |
444 | src = mputprintf(src,"%s_template::~%s_template()\n" | |
445 | "{\n" | |
446 | " clean_up();\n" | |
447 | "}\n\n", name, name); | |
448 | ||
449 | /* clean up */ | |
450 | def = mputstr(def,"void clean_up();\n"); | |
451 | src = mputprintf(src,"void %s_template::clean_up()" | |
452 | "{\n" | |
453 | "if(template_selection == VALUE_LIST ||\n" | |
454 | "template_selection == COMPLEMENTED_LIST)\n" | |
455 | "delete[] value_list.list_value;\n" | |
456 | "template_selection = UNINITIALIZED_TEMPLATE;\n" | |
457 | "}\n\n", name); | |
458 | ||
459 | /* operator = */ | |
460 | def = mputprintf(def,"%s_template& operator=(template_sel other_value);\n" | |
461 | , name); | |
462 | src = mputprintf(src,"%s_template& %s_template::operator=(template_sel " | |
463 | "other_value)\n" | |
464 | "{\n" | |
465 | "check_single_selection(other_value);\n" | |
466 | "clean_up();\n" | |
467 | "set_selection(other_value);\n" | |
468 | "return *this;\n" | |
469 | "}\n\n", name, name); | |
470 | def = mputprintf(def,"%s_template& operator=(%s::function_pointer " | |
471 | "other_value);\n", name, name); | |
472 | src = mputprintf(src,"%s_template& %s_template::operator=" | |
473 | "(%s::function_pointer other_value)\n" | |
474 | "{\n" | |
475 | "clean_up();\n" | |
476 | "set_selection(SPECIFIC_VALUE);\n" | |
477 | "single_value = other_value;\n" | |
478 | "return *this;" | |
479 | "}\n\n", name, name, name); | |
480 | def = mputprintf(def,"%s_template& operator=(const %s& other_value);\n" | |
481 | , name, name); | |
482 | src = mputprintf(src,"%s_template& %s_template::operator=" | |
483 | "(const %s& other_value)\n" | |
484 | "{\n" | |
485 | "other_value.must_bound(\"Assignment of an unbound %s value to a " | |
486 | "template.\");\n" | |
487 | "clean_up();\n" | |
488 | "set_selection(SPECIFIC_VALUE);\n" | |
489 | "single_value = other_value.referred_function;\n" | |
490 | "return *this;\n" | |
491 | "}\n\n", name, name, name, dispname); | |
492 | def = mputprintf(def,"%s_template& operator=(const OPTIONAL<%s>& " | |
493 | "other_value);\n", name, name); | |
494 | src = mputprintf(src,"%s_template& %s_template::operator=(const " | |
495 | "OPTIONAL<%s>& other_value)\n" | |
496 | "{\n" | |
497 | "clean_up();\n" | |
498 | "if(other_value.ispresent()) { \n" | |
499 | "set_selection(SPECIFIC_VALUE);\n" | |
500 | "single_value = ((const %s&)other_value()).referred_function;\n" | |
501 | "} else set_selection(OMIT_VALUE);\n" | |
502 | "return *this;" | |
503 | "}\n\n", name, name, name, name); | |
504 | def = mputprintf(def,"%s_template& operator=(const %s_template& " | |
505 | "other_value);\n", name, name); | |
506 | src = mputprintf(src,"%s_template& %s_template::operator=(const %s_template& " | |
507 | "other_value)\n" | |
508 | "{\n" | |
509 | "if(&other_value != this) {\n" | |
510 | "clean_up();" | |
511 | "copy_template(other_value);\n" | |
512 | "}\n" | |
513 | "return *this;\n" | |
514 | "}\n\n", name ,name, name); | |
515 | ||
516 | /* match functions */ | |
517 | def = mputprintf(def,"boolean match(%s::function_pointer " | |
3abe9331 | 518 | "other_value, boolean legacy = FALSE) const;\n", name); |
970ed795 | 519 | src = mputprintf(src,"boolean %s_template::match(%s::function_pointer " |
3abe9331 | 520 | "other_value, boolean) const\n" |
970ed795 EL |
521 | "{\n" |
522 | "switch(template_selection) {\n" | |
523 | "case SPECIFIC_VALUE:\n" | |
524 | "return single_value == other_value;\n" | |
525 | "case OMIT_VALUE:\n" | |
526 | "return FALSE;\n" | |
527 | "case ANY_VALUE:\n" | |
528 | "case ANY_OR_OMIT:\n" | |
529 | "return TRUE;\n" | |
530 | "case VALUE_LIST:\n" | |
531 | "case COMPLEMENTED_LIST:\n" | |
532 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
533 | "if(value_list.list_value[i].match(other_value))\n" | |
534 | "return template_selection == VALUE_LIST;\n" | |
535 | "return template_selection == COMPLEMENTED_LIST;\n" | |
536 | "default:\n" | |
537 | "TTCN_error(\"Matching with an unitialized/unsupported %s template." | |
538 | "\");\n" | |
539 | "};\n" | |
540 | "return FALSE;\n" | |
541 | "}\n\n", name, name, dispname); | |
3abe9331 | 542 | def = mputprintf(def,"boolean match(const %s& other_value, boolean legacy " |
543 | "= FALSE) const;\n", name); | |
544 | src = mputprintf(src,"boolean %s_template::match(const %s& other_value, " | |
545 | "boolean) const\n" | |
970ed795 | 546 | "{\n" |
3abe9331 | 547 | " if (!other_value.is_bound()) return FALSE;\n" |
970ed795 EL |
548 | "return match(other_value.referred_function);\n" |
549 | "}\n\n", name, name); | |
550 | ||
551 | /* value of function */ | |
552 | def = mputprintf(def,"%s valueof() const;\n", name); | |
553 | src = mputprintf(src,"%s %s_template::valueof() const\n" | |
554 | "{\n" | |
555 | "if(template_selection != SPECIFIC_VALUE || is_ifpresent)\n" | |
556 | "TTCN_error(\"Performing a valueof or send operation on a " | |
557 | "non-specific %s template.\");\n" | |
558 | "return single_value;\n}\n\n", name, name, dispname); | |
559 | ||
560 | /* set type */ | |
561 | def = mputstr(def,"void set_type(template_sel template_type, " | |
562 | "unsigned int list_length);\n"); | |
563 | src = mputprintf(src,"void %s_template::set_type(template_sel template_type, " | |
564 | "unsigned int list_length)\n" | |
565 | "{\n" | |
566 | "if(template_type != VALUE_LIST && " | |
567 | "template_type != COMPLEMENTED_LIST)\n" | |
568 | "TTCN_error(\"Setting an invalid type for an %s template.\");\n" | |
569 | "clean_up();\n" | |
570 | "set_selection(template_type);\n" | |
571 | "value_list.n_values = list_length;\n" | |
572 | "value_list.list_value = new %s_template[list_length];\n" | |
573 | "}\n\n", name, dispname, name); | |
574 | ||
575 | /* list item */ | |
576 | def = mputprintf(def,"%s_template& list_item(unsigned int list_index) " | |
577 | "const;\n", name); | |
578 | src = mputprintf(src,"%s_template& %s_template::list_item(" | |
579 | "unsigned int list_index) const\n" | |
580 | "{\n" | |
581 | "if(template_selection != VALUE_LIST && " | |
582 | "template_selection != COMPLEMENTED_LIST)\n" | |
583 | "TTCN_error(\"Accessing a list element of a non-list template of " | |
584 | "type %s\");\n" | |
585 | "if(list_index >= value_list.n_values)\n" | |
586 | "TTCN_error(\"Index overflow in a value list template of type %s." | |
587 | "\");\n" | |
588 | "return value_list.list_value[list_index];\n" | |
589 | "}\n\n", name, name, dispname, dispname); | |
590 | ||
591 | if (use_runtime_2) { | |
592 | /* functions in alternative runtime */ | |
593 | def = mputstr(def, | |
594 | "void valueofv(Base_Type* value) const;\n" | |
595 | "void set_value(template_sel other_value);\n" | |
596 | "void copy_value(const Base_Type* other_value);\n" | |
597 | "Base_Template* clone() const;\n" | |
598 | "const TTCN_Typedescriptor_t* get_descriptor() const;\n" | |
3abe9331 | 599 | "boolean matchv(const Base_Type* other_value, boolean legacy) const;\n" |
600 | "void log_matchv(const Base_Type* match_value, boolean legacy) const;\n"); | |
970ed795 EL |
601 | src = mputprintf(src, |
602 | "void %s_template::valueofv(Base_Type* value) const " | |
603 | "{ *(static_cast<%s*>(value)) = valueof(); }\n" | |
604 | "void %s_template::set_value(template_sel other_value) " | |
605 | "{ *this = other_value; }\n" | |
606 | "void %s_template::copy_value(const Base_Type* other_value) " | |
607 | "{ *this = *(static_cast<const %s*>(other_value)); }\n" | |
608 | "Base_Template* %s_template::clone() const " | |
609 | "{ return new %s_template(*this); }\n" | |
610 | "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const " | |
611 | "{ return &%s_descr_; }\n" | |
3abe9331 | 612 | "boolean %s_template::matchv(const Base_Type* other_value, " |
613 | "boolean legacy) const " | |
614 | "{ return match(*(static_cast<const %s*>(other_value)), legacy); }\n" | |
615 | "void %s_template::log_matchv(const Base_Type* match_value, " | |
616 | "boolean legacy) const " | |
617 | " { log_match(*(static_cast<const %s*>(match_value)), legacy); }\n", | |
970ed795 EL |
618 | name, name, |
619 | name, | |
620 | name, name, | |
621 | name, name, | |
622 | name, name, | |
623 | name, name, | |
624 | name, name); | |
625 | } | |
626 | ||
627 | /* log function */ | |
628 | def = mputstr(def,"void log() const;\n"); | |
629 | src = mputprintf(src,"void %s_template::log() const\n" | |
630 | "{\n" | |
631 | "switch(template_selection) {\n" | |
632 | "case SPECIFIC_VALUE:\n" | |
633 | "Module_List::log_%s((genericfunc_t)single_value);\n" | |
634 | "break;\n" | |
635 | "case COMPLEMENTED_LIST:\n" | |
636 | "TTCN_Logger::log_event_str(\"complement \");\n" | |
637 | "case VALUE_LIST:\n" | |
638 | "TTCN_Logger::log_char('(');\n" | |
639 | "for(unsigned int i = 0; i < value_list.n_values; i++) {\n" | |
640 | "if(i > 0) TTCN_Logger::log_event_str(\", \");\n" | |
641 | "value_list.list_value[i].log();\n" | |
642 | "}\n" | |
643 | "TTCN_Logger::log_char(')');\n" | |
644 | "break;\n" | |
645 | "default:\n" | |
646 | "log_generic();\n" | |
647 | "}\n" | |
648 | "log_ifpresent();\n" | |
649 | "}\n\n", name, fat_string); | |
650 | ||
651 | /* log_match function */ | |
3abe9331 | 652 | def = mputprintf(def,"void log_match(const %s& match_value, " |
653 | "boolean legacy = FALSE) const;\n", name); | |
654 | src = mputprintf(src,"void %s_template::log_match(const %s& match_value, " | |
655 | "boolean legacy) const\n" | |
970ed795 EL |
656 | "{\n" |
657 | "log();\n" | |
658 | "TTCN_Logger::log_event_str(\" with \");\n" | |
659 | "match_value.log();\n" | |
3abe9331 | 660 | "if(match(match_value, legacy)) TTCN_Logger::log_event_str(\" matched\");\n" |
970ed795 EL |
661 | "else TTCN_Logger::log_event_str(\" unmatched\");\n" |
662 | "}\n\n", name, name); | |
663 | ||
664 | /* encode_text / decode_text */ | |
665 | def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n"); | |
666 | src = mputprintf(src,"void %s_template::encode_text(Text_Buf&", name); | |
667 | if (fdef->runs_on_self) { | |
668 | src = mputprintf(src, ") const\n" | |
669 | "{\n" | |
670 | "TTCN_error(\"Templates of type %s cannot be sent to " | |
671 | "other test components.\");\n", dispname); | |
672 | } else { | |
673 | src = mputprintf(src, " text_buf) const\n" | |
674 | "{\n" | |
675 | "encode_text_base(text_buf);\n" | |
676 | "switch(template_selection) {\n" | |
677 | "case OMIT_VALUE:\n" | |
678 | "case ANY_VALUE:\n" | |
679 | "case ANY_OR_OMIT:\n" | |
680 | "break;\n" | |
681 | "case SPECIFIC_VALUE:\n" | |
682 | "Module_List::encode_%s(text_buf, (genericfunc_t)single_value);\n" | |
683 | "break;\n" | |
684 | "case VALUE_LIST:\n" | |
685 | "case COMPLEMENTED_LIST:\n" | |
686 | "text_buf.push_int(value_list.n_values);\n" | |
687 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
688 | "value_list.list_value[i].encode_text(text_buf);\n" | |
689 | "break;\n" | |
690 | "default:\n" | |
691 | "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported template " | |
692 | "of type %s.\");\n" | |
693 | "}\n", fat_string, dispname); | |
694 | } | |
695 | src = mputstr(src,"}\n\n"); | |
696 | def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n"); | |
697 | src = mputprintf(src,"void %s_template::decode_text(Text_Buf&", name); | |
698 | if (fdef->runs_on_self) { | |
699 | src = mputprintf(src, ")\n" | |
700 | "{\n" | |
701 | "TTCN_error(\"Templates of type %s cannot be received " | |
702 | "from other test components.\");\n", dispname); | |
703 | } else { | |
704 | src = mputprintf(src, " text_buf)\n" | |
705 | "{\n" | |
706 | "clean_up();\n" | |
707 | "decode_text_base(text_buf);\n" | |
708 | "switch(template_selection) {\n" | |
709 | "case OMIT_VALUE:\n" | |
710 | "case ANY_VALUE:\n" | |
711 | "case ANY_OR_OMIT:\n" | |
712 | "break;\n" | |
713 | "case SPECIFIC_VALUE:\n" | |
714 | "Module_List::decode_%s(text_buf,(genericfunc_t*)&single_value);\n" | |
715 | "break;\n" | |
716 | "case VALUE_LIST:\n" | |
717 | "case COMPLEMENTED_LIST:\n" | |
718 | "value_list.n_values = text_buf.pull_int().get_val();\n" | |
719 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
720 | "for(unsigned int i = 0; i < value_list.n_values; i++)\n" | |
721 | "value_list.list_value[i].decode_text(text_buf);\n" | |
722 | "default:\n" | |
723 | "TTCN_error(\"Text decoder: An unknown/unsupported selection was received " | |
724 | "in a template of type %s.\");\n" | |
725 | "}\n", fat_string, name, dispname); | |
726 | } | |
727 | src = mputstr(src,"}\n\n"); | |
728 | ||
729 | /* TTCN-3 ispresent() function */ | |
3abe9331 | 730 | def = mputstr(def, "boolean is_present(boolean legacy = FALSE) const;\n"); |
970ed795 | 731 | src = mputprintf(src, |
3abe9331 | 732 | "boolean %s_template::is_present(boolean legacy) const\n" |
970ed795 EL |
733 | "{\n" |
734 | "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n" | |
3abe9331 | 735 | "return !match_omit(legacy);\n" |
970ed795 EL |
736 | "}\n\n", name); |
737 | ||
738 | /* match_omit() */ | |
3abe9331 | 739 | def = mputstr(def, "boolean match_omit(boolean legacy = FALSE) const;\n"); |
970ed795 | 740 | src = mputprintf(src, |
3abe9331 | 741 | "boolean %s_template::match_omit(boolean legacy) const\n" |
970ed795 EL |
742 | "{\n" |
743 | "if (is_ifpresent) return TRUE;\n" | |
744 | "switch (template_selection) {\n" | |
745 | "case OMIT_VALUE:\n" | |
746 | "case ANY_OR_OMIT:\n" | |
747 | "return TRUE;\n" | |
748 | "case VALUE_LIST:\n" | |
749 | "case COMPLEMENTED_LIST:\n" | |
3abe9331 | 750 | "if (legacy) {\n" |
970ed795 EL |
751 | "for (unsigned int i=0; i<value_list.n_values; i++)\n" |
752 | "if (value_list.list_value[i].match_omit())\n" | |
753 | "return template_selection==VALUE_LIST;\n" | |
754 | "return template_selection==COMPLEMENTED_LIST;\n" | |
3abe9331 | 755 | "} // else fall through\n" |
970ed795 EL |
756 | "default:\n" |
757 | "return FALSE;\n" | |
758 | "}\n" | |
759 | "return FALSE;\n" | |
760 | "}\n\n", name); | |
761 | ||
762 | /* set_param */ | |
763 | def = mputstr(def,"void set_param(Module_Param& param);\n"); | |
764 | src = mputprintf(src,"void %s_template::set_param(Module_Param& param)\n" | |
765 | "{\n" | |
766 | " param.error(\"Not supported.\");\n" | |
767 | "}\n\n", name); | |
3abe9331 | 768 | |
769 | /* get_param */ | |
770 | def = mputstr(def,"Module_Param* get_param(Module_Param_Name& param_name) const;\n"); | |
771 | src = mputprintf(src,"Module_Param* %s_template::get_param(Module_Param_Name& /* param_name */) const\n" | |
772 | "{\n" | |
773 | " return NULL;\n" | |
774 | "}\n\n", name); | |
970ed795 EL |
775 | |
776 | if (!use_runtime_2) { | |
777 | /* check template restriction */ | |
778 | def = mputstr(def, "void check_restriction(template_res t_res, " | |
3abe9331 | 779 | "const char* t_name=NULL, boolean legacy = FALSE) const;\n"); |
970ed795 EL |
780 | src = mputprintf(src, |
781 | "void %s_template::check_restriction" | |
3abe9331 | 782 | "(template_res t_res, const char* t_name, boolean legacy) const\n" |
970ed795 EL |
783 | "{\n" |
784 | "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n" | |
785 | "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n" | |
786 | "case TR_VALUE:\n" | |
787 | "if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return;\n" | |
788 | "break;\n" | |
789 | "case TR_OMIT:\n" | |
790 | "if (!is_ifpresent && (template_selection==OMIT_VALUE || " | |
791 | "template_selection==SPECIFIC_VALUE)) return;\n" | |
792 | "break;\n" | |
793 | "case TR_PRESENT:\n" | |
3abe9331 | 794 | "if (!match_omit(legacy)) return;\n" |
970ed795 EL |
795 | "break;\n" |
796 | "default:\n" | |
797 | "return;\n" | |
798 | "}\n" | |
799 | "TTCN_error(\"Restriction `%%s' on template of type %%s violated.\", " | |
800 | "get_res_name(t_res), t_name ? t_name : \"%s\");\n" | |
801 | "}\n\n", name, dispname); | |
802 | } | |
803 | ||
804 | def = mputstr(def,"};\n\n"); | |
805 | ||
806 | Free(fat_string); | |
807 | ||
808 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
809 | Free(def); | |
810 | ||
811 | output->source.methods = mputstr(output->source.methods, src); | |
812 | Free(src); | |
813 | } |