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