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 /** 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;
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;
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. */
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
53 #ifdef TITAN_RUNTIME_2
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)
65 optional_value = new T_type;
69 /** Set the optional value to omit.
70 * If the value was present, frees it.
71 * @post optional_selection is OPTIONAL_OMIT
73 #ifdef TITAN_RUNTIME_2
80 #ifdef TITAN_RUNTIME_2
83 optional_value->clean_up();
86 delete optional_value;
87 optional_value = NULL;
91 if (optional_selection == OPTIONAL_PRESENT) {
92 delete optional_value;
95 optional_selection = OPTIONAL_OMIT;
99 /// Default constructor creates an unbound object
100 OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
101 #ifdef TITAN_RUNTIME_2
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);
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);
115 /// Construct from an optional of different type
116 template <typename T_tmp>
117 OPTIONAL(const OPTIONAL<T_tmp>& other_value);
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
130 #ifdef TITAN_RUNTIME_2
131 if (NULL != optional_value)
133 if (optional_selection == OPTIONAL_PRESENT)
135 delete optional_value;
141 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
142 OPTIONAL& operator=(template_sel other_value);
145 OPTIONAL& operator=(const OPTIONAL& other_value);
147 /// Assign from an optional of another type
148 template <typename T_tmp>
149 OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);
152 template <typename T_tmp>
153 OPTIONAL& operator=(const T_tmp& other_value);
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;
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); }
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); }
188 #ifdef TITAN_RUNTIME_2
189 boolean is_bound() const;
191 inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
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;
200 inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
203 #ifdef TITAN_RUNTIME_2
204 /** @name override virtual functions of Base_Type
207 /** Return \c true (this \b is an optional field) */
208 boolean is_optional() const { return TRUE; }
210 /** Access the value for read/write
212 * @return a pointer to the (modifiable) value
213 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
215 Base_Type* get_opt_value();
217 /** Access the value (read/only)
219 * @return a pointer to the (const) value
220 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
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;
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
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)
240 boolean ispresent() const;
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;
248 inline optional_sel get_selection() const { return optional_selection; }
252 void set_param(Module_Param& param);
253 void encode_text(Text_Buf& text_buf) const;
254 void decode_text(Text_Buf& text_buf);
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);
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;
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.
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);
281 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
284 operator const T_type&() const;
286 inline T_type& operator()() { return (T_type&)*this; }
287 inline const T_type& operator()() const { return (const T_type&)*this; }
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;
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);
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);
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;
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);
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);
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);
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/
333 * Redirects the call to the optional value. */
338 #pragma GCC diagnostic push
339 #pragma GCC diagnostic ignored "-Wswitch-enum"
342 #ifdef TITAN_RUNTIME_2
344 template<typename T_type>
345 Base_Type* OPTIONAL<T_type>::get_opt_value()
347 #ifdef TITAN_RUNTIME_2
350 if (optional_selection!=OPTIONAL_PRESENT)
352 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
353 return optional_value;
356 template<typename T_type>
357 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
359 #ifdef TITAN_RUNTIME_2
362 if (optional_selection!=OPTIONAL_PRESENT)
364 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
365 return optional_value;
368 template<typename T_type>
369 boolean OPTIONAL<T_type>::is_seof() const
372 #ifdef TITAN_RUNTIME_2
375 (optional_selection==OPTIONAL_PRESENT)
377 ? optional_value->is_seof() : T_type().is_seof();
380 template<typename T_type>
381 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
384 #ifdef TITAN_RUNTIME_2
387 (optional_selection==OPTIONAL_PRESENT)
389 ? optional_value->get_descriptor() : T_type().get_descriptor();
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
401 if (other_value != OMIT_VALUE)
402 TTCN_error("Setting an optional field to an invalid value.");
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
414 switch (other_value.optional_selection) {
415 case OPTIONAL_PRESENT:
416 optional_value = new T_type(*other_value.optional_value);
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
432 switch (other_value.get_selection()) {
433 case OPTIONAL_PRESENT:
434 optional_value = new T_type((const T_tmp&)other_value);
443 template<typename T_type>
444 void OPTIONAL<T_type>::clean_up()
446 #ifdef TITAN_RUNTIME_2
448 if (param_refs > 0) {
449 optional_value->clean_up();
452 delete optional_value;
453 optional_value = NULL;
457 if (OPTIONAL_PRESENT == optional_selection) {
458 delete optional_value;
461 optional_selection = OPTIONAL_UNBOUND;
464 template<typename T_type>
465 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
467 if (other_value != OMIT_VALUE)
468 TTCN_error("Internal error: Setting an optional field to an invalid value.");
473 template<typename T_type>
474 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
476 switch (other_value.optional_selection) {
477 case OPTIONAL_PRESENT:
478 #ifdef TITAN_RUNTIME_2
479 if (NULL == optional_value) {
481 if (optional_selection != OPTIONAL_PRESENT) {
483 optional_value = new T_type(*other_value.optional_value);
484 optional_selection = OPTIONAL_PRESENT;
485 } else *optional_value = *other_value.optional_value;
488 if (&other_value != this) set_to_omit();
497 template<typename T_type> template <typename T_tmp>
499 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
501 switch (other_value.get_selection()) {
502 case OPTIONAL_PRESENT:
503 #ifdef TITAN_RUNTIME_2
504 if (NULL == optional_value) {
506 if (optional_selection != OPTIONAL_PRESENT) {
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;
522 template<typename T_type> template <typename T_tmp>
524 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
526 #ifdef TITAN_RUNTIME_2
527 if (NULL == optional_value) {
529 if (optional_selection != OPTIONAL_PRESENT) {
531 optional_value = new T_type(other_value);
532 optional_selection = OPTIONAL_PRESENT;
533 } else *optional_value = other_value;
537 template<typename T_type>
538 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
540 #ifdef TITAN_RUNTIME_2
543 if (optional_selection == OPTIONAL_UNBOUND) {
545 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
546 TTCN_error("The left operand of comparison is an unbound optional value.");
548 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
549 "of comparison is an invalid value.");
551 #ifdef TITAN_RUNTIME_2
554 optional_selection == OPTIONAL_OMIT;
558 template<typename T_type>
559 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
561 #ifdef TITAN_RUNTIME_2
563 if (!other_value.is_bound())
565 if (optional_selection == OPTIONAL_UNBOUND) {
566 if (other_value.optional_selection == OPTIONAL_UNBOUND)
569 TTCN_error("The left operand of "
570 "comparison is an unbound optional value.");
572 #ifdef TITAN_RUNTIME_2
573 if (!other_value.is_bound())
575 if (other_value.optional_selection == OPTIONAL_UNBOUND)
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;
583 if (optional_selection != other_value.optional_selection) return FALSE;
584 else if (optional_selection == OPTIONAL_PRESENT)
586 return *optional_value == *other_value.optional_value;
590 template<typename T_type> template <typename T_tmp>
591 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
593 #ifdef TITAN_RUNTIME_2
594 switch (get_selection()) {
596 switch (optional_selection) {
598 case OPTIONAL_PRESENT:
599 return *optional_value == other_value;
603 TTCN_error("The left operand of comparison is an unbound optional value.");
608 template<typename T_type> template <typename T_tmp>
609 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
611 #ifdef TITAN_RUNTIME_2
613 if (!other_value.is_bound())
615 optional_sel other_selection = other_value.get_selection();
616 if (optional_selection == OPTIONAL_UNBOUND) {
617 if (other_selection == OPTIONAL_UNBOUND)
620 TTCN_error("The left operand of "
621 "comparison is an unbound optional value.");
623 #ifdef TITAN_RUNTIME_2
624 if (!other_value.is_bound())
626 if (other_selection == OPTIONAL_UNBOUND)
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;
634 if (optional_selection != other_selection) return FALSE;
635 else if (optional_selection == OPTIONAL_PRESENT)
637 return *optional_value == (const T_tmp&)other_value;
641 #ifdef TITAN_RUNTIME_2
642 template<typename T_type>
643 boolean OPTIONAL<T_type>::is_bound() const
645 switch (optional_selection) {
646 case OPTIONAL_PRESENT:
650 if (NULL != optional_value) {
651 return optional_value->is_bound();
657 template<typename T_type>
658 boolean OPTIONAL<T_type>::is_present() const
660 switch (optional_selection) {
661 case OPTIONAL_PRESENT:
665 if (NULL != optional_value) {
666 return optional_value->is_bound();
673 template<typename T_type>
674 boolean OPTIONAL<T_type>::ispresent() const
676 switch (optional_selection) {
677 case OPTIONAL_PRESENT:
680 #ifdef TITAN_RUNTIME_2
681 if (NULL != optional_value) {
682 return optional_value->is_bound();
687 #ifdef TITAN_RUNTIME_2
688 if (NULL != optional_value && optional_value->is_bound()) {
692 TTCN_error("Using an unbound optional field.");
697 #ifdef TITAN_RUNTIME_2
698 template<typename T_type>
699 optional_sel OPTIONAL<T_type>::get_selection() const
702 return OPTIONAL_PRESENT;
705 // not present, but bound => omit
706 return OPTIONAL_OMIT;
708 return OPTIONAL_UNBOUND;
712 template<typename T_type>
713 void OPTIONAL<T_type>::log() const
715 #ifdef TITAN_RUNTIME_2
716 switch (get_selection()) {
718 switch (optional_selection) {
720 case OPTIONAL_PRESENT:
721 optional_value->log();
724 TTCN_Logger::log_event_str("omit");
727 TTCN_Logger::log_event_unbound();
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");
741 optional_value->set_param(param);
744 template<typename T_type>
745 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
747 #ifdef TITAN_RUNTIME_2
748 switch (get_selection()) {
750 switch (optional_selection) {
753 text_buf.push_int((RInt)FALSE);
755 case OPTIONAL_PRESENT:
756 text_buf.push_int((RInt)TRUE);
757 optional_value->encode_text(text_buf);
760 TTCN_error("Text encoder: Encoding an unbound optional value.");
764 template<typename T_type>
765 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
767 if (text_buf.pull_int().get_val()) {
769 optional_value->decode_text(text_buf);
770 } else set_to_omit();
773 template<typename T_type>
774 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
776 #ifdef TITAN_RUNTIME_2
777 switch(get_selection()) {
779 switch(optional_selection) {
781 case OPTIONAL_PRESENT:
782 return optional_value->JSON_encode(p_td, p_tok);
784 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
786 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
787 "Encoding an unbound optional value.");
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)
795 // try the optional value first
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) {
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) {
817 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
818 dec_len = JSON_ERROR_INVALID_TOKEN;
824 #ifdef TITAN_RUNTIME_2
825 template<typename T_type>
826 void OPTIONAL<T_type>::add_refd_index(int index)
830 optional_value->add_refd_index(index);
833 template<typename T_type>
834 void OPTIONAL<T_type>::remove_refd_index(int index)
837 optional_value->remove_refd_index(index);
841 template<typename T_type>
842 OPTIONAL<T_type>::operator T_type&()
845 return *optional_value;
848 template<typename T_type>
849 OPTIONAL<T_type>::operator const T_type&() const
851 #ifdef TITAN_RUNTIME_2
854 if (optional_selection != OPTIONAL_PRESENT)
856 TTCN_error("Using the value of an optional field containing omit.");
857 return *optional_value;
860 template<typename T_type>
862 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
863 unsigned p_coding) const
866 #ifdef TITAN_RUNTIME_2
867 switch (get_selection()) {
869 switch (optional_selection) {
871 case OPTIONAL_PRESENT:
872 return optional_value->BER_encode_TLV(p_td, p_coding);
874 return ASN_BER_TLV_t::construct();
876 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
880 #ifdef TITAN_RUNTIME_2
881 template<typename T_type>
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
887 #ifdef TITAN_RUNTIME_2
888 switch (get_selection()) {
890 switch (optional_selection) {
892 case OPTIONAL_PRESENT:
893 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
895 return ASN_BER_TLV_t::construct();
897 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
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 */ )
906 TTCN_error("RAW decoding requested for optional type '%s'"
907 " which has no RAW decoding method.",p_td.name);
912 template<typename T_type>
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
916 #ifdef TITAN_RUNTIME_2
917 switch (get_selection()) {
919 switch (optional_selection) {
921 case OPTIONAL_PRESENT:
922 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
924 return 0; // nothing to do !
926 TTCN_EncDec_ErrorContext::error(
927 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
932 #ifdef TITAN_RUNTIME_2
933 template<typename T_type>
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
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);
943 return 0; // nothing to do !
945 TTCN_EncDec_ErrorContext::error(
946 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
953 template<typename T_type>
955 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
957 // If the record has no elements, then it can't have an AnyElement
958 if (parent_tag_closed) {
963 while (reader.Ok()) {
964 // Leaving the record before finding an element -> no AnyElement
965 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
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)) {
984 template<typename T_type>
986 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
987 unsigned int flavor, embed_values_dec_struct_t* emb_val)
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
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;
1001 name = (const char*)reader.LocalName();
1002 if (!check_name(name, p_td, exer)) { // it's not us, bail
1005 // we already checked for exer==1
1006 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1009 optional_value->XER_decode(p_td, reader, flavor, emb_val);
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
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);
1028 else break; // it's not us, bail
1032 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1035 else if (XML_READER_TYPE_END_ELEMENT == type) {
1038 // else circle around
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()) {
1052 switch (optional_selection) {
1054 case OPTIONAL_PRESENT:
1055 return optional_value->collect_ns(p_td, num, def_ns);
1061 TTCN_EncDec_ErrorContext::error(
1062 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
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,
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);
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)
1086 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1089 template<typename T_type>
1090 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1093 #ifdef TITAN_RUNTIME_2
1095 optional_selection = OPTIONAL_PRESENT;
1097 if (optional_selection==OPTIONAL_PRESENT) {
1099 optional_value->BER_decode_opentypes(p_typelist, L_form);
1103 #ifdef TITAN_RUNTIME_2
1105 template<typename T_type>
1106 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1107 TTCN_Buffer& buff) const
1110 return optional_value->TEXT_encode(p_td, buff);
1111 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
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
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.");
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)
1130 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
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); }
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); }
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); }
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); }
1163 #pragma GCC diagnostic pop