OnixS C++ EuroTLX GTP Market Data Handler  1.4.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 EuroTLX
31  {
32  namespace MarketData
33  {
34  namespace GTP
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_EUROTLX_GTP_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 #if defined(ONIXS_EUROTLX_GTP_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_EUROTLX_GTP_COMPILER_CXX_RVALUE_REFERENCES
92 
95  : mantissa_(std::move(other.mantissa_))
96  {
97  }
98 
100  operator=(
101  FixedPointDecimal&& other)
103  {
104  if (this == &other)
105  return *this;
106 
107  mantissa_ = std::move(other.mantissa_);
108 
109  return *this;
110  }
111 #endif
112 
113  /// Returns mantissa of given decimal.
114  Mantissa mantissa() const
116  {
117  return mantissa_;
118  }
119 
120  /// Returns exponent of given decimal.
121  Exponent exponent() const
123  {
124  return Exponent();
125  }
126 
127  /// Re-initializes instance as copy of the other one.
128  FixedPointDecimal& operator =(const FixedPointDecimal& other)
130  {
131  mantissa_ = other.mantissa_;
132 
133  return *this;
134  }
135  };
136 
137  /// Compares two fixed-point decimals.
138  template<class Mantissa, class Exponent>
142  {
143  return left.mantissa() == right.mantissa();
144  }
145 
146  /// Compares two fixed-point decimals.
147  template <class Mantissa, class Exponent>
151  {
152  return left.mantissa() != right.mantissa();
153  }
154 
155  /// Compares two fixed-point decimals.
156  template<class Mantissa, class Exponent>
160  {
161  return left.mantissa() < right.mantissa();
162  }
163 
164  /// Compares two fixed-point decimals.
165  template<class Mantissa, class Exponent>
169  {
170  return left.mantissa() > right.mantissa();
171  }
172 
173  /// Compares two fixed-point decimals.
174  template<class Mantissa, class Exponent>
178  {
179  return left.mantissa() <= right.mantissa();
180  }
181 
182  /// Compares two fixed-point decimals.
183  template<class Mantissa, class Exponent>
187  {
188  return left.mantissa() >= right.mantissa();
189  }
190 
191  /// Serializes decimal presented by
192  /// mantissa and exponent into a string.
193  ONIXS_EUROTLX_GTP_API void decimalToStr(std::string&, Int64, Int32);
194 
195  /// Serializes fixed-point decimal into a string.
196  template <class Mantissa, class Exponent>
197  inline void toStr(std::string& str, const FixedPointDecimal<Mantissa, Exponent>& number)
198  {
199  decimalToStr(str, number.mantissa(), static_cast<Int32>(number.exponent().value()));
200  }
201 
202  /// Serializes fixed-point decimal into a string.
203  template<class Mantissa, class Exponent>
204  inline std::string toStr(const FixedPointDecimal<Mantissa, Exponent>& number)
205  {
206  std::string str;
207 
208  toStr(str, number);
209 
210  return str;
211  }
212 
213  // Serializes fixed-point decimal to stream
214  template<class Mantissa, class Exponent>
215  std::ostream& operator << (std::ostream& stream, const FixedPointDecimal<Mantissa, Exponent>& number)
216  {
217  return stream << toStr(number);
218  }
219  }
220  }
221  }
222 }
Exponent exponent() const ONIXS_EUROTLX_GTP_NOTHROW
Returns exponent of given decimal.
Definition: Decimal.h:121
ONIXS_EUROTLX_GTP_API void decimalToStr(std::string &, Int64, Int32)
#define ONIXS_EUROTLX_GTP_NOTHROW
Definition: Compiler.h:27
bool operator>(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:166
Mantissa mantissa() const ONIXS_EUROTLX_GTP_NOTHROW
Returns mantissa of given decimal.
Definition: Decimal.h:114
bool operator==(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:139
FixedPointDecimal() ONIXS_EUROTLX_GTP_NOTHROW
Default (zero) initialization.
Definition: Decimal.h:70
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:148
ExponentType Exponent
Aliases exponent component type.
Definition: Decimal.h:55
MantissaType Mantissa
Aliases mantissa component type.
Definition: Decimal.h:52
FixedPointDecimal(Mantissa mantissa) ONIXS_EUROTLX_GTP_NOTHROW
Explicitly initializes instance from its mantissa value.
Definition: Decimal.h:78
FixedPointDecimal(const FixedPointDecimal &other) ONIXS_EUROTLX_GTP_NOTHROW
Initializes instance as copy of the other one.
Definition: Decimal.h:85
bool operator<(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:157
bool operator>=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:184
ONIXS_EUROTLX_GTP_API void toStr(std::string &, EventCode::Enum)
Appends string presentation of object.
bool operator<=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
Definition: Decimal.h:175