File: Optional.hh

package info (click to toggle)
eclipse-titan 6.1.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 79,084 kB
  • ctags: 29,092
  • sloc: cpp: 210,764; ansic: 44,862; yacc: 21,034; sh: 12,594; makefile: 12,225; lex: 8,972; xml: 5,348; java: 4,849; perl: 3,780; python: 2,834; php: 175
file content (1247 lines) | stat: -rw-r--r-- 36,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
/******************************************************************************
 * Copyright (c) 2000-2016 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Forstner, Matyas
 *   Horvath, Gabriella
 *   Kovacs, Ferenc
 *   Ormandi, Matyas
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *
 ******************************************************************************/
#ifndef OPTIONAL_HH
#define OPTIONAL_HH

#include "Types.h"
#include "Param_Types.hh"
#include "Basetype.hh"
#include "Template.hh"
#include "BER.hh"
#include "Logger.hh"
#include "Encdec.hh"
#include "Textbuf.hh"
#include "Error.hh"
#include "Parameters.h"
#include "XER.hh"
#include "JSON.hh"
#include "XmlReader.hh"

enum optional_sel { OPTIONAL_UNBOUND, OPTIONAL_OMIT, OPTIONAL_PRESENT };

template <typename T_type>
class OPTIONAL : public Base_Type 
#ifdef TITAN_RUNTIME_2
  , public RefdIndexInterface
