OnixS C++ CME MDP Premium Market Data Handler  5.8.3
API Documentation
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 
25 #include <OnixS/CME/MDH/Integral.h>
26 
28 
29 /// Represents real number with constant exponent.
30 ///
31 /// CME Market Data Platform currently operates prices
32 /// and scaling factors as real numbers with constant exponent.
33 /// This class implements concept of fixed point decimal and
34 /// lets to dramatically improve performance of book-related
35 /// operations.
36 template <class MantissaType, class ExponentType>
38 {
39 public:
40  /// Aliases mantissa component type.
41  typedef MantissaType Mantissa;
42 
43  /// Aliases exponent component type.
44  typedef ExponentType Exponent;
45 
46  /// A few traits.
47  enum
48  {
49  /// Size of class in bytes.
50  ///
51  /// Instantiations of given class are used
52  /// while manipulating binary messages. Regular
53  /// composites expose given characteristics so
54  /// as this template class does.
55  Size = sizeof(Mantissa)
56  };
57 
58  /// Default (zero) initialization.
60  : mantissa_()
61  {
62  }
63 
64  /// Explicitly initializes instance from its mantissa value.
65  explicit FixedPointDecimal(Mantissa mantissa)
66  : mantissa_(mantissa)
67  {
68  }
69 
70  /// Returns mantissa of given decimal.
71  Mantissa mantissa() const
72  {
73  return mantissa_;
74  }
75 
76  /// Returns exponent of given decimal.
77  Exponent exponent() const
78  {
79  return Exponent();
80  }
81 
82 private:
83  // Only mantissa is stored.
84  MantissaType mantissa_;
85 };
86 
87 /// Compares two fixed-point decimals.
88 template <class Mantissa, class Exponent>
90 {
91  return (left.mantissa() == right.mantissa());
92 }
93 
94 /// Compares two fixed-point decimals.
95 template <class Mantissa, class Exponent>
97 {
98  return (left.mantissa() != right.mantissa());
99 }
100 
101 /// Compares two fixed-point decimals.
102 template <class Mantissa, class Exponent>
103 bool operator<(const FixedPointDecimal<Mantissa, Exponent>& left, const FixedPointDecimal<Mantissa, Exponent>& right)
104 {
105  return (left.mantissa() < right.mantissa());
106 }
107 
108 /// Compares two fixed-point decimals.
109 template <class Mantissa, class Exponent>
111 {
112  return (left.mantissa() > right.mantissa());
113 }
114 
115 /// Compares two fixed-point decimals.
116 template <class Mantissa, class Exponent>
117 bool operator<=(const FixedPointDecimal<Mantissa, Exponent>& left, const FixedPointDecimal<Mantissa, Exponent>& right)
118 {
119  return (left.mantissa() <= right.mantissa());
120 }
121 
122 /// Compares two fixed-point decimals.
123 template <class Mantissa, class Exponent>
125 {
126  return (left.mantissa() >= right.mantissa());
127 }
128 
129 /// Aliases mantissa component type for the decimal type.
130 typedef Int64 DecimalMantissa;
131 
132 /// Aliases exponent component type for the decimal type.
134 
135 /// A real number with floating exponent.
137 {
138 public:
139  /// Aliases mantissa component type.
141 
142  /// Aliases exponent component type.
144 
145  /// Default (zero) initialization.
147  : mantissa_(0)
148  , exponent_(0)
149  {
150  }
151 
152  /// Explicitly initializes instance from its mantissa value.
153  Decimal(Mantissa mantissa, Exponent exponent)
154  : mantissa_(mantissa)
155  , exponent_(exponent)
156  {
157  }
158 
159  /// Initializes instance as copy of the other one.
160  Decimal(const Decimal& other)
161  : mantissa_(other.mantissa_)
162  , exponent_(other.exponent_)
163  {
164  }
165 
166  /// Initializes instance from
167  /// the fixed point decimal.
168  template <class AMantissa, class AExponent>
170  : mantissa_(other.mantissa())
171  , exponent_(other.exponent())
172  {
173  }
174 
175  /// Returns mantissa of given decimal.
176  Mantissa mantissa() const
177  {
178  return mantissa_;
179  }
180 
181  /// Returns exponent of given decimal.
182  Exponent exponent() const
183  {
184  return exponent_;
185  }
186 
187  /// Re-initializes instance as copy of the other one.
188  Decimal& operator=(const Decimal& other)
189  {
190  mantissa_ = other.mantissa_;
191  exponent_ = other.exponent_;
192 
193  return *this;
194  }
195 
196  /// Re-initializes instance as a
197  /// copy of the fixed point value.
198  template <class AMantissa, class AExponent>
200  {
201  mantissa_ = other.mantissa();
202  exponent_ = other.exponent();
203 
204  return *this;
205  }
206 
207 private:
208  DecimalMantissa mantissa_;
209  DecimalExponent exponent_;
210 };
211 
212 /// Checks two decimals for equality.
214 bool decimalEqual(const Decimal& left, const Decimal& right);
215 
216 /// Checks two decimals for equality.
217 inline bool operator==(const Decimal& left, const Decimal& right)
218 {
219  return ((left.exponent() == right.exponent()) ? (left.mantissa() == right.mantissa()) : decimalEqual(left, right));
220 }
221 
222 /// Checks two decimals for inequality.
223 inline bool operator!=(const Decimal& left, const Decimal& right)
224 {
225  return ((left.exponent() == right.exponent()) ? (left.mantissa() != right.mantissa()) : !decimalEqual(left, right));
226 }
227 
228 /// Checks two decimals for equality.
229 template <class Mantissa, class Exponent>
230 inline bool operator==(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
231 {
232  return (left == Decimal(right));
233 }
234 
235 /// Checks two decimals for equality.
236 template <class Mantissa, class Exponent>
237 inline bool operator==(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
238 {
239  return (Decimal(left) == right);
240 }
241 
242 /// Checks two decimals for inequality.
243 template <class Mantissa, class Exponent>
244 inline bool operator!=(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
245 {
246  return (left != Decimal(right));
247 }
248 
249 /// Checks two decimals for inequality.
250 template <class Mantissa, class Exponent>
251 inline bool operator!=(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
252 {
253  return (Decimal(left) != right);
254 }
255 
256 /// Compares two decimals.
258 bool decimalLess(const Decimal& left, const Decimal& right);
259 
260 /// Compares two decimals.
261 inline bool operator<(const Decimal& left, const Decimal& right)
262 {
263  return ((left.exponent() == right.exponent()) ? (left.mantissa() < right.mantissa()) : decimalLess(left, right));
264 }
265 
266 /// Compares two decimals.
267 inline bool operator>(const Decimal& left, const Decimal& right)
268 {
269  return (right < left);
270 }
271 
272 /// Compares two decimals.
273 inline bool operator<=(const Decimal& left, const Decimal& right)
274 {
275  return !(right < left);
276 }
277 
278 /// Compares two decimals.
279 inline bool operator>=(const Decimal& left, const Decimal& right)
280 {
281  return !(right > left);
282 }
283 
284 /// Compares two decimals.
285 template <class Mantissa, class Exponent>
286 inline bool operator<(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
287 {
288  return (left < Decimal(right));
289 }
290 
291 /// Compares two decimals.
292 template <class Mantissa, class Exponent>
293 inline bool operator<(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
294 {
295  return (Decimal(left) < right);
296 }
297 
298 /// Compares two decimals.
299 template <class Mantissa, class Exponent>
300 inline bool operator>(const Decimal& left, const FixedPointDecimal<Mantissa, Exponent>& right)
301 {
302  return (left > Decimal(right));
303 }
304 
305 /// Compares two decimals.
306 template <class Mantissa, class Exponent>
307 inline bool operator>(const FixedPointDecimal<Mantissa, Exponent>& left, const Decimal& right)
308 {
309  return (Decimal(left) > right);
310 }
311 
312 /// Deserializes a decimal number
313 /// from the given text presentation.
315 bool fromStr(Decimal&, const Char*, size_t);
316 
317 /// Serializes decimal presented by
318 /// mantissa and exponent into a string.
320 void decimalToStr(std::string&, Int64, Int32);
321 
322 /// Serializes decimal into a string.
323 inline void toStr(std::string& str, const Decimal& number)
324 {
325  decimalToStr(str, number.mantissa(), number.exponent());
326 }
327 
328 /// Serializes decimal into a string.
329 inline std::string toStr(const Decimal& number)
330 {
331  std::string str;
332 
333  toStr(str, number);
334 
335  return str;
336 }
337 
338 /// Serializes fixed-point decimal into a string.
339 template <class Mantissa, class Exponent>
340 inline void toStr(std::string& str, const FixedPointDecimal<Mantissa, Exponent>& number)
341 {
342  toStr(str, Decimal(number));
343 }
344 
345 /// Serializes fixed-point decimal into a string.
346 template <class Mantissa, class Exponent>
347 inline std::string toStr(const FixedPointDecimal<Mantissa, Exponent>& number)
348 {
349  std::string str;
350 
351  toStr(str, number);
352 
353  return str;
354 }
355 
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition: Decimal.h:130
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Checks two decimals for equality.
Definition: Decimal.h:237
Int32 Int32
int32.
Definition: Fields.h:60
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition: Decimal.h:140
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition: Decimal.h:133
DecimalExponent Exponent
Aliases exponent component type.
Definition: Decimal.h:143
Decimal()
Default (zero) initialization.
Definition: Decimal.h:146
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:160
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition: Decimal.h:347
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:153
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:307
void decimalToStr(std::string &, Int64, Int32)
Serializes decimal presented by mantissa and exponent into a string.
Represents real number with constant exponent.
Definition: Decimal.h:37
#define ONIXS_CMEMDH_LTWT
Definition: Bootstrap.h:46
Decimal(const FixedPointDecimal< AMantissa, AExponent > &other)
Initializes instance from the fixed point decimal.
Definition: Decimal.h:169
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:176
Decimal & operator=(const FixedPointDecimal< AMantissa, AExponent > &other)
Re-initializes instance as a copy of the fixed point value.
Definition: Decimal.h:199
char Char
Character type alias.
Definition: String.h:36
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:41
bool decimalEqual(const Decimal &left, const Decimal &right)
Checks two decimals for equality.
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:65
bool decimalLess(const Decimal &left, const Decimal &right)
Compares two decimals.
A real number with floating exponent.
Definition: Decimal.h:136
bool operator>=(const Decimal &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:279
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:71
bool fromStr(Decimal &, const Char *, size_t)
Deserializes a decimal number from the given text presentation.
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:182
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:77
#define ONIXS_CMEMDH_EXPORTED
Definition: Compiler.h:135
Decimal & operator=(const Decimal &other)
Re-initializes instance as copy of the other one.
Definition: Decimal.h:188
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:293
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:44
bool operator<=(const Decimal &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:273
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Checks two decimals for inequality.
Definition: Decimal.h:251
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:59
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68