Sync with 5.4.0
[deliverable/titan.core.git] / core / Optional.hh
CommitLineData
970ed795 1///////////////////////////////////////////////////////////////////////////////
3abe9331 2// Copyright (c) 2000-2015 Ericsson Telecom AB
970ed795
EL
3// All rights reserved. This program and the accompanying materials
4// are made available under the terms of the Eclipse Public License v1.0
5// which accompanies this distribution, and is available at
6// http://www.eclipse.org/legal/epl-v10.html
7///////////////////////////////////////////////////////////////////////////////
8#ifndef OPTIONAL_HH
9#define OPTIONAL_HH
10
11#include "Types.h"
12#include "Param_Types.hh"
13#include "Basetype.hh"
14#include "Template.hh"
15#include "BER.hh"
16#include "Logger.hh"
17#include "Encdec.hh"
18#include "Textbuf.hh"
19#include "Error.hh"
20#include "Parameters.h"
21#include "XER.hh"
22#include "JSON.hh"
23#include "XmlReader.hh"
24
25enum optional_sel { OPTIONAL_UNBOUND, OPTIONAL_OMIT, OPTIONAL_PRESENT };
26
27template <typename T_type>
a38c6d4c 28class OPTIONAL : public Base_Type
29#ifdef TITAN_RUNTIME_2
30 , public RefdIndexInterface
31#endif
32{
af710487 33 /** The value, if present (owned by OPTIONAL)
34 * In Runtime2 the pointer is null, when the value is not present.
35 * In Runtime1 its presence is indicated by the optional_selection member. */
36 T_type *optional_value;
37
970ed795 38 /** Specifies the state of the optional field
af710487 39 * @tricky In Runtime2 the optional value can be modified through parameter references,
970ed795
EL
40 * in which case this member variable will not be updated. Always use the function
41 * get_selection() instead of directly referencing this variable. */
af710487 42 optional_sel optional_selection;
970ed795 43
af710487 44#ifdef TITAN_RUNTIME_2
970ed795 45 /** Stores the number of elements referenced by 'out' and 'inout' parameters,
af710487 46 * if the optional field is a record of/set of/array (only in Runtime2).
970ed795 47 * If at least one element is referenced, the value must not be deleted. */
af710487 48 int param_refs;
49#endif
970ed795
EL
50
51 /** Set the optional value to present.
52 * If the value was already present, does nothing.
53 * Else allocates a new (uninitialized) T-type.
54 * @post \c optional_selection is OPTIONAL_PRESENT
55 * @post \c optional_value is not NULL
56 */
57#ifdef TITAN_RUNTIME_2
58public:
59 virtual
60#else
61 inline
62#endif
63 void set_to_present() {
64 if (optional_selection != OPTIONAL_PRESENT) {
65 optional_selection = OPTIONAL_PRESENT;
af710487 66#ifdef TITAN_RUNTIME_2
67 if (optional_value == NULL)
68#endif
970ed795 69 optional_value = new T_type;
970ed795
EL
70 }
71 }
72
73 /** Set the optional value to omit.
74 * If the value was present, frees it.
75 * @post optional_selection is OPTIONAL_OMIT
76 */
77#ifdef TITAN_RUNTIME_2
78public:
79 virtual
80#else
81 inline
82#endif
83 void set_to_omit() {
af710487 84#ifdef TITAN_RUNTIME_2
970ed795
EL
85 if (is_present()) {
86 if (param_refs > 0) {
87 optional_value->clean_up();
88 }
89 else {
90 delete optional_value;
91 optional_value = NULL;
92 }
93 }
af710487 94#else
95 if (optional_selection == OPTIONAL_PRESENT) {
96 delete optional_value;
97 }
98#endif
970ed795
EL
99 optional_selection = OPTIONAL_OMIT;
100 }
101
102public:
103 /// Default constructor creates an unbound object
af710487 104 OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
105#ifdef TITAN_RUNTIME_2
106 , param_refs(0)
107#endif
108 { }
970ed795
EL
109
110 /// Construct an optional object set to omit.
111 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
112 OPTIONAL(template_sel other_value);
113
114 /// Copy constructor.
115 /// @note Copying an unbound object creates another unbound object,
116 /// without causing a dynamic test case immediately.
117 OPTIONAL(const OPTIONAL& other_value);
118
119 /// Construct from an optional of different type
120 template <typename T_tmp>
121 OPTIONAL(const OPTIONAL<T_tmp>& other_value);
122
123 /// Construct from an object of different type
124 template <typename T_tmp>
125 OPTIONAL(const T_tmp& other_value)
af710487 126 : optional_value(new T_type(other_value))
127 , optional_selection(OPTIONAL_PRESENT)
128#ifdef TITAN_RUNTIME_2
129 , param_refs(0)
130#endif
131 { }
970ed795 132
af710487 133 ~OPTIONAL() {
134#ifdef TITAN_RUNTIME_2
135 if (NULL != optional_value)
136#else
137 if (optional_selection == OPTIONAL_PRESENT)
138#endif
139 delete optional_value;
140 }
970ed795
EL
141
142 void clean_up();
143
144 /// Set to omit.
145 /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
146 OPTIONAL& operator=(template_sel other_value);
147
148 /// Copy assignment
149 OPTIONAL& operator=(const OPTIONAL& other_value);
150
151 /// Assign from an optional of another type
152 template <typename T_tmp>
153 OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);
154
155 /// Assign the value
156 template <typename T_tmp>
157 OPTIONAL& operator=(const T_tmp& other_value);
158
159 boolean is_equal(template_sel other_value) const;
160 boolean is_equal(const OPTIONAL& other_value) const;
161 template <typename T_tmp>
162 boolean is_equal(const OPTIONAL<T_tmp>& other_value) const;
163 template <typename T_tmp>
164 boolean is_equal(const T_tmp& other_value) const;
165
166 inline boolean operator==(template_sel other_value) const
167 { return is_equal(other_value); }
168 inline boolean operator!=(template_sel other_value) const
169 { return !is_equal(other_value); }
170 inline boolean operator==(const OPTIONAL& other_value) const
171 { return is_equal(other_value); }
172 inline boolean operator!=(const OPTIONAL& other_value) const
173 { return !is_equal(other_value); }
174 template <typename T_tmp>
175 inline boolean operator==(const T_tmp& other_value) const
176 { return is_equal(other_value); }
177 template <typename T_tmp>
178 inline boolean operator!=(const T_tmp& other_value) const
179 { return !is_equal(other_value); }
180#ifdef __SUNPRO_CC
181 /* Note: Without these functions the Sun Workshop Pro C++ compiler reports
182 * overloading ambiguity when comparing an optional charstring field with an
183 * optional universal charstring. */
184 template <typename T_tmp>
185 inline boolean operator==(const OPTIONAL<T_tmp>& other_value) const
186 { return is_equal(other_value); }
187 template <typename T_tmp>
188 inline boolean operator!=(const OPTIONAL<T_tmp>& other_value) const
189 { return is_equal(other_value); }
190#endif
191
af710487 192#ifdef TITAN_RUNTIME_2
970ed795 193 boolean is_bound() const;
af710487 194#else
195 inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
196#endif
970ed795
EL
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 */
af710487 201#ifdef TITAN_RUNTIME_2
970ed795 202 boolean is_present() const;
af710487 203#else
204 inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
205#endif
970ed795
EL
206
207#ifdef TITAN_RUNTIME_2
208 /** @name override virtual functions of Base_Type
209 * @{ */
210
211 /** Return \c true (this \b is an optional field) */
212 boolean is_optional() const { return TRUE; }
213
214 /** Access the value for read/write
215 *
216 * @return a pointer to the (modifiable) value
217 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
218 */
219 Base_Type* get_opt_value();
220
221 /** Access the value (read/only)
222 *
223 * @return a pointer to the (const) value
224 * @pre \p optional_selection must be \p OPTIONAL_PRESENT
225 */
226 const Base_Type* get_opt_value() const;
227 boolean is_seof() const;
228 boolean is_equal(const Base_Type* other_value) const { return is_equal(*(static_cast<const OPTIONAL*>(other_value))); }
229 void set_value(const Base_Type* other_value) { *this = *(static_cast<const OPTIONAL*>(other_value)); }
230 Base_Type* clone() const { return new OPTIONAL(*this); }
231 const TTCN_Typedescriptor_t* get_descriptor() const;
232 /** @} */
233#endif
234
235 /** Whether the value is present.
236 * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility
237 * with the runtime and existing testports which use this version where
238 * unbound errors are caught before causing more trouble
239 *
240 * @return TRUE if the value is present (optional_selection==OPTIONAL_PRESENT)
241 * @return FALSE if the value is not present (optional_selection==OPTIONAL_OMIT)
242 * @pre the value is bound (optional_selection!=OPTIONAL_UNBOUND)
243 */
244 boolean ispresent() const;
245
af710487 246#ifdef TITAN_RUNTIME_2
970ed795 247 /** @tricky Calculates and returns the actual state of the optional object,
af710487 248 * not just the optional_selection member.
249 * (Only needed in Runtime2, in Runtime1 optional_selection is always up to date.) */
970ed795 250 optional_sel get_selection() const;
af710487 251#else
252 inline optional_sel get_selection() const { return optional_selection; }
253#endif
970ed795
EL
254
255 void log() const;
256 void set_param(Module_Param& param);
3abe9331 257 Module_Param* get_param(Module_Param_Name& param_name) const;
970ed795
EL
258 void encode_text(Text_Buf& text_buf) const;
259 void decode_text(Text_Buf& text_buf);
260
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);
264#endif
265
af710487 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;
970ed795
EL
268#ifdef TITAN_RUNTIME_2
269 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
af710487 270 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
271 int indent, embed_values_enc_struct_t* emb_val) const;
970ed795
EL
272#endif
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.
278 *
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);
af710487 283 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
284 unsigned int flavor, embed_values_dec_struct_t* emb_val);
970ed795
EL
285
286 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
287
288 operator T_type&();
289 operator const T_type&() const;
290
291 inline T_type& operator()() { return (T_type&)*this; }
292 inline const T_type& operator()() const { return (const T_type&)*this; }
293
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;
299#endif
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);
305
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#endif
313
314 /** Encodes accordingly to the JSON encoding rules.
315 * Returns the length of the encoded data. */
316 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
317
318 /** Decodes accordingly to the JSON encoding rules.
319 * Returns the length of the decoded data. */
320 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
321
af710487 322#ifdef TITAN_RUNTIME_2
970ed795 323 /** Called before an element of an optional record of/set of is indexed and passed as an
af710487 324 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795
EL
325 * Sets the optional value to present (this would be done by the indexing operation
326 * anyway) and redirects the call to the optional value. */
a38c6d4c 327 virtual void add_refd_index(int index);
970ed795
EL
328
329 /** Called after an element of an optional record of/set of is passed as an
af710487 330 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795 331 * Redirects the call to the optional value. */
a38c6d4c 332 virtual void remove_refd_index(int index);
af710487 333#endif
970ed795
EL
334};
335
336#if HAVE_GCC(4,6)
337#pragma GCC diagnostic push
338#pragma GCC diagnostic ignored "-Wswitch-enum"
339#endif
340
341#ifdef TITAN_RUNTIME_2
342
343template<typename T_type>
344Base_Type* OPTIONAL<T_type>::get_opt_value()
345{
af710487 346#ifdef TITAN_RUNTIME_2
970ed795 347 if (!is_present())
af710487 348#else
349 if (optional_selection!=OPTIONAL_PRESENT)
350#endif
970ed795
EL
351 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
352 return optional_value;
353}
354
355template<typename T_type>
356const Base_Type* OPTIONAL<T_type>::get_opt_value() const
357{
af710487 358#ifdef TITAN_RUNTIME_2
970ed795 359 if (!is_present())
af710487 360#else
361 if (optional_selection!=OPTIONAL_PRESENT)
362#endif
970ed795
EL
363 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
364 return optional_value;
365}
366
367template<typename T_type>
368boolean OPTIONAL<T_type>::is_seof() const
369{
af710487 370 return
371#ifdef TITAN_RUNTIME_2
372 (is_present())
373#else
374 (optional_selection==OPTIONAL_PRESENT)
375#endif
376 ? optional_value->is_seof() : T_type().is_seof();
970ed795
EL
377}
378
379template<typename T_type>
380const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
381{
af710487 382 return
383#ifdef TITAN_RUNTIME_2
384 (is_present())
385#else
386 (optional_selection==OPTIONAL_PRESENT)
387#endif
388 ? optional_value->get_descriptor() : T_type().get_descriptor();
970ed795
EL
389}
390
391#endif
392
393template<typename T_type>
394OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
af710487 395 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
396#ifdef TITAN_RUNTIME_2
397 , param_refs(0)
398#endif
970ed795
EL
399{
400 if (other_value != OMIT_VALUE)
401 TTCN_error("Setting an optional field to an invalid value.");
402}
403
404template<typename T_type>
405OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
406 : Base_Type(other_value)
3abe9331 407#ifdef TITAN_RUNTIME_2
408 , RefdIndexInterface(other_value)
409#endif
970ed795 410 , optional_value(NULL)
af710487 411 , optional_selection(other_value.optional_selection)
412#ifdef TITAN_RUNTIME_2
970ed795 413 , param_refs(0)
af710487 414#endif
970ed795
EL
415{
416 switch (other_value.optional_selection) {
417 case OPTIONAL_PRESENT:
418 optional_value = new T_type(*other_value.optional_value);
419 break;
420 case OPTIONAL_OMIT:
421 break;
422 default:
423 break;
424 }
425}
426
427template<typename T_type> template<typename T_tmp>
428OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
af710487 429 : optional_value(NULL), optional_selection(other_value.get_selection())
430#ifdef TITAN_RUNTIME_2
431 , param_refs(0)
432#endif
970ed795
EL
433{
434 switch (other_value.get_selection()) {
435 case OPTIONAL_PRESENT:
436 optional_value = new T_type((const T_tmp&)other_value);
437 break;
438 case OPTIONAL_OMIT:
439 break;
440 default:
441 break;
442 }
443}
444
445template<typename T_type>
446void OPTIONAL<T_type>::clean_up()
447{
af710487 448#ifdef TITAN_RUNTIME_2
970ed795
EL
449 if (is_present()) {
450 if (param_refs > 0) {
451 optional_value->clean_up();
452 }
453 else {
454 delete optional_value;
455 optional_value = NULL;
456 }
457 }
af710487 458#else
459 if (OPTIONAL_PRESENT == optional_selection) {
460 delete optional_value;
461 }
462#endif
970ed795
EL
463 optional_selection = OPTIONAL_UNBOUND;
464}
465
466template<typename T_type>
467OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
468{
469 if (other_value != OMIT_VALUE)
470 TTCN_error("Internal error: Setting an optional field to an invalid value.");
471 set_to_omit();
472 return *this;
473}
474
475template<typename T_type>
476OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
477{
478 switch (other_value.optional_selection) {
479 case OPTIONAL_PRESENT:
af710487 480#ifdef TITAN_RUNTIME_2
970ed795 481 if (NULL == optional_value) {
af710487 482#else
483 if (optional_selection != OPTIONAL_PRESENT) {
484#endif
970ed795 485 optional_value = new T_type(*other_value.optional_value);
af710487 486 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
487 } else *optional_value = *other_value.optional_value;
488 break;
489 case OPTIONAL_OMIT:
490 if (&other_value != this) set_to_omit();
491 break;
492 default:
493 clean_up();
494 break;
495 }
496 return *this;
497}
498
499template<typename T_type> template <typename T_tmp>
500OPTIONAL<T_type>&
501OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
502{
503 switch (other_value.get_selection()) {
504 case OPTIONAL_PRESENT:
af710487 505#ifdef TITAN_RUNTIME_2
970ed795 506 if (NULL == optional_value) {
af710487 507#else
508 if (optional_selection != OPTIONAL_PRESENT) {
509#endif
970ed795 510 optional_value = new T_type((const T_tmp&)other_value);
af710487 511 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
512 } else *optional_value = (const T_tmp&)other_value;
513 break;
514 case OPTIONAL_OMIT:
515 set_to_omit();
516 break;
517 default:
518 clean_up();
519 break;
520 }
521 return *this;
522}
523
524template<typename T_type> template <typename T_tmp>
525OPTIONAL<T_type>&
526OPTIONAL<T_type>::operator=(const T_tmp& other_value)
527{
af710487 528#ifdef TITAN_RUNTIME_2
970ed795 529 if (NULL == optional_value) {
af710487 530#else
531 if (optional_selection != OPTIONAL_PRESENT) {
532#endif
970ed795 533 optional_value = new T_type(other_value);
af710487 534 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
535 } else *optional_value = other_value;
536 return *this;
537}
538
539template<typename T_type>
540boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
541{
af710487 542#ifdef TITAN_RUNTIME_2
970ed795 543 if (!is_bound()) {
af710487 544#else
545 if (optional_selection == OPTIONAL_UNBOUND) {
546#endif
970ed795
EL
547 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
548 TTCN_error("The left operand of comparison is an unbound optional value.");
549 }
550 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
551 "of comparison is an invalid value.");
af710487 552 return
553#ifdef TITAN_RUNTIME_2
554 !is_present();
555#else
556 optional_selection == OPTIONAL_OMIT;
557#endif
970ed795
EL
558}
559
560template<typename T_type>
561boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
562{
af710487 563#ifdef TITAN_RUNTIME_2
970ed795 564 if (!is_bound()) {
af710487 565 if (!other_value.is_bound())
566#else
567 if (optional_selection == OPTIONAL_UNBOUND) {
568 if (other_value.optional_selection == OPTIONAL_UNBOUND)
569#endif
570 return TRUE;
970ed795
EL
571 TTCN_error("The left operand of "
572 "comparison is an unbound optional value.");
573 }
af710487 574#ifdef TITAN_RUNTIME_2
970ed795 575 if (!other_value.is_bound())
af710487 576#else
577 if (other_value.optional_selection == OPTIONAL_UNBOUND)
578#endif
970ed795 579 TTCN_error("The right operand of comparison is an unbound optional value.");
af710487 580#ifdef TITAN_RUNTIME_2
970ed795
EL
581 boolean present = is_present();
582 if (present != other_value.is_present()) return FALSE;
583 else if (present)
af710487 584#else
585 if (optional_selection != other_value.optional_selection) return FALSE;
586 else if (optional_selection == OPTIONAL_PRESENT)
587#endif
970ed795
EL
588 return *optional_value == *other_value.optional_value;
589 else return TRUE;
590}
591
592template<typename T_type> template <typename T_tmp>
593boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
594{
af710487 595#ifdef TITAN_RUNTIME_2
970ed795 596 switch (get_selection()) {
af710487 597#else
598 switch (optional_selection) {
599#endif
970ed795
EL
600 case OPTIONAL_PRESENT:
601 return *optional_value == other_value;
602 case OPTIONAL_OMIT:
603 return FALSE;
604 default:
605 TTCN_error("The left operand of comparison is an unbound optional value.");
606 }
607 return FALSE;
608}
609
610template<typename T_type> template <typename T_tmp>
611boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
612{
af710487 613#ifdef TITAN_RUNTIME_2
970ed795 614 if (!is_bound()) {
af710487 615 if (!other_value.is_bound())
616#else
617 optional_sel other_selection = other_value.get_selection();
618 if (optional_selection == OPTIONAL_UNBOUND) {
619 if (other_selection == OPTIONAL_UNBOUND)
620#endif
621 return TRUE;
970ed795
EL
622 TTCN_error("The left operand of "
623 "comparison is an unbound optional value.");
624 }
af710487 625#ifdef TITAN_RUNTIME_2
626 if (!other_value.is_bound())
627#else
628 if (other_selection == OPTIONAL_UNBOUND)
629#endif
630 TTCN_error("The right operand of comparison is an unbound optional value.");
631#ifdef TITAN_RUNTIME_2
970ed795
EL
632 boolean present = is_present();
633 if (present != other_value.is_present()) return FALSE;
634 else if (present)
af710487 635#else
636 if (optional_selection != other_selection) return FALSE;
637 else if (optional_selection == OPTIONAL_PRESENT)
638#endif
970ed795
EL
639 return *optional_value == (const T_tmp&)other_value;
640 else return TRUE;
641}
642
af710487 643#ifdef TITAN_RUNTIME_2
970ed795
EL
644template<typename T_type>
645boolean OPTIONAL<T_type>::is_bound() const
646{
647 switch (optional_selection) {
648 case OPTIONAL_PRESENT:
649 case OPTIONAL_OMIT:
650 return TRUE;
651 default:
652 if (NULL != optional_value) {
653 return optional_value->is_bound();
654 }
655 return FALSE;
656 }
657}
658
659template<typename T_type>
660boolean OPTIONAL<T_type>::is_present() const
661{
662 switch (optional_selection) {
663 case OPTIONAL_PRESENT:
664 return TRUE;
665 case OPTIONAL_OMIT:
666 default:
667 if (NULL != optional_value) {
668 return optional_value->is_bound();
669 }
670 return FALSE;
671 }
672}
af710487 673#endif
970ed795
EL
674
675template<typename T_type>
676boolean OPTIONAL<T_type>::ispresent() const
677{
678 switch (optional_selection) {
679 case OPTIONAL_PRESENT:
680 return TRUE;
681 case OPTIONAL_OMIT:
af710487 682#ifdef TITAN_RUNTIME_2
970ed795
EL
683 if (NULL != optional_value) {
684 return optional_value->is_bound();
685 }
af710487 686#endif
970ed795
EL
687 return FALSE;
688 default:
af710487 689#ifdef TITAN_RUNTIME_2
970ed795
EL
690 if (NULL != optional_value && optional_value->is_bound()) {
691 return TRUE;
692 }
af710487 693#endif
970ed795 694 TTCN_error("Using an unbound optional field.");
970ed795 695 }
af710487 696 return FALSE;
970ed795
EL
697}
698
af710487 699#ifdef TITAN_RUNTIME_2
970ed795
EL
700template<typename T_type>
701optional_sel OPTIONAL<T_type>::get_selection() const
702{
703 if (is_present()) {
704 return OPTIONAL_PRESENT;
705 }
706 if (is_bound()) {
707 // not present, but bound => omit
708 return OPTIONAL_OMIT;
709 }
710 return OPTIONAL_UNBOUND;
711}
af710487 712#endif
970ed795
EL
713
714template<typename T_type>
715void OPTIONAL<T_type>::log() const
716{
af710487 717#ifdef TITAN_RUNTIME_2
970ed795 718 switch (get_selection()) {
af710487 719#else
720 switch (optional_selection) {
721#endif
970ed795
EL
722 case OPTIONAL_PRESENT:
723 optional_value->log();
724 break;
725 case OPTIONAL_OMIT:
726 TTCN_Logger::log_event_str("omit");
727 break;
728 default:
729 TTCN_Logger::log_event_unbound();
730 break;
731 }
732}
733
734template <typename T_type>
735void OPTIONAL<T_type>::set_param(Module_Param& param) {
736 if (param.get_type()==Module_Param::MP_Omit) {
737 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
738 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
739 set_to_omit();
740 return;
741 }
742 set_to_present();
743 optional_value->set_param(param);
744}
745
3abe9331 746template <typename T_type>
747Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
748{
749#ifdef TITAN_RUNTIME_2
750 switch (get_selection()) {
751#else
752 switch (optional_selection) {
753#endif
754 case OPTIONAL_PRESENT:
755 return optional_value->get_param(param_name);
756 case OPTIONAL_OMIT:
757 return new Module_Param_Omit();
758 default:
759 return new Module_Param_Unbound();
760 }
761}
762
970ed795
EL
763template<typename T_type>
764void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
765{
af710487 766#ifdef TITAN_RUNTIME_2
970ed795 767 switch (get_selection()) {
af710487 768#else
769 switch (optional_selection) {
770#endif
970ed795
EL
771 case OPTIONAL_OMIT:
772 text_buf.push_int((RInt)FALSE);
773 break;
774 case OPTIONAL_PRESENT:
775 text_buf.push_int((RInt)TRUE);
776 optional_value->encode_text(text_buf);
777 break;
778 default:
779 TTCN_error("Text encoder: Encoding an unbound optional value.");
780 }
781}
782
783template<typename T_type>
784void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
785{
786 if (text_buf.pull_int().get_val()) {
787 set_to_present();
788 optional_value->decode_text(text_buf);
789 } else set_to_omit();
790}
791
792template<typename T_type>
793int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
794{
af710487 795#ifdef TITAN_RUNTIME_2
970ed795 796 switch(get_selection()) {
af710487 797#else
798 switch(optional_selection) {
799#endif
970ed795
EL
800 case OPTIONAL_PRESENT:
801 return optional_value->JSON_encode(p_td, p_tok);
802 case OPTIONAL_OMIT:
803 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
804 default:
805 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
806 "Encoding an unbound optional value.");
807 return -1;
808 }
809}
810
811template<typename T_type>
812int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
813{
af710487 814 // try the optional value first
815 set_to_present();
970ed795 816 size_t buf_pos = p_tok.get_buf_pos();
af710487 817 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
818 if (JSON_ERROR_FATAL == dec_len) {
819 if (p_silent) {
820 clean_up();
821 } else {
822 set_to_omit();
823 }
970ed795 824 }
af710487 825 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
826 // invalid token, rewind the buffer and check if it's a "null" (= omit)
827 // this needs to be checked after the optional value, because it might also be
828 // able to decode a "null" value
970ed795 829 p_tok.set_buf_pos(buf_pos);
af710487 830 json_token_t token = JSON_TOKEN_NONE;
831 dec_len = p_tok.get_next_token(&token, NULL, NULL);
832 if (JSON_TOKEN_LITERAL_NULL == token) {
833 set_to_omit();
834 }
835 else {
836 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
837 dec_len = JSON_ERROR_INVALID_TOKEN;
970ed795 838 }
970ed795
EL
839 }
840 return dec_len;
841}
842
af710487 843#ifdef TITAN_RUNTIME_2
970ed795
EL
844template<typename T_type>
845void OPTIONAL<T_type>::add_refd_index(int index)
846{
847 ++param_refs;
848 set_to_present();
a38c6d4c 849 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
850 if (0 != refd_opt_val) {
851 refd_opt_val->add_refd_index(index);
852 }
970ed795
EL
853}
854
855template<typename T_type>
856void OPTIONAL<T_type>::remove_refd_index(int index)
857{
858 --param_refs;
a38c6d4c 859 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
860 if (0 != refd_opt_val) {
861 refd_opt_val->remove_refd_index(index);
862 }
970ed795 863}
af710487 864#endif
509718e0 865
970ed795
EL
866template<typename T_type>
867OPTIONAL<T_type>::operator T_type&()
868{
869 set_to_present();
870 return *optional_value;
871}
872
873template<typename T_type>
874OPTIONAL<T_type>::operator const T_type&() const
875{
af710487 876#ifdef TITAN_RUNTIME_2
970ed795 877 if (!is_present())
af710487 878#else
879 if (optional_selection != OPTIONAL_PRESENT)
880#endif
970ed795
EL
881 TTCN_error("Using the value of an optional field containing omit.");
882 return *optional_value;
883}
884
885template<typename T_type>
886ASN_BER_TLV_t*
887OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
888 unsigned p_coding) const
889{
890 BER_chk_descr(p_td);
af710487 891#ifdef TITAN_RUNTIME_2
970ed795 892 switch (get_selection()) {
af710487 893#else
894 switch (optional_selection) {
895#endif
970ed795
EL
896 case OPTIONAL_PRESENT:
897 return optional_value->BER_encode_TLV(p_td, p_coding);
898 case OPTIONAL_OMIT:
899 return ASN_BER_TLV_t::construct();
900 default:
901 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
902 }
903}
904
905#ifdef TITAN_RUNTIME_2
906template<typename T_type>
907ASN_BER_TLV_t*
908OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
909 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
910{
911 BER_chk_descr(p_td);
af710487 912#ifdef TITAN_RUNTIME_2
970ed795 913 switch (get_selection()) {
af710487 914#else
915 switch (optional_selection) {
916#endif
970ed795
EL
917 case OPTIONAL_PRESENT:
918 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
919 case OPTIONAL_OMIT:
920 return ASN_BER_TLV_t::construct();
921 default:
922 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
923 }
924}
925
926template<typename T_type>
927int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
928 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
929 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
930{
931 TTCN_error("RAW decoding requested for optional type '%s'"
932 " which has no RAW decoding method.",p_td.name);
933 return 0;
934}
935#endif
936
937template<typename T_type>
938int
af710487 939OPTIONAL<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
970ed795 940{
af710487 941#ifdef TITAN_RUNTIME_2
970ed795 942 switch (get_selection()) {
af710487 943#else
944 switch (optional_selection) {
945#endif
970ed795 946 case OPTIONAL_PRESENT:
af710487 947 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
970ed795
EL
948 case OPTIONAL_OMIT:
949 return 0; // nothing to do !
950 default:
951 TTCN_EncDec_ErrorContext::error(
952 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
953 return 0;
954 }
955}
956
957#ifdef TITAN_RUNTIME_2
958template<typename T_type>
959int
960OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
af710487 961 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
962 embed_values_enc_struct_t* emb_val) const
970ed795
EL
963{
964 switch (get_selection()) {
965 case OPTIONAL_PRESENT:
af710487 966 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
970ed795
EL
967 case OPTIONAL_OMIT:
968 return 0; // nothing to do !
969 default:
970 TTCN_EncDec_ErrorContext::error(
971 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
972 return 0;
973 }
974}
975#endif
976
977
978template<typename T_type>
979bool
980OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
981{
982 // If the record has no elements, then it can't have an AnyElement
983 if (parent_tag_closed) {
984 set_to_omit();
985 return false;
986 }
987
988 while (reader.Ok()) {
989 // Leaving the record before finding an element -> no AnyElement
990 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
991 set_to_omit();
992 return false;
993 }
994 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
995 // The first element found is either the next field's element or the AnyElement
996 if (NULL != next_field_name &&
997 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
998 set_to_omit();
999 return false;
1000 }
1001 break;
1002 }
1003 reader.Read();
1004 }
1005
1006 return true;
1007}
1008
1009template<typename T_type>
1010int
af710487 1011OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1012 unsigned int flavor, embed_values_dec_struct_t* emb_val)
970ed795
EL
1013{
1014 int exer = is_exer(flavor);
1015 for (int success = reader.Ok(); success==1; success=reader.Read()) {
1016 int type = reader.NodeType();
1017 const char * name; // name of the optional field
1018 const char * ns_uri;
1019
1020 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1021 if (XML_READER_TYPE_ATTRIBUTE == type) {
1022 for (; success==1; success = reader.MoveToNextAttribute()) {
1023 if (!reader.IsNamespaceDecl()) break;
1024 }
1025
1026 name = (const char*)reader.LocalName();
1027 if (!check_name(name, p_td, exer)) { // it's not us, bail
1028 break;
1029 }
1030 // we already checked for exer==1
1031 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1032
a38c6d4c 1033 // set to omit if the attribute is empty
1034 const char * value = (const char *)reader.Value();
1035 if (strlen(value) == 0) {
1036 break;
1037 }
1038
970ed795 1039 set_to_present();
af710487 1040 optional_value->XER_decode(p_td, reader, flavor, emb_val);
970ed795
EL
1041 goto finished;
1042 }
1043 else break;
1044 }
1045 else { // not attribute
1046 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1047 name = (const char*)reader.LocalName();
1048 ns_uri = (const char*)reader.NamespaceUri();
1049 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1050 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1051 // If the optional field (a string) has anyElement, accept the element
1052 // regardless of its name. Else the name (and namespace) must match.
1053 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1054 found_it:
1055 set_to_present();
1056 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
af710487 1057 optional_value->XER_decode(p_td, reader, flavor, emb_val);
3abe9331 1058 if (!optional_value->is_bound()) {
1059 set_to_omit();
1060 }
970ed795
EL
1061 }
1062 else break; // it's not us, bail
1063
1064 goto finished;
1065 }
1066 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1067 goto found_it;
1068 }
1069 else if (XML_READER_TYPE_END_ELEMENT == type) {
1070 break;
1071 }
1072 // else circle around
1073 } // if attribute
1074 } // next
1075 set_to_omit();
1076 return 0;
1077finished:
1078 return 1;
1079}
1080
1081template<typename T_type>
1082char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
af710487 1083#ifdef TITAN_RUNTIME_2
970ed795 1084 switch (get_selection()) {
af710487 1085#else
1086 switch (optional_selection) {
1087#endif
970ed795
EL
1088 case OPTIONAL_PRESENT:
1089 return optional_value->collect_ns(p_td, num, def_ns);
1090 case OPTIONAL_OMIT:
1091 def_ns = false;
1092 num = 0;
1093 return 0;
1094 default:
1095 TTCN_EncDec_ErrorContext::error(
1096 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1097 return 0;
1098 }
1099}
1100
1101template<typename T_type>
1102boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1103 const ASN_BER_TLV_t& p_tlv,
1104 unsigned L_form)
1105{
1106 BER_chk_descr(p_td);
1107 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1108 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1109 } else {
1110 set_to_omit();
1111 return TRUE;
1112 }
1113}
1114
1115template<typename T_type>
1116boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1117 const ASN_BER_TLV_t& p_tlv)
1118{
1119 set_to_present();
1120 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1121}
1122
1123template<typename T_type>
1124void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1125 unsigned L_form)
1126{
af710487 1127#ifdef TITAN_RUNTIME_2
970ed795
EL
1128 if (is_present()) {
1129 optional_selection = OPTIONAL_PRESENT;
af710487 1130#else
1131 if (optional_selection==OPTIONAL_PRESENT) {
1132#endif
970ed795
EL
1133 optional_value->BER_decode_opentypes(p_typelist, L_form);
1134 }
1135}
1136
1137#ifdef TITAN_RUNTIME_2
1138
1139template<typename T_type>
1140int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1141 TTCN_Buffer& buff) const
1142{
af710487 1143 if (is_present())
970ed795
EL
1144 return optional_value->TEXT_encode(p_td, buff);
1145 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1146 return 0;
1147}
1148
1149template<typename T_type>
1150int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1151 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1152{
af710487 1153 if (is_present())
970ed795
EL
1154 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1155 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1156 return 0;
1157}
1158
1159template<typename T_type>
1160int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1161 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1162{
1163 set_to_present();
1164 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1165}
1166
1167#endif
1168
1169#if defined(__GNUC__) && __GNUC__ >= 3
1170/** Note: These functions allow most efficient operation by passing the left
1171 * operand OMIT_VALUE as value instead of constant reference.
1172 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1173template<typename T_type>
1174inline boolean operator==(template_sel left_value,
1175 const OPTIONAL<T_type>& right_value)
1176 { return right_value.is_equal(left_value); }
1177
1178template<typename T_type>
1179inline boolean operator!=(template_sel left_value,
1180 const OPTIONAL<T_type>& right_value)
1181 { return !right_value.is_equal(left_value); }
1182#endif
1183
1184template<typename T_left, typename T_right>
1185inline boolean operator==(const T_left& left_value,
1186 const OPTIONAL<T_right>& right_value)
1187 { return right_value.is_equal(left_value); }
1188
1189template<typename T_left, typename T_right>
1190inline boolean operator!=(const T_left& left_value,
1191 const OPTIONAL<T_right>& right_value)
1192 { return !right_value.is_equal(left_value); }
1193
1194#endif
1195
1196#if HAVE_GCC(4,6)
1197#pragma GCC diagnostic pop
1198#endif
This page took 0.086349 seconds and 5 git commands to generate.