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"
642 " case Module_Param::MP_Indexed_List:\n"
643 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
644 " Module_Param* const curr = mp->get_elem(i);\n"
645 " (*this)[curr->get_id()->get_index()].set_param(*curr);\n"
649 " param.type_error(\"%s value\", \"%s\");\n"
652 " case Module_Param::OT_CONCAT:\n"
653 " switch (mp->get_type()) {\n"
654 " case Module_Param::MP_Value_List: {\n"
655 " if (!is_bound()) *this = NULL_VALUE;\n"
656 " int start_idx = lengthof();\n"
657 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
658 " Module_Param* const curr = mp->get_elem(i);\n"
659 " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n"
660 " (*this)[start_idx+(int)i].set_param(*curr);\n"
664 " case Module_Param::MP_Indexed_List:\n"
665 " param.error(\"Cannot concatenate an indexed value list\");\n"
668 " param.type_error(\"%s value\", \"%s\");\n"
672 " TTCN_error(\"Internal error: Unknown operation type.\");\n"
674 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of",
675 dispname
, sdef
->kind
== RECORD_OF
? "record of" : "set of",
676 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
677 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
679 /* get param function */
680 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
683 "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n"
685 " if (!is_bound()) {\n"
686 " return new Module_Param_Unbound();\n"
688 " if (param_name.next_name()) {\n"
689 // Haven't reached the end of the module parameter name
690 // => the name refers to one of the elements, not to the whole record of
691 " char* param_field = param_name.get_current_name();\n"
692 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
693 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
694 " \"expected a valid index for %s type `%s'\");\n"
696 " int param_index = -1;\n"
697 " sscanf(param_field, \"%%d\", ¶m_index);\n"
698 " return (*this)[param_index].get_param(param_name);\n"
700 " Vector<Module_Param*> values;\n"
701 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
702 " values.push_back((*this)[i].get_param(param_name));\n"
704 " Module_Param_Value_List* mp = new Module_Param_Value_List();\n"
705 " mp->add_list_with_implicit_ids(&values);\n"
708 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
710 /* set implicit omit function, recursive */
711 def
= mputstr(def
, " void set_implicit_omit();\n");
712 src
= mputprintf(src
,
713 "void %s::set_implicit_omit()\n{\n"
714 "if (val_ptr == NULL) return;\n"
715 "for (int i = 0; i < val_ptr->n_elements; i++) {\n"
716 "if (val_ptr->value_elements[i] != NULL) val_ptr->value_elements[i]->set_implicit_omit();\n"
719 /* encoding / decoding functions */
720 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
721 src
= mputprintf(src
,
722 "void %s::encode_text(Text_Buf& text_buf) const\n"
724 "if (val_ptr == NULL) "
725 "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n"
726 "text_buf.push_int(val_ptr->n_elements);\n"
727 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
729 "(*this)[elem_count].encode_text(text_buf);\n"
730 "}\n\n", name
, dispname
);
732 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
733 src
= mputprintf(src
,
734 "void %s::decode_text(Text_Buf& text_buf)\n"
737 "val_ptr = new recordof_setof_struct;\n"
738 "val_ptr->ref_count = 1;\n"
739 "val_ptr->n_elements = text_buf.pull_int().get_val();\n"
740 "if (val_ptr->n_elements < 0) TTCN_error(\"Text decoder: Negative size "
741 "was received for a value of type %s.\");\n"
742 "val_ptr->value_elements = (%s**)allocate_pointers(val_ptr->n_elements);\n"
743 "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
745 "val_ptr->value_elements[elem_count] = new %s;\n"
746 "val_ptr->value_elements[elem_count]->decode_text(text_buf);\n"
748 "}\n\n", name
, dispname
, type
, type
);
750 if(ber_needed
|| raw_needed
|| text_needed
|| xer_needed
|| json_needed
)
751 def_encdec(name
, &def
, &src
, ber_needed
, raw_needed
, text_needed
,
752 xer_needed
, json_needed
, FALSE
);
756 "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td,"
757 " TTCN_Buffer& p_buf) const{\n"
758 " int encoded_length=0;\n"
759 " if(p_td.text->begin_encode){\n"
760 " p_buf.put_cs(*p_td.text->begin_encode);\n"
761 " encoded_length+=p_td.text->begin_encode->lengthof();\n"
763 " if(val_ptr==NULL) {\n"
764 " TTCN_EncDec_ErrorContext::error\n"
765 " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
766 " if(p_td.text->end_encode){\n"
767 " p_buf.put_cs(*p_td.text->end_encode);\n"
768 " encoded_length+=p_td.text->end_encode->lengthof();\n"
770 " return encoded_length;\n"
772 " for(int a=0;a<val_ptr->n_elements;a++){\n"
773 " if(a!=0 && p_td.text->separator_encode){\n"
774 " p_buf.put_cs(*p_td.text->separator_encode);\n"
775 " encoded_length+=p_td.text->separator_encode->lengthof();\n"
777 " encoded_length+=(*this)[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n"
779 " if(p_td.text->end_encode){\n"
780 " p_buf.put_cs(*p_td.text->end_encode);\n"
781 " encoded_length+=p_td.text->end_encode->lengthof();\n"
783 " return encoded_length;\n"
787 src
= mputprintf(src
,
788 "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td,"
789 " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err"
790 ", boolean first_call){\n"
791 " int decoded_length=0;\n"
792 " size_t pos=p_buf.get_pos();\n"
793 " boolean sep_found=FALSE;\n"
794 " int sep_length=0;\n"
796 " if(p_td.text->begin_decode){\n"
798 " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n"
799 " if(no_err)return -1;\n"
800 " TTCN_EncDec_ErrorContext::error\n"
801 " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'"
802 " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)"
806 " decoded_length+=tl;\n"
807 " p_buf.increase_pos(tl);\n"
809 " if(p_td.text->end_decode){\n"
810 " limit.add_token(p_td.text->end_decode);\n"
813 " if(p_td.text->separator_decode){\n"
814 " limit.add_token(p_td.text->separator_decode);\n"
817 " if(first_call) {\n"
819 " val_ptr=new recordof_setof_struct;\n"
820 " val_ptr->ref_count=1;\n"
821 " val_ptr->n_elements=0;\n"
822 " val_ptr->value_elements=NULL;\n"
824 " int more=val_ptr->n_elements;\n"
827 " pos=p_buf.get_pos();\n"
828 " int len=val->TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n"
829 " if(len==-1 || (len==0 && !limit.has_token())){\n"
830 " p_buf.set_pos(pos);\n"
833 " p_buf.set_pos(p_buf.get_pos()-sep_length);\n"
834 " decoded_length-=sep_length;\n"
838 " sep_found=FALSE;\n"
839 " val_ptr->value_elements = (%s**)reallocate_pointers"
840 "((void**)val_ptr->value_elements, val_ptr->n_elements, "
841 "val_ptr->n_elements + 1);\n"
842 " val_ptr->value_elements[val_ptr->n_elements]=val;\n"
843 " val_ptr->n_elements++;\n"
844 " decoded_length+=len;\n"
845 " if(p_td.text->separator_decode){\n"
847 " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n"
850 " decoded_length+=tl;\n"
851 " p_buf.increase_pos(tl);\n"
854 " } else if(p_td.text->end_decode){\n"
856 " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n"
857 " decoded_length+=tl;\n"
858 " p_buf.increase_pos(tl);\n"
859 " limit.remove_tokens(ml);\n"
860 " return decoded_length;\n"
862 " } else if(limit.has_token(ml)){\n"
864 " if((tl=limit.match(p_buf,ml))==0){\n"
865 " sep_found=FALSE;\n"
873 " limit.remove_tokens(ml);\n"
874 " if(p_td.text->end_decode){\n"
876 " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n"
878 " if(!first_call){\n"
879 " for(int a=more; a<val_ptr->n_elements; a++) "
880 "delete val_ptr->value_elements[a];\n"
881 " val_ptr->n_elements=more;\n"
885 " TTCN_EncDec_ErrorContext::error"
886 "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'"
887 " not found for '%s': \",(const char*)*(p_td.text->end_decode)"
889 " return decoded_length;\n"
891 " decoded_length+=tl;\n"
892 " p_buf.increase_pos(tl);\n"
894 " if(val_ptr->n_elements==0){\n"
895 " if(!(p_td.text->end_decode || p_td.text->begin_decode)) {\n"
896 " if(no_err)return -1;\n"
897 " TTCN_EncDec_ErrorContext::error"
898 "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n"
899 " return decoded_length;\n"
902 " if(!first_call && more==val_ptr->n_elements && "
903 "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n"
904 " return decoded_length;\n"
911 /* BER_encode_TLV() */
914 "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&"
915 " p_td, unsigned p_coding) const\n"
917 " BER_chk_descr(p_td);\n"
918 " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n"
920 " new_tlv=ASN_BER_TLV_t::construct(NULL);\n"
921 " TTCN_EncDec_ErrorContext ec;\n"
922 " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
923 " ec.set_msg(\"Component #%%d: \", elem_i);\n"
924 " new_tlv->add_TLV((*this)[elem_i].BER_encode_TLV"
925 "(*p_td.oftype_descr, p_coding));\n"
929 " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n"
933 /* BER_decode_TLV() */
934 "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,"
935 " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n"
937 " BER_chk_descr(p_td);\n"
938 " ASN_BER_TLV_t stripped_tlv;\n"
939 " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n"
940 " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \","
942 " stripped_tlv.chk_constructed_flag(TRUE);\n"
944 " val_ptr = new recordof_setof_struct;\n"
945 " val_ptr->ref_count = 1;\n"
946 " val_ptr->n_elements = 0;\n"
947 " val_ptr->value_elements = NULL;\n"
949 " ASN_BER_TLV_t tmp_tlv;\n"
950 " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n"
951 " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n"
952 " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, "
954 " val_ptr->value_elements = (%s**)reallocate_pointers("
955 "(void**)val_ptr->value_elements, val_ptr->n_elements, "
956 "val_ptr->n_elements + 1);\n"
957 " val_ptr->n_elements++;\n"
958 " val_ptr->value_elements[val_ptr->n_elements - 1] = new %s;\n"
959 " val_ptr->value_elements[val_ptr->n_elements - 1]->BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, "
961 " ec_2.set_msg(\"%%d: \", val_ptr->n_elements);\n"
967 , sdef
->kind
==SET_OF
?" new_tlv->sort_tlvs();\n":""
971 if(sdef
->has_opentypes
) {
972 /* BER_decode_opentypes() */
975 "void BER_decode_opentypes(TTCN_Type_list& p_typelist,"
976 " unsigned L_form);\n");
979 "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist,"
980 " unsigned L_form)\n"
982 " p_typelist.push(this);\n"
983 " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n"
984 " TTCN_EncDec_ErrorContext ec_1;\n"
985 " for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
986 " ec_1.set_msg(\"%%d: \", elem_i);\n"
987 " val_ptr->value_elements[elem_i]->BER_decode_opentypes(p_typelist,"
990 " p_typelist.pop();\n"
1000 "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, "
1001 "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field"
1002 ", boolean first_call){\n"
1003 " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
1004 " limit-=prepaddlength;\n"
1005 " int decoded_length=0;\n"
1006 " int decoded_field_length=0;\n"
1007 " size_t start_of_field=0;\n"
1008 " if(first_call) {\n"
1010 " val_ptr=new recordof_setof_struct;\n"
1011 " val_ptr->ref_count=1;\n"
1012 " val_ptr->n_elements=0;\n"
1013 " val_ptr->value_elements=NULL;\n"
1015 " int start_field=val_ptr->n_elements;\n"
1016 " if(p_td.raw->fieldlength || sel_field!=-1){\n"
1018 " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
1019 " for(a=0;a<sel_field;a++){\n"
1020 " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
1021 "p_buf,limit,top_bit_ord,TRUE);\n"
1022 " if(decoded_field_length < 0) return decoded_field_length;\n"
1023 " decoded_length+=decoded_field_length;\n"
1024 " limit-=decoded_field_length;\n"
1026 " if(a==0) val_ptr->n_elements=0;\n"
1028 " int a=start_field;\n"
1030 " if(!first_call) return -1;\n"
1031 " val_ptr->n_elements=0;\n"
1032 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1035 " while(limit>0){\n"
1036 " start_of_field=p_buf.get_pos_bit();\n"
1037 " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit,"
1038 "top_bit_ord,TRUE);\n"
1039 " if(decoded_field_length < 0){\n"
1040 " delete &(*this)[a];\n"
1041 " val_ptr->n_elements--;\n"
1042 " p_buf.set_pos_bit(start_of_field);\n"
1043 " if(a>start_field){\n"
1044 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1046 " } else return -1;\n"
1048 " decoded_length+=decoded_field_length;\n"
1049 " limit-=decoded_field_length;\n"
1053 if (force_gen_seof
|| (sdef
->raw
.extension_bit
!=XDEFNO
&& sdef
->raw
.extension_bit
!=XDEFDEFAULT
)){
1055 " if (%s%sp_buf.get_last_bit()%s)\n"
1056 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1058 , force_gen_seof
? "EXT_BIT_NO != p_td.raw->extension_bit && ((EXT_BIT_YES != p_td.raw->extension_bit) ^ " : ""
1059 , (force_gen_seof
|| sdef
->raw
.extension_bit
== XDEFYES
) ? "" : "!"
1060 , force_gen_seof
? ")" : "");
1065 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
1068 "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td,"
1069 "RAW_enc_tree& myleaf) const{\n"
1070 " int encoded_length=0;\n"
1071 " int encoded_num_of_records=p_td.raw->fieldlength?"
1072 "smaller(val_ptr->n_elements, p_td.raw->fieldlength)"
1073 ":val_ptr->n_elements;\n"
1074 " myleaf.isleaf=FALSE;\n"
1075 " myleaf.rec_of=TRUE;\n"
1076 " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n"
1077 " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n"
1078 " for(int a=0;a<encoded_num_of_records;a++){\n"
1079 " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf,"
1080 "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n"
1081 " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr,"
1082 "*myleaf.body.node.nodes[a]);\n"
1084 " return myleaf.length=encoded_length;\n}\n\n"
1089 if (xer_needed
) { /* XERSTUFF encoder codegen for record-of, RT1 */
1091 "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n");
1093 /* Write the body of the XER encoder/decoder functions. The declaration
1094 * is written by def_encdec() in encdec.c */
1095 src
= mputprintf(src
,
1096 "boolean %s::can_start(const char *name, const char *uri, "
1097 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
1098 " boolean e_xer = is_exer(flavor);\n"
1099 " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return "
1100 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
1101 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
1104 if (!force_gen_seof
&& sdef
->nFollowers
) {
1105 /* If there are optional fields following the record-of, then seeing
1106 * {any XML tag that belongs to those fields} where the record-of may be
1107 * means that the record-of is empty. */
1109 src
= mputstr(src
, "{\n");
1110 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
1111 src
= mputprintf(src
,
1112 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
1113 , sdef
->followers
[f
].type
1114 , sdef
->followers
[f
].typegen
1121 else src
= mputstr(src
, "return TRUE;\n");
1123 src
= mputprintf(src
,
1124 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
1129 src
= mputprintf(src
,
1130 "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n"
1131 " size_t num_collected;\n"
1132 " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n"
1133 /* The above may throw but then nothing was allocated. */
1134 " if (val_ptr) try {\n"
1136 " size_t num_new;\n"
1137 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1138 " bool def_ns_1 = false;"
1139 " new_ns = (*this)[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n"
1140 " merge_ns(collected_ns, num_collected, new_ns, num_new);\n"
1141 " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */
1145 /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g.
1146 * encoding an unbound value. */
1147 " while (num_collected > 0) Free(collected_ns[--num_collected]);\n"
1148 " Free(collected_ns);\n"
1151 " num = num_collected;\n"
1152 " return collected_ns;\n"
1157 "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, "
1158 "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n"
1159 " if (val_ptr == 0) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */
1160 " int encoded_length=(int)p_buf.get_len();\n"
1161 " boolean e_xer = is_exer(p_flavor);\n"
1162 " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
1163 " || (p_flavor & USE_TYPE_ATTR)));\n"
1164 " boolean indenting = !is_canonical(p_flavor) && own_tag;\n"
1165 "%s" /* Factor out p_indent if not attribute */
1166 " if (val_ptr->n_elements==0) {\n" /* Empty record of */
1168 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1169 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
1171 if (force_gen_seof
|| sdef
->xerAttribute
) {
1173 " if (e_xer%s) {\n" /* Empty attribute. */
1174 " begin_attribute(p_td, p_buf);\n"
1175 " p_buf.put_c('\\'');\n"
1177 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1179 if (force_gen_seof
|| !sdef
->xerAttribute
) {
1184 " {\n" /* Do empty element tag */
1186 " p_buf.put_c('<');\n"
1187 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1188 " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n"
1189 /* namespace declarations for the toplevel element */
1190 " if (e_xer && p_indent==0)\n"
1192 " size_t num_collected = 0;\n"
1193 " char **collected_ns = NULL;\n"
1194 " bool def_ns = false;\n"
1195 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
1196 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
1197 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
1198 " Free(collected_ns[cur_coll]);\n"
1200 " Free(collected_ns);\n"
1203 " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n"
1206 " else {\n" /* Not empty record of. Start tag or attribute */
1207 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1208 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
1210 if (sdef
->xerAnyAttrElem
) {
1212 " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n"
1213 " static const universal_char sp = { 0,0,0,' ' };\n"
1214 " static const universal_char tb = { 0,0,0,9 };\n"
1215 " size_t buf_len = p_buf.get_len(), shorter = 0;\n"
1216 " const unsigned char * const buf_data = p_buf.get_data();\n"
1217 " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n"
1218 " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n"
1219 " unsigned char saved[4];\n"
1220 " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n"
1221 " p_buf.increase_length(-shorter);\n"
1222 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1223 " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n"
1224 " if (val_ptr->value_elements[i] == NULL) {\n"
1225 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
1226 " \"Encoding an unbound universal charstring value.\");\n"
1229 " size_t len = val_ptr->value_elements[i]->lengthof();\n"
1231 " const UNIVERSAL_CHARSTRING_ELEMENT& ue = (*val_ptr->value_elements[i])[len - 1];\n"
1232 " if (sp == ue || tb == ue) --len;\n"
1235 " size_t j, sp_at = 0;\n"
1236 /* Find the "separators" in each string */
1237 " for (j = 0; j < len; j++) {\n"
1238 " UNIVERSAL_CHARSTRING_ELEMENT ue = (*val_ptr->value_elements[i])[j];\n"
1240 " if (sp == ue || tb == ue) {}\n"
1243 " if (sp == ue || tb == ue) sp_at = j;\n"
1246 " size_t buf_start = p_buf.get_len();\n"
1247 /* Now write them */
1248 " if (sp_at > 0) {\n"
1249 " char * ns = mprintf(\" xmlns:b%d='\", i);\n"
1250 " size_t ns_len = mstrlen(ns);\n"
1251 " p_buf.put_s(ns_len, (const unsigned char*)ns);\n"
1253 " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(*val_ptr->value_elements[i]));\n"
1254 " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
1255 // Ensure the namespace abides to its restrictions
1256 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
1257 " TTCN_Buffer ns_buf;\n"
1258 " before.encode_utf8(ns_buf);\n"
1260 " ns_buf.get_string(cs);\n"
1261 " check_namespace_restrictions(p_td, (const char*)cs);\n"
1264 " p_buf.put_c('\\'');\n"
1265 " p_buf.put_c(' ');\n"
1267 /* Keep just the "b%d" part from ns */
1268 " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n"
1269 " p_buf.put_c(':');\n"
1273 " p_buf.put_c(' ');\n"
1275 // Make sure the unqualified namespace is allowed
1276 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
1277 " check_namespace_restrictions(p_td, NULL);\n"
1281 " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(*val_ptr->value_elements[i]) + j);\n"
1282 " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
1283 // Put this attribute in a dummy element and walk through it to check its validity
1284 " TTCN_Buffer check_buf;\n"
1285 " check_buf.put_s(2, (unsigned char*)\"<a\");\n"
1286 " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n"
1287 " check_buf.put_s(2, (unsigned char*)\"/>\");"
1288 " XmlReaderWrap checker(check_buf);\n"
1289 " while (1 == checker.Read()) ;\n"
1292 " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */
1295 if (force_gen_seof
|| sdef
->xerAttribute
) {
1298 " begin_attribute(p_td, p_buf);\n"
1300 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1305 " p_buf.put_c('<');\n"
1306 " boolean write_ns = (e_xer && p_indent==0);\n"
1307 " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n"
1308 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1309 " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), "
1310 "(const unsigned char*)p_td.names[e_xer]);\n"
1312 /* namespace declarations for the toplevel element */
1313 " if (e_xer && p_indent==0)\n"
1315 " size_t num_collected = 0;\n"
1316 " char **collected_ns = NULL;\n"
1317 " bool def_ns = false;\n"
1318 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
1319 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
1320 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
1321 " Free(collected_ns[cur_coll]);\n"
1323 " Free(collected_ns);\n"
1324 " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n"
1326 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
1327 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
1329 if (sdef
->xmlValueList
) {
1330 src
=mputstr(src
, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */
1334 " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n"
1335 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
1336 " TTCN_EncDec_ErrorContext ec_1;\n"
1339 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1340 " if (i > 0 && !own_tag && 0 != emb_val &&\n"
1341 " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n"
1342 " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n"
1343 " if (0 != emb_val->embval_array_reg) {\n"
1344 " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n"
1345 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
1348 " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n"
1349 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
1351 " ++emb_val->embval_index;\n"
1353 " ec_1.set_msg(\"%d: \", i);\n"
1354 " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n"
1355 " (*this)[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n"
1357 " if (indenting && !is_exerlist(p_flavor)) {\n"
1359 if (sdef
->xmlValueList
) {
1360 src
=mputstr(src
, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */
1363 " do_indent(p_buf, p_indent);\n"
1365 if (force_gen_seof
|| sdef
->xerAttribute
) {
1367 " if (e_xer%s) p_buf.put_c('\\'');\n"
1369 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
1373 " p_buf.put_c('<');\n"
1374 " p_buf.put_c('/');\n"
1375 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
1376 " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n"
1378 if (sdef
->xerAnyAttrElem
) {
1379 src
= mputstr(src
, " }\n");
1382 " }\n" /* end if(no elements) */
1383 " return (int)p_buf.get_len() - encoded_length;\n"
1387 src
= mputprintf(src
, /* XERSTUFF decoder codegen for record-of */
1389 "// written by %s in " __FILE__
" at %d\n"
1391 "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, "
1392 "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n"
1393 " boolean e_xer = is_exer(p_flavor);\n"
1394 " int xerbits = p_td.xer_bits;\n"
1395 " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n"
1396 " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
1397 " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */
1398 /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */
1399 " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n"
1400 " int rd_ok=1, xml_depth=-1;\n"
1401 " *this = NULL_VALUE;\n" /* empty but initialized array */
1402 " int type = 0;\n" /* none */
1403 " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n"
1404 " type = p_reader.NodeType();\n"
1405 " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n"
1406 " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n"
1407 " || XML_READER_TYPE_ATTRIBUTE == type) {\n"
1408 " verify_name(p_reader, p_td, e_xer);\n"
1412 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n"
1413 " if (XML_READER_TYPE_TEXT == type) break;\n"
1416 " if (XML_READER_TYPE_ELEMENT == type) {\n"
1417 " verify_name(p_reader, p_td, e_xer);\n"
1418 " xml_depth = p_reader.Depth();\n"
1421 " }\n" /* endif(e_xer && list) */
1422 " }\n" /* next read */
1423 " else xml_depth = p_reader.Depth();\n"
1424 " p_flavor |= XER_RECOF;\n"
1425 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
1426 " TTCN_EncDec_ErrorContext ec_1;\n"
1428 , __FUNCTION__
, __LINE__
1434 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/
1435 " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */
1436 " size_t x_pos = 0;\n"
1437 " size_t x_len = strlen(x_val);\n"
1438 /* The string contains a bunch of values separated by whitespace.
1439 * Tokenize the string and create a new buffer which looks like
1440 * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>),
1441 * then use that to decode the value. */
1442 " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n"
1443 // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
1444 " x_pos += strlen(str) + 1;\n"
1445 " TTCN_Buffer buf_2;\n"
1446 " buf_2.put_c('<');\n"
1447 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
1448 " const char * const exer_name = p_td.oftype_descr->names[1];\n"
1449 " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n"
1450 /* If it has a namespace, chop off the '>' from the end */
1451 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n"
1452 " if (i_can_has_ns) {\n"
1453 " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n"
1454 " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n"
1455 " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n"
1456 " buf_2.put_s(2, (cbyte*)\"='\");\n"
1457 " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n"
1458 " buf_2.put_s(2, (cbyte*)\"'>\");\n"
1460 /* start tag completed */
1461 " buf_2.put_s(strlen(str), (cbyte*)str);\n"
1462 " buf_2.put_c('<');\n"
1463 " buf_2.put_c('/');\n"
1464 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
1465 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n"
1466 " XmlReaderWrap reader_2(buf_2);\n"
1467 " rd_ok = reader_2.Read();\n" /* Move to the start element. */
1468 " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n"
1469 /* Don't move to the #text, that's the callee's responsibility. */
1470 /* The call to the non-const operator[] creates a new element object,
1471 * then we call its XER_decode with the temporary XML reader. */
1472 " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n"
1473 " if (p_flavor & EXIT_ON_ERROR && !(*this)[val_ptr->n_elements - 1].is_bound()) {\n"
1474 " if (1 == val_ptr->n_elements) {\n"
1475 // Failed to decode even the first element
1478 // Some elements were successfully decoded -> only delete the last one
1479 " set_size(val_ptr->n_elements - 1);\n"
1481 " xmlFree(x_val);\n"
1484 " if (x_pos >= x_len) break;\n"
1486 " xmlFree(x_val);\n"
1487 " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n"
1488 /* Let the caller do AdvanceAttribute() */
1489 " else if (own_tag) {\n"
1490 " p_reader.Read();\n" /* on closing tag */
1491 " p_reader.Read();\n" /* past it */
1496 src
= mputprintf(src
,
1498 " if (p_flavor & PARENT_CLOSED) ;\n"
1499 /* Nothing to do, but do not advance past the parent's element */
1500 " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n"
1501 /* It's our XML empty element: nothing to do, skip past it */
1503 /* Note: there is no p_reader.Read() at the end of the loop below.
1504 * Each element is supposed to consume enough to leave the next element
1505 * well-positioned. */
1506 " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n"
1507 " type = p_reader.NodeType();\n"
1508 " if (XML_READER_TYPE_ELEMENT == type)\n"
1510 if (sdef
->xerAnyAttrElem
) {
1511 src
= mputprintf(src
,
1512 " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n"
1513 /* This is a record-of with ANY-ELEMENT applied, which is really meant
1514 * for the element type (a string), so behave like a record-of
1515 * (string with ANY-ELEMENT): call the non-const operator[]
1516 * to create a new element, then read the entire XML element into it. */
1517 " (*this)[val_ptr->n_elements] = (const char*)p_reader.ReadOuterXml();\n"
1518 /* Consume the element, then move ahead */
1519 " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n"
1520 " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n"
1525 /* An untagged record-of ends if it encounters an element with a name
1526 * that doesn't match its component */
1527 " if (!own_tag && !can_start((const char*)p_reader.LocalName(), "
1528 "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n"
1529 " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
1532 " ec_1.set_msg(\"%d: \", val_ptr->n_elements);\n"
1533 /* The call to the non-const operator[] creates the element */
1534 " (*this)[val_ptr->n_elements].XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n"
1536 " if (0 != emb_val && !own_tag && val_ptr->n_elements > 1) {\n"
1537 " ++emb_val->embval_index;\n"
1540 " else if (XML_READER_TYPE_END_ELEMENT == type) {\n"
1541 " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
1543 " verify_end(p_reader, p_td, xml_depth, e_xer);\n"
1544 " rd_ok = p_reader.Read();\n" /* move forward one last time */
1548 " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && val_ptr->n_elements > 0) {\n"
1549 " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n"
1550 " if (0 != emb_val->embval_array_reg) {\n"
1551 " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n"
1554 " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n"
1556 " rd_ok = p_reader.Read();\n"
1559 " rd_ok = p_reader.Read();\n"
1561 " }\n" /* next read */
1562 " }\n" /* if not empty element */
1563 " }\n" /* if not LIST */
1564 " if (!own_tag && e_xer && (p_td.xer_bits & XER_OPTIONAL) && val_ptr->n_elements == 0) {\n"
1565 " clean_up();\n" /* set it to unbound, so the OPTIONAL class sets it to omit */
1573 src
= mputprintf(src
,
1574 "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n"
1576 " if (!is_bound()) {\n"
1577 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
1578 " \"Encoding an unbound value of type %s.\");\n"
1581 " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n"
1582 " for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1583 " if (NULL != p_td.json && p_td.json->metainfo_unbound && !(*this)[i].is_bound()) {\n"
1584 // unbound elements are encoded as { "metainfo []" : "unbound" }
1585 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n"
1586 " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n"
1587 " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n"
1588 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n"
1591 " int ret_val = (*this)[i].JSON_encode(*p_td.oftype_descr, p_tok);\n"
1592 " if (0 > ret_val) break;\n"
1593 " enc_len += ret_val;\n"
1596 " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n"
1597 " return enc_len;\n"
1602 src
= mputprintf(src
,
1603 "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
1605 " json_token_t token = JSON_TOKEN_NONE;\n"
1606 " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
1607 " if (JSON_TOKEN_ERROR == token) {\n"
1608 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n"
1609 " return JSON_ERROR_FATAL;\n"
1611 " else if (JSON_TOKEN_ARRAY_START != token) {\n"
1612 " return JSON_ERROR_INVALID_TOKEN;\n"
1615 " for (int nof_elements = 0; true; ++nof_elements) {\n"
1616 " size_t buf_pos = p_tok.get_buf_pos();\n"
1618 " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n"
1619 // check for metainfo object
1620 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
1621 " if (JSON_TOKEN_OBJECT_START == token) {\n"
1622 " char* value = NULL;\n"
1623 " size_t value_len = 0;\n"
1624 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
1625 " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n"
1626 " 0 == strncmp(value, \"metainfo []\", 11)) {\n"
1627 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
1628 " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n"
1629 " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n"
1630 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
1631 " if (JSON_TOKEN_OBJECT_END == token) {\n"
1632 " dec_len += ret_val;\n"
1638 // metainfo object not found, jump back and let the element type decode it
1639 " p_tok.set_buf_pos(buf_pos);\n"
1641 " %s* val = new %s;\n"
1642 " ret_val = val->JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n"
1643 " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n"
1644 " p_tok.set_buf_pos(buf_pos);\n"
1648 " else if (JSON_ERROR_FATAL == ret_val) {\n"
1650 " if (p_silent) {\n"
1653 " return JSON_ERROR_FATAL;\n"
1655 " val_ptr->value_elements = (%s**)reallocate_pointers(\n"
1656 " (void**)val_ptr->value_elements, val_ptr->n_elements, nof_elements + 1);\n"
1657 " val_ptr->value_elements[nof_elements] = val;\n"
1658 " val_ptr->n_elements = nof_elements + 1;\n"
1659 " dec_len += ret_val;\n"
1661 " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n"
1662 " if (JSON_TOKEN_ARRAY_END != token) {\n"
1663 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n"
1664 " if (p_silent) {\n"
1667 " return JSON_ERROR_FATAL;\n"
1669 " return dec_len;\n"
1671 , name
, type
, type
, type
);
1674 def
= mputstr(def
, "};\n\n");
1676 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
1677 "class %s;\n", name
);
1678 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
1680 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
1682 /* Copied from record.c. */
1683 output
->header
.function_prototypes
=
1684 mputprintf(output
->header
.function_prototypes
,
1685 "extern boolean operator==(null_type null_value, const %s& "
1686 "other_value);\n", name
);
1687 output
->source
.function_bodies
=
1688 mputprintf(output
->source
.function_bodies
,
1689 "boolean operator==(null_type, const %s& other_value)\n"
1691 "if (other_value.val_ptr == NULL)\n"
1692 "TTCN_error(\"The right operand of comparison is an unbound value of "
1694 "return other_value.val_ptr->n_elements == 0;\n"
1695 "}\n\n", name
, dispname
);
1697 output
->header
.function_prototypes
=
1698 mputprintf(output
->header
.function_prototypes
,
1699 "inline boolean operator!=(null_type null_value, const %s& "
1701 "{ return !(null_value == other_value); }\n", name
);
1704 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1706 void defRecordOfClassMemAllocOptimized(const struct_of_def
*sdef
, output_struct
*output
)
1708 char *def
= NULL
, *src
= NULL
;
1709 const char *name
= sdef
->name
, *dispname
= sdef
->dispname
;
1710 const char *type
= sdef
->type
;
1711 boolean ber_needed
= force_gen_seof
|| (sdef
->isASN1
&& enable_ber());
1712 boolean raw_needed
= force_gen_seof
|| (sdef
->hasRaw
&& enable_raw());
1713 boolean text_needed
= force_gen_seof
|| (sdef
->hasText
&& enable_text());
1714 boolean xer_needed
= force_gen_seof
|| (sdef
->hasXer
&& enable_xer());
1715 boolean json_needed
= force_gen_seof
|| (sdef
->hasJson
&& enable_json());
1717 /* Class definition and private data members */
1718 def
= mputprintf(def
,
1720 "// written by %s in " __FILE__
" at %d\n"
1722 "class %s : public Base_Type {\n"
1724 "%s* value_elements;\n"
1726 , __FUNCTION__
, __LINE__
1730 /* private member functions */
1731 def
= mputprintf(def
,
1733 "friend boolean operator==(null_type null_value, "
1734 "const %s& other_value);\n", name
);
1736 def
= mputprintf(def
,
1737 "void copy_value(const %s& other_value);\n", name
);
1738 src
= mputprintf(src
,
1739 "void %s::copy_value(const %s& other_value)\n"
1741 "if (other_value.n_elements==-1) {\n"
1742 "TTCN_error(\"Copying an unbound value of type %s.\");\n"
1743 "} else if (other_value.n_elements==0) {\n"
1745 "value_elements = NULL;\n"
1747 "n_elements = other_value.n_elements;\n"
1748 "value_elements = new %s[n_elements];\n"
1749 "for (int act_elem=0; act_elem<n_elements; act_elem++) {\n"
1750 " if (other_value.value_elements[act_elem].is_bound()) {\n"
1751 " value_elements[act_elem] = other_value.value_elements[act_elem];\n"
1756 name
, name
, dispname
, type
);
1758 if (sdef
->kind
== SET_OF
) {
1759 /* callback function for comparison */
1760 def
= mputstr(def
, "static boolean compare_function("
1761 "const Base_Type *left_ptr, int left_index, "
1762 "const Base_Type *right_ptr, int right_index);\n");
1763 src
= mputprintf(src
, "boolean %s::compare_function("
1764 "const Base_Type *left_ptr, int left_index, "
1765 "const Base_Type *right_ptr, int right_index)\n"
1767 "if (((const %s*)left_ptr)->n_elements==-1) "
1768 "TTCN_error(\"The left operand of comparison is an unbound value of "
1770 "if (((const %s*)right_ptr)->n_elements==-1) "
1771 "TTCN_error(\"The right operand of comparison is an unbound value of "
1773 "if (((const %s*)left_ptr)->value_elements[left_index].is_bound()){\n"
1774 "if (((const %s*)right_ptr)->value_elements[right_index].is_bound()){\n"
1775 "return ((const %s*)left_ptr)->value_elements[left_index] == "
1776 "((const %s*)right_ptr)->value_elements[right_index];\n"
1777 "} else return FALSE;\n"
1779 "return !((const %s*)right_ptr)->value_elements[right_index].is_bound();\n"
1781 "}\n\n", name
, name
, dispname
, name
, dispname
, name
, name
, name
, name
, name
);
1784 /* public member functions */
1785 def
= mputstr(def
, "\npublic:\n");
1786 def
= mputprintf(def
, " typedef %s of_type;\n", sdef
->type
);
1789 def
= mputprintf(def
, "%s(): n_elements(-1), value_elements(NULL) {}\n", name
);
1791 def
= mputprintf(def
, "%s(null_type): n_elements(0), value_elements(NULL) {}\n", name
);
1793 /* copy constructor */
1794 def
= mputprintf(def
, "%s(const %s& other_value) { copy_value(other_value); }\n", name
, name
);
1797 def
= mputprintf(def
, "~%s() { clean_up(); }\n\n", name
);
1799 /* clean_up function */
1800 def
= mputstr(def
, "void clean_up();\n");
1801 src
= mputprintf(src
,
1802 "void %s::clean_up()\n"
1804 "if (n_elements!=-1) {\n"
1805 "delete[] value_elements;\n"
1806 "n_elements = -1;\n"
1807 "value_elements = NULL;\n"
1811 /* assignment operators */
1812 def
= mputprintf(def
, "%s& operator=(null_type other_value);\n", name
);
1813 src
= mputprintf(src
,
1814 "%s& %s::operator=(null_type)\n"
1818 "value_elements=NULL;\n"
1820 "}\n\n", name
, name
);
1822 def
= mputprintf(def
, "%s& operator=(const %s& other_value);\n\n",
1824 src
= mputprintf(src
,
1825 "%s& %s::operator=(const %s& other_value)\n"
1827 "if (other_value.n_elements == -1) "
1828 "TTCN_error(\"Assigning an unbound value of type %s.\");\n"
1829 "if (this != &other_value) {\n"
1831 "copy_value(other_value);\n"
1834 "}\n\n", name
, name
, name
, dispname
);
1836 /* comparison operators */
1837 def
= mputstr(def
, "boolean operator==(null_type other_value) const;\n");
1838 src
= mputprintf(src
,
1839 "boolean %s::operator==(null_type) const\n"
1841 "if (n_elements==-1)\n"
1842 "TTCN_error(\"The left operand of comparison is an unbound value of "
1844 "return n_elements==0;\n"
1845 "}\n\n", name
, dispname
);
1847 def
= mputprintf(def
, "boolean operator==(const %s& other_value) const;\n",
1849 src
= mputprintf(src
,
1850 "boolean %s::operator==(const %s& other_value) const\n"
1852 "if (n_elements==-1) "
1853 "TTCN_error(\"The left operand of comparison is an unbound value of type "
1855 "if (other_value.n_elements==-1) "
1856 "TTCN_error(\"The right operand of comparison is an unbound value of type "
1858 "if (this==&other_value) return TRUE;\n", name
, name
,
1859 dispname
, dispname
);
1861 if (sdef
->kind
== SET_OF
) {
1863 "return compare_set_of(this, n_elements, &other_value, "
1864 "other_value.n_elements, compare_function);\n");
1867 "if (n_elements!=other_value.n_elements) return FALSE;\n"
1868 "for (int elem_count = 0; elem_count < n_elements; elem_count++){\n"
1869 "if (value_elements[elem_count].is_bound()){\n"
1870 "if (other_value.value_elements[elem_count].is_bound()){\n"
1871 " if (value_elements[elem_count] != "
1872 "other_value.value_elements[elem_count]) return FALSE;\n"
1873 "} else return FALSE;\n"
1875 "if (other_value.value_elements[elem_count].is_bound()) "
1881 src
= mputstr(src
, "}\n\n");
1883 def
= mputstr(def
, "inline boolean operator!=(null_type other_value) const "
1884 "{ return !(*this == other_value); }\n");
1885 def
= mputprintf(def
, "inline boolean operator!=(const %s& other_value) "
1886 "const { return !(*this == other_value); }\n\n", name
);
1888 /* indexing operators */
1889 /* Non-const operator[] is allowed to extend the record-of */
1890 def
= mputprintf(def
, "%s& operator[](int index_value);\n", type
);
1891 src
= mputprintf(src
,
1892 "%s& %s::operator[](int index_value)\n"
1894 "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
1895 "using a negative index: %%d.\", index_value);\n"
1896 "if (index_value >= n_elements) set_size(index_value + 1);\n"
1897 "return value_elements[index_value];\n"
1898 "}\n\n", type
, name
, dispname
);
1900 def
= mputprintf(def
, "%s& operator[](const INTEGER& index_value);\n",
1902 src
= mputprintf(src
,
1903 "%s& %s::operator[](const INTEGER& index_value)\n"
1905 "index_value.must_bound(\"Using an unbound integer value for indexing "
1906 "a value of type %s.\");\n"
1907 "return (*this)[(int)index_value];\n"
1908 "}\n\n", type
, name
, dispname
);
1910 /* Const operator[] throws an error if over-indexing */
1911 def
= mputprintf(def
, "const %s& operator[](int index_value) const;\n",
1913 src
= mputprintf(src
,
1914 "const %s& %s::operator[](int index_value) const\n"
1916 "if (n_elements==-1) TTCN_error(\"Accessing an element in an unbound "
1917 "value of type %s.\");\n"
1918 "if (index_value<0) TTCN_error(\"Accessing an element of type %s "
1919 "using a negative index: %%d.\", index_value);\n"
1920 "if (index_value>=n_elements) TTCN_error(\"Index overflow in a value "
1921 "of type %s: The index is %%d, but the value has only %%d elements.\""
1922 ", index_value, n_elements);\n"
1923 "return value_elements[index_value];\n"
1924 "}\n\n", type
, name
, dispname
, dispname
, dispname
);
1926 def
= mputprintf(def
, "const %s& operator[](const INTEGER& index_value) "
1927 "const;\n\n", type
);
1928 src
= mputprintf(src
,
1929 "const %s& %s::operator[](const INTEGER& index_value) const\n"
1931 "index_value.must_bound(\"Using an unbound integer value for indexing "
1932 "a value of type %s.\");\n"
1933 "return (*this)[(int)index_value];\n"
1934 "}\n\n", type
, name
, dispname
);
1936 /* rotation operators */
1937 def
= mputprintf(def
,
1938 "%s operator<<=(int rotate_count) const;\n"
1939 "%s operator<<=(const INTEGER& rotate_count) const;\n"
1940 "%s operator>>=(int rotate_count) const;\n"
1941 "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
1942 name
, name
, name
, name
);
1943 src
= mputprintf(src
,
1944 "%s %s::operator<<=(int rotate_count) const\n"
1946 "return *this >>= (-rotate_count);\n"
1948 "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
1950 "rotate_count.must_bound(\""
1951 "Unbound integer operand of rotate left operator.\");\n"
1952 "return *this >>= (int)(-rotate_count);\n"
1954 "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
1956 "rotate_count.must_bound(\""
1957 "Unbound integer operand of rotate right operator.\");\n"
1958 "return *this >>= (int)rotate_count;\n"
1960 "%s %s::operator>>=(int rotate_count) const\n"
1962 "if (n_elements==-1) TTCN_error(\"Performing rotation operation on an "
1963 "unbound value of type %s.\");\n"
1964 "if (n_elements==0) return *this;\n"
1966 "if (rotate_count>=0) rc = rotate_count %% n_elements;\n"
1967 "else rc = n_elements - ((-rotate_count) %% n_elements);\n"
1968 "if (rc == 0) return *this;\n"
1970 "ret_val.set_size(n_elements);\n"
1971 "for (int i=0; i<n_elements; i++) {\n"
1972 "if (value_elements[i].is_bound()) "
1973 "ret_val.value_elements[(i+rc)%%n_elements] = value_elements[i];\n"
1977 name
, name
, name
, name
, name
, name
, name
, name
, dispname
, name
);
1980 def
= mputprintf(def
,
1981 "%s operator+(const %s& other_value) const;\n\n", name
, name
);
1982 src
= mputprintf(src
,
1983 "%s %s::operator+(const %s& other_value) const\n"
1985 "if (n_elements==-1 || other_value.n_elements==-1) "
1986 "TTCN_error(\"Unbound operand of %s concatenation.\");\n"
1987 "if (n_elements==0) return other_value;\n"
1988 "if (other_value.n_elements==0) return *this;\n"
1990 "ret_val.set_size(n_elements+other_value.n_elements);\n"
1991 "for (int i=0; i<n_elements; i++) {\n"
1992 "if (value_elements[i].is_bound()) "
1993 "ret_val.value_elements[i] = value_elements[i];\n"
1995 "for (int i=0; i<other_value.n_elements; i++) {\n"
1996 "if (other_value.value_elements[i].is_bound()) "
1997 "ret_val.value_elements[i+n_elements] = other_value.value_elements[i];\n"
2000 "}\n\n", name
, name
, name
, dispname
, name
);
2003 def
= mputprintf(def
,
2004 "%s substr(int index, int returncount) const;\n\n", name
);
2005 src
= mputprintf(src
,
2006 "%s %s::substr(int index, int returncount) const\n"
2008 "if (n_elements==-1) TTCN_error(\"The first argument of substr() is an "
2009 "unbound value of type %s.\");\n"
2010 "check_substr_arguments(n_elements, index, returncount, \"%s\",\"element\");\n"
2012 "ret_val.set_size(returncount);\n"
2013 "for (int i=0; i<returncount; i++) {\n"
2014 "if (value_elements[i+index].is_bound()) "
2015 "ret_val.value_elements[i] = value_elements[i+index];\n"
2018 "}\n\n", name
, name
, dispname
, dispname
, name
);
2021 def
= mputprintf(def
,
2022 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
2023 src
= mputprintf(src
,
2024 "%s %s::replace(int index, int len, const %s& repl) const\n"
2026 "if (n_elements==-1) TTCN_error(\"The first argument of replace() is an "
2027 "unbound value of type %s.\");\n"
2028 "if (repl.n_elements==-1) TTCN_error(\"The fourth argument of replace() "
2029 "is an unbound value of type %s.\");\n"
2030 "check_replace_arguments(n_elements, index, len, \"%s\",\"element\");\n"
2032 "ret_val.set_size(n_elements + repl.n_elements - len);\n"
2033 "for (int i = 0; i < index; i++) {\n"
2034 "if (value_elements[i].is_bound()) "
2035 "ret_val.value_elements[i] = value_elements[i];\n"
2037 "for (int i = 0; i < repl.n_elements; i++) {\n"
2038 "if (repl.value_elements[i].is_bound()) "
2039 "ret_val.value_elements[i+index] = repl.value_elements[i];\n"
2041 "for (int i = 0; i < n_elements - index - len; i++) {\n"
2042 "if (value_elements[index+i+len].is_bound()) "
2043 "ret_val.value_elements[index+i+repl.n_elements] = value_elements[index+i+len];\n"
2046 "}\n\n", name
, name
, name
, dispname
, dispname
, dispname
, name
);
2047 def
= mputprintf(def
,
2048 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
2050 src
= mputprintf(src
,
2051 "%s %s::replace(int index, int len, const %s_template& repl) const\n"
2053 "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
2054 "replace() is a template with non-specific value.\");\n"
2055 "return replace(index, len, repl.valueof());\n"
2056 "}\n\n", name
, name
, name
);
2058 /* set_size function */
2059 def
= mputstr(def
, "void set_size(int new_size);\n");
2060 src
= mputprintf(src
, "void %s::set_size(int new_size)\n"
2062 "if (new_size<0) TTCN_error(\"Internal error: Setting a negative size for "
2063 "a value of type %s.\");\n"
2064 "if (new_size==n_elements) return;\n"
2065 "if (new_size==0) {\n"
2067 " n_elements = 0;\n"
2068 " value_elements = NULL;\n"
2071 "%s* new_elem_v = new %s[new_size];\n"
2072 "for (int act_elem = 0; act_elem<n_elements; act_elem++) {\n"
2073 " if (act_elem>=new_size) break;\n"
2074 " if (value_elements[act_elem].is_bound()) new_elem_v[act_elem] = value_elements[act_elem];\n"
2077 "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n"
2078 "if((n_elements/1000)!=(new_size/1000)) "
2079 "TTCN_warning(\"New size of type %s: %%d\",new_size);\n"
2081 "n_elements = new_size;\n"
2082 "value_elements = new_elem_v;\n"
2083 "}\n\n", name
, dispname
, type
, type
, dispname
);
2085 /* is_bound function */
2087 "inline boolean is_bound() const {return n_elements!=-1; }\n");
2089 /* is_present function */
2091 "inline boolean is_present() const { return is_bound(); }\n");
2093 /* is_value function */
2095 "boolean is_value() const;\n");
2096 src
= mputprintf(src
,
2097 "boolean %s::is_value() const\n"
2099 "if (n_elements==-1) return FALSE;\n"
2100 "for (int i = 0; i < n_elements; ++i) {\n"
2101 " if (!value_elements[i].is_value()) return FALSE;\n"
2106 /* sizeof operation */
2108 "int size_of() const;\n"
2109 "int n_elem() const { return size_of(); }\n");
2110 src
= mputprintf(src
,
2111 "int %s::size_of() const\n"
2113 "if (n_elements==-1) TTCN_error(\"Performing sizeof operation on an "
2114 "unbound value of type %s.\");\n"
2115 "return n_elements;\n"
2116 "}\n\n", name
, dispname
);
2118 /* lengthof operation */
2119 def
= mputstr(def
, "int lengthof() const;\n");
2120 src
= mputprintf(src
,
2121 "int %s::lengthof() const\n"
2123 "if (n_elements==-1) TTCN_error(\"Performing lengthof operation on an "
2124 "unbound value of type %s.\");\n"
2125 "for (int my_length=n_elements; my_length>0; my_length--) "
2126 "if (value_elements[my_length-1].is_bound()) return my_length;\n"
2128 "}\n\n", name
, dispname
);
2131 def
= mputstr(def
, "void log() const;\n");
2132 src
= mputprintf(src
,
2133 "void %s::log() const\n"
2135 "if (n_elements==-1) {;\n"
2136 "TTCN_Logger::log_event_unbound();\n"
2139 "switch (n_elements) {\n"
2141 "TTCN_Logger::log_event_str(\"{ }\");\n"
2144 "TTCN_Logger::log_event_str(\"{ \");\n"
2145 "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n"
2146 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
2147 "value_elements[elem_count].log();\n"
2149 "TTCN_Logger::log_event_str(\" }\");\n"
2153 /* set_param function */ /* this is an exact copy of the previous one in this source file, if we didn't forget... */
2154 def
= mputstr(def
, "void set_param(Module_Param& param);\n");
2155 src
= mputprintf(src
,
2156 "void %s::set_param(Module_Param& param)\n"
2158 " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n"
2159 " param.get_id()->next_name()) {\n"
2160 // Haven't reached the end of the module parameter name
2161 // => the name refers to one of the elements, not to the whole record of
2162 " char* param_field = param.get_id()->get_current_name();\n"
2163 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
2164 " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n"
2165 " \" index for %s type `%s'\");\n"
2167 " int param_index = -1;\n"
2168 " sscanf(param_field, \"%%d\", ¶m_index);\n"
2169 " (*this)[param_index].set_param(param);\n"
2172 " param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n"
2173 " Module_Param_Ptr mp = ¶m;\n"
2174 " if (param.get_type() == Module_Param::MP_Reference) {\n"
2175 " mp = param.get_referenced_param();\n"
2177 " switch (param.get_operation_type()) {\n"
2178 " case Module_Param::OT_ASSIGN:\n"
2179 " if (mp->get_type()==Module_Param::MP_Value_List && mp->get_size()==0) {\n"
2180 " *this = NULL_VALUE;\n"
2183 " switch (mp->get_type()) {\n"
2184 " case Module_Param::MP_Value_List:\n"
2185 " set_size(mp->get_size());\n"
2186 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2187 " Module_Param* const curr = mp->get_elem(i);\n"
2188 " if (curr->get_type()!=Module_Param::MP_NotUsed) {\n"
2189 " (*this)[i].set_param(*curr);\n"
2193 " case Module_Param::MP_Indexed_List:\n"
2194 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2195 " Module_Param* const curr = mp->get_elem(i);\n"
2196 " (*this)[curr->get_id()->get_index()].set_param(*curr);\n"
2200 " param.type_error(\"%s value\", \"%s\");\n"
2203 " case Module_Param::OT_CONCAT:\n"
2204 " switch (mp->get_type()) {\n"
2205 " case Module_Param::MP_Value_List: {\n"
2206 " if (!is_bound()) *this = NULL_VALUE;\n"
2207 " int start_idx = lengthof();\n"
2208 " for (size_t i=0; i<mp->get_size(); ++i) {\n"
2209 " Module_Param* const curr = mp->get_elem(i);\n"
2210 " if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n"
2211 " (*this)[start_idx+(int)i].set_param(*curr);\n"
2215 " case Module_Param::MP_Indexed_List:\n"
2216 " param.error(\"Cannot concatenate an indexed value list\");\n"
2219 " param.type_error(\"%s value\", \"%s\");\n"
2223 " TTCN_error(\"Internal error: Unknown operation type.\");\n"
2225 "}\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
2226 sdef
->kind
== RECORD_OF
? "record of" : "set of",
2227 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
,
2228 sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
2230 /* get param function */
2231 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
2234 "Module_Param* %s::get_param(Module_Param_Name& param_name) const\n"
2236 " if (!is_bound()) {\n"
2237 " return new Module_Param_Unbound();\n"
2239 " if (param_name.next_name()) {\n"
2240 // Haven't reached the end of the module parameter name
2241 // => the name refers to one of the elements, not to the whole record of
2242 " char* param_field = param_name.get_current_name();\n"
2243 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
2244 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
2245 " \"expected a valid index for %s type `%s'\");\n"
2247 " int param_index = -1;\n"
2248 " sscanf(param_field, \"%%d\", ¶m_index);\n"
2249 " return (*this)[param_index].get_param(param_name);\n"
2251 " Vector<Module_Param*> values;\n"
2252 " for (int i = 0; i < n_elements; ++i) {\n"
2253 " values.push_back((*this)[i].get_param(param_name));\n"
2255 " Module_Param_Value_List* mp = new Module_Param_Value_List();\n"
2256 " mp->add_list_with_implicit_ids(&values);\n"
2257 " values.clear();\n"
2259 "}\n\n", name
, sdef
->kind
== RECORD_OF
? "record of" : "set of", dispname
);
2261 /* encoding / decoding functions */
2262 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
2263 src
= mputprintf(src
,
2264 "void %s::encode_text(Text_Buf& text_buf) const\n"
2266 "if (n_elements==-1) "
2267 "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n"
2268 "text_buf.push_int(n_elements);\n"
2269 "for (int elem_count = 0; elem_count < n_elements; elem_count++)\n"
2270 "value_elements[elem_count].encode_text(text_buf);\n"
2271 "}\n\n", name
, dispname
);
2273 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
2274 src
= mputprintf(src
,
2275 "void %s::decode_text(Text_Buf& text_buf)\n"
2278 "n_elements = text_buf.pull_int().get_val();\n"
2279 "if (n_elements < 0) TTCN_error(\"Text decoder: Negative size "
2280 "was received for a value of type %s.\");\n"
2281 "if (n_elements==0) {\n"
2282 " value_elements = NULL;\n"
2285 "value_elements = new %s[n_elements];\n"
2286 "for (int elem_count = 0; elem_count < n_elements; elem_count++) {\n"
2287 " value_elements[elem_count].decode_text(text_buf);\n"
2289 "}\n\n", name
, dispname
, type
);
2291 if(ber_needed
|| raw_needed
|| text_needed
|| xer_needed
|| json_needed
)
2292 def_encdec(name
, &def
, &src
, ber_needed
, raw_needed
, text_needed
,
2293 xer_needed
, json_needed
, FALSE
);
2297 "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td,"
2298 " TTCN_Buffer& p_buf) const{\n"
2299 " int encoded_length=0;\n"
2300 " if(p_td.text->begin_encode){\n"
2301 " p_buf.put_cs(*p_td.text->begin_encode);\n"
2302 " encoded_length+=p_td.text->begin_encode->lengthof();\n"
2304 " if(n_elements==-1) {\n"
2305 " TTCN_EncDec_ErrorContext::error\n"
2306 " (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
2307 " if(p_td.text->end_encode){\n"
2308 " p_buf.put_cs(*p_td.text->end_encode);\n"
2309 " encoded_length+=p_td.text->end_encode->lengthof();\n"
2311 " return encoded_length;\n"
2313 " for(int a=0;a<n_elements;a++){\n"
2314 " if(a!=0 && p_td.text->separator_encode){\n"
2315 " p_buf.put_cs(*p_td.text->separator_encode);\n"
2316 " encoded_length+=p_td.text->separator_encode->lengthof();\n"
2318 " encoded_length+=value_elements[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n"
2320 " if(p_td.text->end_encode){\n"
2321 " p_buf.put_cs(*p_td.text->end_encode);\n"
2322 " encoded_length+=p_td.text->end_encode->lengthof();\n"
2324 " return encoded_length;\n"
2328 src
= mputprintf(src
,
2329 "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td,"
2330 " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err"
2331 ", boolean first_call){\n"
2332 " int decoded_length=0;\n"
2333 " size_t pos=p_buf.get_pos();\n"
2334 " boolean sep_found=FALSE;\n"
2335 " int sep_length=0;\n"
2337 " if(p_td.text->begin_decode){\n"
2339 " if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n"
2340 " if(no_err)return -1;\n"
2341 " TTCN_EncDec_ErrorContext::error\n"
2342 " (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'"
2343 " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)"
2347 " decoded_length+=tl;\n"
2348 " p_buf.increase_pos(tl);\n"
2350 " if(p_td.text->end_decode){\n"
2351 " limit.add_token(p_td.text->end_decode);\n"
2354 " if(p_td.text->separator_decode){\n"
2355 " limit.add_token(p_td.text->separator_decode);\n"
2358 " if(first_call) {\n"
2361 " int more=n_elements;\n"
2364 " pos=p_buf.get_pos();\n"
2365 " int len=val.TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n"
2366 " if(len==-1 || (len==0 && !limit.has_token())){\n"
2367 " p_buf.set_pos(pos);\n"
2369 " p_buf.set_pos(p_buf.get_pos()-sep_length);\n"
2370 " decoded_length-=sep_length;\n"
2374 " sep_found=FALSE;\n"
2375 " set_size(n_elements+1);\n"
2376 " value_elements[n_elements-1]=val;\n"
2377 " decoded_length+=len;\n"
2378 " if(p_td.text->separator_decode){\n"
2380 " if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n"
2383 " decoded_length+=tl;\n"
2384 " p_buf.increase_pos(tl);\n"
2386 " sep_found=TRUE;\n"
2387 " } else if(p_td.text->end_decode){\n"
2389 " if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n"
2390 " decoded_length+=tl;\n"
2391 " p_buf.increase_pos(tl);\n"
2392 " limit.remove_tokens(ml);\n"
2393 " return decoded_length;\n"
2395 " } else if(limit.has_token(ml)){\n"
2397 " if((tl=limit.match(p_buf,ml))==0){\n"
2398 " sep_found=FALSE;\n"
2406 " limit.remove_tokens(ml);\n"
2407 " if(p_td.text->end_decode){\n"
2409 " if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n"
2411 " if(!first_call){\n"
2412 " set_size(more);\n"
2416 " TTCN_EncDec_ErrorContext::error"
2417 "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'"
2418 " not found for '%s': \",(const char*)*(p_td.text->end_decode)"
2420 " return decoded_length;\n"
2422 " decoded_length+=tl;\n"
2423 " p_buf.increase_pos(tl);\n"
2425 " if(n_elements==0){\n"
2426 " if(p_td.text->end_decode || p_td.text->begin_decode) n_elements=0;\n"
2428 " if(no_err)return -1;\n"
2429 " TTCN_EncDec_ErrorContext::error"
2430 "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n"
2431 " return decoded_length;\n"
2434 " if(!first_call && more==n_elements && "
2435 "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n"
2436 " return decoded_length;\n"
2443 /* BER_encode_TLV() */
2446 "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&"
2447 " p_td, unsigned p_coding) const\n"
2449 " BER_chk_descr(p_td);\n"
2450 " ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n"
2452 " new_tlv=ASN_BER_TLV_t::construct(NULL);\n"
2453 " TTCN_EncDec_ErrorContext ec;\n"
2454 " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n"
2455 " ec.set_msg(\"Component #%%d: \", elem_i);\n"
2456 " new_tlv->add_TLV(value_elements[elem_i].BER_encode_TLV"
2457 "(*p_td.oftype_descr, p_coding));\n"
2461 " new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n"
2462 " return new_tlv;\n"
2465 /* BER_decode_TLV() */
2466 "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,"
2467 " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n"
2469 " BER_chk_descr(p_td);\n"
2470 " ASN_BER_TLV_t stripped_tlv;\n"
2471 " BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n"
2472 " TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \","
2474 " stripped_tlv.chk_constructed_flag(TRUE);\n"
2476 " size_t V_pos=0;\n"
2477 " ASN_BER_TLV_t tmp_tlv;\n"
2478 " TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n"
2479 " TTCN_EncDec_ErrorContext ec_2(\"0: \");\n"
2480 " while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, "
2482 " set_size(n_elements+1);\n"
2483 " value_elements[n_elements-1].BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, "
2485 " ec_2.set_msg(\"%%d: \", n_elements);\n"
2491 , sdef
->kind
==SET_OF
?" new_tlv->sort_tlvs();\n":""
2495 if(sdef
->has_opentypes
) {
2496 /* BER_decode_opentypes() */
2499 "void BER_decode_opentypes(TTCN_Type_list& p_typelist,"
2500 " unsigned L_form);\n");
2503 "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist,"
2504 " unsigned L_form)\n"
2506 " p_typelist.push(this);\n"
2507 " TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n"
2508 " TTCN_EncDec_ErrorContext ec_1;\n"
2509 " for(int elem_i=0; elem_i<n_elements; elem_i++) {\n"
2510 " ec_1.set_msg(\"%%d: \", elem_i);\n"
2511 " value_elements[elem_i].BER_decode_opentypes(p_typelist,"
2514 " p_typelist.pop();\n"
2524 "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, "
2525 "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field"
2526 ", boolean first_call){\n"
2527 " int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
2528 " limit-=prepaddlength;\n"
2529 " int decoded_length=0;\n"
2530 " int decoded_field_length=0;\n"
2531 " size_t start_of_field=0;\n"
2532 " if (first_call) set_size(0);\n"
2533 " int start_field=n_elements;\n"
2534 " if(p_td.raw->fieldlength || sel_field!=-1){\n"
2536 " if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
2537 " for(a=0;a<sel_field;a++){\n"
2538 " decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
2539 "p_buf,limit,top_bit_ord,TRUE);\n"
2540 " if(decoded_field_length < 0) return decoded_field_length;\n"
2541 " decoded_length+=decoded_field_length;\n"
2542 " limit-=decoded_field_length;\n"
2544 " if(a==0) n_elements=0;\n"
2546 " int a=start_field;\n"
2548 " if(!first_call) return -1;\n"
2550 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2553 " while(limit>0){\n"
2554 " start_of_field=p_buf.get_pos_bit();\n"
2555 " decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit,"
2556 "top_bit_ord,TRUE);\n"
2557 " if(decoded_field_length < 0){\n"
2558 /*" delete &(*this)[a];\n"*/
2560 " p_buf.set_pos_bit(start_of_field);\n"
2561 " if(a>start_field){\n"
2562 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2564 " } else return -1;\n"
2566 " decoded_length+=decoded_field_length;\n"
2567 " limit-=decoded_field_length;\n"
2571 if(sdef
->raw
.extension_bit
!=XDEFNO
&& sdef
->raw
.extension_bit
!=XDEFDEFAULT
){
2573 " if (%sp_buf.get_last_bit())\n"
2574 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2575 "+prepaddlength;\n", sdef
->raw
.extension_bit
== XDEFYES
? "" : "!");
2580 " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
2583 "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td,"
2584 "RAW_enc_tree& myleaf) const{\n"
2585 " int encoded_length=0;\n"
2586 " int encoded_num_of_records=p_td.raw->fieldlength?"
2587 "smaller(n_elements, p_td.raw->fieldlength):n_elements;\n"
2588 " myleaf.isleaf=FALSE;\n"
2589 " myleaf.rec_of=TRUE;\n"
2590 " myleaf.body.node.num_of_nodes=encoded_num_of_records;\n"
2591 " myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n"
2592 " for(int a=0;a<encoded_num_of_records;a++){\n"
2593 " myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf,"
2594 "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n"
2595 " encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr,"
2596 "*myleaf.body.node.nodes[a]);\n"
2598 " return myleaf.length=encoded_length;\n}\n\n"
2603 if (xer_needed
) { /* XERSTUFF encoder codegen for record-of, RT1 */
2605 "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;\n");
2607 /* Write the body of the XER encoder/decoder functions. The declaration
2608 * is written by def_encdec() in encdec.c */
2609 src
= mputprintf(src
,
2610 "boolean %s::can_start(const char *name, const char *uri, "
2611 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
2612 " boolean e_xer = is_exer(flavor);\n"
2613 " if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return "
2614 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
2615 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
2618 if (!force_gen_seof
&& sdef
->nFollowers
) {
2619 /* If there are optional fields following the record-of, then seeing
2620 * {any XML tag that belongs to those fields} where the record-of may be
2621 * means that the record-of is empty. */
2623 src
= mputstr(src
, "{\n");
2624 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
2625 src
= mputprintf(src
,
2626 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
2627 , sdef
->followers
[f
].type
2628 , sdef
->followers
[f
].typegen
2635 else src
= mputstr(src
, "return TRUE;\n");
2637 src
= mputprintf(src
,
2638 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
2643 src
= mputprintf(src
,
2644 "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {\n"
2645 " size_t num_collected;\n"
2646 " char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns);\n"
2647 /* The above may throw but then nothing was allocated. */
2648 " if (n_elements!=-1) try {\n"
2650 " size_t num_new;\n"
2651 " for (int i = 0; i < n_elements; ++i) {\n"
2652 " bool def_ns_1 = false;"
2653 " new_ns = value_elements[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1);\n"
2654 " merge_ns(collected_ns, num_collected, new_ns, num_new);\n"
2655 " def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */
2659 /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g.
2660 * encoding an unbound value. */
2661 " while (num_collected > 0) Free(collected_ns[--num_collected]);\n"
2662 " Free(collected_ns);\n"
2665 " num = num_collected;\n"
2666 " return collected_ns;\n"
2671 "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, "
2672 "unsigned int p_flavor, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n"
2673 " if (n_elements==-1) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */
2674 " int encoded_length=(int)p_buf.get_len();\n"
2675 " boolean e_xer = is_exer(p_flavor);\n"
2676 " boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
2677 " || (p_flavor & USE_TYPE_ATTR)));\n"
2678 " boolean indenting = !is_canonical(p_flavor) && own_tag;\n"
2679 "%s" /* Factor out p_indent if not attribute */
2680 " if (n_elements==0) {\n" /* Empty record of */
2682 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2683 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
2685 if (force_gen_seof
|| sdef
->xerAttribute
) {
2687 " if (e_xer%s) {\n" /* Empty attribute. */
2688 " begin_attribute(p_td, p_buf);\n"
2689 " p_buf.put_c('\\'');\n"
2691 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2693 if (force_gen_seof
|| !sdef
->xerAttribute
) {
2698 " {\n" /* Do empty element tag */
2700 " p_buf.put_c('<');\n"
2701 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2702 " p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n"
2703 /* namespace declarations for the toplevel element */
2704 " if (e_xer && p_indent==0)\n"
2706 " size_t num_collected = 0;\n"
2707 " char **collected_ns = NULL;\n"
2708 " bool def_ns = false;\n"
2709 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
2710 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
2711 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
2712 " Free(collected_ns[cur_coll]);\n"
2714 " Free(collected_ns);\n"
2717 " p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n"
2720 " else {\n" /* Not empty record of. Start tag or attribute */
2721 , force_gen_seof
? " if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2722 : (sdef
->xerAttribute
? "" : " if (indenting) do_indent(p_buf, p_indent);\n")
2724 if (sdef
->xerAnyAttrElem
) {
2726 " if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n"
2727 " static const universal_char sp = { 0,0,0,' ' };\n"
2728 " static const universal_char tb = { 0,0,0,9 };\n"
2729 " size_t buf_len = p_buf.get_len(), shorter = 0;\n"
2730 " const unsigned char * const buf_data = p_buf.get_data();\n"
2731 " if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n"
2732 " if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n"
2733 " unsigned char saved[4];\n"
2734 " memcpy(saved, buf_data + (buf_len - shorter), shorter);\n"
2735 " p_buf.increase_length(-shorter);\n"
2736 " for (int i = 0; i < n_elements; ++i) {\n"
2737 " TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n"
2738 " size_t len = value_elements[i].lengthof();\n"
2740 " const UNIVERSAL_CHARSTRING_ELEMENT& ue = value_elements[i][len - 1];\n"
2741 " if (sp == ue || tb == ue) --len;\n"
2744 " size_t j, sp_at = 0;\n"
2745 /* Find the "separators" in each string */
2746 " for (j = 0; j < len; j++) {\n"
2747 " UNIVERSAL_CHARSTRING_ELEMENT ue = value_elements[i][j];\n"
2749 " if (sp == ue || tb == ue) {}\n"
2752 " if (sp == ue || tb == ue) sp_at = j;\n"
2755 " size_t buf_start = p_buf.get_len();\n"
2756 /* Now write them */
2757 " if (sp_at > 0) {\n"
2758 " char * ns = mprintf(\" xmlns:b%d='\", i);\n"
2759 " size_t ns_len = mstrlen(ns);\n"
2760 " p_buf.put_s(ns_len, (const unsigned char*)ns);\n"
2762 " UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(value_elements[i]));\n"
2763 " before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
2764 // Ensure the namespace abides to its restrictions
2765 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
2766 " TTCN_Buffer ns_buf;\n"
2767 " before.encode_utf8(ns_buf);\n"
2769 " ns_buf.get_string(cs);\n"
2770 " check_namespace_restrictions(p_td, (const char*)cs);\n"
2773 " p_buf.put_c('\\'');\n"
2774 " p_buf.put_c(' ');\n"
2776 /* Keep just the "b%d" part from ns */
2777 " p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n"
2778 " p_buf.put_c(':');\n"
2782 " p_buf.put_c(' ');\n"
2784 // Make sure the unqualified namespace is allowed
2785 " if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
2786 " check_namespace_restrictions(p_td, NULL);\n"
2790 " UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(value_elements[i]) + j);\n"
2791 " after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_indent, 0);\n"
2792 // Put this attribute in a dummy element and walk through it to check its validity
2793 " TTCN_Buffer check_buf;\n"
2794 " check_buf.put_s(2, (unsigned char*)\"<a\");\n"
2795 " check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n"
2796 " check_buf.put_s(2, (unsigned char*)\"/>\");"
2797 " XmlReaderWrap checker(check_buf);\n"
2798 " while (1 == checker.Read()) ;\n"
2801 " p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */
2804 if (force_gen_seof
|| sdef
->xerAttribute
) {
2807 " begin_attribute(p_td, p_buf);\n"
2809 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2814 " p_buf.put_c('<');\n"
2815 " boolean write_ns = (e_xer && p_indent==0);\n"
2816 " boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n"
2817 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2818 " p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), "
2819 "(const unsigned char*)p_td.names[e_xer]);\n"
2821 /* namespace declarations for the toplevel element */
2822 " if (e_xer && p_indent==0)\n"
2824 " size_t num_collected = 0;\n"
2825 " char **collected_ns = NULL;\n"
2826 " bool def_ns = false;\n"
2827 " collected_ns = collect_ns(p_td, num_collected, def_ns);\n"
2828 " for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
2829 " p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
2830 " Free(collected_ns[cur_coll]);\n"
2832 " Free(collected_ns);\n"
2833 " p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n"
2835 , force_gen_seof
? " if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
2836 : (sdef
->xerAttribute
? " if (indenting) do_indent(p_buf, p_indent);\n" : "")
2838 if (sdef
->xmlValueList
) {
2839 src
=mputstr(src
, " if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */
2843 " p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n"
2844 " TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
2845 " TTCN_EncDec_ErrorContext ec_1;\n"
2848 " for (int i = 0; i < n_elements; ++i) {\n"
2849 " if (i > 0 && !own_tag && 0 != emb_val &&\n"
2850 " emb_val->embval_index < (0 != emb_val->embval_array_reg ?\n"
2851 " emb_val->embval_array_reg->size_of() : emb_val->embval_array_opt->size_of())) {\n"
2852 " if (0 != emb_val->embval_array_reg) {\n"
2853 " (*emb_val->embval_array_reg)[emb_val->embval_index].XER_encode(\n"
2854 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
2857 " (*emb_val->embval_array_opt)[emb_val->embval_index].XER_encode(\n"
2858 " UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | EMBED_VALUES, p_indent+1, 0);\n"
2860 " ++emb_val->embval_index;\n"
2862 " ec_1.set_msg(\"%d: \", i);\n"
2863 " if (e_xer && (p_td.xer_bits & XER_LIST) && i>0) p_buf.put_c(' ');\n"
2864 " value_elements[i].XER_encode(*p_td.oftype_descr, p_buf, p_flavor, p_indent+own_tag, emb_val);\n"
2866 " if (indenting && !is_exerlist(p_flavor)) {\n"
2868 if (sdef
->xmlValueList
) {
2869 src
=mputstr(src
, " if (!e_xer) p_buf.put_c('\\n');\n"); /* !e_xer or GDMO */
2872 " do_indent(p_buf, p_indent);\n"
2874 if (force_gen_seof
|| sdef
->xerAttribute
) {
2876 " if (e_xer%s) p_buf.put_c('\\'');\n"
2878 , force_gen_seof
? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
2882 " p_buf.put_c('<');\n"
2883 " p_buf.put_c('/');\n"
2884 " if (e_xer) write_ns_prefix(p_td, p_buf);\n"
2885 " p_buf.put_s((size_t)p_td.namelens[e_xer]-!indenting, (const unsigned char*)p_td.names[e_xer]);\n"
2887 if (sdef
->xerAnyAttrElem
) {
2888 src
= mputstr(src
, " }\n");
2891 " }\n" /* end if(no elements) */
2892 " return (int)p_buf.get_len() - encoded_length;\n"
2896 src
= mputprintf(src
, /* XERSTUFF decoder codegen for record-of */
2898 "// written by %s in " __FILE__
" at %d\n"
2900 "int %s::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& p_reader, "
2901 "unsigned int p_flavor, unsigned int p_flavor2, embed_values_dec_struct_t* emb_val)\n{\n"
2902 " boolean e_xer = is_exer(p_flavor);\n"
2903 " int xerbits = p_td.xer_bits;\n"
2904 " if (p_flavor & XER_TOPLEVEL) xerbits &= ~UNTAGGED;\n"
2905 " boolean own_tag = !(e_xer && ((xerbits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))"
2906 " || (p_flavor & USE_TYPE_ATTR)));\n" /* incase the parent has USE-UNION */
2907 /* not toplevel anymore and remove the flags for USE-UNION the oftype doesn't need them */
2908 " p_flavor &= ~XER_TOPLEVEL & ~XER_LIST & ~USE_TYPE_ATTR;\n"
2909 " int rd_ok=1, xml_depth=-1;\n"
2910 " *this = NULL_VALUE;\n" /* empty but initialized array */
2911 " int type = 0;\n" /* none */
2912 " if (own_tag) for (rd_ok = p_reader.Ok(); rd_ok == 1; rd_ok = p_reader.Read()) {\n"
2913 " type = p_reader.NodeType();\n"
2914 " if (e_xer && (p_td.xer_bits & XER_ATTRIBUTE)) {\n"
2915 " if ((XML_READER_TYPE_ELEMENT == type && p_reader.MoveToFirstAttribute() == 1)\n"
2916 " || XML_READER_TYPE_ATTRIBUTE == type) {\n"
2917 " verify_name(p_reader, p_td, e_xer);\n"
2921 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n"
2922 " if (XML_READER_TYPE_TEXT == type) break;\n"
2925 " if (XML_READER_TYPE_ELEMENT == type) {\n"
2926 " verify_name(p_reader, p_td, e_xer);\n"
2927 " xml_depth = p_reader.Depth();\n"
2930 " }\n" /* endif(e_xer && list) */
2931 " }\n" /* next read */
2932 " else xml_depth = p_reader.Depth();\n"
2933 " p_flavor |= XER_RECOF;\n"
2935 , __FUNCTION__
, __LINE__
2941 " if (e_xer && (p_td.xer_bits & XER_LIST)) {\n" /* LIST decoding*/
2942 " char *x_val = (char*)p_reader.NewValue();\n" /* we own it */
2943 " size_t x_pos = 0;\n"
2944 " size_t x_len = strlen(x_val);\n"
2945 /* The string contains a bunch of values separated by whitespace.
2946 * Tokenize the string and create a new buffer which looks like
2947 * an XML element (<ns:name xmlns:ns='uri'>value</ns:name>),
2948 * then use that to decode the value. */
2949 " for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n"
2950 // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
2951 " x_pos += strlen(str) + 1;\n"
2952 " TTCN_Buffer buf_2;\n"
2953 " buf_2.put_c('<');\n"
2954 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
2955 " const char * const exer_name = p_td.oftype_descr->names[1];\n"
2956 " boolean i_can_has_ns = p_td.oftype_descr->my_module != 0 && p_td.oftype_descr->ns_index != -1;\n"
2957 /* If it has a namespace, chop off the '>' from the end */
2958 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1]-1-i_can_has_ns, (cbyte*)exer_name);\n"
2959 " if (i_can_has_ns) {\n"
2960 " const namespace_t * const pns = p_td.oftype_descr->my_module->get_ns(p_td.oftype_descr->ns_index);\n"
2961 " buf_2.put_s(7 - (*pns->px == 0), (cbyte*)\" xmlns:\");\n"
2962 " buf_2.put_s(strlen(pns->px), (cbyte*)pns->px);\n"
2963 " buf_2.put_s(2, (cbyte*)\"='\");\n"
2964 " buf_2.put_s(strlen(pns->ns), (cbyte*)pns->ns);\n"
2965 " buf_2.put_s(2, (cbyte*)\"'>\");\n"
2967 /* start tag completed */
2968 " buf_2.put_s(strlen(str), (cbyte*)str);\n"
2969 " buf_2.put_c('<');\n"
2970 " buf_2.put_c('/');\n"
2971 " write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
2972 " buf_2.put_s((size_t)p_td.oftype_descr->namelens[1], (cbyte*)exer_name);\n"
2973 " XmlReaderWrap reader_2(buf_2);\n"
2974 " rd_ok = reader_2.Read();\n" /* Move to the start element. */
2975 /* Don't move to the #text, that's the callee's responsibility. */
2976 /* The call to the non-const operator[] creates a new element object,
2977 * then we call its XER_decode with the temporary XML reader. */
2978 " (*this)[n_elements].XER_decode(*p_td.oftype_descr, reader_2, p_flavor, p_flavor2, 0);\n"
2979 " if (p_flavor & EXIT_ON_ERROR && !(*this)[n_elements - 1].is_bound()) {\n"
2980 " if (1 == n_elements) {\n"
2981 // Failed to decode even the first element
2984 // Some elements were successfully decoded -> only delete the last one
2985 " set_size(n_elements - 1);\n"
2987 " xmlFree(x_val);\n"
2990 " if (x_pos >= x_len) break;\n"
2992 " xmlFree(x_val);\n"
2993 " if ((p_td.xer_bits & XER_ATTRIBUTE)) ;\n"
2994 /* Let the caller do AdvanceAttribute() */
2995 " else if (own_tag) {\n"
2996 " p_reader.Read();\n" /* on closing tag */
2997 " p_reader.Read();\n" /* past it */
3002 src
= mputprintf(src
,
3004 " if (p_flavor & PARENT_CLOSED) ;\n"
3005 /* Nothing to do, but do not advance past the parent's element */
3006 " else if (own_tag && p_reader.IsEmptyElement()) rd_ok = p_reader.Read();\n"
3007 /* It's our XML empty element: nothing to do, skip past it */
3009 /* Note: there is no p_reader.Read() at the end of the loop below.
3010 * Each element is supposed to consume enough to leave the next element
3011 * well-positioned. */
3012 " for (rd_ok = own_tag ? p_reader.Read() : p_reader.Ok(); rd_ok == 1; ) {\n"
3013 " type = p_reader.NodeType();\n"
3014 " if (XML_READER_TYPE_ELEMENT == type)\n"
3016 if (sdef
->xerAnyAttrElem
) {
3017 src
= mputprintf(src
,
3018 " if (e_xer && (p_td.xer_bits & ANY_ELEMENT)) {\n"
3019 /* This is a record-of with ANY-ELEMENT applied, which is really meant
3020 * for the element type (a string), so behave like a record-of
3021 * (string with ANY-ELEMENT): call the non-const operator[]
3022 * to create a new element, then read the entire XML element into it. */
3023 " (*this)[n_elements] = (const char*)p_reader.ReadOuterXml();\n"
3024 /* Consume the element, then move ahead */
3025 " for (rd_ok = p_reader.Read(); rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) {}\n"
3026 " if (p_reader.NodeType() != XML_READER_TYPE_ELEMENT) rd_ok = p_reader.Read();\n"
3031 /* An untagged record-of ends if it encounters an element with a name
3032 * that doesn't match its component */
3033 " if (!own_tag && !can_start((const char*)p_reader.LocalName(), "
3034 "(const char*)p_reader.NamespaceUri(), p_td, p_flavor)) {\n"
3035 " for (; rd_ok == 1 && p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
3038 /* The call to the non-const operator[] creates the element */
3039 " operator [](n_elements).XER_decode(*p_td.oftype_descr, p_reader, p_flavor, p_flavor2, emb_val);\n"
3041 " if (0 != emb_val && !own_tag && n_elements > 1) {\n"
3042 " ++emb_val->embval_index;\n"
3045 " else if (XML_READER_TYPE_END_ELEMENT == type) {\n"
3046 " for (; p_reader.Depth() > xml_depth; rd_ok = p_reader.Read()) ;\n"
3048 " verify_end(p_reader, p_td, xml_depth, e_xer);\n"
3049 " rd_ok = p_reader.Read();\n" /* move forward one last time */
3053 " else if (XML_READER_TYPE_TEXT == type && 0 != emb_val && !own_tag && n_elements > 0) {\n"
3054 " UNIVERSAL_CHARSTRING emb_ustr((const char*)p_reader.Value());\n"
3055 " if (0 != emb_val->embval_array_reg) {\n"
3056 " (*emb_val->embval_array_reg)[emb_val->embval_index] = emb_ustr;\n"
3059 " (*emb_val->embval_array_opt)[emb_val->embval_index] = emb_ustr;\n"
3061 " rd_ok = p_reader.Read();\n"
3064 " rd_ok = p_reader.Read();\n"
3066 " }\n" /* next read */
3067 " }\n" /* if not empty element */
3068 " }\n" /* if not LIST */
3074 // JSON encode, RT1, mem. alloc. optimised
3075 src
= mputprintf(src
,
3076 "int %s::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const\n"
3078 " if (!is_bound()) {\n"
3079 " TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
3080 " \"Encoding an unbound value of type %s.\");\n"
3083 " int enc_len = p_tok.put_next_token(JSON_TOKEN_ARRAY_START, NULL);\n"
3084 " for (int i = 0; i < n_elements; ++i) {\n"
3085 " if (NULL != p_td.json && p_td.json->metainfo_unbound && !value_elements[i].is_bound()) {\n"
3086 // unbound elements are encoded as { "metainfo []" : "unbound" }
3087 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_START, NULL);\n"
3088 " enc_len += p_tok.put_next_token(JSON_TOKEN_NAME, \"metainfo []\");\n"
3089 " enc_len += p_tok.put_next_token(JSON_TOKEN_STRING, \"\\\"unbound\\\"\");\n"
3090 " enc_len += p_tok.put_next_token(JSON_TOKEN_OBJECT_END, NULL);\n"
3093 " int ret_val = value_elements[i].JSON_encode(*p_td.oftype_descr, p_tok);\n"
3094 " if (0 > ret_val) break;\n"
3095 " enc_len += ret_val;\n"
3098 " enc_len += p_tok.put_next_token(JSON_TOKEN_ARRAY_END, NULL);\n"
3099 " return enc_len;\n"
3103 // JSON decode, RT1, mem. alloc. optimised
3104 src
= mputprintf(src
,
3105 "int %s::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)\n"
3107 " json_token_t token = JSON_TOKEN_NONE;\n"
3108 " int dec_len = p_tok.get_next_token(&token, NULL, NULL);\n"
3109 " if (JSON_TOKEN_ERROR == token) {\n"
3110 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_BAD_TOKEN_ERROR, \"\");\n"
3111 " return JSON_ERROR_FATAL;\n"
3113 " else if (JSON_TOKEN_ARRAY_START != token) {\n"
3114 " return JSON_ERROR_INVALID_TOKEN;\n"
3117 " for (int nof_elements = 0; true; ++nof_elements) {\n"
3118 " size_t buf_pos = p_tok.get_buf_pos();\n"
3120 " if (NULL != p_td.json && p_td.json->metainfo_unbound) {\n"
3121 // check for metainfo object
3122 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
3123 " if (JSON_TOKEN_OBJECT_START == token) {\n"
3124 " char* value = NULL;\n"
3125 " size_t value_len = 0;\n"
3126 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
3127 " if (JSON_TOKEN_NAME == token && 11 == value_len &&\n"
3128 " 0 == strncmp(value, \"metainfo []\", 11)) {\n"
3129 " ret_val += p_tok.get_next_token(&token, &value, &value_len);\n"
3130 " if (JSON_TOKEN_STRING == token && 9 == value_len &&\n"
3131 " 0 == strncmp(value, \"\\\"unbound\\\"\", 9)) {\n"
3132 " ret_val = p_tok.get_next_token(&token, NULL, NULL);\n"
3133 " if (JSON_TOKEN_OBJECT_END == token) {\n"
3134 " dec_len += ret_val;\n"
3140 // metainfo object not found, jump back and let the element type decode it
3141 " p_tok.set_buf_pos(buf_pos);\n"
3144 " ret_val = val.JSON_decode(*p_td.oftype_descr, p_tok, p_silent);\n"
3145 " if (JSON_ERROR_INVALID_TOKEN == ret_val) {\n"
3146 " p_tok.set_buf_pos(buf_pos);\n"
3149 " else if (JSON_ERROR_FATAL == ret_val) {\n"
3150 " if (p_silent) {\n"
3153 " return JSON_ERROR_FATAL;\n"
3155 " set_size(nof_elements + 1);\n"
3156 " value_elements[nof_elements] = val;\n"
3157 " dec_len += ret_val;\n"
3159 " dec_len += p_tok.get_next_token(&token, NULL, NULL);\n"
3160 " if (JSON_TOKEN_ARRAY_END != token) {\n"
3161 " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_REC_OF_END_TOKEN_ERROR, \"\");\n"
3162 " if (p_silent) {\n"
3165 " return JSON_ERROR_FATAL;\n"
3167 " return dec_len;\n"
3172 def
= mputstr(def
, "};\n\n");
3174 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
3175 "class %s;\n", name
);
3176 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
3178 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
3180 /* Copied from record.c. */
3181 output
->header
.function_prototypes
=
3182 mputprintf(output
->header
.function_prototypes
,
3183 "extern boolean operator==(null_type null_value, const %s& "
3184 "other_value);\n", name
);
3185 output
->source
.function_bodies
=
3186 mputprintf(output
->source
.function_bodies
,
3187 "boolean operator==(null_type, const %s& other_value)\n"
3189 "if (other_value.n_elements==-1)\n"
3190 "TTCN_error(\"The right operand of comparison is an unbound value of "
3192 "return other_value.n_elements == 0;\n"
3193 "}\n\n", name
, dispname
);
3195 output
->header
.function_prototypes
=
3196 mputprintf(output
->header
.function_prototypes
,
3197 "inline boolean operator!=(null_type null_value, const %s& "
3199 "{ return !(null_value == other_value); }\n", name
);
3202 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3204 void defRecordOfClass2(const struct_of_def
*sdef
, output_struct
*output
)
3206 char *def
= NULL
, *src
= NULL
;
3207 const char *name
= sdef
->name
;
3208 const char *type
= sdef
->type
;
3209 boolean raw_needed
= force_gen_seof
|| (sdef
->hasRaw
&& enable_raw());
3210 boolean xer_needed
= force_gen_seof
|| (sdef
->hasXer
&& enable_xer());
3212 /* Class definition */
3213 def
= mputprintf(def
,
3214 "class %s : public Record_Of_Type {\n", name
);
3216 /* constant unbound element */
3217 def
= mputprintf(def
, "\nstatic const %s UNBOUND_ELEM;\n", type
);
3218 src
= mputprintf(src
, "\nconst %s %s::UNBOUND_ELEM;\n", type
, name
);
3220 /* public member functions */
3221 def
= mputstr(def
, "\npublic:\n");
3224 def
= mputprintf(def
, "%s(): Record_Of_Type() {}\n", name
);
3225 def
= mputprintf(def
, "%s(null_type other_value): Record_Of_Type(other_value) {}\n", name
);
3226 /* copy constructor */
3227 def
= mputprintf(def
, "%s(const %s& other_value): Record_Of_Type(other_value) {}\n", name
, name
);
3229 def
= mputprintf(def
, "~%s() { clean_up(); }\n\n", name
);
3231 /* assignment operators */
3232 def
= mputprintf(def
, "inline %s& operator=(null_type other_value) "
3233 "{ set_val(other_value); return *this; }\n", name
);
3234 def
= mputprintf(def
, "inline %s& operator=(const %s& other_value) "
3235 "{ set_value(&other_value); return *this; }\n\n", name
, name
);
3237 /* comparison operators */
3238 def
= mputprintf(def
, "inline boolean operator==(const %s& other_value) const "
3239 "{ return is_equal(&other_value); }\n", name
);
3240 def
= mputprintf(def
, "boolean operator!=(const %s& other_value) const "
3241 "{ return !is_equal(&other_value); }\n", name
);
3243 /* indexing operators */
3244 def
= mputprintf(def
,
3245 "%s& operator[](int index_value);\n"
3246 "%s& operator[](const INTEGER& index_value);\n"
3247 "const %s& operator[](int index_value) const;\n"
3248 "const %s& operator[](const INTEGER& index_value) const;\n",
3254 src
= mputprintf(src
,
3255 "%s& %s::operator[](int index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n"
3256 "%s& %s::operator[](const INTEGER& index_value) { return *(static_cast<%s*>(get_at(index_value))); }\n"
3257 "const %s& %s::operator[](int index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n"
3258 "const %s& %s::operator[](const INTEGER& index_value) const { return *(static_cast<const %s*>(get_at(index_value))); }\n\n",
3264 /* rotation operators */
3265 def
= mputprintf(def
,
3266 "%s operator<<=(int rotate_count) const;\n"
3267 "%s operator<<=(const INTEGER& rotate_count) const;\n"
3268 "%s operator>>=(int rotate_count) const;\n"
3269 "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
3270 name
, name
, name
, name
);
3271 src
= mputprintf(src
,
3272 "%s %s::operator<<=(int rotate_count) const\n"
3274 "return *this >>= (-rotate_count);\n"
3276 "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
3279 "return *((%s*)rotl(rotate_count, &rec_of));\n"
3281 "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
3284 "return *((%s*)rotr(rotate_count, &rec_of));\n"
3286 "%s %s::operator>>=(int rotate_count) const\n"
3289 "return *((%s*)rotr(rotate_count, &rec_of));\n"
3291 name
, name
, name
, name
, name
, name
, name
, name
, name
, name
, name
,
3295 def
= mputprintf(def
,
3296 "%s operator+(const %s& other_value) const;\n\n", name
, name
);
3297 src
= mputprintf(src
,
3298 "%s %s::operator+(const %s& other_value) const\n"
3301 "return *((%s*)concat(&other_value, &rec_of));\n"
3302 "}\n\n", name
, name
, name
, name
, name
);
3305 def
= mputprintf(def
,
3306 "%s substr(int index, int returncount) const;\n\n", name
);
3307 src
= mputprintf(src
,
3308 "%s %s::substr(int index, int returncount) const\n"
3311 "substr_(index, returncount, &rec_of);\n"
3313 "}\n\n", name
, name
, name
);
3316 def
= mputprintf(def
,
3317 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
3318 src
= mputprintf(src
,
3319 "%s %s::replace(int index, int len, const %s& repl) const\n"
3322 "replace_(index, len, &repl, &rec_of);\n"
3324 "}\n\n", name
, name
, name
, name
);
3325 def
= mputprintf(def
,
3326 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
3328 src
= mputprintf(src
,
3329 "%s %s::replace(int index, int len, const %s_template& repl) const\n"
3332 "replace_(index, len, &repl, &rec_of);\n"
3334 "}\n\n", name
, name
, name
, name
);
3336 def
= mputprintf(def
,
3337 "Base_Type* clone() const { return new %s(*this); }\n"
3338 "const TTCN_Typedescriptor_t* get_descriptor() const;\n"
3339 "const TTCN_Typedescriptor_t* get_elem_descr() const;\n"
3340 "Base_Type* create_elem() const;\n"
3341 "const Base_Type* get_unbound_elem() const;\n"
3342 "boolean is_set() const { return %s; }\n",
3344 (sdef
->kind
== SET_OF
) ? "TRUE" : "FALSE");
3346 src
= mputprintf(src
,
3347 "Base_Type* %s::create_elem() const { return new %s; }\n"
3348 "const Base_Type* %s::get_unbound_elem() const { return &UNBOUND_ELEM; }\n"
3349 "const TTCN_Typedescriptor_t* %s::get_descriptor() const { return &%s_descr_; }\n",
3354 /* helper functions called by enc/dec members of the ancestor class */
3356 def
= mputprintf(def
, "int rawdec_ebv() const { return %d; }\n",
3357 (int)sdef
->raw
.extension_bit
);
3360 def
= mputprintf(def
, "boolean isXerAttribute() const { return %s; }\n"
3361 "virtual boolean can_start_v(const char * name, const char *uri, "
3362 "XERdescriptor_t const& xd, unsigned int);\n"
3363 "static boolean can_start (const char * name, const char *uri, "
3364 "XERdescriptor_t const& xd, unsigned int);\n",
3365 sdef
->xerAttribute
? "TRUE" : "FALSE");
3366 src
= mputprintf(src
,
3367 /* The virtual can_start_v hands off to the static can_start.
3368 * We must make a virtual call in Record_Of_Type::XER_decode because
3369 * we don't know the actual type (derived from Record_Of_Type) */
3370 "boolean %s::can_start_v(const char *name, const char *uri, "
3371 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
3372 " return can_start(name, uri, xd, flavor);\n"
3374 "boolean %s::can_start(const char *name, const char *uri, "
3375 "XERdescriptor_t const& xd, unsigned int flavor) {\n"
3376 " boolean e_xer = is_exer(flavor);\n"
3377 /* if EXER and UNTAGGED, it can begin with the tag of the element,
3378 * otherwise it must be the tag of the type itself,
3379 * specified in the supplied parameter.
3380 * If flavor contains UNTAGGED, that's a signal to go directly
3381 * to the embedded type. */
3382 " if (!e_xer || !((xd.xer_bits|flavor) & UNTAGGED)) return "
3383 "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
3384 /* a record-of with ANY-ELEMENT can start with any tag
3385 * :-( with some exceptions )-: */
3386 " if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
3390 if (!force_gen_seof
&& sdef
->nFollowers
) {
3392 src
= mputstr(src
, "{\n");
3393 for (f
= 0; f
< sdef
->nFollowers
; ++f
) {
3394 src
= mputprintf(src
,
3395 " if (%s::can_start(name, uri, %s_xer_, flavor)) return FALSE;\n"
3396 , sdef
->followers
[f
].type
3397 , sdef
->followers
[f
].typegen
3405 src
= mputstr(src
, "return TRUE;\n");
3407 src
= mputprintf(src
,
3408 " return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF);\n"
3409 "}\n\n", sdef
->type
);
3410 def
= mputprintf(def
, "boolean isXmlValueList() const { return %s; }\n\n",
3411 sdef
->xmlValueList
? "TRUE" : "FALSE");
3414 /* The call in XER_decode is still there, can_start_v must exist. */
3416 "virtual boolean can_start_v(const char *, const char *, "
3417 "XERdescriptor_t const&, unsigned int) { return FALSE; }\n");
3421 def
= mputstr(def
, "};\n\n");
3423 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
3424 "class %s;\n", name
);
3425 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
3427 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
3431 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3433 void defRecordOfTemplate1(const struct_of_def
*sdef
, output_struct
*output
)
3435 char *def
= NULL
, *src
= NULL
;
3436 const char *name
= sdef
->name
, *dispname
= sdef
->dispname
;
3437 const char *type
= sdef
->type
;
3438 const char *base_class
= sdef
->kind
== RECORD_OF
? "Record_Of_Template" :
3439 "Restricted_Length_Template";
3441 /* Class definition and private data members */
3442 def
= mputprintf(def
,
3443 "class %s_template : public %s {\n"
3447 "%s_template **value_elements;\n"
3450 "unsigned int n_values;\n"
3451 "%s_template *list_value;\n"
3452 "} value_list;\n", name
, base_class
, type
, name
);
3453 if (sdef
->kind
== SET_OF
) {
3454 def
= mputprintf(def
,
3456 "unsigned int n_items;\n"
3457 "%s_template *set_items;\n"
3458 "} value_set;\n", type
);
3460 def
= mputstr(def
, "};\n");
3462 /* private member functions */
3464 /* copy_value function */
3465 def
= mputprintf(def
, "void copy_value(const %s& other_value);\n", name
);
3466 src
= mputprintf(src
,
3467 "void %s_template::copy_value(const %s& other_value)\n"
3469 "if (!other_value.is_bound()) "
3470 "TTCN_error(\"Initialization of a template of type %s with an unbound "
3472 "single_value.n_elements = other_value.size_of();\n"
3473 "single_value.value_elements = "
3474 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
3475 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3477 "if (other_value[elem_count].is_bound()) {\n"
3478 "single_value.value_elements[elem_count] = "
3479 "new %s_template(other_value[elem_count]);\n"
3481 "single_value.value_elements[elem_count] = new %s_template;\n"
3484 "set_selection(SPECIFIC_VALUE);\n"
3485 "}\n\n", name
, name
, dispname
, type
, type
, type
);
3487 /* copy_template function */
3488 def
= mputprintf(def
, "void copy_template(const %s_template& "
3489 "other_value);\n", name
);
3490 src
= mputprintf(src
,
3491 "void %s_template::copy_template(const %s_template& other_value)\n"
3493 "switch (other_value.template_selection) {\n"
3494 "case SPECIFIC_VALUE:\n"
3495 "single_value.n_elements = other_value.single_value.n_elements;\n"
3496 "single_value.value_elements = "
3497 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
3498 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3500 "if (UNINITIALIZED_TEMPLATE != "
3501 "other_value.single_value.value_elements[elem_count]->get_selection()) {\n"
3502 "single_value.value_elements[elem_count] = new %s_template"
3503 "(*other_value.single_value.value_elements[elem_count]);\n"
3505 "single_value.value_elements[elem_count] = new %s_template;\n"
3508 "case OMIT_VALUE:\n"
3510 "case ANY_OR_OMIT:\n"
3512 "case VALUE_LIST:\n"
3513 "case COMPLEMENTED_LIST:\n"
3514 "value_list.n_values = other_value.value_list.n_values;\n"
3515 "value_list.list_value = new %s_template[value_list.n_values];\n"
3516 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
3518 "value_list.list_value[list_count].copy_template("
3519 "other_value.value_list.list_value[list_count]);\n"
3520 "break;\n", name
, name
, type
, type
, type
, name
);
3521 if (sdef
->kind
== SET_OF
) {
3522 src
= mputprintf(src
,
3523 "case SUPERSET_MATCH:\n"
3524 "case SUBSET_MATCH:\n"
3525 "value_set.n_items = other_value.value_set.n_items;\n"
3526 "value_set.set_items = new %s_template[value_set.n_items];\n"
3527 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
3529 "value_set.set_items[set_count] = "
3530 "other_value.value_set.set_items[set_count];\n"
3533 src
= mputprintf(src
,
3535 "TTCN_error(\"Copying an uninitialized/unsupported template of type "
3539 "set_selection(other_value);\n"
3542 /* callback function for matching specific values */
3544 "static boolean match_function_specific(const Base_Type *value_ptr, "
3545 "int value_index, const Restricted_Length_Template *template_ptr, "
3546 "int template_index, boolean legacy);\n");
3547 src
= mputprintf(src
,
3548 "boolean %s_template::match_function_specific(const Base_Type *value_ptr, "
3549 "int value_index, const Restricted_Length_Template *template_ptr, "
3550 "int template_index, boolean legacy)\n"
3552 "if (value_index >= 0) return ((const %s_template*)template_ptr)->"
3553 "single_value.value_elements[template_index]->"
3554 "match((*(const %s*)value_ptr)[value_index], legacy);\n"
3555 "else return ((const %s_template*)template_ptr)->"
3556 "single_value.value_elements[template_index]->is_any_or_omit();\n"
3557 "}\n\n", name
, name
, name
, name
);
3559 if (sdef
->kind
== SET_OF
) {
3560 /* callback function for matching superset and subset */
3562 "static boolean match_function_set(const Base_Type *value_ptr, "
3563 "int value_index, const Restricted_Length_Template *template_ptr, "
3564 "int template_index, boolean legacy);\n");
3565 src
= mputprintf(src
,
3566 "boolean %s_template::match_function_set(const Base_Type *value_ptr, "
3567 "int value_index, const Restricted_Length_Template *template_ptr, "
3568 "int template_index, boolean legacy)\n"
3570 "if (value_index >= 0) return ((const %s_template*)template_ptr)->"
3571 "value_set.set_items[template_index].match("
3572 "(*(const %s*)value_ptr)[value_index], legacy);\n"
3573 "else return ((const %s_template*)template_ptr)->"
3574 "value_set.set_items[template_index].is_any_or_omit();\n"
3575 "}\n\n", name
, name
, name
, name
);
3577 /* callback function for log_match_heuristics */
3579 "static void log_function(const Base_Type *value_ptr, "
3580 "const Restricted_Length_Template *template_ptr,"
3581 " int index_value, int index_template, boolean legacy);\n");
3582 src
= mputprintf(src
,
3583 "void %s_template::log_function(const Base_Type *value_ptr, "
3584 "const Restricted_Length_Template *template_ptr,"
3585 " int index_value, int index_template, boolean legacy)\n"
3587 "if (value_ptr != NULL && template_ptr != NULL)"
3588 "((const %s_template*)template_ptr)"
3589 "->single_value.value_elements[index_template]"
3590 "->log_match((*(const %s*)value_ptr)[index_value], legacy);\n"
3591 "else if (value_ptr != NULL) (*(const %s*)value_ptr)[index_value].log();\n"
3592 "else if (template_ptr != NULL) ((const %s_template*)template_ptr)"
3593 "->single_value.value_elements[index_template]->log();\n"
3594 "}\n\n", name
, name
, name
, name
, name
);
3597 /* public member functions */
3598 def
= mputstr(def
, "\npublic:\n");
3601 def
= mputprintf(def
, "%s_template();\n", name
);
3602 src
= mputprintf(src
, "%s_template::%s_template()\n"
3604 "}\n\n", name
, name
);
3606 def
= mputprintf(def
, "%s_template(template_sel other_value);\n", name
);
3607 src
= mputprintf(src
, "%s_template::%s_template(template_sel other_value)\n"
3608 " : %s(other_value)\n"
3610 "check_single_selection(other_value);\n"
3611 "}\n\n", name
, name
, base_class
);
3613 def
= mputprintf(def
, "%s_template(null_type other_value);\n", name
);
3614 src
= mputprintf(src
, "%s_template::%s_template(null_type)\n"
3615 " : %s(SPECIFIC_VALUE)\n"
3617 "single_value.n_elements = 0;\n"
3618 "single_value.value_elements = NULL;\n"
3619 "}\n\n", name
, name
, base_class
);
3621 def
= mputprintf(def
, "%s_template(const %s& other_value);\n", name
, name
);
3622 src
= mputprintf(src
, "%s_template::%s_template(const %s& other_value)\n"
3624 "copy_value(other_value);\n"
3625 "}\n\n", name
, name
, name
);
3627 def
= mputprintf(def
, "%s_template(const OPTIONAL<%s>& other_value);\n",
3629 src
= mputprintf(src
,
3630 "%s_template::%s_template(const OPTIONAL<%s>& other_value)\n"
3632 "switch (other_value.get_selection()) {\n"
3633 "case OPTIONAL_PRESENT:\n"
3634 "copy_value((const %s&)other_value);\n"
3636 "case OPTIONAL_OMIT:\n"
3637 "set_selection(OMIT_VALUE);\n"
3640 "TTCN_error(\"Creating a template of type %s from an unbound optional "
3643 "}\n\n", name
, name
, name
, name
, dispname
);
3645 /* copy constructor */
3646 def
= mputprintf(def
, "%s_template(const %s_template& other_value);\n",
3648 src
= mputprintf(src
,
3649 "%s_template::%s_template(const %s_template& other_value)\n"
3652 "copy_template(other_value);\n"
3653 "}\n\n", name
, name
, name
, base_class
);
3656 def
= mputprintf(def
, "~%s_template();\n\n", name
);
3657 src
= mputprintf(src
,
3658 "%s_template::~%s_template()\n"
3661 "}\n\n", name
, name
);
3663 /* clean_up function */
3664 def
= mputstr(def
, "void clean_up();\n");
3665 src
= mputprintf(src
,
3666 "void %s_template::clean_up()\n"
3668 "switch (template_selection) {\n"
3669 "case SPECIFIC_VALUE:\n"
3670 "for (int elem_count = 0; elem_count < single_value.n_elements; "
3672 "delete single_value.value_elements[elem_count];\n"
3673 "free_pointers((void**)single_value.value_elements);\n"
3675 "case VALUE_LIST:\n"
3676 "case COMPLEMENTED_LIST:\n"
3677 "delete [] value_list.list_value;\n", name
);
3678 if (sdef
->kind
== SET_OF
) {
3681 "case SUPERSET_MATCH:\n"
3682 "case SUBSET_MATCH:\n"
3683 "delete [] value_set.set_items;\n");
3689 "template_selection = UNINITIALIZED_TEMPLATE;\n"
3692 /* assignment operators */
3693 def
= mputprintf(def
, "%s_template& operator=(template_sel other_value);\n",
3695 src
= mputprintf(src
,
3696 "%s_template& %s_template::operator=(template_sel other_value)\n"
3698 "check_single_selection(other_value);\n"
3700 "set_selection(other_value);\n"
3702 "}\n\n", name
, name
);
3704 def
= mputprintf(def
, "%s_template& operator=(null_type other_value);\n",
3706 src
= mputprintf(src
,
3707 "%s_template& %s_template::operator=(null_type)\n"
3710 "set_selection(SPECIFIC_VALUE);\n"
3711 "single_value.n_elements = 0;\n"
3712 "single_value.value_elements = NULL;\n"
3714 "}\n\n", name
, name
);
3716 def
= mputprintf(def
, "%s_template& operator=(const %s& other_value);\n",
3718 src
= mputprintf(src
,
3719 "%s_template& %s_template::operator=(const %s& other_value)\n"
3722 "copy_value(other_value);\n"
3724 "}\n\n", name
, name
, name
);
3726 def
= mputprintf(def
, "%s_template& operator=(const OPTIONAL<%s>& "
3727 "other_value);\n", name
, name
);
3728 src
= mputprintf(src
,
3729 "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n"
3732 "switch (other_value.get_selection()) {\n"
3733 "case OPTIONAL_PRESENT:\n"
3734 "copy_value((const %s&)other_value);\n"
3736 "case OPTIONAL_OMIT:\n"
3737 "set_selection(OMIT_VALUE);\n"
3740 "TTCN_error(\"Assignment of an unbound optional field to a template of "
3744 "}\n\n", name
, name
, name
, name
, dispname
);
3746 def
= mputprintf(def
, "%s_template& operator=(const %s_template& "
3747 "other_value);\n\n", name
, name
);
3748 src
= mputprintf(src
,
3749 "%s_template& %s_template::operator=(const %s_template& other_value)\n"
3751 "if (&other_value != this) {\n"
3753 "copy_template(other_value);\n"
3756 "}\n\n", name
, name
, name
);
3758 /* indexing operators */
3759 /* Non-const operator[] is allowed to extend */
3760 def
= mputprintf(def
, "%s_template& operator[](int index_value);\n", type
);
3761 src
= mputprintf(src
,
3762 "%s_template& %s_template::operator[](int index_value)\n"
3764 "if (index_value < 0) TTCN_error(\"Accessing an element of a template "
3765 "for type %s using a negative index: %%d.\", index_value);\n"
3766 "switch (template_selection)\n"
3768 " case SPECIFIC_VALUE:\n"
3769 " if(index_value < single_value.n_elements) break;\n"
3771 " case OMIT_VALUE:\n"
3772 " case ANY_VALUE:\n"
3773 " case ANY_OR_OMIT:\n"
3774 " case UNINITIALIZED_TEMPLATE:\n"
3775 " set_size(index_value + 1);\n"
3778 " TTCN_error(\"Accessing an "
3779 "element of a non-specific template for type %s.\");\n"
3782 "return *single_value.value_elements[index_value];\n"
3783 "}\n\n", type
, name
, dispname
, dispname
);
3785 def
= mputprintf(def
, "%s_template& operator[](const INTEGER& "
3786 "index_value);\n", type
);
3787 src
= mputprintf(src
,
3788 "%s_template& %s_template::operator[](const INTEGER& index_value)\n"
3790 "index_value.must_bound(\"Using an unbound integer value for indexing "
3791 "a template of type %s.\");\n"
3792 "return (*this)[(int)index_value];\n"
3793 "}\n\n", type
, name
, dispname
);
3795 /* Const operator[] throws an error if over-indexing */
3796 def
= mputprintf(def
, "const %s_template& operator[](int index_value) "
3798 src
= mputprintf(src
,
3799 "const %s_template& %s_template::operator[](int index_value) const\n"
3801 "if (index_value < 0) TTCN_error(\"Accessing an element of a template "
3802 "for type %s using a negative index: %%d.\", index_value);\n"
3803 "if (template_selection != SPECIFIC_VALUE) TTCN_error(\"Accessing an "
3804 "element of a non-specific template for type %s.\");\n"
3805 "if (index_value >= single_value.n_elements) "
3806 "TTCN_error(\"Index overflow in a template of type %s: "
3807 "The index is %%d, but the template has only %%d elements.\", "
3808 "index_value, single_value.n_elements);\n"
3809 "return *single_value.value_elements[index_value];\n"
3810 "}\n\n", type
, name
, dispname
, dispname
, dispname
);
3812 def
= mputprintf(def
, "const %s_template& operator[](const INTEGER& "
3813 "index_value) const;\n\n", type
);
3814 src
= mputprintf(src
,
3815 "const %s_template& %s_template::operator[](const INTEGER& index_value) "
3818 "index_value.must_bound(\"Using an unbound integer value for indexing "
3819 "a template of type %s.\");\n"
3820 "return (*this)[(int)index_value];\n"
3821 "}\n\n", type
, name
, dispname
);
3823 /* set_size function */
3824 def
= mputstr(def
, "void set_size(int new_size);\n");
3825 src
= mputprintf(src
, "void %s_template::set_size(int new_size)\n"
3827 "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size "
3828 "for a template of type %s.\");\n"
3829 "template_sel old_selection = template_selection;\n"
3830 "if (old_selection != SPECIFIC_VALUE) {\n"
3832 "set_selection(SPECIFIC_VALUE);\n"
3833 "single_value.n_elements = 0;\n"
3834 "single_value.value_elements = NULL;\n"
3836 "if (new_size > single_value.n_elements) {\n"
3837 "single_value.value_elements = (%s_template**)reallocate_pointers((void**)"
3838 "single_value.value_elements, single_value.n_elements, new_size);\n"
3839 "if (old_selection == ANY_VALUE || old_selection == ANY_OR_OMIT) {\n"
3840 "for (int elem_count = single_value.n_elements; elem_count < new_size; "
3842 "single_value.value_elements[elem_count] = new %s_template(ANY_VALUE);\n"
3844 "for (int elem_count = single_value.n_elements; elem_count < new_size; "
3846 "single_value.value_elements[elem_count] = new %s_template;\n"
3848 "single_value.n_elements = new_size;\n"
3849 "} else if (new_size < single_value.n_elements) {\n"
3850 "for (int elem_count = new_size; elem_count < single_value.n_elements; "
3852 "delete single_value.value_elements[elem_count];\n"
3853 "single_value.value_elements = (%s_template**)reallocate_pointers((void**)"
3854 "single_value.value_elements, single_value.n_elements, new_size);\n"
3855 "single_value.n_elements = new_size;\n"
3857 "}\n\n", name
, dispname
, type
, type
, type
, type
);
3860 def
= mputstr(def
, "int n_elem() const;\n");
3861 src
= mputprintf(src
,
3862 "int %s_template::n_elem() const\n"
3864 " switch (template_selection) {\n"
3865 " case SPECIFIC_VALUE:\n"
3866 " return single_value.n_elements;\n"
3868 " case VALUE_LIST:\n"
3869 " return value_list.n_values;\n"
3871 /* if (sdef->kind == SET_OF) {
3872 src = mputprintf(src,
3875 src
= mputstr(src
, " default:\n"
3876 " TTCN_error(\"Performing n_elem\");\n"
3881 /* sizeof operation */
3883 "int size_of(boolean is_size) const;\n"
3884 "inline int size_of() const { return size_of(TRUE); }\n"
3885 "inline int lengthof() const { return size_of(FALSE); }\n"
3887 src
= mputprintf(src
,
3888 "int %s_template::size_of(boolean is_size) const\n"
3890 "const char* op_name = is_size ? \"size\" : \"length\";\n"
3892 "boolean has_any_or_none;\n"
3893 "if (is_ifpresent) TTCN_error(\"Performing %%sof() operation on a "
3894 "template of type %s which has an ifpresent attribute.\", op_name);\n"
3895 "switch (template_selection)\n"
3897 "case SPECIFIC_VALUE: {\n"
3899 " has_any_or_none = FALSE;\n"
3900 " int elem_count = single_value.n_elements;\n"
3901 " if (!is_size) { while (elem_count>0 && !single_value.value_elements"
3902 "[elem_count-1]->is_bound()) elem_count--; }\n"
3903 " for (int i=0; i<elem_count; i++) {\n"
3904 " switch (single_value.value_elements[i]->get_selection()) {\n"
3905 " case OMIT_VALUE:\n"
3906 " TTCN_error(\"Performing %%sof() operation on a template of type "
3907 "%s containing omit element.\", op_name);\n"
3908 " case ANY_OR_OMIT:\n"
3909 " has_any_or_none = TRUE;\n"
3917 name
, dispname
, dispname
);
3918 if (sdef
->kind
== SET_OF
) {
3919 src
= mputprintf(src
,
3920 "case SUPERSET_MATCH:\n"
3921 "case SUBSET_MATCH: {\n"
3923 " has_any_or_none = FALSE;\n"
3924 " int elem_count = value_set.n_items;\n"
3925 " if (!is_size) { while (elem_count>0 && !value_set.set_items"
3926 "[elem_count-1].is_bound()) elem_count--; }\n"
3927 " for (int i=0; i<elem_count; i++) {\n"
3928 " switch (value_set.set_items[i].get_selection())\n"
3930 " case OMIT_VALUE:\n"
3931 " TTCN_error(\"Performing %%sof() operation on a template of type "
3932 "%s containing omit element.\", op_name);\n"
3933 " case ANY_OR_OMIT:\n"
3934 " has_any_or_none = TRUE;\n"
3941 " if (template_selection==SUPERSET_MATCH) {\n"
3942 " has_any_or_none = TRUE;\n"
3944 " int max_size = min_size;\n"
3946 " if (!has_any_or_none) { // [0,max_size]\n"
3947 " switch (length_restriction_type) {\n"
3948 " case NO_LENGTH_RESTRICTION:\n"
3949 " if (max_size==0) return 0;\n"
3950 " TTCN_error(\"Performing %%sof() operation on a template of "
3951 "type %s with no exact size.\", op_name);\n"
3952 " case SINGLE_LENGTH_RESTRICTION:\n"
3953 " if (length_restriction.single_length<=max_size)\n"
3954 " return length_restriction.single_length;\n"
3955 " TTCN_error(\"Performing %%sof() operation on an invalid "
3956 "template of type %s. The maximum size (%%d) contradicts the length "
3957 "restriction (%%d).\", op_name, max_size, "
3958 "length_restriction.single_length);\n"
3959 " case RANGE_LENGTH_RESTRICTION:\n"
3960 " if (max_size==length_restriction.range_length.min_length) {\n"
3961 " return max_size;\n"
3962 " } else if (max_size>length_restriction.range_length.min_length)"
3964 " TTCN_error(\"Performing %%sof() operation on a template of "
3965 "type %s with no exact size.\", op_name);\n"
3967 " TTCN_error(\"Performing %%sof() operation on an invalid "
3968 "template of type %s. Maximum size (%%d) contradicts the length "
3969 "restriction (%%d..%%d).\", op_name, max_size, "
3970 "length_restriction.range_length.min_length, "
3971 "length_restriction.range_length.max_length);\n"
3973 " TTCN_error(\"Internal error: Template has invalid length "
3974 "restriction type.\");\n"
3979 dispname
, dispname
, dispname
, dispname
, dispname
);
3981 src
= mputprintf(src
,
3982 "case OMIT_VALUE:\n"
3983 " TTCN_error(\"Performing %%sof() operation on a template of type %s "
3984 "containing omit value.\", op_name);\n"
3986 "case ANY_OR_OMIT:\n"
3988 " has_any_or_none = TRUE;\n"
3990 "case VALUE_LIST:\n"
3992 " if (value_list.n_values<1)\n"
3993 " TTCN_error(\"Performing %%sof() operation on a "
3994 "template of type %s containing an empty list.\", op_name);\n"
3995 " int item_size = value_list.list_value[0].size_of(is_size);\n"
3996 " for (unsigned int i = 1; i < value_list.n_values; i++) {\n"
3997 " if (value_list.list_value[i].size_of(is_size)!=item_size)\n"
3998 " TTCN_error(\"Performing %%sof() operation on a template of type "
3999 "%s containing a value list with different sizes.\", op_name);\n"
4001 " min_size = item_size;\n"
4002 " has_any_or_none = FALSE;\n"
4005 "case COMPLEMENTED_LIST:\n"
4006 " TTCN_error(\"Performing %%sof() operation on a template of type %s "
4007 "containing complemented list.\", op_name);\n"
4009 " TTCN_error(\"Performing %%sof() operation on an "
4010 "uninitialized/unsupported template of type %s.\", op_name);\n"
4012 "return check_section_is_single(min_size, has_any_or_none, "
4013 "op_name, \"a\", \"template of type %s\");\n"
4015 dispname
, dispname
, dispname
, dispname
, dispname
, dispname
);
4017 /* match operation */
4018 def
= mputprintf(def
, "boolean match(const %s& other_value, boolean legacy "
4019 "= FALSE) const;\n", name
);
4020 src
= mputprintf(src
,
4021 "boolean %s_template::match(const %s& other_value, boolean legacy) const\n"
4023 "if (!other_value.is_bound()) return FALSE;\n"
4024 "int value_length = other_value.size_of();\n"
4025 "if (!match_length(value_length)) return FALSE;\n"
4026 "switch (template_selection) {\n"
4027 "case SPECIFIC_VALUE:\n"
4028 "return match_%s_of(&other_value, value_length, this, "
4029 "single_value.n_elements, match_function_specific, legacy);\n"
4030 "case OMIT_VALUE:\n"
4033 "case ANY_OR_OMIT:\n"
4035 "case VALUE_LIST:\n"
4036 "case COMPLEMENTED_LIST:\n"
4037 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4039 "if (value_list.list_value[list_count].match(other_value, legacy)) "
4040 "return template_selection == VALUE_LIST;\n"
4041 "return template_selection == COMPLEMENTED_LIST;\n",
4042 name
, name
, sdef
->kind
== RECORD_OF
? "record" : "set");
4043 if (sdef
->kind
== SET_OF
) {
4045 "case SUPERSET_MATCH:\n"
4046 "case SUBSET_MATCH:\n"
4047 "return match_set_of(&other_value, value_length, this, "
4048 "value_set.n_items, match_function_set, legacy);\n");
4050 src
= mputprintf(src
,
4052 "TTCN_error(\"Matching with an uninitialized/unsupported template "
4058 /* is_bound function */
4060 "inline boolean is_bound() const \n"
4061 " {return template_selection != UNINITIALIZED_TEMPLATE; }\n");
4063 /* is_value operation */
4064 def
= mputstr(def
, "boolean is_value() const;\n");
4065 src
= mputprintf(src
,
4066 "boolean %s_template::is_value() const\n"
4068 "if (template_selection != SPECIFIC_VALUE || is_ifpresent) return false;\n"
4069 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4071 "if (!single_value.value_elements[elem_count]->is_value()) return false;\n"
4075 /* valueof operation */
4076 def
= mputprintf(def
, "%s valueof() const;\n", name
);
4077 src
= mputprintf(src
,
4078 "%s %s_template::valueof() const\n"
4080 "if (template_selection != SPECIFIC_VALUE || is_ifpresent) TTCN_error(\""
4081 "Performing a valueof or send operation on a non-specific template of type "
4084 "ret_val.set_size(single_value.n_elements);\n"
4085 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4087 "if (single_value.value_elements[elem_count]->is_bound()) {\n"
4088 "ret_val[elem_count] = single_value.value_elements[elem_count]->valueof();\n"
4091 "}\n\n", name
, name
, dispname
, name
);
4093 /* substr() predefined function for templates */
4094 def
= mputprintf(def
,
4095 "%s substr(int index, int returncount) const;\n\n", name
);
4096 src
= mputprintf(src
,
4097 "%s %s_template::substr(int index, int returncount) const\n"
4099 "if (!is_value()) TTCN_error(\"The first argument of function substr() is "
4100 "a template with non-specific value.\");\n"
4101 "return valueof().substr(index, returncount);\n"
4102 "}\n\n", name
, name
);
4104 /* replace() predefined function for templates */
4105 def
= mputprintf(def
,
4106 "%s replace(int index, int len, const %s_template& repl) const;\n\n", name
, name
);
4107 src
= mputprintf(src
,
4108 "%s %s_template::replace(int index, int len, const %s_template& repl) const\n"
4110 "if (!is_value()) TTCN_error(\"The first argument of function replace() is "
4111 "a template with non-specific value.\");\n"
4112 "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
4113 "replace() is a template with non-specific value.\");\n"
4114 "return valueof().replace(index, len, repl.valueof());\n"
4115 "}\n\n", name
, name
, name
);
4116 def
= mputprintf(def
,
4117 "%s replace(int index, int len, const %s& repl) const;\n\n", name
, name
);
4118 src
= mputprintf(src
,
4119 "%s %s_template::replace(int index, int len, const %s& repl) const\n"
4121 "if (!is_value()) TTCN_error(\"The first argument of function replace() is "
4122 "a template with non-specific value.\");\n"
4123 "return valueof().replace(index, len, repl);\n"
4124 "}\n\n", name
, name
, name
);
4126 /* value list and set handling operators */
4128 "void set_type(template_sel template_type, unsigned int list_length);\n");
4129 src
= mputprintf(src
,
4130 "void %s_template::set_type(template_sel template_type, "
4131 "unsigned int list_length)\n"
4134 "switch (template_type) {\n"
4135 "case VALUE_LIST:\n"
4136 "case COMPLEMENTED_LIST:\n"
4137 "value_list.n_values = list_length;\n"
4138 "value_list.list_value = new %s_template[list_length];\n"
4139 "break;\n", name
, name
);
4140 if (sdef
->kind
== SET_OF
) {
4141 src
= mputprintf(src
,
4142 "case SUPERSET_MATCH:\n"
4143 "case SUBSET_MATCH:\n"
4144 "value_set.n_items = list_length;\n"
4145 "value_set.set_items = new %s_template[list_length];\n"
4148 src
= mputprintf(src
,
4150 "TTCN_error(\"Internal error: Setting an invalid type for a template of "
4153 "set_selection(template_type);\n"
4156 def
= mputprintf(def
,
4157 "%s_template& list_item(unsigned int list_index);\n", name
);
4158 src
= mputprintf(src
,
4159 "%s_template& %s_template::list_item(unsigned int list_index)\n"
4161 "if (template_selection != VALUE_LIST && "
4162 "template_selection != COMPLEMENTED_LIST) "
4163 "TTCN_error(\"Internal error: Accessing a list element of a non-list "
4164 "template of type %s.\");\n"
4165 "if (list_index >= value_list.n_values) "
4166 "TTCN_error(\"Internal error: Index overflow in a value list template "
4168 "return value_list.list_value[list_index];\n"
4169 "}\n\n", name
, name
, dispname
, dispname
);
4171 if (sdef
->kind
== SET_OF
) {
4172 def
= mputprintf(def
,
4173 "%s_template& set_item(unsigned int set_index);\n", type
);
4174 src
= mputprintf(src
,
4175 "%s_template& %s_template::set_item(unsigned int set_index)\n"
4177 "if (template_selection != SUPERSET_MATCH && "
4178 "template_selection != SUBSET_MATCH) "
4179 "TTCN_error(\"Internal error: Accessing a set element of a non-set "
4180 "template of type %s.\");\n"
4181 "if (set_index >= value_set.n_items) "
4182 "TTCN_error(\"Internal error: Index overflow in a set template of "
4184 "return value_set.set_items[set_index];\n"
4185 "}\n\n", type
, name
, dispname
, dispname
);
4188 /* logging functions */
4189 def
= mputstr(def
, "void log() const;\n");
4192 "void %s_template::log() const\n"
4194 "switch (template_selection) {\n"
4195 "case SPECIFIC_VALUE:\n"
4196 "if (single_value.n_elements > 0) {\n"
4197 "TTCN_Logger::log_event_str(\"{ \");\n"
4198 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4200 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n", name
);
4201 if (sdef
->kind
== RECORD_OF
) {
4203 "if (permutation_starts_at(elem_count)) "
4204 "TTCN_Logger::log_event_str(\"permutation(\");\n");
4206 src
= mputstr(src
, "single_value.value_elements[elem_count]->log();\n");
4207 if (sdef
->kind
== RECORD_OF
) {
4209 "if (permutation_ends_at(elem_count)) TTCN_Logger::log_char(')');\n");
4213 "TTCN_Logger::log_event_str(\" }\");\n"
4214 "} else TTCN_Logger::log_event_str(\"{ }\");\n"
4216 "case COMPLEMENTED_LIST:\n"
4217 "TTCN_Logger::log_event_str(\"complement\");\n"
4218 "case VALUE_LIST:\n"
4219 "TTCN_Logger::log_char('(');\n"
4220 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4222 "if (list_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4223 "value_list.list_value[list_count].log();\n"
4225 "TTCN_Logger::log_char(')');\n"
4227 if (sdef
->kind
== SET_OF
) {
4229 "case SUPERSET_MATCH:\n"
4230 "case SUBSET_MATCH:\n"
4231 "TTCN_Logger::log_event(\"%s(\", template_selection == SUPERSET_MATCH "
4232 "? \"superset\" : \"subset\");\n"
4233 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4235 "if (set_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4236 "value_set.set_items[set_count].log();\n"
4238 "TTCN_Logger::log_char(')');\n"
4245 "log_restricted();\n"
4246 "log_ifpresent();\n"
4249 def
= mputprintf(def
, "void log_match(const %s& match_value, "
4250 "boolean legacy = FALSE) const;\n", name
);
4251 src
= mputprintf(src
, "void %s_template::log_match(const %s& match_value, "
4252 "boolean legacy) const\n"
4254 "if(TTCN_Logger::VERBOSITY_COMPACT == TTCN_Logger::get_matching_verbosity()){\n"
4255 "if(match(match_value, legacy)){\n"
4256 "TTCN_Logger::print_logmatch_buffer();\n"
4257 "TTCN_Logger::log_event_str(\" matched\");\n"
4258 "}else{\n", name
, name
);
4260 if (sdef
->kind
== RECORD_OF
) {
4262 "if (template_selection == SPECIFIC_VALUE && "
4263 "single_value.n_elements > 0 && get_number_of_permutations() == 0 && "
4264 "single_value.n_elements == match_value.size_of()) {\n"
4265 "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n"
4266 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4268 "if(!single_value.value_elements[elem_count]->match(match_value[elem_count], legacy)){\n"
4269 "TTCN_Logger::log_logmatch_info(\"[%d]\", elem_count);\n"
4270 "single_value.value_elements[elem_count]->log_match(match_value[elem_count], legacy);\n"
4271 "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n"
4274 "log_match_length(single_value.n_elements);\n"
4276 "TTCN_Logger::print_logmatch_buffer();\n"
4277 "match_value.log();\n"
4278 "TTCN_Logger::log_event_str(\" with \");\n"
4280 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4283 if (sdef
->kind
== SET_OF
) {
4285 "size_t previous_size = TTCN_Logger::get_logmatch_buffer_len();\n"
4286 "if (template_selection == SPECIFIC_VALUE)\n"
4287 " log_match_heuristics(&match_value, match_value.size_of(), this, "
4288 "single_value.n_elements, match_function_specific, log_function, legacy);\n"
4290 "if(previous_size != 0){\n"
4291 "TTCN_Logger::print_logmatch_buffer();\n"
4292 "TTCN_Logger::set_logmatch_buffer_len(previous_size);\n"
4293 "TTCN_Logger::log_event_str(\":=\");\n"
4296 "match_value.log();\n"
4297 "TTCN_Logger::log_event_str(\" with \");\n"
4299 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4303 src
= mputstr(src
, "}\n"
4306 if (sdef
->kind
== RECORD_OF
) {
4307 /* logging by element is meaningful for 'record of' only */
4309 "if (template_selection == SPECIFIC_VALUE && "
4310 "single_value.n_elements > 0 && get_number_of_permutations() == 0 && "
4311 "single_value.n_elements == match_value.size_of()) {\n"
4312 "TTCN_Logger::log_event_str(\"{ \");\n"
4313 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4315 "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
4316 "single_value.value_elements[elem_count]->log_match"
4317 "(match_value[elem_count], legacy);\n"
4319 "TTCN_Logger::log_event_str(\" }\");\n"
4320 "log_match_length(single_value.n_elements);\n"
4324 "match_value.log();\n"
4325 "TTCN_Logger::log_event_str(\" with \");\n"
4327 "if (match(match_value, legacy)) TTCN_Logger::log_event_str(\" matched\");\n");
4328 if (sdef
->kind
== SET_OF
) {
4329 src
= mputstr(src
, "else {\n"
4330 "TTCN_Logger::log_event_str(\" unmatched\");\n"
4331 "if (template_selection == SPECIFIC_VALUE) log_match_heuristics("
4332 "&match_value, match_value.size_of(), this, single_value.n_elements, "
4333 "match_function_specific, log_function, legacy);\n"
4336 src
= mputstr(src
, "else TTCN_Logger::log_event_str(\" unmatched\");\n"
4339 src
= mputstr(src
, "}\n\n");
4341 /* encoding/decoding functions */
4342 def
= mputstr(def
, "void encode_text(Text_Buf& text_buf) const;\n");
4343 src
= mputprintf(src
,
4344 "void %s_template::encode_text(Text_Buf& text_buf) const\n"
4346 "encode_text_%s(text_buf);\n"
4347 "switch (template_selection) {\n"
4348 "case SPECIFIC_VALUE:\n"
4349 "text_buf.push_int(single_value.n_elements);\n"
4350 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4352 "single_value.value_elements[elem_count]->encode_text(text_buf);\n"
4353 "case OMIT_VALUE:\n"
4355 "case ANY_OR_OMIT:\n"
4357 "case VALUE_LIST:\n"
4358 "case COMPLEMENTED_LIST:\n"
4359 "text_buf.push_int(value_list.n_values);\n"
4360 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4362 "value_list.list_value[list_count].encode_text(text_buf);\n"
4363 "break;\n", name
, sdef
->kind
== RECORD_OF
? "permutation" : "restricted");
4364 if (sdef
->kind
== SET_OF
) {
4366 "case SUPERSET_MATCH:\n"
4367 "case SUBSET_MATCH:\n"
4368 "text_buf.push_int(value_set.n_items);\n"
4369 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4371 "value_set.set_items[set_count].encode_text(text_buf);\n"
4374 src
= mputprintf(src
,
4376 "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported "
4377 "template of type %s.\");\n"
4381 def
= mputstr(def
, "void decode_text(Text_Buf& text_buf);\n");
4382 src
= mputprintf(src
,
4383 "void %s_template::decode_text(Text_Buf& text_buf)\n"
4386 "decode_text_%s(text_buf);\n"
4387 "switch (template_selection) {\n"
4388 "case SPECIFIC_VALUE:\n"
4389 "single_value.n_elements = text_buf.pull_int().get_val();\n"
4390 "if (single_value.n_elements < 0) TTCN_error(\"Text decoder: Negative "
4391 "size was received for a template of type %s.\");\n"
4392 "single_value.value_elements = "
4393 "(%s_template**)allocate_pointers(single_value.n_elements);\n"
4394 "for (int elem_count = 0; elem_count < single_value.n_elements; "
4396 "single_value.value_elements[elem_count] = new %s_template;\n"
4397 "single_value.value_elements[elem_count]->decode_text(text_buf);\n"
4399 "case OMIT_VALUE:\n"
4401 "case ANY_OR_OMIT:\n"
4403 "case VALUE_LIST:\n"
4404 "case COMPLEMENTED_LIST:\n"
4405 "value_list.n_values = text_buf.pull_int().get_val();\n"
4406 "value_list.list_value = new %s_template[value_list.n_values];\n"
4407 "for (unsigned int list_count = 0; list_count < value_list.n_values; "
4409 "value_list.list_value[list_count].decode_text(text_buf);\n"
4410 "break;\n", name
, sdef
->kind
== RECORD_OF
? "permutation" : "restricted",
4411 dispname
, type
, type
, name
);
4412 if (sdef
->kind
== SET_OF
) {
4413 src
= mputprintf(src
,
4414 "case SUPERSET_MATCH:\n"
4415 "case SUBSET_MATCH:\n"
4416 "value_set.n_items = text_buf.pull_int().get_val();\n"
4417 "value_set.set_items = new %s_template[value_set.n_items];\n"
4418 "for (unsigned int set_count = 0; set_count < value_set.n_items; "
4420 "value_set.set_items[set_count].decode_text(text_buf);\n"
4423 src
= mputprintf(src
,
4425 "TTCN_error(\"Text decoder: An unknown/unsupported selection was "
4426 "received for a template of type %s.\");\n"
4430 /* TTCN-3 ispresent() function */
4431 def
= mputstr(def
, "boolean is_present(boolean legacy = FALSE) const;\n");
4432 src
= mputprintf(src
,
4433 "boolean %s_template::is_present(boolean legacy) const\n"
4435 "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n"
4436 "return !match_omit(legacy);\n"
4440 def
= mputstr(def
, "boolean match_omit(boolean legacy = FALSE) const;\n");
4441 src
= mputprintf(src
,
4442 "boolean %s_template::match_omit(boolean legacy) const\n"
4444 "if (is_ifpresent) return TRUE;\n"
4445 "switch (template_selection) {\n"
4446 "case OMIT_VALUE:\n"
4447 "case ANY_OR_OMIT:\n"
4449 "case VALUE_LIST:\n"
4450 "case COMPLEMENTED_LIST:\n"
4452 "for (unsigned int i=0; i<value_list.n_values; i++)\n"
4453 "if (value_list.list_value[i].match_omit())\n"
4454 "return template_selection==VALUE_LIST;\n"
4455 "return template_selection==COMPLEMENTED_LIST;\n"
4456 "} // else fall through\n"
4464 def
= mputstr(def
, "void set_param(Module_Param& param);\n");
4465 src
= mputprintf(src
,
4466 "void %s_template::set_param(Module_Param& param)\n"
4468 " if (dynamic_cast<Module_Param_Name*>(param.get_id()) != NULL &&\n"
4469 " param.get_id()->next_name()) {\n"
4470 // Haven't reached the end of the module parameter name
4471 // => the name refers to one of the elements, not to the whole record of
4472 " char* param_field = param.get_id()->get_current_name();\n"
4473 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
4474 " param.error(\"Unexpected record field name in module parameter, expected a valid\"\n"
4475 " \" index for %s template type `%s'\");\n"
4477 " int param_index = -1;\n"
4478 " sscanf(param_field, \"%%d\", ¶m_index);\n"
4479 " (*this)[param_index].set_param(param);\n"
4482 " param.basic_check(Module_Param::BC_TEMPLATE|Module_Param::BC_LIST, \"%s of template\");\n"
4483 " Module_Param_Ptr mp = ¶m;\n"
4484 " if (param.get_type() == Module_Param::MP_Reference) {\n"
4485 " mp = param.get_referenced_param();\n"
4487 " switch (mp->get_type()) {\n"
4488 " case Module_Param::MP_Omit:\n"
4489 " *this = OMIT_VALUE;\n"
4491 " case Module_Param::MP_Any:\n"
4492 " *this = ANY_VALUE;\n"
4494 " case Module_Param::MP_AnyOrNone:\n"
4495 " *this = ANY_OR_OMIT;\n"
4497 " case Module_Param::MP_List_Template:\n"
4498 " case Module_Param::MP_ComplementList_Template: {\n"
4499 " %s_template temp;\n"
4500 " temp.set_type(mp->get_type()==Module_Param::MP_List_Template ? "
4501 "VALUE_LIST : COMPLEMENTED_LIST, mp->get_size());\n"
4502 " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n"
4503 " temp.list_item(p_i).set_param(*mp->get_elem(p_i));\n"
4507 " case Module_Param::MP_Indexed_List:\n"
4508 " if (template_selection!=SPECIFIC_VALUE) set_size(0);\n"
4509 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4510 " (*this)[(int)(mp->get_elem(p_i)->get_id()->get_index())].set_param(*mp->get_elem(p_i));\n"
4513 name
, sdef
->kind
==RECORD_OF
?"record":"set", dispname
, sdef
->kind
==RECORD_OF
?"record":"set", name
);
4514 if (sdef
->kind
== RECORD_OF
) {
4516 " case Module_Param::MP_Value_List: {\n"
4517 " set_size(mp->get_size());\n"
4518 " int curr_idx = 0;\n"
4519 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4520 " switch (mp->get_elem(p_i)->get_type()) {\n"
4521 " case Module_Param::MP_NotUsed:\n"
4524 " case Module_Param::MP_Permutation_Template: {\n"
4525 " int perm_start_idx = curr_idx;\n"
4526 " for (size_t perm_i=0; perm_i<mp->get_elem(p_i)->get_size(); perm_i++) {\n"
4527 " (*this)[curr_idx].set_param(*(mp->get_elem(p_i)->get_elem(perm_i)));\n"
4530 " int perm_end_idx = curr_idx - 1;\n"
4531 " add_permutation(perm_start_idx, perm_end_idx);\n"
4534 " (*this)[curr_idx].set_param(*mp->get_elem(p_i));\n"
4541 " case Module_Param::MP_Value_List:\n"
4542 " set_size(mp->get_size());\n"
4543 " for (size_t p_i=0; p_i<mp->get_size(); ++p_i) {\n"
4544 " if (mp->get_elem(p_i)->get_type()!=Module_Param::MP_NotUsed) {\n"
4545 " (*this)[p_i].set_param(*mp->get_elem(p_i));\n"
4549 " case Module_Param::MP_Superset_Template:\n"
4550 " case Module_Param::MP_Subset_Template:\n"
4551 " set_type(mp->get_type()==Module_Param::MP_Superset_Template ? SUPERSET_MATCH : SUBSET_MATCH, mp->get_size());\n"
4552 " for (size_t p_i=0; p_i<mp->get_size(); p_i++) {\n"
4553 " set_item(p_i).set_param(*mp->get_elem(p_i));\n"
4557 src
= mputprintf(src
,
4559 " param.type_error(\"%s of template\", \"%s\");\n"
4561 " is_ifpresent = param.get_ifpresent() || mp->get_ifpresent();\n"
4562 " if (param.get_length_restriction() != NULL) {\n"
4563 " set_length_range(param);\n"
4566 " set_length_range(*mp);\n"
4568 "}\n\n", sdef
->kind
==RECORD_OF
?"record":"set", dispname
);
4571 def
= mputstr(def
, "Module_Param* get_param(Module_Param_Name& param_name) const;\n");
4574 "Module_Param* %s_template::get_param(Module_Param_Name& param_name) const\n"
4576 " if (param_name.next_name()) {\n"
4577 // Haven't reached the end of the module parameter name
4578 // => the name refers to one of the elements, not to the whole record of
4579 " char* param_field = param_name.get_current_name();\n"
4580 " if (param_field[0] < '0' || param_field[0] > '9') {\n"
4581 " TTCN_error(\"Unexpected record field name in module parameter reference, \"\n"
4582 " \"expected a valid index for %s template type `%s'\");\n"
4584 " int param_index = -1;\n"
4585 " sscanf(param_field, \"%%d\", ¶m_index);\n"
4586 " return (*this)[param_index].get_param(param_name);\n"
4588 " Module_Param* mp = NULL;\n"
4589 " switch (template_selection) {\n"
4590 " case UNINITIALIZED_TEMPLATE:\n"
4591 " mp = new Module_Param_Unbound();\n"
4593 " case OMIT_VALUE:\n"
4594 " mp = new Module_Param_Omit();\n"
4596 " case ANY_VALUE:\n"
4597 " mp = new Module_Param_Any();\n"
4599 " case ANY_OR_OMIT:\n"
4600 " mp = new Module_Param_AnyOrNone();\n"
4602 " case SPECIFIC_VALUE: {\n"
4603 " Vector<Module_Param*> values;\n"
4604 " for (int i = 0; i < single_value.n_elements; ++i) {\n"
4605 " values.push_back((*this)[i].get_param(param_name));\n"
4607 " mp = new Module_Param_Value_List();\n"
4608 " mp->add_list_with_implicit_ids(&values);\n"
4609 " values.clear();\n"
4611 " case VALUE_LIST:\n"
4612 " case COMPLEMENTED_LIST: {\n"
4613 " if (template_selection == VALUE_LIST) {\n"
4614 " mp = new Module_Param_List_Template();\n"
4617 " mp = new Module_Param_ComplementList_Template();\n"
4619 " for (size_t i = 0; i < value_list.n_values; ++i) {\n"
4620 " mp->add_elem(value_list.list_value[i].get_param(param_name));\n"
4626 " if (is_ifpresent) {\n"
4627 " mp->set_ifpresent();\n"
4629 " mp->set_length_restriction(get_length_range());\n"
4631 "}\n\n", name
, sdef
->kind
==RECORD_OF
? "record of" : "set of", dispname
);
4633 /* check template restriction */
4634 def
= mputstr(def
, "void check_restriction(template_res t_res, "
4635 "const char* t_name=NULL, boolean legacy = FALSE) const;\n");
4636 src
= mputprintf(src
,
4637 "void %s_template::check_restriction("
4638 "template_res t_res, const char* t_name, boolean legacy) const\n"
4640 "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n"
4641 "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n"
4643 "if (template_selection==OMIT_VALUE) return;\n"
4645 "if (template_selection!=SPECIFIC_VALUE || is_ifpresent) break;\n"
4646 "for (int i=0; i<single_value.n_elements; i++) "
4647 "single_value.value_elements[i]->check_restriction("
4648 "t_res, t_name ? t_name : \"%s\");\n"
4650 "case TR_PRESENT:\n"
4651 "if (!match_omit(legacy)) return;\n"
4656 "TTCN_error(\"Restriction `%%s' on template of type %%s "
4657 "violated.\", get_res_name(t_res), t_name ? t_name : \"%s\");\n"
4658 "}\n\n", name
, dispname
, dispname
);
4661 def
= mputstr(def
, "};\n\n");
4663 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
4664 "class %s_template;\n", name
);
4665 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
4667 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
4671 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
4673 void defRecordOfTemplate2(const struct_of_def
*sdef
, output_struct
*output
)
4675 char *def
= NULL
, *src
= NULL
;
4676 const char *name
= sdef
->name
;
4677 const char *type
= sdef
->type
;
4678 const char *base_class
= sdef
->kind
== RECORD_OF
? "Record_Of_Template" : "Set_Of_Template";
4680 /* Class definition */
4681 def
= mputprintf(def
, "class %s_template : public %s {\n", name
, base_class
);
4683 /* public member functions */
4684 def
= mputstr(def
, "\npublic:\n");
4687 def
= mputprintf(def
, "%s_template() {}\n", name
);
4689 def
= mputprintf(def
, "%s_template(template_sel other_value): %s(other_value) "
4690 "{ check_single_selection(other_value); }\n", name
, base_class
);
4692 def
= mputprintf(def
, "%s_template(null_type other_value);\n", name
);
4693 src
= mputprintf(src
, "%s_template::%s_template(null_type)\n"
4694 " : %s(SPECIFIC_VALUE)\n"
4696 "single_value.n_elements = 0;\n"
4697 "single_value.value_elements = NULL;\n"
4698 "}\n\n", name
, name
, base_class
);
4700 def
= mputprintf(def
, "%s_template(const %s& other_value) "
4701 "{ copy_value(&other_value); }\n", name
, name
);
4702 def
= mputprintf(def
, "%s_template(const OPTIONAL<%s>& other_value) "
4703 "{ copy_optional(&other_value); }\n", name
, name
);
4705 /* copy constructor */
4706 def
= mputprintf(def
, "%s_template(const %s_template& other_value): %s() { copy_template(other_value); }\n",
4707 name
, name
, base_class
);
4709 /* assignment operators */
4710 def
= mputprintf(def
, "%s_template& operator=(template_sel other_value);\n",
4712 src
= mputprintf(src
,
4713 "%s_template& %s_template::operator=(template_sel other_value)\n"
4715 "check_single_selection(other_value);\n"
4717 "set_selection(other_value);\n"
4719 "}\n\n", name
, name
);
4721 def
= mputprintf(def
, "%s_template& operator=(null_type other_value);\n",
4723 src
= mputprintf(src
,
4724 "%s_template& %s_template::operator=(null_type)\n"
4727 "set_selection(SPECIFIC_VALUE);\n"
4728 "single_value.n_elements = 0;\n"
4729 "single_value.value_elements = NULL;\n"
4731 "}\n\n", name
, name
);
4733 def
= mputprintf(def
, "%s_template& operator=(const %s& other_value);\n",
4735 src
= mputprintf(src
,
4736 "%s_template& %s_template::operator=(const %s& other_value)\n"
4739 "copy_value(&other_value);\n"
4741 "}\n\n", name
, name
, name
);
4743 def
= mputprintf(def
, "%s_template& operator=(const OPTIONAL<%s>& "
4744 "other_value);\n", name
, name
);
4745 src
= mputprintf(src
,
4746 "%s_template& %s_template::operator=(const OPTIONAL<%s>& other_value)\n"
4749 "copy_optional(&other_value);\n"
4751 "}\n\n", name
, name
, name
);
4753 def
= mputprintf(def
, "%s_template& operator=(const %s_template& "
4754 "other_value);\n\n", name
, name
);
4755 src
= mputprintf(src
,
4756 "%s_template& %s_template::operator=(const %s_template& other_value)\n"
4758 "if (&other_value != this) {\n"
4760 "copy_template(other_value);\n"
4763 "}\n\n", name
, name
, name
);
4765 /* indexing operators */
4766 def
= mputprintf(def
,
4767 "%s_template& operator[](int index_value);\n"
4768 "%s_template& operator[](const INTEGER& index_value);\n"
4769 "const %s_template& operator[](int index_value) const;\n"
4770 "const %s_template& operator[](const INTEGER& index_value) const;\n",
4776 src
= mputprintf(src
,
4777 "%s_template& %s_template::operator[](int index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n"
4778 "%s_template& %s_template::operator[](const INTEGER& index_value) { return *(static_cast<%s_template*>(get_at(index_value))); }\n"
4779 "const %s_template& %s_template::operator[](int index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n"
4780 "const %s_template& %s_template::operator[](const INTEGER& index_value) const { return *(static_cast<const %s_template*>(get_at(index_value))); }\n\n",
4786 /* match operation */
4787 def
= mputprintf(def
, "inline boolean match(const %s& match_value, "
4788 "boolean legacy = FALSE) const "
4789 "{ return matchv(&match_value, legacy); }\n", name
);
4791 /* valueof operation */
4792 def
= mputprintf(def
, "%s valueof() const;\n", name
);
4793 src
= mputprintf(src
,
4794 "%s %s_template::valueof() const\n"
4797 "valueofv(&ret_val);\n"
4799 "}\n\n", name
, name
, name
);
4801 /* substr() predefined function for templates */
4802 def
= mputprintf(def
,
4803 "%s substr(int index, int returncount) const;\n\n", name
);
4804 src
= mputprintf(src
,
4805 "%s %s_template::substr(int index, int returncount) const\n"
4808 "substr_(index, returncount, &rec_of);\n"
4810 "}\n\n", name
, name
, name
);
4812 /* replace() predefined function for templates */
4813 def
= mputprintf(def
,
4814 "%s replace(int index, int len, const %s_template& repl) const;\n\n",
4816 src
= mputprintf(src
,
4817 "%s %s_template::replace(int index, int len, const %s_template& repl) const\n"
4820 "replace_(index, len, &repl, &rec_of);\n"
4822 "}\n\n", name
, name
, name
, name
);
4823 def
= mputprintf(def
,
4824 "%s replace(int index, int len, const %s& repl) const;\n\n",
4826 src
= mputprintf(src
,
4827 "%s %s_template::replace(int index, int len, const %s& repl) const\n"
4830 "replace_(index, len, &repl, &rec_of);\n"
4832 "}\n\n", name
, name
, name
, name
);
4834 /* value list and set handling operators */
4835 def
= mputprintf(def
,
4836 "inline %s_template& list_item(int list_index) { return *(static_cast<%s_template*>(get_list_item(list_index))); }\n", name
, name
);
4838 if (sdef
->kind
== SET_OF
) {
4839 def
= mputprintf(def
, "%s_template& set_item(int set_index);\n", type
);
4840 src
= mputprintf(src
,
4841 "%s_template& %s_template::set_item(int set_index) "
4842 "{ return *(static_cast<%s_template*>(get_set_item(set_index))); }\n",
4846 /* logging functions */
4847 def
= mputprintf(def
, "inline void log_match(const %s& match_value, "
4848 "boolean legacy = FALSE) const "
4849 "{ log_matchv(&match_value, legacy); }\n", name
);
4851 /* virtual helper functions */
4852 def
= mputprintf(def
,
4853 "%s* create() const { return new %s_template; }\n"
4854 "Base_Template* create_elem() const;\n"
4855 "const TTCN_Typedescriptor_t* get_descriptor() const;\n",
4857 src
= mputprintf(src
,
4858 "Base_Template* %s_template::create_elem() const { return new %s_template; }\n"
4859 "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const { return &%s_descr_; }\n",
4864 def
= mputstr(def
, "};\n\n");
4866 output
->header
.class_decls
= mputprintf(output
->header
.class_decls
,
4867 "class %s_template;\n", name
);
4868 output
->header
.class_defs
= mputstr(output
->header
.class_defs
, def
);
4870 output
->source
.methods
= mputstr(output
->source
.methods
, src
);
This page took 0.148439 seconds and 5 git commands to generate.