#endif
{
  /** The value, if present (owned by OPTIONAL) 
    * In Runtime2 the pointer is null, when the value is not present.
    * In Runtime1 its presence is indicated by the optional_selection member. */
  T_type *optional_value;
  
  /** Specifies the state of the optional field
    * @tricky In Runtime2 the optional value can be modified through parameter references,
    * in which case this member variable will not be updated. Always use the function
    * get_selection() instead of directly referencing this variable. */
  optional_sel optional_selection;
  
#ifdef TITAN_RUNTIME_2
  /** Stores the number of elements referenced by 'out' and 'inout' parameters,
    * if the optional field is a record of/set of/array (only in Runtime2).
    * If at least one element is referenced, the value must not be deleted. */
  int param_refs;
#endif

  /** Set the optional value to present.
   * If the value was already present, does nothing.
   * Else allocates a new (uninitialized) T-type.
   * @post \c optional_selection is OPTIONAL_PRESENT
   * @post \c optional_value is not NULL
   */
#ifdef TITAN_RUNTIME_2
public:
  virtual
#else
  inline
#endif
  void set_to_present() {
    if (optional_selection != OPTIONAL_PRESENT) {
      optional_selection = OPTIONAL_PRESENT;
#ifdef TITAN_RUNTIME_2
      if (optional_value == NULL)
#endif
        optional_value = new T_type;
    }
  }

  /** Set the optional value to omit.
   * If the value was present, frees it.
   * @post optional_selection is OPTIONAL_OMIT
   */
#ifdef TITAN_RUNTIME_2
public:
  virtual
#else
  inline
#endif
  void set_to_omit() {
#ifdef TITAN_RUNTIME_2
    if (is_present()) {
      if (param_refs > 0) {
        optional_value->clean_up();
      }
      else {
        delete optional_value;
        optional_value = NULL;
      }
    }
#else
    if (optional_selection == OPTIONAL_PRESENT) {
      delete optional_value;
    }
#endif
    optional_selection = OPTIONAL_OMIT;
  }

public:
  /// Default constructor creates an unbound object
  OPTIONAL() : optional_value(NULL), optional_selection(OPTIONAL_UNBOUND)
#ifdef TITAN_RUNTIME_2
  , param_refs(0)
#endif
  { }

  /// Construct an optional object set to omit.
  /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
  OPTIONAL(template_sel other_value);

  /// Copy constructor.
  /// @note Copying an unbound object creates another unbound object,
  /// without causing a dynamic test case immediately.
  OPTIONAL(const OPTIONAL& other_value);

  /// Construct from an optional of different type
  template <typename T_tmp>
  OPTIONAL(const OPTIONAL<T_tmp>& other_value);

  /// Construct from an object of different type
  template <typename T_tmp>
  OPTIONAL(const T_tmp& other_value)
  : optional_value(new T_type(other_value))
  ,  optional_selection(OPTIONAL_PRESENT)
#ifdef TITAN_RUNTIME_2
  ,  param_refs(0)
#endif
  { }

  ~OPTIONAL() {
#ifdef TITAN_RUNTIME_2
    if (NULL != optional_value)
#else
    if (optional_selection == OPTIONAL_PRESENT)
#endif
      delete optional_value; 
  }

  void clean_up();

  /// Set to omit.
  /// @p other_value must be OMIT_VALUE, or else dynamic testcase error.
  OPTIONAL& operator=(template_sel other_value);

  /// Copy assignment
  OPTIONAL& operator=(const OPTIONAL& other_value);

  /// Assign from an optional of another type
  template <typename T_tmp>
  OPTIONAL& operator=(const OPTIONAL<T_tmp>& other_value);

  /// Assign the value
  template <typename T_tmp>
  OPTIONAL& operator=(const T_tmp& other_value);

  boolean is_equal(template_sel other_value) const;
  boolean is_equal(const OPTIONAL& other_value) const;
  template <typename T_tmp>
  boolean is_equal(const OPTIONAL<T_tmp>& other_value) const;
  template <typename T_tmp>
  boolean is_equal(const T_tmp& other_value) const;

  inline boolean operator==(template_sel other_value) const
    { return is_equal(other_value); }
  inline boolean operator!=(template_sel other_value) const
    { return !is_equal(other_value); }
  inline boolean operator==(const OPTIONAL& other_value) const
    { return is_equal(other_value); }
  inline boolean operator!=(const OPTIONAL& other_value) const
    { return !is_equal(other_value); }
  template <typename T_tmp>
  inline boolean operator==(const T_tmp& other_value) const
    { return is_equal(other_value); }
  template <typename T_tmp>
  inline boolean operator!=(const T_tmp& other_value) const
    { return !is_equal(other_value); }
#if defined(__SUNPRO_CC) || defined(__clang__)
  /* Note: Without these functions the Sun Workshop Pro C++ compiler or clang reports
   * overloading ambiguity when comparing an optional charstring field with an
   * optional universal charstring. */
  template <typename T_tmp>
  inline boolean operator==(const OPTIONAL<T_tmp>& other_value) const
    { return is_equal(other_value); }
  template <typename T_tmp>
  inline boolean operator!=(const OPTIONAL<T_tmp>& other_value) const
    { return is_equal(other_value); }
#endif

#ifdef TITAN_RUNTIME_2
  boolean is_bound() const;
#else
  inline boolean is_bound() const { return optional_selection != OPTIONAL_UNBOUND; }
#endif
  boolean is_value() const
   { return optional_selection == OPTIONAL_PRESENT && optional_value->is_value(); }
  /** Whether the optional value is present.
   * @return \c true if optional_selection is OPTIONAL_PRESENT, else \c false */
#ifdef TITAN_RUNTIME_2
  boolean is_present() const;
#else
  inline boolean is_present() const { return optional_selection==OPTIONAL_PRESENT; }
#endif

#ifdef TITAN_RUNTIME_2
  /** @name override virtual functions of Base_Type
   * @{ */

  /** Return \c true (this \b is an optional field) */
  boolean is_optional() const { return TRUE; }

  /** Access the value for read/write
   *
   * @return a pointer to the (modifiable) value
   * @pre \p optional_selection must be \p OPTIONAL_PRESENT
   */
  Base_Type* get_opt_value();

  /** Access the value (read/only)
   *
   * @return a pointer to the (const) value
   * @pre \p optional_selection must be \p OPTIONAL_PRESENT
   */
  const Base_Type* get_opt_value() const;
  boolean is_seof() const;
  boolean is_equal(const Base_Type* other_value) const { return is_equal(*(static_cast<const OPTIONAL*>(other_value))); }
  void set_value(const Base_Type* other_value) { *this = *(static_cast<const OPTIONAL*>(other_value)); }
  Base_Type* clone() const { return new OPTIONAL(*this); }
  const TTCN_Typedescriptor_t* get_descriptor() const;
  /** @} */
#endif

  /** Whether the value is present.
   * Note: this is not the TTCN-3 ispresent(), kept for backward compatibility
   *       with the runtime and existing testports which use this version where 
   *       unbound errors are caught before causing more trouble
   *
   *  @return TRUE  if the value is present (optional_selection==OPTIONAL_PRESENT)
   *  @return FALSE if the value is not present (optional_selection==OPTIONAL_OMIT)
   *  @pre the value is bound (optional_selection!=OPTIONAL_UNBOUND)
   */
  boolean ispresent() const;
  
#ifdef TITAN_RUNTIME_2
  /** @tricky Calculates and returns the actual state of the optional object, 
    * not just the optional_selection member.
    * (Only needed in Runtime2, in Runtime1 optional_selection is always up to date.) */
  optional_sel get_selection() const;
#else
  inline optional_sel get_selection() const { return optional_selection; }
#endif

  void log() const;
  void set_param(Module_Param& param);
#ifdef TITAN_RUNTIME_2
  Module_Param* get_param(Module_Param_Name& param_name) const;
#endif
  void encode_text(Text_Buf& text_buf) const;
  void decode_text(Text_Buf& text_buf);

#ifdef TITAN_RUNTIME_2
  virtual int RAW_decode(const TTCN_Typedescriptor_t& td, TTCN_Buffer& buf, int limit,
    raw_order_t top_bit_ord, boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
#endif

  int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor,
    int indent, embed_values_enc_struct_t* emb_val) const;
#ifdef TITAN_RUNTIME_2
  int XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
    const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
    int indent, embed_values_enc_struct_t* emb_val) const;
