Sync with 5.4.2
[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);
3f84031e 312 int JSON_encode_negtest(const Erroneous_descriptor_t*,
313 const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
970ed795
EL
314#endif
315
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;
319
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);
323
af710487 324#ifdef TITAN_RUNTIME_2
970ed795 325 /** Called before an element of an optional record of/set of is indexed and passed as an
af710487 326 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795
EL
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. */
a38c6d4c 329 virtual void add_refd_index(int index);
970ed795
EL
330
331 /** Called after an element of an optional record of/set of is passed as an
af710487 332 * 'inout' or 'out' parameter to a function (only in Runtime2).
970ed795 333 * Redirects the call to the optional value. */
a38c6d4c 334 virtual void remove_refd_index(int index);
af710487 335#endif
970ed795
EL
336};
337
338#if HAVE_GCC(4,6)
339#pragma GCC diagnostic push
340#pragma GCC diagnostic ignored "-Wswitch-enum"
341#endif
342
343#ifdef TITAN_RUNTIME_2
344
345template<typename T_type>
346Base_Type* OPTIONAL<T_type>::get_opt_value()
347{
af710487 348#ifdef TITAN_RUNTIME_2
970ed795 349 if (!is_present())
af710487 350#else
351 if (optional_selection!=OPTIONAL_PRESENT)
352#endif
970ed795
EL
353 TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
354 return optional_value;
355}
356
357template<typename T_type>
358const Base_Type* OPTIONAL<T_type>::get_opt_value() const
359{
af710487 360#ifdef TITAN_RUNTIME_2
970ed795 361 if (!is_present())
af710487 362#else
363 if (optional_selection!=OPTIONAL_PRESENT)
364#endif
970ed795
EL
365 TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
366 return optional_value;
367}
368
369template<typename T_type>
370boolean OPTIONAL<T_type>::is_seof() const
371{
af710487 372 return
373#ifdef TITAN_RUNTIME_2
374 (is_present())
375#else
376 (optional_selection==OPTIONAL_PRESENT)
377#endif
378 ? optional_value->is_seof() : T_type().is_seof();
970ed795
EL
379}
380
381template<typename T_type>
382const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
383{
af710487 384 return
385#ifdef TITAN_RUNTIME_2
386 (is_present())
387#else
388 (optional_selection==OPTIONAL_PRESENT)
389#endif
390 ? optional_value->get_descriptor() : T_type().get_descriptor();
970ed795
EL
391}
392
393#endif
394
395template<typename T_type>
396OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
af710487 397 : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
398#ifdef TITAN_RUNTIME_2
399 , param_refs(0)
400#endif
970ed795
EL
401{
402 if (other_value != OMIT_VALUE)
403 TTCN_error("Setting an optional field to an invalid value.");
404}
405
406template<typename T_type>
407OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
408 : Base_Type(other_value)
3abe9331 409#ifdef TITAN_RUNTIME_2
410 , RefdIndexInterface(other_value)
411#endif
970ed795 412 , optional_value(NULL)
af710487 413 , optional_selection(other_value.optional_selection)
414#ifdef TITAN_RUNTIME_2
970ed795 415 , param_refs(0)
af710487 416#endif
970ed795
EL
417{
418 switch (other_value.optional_selection) {
419 case OPTIONAL_PRESENT:
420 optional_value = new T_type(*other_value.optional_value);
421 break;
422 case OPTIONAL_OMIT:
423 break;
424 default:
425 break;
426 }
427}
428
429template<typename T_type> template<typename T_tmp>
430OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
af710487 431 : optional_value(NULL), optional_selection(other_value.get_selection())
432#ifdef TITAN_RUNTIME_2
433 , param_refs(0)
434#endif
970ed795
EL
435{
436 switch (other_value.get_selection()) {
437 case OPTIONAL_PRESENT:
438 optional_value = new T_type((const T_tmp&)other_value);
439 break;
440 case OPTIONAL_OMIT:
441 break;
442 default:
443 break;
444 }
445}
446
447template<typename T_type>
448void OPTIONAL<T_type>::clean_up()
449{
af710487 450#ifdef TITAN_RUNTIME_2
970ed795
EL
451 if (is_present()) {
452 if (param_refs > 0) {
453 optional_value->clean_up();
454 }
455 else {
456 delete optional_value;
457 optional_value = NULL;
458 }
459 }
af710487 460#else
461 if (OPTIONAL_PRESENT == optional_selection) {
462 delete optional_value;
463 }
464#endif
970ed795
EL
465 optional_selection = OPTIONAL_UNBOUND;
466}
467
468template<typename T_type>
469OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
470{
471 if (other_value != OMIT_VALUE)
472 TTCN_error("Internal error: Setting an optional field to an invalid value.");
473 set_to_omit();
474 return *this;
475}
476
477template<typename T_type>
478OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
479{
480 switch (other_value.optional_selection) {
481 case OPTIONAL_PRESENT:
af710487 482#ifdef TITAN_RUNTIME_2
970ed795 483 if (NULL == optional_value) {
af710487 484#else
485 if (optional_selection != OPTIONAL_PRESENT) {
486#endif
970ed795 487 optional_value = new T_type(*other_value.optional_value);
af710487 488 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
489 } else *optional_value = *other_value.optional_value;
490 break;
491 case OPTIONAL_OMIT:
492 if (&other_value != this) set_to_omit();
493 break;
494 default:
495 clean_up();
496 break;
497 }
498 return *this;
499}
500
501template<typename T_type> template <typename T_tmp>
502OPTIONAL<T_type>&
503OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
504{
505 switch (other_value.get_selection()) {
506 case OPTIONAL_PRESENT:
af710487 507#ifdef TITAN_RUNTIME_2
970ed795 508 if (NULL == optional_value) {
af710487 509#else
510 if (optional_selection != OPTIONAL_PRESENT) {
511#endif
970ed795 512 optional_value = new T_type((const T_tmp&)other_value);
af710487 513 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
514 } else *optional_value = (const T_tmp&)other_value;
515 break;
516 case OPTIONAL_OMIT:
517 set_to_omit();
518 break;
519 default:
520 clean_up();
521 break;
522 }
523 return *this;
524}
525
526template<typename T_type> template <typename T_tmp>
527OPTIONAL<T_type>&
528OPTIONAL<T_type>::operator=(const T_tmp& other_value)
529{
af710487 530#ifdef TITAN_RUNTIME_2
970ed795 531 if (NULL == optional_value) {
af710487 532#else
533 if (optional_selection != OPTIONAL_PRESENT) {
534#endif
970ed795 535 optional_value = new T_type(other_value);
af710487 536 optional_selection = OPTIONAL_PRESENT;
970ed795
EL
537 } else *optional_value = other_value;
538 return *this;
539}
540
541template<typename T_type>
542boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
543{
af710487 544#ifdef TITAN_RUNTIME_2
970ed795 545 if (!is_bound()) {
af710487 546#else
547 if (optional_selection == OPTIONAL_UNBOUND) {
548#endif
970ed795
EL
549 if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
550 TTCN_error("The left operand of comparison is an unbound optional value.");
551 }
552 if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
553 "of comparison is an invalid value.");
af710487 554 return
555#ifdef TITAN_RUNTIME_2
556 !is_present();
557#else
558 optional_selection == OPTIONAL_OMIT;
559#endif
970ed795
EL
560}
561
562template<typename T_type>
563boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
564{
af710487 565#ifdef TITAN_RUNTIME_2
970ed795 566 if (!is_bound()) {
af710487 567 if (!other_value.is_bound())
568#else
569 if (optional_selection == OPTIONAL_UNBOUND) {
570 if (other_value.optional_selection == OPTIONAL_UNBOUND)
571#endif
572 return TRUE;
970ed795
EL
573 TTCN_error("The left operand of "
574 "comparison is an unbound optional value.");
575 }
af710487 576#ifdef TITAN_RUNTIME_2
970ed795 577 if (!other_value.is_bound())
af710487 578#else
579 if (other_value.optional_selection == OPTIONAL_UNBOUND)
580#endif
970ed795 581 TTCN_error("The right operand of comparison is an unbound optional value.");
af710487 582#ifdef TITAN_RUNTIME_2
970ed795
EL
583 boolean present = is_present();
584 if (present != other_value.is_present()) return FALSE;
585 else if (present)
af710487 586#else
587 if (optional_selection != other_value.optional_selection) return FALSE;
588 else if (optional_selection == OPTIONAL_PRESENT)
589#endif
970ed795
EL
590 return *optional_value == *other_value.optional_value;
591 else return TRUE;
592}
593
594template<typename T_type> template <typename T_tmp>
595boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
596{
af710487 597#ifdef TITAN_RUNTIME_2
970ed795 598 switch (get_selection()) {
af710487 599#else
600 switch (optional_selection) {
601#endif
970ed795
EL
602 case OPTIONAL_PRESENT:
603 return *optional_value == other_value;
604 case OPTIONAL_OMIT:
605 return FALSE;
606 default:
607 TTCN_error("The left operand of comparison is an unbound optional value.");
608 }
609 return FALSE;
610}
611
612template<typename T_type> template <typename T_tmp>
613boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
614{
af710487 615#ifdef TITAN_RUNTIME_2
970ed795 616 if (!is_bound()) {
af710487 617 if (!other_value.is_bound())
618#else
619 optional_sel other_selection = other_value.get_selection();
620 if (optional_selection == OPTIONAL_UNBOUND) {
621 if (other_selection == OPTIONAL_UNBOUND)
622#endif
623 return TRUE;
970ed795
EL
624 TTCN_error("The left operand of "
625 "comparison is an unbound optional value.");
626 }
af710487 627#ifdef TITAN_RUNTIME_2
628 if (!other_value.is_bound())
629#else
630 if (other_selection == OPTIONAL_UNBOUND)
631#endif
632 TTCN_error("The right operand of comparison is an unbound optional value.");
633#ifdef TITAN_RUNTIME_2
970ed795
EL
634 boolean present = is_present();
635 if (present != other_value.is_present()) return FALSE;
636 else if (present)
af710487 637#else
638 if (optional_selection != other_selection) return FALSE;
639 else if (optional_selection == OPTIONAL_PRESENT)
640#endif
970ed795
EL
641 return *optional_value == (const T_tmp&)other_value;
642 else return TRUE;
643}
644
af710487 645#ifdef TITAN_RUNTIME_2
970ed795
EL
646template<typename T_type>
647boolean OPTIONAL<T_type>::is_bound() const
648{
649 switch (optional_selection) {
650 case OPTIONAL_PRESENT:
651 case OPTIONAL_OMIT:
652 return TRUE;
653 default:
654 if (NULL != optional_value) {
655 return optional_value->is_bound();
656 }
657 return FALSE;
658 }
659}
660
661template<typename T_type>
662boolean OPTIONAL<T_type>::is_present() const
663{
664 switch (optional_selection) {
665 case OPTIONAL_PRESENT:
666 return TRUE;
667 case OPTIONAL_OMIT:
668 default:
669 if (NULL != optional_value) {
670 return optional_value->is_bound();
671 }
672 return FALSE;
673 }
674}
af710487 675#endif
970ed795
EL
676
677template<typename T_type>
678boolean OPTIONAL<T_type>::ispresent() const
679{
680 switch (optional_selection) {
681 case OPTIONAL_PRESENT:
682 return TRUE;
683 case OPTIONAL_OMIT:
af710487 684#ifdef TITAN_RUNTIME_2
970ed795
EL
685 if (NULL != optional_value) {
686 return optional_value->is_bound();
687 }
af710487 688#endif
970ed795
EL
689 return FALSE;
690 default:
af710487 691#ifdef TITAN_RUNTIME_2
970ed795
EL
692 if (NULL != optional_value && optional_value->is_bound()) {
693 return TRUE;
694 }
af710487 695#endif
970ed795 696 TTCN_error("Using an unbound optional field.");
970ed795 697 }
af710487 698 return FALSE;
970ed795
EL
699}
700
af710487 701#ifdef TITAN_RUNTIME_2
970ed795
EL
702template<typename T_type>
703optional_sel OPTIONAL<T_type>::get_selection() const
704{
705 if (is_present()) {
706 return OPTIONAL_PRESENT;
707 }
708 if (is_bound()) {
709 // not present, but bound => omit
710 return OPTIONAL_OMIT;
711 }
712 return OPTIONAL_UNBOUND;
713}
af710487 714#endif
970ed795
EL
715
716template<typename T_type>
717void OPTIONAL<T_type>::log() const
718{
af710487 719#ifdef TITAN_RUNTIME_2
970ed795 720 switch (get_selection()) {
af710487 721#else
722 switch (optional_selection) {
723#endif
970ed795
EL
724 case OPTIONAL_PRESENT:
725 optional_value->log();
726 break;
727 case OPTIONAL_OMIT:
728 TTCN_Logger::log_event_str("omit");
729 break;
730 default:
731 TTCN_Logger::log_event_unbound();
732 break;
733 }
734}
735
736template <typename T_type>
737void 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");
741 set_to_omit();
742 return;
743 }
744 set_to_present();
745 optional_value->set_param(param);
746}
747
3abe9331 748template <typename T_type>
749Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
750{
751#ifdef TITAN_RUNTIME_2
752 switch (get_selection()) {
753#else
754 switch (optional_selection) {
755#endif
756 case OPTIONAL_PRESENT:
757 return optional_value->get_param(param_name);
758 case OPTIONAL_OMIT:
759 return new Module_Param_Omit();
760 default:
761 return new Module_Param_Unbound();
762 }
763}
764
970ed795
EL
765template<typename T_type>
766void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
767{
af710487 768#ifdef TITAN_RUNTIME_2
970ed795 769 switch (get_selection()) {
af710487 770#else
771 switch (optional_selection) {
772#endif
970ed795
EL
773 case OPTIONAL_OMIT:
774 text_buf.push_int((RInt)FALSE);
775 break;
776 case OPTIONAL_PRESENT:
777 text_buf.push_int((RInt)TRUE);
778 optional_value->encode_text(text_buf);
779 break;
780 default:
781 TTCN_error("Text encoder: Encoding an unbound optional value.");
782 }
783}
784
785template<typename T_type>
786void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
787{
788 if (text_buf.pull_int().get_val()) {
789 set_to_present();
790 optional_value->decode_text(text_buf);
791 } else set_to_omit();
792}
793
794template<typename T_type>
795int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
796{
af710487 797#ifdef TITAN_RUNTIME_2
970ed795 798 switch(get_selection()) {
af710487 799#else
800 switch(optional_selection) {
801#endif
970ed795
EL
802 case OPTIONAL_PRESENT:
803 return optional_value->JSON_encode(p_td, p_tok);
804 case OPTIONAL_OMIT:
805 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
806 default:
807 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
808 "Encoding an unbound optional value.");
809 return -1;
810 }
811}
812
3f84031e 813#ifdef TITAN_RUNTIME_2
814template<typename T_type>
815int 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
818{
819 switch (get_selection()) {
820 case OPTIONAL_PRESENT:
821 return optional_value->JSON_encode_negtest(p_err_descr, p_td, p_tok);
822 case OPTIONAL_OMIT:
823 return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
824 default:
825 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
826 "Encoding an unbound optional value.");
827 return -1;
828 }
829}
830#endif
831
970ed795
EL
832template<typename T_type>
833int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
834{
af710487 835 // try the optional value first
836 set_to_present();
970ed795 837 size_t buf_pos = p_tok.get_buf_pos();
af710487 838 int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
839 if (JSON_ERROR_FATAL == dec_len) {
840 if (p_silent) {
841 clean_up();
842 } else {
843 set_to_omit();
844 }
970ed795 845 }
af710487 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
970ed795 850 p_tok.set_buf_pos(buf_pos);
af710487 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) {
854 set_to_omit();
855 }
856 else {
857 // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
858 dec_len = JSON_ERROR_INVALID_TOKEN;
970ed795 859 }
970ed795
EL
860 }
861 return dec_len;
862}
863
af710487 864#ifdef TITAN_RUNTIME_2
970ed795
EL
865template<typename T_type>
866void OPTIONAL<T_type>::add_refd_index(int index)
867{
868 ++param_refs;
869 set_to_present();
a38c6d4c 870 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
871 if (0 != refd_opt_val) {
872 refd_opt_val->add_refd_index(index);
873 }
970ed795
EL
874}
875
876template<typename T_type>
877void OPTIONAL<T_type>::remove_refd_index(int index)
878{
879 --param_refs;
a38c6d4c 880 RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
881 if (0 != refd_opt_val) {
882 refd_opt_val->remove_refd_index(index);
883 }
970ed795 884}
af710487 885#endif
509718e0 886
970ed795
EL
887template<typename T_type>
888OPTIONAL<T_type>::operator T_type&()
889{
890 set_to_present();
891 return *optional_value;
892}
893
894template<typename T_type>
895OPTIONAL<T_type>::operator const T_type&() const
896{
af710487 897#ifdef TITAN_RUNTIME_2
970ed795 898 if (!is_present())
af710487 899#else
900 if (optional_selection != OPTIONAL_PRESENT)
901#endif
970ed795
EL
902 TTCN_error("Using the value of an optional field containing omit.");
903 return *optional_value;
904}
905
906template<typename T_type>
907ASN_BER_TLV_t*
908OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
909 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(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
926#ifdef TITAN_RUNTIME_2
927template<typename T_type>
928ASN_BER_TLV_t*
929OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
930 const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
931{
932 BER_chk_descr(p_td);
af710487 933#ifdef TITAN_RUNTIME_2
970ed795 934 switch (get_selection()) {
af710487 935#else
936 switch (optional_selection) {
937#endif
970ed795
EL
938 case OPTIONAL_PRESENT:
939 return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
940 case OPTIONAL_OMIT:
941 return ASN_BER_TLV_t::construct();
942 default:
943 return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
944 }
945}
946
947template<typename T_type>
948int 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 */ )
951{
952 TTCN_error("RAW decoding requested for optional type '%s'"
953 " which has no RAW decoding method.",p_td.name);
954 return 0;
955}
956#endif
957
958template<typename T_type>
959int
af710487 960OPTIONAL<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 961{
af710487 962#ifdef TITAN_RUNTIME_2
970ed795 963 switch (get_selection()) {
af710487 964#else
965 switch (optional_selection) {
966#endif
970ed795 967 case OPTIONAL_PRESENT:
af710487 968 return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
970ed795
EL
969 case OPTIONAL_OMIT:
970 return 0; // nothing to do !
971 default:
972 TTCN_EncDec_ErrorContext::error(
973 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
974 return 0;
975 }
976}
977
978#ifdef TITAN_RUNTIME_2
979template<typename T_type>
980int
981OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
af710487 982 const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
983 embed_values_enc_struct_t* emb_val) const
970ed795
EL
984{
985 switch (get_selection()) {
986 case OPTIONAL_PRESENT:
af710487 987 return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
970ed795
EL
988 case OPTIONAL_OMIT:
989 return 0; // nothing to do !
990 default:
991 TTCN_EncDec_ErrorContext::error(
992 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
993 return 0;
994 }
995}
996#endif
997
998
999template<typename T_type>
1000bool
1001OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed)
1002{
1003 // If the record has no elements, then it can't have an AnyElement
1004 if (parent_tag_closed) {
1005 set_to_omit();
1006 return false;
1007 }
1008
1009 while (reader.Ok()) {
1010 // Leaving the record before finding an element -> no AnyElement
1011 if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
1012 set_to_omit();
1013 return false;
1014 }
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)) {
1019 set_to_omit();
1020 return false;
1021 }
1022 break;
1023 }
1024 reader.Read();
1025 }
1026
1027 return true;
1028}
1029
1030template<typename T_type>
1031int
af710487 1032OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
1033 unsigned int flavor, embed_values_dec_struct_t* emb_val)
970ed795
EL
1034{
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;
1040
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;
1045 }
1046
1047 name = (const char*)reader.LocalName();
1048 if (!check_name(name, p_td, exer)) { // it's not us, bail
1049 break;
1050 }
1051 // we already checked for exer==1
1052 if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;
1053
a38c6d4c 1054 // set to omit if the attribute is empty
1055 const char * value = (const char *)reader.Value();
1056 if (strlen(value) == 0) {
1057 break;
1058 }
1059
970ed795 1060 set_to_present();
af710487 1061 optional_value->XER_decode(p_td, reader, flavor, emb_val);
970ed795
EL
1062 goto finished;
1063 }
1064 else break;
1065 }
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
1075 found_it:
1076 set_to_present();
1077 //success = reader.Read(); // move to next thing TODO should it loop till an element ?
af710487 1078 optional_value->XER_decode(p_td, reader, flavor, emb_val);
3abe9331 1079 if (!optional_value->is_bound()) {
1080 set_to_omit();
1081 }
970ed795
EL
1082 }
1083 else break; // it's not us, bail
1084
1085 goto finished;
1086 }
1087 else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
1088 goto found_it;
1089 }
1090 else if (XML_READER_TYPE_END_ELEMENT == type) {
1091 break;
1092 }
1093 // else circle around
1094 } // if attribute
1095 } // next
1096 set_to_omit();
1097 return 0;
1098finished:
1099 return 1;
1100}
1101
1102template<typename T_type>
1103char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
af710487 1104#ifdef TITAN_RUNTIME_2
970ed795 1105 switch (get_selection()) {
af710487 1106#else
1107 switch (optional_selection) {
1108#endif
970ed795
EL
1109 case OPTIONAL_PRESENT:
1110 return optional_value->collect_ns(p_td, num, def_ns);
1111 case OPTIONAL_OMIT:
1112 def_ns = false;
1113 num = 0;
1114 return 0;
1115 default:
1116 TTCN_EncDec_ErrorContext::error(
1117 TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
1118 return 0;
1119 }
1120}
1121
1122template<typename T_type>
1123boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
1124 const ASN_BER_TLV_t& p_tlv,
1125 unsigned L_form)
1126{
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);
1130 } else {
1131 set_to_omit();
1132 return TRUE;
1133 }
1134}
1135
1136template<typename T_type>
1137boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
1138 const ASN_BER_TLV_t& p_tlv)
1139{
1140 set_to_present();
1141 return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
1142}
1143
1144template<typename T_type>
1145void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
1146 unsigned L_form)
1147{
af710487 1148#ifdef TITAN_RUNTIME_2
970ed795
EL
1149 if (is_present()) {
1150 optional_selection = OPTIONAL_PRESENT;
af710487 1151#else
1152 if (optional_selection==OPTIONAL_PRESENT) {
1153#endif
970ed795
EL
1154 optional_value->BER_decode_opentypes(p_typelist, L_form);
1155 }
1156}
1157
1158#ifdef TITAN_RUNTIME_2
1159
1160template<typename T_type>
1161int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
1162 TTCN_Buffer& buff) const
1163{
af710487 1164 if (is_present())
970ed795
EL
1165 return optional_value->TEXT_encode(p_td, buff);
1166 TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
1167 return 0;
1168}
1169
1170template<typename T_type>
1171int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
1172 const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
1173{
af710487 1174 if (is_present())
970ed795
EL
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.");
1177 return 0;
1178}
1179
1180template<typename T_type>
1181int 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)
1183{
1184 set_to_present();
1185 return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
1186}
1187
1188#endif
1189
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. */
1194template<typename T_type>
1195inline boolean operator==(template_sel left_value,
1196 const OPTIONAL<T_type>& right_value)
1197 { return right_value.is_equal(left_value); }
1198
1199template<typename T_type>
1200inline boolean operator!=(template_sel left_value,
1201 const OPTIONAL<T_type>& right_value)
1202 { return !right_value.is_equal(left_value); }
1203#endif
1204
1205template<typename T_left, typename T_right>
1206inline boolean operator==(const T_left& left_value,
1207 const OPTIONAL<T_right>& right_value)
1208 { return right_value.is_equal(left_value); }
1209
1210template<typename T_left, typename T_right>
1211inline boolean operator!=(const T_left& left_value,
1212 const OPTIONAL<T_right>& right_value)
1213 { return !right_value.is_equal(left_value); }
1214
1215#endif
1216
1217#if HAVE_GCC(4,6)
1218#pragma GCC diagnostic pop
1219#endif
This page took 0.072372 seconds and 5 git commands to generate.