OnixS C++ FIX Engine  4.7.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 short int Int16;
33 typedef short unsigned int UInt16;
34 
35 typedef int Int32;
36 typedef unsigned int UInt32;
37 
38 typedef long long Int64;
39 typedef unsigned long long UInt64;
40 
41 typedef double Double;
42 
43 typedef Int64 DecimalMantissa;
44 typedef Int32 DecimalExponent;
45 
46 /// Decimal type for better precision.
48 {
49 public:
50  /// Initializes instance from compound components.
51  Decimal(
52  DecimalMantissa mantissa = 0,
53  DecimalExponent exponent = 0);
54 
55  /// Converts Double value to decimal.
56  /// @param value double precision floating-point value.
57  /// @param precision defines conversion precision.
58  explicit Decimal(Double value, size_t precision = 17);
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  /// Attempts to parse decimal value
140  /// from its string/text presentation.
141  /// @return false on parsing failure.
142  static
143  bool
144  tryParse(
145  const char * buffer,
146  size_t bufferSize,
147  Decimal &);
148 
149  /// Parses decimal from string presentation.
150  /// @throw std::exception on failure.
151  static
152  Decimal
153  parse(
154  const char * buffer,
155  size_t bufferSize);
156 
157 private:
158  /// Mantissa.
159  DecimalMantissa mantissa_;
160 
161  /// Exponent.
162  DecimalExponent exponent_;
163 };
164 
165 ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &, Decimal const &);
166 
167 inline
169  DecimalMantissa mantissa,
170  DecimalExponent exponent)
171  : mantissa_(mantissa),
172  exponent_(exponent)
173 {
174 }
175 
176 inline
177 DecimalMantissa
179 {
180  return mantissa_;
181 }
182 
183 inline
184 void
186  DecimalMantissa value)
187 {
188  mantissa_ = value;
189 }
190 
191 inline
192 DecimalExponent
194 {
195  return exponent_;
196 }
197 
198 inline
199 void
201  DecimalExponent value)
202 {
203  exponent_ = value;
204 }
205 
206 inline
207 Decimal::operator Int32() const
208 {
209  Int32 number;
210 
211  if(toNumber(number))
212  return number;
213 
214  throw std::domain_error(
215  "Cannot cast value to target type. ");
216 }
217 
218 inline
219 Decimal::operator UInt32() const
220 {
221  UInt32 number;
222 
223  if(toNumber(number))
224  return number;
225 
226  throw std::domain_error(
227  "Cannot cast value to target type. ");
228 }
229 
230 inline
231 Decimal::operator Int64() const
232 {
233  Int64 number;
234 
235  if(toNumber(number))
236  return number;
237 
238  throw std::domain_error(
239  "Cannot cast value to target type. ");
240 }
241 
242 inline
243 Decimal::operator UInt64() const
244 {
245  UInt64 number;
246 
247  if(toNumber(number))
248  return number;
249 
250  throw std::domain_error(
251  "Cannot cast value to target type. ");
252 }
253 
254 inline
255 Decimal::operator Double() const
256 {
257  Double number;
258 
259  if(toNumber(number))
260  return number;
261 
262  throw std::domain_error(
263  "Cannot cast value to target type. ");
264 }
265 
266 inline
267 std::string
269 {
270  std::string presentation;
271  toString(presentation);
272  return presentation;
273 }
274 
275 /// Helper class for conversion from string to number.
276 struct
278  Number {
279  static
280  bool
281  tryParse(
282  const char * buffer,
283  size_t bufferSize,
284  Int32 & number);
285 
286  static
287  bool
288  tryParse(
289  const char * buffer,
290  size_t bufferSize,
291  UInt32 & number);
292 
293  static
294  bool
295  tryParse(
296  const char * buffer,
297  size_t bufferSize,
298  Int64 & number);
299 
300  static
301  bool
302  tryParse(
303  const char * buffer,
304  size_t bufferSize,
305  UInt64 & number);
306 
307  static
308  bool
309  tryParse(
310  const char * buffer,
311  size_t bufferSize,
312  Double & number);
313 
314  static
315  bool
316  tryParse(
317  const char * buffer,
318  size_t bufferSize,
319  Decimal & number);
320 };
321 }
322 }
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
Int64 DecimalMantissa
Definition: Numeric.h:43
unsigned int UInt32
Definition: Numeric.h:36
long long Int64
Definition: Numeric.h:38
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
bool toNumber(Int32 &) const
Casts to whole integer number as regular floating point value is casted.
Int32 DecimalExponent
Definition: Numeric.h:44
std::string toString() const
Returns text presentation of decimal.
Definition: Numeric.h:268
DecimalMantissa mantissa() const
Returns mantissa part of decimal.
Definition: Numeric.h:178
int Int32
Definition: Numeric.h:35
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
Attempts to parse decimal value from its string/text presentation.
unsigned long long UInt64
Definition: Numeric.h:39
bool operator==(const FieldValueRef &ref, const std::string &str)
DecimalExponent exponent() const
Returns exponent part of decimal.
Definition: Numeric.h:193
Decimal type for better precision.
Definition: Numeric.h:47
Decimal(DecimalMantissa mantissa=0, DecimalExponent exponent=0)
Initializes instance from compound components.
Definition: Numeric.h:168
short unsigned int UInt16
Definition: Numeric.h:33
Helper class for conversion from string to number.
Definition: Numeric.h:276
short int Int16
Definition: Numeric.h:32
double Double
Definition: Numeric.h:41
char Char
Definition: Numeric.h:30
bool operator!=(const FieldValueRef &ref, const std::string &str)