Merge pull request #10 from egerpil/master
[deliverable/titan.core.git] / core / Template.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef TEMPLATE_HH
9 #define TEMPLATE_HH
10
11 #include "Types.h"
12
13 #ifdef TITAN_RUNTIME_2
14 #include "Struct_of.hh"
15 #include "RefdIndex.hh"
16 struct TTCN_Typedescriptor_t;
17 struct Erroneous_descriptor_t;
18 #endif
19
20 class Text_Buf;
21 class Module_Param;
22 class Module_Param_Name;
23 class Module_Param_Length_Restriction;
24
25 enum template_sel {
26 UNINITIALIZED_TEMPLATE = -1,
27 SPECIFIC_VALUE = 0,
28 OMIT_VALUE = 1,
29 ANY_VALUE = 2,
30 ANY_OR_OMIT = 3,
31 VALUE_LIST = 4,
32 COMPLEMENTED_LIST = 5,
33 VALUE_RANGE = 6,
34 STRING_PATTERN = 7,
35 SUPERSET_MATCH = 8,
36 SUBSET_MATCH = 9
37 };
38
39 enum template_res {
40 TR_VALUE,
41 TR_OMIT,
42 TR_PRESENT
43 };
44
45 #ifdef TITAN_RUNTIME_2
46 #define VIRTUAL_IF_RUNTIME_2 virtual
47 #else
48 #define VIRTUAL_IF_RUNTIME_2
49 #endif
50
51 class Base_Template {
52 protected:
53 template_sel template_selection;
54 boolean is_ifpresent;
55
56 Base_Template();
57 Base_Template(template_sel other_value);
58 // Compiler-generated copy constructor and assignment are acceptable
59
60 static void check_single_selection(template_sel other_value);
61 void set_selection(template_sel other_value);
62 void set_selection(const Base_Template& other_value);
63
64 void log_generic() const;
65 void log_ifpresent() const;
66
67 void encode_text_base(Text_Buf& text_buf) const;
68 void decode_text_base(Text_Buf& text_buf);
69
70 public:
71 inline template_sel get_selection() const { return template_selection; }
72
73 void set_ifpresent();
74
75 VIRTUAL_IF_RUNTIME_2 boolean is_omit() const;
76 VIRTUAL_IF_RUNTIME_2 boolean is_any_or_omit() const;
77 /** Returns FALSE if \a template_selection is \a UNINITIALIZED_TEMPLATE,
78 * TRUE otherwise. */
79 VIRTUAL_IF_RUNTIME_2 boolean is_bound() const
80 { return UNINITIALIZED_TEMPLATE != template_selection; }
81 VIRTUAL_IF_RUNTIME_2 boolean is_value() const
82 { return !is_ifpresent && template_selection==SPECIFIC_VALUE; }
83 VIRTUAL_IF_RUNTIME_2 void clean_up()
84 { template_selection = UNINITIALIZED_TEMPLATE; }
85 /** return the name of template restriction \a tr */
86 static const char* get_res_name(template_res tr);
87
88 /** Initialize this object (or one of its fields/elements) with a
89 * module parameter value. The module parameter may contain references to
90 * other module parameters or module parameter expressions, which are processed
91 * by this method to calculated the final result.
92 * @note Sets the 'ifpresent' flag if either the reference (if any) or the
93 * (referenced) module parameter value have their 'ifpresent' flag
94 * @note If both the reference (if any) and the (referenced) module parameter
95 * have a length restriction, then the reference's length restriction is used.
96 * @note A temporary object is used when setting VALUE_LIST or COMPLEMENT_LIST
97 * templates, as the list of templates might contain a reference to this object.
98 * @param param module parameter value (its ID specifies which object is to be set) */
99 VIRTUAL_IF_RUNTIME_2 void set_param(Module_Param& param);
100 /** Create a module parameter value equivalent to this object (or one of its
101 * fields/elements)
102 * @param param_name module parameter ID, specifies which object to convert */
103 VIRTUAL_IF_RUNTIME_2 Module_Param* get_param(Module_Param_Name& param_name) const;
104
105 /** not a component by default (component templates will return true) */
106 inline boolean is_component() { return FALSE; }
107
108 #ifdef TITAN_RUNTIME_2
109 /** set the value of the object pointed to by \a value */
110 virtual void valueofv(Base_Type* value) const = 0;
111 virtual void set_value(template_sel other_value) = 0;
112 virtual void copy_value(const Base_Type* other_value) = 0;
113
114 virtual Base_Template* clone() const = 0;
115 virtual const TTCN_Typedescriptor_t* get_descriptor() const = 0;
116
117 virtual void log() const = 0;
118
119 // virtual functions for match and log_match
120 virtual boolean matchv(const Base_Type* other_value, boolean legacy) const = 0;
121 virtual void log_matchv(const Base_Type* match_value, boolean legacy) const = 0;
122
123 virtual void encode_text(Text_Buf& text_buf) const = 0;
124 virtual void decode_text(Text_Buf& text_buf) = 0;
125 virtual boolean is_present(boolean legacy = FALSE) const = 0;
126 virtual boolean match_omit(boolean legacy = FALSE) const = 0;
127
128 virtual void check_restriction(template_res t_res, const char* t_name=NULL,
129 boolean legacy = FALSE) const;
130
131 virtual ~Base_Template() { }
132 #endif
133 };
134
135 class Restricted_Length_Template : public Base_Template
136 #ifdef TITAN_RUNTIME_2
137 , public RefdIndexInterface
138 #endif
139 {
140 protected:
141 enum length_restriction_type_t {
142 NO_LENGTH_RESTRICTION = 0,
143 SINGLE_LENGTH_RESTRICTION = 1,
144 RANGE_LENGTH_RESTRICTION =2
145 } length_restriction_type;
146 union {
147 int single_length;
148 struct {
149 int min_length, max_length;
150 boolean max_length_set;
151 } range_length;
152 } length_restriction;
153
154 Restricted_Length_Template();
155 Restricted_Length_Template(template_sel other_value);
156 // Compiler-generated copy constructor and assignment are acceptable
157
158 void set_selection(template_sel new_selection);
159 void set_selection(const Restricted_Length_Template& other_value);
160
161 public:
162 boolean match_length(int value_length) const;
163
164 protected:
165 int check_section_is_single(int min_size, boolean has_any_or_none,
166 const char* operation_name,
167 const char* type_name_prefix,
168 const char* type_name) const;
169
170 void log_restricted() const;
171 void log_match_length(int value_length) const;
172
173 void encode_text_restricted(Text_Buf& text_buf) const;
174 void decode_text_restricted(Text_Buf& text_buf);
175
176 void set_length_range(const Module_Param& param);
177 Module_Param_Length_Restriction* get_length_range() const;
178
179 public:
180
181 void set_single_length(int single_length);
182 void set_min_length(int min_length);
183 void set_max_length(int max_length);
184
185 boolean is_omit() const;
186 boolean is_any_or_omit() const;
187 };
188
189 #ifndef TITAN_RUNTIME_2
190
191 class Record_Of_Template : public Restricted_Length_Template {
192 protected:
193 struct Pair_of_elements;
194 Pair_of_elements *permutation_intervals;
195 unsigned int number_of_permutations;
196
197 Record_Of_Template();
198 Record_Of_Template(template_sel new_selection);
199 Record_Of_Template(const Record_Of_Template& other_value);
200 ~Record_Of_Template();
201
202 void clean_up_intervals();
203
204 void set_selection(template_sel new_selection);
205 void set_selection(const Record_Of_Template& other_value);
206
207 void encode_text_permutation(Text_Buf& text_buf) const;
208 void decode_text_permutation(Text_Buf& text_buf);
209 private:
210 Record_Of_Template& operator=(const Record_Of_Template& other_value);
211
212 public:
213 void add_permutation(unsigned int start_index, unsigned int end_index);
214
215 /** Removes all permutations set on this template, used when template variables
216 * are given new values. */
217 void remove_all_permutations() { clean_up_intervals(); }
218
219 unsigned int get_number_of_permutations() const;
220 unsigned int get_permutation_start(unsigned int index_value) const;
221 unsigned int get_permutation_end(unsigned int index_value) const;
222 unsigned int get_permutation_size(unsigned int index_value) const;
223 boolean permutation_starts_at(unsigned int index_value) const;
224 boolean permutation_ends_at(unsigned int index_value) const;
225 };
226
227 #else
228
229 class INTEGER;
230
231 class Set_Of_Template : public Restricted_Length_Template {
232 protected:
233 union {
234 struct {
235 int n_elements;
236 Base_Template** value_elements; // instances of a class derived from Base_Template
237 } single_value;
238 struct {
239 int n_values;
240 Set_Of_Template** list_value; // instances of a class derived from Set_Of_Template
241 } value_list;
242 };
243 Erroneous_descriptor_t* err_descr;
244
245 Set_Of_Template();
246 Set_Of_Template(template_sel new_selection);
247 /** Copy constructor not implemented */
248 Set_Of_Template(const Set_Of_Template& other_value);
249 /** Assignment disabled */
250 Set_Of_Template& operator=(const Set_Of_Template& other_value);
251 ~Set_Of_Template();
252
253 void copy_template(const Set_Of_Template& other_value);
254 void copy_optional(const Base_Type* other_value);
255
256 public:
257 Base_Template* clone() const;
258 void copy_value(const Base_Type* other_value);
259 void clean_up();
260
261 int size_of() const { return size_of(TRUE); }
262 int lengthof() const { return size_of(FALSE); }
263 int n_elem() const;
264 void set_size(int new_size);
265 void set_type(template_sel template_type, int list_length);
266 boolean is_value() const;
267 void valueofv(Base_Type* value) const;
268 void set_value(template_sel other_value);
269 protected:
270 Base_Template* get_at(int index_value);
271 Base_Template* get_at(const INTEGER& index_value);
272 const Base_Template* get_at(int index_value) const;
273 const Base_Template* get_at(const INTEGER& index_value) const;
274
275 int size_of(boolean is_size) const;
276 Set_Of_Template* get_list_item(int list_index);
277 Base_Template* get_set_item(int set_index);
278
279 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
280 void replace_(int index, int len, const Set_Of_Template* repl, Record_Of_Type* rec_of) const;
281 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
282
283 public:
284 void log() const;
285
286 boolean matchv(const Base_Type* other_value, boolean legacy) const;
287 void log_matchv(const Base_Type* match_value, boolean legacy) const;
288 /** create an instance of this */
289 virtual Set_Of_Template* create() const = 0;
290 /** create an instance of the element class */
291 virtual Base_Template* create_elem() const = 0;
292
293 // used for both set of and record of types
294 static boolean match_function_specific(
295 const Base_Type *value_ptr, int value_index,
296 const Restricted_Length_Template *template_ptr, int template_index,
297 boolean legacy);
298 // 2 static functions only for set of types
299 static boolean match_function_set(
300 const Base_Type *value_ptr, int value_index,
301 const Restricted_Length_Template *template_ptr, int template_index,
302 boolean legacy);
303 static void log_function(const Base_Type *value_ptr,
304 const Restricted_Length_Template *template_ptr,
305 int index_value, int index_template, boolean legacy);
306
307 void encode_text(Text_Buf& text_buf) const;
308 void decode_text(Text_Buf& text_buf);
309 boolean is_present(boolean legacy = FALSE) const;
310 boolean match_omit(boolean legacy = FALSE) const;
311
312 void set_param(Module_Param& param);
313 Module_Param* get_param(Module_Param_Name& param_name) const;
314
315 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
316 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
317 };
318
319 class Record_Of_Template : public Restricted_Length_Template {
320 protected:
321 union {
322 struct {
323 int n_elements;
324 Base_Template **value_elements; // instances of a class derived from Base_Template
325 } single_value;
326 struct {
327 int n_values;
328 Record_Of_Template** list_value; // instances of a class derived from Record_Of_Template
329 } value_list;
330 };
331 Erroneous_descriptor_t* err_descr;
332
333 struct Pair_of_elements;
334 Pair_of_elements *permutation_intervals;
335 unsigned int number_of_permutations;
336
337 Record_Of_Template();
338 Record_Of_Template(template_sel new_selection);
339 /** Copy constructor not implemented */
340 Record_Of_Template(const Record_Of_Template& other_value);
341 /** Assignment disabled */
342 Record_Of_Template& operator=(const Record_Of_Template& other_value);
343 ~Record_Of_Template();
344
345 void clean_up_intervals();
346
347 void set_selection(template_sel new_selection);
348 void set_selection(const Record_Of_Template& other_value);
349
350 void encode_text_permutation(Text_Buf& text_buf) const;
351 void decode_text_permutation(Text_Buf& text_buf);
352
353 void copy_template(const Record_Of_Template& other_value);
354 void copy_optional(const Base_Type* other_value);
355
356 public:
357 Base_Template* clone() const;
358 void copy_value(const Base_Type* other_value);
359 void clean_up();
360
361 void add_permutation(unsigned int start_index, unsigned int end_index);
362
363 /** Removes all permutations set on this template, used when template variables
364 * are given new values. */
365 void remove_all_permutations() { clean_up_intervals(); }
366
367 unsigned int get_number_of_permutations() const;
368 unsigned int get_permutation_start(unsigned int index_value) const;
369 unsigned int get_permutation_end(unsigned int index_value) const;
370 unsigned int get_permutation_size(unsigned int index_value) const;
371 boolean permutation_starts_at(unsigned int index_value) const;
372 boolean permutation_ends_at(unsigned int index_value) const;
373
374 void set_size(int new_size);
375 void set_type(template_sel template_type, int list_length);
376 boolean is_value() const;
377 void valueofv(Base_Type* value) const;
378 void set_value(template_sel other_value);
379 protected:
380 Base_Template* get_at(int index_value);
381 Base_Template* get_at(const INTEGER& index_value);
382 const Base_Template* get_at(int index_value) const;
383 const Base_Template* get_at(const INTEGER& index_value) const;
384
385 int size_of(boolean is_size) const;
386 Record_Of_Template* get_list_item(int list_index);
387
388 void substr_(int index, int returncount, Record_Of_Type* rec_of) const;
389 void replace_(int index, int len, const Record_Of_Template* repl, Record_Of_Type* rec_of) const;
390 void replace_(int index, int len, const Record_Of_Type* repl, Record_Of_Type* rec_of) const;
391
392 public:
393 int size_of() const { return size_of(TRUE); }
394 int lengthof() const { return size_of(FALSE); }
395 int n_elem() const;
396 void log() const;
397
398 boolean matchv(const Base_Type* other_value, boolean legacy) const;
399 void log_matchv(const Base_Type* match_value, boolean legacy) const;
400 /** create an instance of this */
401 virtual Record_Of_Template* create() const = 0;
402 /** create an instance of the element class */
403 virtual Base_Template* create_elem() const = 0;
404
405 // used for both set of and record of types
406 static boolean match_function_specific(
407 const Base_Type *value_ptr, int value_index,
408 const Restricted_Length_Template *template_ptr, int template_index, boolean legacy);
409
410 void encode_text(Text_Buf& text_buf) const;
411 void decode_text(Text_Buf& text_buf);
412 boolean is_present(boolean legacy = FALSE) const;
413 boolean match_omit(boolean legacy = FALSE) const;
414
415 void set_param(Module_Param& param);
416 Module_Param* get_param(Module_Param_Name& param_name) const;
417
418 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
419 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
420 };
421
422 class Record_Template : public Base_Template
423 {
424 protected:
425 union {
426 struct {
427 int n_elements;
428 Base_Template** value_elements;
429 } single_value;
430 struct {
431 int n_values;
432 Record_Template** list_value;
433 } value_list;
434 };
435 Erroneous_descriptor_t* err_descr;
436
437 /** create value elements by calling their constructor */
438 virtual void set_specific() = 0;
439
440 void copy_optional(const Base_Type* other_value);
441 void copy_template(const Record_Template& other_value);
442
443 Record_Template();
444 Record_Template(template_sel other_value);
445 /** Copy constructor not implemented */
446 Record_Template(const Record_Template& other_value);
447 /** Assignment disabled */
448 Record_Template& operator=(const Record_Template& other_value);
449 ~Record_Template();
450
451 public:
452 void copy_value(const Base_Type* other_value);
453 void clean_up();
454
455 void set_type(template_sel template_type, int list_length);
456 Record_Template* get_list_item(int list_index) const;
457
458 int size_of() const;
459
460 boolean is_value() const;
461 void valueofv(Base_Type* value) const;
462 void set_value(template_sel other_value);
463
464 protected:
465 Base_Template* get_at(int index_value);
466 const Base_Template* get_at(int index_value) const;
467
468 /** create an instance by calling the default constructor */
469 virtual Record_Template* create() const = 0;
470
471 virtual const char* fld_name(int /*field_index*/) const { return ""; }
472
473 public:
474 Base_Template* clone() const;
475
476 void log() const;
477
478 boolean matchv(const Base_Type* other_value, boolean legacy) const;
479 void log_matchv(const Base_Type* match_value, boolean legacy) const;
480
481 void encode_text(Text_Buf& text_buf) const;
482 void decode_text(Text_Buf& text_buf);
483 boolean is_present(boolean legacy = FALSE) const;
484 boolean match_omit(boolean legacy = FALSE) const;
485
486 void set_param(Module_Param& param);
487 Module_Param* get_param(Module_Param_Name& param_name) const;
488
489 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
490 void set_err_descr(Erroneous_descriptor_t* p_err_descr) { err_descr=p_err_descr; }
491 };
492
493 class Empty_Record_Template : public Base_Template
494 {
495 protected:
496 struct {
497 int n_values;
498 Empty_Record_Template** list_value;
499 } value_list;
500
501 void copy_optional(const Base_Type* other_value);
502 void copy_template(const Empty_Record_Template& other_value);
503
504 Empty_Record_Template();
505 Empty_Record_Template(template_sel other_value);
506 /** Copy constructor not implemented */
507 Empty_Record_Template(const Empty_Record_Template& other_value);
508 /** Assignment disabled */
509 Empty_Record_Template& operator=(const Empty_Record_Template& other_value);
510 ~Empty_Record_Template();
511
512 public:
513 void copy_value(const Base_Type* other_value);
514 void clean_up();
515
516 void set_type(template_sel template_type, int list_length);
517 Empty_Record_Template* get_list_item(int list_index) const;
518
519 int size_of() const;
520
521 boolean is_value() const;
522 void valueofv(Base_Type* value) const;
523 void set_value(template_sel other_value);
524
525 protected:
526 /** create an instance by calling the default constructor */
527 virtual Empty_Record_Template* create() const = 0;
528
529 public:
530 Base_Template* clone() const;
531
532 void log() const;
533
534 boolean matchv(const Base_Type* other_value, boolean legacy) const;
535 void log_matchv(const Base_Type* match_value, boolean legacy) const;
536
537 void encode_text(Text_Buf& text_buf) const;
538 void decode_text(Text_Buf& text_buf);
539 boolean is_present(boolean legacy = FALSE) const;
540 boolean match_omit(boolean legacy = FALSE) const;
541
542 void set_param(Module_Param& param);
543 Module_Param* get_param(Module_Param_Name& param_name) const;
544 };
545
546 #undef VIRTUAL_IF_RUNTIME_2
547 #endif
548
549 #endif
This page took 0.043519 seconds and 5 git commands to generate.