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