#endif
  /** Used during XML decoding, in case this object is an AnyElement field in a record.
    * Determines whether XER_decode() should be called or this field should be omitted.
    * The field should be omitted if:
    *  - the next element in the encoded XML is the next field in the record or
    *  - there are no more elements until the end of the record's XML element.
    * 
    * @param reader parses the encoded XML
    * @param next_field_name name of the next field in the record, or null if this is the last one
    * @param parent_tag_closed true, if the record's XML tag is closed (is an empty element)*/
  bool XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed);
  int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
    unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val);

  char ** collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const;

  operator T_type&();
  operator const T_type&() const;

  inline T_type& operator()() { return (T_type&)*this; }
  inline const T_type& operator()() const { return (const T_type&)*this; }

  ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
                                unsigned p_coding) const;
#ifdef TITAN_RUNTIME_2
  ASN_BER_TLV_t* BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
    const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const;
#endif
  boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                         const ASN_BER_TLV_t& p_tlv, unsigned L_form);
  boolean BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
                             const ASN_BER_TLV_t& p_tlv);
  void BER_decode_opentypes(TTCN_Type_list& p_typelist, unsigned L_form);

#ifdef TITAN_RUNTIME_2
  int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
  int TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
    const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
  int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, Limit_Token_List&,
                  boolean no_err=FALSE, boolean first_call=TRUE);
  int JSON_encode_negtest(const Erroneous_descriptor_t*,
                          const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
#endif
  
  /** Encodes accordingly to the JSON encoding rules.
    * Returns the length of the encoded data. */
  int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
  
  /** Decodes accordingly to the JSON encoding rules.
    * Returns the length of the decoded data. */
  int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
  
#ifdef TITAN_RUNTIME_2
  /** Called before an element of an optional record of/set of is indexed and passed as an
    * 'inout' or 'out' parameter to a function (only in Runtime2).
    * Sets the optional value to present (this would be done by the indexing operation
    * anyway) and redirects the call to the optional value. */
  virtual void add_refd_index(int index);
  
  /** Called after an element of an optional record of/set of is passed as an
    * 'inout' or 'out' parameter to a function (only in Runtime2).
    * Redirects the call to the optional value. */
  virtual void remove_refd_index(int index);
#endif
};

#if HAVE_GCC(4,6)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
#endif

#ifdef TITAN_RUNTIME_2

template<typename T_type>
Base_Type* OPTIONAL<T_type>::get_opt_value()
{
#ifdef TITAN_RUNTIME_2
  if (!is_present())
#else
  if (optional_selection!=OPTIONAL_PRESENT)
#endif
    TTCN_error("Internal error: get_opt_value() called on a non-present optional field.");
  return optional_value;
}

template<typename T_type>
const Base_Type* OPTIONAL<T_type>::get_opt_value() const
{
#ifdef TITAN_RUNTIME_2
  if (!is_present())
#else
  if (optional_selection!=OPTIONAL_PRESENT)
#endif
    TTCN_error("Internal error: get_opt_value() const called on a non-present optional field.");
  return optional_value;
}

template<typename T_type>
boolean OPTIONAL<T_type>::is_seof() const
{
  return
#ifdef TITAN_RUNTIME_2
  (is_present())
#else
  (optional_selection==OPTIONAL_PRESENT)
#endif
  ? optional_value->is_seof() : T_type().is_seof();
}

template<typename T_type>
const TTCN_Typedescriptor_t* OPTIONAL<T_type>::get_descriptor() const
{
  return 
#ifdef TITAN_RUNTIME_2
  (is_present())
#else
  (optional_selection==OPTIONAL_PRESENT)
#endif
  ? optional_value->get_descriptor() : T_type().get_descriptor();
}

#endif

template<typename T_type>
OPTIONAL<T_type>::OPTIONAL(template_sel other_value)
  : optional_value(NULL), optional_selection(OPTIONAL_OMIT)
#ifdef TITAN_RUNTIME_2
  , param_refs(0)
#endif
{
  if (other_value != OMIT_VALUE)
    TTCN_error("Setting an optional field to an invalid value.");
}

template<typename T_type>
OPTIONAL<T_type>::OPTIONAL(const OPTIONAL& other_value)
  : Base_Type(other_value)
