Merge pull request #78 from balaskoa/master
[deliverable/titan.core.git] / core / Float.hh
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 * Raduly, Csaba
15 * Szabados, Kristof
16 * Szabo, Bence Janos
17 * Szabo, Janos Zoltan – initial implementation
18 * Szalai, Gabor
19 * Tatarka, Gabor
20 *
21 ******************************************************************************/
22 #ifndef FLOAT_HH
23 #define FLOAT_HH
24
25 #include "Basetype.hh"
26 #include "Template.hh"
27 #include "Optional.hh"
28 #include "Error.hh"
29 #include "ttcn3float.hh"
30
31 class Module_Param;
32
33 // float value class
34
35 class FLOAT : public Base_Type {
36
37 friend class FLOAT_template;
38 friend class TIMER;
39
40 friend double operator+(double double_value, const FLOAT& other_value);
41 friend double operator-(double double_value, const FLOAT& other_value);
42 friend double operator*(double double_value, const FLOAT& other_value);
43 friend double operator/(double double_value, const FLOAT& other_value);
44
45 friend boolean operator==(double double_value, const FLOAT& other_value);
46 friend boolean operator< (double double_value, const FLOAT& other_value);
47 friend boolean operator> (double double_value, const FLOAT& other_value);
48
49 boolean bound_flag;
50 ttcn3float float_value;
51
52 /** Returns true if the string parameter contains the string representation
53 * of a real number, otherwise returns false. */
54 boolean is_float(const char* p_str);
55
56 public:
57
58 FLOAT();
59 FLOAT(double other_value);
60 FLOAT(const FLOAT& other_value);
61 void clean_up();
62
63 FLOAT& operator=(double other_value);
64 FLOAT& operator=(const FLOAT& other_value);
65
66 double operator+() const;
67 double operator-() const;
68
69 double operator+(double other_value) const;
70 double operator+(const FLOAT& other_value) const;
71 double operator-(double other_value) const;
72 double operator-(const FLOAT& other_value) const;
73 double operator*(double other_value) const;
74 double operator*(const FLOAT& other_value) const;
75 double operator/(double other_value) const;
76 double operator/(const FLOAT& other_value) const;
77
78 boolean operator==(double other_value) const;
79 boolean operator==(const FLOAT& other_value) const;
80 inline boolean operator!=(double other_value) const
81 { return !(*this == other_value); }
82 inline boolean operator!=(const FLOAT& other_value) const
83 { return !(*this == other_value); }
84
85 boolean operator<(double other_value) const;
86 boolean operator<(const FLOAT& other_value) const;
87 boolean operator>(double other_value) const;
88 boolean operator>(const FLOAT& other_value) const;
89 inline boolean operator<=(double other_value) const
90 { return !(*this > other_value); }
91 inline boolean operator<=(const FLOAT& other_value) const
92 { return !(*this > other_value); }
93 inline boolean operator>=(double other_value) const
94 { return !(*this < other_value); }
95 inline boolean operator>=(const FLOAT& other_value) const
96 { return !(*this < other_value); }
97
98 operator double() const;
99
100 inline boolean is_bound() const { return bound_flag; }
101 inline boolean is_value() const { return bound_flag; }
102 inline void must_bound(const char *err_msg) const
103 { if (!bound_flag) TTCN_error("%s", err_msg); }
104
105 /** special TTCN-3 float values are not_a_number and +/- infinity */
106 static bool is_special(double flt_val);
107 static void check_numeric(double flt_val, const char *err_msg_begin);
108
109 void log() const;
110
111 #ifdef TITAN_RUNTIME_2
112 boolean is_equal(const Base_Type* other_value) const { return *this == *(static_cast<const FLOAT*>(other_value)); }
113 void set_value(const Base_Type* other_value) { *this = *(static_cast<const FLOAT*>(other_value)); }
114 Base_Type* clone() const { return new FLOAT(*this); }
115 const TTCN_Typedescriptor_t* get_descriptor() const { return &FLOAT_descr_; }
116 #else
117 inline boolean is_present() const { return is_bound(); }
118 #endif
119
120 void set_param(Module_Param& param);
121 Module_Param* get_param(Module_Param_Name& param_name) const;
122
123 void encode_text(Text_Buf& text_buf) const;
124 void decode_text(Text_Buf& text_buf);
125
126 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
127 TTCN_EncDec::coding_t p_coding, ...) const;
128
129 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
130 TTCN_EncDec::coding_t p_coding, ...);
131
132 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
133 unsigned p_coding) const;
134
135 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
136 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
137
138 /** Encodes the value of the variable according to the
139 * TTCN_Typedescriptor_t. It must be public because called by
140 * another types during encoding. Returns the length of encoded data*/
141 int RAW_encode(const TTCN_Typedescriptor_t&, RAW_enc_tree&) const;
142
143 /** Decodes the value of the variable according to the
144 * TTCN_Typedescriptor_t. It must be public because called by
145 * another types during encoding. Returns the number of decoded bits*/
146 int RAW_decode(const TTCN_Typedescriptor_t&,
147 TTCN_Buffer&, int, raw_order_t, boolean no_err=FALSE,
148 int sel_field=-1, boolean first_call=TRUE);
149
150 int XER_encode(const XERdescriptor_t& p_td,
151 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
152 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
153 unsigned int flavor, unsigned int flavor2, embed_values_dec_struct_t*);
154
155 /** Encodes accordingly to the JSON encoding rules.
156 * Returns the length of the encoded data. */
157 int JSON_encode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&) const;
158
159 /** Decodes accordingly to the JSON encoding rules.
160 * Returns the length of the decoded data. */
161 int JSON_decode(const TTCN_Typedescriptor_t&, JSON_Tokenizer&, boolean);
162 };
163
164 extern double operator+(double double_value, const FLOAT& other_value);
165 extern double operator-(double double_value, const FLOAT& other_value);
166 extern double operator*(double double_value, const FLOAT& other_value);
167 extern double operator/(double double_value, const FLOAT& other_value);
168
169 extern boolean operator==(double double_value, const FLOAT& other_value);
170 extern boolean operator<(double double_value, const FLOAT& other_value);
171 extern boolean operator>(double double_value, const FLOAT& other_value);
172
173 inline boolean operator!=(double double_value, const FLOAT& other_value)
174 {
175 return !(double_value == other_value);
176 }
177
178 inline boolean operator<=(double double_value, const FLOAT& other_value)
179 {
180 return !(double_value > other_value);
181 }
182
183 inline boolean operator>=(double double_value, const FLOAT& other_value)
184 {
185 return !(double_value < other_value);
186 }
187
188 // float template class
189
190 class FLOAT_template : public Base_Template {
191 private:
192 union {
193 double single_value;
194 struct {
195 unsigned int n_values;
196 FLOAT_template *list_value;
197 } value_list;
198 struct {
199 double min_value, max_value;
200 boolean min_is_present, max_is_present;
201 } value_range;
202 };
203
204 void copy_template(const FLOAT_template& other_value);
205
206 public:
207 FLOAT_template();
208 FLOAT_template(template_sel other_value);
209 FLOAT_template(double other_value);
210 FLOAT_template(const FLOAT& other_value);
211 FLOAT_template(const OPTIONAL<FLOAT>& other_value);
212 FLOAT_template(const FLOAT_template& other_value);
213
214 ~FLOAT_template();
215 void clean_up();
216
217 FLOAT_template& operator=(template_sel other_value);
218 FLOAT_template& operator=(double other_value);
219 FLOAT_template& operator=(const FLOAT& other_value);
220 FLOAT_template& operator=(const OPTIONAL<FLOAT>& other_value);
221 FLOAT_template& operator=(const FLOAT_template& other_value);
222
223 boolean match(double other_value, boolean legacy = FALSE) const;
224 boolean match(const FLOAT& other_value, boolean legacy = FALSE) const;
225
226 void set_type(template_sel template_type, unsigned int list_length = 0);
227 FLOAT_template& list_item(unsigned int list_index);
228
229 void set_min(double min_value);
230 void set_min(const FLOAT& min_value);
231 void set_max(double max_value);
232 void set_max(const FLOAT& max_value);
233
234 double valueof() const;
235
236 void log() const;
237 void log_match(const FLOAT& match_value, boolean legacy = FALSE) const;
238
239 void set_param(Module_Param& param);
240 Module_Param* get_param(Module_Param_Name& param_name) const;
241
242 void encode_text(Text_Buf& text_buf) const;
243 void decode_text(Text_Buf& text_buf);
244
245 boolean is_present(boolean legacy = FALSE) const;
246 boolean match_omit(boolean legacy = FALSE) const;
247 #ifdef TITAN_RUNTIME_2
248 void valueofv(Base_Type* value) const { *(static_cast<FLOAT*>(value)) = valueof(); }
249 void set_value(template_sel other_value) { *this = other_value; }
250 void copy_value(const Base_Type* other_value) { *this = *(static_cast<const FLOAT*>(other_value)); }
251 Base_Template* clone() const { return new FLOAT_template(*this); }
252 const TTCN_Typedescriptor_t* get_descriptor() const { return &FLOAT_descr_; }
253 boolean matchv(const Base_Type* other_value, boolean legacy) const { return match(*(static_cast<const FLOAT*>(other_value)), legacy); }
254 void log_matchv(const Base_Type* match_value, boolean legacy) const { log_match(*(static_cast<const FLOAT*>(match_value)), legacy); }
255 #else
256 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
257 #endif
258 };
259
260 extern const FLOAT PLUS_INFINITY, MINUS_INFINITY, NOT_A_NUMBER;
261
262 #endif
This page took 0.059897 seconds and 5 git commands to generate.