OnixS C++ CBOE CFE Binary Order Entry (BOE) Handler  1.12.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 
27 namespace OnixS
28 {
29  namespace CboeCFE
30  {
31  namespace Trading
32  {
33  namespace BOE
34  {
35 
36  /// Represents real number with constant exponent.
37  ///
38  /// BOE Platform currently operates prices
39  /// and scaling factors as real numbers with constant exponent.
40  /// This class implements concept of fixed point decimal and
41  /// lets to dramatically improve performance of book-related
42  /// operations.
43  template<class MantissaType, class ExponentType>
45  {
46  // Only mantissa is stored.
47  MantissaType mantissa_;
48 
49  public:
50  /// Aliases mantissa component type.
51  typedef MantissaType Mantissa;
52 
53  /// Aliases exponent component type.
54  typedef ExponentType Exponent;
55 
56  /// A few traits.
57  enum
58  {
59  /// Size of class in bytes.
60  ///
61  /// Instantiations of given class are used
62  /// while manipulating binary messages. Regular
63  /// composites expose given characteristics so
64  /// as this template class does.
65  Size = sizeof(Mantissa)
66  };
67 
68  /// Default (zero) initialization.
70  : mantissa_()
71  {
72  }
73 
74  /// Explicitly initializes instance from its mantissa value.
75  explicit FixedPointDecimal(Mantissa mantissa)
76  : mantissa_(mantissa)
77  {
78  }
79 
80  /// Initializes instance as copy of the other one.
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  ONIXS_BATS_BOE_CONSTEXPR
94  Exponent exponent() const
95  {
96  return Exponent();
97  }
98 
99  /// Re-initializes instance as copy of the other one.
101  {
102  mantissa_ = other.mantissa_;
103 
104  return *this;
105  }
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>
118  {
119  return left.mantissa() != right.mantissa();
120  }
121 
122  /// Compares two fixed-point decimals.
123  template<class Mantissa, class Exponent>
124  bool operator <(const FixedPointDecimal<Mantissa, Exponent>& left, const FixedPointDecimal<Mantissa, Exponent>& right)
125  {
126  return left.mantissa() < right.mantissa();
127  }
128 
129  /// Compares two fixed-point decimals.
130  template<class Mantissa, class Exponent>
132  {
133  return left.mantissa() > right.mantissa();
134  }
135 
136  /// Compares two fixed-point decimals.
137  template<class Mantissa, class Exponent>
138  bool operator <=(const FixedPointDecimal<Mantissa, Exponent>& left, const FixedPointDecimal<Mantissa, Exponent>& right)
139  {
140  return left.mantissa() <= right.mantissa();
141  }
142 
143  /// Compares two fixed-point decimals.
144  template<class Mantissa, class Exponent>
146  {
147  return left.mantissa() >= right.mantissa();
148  }
149 
150  /// Serializes decimal presented by
151  /// mantissa and exponent into a string.
152  ONIXS_CBOE_CFE_BOE_API void decimalToStr(std::string&, Int64, Int32);
153 
154  /// Serializes fixed-point decimal into a string.
155  template <class Mantissa, class Exponent>
156  inline void toStr(std::string& str, const FixedPointDecimal <Mantissa, Exponent>& number)
157  {
158  decimalToStr(str, number.mantissa(), static_cast<Int32>(number.exponent()));
159  }
160 
161  /// Serializes fixed-point decimal into a string.
162  template<class Mantissa, class Exponent>
163  inline std::string toStr(const FixedPointDecimal<Mantissa, Exponent>& number)
164  {
165  std::string str;
166 
167  toStr(str, number);
168 
169  return str;
170  }
171 
172  // Serializes fixed-point decimal to stream
173  template<class Mantissa, class Exponent>
174  std::ostream& operator << (std::ostream& stream, const FixedPointDecimal<Mantissa, Exponent>& number)
175  {
176  return stream << toStr(number);
177  }
178  }
179  }
180  }
181 }
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition: Decimal.h:87
FixedPointDecimal & operator=(const FixedPointDecimal &other)
Re-initializes instance as copy of the other one.
Definition: Decimal.h:100
ONIXS_BATS_BOE_CONSTEXPR Exponent exponent() const
Returns exponent of given decimal.
Definition: Decimal.h:94
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:131
ONIXS_CBOE_CFE_BOE_API void decimalToStr(std::string &, Int64, Int32)
void toStr(std::string &str, const FixedPointDecimal< Mantissa, Exponent > &number)
Serializes fixed-point decimal into a string.
Definition: Decimal.h:156
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:54
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:145
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:110
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:75
FixedPointDecimal(const FixedPointDecimal &other)
Initializes instance as copy of the other one.
Definition: Decimal.h:81
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:117
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:51
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:69