#ifdef TITAN_RUNTIME_2
  , RefdIndexInterface(other_value)
#endif     
  , optional_value(NULL)
  , optional_selection(other_value.optional_selection)
#ifdef TITAN_RUNTIME_2
  , param_refs(0)
#endif
{
  switch (other_value.optional_selection) {
  case OPTIONAL_PRESENT:
    optional_value = new T_type(*other_value.optional_value);
    break;
  case OPTIONAL_OMIT:
    break;
  default:
    break;
  }
}

template<typename T_type> template<typename T_tmp>
OPTIONAL<T_type>::OPTIONAL(const OPTIONAL<T_tmp>& other_value)
  : optional_value(NULL), optional_selection(other_value.get_selection())
#ifdef TITAN_RUNTIME_2
  , param_refs(0)
#endif
{
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
    optional_value = new T_type((const T_tmp&)other_value);
    break;
  case OPTIONAL_OMIT:
    break;
  default:
    break;
  }
}

template<typename T_type>
void OPTIONAL<T_type>::clean_up()
{
#ifdef TITAN_RUNTIME_2
  if (is_present()) {
    if (param_refs > 0) {
      optional_value->clean_up();
    }
    else {
      delete optional_value;
      optional_value = NULL;
    }
  }
#else
  if (OPTIONAL_PRESENT == optional_selection) {
    delete optional_value;
  }
#endif
  optional_selection = OPTIONAL_UNBOUND;
}

template<typename T_type>
OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(template_sel other_value)
{
  if (other_value != OMIT_VALUE)
    TTCN_error("Internal error: Setting an optional field to an invalid value.");
  set_to_omit();
  return *this;
}

