Sync with 5.2.0
[deliverable/titan.core.git] / core / Integer.hh
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 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef INTEGER_HH
9 #define INTEGER_HH
10
11 #include "Basetype.hh"
12 #include "Template.hh"
13 #include "Optional.hh"
14 #include "Error.hh"
15 #include "RInt.hh"
16
17 class BITSTRING;
18 class CHARSTRING;
19 class HEXSTRING;
20 class OCTETSTRING;
21
22 class Module_Param;
23
24 class INTEGER : public Base_Type {
25 // Private constructor for internal initialization. It's not part of the
26 // public API.
27 explicit INTEGER(BIGNUM *other_value);
28 int from_string(const char *);
29
30 int get_nof_digits();
31
32 friend class INTEGER_template;
33 friend INTEGER operator+(int int_value, const INTEGER& other_value);
34 friend INTEGER operator-(int int_value, const INTEGER& other_value);
35 friend INTEGER operator*(int int_value, const INTEGER& other_value);
36 friend INTEGER operator/(int int_value, const INTEGER& other_value);
37 friend INTEGER rem(const INTEGER& left_value, const INTEGER& right_value);
38 friend INTEGER rem(const INTEGER& left_value, int right_value);
39 friend INTEGER rem(int left_value, const INTEGER& right_value);
40 friend INTEGER mod(const INTEGER& left_value, const INTEGER& right_value);
41 friend INTEGER mod(const INTEGER& left_value, int right_value);
42 friend INTEGER mod(int left_value, const INTEGER& right_value);
43 friend boolean operator==(int int_value, const INTEGER& other_value);
44 friend boolean operator< (int int_value, const INTEGER& other_value);
45 friend boolean operator> (int int_value, const INTEGER& other_value);
46
47 friend INTEGER bit2int(const BITSTRING& value);
48 friend INTEGER hex2int(const HEXSTRING& value);
49 friend INTEGER oct2int(const OCTETSTRING& value);
50 friend INTEGER str2int(const CHARSTRING& value);
51
52 friend void log_param_value();
53
54 boolean bound_flag;
55 boolean native_flag;
56 union {
57 RInt native;
58 BIGNUM *openssl;
59 } val;
60
61 public:
62 INTEGER();
63 INTEGER(const INTEGER& other_value);
64 INTEGER(int other_value);
65 explicit INTEGER(const char *other_value);
66 ~INTEGER();
67 void clean_up();
68
69 INTEGER& operator=(int other_value);
70 INTEGER& operator=(const INTEGER& other_value);
71 INTEGER& operator++();
72 INTEGER& operator--();
73
74 INTEGER operator+() const;
75 INTEGER operator-() const;
76
77 INTEGER operator+(int other_value) const;
78 INTEGER operator+(const INTEGER& other_value) const;
79 INTEGER operator-(int other_value) const;
80 INTEGER operator-(const INTEGER& other_value) const;
81 INTEGER operator*(int other_value) const;
82 INTEGER operator*(const INTEGER& other_value) const;
83 INTEGER operator/(int other_value) const;
84 INTEGER operator/(const INTEGER& other_value) const;
85
86 boolean operator==(int other_value) const;
87 boolean operator==(const INTEGER& other_value) const;
88 inline boolean operator!=(int other_value) const
89 { return !(*this == other_value); }
90 inline boolean operator!=(const INTEGER& other_value) const
91 { return !(*this == other_value); }
92
93 boolean operator<(int other_value) const;
94 boolean operator<(const INTEGER& other_value) const;
95 boolean operator>(int other_value) const;
96 boolean operator>(const INTEGER& other_value) const;
97 inline boolean operator<=(int other_value) const
98 { return !(*this > other_value); }
99 inline boolean operator<=(const INTEGER& other_value) const
100 { return !(*this > other_value); }
101 inline boolean operator>=(int other_value) const
102 { return !(*this < other_value); }
103 inline boolean operator>=(const INTEGER& other_value) const
104 { return !(*this < other_value); }
105
106 operator int() const;
107 long long int get_long_long_val() const;
108 void set_long_long_val(long long int other_value);
109
110 inline boolean is_native() const { return native_flag; }
111 inline boolean is_bound() const { return bound_flag; }
112 inline boolean is_value() const { return bound_flag; }
113 inline void must_bound(const char *err_msg) const
114 { if (!bound_flag)
115 TTCN_error("%s", err_msg);
116 }
117 int_val_t get_val() const;
118 void set_val(const int_val_t& other_value);
119
120 void log() const;
121
122 #ifdef TITAN_RUNTIME_2
123 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const INTEGER*>(other_value)); }
124 void set_value(const Base_Type* other_value) { *this = *(static_cast<const INTEGER*>(other_value)); }
125 Base_Type* clone() const { return new INTEGER(*this); }
126 const TTCN_Typedescriptor_t* get_descriptor() const { return &INTEGER_descr_; }
127 #else
128 inline boolean is_present() const { return is_bound(); }
129 #endif
130
131 void set_param(Module_Param& param);
132 void encode_text(Text_Buf& text_buf) const;
133 void decode_text(Text_Buf& text_buf);
134
135 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
136 TTCN_EncDec::coding_t p_coding, ...) const;
137
138 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
139 TTCN_EncDec::coding_t p_coding, ...);
140
141 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
142 unsigned p_coding) const;
143
144 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
145 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
146
147 /** Encodes the value of the variable according to the
148 * TTCN_Typedescriptor_t. It must be public because called by
149 * another types during encoding. Returns the length of encoded data. */
150 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
151 int RAW_encode_openssl(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
152
153 /** Decodes the value of the variable according to the
154 * TTCN_Typedescriptor_t. It must be public because called by
155 * another types during encoding. Returns the number of decoded bits. */
156 int RAW_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&, int, raw_order_t,
157 boolean no_err=FALSE, int sel_field=-1, boolean first_call=TRUE);
158 int TEXT_encode(const TTCN_Typedescriptor_t&, TTCN_Buffer&) const;
159 int TEXT_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer&,
160 Limit_Token_List&, boolean no_err = FALSE, boolean first_call=TRUE);
161 /** @brief Encode according to XML Encoding Rules.
162 **/
163 int XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, unsigned int flavor,
164 int indent, embed_values_enc_struct_t*) const;
165 /** @brief Decode according to XML Encoding Rules.
166 **/
167 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
168 unsigned int flavor, embed_values_dec_struct_t*);
169
170 /** Encodes accordingly to the JSON encoding rules.
171 * Returns the length of the encoded data. */
172 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
173
174 /** Decodes accordingly to the JSON encoding rules.
175 * Returns the length of the decoded data. */
176 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
177 };
178
179 extern INTEGER operator+(int int_value, const INTEGER& other_value);
180 extern INTEGER operator-(int int_value, const INTEGER& other_value);
181 extern INTEGER operator*(int int_value, const INTEGER& other_value);
182 extern INTEGER operator/(int int_value, const INTEGER& other_value);
183
184 extern INTEGER rem(int left_value, int right_value);
185 extern INTEGER rem(const INTEGER& left_value, const INTEGER& right_value);
186 extern INTEGER rem(const INTEGER& left_value, int right_value);
187 extern INTEGER rem(int left_value, const INTEGER& right_value);
188
189 extern INTEGER mod(int left_value, int right_value);
190 extern INTEGER mod(const INTEGER& left_value, const INTEGER& right_value);
191 extern INTEGER mod(const INTEGER& left_value, int right_value);
192 extern INTEGER mod(int left_value, const INTEGER& right_value);
193
194 extern boolean operator==(int int_value, const INTEGER& other_value);
195 extern boolean operator<(int int_value, const INTEGER& other_value);
196 extern boolean operator>(int int_value, const INTEGER& other_value);
197
198 inline boolean operator!=(int int_value, const INTEGER& other_value)
199 {
200 return !(int_value == other_value);
201 }
202
203 inline boolean operator<=(int int_value, const INTEGER& other_value)
204 {
205 return !(int_value > other_value);
206 }
207
208 inline boolean operator>=(int int_value, const INTEGER& other_value)
209 {
210 return !(int_value < other_value);
211 }
212
213 // Integer template class.
214
215 class INTEGER_template : public Base_Template {
216 private:
217 union {
218 struct {
219 boolean native_flag;
220 union {
221 RInt native;
222 BIGNUM *openssl;
223 } val;
224 } int_val;
225 struct {
226 unsigned int n_values;
227 INTEGER_template *list_value;
228 } value_list;
229 struct {
230 boolean min_is_present, max_is_present;
231 struct {
232 boolean native_flag;
233 union {
234 RInt native;
235 BIGNUM *openssl;
236 } val;
237 } min_value, max_value;
238 } value_range;
239 };
240
241 void copy_template(const INTEGER_template& other_value);
242
243 public:
244 INTEGER_template();
245 INTEGER_template(const INTEGER_template& other_value);
246 INTEGER_template(template_sel other_value);
247 INTEGER_template(int other_value);
248 INTEGER_template(const INTEGER& other_value);
249 INTEGER_template(const OPTIONAL<INTEGER>& other_value);
250 ~INTEGER_template();
251 void clean_up();
252
253 INTEGER_template& operator=(template_sel other_value);
254 INTEGER_template& operator=(int other_value);
255 INTEGER_template& operator=(const INTEGER& other_value);
256 INTEGER_template& operator=(const OPTIONAL<INTEGER>& other_value);
257 INTEGER_template& operator=(const INTEGER_template& other_value);
258
259 boolean match(int other_value) const;
260 boolean match(const INTEGER& other_value) const;
261 INTEGER valueof() const;
262
263 /** Sets the template type.
264 *
265 * Calls clean_up(), so the template becomes uninitialized.
266 *
267 * @param template_type
268 * @param list_length used for VALUE_LIST and COMPLEMENTED_LIST only.
269 *
270 * @post template_selection = UNINITIALIZED_TEMPLATE
271 */
272 void set_type(template_sel template_type, unsigned int list_length = 0);
273 /** Returns the specified list item
274 *
275 * @param list_index
276 * @return the list item
277 * @pre template_selection is VALUE_LIST or COMPLEMENTED_LIST
278 */
279 INTEGER_template& list_item(unsigned int list_index);
280
281 /** Sets the lower bound of the value range.
282 *
283 * @param min_value
284 * @pre template_selection == VALUE_RANGE; else DTE
285 */
286 void set_min(int min_value);
287 /** Sets the lower bound of the value range.
288 *
289 * @param min_value
290 * @pre template_selection == VALUE_RANGE; else DTE
291 * @pre min_value must be bound; else DTE
292 */
293 void set_min(const INTEGER& min_value);
294 /** Sets the upper bound of the value range.
295 *
296 * @param max_value
297 * @pre template_selection == VALUE_RANGE; else DTE
298 * @pre min_value must be bound; else DTE
299 */
300 void set_max(int max_value);
301 /** Sets the upper bound of the value range.
302 *
303 * @param max_value
304 * @pre template_selection == VALUE_RANGE; else DTE
305 * @pre min_value must be bound; else DTE
306 */
307 void set_max(const INTEGER& max_value);
308
309 void log() const;
310 void log_match(const INTEGER& match_value) const;
311
312 void set_param(Module_Param& param);
313
314 void encode_text(Text_Buf& text_buf) const;
315 void decode_text(Text_Buf& text_buf);
316
317 boolean is_present() const;
318 boolean match_omit() const;
319
320 #ifdef TITAN_RUNTIME_2
321 void valueofv(Base_Type* value) const { *(static_cast<INTEGER*>(value)) = valueof(); }
322 void set_value(template_sel other_value) { *this = other_value; }
323 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const INTEGER*>(other_value)); }
324 Base_Template* clone() const { return new INTEGER_template(*this); }
325 const TTCN_Typedescriptor_t* get_descriptor() const { return &INTEGER_descr_; }
326 boolean matchv(const Base_Type* other_value) const { return match(*(static_cast<const INTEGER*>(other_value))); }
327 void log_matchv(const Base_Type* match_value) const { log_match(*(static_cast<const INTEGER*>(match_value))); }
328 #else
329 void check_restriction(template_res t_res, const char* t_name=NULL) const;
330 #endif
331 };
332
333 #endif // INTEGER_HH
334
This page took 0.053585 seconds and 5 git commands to generate.