1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
18 * Szabo, Janos Zoltan – initial implementation
22 ******************************************************************************/
26 #include "Basetype.hh"
27 #include "Template.hh"
28 #include "Optional.hh"
39 class INTEGER : public Base_Type {
40 // Private constructor for internal initialization. It's not part of the
42 explicit INTEGER(BIGNUM *other_value);
43 int from_string(const char *);
47 friend class INTEGER_template;
48 friend INTEGER operator+(int int_value, const INTEGER& other_value);
49 friend INTEGER operator-(int int_value, const INTEGER& other_value);
50 friend INTEGER operator*(int int_value, const INTEGER& other_value);
51 friend INTEGER operator/(int int_value, const INTEGER& other_value);
52 friend INTEGER rem(const INTEGER& left_value, const INTEGER& right_value);
53 friend INTEGER rem(const INTEGER& left_value, int right_value);
54 friend INTEGER rem(int left_value, const INTEGER& right_value);
55 friend INTEGER mod(const INTEGER& left_value, const INTEGER& right_value);
56 friend INTEGER mod(const INTEGER& left_value, int right_value);
57 friend INTEGER mod(int left_value, const INTEGER& right_value);
58 friend boolean operator==(int int_value, const INTEGER& other_value);
59 friend boolean operator< (int int_value, const INTEGER& other_value);
60 friend boolean operator> (int int_value, const INTEGER& other_value);
62 friend INTEGER bit2int(const BITSTRING& value);
63 friend INTEGER hex2int(const HEXSTRING& value);
64 friend INTEGER oct2int(const OCTETSTRING& value);
65 friend INTEGER str2int(const CHARSTRING& value);
67 friend void log_param_value();
78 INTEGER(const INTEGER& other_value);
79 INTEGER(int other_value);
80 explicit INTEGER(const char *other_value);
84 INTEGER& operator=(int other_value);
85 INTEGER& operator=(const INTEGER& other_value);
86 INTEGER& operator++();
87 INTEGER& operator--();
89 INTEGER operator+() const;
90 INTEGER operator-() const;
92 INTEGER operator+(int other_value) const;
93 INTEGER operator+(const INTEGER& other_value) const;
94 INTEGER operator-(int other_value) const;
95 INTEGER operator-(const INTEGER& other_value) const;
96 INTEGER operator*(int other_value) const;
97 INTEGER operator*(const INTEGER& other_value) const;
98 INTEGER operator/(int other_value) const;
99 INTEGER operator/(const INTEGER& other_value) const;
101 boolean operator==(int other_value) const;
102 boolean operator==(const INTEGER& other_value) const;
103 inline boolean operator!=(int other_value) const
104 { return !(*this == other_value); }
105 inline boolean operator!=(const INTEGER& other_value) const
106 { return !(*this == other_value); }
108 boolean operator<(int other_value) const;
109 boolean operator<(const INTEGER& other_value) const;
110 boolean operator>(int other_value) const;
111 boolean operator>(const INTEGER& other_value) const;
112 inline boolean operator<=(int other_value) const
113 { return !(*this > other_value); }
114 inline boolean operator<=(const INTEGER& other_value) const
115 { return !(*this > other_value); }
116 inline boolean operator>=(int other_value) const
117 { return !(*this < other_value); }
118 inline boolean operator>=(const INTEGER& other_value) const
119 { return !(*this < other_value); }
121 operator int() const;
122 long long int get_long_long_val() const;
123 void set_long_long_val(long long int other_value);
125 inline boolean is_native() const { return native_flag; }
126 inline boolean is_bound() const { return bound_flag; }
127 inline boolean is_value() const { return bound_flag; }
128 inline void must_bound(const char *err_msg) const
130 TTCN_error("%s", err_msg);
132 int_val_t get_val() const;
133 void set_val(const int_val_t& other_value);
137 #ifdef TITAN_RUNTIME_2
138 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const INTEGER*>(other_value)); }
139 void set_value(const Base_Type* other_value) { *this = *(static_cast<const INTEGER*>(other_value)); }
140 Base_Type* clone() const { return new INTEGER(*this); }
141 const TTCN_Typedescriptor_t* get_descriptor() const { return &INTEGER_descr_; }
143 inline boolean is_present() const { return is_bound(); }
146 void set_param(Module_Param& param);
147 Module_Param* get_param(Module_Param_Name& param_name) const;
148 void encode_text(Text_Buf& text_buf) const;
149 void decode_text(Text_Buf& text_buf);
151 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
152 TTCN_EncDec::coding_t p_coding, ...) const;
154 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
155 TTCN_EncDec::coding_t p_coding, ...);
157 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
158 unsigned p_coding) const;
160 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
161 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
163 /** Encodes the value of the variable according to the
164 * TTCN_Typedescriptor_t. It must be public because called by
165 * another types during encoding. Returns the length of encoded data. */
166 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
167 int RAW_encode_openssl(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
169 /** Decodes the value of the variable according to the
170 * TTCN_Typedescriptor_t. It must be public because called by
171 * another types during encoding. Returns the number of decoded bits. */
172 int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t,
173 boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
174 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
175 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&,
176 Limit_Token_List&, boolean no_err = FALSE, boolean first_call=TRUE);
177 /** @brief Encode according to XML Encoding Rules.
179 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
180 int indent, embed_values_enc_struct_t*) const;
181 /** @brief Decode according to XML Encoding Rules.
183 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
184 unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t*);
186 /** Encodes accordingly to the JSON encoding rules.
187 * Returns the length of the encoded data. */
188 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
190 /** Decodes accordingly to the JSON encoding rules.
191 * Returns the length of the decoded data. */
192 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
195 extern INTEGER operator+(int int_value, const INTEGER& other_value);
196 extern INTEGER operator-(int int_value, const INTEGER& other_value);
197 extern INTEGER operator*(int int_value, const INTEGER& other_value);
198 extern INTEGER operator/(int int_value, const INTEGER& other_value);
200 extern INTEGER rem(int left_value, int right_value);
201 extern INTEGER rem(const INTEGER& left_value, const INTEGER& right_value);
202 extern INTEGER rem(const INTEGER& left_value, int right_value);
203 extern INTEGER rem(int left_value, const INTEGER& right_value);
205 extern INTEGER mod(int left_value, int right_value);
206 extern INTEGER mod(const INTEGER& left_value, const INTEGER& right_value);
207 extern INTEGER mod(const INTEGER& left_value, int right_value);
208 extern INTEGER mod(int left_value, const INTEGER& right_value);
210 extern boolean operator==(int int_value, const INTEGER& other_value);
211 extern boolean operator<(int int_value, const INTEGER& other_value);
212 extern boolean operator>(int int_value, const INTEGER& other_value);
214 inline boolean operator!=(int int_value, const INTEGER& other_value)
216 return !(int_value == other_value);
219 inline boolean operator<=(int int_value, const INTEGER& other_value)
221 return !(int_value > other_value);
224 inline boolean operator>=(int int_value, const INTEGER& other_value)
226 return !(int_value < other_value);
229 // Integer template class.
231 class INTEGER_template : public Base_Template {
242 unsigned int n_values;
243 INTEGER_template *list_value;
246 boolean min_is_present, max_is_present;
253 } min_value, max_value;
257 void copy_template(const INTEGER_template& other_value);
261 INTEGER_template(const INTEGER_template& other_value);
262 INTEGER_template(template_sel other_value);
263 INTEGER_template(int other_value);
264 INTEGER_template(const INTEGER& other_value);
265 INTEGER_template(const OPTIONAL<INTEGER>& other_value);
269 INTEGER_template& operator=(template_sel other_value);
270 INTEGER_template& operator=(int other_value);
271 INTEGER_template& operator=(const INTEGER& other_value);
272 INTEGER_template& operator=(const OPTIONAL<INTEGER>& other_value);
273 INTEGER_template& operator=(const INTEGER_template& other_value);
275 boolean match(int other_value, boolean legacy = FALSE) const;
276 boolean match(const INTEGER& other_value, boolean legacy = FALSE) const;
277 INTEGER valueof() const;
279 /** Sets the template type.
281 * Calls clean_up(), so the template becomes uninitialized.
283 * @param template_type
284 * @param list_length used for VALUE_LIST and COMPLEMENTED_LIST only.
286 * @post template_selection = UNINITIALIZED_TEMPLATE
288 void set_type(template_sel template_type, unsigned int list_length = 0);
289 /** Returns the specified list item
292 * @return the list item
293 * @pre template_selection is VALUE_LIST or COMPLEMENTED_LIST
295 INTEGER_template& list_item(unsigned int list_index);
297 /** Sets the lower bound of the value range.
300 * @pre template_selection == VALUE_RANGE; else DTE
302 void set_min(int min_value);
303 /** Sets the lower bound of the value range.
306 * @pre template_selection == VALUE_RANGE; else DTE
307 * @pre min_value must be bound; else DTE
309 void set_min(const INTEGER& min_value);
310 /** Sets the upper bound of the value range.
313 * @pre template_selection == VALUE_RANGE; else DTE
314 * @pre min_value must be bound; else DTE
316 void set_max(int max_value);
317 /** Sets the upper bound of the value range.
320 * @pre template_selection == VALUE_RANGE; else DTE
321 * @pre min_value must be bound; else DTE
323 void set_max(const INTEGER& max_value);
326 void log_match(const INTEGER& match_value, boolean legacy = FALSE) const;
328 void set_param(Module_Param& param);
329 Module_Param* get_param(Module_Param_Name& param_name) const;
331 void encode_text(Text_Buf& text_buf) const;
332 void decode_text(Text_Buf& text_buf);
334 boolean is_present(boolean legacy = FALSE) const;
335 boolean match_omit(boolean legacy = FALSE) const;
337 #ifdef TITAN_RUNTIME_2
338 void valueofv(Base_Type* value) const { *(static_cast<INTEGER*>(value)) = valueof(); }
339 void set_value(template_sel other_value) { *this = other_value; }
340 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const INTEGER*>(other_value)); }
341 Base_Template* clone() const { return new INTEGER_template(*this); }
342 const TTCN_Typedescriptor_t* get_descriptor() const { return &INTEGER_descr_; }
343 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const INTEGER*>(other_value)), legacy); }
344 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const INTEGER*>(match_value)), legacy); }
346 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;