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 ///////////////////////////////////////////////////////////////////////////////
12 #include "Param_Types.hh"
13 #include "Basetype.hh"
14 #include "Template.hh"
20 #include "Parameters.h"
23 #include "XmlReader.hh"
25 enum optional_sel { OPTIONAL_UNBOUND, OPTIONAL_OMIT, OPTIONAL_PRESENT };
27 template <typename T_type>
28 class OPTIONAL : public Base_Type
29 #ifdef TITAN_RUNTIME_2
30 , public RefdIndexInterface
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;
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;
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. */
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
57 #ifdef TITAN_RUNTIME_2
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)
69 optional_value = new T_type;
73 /** Set the optional value to omit.
74 * If the value was present, frees it.
75 * @post optional_selection is OPTIONAL_OMIT
77 #ifdef TITAN_RUNTIME_2
84 #ifdef TITAN_RUNTIME_2
87 optional_value->clean_up();
90 delete optional_value;
91 optional_value = NULL;
95 if (optional_selection == OPTIONAL_PRESENT) {
96 delete optional_value;
99 optional_selection = OPTIONAL_OMIT;
103 /// Default constructor creates an unbound object
104 OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
105 #ifdef TITAN_RUNTIME_2
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);
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);
119 /// Construct from an optional of different type
120 template <typename T_tmp>
121 OPTIONAL(const OPTIONAL<T_tmp>& other_value);
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
134 #ifdef TITAN_RUNTIME_2
135 if (NULL != optional_value)
137 if (optional_selection == OPTIONAL_PRESENT)
139 delete optional_value;
145 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
146 OPTIONAL& operator=(template_sel other_value);
149 OPTIONAL& operator=(const OPTIONAL& other_value);
151 /// Assign from an optional of another type
152 template <typename T_tmp>
153 OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);
156 template <typename T_tmp>
157 OPTIONAL& operator=(const T_tmp& other_value);
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;
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); }
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); }
192 #ifdef TITAN_RUNTIME_2
193 boolean is_bound() const;
195 inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
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;
204 inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
207 #ifdef TITAN_RUNTIME_2
208 /** @name override virtual functions of Base_Type
211 /** Return \c true (this \b is an optional field) */
212 boolean is_optional() const { return TRUE; }
214 /** Access the value for read/write
216 * @return a pointer to the (modifiable) value
217 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
219 Base_Type* get_opt_value();
221 /** Access the value (read/only)
223 * @return a pointer to the (const) value
224 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
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;
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
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)
244 boolean ispresent() const;
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;
252 inline optional_sel get_selection() const { return optional_selection; }
256 void set_param(Module_Param& param);
257 void encode_text(Text_Buf& text_buf) const;
258 void decode_text(Text_Buf& text_buf);
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);
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;
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.
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);
285 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
288 operator const T_type&() const;
290 inline T_type& operator()() { return (T_type&)*this; }
291 inline const T_type& operator()() const { return (const T_type&)*this; }
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;
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);
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);
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;
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);
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);
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);
336 #pragma GCC diagnostic push
337 #pragma GCC diagnostic ignored "-Wswitch-enum"
340 #ifdef TITAN_RUNTIME_2
342 template<typename T_type>
343 Base_Type* OPTIONAL<T_type>::get_opt_value()
345 #ifdef TITAN_RUNTIME_2
348 if (optional_selection!=OPTIONAL_PRESENT)
350 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
351 return optional_value;
354 template<typename T_type>
355 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
357 #ifdef TITAN_RUNTIME_2
360 if (optional_selection!=OPTIONAL_PRESENT)
362 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
363 return optional_value;
366 template<typename T_type>
367 boolean OPTIONAL<T_type>::is_seof() const
370 #ifdef TITAN_RUNTIME_2
373 (optional_selection==OPTIONAL_PRESENT)
375 ? optional_value->is_seof() : T_type().is_seof();
378 template<typename T_type>
379 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
382 #ifdef TITAN_RUNTIME_2
385 (optional_selection==OPTIONAL_PRESENT)
387 ? optional_value->get_descriptor() : T_type().get_descriptor();
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
399 if (other_value != OMIT_VALUE)
400 TTCN_error("Setting an optional field to an invalid value.");
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
412 switch (other_value.optional_selection) {
413 case OPTIONAL_PRESENT:
414 optional_value = new T_type(*other_value.optional_value);
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
430 switch (other_value.get_selection()) {
431 case OPTIONAL_PRESENT:
432 optional_value = new T_type((const T_tmp&)other_value);
441 template<typename T_type>
442 void OPTIONAL<T_type>::clean_up()
444 #ifdef TITAN_RUNTIME_2
446 if (param_refs > 0) {
447 optional_value->clean_up();
450 delete optional_value;
451 optional_value = NULL;
455 if (OPTIONAL_PRESENT == optional_selection) {
456 delete optional_value;
459 optional_selection = OPTIONAL_UNBOUND;
462 template<typename T_type>
463 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
465 if (other_value != OMIT_VALUE)
466 TTCN_error("Internal error: Setting an optional field to an invalid value.");
471 template<typename T_type>
472 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
474 switch (other_value.optional_selection) {
475 case OPTIONAL_PRESENT:
476 #ifdef TITAN_RUNTIME_2
477 if (NULL == optional_value) {
479 if (optional_selection != OPTIONAL_PRESENT) {
481 optional_value = new T_type(*other_value.optional_value);
482 optional_selection = OPTIONAL_PRESENT;
483 } else *optional_value = *other_value.optional_value;
486 if (&other_value != this) set_to_omit();
495 template<typename T_type> template <typename T_tmp>
497 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
499 switch (other_value.get_selection()) {
500 case OPTIONAL_PRESENT:
501 #ifdef TITAN_RUNTIME_2
502 if (NULL == optional_value) {
504 if (optional_selection != OPTIONAL_PRESENT) {
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;
520 template<typename T_type> template <typename T_tmp>
522 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
524 #ifdef TITAN_RUNTIME_2
525 if (NULL == optional_value) {
527 if (optional_selection != OPTIONAL_PRESENT) {
529 optional_value = new T_type(other_value);
530 optional_selection = OPTIONAL_PRESENT;
531 } else *optional_value = other_value;
535 template<typename T_type>
536 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
538 #ifdef TITAN_RUNTIME_2
541 if (optional_selection == OPTIONAL_UNBOUND) {
543 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
544 TTCN_error("The left operand of comparison is an unbound optional value.");
546 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
547 "of comparison is an invalid value.");
549 #ifdef TITAN_RUNTIME_2
552 optional_selection == OPTIONAL_OMIT;
556 template<typename T_type>
557 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
559 #ifdef TITAN_RUNTIME_2
561 if (!other_value.is_bound())
563 if (optional_selection == OPTIONAL_UNBOUND) {
564 if (other_value.optional_selection == OPTIONAL_UNBOUND)
567 TTCN_error("The left operand of "
568 "comparison is an unbound optional value.");
570 #ifdef TITAN_RUNTIME_2
571 if (!other_value.is_bound())
573 if (other_value.optional_selection == OPTIONAL_UNBOUND)
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;
581 if (optional_selection != other_value.optional_selection) return FALSE;
582 else if (optional_selection == OPTIONAL_PRESENT)
584 return *optional_value == *other_value.optional_value;
588 template<typename T_type> template <typename T_tmp>
589 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
591 #ifdef TITAN_RUNTIME_2
592 switch (get_selection()) {
594 switch (optional_selection) {
596 case OPTIONAL_PRESENT:
597 return *optional_value == other_value;
601 TTCN_error("The left operand of comparison is an unbound optional value.");
606 template<typename T_type> template <typename T_tmp>
607 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
609 #ifdef TITAN_RUNTIME_2
611 if (!other_value.is_bound())
613 optional_sel other_selection = other_value.get_selection();
614 if (optional_selection == OPTIONAL_UNBOUND) {
615 if (other_selection == OPTIONAL_UNBOUND)
618 TTCN_error("The left operand of "
619 "comparison is an unbound optional value.");
621 #ifdef TITAN_RUNTIME_2
622 if (!other_value.is_bound())
624 if (other_selection == OPTIONAL_UNBOUND)
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;
632 if (optional_selection != other_selection) return FALSE;
633 else if (optional_selection == OPTIONAL_PRESENT)
635 return *optional_value == (const T_tmp&)other_value;
639 #ifdef TITAN_RUNTIME_2
640 template<typename T_type>
641 boolean OPTIONAL<T_type>::is_bound() const
643 switch (optional_selection) {
644 case OPTIONAL_PRESENT:
648 if (NULL != optional_value) {
649 return optional_value->is_bound();
655 template<typename T_type>
656 boolean OPTIONAL<T_type>::is_present() const
658 switch (optional_selection) {
659 case OPTIONAL_PRESENT:
663 if (NULL != optional_value) {
664 return optional_value->is_bound();
671 template<typename T_type>
672 boolean OPTIONAL<T_type>::ispresent() const
674 switch (optional_selection) {
675 case OPTIONAL_PRESENT:
678 #ifdef TITAN_RUNTIME_2
679 if (NULL != optional_value) {
680 return optional_value->is_bound();
685 #ifdef TITAN_RUNTIME_2
686 if (NULL != optional_value && optional_value->is_bound()) {
690 TTCN_error("Using an unbound optional field.");
695 #ifdef TITAN_RUNTIME_2
696 template<typename T_type>
697 optional_sel OPTIONAL<T_type>::get_selection() const
700 return OPTIONAL_PRESENT;
703 // not present, but bound => omit
704 return OPTIONAL_OMIT;
706 return OPTIONAL_UNBOUND;
710 template<typename T_type>
711 void OPTIONAL<T_type>::log() const
713 #ifdef TITAN_RUNTIME_2
714 switch (get_selection()) {
716 switch (optional_selection) {
718 case OPTIONAL_PRESENT:
719 optional_value->log();
722 TTCN_Logger::log_event_str("omit");
725 TTCN_Logger::log_event_unbound();
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");
739 optional_value->set_param(param);
742 template<typename T_type>
743 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
745 #ifdef TITAN_RUNTIME_2
746 switch (get_selection()) {
748 switch (optional_selection) {
751 text_buf.push_int((RInt)FALSE);
753 case OPTIONAL_PRESENT:
754 text_buf.push_int((RInt)TRUE);
755 optional_value->encode_text(text_buf);
758 TTCN_error("Text encoder: Encoding an unbound optional value.");
762 template<typename T_type>
763 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
765 if (text_buf.pull_int().get_val()) {
767 optional_value->decode_text(text_buf);
768 } else set_to_omit();
771 template<typename T_type>
772 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
774 #ifdef TITAN_RUNTIME_2
775 switch(get_selection()) {
777 switch(optional_selection) {
779 case OPTIONAL_PRESENT:
780 return optional_value->JSON_encode(p_td, p_tok);
782 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
784 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
785 "Encoding an unbound optional value.");
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)
793 // try the optional value first
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) {
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) {
815 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
816 dec_len = JSON_ERROR_INVALID_TOKEN;
822 #ifdef TITAN_RUNTIME_2
823 template<typename T_type>
824 void OPTIONAL<T_type>::add_refd_index(int index)
828 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
829 if (0 != refd_opt_val) {
830 refd_opt_val->add_refd_index(index);
834 template<typename T_type>
835 void OPTIONAL<T_type>::remove_refd_index(int index)
838 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
839 if (0 != refd_opt_val) {
840 refd_opt_val->remove_refd_index(index);
845 template<typename T_type>
846 OPTIONAL<T_type>::operator T_type&()
849 return *optional_value;
852 template<typename T_type>
853 OPTIONAL<T_type>::operator const T_type&() const
855 #ifdef TITAN_RUNTIME_2
858 if (optional_selection != OPTIONAL_PRESENT)
860 TTCN_error("Using the value of an optional field containing omit.");
861 return *optional_value;
864 template<typename T_type>
866 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
867 unsigned p_coding) const
870 #ifdef TITAN_RUNTIME_2
871 switch (get_selection()) {
873 switch (optional_selection) {
875 case OPTIONAL_PRESENT:
876 return optional_value->BER_encode_TLV(p_td, p_coding);
878 return ASN_BER_TLV_t::construct();
880 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
884 #ifdef TITAN_RUNTIME_2
885 template<typename T_type>
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
891 #ifdef TITAN_RUNTIME_2
892 switch (get_selection()) {
894 switch (optional_selection) {
896 case OPTIONAL_PRESENT:
897 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
899 return ASN_BER_TLV_t::construct();
901 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
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 */ )
910 TTCN_error("RAW decoding requested for optional type '%s'"
911 " which has no RAW decoding method.",p_td.name);
916 template<typename T_type>
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
920 #ifdef TITAN_RUNTIME_2
921 switch (get_selection()) {
923 switch (optional_selection) {
925 case OPTIONAL_PRESENT:
926 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
928 return 0; // nothing to do !
930 TTCN_EncDec_ErrorContext::error(
931 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
936 #ifdef TITAN_RUNTIME_2
937 template<typename T_type>
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
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);
947 return 0; // nothing to do !
949 TTCN_EncDec_ErrorContext::error(
950 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
957 template<typename T_type>
959 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
961 // If the record has no elements, then it can't have an AnyElement
962 if (parent_tag_closed) {
967 while (reader.Ok()) {
968 // Leaving the record before finding an element -> no AnyElement
969 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
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)) {
988 template<typename T_type>
990 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
991 unsigned int flavor, embed_values_dec_struct_t* emb_val)
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
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;
1005 name = (const char*)reader.LocalName();
1006 if (!check_name(name, p_td, exer)) { // it's not us, bail
1009 // we already checked for exer==1
1010 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1012 // set to omit if the attribute is empty
1013 const char * value = (const char *)reader.Value();
1014 if (strlen(value) == 0) {
1019 optional_value->XER_decode(p_td, reader, flavor, emb_val);
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
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);
1038 else break; // it's not us, bail
1042 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1045 else if (XML_READER_TYPE_END_ELEMENT == type) {
1048 // else circle around
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()) {
1062 switch (optional_selection) {
1064 case OPTIONAL_PRESENT:
1065 return optional_value->collect_ns(p_td, num, def_ns);
1071 TTCN_EncDec_ErrorContext::error(
1072 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
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,
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);
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)
1096 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1099 template<typename T_type>
1100 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1103 #ifdef TITAN_RUNTIME_2
1105 optional_selection = OPTIONAL_PRESENT;
1107 if (optional_selection==OPTIONAL_PRESENT) {
1109 optional_value->BER_decode_opentypes(p_typelist, L_form);
1113 #ifdef TITAN_RUNTIME_2
1115 template<typename T_type>
1116 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1117 TTCN_Buffer& buff) const
1120 return optional_value->TEXT_encode(p_td, buff);
1121 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
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
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.");
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)
1140 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
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); }
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); }
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); }
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); }
1173 #pragma GCC diagnostic pop