Update README.linux
[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 #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 void encode_text(Text_Buf& text_buf) const;
258 void decode_text(Text_Buf& text_buf);
259
260 #ifdef TITAN_RUNTIME_2
261 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
262 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
263 #endif
264
265 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
266 int indent, embed_values_enc_struct_t* emb_val) const;
267 #ifdef TITAN_RUNTIME_2
268 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
269 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
270 int indent, embed_values_enc_struct_t* emb_val) const;
271 #endif
272 /** Used during XML decoding, in case this object is an AnyElement field in a record.
273 * Determines whether XER_decode() should be called or this field should be omitted.
274 * The field should be omitted if:
275 * - the next element in the encoded XML is the next field in the record or
276 * - there are no more elements until the end of the record's XML element.
277 *
278 * @param reader parses the encoded XML
279 * @param next_field_name name of the next field in the record, or null if this is the last one
280 * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
281 bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
282 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
283 unsigned int flavor, embed_values_dec_struct_t* emb_val);
284
285 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
286
287 operator T_type&();
288 operator const T_type&() const;
289
290 inline T_type& operator()() { return (T_type&)*this; }
291 inline const T_type& operator()() const { return (const T_type&)*this; }
292
293 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
294 unsigned p_coding) const;
295 #ifdef TITAN_RUNTIME_2
296 ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
297 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
298 #endif
299 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
300 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
301 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
302 const ASN_BER_TLV_t& p_tlv);
303 void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
304
305 #ifdef TITAN_RUNTIME_2
306 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
307 int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
308 const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
309 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
310 boolean no_err=FALSE, boolean first_call=TRUE);
311 #endif
312
313 /** Encodes accordingly to the JSON encoding rules.
314 * Returns the length of the encoded data. */
315 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
316
317 /** Decodes accordingly to the JSON encoding rules.
318 * Returns the length of the decoded data. */
319 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
320
321 #ifdef TITAN_RUNTIME_2
322 /** Called before an element of an optional record of/set of is indexed and passed as an
323 * 'inout' or 'out' parameter to a function (only in Runtime2).
324 * Sets the optional value to present (this would be done by the indexing operation
325 * anyway) and redirects the call to the optional value. */
326 virtual void add_refd_index(int index);
327
328 /** Called after an element of an optional record of/set of is passed as an
329 * 'inout' or 'out' parameter to a function (only in Runtime2).
330 * Redirects the call to the optional value. */
331 virtual void remove_refd_index(int index);
332 #endif
333 };
334
335 #if HAVE_GCC(4,6)
336 #pragma GCC diagnostic push
337 #pragma GCC diagnostic ignored "-Wswitch-enum"
338 #endif
339
340 #ifdef TITAN_RUNTIME_2
341
342 template<typename T_type>
343 Base_Type* OPTIONAL<T_type>::get_opt_value()
344 {
345 #ifdef TITAN_RUNTIME_2
346 if (!is_present())
347 #else
348 if (optional_selection!=OPTIONAL_PRESENT)
349 #endif
350 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
351 return optional_value;
352 }
353
354 template<typename T_type>
355 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
356 {
357 #ifdef TITAN_RUNTIME_2
358 if (!is_present())
359 #else
360 if (optional_selection!=OPTIONAL_PRESENT)
361 #endif
362 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
363 return optional_value;
364 }
365
366 template<typename T_type>
367 boolean OPTIONAL<T_type>::is_seof() const
368 {
369 return
370 #ifdef TITAN_RUNTIME_2
371 (is_present())
372 #else
373 (optional_selection==OPTIONAL_PRESENT)
374 #endif
375 ? optional_value->is_seof() : T_type().is_seof();
376 }
377
378 template<typename T_type>
379 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
380 {
381 return
382 #ifdef TITAN_RUNTIME_2
383 (is_present())
384 #else
385 (optional_selection==OPTIONAL_PRESENT)
386 #endif
387 ? optional_value->get_descriptor() : T_type().get_descriptor();
388 }
389
390 #endif
391
392 template<typename T_type>
393 OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
394 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
395 #ifdef TITAN_RUNTIME_2
396 , param_refs(0)
397 #endif
398 {
399 if (other_value != OMIT_VALUE)
400 TTCN_error("Setting an optional field to an invalid value.");
401 }
402
403 template<typename T_type>
404 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
405 : Base_Type(other_value)
406 , optional_value(NULL)
407 , optional_selection(other_value.optional_selection)
408 #ifdef TITAN_RUNTIME_2
409 , param_refs(0)
410 #endif
411 {
412 switch (other_value.optional_selection) {
413 case OPTIONAL_PRESENT:
414 optional_value = new T_type(*other_value.optional_value);
415 break;
416 case OPTIONAL_OMIT:
417 break;
418 default:
419 break;
420 }
421 }
422
423 template<typename T_type> template<typename T_tmp>
424 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
425 : optional_value(NULL), optional_selection(other_value.get_selection())
426 #ifdef TITAN_RUNTIME_2
427 , param_refs(0)
428 #endif
429 {
430 switch (other_value.get_selection()) {
431 case OPTIONAL_PRESENT:
432 optional_value = new T_type((const T_tmp&)other_value);
433 break;
434 case OPTIONAL_OMIT:
435 break;
436 default:
437 break;
438 }
439 }
440
441 template<typename T_type>
442 void OPTIONAL<T_type>::clean_up()
443 {
444 #ifdef TITAN_RUNTIME_2
445 if (is_present()) {
446 if (param_refs > 0) {
447 optional_value->clean_up();
448 }
449 else {
450 delete optional_value;
451 optional_value = NULL;
452 }
453 }
454 #else
455 if (OPTIONAL_PRESENT == optional_selection) {
456 delete optional_value;
457 }
458 #endif
459 optional_selection = OPTIONAL_UNBOUND;
460 }
461
462 template<typename T_type>
463 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
464 {
465 if (other_value != OMIT_VALUE)
466 TTCN_error("Internal error: Setting an optional field to an invalid value.");
467 set_to_omit();
468 return *this;
469 }
470
471 template<typename T_type>
472 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
473 {
474 switch (other_value.optional_selection) {
475 case OPTIONAL_PRESENT:
476 #ifdef TITAN_RUNTIME_2
477 if (NULL == optional_value) {
478 #else
479 if (optional_selection != OPTIONAL_PRESENT) {
480 #endif
481 optional_value = new T_type(*other_value.optional_value);
482 optional_selection = OPTIONAL_PRESENT;
483 } else *optional_value = *other_value.optional_value;
484 break;
485 case OPTIONAL_OMIT:
486 if (&other_value != this) set_to_omit();
487 break;
488 default:
489 clean_up();
490 break;
491 }
492 return *this;
493 }
494
495 template<typename T_type> template <typename T_tmp>
496 OPTIONAL<T_type>&
497 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
498 {
499 switch (other_value.get_selection()) {
500 case OPTIONAL_PRESENT:
501 #ifdef TITAN_RUNTIME_2
502 if (NULL == optional_value) {
503 #else
504 if (optional_selection != OPTIONAL_PRESENT) {
505 #endif
506 optional_value = new T_type((const T_tmp&)other_value);
507 optional_selection = OPTIONAL_PRESENT;
508 } else *optional_value = (const T_tmp&)other_value;
509 break;
510 case OPTIONAL_OMIT:
511 set_to_omit();
512 break;
513 default:
514 clean_up();
515 break;
516 }
517 return *this;
518 }
519
520 template<typename T_type> template <typename T_tmp>
521 OPTIONAL<T_type>&
522 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
523 {
524 #ifdef TITAN_RUNTIME_2
525 if (NULL == optional_value) {
526 #else
527 if (optional_selection != OPTIONAL_PRESENT) {
528 #endif
529 optional_value = new T_type(other_value);
530 optional_selection = OPTIONAL_PRESENT;
531 } else *optional_value = other_value;
532 return *this;
533 }
534
535 template<typename T_type>
536 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
537 {
538 #ifdef TITAN_RUNTIME_2
539 if (!is_bound()) {
540 #else
541 if (optional_selection == OPTIONAL_UNBOUND) {
542 #endif
543 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
544 TTCN_error("The left operand of comparison is an unbound optional value.");
545 }
546 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
547 "of comparison is an invalid value.");
548 return
549 #ifdef TITAN_RUNTIME_2
550 !is_present();
551 #else
552 optional_selection == OPTIONAL_OMIT;
553 #endif
554 }
555
556 template<typename T_type>
557 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
558 {
559 #ifdef TITAN_RUNTIME_2
560 if (!is_bound()) {
561 if (!other_value.is_bound())
562 #else
563 if (optional_selection == OPTIONAL_UNBOUND) {
564 if (other_value.optional_selection == OPTIONAL_UNBOUND)
565 #endif
566 return TRUE;
567 TTCN_error("The left operand of "
568 "comparison is an unbound optional value.");
569 }
570 #ifdef TITAN_RUNTIME_2
571 if (!other_value.is_bound())
572 #else
573 if (other_value.optional_selection == OPTIONAL_UNBOUND)
574 #endif
575 TTCN_error("The right operand of comparison is an unbound optional value.");
576 #ifdef TITAN_RUNTIME_2
577 boolean present = is_present();
578 if (present != other_value.is_present()) return FALSE;
579 else if (present)
580 #else
581 if (optional_selection != other_value.optional_selection) return FALSE;
582 else if (optional_selection == OPTIONAL_PRESENT)
583 #endif
584 return *optional_value == *other_value.optional_value;
585 else return TRUE;
586 }
587
588 template<typename T_type> template <typename T_tmp>
589 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
590 {
591 #ifdef TITAN_RUNTIME_2
592 switch (get_selection()) {
593 #else
594 switch (optional_selection) {
595 #endif
596 case OPTIONAL_PRESENT:
597 return *optional_value == other_value;
598 case OPTIONAL_OMIT:
599 return FALSE;
600 default:
601 TTCN_error("The left operand of comparison is an unbound optional value.");
602 }
603 return FALSE;
604 }
605
606 template<typename T_type> template <typename T_tmp>
607 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
608 {
609 #ifdef TITAN_RUNTIME_2
610 if (!is_bound()) {
611 if (!other_value.is_bound())
612 #else
613 optional_sel other_selection = other_value.get_selection();
614 if (optional_selection == OPTIONAL_UNBOUND) {
615 if (other_selection == OPTIONAL_UNBOUND)
616 #endif
617 return TRUE;
618 TTCN_error("The left operand of "
619 "comparison is an unbound optional value.");
620 }
621 #ifdef TITAN_RUNTIME_2
622 if (!other_value.is_bound())
623 #else
624 if (other_selection == OPTIONAL_UNBOUND)
625 #endif
626 TTCN_error("The right operand of comparison is an unbound optional value.");
627 #ifdef TITAN_RUNTIME_2
628 boolean present = is_present();
629 if (present != other_value.is_present()) return FALSE;
630 else if (present)
631 #else
632 if (optional_selection != other_selection) return FALSE;
633 else if (optional_selection == OPTIONAL_PRESENT)
634 #endif
635 return *optional_value == (const T_tmp&)other_value;
636 else return TRUE;
637 }
638
639 #ifdef TITAN_RUNTIME_2
640 template<typename T_type>
641 boolean OPTIONAL<T_type>::is_bound() const
642 {
643 switch (optional_selection) {
644 case OPTIONAL_PRESENT:
645 case OPTIONAL_OMIT:
646 return TRUE;
647 default:
648 if (NULL != optional_value) {
649 return optional_value->is_bound();
650 }
651 return FALSE;
652 }
653 }
654
655 template<typename T_type>
656 boolean OPTIONAL<T_type>::is_present() const
657 {
658 switch (optional_selection) {
659 case OPTIONAL_PRESENT:
660 return TRUE;
661 case OPTIONAL_OMIT:
662 default:
663 if (NULL != optional_value) {
664 return optional_value->is_bound();
665 }
666 return FALSE;
667 }
668 }
669 #endif
670
671 template<typename T_type>
672 boolean OPTIONAL<T_type>::ispresent() const
673 {
674 switch (optional_selection) {
675 case OPTIONAL_PRESENT:
676 return TRUE;
677 case OPTIONAL_OMIT:
678 #ifdef TITAN_RUNTIME_2
679 if (NULL != optional_value) {
680 return optional_value->is_bound();
681 }
682 #endif
683 return FALSE;
684 default:
685 #ifdef TITAN_RUNTIME_2
686 if (NULL != optional_value && optional_value->is_bound()) {
687 return TRUE;
688 }
689 #endif
690 TTCN_error("Using an unbound optional field.");
691 }
692 return FALSE;
693 }
694
695 #ifdef TITAN_RUNTIME_2
696 template<typename T_type>
697 optional_sel OPTIONAL<T_type>::get_selection() const
698 {
699 if (is_present()) {
700 return OPTIONAL_PRESENT;
701 }
702 if (is_bound()) {
703 // not present, but bound => omit
704 return OPTIONAL_OMIT;
705 }
706 return OPTIONAL_UNBOUND;
707 }
708 #endif
709
710 template<typename T_type>
711 void OPTIONAL<T_type>::log() const
712 {
713 #ifdef TITAN_RUNTIME_2
714 switch (get_selection()) {
715 #else
716 switch (optional_selection) {
717 #endif
718 case OPTIONAL_PRESENT:
719 optional_value->log();
720 break;
721 case OPTIONAL_OMIT:
722 TTCN_Logger::log_event_str("omit");
723 break;
724 default:
725 TTCN_Logger::log_event_unbound();
726 break;
727 }
728 }
729
730 template <typename T_type>
731 void OPTIONAL<T_type>::set_param(Module_Param& param) {
732 if (param.get_type()==Module_Param::MP_Omit) {
733 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
734 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
735 set_to_omit();
736 return;
737 }
738 set_to_present();
739 optional_value->set_param(param);
740 }
741
742 template<typename T_type>
743 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
744 {
745 #ifdef TITAN_RUNTIME_2
746 switch (get_selection()) {
747 #else
748 switch (optional_selection) {
749 #endif
750 case OPTIONAL_OMIT:
751 text_buf.push_int((RInt)FALSE);
752 break;
753 case OPTIONAL_PRESENT:
754 text_buf.push_int((RInt)TRUE);
755 optional_value->encode_text(text_buf);
756 break;
757 default:
758 TTCN_error("Text encoder: Encoding an unbound optional value.");
759 }
760 }
761
762 template<typename T_type>
763 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
764 {
765 if (text_buf.pull_int().get_val()) {
766 set_to_present();
767 optional_value->decode_text(text_buf);
768 } else set_to_omit();
769 }
770
771 template<typename T_type>
772 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
773 {
774 #ifdef TITAN_RUNTIME_2
775 switch(get_selection()) {
776 #else
777 switch(optional_selection) {
778 #endif
779 case OPTIONAL_PRESENT:
780 return optional_value->JSON_encode(p_td, p_tok);
781 case OPTIONAL_OMIT:
782 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
783 default:
784 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
785 "Encoding an unbound optional value.");
786 return -1;
787 }
788 }
789
790 template<typename T_type>
791 int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
792 {
793 // try the optional value first
794 set_to_present();
795 size_t buf_pos = p_tok.get_buf_pos();
796 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
797 if (JSON_ERROR_FATAL == dec_len) {
798 if (p_silent) {
799 clean_up();
800 } else {
801 set_to_omit();
802 }
803 }
804 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
805 // invalid token, rewind the buffer and check if it's a "null" (= omit)
806 // this needs to be checked after the optional value, because it might also be
807 // able to decode a "null" value
808 p_tok.set_buf_pos(buf_pos);
809 json_token_t token = JSON_TOKEN_NONE;
810 dec_len = p_tok.get_next_token(&token, NULL, NULL);
811 if (JSON_TOKEN_LITERAL_NULL == token) {
812 set_to_omit();
813 }
814 else {
815 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
816 dec_len = JSON_ERROR_INVALID_TOKEN;
817 }
818 }
819 return dec_len;
820 }
821
822 #ifdef TITAN_RUNTIME_2
823 template<typename T_type>
824 void OPTIONAL<T_type>::add_refd_index(int index)
825 {
826 ++param_refs;
827 set_to_present();
828 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
829 if (0 != refd_opt_val) {
830 refd_opt_val->add_refd_index(index);
831 }
832 }
833
834 template<typename T_type>
835 void OPTIONAL<T_type>::remove_refd_index(int index)
836 {
837 --param_refs;
838 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
839 if (0 != refd_opt_val) {
840 refd_opt_val->remove_refd_index(index);
841 }
842 }
843 #endif
844
845 template<typename T_type>
846 OPTIONAL<T_type>::operator T_type&()
847 {
848 set_to_present();
849 return *optional_value;
850 }
851
852 template<typename T_type>
853 OPTIONAL<T_type>::operator const T_type&() const
854 {
855 #ifdef TITAN_RUNTIME_2
856 if (!is_present())
857 #else
858 if (optional_selection != OPTIONAL_PRESENT)
859 #endif
860 TTCN_error("Using the value of an optional field containing omit.");
861 return *optional_value;
862 }
863
864 template<typename T_type>
865 ASN_BER_TLV_t*
866 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
867 unsigned p_coding) const
868 {
869 BER_chk_descr(p_td);
870 #ifdef TITAN_RUNTIME_2
871 switch (get_selection()) {
872 #else
873 switch (optional_selection) {
874 #endif
875 case OPTIONAL_PRESENT:
876 return optional_value->BER_encode_TLV(p_td, p_coding);
877 case OPTIONAL_OMIT:
878 return ASN_BER_TLV_t::construct();
879 default:
880 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
881 }
882 }
883
884 #ifdef TITAN_RUNTIME_2
885 template<typename T_type>
886 ASN_BER_TLV_t*
887 OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
888 const TTCN_Typedescriptor_t& p_td, 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_negtest(p_err_descr, 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 template<typename T_type>
906 int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
907 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
908 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
909 {
910 TTCN_error("RAW decoding requested for optional type '%s'"
911 " which has no RAW decoding method.",p_td.name);
912 return 0;
913 }
914 #endif
915
916 template<typename T_type>
917 int
918 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
919 {
920 #ifdef TITAN_RUNTIME_2
921 switch (get_selection()) {
922 #else
923 switch (optional_selection) {
924 #endif
925 case OPTIONAL_PRESENT:
926 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
927 case OPTIONAL_OMIT:
928 return 0; // nothing to do !
929 default:
930 TTCN_EncDec_ErrorContext::error(
931 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
932 return 0;
933 }
934 }
935
936 #ifdef TITAN_RUNTIME_2
937 template<typename T_type>
938 int
939 OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
940 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
941 embed_values_enc_struct_t* emb_val) const
942 {
943 switch (get_selection()) {
944 case OPTIONAL_PRESENT:
945 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
946 case OPTIONAL_OMIT:
947 return 0; // nothing to do !
948 default:
949 TTCN_EncDec_ErrorContext::error(
950 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
951 return 0;
952 }
953 }
954 #endif
955
956
957 template<typename T_type>
958 bool
959 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
960 {
961 // If the record has no elements, then it can't have an AnyElement
962 if (parent_tag_closed) {
963 set_to_omit();
964 return false;
965 }
966
967 while (reader.Ok()) {
968 // Leaving the record before finding an element -> no AnyElement
969 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
970 set_to_omit();
971 return false;
972 }
973 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
974 // The first element found is either the next field's element or the AnyElement
975 if (NULL != next_field_name &&
976 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
977 set_to_omit();
978 return false;
979 }
980 break;
981 }
982 reader.Read();
983 }
984
985 return true;
986 }
987
988 template<typename T_type>
989 int
990 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
991 unsigned int flavor, embed_values_dec_struct_t* emb_val)
992 {
993 int exer = is_exer(flavor);
994 for (int success = reader.Ok(); success==1; success=reader.Read()) {
995 int type = reader.NodeType();
996 const char * name; // name of the optional field
997 const char * ns_uri;
998
999 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1000 if (XML_READER_TYPE_ATTRIBUTE == type) {
1001 for (; success==1; success = reader.MoveToNextAttribute()) {
1002 if (!reader.IsNamespaceDecl()) break;
1003 }
1004
1005 name = (const char*)reader.LocalName();
1006 if (!check_name(name, p_td, exer)) { // it's not us, bail
1007 break;
1008 }
1009 // we already checked for exer==1
1010 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1011
1012 // set to omit if the attribute is empty
1013 const char * value = (const char *)reader.Value();
1014 if (strlen(value) == 0) {
1015 break;
1016 }
1017
1018 set_to_present();
1019 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1020 goto finished;
1021 }
1022 else break;
1023 }
1024 else { // not attribute
1025 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1026 name = (const char*)reader.LocalName();
1027 ns_uri = (const char*)reader.NamespaceUri();
1028 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1029 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1030 // If the optional field (a string) has anyElement, accept the element
1031 // regardless of its name. Else the name (and namespace) must match.
1032 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1033 found_it:
1034 set_to_present();
1035 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
1036 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1037 }
1038 else break; // it's not us, bail
1039
1040 goto finished;
1041 }
1042 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1043 goto found_it;
1044 }
1045 else if (XML_READER_TYPE_END_ELEMENT == type) {
1046 break;
1047 }
1048 // else circle around
1049 } // if attribute
1050 } // next
1051 set_to_omit();
1052 return 0;
1053 finished:
1054 return 1;
1055 }
1056
1057 template<typename T_type>
1058 char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
1059 #ifdef TITAN_RUNTIME_2
1060 switch (get_selection()) {
1061 #else
1062 switch (optional_selection) {
1063 #endif
1064 case OPTIONAL_PRESENT:
1065 return optional_value->collect_ns(p_td, num, def_ns);
1066 case OPTIONAL_OMIT:
1067 def_ns = false;
1068 num = 0;
1069 return 0;
1070 default:
1071 TTCN_EncDec_ErrorContext::error(
1072 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1073 return 0;
1074 }
1075 }
1076
1077 template<typename T_type>
1078 boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1079 const ASN_BER_TLV_t& p_tlv,
1080 unsigned L_form)
1081 {
1082 BER_chk_descr(p_td);
1083 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1084 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1085 } else {
1086 set_to_omit();
1087 return TRUE;
1088 }
1089 }
1090
1091 template<typename T_type>
1092 boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1093 const ASN_BER_TLV_t& p_tlv)
1094 {
1095 set_to_present();
1096 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1097 }
1098
1099 template<typename T_type>
1100 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1101 unsigned L_form)
1102 {
1103 #ifdef TITAN_RUNTIME_2
1104 if (is_present()) {
1105 optional_selection = OPTIONAL_PRESENT;
1106 #else
1107 if (optional_selection==OPTIONAL_PRESENT) {
1108 #endif
1109 optional_value->BER_decode_opentypes(p_typelist, L_form);
1110 }
1111 }
1112
1113 #ifdef TITAN_RUNTIME_2
1114
1115 template<typename T_type>
1116 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1117 TTCN_Buffer& buff) const
1118 {
1119 if (is_present())
1120 return optional_value->TEXT_encode(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_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1127 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1128 {
1129 if (is_present())
1130 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1131 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1132 return 0;
1133 }
1134
1135 template<typename T_type>
1136 int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1137 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1138 {
1139 set_to_present();
1140 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1141 }
1142
1143 #endif
1144
1145 #if defined(__GNUC__) && __GNUC__ >= 3
1146 /** Note: These functions allow most efficient operation by passing the left
1147 * operand OMIT_VALUE as value instead of constant reference.
1148 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1149 template<typename T_type>
1150 inline boolean operator==(template_sel left_value,
1151 const OPTIONAL<T_type>& right_value)
1152 { return right_value.is_equal(left_value); }
1153
1154 template<typename T_type>
1155 inline boolean operator!=(template_sel left_value,
1156 const OPTIONAL<T_type>& right_value)
1157 { return !right_value.is_equal(left_value); }
1158 #endif
1159
1160 template<typename T_left, typename T_right>
1161 inline boolean operator==(const T_left& left_value,
1162 const OPTIONAL<T_right>& right_value)
1163 { return right_value.is_equal(left_value); }
1164
1165 template<typename T_left, typename T_right>
1166 inline boolean operator!=(const T_left& left_value,
1167 const OPTIONAL<T_right>& right_value)
1168 { return !right_value.is_equal(left_value); }
1169
1170 #endif
1171
1172 #if HAVE_GCC(4,6)
1173 #pragma GCC diagnostic pop
1174 #endif
This page took 0.057378 seconds and 5 git commands to generate.