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 ///////////////////////////////////////////////////////////////////////////////
11 #include "Basetype.hh"
12 #include "Template.hh"
13 #include "Optional.hh"
15 #include "ttcn3float.hh"
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
28 class FLOAT : public Base_Type {
30 friend class FLOAT_template;
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);
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);
43 ttcn3float float_value;
48 FLOAT(double other_value);
49 FLOAT(const FLOAT& other_value);
52 FLOAT& operator=(double other_value);
53 FLOAT& operator=(const FLOAT& other_value);
55 double operator+() const;
56 double operator-() const;
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;
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); }
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); }
87 operator double() const;
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); }
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);
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_; }
106 inline boolean is_present() const { return is_bound(); }
109 void set_param(Module_Param& param);
111 void encode_text(Text_Buf& text_buf) const;
112 void decode_text(Text_Buf& text_buf);
114 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
115 TTCN_EncDec::coding_t p_coding, ...) const;
117 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
118 TTCN_EncDec::coding_t p_coding, ...);
120 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
121 unsigned p_coding) const;
123 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
124 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
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;
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);
138 int XER_encode(const XERdescriptor_t& p_td,
139 TTCN_Buffer& p_buf, unsigned int flavor, int indent, embed_values_enc_struct_t*) const;
140 int XER_decode(const XERdescriptor_t& p_td, XmlReaderWrap& reader,
141 unsigned int flavor, embed_values_dec_struct_t*);
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;
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);
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);
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);
161 inline boolean operator!=(double double_value, const FLOAT& other_value)
163 return !(double_value == other_value);
166 inline boolean operator<=(double double_value, const FLOAT& other_value)
168 return !(double_value > other_value);
171 inline boolean operator>=(double double_value, const FLOAT& other_value)
173 return !(double_value < other_value);
176 // float template class
178 class FLOAT_template : public Base_Template {
183 unsigned int n_values;
184 FLOAT_template *list_value;
187 double min_value, max_value;
188 boolean min_is_present, max_is_present;
192 void copy_template(const FLOAT_template& other_value);
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);
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);
211 boolean match(double other_value) const;
212 boolean match(const FLOAT& other_value) const;
214 void set_type(template_sel template_type, unsigned int list_length = 0);
215 FLOAT_template& list_item(unsigned int list_index);
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);
222 double valueof() const;
225 void log_match(const FLOAT& match_value) const;
227 void set_param(Module_Param& param);
229 void encode_text(Text_Buf& text_buf) const;
230 void decode_text(Text_Buf& text_buf);
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))); }
243 void check_restriction(template_res t_res, const char* t_name=NULL) const;
247 extern const FLOAT PLUS_INFINITY, MINUS_INFINITY, NOT_A_NUMBER;