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
17 * Szabo, Janos Zoltan – initial implementation
21 ******************************************************************************/
25 #include "Basetype.hh"
26 #include "Template.hh"
27 #include "Optional.hh"
29 #include "ttcn3float.hh"
35 class FLOAT : public Base_Type {
37 friend class FLOAT_template;
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);
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);
50 ttcn3float float_value;
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);
59 FLOAT(double other_value);
60 FLOAT(const FLOAT& other_value);
63 FLOAT& operator=(double other_value);
64 FLOAT& operator=(const FLOAT& other_value);
66 double operator+() const;
67 double operator-() const;
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;
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); }
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); }
98 operator double() const;
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); }
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);
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_; }
117 inline boolean is_present() const { return is_bound(); }
120 void set_param(Module_Param& param);
121 Module_Param* get_param(Module_Param_Name& param_name) const;
123 void encode_text(Text_Buf& text_buf) const;
124 void decode_text(Text_Buf& text_buf);
126 void encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
127 TTCN_EncDec::coding_t p_coding, ...) const;
129 void decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf,
130 TTCN_EncDec::coding_t p_coding, ...);
132 ASN_BER_TLV_t* BER_encode_TLV(const TTCN_Typedescriptor_t& p_td,
133 unsigned p_coding) const;
135 boolean BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,
136 const ASN_BER_TLV_t& p_tlv, unsigned L_form);
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;
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);
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*);
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;
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);
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);
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);
173 inline boolean operator!=(double double_value, const FLOAT& other_value)
175 return !(double_value == other_value);
178 inline boolean operator<=(double double_value, const FLOAT& other_value)
180 return !(double_value > other_value);
183 inline boolean operator>=(double double_value, const FLOAT& other_value)
185 return !(double_value < other_value);
188 // float template class
190 class FLOAT_template : public Base_Template {
195 unsigned int n_values;
196 FLOAT_template *list_value;
199 double min_value, max_value;
200 boolean min_is_present, max_is_present;
204 void copy_template(const FLOAT_template& other_value);
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);
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);
223 boolean match(double other_value, boolean legacy = FALSE) const;
224 boolean match(const FLOAT& other_value, boolean legacy = FALSE) const;
226 void set_type(template_sel template_type, unsigned int list_length = 0);
227 FLOAT_template& list_item(unsigned int list_index);
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);
234 double valueof() const;
237 void log_match(const FLOAT& match_value, boolean legacy = FALSE) const;
239 void set_param(Module_Param& param);
240 Module_Param* get_param(Module_Param_Name& param_name) const;
242 void encode_text(Text_Buf& text_buf) const;
243 void decode_text(Text_Buf& text_buf);
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); }
256 void check_restriction(template_res t_res, const char* t_name=NULL, boolean legacy = FALSE) const;
260 extern const FLOAT PLUS_INFINITY, MINUS_INFINITY, NOT_A_NUMBER;