OnixS C++ FIX Engine 4.13.0
API Documentation
Loading...
Searching...
No Matches
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
28namespace OnixS {
29namespace FIX {
30typedef char Char;
31
32typedef short int Int16;
33typedef short unsigned int UInt16;
34
35typedef int Int32;
36typedef unsigned int UInt32;
37
38typedef long long Int64;
39typedef unsigned long long UInt64;
40
41typedef double Double;
42
45
48{
49public:
51 Decimal(
54
58 explicit Decimal(Double value, size_t precision = 17);
59
62
65
68
71
73 bool operator == (const Decimal &) const;
74
76 bool operator != (const Decimal &) const;
77
79 bool operator < (const Decimal &) const;
80
82 bool operator > (const Decimal &) const;
83
87 operator Int32() const;
88
92 operator UInt32() const;
93
97 operator Int64() const;
98
102 operator UInt64() const;
103
107 operator Double() const;
108
112 bool toNumber(Int32 &) const;
113
117 bool toNumber(UInt32 &) const;
118
122 bool toNumber(Int64 &) const;
123
127 bool toNumber(UInt64 &) const;
128
131 bool toNumber(Double &) const;
132
134 void toString(std::string &) const;
135
137 std::string toString() const;
138
142 static
143 bool
145 const char * buffer,
146 size_t bufferSize,
147 Decimal &);
148
151 static
152 Decimal
154 const char * buffer,
155 size_t bufferSize);
156
157private:
159 DecimalMantissa mantissa_;
160
162 DecimalExponent exponent_;
163};
164
165ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &, Decimal const &);
166
167inline
171 : mantissa_(mantissa),
172 exponent_(exponent)
173{
174}
175
176inline
179{
180 return mantissa_;
181}
182
183inline
184void
186 DecimalMantissa value)
187{
188 mantissa_ = value;
189}
190
191inline
194{
195 return exponent_;
196}
197
198inline
199void
201 DecimalExponent value)
202{
203 exponent_ = value;
204}
205
206inline
207Decimal::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
218inline
219Decimal::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
230inline
231Decimal::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
242inline
243Decimal::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
254inline
255Decimal::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
266inline
267std::string
269{
270 std::string presentation;
271 toString(presentation);
272 return presentation;
273}
274
276struct
278 Number {
279 static
280 bool
282 const char * buffer,
283 size_t bufferSize,
284 Int32 & number);
285
286 static
287 bool
289 const char * buffer,
290 size_t bufferSize,
291 UInt32 & number);
292
293 static
294 bool
296 const char * buffer,
297 size_t bufferSize,
298 Int64 & number);
299
300 static
301 bool
303 const char * buffer,
304 size_t bufferSize,
305 UInt64 & number);
306
307 static
308 bool
310 const char * buffer,
311 size_t bufferSize,
312 Double & number);
313
314 static
315 bool
317 const char * buffer,
318 size_t bufferSize,
319 Decimal & number);
320};
321}
322}
#define ONIXS_FIXENGINE_API
Definition ABI.h:45
The Decimal type for a better precision.
Definition Numeric.h:48
Decimal(DecimalMantissa mantissa=0, DecimalExponent exponent=0)
Initializes an instance from compound components.
Definition Numeric.h:168
bool toNumber(Int32 &) const
Casts to the whole integer number as a regular floating point value is casted.
std::string toString() const
Returns the text presentation of the decimal.
Definition Numeric.h:268
static Decimal parse(const char *buffer, size_t bufferSize)
Parses the decimal from the string presentation.
bool toNumber(Int64 &) const
Casts to the whole integer number as a regular floating point value is casted.
Decimal(Double value, size_t precision=17)
Converts the Double value to a decimal.
DecimalExponent exponent() const
Returns the exponent part of the decimal.
Definition Numeric.h:193
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
Attempts to parse the decimal value from its string/text presentation.
bool toNumber(Double &) const
Casts to the floating point number.
void toString(std::string &) const
Appends the text presentation to the given string.
bool toNumber(UInt64 &) const
Casts to the whole integer number as a regular floating point value is casted.
bool toNumber(UInt32 &) const
Casts to the whole integer number as a regular floating point value is casted.
DecimalMantissa mantissa() const
Returns the mantissa part of the decimal.
Definition Numeric.h:178
Int32 DecimalExponent
Definition Numeric.h:44
Int64 DecimalMantissa
Definition Numeric.h:43
double Double
Definition Numeric.h:41
short int Int16
Definition Numeric.h:32
unsigned int UInt32
Definition Numeric.h:36
long long Int64
Definition Numeric.h:38
ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &os, const Group &group)
Stream output.
char Char
Definition Numeric.h:30
short unsigned int UInt16
Definition Numeric.h:33
unsigned long long UInt64
Definition Numeric.h:39
int Int32
Definition Numeric.h:35
The helper class for the conversion from a string to a number.
Definition Numeric.h:278
static bool tryParse(const char *buffer, size_t bufferSize, Int32 &number)
static bool tryParse(const char *buffer, size_t bufferSize, UInt32 &number)
static bool tryParse(const char *buffer, size_t bufferSize, UInt64 &number)
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &number)
static bool tryParse(const char *buffer, size_t bufferSize, Double &number)
static bool tryParse(const char *buffer, size_t bufferSize, Int64 &number)