Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Baranyi, Botond | |
11 | * Beres, Szabolcs | |
12 | * Delic, Adam | |
13 | * Forstner, Matyas | |
14 | * Kovacs, Ferenc | |
15 | * Raduly, Csaba | |
16 | * Szabados, Kristof | |
17 | * Szabo, Bence Janos | |
18 | * Szabo, Janos Zoltan – initial implementation | |
19 | * Szalai, Gabor | |
20 | * Tatarka, Gabor | |
21 | * | |
22 | ******************************************************************************/ | |
970ed795 EL |
23 | #ifndef INTEGER_HH |
24 | #define INTEGER_HH | |
25 | ||
26 | #include "Basetype.hh" | |
27 | #include "Template.hh" | |
28 | #include "Optional.hh" | |
29 | #include "Error.hh" | |
30 | #include "RInt.hh" | |
31 | ||
32 | class BITSTRING; | |
33 | class CHARSTRING; | |
34 | class HEXSTRING; | |
35 | class OCTETSTRING; | |
36 | ||
37 | class Module_Param; | |
38 | ||
39 | class INTEGER : public Base_Type { | |
40 | // Private constructor for internal initialization. It's not part of the | |
41 | // public API. | |
42 | explicit INTEGER(BIGNUM *other_value); | |
43 | int from_string(const char *); | |
44 | ||
45 | int get_nof_digits(); | |
46 | ||
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); | |
61 | ||
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); | |
66 | ||
67 | friend void log_param_value(); | |
68 | ||
69 | boolean bound_flag; | |
70 | boolean native_flag; | |
71 | union { | |
72 | RInt native; | |
73 | BIGNUM *openssl; | |
74 | } val; | |
75 | ||
76 | public: | |
77 | INTEGER(); | |
78 | INTEGER(const INTEGER& other_value); | |
79 | INTEGER(int other_value); | |
80 | explicit INTEGER(const char *other_value); | |
81 | ~INTEGER(); | |
82 | void clean_up(); | |
83 | ||
84 | INTEGER& operator=(int other_value); | |
85 | INTEGER& operator=(const INTEGER& other_value); | |
86 | INTEGER& operator++(); | |
87 | INTEGER& operator--(); | |
88 | ||
89 | INTEGER operator+() const; | |
90 | INTEGER operator-() const; | |
91 | ||
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; | |
100 | ||
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); } | |
107 | ||
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); } | |
120 | ||
121 | operator int() const; | |
122 | long long int get_long_long_val() const; | |
123 | void set_long_long_val(long long int other_value); | |
124 | ||
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 | |
129 | { if (!bound_flag) | |
130 | TTCN_error("%s", err_msg); | |
131 | } | |
132 | int_val_t get_val() const; | |
133 | void set_val(const int_val_t& other_value); | |
134 | ||
135 | void log() const; | |
136 | ||
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_; } | |
142 | #else | |
143 | inline boolean is_present() const { return is_bound(); } | |
144 | #endif | |
145 | ||
146 | void set_param(Module_Param& param); | |
3abe9331 | 147 | Module_Param* get_param(Module_Param_Name& param_name) const; |
970ed795 EL |
148 | void encode_text(Text_Buf& text_buf) const; |
149 | void decode_text(Text_Buf& text_buf); | |
150 | ||
151 | void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
152 | TTCN_EncDec::coding_t p_coding, ...) const; | |
153 | ||
154 | void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, | |
155 | TTCN_EncDec::coding_t p_coding, ...); | |
156 | ||
157 | ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td, | |
158 | unsigned p_coding) const; | |
159 | ||
160 | boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td, | |
161 | const ASN_BER_TLV_t& p_tlv, unsigned L_form); | |
162 | ||
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; | |
168 | ||
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. | |
178 | **/ | |
179 | int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor, | |
af710487 | 180 | int indent, embed_values_enc_struct_t*) const; |
970ed795 EL |
181 | /** @brief Decode according to XML Encoding Rules. |
182 | **/ | |
183 | int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader, | |
feade998 | 184 | unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t*); |
970ed795 EL |
185 | |
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; | |
189 | ||
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); | |
193 | }; | |
194 | ||
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); | |
199 | ||
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); | |
204 | ||
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); | |
209 | ||
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); | |
213 | ||
214 | inline boolean operator!=(int int_value, const INTEGER& other_value) | |
215 | { | |
216 | return !(int_value == other_value); | |
217 | } | |
218 | ||
219 | inline boolean operator<=(int int_value, const INTEGER& other_value) | |
220 | { | |
221 | return !(int_value > other_value); | |
222 | } | |
223 | ||
224 | inline boolean operator>=(int int_value, const INTEGER& other_value) | |
225 | { | |
226 | return !(int_value < other_value); | |
227 | } | |
228 | ||
229 | // Integer template class. | |
230 | ||
231 | class INTEGER_template : public Base_Template { | |
232 | private: | |
233 | union { | |
234 | struct { | |
235 | boolean native_flag; | |
236 | union { | |
237 | RInt native; | |
238 | BIGNUM *openssl; | |
239 | } val; | |
240 | } int_val; | |
241 | struct { | |
242 | unsigned int n_values; | |
243 | INTEGER_template *list_value; | |
244 | } value_list; | |
245 | struct { | |
246 | boolean min_is_present, max_is_present; | |
247 | struct { | |
248 | boolean native_flag; | |
249 | union { | |
250 | RInt native; | |
251 | BIGNUM *openssl; | |
252 | } val; | |
253 | } min_value, max_value; | |
254 | } value_range; | |
255 | }; | |
256 | ||
257 | void copy_template(const INTEGER_template& other_value); | |
258 | ||
259 | public: | |
260 | INTEGER_template(); | |
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); | |
266 | ~INTEGER_template(); | |
267 | void clean_up(); | |
268 | ||
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); | |
274 | ||
3abe9331 | 275 | boolean match(int other_value, boolean legacy = FALSE) const; |
276 | boolean match(const INTEGER& other_value, boolean legacy = FALSE) const; | |
970ed795 EL |
277 | INTEGER valueof() const; |
278 | ||
279 | /** Sets the template type. | |
280 | * | |
281 | * Calls clean_up(), so the template becomes uninitialized. | |
282 | * | |
283 | * @param template_type | |
284 | * @param list_length used for VALUE_LIST and COMPLEMENTED_LIST only. | |
285 | * | |
286 | * @post template_selection = UNINITIALIZED_TEMPLATE | |
287 | */ | |
288 | void set_type(template_sel template_type, unsigned int list_length = 0); | |
289 | /** Returns the specified list item | |
290 | * | |
291 | * @param list_index | |
292 | * @return the list item | |
293 | * @pre template_selection is VALUE_LIST or COMPLEMENTED_LIST | |
294 | */ | |
295 | INTEGER_template& list_item(unsigned int list_index); | |
296 | ||
297 | /** Sets the lower bound of the value range. | |
298 | * | |
299 | * @param min_value | |
300 | * @pre template_selection == VALUE_RANGE; else DTE | |
301 | */ | |
302 | void set_min(int min_value); | |
303 | /** Sets the lower bound of the value range. | |
304 | * | |
305 | * @param min_value | |
306 | * @pre template_selection == VALUE_RANGE; else DTE | |
307 | * @pre min_value must be bound; else DTE | |
308 | */ | |
309 | void set_min(const INTEGER& min_value); | |
310 | /** Sets the upper bound of the value range. | |
311 | * | |
312 | * @param max_value | |
313 | * @pre template_selection == VALUE_RANGE; else DTE | |
314 | * @pre min_value must be bound; else DTE | |
315 | */ | |
316 | void set_max(int max_value); | |
317 | /** Sets the upper bound of the value range. | |
318 | * | |
319 | * @param max_value | |
320 | * @pre template_selection == VALUE_RANGE; else DTE | |
321 | * @pre min_value must be bound; else DTE | |
322 | */ | |
323 | void set_max(const INTEGER& max_value); | |
324 | ||
325 | void log() const; | |
3abe9331 | 326 | void log_match(const INTEGER& match_value, boolean legacy = FALSE) const; |
970ed795 EL |
327 | |
328 | void set_param(Module_Param& param); | |
3abe9331 | 329 | Module_Param* get_param(Module_Param_Name& param_name) const; |
970ed795 EL |
330 | |
331 | void encode_text(Text_Buf& text_buf) const; | |
332 | void decode_text(Text_Buf& text_buf); | |
333 | ||
3abe9331 | 334 | boolean is_present(boolean legacy = FALSE) const; |
335 | boolean match_omit(boolean legacy = FALSE) const; | |
970ed795 EL |
336 | |
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_; } | |
3abe9331 | 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); } | |
970ed795 | 345 | #else |
3abe9331 | 346 | void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const; |
970ed795 EL |
347 | #endif |
348 | }; | |
349 | ||
350 | #endif // INTEGER_HH | |
351 |