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