1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
10 #include "../common/dbgnew.hh"
13 #include "../common/static_check.h"
16 static const size_t MIN_COMPONENTS
= 2;
18 struct OBJID::objid_struct
{
19 unsigned int ref_count
;
20 int n_components
; ///< number of elements in \a components_ptr (min. 2)
21 int overflow_idx
; ///< index of the first overflow, or -1
22 objid_element components_ptr
[MIN_COMPONENTS
];
25 #define OBJID_FMT "%u"
26 //#define OBJID_FMT "%lu"
28 void OBJID::init_struct(int n_components
)
30 if (n_components
< 0) {
32 TTCN_error("Initializing an objid value with a negative number of "
35 // TODO check n_components >= 2
36 val_ptr
= (objid_struct
*)Malloc(sizeof(objid_struct
)
37 + (n_components
- MIN_COMPONENTS
) * sizeof(objid_element
));
38 val_ptr
->ref_count
= 1;
39 val_ptr
->n_components
= n_components
;
40 val_ptr
->overflow_idx
= -1;
43 void OBJID::copy_value()
45 if (val_ptr
!= NULL
&& val_ptr
->ref_count
> 1) {
46 objid_struct
*old_ptr
= val_ptr
;
48 init_struct(old_ptr
->n_components
); // val_ptr reallocated
49 memcpy(val_ptr
->components_ptr
, old_ptr
->components_ptr
,
50 old_ptr
->n_components
* sizeof(objid_element
));
51 val_ptr
->overflow_idx
= old_ptr
->overflow_idx
;
55 void OBJID::clean_up()
57 if (val_ptr
!= NULL
) {
58 if (val_ptr
->ref_count
> 1) val_ptr
->ref_count
--;
59 else if (val_ptr
->ref_count
== 1) Free(val_ptr
);
60 else TTCN_error("Internal error: Invalid reference counter in an objid "
68 val_ptr
= NULL
; // unbound
71 OBJID::OBJID(int init_n_components
, ...)
73 init_struct(init_n_components
);
75 va_start(ap
, init_n_components
);
76 for (int i
= 0; i
< init_n_components
; i
++) {
77 val_ptr
->components_ptr
[i
] = va_arg(ap
, objid_element
);
83 OBJID::OBJID(int init_n_components
, const objid_element
*init_components
)
85 init_struct(init_n_components
);
86 memcpy(val_ptr
->components_ptr
, init_components
, init_n_components
*
87 sizeof(objid_element
));
90 OBJID::OBJID(const OBJID
& other_value
)
91 : Base_Type(other_value
)
93 if (other_value
.val_ptr
== NULL
)
94 TTCN_error("Copying an unbound objid value.");
95 val_ptr
= other_value
.val_ptr
;
104 OBJID
& OBJID::operator=(const OBJID
& other_value
)
106 if (other_value
.val_ptr
== NULL
)
107 TTCN_error("Assignment of an unbound objid value.");
108 if (&other_value
!= this) {
110 val_ptr
= other_value
.val_ptr
;
111 val_ptr
->ref_count
++;
116 boolean
OBJID::operator==(const OBJID
& other_value
) const
118 if (val_ptr
== NULL
) TTCN_error("The left operand of comparison is an "
119 "unbound objid value.");
120 if (other_value
.val_ptr
== NULL
) TTCN_error("The right operand of comparison "
121 "is an unbound objid value.");
122 if (val_ptr
->n_components
!= other_value
.val_ptr
->n_components
) return FALSE
;
123 if (val_ptr
->overflow_idx
!= other_value
.val_ptr
->overflow_idx
) return FALSE
;
124 return !memcmp(val_ptr
->components_ptr
,
125 other_value
.val_ptr
->components_ptr
,
126 val_ptr
->n_components
* sizeof(objid_element
));
129 OBJID::objid_element
& OBJID::operator[](int index_value
)
131 if (val_ptr
== NULL
) {
132 if (index_value
!= 0)
133 TTCN_error("Accessing a component of an unbound objid value.");
135 return val_ptr
->components_ptr
[0];
137 if (index_value
< 0) TTCN_error("Accessing an objid component using "
138 "a negative index (%d).", index_value
);
139 int n_components
= val_ptr
->n_components
;
140 if (index_value
> n_components
) TTCN_error("Index overflow when accessing "
141 "an objid component: the index is %d, but the value has only %d "
142 "components.", index_value
, n_components
);
143 else if (index_value
== n_components
) {
144 if (val_ptr
->ref_count
== 1) {
145 val_ptr
= (objid_struct
*)
146 Realloc(val_ptr
, sizeof(objid_struct
)
147 + n_components
* sizeof(objid_element
));
148 val_ptr
->n_components
++;
150 objid_struct
*old_ptr
= val_ptr
;
151 old_ptr
->ref_count
--;
152 init_struct(n_components
+ 1);
153 memcpy(val_ptr
->components_ptr
, old_ptr
->components_ptr
,
154 n_components
* sizeof(objid_element
));
157 return val_ptr
->components_ptr
[index_value
];
161 OBJID::objid_element
OBJID::operator[](int index_value
) const
164 TTCN_error("Accessing a component of an unbound objid value.");
166 TTCN_error("Accessing an objid component using a negative index (%d).",
168 if (index_value
>= val_ptr
->n_components
) TTCN_error("Index overflow when "
169 "accessing an objid component: the index is %d, but the value has only %d "
170 "components.", index_value
, val_ptr
->n_components
);
171 return val_ptr
->components_ptr
[index_value
];
174 int OBJID::size_of() const
177 TTCN_error("Getting the size of an unbound objid value.");
178 return val_ptr
->n_components
;
181 OBJID::operator const objid_element
*() const
184 TTCN_error("Casting an unbound objid value to const int*.");
185 return val_ptr
->components_ptr
;
188 OBJID::objid_element
OBJID::from_INTEGER(const INTEGER
& p_int
)
190 int_val_t i_val
= p_int
.get_val();
191 if (i_val
.is_negative()) {
192 TTCN_error("An OBJECT IDENTIFIER component cannot be negative");
194 if (!i_val
.is_native()) {
195 TTCN_error("The value of an OBJECT IDENTIFIER component cannot exceed %u",
198 return (OBJID::objid_element
)i_val
.get_val();
201 void OBJID::log() const
203 if (val_ptr
!= NULL
) {
204 TTCN_Logger::log_event_str("objid { ");
205 for (int i
= 0; i
< val_ptr
->n_components
; i
++) {
206 if (i
== val_ptr
->overflow_idx
) {
207 TTCN_Logger::log_event_str("overflow:");
210 TTCN_Logger::log_event(OBJID_FMT
" ", val_ptr
->components_ptr
[i
]);
212 TTCN_Logger::log_char('}');
213 } else TTCN_Logger::log_event_unbound();
216 void OBJID::set_param(Module_Param
& param
) {
217 param
.basic_check(Module_Param::BC_VALUE
, "objid value");
218 if (param
.get_type()!=Module_Param::MP_Objid
) param
.type_error("objid value");
219 if (sizeof(objid_element
)!=sizeof(int)) TTCN_error("Internal error: OBJID::set_param()");
221 init_struct(param
.get_string_size());
222 memcpy(val_ptr
->components_ptr
, param
.get_string_data(), val_ptr
->n_components
* sizeof(objid_element
));
225 void OBJID::encode_text(Text_Buf
& text_buf
) const
228 TTCN_error("Text encoder: Encoding an unbound objid value.");
229 text_buf
.push_int(val_ptr
->n_components
);
230 for (int i
= 0; i
< val_ptr
->n_components
; i
++)
231 text_buf
.push_int(val_ptr
->components_ptr
[i
]);
234 void OBJID::decode_text(Text_Buf
& text_buf
)
236 int n_components
= text_buf
.pull_int().get_val();
237 if (n_components
< 0) TTCN_error("Text decoder: Negative number of "
238 "components was received for an objid value.");
240 init_struct(n_components
);
241 for (int i
= 0; i
< n_components
; i
++)
242 val_ptr
->components_ptr
[i
] = text_buf
.pull_int().get_val();
245 void OBJID::encode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
246 TTCN_EncDec::coding_t p_coding
, ...) const
249 va_start(pvar
, p_coding
);
251 case TTCN_EncDec::CT_BER
: {
252 TTCN_EncDec_ErrorContext
ec("While BER-encoding type '%s': ", p_td
.name
);
253 unsigned BER_coding
=va_arg(pvar
, unsigned);
254 BER_encode_chk_coding(BER_coding
);
255 ASN_BER_TLV_t
*tlv
=BER_encode_TLV(p_td
, BER_coding
);
256 tlv
->put_in_buffer(p_buf
);
257 ASN_BER_TLV_t::destruct(tlv
);
259 case TTCN_EncDec::CT_RAW
: {
260 TTCN_EncDec_ErrorContext
ec("While RAW-encoding type '%s': ", p_td
.name
);
261 TTCN_EncDec_ErrorContext::error_internal
262 ("No RAW descriptor available for type '%s'.", p_td
.name
);
264 case TTCN_EncDec::CT_XER
: {
265 TTCN_EncDec_ErrorContext
ec("While XER-encoding type '%s': ", p_td
.name
);
266 unsigned XER_coding
=va_arg(pvar
, unsigned);
267 XER_encode(*p_td
.xer
, p_buf
, XER_coding
, 0, 0);
269 case TTCN_EncDec::CT_JSON
: {
270 TTCN_EncDec_ErrorContext
ec("While JSON-encoding type '%s': ", p_td
.name
);
272 TTCN_EncDec_ErrorContext::error_internal
273 ("No JSON descriptor available for type '%s'.", p_td
.name
);
274 JSON_Tokenizer
tok(va_arg(pvar
, int) != 0);
275 JSON_encode(p_td
, tok
);
276 p_buf
.put_s(tok
.get_buffer_length(), (const unsigned char*)tok
.get_buffer());
279 TTCN_error("Unknown coding method requested to encode type '%s'",
285 void OBJID::decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
286 TTCN_EncDec::coding_t p_coding
, ...)
289 va_start(pvar
, p_coding
);
291 case TTCN_EncDec::CT_BER
: {
292 TTCN_EncDec_ErrorContext
ec("While BER-decoding type '%s': ", p_td
.name
);
293 unsigned L_form
=va_arg(pvar
, unsigned);
295 BER_decode_str2TLV(p_buf
, tlv
, L_form
);
296 BER_decode_TLV(p_td
, tlv
, L_form
);
297 if(tlv
.isComplete
) p_buf
.increase_pos(tlv
.get_len());
299 case TTCN_EncDec::CT_RAW
: {
300 TTCN_EncDec_ErrorContext
ec("While RAW-decoding type '%s': ", p_td
.name
);
301 TTCN_EncDec_ErrorContext::error_internal
302 ("No RAW descriptor available for type '%s'.", p_td
.name
);
304 case TTCN_EncDec::CT_XER
: {
305 TTCN_EncDec_ErrorContext
ec("While XER-decoding type '%s': ", p_td
.name
);
306 unsigned XER_coding
=va_arg(pvar
, unsigned);
307 XmlReaderWrap
reader(p_buf
);
308 int success
= reader
.Read();
309 for (; success
==1; success
=reader
.Read()) {
310 int type
= reader
.NodeType();
311 if (type
==XML_READER_TYPE_ELEMENT
)
314 XER_decode(*p_td
.xer
, reader
, XER_coding
, 0);
315 size_t bytes
= reader
.ByteConsumed();
316 p_buf
.set_pos(bytes
);
318 case TTCN_EncDec::CT_JSON
: {
319 TTCN_EncDec_ErrorContext
ec("While JSON-decoding type '%s': ", p_td
.name
);
321 TTCN_EncDec_ErrorContext::error_internal
322 ("No JSON descriptor available for type '%s'.", p_td
.name
);
323 JSON_Tokenizer
tok((const char*)p_buf
.get_data(), p_buf
.get_len());
324 if(JSON_decode(p_td
, tok
, false)<0)
325 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
326 "Can not decode type '%s', because invalid or incomplete"
327 " message was received"
329 p_buf
.set_pos(tok
.get_buf_pos());
332 TTCN_error("Unknown coding method requested to decode type '%s'",
339 OBJID::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
340 unsigned p_coding
) const
343 ASN_BER_TLV_t
*new_tlv
=BER_encode_chk_bound(is_bound());
346 switch(p_td
.asnbasetype
) {
347 case TTCN_Typedescriptor_t::OBJID
:
348 if(val_ptr
->n_components
<2)
349 TTCN_EncDec_ErrorContext::error_internal
350 ("OBJID must have at least 2 components.");
351 V_len
=(min_needed_bits(val_ptr
->components_ptr
[0]*40
352 +val_ptr
->components_ptr
[1])+6)/7;
353 for(int i
=2; i
<val_ptr
->n_components
; i
++)
354 V_len
+=(min_needed_bits(val_ptr
->components_ptr
[i
])+6)/7;
356 case TTCN_Typedescriptor_t::ROID
:
357 for(int i
=0; i
<val_ptr
->n_components
; i
++)
358 V_len
+=(min_needed_bits(val_ptr
->components_ptr
[i
])+6)/7;
361 TTCN_EncDec_ErrorContext::error_internal
362 ("Missing/wrong basetype info for type '%s'.", p_td
.name
);
364 new_tlv
=ASN_BER_TLV_t::construct(V_len
, NULL
);
365 unsigned char *Vptr
=new_tlv
->V
.str
.Vstr
;
366 for(int i
=0; i
<val_ptr
->n_components
; i
++) {
368 if(i
==0 && p_td
.asnbasetype
==TTCN_Typedescriptor_t::OBJID
) {
369 ul
=val_ptr
->components_ptr
[0]*40+val_ptr
->components_ptr
[1];
372 else ul
=val_ptr
->components_ptr
[i
];
373 size_t noo
=(min_needed_bits(ul
)+6)/7;
374 for(size_t j
=noo
; j
>0; j
--) {
375 Vptr
[j
-1]=(ul
& 0x7F) | 0x80;
382 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
387 boolean
OBJID::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
388 const ASN_BER_TLV_t
& p_tlv
,
393 ASN_BER_TLV_t stripped_tlv
;
394 BER_decode_strip_tags(*p_td
.ber
, p_tlv
, L_form
, stripped_tlv
);
395 TTCN_EncDec_ErrorContext
ec("While decoding OBJID type: ");
396 stripped_tlv
.chk_constructed_flag(FALSE
);
397 if (!stripped_tlv
.isComplete
) return FALSE
;
398 if (!stripped_tlv
.V_tlvs_selected
&& stripped_tlv
.V
.str
.Vlen
==0) {
399 ec
.error(TTCN_EncDec::ET_INVAL_MSG
, "Length of V-part is 0.");
402 switch(p_td
.asnbasetype
) {
403 case TTCN_Typedescriptor_t::OBJID
:
404 case TTCN_Typedescriptor_t::ROID
:
407 TTCN_EncDec_ErrorContext::error_internal
408 ("Missing/wrong basetype info for type '%s'.", p_td
.name
);
410 unsigned char *Vptr
=stripped_tlv
.V
.str
.Vstr
;
411 boolean eoc
=FALSE
; // end-of-component
413 unsigned long long ull
=0;
414 STATIC_ASSERT(sizeof(ull
) > sizeof(objid_element
));
416 boolean err_repr
=FALSE
;
417 while (Vptr
< stripped_tlv
.V
.str
.Vstr
+ stripped_tlv
.V
.str
.Vlen
) {
419 if ((*Vptr
& 0x80) && err_repr
==FALSE
) { // not-eoc
420 if (ull
& unsigned_llong_7msb
) {
421 ec
.error(TTCN_EncDec::ET_REPR
,
422 "Value of the #%d component is too big.", i
+1);
429 if (i
==0 && p_td
.asnbasetype
==TTCN_Typedescriptor_t::OBJID
) {
430 // first two component of objid
439 (*this)[1]=(int)(ull
-40*(*this)[0]);
442 else { // other components (>2)
443 // objid_element is UINT/ULONG; the result of the cast is Uxxx_MAX.
444 // It's computed at compile time.
445 if(ull
> ((objid_element
)-1)) {
447 ec
.error(TTCN_EncDec::ET_REPR
,
448 "Value of the #%d component is too big.", i
+1);
449 (*this)[i
]=(objid_element
)-1;
450 // remember the first overflow
451 if (val_ptr
->overflow_idx
< 0) val_ptr
->overflow_idx
= i
;
454 (*this)[i
]=(objid_element
)ull
;
464 ec
.error(TTCN_EncDec::ET_INVAL_MSG
,
465 "The last component (#%d) is unterminated.", i
+1);
470 int OBJID::XER_encode(const XERdescriptor_t
& p_td
,
471 TTCN_Buffer
& p_buf
, unsigned int flavor
, int indent
, embed_values_enc_struct_t
*) const
474 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
475 "Encoding an unbound object identifier value.");
477 int encoded_length
=(int)p_buf
.get_len();
479 flavor
|= SIMPLE_TYPE
;
480 flavor
&= ~XER_RECOF
; // object identifier doesn't care
481 begin_xml(p_td
, p_buf
, flavor
, indent
, false);
483 static char str_buf
[64];
484 for (int i
= 0; i
< val_ptr
->n_components
; ++i
) {
485 // output dot before the second and subsequent components
486 if (i
> 0) p_buf
.put_c('.');
487 // output current component
488 int str_len
= snprintf(str_buf
, sizeof(str_buf
), OBJID_FMT
,
489 val_ptr
->components_ptr
[i
]);
490 if (str_len
< 0 || str_len
>= (int)sizeof(str_buf
)) {
491 TTCN_error("Internal error: system call snprintf() returned "
492 "unexpected status code %d when converting value " OBJID_FMT
,
493 str_len
, val_ptr
->components_ptr
[i
]);
495 else p_buf
.put_s(str_len
, (const unsigned char*)str_buf
);
498 end_xml(p_td
, p_buf
, flavor
, indent
, false);
500 return (int)p_buf
.get_len() - encoded_length
;
503 void OBJID::from_string(char* p_str
)
505 // Count dots to find number of components. (1 dot = 2 components, etc.)
508 for (p
= p_str
; *p
!= 0; ++p
) {
509 if (*p
== '.') ++comps
;
511 // p now points at the end of the string. If it was empty, then there were
512 // no components; compensate the fact that we started at 1.
513 init_struct((p
!= p_str
) ? comps
: 0);
517 for (beg
= p_str
; beg
< p
; ++beg
) {
519 long ret
= strtol(beg
, &end
, 10);
522 // TODO check value for too big ?
523 (*this)[comps
++] = ret
;
524 beg
= end
; // move to the dot; will move past it when incremented
528 int OBJID::XER_decode(const XERdescriptor_t
& p_td
, XmlReaderWrap
& reader
,
529 unsigned int flavor
, embed_values_dec_struct_t
*)
531 int exer
= is_exer(flavor
);
532 int success
= reader
.Ok(), depth
= -1;
533 for (; success
== 1; success
= reader
.Read()) {
534 int type
= reader
.NodeType();
535 if (XML_READER_TYPE_ELEMENT
== type
) {
536 verify_name(reader
, p_td
, exer
);
537 depth
= reader
.Depth();
542 char * val
= (char *)reader
.ReadString(); // We own this (writable) string
544 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INVAL_MSG
, "Bogus object identifier");
552 for (success
= reader
.Read(); success
== 1; success
= reader
.Read()) {
553 int type
= reader
.NodeType();
554 if (XML_READER_TYPE_END_ELEMENT
== type
) {
555 verify_end(reader
, p_td
, depth
, exer
);
560 return 1; // decode successful
563 int OBJID::JSON_encode(const TTCN_Typedescriptor_t
&, JSON_Tokenizer
& p_tok
) const
566 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
567 "Encoding an unbound object identifier value.");
571 char* objid_str
= mcopystrn("\"", 1);
572 for (int i
= 0; i
< val_ptr
->n_components
; ++i
) {
573 objid_str
= mputprintf(objid_str
, "%s" OBJID_FMT
, (i
> 0 ? "." : ""), val_ptr
->components_ptr
[i
]);
575 objid_str
= mputstrn(objid_str
, "\"", 1);
576 int enc_len
= p_tok
.put_next_token(JSON_TOKEN_STRING
, objid_str
);
581 int OBJID::JSON_decode(const TTCN_Typedescriptor_t
& p_td
, JSON_Tokenizer
& p_tok
, boolean p_silent
)
583 json_token_t token
= JSON_TOKEN_NONE
;
585 size_t value_len
= 0;
586 boolean error
= false;
588 boolean use_default
= p_td
.json
->default_value
&& 0 == p_tok
.get_buffer_length();
590 // No JSON data in the buffer -> use default value
591 value
= (char*)p_td
.json
->default_value
;
592 value_len
= strlen(value
);
594 dec_len
= p_tok
.get_next_token(&token
, &value
, &value_len
);
596 if (JSON_TOKEN_ERROR
== token
) {
597 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_BAD_TOKEN_ERROR
, "");
598 return JSON_ERROR_FATAL
;
600 else if (JSON_TOKEN_STRING
== token
|| use_default
) {
601 if (use_default
|| (value_len
> 2 && value
[0] == '\"' && value
[value_len
- 1] == '\"')) {
603 // The default value doesn't have quotes around it
607 // need a null-terminated string
608 char* value2
= mcopystrn(value
, value_len
);
614 return JSON_ERROR_INVALID_TOKEN
;
618 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_FORMAT_ERROR
, "string", "object identifier");
622 return JSON_ERROR_FATAL
;
627 void OBJID_template::clean_up()
629 if (template_selection
== VALUE_LIST
||
630 template_selection
== COMPLEMENTED_LIST
) delete [] value_list
.list_value
;
631 template_selection
= UNINITIALIZED_TEMPLATE
;
634 void OBJID_template::copy_template(const OBJID_template
& other_value
)
636 switch (other_value
.template_selection
) {
638 single_value
= other_value
.single_value
;
645 case COMPLEMENTED_LIST
:
646 value_list
.n_values
= other_value
.value_list
.n_values
;
647 value_list
.list_value
= new OBJID_template
[value_list
.n_values
];
648 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
649 value_list
.list_value
[i
].copy_template(
650 other_value
.value_list
.list_value
[i
]);
653 TTCN_error("Copying an uninitialized/unsupported objid template.");
655 set_selection(other_value
);
658 OBJID_template::OBJID_template()
663 OBJID_template::OBJID_template(template_sel other_value
)
664 : Base_Template(other_value
)
666 check_single_selection(other_value
);
669 OBJID_template::OBJID_template(const OBJID
& other_value
)
670 : Base_Template(SPECIFIC_VALUE
), single_value(other_value
)
675 OBJID_template::OBJID_template(const OPTIONAL
<OBJID
>& other_value
)
677 switch (other_value
.get_selection()) {
678 case OPTIONAL_PRESENT
:
679 set_selection(SPECIFIC_VALUE
);
680 single_value
= (const OBJID
&)other_value
;
683 set_selection(OMIT_VALUE
);
686 TTCN_error("Creating an objid template from an unbound optional field.");
690 OBJID_template::OBJID_template(const OBJID_template
& other_value
)
693 copy_template(other_value
);
696 OBJID_template::~OBJID_template()
701 OBJID_template
& OBJID_template::operator=(template_sel other_value
)
703 check_single_selection(other_value
);
705 set_selection(other_value
);
709 OBJID_template
& OBJID_template::operator=(const OBJID
& other_value
)
711 if (!other_value
.is_bound())
712 TTCN_error("Assignment of an unbound objid value to a template.");
714 set_selection(SPECIFIC_VALUE
);
715 single_value
= other_value
;
719 OBJID_template
& OBJID_template::operator=(const OPTIONAL
<OBJID
>& other_value
)
722 switch (other_value
.get_selection()) {
723 case OPTIONAL_PRESENT
:
724 set_selection(SPECIFIC_VALUE
);
725 single_value
= (const OBJID
&)other_value
;
728 set_selection(OMIT_VALUE
);
731 TTCN_error("Assignment of an unbound optional field to an objid template.");
736 OBJID_template
& OBJID_template::operator=(const OBJID_template
& other_value
)
738 if (&other_value
!= this) {
740 copy_template(other_value
);
745 boolean
OBJID_template::match(const OBJID
& other_value
) const
747 if (!other_value
.is_bound()) return FALSE
;
748 switch (template_selection
) {
750 return single_value
== other_value
;
757 case COMPLEMENTED_LIST
:
758 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
759 if (value_list
.list_value
[i
].match(other_value
))
760 return template_selection
== VALUE_LIST
;
761 return template_selection
== COMPLEMENTED_LIST
;
763 TTCN_error("Matching with an uninitialized/unsupported objid template.");
768 const OBJID
& OBJID_template::valueof() const
770 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
771 TTCN_error("Performing a valueof "
772 "or send operation on a non-specific objid template.");
776 int OBJID_template::size_of() const
778 switch (template_selection
)
781 return single_value
.size_of();
783 TTCN_error("Performing sizeof() operation on an objid template "
784 "containing omit value.");
787 TTCN_error("Performing sizeof() operation on a */? objid template.");
790 if (value_list
.n_values
<1)
791 TTCN_error("Internal error: "
792 "Performing sizeof() operation on an objid template "
793 "containing an empty list.");
794 int item_size
= value_list
.list_value
[0].size_of();
795 for (unsigned int i
= 1; i
< value_list
.n_values
; i
++) {
796 if (value_list
.list_value
[i
].size_of()!=item_size
)
797 TTCN_error("Performing sizeof() operation on an objid template "
798 "containing a value list with different sizes.");
802 case COMPLEMENTED_LIST
:
803 TTCN_error("Performing sizeof() operation on an objid template "
804 "containing complemented list.");
806 TTCN_error("Performing sizeof() operation on an "
807 "uninitialized/unsupported objid template.");
812 void OBJID_template::set_type(template_sel template_type
,
813 unsigned int list_length
)
815 if (template_type
!= VALUE_LIST
&& template_type
!= COMPLEMENTED_LIST
)
816 TTCN_error("Setting an invalid list type for an objid template.");
818 set_selection(template_type
);
819 value_list
.n_values
= list_length
;
820 value_list
.list_value
= new OBJID_template
[list_length
];
823 OBJID_template
& OBJID_template::list_item(unsigned int list_index
)
825 if (template_selection
!= VALUE_LIST
&&
826 template_selection
!= COMPLEMENTED_LIST
)
827 TTCN_error("Accessing a list element of a non-list objid template.");
828 if (list_index
>= value_list
.n_values
)
829 TTCN_error("Index overflow in an objid value list template.");
830 return value_list
.list_value
[list_index
];
833 void OBJID_template::log() const
835 switch (template_selection
) {
839 case COMPLEMENTED_LIST
:
840 TTCN_Logger::log_event_str("complement ");
843 TTCN_Logger::log_char('(');
844 for(unsigned int i
= 0; i
< value_list
.n_values
; i
++) {
845 if (i
> 0) TTCN_Logger::log_event_str(", ");
846 value_list
.list_value
[i
].log();
848 TTCN_Logger::log_char(')');
857 void OBJID_template::log_match(const OBJID
& match_value
) const
859 if (TTCN_Logger::VERBOSITY_COMPACT
== TTCN_Logger::get_matching_verbosity()
860 && TTCN_Logger::get_logmatch_buffer_len() != 0) {
861 TTCN_Logger::print_logmatch_buffer();
862 TTCN_Logger::log_event_str(" := ");
865 TTCN_Logger::log_event_str(" with ");
867 if (match(match_value
)) TTCN_Logger::log_event_str(" matched");
868 else TTCN_Logger::log_event_str(" unmatched");
871 void OBJID_template::set_param(Module_Param
& param
) {
872 param
.basic_check(Module_Param::BC_TEMPLATE
, "objid template");
873 switch (param
.get_type()) {
874 case Module_Param::MP_Omit
:
877 case Module_Param::MP_Any
:
880 case Module_Param::MP_AnyOrNone
:
883 case Module_Param::MP_List_Template
:
884 case Module_Param::MP_ComplementList_Template
:
885 set_type(param
.get_type()==Module_Param::MP_List_Template
? VALUE_LIST
: COMPLEMENTED_LIST
, param
.get_size());
886 for (size_t i
=0; i
<param
.get_size(); i
++) {
887 list_item(i
).set_param(*param
.get_elem(i
));
890 case Module_Param::MP_Objid
:
891 if (sizeof(OBJID::objid_element
)!=sizeof(int)) TTCN_error("Internal error: OBJID_template::set_param()");
892 *this = OBJID(param
.get_string_size(), (OBJID::objid_element
*)param
.get_string_data());
894 //case Module_Param::MP_Objid_Template:
898 param
.type_error("objid template");
900 is_ifpresent
= param
.get_ifpresent();
903 void OBJID_template::encode_text(Text_Buf
& text_buf
) const
905 encode_text_base(text_buf
);
906 switch (template_selection
) {
912 single_value
.encode_text(text_buf
);
915 case COMPLEMENTED_LIST
:
916 text_buf
.push_int(value_list
.n_values
);
917 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
918 value_list
.list_value
[i
].encode_text(text_buf
);
921 TTCN_error("Text encoder: Encoding an undefined/unsupported objid "
926 void OBJID_template::decode_text(Text_Buf
& text_buf
)
929 decode_text_base(text_buf
);
930 switch (template_selection
) {
936 single_value
.decode_text(text_buf
);
939 case COMPLEMENTED_LIST
:
940 value_list
.n_values
= text_buf
.pull_int().get_val();
941 value_list
.list_value
= new OBJID_template
[value_list
.n_values
];
942 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
943 value_list
.list_value
[i
].decode_text(text_buf
);
946 TTCN_error("Text decoder: An unknown/unsupported selection was "
947 "received for an objid template.");
951 boolean
OBJID_template::is_present() const
953 if (template_selection
==UNINITIALIZED_TEMPLATE
) return FALSE
;
954 return !match_omit();
957 boolean
OBJID_template::match_omit() const
959 if (is_ifpresent
) return TRUE
;
960 switch (template_selection
) {
965 case COMPLEMENTED_LIST
:
966 for (unsigned int i
=0; i
<value_list
.n_values
; i
++)
967 if (value_list
.list_value
[i
].match_omit())
968 return template_selection
==VALUE_LIST
;
969 return template_selection
==COMPLEMENTED_LIST
;
976 #ifndef TITAN_RUNTIME_2
977 void OBJID_template::check_restriction(template_res t_res
, const char* t_name
) const
979 if (template_selection
==UNINITIALIZED_TEMPLATE
) return;
980 switch ((t_name
&&(t_res
==TR_VALUE
))?TR_OMIT
:t_res
) {
982 if (!is_ifpresent
&& template_selection
==SPECIFIC_VALUE
) return;
985 if (!is_ifpresent
&& (template_selection
==OMIT_VALUE
||
986 template_selection
==SPECIFIC_VALUE
)) return;
989 if (!match_omit()) return;
994 TTCN_error("Restriction `%s' on template of type %s violated.",
995 get_res_name(t_res
), t_name
? t_name
: "objid");