OnixS C++ CME MDP Conflated UDP Handler  1.1.2
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 
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  /// Returns mantissa of given decimal.
80  Mantissa mantissa() const
81  {
82  return mantissa_;
83  }
84 
85  /// Returns exponent of given decimal.
86  Exponent exponent() const
87  {
88  return Exponent();
89  }
90 };
91 
92 /// Compares two fixed-point decimals.
93 template
94 <
95  class Mantissa,
96  class Exponent
97 >
98 bool
100  const
102  <Mantissa, Exponent>& left,
103  const
105  <Mantissa, Exponent>& right)
106 {
107  return
108  (
109  left.mantissa() ==
110  right.mantissa()
111  );
112 }
113 
114 /// Compares two fixed-point decimals.
115 template
116 <
117  class Mantissa,
118  class Exponent
119 >
120 bool
122  const
124  <Mantissa, Exponent>& left,
125  const
127  <Mantissa, Exponent>& right)
128 {
129  return
130  (
131  left.mantissa() !=
132  right.mantissa()
133  );
134 }
135 
136 /// Compares two fixed-point decimals.
137 template
138 <
139  class Mantissa,
140  class Exponent
141 >
142 bool
144  const
146  <Mantissa, Exponent>& left,
147  const
149  <Mantissa, Exponent>& right)
150 {
151  return
152  (
153  left.mantissa() <
154  right.mantissa()
155  );
156 }
157 
158 /// Compares two fixed-point decimals.
159 template
160 <
161  class Mantissa,
162  class Exponent
163 >
164 bool
166  const
168  <Mantissa, Exponent>& left,
169  const
171  <Mantissa, Exponent>& right)
172 {
173  return
174  (
175  left.mantissa() >
176  right.mantissa()
177  );
178 }
179 
180 /// Compares two fixed-point decimals.
181 template
182 <
183  class Mantissa,
184  class Exponent
185 >
186 bool
188  const
190  <Mantissa, Exponent>& left,
191  const
193  <Mantissa, Exponent>& right)
194 {
195  return
196  (
197  left.mantissa() <=
198  right.mantissa()
199  );
200 }
201 
202 /// Compares two fixed-point decimals.
203 template
204 <
205  class Mantissa,
206  class Exponent
207 >
208 bool
210  const
212  <Mantissa, Exponent>& left,
213  const
215  <Mantissa, Exponent>& right)
216 {
217  return
218  (
219  left.mantissa() >=
220  right.mantissa()
221  );
222 }
223 
224 /// Aliases mantissa component type for the decimal type.
225 typedef Int64 DecimalMantissa;
226 
227 /// Aliases exponent component type for the decimal type.
229 
230 /// A real number with floating exponent.
232 {
233  DecimalMantissa mantissa_;
234  DecimalExponent exponent_;
235 
236 public:
237  /// Aliases mantissa component type.
238  typedef
241 
242  /// Aliases exponent component type.
243  typedef
246 
247  /// Default (zero) initialization.
249  : mantissa_(0)
250  , exponent_(0)
251  {
252  }
253 
254  /// Explicitly initializes instance from its mantissa value.
256  Mantissa mantissa,
257  Exponent exponent)
258  : mantissa_(mantissa)
259  , exponent_(exponent)
260  {
261  }
262 
263  /// Initializes instance as copy of the other one.
265  const Decimal& other)
266  : mantissa_(other.mantissa_)
267  , exponent_(other.exponent_)
268  {
269  }
270 
271  /// Initializes instance from
272  /// the fixed point decimal.
273  template
274  <
275  class AMantissa,
276  class AExponent
277  >
279  const
281  <AMantissa, AExponent>& other)
282  : mantissa_(
283  other.mantissa())
284  , exponent_(
285  other.exponent())
286  {
287  }
288 
289  /// Returns mantissa of given decimal.
291  {
292  return mantissa_;
293  }
294 
295  /// Returns exponent of given decimal.
297  {
298  return exponent_;
299  }
300 
301  /// Re-initializes instance as copy of the other one.
302  Decimal&
303  operator =(
304  const Decimal& other)
305  {
306  mantissa_ = other.mantissa_;
307  exponent_ = other.exponent_;
308 
309  return *this;
310  }
311 
312  /// Re-initializes instance as a
313  /// copy of the fixed point value.
314  template
315  <
316  class AMantissa,
317  class AExponent
318  >
319  Decimal&
320  operator =(
321  const
323  <AMantissa, AExponent>& other)
324  {
325  mantissa_ = other.mantissa();
326  exponent_ = other.exponent();
327 
328  return *this;
329  }
330 };
331 
332 /// Checks two decimals for equality.
333 ONIXS_CONFLATEDUDP_EXPORTED
334 bool
336  const Decimal& left,
337  const Decimal& right);
338 
339 /// Checks two decimals for equality.
340 inline
341 bool
343  const Decimal& left,
344  const Decimal& right)
345 {
346  return
347  (
348  (
349  left.exponent() ==
350  right.exponent()
351  )
352  ? (
353  left.mantissa() ==
354  right.mantissa()
355  )
356  : decimalEqual
357  (
358  left,
359  right
360  )
361  );
362 }
363 
364 /// Checks two decimals for inequality.
365 inline
366 bool
368  const Decimal& left,
369  const Decimal& right)
370 {
371  return
372  (
373  (
374  left.exponent() ==
375  right.exponent()
376  )
377  ? (
378  left.mantissa() !=
379  right.mantissa()
380  )
381  : !decimalEqual
382  (
383  left,
384  right
385  )
386  );
387 }
388 
389 /// Checks two decimals for equality.
390 template
391 <
392  class Mantissa,
393  class Exponent
394 >
395 inline
396 bool
398  const Decimal& left,
399  const
401  <Mantissa, Exponent>& right)
402 {
403  return (left == Decimal(right));
404 }
405 
406 /// Checks two decimals for equality.
407 template
408 <
409  class Mantissa,
410  class Exponent
411 >
412 inline
413 bool
415  const
417  <Mantissa, Exponent>& left,
418  const Decimal& right)
419 {
420  return (Decimal(left) == right);
421 }
422 
423 /// Checks two decimals for inequality.
424 template
425 <
426  class Mantissa,
427  class Exponent
428 >
429 inline
430 bool
432  const Decimal& left,
433  const
435  <Mantissa, Exponent>& right)
436 {
437  return (left != Decimal(right));
438 }
439 
440 /// Checks two decimals for inequality.
441 template
442 <
443  class Mantissa,
444  class Exponent
445 >
446 inline
447 bool
449  const
451  <Mantissa, Exponent>& left,
452  const Decimal& right)
453 {
454  return (Decimal(left) != right);
455 }
456 
457 /// Compares two decimals.
458 ONIXS_CONFLATEDUDP_EXPORTED
459 bool
461  const Decimal& left,
462  const Decimal& right);
463 
464 /// Compares two decimals.
465 inline
466 bool
468  const Decimal& left,
469  const Decimal& right)
470 {
471  return
472  (
473  (
474  left.exponent() ==
475  right.exponent()
476  )
477  ? (
478  left.mantissa() <
479  right.mantissa()
480  )
481  : decimalLess
482  (
483  left,
484  right
485  )
486  );
487 }
488 
489 /// Compares two decimals.
490 inline
491 bool
493  const Decimal& left,
494  const Decimal& right)
495 {
496  return (right < left);
497 }
498 
499 /// Compares two decimals.
500 inline
501 bool
503  const Decimal& left,
504  const Decimal& right)
505 {
506  return !(right < left);
507 }
508 
509 /// Compares two decimals.
510 inline
511 bool
513  const Decimal& left,
514  const Decimal& right)
515 {
516  return !(right > left);
517 }
518 
519 /// Compares two decimals.
520 template
521 <
522  class Mantissa,
523  class Exponent
524 >
525 inline
526 bool
528  const Decimal& left,
529  const
531  <Mantissa, Exponent>& right)
532 {
533  return (left < Decimal(right));
534 }
535 
536 /// Compares two decimals.
537 template
538 <
539  class Mantissa,
540  class Exponent
541 >
542 inline
543 bool
545  const
547  <Mantissa, Exponent>& left,
548  const Decimal& right)
549 {
550  return (Decimal(left) < right);
551 }
552 
553 /// Compares two decimals.
554 template
555 <
556  class Mantissa,
557  class Exponent
558 >
559 inline
560 bool
562  const Decimal& left,
563  const
565  <Mantissa, Exponent>& right)
566 {
567  return (left > Decimal(right));
568 }
569 
570 /// Compares two decimals.
571 template
572 <
573  class Mantissa,
574  class Exponent
575 >
576 inline
577 bool
579  const
581  <Mantissa, Exponent>& left,
582  const Decimal& right)
583 {
584  return (Decimal(left) > right);
585 }
586 
587 /// Deserializes a decimal number
588 /// from the given text presentation.
589 ONIXS_CONFLATEDUDP_EXPORTED
590 bool
591 fromStr(
592  Decimal&,
593  const Char*,
594  size_t);
595 
596 /// Serializes decimal presented by
597 /// mantissa and exponent into a string.
598 ONIXS_CONFLATEDUDP_EXPORTED
599 void
601  std::string&,
602  Int64,
603  Int32);
604 
605 /// Serializes decimal into a string.
606 inline
607 void
609  std::string& str,
610  const Decimal& number)
611 {
613  (
614  str,
615  number.mantissa(),
616  number.exponent()
617  );
618 }
619 
620 /// Serializes decimal into a string.
621 inline
622 std::string
624  const Decimal& number)
625 {
626  std::string str;
627 
628  toStr(str, number);
629 
630  return str;
631 }
632 
633 /// Serializes fixed-point decimal into a string.
634 template
635 <
636  class Mantissa,
637  class Exponent
638 >
639 inline
640 void
642  std::string& str,
643  const
645  <Mantissa, Exponent>& number)
646 {
647  toStr(
648  str,
649  Decimal(number));
650 }
651 
652 /// Serializes fixed-point decimal into a string.
653 template
654 <
655  class Mantissa,
656  class Exponent
657 >
658 inline
659 std::string
661  const
663  <
664  Mantissa,
665  Exponent
666  >& number)
667 {
668  std::string str;
669 
670  toStr(str, number);
671 
672  return str;
673 }
674 
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:578
Decimal(const FixedPointDecimal< AMantissa, AExponent > &other)
Definition: Decimal.h:278
A real number with floating exponent.
Definition: Decimal.h:231
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:73
Decimal()
Default (zero) initialization.
Definition: Decimal.h:248
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition: Decimal.h:660
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:80
ONIXS_CONFLATEDUDP_EXPORTED void decimalToStr(std::string &, Int64, Int32)
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:290
Int32 Int32
int32.
Definition: Fields.h:69
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:255
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:264
ONIXS_CONFLATEDUDP_EXPORTED bool decimalLess(const Decimal &left, const Decimal &right)
Compares two decimals.
char Char
Character type alias.
Definition: String.h:36
bool operator<=(const Decimal &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:502
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Checks two decimals for inequality.
Definition: Decimal.h:448
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:48
DecimalExponent Exponent
Aliases exponent component type.
Definition: Decimal.h:245
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:544
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:296
ONIXS_CONFLATEDUDP_EXPORTED bool fromStr(Decimal &, const Char *, size_t)
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition: Bootstrap.h:157
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Checks two decimals for equality.
Definition: Decimal.h:414
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:86
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition: Decimal.h:240
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition: Decimal.h:225
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:66
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:51
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition: Bootstrap.h:95
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition: Decimal.h:228
ONIXS_CONFLATEDUDP_EXPORTED bool decimalEqual(const Decimal &left, const Decimal &right)
Checks two decimals for equality.
bool operator>=(const Decimal &left, const Decimal &right)
Compares two decimals.
Definition: Decimal.h:512
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition: Bootstrap.h:153