OnixS C++ CME Market Data Handler  4.8.0.10
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
37 <
38  class MantissaType,
39  class ExponentType
40 >
42 {
43  // Only mantissa is stored.
44  MantissaType mantissa_;
45 
46 public:
47  /// Aliases mantissa component type.
48  typedef MantissaType Mantissa;
49 
50  /// Aliases exponent component type.
51  typedef ExponentType Exponent;
52 
53  /// A few traits.
54  enum
55  {
56  /// Size of class in bytes.
57  ///
58  /// Instantiations of given class are used
59  /// while manipulating binary messages. Regular
60  /// composites expose given characteristics so
61  /// as this template class does.
62  Size = sizeof(Mantissa)
63  };
64 
65  /// Default (zero) initialization.
67  : mantissa_()
68  {
69  }
70 
71  /// Explicitly initializes instance from its mantissa value.
72  explicit
74  Mantissa mantissa)
75  : mantissa_(mantissa)
76  {
77  }
78 
79  /// Initializes instance as copy of the other one.
81  const FixedPointDecimal& other)
82  : mantissa_(other.mantissa_)
83  {
84  }
85 
86  /// Returns mantissa of given decimal.
87  Mantissa mantissa() const
88  {
89  return mantissa_;
90  }
91 
92  /// Returns exponent of given decimal.
93  Exponent exponent() const
94  {
95  return Exponent();
96  }
97 
98  /// Re-initializes instance as copy of the other one.
100  operator =(
101  const FixedPointDecimal& other)
102  {
103  mantissa_ = other.mantissa_;
104 
105  return *this;
106  }
107 };
108 
109 /// Compares two fixed-point decimals.
110 template
111 <
112  class Mantissa,
113  class Exponent
114 >
115 bool
117  const
119  <Mantissa, Exponent>& left,
120  const
122  <Mantissa, Exponent>& right)
123 {
124  return left.mantissa() == right.mantissa();
125 }
126 
127 /// Compares two fixed-point decimals.
128 template
129 <
130  class Mantissa,
131  class Exponent
132 >
133 bool
135  const
137  <Mantissa, Exponent>& left,
138  const
140  <Mantissa, Exponent>& right)
141 {
142  return left.mantissa() != right.mantissa();
143 }
144 
145 /// Compares two fixed-point decimals.
146 template
147 <
148  class Mantissa,
149  class Exponent
150 >
151 bool
153  const
155  <Mantissa, Exponent>& left,
156  const
158  <Mantissa, Exponent>& right)
159 {
160  return left.mantissa() < right.mantissa();
161 }
162 
163 /// Compares two fixed-point decimals.
164 template
165 <
166  class Mantissa,
167  class Exponent
168 >
169 bool
171  const
173  <Mantissa, Exponent>& left,
174  const
176  <Mantissa, Exponent>& right)
177 {
178  return left.mantissa() > right.mantissa();
179 }
180 
181 /// Compares two fixed-point decimals.
182 template
183 <
184  class Mantissa,
185  class Exponent
186 >
187 bool
189  const
191  <Mantissa, Exponent>& left,
192  const
194  <Mantissa, Exponent>& right)
195 {
196  return left.mantissa() <= right.mantissa();
197 }
198 
199 /// Compares two fixed-point decimals.
200 template
201 <
202  class Mantissa,
203  class Exponent
204 >
205 bool
207  const
209  <Mantissa, Exponent>& left,
210  const
212  <Mantissa, Exponent>& right)
213 {
214  return left.mantissa() >= right.mantissa();
215 }
216 
217 //
218 
219 /// A real number with floating exponent.
221 {
222 public:
223  /// Aliases mantissa component type.
224  typedef Int64 Mantissa;
225 
226  /// Aliases exponent component type.
227  typedef Int32 Exponent;
228 
229  /// Default (zero) initialization.
231  : mantissa_(0)
232  , exponent_(0)
233  {
234  }
235 
236  /// Explicitly initializes instance from its mantissa value.
238  Mantissa mantissa,
239  Exponent exponent)
240  : mantissa_(mantissa)
241  , exponent_(exponent)
242  {
243  }
244 
245  /// Initializes instance as copy of the other one.
247  const Decimal& other)
248  : mantissa_(other.mantissa_)
249  , exponent_(other.exponent_)
250  {
251  }
252 
253  /// Initializes instance from
254  /// the fixed point decimal.
255  template
256  <
257  class OtherMantissa,
258  class OtherExponent
259  >
261  const FixedPointDecimal
262  <OtherMantissa, OtherExponent>& other)
263  : mantissa_(other.mantissa())
264  , exponent_(other.exponent())
265  {
266  }
267 
268  /// Returns mantissa of given decimal.
269  Mantissa mantissa() const
270  {
271  return mantissa_;
272  }
273 
274  /// Returns exponent of given decimal.
275  Exponent exponent() const
276  {
277  return exponent_;
278  }
279 
280  /// Re-initializes instance as copy of the other one.
281  Decimal&
282  operator =(
283  const Decimal& other)
284  {
285  mantissa_ = other.mantissa_;
286  exponent_ = other.exponent_;
287 
288  return *this;
289  }
290 
291  /// Re-initializes instance as a
292  /// copy of the fixed point value.
293  template
294  <
295  class OtherMantissa,
296  class OtherExponent
297  >
298  Decimal&
299  operator =(
300  const FixedPointDecimal
301  <OtherMantissa, OtherExponent>& other)
302  {
303  mantissa_ = other.mantissa();
304  exponent_ = other.exponent();
305 
306  return *this;
307  }
308 
309 private:
310  Mantissa mantissa_;
311  Exponent exponent_;
312 };
313 
314 //
315 
316 ONIXS_CMEMDH_EXPORTED
317 bool
319  const Decimal& left,
320  const Decimal& right);
321 
322 ONIXS_CMEMDH_EXPORTED
323 bool
325  const Decimal& left,
326  const Decimal& right);
327 
328 template
329 <
330  class Mantissa,
331  class Exponent
332 >
333 inline
334 bool
336  const Decimal& left,
337  const
339  <Mantissa, Exponent>& right)
340 {
341  return (left == Decimal(right));
342 }
343 
344 template
345 <
346  class Mantissa,
347  class Exponent
348 >
349 inline
350 bool
352  const
354  <Mantissa, Exponent>& left,
355  const Decimal& right)
356 {
357  return (Decimal(left) == right);
358 }
359 
360 template
361 <
362  class Mantissa,
363  class Exponent
364 >
365 inline
366 bool
368  const Decimal& left,
369  const
371  <Mantissa, Exponent>& right)
372 {
373  return (left != Decimal(right));
374 }
375 
376 template
377 <
378  class Mantissa,
379  class Exponent
380 >
381 inline
382 bool
384  const
386  <Mantissa, Exponent>& left,
387  const Decimal& right)
388 {
389  return (Decimal(left) != right);
390 }
391 
392 ONIXS_CMEMDH_EXPORTED
393 bool
394 operator <(
395  const Decimal& left,
396  const Decimal& right);
397 
398 ONIXS_CMEMDH_EXPORTED
399 bool
401  const Decimal& left,
402  const Decimal& right);
403 
404 inline
405 bool
407  const Decimal& left,
408  const Decimal& right)
409 {
410  return (right < left);
411 }
412 
413 template
414 <
415  class Mantissa,
416  class Exponent
417 >
418 inline
419 bool
421  const Decimal& left,
422  const
424  <Mantissa, Exponent>& right)
425 {
426  return (left < Decimal(right));
427 }
428 
429 template
430 <
431  class Mantissa,
432  class Exponent
433 >
434 inline
435 bool
437  const
439  <Mantissa, Exponent>& left,
440  const Decimal& right)
441 {
442  return (Decimal(left) < right);
443 }
444 
445 template
446 <
447  class Mantissa,
448  class Exponent
449 >
450 inline
451 bool
453  const Decimal& left,
454  const
456  <Mantissa, Exponent>& right)
457 {
458  return (left > Decimal(right));
459 }
460 
461 template
462 <
463  class Mantissa,
464  class Exponent
465 >
466 inline
467 bool
469  const
471  <Mantissa, Exponent>& left,
472  const Decimal& right)
473 {
474  return (Decimal(left) > right);
475 }
476 
477 /// Serializes decimal presented by
478 /// mantissa and exponent into a string.
479 ONIXS_CMEMDH_EXPORTED
480 void
482  std::string&,
483  Int64,
484  Int32);
485 
486 inline
487 void
489  std::string& str,
490  const Decimal& number)
491 {
493  (
494  str,
495  number.mantissa(),
496  number.exponent()
497  );
498 }
499 
500 inline
501 std::string
503  const Decimal& number)
504 {
505  std::string str;
506 
507  toStr(str, number);
508 
509  return str;
510 }
511 
512 /// Serializes fixed-point decimal into a string.
513 template
514 <
515  class Mantissa,
516  class Exponent
517 >
518 inline
519 void
521  std::string& str,
522  const
524  <Mantissa, Exponent>& number)
525 {
526  toStr(
527  str,
528  Decimal(number));
529 }
530 
531 /// Serializes fixed-point decimal into a string.
532 template
533 <
534  class Mantissa,
535  class Exponent
536 >
537 inline
538 std::string
540  const
542  <
543  Mantissa,
544  Exponent
545  >& number)
546 {
547  std::string str;
548 
549  toStr(str, number);
550 
551  return str;
552 }
553 
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:351
#define ONIXS_CMEMDH_LTWT_CLASS
Definition: Bootstrap.h:110
ONIXS_CMEMDH_EXPORTED bool operator<=(const Decimal &left, const Decimal &right)
Int32 Int32
int32.
Definition: Fields.h:63
Int32 Exponent
Aliases exponent component type.
Definition: Decimal.h:227
Decimal()
Default (zero) initialization.
Definition: Decimal.h:230
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:246
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition: Decimal.h:539
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:237
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:468
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:269
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:48
Decimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other)
Definition: Decimal.h:260
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:73
A real number with floating exponent.
Definition: Decimal.h:220
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:168
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:87
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:275
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:93
Int64 Mantissa
Aliases mantissa component type.
Definition: Decimal.h:224
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:436
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:51
FixedPointDecimal(const FixedPointDecimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:80
ONIXS_CMEMDH_EXPORTED void decimalToStr(std::string &, Int64, Int32)
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:383
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:66
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:206
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:172