Sync with 5.3.0
[deliverable/titan.core.git] / core / Optional.hh
CommitLineData
970ed795
EL
1///////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2000-2014 Ericsson Telecom AB
3// All rights reserved. This program and the accompanying materials
4// are made available under the terms of the Eclipse Public License v1.0
5// which accompanies this distribution, and is available at
6// http://www.eclipse.org/legal/epl-v10.html
7///////////////////////////////////////////////////////////////////////////////
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);
257 void encode_text(Text_Buf& text_buf) const;
258 void decode_text(Text_Buf& text_buf);
259
260#ifdef TITAN_RUNTIME_2
261 virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
262 raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
263#endif
264
af710487 265 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
266 int indent, embed_values_enc_struct_t* emb_val) const;
970ed795
EL
267#ifdef TITAN_RUNTIME_2
268 int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
af710487 269 const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
270 int indent, embed_values_enc_struct_t* emb_val) const;
970ed795
EL
271#endif
272 /** Used during XML decoding, in case this object is an AnyElement field in a record.
273 * Determines whether XER_decode() should be called or this field should be omitted.
274 * The field should be omitted if:
275 * - the next element in the encoded XML is the next field in the record or
276 * - there are no more elements until the end of the record's XML element.
277 *
278 * @param reader parses the encoded XML
279 * @param next_field_name name of the next field in the record, or null if this is the last one
280 * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
281 bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
af710487 282 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
283 unsigned int flavor, embed_values_dec_struct_t* emb_val);
970ed795
EL
284
285 char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;
286
287 operator T_type&();
288 operator const T_type&() const;
289
290 inline T_type& operator()() { return (T_type&)*this; }
291 inline const T_type& operator()() const { return (const T_type&)*this; }
292
293 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
294 unsigned p_coding) const;
295#ifdef TITAN_RUNTIME_2
296 ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
297 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
298#endif
299 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
300 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
301 boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
302 const ASN_BER_TLV_t& p_tlv);
303 void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);
304
305#ifdef TITAN_RUNTIME_2
306 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
307 int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
308 const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
309 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
310 boolean no_err=FALSE, boolean first_call=TRUE);
311#endif
312
313 /** Encodes accordingly to the JSON encoding rules.
314 * Returns the length of the encoded data. */
315 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
316
317 /** Decodes accordingly to the JSON encoding rules.
318 * Returns the length of the decoded data. */
319 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
320
af710487 321#ifdef TITAN_RUNTIME_2
970ed795 322 /** Called before an element of an optional record of/set of is indexed and passed as an
af710487 323 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795
EL
324 * Sets the optional value to present (this would be done by the indexing operation
325 * anyway) and redirects the call to the optional value. */
a38c6d4c 326 virtual void add_refd_index(int index);
970ed795
EL
327
328 /** Called after an element of an optional record of/set of is passed as an
af710487 329 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795 330 * Redirects the call to the optional value. */
a38c6d4c 331 virtual void remove_refd_index(int index);
af710487 332#endif
970ed795
EL
333};
334
335#if HAVE_GCC(4,6)
336#pragma GCC diagnostic push
337#pragma GCC diagnostic ignored "-Wswitch-enum"
338#endif
339
340#ifdef TITAN_RUNTIME_2
341
342template<typename T_type>
343Base_Type* OPTIONAL<T_type>::get_opt_value()
344{
af710487 345#ifdef TITAN_RUNTIME_2
970ed795 346 if (!is_present())
af710487 347#else
348 if (optional_selection!=OPTIONAL_PRESENT)
349#endif
970ed795
EL
350 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
351 return optional_value;
352}
353
354template<typename T_type>
355const Base_Type* OPTIONAL<T_type>::get_opt_value() const
356{
af710487 357#ifdef TITAN_RUNTIME_2
970ed795 358 if (!is_present())
af710487 359#else
360 if (optional_selection!=OPTIONAL_PRESENT)
361#endif
970ed795
EL
362 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
363 return optional_value;
364}
365
366template<typename T_type>
367boolean OPTIONAL<T_type>::is_seof() const
368{
af710487 369 return
370#ifdef TITAN_RUNTIME_2
371 (is_present())
372#else
373 (optional_selection==OPTIONAL_PRESENT)
374#endif
375 ? optional_value->is_seof() : T_type().is_seof();
970ed795
EL
376}
377
378template<typename T_type>
379const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
380{
af710487 381 return
382#ifdef TITAN_RUNTIME_2
383 (is_present())
384#else
385 (optional_selection==OPTIONAL_PRESENT)
386#endif
387 ? optional_value->get_descriptor() : T_type().get_descriptor();
970ed795
EL
388}
389
390#endif
391
392template<typename T_type>
393OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
af710487 394 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
395#ifdef TITAN_RUNTIME_2
396 , param_refs(0)
397#endif
970ed795
EL
398{
399 if (other_value != OMIT_VALUE)
400 TTCN_error("Setting an optional field to an invalid value.");
401}
402
403template<typename T_type>
404OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
405 : Base_Type(other_value)
970ed795 406 , optional_value(NULL)
af710487 407 , optional_selection(other_value.optional_selection)
408#ifdef TITAN_RUNTIME_2
970ed795 409 , param_refs(0)
af710487 410#endif
970ed795
EL
411{
412 switch (other_value.optional_selection) {
413 case OPTIONAL_PRESENT:
414 optional_value = new T_type(*other_value.optional_value);
415 break;
416 case OPTIONAL_OMIT:
417 break;
418 default:
419 break;
420 }
421}
422
423template<typename T_type> template<typename T_tmp>
424OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
af710487 425 : optional_value(NULL), optional_selection(other_value.get_selection())
426#ifdef TITAN_RUNTIME_2
427 , param_refs(0)
428#endif
970ed795
EL
429{
430 switch (other_value.get_selection()) {
431 case OPTIONAL_PRESENT:
432 optional_value = new T_type((const T_tmp&)other_value);
433 break;
434 case OPTIONAL_OMIT:
435 break;
436 default:
437 break;
438 }
439}
440
441template<typename T_type>
442void OPTIONAL<T_type>::clean_up()
443{
af710487 444#ifdef TITAN_RUNTIME_2
970ed795
EL
445 if (is_present()) {
446 if (param_refs > 0) {
447 optional_value->clean_up();
448 }
449 else {
450 delete optional_value;
451 optional_value = NULL;
452 }
453 }
af710487 454#else
455 if (OPTIONAL_PRESENT == optional_selection) {
456 delete optional_value;
457 }
458#endif
970ed795
EL
459 optional_selection = OPTIONAL_UNBOUND;
460}
461
462template<typename T_type>
463OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
464{
465 if (other_value != OMIT_VALUE)
466 TTCN_error("Internal error: Setting an optional field to an invalid value.");
467 set_to_omit();
468 return *this;
469}
470
471template<typename T_type>
472OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
473{
474 switch (other_value.optional_selection) {
475 case OPTIONAL_PRESENT:
af710487 476#ifdef TITAN_RUNTIME_2
970ed795 477 if (NULL == optional_value) {
af710487 478#else
479 if (optional_selection != OPTIONAL_PRESENT) {
480#endif
970ed795 481 optional_value = new T_type(*other_value.optional_value);
af710487 482 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
483 } else *optional_value = *other_value.optional_value;
484 break;
485 case OPTIONAL_OMIT:
486 if (&other_value != this) set_to_omit();
487 break;
488 default:
489 clean_up();
490 break;
491 }
492 return *this;
493}
494
495template<typename T_type> template <typename T_tmp>
496OPTIONAL<T_type>&
497OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
498{
499 switch (other_value.get_selection()) {
500 case OPTIONAL_PRESENT:
af710487 501#ifdef TITAN_RUNTIME_2
970ed795 502 if (NULL == optional_value) {
af710487 503#else
504 if (optional_selection != OPTIONAL_PRESENT) {
505#endif
970ed795 506 optional_value = new T_type((const T_tmp&)other_value);
af710487 507 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
508 } else *optional_value = (const T_tmp&)other_value;
509 break;
510 case OPTIONAL_OMIT:
511 set_to_omit();
512 break;
513 default:
514 clean_up();
515 break;
516 }
517 return *this;
518}
519
520template<typename T_type> template <typename T_tmp>
521OPTIONAL<T_type>&
522OPTIONAL<T_type>::operator=(const T_tmp& other_value)
523{
af710487 524#ifdef TITAN_RUNTIME_2
970ed795 525 if (NULL == optional_value) {
af710487 526#else
527 if (optional_selection != OPTIONAL_PRESENT) {
528#endif
970ed795 529 optional_value = new T_type(other_value);
af710487 530 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
531 } else *optional_value = other_value;
532 return *this;
533}
534
535template<typename T_type>
536boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
537{
af710487 538#ifdef TITAN_RUNTIME_2
970ed795 539 if (!is_bound()) {
af710487 540#else
541 if (optional_selection == OPTIONAL_UNBOUND) {
542#endif
970ed795
EL
543 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
544 TTCN_error("The left operand of comparison is an unbound optional value.");
545 }
546 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
547 "of comparison is an invalid value.");
af710487 548 return
549#ifdef TITAN_RUNTIME_2
550 !is_present();
551#else
552 optional_selection == OPTIONAL_OMIT;
553#endif
970ed795
EL
554}
555
556template<typename T_type>
557boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
558{
af710487 559#ifdef TITAN_RUNTIME_2
970ed795 560 if (!is_bound()) {
af710487 561 if (!other_value.is_bound())
562#else
563 if (optional_selection == OPTIONAL_UNBOUND) {
564 if (other_value.optional_selection == OPTIONAL_UNBOUND)
565#endif
566 return TRUE;
970ed795
EL
567 TTCN_error("The left operand of "
568 "comparison is an unbound optional value.");
569 }
af710487 570#ifdef TITAN_RUNTIME_2
970ed795 571 if (!other_value.is_bound())
af710487 572#else
573 if (other_value.optional_selection == OPTIONAL_UNBOUND)
574#endif
970ed795 575 TTCN_error("The right operand of comparison is an unbound optional value.");
af710487 576#ifdef TITAN_RUNTIME_2
970ed795
EL
577 boolean present = is_present();
578 if (present != other_value.is_present()) return FALSE;
579 else if (present)
af710487 580#else
581 if (optional_selection != other_value.optional_selection) return FALSE;
582 else if (optional_selection == OPTIONAL_PRESENT)
583#endif
970ed795
EL
584 return *optional_value == *other_value.optional_value;
585 else return TRUE;
586}
587
588template<typename T_type> template <typename T_tmp>
589boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
590{
af710487 591#ifdef TITAN_RUNTIME_2
970ed795 592 switch (get_selection()) {
af710487 593#else
594 switch (optional_selection) {
595#endif
970ed795
EL
596 case OPTIONAL_PRESENT:
597 return *optional_value == other_value;
598 case OPTIONAL_OMIT:
599 return FALSE;
600 default:
601 TTCN_error("The left operand of comparison is an unbound optional value.");
602 }
603 return FALSE;
604}
605
606template<typename T_type> template <typename T_tmp>
607boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
608{
af710487 609#ifdef TITAN_RUNTIME_2
970ed795 610 if (!is_bound()) {
af710487 611 if (!other_value.is_bound())
612#else
613 optional_sel other_selection = other_value.get_selection();
614 if (optional_selection == OPTIONAL_UNBOUND) {
615 if (other_selection == OPTIONAL_UNBOUND)
616#endif
617 return TRUE;
970ed795
EL
618 TTCN_error("The left operand of "
619 "comparison is an unbound optional value.");
620 }
af710487 621#ifdef TITAN_RUNTIME_2
622 if (!other_value.is_bound())
623#else
624 if (other_selection == OPTIONAL_UNBOUND)
625#endif
626 TTCN_error("The right operand of comparison is an unbound optional value.");
627#ifdef TITAN_RUNTIME_2
970ed795
EL
628 boolean present = is_present();
629 if (present != other_value.is_present()) return FALSE;
630 else if (present)
af710487 631#else
632 if (optional_selection != other_selection) return FALSE;
633 else if (optional_selection == OPTIONAL_PRESENT)
634#endif
970ed795
EL
635 return *optional_value == (const T_tmp&)other_value;
636 else return TRUE;
637}
638
af710487 639#ifdef TITAN_RUNTIME_2
970ed795
EL
640template<typename T_type>
641boolean OPTIONAL<T_type>::is_bound() const
642{
643 switch (optional_selection) {
644 case OPTIONAL_PRESENT:
645 case OPTIONAL_OMIT:
646 return TRUE;
647 default:
648 if (NULL != optional_value) {
649 return optional_value->is_bound();
650 }
651 return FALSE;
652 }
653}
654
655template<typename T_type>
656boolean OPTIONAL<T_type>::is_present() const
657{
658 switch (optional_selection) {
659 case OPTIONAL_PRESENT:
660 return TRUE;
661 case OPTIONAL_OMIT:
662 default:
663 if (NULL != optional_value) {
664 return optional_value->is_bound();
665 }
666 return FALSE;
667 }
668}
af710487 669#endif
970ed795
EL
670
671template<typename T_type>
672boolean OPTIONAL<T_type>::ispresent() const
673{
674 switch (optional_selection) {
675 case OPTIONAL_PRESENT:
676 return TRUE;
677 case OPTIONAL_OMIT:
af710487 678#ifdef TITAN_RUNTIME_2
970ed795
EL
679 if (NULL != optional_value) {
680 return optional_value->is_bound();
681 }
af710487 682#endif
970ed795
EL
683 return FALSE;
684 default:
af710487 685#ifdef TITAN_RUNTIME_2
970ed795
EL
686 if (NULL != optional_value && optional_value->is_bound()) {
687 return TRUE;
688 }
af710487 689#endif
970ed795 690 TTCN_error("Using an unbound optional field.");
970ed795 691 }
af710487 692 return FALSE;
970ed795
EL
693}
694
af710487 695#ifdef TITAN_RUNTIME_2
970ed795
EL
696template<typename T_type>
697optional_sel OPTIONAL<T_type>::get_selection() const
698{
699 if (is_present()) {
700 return OPTIONAL_PRESENT;
701 }
702 if (is_bound()) {
703 // not present, but bound => omit
704 return OPTIONAL_OMIT;
705 }
706 return OPTIONAL_UNBOUND;
707}
af710487 708#endif
970ed795
EL
709
710template<typename T_type>
711void OPTIONAL<T_type>::log() const
712{
af710487 713#ifdef TITAN_RUNTIME_2
970ed795 714 switch (get_selection()) {
af710487 715#else
716 switch (optional_selection) {
717#endif
970ed795
EL
718 case OPTIONAL_PRESENT:
719 optional_value->log();
720 break;
721 case OPTIONAL_OMIT:
722 TTCN_Logger::log_event_str("omit");
723 break;
724 default:
725 TTCN_Logger::log_event_unbound();
726 break;
727 }
728}
729
730template <typename T_type>
731void OPTIONAL<T_type>::set_param(Module_Param& param) {
732 if (param.get_type()==Module_Param::MP_Omit) {
733 if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
734 if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
735 set_to_omit();
736 return;
737 }
738 set_to_present();
739 optional_value->set_param(param);
740}
741
742template<typename T_type>
743void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
744{
af710487 745#ifdef TITAN_RUNTIME_2
970ed795 746 switch (get_selection()) {
af710487 747#else
748 switch (optional_selection) {
749#endif
970ed795
EL
750 case OPTIONAL_OMIT:
751 text_buf.push_int((RInt)FALSE);
752 break;
753 case OPTIONAL_PRESENT:
754 text_buf.push_int((RInt)TRUE);
755 optional_value->encode_text(text_buf);
756 break;
757 default:
758 TTCN_error("Text encoder: Encoding an unbound optional value.");
759 }
760}
761
762template<typename T_type>
763void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
764{
765 if (text_buf.pull_int().get_val()) {
766 set_to_present();
767 optional_value->decode_text(text_buf);
768 } else set_to_omit();
769}
770
771template<typename T_type>
772int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
773{
af710487 774#ifdef TITAN_RUNTIME_2
970ed795 775 switch(get_selection()) {
af710487 776#else
777 switch(optional_selection) {
778#endif
970ed795
EL
779 case OPTIONAL_PRESENT:
780 return optional_value->JSON_encode(p_td, p_tok);
781 case OPTIONAL_OMIT:
782 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
783 default:
784 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
785 "Encoding an unbound optional value.");
786 return -1;
787 }
788}
789
790template<typename T_type>
791int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
792{
af710487 793 // try the optional value first
794 set_to_present();
970ed795 795 size_t buf_pos = p_tok.get_buf_pos();
af710487 796 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
797 if (JSON_ERROR_FATAL == dec_len) {
798 if (p_silent) {
799 clean_up();
800 } else {
801 set_to_omit();
802 }
970ed795 803 }
af710487 804 else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
805 // invalid token, rewind the buffer and check if it's a "null" (= omit)
806 // this needs to be checked after the optional value, because it might also be
807 // able to decode a "null" value
970ed795 808 p_tok.set_buf_pos(buf_pos);
af710487 809 json_token_t token = JSON_TOKEN_NONE;
810 dec_len = p_tok.get_next_token(&token, NULL, NULL);
811 if (JSON_TOKEN_LITERAL_NULL == token) {
812 set_to_omit();
813 }
814 else {
815 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
816 dec_len = JSON_ERROR_INVALID_TOKEN;
970ed795 817 }
970ed795
EL
818 }
819 return dec_len;
820}
821
af710487 822#ifdef TITAN_RUNTIME_2
970ed795
EL
823template<typename T_type>
824void OPTIONAL<T_type>::add_refd_index(int index)
825{
826 ++param_refs;
827 set_to_present();
a38c6d4c 828 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
829 if (0 != refd_opt_val) {
830 refd_opt_val->add_refd_index(index);
831 }
970ed795
EL
832}
833
834template<typename T_type>
835void OPTIONAL<T_type>::remove_refd_index(int index)
836{
837 --param_refs;
a38c6d4c 838 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
839 if (0 != refd_opt_val) {
840 refd_opt_val->remove_refd_index(index);
841 }
970ed795 842}
af710487 843#endif
509718e0 844
970ed795
EL
845template<typename T_type>
846OPTIONAL<T_type>::operator T_type&()
847{
848 set_to_present();
849 return *optional_value;
850}
851
852template<typename T_type>
853OPTIONAL<T_type>::operator const T_type&() const
854{
af710487 855#ifdef TITAN_RUNTIME_2
970ed795 856 if (!is_present())
af710487 857#else
858 if (optional_selection != OPTIONAL_PRESENT)
859#endif
970ed795
EL
860 TTCN_error("Using the value of an optional field containing omit.");
861 return *optional_value;
862}
863
864template<typename T_type>
865ASN_BER_TLV_t*
866OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
867 unsigned p_coding) const
868{
869 BER_chk_descr(p_td);
af710487 870#ifdef TITAN_RUNTIME_2
970ed795 871 switch (get_selection()) {
af710487 872#else
873 switch (optional_selection) {
874#endif
970ed795
EL
875 case OPTIONAL_PRESENT:
876 return optional_value->BER_encode_TLV(p_td, p_coding);
877 case OPTIONAL_OMIT:
878 return ASN_BER_TLV_t::construct();
879 default:
880 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
881 }
882}
883
884#ifdef TITAN_RUNTIME_2
885template<typename T_type>
886ASN_BER_TLV_t*
887OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
888 const TTCN_Typedescriptor_t& p_td, 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_negtest(p_err_descr, 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
905template<typename T_type>
906int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
907 TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
908 boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
909{
910 TTCN_error("RAW decoding requested for optional type '%s'"
911 " which has no RAW decoding method.",p_td.name);
912 return 0;
913}
914#endif
915
916template<typename T_type>
917int
af710487 918OPTIONAL<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 919{
af710487 920#ifdef TITAN_RUNTIME_2
970ed795 921 switch (get_selection()) {
af710487 922#else
923 switch (optional_selection) {
924#endif
970ed795 925 case OPTIONAL_PRESENT:
af710487 926 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
970ed795
EL
927 case OPTIONAL_OMIT:
928 return 0; // nothing to do !
929 default:
930 TTCN_EncDec_ErrorContext::error(
931 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
932 return 0;
933 }
934}
935
936#ifdef TITAN_RUNTIME_2
937template<typename T_type>
938int
939OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
af710487 940 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
941 embed_values_enc_struct_t* emb_val) const
970ed795
EL
942{
943 switch (get_selection()) {
944 case OPTIONAL_PRESENT:
af710487 945 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
970ed795
EL
946 case OPTIONAL_OMIT:
947 return 0; // nothing to do !
948 default:
949 TTCN_EncDec_ErrorContext::error(
950 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
951 return 0;
952 }
953}
954#endif
955
956
957template<typename T_type>
958bool
959OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
960{
961 // If the record has no elements, then it can't have an AnyElement
962 if (parent_tag_closed) {
963 set_to_omit();
964 return false;
965 }
966
967 while (reader.Ok()) {
968 // Leaving the record before finding an element -> no AnyElement
969 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
970 set_to_omit();
971 return false;
972 }
973 if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
974 // The first element found is either the next field's element or the AnyElement
975 if (NULL != next_field_name &&
976 0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
977 set_to_omit();
978 return false;
979 }
980 break;
981 }
982 reader.Read();
983 }
984
985 return true;
986}
987
988template<typename T_type>
989int
af710487 990OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
991 unsigned int flavor, embed_values_dec_struct_t* emb_val)
970ed795
EL
992{
993 int exer = is_exer(flavor);
994 for (int success = reader.Ok(); success==1; success=reader.Read()) {
995 int type = reader.NodeType();
996 const char * name; // name of the optional field
997 const char * ns_uri;
998
999 if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
1000 if (XML_READER_TYPE_ATTRIBUTE == type) {
1001 for (; success==1; success = reader.MoveToNextAttribute()) {
1002 if (!reader.IsNamespaceDecl()) break;
1003 }
1004
1005 name = (const char*)reader.LocalName();
1006 if (!check_name(name, p_td, exer)) { // it's not us, bail
1007 break;
1008 }
1009 // we already checked for exer==1
1010 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1011
a38c6d4c 1012 // set to omit if the attribute is empty
1013 const char * value = (const char *)reader.Value();
1014 if (strlen(value) == 0) {
1015 break;
1016 }
1017
970ed795 1018 set_to_present();
af710487 1019 optional_value->XER_decode(p_td, reader, flavor, emb_val);
970ed795
EL
1020 goto finished;
1021 }
1022 else break;
1023 }
1024 else { // not attribute
1025 if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
1026 name = (const char*)reader.LocalName();
1027 ns_uri = (const char*)reader.NamespaceUri();
1028 if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
1029 || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
1030 // If the optional field (a string) has anyElement, accept the element
1031 // regardless of its name. Else the name (and namespace) must match.
1032 || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
1033 found_it:
1034 set_to_present();
1035 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
af710487 1036 optional_value->XER_decode(p_td, reader, flavor, emb_val);
970ed795
EL
1037 }
1038 else break; // it's not us, bail
1039
1040 goto finished;
1041 }
1042 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1043 goto found_it;
1044 }
1045 else if (XML_READER_TYPE_END_ELEMENT == type) {
1046 break;
1047 }
1048 // else circle around
1049 } // if attribute
1050 } // next
1051 set_to_omit();
1052 return 0;
1053finished:
1054 return 1;
1055}
1056
1057template<typename T_type>
1058char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
af710487 1059#ifdef TITAN_RUNTIME_2
970ed795 1060 switch (get_selection()) {
af710487 1061#else
1062 switch (optional_selection) {
1063#endif
970ed795
EL
1064 case OPTIONAL_PRESENT:
1065 return optional_value->collect_ns(p_td, num, def_ns);
1066 case OPTIONAL_OMIT:
1067 def_ns = false;
1068 num = 0;
1069 return 0;
1070 default:
1071 TTCN_EncDec_ErrorContext::error(
1072 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1073 return 0;
1074 }
1075}
1076
1077template<typename T_type>
1078boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1079 const ASN_BER_TLV_t& p_tlv,
1080 unsigned L_form)
1081{
1082 BER_chk_descr(p_td);
1083 if (BER_decode_isMyMsg(p_td, p_tlv)) {
1084 return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
1085 } else {
1086 set_to_omit();
1087 return TRUE;
1088 }
1089}
1090
1091template<typename T_type>
1092boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1093 const ASN_BER_TLV_t& p_tlv)
1094{
1095 set_to_present();
1096 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1097}
1098
1099template<typename T_type>
1100void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1101 unsigned L_form)
1102{
af710487 1103#ifdef TITAN_RUNTIME_2
970ed795
EL
1104 if (is_present()) {
1105 optional_selection = OPTIONAL_PRESENT;
af710487 1106#else
1107 if (optional_selection==OPTIONAL_PRESENT) {
1108#endif
970ed795
EL
1109 optional_value->BER_decode_opentypes(p_typelist, L_form);
1110 }
1111}
1112
1113#ifdef TITAN_RUNTIME_2
1114
1115template<typename T_type>
1116int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1117 TTCN_Buffer& buff) const
1118{
af710487 1119 if (is_present())
970ed795
EL
1120 return optional_value->TEXT_encode(p_td, buff);
1121 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1122 return 0;
1123}
1124
1125template<typename T_type>
1126int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1127 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1128{
af710487 1129 if (is_present())
970ed795
EL
1130 return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
1131 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1132 return 0;
1133}
1134
1135template<typename T_type>
1136int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
1137 TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
1138{
1139 set_to_present();
1140 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1141}
1142
1143#endif
1144
1145#if defined(__GNUC__) && __GNUC__ >= 3
1146/** Note: These functions allow most efficient operation by passing the left
1147 * operand OMIT_VALUE as value instead of constant reference.
1148 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
1149template<typename T_type>
1150inline boolean operator==(template_sel left_value,
1151 const OPTIONAL<T_type>& right_value)
1152 { return right_value.is_equal(left_value); }
1153
1154template<typename T_type>
1155inline boolean operator!=(template_sel left_value,
1156 const OPTIONAL<T_type>& right_value)
1157 { return !right_value.is_equal(left_value); }
1158#endif
1159
1160template<typename T_left, typename T_right>
1161inline boolean operator==(const T_left& left_value,
1162 const OPTIONAL<T_right>& right_value)
1163 { return right_value.is_equal(left_value); }
1164
1165template<typename T_left, typename T_right>
1166inline boolean operator!=(const T_left& left_value,
1167 const OPTIONAL<T_right>& right_value)
1168 { return !right_value.is_equal(left_value); }
1169
1170#endif
1171
1172#if HAVE_GCC(4,6)
1173#pragma GCC diagnostic pop
1174#endif
This page took 0.072461 seconds and 5 git commands to generate.