1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
19 * Szabo, Janos Zoltan – initial implementation
21 * Zalanyi, Balazs Andor
23 ******************************************************************************/
24 #include "../common/memory.h"
25 #include "datatypes.h"
26 #include "record_of.h"
30 #include "ttcn3/compiler.h"
32 /** code generation for original runtime */
33 static void defRecordOfClass1(const struct_of_def
*sdef
, output_struct
*output
);
34 static void defRecordOfTemplate1(const struct_of_def
*sdef
, output_struct
*output
);
35 /** code generation for alternative runtime (TITAN_RUNTIME_2) */
36 static void defRecordOfClass2(const struct_of_def
*sdef
, output_struct
*output
);
37 static void defRecordOfTemplate2(const struct_of_def
*sdef
, output_struct
*output
);
39 void defRecordOfClass(const struct_of_def
*sdef
, output_struct
*output
)
41 if (use_runtime_2
) defRecordOfClass2(sdef
, output
);
42 else defRecordOfClass1(sdef
, output
);
45 void defRecordOfTemplate(const struct_of_def
*sdef
, output_struct
*output
)
47 if (use_runtime_2
) defRecordOfTemplate2(sdef
, output
);
48 else defRecordOfTemplate1(sdef
, output
);
51 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
53 void defRecordOfClass1(const struct_of_def
*sdef
, output_struct
*output
)
55 char *def
= NULL
, *src
= NULL
;
56 const char *name
= sdef
->name
, *dispname
= sdef
->dispname
;
57 const char *type
= sdef
->type
;
58 boolean ber_needed
= force_gen_seof
|| (sdef
->isASN1
&& enable_ber());
59 boolean raw_needed
= force_gen_seof
|| (sdef
->hasRaw
&& enable_raw());
60 boolean text_needed
= force_gen_seof
|| (sdef
->hasText
&& enable_text());
61 boolean xer_needed
= force_gen_seof
|| (sdef
->hasXer
&& enable_xer());
62 boolean json_needed
= force_gen_seof
|| (sdef
->hasJson
&& enable_json());
64 /* Class definition and private data members */
67 "// written by %s in " __FILE__
" at %d\n"
69 "class %s : public Base_Type {\n"
70 "struct recordof_setof_struct {\n"
73 "%s **value_elements;\n"
76 , __FUNCTION__
, __LINE__
80 /* constant unbound element */
81 def
= mputprintf(def
, "\nstatic const %s UNBOUND_ELEM;\n", type
);
82 src
= mputprintf(src
, "\nconst %s %s::UNBOUND_ELEM;\n", type
, name
);
84 /* private member functions */
87 "friend boolean operator==(null_type null_value, "
88 "const %s& other_value);\n", name
);
90 if (sdef
->kind
== SET_OF
) {
91 /* callback function for comparison */
92 def
= mputstr(def
, "static boolean compare_function("
93 "const Base_Type *left_ptr, int left_index, "
94 "const Base_Type *right_ptr, int right_index);\n");
95 src
= mputprintf(src
, "boolean %s::compare_function("
96 "const Base_Type *left_ptr, int left_index, "
97 "const Base_Type *right_ptr, int right_index)\n"
99 "if (((const %s*)left_ptr)->val_ptr == NULL) "
100 "TTCN_error(\"The left operand of comparison is an unbound value of "
102 "if (((const %s*)right_ptr)->val_ptr == NULL) "
103 "TTCN_error(\"The right operand of comparison is an unbound value of "
105 "if (((const %s*)left_ptr)->val_ptr->value_elements[left_index] != NULL){\n"
106 "if (((const %s*)right_ptr)->val_ptr->value_elements[right_index] != NULL){\n"
107 "return *((const %s*)left_ptr)->val_ptr->value_elements[left_index] == "
108 "*((const %s*)right_ptr)->val_ptr->value_elements[right_index];\n"
109 "} else return FALSE;\n"
111 "return ((const %s*)right_ptr)->val_ptr->value_elements[right_index] == NULL;\n"
113 "}\n\n", name
, name
, dispname
, name
, dispname
, name
, name
, name
, name
, name
);
116 /* public member functions */
117 def
= mputstr(def
, "\npublic:\n");
118 def
= mputprintf(def
, " typedef %s of_type;\n", sdef
->type
);
121 def
= mputprintf(def
, "%s();\n", name
);
122 src
= mputprintf(src
,
126 "}\n\n", name
, name
);
128 def
= mputprintf(def
, "%s(null_type other_value);\n", name
);
129 src
= mputprintf(src
,
130 "%s::%s(null_type)\n"
132 "val_ptr = new recordof_setof_struct;\n"
133 "val_ptr->ref_count = 1;\n"
134 "val_ptr->n_elements = 0;\n"
135 "val_ptr->value_elements = NULL;\n"
136 "}\n\n", name
, name
);
138 /* copy constructor */
139 def
= mputprintf(def
, "%s(const %s& other_value);\n", name
, name
);
140 src
= mputprintf(src
,
141 "%s::%s(const %s& other_value)\n"
143 "if (!other_value.is_bound()) "
144 "TTCN_error(\"Copying an unbound value of type %s.\");\n"
145 "val_ptr = other_value.val_ptr;\n"
146 "val_ptr->ref_count++;\n"
147 "}\n\n", name
, name
, name
, dispname
);
150 def
= mputprintf(def
, "~%s();\n\n", name
);
151 src
= mputprintf(src
,
155 "if (val_ptr != NULL) val_ptr = NULL;\n"
156 "}\n\n", name
, name
);
158 /* clean_up function */
159 def
= mputstr(def
, "void clean_up();\n");
162 "void %s::clean_up()\n"
164 "if (val_ptr != NULL) {\n"
165 "if (val_ptr->ref_count > 1) {\n"
166 "val_ptr->ref_count--;\n"
169 "else if (val_ptr->ref_count == 1) {\n"
170 "for (int elem_count = 0; elem_count < val_ptr->n_elements;\n"
172 "if (val_ptr->value_elements[elem_count] != NULL)\n"
173 "delete val_ptr->value_elements[elem_count];\n"
174 "free_pointers((void**)val_ptr->value_elements);\n"
179 "TTCN_error(\"Internal error: Invalid reference counter in a record "
180 "of/set of value.\");\n"
184 /* assignment operators */
185 def
= mputprintf(def
, "%s& operator=(null_type other_value);\n", name
);
186 src
= mputprintf(src
,
187 "%s& %s::operator=(null_type)\n"
190 "val_ptr = new recordof_setof_struct;\n"
191 "val_ptr->ref_count = 1;\n"
192 "val_ptr->n_elements = 0;\n"
193 "val_ptr->value_elements = NULL;\n"
195 "}\n\n", name
, name
);
197 def
= mputprintf(def
, "%s& operator=(const %s& other_value);\n\n",
199 src
= mputprintf(src
,
200 "%s& %s::operator=(const %s& other_value)\n"
202 "if (other_value.val_ptr == NULL) "
203 "TTCN_error(\"Assigning an unbound value of type %s.\");\n"
204 "if (this != &other_value) {\n"
206 "val_ptr = other_value.val_ptr;\n"
207 "val_ptr->ref_count++;\n"
210 "}\n\n", name
, name
, name
, dispname
);
212 /* comparison operators */
213 def
= mputstr(def
, "boolean operator==(null_type other_value) const;\n");
214 src
= mputprintf(src
,
215 "boolean %s::operator==(null_type) const\n"
217 "if (val_ptr == NULL)\n"
218 "TTCN_error(\"The left operand of comparison is an unbound value of "
220 "return val_ptr->n_elements == 0 ;\n"
221 "}\n\n", name
, dispname
);
223 def
= mputprintf(def
, "boolean operator==(const %s& other_value) const;\n",
225 src
= mputprintf(src
,
226 "boolean %s::operator==(const %s& other_value) const\n"
228 "if (val_ptr == NULL) "
229 "TTCN_error(\"The left operand of comparison is an unbound value of type "
231 "if (other_value.val_ptr == NULL) "
232 "TTCN_error(\"The right operand of comparison is an unbound value of type "
234 "if (val_ptr == other_value.val_ptr) return TRUE;\n", name
, name
,
236 if (sdef
->kind
== SET_OF
) {
238 "return compare_set_of(this, val_ptr->n_elements, &other_value, "
239 "(other_value.val_ptr)->n_elements, compare_function);\n");
243 "if (val_ptr->n_elements != (other_value.val_ptr)->n_elements)\n"
245 "for (int elem_count = 0; elem_count < val_ptr->n_elements; elem_count++){\n"
246 "if (val_ptr->value_elements[elem_count] != NULL){\n"
247 "if ((other_value.val_ptr)->value_elements[elem_count] != NULL){\n"
248 " if (*val_ptr->value_elements[elem_count] != "
249 "*(other_value.val_ptr)->value_elements[elem_count]) "
251 "} else return FALSE;\n"
253 "if ((other_value.val_ptr)->value_elements[elem_count] != NULL) "
259 src
= mputstr(src
, "}\n\n");
261 def
= mputstr(def
, "inline boolean operator!=(null_type other_value) const "
262 "{ return !(*this == other_value); }\n");
263 def
= mputprintf(def
, "inline boolean operator!=(const %s& other_value) "
264 "const { return !(*this == other_value); }\n\n", name
);
266 /* indexing operators */
267 /* Non-const operator[] is allowed to extend the record-of */
268 def
= mputprintf(def
, "%s& operator[](int index_value);\n", type
);
269 src
= mputprintf(src
,
270 "%s& %s::operator[](int index_value)\n"
272 "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
273 "using a negative index: %%d.\", index_value);\n"
274 "if (val_ptr == NULL) {\n"
275 "val_ptr = new recordof_setof_struct;\n"
276 "val_ptr->ref_count = 1;\n"
277 "val_ptr->n_elements = 0;\n"
278 "val_ptr->value_elements = NULL;\n"
279 "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */
280 "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n"
281 "new_val_ptr->ref_count = 1;\n"
282 "new_val_ptr->n_elements = (index_value >= val_ptr->n_elements) ? "
283 "index_value + 1 : val_ptr->n_elements;\n"
284 "new_val_ptr->value_elements = "
285 "(%s**)allocate_pointers(new_val_ptr->n_elements);\n"
286 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
288 "if (val_ptr->value_elements[elem_count] != NULL){\n"
289 "new_val_ptr->value_elements[elem_count] = "
290 "new %s(*(val_ptr->value_elements[elem_count]));\n"
294 "val_ptr = new_val_ptr;\n"
296 "if (index_value >= val_ptr->n_elements) set_size(index_value + 1);\n"
297 "if (val_ptr->value_elements[index_value] == NULL) {\n"
298 "val_ptr->value_elements[index_value] = new %s;\n"
300 "return *val_ptr->value_elements[index_value];\n"
301 "}\n\n", type
, name
, dispname
, type
, type
, type
);
303 def
= mputprintf(def
, "%s& operator[](const INTEGER& index_value);\n",
305 src
= mputprintf(src
,
306 "%s& %s::operator[](const INTEGER& index_value)\n"
308 "index_value.must_bound(\"Using an unbound integer value for indexing "
309 "a value of type %s.\");\n"
310 "return (*this)[(int)index_value];\n"
311 "}\n\n", type
, name
, dispname
);
313 /* Const operator[] throws an error if over-indexing */
314 def
= mputprintf(def
, "const %s& operator[](int index_value) const;\n",
316 src
= mputprintf(src
,
317 "const %s& %s::operator[](int index_value) const\n"
319 "if (val_ptr == NULL)\n"
320 "TTCN_error(\"Accessing an element in an unbound value of type %s.\");\n"
321 "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
322 "using a negative index: %%d.\", index_value);\n"
323 "if (index_value >= val_ptr->n_elements) TTCN_error(\"Index overflow in "
324 "a value of type %s: The index is %%d, but the value has only %%d "
325 "elements.\", index_value, val_ptr->n_elements);\n"
326 "return (val_ptr->value_elements[index_value] != NULL) ?\n"
327 "*val_ptr->value_elements[index_value] : UNBOUND_ELEM;\n"
328 "}\n\n", type
, name
, dispname
, dispname
, dispname
);
330 def
= mputprintf(def
, "const %s& operator[](const INTEGER& index_value) "
332 src
= mputprintf(src
,
333 "const %s& %s::operator[](const INTEGER& index_value) const\n"
335 "index_value.must_bound(\"Using an unbound integer value for indexing "
336 "a value of type %s.\");\n"
337 "return (*this)[(int)index_value];\n"
338 "}\n\n", type
, name
, dispname
);
340 /* rotation operators */
341 def
= mputprintf(def
,
342 "%s operator<<=(int rotate_count) const;\n"
343 "%s operator<<=(const INTEGER& rotate_count) const;\n"
344 "%s operator>>=(int rotate_count) const;\n"
345 "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
346 name
, name
, name
, name
);
347 src
= mputprintf(src
,
348 "%s %s::operator<<=(int rotate_count) const\n"
350 "return *this >>= (-rotate_count);\n"
352 "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
354 "rotate_count.must_bound(\""
355 "Unbound integer operand of rotate left operator.\");\n"
356 "return *this >>= (int)(-rotate_count);\n"
358 "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
360 "rotate_count.must_bound(\""
361 "Unbound integer operand of rotate right operator.\");\n"
362 "return *this >>= (int)rotate_count;\n"
364 "%s %s::operator>>=(int rotate_count) const\n"
366 "if (val_ptr == NULL) "
367 "TTCN_error(\"Performing rotation operation on an unbound value of type "
369 "if (val_ptr->n_elements == 0) return *this;\n"
371 "if (rotate_count>=0) rc = rotate_count %% val_ptr->n_elements;\n"
372 "else rc = val_ptr->n_elements - ((-rotate_count) %% val_ptr->n_elements);\n"
373 "if (rc == 0) return *this;\n"
375 "ret_val.set_size(val_ptr->n_elements);\n"
376 "for (int i=0; i<val_ptr->n_elements; i++) {\n"
377 "if (val_ptr->value_elements[i] != NULL) {\n"
378 "ret_val.val_ptr->value_elements[(i+rc)%%val_ptr->n_elements] ="
379 "new %s(*val_ptr->value_elements[i]);\n"
384 name
, name
, name
, name
, name
, name
, name
, name
, dispname
, name
, type
);
387 def
= mputprintf(def
,
388 "%s operator+(const %s& other_value) const;\n\n", name
, name
);
389 src
= mputprintf(src
,
390 "%s %s::operator+(const %s& other_value) const\n"
392 "if (val_ptr == NULL || other_value.val_ptr == NULL) "
393 "TTCN_error(\"Unbound operand of %s concatenation.\");\n"
394 "if (val_ptr->n_elements == 0) return other_value;\n"
395 "if (other_value.val_ptr->n_elements == 0) return *this;\n"
397 "ret_val.set_size(val_ptr->n_elements+other_value.val_ptr->n_elements);\n"
398 "for (int i=0; i<val_ptr->n_elements; i++) {\n"
399 "if (val_ptr->value_elements[i] != NULL) {\n"
400 "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n"
403 "for (int i=0; i<other_value.val_ptr->n_elements; i++) {\n"
404 "if (other_value.val_ptr->value_elements[i] != NULL) {\n"
405 "ret_val.val_ptr->value_elements[i+val_ptr->n_elements] = "
406 "new %s(*other_value.val_ptr->value_elements[i]);\n"
410 "}\n\n", name
, name
, name
, dispname
, name
, type
, type
);
413 def
= mputprintf(def
,
414 "%s substr(int index, int returncount) const;\n\n", name
);
415 src
= mputprintf(src
,
416 "%s %s::substr(int index, int returncount) const\n"
418 "if (val_ptr == NULL) "
419 "TTCN_error(\"The first argument of substr() is an unbound value of "
421 "check_substr_arguments(val_ptr->n_elements, index, returncount, "
422 "\"%s\",\"element\");\n"
424 "ret_val.set_size(returncount);\n"
425 "for (int i=0; i<returncount; i++) {\n"
426 "if (val_ptr->value_elements[i+index] != NULL) {\n"
427 "ret_val.val_ptr->value_elements[i] = "
428 "new %s(*val_ptr->value_elements[i+index]);\n"
432 "}\n\n", name
, name
, dispname
, dispname
, name
, type
);
435 def
= mputprintf(def
,
436 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
437 src
= mputprintf(src
,
438 "%s %s::replace(int index, int len, const %s& repl) const\n"
440 "if (val_ptr == NULL) "
441 "TTCN_error(\"The first argument of replace() is an unbound value of "
443 "if (repl.val_ptr == NULL) "
444 "TTCN_error(\"The fourth argument of replace() is an unbound value of "
446 "check_replace_arguments(val_ptr->n_elements, index, len, "
447 "\"%s\",\"element\");\n"
449 "ret_val.set_size(val_ptr->n_elements + repl.val_ptr->n_elements - len);\n"
450 "for (int i = 0; i < index; i++) {\n"
451 "if (val_ptr->value_elements[i] != NULL) {\n"
452 "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n"
455 "for (int i = 0; i < repl.val_ptr->n_elements; i++) {\n"
456 "if (repl.val_ptr->value_elements[i] != NULL) {\n"
457 "ret_val.val_ptr->value_elements[i+index] = "
458 "new %s(*repl.val_ptr->value_elements[i]);\n"
461 "for (int i = 0; i < val_ptr->n_elements - index - len; i++) {\n"
462 "if (val_ptr->value_elements[index+i+len] != NULL) {\n"
463 "ret_val.val_ptr->value_elements[index+i+repl.val_ptr->n_elements] = "
464 "new %s(*val_ptr->value_elements[index+i+len]);\n"
468 "}\n\n", name
, name
, name
, dispname
, dispname
, dispname
, name
, type
, type
, type
);
469 def
= mputprintf(def
,
470 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
472 src
= mputprintf(src
,
473 "%s %s::replace(int index, int len, const %s_template& repl) const\n"
475 "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
476 "replace() is a template with non-specific value.\");\n"
477 "return replace(index, len, repl.valueof());\n"
478 "}\n\n", name
, name
, name
);
480 /* set_size function */
481 def
= mputstr(def
, "void set_size(int new_size);\n");
482 src
= mputprintf(src
, "void %s::set_size(int new_size)\n"
484 "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size "
485 "for a value of type %s.\");\n"
486 "if (val_ptr == NULL) {\n"
487 "val_ptr = new recordof_setof_struct;\n"
488 "val_ptr->ref_count = 1;\n"
489 "val_ptr->n_elements = 0;\n"
490 "val_ptr->value_elements = NULL;\n"
491 "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */
492 "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n"
493 "new_val_ptr->ref_count = 1;\n"
494 "new_val_ptr->n_elements = (new_size < val_ptr->n_elements) ? "
495 "new_size : val_ptr->n_elements;\n"
496 "new_val_ptr->value_elements = "
497 "(%s**)allocate_pointers(new_val_ptr->n_elements);\n"
498 "for (int elem_count = 0; elem_count < new_val_ptr->n_elements; "
500 "if (val_ptr->value_elements[elem_count] != NULL){\n"
501 "new_val_ptr->value_elements[elem_count] = "
502 "new %s(*(val_ptr->value_elements[elem_count]));\n"
506 "val_ptr = new_val_ptr;\n"
508 "if (new_size > val_ptr->n_elements) {\n"
509 "val_ptr->value_elements = (%s**)"
510 "reallocate_pointers((void**)val_ptr->value_elements, "
511 "val_ptr->n_elements, new_size);\n"
512 "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n"
513 "if((val_ptr->n_elements/1000)!=(new_size/1000)) "
514 "TTCN_warning(\"New size of type %s: %%d\",new_size);\n"
516 "val_ptr->n_elements = new_size;\n"
517 "} else if (new_size < val_ptr->n_elements) {\n"
518 "for (int elem_count = new_size; elem_count < val_ptr->n_elements; "
520 "if (val_ptr->value_elements[elem_count] != NULL)"
521 "delete val_ptr->value_elements[elem_count];\n"
522 "val_ptr->value_elements = (%s**)"
523 "reallocate_pointers((void**)val_ptr->value_elements, "
524 "val_ptr->n_elements, new_size);\n"
525 "val_ptr->n_elements = new_size;\n"
527 "}\n\n", name
, dispname
, type
, type
, type
, dispname
, type
);
529 /* is_bound function */
531 "inline boolean is_bound() const {return val_ptr != NULL; }\n");
533 /* is_present function */
535 "inline boolean is_present() const { return is_bound(); }\n");
537 /* is_value function */
539 "boolean is_value() const;\n");
540 src
= mputprintf(src
,
541 "boolean %s::is_value() const\n"
543 "if (val_ptr == NULL) return false;\n"
544 "for(int i = 0; i < val_ptr->n_elements; ++i) {\n"
545 "if (val_ptr->value_elements[i] == NULL || "
546 "!val_ptr->value_elements[i]->is_value()) return FALSE;\n"
551 /* sizeof operation */
553 "int size_of() const;\n"
554 "int n_elem() const { return size_of(); }\n");
555 src
= mputprintf(src
,
556 "int %s::size_of() const\n"
558 "if (val_ptr == NULL) "
559 "TTCN_error(\"Performing sizeof operation on an unbound value of type "
561 "return val_ptr->n_elements;\n"
562 "}\n\n", name
, dispname
);
564 /* lengthof operation */
565 def
= mputstr(def
, "int lengthof() const;\n");
566 src
= mputprintf(src
,
567 "int %s::lengthof() const\n"
569 "if (val_ptr == NULL) "
570 "TTCN_error(\"Performing lengthof operation on an unbound value of type "
572 "for (int my_length=val_ptr->n_elements; my_length>0; my_length--) "
573 "if (val_ptr->value_elements[my_length-1] != NULL) return my_length;\n"
575 "}\n\n", name
, dispname
);
578 def
= mputstr(def
, "void log() const;\n");
581 "void %s::log() const\n"
583 "if (val_ptr == NULL) {;\n"
584 "TTCN_Logger::log_event_unbound();\n"
587 "switch (val_ptr->n_elements) {\n"
589 "TTCN_Logger::log_event_str(\"{ }\");\n"
592 "TTCN_Logger::log_event_str(\"{ \");\n"
593 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
595 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
596 "(*this)[elem_count].log();\n"
598 "TTCN_Logger::log_event_str(\" }\");\n"
602 /* set_param function */
603 def
= mputstr(def
, "void set_param(Module_Param& param);\n");
604 src
= mputprintf(src
,
605 "void %s::set_param(Module_Param& param)\n"
607 " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n"
608 " param.get_id()->next_name()) {\n"
609 // Haven't reached the end of the module parameter name
610 // => the name refers to one of the elements, not to the whole record of
611 " char* param_field = param.get_id()->get_current_name();\n"
612 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
613 " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n"
614 " \" index for %s type `%s'\");\n"
616 " int param_index = -1;\n"
617 " sscanf(param_field, \"%%d\", ¶m_index);\n"
618 " (*this)[param_index].set_param(param);\n"
621 " param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n"
622 " Module_Param_Ptr mp = ¶m;\n"
623 " if (param.get_type() == Module_Param::MP_Reference) {\n"
624 " mp = param.get_referenced_param();\n"
626 " switch (param.get_operation_type()) {\n"
627 " case Module_Param::OT_ASSIGN:\n"
628 " if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {\n"
629 " *this = NULL_VALUE;\n"
632 " switch (mp->get_type()) {\n"
633 " case Module_Param::MP_Value_List:\n"
634 " set_size(mp->get_size());\n"
635 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
636 " Module_Param* const curr = mp->get_elem(i);\n"
637 " if (curr->get_type()!=Module_Param::MP_NotUsed) {\n"
638 " (*this)[i].set_param(*curr);\n"
639 " if (!(*this)[i].is_bound()) {\n"
640 " delete val_ptr->value_elements[i];\n"
641 " val_ptr->value_elements[i] = NULL;\n"
646 " case Module_Param::MP_Indexed_List:\n"
647 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
648 " Module_Param* const curr = mp->get_elem(i);\n"
649 " (*this)[curr->get_id()->get_index()].set_param(*curr);\n"
650 " if (!(*this)[curr->get_id()->get_index()].is_bound()) {\n"
651 " delete val_ptr->value_elements[curr->get_id()->get_index()];\n"
652 " val_ptr->value_elements[curr->get_id()->get_index()] = NULL;\n"
657 " param.type_error(\"%s value\", \"%s\");\n"
660 " case Module_Param::OT_CONCAT:\n"
661 " switch (mp->get_type()) {\n"
662 " case Module_Param::MP_Value_List: {\n"
663 " if (!is_bound()) *this = NULL_VALUE;\n"
664 " int start_idx = lengthof();\n"
665 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
666 " Module_Param* const curr = mp->get_elem(i);\n"
667 " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n"
668 " (*this)[start_idx+(int)i].set_param(*curr);\n"
672 " case Module_Param::MP_Indexed_List:\n"
673 " param.error(\"Cannot concatenate an indexed value list\");\n"
676 " param.type_error(\"%s value\", \"%s\");\n"
680 " TTCN_error(\"Internal error: Unknown operation type.\");\n"
682 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of",
683 dispname
, sdef
->kind
== RECORD_OF
? "record of" : "set of",
684 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
685 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
687 /* get param function */
688 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
691 "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n"
693 " if (!is_bound()) {\n"
694 " return new Module_Param_Unbound();\n"
696 " if (param_name.next_name()) {\n"
697 // Haven't reached the end of the module parameter name
698 // => the name refers to one of the elements, not to the whole record of
699 " char* param_field = param_name.get_current_name();\n"
700 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
701 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
702 " \"expected a valid index for %s type `%s'\");\n"
704 " int param_index = -1;\n"
705 " sscanf(param_field, \"%%d\", ¶m_index);\n"
706 " return (*this)[param_index].get_param(param_name);\n"
708 " Vector<Module_Param*> values;\n"
709 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
710 " values.push_back((*this)[i].get_param(param_name));\n"
712 " Module_Param_Value_List* mp = new Module_Param_Value_List();\n"
713 " mp->add_list_with_implicit_ids(&values);\n"
716 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
718 /* set implicit omit function, recursive */
719 def
= mputstr(def
, " void set_implicit_omit();\n");
720 src
= mputprintf(src
,
721 "void %s::set_implicit_omit()\n{\n"
722 "if (val_ptr == NULL) return;\n"
723 "for (int i = 0; i < val_ptr->n_elements; i++) {\n"
724 "if (val_ptr->value_elements[i] != NULL) val_ptr->value_elements[i]->set_implicit_omit();\n"
727 /* encoding / decoding functions */
728 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
729 src
= mputprintf(src
,
730 "void %s::encode_text(Text_Buf& text_buf) const\n"
732 "if (val_ptr == NULL) "
733 "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n"
734 "text_buf.push_int(val_ptr->n_elements);\n"
735 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
737 "(*this)[elem_count].encode_text(text_buf);\n"
738 "}\n\n", name
, dispname
);
740 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
741 src
= mputprintf(src
,
742 "void %s::decode_text(Text_Buf& text_buf)\n"
745 "val_ptr = new recordof_setof_struct;\n"
746 "val_ptr->ref_count = 1;\n"
747 "val_ptr->n_elements = text_buf.pull_int().get_val();\n"
748 "if (val_ptr->n_elements < 0) TTCN_error(\"Text decoder: Negative size "
749 "was received for a value of type %s.\");\n"
750 "val_ptr->value_elements = (%s**)allocate_pointers(val_ptr->n_elements);\n"
751 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
753 "val_ptr->value_elements[elem_count] = new %s;\n"
754 "val_ptr->value_elements[elem_count]->decode_text(text_buf);\n"
756 "}\n\n", name
, dispname
, type
, type
);
758 if(ber_needed
|| raw_needed
|| text_needed
|| xer_needed
|| json_needed
)
759 def_encdec(name
, &def
, &src
, ber_needed
, raw_needed
, text_needed
,
760 xer_needed
, json_needed
, FALSE
);
764 "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td,"
765 " TTCN_Buffer& p_buf) const{\n"
766 " int encoded_length=0;\n"
767 " if(p_td.text->begin_encode){\n"
768 " p_buf.put_cs(*p_td.text->begin_encode);\n"
769 " encoded_length+=p_td.text->begin_encode->lengthof();\n"
771 " if(val_ptr==NULL) {\n"
772 " TTCN_EncDec_ErrorContext::error\n"
773 " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
774 " if(p_td.text->end_encode){\n"
775 " p_buf.put_cs(*p_td.text->end_encode);\n"
776 " encoded_length+=p_td.text->end_encode->lengthof();\n"
778 " return encoded_length;\n"
780 " for(int a=0;a<val_ptr->n_elements;a++){\n"
781 " if(a!=0 && p_td.text->separator_encode){\n"
782 " p_buf.put_cs(*p_td.text->separator_encode);\n"
783 " encoded_length+=p_td.text->separator_encode->lengthof();\n"
785 " encoded_length+=(*this)[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n"
787 " if(p_td.text->end_encode){\n"
788 " p_buf.put_cs(*p_td.text->end_encode);\n"
789 " encoded_length+=p_td.text->end_encode->lengthof();\n"
791 " return encoded_length;\n"
795 src
= mputprintf(src
,
796 "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td,"
797 " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err"
798 ", boolean first_call){\n"
799 " int decoded_length=0;\n"
800 " size_t pos=p_buf.get_pos();\n"
801 " boolean sep_found=FALSE;\n"
802 " int sep_length=0;\n"
804 " if(p_td.text->begin_decode){\n"
806 " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n"
807 " if(no_err)return -1;\n"
808 " TTCN_EncDec_ErrorContext::error\n"
809 " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'"
810 " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)"
814 " decoded_length+=tl;\n"
815 " p_buf.increase_pos(tl);\n"
817 " if(p_td.text->end_decode){\n"
818 " limit.add_token(p_td.text->end_decode);\n"
821 " if(p_td.text->separator_decode){\n"
822 " limit.add_token(p_td.text->separator_decode);\n"
825 " if(first_call) {\n"
827 " val_ptr=new recordof_setof_struct;\n"
828 " val_ptr->ref_count=1;\n"
829 " val_ptr->n_elements=0;\n"
830 " val_ptr->value_elements=NULL;\n"
832 " int more=val_ptr->n_elements;\n"
835 " pos=p_buf.get_pos();\n"
836 " int len=val->TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n"
837 " if(len==-1 || (len==0 && !limit.has_token())){\n"
838 " p_buf.set_pos(pos);\n"
841 " p_buf.set_pos(p_buf.get_pos()-sep_length);\n"
842 " decoded_length-=sep_length;\n"
846 " sep_found=FALSE;\n"
847 " val_ptr->value_elements = (%s**)reallocate_pointers"
848 "((void**)val_ptr->value_elements, val_ptr->n_elements, "
849 "val_ptr->n_elements + 1);\n"
850 " val_ptr->value_elements[val_ptr->n_elements]=val;\n"
851 " val_ptr->n_elements++;\n"
852 " decoded_length+=len;\n"
853 " if(p_td.text->separator_decode){\n"
855 " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n"
858 " decoded_length+=tl;\n"
859 " p_buf.increase_pos(tl);\n"
862 " } else if(p_td.text->end_decode){\n"
864 " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n"
865 " decoded_length+=tl;\n"
866 " p_buf.increase_pos(tl);\n"
867 " limit.remove_tokens(ml);\n"
868 " return decoded_length;\n"
870 " } else if(limit.has_token(ml)){\n"
872 " if((tl=limit.match(p_buf,ml))==0){\n"
873 " sep_found=FALSE;\n"
881 " limit.remove_tokens(ml);\n"
882 " if(p_td.text->end_decode){\n"
884 " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n"
886 " if(!first_call){\n"
887 " for(int a=more; a<val_ptr->n_elements; a++) "
888 "delete val_ptr->value_elements[a];\n"
889 " val_ptr->n_elements=more;\n"
893 " TTCN_EncDec_ErrorContext::error"
894 "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'"
895 " not found for '%s': \",(const char*)*(p_td.text->end_decode)"
897 " return decoded_length;\n"
899 " decoded_length+=tl;\n"
900 " p_buf.increase_pos(tl);\n"
902 " if(val_ptr->n_elements==0){\n"
903 " if(!(p_td.text->end_decode || p_td.text->begin_decode)) {\n"
904 " if(no_err)return -1;\n"
905 " TTCN_EncDec_ErrorContext::error"
906 "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n"
907 " return decoded_length;\n"
910 " if(!first_call && more==val_ptr->n_elements && "
911 "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n"
912 " return decoded_length;\n"
919 /* BER_encode_TLV() */
922 "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&"
923 " p_td, unsigned p_coding) const\n"
925 " BER_chk_descr(p_td);\n"
926 " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n"
928 " new_tlv=ASN_BER_TLV_t::construct(NULL);\n"
929 " TTCN_EncDec_ErrorContext ec;\n"
930 " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
931 " ec.set_msg(\"Component #%%d: \", elem_i);\n"
932 " new_tlv->add_TLV((*this)[elem_i].BER_encode_TLV"
933 "(*p_td.oftype_descr, p_coding));\n"
937 " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n"
941 /* BER_decode_TLV() */
942 "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,"
943 " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n"
945 " BER_chk_descr(p_td);\n"
946 " ASN_BER_TLV_t stripped_tlv;\n"
947 " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n"
948 " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \","
950 " stripped_tlv.chk_constructed_flag(TRUE);\n"
952 " val_ptr = new recordof_setof_struct;\n"
953 " val_ptr->ref_count = 1;\n"
954 " val_ptr->n_elements = 0;\n"
955 " val_ptr->value_elements = NULL;\n"
957 " ASN_BER_TLV_t tmp_tlv;\n"
958 " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n"
959 " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n"
960 " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, "
962 " val_ptr->value_elements = (%s**)reallocate_pointers("
963 "(void**)val_ptr->value_elements, val_ptr->n_elements, "
964 "val_ptr->n_elements + 1);\n"
965 " val_ptr->n_elements++;\n"
966 " val_ptr->value_elements[val_ptr->n_elements - 1] = new %s;\n"
967 " val_ptr->value_elements[val_ptr->n_elements - 1]->BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, "
969 " ec_2.set_msg(\"%%d: \", val_ptr->n_elements);\n"
975 , sdef
->kind
==SET_OF
?" new_tlv->sort_tlvs();\n":""
979 if(sdef
->has_opentypes
) {
980 /* BER_decode_opentypes() */
983 "void BER_decode_opentypes(TTCN_Type_list& p_typelist,"
984 " unsigned L_form);\n");
987 "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist,"
988 " unsigned L_form)\n"
990 " p_typelist.push(this);\n"
991 " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n"
992 " TTCN_EncDec_ErrorContext ec_1;\n"
993 " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
994 " ec_1.set_msg(\"%%d: \", elem_i);\n"
995 " val_ptr->value_elements[elem_i]->BER_decode_opentypes(p_typelist,"
998 " p_typelist.pop();\n"
1008 "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, "
1009 "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field"
1010 ", boolean first_call){\n"
1011 " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
1012 " limit-=prepaddlength;\n"
1013 " int decoded_length=0;\n"
1014 " int decoded_field_length=0;\n"
1015 " size_t start_of_field=0;\n"
1016 " if(first_call) {\n"
1018 " val_ptr=new recordof_setof_struct;\n"
1019 " val_ptr->ref_count=1;\n"
1020 " val_ptr->n_elements=0;\n"
1021 " val_ptr->value_elements=NULL;\n"
1023 " int start_field=val_ptr->n_elements;\n"
1024 " if(p_td.raw->fieldlength || sel_field!=-1){\n"
1026 " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
1027 " for(a=0;a<sel_field;a++){\n"
1028 " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
1029 "p_buf,limit,top_bit_ord,TRUE);\n"
1030 " if(decoded_field_length < 0) return decoded_field_length;\n"
1031 " decoded_length+=decoded_field_length;\n"
1032 " limit-=decoded_field_length;\n"
1034 " if(a==0) val_ptr->n_elements=0;\n"
1036 " int a=start_field;\n"
1038 " if(!first_call) return -1;\n"
1039 " val_ptr->n_elements=0;\n"
1040 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1043 " while(limit>0){\n"
1044 " start_of_field=p_buf.get_pos_bit();\n"
1045 " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit,"
1046 "top_bit_ord,TRUE);\n"
1047 " if(decoded_field_length < 0){\n"
1048 " delete &(*this)[a];\n"
1049 " val_ptr->n_elements--;\n"
1050 " p_buf.set_pos_bit(start_of_field);\n"
1051 " if(a>start_field){\n"
1052 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1054 " } else return -1;\n"
1056 " decoded_length+=decoded_field_length;\n"
1057 " limit-=decoded_field_length;\n"
1061 if (force_gen_seof
|| (sdef
->raw
.extension_bit
!=XDEFNO
&& sdef
->raw
.extension_bit
!=XDEFDEFAULT
)){
1063 " if (%s%sp_buf.get_last_bit()%s)\n"
1064 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1066 , force_gen_seof
? "EXT_BIT_NO != p_td.raw->extension_bit && ((EXT_BIT_YES != p_td.raw->extension_bit) ^ " : ""
1067 , (force_gen_seof
|| sdef
->raw
.extension_bit
== XDEFYES
) ? "" : "!"
1068 , force_gen_seof
? ")" : "");
1073 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1076 "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td,"
1077 "RAW_enc_tree& myleaf) const{\n"
1078 " int encoded_length=0;\n"
1079 " int encoded_num_of_records=p_td.raw->fieldlength?"
1080 "smaller(val_ptr->n_elements, p_td.raw->fieldlength)"
1081 ":val_ptr->n_elements;\n"
1082 " myleaf.isleaf=FALSE;\n"
1083 " myleaf.rec_of=TRUE;\n"
1084 " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n"
1085 " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n"
1086 " for(int a=0;a<encoded_num_of_records;a++){\n"
1087 " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf,"
1088 "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n"
1089 " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr,"
1090 "*myleaf.body.node.nodes[a]);\n"
1092 " return myleaf.length=encoded_length;\n}\n\n"
1097 if (xer_needed
) { /* XERSTUFF encoder codegen for record-of, RT1 */
1099 "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n");
1101 /* Write the body of the XER encoder/decoder functions. The declaration
1102 * is written by def_encdec() in encdec.c */
1103 src
= mputprintf(src
,
1104 "boolean %s::can_start(const char *name, const char *uri, "
1105 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
1106 " boolean e_xer = is_exer(flavor);\n"
1107 " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return "
1108 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
1109 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
1112 if (!force_gen_seof
&& sdef
->nFollowers
) {
1113 /* If there are optional fields following the record-of, then seeing
1114 * {any XML tag that belongs to those fields} where the record-of may be
1115 * means that the record-of is empty. */
1117 src
= mputstr(src
, "{\n");
1118 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
1119 src
= mputprintf(src
,
1120 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
1121 , sdef
->followers
[f
].type
1122 , sdef
->followers
[f
].typegen
1129 else src
= mputstr(src
, "return TRUE;\n");
1131 src
= mputprintf(src
,
1132 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
1137 src
= mputprintf(src
,
1138 "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n"
1139 " size_t num_collected;\n"
1140 " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n"
1141 /* The above may throw but then nothing was allocated. */
1142 " if (val_ptr) try {\n"
1144 " size_t num_new;\n"
1145 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1146 " bool def_ns_1 = false;"
1147 " new_ns = (*this)[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n"
1148 " merge_ns(collected_ns, num_collected, new_ns, num_new);\n"
1149 " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */
1153 /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g.
1154 * encoding an unbound value. */
1155 " while (num_collected > 0) Free(collected_ns[--num_collected]);\n"
1156 " Free(collected_ns);\n"
1159 " num = num_collected;\n"
1160 " return collected_ns;\n"
1165 "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, "
1166 "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n"
1167 " if (val_ptr == 0) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */
1168 " int encoded_length=(int)p_buf.get_len();\n"
1169 " boolean e_xer = is_exer(p_flavor);\n"
1170 " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
1171 " || (p_flavor & USE_TYPE_ATTR)));\n"
1172 " boolean indenting = !is_canonical(p_flavor) && own_tag;\n"
1173 "%s" /* Factor out p_indent if not attribute */
1174 " if (val_ptr->n_elements==0) {\n" /* Empty record of */
1176 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1177 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
1179 if (force_gen_seof
|| sdef
->xerAttribute
) {
1181 " if (e_xer%s) {\n" /* Empty attribute. */
1182 " begin_attribute(p_td, p_buf);\n"
1183 " p_buf.put_c('\\'');\n"
1185 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1187 if (force_gen_seof
|| !sdef
->xerAttribute
) {
1192 " {\n" /* Do empty element tag */
1194 " p_buf.put_c('<');\n"
1195 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1196 " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n"
1197 /* namespace declarations for the toplevel element */
1198 " if (e_xer && p_indent==0)\n"
1200 " size_t num_collected = 0;\n"
1201 " char **collected_ns = NULL;\n"
1202 " bool def_ns = false;\n"
1203 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
1204 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
1205 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
1206 " Free(collected_ns[cur_coll]);\n"
1208 " Free(collected_ns);\n"
1211 " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n"
1214 " else {\n" /* Not empty record of. Start tag or attribute */
1215 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1216 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
1218 if (sdef
->xerAnyAttrElem
) {
1220 " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n"
1221 " static const universal_char sp = { 0,0,0,' ' };\n"
1222 " static const universal_char tb = { 0,0,0,9 };\n"
1223 " size_t buf_len = p_buf.get_len(), shorter = 0;\n"
1224 " const unsigned char * const buf_data = p_buf.get_data();\n"
1225 " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n"
1226 " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n"
1227 " unsigned char saved[4];\n"
1228 " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n"
1229 " p_buf.increase_length(-shorter);\n"
1230 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1231 " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n"
1232 " if (val_ptr->value_elements[i] == NULL) {\n"
1233 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
1234 " \"Encoding an unbound universal charstring value.\");\n"
1237 " size_t len = val_ptr->value_elements[i]->lengthof();\n"
1239 " const UNIVERSAL_CHARSTRING_ELEMENT& ue = (*val_ptr->value_elements[i])[len - 1];\n"
1240 " if (sp == ue || tb == ue) --len;\n"
1243 " size_t j, sp_at = 0;\n"
1244 /* Find the "separators" in each string */
1245 " for (j = 0; j < len; j++) {\n"
1246 " UNIVERSAL_CHARSTRING_ELEMENT ue = (*val_ptr->value_elements[i])[j];\n"
1248 " if (sp == ue || tb == ue) {}\n"
1251 " if (sp == ue || tb == ue) sp_at = j;\n"
1254 " size_t buf_start = p_buf.get_len();\n"
1255 /* Now write them */
1256 " if (sp_at > 0) {\n"
1257 " char * ns = mprintf(\" xmlns:b%d='\", i);\n"
1258 " size_t ns_len = mstrlen(ns);\n"
1259 " p_buf.put_s(ns_len, (const unsigned char*)ns);\n"
1261 " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(*val_ptr->value_elements[i]));\n"
1262 " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
1263 // Ensure the namespace abides to its restrictions
1264 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
1265 " TTCN_Buffer ns_buf;\n"
1266 " before.encode_utf8(ns_buf);\n"
1268 " ns_buf.get_string(cs);\n"
1269 " check_namespace_restrictions(p_td, (const char*)cs);\n"
1272 " p_buf.put_c('\\'');\n"
1273 " p_buf.put_c(' ');\n"
1275 /* Keep just the "b%d" part from ns */
1276 " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n"
1277 " p_buf.put_c(':');\n"
1281 " p_buf.put_c(' ');\n"
1283 // Make sure the unqualified namespace is allowed
1284 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
1285 " check_namespace_restrictions(p_td, NULL);\n"
1289 " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(*val_ptr->value_elements[i]) + j);\n"
1290 " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
1291 // Put this attribute in a dummy element and walk through it to check its validity
1292 " TTCN_Buffer check_buf;\n"
1293 " check_buf.put_s(2, (unsigned char*)\"<a\");\n"
1294 " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n"
1295 " check_buf.put_s(2, (unsigned char*)\"/>\");"
1296 " XmlReaderWrap checker(check_buf);\n"
1297 " while (1 == checker.Read()) ;\n"
1300 " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */
1303 if (force_gen_seof
|| sdef
->xerAttribute
) {
1306 " begin_attribute(p_td, p_buf);\n"
1308 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1313 " p_buf.put_c('<');\n"
1314 " boolean write_ns = (e_xer && p_indent==0);\n"
1315 " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n"
1316 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1317 " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), "
1318 "(const unsigned char*)p_td.names[e_xer]);\n"
1320 /* namespace declarations for the toplevel element */
1321 " if (e_xer && p_indent==0)\n"
1323 " size_t num_collected = 0;\n"
1324 " char **collected_ns = NULL;\n"
1325 " bool def_ns = false;\n"
1326 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
1327 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
1328 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
1329 " Free(collected_ns[cur_coll]);\n"
1331 " Free(collected_ns);\n"
1332 " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n"
1334 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1335 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
1337 if (sdef
->xmlValueList
) {
1338 src
=mputstr(src
, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */
1342 " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n"
1343 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
1344 " TTCN_EncDec_ErrorContext ec_1;\n"
1347 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1348 " if (i > 0 && !own_tag && 0 != emb_val &&\n"
1349 " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n"
1350 " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n"
1351 " if (0 != emb_val->embval_array_reg) {\n"
1352 " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n"
1353 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
1356 " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n"
1357 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
1359 " ++emb_val->embval_index;\n"
1361 " ec_1.set_msg(\"%d: \", i);\n"
1362 " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n"
1363 " (*this)[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n"
1365 " if (indenting && !is_exerlist(p_flavor)) {\n"
1367 if (sdef
->xmlValueList
) {
1368 src
=mputstr(src
, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */
1371 " do_indent(p_buf, p_indent);\n"
1373 if (force_gen_seof
|| sdef
->xerAttribute
) {
1375 " if (e_xer%s) p_buf.put_c('\\'');\n"
1377 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1381 " p_buf.put_c('<');\n"
1382 " p_buf.put_c('/');\n"
1383 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1384 " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n"
1386 if (sdef
->xerAnyAttrElem
) {
1387 src
= mputstr(src
, " }\n");
1390 " }\n" /* end if(no elements) */
1391 " return (int)p_buf.get_len() - encoded_length;\n"
1395 src
= mputprintf(src
, /* XERSTUFF decoder codegen for record-of */
1397 "// written by %s in " __FILE__
" at %d\n"
1399 "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, "
1400 "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n"
1401 " boolean e_xer = is_exer(p_flavor);\n"
1402 " int xerbits = p_td.xer_bits;\n"
1403 " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n"
1404 " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
1405 " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */
1406 /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */
1407 " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n"
1408 " int rd_ok=1, xml_depth=-1;\n"
1409 " *this = NULL_VALUE;\n" /* empty but initialized array */
1410 " int type = 0;\n" /* none */
1411 " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n"
1412 " type = p_reader.NodeType();\n"
1413 " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n"
1414 " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n"
1415 " || XML_READER_TYPE_ATTRIBUTE == type) {\n"
1416 " verify_name(p_reader, p_td, e_xer);\n"
1420 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n"
1421 " if (XML_READER_TYPE_TEXT == type) break;\n"
1424 " if (XML_READER_TYPE_ELEMENT == type) {\n"
1425 " verify_name(p_reader, p_td, e_xer);\n"
1426 " xml_depth = p_reader.Depth();\n"
1429 " }\n" /* endif(e_xer && list) */
1430 " }\n" /* next read */
1431 " else xml_depth = p_reader.Depth();\n"
1432 " p_flavor |= XER_RECOF;\n"
1433 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
1434 " TTCN_EncDec_ErrorContext ec_1;\n"
1436 , __FUNCTION__
, __LINE__
1442 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/
1443 " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */
1444 " size_t x_pos = 0;\n"
1445 " size_t x_len = strlen(x_val);\n"
1446 /* The string contains a bunch of values separated by whitespace.
1447 * Tokenize the string and create a new buffer which looks like
1448 * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>),
1449 * then use that to decode the value. */
1450 " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n"
1451 // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
1452 " x_pos += strlen(str) + 1;\n"
1453 " TTCN_Buffer buf_2;\n"
1454 " buf_2.put_c('<');\n"
1455 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
1456 " const char * const exer_name = p_td.oftype_descr->names[1];\n"
1457 " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n"
1458 /* If it has a namespace, chop off the '>' from the end */
1459 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n"
1460 " if (i_can_has_ns) {\n"
1461 " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n"
1462 " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n"
1463 " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n"
1464 " buf_2.put_s(2, (cbyte*)\"='\");\n"
1465 " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n"
1466 " buf_2.put_s(2, (cbyte*)\"'>\");\n"
1468 /* start tag completed */
1469 " buf_2.put_s(strlen(str), (cbyte*)str);\n"
1470 " buf_2.put_c('<');\n"
1471 " buf_2.put_c('/');\n"
1472 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
1473 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n"
1474 " XmlReaderWrap reader_2(buf_2);\n"
1475 " rd_ok = reader_2.Read();\n" /* Move to the start element. */
1476 " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n"
1477 /* Don't move to the #text, that's the callee's responsibility. */
1478 /* The call to the non-const operator[] creates a new element object,
1479 * then we call its XER_decode with the temporary XML reader. */
1480 " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n"
1481 " if (p_flavor & EXIT_ON_ERROR && !(*this)[val_ptr->n_elements - 1].is_bound()) {\n"
1482 " if (1 == val_ptr->n_elements) {\n"
1483 // Failed to decode even the first element
1486 // Some elements were successfully decoded -> only delete the last one
1487 " set_size(val_ptr->n_elements - 1);\n"
1489 " xmlFree(x_val);\n"
1492 " if (x_pos >= x_len) break;\n"
1494 " xmlFree(x_val);\n"
1495 " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n"
1496 /* Let the caller do AdvanceAttribute() */
1497 " else if (own_tag) {\n"
1498 " p_reader.Read();\n" /* on closing tag */
1499 " p_reader.Read();\n" /* past it */
1504 src
= mputprintf(src
,
1506 " if (p_flavor & PARENT_CLOSED) ;\n"
1507 /* Nothing to do, but do not advance past the parent's element */
1508 " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n"
1509 /* It's our XML empty element: nothing to do, skip past it */
1511 /* Note: there is no p_reader.Read() at the end of the loop below.
1512 * Each element is supposed to consume enough to leave the next element
1513 * well-positioned. */
1514 " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n"
1515 " type = p_reader.NodeType();\n"
1516 " if (XML_READER_TYPE_ELEMENT == type)\n"
1518 if (sdef
->xerAnyAttrElem
) {
1519 src
= mputprintf(src
,
1520 " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n"
1521 /* This is a record-of with ANY-ELEMENT applied, which is really meant
1522 * for the element type (a string), so behave like a record-of
1523 * (string with ANY-ELEMENT): call the non-const operator[]
1524 * to create a new element, then read the entire XML element into it. */
1525 " (*this)[val_ptr->n_elements] = (const char*)p_reader.ReadOuterXml();\n"
1526 /* Consume the element, then move ahead */
1527 " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n"
1528 " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n"
1533 /* An untagged record-of ends if it encounters an element with a name
1534 * that doesn't match its component */
1535 " if (!own_tag && !can_start((const char*)p_reader.LocalName(), "
1536 "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n"
1537 " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
1540 " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n"
1541 /* The call to the non-const operator[] creates the element */
1542 " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n"
1544 " if (0 != emb_val && !own_tag && val_ptr->n_elements > 1) {\n"
1545 " ++emb_val->embval_index;\n"
1548 " else if (XML_READER_TYPE_END_ELEMENT == type) {\n"
1549 " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
1551 " verify_end(p_reader, p_td, xml_depth, e_xer);\n"
1552 " rd_ok = p_reader.Read();\n" /* move forward one last time */
1556 " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && val_ptr->n_elements > 0) {\n"
1557 " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n"
1558 " if (0 != emb_val->embval_array_reg) {\n"
1559 " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n"
1562 " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n"
1564 " rd_ok = p_reader.Read();\n"
1567 " rd_ok = p_reader.Read();\n"
1569 " }\n" /* next read */
1570 " }\n" /* if not empty element */
1571 " }\n" /* if not LIST */
1572 " if (!own_tag && e_xer && (p_td.xer_bits & XER_OPTIONAL) && val_ptr->n_elements == 0) {\n"
1573 " clean_up();\n" /* set it to unbound, so the OPTIONAL class sets it to omit */
1581 src
= mputprintf(src
,
1582 "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n"
1584 " if (!is_bound()) {\n"
1585 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
1586 " \"Encoding an unbound value of type %s.\");\n"
1589 " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n"
1590 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1591 " if (NULL != p_td.json && p_td.json->metainfo_unbound && !(*this)[i].is_bound()) {\n"
1592 // unbound elements are encoded as { "metainfo []" : "unbound" }
1593 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n"
1594 " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n"
1595 " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n"
1596 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n"
1599 " int ret_val = (*this)[i].JSON_encode(*p_td.oftype_descr, p_tok);\n"
1600 " if (0 > ret_val) break;\n"
1601 " enc_len += ret_val;\n"
1604 " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n"
1605 " return enc_len;\n"
1610 src
= mputprintf(src
,
1611 "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
1613 " json_token_t token = JSON_TOKEN_NONE;\n"
1614 " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
1615 " if (JSON_TOKEN_ERROR == token) {\n"
1616 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n"
1617 " return JSON_ERROR_FATAL;\n"
1619 " else if (JSON_TOKEN_ARRAY_START != token) {\n"
1620 " return JSON_ERROR_INVALID_TOKEN;\n"
1623 " for (int nof_elements = 0; true; ++nof_elements) {\n"
1624 " size_t buf_pos = p_tok.get_buf_pos();\n"
1626 " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n"
1627 // check for metainfo object
1628 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
1629 " if (JSON_TOKEN_OBJECT_START == token) {\n"
1630 " char* value = NULL;\n"
1631 " size_t value_len = 0;\n"
1632 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
1633 " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n"
1634 " 0 == strncmp(value, \"metainfo []\", 11)) {\n"
1635 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
1636 " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n"
1637 " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n"
1638 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
1639 " if (JSON_TOKEN_OBJECT_END == token) {\n"
1640 " dec_len += ret_val;\n"
1646 // metainfo object not found, jump back and let the element type decode it
1647 " p_tok.set_buf_pos(buf_pos);\n"
1649 " %s* val = new %s;\n"
1650 " ret_val = val->JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n"
1651 " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n"
1652 " p_tok.set_buf_pos(buf_pos);\n"
1656 " else if (JSON_ERROR_FATAL == ret_val) {\n"
1658 " if (p_silent) {\n"
1661 " return JSON_ERROR_FATAL;\n"
1663 " val_ptr->value_elements = (%s**)reallocate_pointers(\n"
1664 " (void**)val_ptr->value_elements, val_ptr->n_elements, nof_elements + 1);\n"
1665 " val_ptr->value_elements[nof_elements] = val;\n"
1666 " val_ptr->n_elements = nof_elements + 1;\n"
1667 " dec_len += ret_val;\n"
1669 " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n"
1670 " if (JSON_TOKEN_ARRAY_END != token) {\n"
1671 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n"
1672 " if (p_silent) {\n"
1675 " return JSON_ERROR_FATAL;\n"
1677 " return dec_len;\n"
1679 , name
, type
, type
, type
);
1682 def
= mputstr(def
, "};\n\n");
1684 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
1685 "class %s;\n", name
);
1686 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
1688 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
1690 /* Copied from record.c. */
1691 output
->header
.function_prototypes
=
1692 mputprintf(output
->header
.function_prototypes
,
1693 "extern boolean operator==(null_type null_value, const %s& "
1694 "other_value);\n", name
);
1695 output
->source
.function_bodies
=
1696 mputprintf(output
->source
.function_bodies
,
1697 "boolean operator==(null_type, const %s& other_value)\n"
1699 "if (other_value.val_ptr == NULL)\n"
1700 "TTCN_error(\"The right operand of comparison is an unbound value of "
1702 "return other_value.val_ptr->n_elements == 0;\n"
1703 "}\n\n", name
, dispname
);
1705 output
->header
.function_prototypes
=
1706 mputprintf(output
->header
.function_prototypes
,
1707 "inline boolean operator!=(null_type null_value, const %s& "
1709 "{ return !(null_value == other_value); }\n", name
);
1712 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1714 void defRecordOfClassMemAllocOptimized(const struct_of_def
*sdef
, output_struct
*output
)
1716 char *def
= NULL
, *src
= NULL
;
1717 const char *name
= sdef
->name
, *dispname
= sdef
->dispname
;
1718 const char *type
= sdef
->type
;
1719 boolean ber_needed
= force_gen_seof
|| (sdef
->isASN1
&& enable_ber());
1720 boolean raw_needed
= force_gen_seof
|| (sdef
->hasRaw
&& enable_raw());
1721 boolean text_needed
= force_gen_seof
|| (sdef
->hasText
&& enable_text());
1722 boolean xer_needed
= force_gen_seof
|| (sdef
->hasXer
&& enable_xer());
1723 boolean json_needed
= force_gen_seof
|| (sdef
->hasJson
&& enable_json());
1725 /* Class definition and private data members */
1726 def
= mputprintf(def
,
1728 "// written by %s in " __FILE__
" at %d\n"
1730 "class %s : public Base_Type {\n"
1732 "%s* value_elements;\n"
1734 , __FUNCTION__
, __LINE__
1738 /* private member functions */
1739 def
= mputprintf(def
,
1741 "friend boolean operator==(null_type null_value, "
1742 "const %s& other_value);\n", name
);
1744 def
= mputprintf(def
,
1745 "void copy_value(const %s& other_value);\n", name
);
1746 src
= mputprintf(src
,
1747 "void %s::copy_value(const %s& other_value)\n"
1749 "if (other_value.n_elements==-1) {\n"
1750 "TTCN_error(\"Copying an unbound value of type %s.\");\n"
1751 "} else if (other_value.n_elements==0) {\n"
1753 "value_elements = NULL;\n"
1755 "n_elements = other_value.n_elements;\n"
1756 "value_elements = new %s[n_elements];\n"
1757 "for (int act_elem=0; act_elem<n_elements; act_elem++) {\n"
1758 " if (other_value.value_elements[act_elem].is_bound()) {\n"
1759 " value_elements[act_elem] = other_value.value_elements[act_elem];\n"
1764 name
, name
, dispname
, type
);
1766 if (sdef
->kind
== SET_OF
) {
1767 /* callback function for comparison */
1768 def
= mputstr(def
, "static boolean compare_function("
1769 "const Base_Type *left_ptr, int left_index, "
1770 "const Base_Type *right_ptr, int right_index);\n");
1771 src
= mputprintf(src
, "boolean %s::compare_function("
1772 "const Base_Type *left_ptr, int left_index, "
1773 "const Base_Type *right_ptr, int right_index)\n"
1775 "if (((const %s*)left_ptr)->n_elements==-1) "
1776 "TTCN_error(\"The left operand of comparison is an unbound value of "
1778 "if (((const %s*)right_ptr)->n_elements==-1) "
1779 "TTCN_error(\"The right operand of comparison is an unbound value of "
1781 "if (((const %s*)left_ptr)->value_elements[left_index].is_bound()){\n"
1782 "if (((const %s*)right_ptr)->value_elements[right_index].is_bound()){\n"
1783 "return ((const %s*)left_ptr)->value_elements[left_index] == "
1784 "((const %s*)right_ptr)->value_elements[right_index];\n"
1785 "} else return FALSE;\n"
1787 "return !((const %s*)right_ptr)->value_elements[right_index].is_bound();\n"
1789 "}\n\n", name
, name
, dispname
, name
, dispname
, name
, name
, name
, name
, name
);
1792 /* public member functions */
1793 def
= mputstr(def
, "\npublic:\n");
1794 def
= mputprintf(def
, " typedef %s of_type;\n", sdef
->type
);
1797 def
= mputprintf(def
, "%s(): n_elements(-1), value_elements(NULL) {}\n", name
);
1799 def
= mputprintf(def
, "%s(null_type): n_elements(0), value_elements(NULL) {}\n", name
);
1801 /* copy constructor */
1802 def
= mputprintf(def
, "%s(const %s& other_value) { copy_value(other_value); }\n", name
, name
);
1805 def
= mputprintf(def
, "~%s() { clean_up(); }\n\n", name
);
1807 /* clean_up function */
1808 def
= mputstr(def
, "void clean_up();\n");
1809 src
= mputprintf(src
,
1810 "void %s::clean_up()\n"
1812 "if (n_elements!=-1) {\n"
1813 "delete[] value_elements;\n"
1814 "n_elements = -1;\n"
1815 "value_elements = NULL;\n"
1819 /* assignment operators */
1820 def
= mputprintf(def
, "%s& operator=(null_type other_value);\n", name
);
1821 src
= mputprintf(src
,
1822 "%s& %s::operator=(null_type)\n"
1826 "value_elements=NULL;\n"
1828 "}\n\n", name
, name
);
1830 def
= mputprintf(def
, "%s& operator=(const %s& other_value);\n\n",
1832 src
= mputprintf(src
,
1833 "%s& %s::operator=(const %s& other_value)\n"
1835 "if (other_value.n_elements == -1) "
1836 "TTCN_error(\"Assigning an unbound value of type %s.\");\n"
1837 "if (this != &other_value) {\n"
1839 "copy_value(other_value);\n"
1842 "}\n\n", name
, name
, name
, dispname
);
1844 /* comparison operators */
1845 def
= mputstr(def
, "boolean operator==(null_type other_value) const;\n");
1846 src
= mputprintf(src
,
1847 "boolean %s::operator==(null_type) const\n"
1849 "if (n_elements==-1)\n"
1850 "TTCN_error(\"The left operand of comparison is an unbound value of "
1852 "return n_elements==0;\n"
1853 "}\n\n", name
, dispname
);
1855 def
= mputprintf(def
, "boolean operator==(const %s& other_value) const;\n",
1857 src
= mputprintf(src
,
1858 "boolean %s::operator==(const %s& other_value) const\n"
1860 "if (n_elements==-1) "
1861 "TTCN_error(\"The left operand of comparison is an unbound value of type "
1863 "if (other_value.n_elements==-1) "
1864 "TTCN_error(\"The right operand of comparison is an unbound value of type "
1866 "if (this==&other_value) return TRUE;\n", name
, name
,
1867 dispname
, dispname
);
1869 if (sdef
->kind
== SET_OF
) {
1871 "return compare_set_of(this, n_elements, &other_value, "
1872 "other_value.n_elements, compare_function);\n");
1875 "if (n_elements!=other_value.n_elements) return FALSE;\n"
1876 "for (int elem_count = 0; elem_count < n_elements; elem_count++){\n"
1877 "if (value_elements[elem_count].is_bound()){\n"
1878 "if (other_value.value_elements[elem_count].is_bound()){\n"
1879 " if (value_elements[elem_count] != "
1880 "other_value.value_elements[elem_count]) return FALSE;\n"
1881 "} else return FALSE;\n"
1883 "if (other_value.value_elements[elem_count].is_bound()) "
1889 src
= mputstr(src
, "}\n\n");
1891 def
= mputstr(def
, "inline boolean operator!=(null_type other_value) const "
1892 "{ return !(*this == other_value); }\n");
1893 def
= mputprintf(def
, "inline boolean operator!=(const %s& other_value) "
1894 "const { return !(*this == other_value); }\n\n", name
);
1896 /* indexing operators */
1897 /* Non-const operator[] is allowed to extend the record-of */
1898 def
= mputprintf(def
, "%s& operator[](int index_value);\n", type
);
1899 src
= mputprintf(src
,
1900 "%s& %s::operator[](int index_value)\n"
1902 "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
1903 "using a negative index: %%d.\", index_value);\n"
1904 "if (index_value >= n_elements) set_size(index_value + 1);\n"
1905 "return value_elements[index_value];\n"
1906 "}\n\n", type
, name
, dispname
);
1908 def
= mputprintf(def
, "%s& operator[](const INTEGER& index_value);\n",
1910 src
= mputprintf(src
,
1911 "%s& %s::operator[](const INTEGER& index_value)\n"
1913 "index_value.must_bound(\"Using an unbound integer value for indexing "
1914 "a value of type %s.\");\n"
1915 "return (*this)[(int)index_value];\n"
1916 "}\n\n", type
, name
, dispname
);
1918 /* Const operator[] throws an error if over-indexing */
1919 def
= mputprintf(def
, "const %s& operator[](int index_value) const;\n",
1921 src
= mputprintf(src
,
1922 "const %s& %s::operator[](int index_value) const\n"
1924 "if (n_elements==-1) TTCN_error(\"Accessing an element in an unbound "
1925 "value of type %s.\");\n"
1926 "if (index_value<0) TTCN_error(\"Accessing an element of type %s "
1927 "using a negative index: %%d.\", index_value);\n"
1928 "if (index_value>=n_elements) TTCN_error(\"Index overflow in a value "
1929 "of type %s: The index is %%d, but the value has only %%d elements.\""
1930 ", index_value, n_elements);\n"
1931 "return value_elements[index_value];\n"
1932 "}\n\n", type
, name
, dispname
, dispname
, dispname
);
1934 def
= mputprintf(def
, "const %s& operator[](const INTEGER& index_value) "
1935 "const;\n\n", type
);
1936 src
= mputprintf(src
,
1937 "const %s& %s::operator[](const INTEGER& index_value) const\n"
1939 "index_value.must_bound(\"Using an unbound integer value for indexing "
1940 "a value of type %s.\");\n"
1941 "return (*this)[(int)index_value];\n"
1942 "}\n\n", type
, name
, dispname
);
1944 /* rotation operators */
1945 def
= mputprintf(def
,
1946 "%s operator<<=(int rotate_count) const;\n"
1947 "%s operator<<=(const INTEGER& rotate_count) const;\n"
1948 "%s operator>>=(int rotate_count) const;\n"
1949 "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
1950 name
, name
, name
, name
);
1951 src
= mputprintf(src
,
1952 "%s %s::operator<<=(int rotate_count) const\n"
1954 "return *this >>= (-rotate_count);\n"
1956 "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
1958 "rotate_count.must_bound(\""
1959 "Unbound integer operand of rotate left operator.\");\n"
1960 "return *this >>= (int)(-rotate_count);\n"
1962 "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
1964 "rotate_count.must_bound(\""
1965 "Unbound integer operand of rotate right operator.\");\n"
1966 "return *this >>= (int)rotate_count;\n"
1968 "%s %s::operator>>=(int rotate_count) const\n"
1970 "if (n_elements==-1) TTCN_error(\"Performing rotation operation on an "
1971 "unbound value of type %s.\");\n"
1972 "if (n_elements==0) return *this;\n"
1974 "if (rotate_count>=0) rc = rotate_count %% n_elements;\n"
1975 "else rc = n_elements - ((-rotate_count) %% n_elements);\n"
1976 "if (rc == 0) return *this;\n"
1978 "ret_val.set_size(n_elements);\n"
1979 "for (int i=0; i<n_elements; i++) {\n"
1980 "if (value_elements[i].is_bound()) "
1981 "ret_val.value_elements[(i+rc)%%n_elements] = value_elements[i];\n"
1985 name
, name
, name
, name
, name
, name
, name
, name
, dispname
, name
);
1988 def
= mputprintf(def
,
1989 "%s operator+(const %s& other_value) const;\n\n", name
, name
);
1990 src
= mputprintf(src
,
1991 "%s %s::operator+(const %s& other_value) const\n"
1993 "if (n_elements==-1 || other_value.n_elements==-1) "
1994 "TTCN_error(\"Unbound operand of %s concatenation.\");\n"
1995 "if (n_elements==0) return other_value;\n"
1996 "if (other_value.n_elements==0) return *this;\n"
1998 "ret_val.set_size(n_elements+other_value.n_elements);\n"
1999 "for (int i=0; i<n_elements; i++) {\n"
2000 "if (value_elements[i].is_bound()) "
2001 "ret_val.value_elements[i] = value_elements[i];\n"
2003 "for (int i=0; i<other_value.n_elements; i++) {\n"
2004 "if (other_value.value_elements[i].is_bound()) "
2005 "ret_val.value_elements[i+n_elements] = other_value.value_elements[i];\n"
2008 "}\n\n", name
, name
, name
, dispname
, name
);
2011 def
= mputprintf(def
,
2012 "%s substr(int index, int returncount) const;\n\n", name
);
2013 src
= mputprintf(src
,
2014 "%s %s::substr(int index, int returncount) const\n"
2016 "if (n_elements==-1) TTCN_error(\"The first argument of substr() is an "
2017 "unbound value of type %s.\");\n"
2018 "check_substr_arguments(n_elements, index, returncount, \"%s\",\"element\");\n"
2020 "ret_val.set_size(returncount);\n"
2021 "for (int i=0; i<returncount; i++) {\n"
2022 "if (value_elements[i+index].is_bound()) "
2023 "ret_val.value_elements[i] = value_elements[i+index];\n"
2026 "}\n\n", name
, name
, dispname
, dispname
, name
);
2029 def
= mputprintf(def
,
2030 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
2031 src
= mputprintf(src
,
2032 "%s %s::replace(int index, int len, const %s& repl) const\n"
2034 "if (n_elements==-1) TTCN_error(\"The first argument of replace() is an "
2035 "unbound value of type %s.\");\n"
2036 "if (repl.n_elements==-1) TTCN_error(\"The fourth argument of replace() "
2037 "is an unbound value of type %s.\");\n"
2038 "check_replace_arguments(n_elements, index, len, \"%s\",\"element\");\n"
2040 "ret_val.set_size(n_elements + repl.n_elements - len);\n"
2041 "for (int i = 0; i < index; i++) {\n"
2042 "if (value_elements[i].is_bound()) "
2043 "ret_val.value_elements[i] = value_elements[i];\n"
2045 "for (int i = 0; i < repl.n_elements; i++) {\n"
2046 "if (repl.value_elements[i].is_bound()) "
2047 "ret_val.value_elements[i+index] = repl.value_elements[i];\n"
2049 "for (int i = 0; i < n_elements - index - len; i++) {\n"
2050 "if (value_elements[index+i+len].is_bound()) "
2051 "ret_val.value_elements[index+i+repl.n_elements] = value_elements[index+i+len];\n"
2054 "}\n\n", name
, name
, name
, dispname
, dispname
, dispname
, name
);
2055 def
= mputprintf(def
,
2056 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
2058 src
= mputprintf(src
,
2059 "%s %s::replace(int index, int len, const %s_template& repl) const\n"
2061 "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
2062 "replace() is a template with non-specific value.\");\n"
2063 "return replace(index, len, repl.valueof());\n"
2064 "}\n\n", name
, name
, name
);
2066 /* set_size function */
2067 def
= mputstr(def
, "void set_size(int new_size);\n");
2068 src
= mputprintf(src
, "void %s::set_size(int new_size)\n"
2070 "if (new_size<0) TTCN_error(\"Internal error: Setting a negative size for "
2071 "a value of type %s.\");\n"
2072 "if (new_size==n_elements) return;\n"
2073 "if (new_size==0) {\n"
2075 " n_elements = 0;\n"
2076 " value_elements = NULL;\n"
2079 "%s* new_elem_v = new %s[new_size];\n"
2080 "for (int act_elem = 0; act_elem<n_elements; act_elem++) {\n"
2081 " if (act_elem>=new_size) break;\n"
2082 " if (value_elements[act_elem].is_bound()) new_elem_v[act_elem] = value_elements[act_elem];\n"
2085 "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n"
2086 "if((n_elements/1000)!=(new_size/1000)) "
2087 "TTCN_warning(\"New size of type %s: %%d\",new_size);\n"
2089 "n_elements = new_size;\n"
2090 "value_elements = new_elem_v;\n"
2091 "}\n\n", name
, dispname
, type
, type
, dispname
);
2093 /* is_bound function */
2095 "inline boolean is_bound() const {return n_elements!=-1; }\n");
2097 /* is_present function */
2099 "inline boolean is_present() const { return is_bound(); }\n");
2101 /* is_value function */
2103 "boolean is_value() const;\n");
2104 src
= mputprintf(src
,
2105 "boolean %s::is_value() const\n"
2107 "if (n_elements==-1) return FALSE;\n"
2108 "for (int i = 0; i < n_elements; ++i) {\n"
2109 " if (!value_elements[i].is_value()) return FALSE;\n"
2114 /* sizeof operation */
2116 "int size_of() const;\n"
2117 "int n_elem() const { return size_of(); }\n");
2118 src
= mputprintf(src
,
2119 "int %s::size_of() const\n"
2121 "if (n_elements==-1) TTCN_error(\"Performing sizeof operation on an "
2122 "unbound value of type %s.\");\n"
2123 "return n_elements;\n"
2124 "}\n\n", name
, dispname
);
2126 /* lengthof operation */
2127 def
= mputstr(def
, "int lengthof() const;\n");
2128 src
= mputprintf(src
,
2129 "int %s::lengthof() const\n"
2131 "if (n_elements==-1) TTCN_error(\"Performing lengthof operation on an "
2132 "unbound value of type %s.\");\n"
2133 "for (int my_length=n_elements; my_length>0; my_length--) "
2134 "if (value_elements[my_length-1].is_bound()) return my_length;\n"
2136 "}\n\n", name
, dispname
);
2139 def
= mputstr(def
, "void log() const;\n");
2140 src
= mputprintf(src
,
2141 "void %s::log() const\n"
2143 "if (n_elements==-1) {;\n"
2144 "TTCN_Logger::log_event_unbound();\n"
2147 "switch (n_elements) {\n"
2149 "TTCN_Logger::log_event_str(\"{ }\");\n"
2152 "TTCN_Logger::log_event_str(\"{ \");\n"
2153 "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n"
2154 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
2155 "value_elements[elem_count].log();\n"
2157 "TTCN_Logger::log_event_str(\" }\");\n"
2161 /* set_param function */ /* this is an exact copy of the previous one in this source file, if we didn't forget... */
2162 def
= mputstr(def
, "void set_param(Module_Param& param);\n");
2163 src
= mputprintf(src
,
2164 "void %s::set_param(Module_Param& param)\n"
2166 " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n"
2167 " param.get_id()->next_name()) {\n"
2168 // Haven't reached the end of the module parameter name
2169 // => the name refers to one of the elements, not to the whole record of
2170 " char* param_field = param.get_id()->get_current_name();\n"
2171 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
2172 " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n"
2173 " \" index for %s type `%s'\");\n"
2175 " int param_index = -1;\n"
2176 " sscanf(param_field, \"%%d\", ¶m_index);\n"
2177 " (*this)[param_index].set_param(param);\n"
2180 " param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n"
2181 " Module_Param_Ptr mp = ¶m;\n"
2182 " if (param.get_type() == Module_Param::MP_Reference) {\n"
2183 " mp = param.get_referenced_param();\n"
2185 " switch (param.get_operation_type()) {\n"
2186 " case Module_Param::OT_ASSIGN:\n"
2187 " if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {\n"
2188 " *this = NULL_VALUE;\n"
2191 " switch (mp->get_type()) {\n"
2192 " case Module_Param::MP_Value_List:\n"
2193 " set_size(mp->get_size());\n"
2194 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2195 " Module_Param* const curr = mp->get_elem(i);\n"
2196 " if (curr->get_type()!=Module_Param::MP_NotUsed) {\n"
2197 " (*this)[i].set_param(*curr);\n"
2201 " case Module_Param::MP_Indexed_List:\n"
2202 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2203 " Module_Param* const curr = mp->get_elem(i);\n"
2204 " (*this)[curr->get_id()->get_index()].set_param(*curr);\n"
2208 " param.type_error(\"%s value\", \"%s\");\n"
2211 " case Module_Param::OT_CONCAT:\n"
2212 " switch (mp->get_type()) {\n"
2213 " case Module_Param::MP_Value_List: {\n"
2214 " if (!is_bound()) *this = NULL_VALUE;\n"
2215 " int start_idx = lengthof();\n"
2216 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2217 " Module_Param* const curr = mp->get_elem(i);\n"
2218 " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n"
2219 " (*this)[start_idx+(int)i].set_param(*curr);\n"
2223 " case Module_Param::MP_Indexed_List:\n"
2224 " param.error(\"Cannot concatenate an indexed value list\");\n"
2227 " param.type_error(\"%s value\", \"%s\");\n"
2231 " TTCN_error(\"Internal error: Unknown operation type.\");\n"
2233 "}\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
2234 sdef
->kind
== RECORD_OF
? "record of" : "set of",
2235 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
2236 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
2238 /* get param function */
2239 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
2242 "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n"
2244 " if (!is_bound()) {\n"
2245 " return new Module_Param_Unbound();\n"
2247 " if (param_name.next_name()) {\n"
2248 // Haven't reached the end of the module parameter name
2249 // => the name refers to one of the elements, not to the whole record of
2250 " char* param_field = param_name.get_current_name();\n"
2251 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
2252 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
2253 " \"expected a valid index for %s type `%s'\");\n"
2255 " int param_index = -1;\n"
2256 " sscanf(param_field, \"%%d\", ¶m_index);\n"
2257 " return (*this)[param_index].get_param(param_name);\n"
2259 " Vector<Module_Param*> values;\n"
2260 " for (int i = 0; i < n_elements; ++i) {\n"
2261 " values.push_back((*this)[i].get_param(param_name));\n"
2263 " Module_Param_Value_List* mp = new Module_Param_Value_List();\n"
2264 " mp->add_list_with_implicit_ids(&values);\n"
2265 " values.clear();\n"
2267 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
2269 /* encoding / decoding functions */
2270 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
2271 src
= mputprintf(src
,
2272 "void %s::encode_text(Text_Buf& text_buf) const\n"
2274 "if (n_elements==-1) "
2275 "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n"
2276 "text_buf.push_int(n_elements);\n"
2277 "for (int elem_count = 0; elem_count < n_elements; elem_count++)\n"
2278 "value_elements[elem_count].encode_text(text_buf);\n"
2279 "}\n\n", name
, dispname
);
2281 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
2282 src
= mputprintf(src
,
2283 "void %s::decode_text(Text_Buf& text_buf)\n"
2286 "n_elements = text_buf.pull_int().get_val();\n"
2287 "if (n_elements < 0) TTCN_error(\"Text decoder: Negative size "
2288 "was received for a value of type %s.\");\n"
2289 "if (n_elements==0) {\n"
2290 " value_elements = NULL;\n"
2293 "value_elements = new %s[n_elements];\n"
2294 "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n"
2295 " value_elements[elem_count].decode_text(text_buf);\n"
2297 "}\n\n", name
, dispname
, type
);
2299 if(ber_needed
|| raw_needed
|| text_needed
|| xer_needed
|| json_needed
)
2300 def_encdec(name
, &def
, &src
, ber_needed
, raw_needed
, text_needed
,
2301 xer_needed
, json_needed
, FALSE
);
2305 "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td,"
2306 " TTCN_Buffer& p_buf) const{\n"
2307 " int encoded_length=0;\n"
2308 " if(p_td.text->begin_encode){\n"
2309 " p_buf.put_cs(*p_td.text->begin_encode);\n"
2310 " encoded_length+=p_td.text->begin_encode->lengthof();\n"
2312 " if(n_elements==-1) {\n"
2313 " TTCN_EncDec_ErrorContext::error\n"
2314 " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
2315 " if(p_td.text->end_encode){\n"
2316 " p_buf.put_cs(*p_td.text->end_encode);\n"
2317 " encoded_length+=p_td.text->end_encode->lengthof();\n"
2319 " return encoded_length;\n"
2321 " for(int a=0;a<n_elements;a++){\n"
2322 " if(a!=0 && p_td.text->separator_encode){\n"
2323 " p_buf.put_cs(*p_td.text->separator_encode);\n"
2324 " encoded_length+=p_td.text->separator_encode->lengthof();\n"
2326 " encoded_length+=value_elements[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n"
2328 " if(p_td.text->end_encode){\n"
2329 " p_buf.put_cs(*p_td.text->end_encode);\n"
2330 " encoded_length+=p_td.text->end_encode->lengthof();\n"
2332 " return encoded_length;\n"
2336 src
= mputprintf(src
,
2337 "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td,"
2338 " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err"
2339 ", boolean first_call){\n"
2340 " int decoded_length=0;\n"
2341 " size_t pos=p_buf.get_pos();\n"
2342 " boolean sep_found=FALSE;\n"
2343 " int sep_length=0;\n"
2345 " if(p_td.text->begin_decode){\n"
2347 " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n"
2348 " if(no_err)return -1;\n"
2349 " TTCN_EncDec_ErrorContext::error\n"
2350 " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'"
2351 " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)"
2355 " decoded_length+=tl;\n"
2356 " p_buf.increase_pos(tl);\n"
2358 " if(p_td.text->end_decode){\n"
2359 " limit.add_token(p_td.text->end_decode);\n"
2362 " if(p_td.text->separator_decode){\n"
2363 " limit.add_token(p_td.text->separator_decode);\n"
2366 " if(first_call) {\n"
2369 " int more=n_elements;\n"
2372 " pos=p_buf.get_pos();\n"
2373 " int len=val.TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n"
2374 " if(len==-1 || (len==0 && !limit.has_token())){\n"
2375 " p_buf.set_pos(pos);\n"
2377 " p_buf.set_pos(p_buf.get_pos()-sep_length);\n"
2378 " decoded_length-=sep_length;\n"
2382 " sep_found=FALSE;\n"
2383 " set_size(n_elements+1);\n"
2384 " value_elements[n_elements-1]=val;\n"
2385 " decoded_length+=len;\n"
2386 " if(p_td.text->separator_decode){\n"
2388 " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n"
2391 " decoded_length+=tl;\n"
2392 " p_buf.increase_pos(tl);\n"
2394 " sep_found=TRUE;\n"
2395 " } else if(p_td.text->end_decode){\n"
2397 " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n"
2398 " decoded_length+=tl;\n"
2399 " p_buf.increase_pos(tl);\n"
2400 " limit.remove_tokens(ml);\n"
2401 " return decoded_length;\n"
2403 " } else if(limit.has_token(ml)){\n"
2405 " if((tl=limit.match(p_buf,ml))==0){\n"
2406 " sep_found=FALSE;\n"
2414 " limit.remove_tokens(ml);\n"
2415 " if(p_td.text->end_decode){\n"
2417 " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n"
2419 " if(!first_call){\n"
2420 " set_size(more);\n"
2424 " TTCN_EncDec_ErrorContext::error"
2425 "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'"
2426 " not found for '%s': \",(const char*)*(p_td.text->end_decode)"
2428 " return decoded_length;\n"
2430 " decoded_length+=tl;\n"
2431 " p_buf.increase_pos(tl);\n"
2433 " if(n_elements==0){\n"
2434 " if(p_td.text->end_decode || p_td.text->begin_decode) n_elements=0;\n"
2436 " if(no_err)return -1;\n"
2437 " TTCN_EncDec_ErrorContext::error"
2438 "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n"
2439 " return decoded_length;\n"
2442 " if(!first_call && more==n_elements && "
2443 "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n"
2444 " return decoded_length;\n"
2451 /* BER_encode_TLV() */
2454 "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&"
2455 " p_td, unsigned p_coding) const\n"
2457 " BER_chk_descr(p_td);\n"
2458 " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n"
2460 " new_tlv=ASN_BER_TLV_t::construct(NULL);\n"
2461 " TTCN_EncDec_ErrorContext ec;\n"
2462 " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n"
2463 " ec.set_msg(\"Component #%%d: \", elem_i);\n"
2464 " new_tlv->add_TLV(value_elements[elem_i].BER_encode_TLV"
2465 "(*p_td.oftype_descr, p_coding));\n"
2469 " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n"
2470 " return new_tlv;\n"
2473 /* BER_decode_TLV() */
2474 "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,"
2475 " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n"
2477 " BER_chk_descr(p_td);\n"
2478 " ASN_BER_TLV_t stripped_tlv;\n"
2479 " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n"
2480 " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \","
2482 " stripped_tlv.chk_constructed_flag(TRUE);\n"
2484 " size_t V_pos=0;\n"
2485 " ASN_BER_TLV_t tmp_tlv;\n"
2486 " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n"
2487 " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n"
2488 " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, "
2490 " set_size(n_elements+1);\n"
2491 " value_elements[n_elements-1].BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, "
2493 " ec_2.set_msg(\"%%d: \", n_elements);\n"
2499 , sdef
->kind
==SET_OF
?" new_tlv->sort_tlvs();\n":""
2503 if(sdef
->has_opentypes
) {
2504 /* BER_decode_opentypes() */
2507 "void BER_decode_opentypes(TTCN_Type_list& p_typelist,"
2508 " unsigned L_form);\n");
2511 "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist,"
2512 " unsigned L_form)\n"
2514 " p_typelist.push(this);\n"
2515 " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n"
2516 " TTCN_EncDec_ErrorContext ec_1;\n"
2517 " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n"
2518 " ec_1.set_msg(\"%%d: \", elem_i);\n"
2519 " value_elements[elem_i].BER_decode_opentypes(p_typelist,"
2522 " p_typelist.pop();\n"
2532 "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, "
2533 "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field"
2534 ", boolean first_call){\n"
2535 " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
2536 " limit-=prepaddlength;\n"
2537 " int decoded_length=0;\n"
2538 " int decoded_field_length=0;\n"
2539 " size_t start_of_field=0;\n"
2540 " if (first_call) set_size(0);\n"
2541 " int start_field=n_elements;\n"
2542 " if(p_td.raw->fieldlength || sel_field!=-1){\n"
2544 " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
2545 " for(a=0;a<sel_field;a++){\n"
2546 " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
2547 "p_buf,limit,top_bit_ord,TRUE);\n"
2548 " if(decoded_field_length < 0) return decoded_field_length;\n"
2549 " decoded_length+=decoded_field_length;\n"
2550 " limit-=decoded_field_length;\n"
2552 " if(a==0) n_elements=0;\n"
2554 " int a=start_field;\n"
2556 " if(!first_call) return -1;\n"
2558 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2561 " while(limit>0){\n"
2562 " start_of_field=p_buf.get_pos_bit();\n"
2563 " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit,"
2564 "top_bit_ord,TRUE);\n"
2565 " if(decoded_field_length < 0){\n"
2566 /*" delete &(*this)[a];\n"*/
2568 " p_buf.set_pos_bit(start_of_field);\n"
2569 " if(a>start_field){\n"
2570 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2572 " } else return -1;\n"
2574 " decoded_length+=decoded_field_length;\n"
2575 " limit-=decoded_field_length;\n"
2579 if(sdef
->raw
.extension_bit
!=XDEFNO
&& sdef
->raw
.extension_bit
!=XDEFDEFAULT
){
2581 " if (%sp_buf.get_last_bit())\n"
2582 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2583 "+prepaddlength;\n", sdef
->raw
.extension_bit
== XDEFYES
? "" : "!");
2588 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2591 "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td,"
2592 "RAW_enc_tree& myleaf) const{\n"
2593 " int encoded_length=0;\n"
2594 " int encoded_num_of_records=p_td.raw->fieldlength?"
2595 "smaller(n_elements, p_td.raw->fieldlength):n_elements;\n"
2596 " myleaf.isleaf=FALSE;\n"
2597 " myleaf.rec_of=TRUE;\n"
2598 " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n"
2599 " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n"
2600 " for(int a=0;a<encoded_num_of_records;a++){\n"
2601 " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf,"
2602 "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n"
2603 " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr,"
2604 "*myleaf.body.node.nodes[a]);\n"
2606 " return myleaf.length=encoded_length;\n}\n\n"
2611 if (xer_needed
) { /* XERSTUFF encoder codegen for record-of, RT1 */
2613 "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n");
2615 /* Write the body of the XER encoder/decoder functions. The declaration
2616 * is written by def_encdec() in encdec.c */
2617 src
= mputprintf(src
,
2618 "boolean %s::can_start(const char *name, const char *uri, "
2619 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
2620 " boolean e_xer = is_exer(flavor);\n"
2621 " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return "
2622 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
2623 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
2626 if (!force_gen_seof
&& sdef
->nFollowers
) {
2627 /* If there are optional fields following the record-of, then seeing
2628 * {any XML tag that belongs to those fields} where the record-of may be
2629 * means that the record-of is empty. */
2631 src
= mputstr(src
, "{\n");
2632 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
2633 src
= mputprintf(src
,
2634 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
2635 , sdef
->followers
[f
].type
2636 , sdef
->followers
[f
].typegen
2643 else src
= mputstr(src
, "return TRUE;\n");
2645 src
= mputprintf(src
,
2646 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
2651 src
= mputprintf(src
,
2652 "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n"
2653 " size_t num_collected;\n"
2654 " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n"
2655 /* The above may throw but then nothing was allocated. */
2656 " if (n_elements!=-1) try {\n"
2658 " size_t num_new;\n"
2659 " for (int i = 0; i < n_elements; ++i) {\n"
2660 " bool def_ns_1 = false;"
2661 " new_ns = value_elements[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n"
2662 " merge_ns(collected_ns, num_collected, new_ns, num_new);\n"
2663 " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */
2667 /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g.
2668 * encoding an unbound value. */
2669 " while (num_collected > 0) Free(collected_ns[--num_collected]);\n"
2670 " Free(collected_ns);\n"
2673 " num = num_collected;\n"
2674 " return collected_ns;\n"
2679 "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, "
2680 "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n"
2681 " if (n_elements==-1) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */
2682 " int encoded_length=(int)p_buf.get_len();\n"
2683 " boolean e_xer = is_exer(p_flavor);\n"
2684 " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
2685 " || (p_flavor & USE_TYPE_ATTR)));\n"
2686 " boolean indenting = !is_canonical(p_flavor) && own_tag;\n"
2687 "%s" /* Factor out p_indent if not attribute */
2688 " if (n_elements==0) {\n" /* Empty record of */
2690 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2691 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
2693 if (force_gen_seof
|| sdef
->xerAttribute
) {
2695 " if (e_xer%s) {\n" /* Empty attribute. */
2696 " begin_attribute(p_td, p_buf);\n"
2697 " p_buf.put_c('\\'');\n"
2699 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2701 if (force_gen_seof
|| !sdef
->xerAttribute
) {
2706 " {\n" /* Do empty element tag */
2708 " p_buf.put_c('<');\n"
2709 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2710 " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n"
2711 /* namespace declarations for the toplevel element */
2712 " if (e_xer && p_indent==0)\n"
2714 " size_t num_collected = 0;\n"
2715 " char **collected_ns = NULL;\n"
2716 " bool def_ns = false;\n"
2717 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
2718 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
2719 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
2720 " Free(collected_ns[cur_coll]);\n"
2722 " Free(collected_ns);\n"
2725 " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n"
2728 " else {\n" /* Not empty record of. Start tag or attribute */
2729 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2730 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
2732 if (sdef
->xerAnyAttrElem
) {
2734 " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n"
2735 " static const universal_char sp = { 0,0,0,' ' };\n"
2736 " static const universal_char tb = { 0,0,0,9 };\n"
2737 " size_t buf_len = p_buf.get_len(), shorter = 0;\n"
2738 " const unsigned char * const buf_data = p_buf.get_data();\n"
2739 " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n"
2740 " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n"
2741 " unsigned char saved[4];\n"
2742 " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n"
2743 " p_buf.increase_length(-shorter);\n"
2744 " for (int i = 0; i < n_elements; ++i) {\n"
2745 " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n"
2746 " size_t len = value_elements[i].lengthof();\n"
2748 " const UNIVERSAL_CHARSTRING_ELEMENT& ue = value_elements[i][len - 1];\n"
2749 " if (sp == ue || tb == ue) --len;\n"
2752 " size_t j, sp_at = 0;\n"
2753 /* Find the "separators" in each string */
2754 " for (j = 0; j < len; j++) {\n"
2755 " UNIVERSAL_CHARSTRING_ELEMENT ue = value_elements[i][j];\n"
2757 " if (sp == ue || tb == ue) {}\n"
2760 " if (sp == ue || tb == ue) sp_at = j;\n"
2763 " size_t buf_start = p_buf.get_len();\n"
2764 /* Now write them */
2765 " if (sp_at > 0) {\n"
2766 " char * ns = mprintf(\" xmlns:b%d='\", i);\n"
2767 " size_t ns_len = mstrlen(ns);\n"
2768 " p_buf.put_s(ns_len, (const unsigned char*)ns);\n"
2770 " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(value_elements[i]));\n"
2771 " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
2772 // Ensure the namespace abides to its restrictions
2773 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
2774 " TTCN_Buffer ns_buf;\n"
2775 " before.encode_utf8(ns_buf);\n"
2777 " ns_buf.get_string(cs);\n"
2778 " check_namespace_restrictions(p_td, (const char*)cs);\n"
2781 " p_buf.put_c('\\'');\n"
2782 " p_buf.put_c(' ');\n"
2784 /* Keep just the "b%d" part from ns */
2785 " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n"
2786 " p_buf.put_c(':');\n"
2790 " p_buf.put_c(' ');\n"
2792 // Make sure the unqualified namespace is allowed
2793 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
2794 " check_namespace_restrictions(p_td, NULL);\n"
2798 " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(value_elements[i]) + j);\n"
2799 " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
2800 // Put this attribute in a dummy element and walk through it to check its validity
2801 " TTCN_Buffer check_buf;\n"
2802 " check_buf.put_s(2, (unsigned char*)\"<a\");\n"
2803 " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n"
2804 " check_buf.put_s(2, (unsigned char*)\"/>\");"
2805 " XmlReaderWrap checker(check_buf);\n"
2806 " while (1 == checker.Read()) ;\n"
2809 " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */
2812 if (force_gen_seof
|| sdef
->xerAttribute
) {
2815 " begin_attribute(p_td, p_buf);\n"
2817 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2822 " p_buf.put_c('<');\n"
2823 " boolean write_ns = (e_xer && p_indent==0);\n"
2824 " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n"
2825 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2826 " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), "
2827 "(const unsigned char*)p_td.names[e_xer]);\n"
2829 /* namespace declarations for the toplevel element */
2830 " if (e_xer && p_indent==0)\n"
2832 " size_t num_collected = 0;\n"
2833 " char **collected_ns = NULL;\n"
2834 " bool def_ns = false;\n"
2835 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
2836 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
2837 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
2838 " Free(collected_ns[cur_coll]);\n"
2840 " Free(collected_ns);\n"
2841 " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n"
2843 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2844 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
2846 if (sdef
->xmlValueList
) {
2847 src
=mputstr(src
, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */
2851 " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n"
2852 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
2853 " TTCN_EncDec_ErrorContext ec_1;\n"
2856 " for (int i = 0; i < n_elements; ++i) {\n"
2857 " if (i > 0 && !own_tag && 0 != emb_val &&\n"
2858 " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n"
2859 " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n"
2860 " if (0 != emb_val->embval_array_reg) {\n"
2861 " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n"
2862 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
2865 " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n"
2866 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
2868 " ++emb_val->embval_index;\n"
2870 " ec_1.set_msg(\"%d: \", i);\n"
2871 " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n"
2872 " value_elements[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n"
2874 " if (indenting && !is_exerlist(p_flavor)) {\n"
2876 if (sdef
->xmlValueList
) {
2877 src
=mputstr(src
, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */
2880 " do_indent(p_buf, p_indent);\n"
2882 if (force_gen_seof
|| sdef
->xerAttribute
) {
2884 " if (e_xer%s) p_buf.put_c('\\'');\n"
2886 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2890 " p_buf.put_c('<');\n"
2891 " p_buf.put_c('/');\n"
2892 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2893 " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n"
2895 if (sdef
->xerAnyAttrElem
) {
2896 src
= mputstr(src
, " }\n");
2899 " }\n" /* end if(no elements) */
2900 " return (int)p_buf.get_len() - encoded_length;\n"
2904 src
= mputprintf(src
, /* XERSTUFF decoder codegen for record-of */
2906 "// written by %s in " __FILE__
" at %d\n"
2908 "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, "
2909 "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n"
2910 " boolean e_xer = is_exer(p_flavor);\n"
2911 " int xerbits = p_td.xer_bits;\n"
2912 " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n"
2913 " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))"
2914 " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */
2915 /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */
2916 " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n"
2917 " int rd_ok=1, xml_depth=-1;\n"
2918 " *this = NULL_VALUE;\n" /* empty but initialized array */
2919 " int type = 0;\n" /* none */
2920 " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n"
2921 " type = p_reader.NodeType();\n"
2922 " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n"
2923 " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n"
2924 " || XML_READER_TYPE_ATTRIBUTE == type) {\n"
2925 " verify_name(p_reader, p_td, e_xer);\n"
2929 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n"
2930 " if (XML_READER_TYPE_TEXT == type) break;\n"
2933 " if (XML_READER_TYPE_ELEMENT == type) {\n"
2934 " verify_name(p_reader, p_td, e_xer);\n"
2935 " xml_depth = p_reader.Depth();\n"
2938 " }\n" /* endif(e_xer && list) */
2939 " }\n" /* next read */
2940 " else xml_depth = p_reader.Depth();\n"
2941 " p_flavor |= XER_RECOF;\n"
2943 , __FUNCTION__
, __LINE__
2949 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/
2950 " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */
2951 " size_t x_pos = 0;\n"
2952 " size_t x_len = strlen(x_val);\n"
2953 /* The string contains a bunch of values separated by whitespace.
2954 * Tokenize the string and create a new buffer which looks like
2955 * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>),
2956 * then use that to decode the value. */
2957 " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n"
2958 // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
2959 " x_pos += strlen(str) + 1;\n"
2960 " TTCN_Buffer buf_2;\n"
2961 " buf_2.put_c('<');\n"
2962 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
2963 " const char * const exer_name = p_td.oftype_descr->names[1];\n"
2964 " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n"
2965 /* If it has a namespace, chop off the '>' from the end */
2966 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n"
2967 " if (i_can_has_ns) {\n"
2968 " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n"
2969 " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n"
2970 " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n"
2971 " buf_2.put_s(2, (cbyte*)\"='\");\n"
2972 " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n"
2973 " buf_2.put_s(2, (cbyte*)\"'>\");\n"
2975 /* start tag completed */
2976 " buf_2.put_s(strlen(str), (cbyte*)str);\n"
2977 " buf_2.put_c('<');\n"
2978 " buf_2.put_c('/');\n"
2979 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
2980 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n"
2981 " XmlReaderWrap reader_2(buf_2);\n"
2982 " rd_ok = reader_2.Read();\n" /* Move to the start element. */
2983 /* Don't move to the #text, that's the callee's responsibility. */
2984 /* The call to the non-const operator[] creates a new element object,
2985 * then we call its XER_decode with the temporary XML reader. */
2986 " (*this)[n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n"
2987 " if (p_flavor & EXIT_ON_ERROR && !(*this)[n_elements - 1].is_bound()) {\n"
2988 " if (1 == n_elements) {\n"
2989 // Failed to decode even the first element
2992 // Some elements were successfully decoded -> only delete the last one
2993 " set_size(n_elements - 1);\n"
2995 " xmlFree(x_val);\n"
2998 " if (x_pos >= x_len) break;\n"
3000 " xmlFree(x_val);\n"
3001 " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n"
3002 /* Let the caller do AdvanceAttribute() */
3003 " else if (own_tag) {\n"
3004 " p_reader.Read();\n" /* on closing tag */
3005 " p_reader.Read();\n" /* past it */
3010 src
= mputprintf(src
,
3012 " if (p_flavor & PARENT_CLOSED) ;\n"
3013 /* Nothing to do, but do not advance past the parent's element */
3014 " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n"
3015 /* It's our XML empty element: nothing to do, skip past it */
3017 /* Note: there is no p_reader.Read() at the end of the loop below.
3018 * Each element is supposed to consume enough to leave the next element
3019 * well-positioned. */
3020 " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n"
3021 " type = p_reader.NodeType();\n"
3022 " if (XML_READER_TYPE_ELEMENT == type)\n"
3024 if (sdef
->xerAnyAttrElem
) {
3025 src
= mputprintf(src
,
3026 " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n"
3027 /* This is a record-of with ANY-ELEMENT applied, which is really meant
3028 * for the element type (a string), so behave like a record-of
3029 * (string with ANY-ELEMENT): call the non-const operator[]
3030 * to create a new element, then read the entire XML element into it. */
3031 " (*this)[n_elements] = (const char*)p_reader.ReadOuterXml();\n"
3032 /* Consume the element, then move ahead */
3033 " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n"
3034 " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n"
3039 /* An untagged record-of ends if it encounters an element with a name
3040 * that doesn't match its component */
3041 " if (!own_tag && !can_start((const char*)p_reader.LocalName(), "
3042 "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n"
3043 " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
3046 /* The call to the non-const operator[] creates the element */
3047 " operator [](n_elements).XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n"
3049 " if (0 != emb_val && !own_tag && n_elements > 1) {\n"
3050 " ++emb_val->embval_index;\n"
3053 " else if (XML_READER_TYPE_END_ELEMENT == type) {\n"
3054 " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
3056 " verify_end(p_reader, p_td, xml_depth, e_xer);\n"
3057 " rd_ok = p_reader.Read();\n" /* move forward one last time */
3061 " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && n_elements > 0) {\n"
3062 " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n"
3063 " if (0 != emb_val->embval_array_reg) {\n"
3064 " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n"
3067 " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n"
3069 " rd_ok = p_reader.Read();\n"
3072 " rd_ok = p_reader.Read();\n"
3074 " }\n" /* next read */
3075 " }\n" /* if not empty element */
3076 " }\n" /* if not LIST */
3082 // JSON encode, RT1, mem. alloc. optimised
3083 src
= mputprintf(src
,
3084 "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n"
3086 " if (!is_bound()) {\n"
3087 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
3088 " \"Encoding an unbound value of type %s.\");\n"
3091 " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n"
3092 " for (int i = 0; i < n_elements; ++i) {\n"
3093 " if (NULL != p_td.json && p_td.json->metainfo_unbound && !value_elements[i].is_bound()) {\n"
3094 // unbound elements are encoded as { "metainfo []" : "unbound" }
3095 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n"
3096 " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n"
3097 " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n"
3098 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n"
3101 " int ret_val = value_elements[i].JSON_encode(*p_td.oftype_descr, p_tok);\n"
3102 " if (0 > ret_val) break;\n"
3103 " enc_len += ret_val;\n"
3106 " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n"
3107 " return enc_len;\n"
3111 // JSON decode, RT1, mem. alloc. optimised
3112 src
= mputprintf(src
,
3113 "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
3115 " json_token_t token = JSON_TOKEN_NONE;\n"
3116 " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
3117 " if (JSON_TOKEN_ERROR == token) {\n"
3118 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n"
3119 " return JSON_ERROR_FATAL;\n"
3121 " else if (JSON_TOKEN_ARRAY_START != token) {\n"
3122 " return JSON_ERROR_INVALID_TOKEN;\n"
3125 " for (int nof_elements = 0; true; ++nof_elements) {\n"
3126 " size_t buf_pos = p_tok.get_buf_pos();\n"
3128 " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n"
3129 // check for metainfo object
3130 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
3131 " if (JSON_TOKEN_OBJECT_START == token) {\n"
3132 " char* value = NULL;\n"
3133 " size_t value_len = 0;\n"
3134 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
3135 " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n"
3136 " 0 == strncmp(value, \"metainfo []\", 11)) {\n"
3137 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
3138 " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n"
3139 " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n"
3140 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
3141 " if (JSON_TOKEN_OBJECT_END == token) {\n"
3142 " dec_len += ret_val;\n"
3148 // metainfo object not found, jump back and let the element type decode it
3149 " p_tok.set_buf_pos(buf_pos);\n"
3152 " ret_val = val.JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n"
3153 " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n"
3154 " p_tok.set_buf_pos(buf_pos);\n"
3157 " else if (JSON_ERROR_FATAL == ret_val) {\n"
3158 " if (p_silent) {\n"
3161 " return JSON_ERROR_FATAL;\n"
3163 " set_size(nof_elements + 1);\n"
3164 " value_elements[nof_elements] = val;\n"
3165 " dec_len += ret_val;\n"
3167 " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n"
3168 " if (JSON_TOKEN_ARRAY_END != token) {\n"
3169 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n"
3170 " if (p_silent) {\n"
3173 " return JSON_ERROR_FATAL;\n"
3175 " return dec_len;\n"
3180 def
= mputstr(def
, "};\n\n");
3182 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
3183 "class %s;\n", name
);
3184 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
3186 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
3188 /* Copied from record.c. */
3189 output
->header
.function_prototypes
=
3190 mputprintf(output
->header
.function_prototypes
,
3191 "extern boolean operator==(null_type null_value, const %s& "
3192 "other_value);\n", name
);
3193 output
->source
.function_bodies
=
3194 mputprintf(output
->source
.function_bodies
,
3195 "boolean operator==(null_type, const %s& other_value)\n"
3197 "if (other_value.n_elements==-1)\n"
3198 "TTCN_error(\"The right operand of comparison is an unbound value of "
3200 "return other_value.n_elements == 0;\n"
3201 "}\n\n", name
, dispname
);
3203 output
->header
.function_prototypes
=
3204 mputprintf(output
->header
.function_prototypes
,
3205 "inline boolean operator!=(null_type null_value, const %s& "
3207 "{ return !(null_value == other_value); }\n", name
);
3210 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3212 void defRecordOfClass2(const struct_of_def
*sdef
, output_struct
*output
)
3214 char *def
= NULL
, *src
= NULL
;
3215 const char *name
= sdef
->name
;
3216 const char *type
= sdef
->type
;
3217 boolean raw_needed
= force_gen_seof
|| (sdef
->hasRaw
&& enable_raw());
3218 boolean xer_needed
= force_gen_seof
|| (sdef
->hasXer
&& enable_xer());
3220 /* Class definition */
3221 def
= mputprintf(def
,
3222 "class %s : public Record_Of_Type {\n", name
);
3224 /* constant unbound element */
3225 def
= mputprintf(def
, "\nstatic const %s UNBOUND_ELEM;\n", type
);
3226 src
= mputprintf(src
, "\nconst %s %s::UNBOUND_ELEM;\n", type
, name
);
3228 /* public member functions */
3229 def
= mputstr(def
, "\npublic:\n");
3232 def
= mputprintf(def
, "%s(): Record_Of_Type() {}\n", name
);
3233 def
= mputprintf(def
, "%s(null_type other_value): Record_Of_Type(other_value) {}\n", name
);
3234 /* copy constructor */
3235 def
= mputprintf(def
, "%s(const %s& other_value): Record_Of_Type(other_value) {}\n", name
, name
);
3237 def
= mputprintf(def
, "~%s() { clean_up(); }\n\n", name
);
3239 /* assignment operators */
3240 def
= mputprintf(def
, "inline %s& operator=(null_type other_value) "
3241 "{ set_val(other_value); return *this; }\n", name
);
3242 def
= mputprintf(def
, "inline %s& operator=(const %s& other_value) "
3243 "{ set_value(&other_value); return *this; }\n\n", name
, name
);
3245 /* comparison operators */
3246 def
= mputprintf(def
, "inline boolean operator==(const %s& other_value) const "
3247 "{ return is_equal(&other_value); }\n", name
);
3248 def
= mputprintf(def
, "boolean operator!=(const %s& other_value) const "
3249 "{ return !is_equal(&other_value); }\n", name
);
3251 /* indexing operators */
3252 def
= mputprintf(def
,
3253 "%s& operator[](int index_value);\n"
3254 "%s& operator[](const INTEGER& index_value);\n"
3255 "const %s& operator[](int index_value) const;\n"
3256 "const %s& operator[](const INTEGER& index_value) const;\n",
3262 src
= mputprintf(src
,
3263 "%s& %s::operator[](int index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n"
3264 "%s& %s::operator[](const INTEGER& index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n"
3265 "const %s& %s::operator[](int index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n"
3266 "const %s& %s::operator[](const INTEGER& index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n\n",
3272 /* rotation operators */
3273 def
= mputprintf(def
,
3274 "%s operator<<=(int rotate_count) const;\n"
3275 "%s operator<<=(const INTEGER& rotate_count) const;\n"
3276 "%s operator>>=(int rotate_count) const;\n"
3277 "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
3278 name
, name
, name
, name
);
3279 src
= mputprintf(src
,
3280 "%s %s::operator<<=(int rotate_count) const\n"
3282 "return *this >>= (-rotate_count);\n"
3284 "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
3287 "return *((%s*)rotl(rotate_count, &rec_of));\n"
3289 "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
3292 "return *((%s*)rotr(rotate_count, &rec_of));\n"
3294 "%s %s::operator>>=(int rotate_count) const\n"
3297 "return *((%s*)rotr(rotate_count, &rec_of));\n"
3299 name
, name
, name
, name
, name
, name
, name
, name
, name
, name
, name
,
3303 def
= mputprintf(def
,
3304 "%s operator+(const %s& other_value) const;\n\n", name
, name
);
3305 src
= mputprintf(src
,
3306 "%s %s::operator+(const %s& other_value) const\n"
3309 "return *((%s*)concat(&other_value, &rec_of));\n"
3310 "}\n\n", name
, name
, name
, name
, name
);
3313 def
= mputprintf(def
,
3314 "%s substr(int index, int returncount) const;\n\n", name
);
3315 src
= mputprintf(src
,
3316 "%s %s::substr(int index, int returncount) const\n"
3319 "substr_(index, returncount, &rec_of);\n"
3321 "}\n\n", name
, name
, name
);
3324 def
= mputprintf(def
,
3325 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
3326 src
= mputprintf(src
,
3327 "%s %s::replace(int index, int len, const %s& repl) const\n"
3330 "replace_(index, len, &repl, &rec_of);\n"
3332 "}\n\n", name
, name
, name
, name
);
3333 def
= mputprintf(def
,
3334 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
3336 src
= mputprintf(src
,
3337 "%s %s::replace(int index, int len, const %s_template& repl) const\n"
3340 "replace_(index, len, &repl, &rec_of);\n"
3342 "}\n\n", name
, name
, name
, name
);
3344 def
= mputprintf(def
,
3345 "Base_Type* clone() const { return new %s(*this); }\n"
3346 "const TTCN_Typedescriptor_t* get_descriptor() const;\n"
3347 "const TTCN_Typedescriptor_t* get_elem_descr() const;\n"
3348 "Base_Type* create_elem() const;\n"
3349 "const Base_Type* get_unbound_elem() const;\n"
3350 "boolean is_set() const { return %s; }\n",
3352 (sdef
->kind
== SET_OF
) ? "TRUE" : "FALSE");
3354 src
= mputprintf(src
,
3355 "Base_Type* %s::create_elem() const { return new %s; }\n"
3356 "const Base_Type* %s::get_unbound_elem() const { return &UNBOUND_ELEM; }\n"
3357 "const TTCN_Typedescriptor_t* %s::get_descriptor() const { return &%s_descr_; }\n",
3362 /* helper functions called by enc/dec members of the ancestor class */
3364 def
= mputprintf(def
, "int rawdec_ebv() const { return %d; }\n",
3365 (int)sdef
->raw
.extension_bit
);
3368 def
= mputprintf(def
, "boolean isXerAttribute() const { return %s; }\n"
3369 "virtual boolean can_start_v(const char * name, const char *uri, "
3370 "XERdescriptor_t const& xd, unsigned int);\n"
3371 "static boolean can_start (const char * name, const char *uri, "
3372 "XERdescriptor_t const& xd, unsigned int);\n",
3373 sdef
->xerAttribute
? "TRUE" : "FALSE");
3374 src
= mputprintf(src
,
3375 /* The virtual can_start_v hands off to the static can_start.
3376 * We must make a virtual call in Record_Of_Type::XER_decode because
3377 * we don't know the actual type (derived from Record_Of_Type) */
3378 "boolean %s::can_start_v(const char *name, const char *uri, "
3379 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
3380 " return can_start(name, uri, xd, flavor);\n"
3382 "boolean %s::can_start(const char *name, const char *uri, "
3383 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
3384 " boolean e_xer = is_exer(flavor);\n"
3385 /* if EXER and UNTAGGED, it can begin with the tag of the element,
3386 * otherwise it must be the tag of the type itself,
3387 * specified in the supplied parameter.
3388 * If flavor contains UNTAGGED, that's a signal to go directly
3389 * to the embedded type. */
3390 " if (!e_xer || !((xd.xer_bits|flavor) & UNTAGGED)) return "
3391 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
3392 /* a record-of with ANY-ELEMENT can start with any tag
3393 * :-( with some exceptions )-: */
3394 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
3398 if (!force_gen_seof
&& sdef
->nFollowers
) {
3400 src
= mputstr(src
, "{\n");
3401 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
3402 src
= mputprintf(src
,
3403 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
3404 , sdef
->followers
[f
].type
3405 , sdef
->followers
[f
].typegen
3413 src
= mputstr(src
, "return TRUE;\n");
3415 src
= mputprintf(src
,
3416 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
3417 "}\n\n", sdef
->type
);
3418 def
= mputprintf(def
, "boolean isXmlValueList() const { return %s; }\n\n",
3419 sdef
->xmlValueList
? "TRUE" : "FALSE");
3422 /* The call in XER_decode is still there, can_start_v must exist. */
3424 "virtual boolean can_start_v(const char *, const char *, "
3425 "XERdescriptor_t const&, unsigned int) { return FALSE; }\n");
3429 def
= mputstr(def
, "};\n\n");
3431 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
3432 "class %s;\n", name
);
3433 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
3435 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
3439 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3441 void defRecordOfTemplate1(const struct_of_def
*sdef
, output_struct
*output
)
3443 char *def
= NULL
, *src
= NULL
;
3444 const char *name
= sdef
->name
, *dispname
= sdef
->dispname
;
3445 const char *type
= sdef
->type
;
3446 const char *base_class
= sdef
->kind
== RECORD_OF
? "Record_Of_Template" :
3447 "Restricted_Length_Template";
3449 /* Class definition and private data members */
3450 def
= mputprintf(def
,
3451 "class %s_template : public %s {\n"
3455 "%s_template **value_elements;\n"
3458 "unsigned int n_values;\n"
3459 "%s_template *list_value;\n"
3460 "} value_list;\n", name
, base_class
, type
, name
);
3461 if (sdef
->kind
== SET_OF
) {
3462 def
= mputprintf(def
,
3464 "unsigned int n_items;\n"
3465 "%s_template *set_items;\n"
3466 "} value_set;\n", type
);
3468 def
= mputstr(def
, "};\n");
3470 /* private member functions */
3472 /* copy_value function */
3473 def
= mputprintf(def
, "void copy_value(const %s& other_value);\n", name
);
3474 src
= mputprintf(src
,
3475 "void %s_template::copy_value(const %s& other_value)\n"
3477 "if (!other_value.is_bound()) "
3478 "TTCN_error(\"Initialization of a template of type %s with an unbound "
3480 "single_value.n_elements = other_value.size_of();\n"
3481 "single_value.value_elements = "
3482 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
3483 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3485 "if (other_value[elem_count].is_bound()) {\n"
3486 "single_value.value_elements[elem_count] = "
3487 "new %s_template(other_value[elem_count]);\n"
3489 "single_value.value_elements[elem_count] = new %s_template;\n"
3492 "set_selection(SPECIFIC_VALUE);\n"
3493 "}\n\n", name
, name
, dispname
, type
, type
, type
);
3495 /* copy_template function */
3496 def
= mputprintf(def
, "void copy_template(const %s_template& "
3497 "other_value);\n", name
);
3498 src
= mputprintf(src
,
3499 "void %s_template::copy_template(const %s_template& other_value)\n"
3501 "switch (other_value.template_selection) {\n"
3502 "case SPECIFIC_VALUE:\n"
3503 "single_value.n_elements = other_value.single_value.n_elements;\n"
3504 "single_value.value_elements = "
3505 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
3506 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3508 "if (UNINITIALIZED_TEMPLATE != "
3509 "other_value.single_value.value_elements[elem_count]->get_selection()) {\n"
3510 "single_value.value_elements[elem_count] = new %s_template"
3511 "(*other_value.single_value.value_elements[elem_count]);\n"
3513 "single_value.value_elements[elem_count] = new %s_template;\n"
3516 "case OMIT_VALUE:\n"
3518 "case ANY_OR_OMIT:\n"
3520 "case VALUE_LIST:\n"
3521 "case COMPLEMENTED_LIST:\n"
3522 "value_list.n_values = other_value.value_list.n_values;\n"
3523 "value_list.list_value = new %s_template[value_list.n_values];\n"
3524 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
3526 "value_list.list_value[list_count].copy_template("
3527 "other_value.value_list.list_value[list_count]);\n"
3528 "break;\n", name
, name
, type
, type
, type
, name
);
3529 if (sdef
->kind
== SET_OF
) {
3530 src
= mputprintf(src
,
3531 "case SUPERSET_MATCH:\n"
3532 "case SUBSET_MATCH:\n"
3533 "value_set.n_items = other_value.value_set.n_items;\n"
3534 "value_set.set_items = new %s_template[value_set.n_items];\n"
3535 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
3537 "value_set.set_items[set_count] = "
3538 "other_value.value_set.set_items[set_count];\n"
3541 src
= mputprintf(src
,
3543 "TTCN_error(\"Copying an uninitialized/unsupported template of type "
3547 "set_selection(other_value);\n"
3550 /* callback function for matching specific values */
3552 "static boolean match_function_specific(const Base_Type *value_ptr, "
3553 "int value_index, const Restricted_Length_Template *template_ptr, "
3554 "int template_index, boolean legacy);\n");
3555 src
= mputprintf(src
,
3556 "boolean %s_template::match_function_specific(const Base_Type *value_ptr, "
3557 "int value_index, const Restricted_Length_Template *template_ptr, "
3558 "int template_index, boolean legacy)\n"
3560 "if (value_index >= 0) return ((const %s_template*)template_ptr)->"
3561 "single_value.value_elements[template_index]->"
3562 "match((*(const %s*)value_ptr)[value_index], legacy);\n"
3563 "else return ((const %s_template*)template_ptr)->"
3564 "single_value.value_elements[template_index]->is_any_or_omit();\n"
3565 "}\n\n", name
, name
, name
, name
);
3567 if (sdef
->kind
== SET_OF
) {
3568 /* callback function for matching superset and subset */
3570 "static boolean match_function_set(const Base_Type *value_ptr, "
3571 "int value_index, const Restricted_Length_Template *template_ptr, "
3572 "int template_index, boolean legacy);\n");
3573 src
= mputprintf(src
,
3574 "boolean %s_template::match_function_set(const Base_Type *value_ptr, "
3575 "int value_index, const Restricted_Length_Template *template_ptr, "
3576 "int template_index, boolean legacy)\n"
3578 "if (value_index >= 0) return ((const %s_template*)template_ptr)->"
3579 "value_set.set_items[template_index].match("
3580 "(*(const %s*)value_ptr)[value_index], legacy);\n"
3581 "else return ((const %s_template*)template_ptr)->"
3582 "value_set.set_items[template_index].is_any_or_omit();\n"
3583 "}\n\n", name
, name
, name
, name
);
3585 /* callback function for log_match_heuristics */
3587 "static void log_function(const Base_Type *value_ptr, "
3588 "const Restricted_Length_Template *template_ptr,"
3589 " int index_value, int index_template, boolean legacy);\n");
3590 src
= mputprintf(src
,
3591 "void %s_template::log_function(const Base_Type *value_ptr, "
3592 "const Restricted_Length_Template *template_ptr,"
3593 " int index_value, int index_template, boolean legacy)\n"
3595 "if (value_ptr != NULL && template_ptr != NULL)"
3596 "((const %s_template*)template_ptr)"
3597 "->single_value.value_elements[index_template]"
3598 "->log_match((*(const %s*)value_ptr)[index_value], legacy);\n"
3599 "else if (value_ptr != NULL) (*(const %s*)value_ptr)[index_value].log();\n"
3600 "else if (template_ptr != NULL) ((const %s_template*)template_ptr)"
3601 "->single_value.value_elements[index_template]->log();\n"
3602 "}\n\n", name
, name
, name
, name
, name
);
3605 /* public member functions */
3606 def
= mputstr(def
, "\npublic:\n");
3609 def
= mputprintf(def
, "%s_template();\n", name
);
3610 src
= mputprintf(src
, "%s_template::%s_template()\n"
3612 "}\n\n", name
, name
);
3614 def
= mputprintf(def
, "%s_template(template_sel other_value);\n", name
);
3615 src
= mputprintf(src
, "%s_template::%s_template(template_sel other_value)\n"
3616 " : %s(other_value)\n"
3618 "check_single_selection(other_value);\n"
3619 "}\n\n", name
, name
, base_class
);
3621 def
= mputprintf(def
, "%s_template(null_type other_value);\n", name
);
3622 src
= mputprintf(src
, "%s_template::%s_template(null_type)\n"
3623 " : %s(SPECIFIC_VALUE)\n"
3625 "single_value.n_elements = 0;\n"
3626 "single_value.value_elements = NULL;\n"
3627 "}\n\n", name
, name
, base_class
);
3629 def
= mputprintf(def
, "%s_template(const %s& other_value);\n", name
, name
);
3630 src
= mputprintf(src
, "%s_template::%s_template(const %s& other_value)\n"
3632 "copy_value(other_value);\n"
3633 "}\n\n", name
, name
, name
);
3635 def
= mputprintf(def
, "%s_template(const OPTIONAL<%s>& other_value);\n",
3637 src
= mputprintf(src
,
3638 "%s_template::%s_template(const OPTIONAL<%s>& other_value)\n"
3640 "switch (other_value.get_selection()) {\n"
3641 "case OPTIONAL_PRESENT:\n"
3642 "copy_value((const %s&)other_value);\n"
3644 "case OPTIONAL_OMIT:\n"
3645 "set_selection(OMIT_VALUE);\n"
3648 "TTCN_error(\"Creating a template of type %s from an unbound optional "
3651 "}\n\n", name
, name
, name
, name
, dispname
);
3653 /* copy constructor */
3654 def
= mputprintf(def
, "%s_template(const %s_template& other_value);\n",
3656 src
= mputprintf(src
,
3657 "%s_template::%s_template(const %s_template& other_value)\n"
3660 "copy_template(other_value);\n"
3661 "}\n\n", name
, name
, name
, base_class
);
3664 def
= mputprintf(def
, "~%s_template();\n\n", name
);
3665 src
= mputprintf(src
,
3666 "%s_template::~%s_template()\n"
3669 "}\n\n", name
, name
);
3671 /* clean_up function */
3672 def
= mputstr(def
, "void clean_up();\n");
3673 src
= mputprintf(src
,
3674 "void %s_template::clean_up()\n"
3676 "switch (template_selection) {\n"
3677 "case SPECIFIC_VALUE:\n"
3678 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3680 "delete single_value.value_elements[elem_count];\n"
3681 "free_pointers((void**)single_value.value_elements);\n"
3683 "case VALUE_LIST:\n"
3684 "case COMPLEMENTED_LIST:\n"
3685 "delete [] value_list.list_value;\n", name
);
3686 if (sdef
->kind
== SET_OF
) {
3689 "case SUPERSET_MATCH:\n"
3690 "case SUBSET_MATCH:\n"
3691 "delete [] value_set.set_items;\n");
3697 "template_selection = UNINITIALIZED_TEMPLATE;\n"
3700 /* assignment operators */
3701 def
= mputprintf(def
, "%s_template& operator=(template_sel other_value);\n",
3703 src
= mputprintf(src
,
3704 "%s_template& %s_template::operator=(template_sel other_value)\n"
3706 "check_single_selection(other_value);\n"
3708 "set_selection(other_value);\n"
3710 "}\n\n", name
, name
);
3712 def
= mputprintf(def
, "%s_template& operator=(null_type other_value);\n",
3714 src
= mputprintf(src
,
3715 "%s_template& %s_template::operator=(null_type)\n"
3718 "set_selection(SPECIFIC_VALUE);\n"
3719 "single_value.n_elements = 0;\n"
3720 "single_value.value_elements = NULL;\n"
3722 "}\n\n", name
, name
);
3724 def
= mputprintf(def
, "%s_template& operator=(const %s& other_value);\n",
3726 src
= mputprintf(src
,
3727 "%s_template& %s_template::operator=(const %s& other_value)\n"
3730 "copy_value(other_value);\n"
3732 "}\n\n", name
, name
, name
);
3734 def
= mputprintf(def
, "%s_template& operator=(const OPTIONAL<%s>& "
3735 "other_value);\n", name
, name
);
3736 src
= mputprintf(src
,
3737 "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n"
3740 "switch (other_value.get_selection()) {\n"
3741 "case OPTIONAL_PRESENT:\n"
3742 "copy_value((const %s&)other_value);\n"
3744 "case OPTIONAL_OMIT:\n"
3745 "set_selection(OMIT_VALUE);\n"
3748 "TTCN_error(\"Assignment of an unbound optional field to a template of "
3752 "}\n\n", name
, name
, name
, name
, dispname
);
3754 def
= mputprintf(def
, "%s_template& operator=(const %s_template& "
3755 "other_value);\n\n", name
, name
);
3756 src
= mputprintf(src
,
3757 "%s_template& %s_template::operator=(const %s_template& other_value)\n"
3759 "if (&other_value != this) {\n"
3761 "copy_template(other_value);\n"
3764 "}\n\n", name
, name
, name
);
3766 /* indexing operators */
3767 /* Non-const operator[] is allowed to extend */
3768 def
= mputprintf(def
, "%s_template& operator[](int index_value);\n", type
);
3769 src
= mputprintf(src
,
3770 "%s_template& %s_template::operator[](int index_value)\n"
3772 "if (index_value < 0) TTCN_error(\"Accessing an element of a template "
3773 "for type %s using a negative index: %%d.\", index_value);\n"
3774 "switch (template_selection)\n"
3776 " case SPECIFIC_VALUE:\n"
3777 " if(index_value < single_value.n_elements) break;\n"
3779 " case OMIT_VALUE:\n"
3780 " case ANY_VALUE:\n"
3781 " case ANY_OR_OMIT:\n"
3782 " case UNINITIALIZED_TEMPLATE:\n"
3783 " set_size(index_value + 1);\n"
3786 " TTCN_error(\"Accessing an "
3787 "element of a non-specific template for type %s.\");\n"
3790 "return *single_value.value_elements[index_value];\n"
3791 "}\n\n", type
, name
, dispname
, dispname
);
3793 def
= mputprintf(def
, "%s_template& operator[](const INTEGER& "
3794 "index_value);\n", type
);
3795 src
= mputprintf(src
,
3796 "%s_template& %s_template::operator[](const INTEGER& index_value)\n"
3798 "index_value.must_bound(\"Using an unbound integer value for indexing "
3799 "a template of type %s.\");\n"
3800 "return (*this)[(int)index_value];\n"
3801 "}\n\n", type
, name
, dispname
);
3803 /* Const operator[] throws an error if over-indexing */
3804 def
= mputprintf(def
, "const %s_template& operator[](int index_value) "
3806 src
= mputprintf(src
,
3807 "const %s_template& %s_template::operator[](int index_value) const\n"
3809 "if (index_value < 0) TTCN_error(\"Accessing an element of a template "
3810 "for type %s using a negative index: %%d.\", index_value);\n"
3811 "if (template_selection != SPECIFIC_VALUE) TTCN_error(\"Accessing an "
3812 "element of a non-specific template for type %s.\");\n"
3813 "if (index_value >= single_value.n_elements) "
3814 "TTCN_error(\"Index overflow in a template of type %s: "
3815 "The index is %%d, but the template has only %%d elements.\", "
3816 "index_value, single_value.n_elements);\n"
3817 "return *single_value.value_elements[index_value];\n"
3818 "}\n\n", type
, name
, dispname
, dispname
, dispname
);
3820 def
= mputprintf(def
, "const %s_template& operator[](const INTEGER& "
3821 "index_value) const;\n\n", type
);
3822 src
= mputprintf(src
,
3823 "const %s_template& %s_template::operator[](const INTEGER& index_value) "
3826 "index_value.must_bound(\"Using an unbound integer value for indexing "
3827 "a template of type %s.\");\n"
3828 "return (*this)[(int)index_value];\n"
3829 "}\n\n", type
, name
, dispname
);
3831 /* set_size function */
3832 def
= mputstr(def
, "void set_size(int new_size);\n");
3833 src
= mputprintf(src
, "void %s_template::set_size(int new_size)\n"
3835 "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size "
3836 "for a template of type %s.\");\n"
3837 "template_sel old_selection = template_selection;\n"
3838 "if (old_selection != SPECIFIC_VALUE) {\n"
3840 "set_selection(SPECIFIC_VALUE);\n"
3841 "single_value.n_elements = 0;\n"
3842 "single_value.value_elements = NULL;\n"
3844 "if (new_size > single_value.n_elements) {\n"
3845 "single_value.value_elements = (%s_template**)reallocate_pointers((void**)"
3846 "single_value.value_elements, single_value.n_elements, new_size);\n"
3847 "if (old_selection == ANY_VALUE || old_selection == ANY_OR_OMIT) {\n"
3848 "for (int elem_count = single_value.n_elements; elem_count < new_size; "
3850 "single_value.value_elements[elem_count] = new %s_template(ANY_VALUE);\n"
3852 "for (int elem_count = single_value.n_elements; elem_count < new_size; "
3854 "single_value.value_elements[elem_count] = new %s_template;\n"
3856 "single_value.n_elements = new_size;\n"
3857 "} else if (new_size < single_value.n_elements) {\n"
3858 "for (int elem_count = new_size; elem_count < single_value.n_elements; "
3860 "delete single_value.value_elements[elem_count];\n"
3861 "single_value.value_elements = (%s_template**)reallocate_pointers((void**)"
3862 "single_value.value_elements, single_value.n_elements, new_size);\n"
3863 "single_value.n_elements = new_size;\n"
3865 "}\n\n", name
, dispname
, type
, type
, type
, type
);
3868 def
= mputstr(def
, "int n_elem() const;\n");
3869 src
= mputprintf(src
,
3870 "int %s_template::n_elem() const\n"
3872 " switch (template_selection) {\n"
3873 " case SPECIFIC_VALUE:\n"
3874 " return single_value.n_elements;\n"
3876 " case VALUE_LIST:\n"
3877 " return value_list.n_values;\n"
3879 /* if (sdef->kind == SET_OF) {
3880 src = mputprintf(src,
3883 src
= mputstr(src
, " default:\n"
3884 " TTCN_error(\"Performing n_elem\");\n"
3889 /* sizeof operation */
3891 "int size_of(boolean is_size) const;\n"
3892 "inline int size_of() const { return size_of(TRUE); }\n"
3893 "inline int lengthof() const { return size_of(FALSE); }\n"
3895 src
= mputprintf(src
,
3896 "int %s_template::size_of(boolean is_size) const\n"
3898 "const char* op_name = is_size ? \"size\" : \"length\";\n"
3900 "boolean has_any_or_none;\n"
3901 "if (is_ifpresent) TTCN_error(\"Performing %%sof() operation on a "
3902 "template of type %s which has an ifpresent attribute.\", op_name);\n"
3903 "switch (template_selection)\n"
3905 "case SPECIFIC_VALUE: {\n"
3907 " has_any_or_none = FALSE;\n"
3908 " int elem_count = single_value.n_elements;\n"
3909 " if (!is_size) { while (elem_count>0 && !single_value.value_elements"
3910 "[elem_count-1]->is_bound()) elem_count--; }\n"
3911 " for (int i=0; i<elem_count; i++) {\n"
3912 " switch (single_value.value_elements[i]->get_selection()) {\n"
3913 " case OMIT_VALUE:\n"
3914 " TTCN_error(\"Performing %%sof() operation on a template of type "
3915 "%s containing omit element.\", op_name);\n"
3916 " case ANY_OR_OMIT:\n"
3917 " has_any_or_none = TRUE;\n"
3925 name
, dispname
, dispname
);
3926 if (sdef
->kind
== SET_OF
) {
3927 src
= mputprintf(src
,
3928 "case SUPERSET_MATCH:\n"
3929 "case SUBSET_MATCH: {\n"
3931 " has_any_or_none = FALSE;\n"
3932 " int elem_count = value_set.n_items;\n"
3933 " if (!is_size) { while (elem_count>0 && !value_set.set_items"
3934 "[elem_count-1].is_bound()) elem_count--; }\n"
3935 " for (int i=0; i<elem_count; i++) {\n"
3936 " switch (value_set.set_items[i].get_selection())\n"
3938 " case OMIT_VALUE:\n"
3939 " TTCN_error(\"Performing %%sof() operation on a template of type "
3940 "%s containing omit element.\", op_name);\n"
3941 " case ANY_OR_OMIT:\n"
3942 " has_any_or_none = TRUE;\n"
3949 " if (template_selection==SUPERSET_MATCH) {\n"
3950 " has_any_or_none = TRUE;\n"
3952 " int max_size = min_size;\n"
3954 " if (!has_any_or_none) { // [0,max_size]\n"
3955 " switch (length_restriction_type) {\n"
3956 " case NO_LENGTH_RESTRICTION:\n"
3957 " if (max_size==0) return 0;\n"
3958 " TTCN_error(\"Performing %%sof() operation on a template of "
3959 "type %s with no exact size.\", op_name);\n"
3960 " case SINGLE_LENGTH_RESTRICTION:\n"
3961 " if (length_restriction.single_length<=max_size)\n"
3962 " return length_restriction.single_length;\n"
3963 " TTCN_error(\"Performing %%sof() operation on an invalid "
3964 "template of type %s. The maximum size (%%d) contradicts the length "
3965 "restriction (%%d).\", op_name, max_size, "
3966 "length_restriction.single_length);\n"
3967 " case RANGE_LENGTH_RESTRICTION:\n"
3968 " if (max_size==length_restriction.range_length.min_length) {\n"
3969 " return max_size;\n"
3970 " } else if (max_size>length_restriction.range_length.min_length)"
3972 " TTCN_error(\"Performing %%sof() operation on a template of "
3973 "type %s with no exact size.\", op_name);\n"
3975 " TTCN_error(\"Performing %%sof() operation on an invalid "
3976 "template of type %s. Maximum size (%%d) contradicts the length "
3977 "restriction (%%d..%%d).\", op_name, max_size, "
3978 "length_restriction.range_length.min_length, "
3979 "length_restriction.range_length.max_length);\n"
3981 " TTCN_error(\"Internal error: Template has invalid length "
3982 "restriction type.\");\n"
3987 dispname
, dispname
, dispname
, dispname
, dispname
);
3989 src
= mputprintf(src
,
3990 "case OMIT_VALUE:\n"
3991 " TTCN_error(\"Performing %%sof() operation on a template of type %s "
3992 "containing omit value.\", op_name);\n"
3994 "case ANY_OR_OMIT:\n"
3996 " has_any_or_none = TRUE;\n"
3998 "case VALUE_LIST:\n"
4000 " if (value_list.n_values<1)\n"
4001 " TTCN_error(\"Performing %%sof() operation on a "
4002 "template of type %s containing an empty list.\", op_name);\n"
4003 " int item_size = value_list.list_value[0].size_of(is_size);\n"
4004 " for (unsigned int i = 1; i < value_list.n_values; i++) {\n"
4005 " if (value_list.list_value[i].size_of(is_size)!=item_size)\n"
4006 " TTCN_error(\"Performing %%sof() operation on a template of type "
4007 "%s containing a value list with different sizes.\", op_name);\n"
4009 " min_size = item_size;\n"
4010 " has_any_or_none = FALSE;\n"
4013 "case COMPLEMENTED_LIST:\n"
4014 " TTCN_error(\"Performing %%sof() operation on a template of type %s "
4015 "containing complemented list.\", op_name);\n"
4017 " TTCN_error(\"Performing %%sof() operation on an "
4018 "uninitialized/unsupported template of type %s.\", op_name);\n"
4020 "return check_section_is_single(min_size, has_any_or_none, "
4021 "op_name, \"a\", \"template of type %s\");\n"
4023 dispname
, dispname
, dispname
, dispname
, dispname
, dispname
);
4025 /* match operation */
4026 def
= mputprintf(def
, "boolean match(const %s& other_value, boolean legacy "
4027 "= FALSE) const;\n", name
);
4028 src
= mputprintf(src
,
4029 "boolean %s_template::match(const %s& other_value, boolean legacy) const\n"
4031 "if (!other_value.is_bound()) return FALSE;\n"
4032 "int value_length = other_value.size_of();\n"
4033 "if (!match_length(value_length)) return FALSE;\n"
4034 "switch (template_selection) {\n"
4035 "case SPECIFIC_VALUE:\n"
4036 "return match_%s_of(&other_value, value_length, this, "
4037 "single_value.n_elements, match_function_specific, legacy);\n"
4038 "case OMIT_VALUE:\n"
4041 "case ANY_OR_OMIT:\n"
4043 "case VALUE_LIST:\n"
4044 "case COMPLEMENTED_LIST:\n"
4045 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4047 "if (value_list.list_value[list_count].match(other_value, legacy)) "
4048 "return template_selection == VALUE_LIST;\n"
4049 "return template_selection == COMPLEMENTED_LIST;\n",
4050 name
, name
, sdef
->kind
== RECORD_OF
? "record" : "set");
4051 if (sdef
->kind
== SET_OF
) {
4053 "case SUPERSET_MATCH:\n"
4054 "case SUBSET_MATCH:\n"
4055 "return match_set_of(&other_value, value_length, this, "
4056 "value_set.n_items, match_function_set, legacy);\n");
4058 src
= mputprintf(src
,
4060 "TTCN_error(\"Matching with an uninitialized/unsupported template "
4066 /* is_bound function */
4068 "inline boolean is_bound() const \n"
4069 " {return template_selection != UNINITIALIZED_TEMPLATE; }\n");
4071 /* is_value operation */
4072 def
= mputstr(def
, "boolean is_value() const;\n");
4073 src
= mputprintf(src
,
4074 "boolean %s_template::is_value() const\n"
4076 "if (template_selection != SPECIFIC_VALUE || is_ifpresent) return false;\n"
4077 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4079 "if (!single_value.value_elements[elem_count]->is_value()) return false;\n"
4083 /* valueof operation */
4084 def
= mputprintf(def
, "%s valueof() const;\n", name
);
4085 src
= mputprintf(src
,
4086 "%s %s_template::valueof() const\n"
4088 "if (template_selection != SPECIFIC_VALUE || is_ifpresent) TTCN_error(\""
4089 "Performing a valueof or send operation on a non-specific template of type "
4092 "ret_val.set_size(single_value.n_elements);\n"
4093 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4095 "if (single_value.value_elements[elem_count]->is_bound()) {\n"
4096 "ret_val[elem_count] = single_value.value_elements[elem_count]->valueof();\n"
4099 "}\n\n", name
, name
, dispname
, name
);
4101 /* substr() predefined function for templates */
4102 def
= mputprintf(def
,
4103 "%s substr(int index, int returncount) const;\n\n", name
);
4104 src
= mputprintf(src
,
4105 "%s %s_template::substr(int index, int returncount) const\n"
4107 "if (!is_value()) TTCN_error(\"The first argument of function substr() is "
4108 "a template with non-specific value.\");\n"
4109 "return valueof().substr(index, returncount);\n"
4110 "}\n\n", name
, name
);
4112 /* replace() predefined function for templates */
4113 def
= mputprintf(def
,
4114 "%s replace(int index, int len, const %s_template& repl) const;\n\n", name
, name
);
4115 src
= mputprintf(src
,
4116 "%s %s_template::replace(int index, int len, const %s_template& repl) const\n"
4118 "if (!is_value()) TTCN_error(\"The first argument of function replace() is "
4119 "a template with non-specific value.\");\n"
4120 "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
4121 "replace() is a template with non-specific value.\");\n"
4122 "return valueof().replace(index, len, repl.valueof());\n"
4123 "}\n\n", name
, name
, name
);
4124 def
= mputprintf(def
,
4125 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
4126 src
= mputprintf(src
,
4127 "%s %s_template::replace(int index, int len, const %s& repl) const\n"
4129 "if (!is_value()) TTCN_error(\"The first argument of function replace() is "
4130 "a template with non-specific value.\");\n"
4131 "return valueof().replace(index, len, repl);\n"
4132 "}\n\n", name
, name
, name
);
4134 /* value list and set handling operators */
4136 "void set_type(template_sel template_type, unsigned int list_length);\n");
4137 src
= mputprintf(src
,
4138 "void %s_template::set_type(template_sel template_type, "
4139 "unsigned int list_length)\n"
4142 "switch (template_type) {\n"
4143 "case VALUE_LIST:\n"
4144 "case COMPLEMENTED_LIST:\n"
4145 "value_list.n_values = list_length;\n"
4146 "value_list.list_value = new %s_template[list_length];\n"
4147 "break;\n", name
, name
);
4148 if (sdef
->kind
== SET_OF
) {
4149 src
= mputprintf(src
,
4150 "case SUPERSET_MATCH:\n"
4151 "case SUBSET_MATCH:\n"
4152 "value_set.n_items = list_length;\n"
4153 "value_set.set_items = new %s_template[list_length];\n"
4156 src
= mputprintf(src
,
4158 "TTCN_error(\"Internal error: Setting an invalid type for a template of "
4161 "set_selection(template_type);\n"
4164 def
= mputprintf(def
,
4165 "%s_template& list_item(unsigned int list_index);\n", name
);
4166 src
= mputprintf(src
,
4167 "%s_template& %s_template::list_item(unsigned int list_index)\n"
4169 "if (template_selection != VALUE_LIST && "
4170 "template_selection != COMPLEMENTED_LIST) "
4171 "TTCN_error(\"Internal error: Accessing a list element of a non-list "
4172 "template of type %s.\");\n"
4173 "if (list_index >= value_list.n_values) "
4174 "TTCN_error(\"Internal error: Index overflow in a value list template "
4176 "return value_list.list_value[list_index];\n"
4177 "}\n\n", name
, name
, dispname
, dispname
);
4179 if (sdef
->kind
== SET_OF
) {
4180 def
= mputprintf(def
,
4181 "%s_template& set_item(unsigned int set_index);\n", type
);
4182 src
= mputprintf(src
,
4183 "%s_template& %s_template::set_item(unsigned int set_index)\n"
4185 "if (template_selection != SUPERSET_MATCH && "
4186 "template_selection != SUBSET_MATCH) "
4187 "TTCN_error(\"Internal error: Accessing a set element of a non-set "
4188 "template of type %s.\");\n"
4189 "if (set_index >= value_set.n_items) "
4190 "TTCN_error(\"Internal error: Index overflow in a set template of "
4192 "return value_set.set_items[set_index];\n"
4193 "}\n\n", type
, name
, dispname
, dispname
);
4196 /* logging functions */
4197 def
= mputstr(def
, "void log() const;\n");
4200 "void %s_template::log() const\n"
4202 "switch (template_selection) {\n"
4203 "case SPECIFIC_VALUE:\n"
4204 "if (single_value.n_elements > 0) {\n"
4205 "TTCN_Logger::log_event_str(\"{ \");\n"
4206 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4208 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n", name
);
4209 if (sdef
->kind
== RECORD_OF
) {
4211 "if (permutation_starts_at(elem_count)) "
4212 "TTCN_Logger::log_event_str(\"permutation(\");\n");
4214 src
= mputstr(src
, "single_value.value_elements[elem_count]->log();\n");
4215 if (sdef
->kind
== RECORD_OF
) {
4217 "if (permutation_ends_at(elem_count)) TTCN_Logger::log_char(')');\n");
4221 "TTCN_Logger::log_event_str(\" }\");\n"
4222 "} else TTCN_Logger::log_event_str(\"{ }\");\n"
4224 "case COMPLEMENTED_LIST:\n"
4225 "TTCN_Logger::log_event_str(\"complement\");\n"
4226 "case VALUE_LIST:\n"
4227 "TTCN_Logger::log_char('(');\n"
4228 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4230 "if (list_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4231 "value_list.list_value[list_count].log();\n"
4233 "TTCN_Logger::log_char(')');\n"
4235 if (sdef
->kind
== SET_OF
) {
4237 "case SUPERSET_MATCH:\n"
4238 "case SUBSET_MATCH:\n"
4239 "TTCN_Logger::log_event(\"%s(\", template_selection == SUPERSET_MATCH "
4240 "? \"superset\" : \"subset\");\n"
4241 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4243 "if (set_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4244 "value_set.set_items[set_count].log();\n"
4246 "TTCN_Logger::log_char(')');\n"
4253 "log_restricted();\n"
4254 "log_ifpresent();\n"
4257 def
= mputprintf(def
, "void log_match(const %s& match_value, "
4258 "boolean legacy = FALSE) const;\n", name
);
4259 src
= mputprintf(src
, "void %s_template::log_match(const %s& match_value, "
4260 "boolean legacy) const\n"
4262 "if(TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity()){\n"
4263 "if(match(match_value, legacy)){\n"
4264 "TTCN_Logger::print_logmatch_buffer();\n"
4265 "TTCN_Logger::log_event_str(\" matched\");\n"
4266 "}else{\n", name
, name
);
4268 if (sdef
->kind
== RECORD_OF
) {
4270 "if (template_selection == SPECIFIC_VALUE && "
4271 "single_value.n_elements > 0 && get_number_of_permutations() == 0 && "
4272 "single_value.n_elements == match_value.size_of()) {\n"
4273 "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n"
4274 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4276 "if(!single_value.value_elements[elem_count]->match(match_value[elem_count], legacy)){\n"
4277 "TTCN_Logger::log_logmatch_info(\"[%d]\", elem_count);\n"
4278 "single_value.value_elements[elem_count]->log_match(match_value[elem_count], legacy);\n"
4279 "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n"
4282 "log_match_length(single_value.n_elements);\n"
4284 "TTCN_Logger::print_logmatch_buffer();\n"
4285 "match_value.log();\n"
4286 "TTCN_Logger::log_event_str(\" with \");\n"
4288 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4291 if (sdef
->kind
== SET_OF
) {
4293 "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n"
4294 "if (template_selection == SPECIFIC_VALUE)\n"
4295 " log_match_heuristics(&match_value, match_value.size_of(), this, "
4296 "single_value.n_elements, match_function_specific, log_function, legacy);\n"
4298 "if(previous_size != 0){\n"
4299 "TTCN_Logger::print_logmatch_buffer();\n"
4300 "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n"
4301 "TTCN_Logger::log_event_str(\":=\");\n"
4304 "match_value.log();\n"
4305 "TTCN_Logger::log_event_str(\" with \");\n"
4307 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4311 src
= mputstr(src
, "}\n"
4314 if (sdef
->kind
== RECORD_OF
) {
4315 /* logging by element is meaningful for 'record of' only */
4317 "if (template_selection == SPECIFIC_VALUE && "
4318 "single_value.n_elements > 0 && get_number_of_permutations() == 0 && "
4319 "single_value.n_elements == match_value.size_of()) {\n"
4320 "TTCN_Logger::log_event_str(\"{ \");\n"
4321 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4323 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4324 "single_value.value_elements[elem_count]->log_match"
4325 "(match_value[elem_count], legacy);\n"
4327 "TTCN_Logger::log_event_str(\" }\");\n"
4328 "log_match_length(single_value.n_elements);\n"
4332 "match_value.log();\n"
4333 "TTCN_Logger::log_event_str(\" with \");\n"
4335 "if (match(match_value, legacy)) TTCN_Logger::log_event_str(\" matched\");\n");
4336 if (sdef
->kind
== SET_OF
) {
4337 src
= mputstr(src
, "else {\n"
4338 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4339 "if (template_selection == SPECIFIC_VALUE) log_match_heuristics("
4340 "&match_value, match_value.size_of(), this, single_value.n_elements, "
4341 "match_function_specific, log_function, legacy);\n"
4344 src
= mputstr(src
, "else TTCN_Logger::log_event_str(\" unmatched\");\n"
4347 src
= mputstr(src
, "}\n\n");
4349 /* encoding/decoding functions */
4350 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
4351 src
= mputprintf(src
,
4352 "void %s_template::encode_text(Text_Buf& text_buf) const\n"
4354 "encode_text_%s(text_buf);\n"
4355 "switch (template_selection) {\n"
4356 "case SPECIFIC_VALUE:\n"
4357 "text_buf.push_int(single_value.n_elements);\n"
4358 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4360 "single_value.value_elements[elem_count]->encode_text(text_buf);\n"
4361 "case OMIT_VALUE:\n"
4363 "case ANY_OR_OMIT:\n"
4365 "case VALUE_LIST:\n"
4366 "case COMPLEMENTED_LIST:\n"
4367 "text_buf.push_int(value_list.n_values);\n"
4368 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4370 "value_list.list_value[list_count].encode_text(text_buf);\n"
4371 "break;\n", name
, sdef
->kind
== RECORD_OF
? "permutation" : "restricted");
4372 if (sdef
->kind
== SET_OF
) {
4374 "case SUPERSET_MATCH:\n"
4375 "case SUBSET_MATCH:\n"
4376 "text_buf.push_int(value_set.n_items);\n"
4377 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4379 "value_set.set_items[set_count].encode_text(text_buf);\n"
4382 src
= mputprintf(src
,
4384 "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported "
4385 "template of type %s.\");\n"
4389 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
4390 src
= mputprintf(src
,
4391 "void %s_template::decode_text(Text_Buf& text_buf)\n"
4394 "decode_text_%s(text_buf);\n"
4395 "switch (template_selection) {\n"
4396 "case SPECIFIC_VALUE:\n"
4397 "single_value.n_elements = text_buf.pull_int().get_val();\n"
4398 "if (single_value.n_elements < 0) TTCN_error(\"Text decoder: Negative "
4399 "size was received for a template of type %s.\");\n"
4400 "single_value.value_elements = "
4401 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
4402 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4404 "single_value.value_elements[elem_count] = new %s_template;\n"
4405 "single_value.value_elements[elem_count]->decode_text(text_buf);\n"
4407 "case OMIT_VALUE:\n"
4409 "case ANY_OR_OMIT:\n"
4411 "case VALUE_LIST:\n"
4412 "case COMPLEMENTED_LIST:\n"
4413 "value_list.n_values = text_buf.pull_int().get_val();\n"
4414 "value_list.list_value = new %s_template[value_list.n_values];\n"
4415 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4417 "value_list.list_value[list_count].decode_text(text_buf);\n"
4418 "break;\n", name
, sdef
->kind
== RECORD_OF
? "permutation" : "restricted",
4419 dispname
, type
, type
, name
);
4420 if (sdef
->kind
== SET_OF
) {
4421 src
= mputprintf(src
,
4422 "case SUPERSET_MATCH:\n"
4423 "case SUBSET_MATCH:\n"
4424 "value_set.n_items = text_buf.pull_int().get_val();\n"
4425 "value_set.set_items = new %s_template[value_set.n_items];\n"
4426 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4428 "value_set.set_items[set_count].decode_text(text_buf);\n"
4431 src
= mputprintf(src
,
4433 "TTCN_error(\"Text decoder: An unknown/unsupported selection was "
4434 "received for a template of type %s.\");\n"
4438 /* TTCN-3 ispresent() function */
4439 def
= mputstr(def
, "boolean is_present(boolean legacy = FALSE) const;\n");
4440 src
= mputprintf(src
,
4441 "boolean %s_template::is_present(boolean legacy) const\n"
4443 "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n"
4444 "return !match_omit(legacy);\n"
4448 def
= mputstr(def
, "boolean match_omit(boolean legacy = FALSE) const;\n");
4449 src
= mputprintf(src
,
4450 "boolean %s_template::match_omit(boolean legacy) const\n"
4452 "if (is_ifpresent) return TRUE;\n"
4453 "switch (template_selection) {\n"
4454 "case OMIT_VALUE:\n"
4455 "case ANY_OR_OMIT:\n"
4457 "case VALUE_LIST:\n"
4458 "case COMPLEMENTED_LIST:\n"
4460 "for (unsigned int i=0; i<value_list.n_values; i++)\n"
4461 "if (value_list.list_value[i].match_omit())\n"
4462 "return template_selection==VALUE_LIST;\n"
4463 "return template_selection==COMPLEMENTED_LIST;\n"
4464 "} // else fall through\n"
4472 def
= mputstr(def
, "void set_param(Module_Param& param);\n");
4473 src
= mputprintf(src
,
4474 "void %s_template::set_param(Module_Param& param)\n"
4476 " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n"
4477 " param.get_id()->next_name()) {\n"
4478 // Haven't reached the end of the module parameter name
4479 // => the name refers to one of the elements, not to the whole record of
4480 " char* param_field = param.get_id()->get_current_name();\n"
4481 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
4482 " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n"
4483 " \" index for %s template type `%s'\");\n"
4485 " int param_index = -1;\n"
4486 " sscanf(param_field, \"%%d\", ¶m_index);\n"
4487 " (*this)[param_index].set_param(param);\n"
4490 " param.basic_check(Module_Param::BC_TEMPLATE|Module_Param::BC_LIST, \"%s of template\");\n"
4491 " Module_Param_Ptr mp = ¶m;\n"
4492 " if (param.get_type() == Module_Param::MP_Reference) {\n"
4493 " mp = param.get_referenced_param();\n"
4495 " switch (mp->get_type()) {\n"
4496 " case Module_Param::MP_Omit:\n"
4497 " *this = OMIT_VALUE;\n"
4499 " case Module_Param::MP_Any:\n"
4500 " *this = ANY_VALUE;\n"
4502 " case Module_Param::MP_AnyOrNone:\n"
4503 " *this = ANY_OR_OMIT;\n"
4505 " case Module_Param::MP_List_Template:\n"
4506 " case Module_Param::MP_ComplementList_Template: {\n"
4507 " %s_template temp;\n"
4508 " temp.set_type(mp->get_type()==Module_Param::MP_List_Template ? "
4509 "VALUE_LIST : COMPLEMENTED_LIST, mp->get_size());\n"
4510 " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n"
4511 " temp.list_item(p_i).set_param(*mp->get_elem(p_i));\n"
4515 " case Module_Param::MP_Indexed_List:\n"
4516 " if (template_selection!=SPECIFIC_VALUE) set_size(0);\n"
4517 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4518 " (*this)[(int)(mp->get_elem(p_i)->get_id()->get_index())].set_param(*mp->get_elem(p_i));\n"
4521 name
, sdef
->kind
==RECORD_OF
?"record":"set", dispname
, sdef
->kind
==RECORD_OF
?"record":"set", name
);
4522 if (sdef
->kind
== RECORD_OF
) {
4524 " case Module_Param::MP_Value_List: {\n"
4525 " set_size(mp->get_size());\n"
4526 " int curr_idx = 0;\n"
4527 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4528 " switch (mp->get_elem(p_i)->get_type()) {\n"
4529 " case Module_Param::MP_NotUsed:\n"
4532 " case Module_Param::MP_Permutation_Template: {\n"
4533 " int perm_start_idx = curr_idx;\n"
4534 " for (size_t perm_i=0; perm_i<mp->get_elem(p_i)->get_size(); perm_i++) {\n"
4535 " (*this)[curr_idx].set_param(*(mp->get_elem(p_i)->get_elem(perm_i)));\n"
4538 " int perm_end_idx = curr_idx - 1;\n"
4539 " add_permutation(perm_start_idx, perm_end_idx);\n"
4542 " (*this)[curr_idx].set_param(*mp->get_elem(p_i));\n"
4549 " case Module_Param::MP_Value_List:\n"
4550 " set_size(mp->get_size());\n"
4551 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4552 " if (mp->get_elem(p_i)->get_type()!=Module_Param::MP_NotUsed) {\n"
4553 " (*this)[p_i].set_param(*mp->get_elem(p_i));\n"
4557 " case Module_Param::MP_Superset_Template:\n"
4558 " case Module_Param::MP_Subset_Template:\n"
4559 " set_type(mp->get_type()==Module_Param::MP_Superset_Template ? SUPERSET_MATCH : SUBSET_MATCH, mp->get_size());\n"
4560 " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n"
4561 " set_item(p_i).set_param(*mp->get_elem(p_i));\n"
4565 src
= mputprintf(src
,
4567 " param.type_error(\"%s of template\", \"%s\");\n"
4569 " is_ifpresent = param.get_ifpresent() || mp->get_ifpresent();\n"
4570 " if (param.get_length_restriction() != NULL) {\n"
4571 " set_length_range(param);\n"
4574 " set_length_range(*mp);\n"
4576 "}\n\n", sdef
->kind
==RECORD_OF
?"record":"set", dispname
);
4579 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
4582 "Module_Param* %s_template::get_param(Module_Param_Name& param_name) const\n"
4584 " if (param_name.next_name()) {\n"
4585 // Haven't reached the end of the module parameter name
4586 // => the name refers to one of the elements, not to the whole record of
4587 " char* param_field = param_name.get_current_name();\n"
4588 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
4589 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
4590 " \"expected a valid index for %s template type `%s'\");\n"
4592 " int param_index = -1;\n"
4593 " sscanf(param_field, \"%%d\", ¶m_index);\n"
4594 " return (*this)[param_index].get_param(param_name);\n"
4596 " Module_Param* mp = NULL;\n"
4597 " switch (template_selection) {\n"
4598 " case UNINITIALIZED_TEMPLATE:\n"
4599 " mp = new Module_Param_Unbound();\n"
4601 " case OMIT_VALUE:\n"
4602 " mp = new Module_Param_Omit();\n"
4604 " case ANY_VALUE:\n"
4605 " mp = new Module_Param_Any();\n"
4607 " case ANY_OR_OMIT:\n"
4608 " mp = new Module_Param_AnyOrNone();\n"
4610 " case SPECIFIC_VALUE: {\n"
4611 " Vector<Module_Param*> values;\n"
4612 " for (int i = 0; i < single_value.n_elements; ++i) {\n"
4613 " values.push_back((*this)[i].get_param(param_name));\n"
4615 " mp = new Module_Param_Value_List();\n"
4616 " mp->add_list_with_implicit_ids(&values);\n"
4617 " values.clear();\n"
4619 " case VALUE_LIST:\n"
4620 " case COMPLEMENTED_LIST: {\n"
4621 " if (template_selection == VALUE_LIST) {\n"
4622 " mp = new Module_Param_List_Template();\n"
4625 " mp = new Module_Param_ComplementList_Template();\n"
4627 " for (size_t i = 0; i < value_list.n_values; ++i) {\n"
4628 " mp->add_elem(value_list.list_value[i].get_param(param_name));\n"
4634 " if (is_ifpresent) {\n"
4635 " mp->set_ifpresent();\n"
4637 " mp->set_length_restriction(get_length_range());\n"
4639 "}\n\n", name
, sdef
->kind
==RECORD_OF
? "record of" : "set of", dispname
);
4641 /* check template restriction */
4642 def
= mputstr(def
, "void check_restriction(template_res t_res, "
4643 "const char* t_name=NULL, boolean legacy = FALSE) const;\n");
4644 src
= mputprintf(src
,
4645 "void %s_template::check_restriction("
4646 "template_res t_res, const char* t_name, boolean legacy) const\n"
4648 "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n"
4649 "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n"
4651 "if (template_selection==OMIT_VALUE) return;\n"
4653 "if (template_selection!=SPECIFIC_VALUE || is_ifpresent) break;\n"
4654 "for (int i=0; i<single_value.n_elements; i++) "
4655 "single_value.value_elements[i]->check_restriction("
4656 "t_res, t_name ? t_name : \"%s\");\n"
4658 "case TR_PRESENT:\n"
4659 "if (!match_omit(legacy)) return;\n"
4664 "TTCN_error(\"Restriction `%%s' on template of type %%s "
4665 "violated.\", get_res_name(t_res), t_name ? t_name : \"%s\");\n"
4666 "}\n\n", name
, dispname
, dispname
);
4669 def
= mputstr(def
, "};\n\n");
4671 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
4672 "class %s_template;\n", name
);
4673 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
4675 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
4679 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
4681 void defRecordOfTemplate2(const struct_of_def
*sdef
, output_struct
*output
)
4683 char *def
= NULL
, *src
= NULL
;
4684 const char *name
= sdef
->name
;
4685 const char *type
= sdef
->type
;
4686 const char *base_class
= sdef
->kind
== RECORD_OF
? "Record_Of_Template" : "Set_Of_Template";
4688 /* Class definition */
4689 def
= mputprintf(def
, "class %s_template : public %s {\n", name
, base_class
);
4691 /* public member functions */
4692 def
= mputstr(def
, "\npublic:\n");
4695 def
= mputprintf(def
, "%s_template() {}\n", name
);
4697 def
= mputprintf(def
, "%s_template(template_sel other_value): %s(other_value) "
4698 "{ check_single_selection(other_value); }\n", name
, base_class
);
4700 def
= mputprintf(def
, "%s_template(null_type other_value);\n", name
);
4701 src
= mputprintf(src
, "%s_template::%s_template(null_type)\n"
4702 " : %s(SPECIFIC_VALUE)\n"
4704 "single_value.n_elements = 0;\n"
4705 "single_value.value_elements = NULL;\n"
4706 "}\n\n", name
, name
, base_class
);
4708 def
= mputprintf(def
, "%s_template(const %s& other_value) "
4709 "{ copy_value(&other_value); }\n", name
, name
);
4710 def
= mputprintf(def
, "%s_template(const OPTIONAL<%s>& other_value) "
4711 "{ copy_optional(&other_value); }\n", name
, name
);
4713 /* copy constructor */
4714 def
= mputprintf(def
, "%s_template(const %s_template& other_value): %s() { copy_template(other_value); }\n",
4715 name
, name
, base_class
);
4717 /* assignment operators */
4718 def
= mputprintf(def
, "%s_template& operator=(template_sel other_value);\n",
4720 src
= mputprintf(src
,
4721 "%s_template& %s_template::operator=(template_sel other_value)\n"
4723 "check_single_selection(other_value);\n"
4725 "set_selection(other_value);\n"
4727 "}\n\n", name
, name
);
4729 def
= mputprintf(def
, "%s_template& operator=(null_type other_value);\n",
4731 src
= mputprintf(src
,
4732 "%s_template& %s_template::operator=(null_type)\n"
4735 "set_selection(SPECIFIC_VALUE);\n"
4736 "single_value.n_elements = 0;\n"
4737 "single_value.value_elements = NULL;\n"
4739 "}\n\n", name
, name
);
4741 def
= mputprintf(def
, "%s_template& operator=(const %s& other_value);\n",
4743 src
= mputprintf(src
,
4744 "%s_template& %s_template::operator=(const %s& other_value)\n"
4747 "copy_value(&other_value);\n"
4749 "}\n\n", name
, name
, name
);
4751 def
= mputprintf(def
, "%s_template& operator=(const OPTIONAL<%s>& "
4752 "other_value);\n", name
, name
);
4753 src
= mputprintf(src
,
4754 "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n"
4757 "copy_optional(&other_value);\n"
4759 "}\n\n", name
, name
, name
);
4761 def
= mputprintf(def
, "%s_template& operator=(const %s_template& "
4762 "other_value);\n\n", name
, name
);
4763 src
= mputprintf(src
,
4764 "%s_template& %s_template::operator=(const %s_template& other_value)\n"
4766 "if (&other_value != this) {\n"
4768 "copy_template(other_value);\n"
4771 "}\n\n", name
, name
, name
);
4773 /* indexing operators */
4774 def
= mputprintf(def
,
4775 "%s_template& operator[](int index_value);\n"
4776 "%s_template& operator[](const INTEGER& index_value);\n"
4777 "const %s_template& operator[](int index_value) const;\n"
4778 "const %s_template& operator[](const INTEGER& index_value) const;\n",
4784 src
= mputprintf(src
,
4785 "%s_template& %s_template::operator[](int index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n"
4786 "%s_template& %s_template::operator[](const INTEGER& index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n"
4787 "const %s_template& %s_template::operator[](int index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n"
4788 "const %s_template& %s_template::operator[](const INTEGER& index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n\n",
4794 /* match operation */
4795 def
= mputprintf(def
, "inline boolean match(const %s& match_value, "
4796 "boolean legacy = FALSE) const "
4797 "{ return matchv(&match_value, legacy); }\n", name
);
4799 /* valueof operation */
4800 def
= mputprintf(def
, "%s valueof() const;\n", name
);
4801 src
= mputprintf(src
,
4802 "%s %s_template::valueof() const\n"
4805 "valueofv(&ret_val);\n"
4807 "}\n\n", name
, name
, name
);
4809 /* substr() predefined function for templates */
4810 def
= mputprintf(def
,
4811 "%s substr(int index, int returncount) const;\n\n", name
);
4812 src
= mputprintf(src
,
4813 "%s %s_template::substr(int index, int returncount) const\n"
4816 "substr_(index, returncount, &rec_of);\n"
4818 "}\n\n", name
, name
, name
);
4820 /* replace() predefined function for templates */
4821 def
= mputprintf(def
,
4822 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
4824 src
= mputprintf(src
,
4825 "%s %s_template::replace(int index, int len, const %s_template& repl) const\n"
4828 "replace_(index, len, &repl, &rec_of);\n"
4830 "}\n\n", name
, name
, name
, name
);
4831 def
= mputprintf(def
,
4832 "%s replace(int index, int len, const %s& repl) const;\n\n",
4834 src
= mputprintf(src
,
4835 "%s %s_template::replace(int index, int len, const %s& repl) const\n"
4838 "replace_(index, len, &repl, &rec_of);\n"
4840 "}\n\n", name
, name
, name
, name
);
4842 /* value list and set handling operators */
4843 def
= mputprintf(def
,
4844 "inline %s_template& list_item(int list_index) { return *(static_cast<%s_template*>(get_list_item(list_index))); }\n", name
, name
);
4846 if (sdef
->kind
== SET_OF
) {
4847 def
= mputprintf(def
, "%s_template& set_item(int set_index);\n", type
);
4848 src
= mputprintf(src
,
4849 "%s_template& %s_template::set_item(int set_index) "
4850 "{ return *(static_cast<%s_template*>(get_set_item(set_index))); }\n",
4854 /* logging functions */
4855 def
= mputprintf(def
, "inline void log_match(const %s& match_value, "
4856 "boolean legacy = FALSE) const "
4857 "{ log_matchv(&match_value, legacy); }\n", name
);
4859 /* virtual helper functions */
4860 def
= mputprintf(def
,
4861 "%s* create() const { return new %s_template; }\n"
4862 "Base_Template* create_elem() const;\n"
4863 "const TTCN_Typedescriptor_t* get_descriptor() const;\n",
4865 src
= mputprintf(src
,
4866 "Base_Template* %s_template::create_elem() const { return new %s_template; }\n"
4867 "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const { return &%s_descr_; }\n",
4872 def
= mputstr(def
, "};\n\n");
4874 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
4875 "class %s_template;\n", name
);
4876 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
4878 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
This page took 0.161112 seconds and 5 git commands to generate.