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