template<typename T_type>
OPTIONAL<T_type>& OPTIONAL<T_type>::operator=(const OPTIONAL& other_value)
{
  switch (other_value.optional_selection) {
  case OPTIONAL_PRESENT:
#ifdef TITAN_RUNTIME_2
    if (NULL == optional_value) {
#else
    if (optional_selection != OPTIONAL_PRESENT) {
#endif
      optional_value = new T_type(*other_value.optional_value);
      optional_selection = OPTIONAL_PRESENT;
    } else *optional_value = *other_value.optional_value;
    break;
  case OPTIONAL_OMIT:
    if (&other_value != this) set_to_omit();
    break;
  default:
    clean_up();
    break;
  }
  return *this;
}

template<typename T_type> template <typename T_tmp>
OPTIONAL<T_type>&
OPTIONAL<T_type>::operator=(const OPTIONAL<T_tmp>& other_value)
{
  switch (other_value.get_selection()) {
  case OPTIONAL_PRESENT:
#ifdef TITAN_RUNTIME_2
    if (NULL == optional_value) {
#else
    if (optional_selection != OPTIONAL_PRESENT) {
#endif
      optional_value = new T_type((const T_tmp&)other_value);
      optional_selection = OPTIONAL_PRESENT;
    } else *optional_value = (const T_tmp&)other_value;
    break;
  case OPTIONAL_OMIT:
    set_to_omit();
    break;
  default:
    clean_up();
    break;
  }
  return *this;
}

template<typename T_type> template <typename T_tmp>
OPTIONAL<T_type>&
OPTIONAL<T_type>::operator=(const T_tmp& other_value)
{
#ifdef TITAN_RUNTIME_2
  if (NULL == optional_value) {
#else
  if (optional_selection != OPTIONAL_PRESENT) {
#endif
    optional_value = new T_type(other_value);
    optional_selection = OPTIONAL_PRESENT;
  } else *optional_value = other_value;
  return *this;
}

template<typename T_type>
boolean OPTIONAL<T_type>::is_equal(template_sel other_value) const
{
#ifdef TITAN_RUNTIME_2
  if (!is_bound()) {
#else
  if (optional_selection == OPTIONAL_UNBOUND) {
#endif
    if (other_value == UNINITIALIZED_TEMPLATE) return TRUE;
    TTCN_error("The left operand of comparison is an unbound optional value.");
  }
  if (other_value != OMIT_VALUE) TTCN_error("Internal error: The right operand "
    "of comparison is an invalid value.");
  return
#ifdef TITAN_RUNTIME_2
  !is_present();
#else
  optional_selection == OPTIONAL_OMIT;
#endif
}

template<typename T_type>
boolean OPTIONAL<T_type>::is_equal(const OPTIONAL& other_value) const
{
#ifdef TITAN_RUNTIME_2
  if (!is_bound()) {
    if (!other_value.is_bound())
#else
  if (optional_selection == OPTIONAL_UNBOUND) {
    if (other_value.optional_selection == OPTIONAL_UNBOUND)
#endif
      return TRUE;
    TTCN_error("The left operand of "
    "comparison is an unbound optional value.");
  }
#ifdef TITAN_RUNTIME_2
  if (!other_value.is_bound())
#else
  if (other_value.optional_selection == OPTIONAL_UNBOUND)
#endif
    TTCN_error("The right operand of comparison is an unbound optional value.");
#ifdef TITAN_RUNTIME_2
  boolean present = is_present();
  if (present != other_value.is_present()) return FALSE;
  else if (present)
#else
  if (optional_selection != other_value.optional_selection) return FALSE;
  else if (optional_selection == OPTIONAL_PRESENT)
#endif
    return *optional_value == *other_value.optional_value;
  else return TRUE;
}

template<typename T_type> template <typename T_tmp>
boolean OPTIONAL<T_type>::is_equal(const T_tmp& other_value) const
{
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return *optional_value == other_value;
  case OPTIONAL_OMIT:
    return FALSE;
  default:
    TTCN_error("The left operand of comparison is an unbound optional value.");
  }
  return FALSE;
}

template<typename T_type> template <typename T_tmp>
boolean OPTIONAL<T_type>::is_equal(const OPTIONAL<T_tmp>& other_value) const
{
#ifdef TITAN_RUNTIME_2
  if (!is_bound()) {
    if (!other_value.is_bound())
#else
  optional_sel other_selection = other_value.get_selection();
  if (optional_selection == OPTIONAL_UNBOUND) {
    if (other_selection == OPTIONAL_UNBOUND)
#endif
      return TRUE;
    TTCN_error("The left operand of "
    "comparison is an unbound optional value.");
  }
#ifdef TITAN_RUNTIME_2
  if (!other_value.is_bound())
#else
  if (other_selection == OPTIONAL_UNBOUND)
#endif
    TTCN_error("The right operand of comparison is an unbound optional value.");
#ifdef TITAN_RUNTIME_2
  boolean present = is_present();
  if (present != other_value.is_present()) return FALSE;
  else if (present)
#else
  if (optional_selection != other_selection) return FALSE;
  else if (optional_selection == OPTIONAL_PRESENT)
#endif
    return *optional_value == (const T_tmp&)other_value;
  else return TRUE;
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
boolean OPTIONAL<T_type>::is_bound() const
{
  switch (optional_selection) {
  case OPTIONAL_PRESENT:
  case OPTIONAL_OMIT:
    return TRUE;
  default:
    if (NULL != optional_value) {
      return optional_value->is_bound();
    }
    return FALSE;
  }
}

template<typename T_type>
boolean OPTIONAL<T_type>::is_present() const
{
  switch (optional_selection) {
  case OPTIONAL_PRESENT:
    return TRUE;
  case OPTIONAL_OMIT:
  default:
    if (NULL != optional_value) {
      return optional_value->is_bound();
    }
    return FALSE;
  }
}
#endif

template<typename T_type>
boolean OPTIONAL<T_type>::ispresent() const
{
  switch (optional_selection) {
  case OPTIONAL_PRESENT:
    return TRUE;
  case OPTIONAL_OMIT:
#ifdef TITAN_RUNTIME_2
    if (NULL != optional_value) {
      return optional_value->is_bound();
    }
#endif
    return FALSE;
  default:
#ifdef TITAN_RUNTIME_2
    if (NULL != optional_value && optional_value->is_bound()) {
      return TRUE;
    }
#endif
    TTCN_error("Using an unbound optional field.");
  }
  return FALSE;
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
optional_sel OPTIONAL<T_type>::get_selection() const
{
  if (is_present()) {
    return OPTIONAL_PRESENT;
  }
  if (is_bound()) {
    // not present, but bound => omit
    return OPTIONAL_OMIT;
  }
  return OPTIONAL_UNBOUND;
}
#endif

template<typename T_type>
void OPTIONAL<T_type>::log() const
{
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    optional_value->log();
    break;
  case OPTIONAL_OMIT:
    TTCN_Logger::log_event_str("omit");
    break;
  default:
    TTCN_Logger::log_event_unbound();
    break;
  }
}

template <typename T_type>
void OPTIONAL<T_type>::set_param(Module_Param& param) {
  if (param.get_type()==Module_Param::MP_Omit) {
    if (param.get_ifpresent()) param.error("An optional field of a record value cannot have an 'ifpresent' attribute");
    if (param.get_length_restriction()!=NULL) param.error("An optional field of a record value cannot have a length restriction");
    set_to_omit();
    return;
  }
  set_to_present();
  optional_value->set_param(param);
  if (!optional_value->is_bound()) {
    clean_up();
  }
}

#ifdef TITAN_RUNTIME_2
template <typename T_type>
Module_Param* OPTIONAL<T_type>::get_param(Module_Param_Name& param_name) const
{
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->get_param(param_name);
  case OPTIONAL_OMIT:
    return new Module_Param_Omit();
  default:
    return new Module_Param_Unbound();
  }
}
#endif

template<typename T_type>
void OPTIONAL<T_type>::encode_text(Text_Buf& text_buf) const
{
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_OMIT:
    text_buf.push_int((RInt)FALSE);
    break;
  case OPTIONAL_PRESENT:
    text_buf.push_int((RInt)TRUE);
    optional_value->encode_text(text_buf);
    break;
  default:
    TTCN_error("Text encoder: Encoding an unbound optional value.");
  }
}

template<typename T_type>
void OPTIONAL<T_type>::decode_text(Text_Buf& text_buf)
{
  if (text_buf.pull_int().get_val()) {
    set_to_present();
    optional_value->decode_text(text_buf);
  } else set_to_omit();
}

template<typename T_type>
int OPTIONAL<T_type>::JSON_encode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok) const
{
#ifdef TITAN_RUNTIME_2
  switch(get_selection()) {
#else
  switch(optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->JSON_encode(p_td, p_tok);
  case OPTIONAL_OMIT:
    return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
  default:
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound optional value.");
    return -1;
  }
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
int OPTIONAL<T_type>::JSON_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
                                        const TTCN_Typedescriptor_t& p_td,
                                        JSON_Tokenizer& p_tok) const 
{
  switch (get_selection()) {
  case OPTIONAL_PRESENT:
    return optional_value->JSON_encode_negtest(p_err_descr, p_td, p_tok);
  case OPTIONAL_OMIT:
    return p_tok.put_next_token(JSON_TOKEN_LITERAL_NULL, NULL);
  default:
    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
      "Encoding an unbound optional value.");
    return -1;
  }
}
#endif

template<typename T_type>
int OPTIONAL<T_type>::JSON_decode(const TTCN_Typedescriptor_t& p_td, JSON_Tokenizer& p_tok, boolean p_silent)
{
  // try the optional value first
  set_to_present();
  size_t buf_pos = p_tok.get_buf_pos();
  int dec_len = optional_value->JSON_decode(p_td, p_tok, p_silent);
  if (JSON_ERROR_FATAL == dec_len) {
    if (p_silent) {
      clean_up();
    } else {
      set_to_omit();
    }
  }
  else if (JSON_ERROR_INVALID_TOKEN == dec_len) {
    // invalid token, rewind the buffer and check if it's a "null" (= omit)
    // this needs to be checked after the optional value, because it might also be
    // able to decode a "null" value
    p_tok.set_buf_pos(buf_pos);
    json_token_t token = JSON_TOKEN_NONE;
    dec_len = p_tok.get_next_token(&token, NULL, NULL);
    if (JSON_TOKEN_LITERAL_NULL == token) {
      set_to_omit();
    }
    else {
      // cannot get JSON_TOKEN_ERROR here, that was already checked by the optional value
      dec_len = JSON_ERROR_INVALID_TOKEN;
    }
  }
  return dec_len;
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
void OPTIONAL<T_type>::add_refd_index(int index)
{
  ++param_refs;
  set_to_present();
  RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
  if (0 != refd_opt_val) {
    refd_opt_val->add_refd_index(index);
  }
}

template<typename T_type>
void OPTIONAL<T_type>::remove_refd_index(int index)
{
  --param_refs;
  RefdIndexInterface* refd_opt_val = dynamic_cast<RefdIndexInterface*>(optional_value);
  if (0 != refd_opt_val) {
    refd_opt_val->remove_refd_index(index);
  }
}
#endif

template<typename T_type>
OPTIONAL<T_type>::operator T_type&()
{
  set_to_present();
  return *optional_value;
}

template<typename T_type>
OPTIONAL<T_type>::operator const T_type&() const
{
#ifdef TITAN_RUNTIME_2
  if (!is_present())
#else
  if (optional_selection != OPTIONAL_PRESENT)
#endif
    TTCN_error("Using the value of an optional field containing omit.");
  return *optional_value;
}

template<typename T_type>
ASN_BER_TLV_t*
OPTIONAL<T_type>::BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
                                 unsigned p_coding) const
{
  BER_chk_descr(p_td);
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->BER_encode_TLV(p_td, p_coding);
  case OPTIONAL_OMIT:
    return ASN_BER_TLV_t::construct();
  default:
    return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
  }
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
ASN_BER_TLV_t*
OPTIONAL<T_type>::BER_encode_TLV_negtest(const Erroneous_descriptor_t* p_err_descr,
    const TTCN_Typedescriptor_t& p_td, unsigned p_coding) const
{
  BER_chk_descr(p_td);
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->BER_encode_TLV_negtest(p_err_descr, p_td, p_coding);
  case OPTIONAL_OMIT:
    return ASN_BER_TLV_t::construct();
  default:
    return ASN_BER_V2TLV(BER_encode_chk_bound(FALSE), p_td, p_coding);
  }
}

template<typename T_type>
int OPTIONAL<T_type>::RAW_decode(const TTCN_Typedescriptor_t& p_td,
  TTCN_Buffer&, int /* limit */, raw_order_t /* top_bit_ord */,
  boolean /* no_error */, int /* sel_field */, boolean /* first_call */ )
{
  TTCN_error("RAW decoding requested for optional type '%s'"
             " which has no RAW decoding method.",p_td.name);
  return 0;
}
#endif

template<typename T_type>
int
OPTIONAL<T_type>::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent, embed_values_enc_struct_t* emb_val) const
{
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->XER_encode(p_td, buf, flavor, indent, emb_val);
  case OPTIONAL_OMIT:
    return 0; // nothing to do !
  default:
    TTCN_EncDec_ErrorContext::error(
      TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
    return 0;
  }
}

#ifdef TITAN_RUNTIME_2
template<typename T_type>
int
OPTIONAL<T_type>::XER_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
  const XERdescriptor_t& p_td, TTCN_Buffer& buf, unsigned int flavor, int indent,
  embed_values_enc_struct_t* emb_val) const
{
  switch (get_selection()) {
  case OPTIONAL_PRESENT:
    return optional_value->XER_encode_negtest(p_err_descr, p_td, buf, flavor, indent, emb_val);
  case OPTIONAL_OMIT:
    return 0; // nothing to do !
  default:
    TTCN_EncDec_ErrorContext::error(
      TTCN_EncDec::ET_UNBOUND, "Encoding an unbound optional value.");
    return 0;
  }
}
#endif


