Sync with 5.4.0
[deliverable/titan.core.git] / core / Optional.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 OPTIONAL_HH
9 #define OPTIONAL_HH
10
11 #include "Types.h"
12 #include "Param_Types.hh"
13 #include "Basetype.hh"
14 #include "Template.hh"
15 #include "BER.hh"
16 #include "Logger.hh"
17 #include "Encdec.hh"
18 #include "Textbuf.hh"
19 #include "Error.hh"
20 #include "Parameters.h"
21 #include "XER.hh"
22 #include "JSON.hh"
23 #include "XmlReader.hh"
24
25 enum optional_sel { OPTIONAL_UNBOUND, OPTIONAL_OMIT, OPTIONAL_PRESENT };
26
27 template <typename T_type>
28 class OPTIONAL : public Base_Type
29 #ifdef TITAN_RUNTIME_2
30 , public RefdIndexInterface
31 #endif
32 {
33 /** The value, if present (owned by OPTIONAL)
34 * In Runtime2 the pointer is null, when the value is not present.
35 * In Runtime1 its presence is indicated by the optional_selection member. */
36 T_type *optional_value;
37
38 /** Specifies the state of the optional field
39 * @tricky In Runtime2 the optional value can be modified through parameter references,
40 * in which case this member variable will not be updated. Always use the function
41 * get_selection() instead of directly referencing this variable. */
42 optional_sel optional_selection;
43
44 #ifdef TITAN_RUNTIME_2
45 /** Stores the number of elements referenced by 'out' and 'inout' parameters,
46 * if the optional field is a record of/set of/array (only in Runtime2).
47 * If at least one element is referenced, the value must not be deleted. */
48 int param_refs;
49 #endif
50
51 /** Set the optional value to present.
52 * If the value was already present, does nothing.
53 * Else allocates a new (uninitialized) T-type.
54 * @post \c optional_selection is OPTIONAL_PRESENT
55 * @post \c optional_value is not NULL
56 */
57 #ifdef TITAN_RUNTIME_2
58 public:
59 virtual
60 #else
61 inline
62 #endif
63 void set_to_present() {
64 if (optional_selection != OPTIONAL_PRESENT) {
65 optional_selection = OPTIONAL_PRESENT;
66 #ifdef TITAN_RUNTIME_2
67 if (optional_value == NULL)
68 #endif
69 optional_value = new T_type;
70 }
71 }
72
73 /** Set the optional value to omit.
74 * If the value was present, frees it.
75 * @post optional_selection is OPTIONAL_OMIT
76 */
77 #ifdef TITAN_RUNTIME_2
78 public:
79 virtual
80 #else
81 inline
82 #endif
83 void set_to_omit() {
84 #ifdef TITAN_RUNTIME_2
85 if (is_present()) {
86 if (param_refs > 0) {
87 optional_value->clean_up();
88 }
89 else {
90 delete optional_value;
91 optional_value = NULL;
92 }
93 }
94 #else
95 if (optional_selection == OPTIONAL_PRESENT) {
96 delete optional_value;
97 }
98 #endif
99 optional_selection = OPTIONAL_OMIT;
100 }
101
102 public:
103 /// Default constructor creates an unbound object
104 OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
105 #ifdef TITAN_RUNTIME_2
106 , param_refs(0)
107 #endif
108 { }
109
110 /// Construct an optional object set to omit.
111 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
112 OPTIONAL(template_sel other_value);
113
114 /// Copy constructor.
115 /// @note Copying an unbound object creates another unbound object,
116 /// without causing a dynamic test case immediately.
117 OPTIONAL(const OPTIONAL& other_value);
118
119 /// Construct from an optional of different type
120 template <typename T_tmp>
121 OPTIONAL(const OPTIONAL<T_tmp>& other_value);
122
123 /// Construct from an object of different type
124 template <typename T_tmp>
125 OPTIONAL(const T_tmp& other_value)
126 : optional_value(new T_type(other_value))
127 , optional_selection(OPTIONAL_PRESENT)
128 #ifdef TITAN_RUNTIME_2
129 , param_refs(0)
130 #endif
131 { }
132
133 ~OPTIONAL() {
134 #ifdef TITAN_RUNTIME_2
135 if (NULL != optional_value)
136 #else
137 if (optional_selection == OPTIONAL_PRESENT)
138 #endif
139 delete optional_value;
140 }
141
142 void clean_up();
143
144 /// Set to omit.
145 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
146 OPTIONAL& operator=(template_sel other_value);
147
148 /// Copy assignment
149 OPTIONAL& operator=(const OPTIONAL& other_value);
150
151 /// Assign from an optional of another type
152 template <typename T_tmp>
153 OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);
154
155 /// Assign the value
156 template <typename T_tmp>
157 OPTIONAL& operator=(const T_tmp& other_value);
158
159 boolean is_equal(template_sel other_value) const;
160 boolean is_equal(const OPTIONAL& other_value) const;
161 template <typename T_tmp>
162 boolean is_equal(const OPTIONAL<T_tmp>& other_value) const;
163 template <typename T_tmp>
164 boolean is_equal(const T_tmp& other_value) const;
165
166 inline boolean operator==(template_sel other_value) const
167 { return is_equal(other_value); }
168 inline boolean operator!=(template_sel other_value) const
169 { return !is_equal(other_value); }
170 inline boolean operator==(const OPTIONAL& other_value) const
171 { return is_equal(other_value); }
172 inline boolean operator!=(const OPTIONAL& other_value) const
173 { return !is_equal(other_value); }
174 template <typename T_tmp>
175 inline boolean operator==(const T_tmp& other_value) const
176 { return is_equal(other_value); }
177 template <typename T_tmp>
178 inline boolean operator!=(const T_tmp& other_value) const
179 { return !is_equal(other_value); }
180 #ifdef __SUNPRO_CC
181 /* Note: Without these functions the Sun Workshop Pro C++ compiler reports
182 * overloading ambiguity when comparing an optional charstring field with an
183 * optional universal charstring. */
184 template <typename T_tmp>
185 inline boolean operator==(const OPTIONAL<T_tmp>& other_value) const
186 { return is_equal(other_value); }
187 template <typename T_tmp>
188 inline boolean operator!=(const OPTIONAL<T_tmp>& other_value) const
189 { return is_equal(other_value); }
190 #endif
191
192 #ifdef TITAN_RUNTIME_2
193 boolean is_bound() const;
194 #else
195 inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
196 #endif
197 boolean is_value() const
198 { return optional_selection == OPTIONAL_PRESENT && optional_value->is_value(); }
199 /** Whether the optional value is present.
200 * @return \c true if optional_selection is OPTIONAL_PRESENT, else \c false */
201 #ifdef TITAN_RUNTIME_2
202 boolean is_present() const;
203 #else
204 inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
205 #endif
206
207 #ifdef TITAN_RUNTIME_2
208 /** @name override virtual functions of Base_Type
209 * @{ */
210
211 /** Return \c true (this \b is an optional field) */
212 boolean is_optional() const { return TRUE; }
213
214 /** Access the value for read/write
215 *
216 * @return a pointer to the (modifiable) value
217 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
218 */
219 Base_Type* get_opt_value();
220
221 /** Access the value (read/only)
222 *
223 * @return a pointer to the (const) value
224 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
225 */
226 const Base_Type* get_opt_value() const;
227 boolean is_seof() const;
228 boolean is_equal(const Base_Type* other_value) const { return is_equal(*(static_cast<const OPTIONAL*>(other_value))); }
229 void set_value(const Base_Type* other_value) { *this = *(static_cast<const OPTIONAL*>(other_value)); }
230 Base_Type* clone() const { return new OPTIONAL(*this); }
231 const TTCN_Typedescriptor_t* get_descriptor() const;
232 /** @} */
233 #endif
234
235 /** Whether the value is present.
236 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility
237 * with the runtime and existing testports which use this version where
238 * unbound errors are caught before causing more trouble
239 *
240 * @return TRUE if the value is present (optional_selection==OPTIONAL_PRESENT)
241 * @return FALSE if the value is not present (optional_selection==OPTIONAL_OMIT)
242 * @pre the value is bound (optional_selection!=OPTIONAL_UNBOUND)
243 */
244 boolean ispresent() const;
245
246 #ifdef TITAN_RUNTIME_2
247 /** @tricky Calculates and returns the actual state of the optional object,
248 * not just the optional_selection member.
249 * (Only needed in Runtime2, in Runtime1 optional_selection is always up to date.) */
250 optional_sel get_selection() const;
251 #else
252 inline optional_sel get_selection() const { return optional_selection; }
253 #endif
254
255 void log() const;
256 void set_param(Module_Param& param);
257 Module_Param* get_param(Module_Param_Name& param_name) const;
258 void encode_text(Text_Buf& text_buf) const;
259 void decode_text(Text_Buf& text_buf);
260
261 #ifdef TITAN_RUNTIME_2
262 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
263 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
264 #endif
265
266 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
267 int indent, embed_values_enc_struct_t* emb_val) const;
268 #ifdef TITAN_RUNTIME_2
269 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
270 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
271 int indent, embed_values_enc_struct_t* emb_val) const;
272 #endif
273 /** Used during XML decoding, in case this object is an AnyElement field in a record.
274 * Determines whether XER_decode() should be called or this field should be omitted.
275 * The field should be omitted if:
276 * - the next element in the encoded XML is the next field in the record or
277 * - there are no more elements until the end of the record's XML element.
278 *
279 * @param reader parses the encoded XML
280 * @param next_field_name name of the next field in the record, or null if this is the last one
281 * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
282 bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
283 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
284 unsigned int flavor, embed_values_dec_struct_t* emb_val);
285
286 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
287
288 operator T_type&();
289 operator const T_type&() const;
290
291 inline T_type& operator()() { return (T_type&)*this; }
292 inline const T_type& operator()() const { return (const T_type&)*this; }
293
294 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
295 unsigned p_coding) const;
296 #ifdef TITAN_RUNTIME_2
297 ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
298 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
299 #endif
300 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
301 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
302 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
303 const ASN_BER_TLV_t& p_tlv);
304 void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
305
306 #ifdef TITAN_RUNTIME_2
307 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
308 int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
309 const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
310 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
311 boolean no_err=FALSE, boolean first_call=TRUE);
312 #endif
313
314 /** Encodes accordingly to the JSON encoding rules.
315 * Returns the length of the encoded data. */
316 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
317
318 /** Decodes accordingly to the JSON encoding rules.
319 * Returns the length of the decoded data. */
320 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
321
322 #ifdef TITAN_RUNTIME_2
323 /** Called before an element of an optional record of/set of is indexed and passed as an
324 * 'inout' or 'out' parameter to a function (only in Runtime2).
325 * Sets the optional value to present (this would be done by the indexing operation
326 * anyway) and redirects the call to the optional value. */
327 virtual void add_refd_index(int index);
328
329 /** Called after an element of an optional record of/set of is passed as an
330 * 'inout' or 'out' parameter to a function (only in Runtime2).
331 * Redirects the call to the optional value. */
332 virtual void remove_refd_index(int index);
333 #endif
334 };
335
336 #if HAVE_GCC(4,6)
337 #pragma GCC diagnostic push
338 #pragma GCC diagnostic ignored "-Wswitch-enum"
339 #endif
340
341 #ifdef TITAN_RUNTIME_2
342
343 template<typename T_type>
344 Base_Type* OPTIONAL<T_type>::get_opt_value()
345 {
346 #ifdef TITAN_RUNTIME_2
347 if (!is_present())
348 #else
349 if (optional_selection!=OPTIONAL_PRESENT)
350 #endif
351 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
352 return optional_value;
353 }
354
355 template<typename T_type>
356 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
357 {
358 #ifdef TITAN_RUNTIME_2
359 if (!is_present())
360 #else
361 if (optional_selection!=OPTIONAL_PRESENT)
362 #endif
363 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
364 return optional_value;
365 }
366
367 template<typename T_type>
368 boolean OPTIONAL<T_type>::is_seof() const
369 {
370 return
371 #ifdef TITAN_RUNTIME_2
372 (is_present())
373 #else
374 (optional_selection==OPTIONAL_PRESENT)
375 #endif
376 ? optional_value->is_seof() : T_type().is_seof();
377 }
378
379 template<typename T_type>
380 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
381 {
382 return
383 #ifdef TITAN_RUNTIME_2
384 (is_present())
385 #else
386 (optional_selection==OPTIONAL_PRESENT)
387 #endif
388 ? optional_value->get_descriptor() : T_type().get_descriptor();
389 }
390
391 #endif
392
393 template<typename T_type>
394 OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
395 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
396 #ifdef TITAN_RUNTIME_2
397 , param_refs(0)
398 #endif
399 {
400 if (other_value != OMIT_VALUE)
401 TTCN_error("Setting an optional field to an invalid value.");
402 }
403
404 template<typename T_type>
405 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
406 : Base_Type(other_value)
407 #ifdef TITAN_RUNTIME_2
408 , RefdIndexInterface(other_value)
409 #endif
410 , optional_value(NULL)
411 , optional_selection(other_value.optional_selection)
412 #ifdef TITAN_RUNTIME_2
413 , param_refs(0)
414 #endif
415 {
416 switch (other_value.optional_selection) {
417 case OPTIONAL_PRESENT:
418 optional_value = new T_type(*other_value.optional_value);
419 break;
420 case OPTIONAL_OMIT:
421 break;
422 default:
423 break;
424 }
425 }
426
427 template<typename T_type> template<typename T_tmp>
428 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
429 : optional_value(NULL), optional_selection(other_value.get_selection())
430 #ifdef TITAN_RUNTIME_2
431 , param_refs(0)
432 #endif
433 {
434 switch (other_value.get_selection()) {
435 case OPTIONAL_PRESENT:
436 optional_value = new T_type((const T_tmp&)other_value);
437 break;
438 case OPTIONAL_OMIT:
439 break;
440 default:
441 break;
442 }
443 }
444
445 template<typename T_type>
446 void OPTIONAL<T_type>::clean_up()
447 {
448 #ifdef TITAN_RUNTIME_2
449 if (is_present()) {
450 if (param_refs > 0) {
451 optional_value->clean_up();
452 }
453 else {
454 delete optional_value;
455 optional_value = NULL;
456 }
457 }
458 #else
459 if (OPTIONAL_PRESENT == optional_selection) {
460 delete optional_value;
461 }
462 #endif
463 optional_selection = OPTIONAL_UNBOUND;
464 }
465
466 template<typename T_type>
467 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
468 {
469 if (other_value != OMIT_VALUE)
470 TTCN_error("Internal error: Setting an optional field to an invalid value.");
471 set_to_omit();
472 return *this;
473 }
474
475 template<typename T_type>
476 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
477 {
478 switch (other_value.optional_selection) {
479 case OPTIONAL_PRESENT:
480 #ifdef TITAN_RUNTIME_2
481 if (NULL == optional_value) {
482 #else
483 if (optional_selection != OPTIONAL_PRESENT) {
484 #endif
485 optional_value = new T_type(*other_value.optional_value);
486 optional_selection = OPTIONAL_PRESENT;
487 } else *optional_value = *other_value.optional_value;
488 break;
489 case OPTIONAL_OMIT:
490 if (&other_value != this) set_to_omit();
491 break;
492 default:
493 clean_up();
494 break;
495 }
496 return *this;
497 }
498
499 template<typename T_type> template <typename T_tmp>
500 OPTIONAL<T_type>&
501 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
502 {
503 switch (other_value.get_selection()) {
504 case OPTIONAL_PRESENT:
505 #ifdef TITAN_RUNTIME_2
506 if (NULL == optional_value) {
507 #else
508 if (optional_selection != OPTIONAL_PRESENT) {
509 #endif
510 optional_value = new T_type((const T_tmp&)other_value);
511 optional_selection = OPTIONAL_PRESENT;
512 } else *optional_value = (const T_tmp&)other_value;
513 break;
514 case OPTIONAL_OMIT:
515 set_to_omit();
516 break;
517 default:
518 clean_up();
519 break;
520 }
521 return *this;
522 }
523
524 template<typename T_type> template <typename T_tmp>
525 OPTIONAL<T_type>&
526 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
527 {
528 #ifdef TITAN_RUNTIME_2
529 if (NULL == optional_value) {
530 #else
531 if (optional_selection != OPTIONAL_PRESENT) {
532 #endif
533 optional_value = new T_type(other_value);
534 optional_selection = OPTIONAL_PRESENT;
535 } else *optional_value = other_value;
536 return *this;
537 }
538
539 template<typename T_type>
540 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
541 {
542 #ifdef TITAN_RUNTIME_2
543 if (!is_bound()) {
544 #else
545 if (optional_selection == OPTIONAL_UNBOUND) {
546 #endif
547 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
548 TTCN_error("The left operand of comparison is an unbound optional value.");
549 }
550 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
551 "of comparison is an invalid value.");
552 return
553 #ifdef TITAN_RUNTIME_2
554 !is_present();
555 #else
556 optional_selection == OPTIONAL_OMIT;
557 #endif
558 }
559
560 template<typename T_type>
561 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
562 {
563 #ifdef TITAN_RUNTIME_2
564 if (!is_bound()) {
565 if (!other_value.is_bound())
566 #else
567 if (optional_selection == OPTIONAL_UNBOUND) {
568 if (other_value.optional_selection == OPTIONAL_UNBOUND)
569 #endif
570 return TRUE;
571 TTCN_error("The left operand of "
572 "comparison is an unbound optional value.");
573 }
574 #ifdef TITAN_RUNTIME_2
575 if (!other_value.is_bound())
576 #else
577 if (other_value.optional_selection == OPTIONAL_UNBOUND)
578 #endif
579 TTCN_error("The right operand of comparison is an unbound optional value.");
580 #ifdef TITAN_RUNTIME_2
581 boolean present = is_present();
582 if (present != other_value.is_present()) return FALSE;
583 else if (present)
584 #else
585 if (optional_selection != other_value.optional_selection) return FALSE;
586 else if (optional_selection == OPTIONAL_PRESENT)
587 #endif
588 return *optional_value == *other_value.optional_value;
589 else return TRUE;
590 }
591
592 template<typename T_type> template <typename T_tmp>
593 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
594 {
595 #ifdef TITAN_RUNTIME_2
596 switch (get_selection()) {
597 #else
598 switch (optional_selection) {
599 #endif
600 case OPTIONAL_PRESENT:
601 return *optional_value == other_value;
602 case OPTIONAL_OMIT:
603 return FALSE;
604 default:
605 TTCN_error("The left operand of comparison is an unbound optional value.");
606 }
607 return FALSE;
608 }
609
610 template<typename T_type> template <typename T_tmp>
611 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
612 {
613 #ifdef TITAN_RUNTIME_2
614 if (!is_bound()) {
615 if (!other_value.is_bound())
616 #else
617 optional_sel other_selection = other_value.get_selection();
618 if (optional_selection == OPTIONAL_UNBOUND) {
619 if (other_selection == OPTIONAL_UNBOUND)
620 #endif
621 return TRUE;
622 TTCN_error("The left operand of "
623 "comparison is an unbound optional value.");
624 }
625 #ifdef TITAN_RUNTIME_2
626 if (!other_value.is_bound())
627 #else
628 if (other_selection == OPTIONAL_UNBOUND)
629 #endif
630 TTCN_error("The right operand of comparison is an unbound optional value.");
631 #ifdef TITAN_RUNTIME_2
632 boolean present = is_present();
633 if (present != other_value.is_present()) return FALSE;
634 else if (present)
635 #else
636 if (optional_selection != other_selection) return FALSE;
637 else if (optional_selection == OPTIONAL_PRESENT)
638 #endif
639 return *optional_value == (const T_tmp&)other_value;
640 else return TRUE;
641 }
642
643 #ifdef TITAN_RUNTIME_2
644 template<typename T_type>
645 boolean OPTIONAL<T_type>::is_bound() const
646 {
647 switch (optional_selection) {
648 case OPTIONAL_PRESENT:
649 case OPTIONAL_OMIT:
650 return TRUE;
651 default:
652 if (NULL != optional_value) {
653 return optional_value->is_bound();
654 }
655 return FALSE;
656 }
657 }
658
659 template<typename T_type>
660 boolean OPTIONAL<T_type>::is_present() const
661 {
662 switch (optional_selection) {
663 case OPTIONAL_PRESENT:
664 return TRUE;
665 case OPTIONAL_OMIT:
666 default:
667 if (NULL != optional_value) {
668 return optional_value->is_bound();
669 }
670 return FALSE;
671 }
672 }
673 #endif
674
675 template<typename T_type>
676 boolean OPTIONAL<T_type>::ispresent() const
677 {
678 switch (optional_selection) {
679 case OPTIONAL_PRESENT:
680 return TRUE;
681 case OPTIONAL_OMIT:
682 #ifdef TITAN_RUNTIME_2
683 if (NULL != optional_value) {
684 return optional_value->is_bound();
685 }
686 #endif
687 return FALSE;
688 default:
689 #ifdef TITAN_RUNTIME_2
690 if (NULL != optional_value && optional_value->is_bound()) {
691 return TRUE;
692 }
693 #endif
694 TTCN_error("Using an unbound optional field.");
695 }
696 return FALSE;
697 }
698
699 #ifdef TITAN_RUNTIME_2
700 template<typename T_type>
701 optional_sel OPTIONAL<T_type>::get_selection() const
702 {
703 if (is_present()) {
704 return OPTIONAL_PRESENT;
705 }
706 if (is_bound()) {
707 // not present, but bound => omit
708 return OPTIONAL_OMIT;
709 }
710 return OPTIONAL_UNBOUND;
711 }
712 #endif
713
714 template<typename T_type>
715 void OPTIONAL<T_type>::log() const
716 {
717 #ifdef TITAN_RUNTIME_2
718 switch (get_selection()) {
719 #else
720 switch (optional_selection) {
721 #endif
722 case OPTIONAL_PRESENT:
723 optional_value->log();
724 break;
725 case OPTIONAL_OMIT:
726 TTCN_Logger::log_event_str("omit");
727 break;
728 default:
729 TTCN_Logger::log_event_unbound();
730 break;
731 }
732 }
733
734 template <typename T_type>
735 void OPTIONAL<T_type>::set_param(Module_Param& param) {
736 if (param.get_type()==Module_Param::MP_Omit) {
737 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
738 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
739 set_to_omit();
740 return;
741 }
742 set_to_present();
743 optional_value->set_param(param);
744 }
745
746 template <typename T_type>
747 Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
748 {
749 #ifdef TITAN_RUNTIME_2
750 switch (get_selection()) {
751 #else
752 switch (optional_selection) {
753 #endif
754 case OPTIONAL_PRESENT:
755 return optional_value->get_param(param_name);
756 case OPTIONAL_OMIT:
757 return new Module_Param_Omit();
758 default:
759 return new Module_Param_Unbound();
760 }
761 }
762
763 template<typename T_type>
764 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
765 {
766 #ifdef TITAN_RUNTIME_2
767 switch (get_selection()) {
768 #else
769 switch (optional_selection) {
770 #endif
771 case OPTIONAL_OMIT:
772 text_buf.push_int((RInt)FALSE);
773 break;
774 case OPTIONAL_PRESENT:
775 text_buf.push_int((RInt)TRUE);
776 optional_value->encode_text(text_buf);
777 break;
778 default:
779 TTCN_error("Text encoder: Encoding an unbound optional value.");
780 }
781 }
782
783 template<typename T_type>
784 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
785 {
786 if (text_buf.pull_int().get_val()) {
787 set_to_present();
788 optional_value->decode_text(text_buf);
789 } else set_to_omit();
790 }
791
792 template<typename T_type>
793 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
794 {
795 #ifdef TITAN_RUNTIME_2
796 switch(get_selection()) {
797 #else
798 switch(optional_selection) {
799 #endif
800 case OPTIONAL_PRESENT:
801 return optional_value->JSON_encode(p_td, p_tok);
802 case OPTIONAL_OMIT:
803 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
804 default:
805 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
806 "Encoding an unbound optional value.");
807 return -1;
808 }
809 }
810
811 template<typename T_type>
812 int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
813 {
814 // try the optional value first
815 set_to_present();
816 size_t buf_pos = p_tok.get_buf_pos();
817 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
818 if (JSON_ERROR_FATAL == dec_len) {
819 if (p_silent) {
820 clean_up();
821 } else {
822 set_to_omit();
823 }
824 }
825 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
826 // invalid token, rewind the buffer and check if it's a "null" (= omit)
827 // this needs to be checked after the optional value, because it might also be
828 // able to decode a "null" value
829 p_tok.set_buf_pos(buf_pos);
830 json_token_t token = JSON_TOKEN_NONE;
831 dec_len = p_tok.get_next_token(&token, NULL, NULL);
832 if (JSON_TOKEN_LITERAL_NULL == token) {
833 set_to_omit();
834 }
835 else {
836 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
837 dec_len = JSON_ERROR_INVALID_TOKEN;
838 }
839 }
840 return dec_len;
841 }
842
843 #ifdef TITAN_RUNTIME_2
844 template<typename T_type>
845 void OPTIONAL<T_type>::add_refd_index(int index)
846 {
847 ++param_refs;
848 set_to_present();
849 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
850 if (0 != refd_opt_val) {
851 refd_opt_val->add_refd_index(index);
852 }
853 }
854
855 template<typename T_type>
856 void OPTIONAL<T_type>::remove_refd_index(int index)
857 {
858 --param_refs;
859 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
860 if (0 != refd_opt_val) {
861 refd_opt_val->remove_refd_index(index);
862 }
863 }
864 #endif
865
866 template<typename T_type>
867 OPTIONAL<T_type>::operator T_type&()
868 {
869 set_to_present();
870 return *optional_value;
871 }
872
873 template<typename T_type>
874 OPTIONAL<T_type>::operator const T_type&() const
875 {
876 #ifdef TITAN_RUNTIME_2
877 if (!is_present())
878 #else
879 if (optional_selection != OPTIONAL_PRESENT)
880 #endif
881 TTCN_error("Using the value of an optional field containing omit.");
882 return *optional_value;
883 }
884
885 template<typename T_type>
886 ASN_BER_TLV_t*
887 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
888 unsigned p_coding) const
889 {
890 BER_chk_descr(p_td);
891 #ifdef TITAN_RUNTIME_2
892 switch (get_selection()) {
893 #else
894 switch (optional_selection) {
895 #endif
896 case OPTIONAL_PRESENT:
897 return optional_value->BER_encode_TLV(p_td, p_coding);
898 case OPTIONAL_OMIT:
899 return ASN_BER_TLV_t::construct();
900 default:
901 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
902 }
903 }
904
905 #ifdef TITAN_RUNTIME_2
906 template<typename T_type>
907 ASN_BER_TLV_t*
908 OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
909 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
910 {
911 BER_chk_descr(p_td);
912 #ifdef TITAN_RUNTIME_2
913 switch (get_selection()) {
914 #else
915 switch (optional_selection) {
916 #endif
917 case OPTIONAL_PRESENT:
918 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
919 case OPTIONAL_OMIT:
920 return ASN_BER_TLV_t::construct();
921 default:
922 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
923 }
924 }
925
926 template<typename T_type>
927 int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
928 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
929 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
930 {
931 TTCN_error("RAW decoding requested for optional type '%s'"
932 " which has no RAW decoding method.",p_td.name);
933 return 0;
934 }
935 #endif
936
937 template<typename T_type>
938 int
939 OPTIONAL<T_type>::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const
940 {
941 #ifdef TITAN_RUNTIME_2
942 switch (get_selection()) {
943 #else
944 switch (optional_selection) {
945 #endif
946 case OPTIONAL_PRESENT:
947 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
948 case OPTIONAL_OMIT:
949 return 0; // nothing to do !
950 default:
951 TTCN_EncDec_ErrorContext::error(
952 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
953 return 0;
954 }
955 }
956
957 #ifdef TITAN_RUNTIME_2
958 template<typename T_type>
959 int
960 OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
961 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
962 embed_values_enc_struct_t* emb_val) const
963 {
964 switch (get_selection()) {
965 case OPTIONAL_PRESENT:
966 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
967 case OPTIONAL_OMIT:
968 return 0; // nothing to do !
969 default:
970 TTCN_EncDec_ErrorContext::error(
971 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
972 return 0;
973 }
974 }
975 #endif
976
977
978 template<typename T_type>
979 bool
980 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
981 {
982 // If the record has no elements, then it can't have an AnyElement
983 if (parent_tag_closed) {
984 set_to_omit();
985 return false;
986 }
987
988 while (reader.Ok()) {
989 // Leaving the record before finding an element -> no AnyElement
990 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
991 set_to_omit();
992 return false;
993 }
994 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
995 // The first element found is either the next field's element or the AnyElement
996 if (NULL != next_field_name &&
997 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
998 set_to_omit();
999 return false;
1000 }
1001 break;
1002 }
1003 reader.Read();
1004 }
1005
1006 return true;
1007 }
1008
1009 template<typename T_type>
1010 int
1011 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1012 unsigned int flavor, embed_values_dec_struct_t* emb_val)
1013 {
1014 int exer = is_exer(flavor);
1015 for (int success = reader.Ok(); success==1; success=reader.Read()) {
1016 int type = reader.NodeType();
1017 const char * name; // name of the optional field
1018 const char * ns_uri;
1019
1020 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1021 if (XML_READER_TYPE_ATTRIBUTE == type) {
1022 for (; success==1; success = reader.MoveToNextAttribute()) {
1023 if (!reader.IsNamespaceDecl()) break;
1024 }
1025
1026 name = (const char*)reader.LocalName();
1027 if (!check_name(name, p_td, exer)) { // it's not us, bail
1028 break;
1029 }
1030 // we already checked for exer==1
1031 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1032
1033 // set to omit if the attribute is empty
1034 const char * value = (const char *)reader.Value();
1035 if (strlen(value) == 0) {
1036 break;
1037 }
1038
1039 set_to_present();
1040 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1041 goto finished;
1042 }
1043 else break;
1044 }
1045 else { // not attribute
1046 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1047 name = (const char*)reader.LocalName();
1048 ns_uri = (const char*)reader.NamespaceUri();
1049 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1050 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1051 // If the optional field (a string) has anyElement, accept the element
1052 // regardless of its name. Else the name (and namespace) must match.
1053 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1054 found_it:
1055 set_to_present();
1056 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
1057 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1058 if (!optional_value->is_bound()) {
1059 set_to_omit();
1060 }
1061 }
1062 else break; // it's not us, bail
1063
1064 goto finished;
1065 }
1066 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1067 goto found_it;
1068 }
1069 else if (XML_READER_TYPE_END_ELEMENT == type) {
1070 break;
1071 }
1072 // else circle around
1073 } // if attribute
1074 } // next
1075 set_to_omit();
1076 return 0;
1077 finished:
1078 return 1;
1079 }
1080
1081 template<typename T_type>
1082 char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
1083 #ifdef TITAN_RUNTIME_2
1084 switch (get_selection()) {
1085 #else
1086 switch (optional_selection) {
1087 #endif
1088 case OPTIONAL_PRESENT:
1089 return optional_value->collect_ns(p_td, num, def_ns);
1090 case OPTIONAL_OMIT:
1091 def_ns = false;
1092 num = 0;
1093 return 0;
1094 default:
1095 TTCN_EncDec_ErrorContext::error(
1096 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1097 return 0;
1098 }
1099 }
1100
1101 template<typename T_type>
1102 boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1103 const ASN_BER_TLV_t& p_tlv,
1104 unsigned L_form)
1105 {
1106 BER_chk_descr(p_td);
1107 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1108 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1109 } else {
1110 set_to_omit();
1111 return TRUE;
1112 }
1113 }
1114
1115 template<typename T_type>
1116 boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1117 const ASN_BER_TLV_t& p_tlv)
1118 {
1119 set_to_present();
1120 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1121 }
1122
1123 template<typename T_type>
1124 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1125 unsigned L_form)
1126 {
1127 #ifdef TITAN_RUNTIME_2
1128 if (is_present()) {
1129 optional_selection = OPTIONAL_PRESENT;
1130 #else
1131 if (optional_selection==OPTIONAL_PRESENT) {
1132 #endif
1133 optional_value->BER_decode_opentypes(p_typelist, L_form);
1134 }
1135 }
1136
1137 #ifdef TITAN_RUNTIME_2
1138
1139 template<typename T_type>
1140 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1141 TTCN_Buffer& buff) const
1142 {
1143 if (is_present())
1144 return optional_value->TEXT_encode(p_td, buff);
1145 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1146 return 0;
1147 }
1148
1149 template<typename T_type>
1150 int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1151 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1152 {
1153 if (is_present())
1154 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1155 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1156 return 0;
1157 }
1158
1159 template<typename T_type>
1160 int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1161 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1162 {
1163 set_to_present();
1164 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1165 }
1166
1167 #endif
1168
1169 #if defined(__GNUC__) && __GNUC__ >= 3
1170 /** Note: These functions allow most efficient operation by passing the left
1171 * operand OMIT_VALUE as value instead of constant reference.
1172 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1173 template<typename T_type>
1174 inline boolean operator==(template_sel left_value,
1175 const OPTIONAL<T_type>& right_value)
1176 { return right_value.is_equal(left_value); }
1177
1178 template<typename T_type>
1179 inline boolean operator!=(template_sel left_value,
1180 const OPTIONAL<T_type>& right_value)
1181 { return !right_value.is_equal(left_value); }
1182 #endif
1183
1184 template<typename T_left, typename T_right>
1185 inline boolean operator==(const T_left& left_value,
1186 const OPTIONAL<T_right>& right_value)
1187 { return right_value.is_equal(left_value); }
1188
1189 template<typename T_left, typename T_right>
1190 inline boolean operator!=(const T_left& left_value,
1191 const OPTIONAL<T_right>& right_value)
1192 { return !right_value.is_equal(left_value); }
1193
1194 #endif
1195
1196 #if HAVE_GCC(4,6)
1197 #pragma GCC diagnostic pop
1198 #endif
This page took 0.090069 seconds and 5 git commands to generate.