Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 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 | * Contributors: | |
9 | * Baji, Laszlo | |
10 | * Balasko, Jeno | |
11 | * Baranyi, Botond | |
12 | * Beres, Szabolcs | |
13 | * Delic, Adam | |
14 | * Forstner, Matyas | |
15 | * Kovacs, Ferenc | |
16 | * Raduly, Csaba | |
17 | * Szabados, Kristof | |
18 | * Szabo, Bence Janos | |
19 | * Szabo, Janos Zoltan – initial implementation | |
20 | * Szalai, Gabor | |
21 | * Zalanyi, Balazs Andor | |
22 | * | |
23 | ******************************************************************************/ | |
970ed795 EL |
24 | #include "../common/memory.h" |
25 | #include "datatypes.h" | |
26 | #include "record_of.h" | |
27 | #include "encdec.h" | |
28 | ||
29 | #include "main.hh" | |
30 | #include "ttcn3/compiler.h" | |
31 | ||
32 | /** code generation for original runtime */ | |
33 | static void defRecordOfClass1(const struct_of_def *sdef, output_struct *output); | |
34 | static void defRecordOfTemplate1(const struct_of_def *sdef, output_struct *output); | |
35 | /** code generation for alternative runtime (TITAN_RUNTIME_2) */ | |
36 | static void defRecordOfClass2(const struct_of_def *sdef, output_struct *output); | |
37 | static void defRecordOfTemplate2(const struct_of_def *sdef, output_struct *output); | |
38 | ||
39 | void defRecordOfClass(const struct_of_def *sdef, output_struct *output) | |
40 | { | |
41 | if (use_runtime_2) defRecordOfClass2(sdef, output); | |
42 | else defRecordOfClass1(sdef, output); | |
43 | } | |
44 | ||
45 | void defRecordOfTemplate(const struct_of_def *sdef, output_struct *output) | |
46 | { | |
47 | if (use_runtime_2) defRecordOfTemplate2(sdef, output); | |
48 | else defRecordOfTemplate1(sdef, output); | |
49 | } | |
50 | ||
51 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
52 | ||
53 | void defRecordOfClass1(const struct_of_def *sdef, output_struct *output) | |
54 | { | |
55 | char *def = NULL, *src = NULL; | |
56 | const char *name = sdef->name, *dispname = sdef->dispname; | |
57 | const char *type = sdef->type; | |
a38c6d4c | 58 | boolean ber_needed = force_gen_seof || (sdef->isASN1 && enable_ber()); |
59 | boolean raw_needed = force_gen_seof || (sdef->hasRaw && enable_raw()); | |
60 | boolean text_needed = force_gen_seof || (sdef->hasText && enable_text()); | |
61 | boolean xer_needed = force_gen_seof || (sdef->hasXer && enable_xer()); | |
62 | boolean json_needed = force_gen_seof || (sdef->hasJson && enable_json()); | |
970ed795 EL |
63 | |
64 | /* Class definition and private data members */ | |
65 | def = mputprintf(def, | |
66 | #ifndef NDEBUG | |
67 | "// written by %s in " __FILE__ " at %d\n" | |
68 | #endif | |
69 | "class %s : public Base_Type {\n" | |
70 | "struct recordof_setof_struct {\n" | |
71 | "int ref_count;\n" | |
72 | "int n_elements;\n" | |
73 | "%s **value_elements;\n" | |
74 | "} *val_ptr;\n" | |
970ed795 EL |
75 | #ifndef NDEBUG |
76 | , __FUNCTION__, __LINE__ | |
77 | #endif | |
78 | , name, type); | |
79 | ||
80 | /* constant unbound element */ | |
81 | def = mputprintf(def, "\nstatic const %s UNBOUND_ELEM;\n", type); | |
82 | src = mputprintf(src, "\nconst %s %s::UNBOUND_ELEM;\n", type, name); | |
83 | ||
84 | /* private member functions */ | |
85 | def = mputprintf(def, | |
86 | "private:\n" | |
87 | "friend boolean operator==(null_type null_value, " | |
88 | "const %s& other_value);\n", name); | |
89 | ||
90 | if (sdef->kind == SET_OF) { | |
91 | /* callback function for comparison */ | |
92 | def = mputstr(def, "static boolean compare_function(" | |
93 | "const Base_Type *left_ptr, int left_index, " | |
94 | "const Base_Type *right_ptr, int right_index);\n"); | |
95 | src = mputprintf(src, "boolean %s::compare_function(" | |
96 | "const Base_Type *left_ptr, int left_index, " | |
97 | "const Base_Type *right_ptr, int right_index)\n" | |
98 | "{\n" | |
99 | "if (((const %s*)left_ptr)->val_ptr == NULL) " | |
100 | "TTCN_error(\"The left operand of comparison is an unbound value of " | |
101 | "type %s.\");\n" | |
102 | "if (((const %s*)right_ptr)->val_ptr == NULL) " | |
103 | "TTCN_error(\"The right operand of comparison is an unbound value of " | |
104 | "type %s.\");\n" | |
105 | "if (((const %s*)left_ptr)->val_ptr->value_elements[left_index] != NULL){\n" | |
106 | "if (((const %s*)right_ptr)->val_ptr->value_elements[right_index] != NULL){\n" | |
107 | "return *((const %s*)left_ptr)->val_ptr->value_elements[left_index] == " | |
108 | "*((const %s*)right_ptr)->val_ptr->value_elements[right_index];\n" | |
109 | "} else return FALSE;\n" | |
110 | "} else {\n" | |
111 | "return ((const %s*)right_ptr)->val_ptr->value_elements[right_index] == NULL;\n" | |
112 | "}\n" | |
113 | "}\n\n", name, name, dispname, name, dispname, name, name, name, name, name); | |
114 | } | |
970ed795 EL |
115 | |
116 | /* public member functions */ | |
117 | def = mputstr(def, "\npublic:\n"); | |
118 | def = mputprintf(def, " typedef %s of_type;\n", sdef->type); | |
119 | ||
120 | /* constructors */ | |
121 | def = mputprintf(def, "%s();\n", name); | |
122 | src = mputprintf(src, | |
123 | "%s::%s()\n" | |
124 | "{\n" | |
125 | "val_ptr = NULL;\n" | |
970ed795 EL |
126 | "}\n\n", name, name); |
127 | ||
128 | def = mputprintf(def, "%s(null_type other_value);\n", name); | |
129 | src = mputprintf(src, | |
130 | "%s::%s(null_type)\n" | |
131 | "{\n" | |
132 | "val_ptr = new recordof_setof_struct;\n" | |
133 | "val_ptr->ref_count = 1;\n" | |
134 | "val_ptr->n_elements = 0;\n" | |
135 | "val_ptr->value_elements = NULL;\n" | |
970ed795 EL |
136 | "}\n\n", name, name); |
137 | ||
138 | /* copy constructor */ | |
139 | def = mputprintf(def, "%s(const %s& other_value);\n", name, name); | |
140 | src = mputprintf(src, | |
141 | "%s::%s(const %s& other_value)\n" | |
142 | "{\n" | |
143 | "if (!other_value.is_bound()) " | |
144 | "TTCN_error(\"Copying an unbound value of type %s.\");\n" | |
970ed795 EL |
145 | "val_ptr = other_value.val_ptr;\n" |
146 | "val_ptr->ref_count++;\n" | |
af710487 | 147 | "}\n\n", name, name, name, dispname); |
970ed795 EL |
148 | |
149 | /* destructor */ | |
150 | def = mputprintf(def, "~%s();\n\n", name); | |
151 | src = mputprintf(src, | |
152 | "%s::~%s()\n" | |
153 | "{\n" | |
154 | "clean_up();\n" | |
155 | "if (val_ptr != NULL) val_ptr = NULL;\n" | |
156 | "}\n\n", name, name); | |
157 | ||
158 | /* clean_up function */ | |
159 | def = mputstr(def, "void clean_up();\n"); | |
160 | src = mputprintf | |
161 | (src, | |
162 | "void %s::clean_up()\n" | |
163 | "{\n" | |
164 | "if (val_ptr != NULL) {\n" | |
165 | "if (val_ptr->ref_count > 1) {\n" | |
166 | "val_ptr->ref_count--;\n" | |
167 | "val_ptr = NULL;\n" | |
168 | "}\n" | |
169 | "else if (val_ptr->ref_count == 1) {\n" | |
970ed795 EL |
170 | "for (int elem_count = 0; elem_count < val_ptr->n_elements;\n" |
171 | "elem_count++)\n" | |
172 | "if (val_ptr->value_elements[elem_count] != NULL)\n" | |
173 | "delete val_ptr->value_elements[elem_count];\n" | |
174 | "free_pointers((void**)val_ptr->value_elements);\n" | |
175 | "delete val_ptr;\n" | |
176 | "val_ptr = NULL;\n" | |
177 | "}\n" | |
970ed795 EL |
178 | "else\n" |
179 | "TTCN_error(\"Internal error: Invalid reference counter in a record " | |
180 | "of/set of value.\");\n" | |
181 | "}\n" | |
182 | "}\n\n", name); | |
183 | ||
184 | /* assignment operators */ | |
185 | def = mputprintf(def, "%s& operator=(null_type other_value);\n", name); | |
186 | src = mputprintf(src, | |
187 | "%s& %s::operator=(null_type)\n" | |
188 | "{\n" | |
af710487 | 189 | "clean_up();\n" |
190 | "val_ptr = new recordof_setof_struct;\n" | |
191 | "val_ptr->ref_count = 1;\n" | |
192 | "val_ptr->n_elements = 0;\n" | |
193 | "val_ptr->value_elements = NULL;\n" | |
970ed795 EL |
194 | "return *this;\n" |
195 | "}\n\n", name, name); | |
196 | ||
197 | def = mputprintf(def, "%s& operator=(const %s& other_value);\n\n", | |
198 | name, name); | |
199 | src = mputprintf(src, | |
200 | "%s& %s::operator=(const %s& other_value)\n" | |
201 | "{\n" | |
af710487 | 202 | "if (other_value.val_ptr == NULL) " |
970ed795 EL |
203 | "TTCN_error(\"Assigning an unbound value of type %s.\");\n" |
204 | "if (this != &other_value) {\n" | |
970ed795 EL |
205 | "clean_up();\n" |
206 | "val_ptr = other_value.val_ptr;\n" | |
207 | "val_ptr->ref_count++;\n" | |
208 | "}\n" | |
970ed795 | 209 | "return *this;\n" |
af710487 | 210 | "}\n\n", name, name, name, dispname); |
970ed795 EL |
211 | |
212 | /* comparison operators */ | |
213 | def = mputstr(def, "boolean operator==(null_type other_value) const;\n"); | |
214 | src = mputprintf(src, | |
215 | "boolean %s::operator==(null_type) const\n" | |
216 | "{\n" | |
217 | "if (val_ptr == NULL)\n" | |
218 | "TTCN_error(\"The left operand of comparison is an unbound value of " | |
219 | "type %s.\");\n" | |
af710487 | 220 | "return val_ptr->n_elements == 0 ;\n" |
970ed795 EL |
221 | "}\n\n", name, dispname); |
222 | ||
223 | def = mputprintf(def, "boolean operator==(const %s& other_value) const;\n", | |
224 | name); | |
225 | src = mputprintf(src, | |
226 | "boolean %s::operator==(const %s& other_value) const\n" | |
227 | "{\n" | |
228 | "if (val_ptr == NULL) " | |
229 | "TTCN_error(\"The left operand of comparison is an unbound value of type " | |
230 | "%s.\");\n" | |
231 | "if (other_value.val_ptr == NULL) " | |
232 | "TTCN_error(\"The right operand of comparison is an unbound value of type " | |
233 | "%s.\");\n" | |
234 | "if (val_ptr == other_value.val_ptr) return TRUE;\n", name, name, | |
235 | dispname, dispname); | |
236 | if (sdef->kind == SET_OF) { | |
237 | src = mputstr(src, | |
af710487 | 238 | "return compare_set_of(this, val_ptr->n_elements, &other_value, " |
239 | "(other_value.val_ptr)->n_elements, compare_function);\n"); | |
970ed795 EL |
240 | } else { |
241 | src = mputstr | |
242 | (src, | |
af710487 | 243 | "if (val_ptr->n_elements != (other_value.val_ptr)->n_elements)\n" |
970ed795 | 244 | "return FALSE;\n" |
af710487 | 245 | "for (int elem_count = 0; elem_count < val_ptr->n_elements; elem_count++){\n" |
246 | "if (val_ptr->value_elements[elem_count] != NULL){\n" | |
247 | "if ((other_value.val_ptr)->value_elements[elem_count] != NULL){\n" | |
970ed795 EL |
248 | " if (*val_ptr->value_elements[elem_count] != " |
249 | "*(other_value.val_ptr)->value_elements[elem_count]) " | |
250 | "return FALSE;\n" | |
251 | "} else return FALSE;\n" | |
252 | "} else {\n" | |
af710487 | 253 | "if ((other_value.val_ptr)->value_elements[elem_count] != NULL) " |
254 | "return FALSE;\n" | |
970ed795 EL |
255 | "}\n" |
256 | "}\n" | |
257 | "return TRUE;\n"); | |
258 | } | |
259 | src = mputstr(src, "}\n\n"); | |
260 | ||
261 | def = mputstr(def, "inline boolean operator!=(null_type other_value) const " | |
262 | "{ return !(*this == other_value); }\n"); | |
263 | def = mputprintf(def, "inline boolean operator!=(const %s& other_value) " | |
264 | "const { return !(*this == other_value); }\n\n", name); | |
265 | ||
266 | /* indexing operators */ | |
267 | /* Non-const operator[] is allowed to extend the record-of */ | |
268 | def = mputprintf(def, "%s& operator[](int index_value);\n", type); | |
269 | src = mputprintf(src, | |
270 | "%s& %s::operator[](int index_value)\n" | |
271 | "{\n" | |
272 | "if (index_value < 0) TTCN_error(\"Accessing an element of type %s " | |
273 | "using a negative index: %%d.\", index_value);\n" | |
274 | "if (val_ptr == NULL) {\n" | |
275 | "val_ptr = new recordof_setof_struct;\n" | |
276 | "val_ptr->ref_count = 1;\n" | |
277 | "val_ptr->n_elements = 0;\n" | |
278 | "val_ptr->value_elements = NULL;\n" | |
279 | "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */ | |
280 | "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n" | |
281 | "new_val_ptr->ref_count = 1;\n" | |
282 | "new_val_ptr->n_elements = (index_value >= val_ptr->n_elements) ? " | |
283 | "index_value + 1 : val_ptr->n_elements;\n" | |
284 | "new_val_ptr->value_elements = " | |
285 | "(%s**)allocate_pointers(new_val_ptr->n_elements);\n" | |
286 | "for (int elem_count = 0; elem_count < val_ptr->n_elements; " | |
287 | "elem_count++){\n" | |
288 | "if (val_ptr->value_elements[elem_count] != NULL){\n" | |
289 | "new_val_ptr->value_elements[elem_count] = " | |
290 | "new %s(*(val_ptr->value_elements[elem_count]));\n" | |
291 | "}\n" | |
292 | "}\n" | |
293 | "clean_up();\n" | |
294 | "val_ptr = new_val_ptr;\n" | |
295 | "}\n" | |
296 | "if (index_value >= val_ptr->n_elements) set_size(index_value + 1);\n" | |
297 | "if (val_ptr->value_elements[index_value] == NULL) {\n" | |
298 | "val_ptr->value_elements[index_value] = new %s;\n" | |
299 | "}\n" | |
300 | "return *val_ptr->value_elements[index_value];\n" | |
301 | "}\n\n", type, name, dispname, type, type, type); | |
302 | ||
303 | def = mputprintf(def, "%s& operator[](const INTEGER& index_value);\n", | |
304 | type); | |
305 | src = mputprintf(src, | |
306 | "%s& %s::operator[](const INTEGER& index_value)\n" | |
307 | "{\n" | |
308 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
309 | "a value of type %s.\");\n" | |
310 | "return (*this)[(int)index_value];\n" | |
311 | "}\n\n", type, name, dispname); | |
312 | ||
313 | /* Const operator[] throws an error if over-indexing */ | |
314 | def = mputprintf(def, "const %s& operator[](int index_value) const;\n", | |
315 | type); | |
316 | src = mputprintf(src, | |
317 | "const %s& %s::operator[](int index_value) const\n" | |
318 | "{\n" | |
319 | "if (val_ptr == NULL)\n" | |
320 | "TTCN_error(\"Accessing an element in an unbound value of type %s.\");\n" | |
321 | "if (index_value < 0) TTCN_error(\"Accessing an element of type %s " | |
322 | "using a negative index: %%d.\", index_value);\n" | |
af710487 | 323 | "if (index_value >= val_ptr->n_elements) TTCN_error(\"Index overflow in " |
970ed795 | 324 | "a value of type %s: The index is %%d, but the value has only %%d " |
af710487 | 325 | "elements.\", index_value, val_ptr->n_elements);\n" |
970ed795 EL |
326 | "return (val_ptr->value_elements[index_value] != NULL) ?\n" |
327 | "*val_ptr->value_elements[index_value] : UNBOUND_ELEM;\n" | |
328 | "}\n\n", type, name, dispname, dispname, dispname); | |
329 | ||
330 | def = mputprintf(def, "const %s& operator[](const INTEGER& index_value) " | |
331 | "const;\n\n", type); | |
332 | src = mputprintf(src, | |
333 | "const %s& %s::operator[](const INTEGER& index_value) const\n" | |
334 | "{\n" | |
335 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
336 | "a value of type %s.\");\n" | |
337 | "return (*this)[(int)index_value];\n" | |
338 | "}\n\n", type, name, dispname); | |
339 | ||
340 | /* rotation operators */ | |
341 | def = mputprintf(def, | |
342 | "%s operator<<=(int rotate_count) const;\n" | |
343 | "%s operator<<=(const INTEGER& rotate_count) const;\n" | |
344 | "%s operator>>=(int rotate_count) const;\n" | |
345 | "%s operator>>=(const INTEGER& rotate_count) const;\n\n", | |
346 | name, name, name, name); | |
347 | src = mputprintf(src, | |
348 | "%s %s::operator<<=(int rotate_count) const\n" | |
349 | "{\n" | |
350 | "return *this >>= (-rotate_count);\n" | |
351 | "}\n\n" | |
352 | "%s %s::operator<<=(const INTEGER& rotate_count) const\n" | |
353 | "{\n" | |
354 | "rotate_count.must_bound(\"" | |
355 | "Unbound integer operand of rotate left operator.\");\n" | |
356 | "return *this >>= (int)(-rotate_count);\n" | |
357 | "}\n\n" | |
358 | "%s %s::operator>>=(const INTEGER& rotate_count) const\n" | |
359 | "{\n" | |
360 | "rotate_count.must_bound(\"" | |
361 | "Unbound integer operand of rotate right operator.\");\n" | |
362 | "return *this >>= (int)rotate_count;\n" | |
363 | "}\n\n" | |
364 | "%s %s::operator>>=(int rotate_count) const\n" | |
365 | "{\n" | |
366 | "if (val_ptr == NULL) " | |
367 | "TTCN_error(\"Performing rotation operation on an unbound value of type " | |
368 | "%s.\");\n" | |
af710487 | 369 | "if (val_ptr->n_elements == 0) return *this;\n" |
970ed795 | 370 | "int rc;\n" |
af710487 | 371 | "if (rotate_count>=0) rc = rotate_count %% val_ptr->n_elements;\n" |
372 | "else rc = val_ptr->n_elements - ((-rotate_count) %% val_ptr->n_elements);\n" | |
970ed795 EL |
373 | "if (rc == 0) return *this;\n" |
374 | "%s ret_val;\n" | |
af710487 | 375 | "ret_val.set_size(val_ptr->n_elements);\n" |
376 | "for (int i=0; i<val_ptr->n_elements; i++) {\n" | |
377 | "if (val_ptr->value_elements[i] != NULL) {\n" | |
378 | "ret_val.val_ptr->value_elements[(i+rc)%%val_ptr->n_elements] =" | |
970ed795 EL |
379 | "new %s(*val_ptr->value_elements[i]);\n" |
380 | "}\n" | |
381 | "}\n" | |
382 | "return ret_val;\n" | |
383 | "}\n\n", | |
384 | name, name, name, name, name, name, name, name, dispname, name, type); | |
385 | ||
386 | /* concatenation */ | |
387 | def = mputprintf(def, | |
388 | "%s operator+(const %s& other_value) const;\n\n", name, name); | |
389 | src = mputprintf(src, | |
390 | "%s %s::operator+(const %s& other_value) const\n" | |
391 | "{\n" | |
392 | "if (val_ptr == NULL || other_value.val_ptr == NULL) " | |
393 | "TTCN_error(\"Unbound operand of %s concatenation.\");\n" | |
af710487 | 394 | "if (val_ptr->n_elements == 0) return other_value;\n" |
395 | "if (other_value.val_ptr->n_elements == 0) return *this;\n" | |
970ed795 | 396 | "%s ret_val;\n" |
af710487 | 397 | "ret_val.set_size(val_ptr->n_elements+other_value.val_ptr->n_elements);\n" |
398 | "for (int i=0; i<val_ptr->n_elements; i++) {\n" | |
399 | "if (val_ptr->value_elements[i] != NULL) {\n" | |
970ed795 EL |
400 | "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n" |
401 | "}\n" | |
402 | "}\n" | |
af710487 | 403 | "for (int i=0; i<other_value.val_ptr->n_elements; i++) {\n" |
404 | "if (other_value.val_ptr->value_elements[i] != NULL) {\n" | |
405 | "ret_val.val_ptr->value_elements[i+val_ptr->n_elements] = " | |
970ed795 EL |
406 | "new %s(*other_value.val_ptr->value_elements[i]);\n" |
407 | "}\n" | |
408 | "}\n" | |
409 | "return ret_val;\n" | |
410 | "}\n\n", name, name, name, dispname, name, type, type); | |
411 | ||
412 | /* substr() */ | |
413 | def = mputprintf(def, | |
414 | "%s substr(int index, int returncount) const;\n\n", name); | |
415 | src = mputprintf(src, | |
416 | "%s %s::substr(int index, int returncount) const\n" | |
417 | "{\n" | |
418 | "if (val_ptr == NULL) " | |
419 | "TTCN_error(\"The first argument of substr() is an unbound value of " | |
420 | "type %s.\");\n" | |
af710487 | 421 | "check_substr_arguments(val_ptr->n_elements, index, returncount, " |
970ed795 EL |
422 | "\"%s\",\"element\");\n" |
423 | "%s ret_val;\n" | |
424 | "ret_val.set_size(returncount);\n" | |
425 | "for (int i=0; i<returncount; i++) {\n" | |
af710487 | 426 | "if (val_ptr->value_elements[i+index] != NULL) {\n" |
970ed795 EL |
427 | "ret_val.val_ptr->value_elements[i] = " |
428 | "new %s(*val_ptr->value_elements[i+index]);\n" | |
429 | "}\n" | |
430 | "}\n" | |
431 | "return ret_val;\n" | |
432 | "}\n\n", name, name, dispname, dispname, name, type); | |
433 | ||
434 | /* replace() */ | |
435 | def = mputprintf(def, | |
436 | "%s replace(int index, int len, const %s& repl) const;\n\n", name, name); | |
437 | src = mputprintf(src, | |
438 | "%s %s::replace(int index, int len, const %s& repl) const\n" | |
439 | "{\n" | |
440 | "if (val_ptr == NULL) " | |
441 | "TTCN_error(\"The first argument of replace() is an unbound value of " | |
442 | "type %s.\");\n" | |
443 | "if (repl.val_ptr == NULL) " | |
444 | "TTCN_error(\"The fourth argument of replace() is an unbound value of " | |
445 | "type %s.\");\n" | |
af710487 | 446 | "check_replace_arguments(val_ptr->n_elements, index, len, " |
970ed795 EL |
447 | "\"%s\",\"element\");\n" |
448 | "%s ret_val;\n" | |
af710487 | 449 | "ret_val.set_size(val_ptr->n_elements + repl.val_ptr->n_elements - len);\n" |
970ed795 | 450 | "for (int i = 0; i < index; i++) {\n" |
af710487 | 451 | "if (val_ptr->value_elements[i] != NULL) {\n" |
970ed795 EL |
452 | "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n" |
453 | "}\n" | |
454 | "}\n" | |
af710487 | 455 | "for (int i = 0; i < repl.val_ptr->n_elements; i++) {\n" |
456 | "if (repl.val_ptr->value_elements[i] != NULL) {\n" | |
970ed795 EL |
457 | "ret_val.val_ptr->value_elements[i+index] = " |
458 | "new %s(*repl.val_ptr->value_elements[i]);\n" | |
459 | "}\n" | |
460 | "}\n" | |
af710487 | 461 | "for (int i = 0; i < val_ptr->n_elements - index - len; i++) {\n" |
462 | "if (val_ptr->value_elements[index+i+len] != NULL) {\n" | |
463 | "ret_val.val_ptr->value_elements[index+i+repl.val_ptr->n_elements] = " | |
970ed795 EL |
464 | "new %s(*val_ptr->value_elements[index+i+len]);\n" |
465 | "}\n" | |
466 | "}\n" | |
467 | "return ret_val;\n" | |
468 | "}\n\n", name, name, name, dispname, dispname, dispname, name, type, type, type); | |
469 | def = mputprintf(def, | |
470 | "%s replace(int index, int len, const %s_template& repl) const;\n\n", | |
471 | name, name); | |
472 | src = mputprintf(src, | |
473 | "%s %s::replace(int index, int len, const %s_template& repl) const\n" | |
474 | "{\n" | |
475 | "if (!repl.is_value()) TTCN_error(\"The fourth argument of function " | |
476 | "replace() is a template with non-specific value.\");\n" | |
477 | "return replace(index, len, repl.valueof());\n" | |
478 | "}\n\n", name, name, name); | |
479 | ||
480 | /* set_size function */ | |
481 | def = mputstr(def, "void set_size(int new_size);\n"); | |
482 | src = mputprintf(src, "void %s::set_size(int new_size)\n" | |
483 | "{\n" | |
484 | "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size " | |
485 | "for a value of type %s.\");\n" | |
486 | "if (val_ptr == NULL) {\n" | |
487 | "val_ptr = new recordof_setof_struct;\n" | |
488 | "val_ptr->ref_count = 1;\n" | |
489 | "val_ptr->n_elements = 0;\n" | |
490 | "val_ptr->value_elements = NULL;\n" | |
491 | "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */ | |
492 | "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n" | |
493 | "new_val_ptr->ref_count = 1;\n" | |
494 | "new_val_ptr->n_elements = (new_size < val_ptr->n_elements) ? " | |
495 | "new_size : val_ptr->n_elements;\n" | |
496 | "new_val_ptr->value_elements = " | |
497 | "(%s**)allocate_pointers(new_val_ptr->n_elements);\n" | |
498 | "for (int elem_count = 0; elem_count < new_val_ptr->n_elements; " | |
499 | "elem_count++) {\n" | |
500 | "if (val_ptr->value_elements[elem_count] != NULL){\n" | |
501 | "new_val_ptr->value_elements[elem_count] = " | |
502 | "new %s(*(val_ptr->value_elements[elem_count]));\n" | |
503 | "}\n" | |
504 | "}\n" | |
505 | "clean_up();\n" | |
506 | "val_ptr = new_val_ptr;\n" | |
507 | "}\n" | |
508 | "if (new_size > val_ptr->n_elements) {\n" | |
509 | "val_ptr->value_elements = (%s**)" | |
510 | "reallocate_pointers((void**)val_ptr->value_elements, " | |
511 | "val_ptr->n_elements, new_size);\n" | |
512 | "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n" | |
513 | "if((val_ptr->n_elements/1000)!=(new_size/1000)) " | |
514 | "TTCN_warning(\"New size of type %s: %%d\",new_size);\n" | |
515 | "#endif\n" | |
516 | "val_ptr->n_elements = new_size;\n" | |
517 | "} else if (new_size < val_ptr->n_elements) {\n" | |
518 | "for (int elem_count = new_size; elem_count < val_ptr->n_elements; " | |
af710487 | 519 | "elem_count++)\n" |
520 | "if (val_ptr->value_elements[elem_count] != NULL)" | |
970ed795 | 521 | "delete val_ptr->value_elements[elem_count];\n" |
970ed795 EL |
522 | "val_ptr->value_elements = (%s**)" |
523 | "reallocate_pointers((void**)val_ptr->value_elements, " | |
524 | "val_ptr->n_elements, new_size);\n" | |
525 | "val_ptr->n_elements = new_size;\n" | |
526 | "}\n" | |
970ed795 EL |
527 | "}\n\n", name, dispname, type, type, type, dispname, type); |
528 | ||
529 | /* is_bound function */ | |
530 | def = mputstr(def, | |
af710487 | 531 | "inline boolean is_bound() const {return val_ptr != NULL; }\n"); |
970ed795 EL |
532 | |
533 | /* is_present function */ | |
534 | def = mputstr(def, | |
535 | "inline boolean is_present() const { return is_bound(); }\n"); | |
536 | ||
537 | /* is_value function */ | |
538 | def = mputstr(def, | |
539 | "boolean is_value() const;\n"); | |
540 | src = mputprintf(src, | |
541 | "boolean %s::is_value() const\n" | |
542 | "{\n" | |
543 | "if (val_ptr == NULL) return false;\n" | |
af710487 | 544 | "for(int i = 0; i < val_ptr->n_elements; ++i) {\n" |
545 | "if (val_ptr->value_elements[i] == NULL || " | |
970ed795 EL |
546 | "!val_ptr->value_elements[i]->is_value()) return FALSE;\n" |
547 | "}\n" | |
548 | "return TRUE;\n" | |
549 | "}\n\n", name); | |
550 | ||
551 | /* sizeof operation */ | |
552 | def = mputstr(def, | |
553 | "int size_of() const;\n" | |
554 | "int n_elem() const { return size_of(); }\n"); | |
555 | src = mputprintf(src, | |
556 | "int %s::size_of() const\n" | |
557 | "{\n" | |
558 | "if (val_ptr == NULL) " | |
559 | "TTCN_error(\"Performing sizeof operation on an unbound value of type " | |
560 | "%s.\");\n" | |
af710487 | 561 | "return val_ptr->n_elements;\n" |
970ed795 EL |
562 | "}\n\n", name, dispname); |
563 | ||
564 | /* lengthof operation */ | |
565 | def = mputstr(def, "int lengthof() const;\n"); | |
566 | src = mputprintf(src, | |
567 | "int %s::lengthof() const\n" | |
568 | "{\n" | |
569 | "if (val_ptr == NULL) " | |
570 | "TTCN_error(\"Performing lengthof operation on an unbound value of type " | |
571 | "%s.\");\n" | |
af710487 | 572 | "for (int my_length=val_ptr->n_elements; my_length>0; my_length--) " |
573 | "if (val_ptr->value_elements[my_length-1] != NULL) return my_length;\n" | |
970ed795 EL |
574 | "return 0;\n" |
575 | "}\n\n", name, dispname); | |
576 | ||
577 | /* log function */ | |
578 | def = mputstr(def, "void log() const;\n"); | |
579 | src = mputprintf | |
580 | (src, | |
581 | "void %s::log() const\n" | |
582 | "{\n" | |
583 | "if (val_ptr == NULL) {;\n" | |
584 | "TTCN_Logger::log_event_unbound();\n" | |
585 | "return;\n" | |
586 | "}\n" | |
af710487 | 587 | "switch (val_ptr->n_elements) {\n" |
970ed795 EL |
588 | "case 0:\n" |
589 | "TTCN_Logger::log_event_str(\"{ }\");\n" | |
590 | "break;\n" | |
591 | "default:\n" | |
592 | "TTCN_Logger::log_event_str(\"{ \");\n" | |
af710487 | 593 | "for (int elem_count = 0; elem_count < val_ptr->n_elements; " |
970ed795 EL |
594 | "elem_count++) {\n" |
595 | "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n" | |
596 | "(*this)[elem_count].log();\n" | |
597 | "}\n" | |
598 | "TTCN_Logger::log_event_str(\" }\");\n" | |
599 | "}\n" | |
600 | "}\n\n", name); | |
601 | ||
602 | /* set_param function */ | |
603 | def = mputstr(def, "void set_param(Module_Param& param);\n"); | |
604 | src = mputprintf(src, | |
605 | "void %s::set_param(Module_Param& param)\n" | |
606 | "{\n" | |
607 | " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n" | |
608 | " param.get_id()->next_name()) {\n" | |
609 | // Haven't reached the end of the module parameter name | |
610 | // => the name refers to one of the elements, not to the whole record of | |
611 | " char* param_field = param.get_id()->get_current_name();\n" | |
612 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
613 | " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n" | |
614 | " \" index for %s type `%s'\");\n" | |
615 | " }\n" | |
616 | " int param_index = -1;\n" | |
617 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
618 | " (*this)[param_index].set_param(param);\n" | |
619 | " return;\n" | |
620 | " }\n" | |
621 | " param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n" | |
3abe9331 | 622 | " Module_Param_Ptr mp = ¶m;\n" |
623 | " if (param.get_type() == Module_Param::MP_Reference) {\n" | |
624 | " mp = param.get_referenced_param();\n" | |
625 | " }\n" | |
970ed795 EL |
626 | " switch (param.get_operation_type()) {\n" |
627 | " case Module_Param::OT_ASSIGN:\n" | |
3abe9331 | 628 | " if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {\n" |
970ed795 EL |
629 | " *this = NULL_VALUE;\n" |
630 | " return;\n" | |
631 | " }\n" | |
3abe9331 | 632 | " switch (mp->get_type()) {\n" |
970ed795 | 633 | " case Module_Param::MP_Value_List:\n" |
3abe9331 | 634 | " set_size(mp->get_size());\n" |
635 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" | |
636 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 EL |
637 | " if (curr->get_type()!=Module_Param::MP_NotUsed) {\n" |
638 | " (*this)[i].set_param(*curr);\n" | |
9e3e25dc BB |
639 | " if (!(*this)[i].is_bound()) {\n" |
640 | " delete val_ptr->value_elements[i];\n" | |
641 | " val_ptr->value_elements[i] = NULL;\n" | |
642 | " }\n" | |
970ed795 EL |
643 | " }\n" |
644 | " }\n" | |
645 | " break;\n" | |
646 | " case Module_Param::MP_Indexed_List:\n" | |
3abe9331 | 647 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" |
648 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 | 649 | " (*this)[curr->get_id()->get_index()].set_param(*curr);\n" |
9e3e25dc BB |
650 | " if (!(*this)[curr->get_id()->get_index()].is_bound()) {\n" |
651 | " delete val_ptr->value_elements[curr->get_id()->get_index()];\n" | |
652 | " val_ptr->value_elements[curr->get_id()->get_index()] = NULL;\n" | |
653 | " }\n" | |
970ed795 EL |
654 | " }\n" |
655 | " break;\n" | |
656 | " default:\n" | |
657 | " param.type_error(\"%s value\", \"%s\");\n" | |
658 | " }\n" | |
659 | " break;\n" | |
660 | " case Module_Param::OT_CONCAT:\n" | |
3abe9331 | 661 | " switch (mp->get_type()) {\n" |
970ed795 EL |
662 | " case Module_Param::MP_Value_List: {\n" |
663 | " if (!is_bound()) *this = NULL_VALUE;\n" | |
664 | " int start_idx = lengthof();\n" | |
3abe9331 | 665 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" |
666 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 EL |
667 | " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n" |
668 | " (*this)[start_idx+(int)i].set_param(*curr);\n" | |
669 | " }\n" | |
670 | " }\n" | |
671 | " } break;\n" | |
672 | " case Module_Param::MP_Indexed_List:\n" | |
673 | " param.error(\"Cannot concatenate an indexed value list\");\n" | |
674 | " break;\n" | |
675 | " default:\n" | |
676 | " param.type_error(\"%s value\", \"%s\");\n" | |
677 | " }\n" | |
678 | " break;\n" | |
679 | " default:\n" | |
680 | " TTCN_error(\"Internal error: Unknown operation type.\");\n" | |
681 | " }\n" | |
3abe9331 | 682 | "}\n\n", name, sdef->kind == RECORD_OF ? "record of" : "set of", |
970ed795 EL |
683 | dispname, sdef->kind == RECORD_OF ? "record of" : "set of", |
684 | sdef->kind == RECORD_OF ? "record of" : "set of", dispname, | |
685 | sdef->kind == RECORD_OF ? "record of" : "set of", dispname); | |
3abe9331 | 686 | |
687 | /* get param function */ | |
688 | def = mputstr(def, "Module_Param* get_param(Module_Param_Name& param_name) const;\n"); | |
689 | src = mputprintf | |
690 | (src, | |
691 | "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n" | |
692 | "{\n" | |
693 | " if (!is_bound()) {\n" | |
694 | " return new Module_Param_Unbound();\n" | |
695 | " }\n" | |
696 | " if (param_name.next_name()) {\n" | |
697 | // Haven't reached the end of the module parameter name | |
698 | // => the name refers to one of the elements, not to the whole record of | |
699 | " char* param_field = param_name.get_current_name();\n" | |
700 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
701 | " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n" | |
702 | " \"expected a valid index for %s type `%s'\");\n" | |
703 | " }\n" | |
704 | " int param_index = -1;\n" | |
705 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
706 | " return (*this)[param_index].get_param(param_name);\n" | |
707 | " }\n" | |
708 | " Vector<Module_Param*> values;\n" | |
709 | " for (int i = 0; i < val_ptr->n_elements; ++i) {\n" | |
710 | " values.push_back((*this)[i].get_param(param_name));\n" | |
711 | " }\n" | |
712 | " Module_Param_Value_List* mp = new Module_Param_Value_List();\n" | |
713 | " mp->add_list_with_implicit_ids(&values);\n" | |
714 | " values.clear();\n" | |
715 | " return mp;\n" | |
716 | "}\n\n", name, sdef->kind == RECORD_OF ? "record of" : "set of", dispname); | |
970ed795 EL |
717 | |
718 | /* set implicit omit function, recursive */ | |
719 | def = mputstr(def, " void set_implicit_omit();\n"); | |
720 | src = mputprintf(src, | |
721 | "void %s::set_implicit_omit()\n{\n" | |
722 | "if (val_ptr == NULL) return;\n" | |
af710487 | 723 | "for (int i = 0; i < val_ptr->n_elements; i++) {\n" |
724 | "if (val_ptr->value_elements[i] != NULL) val_ptr->value_elements[i]->set_implicit_omit();\n" | |
970ed795 | 725 | "}\n}\n\n", name); |
970ed795 EL |
726 | |
727 | /* encoding / decoding functions */ | |
728 | def = mputstr(def, "void encode_text(Text_Buf& text_buf) const;\n"); | |
729 | src = mputprintf(src, | |
730 | "void %s::encode_text(Text_Buf& text_buf) const\n" | |
731 | "{\n" | |
732 | "if (val_ptr == NULL) " | |
733 | "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n" | |
af710487 | 734 | "text_buf.push_int(val_ptr->n_elements);\n" |
735 | "for (int elem_count = 0; elem_count < val_ptr->n_elements; " | |
970ed795 EL |
736 | "elem_count++)\n" |
737 | "(*this)[elem_count].encode_text(text_buf);\n" | |
738 | "}\n\n", name, dispname); | |
739 | ||
740 | def = mputstr(def, "void decode_text(Text_Buf& text_buf);\n"); | |
741 | src = mputprintf(src, | |
742 | "void %s::decode_text(Text_Buf& text_buf)\n" | |
743 | "{\n" | |
af710487 | 744 | "clean_up();\n" |
745 | "val_ptr = new recordof_setof_struct;\n" | |
746 | "val_ptr->ref_count = 1;\n" | |
747 | "val_ptr->n_elements = text_buf.pull_int().get_val();\n" | |
748 | "if (val_ptr->n_elements < 0) TTCN_error(\"Text decoder: Negative size " | |
970ed795 | 749 | "was received for a value of type %s.\");\n" |
af710487 | 750 | "val_ptr->value_elements = (%s**)allocate_pointers(val_ptr->n_elements);\n" |
751 | "for (int elem_count = 0; elem_count < val_ptr->n_elements; " | |
970ed795 | 752 | "elem_count++) {\n" |
970ed795 | 753 | "val_ptr->value_elements[elem_count] = new %s;\n" |
970ed795 EL |
754 | "val_ptr->value_elements[elem_count]->decode_text(text_buf);\n" |
755 | "}\n" | |
af710487 | 756 | "}\n\n", name, dispname, type, type); |
970ed795 EL |
757 | |
758 | if(ber_needed || raw_needed || text_needed || xer_needed || json_needed) | |
759 | def_encdec(name, &def, &src, ber_needed, raw_needed, text_needed, | |
760 | xer_needed, json_needed, FALSE); | |
761 | ||
762 | if(text_needed){ | |
763 | src=mputprintf(src, | |
764 | "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td," | |
765 | " TTCN_Buffer& p_buf) const{\n" | |
766 | " int encoded_length=0;\n" | |
767 | " if(p_td.text->begin_encode){\n" | |
768 | " p_buf.put_cs(*p_td.text->begin_encode);\n" | |
769 | " encoded_length+=p_td.text->begin_encode->lengthof();\n" | |
770 | " }\n" | |
771 | " if(val_ptr==NULL) {\n" | |
772 | " TTCN_EncDec_ErrorContext::error\n" | |
773 | " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n" | |
774 | " if(p_td.text->end_encode){\n" | |
775 | " p_buf.put_cs(*p_td.text->end_encode);\n" | |
776 | " encoded_length+=p_td.text->end_encode->lengthof();\n" | |
777 | " }\n" | |
778 | " return encoded_length;\n" | |
779 | " }\n" | |
af710487 | 780 | " for(int a=0;a<val_ptr->n_elements;a++){\n" |
970ed795 EL |
781 | " if(a!=0 && p_td.text->separator_encode){\n" |
782 | " p_buf.put_cs(*p_td.text->separator_encode);\n" | |
783 | " encoded_length+=p_td.text->separator_encode->lengthof();\n" | |
784 | " }\n" | |
a38c6d4c | 785 | " encoded_length+=(*this)[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n" |
970ed795 EL |
786 | " }\n" |
787 | " if(p_td.text->end_encode){\n" | |
788 | " p_buf.put_cs(*p_td.text->end_encode);\n" | |
789 | " encoded_length+=p_td.text->end_encode->lengthof();\n" | |
790 | " }\n" | |
791 | " return encoded_length;\n" | |
792 | "}\n" | |
a38c6d4c | 793 | ,name |
970ed795 EL |
794 | ); |
795 | src = mputprintf(src, | |
796 | "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td," | |
797 | " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err" | |
798 | ", boolean first_call){\n" | |
799 | " int decoded_length=0;\n" | |
800 | " size_t pos=p_buf.get_pos();\n" | |
801 | " boolean sep_found=FALSE;\n" | |
802 | " int sep_length=0;\n" | |
803 | " int ml=0;\n" | |
804 | " if(p_td.text->begin_decode){\n" | |
805 | " int tl;\n" | |
806 | " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n" | |
807 | " if(no_err)return -1;\n" | |
808 | " TTCN_EncDec_ErrorContext::error\n" | |
809 | " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'" | |
810 | " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)" | |
811 | ", p_td.name);\n" | |
812 | " return 0;\n" | |
813 | " }\n" | |
814 | " decoded_length+=tl;\n" | |
815 | " p_buf.increase_pos(tl);\n" | |
816 | " }\n" | |
817 | " if(p_td.text->end_decode){\n" | |
818 | " limit.add_token(p_td.text->end_decode);\n" | |
819 | " ml++;\n" | |
820 | " }\n" | |
821 | " if(p_td.text->separator_decode){\n" | |
822 | " limit.add_token(p_td.text->separator_decode);\n" | |
823 | " ml++;\n" | |
824 | " }\n" | |
825 | " if(first_call) {\n" | |
af710487 | 826 | " clean_up();\n" |
827 | " val_ptr=new recordof_setof_struct;\n" | |
828 | " val_ptr->ref_count=1;\n" | |
829 | " val_ptr->n_elements=0;\n" | |
830 | " val_ptr->value_elements=NULL;\n" | |
970ed795 | 831 | " }\n" |
af710487 | 832 | " int more=val_ptr->n_elements;\n" |
970ed795 EL |
833 | " while(TRUE){\n" |
834 | " %s *val=new %s;\n" | |
835 | " pos=p_buf.get_pos();\n" | |
a38c6d4c | 836 | " int len=val->TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n" |
970ed795 EL |
837 | " if(len==-1 || (len==0 && !limit.has_token())){\n" |
838 | " p_buf.set_pos(pos);\n" | |
839 | " delete val;\n" | |
840 | " if(sep_found){\n" | |
841 | " p_buf.set_pos(p_buf.get_pos()-sep_length);\n" | |
842 | " decoded_length-=sep_length;\n" | |
843 | " }\n" | |
844 | " break;\n" | |
845 | " }\n" | |
846 | " sep_found=FALSE;\n" | |
af710487 | 847 | " val_ptr->value_elements = (%s**)reallocate_pointers" |
970ed795 EL |
848 | "((void**)val_ptr->value_elements, val_ptr->n_elements, " |
849 | "val_ptr->n_elements + 1);\n" | |
af710487 | 850 | " val_ptr->value_elements[val_ptr->n_elements]=val;\n" |
851 | " val_ptr->n_elements++;\n" | |
970ed795 EL |
852 | " decoded_length+=len;\n" |
853 | " if(p_td.text->separator_decode){\n" | |
854 | " int tl;\n" | |
855 | " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n" | |
856 | " break;\n" | |
857 | " }\n" | |
858 | " decoded_length+=tl;\n" | |
859 | " p_buf.increase_pos(tl);\n" | |
860 | " sep_length=tl;\n" | |
861 | " sep_found=TRUE;\n" | |
862 | " } else if(p_td.text->end_decode){\n" | |
863 | " int tl;\n" | |
864 | " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n" | |
865 | " decoded_length+=tl;\n" | |
866 | " p_buf.increase_pos(tl);\n" | |
867 | " limit.remove_tokens(ml);\n" | |
868 | " return decoded_length;\n" | |
869 | " }\n" | |
870 | " } else if(limit.has_token(ml)){\n" | |
871 | " int tl;\n" | |
872 | " if((tl=limit.match(p_buf,ml))==0){\n" | |
873 | " sep_found=FALSE;\n" | |
874 | " break;\n" | |
875 | " }\n" | |
876 | " }\n" | |
877 | " }\n" | |
a38c6d4c | 878 | ,name,type,type,type |
970ed795 EL |
879 | ); |
880 | src = mputstr(src, | |
881 | " limit.remove_tokens(ml);\n" | |
882 | " if(p_td.text->end_decode){\n" | |
883 | " int tl;\n" | |
884 | " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n" | |
885 | " if(no_err){" | |
886 | " if(!first_call){\n" | |
af710487 | 887 | " for(int a=more; a<val_ptr->n_elements; a++) " |
888 | "delete val_ptr->value_elements[a];\n" | |
889 | " val_ptr->n_elements=more;\n" | |
970ed795 EL |
890 | " }\n" |
891 | " return -1;\n" | |
892 | " }\n" | |
893 | " TTCN_EncDec_ErrorContext::error" | |
894 | "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'" | |
895 | " not found for '%s': \",(const char*)*(p_td.text->end_decode)" | |
896 | ",p_td.name);\n" | |
897 | " return decoded_length;\n" | |
898 | " }\n" | |
899 | " decoded_length+=tl;\n" | |
900 | " p_buf.increase_pos(tl);\n" | |
901 | " }\n" | |
af710487 | 902 | " if(val_ptr->n_elements==0){\n" |
970ed795 EL |
903 | " if(!(p_td.text->end_decode || p_td.text->begin_decode)) {\n" |
904 | " if(no_err)return -1;\n" | |
905 | " TTCN_EncDec_ErrorContext::error" | |
906 | "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n" | |
907 | " return decoded_length;\n" | |
908 | " }\n" | |
909 | " }\n" | |
af710487 | 910 | " if(!first_call && more==val_ptr->n_elements && " |
970ed795 EL |
911 | "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n" |
912 | " return decoded_length;\n" | |
913 | "}\n" | |
914 | ); | |
915 | } | |
916 | ||
917 | /* BER functions */ | |
918 | if(ber_needed) { | |
919 | /* BER_encode_TLV() */ | |
920 | src=mputprintf | |
921 | (src, | |
922 | "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&" | |
923 | " p_td, unsigned p_coding) const\n" | |
924 | "{\n" | |
925 | " BER_chk_descr(p_td);\n" | |
926 | " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n" | |
927 | " if(!new_tlv) {\n" | |
928 | " new_tlv=ASN_BER_TLV_t::construct(NULL);\n" | |
929 | " TTCN_EncDec_ErrorContext ec;\n" | |
af710487 | 930 | " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n" |
970ed795 EL |
931 | " ec.set_msg(\"Component #%%d: \", elem_i);\n" |
932 | " new_tlv->add_TLV((*this)[elem_i].BER_encode_TLV" | |
a38c6d4c | 933 | "(*p_td.oftype_descr, p_coding));\n" |
970ed795 EL |
934 | " }\n" |
935 | "%s" | |
936 | " }\n" | |
937 | " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n" | |
938 | " return new_tlv;\n" | |
939 | "}\n" | |
940 | "\n" | |
941 | /* BER_decode_TLV() */ | |
942 | "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td," | |
943 | " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n" | |
944 | "{\n" | |
945 | " BER_chk_descr(p_td);\n" | |
946 | " ASN_BER_TLV_t stripped_tlv;\n" | |
947 | " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n" | |
948 | " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \"," | |
949 | " p_td.name);\n" | |
950 | " stripped_tlv.chk_constructed_flag(TRUE);\n" | |
af710487 | 951 | " clean_up();\n" |
952 | " val_ptr = new recordof_setof_struct;\n" | |
953 | " val_ptr->ref_count = 1;\n" | |
954 | " val_ptr->n_elements = 0;\n" | |
955 | " val_ptr->value_elements = NULL;\n" | |
970ed795 EL |
956 | " size_t V_pos=0;\n" |
957 | " ASN_BER_TLV_t tmp_tlv;\n" | |
958 | " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n" | |
959 | " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n" | |
960 | " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, " | |
961 | "tmp_tlv)) {\n" | |
af710487 | 962 | " val_ptr->value_elements = (%s**)reallocate_pointers(" |
963 | "(void**)val_ptr->value_elements, val_ptr->n_elements, " | |
964 | "val_ptr->n_elements + 1);\n" | |
965 | " val_ptr->n_elements++;\n" | |
966 | " val_ptr->value_elements[val_ptr->n_elements - 1] = new %s;\n" | |
a38c6d4c | 967 | " val_ptr->value_elements[val_ptr->n_elements - 1]->BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, " |
970ed795 | 968 | "L_form);\n" |
af710487 | 969 | " ec_2.set_msg(\"%%d: \", val_ptr->n_elements);\n" |
970ed795 EL |
970 | " }\n" |
971 | " return TRUE;\n" | |
972 | "}\n" | |
973 | "\n" | |
a38c6d4c | 974 | , name |
970ed795 | 975 | , sdef->kind==SET_OF?" new_tlv->sort_tlvs();\n":"" |
a38c6d4c | 976 | , name, type, type |
970ed795 EL |
977 | ); |
978 | ||
979 | if(sdef->has_opentypes) { | |
980 | /* BER_decode_opentypes() */ | |
981 | def=mputstr | |
982 | (def, | |
983 | "void BER_decode_opentypes(TTCN_Type_list& p_typelist," | |
984 | " unsigned L_form);\n"); | |
985 | src=mputprintf | |
986 | (src, | |
987 | "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist," | |
988 | " unsigned L_form)\n" | |
989 | "{\n" | |
990 | " p_typelist.push(this);\n" | |
991 | " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n" | |
992 | " TTCN_EncDec_ErrorContext ec_1;\n" | |
af710487 | 993 | " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n" |
970ed795 | 994 | " ec_1.set_msg(\"%%d: \", elem_i);\n" |
af710487 | 995 | " val_ptr->value_elements[elem_i]->BER_decode_opentypes(p_typelist," |
970ed795 EL |
996 | " L_form);\n" |
997 | " }\n" | |
998 | " p_typelist.pop();\n" | |
999 | "}\n" | |
1000 | "\n" | |
1001 | , name | |
1002 | ); | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | if(raw_needed){ | |
1007 | src=mputprintf(src, | |
1008 | "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, " | |
1009 | "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field" | |
1010 | ", boolean first_call){\n" | |
1011 | " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n" | |
1012 | " limit-=prepaddlength;\n" | |
1013 | " int decoded_length=0;\n" | |
1014 | " int decoded_field_length=0;\n" | |
1015 | " size_t start_of_field=0;\n" | |
1016 | " if(first_call) {\n" | |
af710487 | 1017 | " clean_up();\n" |
1018 | " val_ptr=new recordof_setof_struct;\n" | |
1019 | " val_ptr->ref_count=1;\n" | |
1020 | " val_ptr->n_elements=0;\n" | |
1021 | " val_ptr->value_elements=NULL;\n" | |
970ed795 | 1022 | " }\n" |
af710487 | 1023 | " int start_field=val_ptr->n_elements;\n" |
970ed795 EL |
1024 | " if(p_td.raw->fieldlength || sel_field!=-1){\n" |
1025 | " int a=0;\n" | |
1026 | " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n" | |
1027 | " for(a=0;a<sel_field;a++){\n" | |
a38c6d4c | 1028 | " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr," |
970ed795 EL |
1029 | "p_buf,limit,top_bit_ord,TRUE);\n" |
1030 | " if(decoded_field_length < 0) return decoded_field_length;\n" | |
1031 | " decoded_length+=decoded_field_length;\n" | |
1032 | " limit-=decoded_field_length;\n" | |
1033 | " }\n" | |
af710487 | 1034 | " if(a==0) val_ptr->n_elements=0;\n" |
970ed795 EL |
1035 | " } else {\n" |
1036 | " int a=start_field;\n" | |
1037 | " if(limit==0){\n" | |
1038 | " if(!first_call) return -1;\n" | |
af710487 | 1039 | " val_ptr->n_elements=0;\n" |
970ed795 EL |
1040 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" |
1041 | "+prepaddlength;\n" | |
1042 | " }\n" | |
1043 | " while(limit>0){\n" | |
1044 | " start_of_field=p_buf.get_pos_bit();\n" | |
a38c6d4c | 1045 | " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit," |
970ed795 EL |
1046 | "top_bit_ord,TRUE);\n" |
1047 | " if(decoded_field_length < 0){\n" | |
af710487 | 1048 | " delete &(*this)[a];\n" |
1049 | " val_ptr->n_elements--;\n" | |
970ed795 EL |
1050 | " p_buf.set_pos_bit(start_of_field);\n" |
1051 | " if(a>start_field){\n" | |
1052 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
1053 | "+prepaddlength;\n" | |
1054 | " } else return -1;\n" | |
1055 | " }\n" | |
1056 | " decoded_length+=decoded_field_length;\n" | |
1057 | " limit-=decoded_field_length;\n" | |
1058 | " a++;\n" | |
a38c6d4c | 1059 | ,name |
970ed795 | 1060 | ); |
a38c6d4c | 1061 | if (force_gen_seof || (sdef->raw.extension_bit!=XDEFNO && sdef->raw.extension_bit!=XDEFDEFAULT)){ |
970ed795 | 1062 | src=mputprintf(src, |
a38c6d4c | 1063 | " if (%s%sp_buf.get_last_bit()%s)\n" |
970ed795 | 1064 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" |
a38c6d4c | 1065 | "+prepaddlength;\n" |
1066 | , force_gen_seof ? "EXT_BIT_NO != p_td.raw->extension_bit && ((EXT_BIT_YES != p_td.raw->extension_bit) ^ " : "" | |
1067 | , (force_gen_seof || sdef->raw.extension_bit == XDEFYES) ? "" : "!" | |
1068 | , force_gen_seof ? ")" : ""); | |
970ed795 EL |
1069 | } |
1070 | src=mputprintf(src, | |
1071 | " }\n" | |
1072 | " }\n" | |
1073 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
1074 | "+prepaddlength;\n" | |
1075 | "}\n\n" | |
1076 | "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td," | |
1077 | "RAW_enc_tree& myleaf) const{\n" | |
1078 | " int encoded_length=0;\n" | |
1079 | " int encoded_num_of_records=p_td.raw->fieldlength?" | |
af710487 | 1080 | "smaller(val_ptr->n_elements, p_td.raw->fieldlength)" |
1081 | ":val_ptr->n_elements;\n" | |
970ed795 EL |
1082 | " myleaf.isleaf=FALSE;\n" |
1083 | " myleaf.rec_of=TRUE;\n" | |
1084 | " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n" | |
1085 | " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n" | |
1086 | " for(int a=0;a<encoded_num_of_records;a++){\n" | |
1087 | " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf," | |
a38c6d4c | 1088 | "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n" |
1089 | " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr," | |
970ed795 EL |
1090 | "*myleaf.body.node.nodes[a]);\n" |
1091 | " }\n" | |
1092 | " return myleaf.length=encoded_length;\n}\n\n" | |
a38c6d4c | 1093 | , name |
970ed795 EL |
1094 | ); |
1095 | } | |
1096 | ||
1097 | if (xer_needed) { /* XERSTUFF encoder codegen for record-of, RT1 */ | |
1098 | def = mputstr(def, | |
1099 | "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n"); | |
1100 | ||
1101 | /* Write the body of the XER encoder/decoder functions. The declaration | |
1102 | * is written by def_encdec() in encdec.c */ | |
1103 | src = mputprintf(src, | |
1104 | "boolean %s::can_start(const char *name, const char *uri, " | |
1105 | "XERdescriptor_t const& xd, unsigned int flavor) {\n" | |
1106 | " boolean e_xer = is_exer(flavor);\n" | |
a38c6d4c | 1107 | " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return " |
1108 | "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n" | |
1109 | " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) " | |
970ed795 EL |
1110 | , name |
1111 | ); | |
a38c6d4c | 1112 | if (!force_gen_seof && sdef->nFollowers) { |
970ed795 EL |
1113 | /* If there are optional fields following the record-of, then seeing |
1114 | * {any XML tag that belongs to those fields} where the record-of may be | |
1115 | * means that the record-of is empty. */ | |
1116 | size_t f; | |
1117 | src = mputstr(src, "{\n"); | |
1118 | for (f = 0; f < sdef->nFollowers; ++f) { | |
1119 | src = mputprintf(src, | |
1120 | " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n" | |
1121 | , sdef->followers[f].type | |
1122 | , sdef->followers[f].typegen | |
1123 | ); | |
1124 | } | |
1125 | src = mputstr(src, | |
1126 | " return TRUE;\n" | |
1127 | " }\n"); | |
1128 | } | |
1129 | else src = mputstr(src, "return TRUE;\n"); | |
1130 | ||
1131 | src = mputprintf(src, | |
a38c6d4c | 1132 | " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n" |
970ed795 EL |
1133 | "}\n\n" |
1134 | , sdef->type | |
970ed795 EL |
1135 | ); |
1136 | ||
1137 | src = mputprintf(src, | |
1138 | "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n" | |
1139 | " size_t num_collected;\n" | |
1140 | " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n" | |
1141 | /* The above may throw but then nothing was allocated. */ | |
1142 | " if (val_ptr) try {\n" | |
1143 | " char **new_ns;\n" | |
1144 | " size_t num_new;\n" | |
af710487 | 1145 | " for (int i = 0; i < val_ptr->n_elements; ++i) {\n" |
970ed795 | 1146 | " bool def_ns_1 = false;" |
a38c6d4c | 1147 | " new_ns = (*this)[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n" |
970ed795 EL |
1148 | " merge_ns(collected_ns, num_collected, new_ns, num_new);\n" |
1149 | " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */ | |
1150 | " }\n" | |
1151 | " }\n" | |
1152 | " catch (...) {\n" | |
1153 | /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g. | |
1154 | * encoding an unbound value. */ | |
1155 | " while (num_collected > 0) Free(collected_ns[--num_collected]);\n" | |
1156 | " Free(collected_ns);\n" | |
1157 | " throw;\n" | |
1158 | " }\n" | |
1159 | " num = num_collected;\n" | |
1160 | " return collected_ns;\n" | |
1161 | "}\n\n" | |
a38c6d4c | 1162 | , name); |
970ed795 EL |
1163 | |
1164 | src=mputprintf(src, | |
af710487 | 1165 | "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, " |
1166 | "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n" | |
970ed795 EL |
1167 | " if (val_ptr == 0) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */ |
1168 | " int encoded_length=(int)p_buf.get_len();\n" | |
1169 | " boolean e_xer = is_exer(p_flavor);\n" | |
1170 | " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n" | |
1171 | " || (p_flavor & USE_TYPE_ATTR)));\n" | |
1172 | " boolean indenting = !is_canonical(p_flavor) && own_tag;\n" | |
1173 | "%s" /* Factor out p_indent if not attribute */ | |
af710487 | 1174 | " if (val_ptr->n_elements==0) {\n" /* Empty record of */ |
970ed795 | 1175 | , name |
a38c6d4c | 1176 | , force_gen_seof ? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
1177 | : (sdef->xerAttribute ? "" : " if (indenting) do_indent(p_buf, p_indent);\n") | |
970ed795 | 1178 | ); |
a38c6d4c | 1179 | if (force_gen_seof || sdef->xerAttribute) { |
1180 | src=mputprintf(src, | |
1181 | " if (e_xer%s) {\n" /* Empty attribute. */ | |
970ed795 EL |
1182 | " begin_attribute(p_td, p_buf);\n" |
1183 | " p_buf.put_c('\\'');\n" | |
a38c6d4c | 1184 | " } else\n" |
1185 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 | 1186 | } |
a38c6d4c | 1187 | if (force_gen_seof || !sdef->xerAttribute) { |
970ed795 EL |
1188 | src = mputstr(src, |
1189 | " if (own_tag)"); | |
1190 | } | |
1191 | src=mputprintf(src, | |
1192 | " {\n" /* Do empty element tag */ | |
1193 | "%s" | |
1194 | " p_buf.put_c('<');\n" | |
1195 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
1196 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n" | |
1197 | /* namespace declarations for the toplevel element */ | |
1198 | " if (e_xer && p_indent==0)\n" | |
1199 | " {\n" | |
1200 | " size_t num_collected = 0;\n" | |
1201 | " char **collected_ns = NULL;\n" | |
1202 | " bool def_ns = false;\n" | |
1203 | " collected_ns = collect_ns(p_td, num_collected, def_ns);\n" | |
1204 | " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n" | |
1205 | " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n" | |
1206 | " Free(collected_ns[cur_coll]);\n" | |
1207 | " }\n" | |
1208 | " Free(collected_ns);\n" | |
1209 | " }\n" | |
1210 | ||
1211 | " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n" | |
1212 | " }\n" | |
1213 | " }\n" | |
1214 | " else {\n" /* Not empty record of. Start tag or attribute */ | |
a38c6d4c | 1215 | , force_gen_seof ? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
1216 | : (sdef->xerAttribute ? " if (indenting) do_indent(p_buf, p_indent);\n" : "") | |
970ed795 EL |
1217 | ); |
1218 | if (sdef->xerAnyAttrElem) { | |
1219 | src = mputstr(src, | |
1220 | " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n" | |
1221 | " static const universal_char sp = { 0,0,0,' ' };\n" | |
1222 | " static const universal_char tb = { 0,0,0,9 };\n" | |
1223 | " size_t buf_len = p_buf.get_len(), shorter = 0;\n" | |
1224 | " const unsigned char * const buf_data = p_buf.get_data();\n" | |
1225 | " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n" | |
1226 | " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n" | |
1227 | " unsigned char saved[4];\n" | |
1228 | " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n" | |
1229 | " p_buf.increase_length(-shorter);\n" | |
af710487 | 1230 | " for (int i = 0; i < val_ptr->n_elements; ++i) {\n" |
970ed795 | 1231 | " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n" |
af710487 | 1232 | " if (val_ptr->value_elements[i] == NULL) {\n" |
970ed795 EL |
1233 | " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n" |
1234 | " \"Encoding an unbound universal charstring value.\");\n" | |
1235 | " continue;\n" | |
1236 | " }\n" | |
1237 | " size_t len = val_ptr->value_elements[i]->lengthof();\n" | |
1238 | " for (;;) {\n" | |
1239 | " const UNIVERSAL_CHARSTRING_ELEMENT& ue = (*val_ptr->value_elements[i])[len - 1];\n" | |
1240 | " if (sp == ue || tb == ue) --len;\n" | |
1241 | " else break;\n" | |
1242 | " }\n" | |
1243 | " size_t j, sp_at = 0;\n" | |
1244 | /* Find the "separators" in each string */ | |
1245 | " for (j = 0; j < len; j++) {\n" | |
1246 | " UNIVERSAL_CHARSTRING_ELEMENT ue = (*val_ptr->value_elements[i])[j];\n" | |
1247 | " if (sp_at) {\n" | |
1248 | " if (sp == ue || tb == ue) {}\n" | |
1249 | " else break;\n" | |
1250 | " } else {\n" | |
1251 | " if (sp == ue || tb == ue) sp_at = j;\n" | |
1252 | " }\n" | |
1253 | " } // next j\n" | |
1254 | " size_t buf_start = p_buf.get_len();\n" | |
1255 | /* Now write them */ | |
1256 | " if (sp_at > 0) {\n" | |
1257 | " char * ns = mprintf(\" xmlns:b%d='\", i);\n" | |
1258 | " size_t ns_len = mstrlen(ns);\n" | |
1259 | " p_buf.put_s(ns_len, (const unsigned char*)ns);\n" | |
1260 | ||
1261 | " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(*val_ptr->value_elements[i]));\n" | |
af710487 | 1262 | " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n" |
970ed795 EL |
1263 | // Ensure the namespace abides to its restrictions |
1264 | " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n" | |
1265 | " TTCN_Buffer ns_buf;\n" | |
1266 | " before.encode_utf8(ns_buf);\n" | |
1267 | " CHARSTRING cs;\n" | |
1268 | " ns_buf.get_string(cs);\n" | |
1269 | " check_namespace_restrictions(p_td, (const char*)cs);\n" | |
1270 | " }\n" | |
1271 | ||
1272 | " p_buf.put_c('\\'');\n" | |
1273 | " p_buf.put_c(' ');\n" | |
1274 | ||
1275 | /* Keep just the "b%d" part from ns */ | |
1276 | " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n" | |
1277 | " p_buf.put_c(':');\n" | |
1278 | " Free(ns);\n" | |
1279 | " }\n" | |
1280 | " else {\n" | |
1281 | " p_buf.put_c(' ');\n" | |
1282 | " j = 0;\n" | |
1283 | // Make sure the unqualified namespace is allowed | |
1284 | " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n" | |
1285 | " check_namespace_restrictions(p_td, NULL);\n" | |
1286 | " }\n" | |
1287 | " }\n" | |
1288 | ||
1289 | " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(*val_ptr->value_elements[i]) + j);\n" | |
af710487 | 1290 | " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n" |
970ed795 EL |
1291 | // Put this attribute in a dummy element and walk through it to check its validity |
1292 | " TTCN_Buffer check_buf;\n" | |
1293 | " check_buf.put_s(2, (unsigned char*)\"<a\");\n" | |
1294 | " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n" | |
1295 | " check_buf.put_s(2, (unsigned char*)\"/>\");" | |
1296 | " XmlReaderWrap checker(check_buf);\n" | |
1297 | " while (1 == checker.Read()) ;\n" | |
1298 | " }\n" | |
1299 | ||
1300 | " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */ | |
1301 | " } else {\n"); | |
1302 | } | |
a38c6d4c | 1303 | if (force_gen_seof || sdef->xerAttribute) { |
1304 | src=mputprintf(src, | |
1305 | " if (e_xer%s) {\n" | |
970ed795 | 1306 | " begin_attribute(p_td, p_buf);\n" |
a38c6d4c | 1307 | " } else\n" |
1308 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 EL |
1309 | } |
1310 | src=mputprintf(src, | |
1311 | " if (own_tag) {\n" | |
1312 | "%s" | |
1313 | " p_buf.put_c('<');\n" | |
1314 | " boolean write_ns = (e_xer && p_indent==0);\n" | |
1315 | " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n" | |
1316 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
1317 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), " | |
1318 | "(const unsigned char*)p_td.names[e_xer]);\n" | |
1319 | ||
1320 | /* namespace declarations for the toplevel element */ | |
1321 | " if (e_xer && p_indent==0)\n" | |
1322 | " {\n" | |
1323 | " size_t num_collected = 0;\n" | |
1324 | " char **collected_ns = NULL;\n" | |
1325 | " bool def_ns = false;\n" | |
1326 | " collected_ns = collect_ns(p_td, num_collected, def_ns);\n" | |
1327 | " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n" | |
1328 | " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n" | |
1329 | " Free(collected_ns[cur_coll]);\n" | |
1330 | " }\n" | |
1331 | " Free(collected_ns);\n" | |
1332 | " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n" | |
1333 | " }\n" | |
a38c6d4c | 1334 | , force_gen_seof ? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
1335 | : (sdef->xerAttribute ? " if (indenting) do_indent(p_buf, p_indent);\n" : "") | |
970ed795 EL |
1336 | ); |
1337 | if (sdef->xmlValueList) { | |
1338 | src=mputstr(src, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */ | |
1339 | } | |
1340 | src=mputstr(src, | |
1341 | " }\n" | |
1342 | " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n" | |
1343 | " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n" | |
1344 | " TTCN_EncDec_ErrorContext ec_1;\n" | |
1345 | ); | |
a38c6d4c | 1346 | src=mputstr(src, |
af710487 | 1347 | " for (int i = 0; i < val_ptr->n_elements; ++i) {\n" |
a38c6d4c | 1348 | " if (i > 0 && !own_tag && 0 != emb_val &&\n" |
1349 | " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n" | |
1350 | " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n" | |
1351 | " if (0 != emb_val->embval_array_reg) {\n" | |
1352 | " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n" | |
1353 | " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n" | |
1354 | " }\n" | |
1355 | " else {\n" | |
1356 | " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n" | |
1357 | " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n" | |
1358 | " }\n" | |
af710487 | 1359 | " ++emb_val->embval_index;\n" |
a38c6d4c | 1360 | " }\n" |
1361 | " ec_1.set_msg(\"%d: \", i);\n" | |
970ed795 | 1362 | " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n" |
a38c6d4c | 1363 | " (*this)[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n" |
970ed795 | 1364 | " }\n" |
a38c6d4c | 1365 | " if (indenting && !is_exerlist(p_flavor)) {\n" |
970ed795 EL |
1366 | ); |
1367 | if (sdef->xmlValueList) { | |
1368 | src=mputstr(src, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */ | |
1369 | } | |
1370 | src=mputstr(src, | |
1371 | " do_indent(p_buf, p_indent);\n" | |
1372 | " }\n"); | |
a38c6d4c | 1373 | if (force_gen_seof || sdef->xerAttribute) { |
1374 | src=mputprintf(src, | |
1375 | " if (e_xer%s) p_buf.put_c('\\'');\n" | |
1376 | " else\n" | |
1377 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 EL |
1378 | } |
1379 | src=mputstr(src, | |
1380 | " if (own_tag){\n" | |
1381 | " p_buf.put_c('<');\n" | |
1382 | " p_buf.put_c('/');\n" | |
1383 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
1384 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n" | |
1385 | " }\n"); | |
1386 | if (sdef->xerAnyAttrElem) { | |
1387 | src = mputstr(src, " }\n"); | |
1388 | } | |
1389 | src=mputstr(src, | |
1390 | " }\n" /* end if(no elements) */ | |
1391 | " return (int)p_buf.get_len() - encoded_length;\n" | |
1392 | "}\n\n" | |
1393 | ); | |
1394 | ||
1395 | src = mputprintf(src, /* XERSTUFF decoder codegen for record-of */ | |
1396 | #ifndef NDEBUG | |
1397 | "// written by %s in " __FILE__ " at %d\n" | |
1398 | #endif | |
1399 | "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, " | |
feade998 | 1400 | "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n" |
970ed795 EL |
1401 | " boolean e_xer = is_exer(p_flavor);\n" |
1402 | " int xerbits = p_td.xer_bits;\n" | |
1403 | " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n" | |
1404 | " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n" | |
1405 | " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */ | |
1406 | /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */ | |
1407 | " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n" | |
1408 | " int rd_ok=1, xml_depth=-1;\n" | |
af710487 | 1409 | " *this = NULL_VALUE;\n" /* empty but initialized array */ |
970ed795 EL |
1410 | " int type = 0;\n" /* none */ |
1411 | " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n" | |
1412 | " type = p_reader.NodeType();\n" | |
1413 | " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n" | |
1414 | " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n" | |
1415 | " || XML_READER_TYPE_ATTRIBUTE == type) {\n" | |
1416 | " verify_name(p_reader, p_td, e_xer);\n" | |
51fa56b9 | 1417 | " break;\n" |
970ed795 EL |
1418 | " }\n" |
1419 | " }\n" | |
1420 | " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" | |
1421 | " if (XML_READER_TYPE_TEXT == type) break;\n" | |
1422 | " }\n" | |
1423 | " else {\n" | |
1424 | " if (XML_READER_TYPE_ELEMENT == type) {\n" | |
1425 | " verify_name(p_reader, p_td, e_xer);\n" | |
1426 | " xml_depth = p_reader.Depth();\n" | |
1427 | " break;\n" | |
1428 | " }\n" | |
1429 | " }\n" /* endif(e_xer && list) */ | |
1430 | " }\n" /* next read */ | |
1431 | " else xml_depth = p_reader.Depth();\n" | |
1432 | " p_flavor |= XER_RECOF;\n" | |
a38c6d4c | 1433 | " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n" |
1434 | " TTCN_EncDec_ErrorContext ec_1;\n" | |
970ed795 EL |
1435 | #ifndef NDEBUG |
1436 | , __FUNCTION__, __LINE__ | |
1437 | #endif | |
1438 | , name | |
1439 | ); | |
1440 | ||
a38c6d4c | 1441 | src = mputstr(src, |
970ed795 EL |
1442 | " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/ |
1443 | " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */ | |
1444 | " size_t x_pos = 0;\n" | |
1445 | " size_t x_len = strlen(x_val);\n" | |
1446 | /* The string contains a bunch of values separated by whitespace. | |
1447 | * Tokenize the string and create a new buffer which looks like | |
1448 | * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>), | |
1449 | * then use that to decode the value. */ | |
1450 | " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n" | |
1451 | // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell | |
1452 | " x_pos += strlen(str) + 1;\n" | |
1453 | " TTCN_Buffer buf_2;\n" | |
1454 | " buf_2.put_c('<');\n" | |
a38c6d4c | 1455 | " write_ns_prefix(*p_td.oftype_descr, buf_2);\n" |
1456 | " const char * const exer_name = p_td.oftype_descr->names[1];\n" | |
1457 | " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n" | |
970ed795 | 1458 | /* If it has a namespace, chop off the '>' from the end */ |
a38c6d4c | 1459 | " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n" |
970ed795 | 1460 | " if (i_can_has_ns) {\n" |
a38c6d4c | 1461 | " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n" |
970ed795 EL |
1462 | " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n" |
1463 | " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n" | |
1464 | " buf_2.put_s(2, (cbyte*)\"='\");\n" | |
1465 | " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n" | |
1466 | " buf_2.put_s(2, (cbyte*)\"'>\");\n" | |
1467 | " }\n" | |
1468 | /* start tag completed */ | |
1469 | " buf_2.put_s(strlen(str), (cbyte*)str);\n" | |
1470 | " buf_2.put_c('<');\n" | |
1471 | " buf_2.put_c('/');\n" | |
a38c6d4c | 1472 | " write_ns_prefix(*p_td.oftype_descr, buf_2);\n" |
1473 | " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n" | |
970ed795 EL |
1474 | " XmlReaderWrap reader_2(buf_2);\n" |
1475 | " rd_ok = reader_2.Read();\n" /* Move to the start element. */ | |
a38c6d4c | 1476 | " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n" |
970ed795 EL |
1477 | /* Don't move to the #text, that's the callee's responsibility. */ |
1478 | /* The call to the non-const operator[] creates a new element object, | |
1479 | * then we call its XER_decode with the temporary XML reader. */ | |
feade998 | 1480 | " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n" |
af710487 | 1481 | " if (p_flavor & EXIT_ON_ERROR && !(*this)[val_ptr->n_elements - 1].is_bound()) {\n" |
1482 | " if (1 == val_ptr->n_elements) {\n" | |
970ed795 EL |
1483 | // Failed to decode even the first element |
1484 | " clean_up();\n" | |
1485 | " } else {\n" | |
1486 | // Some elements were successfully decoded -> only delete the last one | |
af710487 | 1487 | " set_size(val_ptr->n_elements - 1);\n" |
970ed795 EL |
1488 | " }\n" |
1489 | " xmlFree(x_val);\n" | |
1490 | " return -1;\n" | |
1491 | " }\n" | |
1492 | " if (x_pos >= x_len) break;\n" | |
1493 | " }\n" | |
1494 | " xmlFree(x_val);\n" | |
1495 | " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n" | |
1496 | /* Let the caller do AdvanceAttribute() */ | |
1497 | " else if (own_tag) {\n" | |
1498 | " p_reader.Read();\n" /* on closing tag */ | |
1499 | " p_reader.Read();\n" /* past it */ | |
1500 | " }\n" | |
1501 | " }\n" | |
970ed795 EL |
1502 | ); |
1503 | ||
1504 | src = mputprintf(src, | |
1505 | " else {\n" | |
1506 | " if (p_flavor & PARENT_CLOSED) ;\n" | |
1507 | /* Nothing to do, but do not advance past the parent's element */ | |
1508 | " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n" | |
1509 | /* It's our XML empty element: nothing to do, skip past it */ | |
1510 | " else {\n" | |
1511 | /* Note: there is no p_reader.Read() at the end of the loop below. | |
1512 | * Each element is supposed to consume enough to leave the next element | |
1513 | * well-positioned. */ | |
1514 | " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n" | |
1515 | " type = p_reader.NodeType();\n" | |
1516 | " if (XML_READER_TYPE_ELEMENT == type)\n" | |
1517 | " {\n"); | |
1518 | if (sdef->xerAnyAttrElem) { | |
1519 | src = mputprintf(src, | |
1520 | " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n" | |
1521 | /* This is a record-of with ANY-ELEMENT applied, which is really meant | |
1522 | * for the element type (a string), so behave like a record-of | |
1523 | * (string with ANY-ELEMENT): call the non-const operator[] | |
1524 | * to create a new element, then read the entire XML element into it. */ | |
af710487 | 1525 | " (*this)[val_ptr->n_elements] = (const char*)p_reader.ReadOuterXml();\n" |
970ed795 EL |
1526 | /* Consume the element, then move ahead */ |
1527 | " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n" | |
1528 | " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n" | |
1529 | " } else"); | |
1530 | } | |
a38c6d4c | 1531 | src = mputstr(src, |
970ed795 EL |
1532 | " {\n" |
1533 | /* An untagged record-of ends if it encounters an element with a name | |
1534 | * that doesn't match its component */ | |
1535 | " if (!own_tag && !can_start((const char*)p_reader.LocalName(), " | |
a38c6d4c | 1536 | "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n" |
970ed795 EL |
1537 | " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n" |
1538 | " break;\n" | |
1539 | " }\n" | |
a38c6d4c | 1540 | " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n" |
970ed795 | 1541 | /* The call to the non-const operator[] creates the element */ |
feade998 | 1542 | " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n" |
1543 | " }\n" | |
1544 | " if (0 != emb_val && !own_tag && val_ptr->n_elements > 1) {\n" | |
1545 | " ++emb_val->embval_index;\n" | |
970ed795 EL |
1546 | " }\n" |
1547 | " }\n" | |
1548 | " else if (XML_READER_TYPE_END_ELEMENT == type) {\n" | |
1549 | " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n" | |
1550 | " if (own_tag) {\n" | |
1551 | " verify_end(p_reader, p_td, xml_depth, e_xer);\n" | |
1552 | " rd_ok = p_reader.Read();\n" /* move forward one last time */ | |
1553 | " }\n" | |
1554 | " break;\n" | |
1555 | " }\n" | |
a38c6d4c | 1556 | " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && val_ptr->n_elements > 0) {\n" |
af710487 | 1557 | " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n" |
a38c6d4c | 1558 | " if (0 != emb_val->embval_array_reg) {\n" |
1559 | " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n" | |
1560 | " }\n" | |
1561 | " else {\n" | |
1562 | " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n" | |
1563 | " }\n" | |
af710487 | 1564 | " rd_ok = p_reader.Read();\n" |
a38c6d4c | 1565 | " }\n" |
970ed795 EL |
1566 | " else {\n" |
1567 | " rd_ok = p_reader.Read();\n" | |
1568 | " }\n" | |
1569 | " }\n" /* next read */ | |
1570 | " }\n" /* if not empty element */ | |
1571 | " }\n" /* if not LIST */ | |
51fa56b9 | 1572 | " if (!own_tag && e_xer && (p_td.xer_bits & XER_OPTIONAL) && val_ptr->n_elements == 0) {\n" |
1573 | " clean_up();\n" /* set it to unbound, so the OPTIONAL class sets it to omit */ | |
1574 | " }\n" | |
970ed795 EL |
1575 | " return 1;\n" |
1576 | "}\n\n" | |
970ed795 EL |
1577 | ); |
1578 | } | |
1579 | if (json_needed) { | |
1580 | // JSON encode, RT1 | |
1581 | src = mputprintf(src, | |
a38c6d4c | 1582 | "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n" |
970ed795 EL |
1583 | "{\n" |
1584 | " if (!is_bound()) {\n" | |
1585 | " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n" | |
1586 | " \"Encoding an unbound value of type %s.\");\n" | |
1587 | " return -1;\n" | |
1588 | " }\n\n" | |
1589 | " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n" | |
feade998 | 1590 | " for (int i = 0; i < val_ptr->n_elements; ++i) {\n" |
1591 | " if (NULL != p_td.json && p_td.json->metainfo_unbound && !(*this)[i].is_bound()) {\n" | |
1592 | // unbound elements are encoded as { "metainfo []" : "unbound" } | |
1593 | " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n" | |
1594 | " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n" | |
1595 | " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n" | |
1596 | " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n" | |
1597 | " }\n" | |
1598 | " else {\n" | |
1599 | " int ret_val = (*this)[i].JSON_encode(*p_td.oftype_descr, p_tok);\n" | |
1600 | " if (0 > ret_val) break;\n" | |
1601 | " enc_len += ret_val;\n" | |
1602 | " }\n" | |
970ed795 EL |
1603 | " }\n" |
1604 | " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n" | |
1605 | " return enc_len;\n" | |
1606 | "}\n\n" | |
a38c6d4c | 1607 | , name, dispname); |
970ed795 EL |
1608 | |
1609 | // JSON decode, RT1 | |
1610 | src = mputprintf(src, | |
a38c6d4c | 1611 | "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n" |
970ed795 EL |
1612 | "{\n" |
1613 | " json_token_t token = JSON_TOKEN_NONE;\n" | |
1614 | " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n" | |
1615 | " if (JSON_TOKEN_ERROR == token) {\n" | |
1616 | " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n" | |
1617 | " return JSON_ERROR_FATAL;\n" | |
1618 | " }\n" | |
1619 | " else if (JSON_TOKEN_ARRAY_START != token) {\n" | |
1620 | " return JSON_ERROR_INVALID_TOKEN;\n" | |
1621 | " }\n\n" | |
1622 | " set_size(0);\n" | |
feade998 | 1623 | " for (int nof_elements = 0; true; ++nof_elements) {\n" |
970ed795 | 1624 | " size_t buf_pos = p_tok.get_buf_pos();\n" |
feade998 | 1625 | " int ret_val;\n" |
1626 | " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n" | |
1627 | // check for metainfo object | |
1628 | " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n" | |
1629 | " if (JSON_TOKEN_OBJECT_START == token) {\n" | |
1630 | " char* value = NULL;\n" | |
1631 | " size_t value_len = 0;\n" | |
1632 | " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n" | |
1633 | " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n" | |
1634 | " 0 == strncmp(value, \"metainfo []\", 11)) {\n" | |
1635 | " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n" | |
1636 | " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n" | |
1637 | " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n" | |
1638 | " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n" | |
1639 | " if (JSON_TOKEN_OBJECT_END == token) {\n" | |
1640 | " dec_len += ret_val;\n" | |
1641 | " continue;\n" | |
1642 | " }\n" | |
1643 | " }\n" | |
1644 | " }\n" | |
1645 | " }\n" | |
1646 | // metainfo object not found, jump back and let the element type decode it | |
1647 | " p_tok.set_buf_pos(buf_pos);\n" | |
1648 | " }\n" | |
970ed795 | 1649 | " %s* val = new %s;\n" |
feade998 | 1650 | " ret_val = val->JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n" |
970ed795 EL |
1651 | " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n" |
1652 | " p_tok.set_buf_pos(buf_pos);\n" | |
1653 | " delete val;\n" | |
1654 | " break;\n" | |
1655 | " }\n" | |
1656 | " else if (JSON_ERROR_FATAL == ret_val) {\n" | |
1657 | " delete val;\n" | |
1658 | " if (p_silent) {\n" | |
1659 | " clean_up();\n" | |
1660 | " }\n" | |
1661 | " return JSON_ERROR_FATAL;\n" | |
1662 | " }\n" | |
af710487 | 1663 | " val_ptr->value_elements = (%s**)reallocate_pointers(\n" |
feade998 | 1664 | " (void**)val_ptr->value_elements, val_ptr->n_elements, nof_elements + 1);\n" |
1665 | " val_ptr->value_elements[nof_elements] = val;\n" | |
1666 | " val_ptr->n_elements = nof_elements + 1;\n" | |
af710487 | 1667 | " dec_len += ret_val;\n" |
970ed795 EL |
1668 | " }\n\n" |
1669 | " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n" | |
1670 | " if (JSON_TOKEN_ARRAY_END != token) {\n" | |
1671 | " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n" | |
1672 | " if (p_silent) {\n" | |
1673 | " clean_up();\n" | |
1674 | " }\n" | |
1675 | " return JSON_ERROR_FATAL;\n" | |
1676 | " }\n\n" | |
1677 | " return dec_len;\n" | |
1678 | "}\n\n" | |
a38c6d4c | 1679 | , name, type, type, type); |
970ed795 EL |
1680 | } |
1681 | /* end of class */ | |
1682 | def = mputstr(def, "};\n\n"); | |
1683 | ||
1684 | output->header.class_decls = mputprintf(output->header.class_decls, | |
1685 | "class %s;\n", name); | |
1686 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
1687 | Free(def); | |
1688 | output->source.methods = mputstr(output->source.methods, src); | |
1689 | Free(src); | |
1690 | /* Copied from record.c. */ | |
1691 | output->header.function_prototypes = | |
1692 | mputprintf(output->header.function_prototypes, | |
1693 | "extern boolean operator==(null_type null_value, const %s& " | |
1694 | "other_value);\n", name); | |
1695 | output->source.function_bodies = | |
1696 | mputprintf(output->source.function_bodies, | |
1697 | "boolean operator==(null_type, const %s& other_value)\n" | |
1698 | "{\n" | |
1699 | "if (other_value.val_ptr == NULL)\n" | |
1700 | "TTCN_error(\"The right operand of comparison is an unbound value of " | |
1701 | "type %s.\");\n" | |
af710487 | 1702 | "return other_value.val_ptr->n_elements == 0;\n" |
970ed795 EL |
1703 | "}\n\n", name, dispname); |
1704 | ||
1705 | output->header.function_prototypes = | |
1706 | mputprintf(output->header.function_prototypes, | |
1707 | "inline boolean operator!=(null_type null_value, const %s& " | |
1708 | "other_value) " | |
1709 | "{ return !(null_value == other_value); }\n", name); | |
1710 | } | |
1711 | ||
1712 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
1713 | ||
1714 | void defRecordOfClassMemAllocOptimized(const struct_of_def *sdef, output_struct *output) | |
1715 | { | |
1716 | char *def = NULL, *src = NULL; | |
1717 | const char *name = sdef->name, *dispname = sdef->dispname; | |
1718 | const char *type = sdef->type; | |
a38c6d4c | 1719 | boolean ber_needed = force_gen_seof || (sdef->isASN1 && enable_ber()); |
1720 | boolean raw_needed = force_gen_seof || (sdef->hasRaw && enable_raw()); | |
1721 | boolean text_needed = force_gen_seof || (sdef->hasText && enable_text()); | |
1722 | boolean xer_needed = force_gen_seof || (sdef->hasXer && enable_xer()); | |
1723 | boolean json_needed = force_gen_seof || (sdef->hasJson && enable_json()); | |
970ed795 EL |
1724 | |
1725 | /* Class definition and private data members */ | |
1726 | def = mputprintf(def, | |
1727 | #ifndef NDEBUG | |
1728 | "// written by %s in " __FILE__ " at %d\n" | |
1729 | #endif | |
1730 | "class %s : public Base_Type {\n" | |
1731 | "int n_elements;\n" | |
1732 | "%s* value_elements;\n" | |
1733 | #ifndef NDEBUG | |
1734 | , __FUNCTION__, __LINE__ | |
1735 | #endif | |
1736 | , name, type); | |
1737 | ||
1738 | /* private member functions */ | |
1739 | def = mputprintf(def, | |
1740 | "private:\n" | |
1741 | "friend boolean operator==(null_type null_value, " | |
1742 | "const %s& other_value);\n", name); | |
1743 | ||
1744 | def = mputprintf(def, | |
1745 | "void copy_value(const %s& other_value);\n", name); | |
1746 | src = mputprintf(src, | |
1747 | "void %s::copy_value(const %s& other_value)\n" | |
1748 | "{\n" | |
1749 | "if (other_value.n_elements==-1) {\n" | |
1750 | "TTCN_error(\"Copying an unbound value of type %s.\");\n" | |
1751 | "} else if (other_value.n_elements==0) {\n" | |
1752 | "n_elements = 0;\n" | |
1753 | "value_elements = NULL;\n" | |
1754 | "} else {\n" | |
1755 | "n_elements = other_value.n_elements;\n" | |
1756 | "value_elements = new %s[n_elements];\n" | |
1757 | "for (int act_elem=0; act_elem<n_elements; act_elem++) {\n" | |
1758 | " if (other_value.value_elements[act_elem].is_bound()) {\n" | |
1759 | " value_elements[act_elem] = other_value.value_elements[act_elem];\n" | |
1760 | " }\n" | |
1761 | "}\n" | |
1762 | "}\n" | |
1763 | "}\n\n", | |
1764 | name, name, dispname, type); | |
1765 | ||
1766 | if (sdef->kind == SET_OF) { | |
1767 | /* callback function for comparison */ | |
1768 | def = mputstr(def, "static boolean compare_function(" | |
1769 | "const Base_Type *left_ptr, int left_index, " | |
1770 | "const Base_Type *right_ptr, int right_index);\n"); | |
1771 | src = mputprintf(src, "boolean %s::compare_function(" | |
1772 | "const Base_Type *left_ptr, int left_index, " | |
1773 | "const Base_Type *right_ptr, int right_index)\n" | |
1774 | "{\n" | |
1775 | "if (((const %s*)left_ptr)->n_elements==-1) " | |
1776 | "TTCN_error(\"The left operand of comparison is an unbound value of " | |
1777 | "type %s.\");\n" | |
1778 | "if (((const %s*)right_ptr)->n_elements==-1) " | |
1779 | "TTCN_error(\"The right operand of comparison is an unbound value of " | |
1780 | "type %s.\");\n" | |
1781 | "if (((const %s*)left_ptr)->value_elements[left_index].is_bound()){\n" | |
1782 | "if (((const %s*)right_ptr)->value_elements[right_index].is_bound()){\n" | |
1783 | "return ((const %s*)left_ptr)->value_elements[left_index] == " | |
1784 | "((const %s*)right_ptr)->value_elements[right_index];\n" | |
1785 | "} else return FALSE;\n" | |
1786 | "} else {\n" | |
1787 | "return !((const %s*)right_ptr)->value_elements[right_index].is_bound();\n" | |
1788 | "}\n" | |
1789 | "}\n\n", name, name, dispname, name, dispname, name, name, name, name, name); | |
1790 | } | |
1791 | ||
1792 | /* public member functions */ | |
1793 | def = mputstr(def, "\npublic:\n"); | |
1794 | def = mputprintf(def, " typedef %s of_type;\n", sdef->type); | |
1795 | ||
1796 | /* constructors */ | |
1797 | def = mputprintf(def, "%s(): n_elements(-1), value_elements(NULL) {}\n", name); | |
1798 | ||
a38c6d4c | 1799 | def = mputprintf(def, "%s(null_type): n_elements(0), value_elements(NULL) {}\n", name); |
970ed795 EL |
1800 | |
1801 | /* copy constructor */ | |
1802 | def = mputprintf(def, "%s(const %s& other_value) { copy_value(other_value); }\n", name, name); | |
1803 | ||
1804 | /* destructor */ | |
1805 | def = mputprintf(def, "~%s() { clean_up(); }\n\n", name); | |
1806 | ||
1807 | /* clean_up function */ | |
1808 | def = mputstr(def, "void clean_up();\n"); | |
1809 | src = mputprintf(src, | |
1810 | "void %s::clean_up()\n" | |
1811 | "{\n" | |
1812 | "if (n_elements!=-1) {\n" | |
1813 | "delete[] value_elements;\n" | |
1814 | "n_elements = -1;\n" | |
1815 | "value_elements = NULL;\n" | |
1816 | "}\n" | |
1817 | "}\n\n", name); | |
1818 | ||
1819 | /* assignment operators */ | |
1820 | def = mputprintf(def, "%s& operator=(null_type other_value);\n", name); | |
1821 | src = mputprintf(src, | |
1822 | "%s& %s::operator=(null_type)\n" | |
1823 | "{\n" | |
1824 | "clean_up();\n" | |
1825 | "n_elements=0;\n" | |
1826 | "value_elements=NULL;\n" | |
1827 | "return *this;\n" | |
1828 | "}\n\n", name, name); | |
1829 | ||
1830 | def = mputprintf(def, "%s& operator=(const %s& other_value);\n\n", | |
1831 | name, name); | |
1832 | src = mputprintf(src, | |
1833 | "%s& %s::operator=(const %s& other_value)\n" | |
1834 | "{\n" | |
1835 | "if (other_value.n_elements == -1) " | |
1836 | "TTCN_error(\"Assigning an unbound value of type %s.\");\n" | |
1837 | "if (this != &other_value) {\n" | |
1838 | "clean_up();\n" | |
1839 | "copy_value(other_value);\n" | |
1840 | "}\n" | |
1841 | "return *this;\n" | |
1842 | "}\n\n", name, name, name, dispname); | |
1843 | ||
1844 | /* comparison operators */ | |
1845 | def = mputstr(def, "boolean operator==(null_type other_value) const;\n"); | |
1846 | src = mputprintf(src, | |
1847 | "boolean %s::operator==(null_type) const\n" | |
1848 | "{\n" | |
1849 | "if (n_elements==-1)\n" | |
1850 | "TTCN_error(\"The left operand of comparison is an unbound value of " | |
1851 | "type %s.\");\n" | |
1852 | "return n_elements==0;\n" | |
1853 | "}\n\n", name, dispname); | |
1854 | ||
1855 | def = mputprintf(def, "boolean operator==(const %s& other_value) const;\n", | |
1856 | name); | |
1857 | src = mputprintf(src, | |
1858 | "boolean %s::operator==(const %s& other_value) const\n" | |
1859 | "{\n" | |
1860 | "if (n_elements==-1) " | |
1861 | "TTCN_error(\"The left operand of comparison is an unbound value of type " | |
1862 | "%s.\");\n" | |
1863 | "if (other_value.n_elements==-1) " | |
1864 | "TTCN_error(\"The right operand of comparison is an unbound value of type " | |
1865 | "%s.\");\n" | |
1866 | "if (this==&other_value) return TRUE;\n", name, name, | |
1867 | dispname, dispname); | |
1868 | ||
1869 | if (sdef->kind == SET_OF) { | |
1870 | src = mputstr(src, | |
1871 | "return compare_set_of(this, n_elements, &other_value, " | |
1872 | "other_value.n_elements, compare_function);\n"); | |
1873 | } else { | |
1874 | src = mputstr(src, | |
1875 | "if (n_elements!=other_value.n_elements) return FALSE;\n" | |
1876 | "for (int elem_count = 0; elem_count < n_elements; elem_count++){\n" | |
1877 | "if (value_elements[elem_count].is_bound()){\n" | |
1878 | "if (other_value.value_elements[elem_count].is_bound()){\n" | |
1879 | " if (value_elements[elem_count] != " | |
1880 | "other_value.value_elements[elem_count]) return FALSE;\n" | |
1881 | "} else return FALSE;\n" | |
1882 | "} else {\n" | |
1883 | "if (other_value.value_elements[elem_count].is_bound()) " | |
1884 | "return FALSE;\n" | |
1885 | "}\n" | |
1886 | "}\n" | |
1887 | "return TRUE;\n"); | |
1888 | } | |
1889 | src = mputstr(src, "}\n\n"); | |
1890 | ||
1891 | def = mputstr(def, "inline boolean operator!=(null_type other_value) const " | |
1892 | "{ return !(*this == other_value); }\n"); | |
1893 | def = mputprintf(def, "inline boolean operator!=(const %s& other_value) " | |
1894 | "const { return !(*this == other_value); }\n\n", name); | |
1895 | ||
1896 | /* indexing operators */ | |
1897 | /* Non-const operator[] is allowed to extend the record-of */ | |
1898 | def = mputprintf(def, "%s& operator[](int index_value);\n", type); | |
1899 | src = mputprintf(src, | |
1900 | "%s& %s::operator[](int index_value)\n" | |
1901 | "{\n" | |
1902 | "if (index_value < 0) TTCN_error(\"Accessing an element of type %s " | |
1903 | "using a negative index: %%d.\", index_value);\n" | |
1904 | "if (index_value >= n_elements) set_size(index_value + 1);\n" | |
1905 | "return value_elements[index_value];\n" | |
1906 | "}\n\n", type, name, dispname); | |
1907 | ||
1908 | def = mputprintf(def, "%s& operator[](const INTEGER& index_value);\n", | |
1909 | type); | |
1910 | src = mputprintf(src, | |
1911 | "%s& %s::operator[](const INTEGER& index_value)\n" | |
1912 | "{\n" | |
1913 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
1914 | "a value of type %s.\");\n" | |
1915 | "return (*this)[(int)index_value];\n" | |
1916 | "}\n\n", type, name, dispname); | |
1917 | ||
1918 | /* Const operator[] throws an error if over-indexing */ | |
1919 | def = mputprintf(def, "const %s& operator[](int index_value) const;\n", | |
1920 | type); | |
1921 | src = mputprintf(src, | |
1922 | "const %s& %s::operator[](int index_value) const\n" | |
1923 | "{\n" | |
1924 | "if (n_elements==-1) TTCN_error(\"Accessing an element in an unbound " | |
1925 | "value of type %s.\");\n" | |
1926 | "if (index_value<0) TTCN_error(\"Accessing an element of type %s " | |
1927 | "using a negative index: %%d.\", index_value);\n" | |
1928 | "if (index_value>=n_elements) TTCN_error(\"Index overflow in a value " | |
1929 | "of type %s: The index is %%d, but the value has only %%d elements.\"" | |
1930 | ", index_value, n_elements);\n" | |
1931 | "return value_elements[index_value];\n" | |
1932 | "}\n\n", type, name, dispname, dispname, dispname); | |
1933 | ||
1934 | def = mputprintf(def, "const %s& operator[](const INTEGER& index_value) " | |
1935 | "const;\n\n", type); | |
1936 | src = mputprintf(src, | |
1937 | "const %s& %s::operator[](const INTEGER& index_value) const\n" | |
1938 | "{\n" | |
1939 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
1940 | "a value of type %s.\");\n" | |
1941 | "return (*this)[(int)index_value];\n" | |
1942 | "}\n\n", type, name, dispname); | |
1943 | ||
1944 | /* rotation operators */ | |
1945 | def = mputprintf(def, | |
1946 | "%s operator<<=(int rotate_count) const;\n" | |
1947 | "%s operator<<=(const INTEGER& rotate_count) const;\n" | |
1948 | "%s operator>>=(int rotate_count) const;\n" | |
1949 | "%s operator>>=(const INTEGER& rotate_count) const;\n\n", | |
1950 | name, name, name, name); | |
1951 | src = mputprintf(src, | |
1952 | "%s %s::operator<<=(int rotate_count) const\n" | |
1953 | "{\n" | |
1954 | "return *this >>= (-rotate_count);\n" | |
1955 | "}\n\n" | |
1956 | "%s %s::operator<<=(const INTEGER& rotate_count) const\n" | |
1957 | "{\n" | |
1958 | "rotate_count.must_bound(\"" | |
1959 | "Unbound integer operand of rotate left operator.\");\n" | |
1960 | "return *this >>= (int)(-rotate_count);\n" | |
1961 | "}\n\n" | |
1962 | "%s %s::operator>>=(const INTEGER& rotate_count) const\n" | |
1963 | "{\n" | |
1964 | "rotate_count.must_bound(\"" | |
1965 | "Unbound integer operand of rotate right operator.\");\n" | |
1966 | "return *this >>= (int)rotate_count;\n" | |
1967 | "}\n\n" | |
1968 | "%s %s::operator>>=(int rotate_count) const\n" | |
1969 | "{\n" | |
1970 | "if (n_elements==-1) TTCN_error(\"Performing rotation operation on an " | |
1971 | "unbound value of type %s.\");\n" | |
1972 | "if (n_elements==0) return *this;\n" | |
1973 | "int rc;\n" | |
1974 | "if (rotate_count>=0) rc = rotate_count %% n_elements;\n" | |
1975 | "else rc = n_elements - ((-rotate_count) %% n_elements);\n" | |
1976 | "if (rc == 0) return *this;\n" | |
1977 | "%s ret_val;\n" | |
1978 | "ret_val.set_size(n_elements);\n" | |
1979 | "for (int i=0; i<n_elements; i++) {\n" | |
1980 | "if (value_elements[i].is_bound()) " | |
1981 | "ret_val.value_elements[(i+rc)%%n_elements] = value_elements[i];\n" | |
1982 | "}\n" | |
1983 | "return ret_val;\n" | |
1984 | "}\n\n", | |
1985 | name, name, name, name, name, name, name, name, dispname, name); | |
1986 | ||
1987 | /* concatenation */ | |
1988 | def = mputprintf(def, | |
1989 | "%s operator+(const %s& other_value) const;\n\n", name, name); | |
1990 | src = mputprintf(src, | |
1991 | "%s %s::operator+(const %s& other_value) const\n" | |
1992 | "{\n" | |
1993 | "if (n_elements==-1 || other_value.n_elements==-1) " | |
1994 | "TTCN_error(\"Unbound operand of %s concatenation.\");\n" | |
1995 | "if (n_elements==0) return other_value;\n" | |
1996 | "if (other_value.n_elements==0) return *this;\n" | |
1997 | "%s ret_val;\n" | |
1998 | "ret_val.set_size(n_elements+other_value.n_elements);\n" | |
1999 | "for (int i=0; i<n_elements; i++) {\n" | |
2000 | "if (value_elements[i].is_bound()) " | |
2001 | "ret_val.value_elements[i] = value_elements[i];\n" | |
2002 | "}\n" | |
2003 | "for (int i=0; i<other_value.n_elements; i++) {\n" | |
2004 | "if (other_value.value_elements[i].is_bound()) " | |
2005 | "ret_val.value_elements[i+n_elements] = other_value.value_elements[i];\n" | |
2006 | "}\n" | |
2007 | "return ret_val;\n" | |
2008 | "}\n\n", name, name, name, dispname, name); | |
2009 | ||
2010 | /* substr() */ | |
2011 | def = mputprintf(def, | |
2012 | "%s substr(int index, int returncount) const;\n\n", name); | |
2013 | src = mputprintf(src, | |
2014 | "%s %s::substr(int index, int returncount) const\n" | |
2015 | "{\n" | |
2016 | "if (n_elements==-1) TTCN_error(\"The first argument of substr() is an " | |
2017 | "unbound value of type %s.\");\n" | |
2018 | "check_substr_arguments(n_elements, index, returncount, \"%s\",\"element\");\n" | |
2019 | "%s ret_val;\n" | |
2020 | "ret_val.set_size(returncount);\n" | |
2021 | "for (int i=0; i<returncount; i++) {\n" | |
2022 | "if (value_elements[i+index].is_bound()) " | |
2023 | "ret_val.value_elements[i] = value_elements[i+index];\n" | |
2024 | "}\n" | |
2025 | "return ret_val;\n" | |
2026 | "}\n\n", name, name, dispname, dispname, name); | |
2027 | ||
2028 | /* replace() */ | |
2029 | def = mputprintf(def, | |
2030 | "%s replace(int index, int len, const %s& repl) const;\n\n", name, name); | |
2031 | src = mputprintf(src, | |
2032 | "%s %s::replace(int index, int len, const %s& repl) const\n" | |
2033 | "{\n" | |
2034 | "if (n_elements==-1) TTCN_error(\"The first argument of replace() is an " | |
2035 | "unbound value of type %s.\");\n" | |
2036 | "if (repl.n_elements==-1) TTCN_error(\"The fourth argument of replace() " | |
2037 | "is an unbound value of type %s.\");\n" | |
2038 | "check_replace_arguments(n_elements, index, len, \"%s\",\"element\");\n" | |
2039 | "%s ret_val;\n" | |
2040 | "ret_val.set_size(n_elements + repl.n_elements - len);\n" | |
2041 | "for (int i = 0; i < index; i++) {\n" | |
2042 | "if (value_elements[i].is_bound()) " | |
2043 | "ret_val.value_elements[i] = value_elements[i];\n" | |
2044 | "}\n" | |
2045 | "for (int i = 0; i < repl.n_elements; i++) {\n" | |
2046 | "if (repl.value_elements[i].is_bound()) " | |
2047 | "ret_val.value_elements[i+index] = repl.value_elements[i];\n" | |
2048 | "}\n" | |
2049 | "for (int i = 0; i < n_elements - index - len; i++) {\n" | |
2050 | "if (value_elements[index+i+len].is_bound()) " | |
2051 | "ret_val.value_elements[index+i+repl.n_elements] = value_elements[index+i+len];\n" | |
2052 | "}\n" | |
2053 | "return ret_val;\n" | |
2054 | "}\n\n", name, name, name, dispname, dispname, dispname, name); | |
2055 | def = mputprintf(def, | |
2056 | "%s replace(int index, int len, const %s_template& repl) const;\n\n", | |
2057 | name, name); | |
2058 | src = mputprintf(src, | |
2059 | "%s %s::replace(int index, int len, const %s_template& repl) const\n" | |
2060 | "{\n" | |
2061 | "if (!repl.is_value()) TTCN_error(\"The fourth argument of function " | |
2062 | "replace() is a template with non-specific value.\");\n" | |
2063 | "return replace(index, len, repl.valueof());\n" | |
2064 | "}\n\n", name, name, name); | |
2065 | ||
2066 | /* set_size function */ | |
2067 | def = mputstr(def, "void set_size(int new_size);\n"); | |
2068 | src = mputprintf(src, "void %s::set_size(int new_size)\n" | |
2069 | "{\n" | |
2070 | "if (new_size<0) TTCN_error(\"Internal error: Setting a negative size for " | |
2071 | "a value of type %s.\");\n" | |
2072 | "if (new_size==n_elements) return;\n" | |
2073 | "if (new_size==0) {\n" | |
2074 | " clean_up();\n" | |
2075 | " n_elements = 0;\n" | |
2076 | " value_elements = NULL;\n" | |
2077 | " return;\n" | |
2078 | "}\n" | |
2079 | "%s* new_elem_v = new %s[new_size];\n" | |
2080 | "for (int act_elem = 0; act_elem<n_elements; act_elem++) {\n" | |
2081 | " if (act_elem>=new_size) break;\n" | |
2082 | " if (value_elements[act_elem].is_bound()) new_elem_v[act_elem] = value_elements[act_elem];\n" | |
2083 | "}\n" | |
2084 | "clean_up();\n" | |
2085 | "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n" | |
2086 | "if((n_elements/1000)!=(new_size/1000)) " | |
2087 | "TTCN_warning(\"New size of type %s: %%d\",new_size);\n" | |
2088 | "#endif\n" | |
2089 | "n_elements = new_size;\n" | |
2090 | "value_elements = new_elem_v;\n" | |
2091 | "}\n\n", name, dispname, type, type, dispname); | |
2092 | ||
2093 | /* is_bound function */ | |
2094 | def = mputstr(def, | |
2095 | "inline boolean is_bound() const {return n_elements!=-1; }\n"); | |
2096 | ||
2097 | /* is_present function */ | |
2098 | def = mputstr(def, | |
2099 | "inline boolean is_present() const { return is_bound(); }\n"); | |
2100 | ||
2101 | /* is_value function */ | |
2102 | def = mputstr(def, | |
2103 | "boolean is_value() const;\n"); | |
2104 | src = mputprintf(src, | |
2105 | "boolean %s::is_value() const\n" | |
2106 | "{\n" | |
2107 | "if (n_elements==-1) return FALSE;\n" | |
2108 | "for (int i = 0; i < n_elements; ++i) {\n" | |
2109 | " if (!value_elements[i].is_value()) return FALSE;\n" | |
2110 | "}\n" | |
2111 | "return TRUE;\n" | |
2112 | "}\n\n", name); | |
2113 | ||
2114 | /* sizeof operation */ | |
2115 | def = mputstr(def, | |
2116 | "int size_of() const;\n" | |
2117 | "int n_elem() const { return size_of(); }\n"); | |
2118 | src = mputprintf(src, | |
2119 | "int %s::size_of() const\n" | |
2120 | "{\n" | |
2121 | "if (n_elements==-1) TTCN_error(\"Performing sizeof operation on an " | |
2122 | "unbound value of type %s.\");\n" | |
2123 | "return n_elements;\n" | |
2124 | "}\n\n", name, dispname); | |
2125 | ||
2126 | /* lengthof operation */ | |
2127 | def = mputstr(def, "int lengthof() const;\n"); | |
2128 | src = mputprintf(src, | |
2129 | "int %s::lengthof() const\n" | |
2130 | "{\n" | |
2131 | "if (n_elements==-1) TTCN_error(\"Performing lengthof operation on an " | |
2132 | "unbound value of type %s.\");\n" | |
2133 | "for (int my_length=n_elements; my_length>0; my_length--) " | |
2134 | "if (value_elements[my_length-1].is_bound()) return my_length;\n" | |
2135 | "return 0;\n" | |
2136 | "}\n\n", name, dispname); | |
2137 | ||
2138 | /* log function */ | |
2139 | def = mputstr(def, "void log() const;\n"); | |
2140 | src = mputprintf(src, | |
2141 | "void %s::log() const\n" | |
2142 | "{\n" | |
2143 | "if (n_elements==-1) {;\n" | |
2144 | "TTCN_Logger::log_event_unbound();\n" | |
2145 | "return;\n" | |
2146 | "}\n" | |
2147 | "switch (n_elements) {\n" | |
2148 | "case 0:\n" | |
2149 | "TTCN_Logger::log_event_str(\"{ }\");\n" | |
2150 | "break;\n" | |
2151 | "default:\n" | |
2152 | "TTCN_Logger::log_event_str(\"{ \");\n" | |
2153 | "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n" | |
2154 | "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n" | |
2155 | "value_elements[elem_count].log();\n" | |
2156 | "}\n" | |
2157 | "TTCN_Logger::log_event_str(\" }\");\n" | |
2158 | "}\n" | |
2159 | "}\n\n", name); | |
2160 | ||
2161 | /* set_param function */ /* this is an exact copy of the previous one in this source file, if we didn't forget... */ | |
2162 | def = mputstr(def, "void set_param(Module_Param& param);\n"); | |
2163 | src = mputprintf(src, | |
2164 | "void %s::set_param(Module_Param& param)\n" | |
2165 | "{\n" | |
2166 | " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n" | |
2167 | " param.get_id()->next_name()) {\n" | |
2168 | // Haven't reached the end of the module parameter name | |
2169 | // => the name refers to one of the elements, not to the whole record of | |
2170 | " char* param_field = param.get_id()->get_current_name();\n" | |
2171 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
2172 | " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n" | |
2173 | " \" index for %s type `%s'\");\n" | |
2174 | " }\n" | |
2175 | " int param_index = -1;\n" | |
2176 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
2177 | " (*this)[param_index].set_param(param);\n" | |
2178 | " return;\n" | |
2179 | " }\n" | |
2180 | " param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n" | |
3abe9331 | 2181 | " Module_Param_Ptr mp = ¶m;\n" |
2182 | " if (param.get_type() == Module_Param::MP_Reference) {\n" | |
2183 | " mp = param.get_referenced_param();\n" | |
2184 | " }\n" | |
970ed795 EL |
2185 | " switch (param.get_operation_type()) {\n" |
2186 | " case Module_Param::OT_ASSIGN:\n" | |
3abe9331 | 2187 | " if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {\n" |
970ed795 EL |
2188 | " *this = NULL_VALUE;\n" |
2189 | " return;\n" | |
2190 | " }\n" | |
3abe9331 | 2191 | " switch (mp->get_type()) {\n" |
970ed795 | 2192 | " case Module_Param::MP_Value_List:\n" |
3abe9331 | 2193 | " set_size(mp->get_size());\n" |
2194 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" | |
2195 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 EL |
2196 | " if (curr->get_type()!=Module_Param::MP_NotUsed) {\n" |
2197 | " (*this)[i].set_param(*curr);\n" | |
2198 | " }\n" | |
2199 | " }\n" | |
2200 | " break;\n" | |
2201 | " case Module_Param::MP_Indexed_List:\n" | |
3abe9331 | 2202 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" |
2203 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 EL |
2204 | " (*this)[curr->get_id()->get_index()].set_param(*curr);\n" |
2205 | " }\n" | |
2206 | " break;\n" | |
2207 | " default:\n" | |
2208 | " param.type_error(\"%s value\", \"%s\");\n" | |
2209 | " }\n" | |
2210 | " break;\n" | |
2211 | " case Module_Param::OT_CONCAT:\n" | |
3abe9331 | 2212 | " switch (mp->get_type()) {\n" |
970ed795 EL |
2213 | " case Module_Param::MP_Value_List: {\n" |
2214 | " if (!is_bound()) *this = NULL_VALUE;\n" | |
2215 | " int start_idx = lengthof();\n" | |
3abe9331 | 2216 | " for (size_t i=0; i<mp->get_size(); ++i) {\n" |
2217 | " Module_Param* const curr = mp->get_elem(i);\n" | |
970ed795 EL |
2218 | " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n" |
2219 | " (*this)[start_idx+(int)i].set_param(*curr);\n" | |
2220 | " }\n" | |
2221 | " }\n" | |
2222 | " } break;\n" | |
2223 | " case Module_Param::MP_Indexed_List:\n" | |
2224 | " param.error(\"Cannot concatenate an indexed value list\");\n" | |
2225 | " break;\n" | |
2226 | " default:\n" | |
2227 | " param.type_error(\"%s value\", \"%s\");\n" | |
2228 | " }\n" | |
2229 | " break;\n" | |
2230 | " default:\n" | |
2231 | " TTCN_error(\"Internal error: Unknown operation type.\");\n" | |
2232 | " }\n" | |
2233 | "}\n", name, sdef->kind == RECORD_OF ? "record of" : "set of", dispname, | |
2234 | sdef->kind == RECORD_OF ? "record of" : "set of", | |
2235 | sdef->kind == RECORD_OF ? "record of" : "set of", dispname, | |
2236 | sdef->kind == RECORD_OF ? "record of" : "set of", dispname); | |
3abe9331 | 2237 | |
2238 | /* get param function */ | |
2239 | def = mputstr(def, "Module_Param* get_param(Module_Param_Name& param_name) const;\n"); | |
2240 | src = mputprintf | |
2241 | (src, | |
2242 | "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n" | |
2243 | "{\n" | |
2244 | " if (!is_bound()) {\n" | |
2245 | " return new Module_Param_Unbound();\n" | |
2246 | " }\n" | |
2247 | " if (param_name.next_name()) {\n" | |
2248 | // Haven't reached the end of the module parameter name | |
2249 | // => the name refers to one of the elements, not to the whole record of | |
2250 | " char* param_field = param_name.get_current_name();\n" | |
2251 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
2252 | " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n" | |
2253 | " \"expected a valid index for %s type `%s'\");\n" | |
2254 | " }\n" | |
2255 | " int param_index = -1;\n" | |
2256 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
2257 | " return (*this)[param_index].get_param(param_name);\n" | |
2258 | " }\n" | |
2259 | " Vector<Module_Param*> values;\n" | |
2260 | " for (int i = 0; i < n_elements; ++i) {\n" | |
2261 | " values.push_back((*this)[i].get_param(param_name));\n" | |
2262 | " }\n" | |
2263 | " Module_Param_Value_List* mp = new Module_Param_Value_List();\n" | |
2264 | " mp->add_list_with_implicit_ids(&values);\n" | |
2265 | " values.clear();\n" | |
2266 | " return mp;\n" | |
2267 | "}\n\n", name, sdef->kind == RECORD_OF ? "record of" : "set of", dispname); | |
970ed795 EL |
2268 | |
2269 | /* encoding / decoding functions */ | |
2270 | def = mputstr(def, "void encode_text(Text_Buf& text_buf) const;\n"); | |
2271 | src = mputprintf(src, | |
2272 | "void %s::encode_text(Text_Buf& text_buf) const\n" | |
2273 | "{\n" | |
2274 | "if (n_elements==-1) " | |
2275 | "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n" | |
2276 | "text_buf.push_int(n_elements);\n" | |
2277 | "for (int elem_count = 0; elem_count < n_elements; elem_count++)\n" | |
2278 | "value_elements[elem_count].encode_text(text_buf);\n" | |
2279 | "}\n\n", name, dispname); | |
2280 | ||
2281 | def = mputstr(def, "void decode_text(Text_Buf& text_buf);\n"); | |
2282 | src = mputprintf(src, | |
2283 | "void %s::decode_text(Text_Buf& text_buf)\n" | |
2284 | "{\n" | |
2285 | "clean_up();\n" | |
2286 | "n_elements = text_buf.pull_int().get_val();\n" | |
2287 | "if (n_elements < 0) TTCN_error(\"Text decoder: Negative size " | |
2288 | "was received for a value of type %s.\");\n" | |
2289 | "if (n_elements==0) {\n" | |
2290 | " value_elements = NULL;\n" | |
2291 | " return;\n" | |
2292 | "}\n" | |
2293 | "value_elements = new %s[n_elements];\n" | |
2294 | "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n" | |
2295 | " value_elements[elem_count].decode_text(text_buf);\n" | |
2296 | "}\n" | |
2297 | "}\n\n", name, dispname, type); | |
2298 | ||
2299 | if(ber_needed || raw_needed || text_needed || xer_needed || json_needed) | |
2300 | def_encdec(name, &def, &src, ber_needed, raw_needed, text_needed, | |
2301 | xer_needed, json_needed, FALSE); | |
2302 | ||
2303 | if (text_needed) { | |
2304 | src=mputprintf(src, | |
2305 | "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td," | |
2306 | " TTCN_Buffer& p_buf) const{\n" | |
2307 | " int encoded_length=0;\n" | |
2308 | " if(p_td.text->begin_encode){\n" | |
2309 | " p_buf.put_cs(*p_td.text->begin_encode);\n" | |
2310 | " encoded_length+=p_td.text->begin_encode->lengthof();\n" | |
2311 | " }\n" | |
2312 | " if(n_elements==-1) {\n" | |
2313 | " TTCN_EncDec_ErrorContext::error\n" | |
2314 | " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n" | |
2315 | " if(p_td.text->end_encode){\n" | |
2316 | " p_buf.put_cs(*p_td.text->end_encode);\n" | |
2317 | " encoded_length+=p_td.text->end_encode->lengthof();\n" | |
2318 | " }\n" | |
2319 | " return encoded_length;\n" | |
2320 | " }\n" | |
2321 | " for(int a=0;a<n_elements;a++){\n" | |
2322 | " if(a!=0 && p_td.text->separator_encode){\n" | |
2323 | " p_buf.put_cs(*p_td.text->separator_encode);\n" | |
2324 | " encoded_length+=p_td.text->separator_encode->lengthof();\n" | |
2325 | " }\n" | |
a38c6d4c | 2326 | " encoded_length+=value_elements[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n" |
970ed795 EL |
2327 | " }\n" |
2328 | " if(p_td.text->end_encode){\n" | |
2329 | " p_buf.put_cs(*p_td.text->end_encode);\n" | |
2330 | " encoded_length+=p_td.text->end_encode->lengthof();\n" | |
2331 | " }\n" | |
2332 | " return encoded_length;\n" | |
2333 | "}\n" | |
a38c6d4c | 2334 | ,name |
970ed795 EL |
2335 | ); |
2336 | src = mputprintf(src, | |
2337 | "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td," | |
2338 | " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err" | |
2339 | ", boolean first_call){\n" | |
2340 | " int decoded_length=0;\n" | |
2341 | " size_t pos=p_buf.get_pos();\n" | |
2342 | " boolean sep_found=FALSE;\n" | |
2343 | " int sep_length=0;\n" | |
2344 | " int ml=0;\n" | |
2345 | " if(p_td.text->begin_decode){\n" | |
2346 | " int tl;\n" | |
2347 | " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n" | |
2348 | " if(no_err)return -1;\n" | |
2349 | " TTCN_EncDec_ErrorContext::error\n" | |
2350 | " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'" | |
2351 | " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)" | |
2352 | ", p_td.name);\n" | |
2353 | " return 0;\n" | |
2354 | " }\n" | |
2355 | " decoded_length+=tl;\n" | |
2356 | " p_buf.increase_pos(tl);\n" | |
2357 | " }\n" | |
2358 | " if(p_td.text->end_decode){\n" | |
2359 | " limit.add_token(p_td.text->end_decode);\n" | |
2360 | " ml++;\n" | |
2361 | " }\n" | |
2362 | " if(p_td.text->separator_decode){\n" | |
2363 | " limit.add_token(p_td.text->separator_decode);\n" | |
2364 | " ml++;\n" | |
2365 | " }\n" | |
2366 | " if(first_call) {\n" | |
2367 | " set_size(0);\n" | |
2368 | " }\n" | |
2369 | " int more=n_elements;\n" | |
2370 | " while(TRUE){\n" | |
2371 | " %s val;\n" | |
2372 | " pos=p_buf.get_pos();\n" | |
a38c6d4c | 2373 | " int len=val.TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n" |
970ed795 EL |
2374 | " if(len==-1 || (len==0 && !limit.has_token())){\n" |
2375 | " p_buf.set_pos(pos);\n" | |
2376 | " if(sep_found){\n" | |
2377 | " p_buf.set_pos(p_buf.get_pos()-sep_length);\n" | |
2378 | " decoded_length-=sep_length;\n" | |
2379 | " }\n" | |
2380 | " break;\n" | |
2381 | " }\n" | |
2382 | " sep_found=FALSE;\n" | |
2383 | " set_size(n_elements+1);\n" | |
2384 | " value_elements[n_elements-1]=val;\n" | |
2385 | " decoded_length+=len;\n" | |
2386 | " if(p_td.text->separator_decode){\n" | |
2387 | " int tl;\n" | |
2388 | " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n" | |
2389 | " break;\n" | |
2390 | " }\n" | |
2391 | " decoded_length+=tl;\n" | |
2392 | " p_buf.increase_pos(tl);\n" | |
2393 | " sep_length=tl;\n" | |
2394 | " sep_found=TRUE;\n" | |
2395 | " } else if(p_td.text->end_decode){\n" | |
2396 | " int tl;\n" | |
2397 | " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n" | |
2398 | " decoded_length+=tl;\n" | |
2399 | " p_buf.increase_pos(tl);\n" | |
2400 | " limit.remove_tokens(ml);\n" | |
2401 | " return decoded_length;\n" | |
2402 | " }\n" | |
2403 | " } else if(limit.has_token(ml)){\n" | |
2404 | " int tl;\n" | |
2405 | " if((tl=limit.match(p_buf,ml))==0){\n" | |
2406 | " sep_found=FALSE;\n" | |
2407 | " break;\n" | |
2408 | " }\n" | |
2409 | " }\n" | |
2410 | " }\n" | |
a38c6d4c | 2411 | ,name,type |
970ed795 EL |
2412 | ); |
2413 | src = mputstr(src, | |
2414 | " limit.remove_tokens(ml);\n" | |
2415 | " if(p_td.text->end_decode){\n" | |
2416 | " int tl;\n" | |
2417 | " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n" | |
2418 | " if(no_err){" | |
2419 | " if(!first_call){\n" | |
2420 | " set_size(more);\n" | |
2421 | " }\n" | |
2422 | " return -1;\n" | |
2423 | " }\n" | |
2424 | " TTCN_EncDec_ErrorContext::error" | |
2425 | "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'" | |
2426 | " not found for '%s': \",(const char*)*(p_td.text->end_decode)" | |
2427 | ",p_td.name);\n" | |
2428 | " return decoded_length;\n" | |
2429 | " }\n" | |
2430 | " decoded_length+=tl;\n" | |
2431 | " p_buf.increase_pos(tl);\n" | |
2432 | " }\n" | |
2433 | " if(n_elements==0){\n" | |
2434 | " if(p_td.text->end_decode || p_td.text->begin_decode) n_elements=0;\n" | |
2435 | " else {\n" | |
2436 | " if(no_err)return -1;\n" | |
2437 | " TTCN_EncDec_ErrorContext::error" | |
2438 | "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n" | |
2439 | " return decoded_length;\n" | |
2440 | " }\n" | |
2441 | " }\n" | |
2442 | " if(!first_call && more==n_elements && " | |
2443 | "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n" | |
2444 | " return decoded_length;\n" | |
2445 | "}\n" | |
2446 | ); | |
2447 | } | |
2448 | ||
2449 | /* BER functions */ | |
2450 | if(ber_needed) { | |
2451 | /* BER_encode_TLV() */ | |
2452 | src=mputprintf | |
2453 | (src, | |
2454 | "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&" | |
2455 | " p_td, unsigned p_coding) const\n" | |
2456 | "{\n" | |
2457 | " BER_chk_descr(p_td);\n" | |
2458 | " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n" | |
2459 | " if(!new_tlv) {\n" | |
2460 | " new_tlv=ASN_BER_TLV_t::construct(NULL);\n" | |
2461 | " TTCN_EncDec_ErrorContext ec;\n" | |
2462 | " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n" | |
2463 | " ec.set_msg(\"Component #%%d: \", elem_i);\n" | |
2464 | " new_tlv->add_TLV(value_elements[elem_i].BER_encode_TLV" | |
a38c6d4c | 2465 | "(*p_td.oftype_descr, p_coding));\n" |
970ed795 EL |
2466 | " }\n" |
2467 | "%s" | |
2468 | " }\n" | |
2469 | " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n" | |
2470 | " return new_tlv;\n" | |
2471 | "}\n" | |
2472 | "\n" | |
2473 | /* BER_decode_TLV() */ | |
2474 | "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td," | |
2475 | " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n" | |
2476 | "{\n" | |
2477 | " BER_chk_descr(p_td);\n" | |
2478 | " ASN_BER_TLV_t stripped_tlv;\n" | |
2479 | " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n" | |
2480 | " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \"," | |
2481 | " p_td.name);\n" | |
2482 | " stripped_tlv.chk_constructed_flag(TRUE);\n" | |
2483 | " set_size(0);\n" | |
2484 | " size_t V_pos=0;\n" | |
2485 | " ASN_BER_TLV_t tmp_tlv;\n" | |
2486 | " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n" | |
2487 | " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n" | |
2488 | " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, " | |
2489 | "tmp_tlv)) {\n" | |
2490 | " set_size(n_elements+1);\n" | |
a38c6d4c | 2491 | " value_elements[n_elements-1].BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, " |
970ed795 EL |
2492 | "L_form);\n" |
2493 | " ec_2.set_msg(\"%%d: \", n_elements);\n" | |
2494 | " }\n" | |
2495 | " return TRUE;\n" | |
2496 | "}\n" | |
2497 | "\n" | |
a38c6d4c | 2498 | , name |
970ed795 | 2499 | , sdef->kind==SET_OF?" new_tlv->sort_tlvs();\n":"" |
a38c6d4c | 2500 | , name |
970ed795 EL |
2501 | ); |
2502 | ||
2503 | if(sdef->has_opentypes) { | |
2504 | /* BER_decode_opentypes() */ | |
2505 | def=mputstr | |
2506 | (def, | |
2507 | "void BER_decode_opentypes(TTCN_Type_list& p_typelist," | |
2508 | " unsigned L_form);\n"); | |
2509 | src=mputprintf | |
2510 | (src, | |
2511 | "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist," | |
2512 | " unsigned L_form)\n" | |
2513 | "{\n" | |
2514 | " p_typelist.push(this);\n" | |
2515 | " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n" | |
2516 | " TTCN_EncDec_ErrorContext ec_1;\n" | |
2517 | " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n" | |
2518 | " ec_1.set_msg(\"%%d: \", elem_i);\n" | |
2519 | " value_elements[elem_i].BER_decode_opentypes(p_typelist," | |
2520 | " L_form);\n" | |
2521 | " }\n" | |
2522 | " p_typelist.pop();\n" | |
2523 | "}\n" | |
2524 | "\n" | |
2525 | , name | |
2526 | ); | |
2527 | } | |
2528 | } | |
2529 | ||
2530 | if(raw_needed){ | |
2531 | src=mputprintf(src, | |
2532 | "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, " | |
2533 | "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field" | |
2534 | ", boolean first_call){\n" | |
2535 | " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n" | |
2536 | " limit-=prepaddlength;\n" | |
2537 | " int decoded_length=0;\n" | |
2538 | " int decoded_field_length=0;\n" | |
2539 | " size_t start_of_field=0;\n" | |
2540 | " if (first_call) set_size(0);\n" | |
2541 | " int start_field=n_elements;\n" | |
2542 | " if(p_td.raw->fieldlength || sel_field!=-1){\n" | |
2543 | " int a=0;\n" | |
2544 | " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n" | |
2545 | " for(a=0;a<sel_field;a++){\n" | |
a38c6d4c | 2546 | " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr," |
970ed795 EL |
2547 | "p_buf,limit,top_bit_ord,TRUE);\n" |
2548 | " if(decoded_field_length < 0) return decoded_field_length;\n" | |
2549 | " decoded_length+=decoded_field_length;\n" | |
2550 | " limit-=decoded_field_length;\n" | |
2551 | " }\n" | |
2552 | " if(a==0) n_elements=0;\n" | |
2553 | " } else {\n" | |
2554 | " int a=start_field;\n" | |
2555 | " if(limit==0){\n" | |
2556 | " if(!first_call) return -1;\n" | |
2557 | " n_elements=0;\n" | |
2558 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
2559 | "+prepaddlength;\n" | |
2560 | " }\n" | |
2561 | " while(limit>0){\n" | |
2562 | " start_of_field=p_buf.get_pos_bit();\n" | |
a38c6d4c | 2563 | " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit," |
970ed795 EL |
2564 | "top_bit_ord,TRUE);\n" |
2565 | " if(decoded_field_length < 0){\n" | |
2566 | /*" delete &(*this)[a];\n"*/ | |
2567 | " n_elements--;\n" | |
2568 | " p_buf.set_pos_bit(start_of_field);\n" | |
2569 | " if(a>start_field){\n" | |
2570 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
2571 | "+prepaddlength;\n" | |
2572 | " } else return -1;\n" | |
2573 | " }\n" | |
2574 | " decoded_length+=decoded_field_length;\n" | |
2575 | " limit-=decoded_field_length;\n" | |
2576 | " a++;\n" | |
a38c6d4c | 2577 | ,name |
970ed795 EL |
2578 | ); |
2579 | if(sdef->raw.extension_bit!=XDEFNO && sdef->raw.extension_bit!=XDEFDEFAULT){ | |
2580 | src=mputprintf(src, | |
2581 | " if (%sp_buf.get_last_bit())\n" | |
2582 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
2583 | "+prepaddlength;\n", sdef->raw.extension_bit == XDEFYES ? "" : "!"); | |
2584 | } | |
2585 | src=mputprintf(src, | |
2586 | " }\n" | |
2587 | " }\n" | |
2588 | " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)" | |
2589 | "+prepaddlength;\n" | |
2590 | "}\n\n" | |
2591 | "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td," | |
2592 | "RAW_enc_tree& myleaf) const{\n" | |
2593 | " int encoded_length=0;\n" | |
2594 | " int encoded_num_of_records=p_td.raw->fieldlength?" | |
2595 | "smaller(n_elements, p_td.raw->fieldlength):n_elements;\n" | |
2596 | " myleaf.isleaf=FALSE;\n" | |
2597 | " myleaf.rec_of=TRUE;\n" | |
2598 | " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n" | |
2599 | " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n" | |
2600 | " for(int a=0;a<encoded_num_of_records;a++){\n" | |
2601 | " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf," | |
a38c6d4c | 2602 | "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n" |
2603 | " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr," | |
970ed795 EL |
2604 | "*myleaf.body.node.nodes[a]);\n" |
2605 | " }\n" | |
2606 | " return myleaf.length=encoded_length;\n}\n\n" | |
a38c6d4c | 2607 | , name |
970ed795 EL |
2608 | ); |
2609 | } | |
2610 | ||
2611 | if (xer_needed) { /* XERSTUFF encoder codegen for record-of, RT1 */ | |
2612 | def = mputstr(def, | |
2613 | "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n"); | |
2614 | ||
2615 | /* Write the body of the XER encoder/decoder functions. The declaration | |
2616 | * is written by def_encdec() in encdec.c */ | |
2617 | src = mputprintf(src, | |
2618 | "boolean %s::can_start(const char *name, const char *uri, " | |
2619 | "XERdescriptor_t const& xd, unsigned int flavor) {\n" | |
2620 | " boolean e_xer = is_exer(flavor);\n" | |
a38c6d4c | 2621 | " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return " |
2622 | "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n" | |
2623 | " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) " | |
970ed795 EL |
2624 | , name |
2625 | ); | |
a38c6d4c | 2626 | if (!force_gen_seof && sdef->nFollowers) { |
970ed795 EL |
2627 | /* If there are optional fields following the record-of, then seeing |
2628 | * {any XML tag that belongs to those fields} where the record-of may be | |
2629 | * means that the record-of is empty. */ | |
2630 | size_t f; | |
2631 | src = mputstr(src, "{\n"); | |
2632 | for (f = 0; f < sdef->nFollowers; ++f) { | |
2633 | src = mputprintf(src, | |
2634 | " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n" | |
2635 | , sdef->followers[f].type | |
2636 | , sdef->followers[f].typegen | |
2637 | ); | |
2638 | } | |
2639 | src = mputstr(src, | |
2640 | " return TRUE;\n" | |
2641 | " }\n"); | |
2642 | } | |
2643 | else src = mputstr(src, "return TRUE;\n"); | |
2644 | ||
2645 | src = mputprintf(src, | |
a38c6d4c | 2646 | " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n" |
970ed795 EL |
2647 | "}\n\n" |
2648 | , sdef->type | |
970ed795 EL |
2649 | ); |
2650 | ||
2651 | src = mputprintf(src, | |
2652 | "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n" | |
2653 | " size_t num_collected;\n" | |
2654 | " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n" | |
2655 | /* The above may throw but then nothing was allocated. */ | |
2656 | " if (n_elements!=-1) try {\n" | |
2657 | " char **new_ns;\n" | |
2658 | " size_t num_new;\n" | |
2659 | " for (int i = 0; i < n_elements; ++i) {\n" | |
2660 | " bool def_ns_1 = false;" | |
a38c6d4c | 2661 | " new_ns = value_elements[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n" |
970ed795 EL |
2662 | " merge_ns(collected_ns, num_collected, new_ns, num_new);\n" |
2663 | " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */ | |
2664 | " }\n" | |
2665 | " }\n" | |
2666 | " catch (...) {\n" | |
2667 | /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g. | |
2668 | * encoding an unbound value. */ | |
2669 | " while (num_collected > 0) Free(collected_ns[--num_collected]);\n" | |
2670 | " Free(collected_ns);\n" | |
2671 | " throw;\n" | |
2672 | " }\n" | |
2673 | " num = num_collected;\n" | |
2674 | " return collected_ns;\n" | |
2675 | "}\n\n" | |
a38c6d4c | 2676 | , name); |
970ed795 EL |
2677 | |
2678 | src=mputprintf(src, | |
af710487 | 2679 | "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, " |
2680 | "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n" | |
970ed795 EL |
2681 | " if (n_elements==-1) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */ |
2682 | " int encoded_length=(int)p_buf.get_len();\n" | |
2683 | " boolean e_xer = is_exer(p_flavor);\n" | |
2684 | " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n" | |
2685 | " || (p_flavor & USE_TYPE_ATTR)));\n" | |
2686 | " boolean indenting = !is_canonical(p_flavor) && own_tag;\n" | |
2687 | "%s" /* Factor out p_indent if not attribute */ | |
2688 | " if (n_elements==0) {\n" /* Empty record of */ | |
2689 | , name | |
a38c6d4c | 2690 | , force_gen_seof ? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
2691 | : (sdef->xerAttribute ? "" : " if (indenting) do_indent(p_buf, p_indent);\n") | |
970ed795 | 2692 | ); |
a38c6d4c | 2693 | if (force_gen_seof || sdef->xerAttribute) { |
2694 | src=mputprintf(src, | |
2695 | " if (e_xer%s) {\n" /* Empty attribute. */ | |
970ed795 EL |
2696 | " begin_attribute(p_td, p_buf);\n" |
2697 | " p_buf.put_c('\\'');\n" | |
a38c6d4c | 2698 | " } else\n" |
2699 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 | 2700 | } |
a38c6d4c | 2701 | if (force_gen_seof || !sdef->xerAttribute) { |
970ed795 EL |
2702 | src = mputstr(src, |
2703 | " if (own_tag)"); | |
2704 | } | |
2705 | src=mputprintf(src, | |
2706 | " {\n" /* Do empty element tag */ | |
2707 | "%s" | |
2708 | " p_buf.put_c('<');\n" | |
2709 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
2710 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n" | |
2711 | /* namespace declarations for the toplevel element */ | |
2712 | " if (e_xer && p_indent==0)\n" | |
2713 | " {\n" | |
2714 | " size_t num_collected = 0;\n" | |
2715 | " char **collected_ns = NULL;\n" | |
2716 | " bool def_ns = false;\n" | |
2717 | " collected_ns = collect_ns(p_td, num_collected, def_ns);\n" | |
2718 | " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n" | |
2719 | " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n" | |
2720 | " Free(collected_ns[cur_coll]);\n" | |
2721 | " }\n" | |
2722 | " Free(collected_ns);\n" | |
2723 | " }\n" | |
2724 | ||
2725 | " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n" | |
2726 | " }\n" | |
2727 | " }\n" | |
2728 | " else {\n" /* Not empty record of. Start tag or attribute */ | |
a38c6d4c | 2729 | , force_gen_seof ? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
2730 | : (sdef->xerAttribute ? "" : " if (indenting) do_indent(p_buf, p_indent);\n") | |
970ed795 EL |
2731 | ); |
2732 | if (sdef->xerAnyAttrElem) { | |
2733 | src = mputstr(src, | |
2734 | " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n" | |
2735 | " static const universal_char sp = { 0,0,0,' ' };\n" | |
2736 | " static const universal_char tb = { 0,0,0,9 };\n" | |
2737 | " size_t buf_len = p_buf.get_len(), shorter = 0;\n" | |
2738 | " const unsigned char * const buf_data = p_buf.get_data();\n" | |
2739 | " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n" | |
2740 | " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n" | |
2741 | " unsigned char saved[4];\n" | |
2742 | " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n" | |
2743 | " p_buf.increase_length(-shorter);\n" | |
2744 | " for (int i = 0; i < n_elements; ++i) {\n" | |
2745 | " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n" | |
2746 | " size_t len = value_elements[i].lengthof();\n" | |
2747 | " for (;;) {\n" | |
2748 | " const UNIVERSAL_CHARSTRING_ELEMENT& ue = value_elements[i][len - 1];\n" | |
2749 | " if (sp == ue || tb == ue) --len;\n" | |
2750 | " else break;\n" | |
2751 | " }\n" | |
2752 | " size_t j, sp_at = 0;\n" | |
2753 | /* Find the "separators" in each string */ | |
2754 | " for (j = 0; j < len; j++) {\n" | |
2755 | " UNIVERSAL_CHARSTRING_ELEMENT ue = value_elements[i][j];\n" | |
2756 | " if (sp_at) {\n" | |
2757 | " if (sp == ue || tb == ue) {}\n" | |
2758 | " else break;\n" | |
2759 | " } else {\n" | |
2760 | " if (sp == ue || tb == ue) sp_at = j;\n" | |
2761 | " }\n" | |
2762 | " } // next j\n" | |
2763 | " size_t buf_start = p_buf.get_len();\n" | |
2764 | /* Now write them */ | |
2765 | " if (sp_at > 0) {\n" | |
2766 | " char * ns = mprintf(\" xmlns:b%d='\", i);\n" | |
2767 | " size_t ns_len = mstrlen(ns);\n" | |
2768 | " p_buf.put_s(ns_len, (const unsigned char*)ns);\n" | |
2769 | ||
2770 | " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(value_elements[i]));\n" | |
af710487 | 2771 | " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n" |
970ed795 EL |
2772 | // Ensure the namespace abides to its restrictions |
2773 | " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n" | |
2774 | " TTCN_Buffer ns_buf;\n" | |
2775 | " before.encode_utf8(ns_buf);\n" | |
2776 | " CHARSTRING cs;\n" | |
2777 | " ns_buf.get_string(cs);\n" | |
2778 | " check_namespace_restrictions(p_td, (const char*)cs);\n" | |
2779 | " }\n" | |
2780 | ||
2781 | " p_buf.put_c('\\'');\n" | |
2782 | " p_buf.put_c(' ');\n" | |
2783 | ||
2784 | /* Keep just the "b%d" part from ns */ | |
2785 | " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n" | |
2786 | " p_buf.put_c(':');\n" | |
2787 | " Free(ns);\n" | |
2788 | " }\n" | |
2789 | " else {\n" | |
2790 | " p_buf.put_c(' ');\n" | |
2791 | " j = 0;\n" | |
2792 | // Make sure the unqualified namespace is allowed | |
2793 | " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n" | |
2794 | " check_namespace_restrictions(p_td, NULL);\n" | |
2795 | " }\n" | |
2796 | " }\n" | |
2797 | ||
2798 | " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(value_elements[i]) + j);\n" | |
af710487 | 2799 | " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n" |
970ed795 EL |
2800 | // Put this attribute in a dummy element and walk through it to check its validity |
2801 | " TTCN_Buffer check_buf;\n" | |
2802 | " check_buf.put_s(2, (unsigned char*)\"<a\");\n" | |
2803 | " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n" | |
2804 | " check_buf.put_s(2, (unsigned char*)\"/>\");" | |
2805 | " XmlReaderWrap checker(check_buf);\n" | |
2806 | " while (1 == checker.Read()) ;\n" | |
2807 | " }\n" | |
2808 | ||
2809 | " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */ | |
2810 | " } else {\n"); | |
2811 | } | |
a38c6d4c | 2812 | if (force_gen_seof || sdef->xerAttribute) { |
2813 | src=mputprintf(src, | |
2814 | " if (e_xer%s) {\n" | |
970ed795 | 2815 | " begin_attribute(p_td, p_buf);\n" |
a38c6d4c | 2816 | " } else\n" |
2817 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 EL |
2818 | } |
2819 | src=mputprintf(src, | |
2820 | " if (own_tag) {\n" | |
2821 | "%s" | |
2822 | " p_buf.put_c('<');\n" | |
2823 | " boolean write_ns = (e_xer && p_indent==0);\n" | |
2824 | " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n" | |
2825 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
2826 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), " | |
2827 | "(const unsigned char*)p_td.names[e_xer]);\n" | |
2828 | ||
2829 | /* namespace declarations for the toplevel element */ | |
2830 | " if (e_xer && p_indent==0)\n" | |
2831 | " {\n" | |
2832 | " size_t num_collected = 0;\n" | |
2833 | " char **collected_ns = NULL;\n" | |
2834 | " bool def_ns = false;\n" | |
2835 | " collected_ns = collect_ns(p_td, num_collected, def_ns);\n" | |
2836 | " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n" | |
2837 | " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n" | |
2838 | " Free(collected_ns[cur_coll]);\n" | |
2839 | " }\n" | |
2840 | " Free(collected_ns);\n" | |
2841 | " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n" | |
2842 | " }\n" | |
a38c6d4c | 2843 | , force_gen_seof ? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n" |
2844 | : (sdef->xerAttribute ? " if (indenting) do_indent(p_buf, p_indent);\n" : "") | |
970ed795 EL |
2845 | ); |
2846 | if (sdef->xmlValueList) { | |
2847 | src=mputstr(src, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */ | |
2848 | } | |
2849 | src=mputstr(src, | |
2850 | " }\n" | |
2851 | " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n" | |
2852 | " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n" | |
2853 | " TTCN_EncDec_ErrorContext ec_1;\n" | |
2854 | ); | |
a38c6d4c | 2855 | src=mputstr(src, |
970ed795 | 2856 | " for (int i = 0; i < n_elements; ++i) {\n" |
a38c6d4c | 2857 | " if (i > 0 && !own_tag && 0 != emb_val &&\n" |
2858 | " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n" | |
2859 | " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n" | |
2860 | " if (0 != emb_val->embval_array_reg) {\n" | |
2861 | " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n" | |
2862 | " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n" | |
2863 | " }\n" | |
2864 | " else {\n" | |
2865 | " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n" | |
2866 | " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n" | |
2867 | " }\n" | |
af710487 | 2868 | " ++emb_val->embval_index;\n" |
a38c6d4c | 2869 | " }\n" |
2870 | " ec_1.set_msg(\"%d: \", i);\n" | |
970ed795 | 2871 | " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n" |
a38c6d4c | 2872 | " value_elements[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n" |
970ed795 | 2873 | " }\n" |
a38c6d4c | 2874 | " if (indenting && !is_exerlist(p_flavor)) {\n" |
970ed795 EL |
2875 | ); |
2876 | if (sdef->xmlValueList) { | |
2877 | src=mputstr(src, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */ | |
2878 | } | |
2879 | src=mputstr(src, | |
2880 | " do_indent(p_buf, p_indent);\n" | |
2881 | " }\n"); | |
a38c6d4c | 2882 | if (force_gen_seof || sdef->xerAttribute) { |
2883 | src=mputprintf(src, | |
2884 | " if (e_xer%s) p_buf.put_c('\\'');\n" | |
2885 | " else\n" | |
2886 | , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : ""); | |
970ed795 EL |
2887 | } |
2888 | src=mputstr(src, | |
2889 | " if (own_tag){\n" | |
2890 | " p_buf.put_c('<');\n" | |
2891 | " p_buf.put_c('/');\n" | |
2892 | " if (e_xer) write_ns_prefix(p_td, p_buf);\n" | |
2893 | " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n" | |
2894 | " }\n"); | |
2895 | if (sdef->xerAnyAttrElem) { | |
2896 | src = mputstr(src, " }\n"); | |
2897 | } | |
2898 | src=mputstr(src, | |
2899 | " }\n" /* end if(no elements) */ | |
2900 | " return (int)p_buf.get_len() - encoded_length;\n" | |
2901 | "}\n\n" | |
2902 | ); | |
2903 | ||
2904 | src = mputprintf(src, /* XERSTUFF decoder codegen for record-of */ | |
2905 | #ifndef NDEBUG | |
2906 | "// written by %s in " __FILE__ " at %d\n" | |
2907 | #endif | |
2908 | "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, " | |
feade998 | 2909 | "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n" |
970ed795 EL |
2910 | " boolean e_xer = is_exer(p_flavor);\n" |
2911 | " int xerbits = p_td.xer_bits;\n" | |
2912 | " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n" | |
2913 | " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))" | |
2914 | " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */ | |
2915 | /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */ | |
2916 | " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n" | |
2917 | " int rd_ok=1, xml_depth=-1;\n" | |
2918 | " *this = NULL_VALUE;\n" /* empty but initialized array */ | |
2919 | " int type = 0;\n" /* none */ | |
2920 | " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n" | |
2921 | " type = p_reader.NodeType();\n" | |
2922 | " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n" | |
2923 | " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n" | |
2924 | " || XML_READER_TYPE_ATTRIBUTE == type) {\n" | |
2925 | " verify_name(p_reader, p_td, e_xer);\n" | |
2926 | " break;" | |
2927 | " }\n" | |
2928 | " }\n" | |
2929 | " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" | |
2930 | " if (XML_READER_TYPE_TEXT == type) break;\n" | |
2931 | " }\n" | |
2932 | " else {\n" | |
2933 | " if (XML_READER_TYPE_ELEMENT == type) {\n" | |
2934 | " verify_name(p_reader, p_td, e_xer);\n" | |
2935 | " xml_depth = p_reader.Depth();\n" | |
2936 | " break;\n" | |
2937 | " }\n" | |
2938 | " }\n" /* endif(e_xer && list) */ | |
2939 | " }\n" /* next read */ | |
2940 | " else xml_depth = p_reader.Depth();\n" | |
2941 | " p_flavor |= XER_RECOF;\n" | |
2942 | #ifndef NDEBUG | |
2943 | , __FUNCTION__, __LINE__ | |
2944 | #endif | |
2945 | , name | |
2946 | ); | |
2947 | ||
a38c6d4c | 2948 | src = mputstr(src, |
970ed795 EL |
2949 | " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/ |
2950 | " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */ | |
2951 | " size_t x_pos = 0;\n" | |
2952 | " size_t x_len = strlen(x_val);\n" | |
2953 | /* The string contains a bunch of values separated by whitespace. | |
2954 | * Tokenize the string and create a new buffer which looks like | |
2955 | * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>), | |
2956 | * then use that to decode the value. */ | |
2957 | " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n" | |
2958 | // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell | |
2959 | " x_pos += strlen(str) + 1;\n" | |
2960 | " TTCN_Buffer buf_2;\n" | |
2961 | " buf_2.put_c('<');\n" | |
a38c6d4c | 2962 | " write_ns_prefix(*p_td.oftype_descr, buf_2);\n" |
2963 | " const char * const exer_name = p_td.oftype_descr->names[1];\n" | |
2964 | " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n" | |
970ed795 | 2965 | /* If it has a namespace, chop off the '>' from the end */ |
a38c6d4c | 2966 | " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n" |
970ed795 | 2967 | " if (i_can_has_ns) {\n" |
a38c6d4c | 2968 | " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n" |
970ed795 EL |
2969 | " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n" |
2970 | " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n" | |
2971 | " buf_2.put_s(2, (cbyte*)\"='\");\n" | |
2972 | " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n" | |
2973 | " buf_2.put_s(2, (cbyte*)\"'>\");\n" | |
2974 | " }\n" | |
2975 | /* start tag completed */ | |
2976 | " buf_2.put_s(strlen(str), (cbyte*)str);\n" | |
2977 | " buf_2.put_c('<');\n" | |
2978 | " buf_2.put_c('/');\n" | |
a38c6d4c | 2979 | " write_ns_prefix(*p_td.oftype_descr, buf_2);\n" |
2980 | " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n" | |
970ed795 EL |
2981 | " XmlReaderWrap reader_2(buf_2);\n" |
2982 | " rd_ok = reader_2.Read();\n" /* Move to the start element. */ | |
2983 | /* Don't move to the #text, that's the callee's responsibility. */ | |
2984 | /* The call to the non-const operator[] creates a new element object, | |
2985 | * then we call its XER_decode with the temporary XML reader. */ | |
feade998 | 2986 | " (*this)[n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n" |
970ed795 EL |
2987 | " if (p_flavor & EXIT_ON_ERROR && !(*this)[n_elements - 1].is_bound()) {\n" |
2988 | " if (1 == n_elements) {\n" | |
2989 | // Failed to decode even the first element | |
2990 | " clean_up();\n" | |
2991 | " } else {\n" | |
2992 | // Some elements were successfully decoded -> only delete the last one | |
2993 | " set_size(n_elements - 1);\n" | |
2994 | " }\n" | |
2995 | " xmlFree(x_val);\n" | |
2996 | " return -1;\n" | |
2997 | " }\n" | |
2998 | " if (x_pos >= x_len) break;\n" | |
2999 | " }\n" | |
3000 | " xmlFree(x_val);\n" | |
3001 | " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n" | |
3002 | /* Let the caller do AdvanceAttribute() */ | |
3003 | " else if (own_tag) {\n" | |
3004 | " p_reader.Read();\n" /* on closing tag */ | |
3005 | " p_reader.Read();\n" /* past it */ | |
3006 | " }\n" | |
3007 | " }\n" | |
970ed795 EL |
3008 | ); |
3009 | ||
3010 | src = mputprintf(src, | |
3011 | " else {\n" | |
3012 | " if (p_flavor & PARENT_CLOSED) ;\n" | |
3013 | /* Nothing to do, but do not advance past the parent's element */ | |
3014 | " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n" | |
3015 | /* It's our XML empty element: nothing to do, skip past it */ | |
3016 | " else {\n" | |
3017 | /* Note: there is no p_reader.Read() at the end of the loop below. | |
3018 | * Each element is supposed to consume enough to leave the next element | |
3019 | * well-positioned. */ | |
3020 | " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n" | |
3021 | " type = p_reader.NodeType();\n" | |
3022 | " if (XML_READER_TYPE_ELEMENT == type)\n" | |
3023 | " {\n"); | |
3024 | if (sdef->xerAnyAttrElem) { | |
3025 | src = mputprintf(src, | |
3026 | " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n" | |
3027 | /* This is a record-of with ANY-ELEMENT applied, which is really meant | |
3028 | * for the element type (a string), so behave like a record-of | |
3029 | * (string with ANY-ELEMENT): call the non-const operator[] | |
3030 | * to create a new element, then read the entire XML element into it. */ | |
3031 | " (*this)[n_elements] = (const char*)p_reader.ReadOuterXml();\n" | |
3032 | /* Consume the element, then move ahead */ | |
3033 | " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n" | |
3034 | " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n" | |
3035 | " } else"); | |
3036 | } | |
a38c6d4c | 3037 | src = mputstr(src, |
970ed795 EL |
3038 | " {\n" |
3039 | /* An untagged record-of ends if it encounters an element with a name | |
3040 | * that doesn't match its component */ | |
3041 | " if (!own_tag && !can_start((const char*)p_reader.LocalName(), " | |
a38c6d4c | 3042 | "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n" |
970ed795 EL |
3043 | " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n" |
3044 | " break;\n" | |
3045 | " }\n" | |
3046 | /* The call to the non-const operator[] creates the element */ | |
feade998 | 3047 | " operator [](n_elements).XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n" |
3048 | " }\n" | |
3049 | " if (0 != emb_val && !own_tag && n_elements > 1) {\n" | |
3050 | " ++emb_val->embval_index;\n" | |
970ed795 EL |
3051 | " }\n" |
3052 | " }\n" | |
3053 | " else if (XML_READER_TYPE_END_ELEMENT == type) {\n" | |
3054 | " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n" | |
3055 | " if (own_tag) {\n" | |
3056 | " verify_end(p_reader, p_td, xml_depth, e_xer);\n" | |
3057 | " rd_ok = p_reader.Read();\n" /* move forward one last time */ | |
3058 | " }\n" | |
3059 | " break;\n" | |
3060 | " }\n" | |
a38c6d4c | 3061 | " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && n_elements > 0) {\n" |
af710487 | 3062 | " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n" |
a38c6d4c | 3063 | " if (0 != emb_val->embval_array_reg) {\n" |
3064 | " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n" | |
3065 | " }\n" | |
3066 | " else {\n" | |
3067 | " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n" | |
3068 | " }\n" | |
af710487 | 3069 | " rd_ok = p_reader.Read();\n" |
a38c6d4c | 3070 | " }\n" |
970ed795 EL |
3071 | " else {\n" |
3072 | " rd_ok = p_reader.Read();\n" | |
3073 | " }\n" | |
3074 | " }\n" /* next read */ | |
3075 | " }\n" /* if not empty element */ | |
3076 | " }\n" /* if not LIST */ | |
3077 | " return 1;\n" | |
3078 | "}\n\n" | |
970ed795 EL |
3079 | ); |
3080 | } | |
3081 | if (json_needed) { | |
3082 | // JSON encode, RT1, mem. alloc. optimised | |
3083 | src = mputprintf(src, | |
a38c6d4c | 3084 | "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n" |
970ed795 EL |
3085 | "{\n" |
3086 | " if (!is_bound()) {\n" | |
3087 | " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n" | |
3088 | " \"Encoding an unbound value of type %s.\");\n" | |
3089 | " return -1;\n" | |
3090 | " }\n\n" | |
3091 | " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n" | |
feade998 | 3092 | " for (int i = 0; i < n_elements; ++i) {\n" |
3093 | " if (NULL != p_td.json && p_td.json->metainfo_unbound && !value_elements[i].is_bound()) {\n" | |
3094 | // unbound elements are encoded as { "metainfo []" : "unbound" } | |
3095 | " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n" | |
3096 | " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n" | |
3097 | " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n" | |
3098 | " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n" | |
3099 | " }\n" | |
3100 | " else {\n" | |
3101 | " int ret_val = value_elements[i].JSON_encode(*p_td.oftype_descr, p_tok);\n" | |
3102 | " if (0 > ret_val) break;\n" | |
3103 | " enc_len += ret_val;\n" | |
3104 | " }\n" | |
970ed795 EL |
3105 | " }\n" |
3106 | " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n" | |
3107 | " return enc_len;\n" | |
3108 | "}\n\n" | |
a38c6d4c | 3109 | , name, dispname); |
970ed795 EL |
3110 | |
3111 | // JSON decode, RT1, mem. alloc. optimised | |
3112 | src = mputprintf(src, | |
a38c6d4c | 3113 | "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n" |
970ed795 EL |
3114 | "{\n" |
3115 | " json_token_t token = JSON_TOKEN_NONE;\n" | |
3116 | " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n" | |
3117 | " if (JSON_TOKEN_ERROR == token) {\n" | |
3118 | " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n" | |
3119 | " return JSON_ERROR_FATAL;\n" | |
3120 | " }\n" | |
3121 | " else if (JSON_TOKEN_ARRAY_START != token) {\n" | |
3122 | " return JSON_ERROR_INVALID_TOKEN;\n" | |
3123 | " }\n\n" | |
3124 | " set_size(0);\n" | |
feade998 | 3125 | " for (int nof_elements = 0; true; ++nof_elements) {\n" |
970ed795 | 3126 | " size_t buf_pos = p_tok.get_buf_pos();\n" |
feade998 | 3127 | " int ret_val;\n" |
3128 | " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n" | |
3129 | // check for metainfo object | |
3130 | " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n" | |
3131 | " if (JSON_TOKEN_OBJECT_START == token) {\n" | |
3132 | " char* value = NULL;\n" | |
3133 | " size_t value_len = 0;\n" | |
3134 | " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n" | |
3135 | " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n" | |
3136 | " 0 == strncmp(value, \"metainfo []\", 11)) {\n" | |
3137 | " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n" | |
3138 | " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n" | |
3139 | " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n" | |
3140 | " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n" | |
3141 | " if (JSON_TOKEN_OBJECT_END == token) {\n" | |
3142 | " dec_len += ret_val;\n" | |
3143 | " continue;\n" | |
3144 | " }\n" | |
3145 | " }\n" | |
3146 | " }\n" | |
3147 | " }\n" | |
3148 | // metainfo object not found, jump back and let the element type decode it | |
3149 | " p_tok.set_buf_pos(buf_pos);\n" | |
3150 | " }\n" | |
970ed795 | 3151 | " %s val;\n" |
feade998 | 3152 | " ret_val = val.JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n" |
970ed795 EL |
3153 | " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n" |
3154 | " p_tok.set_buf_pos(buf_pos);\n" | |
3155 | " break;\n" | |
3156 | " }\n" | |
3157 | " else if (JSON_ERROR_FATAL == ret_val) {\n" | |
3158 | " if (p_silent) {\n" | |
3159 | " clean_up();\n" | |
3160 | " }\n" | |
3161 | " return JSON_ERROR_FATAL;\n" | |
3162 | " }\n" | |
feade998 | 3163 | " set_size(nof_elements + 1);\n" |
3164 | " value_elements[nof_elements] = val;\n" | |
970ed795 EL |
3165 | " dec_len += ret_val;\n" |
3166 | " }\n\n" | |
3167 | " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n" | |
3168 | " if (JSON_TOKEN_ARRAY_END != token) {\n" | |
3169 | " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n" | |
3170 | " if (p_silent) {\n" | |
3171 | " clean_up();\n" | |
3172 | " }\n" | |
3173 | " return JSON_ERROR_FATAL;\n" | |
3174 | " }\n\n" | |
3175 | " return dec_len;\n" | |
3176 | "}\n\n" | |
a38c6d4c | 3177 | , name, type); |
970ed795 | 3178 | } |
970ed795 EL |
3179 | /* end of class */ |
3180 | def = mputstr(def, "};\n\n"); | |
3181 | ||
3182 | output->header.class_decls = mputprintf(output->header.class_decls, | |
3183 | "class %s;\n", name); | |
3184 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
3185 | Free(def); | |
3186 | output->source.methods = mputstr(output->source.methods, src); | |
3187 | Free(src); | |
3188 | /* Copied from record.c. */ | |
3189 | output->header.function_prototypes = | |
3190 | mputprintf(output->header.function_prototypes, | |
3191 | "extern boolean operator==(null_type null_value, const %s& " | |
3192 | "other_value);\n", name); | |
3193 | output->source.function_bodies = | |
3194 | mputprintf(output->source.function_bodies, | |
3195 | "boolean operator==(null_type, const %s& other_value)\n" | |
3196 | "{\n" | |
3197 | "if (other_value.n_elements==-1)\n" | |
3198 | "TTCN_error(\"The right operand of comparison is an unbound value of " | |
3199 | "type %s.\");\n" | |
3200 | "return other_value.n_elements == 0;\n" | |
3201 | "}\n\n", name, dispname); | |
3202 | ||
3203 | output->header.function_prototypes = | |
3204 | mputprintf(output->header.function_prototypes, | |
3205 | "inline boolean operator!=(null_type null_value, const %s& " | |
3206 | "other_value) " | |
3207 | "{ return !(null_value == other_value); }\n", name); | |
3208 | } | |
3209 | ||
3210 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
3211 | ||
3212 | void defRecordOfClass2(const struct_of_def *sdef, output_struct *output) | |
3213 | { | |
3214 | char *def = NULL, *src = NULL; | |
3215 | const char *name = sdef->name; | |
3216 | const char *type = sdef->type; | |
a38c6d4c | 3217 | boolean raw_needed = force_gen_seof || (sdef->hasRaw && enable_raw()); |
3218 | boolean xer_needed = force_gen_seof || (sdef->hasXer && enable_xer()); | |
970ed795 EL |
3219 | |
3220 | /* Class definition */ | |
3221 | def = mputprintf(def, | |
3222 | "class %s : public Record_Of_Type {\n", name); | |
3223 | ||
3224 | /* constant unbound element */ | |
3225 | def = mputprintf(def, "\nstatic const %s UNBOUND_ELEM;\n", type); | |
3226 | src = mputprintf(src, "\nconst %s %s::UNBOUND_ELEM;\n", type, name); | |
3227 | ||
3228 | /* public member functions */ | |
3229 | def = mputstr(def, "\npublic:\n"); | |
3230 | ||
3231 | /* constructors */ | |
3232 | def = mputprintf(def, "%s(): Record_Of_Type() {}\n", name); | |
3233 | def = mputprintf(def, "%s(null_type other_value): Record_Of_Type(other_value) {}\n", name); | |
3234 | /* copy constructor */ | |
3235 | def = mputprintf(def, "%s(const %s& other_value): Record_Of_Type(other_value) {}\n", name, name); | |
3236 | /* destructor */ | |
3237 | def = mputprintf(def, "~%s() { clean_up(); }\n\n", name); | |
3238 | ||
3239 | /* assignment operators */ | |
3240 | def = mputprintf(def, "inline %s& operator=(null_type other_value) " | |
3241 | "{ set_val(other_value); return *this; }\n", name); | |
3242 | def = mputprintf(def, "inline %s& operator=(const %s& other_value) " | |
3243 | "{ set_value(&other_value); return *this; }\n\n", name, name); | |
3244 | ||
3245 | /* comparison operators */ | |
3246 | def = mputprintf(def, "inline boolean operator==(const %s& other_value) const " | |
3247 | "{ return is_equal(&other_value); }\n", name); | |
3248 | def = mputprintf(def, "boolean operator!=(const %s& other_value) const " | |
3249 | "{ return !is_equal(&other_value); }\n", name); | |
3250 | ||
3251 | /* indexing operators */ | |
3252 | def = mputprintf(def, | |
3253 | "%s& operator[](int index_value);\n" | |
3254 | "%s& operator[](const INTEGER& index_value);\n" | |
3255 | "const %s& operator[](int index_value) const;\n" | |
3256 | "const %s& operator[](const INTEGER& index_value) const;\n", | |
3257 | type, | |
3258 | type, | |
3259 | type, | |
3260 | type); | |
3261 | ||
3262 | src = mputprintf(src, | |
3263 | "%s& %s::operator[](int index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n" | |
3264 | "%s& %s::operator[](const INTEGER& index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n" | |
3265 | "const %s& %s::operator[](int index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n" | |
3266 | "const %s& %s::operator[](const INTEGER& index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n\n", | |
3267 | type, name, type, | |
3268 | type, name, type, | |
3269 | type, name, type, | |
3270 | type, name, type); | |
3271 | ||
3272 | /* rotation operators */ | |
3273 | def = mputprintf(def, | |
3274 | "%s operator<<=(int rotate_count) const;\n" | |
3275 | "%s operator<<=(const INTEGER& rotate_count) const;\n" | |
3276 | "%s operator>>=(int rotate_count) const;\n" | |
3277 | "%s operator>>=(const INTEGER& rotate_count) const;\n\n", | |
3278 | name, name, name, name); | |
3279 | src = mputprintf(src, | |
3280 | "%s %s::operator<<=(int rotate_count) const\n" | |
3281 | "{\n" | |
3282 | "return *this >>= (-rotate_count);\n" | |
3283 | "}\n\n" | |
3284 | "%s %s::operator<<=(const INTEGER& rotate_count) const\n" | |
3285 | "{\n" | |
3286 | "%s rec_of;\n" | |
3287 | "return *((%s*)rotl(rotate_count, &rec_of));\n" | |
3288 | "}\n\n" | |
3289 | "%s %s::operator>>=(const INTEGER& rotate_count) const\n" | |
3290 | "{\n" | |
3291 | "%s rec_of;\n" | |
3292 | "return *((%s*)rotr(rotate_count, &rec_of));\n" | |
3293 | "}\n\n" | |
3294 | "%s %s::operator>>=(int rotate_count) const\n" | |
3295 | "{\n" | |
3296 | "%s rec_of;\n" | |
3297 | "return *((%s*)rotr(rotate_count, &rec_of));\n" | |
3298 | "}\n\n", | |
3299 | name, name, name, name, name, name, name, name, name, name, name, | |
3300 | name, name, name); | |
3301 | ||
3302 | /* concatenation */ | |
3303 | def = mputprintf(def, | |
3304 | "%s operator+(const %s& other_value) const;\n\n", name, name); | |
3305 | src = mputprintf(src, | |
3306 | "%s %s::operator+(const %s& other_value) const\n" | |
3307 | "{\n" | |
3308 | "%s rec_of;\n" | |
3309 | "return *((%s*)concat(&other_value, &rec_of));\n" | |
3310 | "}\n\n", name, name, name, name, name); | |
3311 | ||
3312 | /* substr() */ | |
3313 | def = mputprintf(def, | |
3314 | "%s substr(int index, int returncount) const;\n\n", name); | |
3315 | src = mputprintf(src, | |
3316 | "%s %s::substr(int index, int returncount) const\n" | |
3317 | "{\n" | |
3318 | "%s rec_of;\n" | |
3319 | "substr_(index, returncount, &rec_of);\n" | |
3320 | "return rec_of;\n" | |
3321 | "}\n\n", name, name, name); | |
3322 | ||
3323 | /* replace() */ | |
3324 | def = mputprintf(def, | |
3325 | "%s replace(int index, int len, const %s& repl) const;\n\n", name, name); | |
3326 | src = mputprintf(src, | |
3327 | "%s %s::replace(int index, int len, const %s& repl) const\n" | |
3328 | "{\n" | |
3329 | "%s rec_of;\n" | |
3330 | "replace_(index, len, &repl, &rec_of);\n" | |
3331 | "return rec_of;\n" | |
3332 | "}\n\n", name, name, name, name); | |
3333 | def = mputprintf(def, | |
3334 | "%s replace(int index, int len, const %s_template& repl) const;\n\n", | |
3335 | name, name); | |
3336 | src = mputprintf(src, | |
3337 | "%s %s::replace(int index, int len, const %s_template& repl) const\n" | |
3338 | "{\n" | |
3339 | "%s rec_of;\n" | |
3340 | "replace_(index, len, &repl, &rec_of);\n" | |
3341 | "return rec_of;\n" | |
3342 | "}\n\n", name, name, name, name); | |
3343 | ||
3344 | def = mputprintf(def, | |
3345 | "Base_Type* clone() const { return new %s(*this); }\n" | |
3346 | "const TTCN_Typedescriptor_t* get_descriptor() const;\n" | |
3347 | "const TTCN_Typedescriptor_t* get_elem_descr() const;\n" | |
3348 | "Base_Type* create_elem() const;\n" | |
3349 | "const Base_Type* get_unbound_elem() const;\n" | |
3350 | "boolean is_set() const { return %s; }\n", | |
3351 | name, | |
3352 | (sdef->kind == SET_OF) ? "TRUE" : "FALSE"); | |
3353 | ||
3354 | src = mputprintf(src, | |
3355 | "Base_Type* %s::create_elem() const { return new %s; }\n" | |
3356 | "const Base_Type* %s::get_unbound_elem() const { return &UNBOUND_ELEM; }\n" | |
a38c6d4c | 3357 | "const TTCN_Typedescriptor_t* %s::get_descriptor() const { return &%s_descr_; }\n", |
970ed795 EL |
3358 | name, type, |
3359 | name, | |
a38c6d4c | 3360 | name, name); |
970ed795 EL |
3361 | |
3362 | /* helper functions called by enc/dec members of the ancestor class */ | |
3363 | if (raw_needed) { | |
3364 | def = mputprintf(def, "int rawdec_ebv() const { return %d; }\n", | |
3365 | (int)sdef->raw.extension_bit); | |
3366 | } | |
3367 | if (xer_needed) { | |
3368 | def = mputprintf(def, "boolean isXerAttribute() const { return %s; }\n" | |
3369 | "virtual boolean can_start_v(const char * name, const char *uri, " | |
3370 | "XERdescriptor_t const& xd, unsigned int);\n" | |
3371 | "static boolean can_start (const char * name, const char *uri, " | |
3372 | "XERdescriptor_t const& xd, unsigned int);\n", | |
3373 | sdef->xerAttribute ? "TRUE" : "FALSE"); | |
3374 | src = mputprintf(src, | |
3375 | /* The virtual can_start_v hands off to the static can_start. | |
3376 | * We must make a virtual call in Record_Of_Type::XER_decode because | |
3377 | * we don't know the actual type (derived from Record_Of_Type) */ | |
3378 | "boolean %s::can_start_v(const char *name, const char *uri, " | |
3379 | "XERdescriptor_t const& xd, unsigned int flavor) {\n" | |
3380 | " return can_start(name, uri, xd, flavor);\n" | |
3381 | "}\n\n" | |
3382 | "boolean %s::can_start(const char *name, const char *uri, " | |
3383 | "XERdescriptor_t const& xd, unsigned int flavor) {\n" | |
3384 | " boolean e_xer = is_exer(flavor);\n" | |
a38c6d4c | 3385 | /* if EXER and UNTAGGED, it can begin with the tag of the element, |
3386 | * otherwise it must be the tag of the type itself, | |
3387 | * specified in the supplied parameter. | |
3388 | * If flavor contains UNTAGGED, that's a signal to go directly | |
3389 | * to the embedded type. */ | |
3390 | " if (!e_xer || !((xd.xer_bits|flavor) & UNTAGGED)) return " | |
3391 | "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n" | |
970ed795 EL |
3392 | /* a record-of with ANY-ELEMENT can start with any tag |
3393 | * :-( with some exceptions )-: */ | |
a38c6d4c | 3394 | " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) " |
970ed795 EL |
3395 | , name, name |
3396 | ); | |
3397 | ||
a38c6d4c | 3398 | if (!force_gen_seof && sdef->nFollowers) { |
970ed795 EL |
3399 | size_t f; |
3400 | src = mputstr(src, "{\n"); | |
3401 | for (f = 0; f < sdef->nFollowers; ++f) { | |
3402 | src = mputprintf(src, | |
3403 | " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n" | |
3404 | , sdef->followers[f].type | |
3405 | , sdef->followers[f].typegen | |
3406 | ); | |
3407 | } | |
3408 | src = mputstr(src, | |
3409 | " return TRUE;\n" | |
3410 | " }\n"); | |
3411 | } | |
3412 | else { | |
3413 | src = mputstr(src, "return TRUE;\n"); | |
3414 | } | |
3415 | src = mputprintf(src, | |
a38c6d4c | 3416 | " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n" |
3417 | "}\n\n", sdef->type); | |
970ed795 EL |
3418 | def = mputprintf(def, "boolean isXmlValueList() const { return %s; }\n\n", |
3419 | sdef->xmlValueList ? "TRUE" : "FALSE"); | |
3420 | } | |
3421 | else { | |
3422 | /* The call in XER_decode is still there, can_start_v must exist. */ | |
3423 | def = mputstr(def, | |
3424 | "virtual boolean can_start_v(const char *, const char *, " | |
3425 | "XERdescriptor_t const&, unsigned int) { return FALSE; }\n"); | |
3426 | } | |
3427 | ||
3428 | /* end of class */ | |
3429 | def = mputstr(def, "};\n\n"); | |
3430 | ||
3431 | output->header.class_decls = mputprintf(output->header.class_decls, | |
3432 | "class %s;\n", name); | |
3433 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
3434 | Free(def); | |
3435 | output->source.methods = mputstr(output->source.methods, src); | |
3436 | Free(src); | |
3437 | } | |
3438 | ||
3439 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
3440 | ||
3441 | void defRecordOfTemplate1(const struct_of_def *sdef, output_struct *output) | |
3442 | { | |
3443 | char *def = NULL, *src = NULL; | |
3444 | const char *name = sdef->name, *dispname = sdef->dispname; | |
3445 | const char *type = sdef->type; | |
3446 | const char *base_class = sdef->kind == RECORD_OF ? "Record_Of_Template" : | |
3447 | "Restricted_Length_Template"; | |
3448 | ||
3449 | /* Class definition and private data members */ | |
3450 | def = mputprintf(def, | |
3451 | "class %s_template : public %s {\n" | |
3452 | "union {\n" | |
3453 | "struct {\n" | |
3454 | "int n_elements;\n" | |
3455 | "%s_template **value_elements;\n" | |
3456 | "} single_value;\n" | |
3457 | "struct {\n" | |
3458 | "unsigned int n_values;\n" | |
3459 | "%s_template *list_value;\n" | |
3460 | "} value_list;\n", name, base_class, type, name); | |
3461 | if (sdef->kind == SET_OF) { | |
3462 | def = mputprintf(def, | |
3463 | "struct {\n" | |
3464 | "unsigned int n_items;\n" | |
3465 | "%s_template *set_items;\n" | |
3466 | "} value_set;\n", type); | |
3467 | } | |
3468 | def = mputstr(def, "};\n"); | |
3469 | ||
3470 | /* private member functions */ | |
3471 | ||
3472 | /* copy_value function */ | |
3473 | def = mputprintf(def, "void copy_value(const %s& other_value);\n", name); | |
3474 | src = mputprintf(src, | |
3475 | "void %s_template::copy_value(const %s& other_value)\n" | |
3476 | "{\n" | |
3477 | "if (!other_value.is_bound()) " | |
3478 | "TTCN_error(\"Initialization of a template of type %s with an unbound " | |
3479 | "value.\");\n" | |
3480 | "single_value.n_elements = other_value.size_of();\n" | |
3481 | "single_value.value_elements = " | |
3482 | "(%s_template**)allocate_pointers(single_value.n_elements);\n" | |
3483 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
3484 | "elem_count++) {\n" | |
3485 | "if (other_value[elem_count].is_bound()) {\n" | |
3486 | "single_value.value_elements[elem_count] = " | |
3487 | "new %s_template(other_value[elem_count]);\n" | |
3488 | "} else {\n" | |
3489 | "single_value.value_elements[elem_count] = new %s_template;\n" | |
3490 | "}\n" | |
3491 | "}\n" | |
3492 | "set_selection(SPECIFIC_VALUE);\n" | |
3493 | "}\n\n", name, name, dispname, type, type, type); | |
3494 | ||
3495 | /* copy_template function */ | |
3496 | def = mputprintf(def, "void copy_template(const %s_template& " | |
3497 | "other_value);\n", name); | |
3498 | src = mputprintf(src, | |
3499 | "void %s_template::copy_template(const %s_template& other_value)\n" | |
3500 | "{\n" | |
3501 | "switch (other_value.template_selection) {\n" | |
3502 | "case SPECIFIC_VALUE:\n" | |
3503 | "single_value.n_elements = other_value.single_value.n_elements;\n" | |
3504 | "single_value.value_elements = " | |
3505 | "(%s_template**)allocate_pointers(single_value.n_elements);\n" | |
3506 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
3507 | "elem_count++) {\n" | |
3508 | "if (UNINITIALIZED_TEMPLATE != " | |
3509 | "other_value.single_value.value_elements[elem_count]->get_selection()) {\n" | |
3510 | "single_value.value_elements[elem_count] = new %s_template" | |
3511 | "(*other_value.single_value.value_elements[elem_count]);\n" | |
3512 | "} else {\n" | |
3513 | "single_value.value_elements[elem_count] = new %s_template;\n" | |
3514 | "}\n" | |
3515 | "}\n" | |
3516 | "case OMIT_VALUE:\n" | |
3517 | "case ANY_VALUE:\n" | |
3518 | "case ANY_OR_OMIT:\n" | |
3519 | "break;\n" | |
3520 | "case VALUE_LIST:\n" | |
3521 | "case COMPLEMENTED_LIST:\n" | |
3522 | "value_list.n_values = other_value.value_list.n_values;\n" | |
3523 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
3524 | "for (unsigned int list_count = 0; list_count < value_list.n_values; " | |
3525 | "list_count++)\n" | |
3526 | "value_list.list_value[list_count].copy_template(" | |
3527 | "other_value.value_list.list_value[list_count]);\n" | |
3528 | "break;\n", name, name, type, type, type, name); | |
3529 | if (sdef->kind == SET_OF) { | |
3530 | src = mputprintf(src, | |
3531 | "case SUPERSET_MATCH:\n" | |
3532 | "case SUBSET_MATCH:\n" | |
3533 | "value_set.n_items = other_value.value_set.n_items;\n" | |
3534 | "value_set.set_items = new %s_template[value_set.n_items];\n" | |
3535 | "for (unsigned int set_count = 0; set_count < value_set.n_items; " | |
3536 | "set_count++)\n" | |
3537 | "value_set.set_items[set_count] = " | |
3538 | "other_value.value_set.set_items[set_count];\n" | |
3539 | "break;\n", type); | |
3540 | } | |
3541 | src = mputprintf(src, | |
3542 | "default:\n" | |
3543 | "TTCN_error(\"Copying an uninitialized/unsupported template of type " | |
3544 | "%s.\");\n" | |
3545 | "break;\n" | |
3546 | "}\n" | |
3547 | "set_selection(other_value);\n" | |
3548 | "}\n\n", dispname); | |
3549 | ||
3550 | /* callback function for matching specific values */ | |
3551 | def = mputstr(def, | |
3552 | "static boolean match_function_specific(const Base_Type *value_ptr, " | |
3553 | "int value_index, const Restricted_Length_Template *template_ptr, " | |
3abe9331 | 3554 | "int template_index, boolean legacy);\n"); |
970ed795 EL |
3555 | src = mputprintf(src, |
3556 | "boolean %s_template::match_function_specific(const Base_Type *value_ptr, " | |
3557 | "int value_index, const Restricted_Length_Template *template_ptr, " | |
3abe9331 | 3558 | "int template_index, boolean legacy)\n" |
970ed795 EL |
3559 | "{\n" |
3560 | "if (value_index >= 0) return ((const %s_template*)template_ptr)->" | |
3561 | "single_value.value_elements[template_index]->" | |
3abe9331 | 3562 | "match((*(const %s*)value_ptr)[value_index], legacy);\n" |
970ed795 EL |
3563 | "else return ((const %s_template*)template_ptr)->" |
3564 | "single_value.value_elements[template_index]->is_any_or_omit();\n" | |
3565 | "}\n\n", name, name, name, name); | |
3566 | ||
3567 | if (sdef->kind == SET_OF) { | |
3568 | /* callback function for matching superset and subset */ | |
3569 | def = mputstr(def, | |
3570 | "static boolean match_function_set(const Base_Type *value_ptr, " | |
3571 | "int value_index, const Restricted_Length_Template *template_ptr, " | |
3abe9331 | 3572 | "int template_index, boolean legacy);\n"); |
970ed795 EL |
3573 | src = mputprintf(src, |
3574 | "boolean %s_template::match_function_set(const Base_Type *value_ptr, " | |
3575 | "int value_index, const Restricted_Length_Template *template_ptr, " | |
3abe9331 | 3576 | "int template_index, boolean legacy)\n" |
970ed795 EL |
3577 | "{\n" |
3578 | "if (value_index >= 0) return ((const %s_template*)template_ptr)->" | |
3579 | "value_set.set_items[template_index].match(" | |
3abe9331 | 3580 | "(*(const %s*)value_ptr)[value_index], legacy);\n" |
970ed795 EL |
3581 | "else return ((const %s_template*)template_ptr)->" |
3582 | "value_set.set_items[template_index].is_any_or_omit();\n" | |
3583 | "}\n\n", name, name, name, name); | |
3584 | ||
3585 | /* callback function for log_match_heuristics */ | |
3586 | def = mputstr(def, | |
3587 | "static void log_function(const Base_Type *value_ptr, " | |
3588 | "const Restricted_Length_Template *template_ptr," | |
3abe9331 | 3589 | " int index_value, int index_template, boolean legacy);\n"); |
970ed795 EL |
3590 | src = mputprintf(src, |
3591 | "void %s_template::log_function(const Base_Type *value_ptr, " | |
3592 | "const Restricted_Length_Template *template_ptr," | |
3abe9331 | 3593 | " int index_value, int index_template, boolean legacy)\n" |
970ed795 EL |
3594 | "{\n" |
3595 | "if (value_ptr != NULL && template_ptr != NULL)" | |
3596 | "((const %s_template*)template_ptr)" | |
3597 | "->single_value.value_elements[index_template]" | |
3abe9331 | 3598 | "->log_match((*(const %s*)value_ptr)[index_value], legacy);\n" |
970ed795 EL |
3599 | "else if (value_ptr != NULL) (*(const %s*)value_ptr)[index_value].log();\n" |
3600 | "else if (template_ptr != NULL) ((const %s_template*)template_ptr)" | |
3601 | "->single_value.value_elements[index_template]->log();\n" | |
3602 | "}\n\n", name, name, name, name, name); | |
3603 | } | |
3604 | ||
3605 | /* public member functions */ | |
3606 | def = mputstr(def, "\npublic:\n"); | |
3607 | ||
3608 | /* constructors */ | |
3609 | def = mputprintf(def, "%s_template();\n", name); | |
3610 | src = mputprintf(src, "%s_template::%s_template()\n" | |
3611 | "{\n" | |
3612 | "}\n\n", name, name); | |
3613 | ||
3614 | def = mputprintf(def, "%s_template(template_sel other_value);\n", name); | |
3615 | src = mputprintf(src, "%s_template::%s_template(template_sel other_value)\n" | |
3616 | " : %s(other_value)\n" | |
3617 | "{\n" | |
3618 | "check_single_selection(other_value);\n" | |
3619 | "}\n\n", name, name, base_class); | |
3620 | ||
3621 | def = mputprintf(def, "%s_template(null_type other_value);\n", name); | |
3622 | src = mputprintf(src, "%s_template::%s_template(null_type)\n" | |
3623 | " : %s(SPECIFIC_VALUE)\n" | |
3624 | "{\n" | |
3625 | "single_value.n_elements = 0;\n" | |
3626 | "single_value.value_elements = NULL;\n" | |
3627 | "}\n\n", name, name, base_class); | |
3628 | ||
3629 | def = mputprintf(def, "%s_template(const %s& other_value);\n", name, name); | |
3630 | src = mputprintf(src, "%s_template::%s_template(const %s& other_value)\n" | |
3631 | "{\n" | |
3632 | "copy_value(other_value);\n" | |
3633 | "}\n\n", name, name, name); | |
3634 | ||
3635 | def = mputprintf(def, "%s_template(const OPTIONAL<%s>& other_value);\n", | |
3636 | name, name); | |
3637 | src = mputprintf(src, | |
3638 | "%s_template::%s_template(const OPTIONAL<%s>& other_value)\n" | |
3639 | "{\n" | |
3640 | "switch (other_value.get_selection()) {\n" | |
3641 | "case OPTIONAL_PRESENT:\n" | |
3642 | "copy_value((const %s&)other_value);\n" | |
3643 | "break;\n" | |
3644 | "case OPTIONAL_OMIT:\n" | |
3645 | "set_selection(OMIT_VALUE);\n" | |
3646 | "break;\n" | |
3647 | "default:\n" | |
3648 | "TTCN_error(\"Creating a template of type %s from an unbound optional " | |
3649 | "field.\");\n" | |
3650 | "}\n" | |
3651 | "}\n\n", name, name, name, name, dispname); | |
3652 | ||
3653 | /* copy constructor */ | |
3654 | def = mputprintf(def, "%s_template(const %s_template& other_value);\n", | |
3655 | name, name); | |
3656 | src = mputprintf(src, | |
3657 | "%s_template::%s_template(const %s_template& other_value)\n" | |
3658 | " : %s()\n" | |
3659 | "{\n" | |
3660 | "copy_template(other_value);\n" | |
3661 | "}\n\n", name, name, name, base_class); | |
3662 | ||
3663 | /* destructor */ | |
3664 | def = mputprintf(def, "~%s_template();\n\n", name); | |
3665 | src = mputprintf(src, | |
3666 | "%s_template::~%s_template()\n" | |
3667 | "{\n" | |
3668 | "clean_up();\n" | |
3669 | "}\n\n", name, name); | |
3670 | ||
3671 | /* clean_up function */ | |
3672 | def = mputstr(def, "void clean_up();\n"); | |
3673 | src = mputprintf(src, | |
3674 | "void %s_template::clean_up()\n" | |
3675 | "{\n" | |
3676 | "switch (template_selection) {\n" | |
3677 | "case SPECIFIC_VALUE:\n" | |
3678 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
3679 | "elem_count++)\n" | |
3680 | "delete single_value.value_elements[elem_count];\n" | |
3681 | "free_pointers((void**)single_value.value_elements);\n" | |
3682 | "break;\n" | |
3683 | "case VALUE_LIST:\n" | |
3684 | "case COMPLEMENTED_LIST:\n" | |
3685 | "delete [] value_list.list_value;\n", name); | |
3686 | if (sdef->kind == SET_OF) { | |
3687 | src = mputstr(src, | |
3688 | "break;\n" | |
3689 | "case SUPERSET_MATCH:\n" | |
3690 | "case SUBSET_MATCH:\n" | |
3691 | "delete [] value_set.set_items;\n"); | |
3692 | } | |
3693 | src = mputstr(src, | |
3694 | "default:\n" | |
3695 | "break;\n" | |
3696 | "}\n" | |
3697 | "template_selection = UNINITIALIZED_TEMPLATE;\n" | |
3698 | "}\n\n"); | |
3699 | ||
3700 | /* assignment operators */ | |
3701 | def = mputprintf(def, "%s_template& operator=(template_sel other_value);\n", | |
3702 | name); | |
3703 | src = mputprintf(src, | |
3704 | "%s_template& %s_template::operator=(template_sel other_value)\n" | |
3705 | "{\n" | |
3706 | "check_single_selection(other_value);\n" | |
3707 | "clean_up();\n" | |
3708 | "set_selection(other_value);\n" | |
3709 | "return *this;\n" | |
3710 | "}\n\n", name, name); | |
3711 | ||
3712 | def = mputprintf(def, "%s_template& operator=(null_type other_value);\n", | |
3713 | name); | |
3714 | src = mputprintf(src, | |
3715 | "%s_template& %s_template::operator=(null_type)\n" | |
3716 | "{\n" | |
3717 | "clean_up();\n" | |
3718 | "set_selection(SPECIFIC_VALUE);\n" | |
3719 | "single_value.n_elements = 0;\n" | |
3720 | "single_value.value_elements = NULL;\n" | |
3721 | "return *this;\n" | |
3722 | "}\n\n", name, name); | |
3723 | ||
3724 | def = mputprintf(def, "%s_template& operator=(const %s& other_value);\n", | |
3725 | name, name); | |
3726 | src = mputprintf(src, | |
3727 | "%s_template& %s_template::operator=(const %s& other_value)\n" | |
3728 | "{\n" | |
3729 | "clean_up();\n" | |
3730 | "copy_value(other_value);\n" | |
3731 | "return *this;\n" | |
3732 | "}\n\n", name, name, name); | |
3733 | ||
3734 | def = mputprintf(def, "%s_template& operator=(const OPTIONAL<%s>& " | |
3735 | "other_value);\n", name, name); | |
3736 | src = mputprintf(src, | |
3737 | "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n" | |
3738 | "{\n" | |
3739 | "clean_up();\n" | |
3740 | "switch (other_value.get_selection()) {\n" | |
3741 | "case OPTIONAL_PRESENT:\n" | |
3742 | "copy_value((const %s&)other_value);\n" | |
3743 | "break;\n" | |
3744 | "case OPTIONAL_OMIT:\n" | |
3745 | "set_selection(OMIT_VALUE);\n" | |
3746 | "break;\n" | |
3747 | "default:\n" | |
3748 | "TTCN_error(\"Assignment of an unbound optional field to a template of " | |
3749 | "type %s.\");\n" | |
3750 | "}\n" | |
3751 | "return *this;\n" | |
3752 | "}\n\n", name, name, name, name, dispname); | |
3753 | ||
3754 | def = mputprintf(def, "%s_template& operator=(const %s_template& " | |
3755 | "other_value);\n\n", name, name); | |
3756 | src = mputprintf(src, | |
3757 | "%s_template& %s_template::operator=(const %s_template& other_value)\n" | |
3758 | "{\n" | |
3759 | "if (&other_value != this) {\n" | |
3760 | "clean_up();\n" | |
3761 | "copy_template(other_value);\n" | |
3762 | "}\n" | |
3763 | "return *this;\n" | |
3764 | "}\n\n", name, name, name); | |
3765 | ||
3766 | /* indexing operators */ | |
3767 | /* Non-const operator[] is allowed to extend */ | |
3768 | def = mputprintf(def, "%s_template& operator[](int index_value);\n", type); | |
3769 | src = mputprintf(src, | |
3770 | "%s_template& %s_template::operator[](int index_value)\n" | |
3771 | "{\n" | |
3772 | "if (index_value < 0) TTCN_error(\"Accessing an element of a template " | |
3773 | "for type %s using a negative index: %%d.\", index_value);\n" | |
3774 | "switch (template_selection)\n" | |
3775 | "{\n" | |
3776 | " case SPECIFIC_VALUE:\n" | |
3777 | " if(index_value < single_value.n_elements) break;\n" | |
3778 | " // no break\n" | |
3779 | " case OMIT_VALUE:\n" | |
3780 | " case ANY_VALUE:\n" | |
3781 | " case ANY_OR_OMIT:\n" | |
3782 | " case UNINITIALIZED_TEMPLATE:\n" | |
3783 | " set_size(index_value + 1);\n" | |
3784 | " break;\n" | |
3785 | " default:\n" | |
3786 | " TTCN_error(\"Accessing an " | |
3787 | "element of a non-specific template for type %s.\");\n" | |
3788 | " break;\n" | |
3789 | "}\n" | |
3790 | "return *single_value.value_elements[index_value];\n" | |
3791 | "}\n\n", type, name, dispname, dispname); | |
3792 | ||
3793 | def = mputprintf(def, "%s_template& operator[](const INTEGER& " | |
3794 | "index_value);\n", type); | |
3795 | src = mputprintf(src, | |
3796 | "%s_template& %s_template::operator[](const INTEGER& index_value)\n" | |
3797 | "{\n" | |
3798 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
3799 | "a template of type %s.\");\n" | |
3800 | "return (*this)[(int)index_value];\n" | |
3801 | "}\n\n", type, name, dispname); | |
3802 | ||
3803 | /* Const operator[] throws an error if over-indexing */ | |
3804 | def = mputprintf(def, "const %s_template& operator[](int index_value) " | |
3805 | "const;\n", type); | |
3806 | src = mputprintf(src, | |
3807 | "const %s_template& %s_template::operator[](int index_value) const\n" | |
3808 | "{\n" | |
3809 | "if (index_value < 0) TTCN_error(\"Accessing an element of a template " | |
3810 | "for type %s using a negative index: %%d.\", index_value);\n" | |
3811 | "if (template_selection != SPECIFIC_VALUE) TTCN_error(\"Accessing an " | |
3812 | "element of a non-specific template for type %s.\");\n" | |
3813 | "if (index_value >= single_value.n_elements) " | |
3814 | "TTCN_error(\"Index overflow in a template of type %s: " | |
3815 | "The index is %%d, but the template has only %%d elements.\", " | |
3816 | "index_value, single_value.n_elements);\n" | |
3817 | "return *single_value.value_elements[index_value];\n" | |
3818 | "}\n\n", type, name, dispname, dispname, dispname); | |
3819 | ||
3820 | def = mputprintf(def, "const %s_template& operator[](const INTEGER& " | |
3821 | "index_value) const;\n\n", type); | |
3822 | src = mputprintf(src, | |
3823 | "const %s_template& %s_template::operator[](const INTEGER& index_value) " | |
3824 | "const\n" | |
3825 | "{\n" | |
3826 | "index_value.must_bound(\"Using an unbound integer value for indexing " | |
3827 | "a template of type %s.\");\n" | |
3828 | "return (*this)[(int)index_value];\n" | |
3829 | "}\n\n", type, name, dispname); | |
3830 | ||
3831 | /* set_size function */ | |
3832 | def = mputstr(def, "void set_size(int new_size);\n"); | |
3833 | src = mputprintf(src, "void %s_template::set_size(int new_size)\n" | |
3834 | "{\n" | |
3835 | "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size " | |
3836 | "for a template of type %s.\");\n" | |
3837 | "template_sel old_selection = template_selection;\n" | |
3838 | "if (old_selection != SPECIFIC_VALUE) {\n" | |
3839 | "clean_up();\n" | |
3840 | "set_selection(SPECIFIC_VALUE);\n" | |
3841 | "single_value.n_elements = 0;\n" | |
3842 | "single_value.value_elements = NULL;\n" | |
3843 | "}\n" | |
3844 | "if (new_size > single_value.n_elements) {\n" | |
3845 | "single_value.value_elements = (%s_template**)reallocate_pointers((void**)" | |
3846 | "single_value.value_elements, single_value.n_elements, new_size);\n" | |
3847 | "if (old_selection == ANY_VALUE || old_selection == ANY_OR_OMIT) {\n" | |
3848 | "for (int elem_count = single_value.n_elements; elem_count < new_size; " | |
3849 | "elem_count++)\n" | |
3850 | "single_value.value_elements[elem_count] = new %s_template(ANY_VALUE);\n" | |
3851 | "} else {\n" | |
3852 | "for (int elem_count = single_value.n_elements; elem_count < new_size; " | |
3853 | "elem_count++)\n" | |
3854 | "single_value.value_elements[elem_count] = new %s_template;\n" | |
3855 | "}\n" | |
3856 | "single_value.n_elements = new_size;\n" | |
3857 | "} else if (new_size < single_value.n_elements) {\n" | |
3858 | "for (int elem_count = new_size; elem_count < single_value.n_elements; " | |
3859 | "elem_count++)\n" | |
3860 | "delete single_value.value_elements[elem_count];\n" | |
3861 | "single_value.value_elements = (%s_template**)reallocate_pointers((void**)" | |
3862 | "single_value.value_elements, single_value.n_elements, new_size);\n" | |
3863 | "single_value.n_elements = new_size;\n" | |
3864 | "}\n" | |
3865 | "}\n\n", name, dispname, type, type, type, type); | |
3866 | ||
3867 | /* raw length */ | |
3868 | def = mputstr(def, "int n_elem() const;\n"); | |
3869 | src = mputprintf(src, | |
3870 | "int %s_template::n_elem() const\n" | |
3871 | "{\n" | |
3872 | " switch (template_selection) {\n" | |
3873 | " case SPECIFIC_VALUE:\n" | |
3874 | " return single_value.n_elements;\n" | |
3875 | " break;\n" | |
3876 | " case VALUE_LIST:\n" | |
3877 | " return value_list.n_values;\n" | |
3878 | " break;\n", name); | |
3879 | /* if (sdef->kind == SET_OF) { | |
3880 | src = mputprintf(src, | |
3881 | ); | |
3882 | }*/ | |
3883 | src = mputstr(src, " default:\n" | |
3884 | " TTCN_error(\"Performing n_elem\");\n" | |
3885 | " }\n" | |
3886 | "}\n\n" | |
3887 | ); | |
3888 | ||
3889 | /* sizeof operation */ | |
3890 | def = mputstr(def, | |
3891 | "int size_of(boolean is_size) const;\n" | |
3892 | "inline int size_of() const { return size_of(TRUE); }\n" | |
3893 | "inline int lengthof() const { return size_of(FALSE); }\n" | |
3894 | ); | |
3895 | src = mputprintf(src, | |
3896 | "int %s_template::size_of(boolean is_size) const\n" | |
3897 | "{\n" | |
3898 | "const char* op_name = is_size ? \"size\" : \"length\";\n" | |
3899 | "int min_size;\n" | |
3900 | "boolean has_any_or_none;\n" | |
3901 | "if (is_ifpresent) TTCN_error(\"Performing %%sof() operation on a " | |
3902 | "template of type %s which has an ifpresent attribute.\", op_name);\n" | |
3903 | "switch (template_selection)\n" | |
3904 | "{\n" | |
3905 | "case SPECIFIC_VALUE: {\n" | |
3906 | " min_size = 0;\n" | |
3907 | " has_any_or_none = FALSE;\n" | |
3908 | " int elem_count = single_value.n_elements;\n" | |
3909 | " if (!is_size) { while (elem_count>0 && !single_value.value_elements" | |
3910 | "[elem_count-1]->is_bound()) elem_count--; }\n" | |
3911 | " for (int i=0; i<elem_count; i++) {\n" | |
3912 | " switch (single_value.value_elements[i]->get_selection()) {\n" | |
3913 | " case OMIT_VALUE:\n" | |
3914 | " TTCN_error(\"Performing %%sof() operation on a template of type " | |
3915 | "%s containing omit element.\", op_name);\n" | |
3916 | " case ANY_OR_OMIT:\n" | |
3917 | " has_any_or_none = TRUE;\n" | |
3918 | " break;\n" | |
3919 | " default:\n" | |
3920 | " min_size++;\n" | |
3921 | " break;\n" | |
3922 | " }\n" | |
3923 | " }\n" | |
3924 | "} break;\n", | |
3925 | name, dispname, dispname); | |
3926 | if (sdef->kind == SET_OF) { | |
3927 | src = mputprintf(src, | |
3928 | "case SUPERSET_MATCH:\n" | |
3929 | "case SUBSET_MATCH: {\n" | |
3930 | " min_size = 0;\n" | |
3931 | " has_any_or_none = FALSE;\n" | |
3932 | " int elem_count = value_set.n_items;\n" | |
3933 | " if (!is_size) { while (elem_count>0 && !value_set.set_items" | |
3934 | "[elem_count-1].is_bound()) elem_count--; }\n" | |
3935 | " for (int i=0; i<elem_count; i++) {\n" | |
3936 | " switch (value_set.set_items[i].get_selection())\n" | |
3937 | " {\n" | |
3938 | " case OMIT_VALUE:\n" | |
3939 | " TTCN_error(\"Performing %%sof() operation on a template of type " | |
3940 | "%s containing omit element.\", op_name);\n" | |
3941 | " case ANY_OR_OMIT:\n" | |
3942 | " has_any_or_none = TRUE;\n" | |
3943 | " break;\n" | |
3944 | " default:\n" | |
3945 | " min_size++;\n" | |
3946 | " break;\n" | |
3947 | " }\n" | |
3948 | " }\n" | |
3949 | " if (template_selection==SUPERSET_MATCH) {\n" | |
3950 | " has_any_or_none = TRUE;\n" | |
3951 | " } else {\n" | |
3952 | " int max_size = min_size;\n" | |
3953 | " min_size = 0;\n" | |
3954 | " if (!has_any_or_none) { // [0,max_size]\n" | |
3955 | " switch (length_restriction_type) {\n" | |
3956 | " case NO_LENGTH_RESTRICTION:\n" | |
3957 | " if (max_size==0) return 0;\n" | |
3958 | " TTCN_error(\"Performing %%sof() operation on a template of " | |
3959 | "type %s with no exact size.\", op_name);\n" | |
3960 | " case SINGLE_LENGTH_RESTRICTION:\n" | |
3961 | " if (length_restriction.single_length<=max_size)\n" | |
3962 | " return length_restriction.single_length;\n" | |
3963 | " TTCN_error(\"Performing %%sof() operation on an invalid " | |
3964 | "template of type %s. The maximum size (%%d) contradicts the length " | |
3965 | "restriction (%%d).\", op_name, max_size, " | |
3966 | "length_restriction.single_length);\n" | |
3967 | " case RANGE_LENGTH_RESTRICTION:\n" | |
3968 | " if (max_size==length_restriction.range_length.min_length) {\n" | |
3969 | " return max_size;\n" | |
3970 | " } else if (max_size>length_restriction.range_length.min_length)" | |
3971 | "{\n" | |
3972 | " TTCN_error(\"Performing %%sof() operation on a template of " | |
3973 | "type %s with no exact size.\", op_name);\n" | |
3974 | " } else\n" | |
3975 | " TTCN_error(\"Performing %%sof() operation on an invalid " | |
3976 | "template of type %s. Maximum size (%%d) contradicts the length " | |
3977 | "restriction (%%d..%%d).\", op_name, max_size, " | |
3978 | "length_restriction.range_length.min_length, " | |
3979 | "length_restriction.range_length.max_length);\n" | |
3980 | " default:\n" | |
3981 | " TTCN_error(\"Internal error: Template has invalid length " | |
3982 | "restriction type.\");\n" | |
3983 | " }\n" | |
3984 | " }\n" | |
3985 | " }\n" | |
3986 | "} break;\n", | |
3987 | dispname, dispname, dispname, dispname, dispname); | |
3988 | } /* set of */ | |
3989 | src = mputprintf(src, | |
3990 | "case OMIT_VALUE:\n" | |
3991 | " TTCN_error(\"Performing %%sof() operation on a template of type %s " | |
3992 | "containing omit value.\", op_name);\n" | |
3993 | "case ANY_VALUE:\n" | |
3994 | "case ANY_OR_OMIT:\n" | |
3995 | " min_size = 0;\n" | |
3996 | " has_any_or_none = TRUE;\n" | |
3997 | " break;\n" | |
3998 | "case VALUE_LIST:\n" | |
3999 | "{\n" | |
4000 | " if (value_list.n_values<1)\n" | |
4001 | " TTCN_error(\"Performing %%sof() operation on a " | |
4002 | "template of type %s containing an empty list.\", op_name);\n" | |
4003 | " int item_size = value_list.list_value[0].size_of(is_size);\n" | |
4004 | " for (unsigned int i = 1; i < value_list.n_values; i++) {\n" | |
4005 | " if (value_list.list_value[i].size_of(is_size)!=item_size)\n" | |
4006 | " TTCN_error(\"Performing %%sof() operation on a template of type " | |
4007 | "%s containing a value list with different sizes.\", op_name);\n" | |
4008 | " }\n" | |
4009 | " min_size = item_size;\n" | |
4010 | " has_any_or_none = FALSE;\n" | |
4011 | " break;\n" | |
4012 | "}\n" | |
4013 | "case COMPLEMENTED_LIST:\n" | |
4014 | " TTCN_error(\"Performing %%sof() operation on a template of type %s " | |
4015 | "containing complemented list.\", op_name);\n" | |
4016 | "default:\n" | |
4017 | " TTCN_error(\"Performing %%sof() operation on an " | |
4018 | "uninitialized/unsupported template of type %s.\", op_name);\n" | |
4019 | "}\n" | |
4020 | "return check_section_is_single(min_size, has_any_or_none, " | |
4021 | "op_name, \"a\", \"template of type %s\");\n" | |
4022 | "}\n\n", | |
4023 | dispname, dispname, dispname, dispname, dispname, dispname); | |
4024 | ||
4025 | /* match operation */ | |
3abe9331 | 4026 | def = mputprintf(def, "boolean match(const %s& other_value, boolean legacy " |
4027 | "= FALSE) const;\n", name); | |
970ed795 | 4028 | src = mputprintf(src, |
3abe9331 | 4029 | "boolean %s_template::match(const %s& other_value, boolean legacy) const\n" |
970ed795 EL |
4030 | "{\n" |
4031 | "if (!other_value.is_bound()) return FALSE;\n" | |
4032 | "int value_length = other_value.size_of();\n" | |
4033 | "if (!match_length(value_length)) return FALSE;\n" | |
4034 | "switch (template_selection) {\n" | |
4035 | "case SPECIFIC_VALUE:\n" | |
4036 | "return match_%s_of(&other_value, value_length, this, " | |
3abe9331 | 4037 | "single_value.n_elements, match_function_specific, legacy);\n" |
970ed795 EL |
4038 | "case OMIT_VALUE:\n" |
4039 | "return FALSE;\n" | |
4040 | "case ANY_VALUE:\n" | |
4041 | "case ANY_OR_OMIT:\n" | |
4042 | "return TRUE;\n" | |
4043 | "case VALUE_LIST:\n" | |
4044 | "case COMPLEMENTED_LIST:\n" | |
4045 | "for (unsigned int list_count = 0; list_count < value_list.n_values; " | |
4046 | "list_count++)\n" | |
3abe9331 | 4047 | "if (value_list.list_value[list_count].match(other_value, legacy)) " |
970ed795 EL |
4048 | "return template_selection == VALUE_LIST;\n" |
4049 | "return template_selection == COMPLEMENTED_LIST;\n", | |
4050 | name, name, sdef->kind == RECORD_OF ? "record" : "set"); | |
4051 | if (sdef->kind == SET_OF) { | |
4052 | src = mputstr(src, | |
4053 | "case SUPERSET_MATCH:\n" | |
4054 | "case SUBSET_MATCH:\n" | |
4055 | "return match_set_of(&other_value, value_length, this, " | |
3abe9331 | 4056 | "value_set.n_items, match_function_set, legacy);\n"); |
970ed795 EL |
4057 | } |
4058 | src = mputprintf(src, | |
4059 | "default:\n" | |
4060 | "TTCN_error(\"Matching with an uninitialized/unsupported template " | |
4061 | "of type %s.\");\n" | |
4062 | "}\n" | |
4063 | "return FALSE;\n" | |
4064 | "}\n\n", dispname); | |
4065 | ||
4066 | /* is_bound function */ | |
4067 | def = mputstr(def, | |
4068 | "inline boolean is_bound() const \n" | |
4069 | " {return template_selection != UNINITIALIZED_TEMPLATE; }\n"); | |
4070 | ||
4071 | /* is_value operation */ | |
4072 | def = mputstr(def, "boolean is_value() const;\n"); | |
4073 | src = mputprintf(src, | |
4074 | "boolean %s_template::is_value() const\n" | |
4075 | "{\n" | |
4076 | "if (template_selection != SPECIFIC_VALUE || is_ifpresent) return false;\n" | |
4077 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4078 | "elem_count++)\n" | |
4079 | "if (!single_value.value_elements[elem_count]->is_value()) return false;\n" | |
4080 | "return true;\n" | |
4081 | "}\n\n", name); | |
4082 | ||
4083 | /* valueof operation */ | |
4084 | def = mputprintf(def, "%s valueof() const;\n", name); | |
4085 | src = mputprintf(src, | |
4086 | "%s %s_template::valueof() const\n" | |
4087 | "{\n" | |
4088 | "if (template_selection != SPECIFIC_VALUE || is_ifpresent) TTCN_error(\"" | |
4089 | "Performing a valueof or send operation on a non-specific template of type " | |
4090 | "%s.\");\n" | |
4091 | "%s ret_val;\n" | |
4092 | "ret_val.set_size(single_value.n_elements);\n" | |
4093 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4094 | "elem_count++)\n" | |
4095 | "if (single_value.value_elements[elem_count]->is_bound()) {\n" | |
4096 | "ret_val[elem_count] = single_value.value_elements[elem_count]->valueof();\n" | |
4097 | "}\n" | |
4098 | "return ret_val;\n" | |
4099 | "}\n\n", name, name, dispname, name); | |
4100 | ||
4101 | /* substr() predefined function for templates */ | |
4102 | def = mputprintf(def, | |
4103 | "%s substr(int index, int returncount) const;\n\n", name); | |
4104 | src = mputprintf(src, | |
4105 | "%s %s_template::substr(int index, int returncount) const\n" | |
4106 | "{\n" | |
4107 | "if (!is_value()) TTCN_error(\"The first argument of function substr() is " | |
4108 | "a template with non-specific value.\");\n" | |
4109 | "return valueof().substr(index, returncount);\n" | |
4110 | "}\n\n", name, name); | |
4111 | ||
4112 | /* replace() predefined function for templates */ | |
4113 | def = mputprintf(def, | |
4114 | "%s replace(int index, int len, const %s_template& repl) const;\n\n", name, name); | |
4115 | src = mputprintf(src, | |
4116 | "%s %s_template::replace(int index, int len, const %s_template& repl) const\n" | |
4117 | "{\n" | |
4118 | "if (!is_value()) TTCN_error(\"The first argument of function replace() is " | |
4119 | "a template with non-specific value.\");\n" | |
4120 | "if (!repl.is_value()) TTCN_error(\"The fourth argument of function " | |
4121 | "replace() is a template with non-specific value.\");\n" | |
4122 | "return valueof().replace(index, len, repl.valueof());\n" | |
4123 | "}\n\n", name, name, name); | |
4124 | def = mputprintf(def, | |
4125 | "%s replace(int index, int len, const %s& repl) const;\n\n", name, name); | |
4126 | src = mputprintf(src, | |
4127 | "%s %s_template::replace(int index, int len, const %s& repl) const\n" | |
4128 | "{\n" | |
4129 | "if (!is_value()) TTCN_error(\"The first argument of function replace() is " | |
4130 | "a template with non-specific value.\");\n" | |
4131 | "return valueof().replace(index, len, repl);\n" | |
4132 | "}\n\n", name, name, name); | |
4133 | ||
4134 | /* value list and set handling operators */ | |
4135 | def = mputstr(def, | |
4136 | "void set_type(template_sel template_type, unsigned int list_length);\n"); | |
4137 | src = mputprintf(src, | |
4138 | "void %s_template::set_type(template_sel template_type, " | |
4139 | "unsigned int list_length)\n" | |
4140 | "{\n" | |
4141 | "clean_up();\n" | |
4142 | "switch (template_type) {\n" | |
4143 | "case VALUE_LIST:\n" | |
4144 | "case COMPLEMENTED_LIST:\n" | |
4145 | "value_list.n_values = list_length;\n" | |
4146 | "value_list.list_value = new %s_template[list_length];\n" | |
4147 | "break;\n", name, name); | |
4148 | if (sdef->kind == SET_OF) { | |
4149 | src = mputprintf(src, | |
4150 | "case SUPERSET_MATCH:\n" | |
4151 | "case SUBSET_MATCH:\n" | |
4152 | "value_set.n_items = list_length;\n" | |
4153 | "value_set.set_items = new %s_template[list_length];\n" | |
4154 | "break;\n", type); | |
4155 | } | |
4156 | src = mputprintf(src, | |
4157 | "default:\n" | |
4158 | "TTCN_error(\"Internal error: Setting an invalid type for a template of " | |
4159 | "type %s.\");\n" | |
4160 | "}\n" | |
4161 | "set_selection(template_type);\n" | |
4162 | "}\n\n", dispname); | |
4163 | ||
4164 | def = mputprintf(def, | |
4165 | "%s_template& list_item(unsigned int list_index);\n", name); | |
4166 | src = mputprintf(src, | |
4167 | "%s_template& %s_template::list_item(unsigned int list_index)\n" | |
4168 | "{\n" | |
4169 | "if (template_selection != VALUE_LIST && " | |
4170 | "template_selection != COMPLEMENTED_LIST) " | |
4171 | "TTCN_error(\"Internal error: Accessing a list element of a non-list " | |
4172 | "template of type %s.\");\n" | |
4173 | "if (list_index >= value_list.n_values) " | |
4174 | "TTCN_error(\"Internal error: Index overflow in a value list template " | |
4175 | "of type %s.\");\n" | |
4176 | "return value_list.list_value[list_index];\n" | |
4177 | "}\n\n", name, name, dispname, dispname); | |
4178 | ||
4179 | if (sdef->kind == SET_OF) { | |
4180 | def = mputprintf(def, | |
4181 | "%s_template& set_item(unsigned int set_index);\n", type); | |
4182 | src = mputprintf(src, | |
4183 | "%s_template& %s_template::set_item(unsigned int set_index)\n" | |
4184 | "{\n" | |
4185 | "if (template_selection != SUPERSET_MATCH && " | |
4186 | "template_selection != SUBSET_MATCH) " | |
4187 | "TTCN_error(\"Internal error: Accessing a set element of a non-set " | |
4188 | "template of type %s.\");\n" | |
4189 | "if (set_index >= value_set.n_items) " | |
4190 | "TTCN_error(\"Internal error: Index overflow in a set template of " | |
4191 | "type %s.\");\n" | |
4192 | "return value_set.set_items[set_index];\n" | |
4193 | "}\n\n", type, name, dispname, dispname); | |
4194 | } | |
4195 | ||
4196 | /* logging functions */ | |
4197 | def = mputstr(def, "void log() const;\n"); | |
4198 | src = mputprintf | |
4199 | (src, | |
4200 | "void %s_template::log() const\n" | |
4201 | "{\n" | |
4202 | "switch (template_selection) {\n" | |
4203 | "case SPECIFIC_VALUE:\n" | |
4204 | "if (single_value.n_elements > 0) {\n" | |
4205 | "TTCN_Logger::log_event_str(\"{ \");\n" | |
4206 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4207 | "elem_count++) {\n" | |
4208 | "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n", name); | |
4209 | if (sdef->kind == RECORD_OF) { | |
4210 | src = mputstr(src, | |
4211 | "if (permutation_starts_at(elem_count)) " | |
4212 | "TTCN_Logger::log_event_str(\"permutation(\");\n"); | |
4213 | } | |
4214 | src = mputstr(src, "single_value.value_elements[elem_count]->log();\n"); | |
4215 | if (sdef->kind == RECORD_OF) { | |
4216 | src = mputstr(src, | |
4217 | "if (permutation_ends_at(elem_count)) TTCN_Logger::log_char(')');\n"); | |
4218 | } | |
4219 | src = mputstr(src, | |
4220 | "}\n" | |
4221 | "TTCN_Logger::log_event_str(\" }\");\n" | |
4222 | "} else TTCN_Logger::log_event_str(\"{ }\");\n" | |
4223 | "break;\n" | |
4224 | "case COMPLEMENTED_LIST:\n" | |
4225 | "TTCN_Logger::log_event_str(\"complement\");\n" | |
4226 | "case VALUE_LIST:\n" | |
4227 | "TTCN_Logger::log_char('(');\n" | |
4228 | "for (unsigned int list_count = 0; list_count < value_list.n_values; " | |
4229 | "list_count++) {\n" | |
4230 | "if (list_count > 0) TTCN_Logger::log_event_str(\", \");\n" | |
4231 | "value_list.list_value[list_count].log();\n" | |
4232 | "}\n" | |
4233 | "TTCN_Logger::log_char(')');\n" | |
4234 | "break;\n"); | |
4235 | if (sdef->kind == SET_OF) { | |
4236 | src = mputstr(src, | |
4237 | "case SUPERSET_MATCH:\n" | |
4238 | "case SUBSET_MATCH:\n" | |
4239 | "TTCN_Logger::log_event(\"%s(\", template_selection == SUPERSET_MATCH " | |
4240 | "? \"superset\" : \"subset\");\n" | |
4241 | "for (unsigned int set_count = 0; set_count < value_set.n_items; " | |
4242 | "set_count++) {\n" | |
4243 | "if (set_count > 0) TTCN_Logger::log_event_str(\", \");\n" | |
4244 | "value_set.set_items[set_count].log();\n" | |
4245 | "}\n" | |
4246 | "TTCN_Logger::log_char(')');\n" | |
4247 | "break;\n"); | |
4248 | } | |
4249 | src = mputstr(src, | |
4250 | "default:\n" | |
4251 | "log_generic();\n" | |
4252 | "}\n" | |
4253 | "log_restricted();\n" | |
4254 | "log_ifpresent();\n" | |
4255 | "}\n\n"); | |
4256 | ||
3abe9331 | 4257 | def = mputprintf(def, "void log_match(const %s& match_value, " |
4258 | "boolean legacy = FALSE) const;\n", name); | |
4259 | src = mputprintf(src, "void %s_template::log_match(const %s& match_value, " | |
4260 | "boolean legacy) const\n" | |
970ed795 EL |
4261 | "{\n" |
4262 | "if(TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity()){\n" | |
3abe9331 | 4263 | "if(match(match_value, legacy)){\n" |
970ed795 EL |
4264 | "TTCN_Logger::print_logmatch_buffer();\n" |
4265 | "TTCN_Logger::log_event_str(\" matched\");\n" | |
4266 | "}else{\n", name, name); | |
4267 | ||
4268 | if (sdef->kind == RECORD_OF) { | |
4269 | src = mputstr(src, | |
4270 | "if (template_selection == SPECIFIC_VALUE && " | |
4271 | "single_value.n_elements > 0 && get_number_of_permutations() == 0 && " | |
4272 | "single_value.n_elements == match_value.size_of()) {\n" | |
4273 | "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n" | |
4274 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4275 | "elem_count++) {\n" | |
3abe9331 | 4276 | "if(!single_value.value_elements[elem_count]->match(match_value[elem_count], legacy)){\n" |
970ed795 | 4277 | "TTCN_Logger::log_logmatch_info(\"[%d]\", elem_count);\n" |
3abe9331 | 4278 | "single_value.value_elements[elem_count]->log_match(match_value[elem_count], legacy);\n" |
970ed795 EL |
4279 | "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n" |
4280 | "}\n" | |
4281 | "}\n" | |
4282 | "log_match_length(single_value.n_elements);\n" | |
4283 | "} else {\n" | |
4284 | "TTCN_Logger::print_logmatch_buffer();\n" | |
4285 | "match_value.log();\n" | |
4286 | "TTCN_Logger::log_event_str(\" with \");\n" | |
4287 | "log();\n" | |
4288 | "TTCN_Logger::log_event_str(\" unmatched\");\n" | |
4289 | "}\n"); | |
4290 | } | |
4291 | if (sdef->kind == SET_OF) { | |
4292 | src = mputstr(src, | |
4293 | "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n" | |
4294 | "if (template_selection == SPECIFIC_VALUE)\n" | |
4295 | " log_match_heuristics(&match_value, match_value.size_of(), this, " | |
3abe9331 | 4296 | "single_value.n_elements, match_function_specific, log_function, legacy);\n" |
970ed795 EL |
4297 | "else{\n" |
4298 | "if(previous_size != 0){\n" | |
4299 | "TTCN_Logger::print_logmatch_buffer();\n" | |
4300 | "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n" | |
4301 | "TTCN_Logger::log_event_str(\":=\");\n" | |
4302 | "}\n" | |
4303 | "}\n" | |
4304 | "match_value.log();\n" | |
4305 | "TTCN_Logger::log_event_str(\" with \");\n" | |
4306 | "log();\n" | |
4307 | "TTCN_Logger::log_event_str(\" unmatched\");\n" | |
4308 | ); | |
4309 | } | |
4310 | ||
4311 | src = mputstr(src, "}\n" | |
4312 | "return;\n" | |
4313 | "}\n"); | |
4314 | if (sdef->kind == RECORD_OF) { | |
4315 | /* logging by element is meaningful for 'record of' only */ | |
4316 | src = mputstr(src, | |
4317 | "if (template_selection == SPECIFIC_VALUE && " | |
4318 | "single_value.n_elements > 0 && get_number_of_permutations() == 0 && " | |
4319 | "single_value.n_elements == match_value.size_of()) {\n" | |
4320 | "TTCN_Logger::log_event_str(\"{ \");\n" | |
4321 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4322 | "elem_count++) {\n" | |
4323 | "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n" | |
4324 | "single_value.value_elements[elem_count]->log_match" | |
3abe9331 | 4325 | "(match_value[elem_count], legacy);\n" |
970ed795 EL |
4326 | "}\n" |
4327 | "TTCN_Logger::log_event_str(\" }\");\n" | |
4328 | "log_match_length(single_value.n_elements);\n" | |
4329 | "} else {\n"); | |
4330 | } | |
4331 | src = mputstr(src, | |
4332 | "match_value.log();\n" | |
4333 | "TTCN_Logger::log_event_str(\" with \");\n" | |
4334 | "log();\n" | |
3abe9331 | 4335 | "if (match(match_value, legacy)) TTCN_Logger::log_event_str(\" matched\");\n"); |
970ed795 EL |
4336 | if (sdef->kind == SET_OF) { |
4337 | src = mputstr(src, "else {\n" | |
4338 | "TTCN_Logger::log_event_str(\" unmatched\");\n" | |
4339 | "if (template_selection == SPECIFIC_VALUE) log_match_heuristics(" | |
4340 | "&match_value, match_value.size_of(), this, single_value.n_elements, " | |
3abe9331 | 4341 | "match_function_specific, log_function, legacy);\n" |
970ed795 EL |
4342 | "}\n"); |
4343 | } else { | |
4344 | src = mputstr(src, "else TTCN_Logger::log_event_str(\" unmatched\");\n" | |
4345 | "}\n"); | |
4346 | } | |
4347 | src = mputstr(src, "}\n\n"); | |
4348 | ||
4349 | /* encoding/decoding functions */ | |
4350 | def = mputstr(def, "void encode_text(Text_Buf& text_buf) const;\n"); | |
4351 | src = mputprintf(src, | |
4352 | "void %s_template::encode_text(Text_Buf& text_buf) const\n" | |
4353 | "{\n" | |
4354 | "encode_text_%s(text_buf);\n" | |
4355 | "switch (template_selection) {\n" | |
4356 | "case SPECIFIC_VALUE:\n" | |
4357 | "text_buf.push_int(single_value.n_elements);\n" | |
4358 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4359 | "elem_count++)\n" | |
4360 | "single_value.value_elements[elem_count]->encode_text(text_buf);\n" | |
4361 | "case OMIT_VALUE:\n" | |
4362 | "case ANY_VALUE:\n" | |
4363 | "case ANY_OR_OMIT:\n" | |
4364 | "break;\n" | |
4365 | "case VALUE_LIST:\n" | |
4366 | "case COMPLEMENTED_LIST:\n" | |
4367 | "text_buf.push_int(value_list.n_values);\n" | |
4368 | "for (unsigned int list_count = 0; list_count < value_list.n_values; " | |
4369 | "list_count++)\n" | |
4370 | "value_list.list_value[list_count].encode_text(text_buf);\n" | |
4371 | "break;\n", name, sdef->kind == RECORD_OF ? "permutation" : "restricted"); | |
4372 | if (sdef->kind == SET_OF) { | |
4373 | src = mputstr(src, | |
4374 | "case SUPERSET_MATCH:\n" | |
4375 | "case SUBSET_MATCH:\n" | |
4376 | "text_buf.push_int(value_set.n_items);\n" | |
4377 | "for (unsigned int set_count = 0; set_count < value_set.n_items; " | |
4378 | "set_count++)\n" | |
4379 | "value_set.set_items[set_count].encode_text(text_buf);\n" | |
4380 | "break;\n"); | |
4381 | } | |
4382 | src = mputprintf(src, | |
4383 | "default:\n" | |
4384 | "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported " | |
4385 | "template of type %s.\");\n" | |
4386 | "}\n" | |
4387 | "}\n\n", dispname); | |
4388 | ||
4389 | def = mputstr(def, "void decode_text(Text_Buf& text_buf);\n"); | |
4390 | src = mputprintf(src, | |
4391 | "void %s_template::decode_text(Text_Buf& text_buf)\n" | |
4392 | "{\n" | |
4393 | "clean_up();\n" | |
4394 | "decode_text_%s(text_buf);\n" | |
4395 | "switch (template_selection) {\n" | |
4396 | "case SPECIFIC_VALUE:\n" | |
4397 | "single_value.n_elements = text_buf.pull_int().get_val();\n" | |
4398 | "if (single_value.n_elements < 0) TTCN_error(\"Text decoder: Negative " | |
4399 | "size was received for a template of type %s.\");\n" | |
4400 | "single_value.value_elements = " | |
4401 | "(%s_template**)allocate_pointers(single_value.n_elements);\n" | |
4402 | "for (int elem_count = 0; elem_count < single_value.n_elements; " | |
4403 | "elem_count++) {\n" | |
4404 | "single_value.value_elements[elem_count] = new %s_template;\n" | |
4405 | "single_value.value_elements[elem_count]->decode_text(text_buf);\n" | |
4406 | "}\n" | |
4407 | "case OMIT_VALUE:\n" | |
4408 | "case ANY_VALUE:\n" | |
4409 | "case ANY_OR_OMIT:\n" | |
4410 | "break;\n" | |
4411 | "case VALUE_LIST:\n" | |
4412 | "case COMPLEMENTED_LIST:\n" | |
4413 | "value_list.n_values = text_buf.pull_int().get_val();\n" | |
4414 | "value_list.list_value = new %s_template[value_list.n_values];\n" | |
4415 | "for (unsigned int list_count = 0; list_count < value_list.n_values; " | |
4416 | "list_count++)\n" | |
4417 | "value_list.list_value[list_count].decode_text(text_buf);\n" | |
4418 | "break;\n", name, sdef->kind == RECORD_OF ? "permutation" : "restricted", | |
4419 | dispname, type, type, name); | |
4420 | if (sdef->kind == SET_OF) { | |
4421 | src = mputprintf(src, | |
4422 | "case SUPERSET_MATCH:\n" | |
4423 | "case SUBSET_MATCH:\n" | |
4424 | "value_set.n_items = text_buf.pull_int().get_val();\n" | |
4425 | "value_set.set_items = new %s_template[value_set.n_items];\n" | |
4426 | "for (unsigned int set_count = 0; set_count < value_set.n_items; " | |
4427 | "set_count++)\n" | |
4428 | "value_set.set_items[set_count].decode_text(text_buf);\n" | |
4429 | "break;\n", type); | |
4430 | } | |
4431 | src = mputprintf(src, | |
4432 | "default:\n" | |
4433 | "TTCN_error(\"Text decoder: An unknown/unsupported selection was " | |
4434 | "received for a template of type %s.\");\n" | |
4435 | "}\n" | |
4436 | "}\n\n", dispname); | |
4437 | ||
4438 | /* TTCN-3 ispresent() function */ | |
3abe9331 | 4439 | def = mputstr(def, "boolean is_present(boolean legacy = FALSE) const;\n"); |
970ed795 | 4440 | src = mputprintf(src, |
3abe9331 | 4441 | "boolean %s_template::is_present(boolean legacy) const\n" |
970ed795 EL |
4442 | "{\n" |
4443 | "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n" | |
3abe9331 | 4444 | "return !match_omit(legacy);\n" |
970ed795 EL |
4445 | "}\n\n", name); |
4446 | ||
4447 | /* match_omit() */ | |
3abe9331 | 4448 | def = mputstr(def, "boolean match_omit(boolean legacy = FALSE) const;\n"); |
970ed795 | 4449 | src = mputprintf(src, |
3abe9331 | 4450 | "boolean %s_template::match_omit(boolean legacy) const\n" |
970ed795 EL |
4451 | "{\n" |
4452 | "if (is_ifpresent) return TRUE;\n" | |
4453 | "switch (template_selection) {\n" | |
4454 | "case OMIT_VALUE:\n" | |
4455 | "case ANY_OR_OMIT:\n" | |
4456 | "return TRUE;\n" | |
4457 | "case VALUE_LIST:\n" | |
4458 | "case COMPLEMENTED_LIST:\n" | |
3abe9331 | 4459 | "if (legacy) {\n" |
970ed795 EL |
4460 | "for (unsigned int i=0; i<value_list.n_values; i++)\n" |
4461 | "if (value_list.list_value[i].match_omit())\n" | |
4462 | "return template_selection==VALUE_LIST;\n" | |
4463 | "return template_selection==COMPLEMENTED_LIST;\n" | |
3abe9331 | 4464 | "} // else fall through\n" |
970ed795 EL |
4465 | "default:\n" |
4466 | "return FALSE;\n" | |
4467 | "}\n" | |
4468 | "return FALSE;\n" | |
4469 | "}\n\n", name); | |
4470 | ||
4471 | /* set_param() */ | |
4472 | def = mputstr(def, "void set_param(Module_Param& param);\n"); | |
4473 | src = mputprintf(src, | |
4474 | "void %s_template::set_param(Module_Param& param)\n" | |
4475 | "{\n" | |
4476 | " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n" | |
4477 | " param.get_id()->next_name()) {\n" | |
4478 | // Haven't reached the end of the module parameter name | |
4479 | // => the name refers to one of the elements, not to the whole record of | |
4480 | " char* param_field = param.get_id()->get_current_name();\n" | |
4481 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
4482 | " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n" | |
4483 | " \" index for %s template type `%s'\");\n" | |
4484 | " }\n" | |
4485 | " int param_index = -1;\n" | |
4486 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
4487 | " (*this)[param_index].set_param(param);\n" | |
4488 | " return;\n" | |
4489 | " }\n" | |
4490 | " param.basic_check(Module_Param::BC_TEMPLATE|Module_Param::BC_LIST, \"%s of template\");\n" | |
3abe9331 | 4491 | " Module_Param_Ptr mp = ¶m;\n" |
4492 | " if (param.get_type() == Module_Param::MP_Reference) {\n" | |
4493 | " mp = param.get_referenced_param();\n" | |
4494 | " }\n" | |
4495 | " switch (mp->get_type()) {\n" | |
970ed795 EL |
4496 | " case Module_Param::MP_Omit:\n" |
4497 | " *this = OMIT_VALUE;\n" | |
4498 | " break;\n" | |
4499 | " case Module_Param::MP_Any:\n" | |
4500 | " *this = ANY_VALUE;\n" | |
4501 | " break;\n" | |
4502 | " case Module_Param::MP_AnyOrNone:\n" | |
4503 | " *this = ANY_OR_OMIT;\n" | |
4504 | " break;\n" | |
4505 | " case Module_Param::MP_List_Template:\n" | |
3abe9331 | 4506 | " case Module_Param::MP_ComplementList_Template: {\n" |
4507 | " %s_template temp;\n" | |
4508 | " temp.set_type(mp->get_type()==Module_Param::MP_List_Template ? " | |
4509 | "VALUE_LIST : COMPLEMENTED_LIST, mp->get_size());\n" | |
4510 | " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n" | |
4511 | " temp.list_item(p_i).set_param(*mp->get_elem(p_i));\n" | |
970ed795 | 4512 | " }\n" |
3abe9331 | 4513 | " *this = temp;\n" |
4514 | " break; }\n" | |
970ed795 EL |
4515 | " case Module_Param::MP_Indexed_List:\n" |
4516 | " if (template_selection!=SPECIFIC_VALUE) set_size(0);\n" | |
3abe9331 | 4517 | " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n" |
4518 | " (*this)[(int)(mp->get_elem(p_i)->get_id()->get_index())].set_param(*mp->get_elem(p_i));\n" | |
970ed795 EL |
4519 | " }\n" |
4520 | " break;\n", | |
3abe9331 | 4521 | name, sdef->kind==RECORD_OF?"record":"set", dispname, sdef->kind==RECORD_OF?"record":"set", name); |
970ed795 EL |
4522 | if (sdef->kind == RECORD_OF) { |
4523 | src = mputstr(src, | |
4524 | " case Module_Param::MP_Value_List: {\n" | |
3abe9331 | 4525 | " set_size(mp->get_size());\n" |
970ed795 | 4526 | " int curr_idx = 0;\n" |
3abe9331 | 4527 | " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n" |
4528 | " switch (mp->get_elem(p_i)->get_type()) {\n" | |
970ed795 EL |
4529 | " case Module_Param::MP_NotUsed:\n" |
4530 | " curr_idx++;\n" | |
4531 | " break;\n" | |
4532 | " case Module_Param::MP_Permutation_Template: {\n" | |
4533 | " int perm_start_idx = curr_idx;\n" | |
3abe9331 | 4534 | " for (size_t perm_i=0; perm_i<mp->get_elem(p_i)->get_size(); perm_i++) {\n" |
4535 | " (*this)[curr_idx].set_param(*(mp->get_elem(p_i)->get_elem(perm_i)));\n" | |
970ed795 EL |
4536 | " curr_idx++;\n" |
4537 | " }\n" | |
4538 | " int perm_end_idx = curr_idx - 1;\n" | |
4539 | " add_permutation(perm_start_idx, perm_end_idx);\n" | |
4540 | " } break;\n" | |
4541 | " default:\n" | |
3abe9331 | 4542 | " (*this)[curr_idx].set_param(*mp->get_elem(p_i));\n" |
970ed795 EL |
4543 | " curr_idx++;\n" |
4544 | " }\n" | |
4545 | " }\n" | |
4546 | " } break;\n"); | |
4547 | } else { | |
4548 | src = mputstr(src, | |
4549 | " case Module_Param::MP_Value_List:\n" | |
3abe9331 | 4550 | " set_size(mp->get_size());\n" |
4551 | " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n" | |
4552 | " if (mp->get_elem(p_i)->get_type()!=Module_Param::MP_NotUsed) {\n" | |
4553 | " (*this)[p_i].set_param(*mp->get_elem(p_i));\n" | |
970ed795 EL |
4554 | " }\n" |
4555 | " }\n" | |
4556 | " break;\n" | |
4557 | " case Module_Param::MP_Superset_Template:\n" | |
4558 | " case Module_Param::MP_Subset_Template:\n" | |
3abe9331 | 4559 | " set_type(mp->get_type()==Module_Param::MP_Superset_Template ? SUPERSET_MATCH : SUBSET_MATCH, mp->get_size());\n" |
4560 | " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n" | |
4561 | " set_item(p_i).set_param(*mp->get_elem(p_i));\n" | |
970ed795 EL |
4562 | " }\n" |
4563 | " break;\n"); | |
4564 | } | |
4565 | src = mputprintf(src, | |
4566 | " default:\n" | |
4567 | " param.type_error(\"%s of template\", \"%s\");\n" | |
4568 | " }\n" | |
3abe9331 | 4569 | " is_ifpresent = param.get_ifpresent() || mp->get_ifpresent();\n" |
4570 | " if (param.get_length_restriction() != NULL) {\n" | |
4571 | " set_length_range(param);\n" | |
4572 | " }\n" | |
4573 | " else {\n" | |
4574 | " set_length_range(*mp);\n" | |
4575 | " };\n" | |
970ed795 | 4576 | "}\n\n", sdef->kind==RECORD_OF?"record":"set", dispname); |
3abe9331 | 4577 | |
4578 | /* get_param() */ | |
4579 | def = mputstr(def, "Module_Param* get_param(Module_Param_Name& param_name) const;\n"); | |
4580 | src = mputprintf | |
4581 | (src, | |
4582 | "Module_Param* %s_template::get_param(Module_Param_Name& param_name) const\n" | |
4583 | "{\n" | |
4584 | " if (param_name.next_name()) {\n" | |
4585 | // Haven't reached the end of the module parameter name | |
4586 | // => the name refers to one of the elements, not to the whole record of | |
4587 | " char* param_field = param_name.get_current_name();\n" | |
4588 | " if (param_field[0] < '0' || param_field[0] > '9') {\n" | |
4589 | " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n" | |
4590 | " \"expected a valid index for %s template type `%s'\");\n" | |
4591 | " }\n" | |
4592 | " int param_index = -1;\n" | |
4593 | " sscanf(param_field, \"%%d\", ¶m_index);\n" | |
4594 | " return (*this)[param_index].get_param(param_name);\n" | |
4595 | " }\n" | |
4596 | " Module_Param* mp = NULL;\n" | |
4597 | " switch (template_selection) {\n" | |
4598 | " case UNINITIALIZED_TEMPLATE:\n" | |
4599 | " mp = new Module_Param_Unbound();\n" | |
4600 | " break;\n" | |
4601 | " case OMIT_VALUE:\n" | |
4602 | " mp = new Module_Param_Omit();\n" | |
4603 | " break;\n" | |
4604 | " case ANY_VALUE:\n" | |
4605 | " mp = new Module_Param_Any();\n" | |
4606 | " break;\n" | |
4607 | " case ANY_OR_OMIT:\n" | |
4608 | " mp = new Module_Param_AnyOrNone();\n" | |
4609 | " break;\n" | |
4610 | " case SPECIFIC_VALUE: {\n" | |
4611 | " Vector<Module_Param*> values;\n" | |
4612 | " for (int i = 0; i < single_value.n_elements; ++i) {\n" | |
4613 | " values.push_back((*this)[i].get_param(param_name));\n" | |
4614 | " }\n" | |
4615 | " mp = new Module_Param_Value_List();\n" | |
4616 | " mp->add_list_with_implicit_ids(&values);\n" | |
4617 | " values.clear();\n" | |
4618 | " break; }\n" | |
4619 | " case VALUE_LIST:\n" | |
4620 | " case COMPLEMENTED_LIST: {\n" | |
4621 | " if (template_selection == VALUE_LIST) {\n" | |
4622 | " mp = new Module_Param_List_Template();\n" | |
4623 | " }\n" | |
4624 | " else {\n" | |
4625 | " mp = new Module_Param_ComplementList_Template();\n" | |
4626 | " }\n" | |
4627 | " for (size_t i = 0; i < value_list.n_values; ++i) {\n" | |
4628 | " mp->add_elem(value_list.list_value[i].get_param(param_name));\n" | |
4629 | " }\n" | |
4630 | " break; }\n" | |
4631 | " default:\n" | |
4632 | " break;\n" | |
4633 | " }\n" | |
4634 | " if (is_ifpresent) {\n" | |
4635 | " mp->set_ifpresent();\n" | |
4636 | " }\n" | |
4637 | " mp->set_length_restriction(get_length_range());\n" | |
4638 | " return mp;\n" | |
4639 | "}\n\n", name, sdef->kind==RECORD_OF ? "record of" : "set of", dispname); | |
970ed795 EL |
4640 | |
4641 | /* check template restriction */ | |
4642 | def = mputstr(def, "void check_restriction(template_res t_res, " | |
3abe9331 | 4643 | "const char* t_name=NULL, boolean legacy = FALSE) const;\n"); |
970ed795 EL |
4644 | src = mputprintf(src, |
4645 | "void %s_template::check_restriction(" | |
3abe9331 | 4646 | "template_res t_res, const char* t_name, boolean legacy) const\n" |
970ed795 EL |
4647 | "{\n" |
4648 | "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n" | |
4649 | "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n" | |
4650 | "case TR_OMIT:\n" | |
4651 | "if (template_selection==OMIT_VALUE) return;\n" | |
4652 | "case TR_VALUE:\n" | |
4653 | "if (template_selection!=SPECIFIC_VALUE || is_ifpresent) break;\n" | |
4654 | "for (int i=0; i<single_value.n_elements; i++) " | |
4655 | "single_value.value_elements[i]->check_restriction(" | |
4656 | "t_res, t_name ? t_name : \"%s\");\n" | |
4657 | "return;\n" | |
4658 | "case TR_PRESENT:\n" | |
3abe9331 | 4659 | "if (!match_omit(legacy)) return;\n" |
970ed795 EL |
4660 | "break;\n" |
4661 | "default:\n" | |
4662 | "return;\n" | |
4663 | "}\n" | |
4664 | "TTCN_error(\"Restriction `%%s' on template of type %%s " | |
4665 | "violated.\", get_res_name(t_res), t_name ? t_name : \"%s\");\n" | |
4666 | "}\n\n", name, dispname, dispname); | |
4667 | ||
4668 | /* end of class */ | |
4669 | def = mputstr(def, "};\n\n"); | |
4670 | ||
4671 | output->header.class_decls = mputprintf(output->header.class_decls, | |
4672 | "class %s_template;\n", name); | |
4673 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
4674 | Free(def); | |
4675 | output->source.methods = mputstr(output->source.methods, src); | |
4676 | Free(src); | |
4677 | } | |
4678 | ||
4679 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
4680 | ||
4681 | void defRecordOfTemplate2(const struct_of_def *sdef, output_struct *output) | |
4682 | { | |
4683 | char *def = NULL, *src = NULL; | |
4684 | const char *name = sdef->name; | |
4685 | const char *type = sdef->type; | |
4686 | const char *base_class = sdef->kind == RECORD_OF ? "Record_Of_Template" : "Set_Of_Template"; | |
4687 | ||
4688 | /* Class definition */ | |
4689 | def = mputprintf(def, "class %s_template : public %s {\n", name, base_class); | |
4690 | ||
4691 | /* public member functions */ | |
4692 | def = mputstr(def, "\npublic:\n"); | |
4693 | ||
4694 | /* constructors */ | |
4695 | def = mputprintf(def, "%s_template() {}\n", name); | |
4696 | ||
4697 | def = mputprintf(def, "%s_template(template_sel other_value): %s(other_value) " | |
4698 | "{ check_single_selection(other_value); }\n", name, base_class); | |
4699 | ||
4700 | def = mputprintf(def, "%s_template(null_type other_value);\n", name); | |
4701 | src = mputprintf(src, "%s_template::%s_template(null_type)\n" | |
4702 | " : %s(SPECIFIC_VALUE)\n" | |
4703 | "{\n" | |
4704 | "single_value.n_elements = 0;\n" | |
4705 | "single_value.value_elements = NULL;\n" | |
4706 | "}\n\n", name, name, base_class); | |
4707 | ||
4708 | def = mputprintf(def, "%s_template(const %s& other_value) " | |
4709 | "{ copy_value(&other_value); }\n", name, name); | |
4710 | def = mputprintf(def, "%s_template(const OPTIONAL<%s>& other_value) " | |
4711 | "{ copy_optional(&other_value); }\n", name, name); | |
4712 | ||
4713 | /* copy constructor */ | |
4714 | def = mputprintf(def, "%s_template(const %s_template& other_value): %s() { copy_template(other_value); }\n", | |
4715 | name, name, base_class); | |
4716 | ||
4717 | /* assignment operators */ | |
4718 | def = mputprintf(def, "%s_template& operator=(template_sel other_value);\n", | |
4719 | name); | |
4720 | src = mputprintf(src, | |
4721 | "%s_template& %s_template::operator=(template_sel other_value)\n" | |
4722 | "{\n" | |
4723 | "check_single_selection(other_value);\n" | |
4724 | "clean_up();\n" | |
4725 | "set_selection(other_value);\n" | |
4726 | "return *this;\n" | |
4727 | "}\n\n", name, name); | |
4728 | ||
4729 | def = mputprintf(def, "%s_template& operator=(null_type other_value);\n", | |
4730 | name); | |
4731 | src = mputprintf(src, | |
4732 | "%s_template& %s_template::operator=(null_type)\n" | |
4733 | "{\n" | |
4734 | "clean_up();\n" | |
4735 | "set_selection(SPECIFIC_VALUE);\n" | |
4736 | "single_value.n_elements = 0;\n" | |
4737 | "single_value.value_elements = NULL;\n" | |
4738 | "return *this;\n" | |
4739 | "}\n\n", name, name); | |
4740 | ||
4741 | def = mputprintf(def, "%s_template& operator=(const %s& other_value);\n", | |
4742 | name, name); | |
4743 | src = mputprintf(src, | |
4744 | "%s_template& %s_template::operator=(const %s& other_value)\n" | |
4745 | "{\n" | |
4746 | "clean_up();\n" | |
4747 | "copy_value(&other_value);\n" | |
4748 | "return *this;\n" | |
4749 | "}\n\n", name, name, name); | |
4750 | ||
4751 | def = mputprintf(def, "%s_template& operator=(const OPTIONAL<%s>& " | |
4752 | "other_value);\n", name, name); | |
4753 | src = mputprintf(src, | |
4754 | "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n" | |
4755 | "{\n" | |
4756 | "clean_up();\n" | |
4757 | "copy_optional(&other_value);\n" | |
4758 | "return *this;\n" | |
4759 | "}\n\n", name, name, name); | |
4760 | ||
4761 | def = mputprintf(def, "%s_template& operator=(const %s_template& " | |
4762 | "other_value);\n\n", name, name); | |
4763 | src = mputprintf(src, | |
4764 | "%s_template& %s_template::operator=(const %s_template& other_value)\n" | |
4765 | "{\n" | |
4766 | "if (&other_value != this) {\n" | |
4767 | "clean_up();\n" | |
4768 | "copy_template(other_value);\n" | |
4769 | "}\n" | |
4770 | "return *this;\n" | |
4771 | "}\n\n", name, name, name); | |
4772 | ||
4773 | /* indexing operators */ | |
4774 | def = mputprintf(def, | |
4775 | "%s_template& operator[](int index_value);\n" | |
4776 | "%s_template& operator[](const INTEGER& index_value);\n" | |
4777 | "const %s_template& operator[](int index_value) const;\n" | |
4778 | "const %s_template& operator[](const INTEGER& index_value) const;\n", | |
4779 | type, | |
4780 | type, | |
4781 | type, | |
4782 | type); | |
4783 | ||
4784 | src = mputprintf(src, | |
4785 | "%s_template& %s_template::operator[](int index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n" | |
4786 | "%s_template& %s_template::operator[](const INTEGER& index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n" | |
4787 | "const %s_template& %s_template::operator[](int index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n" | |
4788 | "const %s_template& %s_template::operator[](const INTEGER& index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n\n", | |
4789 | type, name, type, | |
4790 | type, name, type, | |
4791 | type, name, type, | |
4792 | type, name, type); | |
4793 | ||
4794 | /* match operation */ | |
3abe9331 | 4795 | def = mputprintf(def, "inline boolean match(const %s& match_value, " |
4796 | "boolean legacy = FALSE) const " | |
4797 | "{ return matchv(&match_value, legacy); }\n", name); | |
970ed795 EL |
4798 | |
4799 | /* valueof operation */ | |
4800 | def = mputprintf(def, "%s valueof() const;\n", name); | |
4801 | src = mputprintf(src, | |
4802 | "%s %s_template::valueof() const\n" | |
4803 | "{\n" | |
4804 | "%s ret_val;\n" | |
4805 | "valueofv(&ret_val);\n" | |
4806 | "return ret_val;\n" | |
4807 | "}\n\n", name, name, name); | |
4808 | ||
4809 | /* substr() predefined function for templates */ | |
4810 | def = mputprintf(def, | |
4811 | "%s substr(int index, int returncount) const;\n\n", name); | |
4812 | src = mputprintf(src, | |
4813 | "%s %s_template::substr(int index, int returncount) const\n" | |
4814 | "{\n" | |
4815 | "%s rec_of;\n" | |
4816 | "substr_(index, returncount, &rec_of);\n" | |
4817 | "return rec_of;\n" | |
4818 | "}\n\n", name, name, name); | |
4819 | ||
4820 | /* replace() predefined function for templates */ | |
4821 | def = mputprintf(def, | |
4822 | "%s replace(int index, int len, const %s_template& repl) const;\n\n", | |
4823 | name, name); | |
4824 | src = mputprintf(src, | |
4825 | "%s %s_template::replace(int index, int len, const %s_template& repl) const\n" | |
4826 | "{\n" | |
4827 | "%s rec_of;\n" | |
4828 | "replace_(index, len, &repl, &rec_of);\n" | |
4829 | "return rec_of;\n" | |
4830 | "}\n\n", name, name, name, name); | |
4831 | def = mputprintf(def, | |
4832 | "%s replace(int index, int len, const %s& repl) const;\n\n", | |
4833 | name, name); | |
4834 | src = mputprintf(src, | |
4835 | "%s %s_template::replace(int index, int len, const %s& repl) const\n" | |
4836 | "{\n" | |
4837 | "%s rec_of;\n" | |
4838 | "replace_(index, len, &repl, &rec_of);\n" | |
4839 | "return rec_of;\n" | |
4840 | "}\n\n", name, name, name, name); | |
4841 | ||
4842 | /* value list and set handling operators */ | |
4843 | def = mputprintf(def, | |
4844 | "inline %s_template& list_item(int list_index) { return *(static_cast<%s_template*>(get_list_item(list_index))); }\n", name, name); | |
4845 | ||
4846 | if (sdef->kind == SET_OF) { | |
4847 | def = mputprintf(def, "%s_template& set_item(int set_index);\n", type); | |
4848 | src = mputprintf(src, | |
4849 | "%s_template& %s_template::set_item(int set_index) " | |
4850 | "{ return *(static_cast<%s_template*>(get_set_item(set_index))); }\n", | |
4851 | type, name, type); | |
4852 | } | |
4853 | ||
4854 | /* logging functions */ | |
3abe9331 | 4855 | def = mputprintf(def, "inline void log_match(const %s& match_value, " |
4856 | "boolean legacy = FALSE) const " | |
4857 | "{ log_matchv(&match_value, legacy); }\n", name); | |
970ed795 EL |
4858 | |
4859 | /* virtual helper functions */ | |
4860 | def = mputprintf(def, | |
4861 | "%s* create() const { return new %s_template; }\n" | |
4862 | "Base_Template* create_elem() const;\n" | |
4863 | "const TTCN_Typedescriptor_t* get_descriptor() const;\n", | |
4864 | base_class, name); | |
4865 | src = mputprintf(src, | |
4866 | "Base_Template* %s_template::create_elem() const { return new %s_template; }\n" | |
4867 | "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const { return &%s_descr_; }\n", | |
4868 | name, type, | |
4869 | name, name); | |
4870 | ||
4871 | /* end of class */ | |
4872 | def = mputstr(def, "};\n\n"); | |
4873 | ||
4874 | output->header.class_decls = mputprintf(output->header.class_decls, | |
4875 | "class %s_template;\n", name); | |
4876 | output->header.class_defs = mputstr(output->header.class_defs, def); | |
4877 | Free(def); | |
4878 | output->source.methods = mputstr(output->source.methods, src); | |
4879 | Free(src); | |
4880 | } |