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