OnixS C++ FIX Engine  4.2.0
API Documentation
Numeric.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #pragma once
21 
22 #include <OnixS/FIXEngine/ABI.h>
23 
24 #include <string>
25 #include <stdexcept>
26 #include <iosfwd>
27 
28 namespace OnixS {
29 namespace FIX {
30 typedef char Char;
31 
32 typedef int Int32;
33 typedef unsigned int UInt32;
34 
35 typedef long long Int64;
36 typedef unsigned long long UInt64;
37 
38 typedef double Double;
39 
40 typedef Int64 DecimalMantissa;
41 typedef Int32 DecimalExponent;
42 
43 /// Decimal type for better precision.
45 {
46 public:
47  /// Initializes instance from compound components.
48  Decimal(
49  DecimalMantissa mantissa = 0,
50  DecimalExponent exponent = 0);
51 
52  /// Converts Double value to decimal.
53  /// @param value double precision floating-point value.
54  /// @param precision defines conversion precision.
55  explicit Decimal(Double value, size_t precision = 17);
56 
57  /// Initializes as copy of given value.
58  Decimal(const Decimal & other);
59 
60  /// Returns mantissa part of decimal.
61  DecimalMantissa mantissa() const;
62 
63  /// Updates mantissa part of decimal.
64  void mantissa(DecimalMantissa);
65 
66  /// Returns exponent part of decimal.
67  DecimalExponent exponent() const;
68 
69  /// Updates exponent part of decimal.
70  void exponent(DecimalExponent);
71 
72  /// Compares two numbers.
73  bool operator == (const Decimal &) const;
74 
75  /// Compares two numbers.
76  bool operator != (const Decimal &) const;
77 
78  /// Compares two numbers.
79  bool operator < (const Decimal &) const;
80 
81  /// Compares two numbers.
82  bool operator > (const Decimal &) const;
83 
84  /// Casts to whole integer number as regular
85  /// floating point value is casted.
86  /// @throw domain_error exception on failure.
87  operator Int32() const;
88 
89  /// Casts to whole integer number as regular
90  /// floating point value is casted.
91  /// @throw domain_error exception on failure.
92  operator UInt32() const;
93 
94  /// Casts to whole integer number as regular
95  /// floating point value is casted.
96  /// @throw domain_error exception on failure.
97  operator Int64() const;
98 
99  /// Casts to whole integer number as regular
100  /// floating point value is casted.
101  /// @throw domain_error exception on failure.
102  operator UInt64() const;
103 
104  /// Casts to whole floating point as regular
105  /// value is casted to a smaller precision.
106  /// @throw domain_error exception on failure.
107  operator Double() const;
108 
109  /// Casts to whole integer number as regular
110  /// floating point value is casted.
111  /// @return false if conversion fails.
112  bool toNumber(Int32 &) const;
113 
114  /// Casts to whole integer number as regular
115  /// floating point value is casted.
116  /// @return false if conversion fails.
117  bool toNumber(UInt32 &) const;
118 
119  /// Casts to whole integer number as regular
120  /// floating point value is casted.
121  /// @return false if conversion fails.
122  bool toNumber(Int64 &) const;
123 
124  /// Casts to whole integer number as regular
125  /// floating point value is casted.
126  /// @return false if conversion fails.
127  bool toNumber(UInt64 &) const;
128 
129  /// Casts to floating point number.
130  /// @return false if conversion fails.
131  bool toNumber(Double &) const;
132 
133  /// Appends text presentation to given string.
134  void toString(std::string &) const;
135 
136  /// Returns text presentation of decimal.
137  std::string toString() const;
138 
139  /// Reinitializes instance from another one.
140  Decimal &
141  operator = (
142  const Decimal & other);
143 
144  /// Attempts to parse decimal value
145  /// from its string/text presentation.
146  /// @return false on parsing failure.
147  static
148  bool
149  tryParse(
150  const char * buffer,
151  size_t bufferSize,
152  Decimal &);
153 
154  /// Parses decimal from string presentation.
155  /// @throw std::exception on failure.
156  static
157  Decimal
158  parse(
159  const char * buffer,
160  size_t bufferSize);
161 
162 private:
163  /// Mantissa.
164  DecimalMantissa mantissa_;
165 
166  /// Exponent.
167  DecimalExponent exponent_;
168 };
169 
170 ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &, Decimal const &);
171 
172 inline
174  DecimalMantissa mantissa,
175  DecimalExponent exponent)
176  : mantissa_(mantissa),
177  exponent_(exponent)
178 {
179 }
180 
181 inline
183  const Decimal & other)
184  : mantissa_(other.mantissa_),
185  exponent_(other.exponent_)
186 {
187 }
188 
189 inline
190 DecimalMantissa
192 {
193  return mantissa_;
194 }
195 
196 inline
197 void
199  DecimalMantissa value)
200 {
201  mantissa_ = value;
202 }
203 
204 inline
205 DecimalExponent
207 {
208  return exponent_;
209 }
210 
211 inline
212 void
214  DecimalExponent value)
215 {
216  exponent_ = value;
217 }
218 
219 inline
220 Decimal::operator Int32() const
221 {
222  Int32 number;
223 
224  if(toNumber(number))
225  return number;
226 
227  throw std::domain_error(
228  "Cannot cast value to target type. ");
229 }
230 
231 inline
232 Decimal::operator UInt32() const
233 {
234  UInt32 number;
235 
236  if(toNumber(number))
237  return number;
238 
239  throw std::domain_error(
240  "Cannot cast value to target type. ");
241 }
242 
243 inline
244 Decimal::operator Int64() const
245 {
246  Int64 number;
247 
248  if(toNumber(number))
249  return number;
250 
251  throw std::domain_error(
252  "Cannot cast value to target type. ");
253 }
254 
255 inline
256 Decimal::operator UInt64() const
257 {
258  UInt64 number;
259 
260  if(toNumber(number))
261  return number;
262 
263  throw std::domain_error(
264  "Cannot cast value to target type. ");
265 }
266 
267 inline
268 Decimal::operator Double() const
269 {
270  Double number;
271 
272  if(toNumber(number))
273  return number;
274 
275  throw std::domain_error(
276  "Cannot cast value to target type. ");
277 }
278 
279 inline
280 std::string
282 {
283  std::string presentation;
284  toString(presentation);
285  return presentation;
286 }
287 
288 /// Helper class for conversion from string to number.
289 struct
291  Number {
292  static
293  bool
294  tryParse(
295  const char * buffer,
296  size_t bufferSize,
297  Int32 & number);
298 
299  static
300  bool
301  tryParse(
302  const char * buffer,
303  size_t bufferSize,
304  UInt32 & number);
305 
306  static
307  bool
308  tryParse(
309  const char * buffer,
310  size_t bufferSize,
311  Int64 & number);
312 
313  static
314  bool
315  tryParse(
316  const char * buffer,
317  size_t bufferSize,
318  UInt64 & number);
319 
320  static
321  bool
322  tryParse(
323  const char * buffer,
324  size_t bufferSize,
325  Double & number);
326 
327  static
328  bool
329  tryParse(
330  const char * buffer,
331  size_t bufferSize,
332  Decimal & number);
333 };
334 }
335 }
std::string toString() const
Returns text presentation of decimal.
Definition: Numeric.h:281
int Int32
Definition: Numeric.h:32
Helper class for conversion from string to number.
Definition: Numeric.h:289
DecimalExponent exponent() const
Returns exponent part of decimal.
Definition: Numeric.h:206
Int32 DecimalExponent
Definition: Numeric.h:41
bool operator!=(const FieldValueRef &ref, const std::string &str)
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
DecimalMantissa mantissa() const
Returns mantissa part of decimal.
Definition: Numeric.h:191
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
double Double
Definition: Numeric.h:38
Decimal type for better precision.
Definition: Numeric.h:44
char Char
Definition: Numeric.h:30
bool toNumber(Int32 &) const
Casts to whole integer number as regular floating point value is casted.
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
Attempts to parse decimal value from its string/text presentation.
Int64 DecimalMantissa
Definition: Numeric.h:40
Decimal(DecimalMantissa mantissa=0, DecimalExponent exponent=0)
Initializes instance from compound components.
Definition: Numeric.h:173
unsigned long long UInt64
Definition: Numeric.h:36
bool operator==(const FieldValueRef &ref, const std::string &str)
long long Int64
Definition: Numeric.h:35
unsigned int UInt32
Definition: Numeric.h:33