OnixS C++ Fenics UST BIMP Market Data Handler  1.1.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 
27 
28 namespace OnixS
29 {
30  namespace FenicsUST
31  {
32  namespace MarketData
33  {
34  namespace Bimp
35  {
36 
37  /// Represents real number with constant exponent.
38  ///
39  /// BOE Platform currently operates prices
40  /// and scaling factors as real numbers with constant exponent.
41  /// This class implements concept of fixed point decimal and
42  /// lets to dramatically improve performance of book-related
43  /// operations.
44  template<class MantissaType, class ExponentType>
45  class ONIXS_FENICSUST_BIMP_API FixedPointDecimal
46  {
47  // Only mantissa is stored.
48  MantissaType mantissa_;
49 
50  public:
51  /// Aliases mantissa component type.
52  typedef MantissaType Mantissa;
53 
54  /// Aliases exponent component type.
55  typedef ExponentType Exponent;
56 
57  /// A few traits.
58  enum
59  {
60  /// Size of class in bytes.
61  ///
62  /// Instantiations of given class are used
63  /// while manipulating binary messages. Regular
64  /// composites expose given characteristics so
65  /// as this template class does.
66  Size = sizeof(Mantissa)
67  };
68 
69  /// Default (zero) initialization.
72  : mantissa_()
73  {
74  }
75 
76  /// Explicitly initializes instance from its mantissa value.
77  explicit
78  FixedPointDecimal(Mantissa mantissa)
80  : mantissa_(mantissa)
81  {
82  }
83 
84  /// Initializes instance as copy of the other one.
87  : mantissa_(other.mantissa_)
88  {
89  }
90 
91  /// Returns mantissa of given decimal.
92  Mantissa mantissa() const
94  {
95  return mantissa_;
96  }
97 
98  /// Returns exponent of given decimal.
99  Exponent exponent() const
101  {
102  return Exponent();
103  }
104 
105  /// Re-initializes instance as copy of the other one.
106  FixedPointDecimal& operator =(const FixedPointDecimal& other)
108  {
109  mantissa_ = other.mantissa_;
110 
111  return *this;
112  }
113  };
114 
115  /// Compares two fixed-point decimals.
116  template<class Mantissa, class Exponent>
120  {
121  return left.mantissa() == right.mantissa();
122  }
123 
124  /// Compares two fixed-point decimals.
125  template <class Mantissa, class Exponent>
129  {
130  return left.mantissa() != right.mantissa();
131  }
132 
133  /// Compares two fixed-point decimals.
134  template<class Mantissa, class Exponent>
138  {
139  return left.mantissa() < right.mantissa();
140  }
141 
142  /// Compares two fixed-point decimals.
143  template<class Mantissa, class Exponent>
147  {
148  return left.mantissa() > right.mantissa();
149  }
150 
151  /// Compares two fixed-point decimals.
152  template<class Mantissa, class Exponent>
156  {
157  return left.mantissa() <= right.mantissa();
158  }
159 
160  /// Compares two fixed-point decimals.
161  template<class Mantissa, class Exponent>
165  {
166  return left.mantissa() >= right.mantissa();
167  }
168 
169  /// Serializes decimal presented by
170  /// mantissa and exponent into a string.
171  ONIXS_FENICSUST_BIMP_API void decimalToStr(std::string&, Int64, Int32);
172 
173  /// Serializes fixed-point decimal into a string.
174  template <class Mantissa, class Exponent>
175  inline void toStr(std::string& str, const FixedPointDecimal<Mantissa, Exponent>& number)
176  {
177  decimalToStr(str, number.mantissa(), static_cast<Int32>(number.exponent().value()));
178  }
179 
180  /// Serializes fixed-point decimal into a string.
181  template<class Mantissa, class Exponent>
182  inline std::string toStr(const FixedPointDecimal<Mantissa, Exponent>& number)
183  {
184  std::string str;
185 
186  toStr(str, number);
187 
188  return str;
189  }
190 
191  // Serializes fixed-point decimal to stream
192  template<class Mantissa, class Exponent>
193  std::ostream& operator << (std::ostream& stream, const FixedPointDecimal<Mantissa, Exponent>& number)
194  {
195  return stream << toStr(number);
196  }
197  }
198  }
199  }
200 }
bool operator==(const PriceLevel &l, const PriceLevel &r) ONIXS_FENICSUST_BIMP_NOTHROW
compare
Definition: OrderBook.h:302
bool operator<=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:153
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:162
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:144
FixedPointDecimal() ONIXS_FENICSUST_BIMP_NOTHROW
Default (zero) initialization.
Definition: Decimal.h:70
Mantissa mantissa() const ONIXS_FENICSUST_BIMP_NOTHROW
Returns mantissa of given decimal.
Definition: Decimal.h:92
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:52
FixedPointDecimal(const FixedPointDecimal &other) ONIXS_FENICSUST_BIMP_NOTHROW
Initializes instance as copy of the other one.
Definition: Decimal.h:85
Exponent exponent() const ONIXS_FENICSUST_BIMP_NOTHROW
Returns exponent of given decimal.
Definition: Decimal.h:99
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:55
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:126
#define ONIXS_FENICSUST_BIMP_NOTHROW
Definition: Compiler.h:27
ONIXS_FENICSUST_BIMP_API void toStr(std::string &, EventCode::Enum)
Appends string presentation of object.
FixedPointDecimal(Mantissa mantissa) ONIXS_FENICSUST_BIMP_NOTHROW
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:78
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:135
ONIXS_FENICSUST_BIMP_API void decimalToStr(std::string &, Int64, Int32)