1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 ///////////////////////////////////////////////////////////////////////////////
11 #include "../common/memory.h"
12 #include "Parameters.h"
13 #include "Param_Types.hh"
20 #include "Charstring.hh"
21 #include "XmlReader.hh"
23 static const Token_Match
boolean_true_match("^(true).*$",TRUE
);
24 static const Token_Match
boolean_false_match("^(false).*$",TRUE
);
31 BOOLEAN::BOOLEAN(boolean other_value
)
34 boolean_value
= other_value
;
37 BOOLEAN::BOOLEAN(const BOOLEAN
& other_value
)
38 : Base_Type(other_value
)
40 other_value
.must_bound("Copying an unbound boolean value.");
42 boolean_value
= other_value
.boolean_value
;
45 BOOLEAN
& BOOLEAN::operator=(boolean other_value
)
48 boolean_value
= other_value
;
52 BOOLEAN
& BOOLEAN::operator=(const BOOLEAN
& other_value
)
54 other_value
.must_bound("Assignment of an unbound boolean value.");
56 boolean_value
= other_value
.boolean_value
;
60 boolean
BOOLEAN::operator!() const
62 must_bound("The operand of not operator is an unbound boolean value.");
63 return !boolean_value
;
66 boolean
BOOLEAN::operator&&(boolean other_value
) const
68 must_bound("The left operand of and operator is an unbound boolean value.");
69 return boolean_value
&& other_value
;
72 boolean
BOOLEAN::operator&&(const BOOLEAN
& other_value
) const
74 must_bound("The left operand of and operator is an unbound boolean value.");
75 if (!boolean_value
) return FALSE
;
76 other_value
.must_bound("The right operand of and operator is an unbound "
78 return other_value
.boolean_value
;
81 boolean
BOOLEAN::operator^(boolean other_value
) const
83 must_bound("The left operand of xor operator is an unbound boolean value.");
84 return boolean_value
!= other_value
;
87 boolean
BOOLEAN::operator^(const BOOLEAN
& other_value
) const
89 must_bound("The left operand of xor operator is an unbound boolean value.");
90 other_value
.must_bound("The right operand of xor operator is an unbound "
92 return boolean_value
!= other_value
.boolean_value
;
95 boolean
BOOLEAN::operator||(boolean other_value
) const
97 must_bound("The left operand of or operator is an unbound boolean value.");
98 return boolean_value
|| other_value
;
101 boolean
BOOLEAN::operator||(const BOOLEAN
& other_value
) const
103 must_bound("The left operand of or operator is an unbound boolean value.");
104 if (boolean_value
) return TRUE
;
105 other_value
.must_bound("The right operand of or operator is an unbound "
107 return other_value
.boolean_value
;
110 boolean
BOOLEAN::operator==(boolean other_value
) const
112 must_bound("The left operand of comparison is an unbound boolean value.");
113 return boolean_value
== other_value
;
116 boolean
BOOLEAN::operator==(const BOOLEAN
& other_value
) const
118 must_bound("The left operand of comparison is an unbound boolean value.");
119 other_value
.must_bound("The right operand of comparison is an unbound "
121 return boolean_value
== other_value
.boolean_value
;
124 BOOLEAN::operator boolean() const
126 must_bound("Using the value of an unbound boolean variable.");
127 return boolean_value
;
130 void BOOLEAN::clean_up()
135 void BOOLEAN::log() const
137 if (bound_flag
) TTCN_Logger::log_event_str(boolean_value
? "true" : "false");
138 else TTCN_Logger::log_event_unbound();
141 void BOOLEAN::encode_text(Text_Buf
& text_buf
) const
143 must_bound("Text encoder: Encoding an unbound boolean value.");
144 text_buf
.push_int(boolean_value
? 1 : 0);
147 void BOOLEAN::decode_text(Text_Buf
& text_buf
)
149 int int_value
= text_buf
.pull_int().get_val();
152 boolean_value
= FALSE
;
155 boolean_value
= TRUE
;
158 TTCN_error("Text decoder: An invalid boolean value (%d) was received.",
164 void BOOLEAN::set_param(Module_Param
& param
) {
165 param
.basic_check(Module_Param::BC_VALUE
, "boolean value");
166 Module_Param_Ptr mp
= ¶m
;
167 if (param
.get_type() == Module_Param::MP_Reference
) {
168 mp
= param
.get_referenced_param();
170 if (mp
->get_type()!=Module_Param::MP_Boolean
) param
.type_error("boolean value");
172 boolean_value
= mp
->get_boolean();
175 Module_Param
* BOOLEAN::get_param(Module_Param_Name
& /* param_name */) const
178 return new Module_Param_Unbound();
180 return new Module_Param_Boolean(boolean_value
);
183 void BOOLEAN::encode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
184 TTCN_EncDec::coding_t p_coding
, ...) const
187 va_start(pvar
, p_coding
);
189 case TTCN_EncDec::CT_BER
: {
190 TTCN_EncDec_ErrorContext
ec("While BER-encoding type '%s': ", p_td
.name
);
191 unsigned BER_coding
=va_arg(pvar
, unsigned);
192 BER_encode_chk_coding(BER_coding
);
193 ASN_BER_TLV_t
*tlv
=BER_encode_TLV(p_td
, BER_coding
);
194 tlv
->put_in_buffer(p_buf
);
195 ASN_BER_TLV_t::destruct(tlv
);
197 case TTCN_EncDec::CT_RAW
: {
198 TTCN_EncDec_ErrorContext
ec("While RAW-encoding type '%s': ", p_td
.name
);
200 TTCN_EncDec_ErrorContext::error_internal
201 ("No RAW descriptor available for type '%s'.", p_td
.name
);
205 RAW_enc_tree
root(TRUE
,NULL
,&rp
,1,p_td
.raw
);
206 RAW_encode(p_td
, root
);
207 root
.put_to_buf(p_buf
);
209 case TTCN_EncDec::CT_TEXT
: {
210 TTCN_EncDec_ErrorContext
ec("While TEXT-encoding type '%s': ", p_td
.name
);
212 TTCN_EncDec_ErrorContext::error_internal
213 ("No TEXT descriptor available for type '%s'.", p_td
.name
);
214 TEXT_encode(p_td
,p_buf
);
216 case TTCN_EncDec::CT_XER
: {
217 TTCN_EncDec_ErrorContext
ec("While XER-encoding type '%s': ", p_td
.name
);
218 unsigned XER_coding
=va_arg(pvar
, unsigned);
219 XER_encode(*p_td
.xer
, p_buf
, XER_coding
, 0, 0);
221 case TTCN_EncDec::CT_JSON
: {
222 TTCN_EncDec_ErrorContext
ec("While JSON-encoding type '%s': ", p_td
.name
);
224 TTCN_EncDec_ErrorContext::error_internal
225 ("No JSON descriptor available for type '%s'.", p_td
.name
);
226 JSON_Tokenizer
tok(va_arg(pvar
, int) != 0);
227 JSON_encode(p_td
, tok
);
228 p_buf
.put_s(tok
.get_buffer_length(), (const unsigned char*)tok
.get_buffer());
231 TTCN_error("Unknown coding method requested to encode type '%s'",
237 void BOOLEAN::decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
238 TTCN_EncDec::coding_t p_coding
, ...)
241 va_start(pvar
, p_coding
);
243 case TTCN_EncDec::CT_BER
: {
244 TTCN_EncDec_ErrorContext
ec("While BER-decoding type '%s': ", p_td
.name
);
245 unsigned L_form
=va_arg(pvar
, unsigned);
247 BER_decode_str2TLV(p_buf
, tlv
, L_form
);
248 BER_decode_TLV(p_td
, tlv
, L_form
);
249 if(tlv
.isComplete
) p_buf
.increase_pos(tlv
.get_len());
251 case TTCN_EncDec::CT_RAW
: {
252 TTCN_EncDec_ErrorContext
ec("While RAW-decoding type '%s': ", p_td
.name
);
254 TTCN_EncDec_ErrorContext::error_internal
255 ("No RAW descriptor available for type '%s'.", p_td
.name
);
257 switch(p_td
.raw
->top_bit_order
){
265 if(RAW_decode(p_td
, p_buf
, p_buf
.get_len()*8, order
)<0)
266 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
267 "Can not decode type '%s', because invalid or incomplete"
268 " message was received"
271 case TTCN_EncDec::CT_TEXT
: {
272 Limit_Token_List limit
;
273 TTCN_EncDec_ErrorContext
ec("While TEXT-decoding type '%s': ", p_td
.name
);
275 TTCN_EncDec_ErrorContext::error_internal
276 ("No TEXT descriptor available for type '%s'.", p_td
.name
);
277 const unsigned char *b
=p_buf
.get_data();
278 if(b
[p_buf
.get_len()-1]!='\0'){
279 p_buf
.set_pos(p_buf
.get_len());
280 p_buf
.put_zero(8,ORDER_LSB
);
283 if(TEXT_decode(p_td
,p_buf
,limit
)<0)
284 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
285 "Can not decode type '%s', because invalid or incomplete"
286 " message was received"
289 case TTCN_EncDec::CT_XER
: {
290 TTCN_EncDec_ErrorContext
ec("While XER-decoding type '%s': ", p_td
.name
);
291 unsigned XER_coding
=va_arg(pvar
, unsigned);
292 XmlReaderWrap
reader(p_buf
);
293 for (int success
= reader
.Read(); success
==1; success
=reader
.Read()) {
294 int type
= reader
.NodeType();
295 if (type
==XML_READER_TYPE_ELEMENT
)
298 XER_decode(*p_td
.xer
, reader
, XER_coding
, 0);
299 size_t bytes
= reader
.ByteConsumed();
300 p_buf
.set_pos(bytes
);
302 case TTCN_EncDec::CT_JSON
: {
303 TTCN_EncDec_ErrorContext
ec("While JSON-decoding type '%s': ", p_td
.name
);
305 TTCN_EncDec_ErrorContext::error_internal
306 ("No JSON descriptor available for type '%s'.", p_td
.name
);
307 JSON_Tokenizer
tok((const char*)p_buf
.get_data(), p_buf
.get_len());
308 if(JSON_decode(p_td
, tok
, false)<0)
309 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
310 "Can not decode type '%s', because invalid or incomplete"
311 " message was received"
313 p_buf
.set_pos(tok
.get_buf_pos());
316 TTCN_error("Unknown coding method requested to decode type '%s'",
323 BOOLEAN::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
324 unsigned p_coding
) const
327 ASN_BER_TLV_t
*new_tlv
=BER_encode_chk_bound(is_bound());
329 new_tlv
=ASN_BER_TLV_t::construct(1, NULL
);
330 new_tlv
->V
.str
.Vstr
[0]=boolean_value
==TRUE
?0xFF:0x00;
332 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
336 boolean
BOOLEAN::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
337 const ASN_BER_TLV_t
& p_tlv
,
342 ASN_BER_TLV_t stripped_tlv
;
343 BER_decode_strip_tags(*p_td
.ber
, p_tlv
, L_form
, stripped_tlv
);
344 TTCN_EncDec_ErrorContext
ec("While decoding BOOLEAN type: ");
345 stripped_tlv
.chk_constructed_flag(FALSE
);
346 if (!stripped_tlv
.isComplete
) return FALSE
;
347 if(stripped_tlv
.V
.str
.Vlen
!=1)
348 ec
.error(TTCN_EncDec::ET_INVAL_MSG
,
349 "Length of V-part is %lu (instead of 1).",
350 (unsigned long) stripped_tlv
.V
.str
.Vlen
);
351 if(stripped_tlv
.V
.str
.Vlen
>=1) {
352 switch(stripped_tlv
.V
.str
.Vstr
[0]) {
367 int BOOLEAN::TEXT_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
368 Limit_Token_List
&, boolean no_err
, boolean
/*first_call*/)
371 int decoded_length
= 0;
373 if (p_td
.text
->begin_decode
) {
375 if ((tl
= p_td
.text
->begin_decode
->match_begin(buff
)) < 0) {
376 if (no_err
) return -1;
377 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
378 "The specified token '%s' not found for '%s': ",
379 (const char*) *(p_td
.text
->begin_decode
), p_td
.name
);
382 decoded_length
+= tl
;
383 buff
.increase_pos(tl
);
385 if (buff
.get_read_len() < 1 && no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
387 boolean found
= FALSE
;
389 if ( p_td
.text
->val
.bool_values
390 && p_td
.text
->val
.bool_values
->true_decode_token
) {
392 if ((tl
= p_td
.text
->val
.bool_values
->true_decode_token
->match_begin(buff
)) > -1) {
395 boolean_value
= TRUE
;
400 if ((tl
= boolean_true_match
.match_begin(buff
)) >= 0) {
403 boolean_value
= TRUE
;
408 if ( p_td
.text
->val
.bool_values
409 && p_td
.text
->val
.bool_values
->false_decode_token
) {
411 if ((tl
= p_td
.text
->val
.bool_values
->false_decode_token
->match_begin(buff
)) > -1) {
414 boolean_value
= FALSE
;
419 if ((tl
= boolean_false_match
.match_begin(buff
)) >= 0) {
422 boolean_value
= FALSE
;
428 decoded_length
+= str_len
;
429 buff
.increase_pos(str_len
);
432 if (no_err
) return -1;
433 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
434 "No boolean token found for '%s': ", p_td
.name
);
435 return decoded_length
;
438 if (p_td
.text
->end_decode
) {
440 if ((tl
= p_td
.text
->end_decode
->match_begin(buff
)) < 0) {
441 if (no_err
) return -1;
442 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
443 "The specified token '%s' not found for '%s': ",
444 (const char*) *(p_td
.text
->end_decode
), p_td
.name
);
447 decoded_length
+= tl
;
448 buff
.increase_pos(tl
);
451 return decoded_length
;
455 int BOOLEAN::TEXT_encode(const TTCN_Typedescriptor_t
& p_td
,
456 TTCN_Buffer
& buff
) const{
457 int encoded_length
=0;
458 if(p_td
.text
->begin_encode
){
459 buff
.put_cs(*p_td
.text
->begin_encode
);
460 encoded_length
+=p_td
.text
->begin_encode
->lengthof();
463 TTCN_EncDec_ErrorContext::error
464 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound value.");
465 if(p_td
.text
->end_encode
){
466 buff
.put_cs(*p_td
.text
->end_encode
);
467 encoded_length
+=p_td
.text
->end_encode
->lengthof();
469 return encoded_length
;
472 if(p_td
.text
->val
.bool_values
==NULL
){
474 buff
.put_s(4,(const unsigned char*)"true");
478 buff
.put_s(5,(const unsigned char*)"false");
483 if(p_td
.text
->val
.bool_values
->true_encode_token
){
484 buff
.put_cs(*p_td
.text
->val
.bool_values
->true_encode_token
);
485 encoded_length
+=p_td
.text
->
486 val
.bool_values
->true_encode_token
->lengthof();
488 buff
.put_s(4,(const unsigned char*)"true");
493 if(p_td
.text
->val
.bool_values
->false_encode_token
){
494 buff
.put_cs(*p_td
.text
->val
.bool_values
->false_encode_token
);
495 encoded_length
+=p_td
.text
->
496 val
.bool_values
->false_encode_token
->lengthof();
498 buff
.put_s(5,(const unsigned char*)"false");
504 if(p_td
.text
->end_encode
){
505 buff
.put_cs(*p_td
.text
->end_encode
);
506 encoded_length
+=p_td
.text
->end_encode
->lengthof();
508 return encoded_length
;
511 int BOOLEAN::RAW_encode(const TTCN_Typedescriptor_t
& p_td
, RAW_enc_tree
& myleaf
) const
514 int loc_length
= p_td
.raw
->fieldlength
? p_td
.raw
->fieldlength
: 1;
515 int length
= (loc_length
+ 7) / 8;
518 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
519 "Encoding an unbound value.");
522 else tmp
= boolean_value
? 0xFF : 0x00;
523 // myleaf.ext_bit=EXT_BIT_NO;
524 if (myleaf
.must_free
) Free(myleaf
.body
.leaf
.data_ptr
);
525 if (length
> RAW_INT_ENC_LENGTH
) {
526 myleaf
.body
.leaf
.data_ptr
= bc
= (unsigned char*)Malloc(length
*sizeof(*bc
));
527 myleaf
.must_free
= TRUE
;
528 myleaf
.data_ptr_used
= TRUE
;
530 else bc
= myleaf
.body
.leaf
.data_array
;
532 memset(bc
, tmp
, length
* sizeof(*bc
));
533 return myleaf
.length
= loc_length
;
536 int BOOLEAN::RAW_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
537 int limit
, raw_order_t top_bit_ord
, boolean no_err
, int /*sel_field*/,
538 boolean
/*first_call*/)
541 int prepaddlength
= buff
.increase_pos_padd(p_td
.raw
->prepadding
);
542 limit
-= prepaddlength
;
543 int decode_length
= p_td
.raw
->fieldlength
> 0 ? p_td
.raw
->fieldlength
: 1;
544 if (decode_length
> limit
) {
545 if (no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
546 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
547 "There is not enough bits in the buffer to decode type %s (needed: %d, "
548 "found: %d).", p_td
.name
, decode_length
, limit
);
549 decode_length
= limit
;
551 int nof_unread_bits
= buff
.unread_len_bit();
552 if (decode_length
> nof_unread_bits
) {
553 if (no_err
) return -TTCN_EncDec::ET_INCOMPL_MSG
;
554 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG
,
555 "There is not enough bits in the buffer to decode type %s (needed: %d, "
556 "found: %d).", p_td
.name
, decode_length
, nof_unread_bits
);
557 decode_length
= nof_unread_bits
;
559 if (decode_length
< 0) return -1;
560 else if (decode_length
== 0) boolean_value
= FALSE
;
563 boolean orders
= FALSE
;
564 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= TRUE
;
565 if (p_td
.raw
->bitorderinfield
== ORDER_MSB
) orders
= !orders
;
566 cp
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
568 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= TRUE
;
569 if (p_td
.raw
->bitorderinfield
== ORDER_MSB
) orders
= !orders
;
570 cp
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
571 cp
.fieldorder
= p_td
.raw
->fieldorder
;
572 cp
.hexorder
= ORDER_LSB
;
573 int length
= (decode_length
+ 7) / 8;
574 unsigned char *data
= (unsigned char*)Malloc(length
*sizeof(unsigned char));
575 buff
.get_b((size_t)decode_length
, data
, cp
, top_bit_ord
);
576 if(decode_length
% 8){
577 data
[length
- 1] &= BitMaskTable
[decode_length
% 8];
579 unsigned char ch
= '\0';
580 for (int a
= 0; a
< length
; a
++) ch
|= data
[a
];
582 boolean_value
= ch
!= '\0';
585 decode_length
+= buff
.increase_pos_padd(p_td
.raw
->padding
);
586 return decode_length
+ prepaddlength
;
589 int BOOLEAN::XER_encode(const XERdescriptor_t
& p_td
,
590 TTCN_Buffer
& p_buf
, unsigned int flavor
, int indent
, embed_values_enc_struct_t
*) const
593 TTCN_EncDec_ErrorContext::error
594 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound boolean value.");
596 int encoded_length
=(int)p_buf
.get_len();
598 int exer
= is_exer(flavor
);
600 flavor
|= (SIMPLE_TYPE
| BXER_EMPTY_ELEM
);
601 if (begin_xml(p_td
, p_buf
, flavor
, indent
, false) == -1) --encoded_length
;
604 if (p_td
.xer_bits
& XER_TEXT
) {
605 p_buf
.put_c(boolean_value
? '1' : '0');
608 if (boolean_value
) p_buf
.put_s(4, (cbyte
*)"true");
609 else p_buf
.put_s(5, (cbyte
*)"false");
613 if (boolean_value
) p_buf
.put_s(7, (cbyte
*)"<true/>");
614 else p_buf
.put_s(8, (cbyte
*)"<false/>");
617 end_xml(p_td
, p_buf
, flavor
, indent
, false);
619 return (int)p_buf
.get_len() - encoded_length
;
622 int BOOLEAN::XER_decode(const XERdescriptor_t
& p_td
, XmlReaderWrap
& reader
,
623 unsigned int flavor
, embed_values_dec_struct_t
*)
625 const boolean exer
= is_exer(flavor
);
626 int XMLValueList
= !exer
&& is_record_of(flavor
);
627 const boolean notag
= (exer
&& (p_td
.xer_bits
& (UNTAGGED
))) ||
628 is_exerlist(flavor
) || XMLValueList
;
629 int depth
= -1, success
, type
;
630 const char *value
= 0;
632 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
633 verify_name(reader
, p_td
, exer
);
634 value
= (const char *)reader
.Value();
637 for (success
= reader
.Ok(); success
== 1; success
= reader
.Read()) {
638 type
= reader
.NodeType();
639 if (!notag
&& depth
== -1) {
640 if (XML_READER_TYPE_ELEMENT
== type
) {
641 verify_name(reader
, p_td
, exer
);
642 depth
= reader
.Depth();
644 if (exer
&& (p_td
.dfeValue
!= 0) && reader
.IsEmptyElement()) {
645 *this = *static_cast<const BOOLEAN
*>(p_td
.dfeValue
);
652 else { // found the enclosing tag already
653 if (!exer
&& XML_READER_TYPE_ELEMENT
== type
) {
654 // this must be EmptyElement Boolean
655 if (!reader
.IsEmptyElement()) TTCN_EncDec_ErrorContext::error(
656 TTCN_EncDec::ET_INVAL_MSG
, "Boolean must be empty element");
657 value
= (const char*)reader
.LocalName();
659 else if (XML_READER_TYPE_TEXT
== type
) {
661 value
= (const char*)reader
.Value();
664 // Must not modify the buffer when attempting to find the selected alternative for USE-UNION
665 if (!exer
|| !(flavor
& EXIT_ON_ERROR
)) reader
.Read();
669 } // if not attribute
671 if (value
!= 0 && *value
!= 0) {
673 if (value
[1]=='\0' && (*value
& 0x3E) == '0')
676 boolean_value
= *value
== '1';
678 else if (!strcmp(value
, "true")) {
679 boolean_value
= true;
682 else if (!strcmp(value
, "false")) {
683 boolean_value
= false;
689 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) { // I am an attribute
690 // Let the caller do reader.AdvanceAttribute();
693 for (success
= reader
.Ok(); success
== 1; success
= reader
.Read()) {
694 type
= reader
.NodeType();
695 if (XML_READER_TYPE_END_ELEMENT
== type
) {
696 verify_end(reader
, p_td
, depth
, exer
);
697 reader
.Read(); // one last time
706 int BOOLEAN::JSON_encode(const TTCN_Typedescriptor_t
&, JSON_Tokenizer
& p_tok
) const
709 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
710 "Encoding an unbound boolean value.");
713 return p_tok
.put_next_token((boolean_value
) ? JSON_TOKEN_LITERAL_TRUE
: JSON_TOKEN_LITERAL_FALSE
, NULL
);
716 int BOOLEAN::JSON_decode(const TTCN_Typedescriptor_t
& p_td
, JSON_Tokenizer
& p_tok
, boolean p_silent
)
718 json_token_t token
= JSON_TOKEN_NONE
;
720 if (p_td
.json
->default_value
&& 0 == p_tok
.get_buffer_length()) {
721 // No JSON data in the buffer -> use default value
722 if (strcmp(p_td
.json
->default_value
, "true") == 0) {
723 token
= JSON_TOKEN_LITERAL_TRUE
;
726 token
= JSON_TOKEN_LITERAL_FALSE
;
729 dec_len
= p_tok
.get_next_token(&token
, NULL
, NULL
);
731 if (JSON_TOKEN_ERROR
== token
) {
732 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_BAD_TOKEN_ERROR
, "");
733 return JSON_ERROR_FATAL
;
735 else if (JSON_TOKEN_LITERAL_TRUE
== token
) {
737 boolean_value
= true;
739 else if (JSON_TOKEN_LITERAL_FALSE
== token
) {
741 boolean_value
= false;
745 return JSON_ERROR_INVALID_TOKEN
;
751 boolean
operator&&(boolean bool_value
, const BOOLEAN
& other_value
)
753 if (!bool_value
) return FALSE
;
754 other_value
.must_bound("The right operand of and operator is an unbound "
756 return other_value
.boolean_value
;
759 boolean
operator^(boolean bool_value
, const BOOLEAN
& other_value
)
761 other_value
.must_bound("The right operand of xor operator is an unbound "
763 return bool_value
!= other_value
.boolean_value
;
766 boolean
operator||(boolean bool_value
, const BOOLEAN
& other_value
)
768 if (bool_value
) return TRUE
;
769 other_value
.must_bound("The right operand of or operator is an unbound "
771 return other_value
.boolean_value
;
774 boolean
operator==(boolean bool_value
, const BOOLEAN
& other_value
)
776 other_value
.must_bound("The right operand of comparison is an unbound "
778 return bool_value
== other_value
.boolean_value
;
781 void BOOLEAN_template::clean_up()
783 if (template_selection
== VALUE_LIST
||
784 template_selection
== COMPLEMENTED_LIST
)
785 delete [] value_list
.list_value
;
786 template_selection
= UNINITIALIZED_TEMPLATE
;
789 void BOOLEAN_template::copy_template(const BOOLEAN_template
& other_value
)
791 switch (other_value
.template_selection
) {
793 single_value
= other_value
.single_value
;
800 case COMPLEMENTED_LIST
:
801 value_list
.n_values
= other_value
.value_list
.n_values
;
802 value_list
.list_value
= new BOOLEAN_template
[value_list
.n_values
];
803 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
804 value_list
.list_value
[i
].copy_template(
805 other_value
.value_list
.list_value
[i
]);
808 TTCN_error("Copying an uninitialized/unsupported boolean template.");
810 set_selection(other_value
);
813 BOOLEAN_template::BOOLEAN_template()
818 BOOLEAN_template::BOOLEAN_template(template_sel other_value
)
819 : Base_Template(other_value
)
821 check_single_selection(other_value
);
824 BOOLEAN_template::BOOLEAN_template(boolean other_value
)
825 : Base_Template(SPECIFIC_VALUE
)
827 single_value
= other_value
;
830 BOOLEAN_template::BOOLEAN_template(const BOOLEAN
& other_value
)
831 : Base_Template(SPECIFIC_VALUE
)
833 other_value
.must_bound("Creating a template from an unbound integer value.");
834 single_value
= other_value
.boolean_value
;
837 BOOLEAN_template::BOOLEAN_template(const OPTIONAL
<BOOLEAN
>& other_value
)
839 switch (other_value
.get_selection()) {
840 case OPTIONAL_PRESENT
:
841 set_selection(SPECIFIC_VALUE
);
842 single_value
= (boolean
)(const BOOLEAN
&)other_value
;
845 set_selection(OMIT_VALUE
);
848 TTCN_error("Creating a boolean template from an unbound optional field.");
852 BOOLEAN_template::BOOLEAN_template(const BOOLEAN_template
& other_value
)
855 copy_template(other_value
);
858 BOOLEAN_template::~BOOLEAN_template()
863 BOOLEAN_template
& BOOLEAN_template::operator=(template_sel other_value
)
865 check_single_selection(other_value
);
867 set_selection(other_value
);
871 BOOLEAN_template
& BOOLEAN_template::operator=(boolean other_value
)
874 set_selection(SPECIFIC_VALUE
);
875 single_value
= other_value
;
879 BOOLEAN_template
& BOOLEAN_template::operator=(const BOOLEAN
& other_value
)
881 other_value
.must_bound("Assignment of an unbound boolean value to a "
884 set_selection(SPECIFIC_VALUE
);
885 single_value
= other_value
.boolean_value
;
889 BOOLEAN_template
& BOOLEAN_template::operator=
890 (const OPTIONAL
<BOOLEAN
>& other_value
)
893 switch (other_value
.get_selection()) {
894 case OPTIONAL_PRESENT
:
895 set_selection(SPECIFIC_VALUE
);
896 single_value
= (boolean
)(const BOOLEAN
&)other_value
;
899 set_selection(OMIT_VALUE
);
902 TTCN_error("Assignment of an unbound optional field to a boolean "
908 BOOLEAN_template
& BOOLEAN_template::operator=
909 (const BOOLEAN_template
& other_value
)
911 if (&other_value
!= this) {
913 copy_template(other_value
);
918 boolean
BOOLEAN_template::match(boolean other_value
,
919 boolean
/* legacy */) const
921 switch (template_selection
) {
923 return single_value
== other_value
;
930 case COMPLEMENTED_LIST
:
931 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
932 if (value_list
.list_value
[i
].match(other_value
))
933 return template_selection
== VALUE_LIST
;
934 return template_selection
== COMPLEMENTED_LIST
;
936 TTCN_error("Matching with an uninitialized/unsupported boolean template.");
941 boolean
BOOLEAN_template::match(const BOOLEAN
& other_value
,
942 boolean
/* legacy */) const
944 if (!other_value
.is_bound()) return FALSE
;
945 return match(other_value
.boolean_value
);
948 boolean
BOOLEAN_template::valueof() const
950 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
951 TTCN_error("Performing valueof or "
952 "send operation on a non-specific boolean template.");
956 void BOOLEAN_template::set_type(template_sel template_type
,
957 unsigned int list_length
)
959 if (template_type
!= VALUE_LIST
&& template_type
!= COMPLEMENTED_LIST
)
960 TTCN_error("Setting an invalid list type for a boolean template.");
962 set_selection(template_type
);
963 value_list
.n_values
= list_length
;
964 value_list
.list_value
= new BOOLEAN_template
[list_length
];
967 BOOLEAN_template
& BOOLEAN_template::list_item(unsigned int list_index
)
969 if (template_selection
!= VALUE_LIST
&&
970 template_selection
!= COMPLEMENTED_LIST
)
971 TTCN_error("Accessing a list element of a non-list boolean template.");
972 if (list_index
>= value_list
.n_values
)
973 TTCN_error("Index overflow in a boolean value list template.");
974 return value_list
.list_value
[list_index
];
977 void BOOLEAN_template::log() const
979 switch (template_selection
) {
981 TTCN_Logger::log_event_str(single_value
? "true" : "false");
983 case COMPLEMENTED_LIST
:
984 TTCN_Logger::log_event_str("complement ");
987 TTCN_Logger::log_char('(');
988 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++) {
989 if (i
> 0) TTCN_Logger::log_event_str(", ");
990 value_list
.list_value
[i
].log();
992 TTCN_Logger::log_char(')');
1001 void BOOLEAN_template::log_match(const BOOLEAN
& match_value
,
1002 boolean
/* legacy */) const
1004 if (TTCN_Logger::VERBOSITY_COMPACT
== TTCN_Logger::get_matching_verbosity()
1005 && TTCN_Logger::get_logmatch_buffer_len() != 0) {
1006 TTCN_Logger::print_logmatch_buffer();
1007 TTCN_Logger::log_event_str(" := ");
1010 TTCN_Logger::log_event_str(" with ");
1012 if (match(match_value
)) TTCN_Logger::log_event_str(" matched");
1013 else TTCN_Logger::log_event_str(" unmatched");
1016 void BOOLEAN_template::set_param(Module_Param
& param
) {
1017 param
.basic_check(Module_Param::BC_TEMPLATE
, "boolean template");
1018 Module_Param_Ptr mp
= ¶m
;
1019 if (param
.get_type() == Module_Param::MP_Reference
) {
1020 mp
= param
.get_referenced_param();
1022 switch (mp
->get_type()) {
1023 case Module_Param::MP_Omit
:
1026 case Module_Param::MP_Any
:
1029 case Module_Param::MP_AnyOrNone
:
1030 *this = ANY_OR_OMIT
;
1032 case Module_Param::MP_List_Template
:
1033 case Module_Param::MP_ComplementList_Template
: {
1034 BOOLEAN_template temp
;
1035 temp
.set_type(mp
->get_type() == Module_Param::MP_List_Template
?
1036 VALUE_LIST
: COMPLEMENTED_LIST
, mp
->get_size());
1037 for (size_t i
=0; i
<mp
->get_size(); i
++) {
1038 temp
.list_item(i
).set_param(*mp
->get_elem(i
));
1042 case Module_Param::MP_Boolean
:
1043 *this = mp
->get_boolean();
1046 param
.type_error("boolean template");
1048 is_ifpresent
= param
.get_ifpresent() || mp
->get_ifpresent();
1051 Module_Param
* BOOLEAN_template::get_param(Module_Param_Name
& param_name
) const
1053 Module_Param
* mp
= NULL
;
1054 switch (template_selection
) {
1055 case UNINITIALIZED_TEMPLATE
:
1056 mp
= new Module_Param_Unbound();
1059 mp
= new Module_Param_Omit();
1062 mp
= new Module_Param_Any();
1065 mp
= new Module_Param_AnyOrNone();
1067 case SPECIFIC_VALUE
:
1068 mp
= new Module_Param_Boolean(single_value
);
1071 case COMPLEMENTED_LIST
: {
1072 if (template_selection
== VALUE_LIST
) {
1073 mp
= new Module_Param_List_Template();
1076 mp
= new Module_Param_ComplementList_Template();
1078 for (size_t i
= 0; i
< value_list
.n_values
; ++i
) {
1079 mp
->add_elem(value_list
.list_value
[i
].get_param(param_name
));
1086 mp
->set_ifpresent();
1091 void BOOLEAN_template::encode_text(Text_Buf
& text_buf
) const
1093 encode_text_base(text_buf
);
1094 switch (template_selection
) {
1099 case SPECIFIC_VALUE
:
1100 text_buf
.push_int(single_value
? 1 : 0);
1103 case COMPLEMENTED_LIST
:
1104 text_buf
.push_int(value_list
.n_values
);
1105 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1106 value_list
.list_value
[i
].encode_text(text_buf
);
1109 TTCN_error("Text encoder: Encoding an uninitialized/unsupported boolean "
1114 void BOOLEAN_template::decode_text(Text_Buf
& text_buf
)
1117 decode_text_base(text_buf
);
1118 switch (template_selection
) {
1123 case SPECIFIC_VALUE
: {
1124 int int_value
= text_buf
.pull_int().get_val();
1125 switch (int_value
) {
1127 single_value
= FALSE
;
1130 single_value
= TRUE
;
1133 TTCN_error("Text decoder: An invalid boolean value (%d) was received for "
1134 "a template.", int_value
);
1138 case COMPLEMENTED_LIST
:
1139 value_list
.n_values
= text_buf
.pull_int().get_val();
1140 value_list
.list_value
= new BOOLEAN_template
[value_list
.n_values
];
1141 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1142 value_list
.list_value
[i
].decode_text(text_buf
);
1145 TTCN_error("Text decoder: An unknown/unsupported selection was "
1146 "received for a boolean template.");
1150 boolean
BOOLEAN_template::is_present(boolean legacy
/* = FALSE */) const
1152 if (template_selection
==UNINITIALIZED_TEMPLATE
) return FALSE
;
1153 return !match_omit(legacy
);
1156 boolean
BOOLEAN_template::match_omit(boolean legacy
/* = FALSE */) const
1158 if (is_ifpresent
) return TRUE
;
1159 switch (template_selection
) {
1164 case COMPLEMENTED_LIST
:
1166 // legacy behavior: 'omit' can appear in the value/complement list
1167 for (unsigned int i
=0; i
<value_list
.n_values
; i
++)
1168 if (value_list
.list_value
[i
].match_omit())
1169 return template_selection
==VALUE_LIST
;
1170 return template_selection
==COMPLEMENTED_LIST
;
1172 // else fall through
1179 #ifndef TITAN_RUNTIME_2
1180 void BOOLEAN_template::check_restriction(template_res t_res
, const char* t_name
,
1181 boolean legacy
/* = FALSE */) const
1183 if (template_selection
==UNINITIALIZED_TEMPLATE
) return;
1184 switch ((t_name
&&(t_res
==TR_VALUE
))?TR_OMIT
:t_res
) {
1186 if (!is_ifpresent
&& template_selection
==SPECIFIC_VALUE
) return;
1189 if (!is_ifpresent
&& (template_selection
==OMIT_VALUE
||
1190 template_selection
==SPECIFIC_VALUE
)) return;
1193 if (!match_omit(legacy
)) return;
1198 TTCN_error("Restriction `%s' on template of type %s violated.",
1199 get_res_name(t_res
), t_name
? t_name
: "boolean");