1 /******************************************************************************
2 * Copyright (c) 2000-2016 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
21 * Szabo, Janos Zoltan – initial implementation
24 ******************************************************************************/
29 #include "Param_Types.hh"
30 #include "Basetype.hh"
31 #include "Template.hh"
37 #include "Parameters.h"
40 #include "XmlReader.hh"
42 enum optional_sel { OPTIONAL_UNBOUND, OPTIONAL_OMIT, OPTIONAL_PRESENT };
44 template <typename T_type>
45 class OPTIONAL : public Base_Type
46 #ifdef TITAN_RUNTIME_2
47 , public RefdIndexInterface
50 /** The value, if present (owned by OPTIONAL)
51 * In Runtime2 the pointer is null, when the value is not present.
52 * In Runtime1 its presence is indicated by the optional_selection member. */
53 T_type *optional_value;
55 /** Specifies the state of the optional field
56 * @tricky In Runtime2 the optional value can be modified through parameter references,
57 * in which case this member variable will not be updated. Always use the function
58 * get_selection() instead of directly referencing this variable. */
59 optional_sel optional_selection;
61 #ifdef TITAN_RUNTIME_2
62 /** Stores the number of elements referenced by 'out' and 'inout' parameters,
63 * if the optional field is a record of/set of/array (only in Runtime2).
64 * If at least one element is referenced, the value must not be deleted. */
68 /** Set the optional value to present.
69 * If the value was already present, does nothing.
70 * Else allocates a new (uninitialized) T-type.
71 * @post \c optional_selection is OPTIONAL_PRESENT
72 * @post \c optional_value is not NULL
74 #ifdef TITAN_RUNTIME_2
80 void set_to_present() {
81 if (optional_selection != OPTIONAL_PRESENT) {
82 optional_selection = OPTIONAL_PRESENT;
83 #ifdef TITAN_RUNTIME_2
84 if (optional_value == NULL)
86 optional_value = new T_type;
90 /** Set the optional value to omit.
91 * If the value was present, frees it.
92 * @post optional_selection is OPTIONAL_OMIT
94 #ifdef TITAN_RUNTIME_2
101 #ifdef TITAN_RUNTIME_2
103 if (param_refs > 0) {
104 optional_value->clean_up();
107 delete optional_value;
108 optional_value = NULL;
112 if (optional_selection == OPTIONAL_PRESENT) {
113 delete optional_value;
116 optional_selection = OPTIONAL_OMIT;
120 /// Default constructor creates an unbound object
121 OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
122 #ifdef TITAN_RUNTIME_2
127 /// Construct an optional object set to omit.
128 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
129 OPTIONAL(template_sel other_value);
131 /// Copy constructor.
132 /// @note Copying an unbound object creates another unbound object,
133 /// without causing a dynamic test case immediately.
134 OPTIONAL(const OPTIONAL& other_value);
136 /// Construct from an optional of different type
137 template <typename T_tmp>
138 OPTIONAL(const OPTIONAL<T_tmp>& other_value);
140 /// Construct from an object of different type
141 template <typename T_tmp>
142 OPTIONAL(const T_tmp& other_value)
143 : optional_value(new T_type(other_value))
144 , optional_selection(OPTIONAL_PRESENT)
145 #ifdef TITAN_RUNTIME_2
151 #ifdef TITAN_RUNTIME_2
152 if (NULL != optional_value)
154 if (optional_selection == OPTIONAL_PRESENT)
156 delete optional_value;
162 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
163 OPTIONAL& operator=(template_sel other_value);
166 OPTIONAL& operator=(const OPTIONAL& other_value);
168 /// Assign from an optional of another type
169 template <typename T_tmp>
170 OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);
173 template <typename T_tmp>
174 OPTIONAL& operator=(const T_tmp& other_value);
176 boolean is_equal(template_sel other_value) const;
177 boolean is_equal(const OPTIONAL& other_value) const;
178 template <typename T_tmp>
179 boolean is_equal(const OPTIONAL<T_tmp>& other_value) const;
180 template <typename T_tmp>
181 boolean is_equal(const T_tmp& other_value) const;
183 inline boolean operator==(template_sel other_value) const
184 { return is_equal(other_value); }
185 inline boolean operator!=(template_sel other_value) const
186 { return !is_equal(other_value); }
187 inline boolean operator==(const OPTIONAL& other_value) const
188 { return is_equal(other_value); }
189 inline boolean operator!=(const OPTIONAL& other_value) const
190 { return !is_equal(other_value); }
191 template <typename T_tmp>
192 inline boolean operator==(const T_tmp& other_value) const
193 { return is_equal(other_value); }
194 template <typename T_tmp>
195 inline boolean operator!=(const T_tmp& other_value) const
196 { return !is_equal(other_value); }
197 #if defined(__SUNPRO_CC) || defined(__clang__)
198 /* Note: Without these functions the Sun Workshop Pro C++ compiler or clang reports
199 * overloading ambiguity when comparing an optional charstring field with an
200 * optional universal charstring. */
201 template <typename T_tmp>
202 inline boolean operator==(const OPTIONAL<T_tmp>& other_value) const
203 { return is_equal(other_value); }
204 template <typename T_tmp>
205 inline boolean operator!=(const OPTIONAL<T_tmp>& other_value) const
206 { return is_equal(other_value); }
209 #ifdef TITAN_RUNTIME_2
210 boolean is_bound() const;
212 inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
214 boolean is_value() const
215 { return optional_selection == OPTIONAL_PRESENT && optional_value->is_value(); }
216 /** Whether the optional value is present.
217 * @return \c true if optional_selection is OPTIONAL_PRESENT, else \c false */
218 #ifdef TITAN_RUNTIME_2
219 boolean is_present() const;
221 inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
224 #ifdef TITAN_RUNTIME_2
225 /** @name override virtual functions of Base_Type
228 /** Return \c true (this \b is an optional field) */
229 boolean is_optional() const { return TRUE; }
231 /** Access the value for read/write
233 * @return a pointer to the (modifiable) value
234 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
236 Base_Type* get_opt_value();
238 /** Access the value (read/only)
240 * @return a pointer to the (const) value
241 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
243 const Base_Type* get_opt_value() const;
244 boolean is_seof() const;
245 boolean is_equal(const Base_Type* other_value) const { return is_equal(*(static_cast<const OPTIONAL*>(other_value))); }
246 void set_value(const Base_Type* other_value) { *this = *(static_cast<const OPTIONAL*>(other_value)); }
247 Base_Type* clone() const { return new OPTIONAL(*this); }
248 const TTCN_Typedescriptor_t* get_descriptor() const;
252 /** Whether the value is present.
253 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility
254 * with the runtime and existing testports which use this version where
255 * unbound errors are caught before causing more trouble
257 * @return TRUE if the value is present (optional_selection==OPTIONAL_PRESENT)
258 * @return FALSE if the value is not present (optional_selection==OPTIONAL_OMIT)
259 * @pre the value is bound (optional_selection!=OPTIONAL_UNBOUND)
261 boolean ispresent() const;
263 #ifdef TITAN_RUNTIME_2
264 /** @tricky Calculates and returns the actual state of the optional object,
265 * not just the optional_selection member.
266 * (Only needed in Runtime2, in Runtime1 optional_selection is always up to date.) */
267 optional_sel get_selection() const;
269 inline optional_sel get_selection() const { return optional_selection; }
273 void set_param(Module_Param& param);
274 Module_Param* get_param(Module_Param_Name& param_name) const;
275 void encode_text(Text_Buf& text_buf) const;
276 void decode_text(Text_Buf& text_buf);
278 #ifdef TITAN_RUNTIME_2
279 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
280 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
283 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
284 int indent, embed_values_enc_struct_t* emb_val) const;
285 #ifdef TITAN_RUNTIME_2
286 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
287 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
288 int indent, embed_values_enc_struct_t* emb_val) const;
290 /** Used during XML decoding, in case this object is an AnyElement field in a record.
291 * Determines whether XER_decode() should be called or this field should be omitted.
292 * The field should be omitted if:
293 * - the next element in the encoded XML is the next field in the record or
294 * - there are no more elements until the end of the record's XML element.
296 * @param reader parses the encoded XML
297 * @param next_field_name name of the next field in the record, or null if this is the last one
298 * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
299 bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
300 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
301 unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val);
303 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
306 operator const T_type&() const;
308 inline T_type& operator()() { return (T_type&)*this; }
309 inline const T_type& operator()() const { return (const T_type&)*this; }
311 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
312 unsigned p_coding) const;
313 #ifdef TITAN_RUNTIME_2
314 ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
315 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
317 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
318 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
319 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
320 const ASN_BER_TLV_t& p_tlv);
321 void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
323 #ifdef TITAN_RUNTIME_2
324 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
325 int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
326 const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
327 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
328 boolean no_err=FALSE, boolean first_call=TRUE);
329 int JSON_encode_negtest(const Erroneous_descriptor_t*,
330 const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
333 /** Encodes accordingly to the JSON encoding rules.
334 * Returns the length of the encoded data. */
335 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
337 /** Decodes accordingly to the JSON encoding rules.
338 * Returns the length of the decoded data. */
339 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
341 #ifdef TITAN_RUNTIME_2
342 /** Called before an element of an optional record of/set of is indexed and passed as an
343 * 'inout' or 'out' parameter to a function (only in Runtime2).
344 * Sets the optional value to present (this would be done by the indexing operation
345 * anyway) and redirects the call to the optional value. */
346 virtual void add_refd_index(int index);
348 /** Called after an element of an optional record of/set of is passed as an
349 * 'inout' or 'out' parameter to a function (only in Runtime2).
350 * Redirects the call to the optional value. */
351 virtual void remove_refd_index(int index);
356 #pragma GCC diagnostic push
357 #pragma GCC diagnostic ignored "-Wswitch-enum"
360 #ifdef TITAN_RUNTIME_2
362 template<typename T_type>
363 Base_Type* OPTIONAL<T_type>::get_opt_value()
365 #ifdef TITAN_RUNTIME_2
368 if (optional_selection!=OPTIONAL_PRESENT)
370 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
371 return optional_value;
374 template<typename T_type>
375 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
377 #ifdef TITAN_RUNTIME_2
380 if (optional_selection!=OPTIONAL_PRESENT)
382 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
383 return optional_value;
386 template<typename T_type>
387 boolean OPTIONAL<T_type>::is_seof() const
390 #ifdef TITAN_RUNTIME_2
393 (optional_selection==OPTIONAL_PRESENT)
395 ? optional_value->is_seof() : T_type().is_seof();
398 template<typename T_type>
399 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
402 #ifdef TITAN_RUNTIME_2
405 (optional_selection==OPTIONAL_PRESENT)
407 ? optional_value->get_descriptor() : T_type().get_descriptor();
412 template<typename T_type>
413 OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
414 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
415 #ifdef TITAN_RUNTIME_2
419 if (other_value != OMIT_VALUE)
420 TTCN_error("Setting an optional field to an invalid value.");
423 template<typename T_type>
424 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
425 : Base_Type(other_value)
426 #ifdef TITAN_RUNTIME_2
427 , RefdIndexInterface(other_value)
429 , optional_value(NULL)
430 , optional_selection(other_value.optional_selection)
431 #ifdef TITAN_RUNTIME_2
435 switch (other_value.optional_selection) {
436 case OPTIONAL_PRESENT:
437 optional_value = new T_type(*other_value.optional_value);
446 template<typename T_type> template<typename T_tmp>
447 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
448 : optional_value(NULL), optional_selection(other_value.get_selection())
449 #ifdef TITAN_RUNTIME_2
453 switch (other_value.get_selection()) {
454 case OPTIONAL_PRESENT:
455 optional_value = new T_type((const T_tmp&)other_value);
464 template<typename T_type>
465 void OPTIONAL<T_type>::clean_up()
467 #ifdef TITAN_RUNTIME_2
469 if (param_refs > 0) {
470 optional_value->clean_up();
473 delete optional_value;
474 optional_value = NULL;
478 if (OPTIONAL_PRESENT == optional_selection) {
479 delete optional_value;
482 optional_selection = OPTIONAL_UNBOUND;
485 template<typename T_type>
486 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
488 if (other_value != OMIT_VALUE)
489 TTCN_error("Internal error: Setting an optional field to an invalid value.");
494 template<typename T_type>
495 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
497 switch (other_value.optional_selection) {
498 case OPTIONAL_PRESENT:
499 #ifdef TITAN_RUNTIME_2
500 if (NULL == optional_value) {
502 if (optional_selection != OPTIONAL_PRESENT) {
504 optional_value = new T_type(*other_value.optional_value);
505 optional_selection = OPTIONAL_PRESENT;
506 } else *optional_value = *other_value.optional_value;
509 if (&other_value != this) set_to_omit();
518 template<typename T_type> template <typename T_tmp>
520 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
522 switch (other_value.get_selection()) {
523 case OPTIONAL_PRESENT:
524 #ifdef TITAN_RUNTIME_2
525 if (NULL == optional_value) {
527 if (optional_selection != OPTIONAL_PRESENT) {
529 optional_value = new T_type((const T_tmp&)other_value);
530 optional_selection = OPTIONAL_PRESENT;
531 } else *optional_value = (const T_tmp&)other_value;
543 template<typename T_type> template <typename T_tmp>
545 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
547 #ifdef TITAN_RUNTIME_2
548 if (NULL == optional_value) {
550 if (optional_selection != OPTIONAL_PRESENT) {
552 optional_value = new T_type(other_value);
553 optional_selection = OPTIONAL_PRESENT;
554 } else *optional_value = other_value;
558 template<typename T_type>
559 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
561 #ifdef TITAN_RUNTIME_2
564 if (optional_selection == OPTIONAL_UNBOUND) {
566 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
567 TTCN_error("The left operand of comparison is an unbound optional value.");
569 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
570 "of comparison is an invalid value.");
572 #ifdef TITAN_RUNTIME_2
575 optional_selection == OPTIONAL_OMIT;
579 template<typename T_type>
580 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
582 #ifdef TITAN_RUNTIME_2
584 if (!other_value.is_bound())
586 if (optional_selection == OPTIONAL_UNBOUND) {
587 if (other_value.optional_selection == OPTIONAL_UNBOUND)
590 TTCN_error("The left operand of "
591 "comparison is an unbound optional value.");
593 #ifdef TITAN_RUNTIME_2
594 if (!other_value.is_bound())
596 if (other_value.optional_selection == OPTIONAL_UNBOUND)
598 TTCN_error("The right operand of comparison is an unbound optional value.");
599 #ifdef TITAN_RUNTIME_2
600 boolean present = is_present();
601 if (present != other_value.is_present()) return FALSE;
604 if (optional_selection != other_value.optional_selection) return FALSE;
605 else if (optional_selection == OPTIONAL_PRESENT)
607 return *optional_value == *other_value.optional_value;
611 template<typename T_type> template <typename T_tmp>
612 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
614 #ifdef TITAN_RUNTIME_2
615 switch (get_selection()) {
617 switch (optional_selection) {
619 case OPTIONAL_PRESENT:
620 return *optional_value == other_value;
624 TTCN_error("The left operand of comparison is an unbound optional value.");
629 template<typename T_type> template <typename T_tmp>
630 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
632 #ifdef TITAN_RUNTIME_2
634 if (!other_value.is_bound())
636 optional_sel other_selection = other_value.get_selection();
637 if (optional_selection == OPTIONAL_UNBOUND) {
638 if (other_selection == OPTIONAL_UNBOUND)
641 TTCN_error("The left operand of "
642 "comparison is an unbound optional value.");
644 #ifdef TITAN_RUNTIME_2
645 if (!other_value.is_bound())
647 if (other_selection == OPTIONAL_UNBOUND)
649 TTCN_error("The right operand of comparison is an unbound optional value.");
650 #ifdef TITAN_RUNTIME_2
651 boolean present = is_present();
652 if (present != other_value.is_present()) return FALSE;
655 if (optional_selection != other_selection) return FALSE;
656 else if (optional_selection == OPTIONAL_PRESENT)
658 return *optional_value == (const T_tmp&)other_value;
662 #ifdef TITAN_RUNTIME_2
663 template<typename T_type>
664 boolean OPTIONAL<T_type>::is_bound() const
666 switch (optional_selection) {
667 case OPTIONAL_PRESENT:
671 if (NULL != optional_value) {
672 return optional_value->is_bound();
678 template<typename T_type>
679 boolean OPTIONAL<T_type>::is_present() const
681 switch (optional_selection) {
682 case OPTIONAL_PRESENT:
686 if (NULL != optional_value) {
687 return optional_value->is_bound();
694 template<typename T_type>
695 boolean OPTIONAL<T_type>::ispresent() const
697 switch (optional_selection) {
698 case OPTIONAL_PRESENT:
701 #ifdef TITAN_RUNTIME_2
702 if (NULL != optional_value) {
703 return optional_value->is_bound();
708 #ifdef TITAN_RUNTIME_2
709 if (NULL != optional_value && optional_value->is_bound()) {
713 TTCN_error("Using an unbound optional field.");
718 #ifdef TITAN_RUNTIME_2
719 template<typename T_type>
720 optional_sel OPTIONAL<T_type>::get_selection() const
723 return OPTIONAL_PRESENT;
726 // not present, but bound => omit
727 return OPTIONAL_OMIT;
729 return OPTIONAL_UNBOUND;
733 template<typename T_type>
734 void OPTIONAL<T_type>::log() const
736 #ifdef TITAN_RUNTIME_2
737 switch (get_selection()) {
739 switch (optional_selection) {
741 case OPTIONAL_PRESENT:
742 optional_value->log();
745 TTCN_Logger::log_event_str("omit");
748 TTCN_Logger::log_event_unbound();
753 template <typename T_type>
754 void OPTIONAL<T_type>::set_param(Module_Param& param) {
755 if (param.get_type()==Module_Param::MP_Omit) {
756 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
757 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
762 optional_value->set_param(param);
763 if (!optional_value->is_bound()) {
768 template <typename T_type>
769 Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
771 #ifdef TITAN_RUNTIME_2
772 switch (get_selection()) {
774 switch (optional_selection) {
776 case OPTIONAL_PRESENT:
777 return optional_value->get_param(param_name);
779 return new Module_Param_Omit();
781 return new Module_Param_Unbound();
785 template<typename T_type>
786 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
788 #ifdef TITAN_RUNTIME_2
789 switch (get_selection()) {
791 switch (optional_selection) {
794 text_buf.push_int((RInt)FALSE);
796 case OPTIONAL_PRESENT:
797 text_buf.push_int((RInt)TRUE);
798 optional_value->encode_text(text_buf);
801 TTCN_error("Text encoder: Encoding an unbound optional value.");
805 template<typename T_type>
806 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
808 if (text_buf.pull_int().get_val()) {
810 optional_value->decode_text(text_buf);
811 } else set_to_omit();
814 template<typename T_type>
815 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
817 #ifdef TITAN_RUNTIME_2
818 switch(get_selection()) {
820 switch(optional_selection) {
822 case OPTIONAL_PRESENT:
823 return optional_value->JSON_encode(p_td, p_tok);
825 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
827 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
828 "Encoding an unbound optional value.");
833 #ifdef TITAN_RUNTIME_2
834 template<typename T_type>
835 int OPTIONAL<T_type>::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
836 const TTCN_Typedescriptor_t& p_td,
837 JSON_Tokenizer& p_tok) const
839 switch (get_selection()) {
840 case OPTIONAL_PRESENT:
841 return optional_value->JSON_encode_negtest(p_err_descr, p_td, p_tok);
843 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
845 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
846 "Encoding an unbound optional value.");
852 template<typename T_type>
853 int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
855 // try the optional value first
857 size_t buf_pos = p_tok.get_buf_pos();
858 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
859 if (JSON_ERROR_FATAL == dec_len) {
866 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
867 // invalid token, rewind the buffer and check if it's a "null" (= omit)
868 // this needs to be checked after the optional value, because it might also be
869 // able to decode a "null" value
870 p_tok.set_buf_pos(buf_pos);
871 json_token_t token = JSON_TOKEN_NONE;
872 dec_len = p_tok.get_next_token(&token, NULL, NULL);
873 if (JSON_TOKEN_LITERAL_NULL == token) {
877 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
878 dec_len = JSON_ERROR_INVALID_TOKEN;
884 #ifdef TITAN_RUNTIME_2
885 template<typename T_type>
886 void OPTIONAL<T_type>::add_refd_index(int index)
890 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
891 if (0 != refd_opt_val) {
892 refd_opt_val->add_refd_index(index);
896 template<typename T_type>
897 void OPTIONAL<T_type>::remove_refd_index(int index)
900 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
901 if (0 != refd_opt_val) {
902 refd_opt_val->remove_refd_index(index);
907 template<typename T_type>
908 OPTIONAL<T_type>::operator T_type&()
911 return *optional_value;
914 template<typename T_type>
915 OPTIONAL<T_type>::operator const T_type&() const
917 #ifdef TITAN_RUNTIME_2
920 if (optional_selection != OPTIONAL_PRESENT)
922 TTCN_error("Using the value of an optional field containing omit.");
923 return *optional_value;
926 template<typename T_type>
928 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
929 unsigned p_coding) const
932 #ifdef TITAN_RUNTIME_2
933 switch (get_selection()) {
935 switch (optional_selection) {
937 case OPTIONAL_PRESENT:
938 return optional_value->BER_encode_TLV(p_td, p_coding);
940 return ASN_BER_TLV_t::construct();
942 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
946 #ifdef TITAN_RUNTIME_2
947 template<typename T_type>
949 OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
950 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
953 #ifdef TITAN_RUNTIME_2
954 switch (get_selection()) {
956 switch (optional_selection) {
958 case OPTIONAL_PRESENT:
959 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
961 return ASN_BER_TLV_t::construct();
963 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
967 template<typename T_type>
968 int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
969 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
970 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
972 TTCN_error("RAW decoding requested for optional type '%s'"
973 " which has no RAW decoding method.",p_td.name);
978 template<typename T_type>
980 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
982 #ifdef TITAN_RUNTIME_2
983 switch (get_selection()) {
985 switch (optional_selection) {
987 case OPTIONAL_PRESENT:
988 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
990 return 0; // nothing to do !
992 TTCN_EncDec_ErrorContext::error(
993 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
998 #ifdef TITAN_RUNTIME_2
999 template<typename T_type>
1001 OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1002 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
1003 embed_values_enc_struct_t* emb_val) const
1005 switch (get_selection()) {
1006 case OPTIONAL_PRESENT:
1007 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
1009 return 0; // nothing to do !
1011 TTCN_EncDec_ErrorContext::error(
1012 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
1019 template<typename T_type>
1021 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
1023 // If the record has no elements, then it can't have an AnyElement
1024 if (parent_tag_closed) {
1029 while (reader.Ok()) {
1030 // Leaving the record before finding an element -> no AnyElement
1031 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
1035 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
1036 // The first element found is either the next field's element or the AnyElement
1037 if (NULL != next_field_name &&
1038 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
1050 template<typename T_type>
1052 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1053 unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val)
1055 int exer = is_exer(flavor);
1056 for (int success = reader.Ok(); success==1; success=reader.Read()) {
1057 int type = reader.NodeType();
1058 const char * name; // name of the optional field
1059 const char * ns_uri;
1061 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1062 if (XML_READER_TYPE_ATTRIBUTE == type) {
1063 for (; success==1; success = reader.MoveToNextAttribute()) {
1064 if (!reader.IsNamespaceDecl()) break;
1067 name = (const char*)reader.LocalName();
1068 if (!check_name(name, p_td, exer)) { // it's not us, bail
1071 // we already checked for exer==1
1072 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1074 // set to omit if the attribute is empty
1075 const char * value = (const char *)reader.Value();
1076 if (strlen(value) == 0) {
1081 optional_value->XER_decode(p_td, reader, flavor, flavor2, emb_val);
1086 else if(XML_READER_TYPE_ATTRIBUTE == type && (flavor & USE_NIL)){
1089 else { // not attribute
1090 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1091 name = (const char*)reader.LocalName();
1092 ns_uri = (const char*)reader.NamespaceUri();
1093 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1094 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1095 // If the optional field (a string) has anyElement, accept the element
1096 // regardless of its name. Else the name (and namespace) must match.
1097 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1100 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
1101 optional_value->XER_decode(p_td, reader, flavor, flavor2, emb_val);
1102 if (!optional_value->is_bound()) {
1106 else break; // it's not us, bail
1110 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1113 else if (XML_READER_TYPE_END_ELEMENT == type) {
1116 // else circle around
1125 template<typename T_type>
1126 char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
1127 #ifdef TITAN_RUNTIME_2
1128 switch (get_selection()) {
1130 switch (optional_selection) {
1132 case OPTIONAL_PRESENT:
1133 return optional_value->collect_ns(p_td, num, def_ns);
1139 TTCN_EncDec_ErrorContext::error(
1140 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1145 template<typename T_type>
1146 boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1147 const ASN_BER_TLV_t& p_tlv,
1150 BER_chk_descr(p_td);
1151 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1152 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1159 template<typename T_type>
1160 boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1161 const ASN_BER_TLV_t& p_tlv)
1164 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1167 template<typename T_type>
1168 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1171 #ifdef TITAN_RUNTIME_2
1173 optional_selection = OPTIONAL_PRESENT;
1175 if (optional_selection==OPTIONAL_PRESENT) {
1177 optional_value->BER_decode_opentypes(p_typelist, L_form);
1181 #ifdef TITAN_RUNTIME_2
1183 template<typename T_type>
1184 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1185 TTCN_Buffer& buff) const
1188 return optional_value->TEXT_encode(p_td, buff);
1189 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1193 template<typename T_type>
1194 int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1195 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1198 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1199 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1203 template<typename T_type>
1204 int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1205 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1208 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1213 #if defined(__GNUC__) && __GNUC__ >= 3
1214 /** Note: These functions allow most efficient operation by passing the left
1215 * operand OMIT_VALUE as value instead of constant reference.
1216 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1217 template<typename T_type>
1218 inline boolean operator==(template_sel left_value,
1219 const OPTIONAL<T_type>& right_value)
1220 { return right_value.is_equal(left_value); }
1222 template<typename T_type>
1223 inline boolean operator!=(template_sel left_value,
1224 const OPTIONAL<T_type>& right_value)
1225 { return !right_value.is_equal(left_value); }
1228 template<typename T_left, typename T_right>
1229 inline boolean operator==(const T_left& left_value,
1230 const OPTIONAL<T_right>& right_value)
1231 { return right_value.is_equal(left_value); }
1233 template<typename T_left, typename T_right>
1234 inline boolean operator!=(const T_left& left_value,
1235 const OPTIONAL<T_right>& right_value)
1236 { return !right_value.is_equal(left_value); }
1241 #pragma GCC diagnostic pop