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