OnixS C++ CME MDP Streamlined Market Data Handler  1.2.0
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  /// 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 /// Aliases mantissa component type for the decimal type.
218 typedef Int64 DecimalMantissa;
219 
220 /// Aliases exponent component type for the decimal type.
222 
223 /// A real number with floating exponent.
225 {
226  DecimalMantissa mantissa_;
227  DecimalExponent exponent_;
228 
229 public:
230  /// Aliases mantissa component type.
231  typedef
234 
235  /// Aliases exponent component type.
236  typedef
239 
240  /// Default (zero) initialization.
242  : mantissa_(0)
243  , exponent_(0)
244  {
245  }
246 
247  /// Explicitly initializes instance from its mantissa value.
249  Mantissa mantissa,
250  Exponent exponent)
251  : mantissa_(mantissa)
252  , exponent_(exponent)
253  {
254  }
255 
256  /// Initializes instance as copy of the other one.
258  const Decimal& other)
259  : mantissa_(other.mantissa_)
260  , exponent_(other.exponent_)
261  {
262  }
263 
264  /// Initializes instance from
265  /// the fixed point decimal.
266  template
267  <
268  class OtherMantissa,
269  class OtherExponent
270  >
272  const FixedPointDecimal
273  <OtherMantissa, OtherExponent>& other)
274  : mantissa_(other.mantissa())
275  , exponent_(other.exponent())
276  {
277  }
278 
279  /// Returns mantissa of given decimal.
281  {
282  return mantissa_;
283  }
284 
285  /// Returns exponent of given decimal.
287  {
288  return exponent_;
289  }
290 
291  /// Re-initializes instance as copy of the other one.
292  Decimal&
293  operator =(
294  const Decimal& other)
295  {
296  mantissa_ = other.mantissa_;
297  exponent_ = other.exponent_;
298 
299  return *this;
300  }
301 
302  /// Re-initializes instance as a
303  /// copy of the fixed point value.
304  template
305  <
306  class OtherMantissa,
307  class OtherExponent
308  >
309  Decimal&
310  operator =(
311  const FixedPointDecimal
312  <OtherMantissa, OtherExponent>& other)
313  {
314  mantissa_ = other.mantissa();
315  exponent_ = other.exponent();
316 
317  return *this;
318  }
319 };
320 
321 //
322 
324 bool
326  const Decimal& left,
327  const Decimal& right);
328 
330 bool
332  const Decimal& left,
333  const Decimal& right);
334 
335 template
336 <
337  class Mantissa,
338  class Exponent
339 >
340 inline
341 bool
343  const Decimal& left,
344  const
346  <Mantissa, Exponent>& right)
347 {
348  return (left == Decimal(right));
349 }
350 
351 template
352 <
353  class Mantissa,
354  class Exponent
355 >
356 inline
357 bool
359  const
361  <Mantissa, Exponent>& left,
362  const Decimal& right)
363 {
364  return (Decimal(left) == right);
365 }
366 
367 template
368 <
369  class Mantissa,
370  class Exponent
371 >
372 inline
373 bool
375  const Decimal& left,
376  const
378  <Mantissa, Exponent>& right)
379 {
380  return (left != Decimal(right));
381 }
382 
383 template
384 <
385  class Mantissa,
386  class Exponent
387 >
388 inline
389 bool
391  const
393  <Mantissa, Exponent>& left,
394  const Decimal& right)
395 {
396  return (Decimal(left) != right);
397 }
398 
400 bool
401 operator <(
402  const Decimal& left,
403  const Decimal& right);
404 
406 bool
408  const Decimal& left,
409  const Decimal& right);
410 
411 inline
412 bool
414  const Decimal& left,
415  const Decimal& right)
416 {
417  return (right < left);
418 }
419 
420 template
421 <
422  class Mantissa,
423  class Exponent
424 >
425 inline
426 bool
428  const Decimal& left,
429  const
431  <Mantissa, Exponent>& right)
432 {
433  return (left < Decimal(right));
434 }
435 
436 template
437 <
438  class Mantissa,
439  class Exponent
440 >
441 inline
442 bool
444  const
446  <Mantissa, Exponent>& left,
447  const Decimal& right)
448 {
449  return (Decimal(left) < right);
450 }
451 
452 template
453 <
454  class Mantissa,
455  class Exponent
456 >
457 inline
458 bool
460  const Decimal& left,
461  const
463  <Mantissa, Exponent>& right)
464 {
465  return (left > Decimal(right));
466 }
467 
468 template
469 <
470  class Mantissa,
471  class Exponent
472 >
473 inline
474 bool
476  const
478  <Mantissa, Exponent>& left,
479  const Decimal& right)
480 {
481  return (Decimal(left) > right);
482 }
483 
485 bool
486 fromStr(
487  Decimal&,
488  const char*,
489  size_t);
490 
491 /// Serializes decimal presented by
492 /// mantissa and exponent into a string.
494 void
496  std::string&,
497  Int64,
498  Int32);
499 
500 inline
501 void
503  std::string& str,
504  const Decimal& number)
505 {
507  (
508  str,
509  number.mantissa(),
510  number.exponent()
511  );
512 }
513 
514 inline
515 std::string
517  const Decimal& number)
518 {
519  std::string str;
520 
521  toStr(str, number);
522 
523  return str;
524 }
525 
526 /// Serializes fixed-point decimal into a string.
527 template
528 <
529  class Mantissa,
530  class Exponent
531 >
532 inline
533 void
535  std::string& str,
536  const
538  <Mantissa, Exponent>& number)
539 {
540  toStr(
541  str,
542  Decimal(number));
543 }
544 
545 /// Serializes fixed-point decimal into a string.
546 template
547 <
548  class Mantissa,
549  class Exponent
550 >
551 inline
552 std::string
554  const
556  <
557  Mantissa,
558  Exponent
559  >& number)
560 {
561  std::string str;
562 
563  toStr(str, number);
564 
565  return str;
566 }
567 
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:390
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition: Decimal.h:553
bool fromStr(Decimal &, const char *, size_t)
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:280
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:257
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition: Decimal.h:233
void decimalToStr(std::string &, Int64, Int32)
Serializes decimal presented by mantissa and exponent into a string.
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition: Decimal.h:218
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:206
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:66
Represents real number with constant exponent.
Definition: Decimal.h:41
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition: Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_LTWT_CLASS
Definition: Bootstrap.h:111
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED
Definition: Compiler.h:160
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:443
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:475
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:48
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition: Decimal.h:221
FixedPointDecimal(const FixedPointDecimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:80
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:51
DecimalExponent Exponent
Aliases exponent component type.
Definition: Decimal.h:238
Decimal()
Default (zero) initialization.
Definition: Decimal.h:241
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:93
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:87
Decimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other)
Initializes instance from the fixed point decimal.
Definition: Decimal.h:271
A real number with floating exponent.
Definition: Decimal.h:224
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:248
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const Decimal &right)
Definition: Decimal.h:358
bool operator<=(const Decimal &left, const Decimal &right)
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:73
Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:286
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:169