template<typename T_type>
bool
OPTIONAL<T_type>::XER_check_any_elem(XmlReaderWrap& reader, const char* next_field_name, bool parent_tag_closed) 
{
  // If the record has no elements, then it can't have an AnyElement
  if (parent_tag_closed) {
    set_to_omit();
    return false;
  }
  
  while (reader.Ok()) {
    // Leaving the record before finding an element -> no AnyElement
    if (XML_READER_TYPE_END_ELEMENT == reader.NodeType()) {
      set_to_omit();
      return false;
    }
    if (XML_READER_TYPE_ELEMENT == reader.NodeType()) {
      // The first element found is either the next field's element or the AnyElement
      if (NULL != next_field_name && 
          0 == strcmp((const char*)reader.LocalName(), next_field_name)) {
        set_to_omit();
        return false;
      }
      break;
    }
    reader.Read();
  }
   
  return true;
}

template<typename T_type>
int
OPTIONAL<T_type>::XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
  unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t* emb_val)
{
  int exer  = is_exer(flavor);
  for (int success = reader.Ok(); success==1; success=reader.Read()) {
    int type = reader.NodeType();
    const char * name; // name of the optional field
    const char * ns_uri;

    if (exer && (p_td.xer_bits & XER_ATTRIBUTE)) {
      if (XML_READER_TYPE_ATTRIBUTE == type) {
        for (; success==1; success = reader.MoveToNextAttribute()) {
          if (!reader.IsNamespaceDecl()) break;
        }

        name = (const char*)reader.LocalName();
        if (!check_name(name, p_td, exer)) { // it's not us, bail
          break;
        }
        // we already checked for exer==1
        if (!check_namespace((const char*)reader.NamespaceUri(), p_td)) break;

        // set to omit if the attribute is empty
        const char * value = (const char *)reader.Value();
        if (strlen(value) == 0) {
          break;
        }
        
        set_to_present();
        optional_value->XER_decode(p_td, reader, flavor, flavor2, emb_val);
        goto finished;
      }
      else break;
    }
    else if(XML_READER_TYPE_ATTRIBUTE == type && (flavor & USE_NIL)){
        goto found_it;
    }
    else { // not attribute
      if (XML_READER_TYPE_ELEMENT == type) { // we are at an element
        name   = (const char*)reader.LocalName();
        ns_uri = (const char*)reader.NamespaceUri();
        if ((p_td.xer_bits & ANY_ELEMENT) || (exer && (flavor & USE_NIL))
            || ( (p_td.xer_bits & UNTAGGED) && !reader.IsEmptyElement())
          // If the optional field (a string) has anyElement, accept the element
          // regardless of its name. Else the name (and namespace) must match.
          || T_type::can_start(name, ns_uri, p_td, flavor)) { // it is us
          found_it:
          set_to_present();
          //success = reader.Read(); // move to next thing TODO should it loop till an element ?
          // Pass XER_OPTIONAL to flavor, to sign that we are optional
          optional_value->XER_decode(p_td, reader, flavor | XER_OPTIONAL, flavor2, emb_val);
          if (!optional_value->is_bound()) {
            set_to_omit();
          }
        }
        else break; // it's not us, bail

        goto finished;
      }
      else if (XML_READER_TYPE_TEXT == type && (flavor & USE_NIL)) {
        goto found_it;
      }
      else if (XML_READER_TYPE_END_ELEMENT == type) {
        break;
      }
      // else circle around
    } // if attribute
  } // next
  set_to_omit();
  return 0;
finished:
  return 1;
}

