OnixS C++ CME MDP Premium Market Data Handler 5.9.0
API Documentation
Loading...
Searching...
No Matches
Decimal.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <string>
24
26
28
36template <class MantissaType, class ExponentType>
38{
39public:
41 typedef MantissaType Mantissa;
42
44 typedef ExponentType Exponent;
45
47 enum
48 {
55 Size = sizeof(Mantissa)
56 };
57
60 : mantissa_()
61 {
62 }
63
66 : mantissa_(mantissa)
67 {
68 }
69
72 {
73 return mantissa_;
74 }
75
78 {
79 return Exponent();
80 }
81
82private:
83 // Only mantissa is stored.
84 MantissaType mantissa_;
85};
86
88template <class Mantissa, class Exponent>
90{
91 return (left.mantissa() == right.mantissa());
92}
93
95template <class Mantissa, class Exponent>
97{
98 return (left.mantissa() != right.mantissa());
99}
100
102template <class Mantissa, class Exponent>
104{
105 return (left.mantissa() < right.mantissa());
106}
107
109template <class Mantissa, class Exponent>
111{
112 return (left.mantissa() > right.mantissa());
113}
114
116template <class Mantissa, class Exponent>
118{
119 return (left.mantissa() <= right.mantissa());
120}
121
123template <class Mantissa, class Exponent>
125{
126 return (left.mantissa() >= right.mantissa());
127}
128
130typedef Int64 DecimalMantissa;
131
134
137{
138public:
141
144
147 : mantissa_(0)
148 , exponent_(0)
149 {
150 }
151
154 : mantissa_(mantissa)
155 , exponent_(exponent)
156 {
157 }
158
160 Decimal(const Decimal& other)
161 : mantissa_(other.mantissa_)
162 , exponent_(other.exponent_)
163 {
164 }
165
168 template <class AMantissa, class AExponent>
170 : mantissa_(other.mantissa())
171 , exponent_(other.exponent())
172 {
173 }
174
177 {
178 return mantissa_;
179 }
180
183 {
184 return exponent_;
185 }
186
188 Decimal& operator=(const Decimal& other)
189 {
190 mantissa_ = other.mantissa_;
191 exponent_ = other.exponent_;
192
193 return *this;
194 }
195
198 template <class AMantissa, class AExponent>
200 {
201 mantissa_ = other.mantissa();
202 exponent_ = other.exponent();
203
204 return *this;
205 }
206
207private:
208 DecimalMantissa mantissa_;
209 DecimalExponent exponent_;
210};
211
214bool decimalEqual(const Decimal& left, const Decimal& right);
215
217inline bool operator==(const Decimal& left, const Decimal& right)
218{
219 return ((left.exponent() == right.exponent()) ? (left.mantissa() == right.mantissa()) : decimalEqual(left, right));
220}
221
223inline bool operator!=(const Decimal& left, const Decimal& right)
224{
225 return ((left.exponent() == right.exponent()) ? (left.mantissa() != right.mantissa()) : !decimalEqual(left, right));
226}
227
229template <class Mantissa, class Exponent>
230inline bool operator==(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
231{
232 return (left == Decimal(right));
233}
234
236template <class Mantissa, class Exponent>
237inline bool operator==(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
238{
239 return (Decimal(left) == right);
240}
241
243template <class Mantissa, class Exponent>
244inline bool operator!=(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
245{
246 return (left != Decimal(right));
247}
248
250template <class Mantissa, class Exponent>
251inline bool operator!=(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
252{
253 return (Decimal(left) != right);
254}
255
258bool decimalLess(const Decimal& left, const Decimal& right);
259
261inline bool operator<(const Decimal& left, const Decimal& right)
262{
263 return ((left.exponent() == right.exponent()) ? (left.mantissa() < right.mantissa()) : decimalLess(left, right));
264}
265
267inline bool operator>(const Decimal& left, const Decimal& right)
268{
269 return (right < left);
270}
271
273inline bool operator<=(const Decimal& left, const Decimal& right)
274{
275 return !(right < left);
276}
277
279inline bool operator>=(const Decimal& left, const Decimal& right)
280{
281 return !(right > left);
282}
283
285template <class Mantissa, class Exponent>
286inline bool operator<(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
287{
288 return (left < Decimal(right));
289}
290
292template <class Mantissa, class Exponent>
293inline bool operator<(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
294{
295 return (Decimal(left) < right);
296}
297
299template <class Mantissa, class Exponent>
300inline bool operator>(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
301{
302 return (left > Decimal(right));
303}
304
306template <class Mantissa, class Exponent>
307inline bool operator>(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
308{
309 return (Decimal(left) > right);
310}
311
315bool fromStr(Decimal&, const Char*, size_t);
316
320void decimalToStr(std::string&, Int64, Int32);
321
323inline void toStr(std::string& str, const Decimal& number)
324{
325 decimalToStr(str, number.mantissa(), number.exponent());
326}
327
329inline std::string toStr(const Decimal& number)
330{
331 std::string str;
332
333 toStr(str, number);
334
335 return str;
336}
337
339template <class Mantissa, class Exponent>
340inline void toStr(std::string& str, const FixedPointDecimal<Mantissa, Exponent>& number)
341{
342 toStr(str, Decimal(number));
343}
344
346template <class Mantissa, class Exponent>
347inline std::string toStr(const FixedPointDecimal<Mantissa, Exponent>& number)
348{
349 std::string str;
350
351 toStr(str, number);
352
353 return str;
354}
355
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:67
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:46
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:68
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:171
A real number with floating exponent.
Definition Decimal.h:137
Decimal & operator=(const Decimal &other)
Re-initializes instance as copy of the other one.
Definition Decimal.h:188
Decimal & operator=(const FixedPointDecimal< AMantissa, AExponent > &other)
Re-initializes instance as a copy of the fixed point value.
Definition Decimal.h:199
DecimalExponent Exponent
Aliases exponent component type.
Definition Decimal.h:143
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition Decimal.h:176
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:153
Decimal(const FixedPointDecimal< AMantissa, AExponent > &other)
Initializes instance from the fixed point decimal.
Definition Decimal.h:169
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition Decimal.h:160
Decimal()
Default (zero) initialization.
Definition Decimal.h:146
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition Decimal.h:140
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:182
Represents real number with constant exponent.
Definition Decimal.h:38
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:65
FixedPointDecimal()
Default (zero) initialization.
Definition Decimal.h:59
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:77
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:110
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition Decimal.h:133
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition Decimal.h:130
bool decimalLess(const Decimal &left, const Decimal &right)
Compares two decimals.
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:96
bool fromStr(Decimal &, const Char *, size_t)
Deserializes a decimal number from the given text presentation.
bool operator<=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:117
Int32 Int32
int32.
Definition Fields.h:60
char Char
Character type alias.
Definition String.h:36
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:89
void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
bool decimalEqual(const Decimal &left, const Decimal &right)
Checks two decimals for equality.
void decimalToStr(std::string &, Int64, Int32)
Serializes decimal presented by mantissa and exponent into a string.
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:124
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition Decimal.h:103