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