template<typename T_type>
char ** OPTIONAL<T_type>::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns) const {
#ifdef TITAN_RUNTIME_2
  switch (get_selection()) {
#else
  switch (optional_selection) {
#endif
  case OPTIONAL_PRESENT:
    return optional_value->collect_ns(p_td, num, def_ns);
  case OPTIONAL_OMIT:
    def_ns = false;
    num = 0;
    return 0;
  default:
    TTCN_EncDec_ErrorContext::error(
      TTCN_EncDec::ET_UNBOUND, "Encoding an unbound value.");
    return 0;
  }
}

template<typename T_type>
boolean OPTIONAL<T_type>::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
                                         const ASN_BER_TLV_t& p_tlv,
                                         unsigned L_form)
{
  BER_chk_descr(p_td);
  if (BER_decode_isMyMsg(p_td, p_tlv)) {
    return optional_value->BER_decode_TLV(p_td, p_tlv, L_form);
  } else {
    set_to_omit();
    return TRUE;
  }
}

template<typename T_type>
boolean OPTIONAL<T_type>::BER_decode_isMyMsg(const TTCN_Typedescriptor_t& p_td,
  const ASN_BER_TLV_t& p_tlv)
{
  set_to_present();
  return optional_value->BER_decode_isMyMsg(p_td, p_tlv);
}

