1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 Module_Param* get_param(Module_Param_Name& param_name) const;
258 void encode_text(Text_Buf& text_buf) const;
259 void decode_text(Text_Buf& text_buf);
261 #ifdef TITAN_RUNTIME_2
262 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
263 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
266 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
267 int indent, embed_values_enc_struct_t* emb_val) const;
268 #ifdef TITAN_RUNTIME_2
269 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
270 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
271 int indent, embed_values_enc_struct_t* emb_val) const;
273 /** Used during XML decoding, in case this object is an AnyElement field in a record.
274 * Determines whether XER_decode() should be called or this field should be omitted.
275 * The field should be omitted if:
276 * - the next element in the encoded XML is the next field in the record or
277 * - there are no more elements until the end of the record's XML element.
279 * @param reader parses the encoded XML
280 * @param next_field_name name of the next field in the record, or null if this is the last one
281 * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
282 bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
283 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
284 unsigned int flavor, embed_values_dec_struct_t* emb_val);
286 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
289 operator const T_type&() const;
291 inline T_type& operator()() { return (T_type&)*this; }
292 inline const T_type& operator()() const { return (const T_type&)*this; }
294 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
295 unsigned p_coding) const;
296 #ifdef TITAN_RUNTIME_2
297 ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
298 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
300 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
301 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
302 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
303 const ASN_BER_TLV_t& p_tlv);
304 void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
306 #ifdef TITAN_RUNTIME_2
307 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
308 int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
309 const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
310 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
311 boolean no_err=FALSE, boolean first_call=TRUE);
312 int JSON_encode_negtest(const Erroneous_descriptor_t*,
313 const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
316 /** Encodes accordingly to the JSON encoding rules.
317 * Returns the length of the encoded data. */
318 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
320 /** Decodes accordingly to the JSON encoding rules.
321 * Returns the length of the decoded data. */
322 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
324 #ifdef TITAN_RUNTIME_2
325 /** Called before an element of an optional record of/set of is indexed and passed as an
326 * 'inout' or 'out' parameter to a function (only in Runtime2).
327 * Sets the optional value to present (this would be done by the indexing operation
328 * anyway) and redirects the call to the optional value. */
329 virtual void add_refd_index(int index);
331 /** Called after an element of an optional record of/set of is passed as an
332 * 'inout' or 'out' parameter to a function (only in Runtime2).
333 * Redirects the call to the optional value. */
334 virtual void remove_refd_index(int index);
339 #pragma GCC diagnostic push
340 #pragma GCC diagnostic ignored "-Wswitch-enum"
343 #ifdef TITAN_RUNTIME_2
345 template<typename T_type>
346 Base_Type* OPTIONAL<T_type>::get_opt_value()
348 #ifdef TITAN_RUNTIME_2
351 if (optional_selection!=OPTIONAL_PRESENT)
353 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
354 return optional_value;
357 template<typename T_type>
358 const Base_Type* OPTIONAL<T_type>::get_opt_value() const
360 #ifdef TITAN_RUNTIME_2
363 if (optional_selection!=OPTIONAL_PRESENT)
365 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
366 return optional_value;
369 template<typename T_type>
370 boolean OPTIONAL<T_type>::is_seof() const
373 #ifdef TITAN_RUNTIME_2
376 (optional_selection==OPTIONAL_PRESENT)
378 ? optional_value->is_seof() : T_type().is_seof();
381 template<typename T_type>
382 const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
385 #ifdef TITAN_RUNTIME_2
388 (optional_selection==OPTIONAL_PRESENT)
390 ? optional_value->get_descriptor() : T_type().get_descriptor();
395 template<typename T_type>
396 OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
397 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
398 #ifdef TITAN_RUNTIME_2
402 if (other_value != OMIT_VALUE)
403 TTCN_error("Setting an optional field to an invalid value.");
406 template<typename T_type>
407 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
408 : Base_Type(other_value)
409 #ifdef TITAN_RUNTIME_2
410 , RefdIndexInterface(other_value)
412 , optional_value(NULL)
413 , optional_selection(other_value.optional_selection)
414 #ifdef TITAN_RUNTIME_2
418 switch (other_value.optional_selection) {
419 case OPTIONAL_PRESENT:
420 optional_value = new T_type(*other_value.optional_value);
429 template<typename T_type> template<typename T_tmp>
430 OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
431 : optional_value(NULL), optional_selection(other_value.get_selection())
432 #ifdef TITAN_RUNTIME_2
436 switch (other_value.get_selection()) {
437 case OPTIONAL_PRESENT:
438 optional_value = new T_type((const T_tmp&)other_value);
447 template<typename T_type>
448 void OPTIONAL<T_type>::clean_up()
450 #ifdef TITAN_RUNTIME_2
452 if (param_refs > 0) {
453 optional_value->clean_up();
456 delete optional_value;
457 optional_value = NULL;
461 if (OPTIONAL_PRESENT == optional_selection) {
462 delete optional_value;
465 optional_selection = OPTIONAL_UNBOUND;
468 template<typename T_type>
469 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
471 if (other_value != OMIT_VALUE)
472 TTCN_error("Internal error: Setting an optional field to an invalid value.");
477 template<typename T_type>
478 OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
480 switch (other_value.optional_selection) {
481 case OPTIONAL_PRESENT:
482 #ifdef TITAN_RUNTIME_2
483 if (NULL == optional_value) {
485 if (optional_selection != OPTIONAL_PRESENT) {
487 optional_value = new T_type(*other_value.optional_value);
488 optional_selection = OPTIONAL_PRESENT;
489 } else *optional_value = *other_value.optional_value;
492 if (&other_value != this) set_to_omit();
501 template<typename T_type> template <typename T_tmp>
503 OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
505 switch (other_value.get_selection()) {
506 case OPTIONAL_PRESENT:
507 #ifdef TITAN_RUNTIME_2
508 if (NULL == optional_value) {
510 if (optional_selection != OPTIONAL_PRESENT) {
512 optional_value = new T_type((const T_tmp&)other_value);
513 optional_selection = OPTIONAL_PRESENT;
514 } else *optional_value = (const T_tmp&)other_value;
526 template<typename T_type> template <typename T_tmp>
528 OPTIONAL<T_type>::operator=(const T_tmp& other_value)
530 #ifdef TITAN_RUNTIME_2
531 if (NULL == optional_value) {
533 if (optional_selection != OPTIONAL_PRESENT) {
535 optional_value = new T_type(other_value);
536 optional_selection = OPTIONAL_PRESENT;
537 } else *optional_value = other_value;
541 template<typename T_type>
542 boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
544 #ifdef TITAN_RUNTIME_2
547 if (optional_selection == OPTIONAL_UNBOUND) {
549 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
550 TTCN_error("The left operand of comparison is an unbound optional value.");
552 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
553 "of comparison is an invalid value.");
555 #ifdef TITAN_RUNTIME_2
558 optional_selection == OPTIONAL_OMIT;
562 template<typename T_type>
563 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
565 #ifdef TITAN_RUNTIME_2
567 if (!other_value.is_bound())
569 if (optional_selection == OPTIONAL_UNBOUND) {
570 if (other_value.optional_selection == OPTIONAL_UNBOUND)
573 TTCN_error("The left operand of "
574 "comparison is an unbound optional value.");
576 #ifdef TITAN_RUNTIME_2
577 if (!other_value.is_bound())
579 if (other_value.optional_selection == OPTIONAL_UNBOUND)
581 TTCN_error("The right operand of comparison is an unbound optional value.");
582 #ifdef TITAN_RUNTIME_2
583 boolean present = is_present();
584 if (present != other_value.is_present()) return FALSE;
587 if (optional_selection != other_value.optional_selection) return FALSE;
588 else if (optional_selection == OPTIONAL_PRESENT)
590 return *optional_value == *other_value.optional_value;
594 template<typename T_type> template <typename T_tmp>
595 boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
597 #ifdef TITAN_RUNTIME_2
598 switch (get_selection()) {
600 switch (optional_selection) {
602 case OPTIONAL_PRESENT:
603 return *optional_value == other_value;
607 TTCN_error("The left operand of comparison is an unbound optional value.");
612 template<typename T_type> template <typename T_tmp>
613 boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
615 #ifdef TITAN_RUNTIME_2
617 if (!other_value.is_bound())
619 optional_sel other_selection = other_value.get_selection();
620 if (optional_selection == OPTIONAL_UNBOUND) {
621 if (other_selection == OPTIONAL_UNBOUND)
624 TTCN_error("The left operand of "
625 "comparison is an unbound optional value.");
627 #ifdef TITAN_RUNTIME_2
628 if (!other_value.is_bound())
630 if (other_selection == OPTIONAL_UNBOUND)
632 TTCN_error("The right operand of comparison is an unbound optional value.");
633 #ifdef TITAN_RUNTIME_2
634 boolean present = is_present();
635 if (present != other_value.is_present()) return FALSE;
638 if (optional_selection != other_selection) return FALSE;
639 else if (optional_selection == OPTIONAL_PRESENT)
641 return *optional_value == (const T_tmp&)other_value;
645 #ifdef TITAN_RUNTIME_2
646 template<typename T_type>
647 boolean OPTIONAL<T_type>::is_bound() const
649 switch (optional_selection) {
650 case OPTIONAL_PRESENT:
654 if (NULL != optional_value) {
655 return optional_value->is_bound();
661 template<typename T_type>
662 boolean OPTIONAL<T_type>::is_present() const
664 switch (optional_selection) {
665 case OPTIONAL_PRESENT:
669 if (NULL != optional_value) {
670 return optional_value->is_bound();
677 template<typename T_type>
678 boolean OPTIONAL<T_type>::ispresent() const
680 switch (optional_selection) {
681 case OPTIONAL_PRESENT:
684 #ifdef TITAN_RUNTIME_2
685 if (NULL != optional_value) {
686 return optional_value->is_bound();
691 #ifdef TITAN_RUNTIME_2
692 if (NULL != optional_value && optional_value->is_bound()) {
696 TTCN_error("Using an unbound optional field.");
701 #ifdef TITAN_RUNTIME_2
702 template<typename T_type>
703 optional_sel OPTIONAL<T_type>::get_selection() const
706 return OPTIONAL_PRESENT;
709 // not present, but bound => omit
710 return OPTIONAL_OMIT;
712 return OPTIONAL_UNBOUND;
716 template<typename T_type>
717 void OPTIONAL<T_type>::log() const
719 #ifdef TITAN_RUNTIME_2
720 switch (get_selection()) {
722 switch (optional_selection) {
724 case OPTIONAL_PRESENT:
725 optional_value->log();
728 TTCN_Logger::log_event_str("omit");
731 TTCN_Logger::log_event_unbound();
736 template <typename T_type>
737 void OPTIONAL<T_type>::set_param(Module_Param& param) {
738 if (param.get_type()==Module_Param::MP_Omit) {
739 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
740 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
745 optional_value->set_param(param);
748 template <typename T_type>
749 Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
751 #ifdef TITAN_RUNTIME_2
752 switch (get_selection()) {
754 switch (optional_selection) {
756 case OPTIONAL_PRESENT:
757 return optional_value->get_param(param_name);
759 return new Module_Param_Omit();
761 return new Module_Param_Unbound();
765 template<typename T_type>
766 void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
768 #ifdef TITAN_RUNTIME_2
769 switch (get_selection()) {
771 switch (optional_selection) {
774 text_buf.push_int((RInt)FALSE);
776 case OPTIONAL_PRESENT:
777 text_buf.push_int((RInt)TRUE);
778 optional_value->encode_text(text_buf);
781 TTCN_error("Text encoder: Encoding an unbound optional value.");
785 template<typename T_type>
786 void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
788 if (text_buf.pull_int().get_val()) {
790 optional_value->decode_text(text_buf);
791 } else set_to_omit();
794 template<typename T_type>
795 int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
797 #ifdef TITAN_RUNTIME_2
798 switch(get_selection()) {
800 switch(optional_selection) {
802 case OPTIONAL_PRESENT:
803 return optional_value->JSON_encode(p_td, p_tok);
805 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
807 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
808 "Encoding an unbound optional value.");
813 #ifdef TITAN_RUNTIME_2
814 template<typename T_type>
815 int OPTIONAL<T_type>::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
816 const TTCN_Typedescriptor_t& p_td,
817 JSON_Tokenizer& p_tok) const
819 switch (get_selection()) {
820 case OPTIONAL_PRESENT:
821 return optional_value->JSON_encode_negtest(p_err_descr, p_td, p_tok);
823 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
825 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
826 "Encoding an unbound optional value.");
832 template<typename T_type>
833 int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
835 // try the optional value first
837 size_t buf_pos = p_tok.get_buf_pos();
838 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
839 if (JSON_ERROR_FATAL == dec_len) {
846 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
847 // invalid token, rewind the buffer and check if it's a "null" (= omit)
848 // this needs to be checked after the optional value, because it might also be
849 // able to decode a "null" value
850 p_tok.set_buf_pos(buf_pos);
851 json_token_t token = JSON_TOKEN_NONE;
852 dec_len = p_tok.get_next_token(&token, NULL, NULL);
853 if (JSON_TOKEN_LITERAL_NULL == token) {
857 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
858 dec_len = JSON_ERROR_INVALID_TOKEN;
864 #ifdef TITAN_RUNTIME_2
865 template<typename T_type>
866 void OPTIONAL<T_type>::add_refd_index(int index)
870 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
871 if (0 != refd_opt_val) {
872 refd_opt_val->add_refd_index(index);
876 template<typename T_type>
877 void OPTIONAL<T_type>::remove_refd_index(int index)
880 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
881 if (0 != refd_opt_val) {
882 refd_opt_val->remove_refd_index(index);
887 template<typename T_type>
888 OPTIONAL<T_type>::operator T_type&()
891 return *optional_value;
894 template<typename T_type>
895 OPTIONAL<T_type>::operator const T_type&() const
897 #ifdef TITAN_RUNTIME_2
900 if (optional_selection != OPTIONAL_PRESENT)
902 TTCN_error("Using the value of an optional field containing omit.");
903 return *optional_value;
906 template<typename T_type>
908 OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
909 unsigned p_coding) const
912 #ifdef TITAN_RUNTIME_2
913 switch (get_selection()) {
915 switch (optional_selection) {
917 case OPTIONAL_PRESENT:
918 return optional_value->BER_encode_TLV(p_td, p_coding);
920 return ASN_BER_TLV_t::construct();
922 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
926 #ifdef TITAN_RUNTIME_2
927 template<typename T_type>
929 OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
930 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
933 #ifdef TITAN_RUNTIME_2
934 switch (get_selection()) {
936 switch (optional_selection) {
938 case OPTIONAL_PRESENT:
939 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
941 return ASN_BER_TLV_t::construct();
943 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
947 template<typename T_type>
948 int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
949 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
950 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
952 TTCN_error("RAW decoding requested for optional type '%s'"
953 " which has no RAW decoding method.",p_td.name);
958 template<typename T_type>
960 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
962 #ifdef TITAN_RUNTIME_2
963 switch (get_selection()) {
965 switch (optional_selection) {
967 case OPTIONAL_PRESENT:
968 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
970 return 0; // nothing to do !
972 TTCN_EncDec_ErrorContext::error(
973 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
978 #ifdef TITAN_RUNTIME_2
979 template<typename T_type>
981 OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
982 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
983 embed_values_enc_struct_t* emb_val) const
985 switch (get_selection()) {
986 case OPTIONAL_PRESENT:
987 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
989 return 0; // nothing to do !
991 TTCN_EncDec_ErrorContext::error(
992 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
999 template<typename T_type>
1001 OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
1003 // If the record has no elements, then it can't have an AnyElement
1004 if (parent_tag_closed) {
1009 while (reader.Ok()) {
1010 // Leaving the record before finding an element -> no AnyElement
1011 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
1015 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
1016 // The first element found is either the next field's element or the AnyElement
1017 if (NULL != next_field_name &&
1018 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
1030 template<typename T_type>
1032 OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1033 unsigned int flavor, embed_values_dec_struct_t* emb_val)
1035 int exer = is_exer(flavor);
1036 for (int success = reader.Ok(); success==1; success=reader.Read()) {
1037 int type = reader.NodeType();
1038 const char * name; // name of the optional field
1039 const char * ns_uri;
1041 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1042 if (XML_READER_TYPE_ATTRIBUTE == type) {
1043 for (; success==1; success = reader.MoveToNextAttribute()) {
1044 if (!reader.IsNamespaceDecl()) break;
1047 name = (const char*)reader.LocalName();
1048 if (!check_name(name, p_td, exer)) { // it's not us, bail
1051 // we already checked for exer==1
1052 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1054 // set to omit if the attribute is empty
1055 const char * value = (const char *)reader.Value();
1056 if (strlen(value) == 0) {
1061 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1066 else { // not attribute
1067 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1068 name = (const char*)reader.LocalName();
1069 ns_uri = (const char*)reader.NamespaceUri();
1070 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1071 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1072 // If the optional field (a string) has anyElement, accept the element
1073 // regardless of its name. Else the name (and namespace) must match.
1074 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1077 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
1078 optional_value->XER_decode(p_td, reader, flavor, emb_val);
1079 if (!optional_value->is_bound()) {
1083 else break; // it's not us, bail
1087 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1090 else if (XML_READER_TYPE_END_ELEMENT == type) {
1093 // else circle around
1102 template<typename T_type>
1103 char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
1104 #ifdef TITAN_RUNTIME_2
1105 switch (get_selection()) {
1107 switch (optional_selection) {
1109 case OPTIONAL_PRESENT:
1110 return optional_value->collect_ns(p_td, num, def_ns);
1116 TTCN_EncDec_ErrorContext::error(
1117 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1122 template<typename T_type>
1123 boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1124 const ASN_BER_TLV_t& p_tlv,
1127 BER_chk_descr(p_td);
1128 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1129 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1136 template<typename T_type>
1137 boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1138 const ASN_BER_TLV_t& p_tlv)
1141 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1144 template<typename T_type>
1145 void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1148 #ifdef TITAN_RUNTIME_2
1150 optional_selection = OPTIONAL_PRESENT;
1152 if (optional_selection==OPTIONAL_PRESENT) {
1154 optional_value->BER_decode_opentypes(p_typelist, L_form);
1158 #ifdef TITAN_RUNTIME_2
1160 template<typename T_type>
1161 int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1162 TTCN_Buffer& buff) const
1165 return optional_value->TEXT_encode(p_td, buff);
1166 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1170 template<typename T_type>
1171 int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1172 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1175 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1176 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1180 template<typename T_type>
1181 int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1182 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1185 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1190 #if defined(__GNUC__) && __GNUC__ >= 3
1191 /** Note: These functions allow most efficient operation by passing the left
1192 * operand OMIT_VALUE as value instead of constant reference.
1193 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1194 template<typename T_type>
1195 inline boolean operator==(template_sel left_value,
1196 const OPTIONAL<T_type>& right_value)
1197 { return right_value.is_equal(left_value); }
1199 template<typename T_type>
1200 inline boolean operator!=(template_sel left_value,
1201 const OPTIONAL<T_type>& right_value)
1202 { return !right_value.is_equal(left_value); }
1205 template<typename T_left, typename T_right>
1206 inline boolean operator==(const T_left& left_value,
1207 const OPTIONAL<T_right>& right_value)
1208 { return right_value.is_equal(left_value); }
1210 template<typename T_left, typename T_right>
1211 inline boolean operator!=(const T_left& left_value,
1212 const OPTIONAL<T_right>& right_value)
1213 { return !right_value.is_equal(left_value); }
1218 #pragma GCC diagnostic pop