template<typename T_type>
void OPTIONAL<T_type>::BER_decode_opentypes(TTCN_Type_list& p_typelist,
  unsigned L_form)
{
#ifdef TITAN_RUNTIME_2
  if (is_present()) {
    optional_selection = OPTIONAL_PRESENT;
#else
  if (optional_selection==OPTIONAL_PRESENT) {
#endif
    optional_value->BER_decode_opentypes(p_typelist, L_form);
  }
}

#ifdef TITAN_RUNTIME_2

template<typename T_type>
int OPTIONAL<T_type>::TEXT_encode(const TTCN_Typedescriptor_t& p_td,
                           TTCN_Buffer& buff) const
{
  if (is_present())
    return optional_value->TEXT_encode(p_td, buff);
  TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
  return 0;
}

template<typename T_type>
int OPTIONAL<T_type>::TEXT_encode_negtest(const Erroneous_descriptor_t* p_err_descr,
  const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff) const
{
  if (is_present())
    return optional_value->TEXT_encode_negtest(p_err_descr, p_td, buff);
  TTCN_error("Internal error: TEXT encoding an unbound/omit optional field.");
  return 0;
}

template<typename T_type>
int OPTIONAL<T_type>::TEXT_decode(const TTCN_Typedescriptor_t& p_td,
  TTCN_Buffer& buff, Limit_Token_List& limit, boolean no_err, boolean first_call)
{
  set_to_present();
  return optional_value->TEXT_decode(p_td, buff, limit, no_err, first_call);
}

#endif

#if defined(__GNUC__) && __GNUC__ >= 3
/** Note: These functions allow most efficient operation by passing the left
 * operand OMIT_VALUE as value instead of constant reference.
 * However, with GCC 2.95.x the functions cause overloading ambiguities. */
template<typename T_type>
inline boolean operator==(template_sel left_value,
  const OPTIONAL<T_type>& right_value)
  { return right_value.is_equal(left_value); }

template<typename T_type>
inline boolean operator!=(template_sel left_value,
  const OPTIONAL<T_type>& right_value)
  { return !right_value.is_equal(left_value); }
#endif

template<typename T_left, typename T_right>
inline boolean operator==(const T_left& left_value,
  const OPTIONAL<T_right>& right_value)
  { return right_value.is_equal(left_value); }

template<typename T_left, typename T_right>
inline boolean operator!=(const T_left& left_value,
  const OPTIONAL<T_right>& right_value)
  { return !right_value.is_equal(left_value); }

#endif

#if HAVE_GCC(4,6)
#pragma GCC diagnostic pop
#endif