OnixS C++ B3 BOE Binary Order Entry  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 
25 
26 #include <string>
27 
29 
30 /// Forward declarations
31 template <class MantissaType, class ExponentType> class FixedPointDecimal;
32 template <class MantissaType, class ExponentType> class FloatingPointDecimal;
33 template<class Mantissa, class Exponent> std::string toStr(const FixedPointDecimal <Mantissa, Exponent>&);
34 template<class Mantissa, class Exponent> std::string toStr(const FloatingPointDecimal <Mantissa, Exponent>&);
35 
36 /// A real number with a floating exponent.
37 template <class MantissaType, class ExponentType >
39 {
40  MantissaType mantissa_;
41  ExponentType exponent_;
42 
43 public:
44  /// \private
45  /// Init traits.
46  struct MemberTraits
47  {
48  enum { Count = 2 };
49 
50  typedef MantissaType FirstArgType;
51 
52  typedef ExponentType SecondArgType;
53  };
54 
55  /// Mantissa component type.
56  typedef MantissaType Mantissa;
57 
58  /// Exponent component type.
59  typedef ExponentType Exponent;
60 
61  /// Traits.
62  enum
63  {
64  /// Size of the class in bytes.
65  Size = sizeof(Mantissa) + sizeof(Exponent)
66  };
67 
68  /// \return a human-readable presentation.
70  std::string toString() const
71  {
72  return toStr(*this);
73  }
74 
75  /// Default (zero) initialization.
78  : mantissa_()
79  , exponent_()
80  {
81  }
82 
83  /// Explicitly initializes the instance from the mantissa and exponent values.
85  FloatingPointDecimal(Mantissa mantissa, Exponent exponent) ONIXS_B3_BOE_NOTHROW
86  : mantissa_(mantissa)
87  , exponent_(exponent)
88  {
89  }
90 
91  /// Initializes the instance as a copy of the given one.
95  : mantissa_(other.mantissa_)
96  , exponent_(other.exponent_)
97  {
98  }
99 
100  /// \return mantissa.
102  {
103  return mantissa_;
104  }
105 
106  /// \return exponent.
108  {
109  return exponent_;
110  }
111 
112  /// Re-initializes the instance as copy of the given one.
114  {
115  mantissa_ = other.mantissa_;
116  exponent_ = other.exponent_;
117 
118  return *this;
119  }
120 
121 
122  /// Initializes the instance from the floating point decimal.
123  template<class OtherMantissa, class OtherExponent>
125  : mantissa_(other.mantissa())
126  , exponent_(other.exponent())
127  {
128  }
129 
130  /// Initializes the instance from the fixed point decimal.
131  template <class OtherMantissa, class OtherExponent>
133  : mantissa_(other.mantissa())
134  , exponent_(other.exponent())
135  {
136  }
137 
138  /// Re-initializes instance as a copy of the floating point value.
139  template
140  <
141  class OtherMantissa,
142  class OtherExponent
143  >
145  operator =(
147  <OtherMantissa, OtherExponent>& other) ONIXS_B3_BOE_NOTHROW
148  {
149  mantissa_ = other.mantissa();
150  exponent_ = other.exponent();
151 
152  return *this;
153  }
154 
155  /// Re-initializes instance as a copy of the fixed point value.
156  template
157  <
158  class OtherMantissa,
159  class OtherExponent
160  >
162  {
163  mantissa_ = other.mantissa();
164  exponent_ = other.exponent();
165 
166  return *this;
167  }
168 
169  /// Serializes to the given data buffer.
170  void serialize(void* addr) const ONIXS_B3_BOE_NOTHROW
171  {
172  assert(addr);
173 
174  std::memcpy(addr, &mantissa_, sizeof(mantissa_));
175  addr = advanceByBytes(addr, sizeof(mantissa_));
176 
177  std::memcpy(addr, &exponent_, sizeof(exponent_));
178  }
179 };
180 
181 /// A real number with a constant exponent.
182 template
183 <
184  class MantissaType,
185  class ExponentType
186 >
187 class FixedPointDecimal
188 {
189  // Only mantissa is stored.
190  MantissaType mantissa_;
191 
192 public:
193  /// \private
194  /// Init traits.
195  struct MemberTraits
196  {
197  enum { Count = 1 };
198 
199  typedef MantissaType FirstArgType;
200  };
201 
202  /// Mantissa component type.
203  typedef MantissaType Mantissa;
204 
205  /// Exponent component type.
206  typedef ExponentType Exponent;
207 
208  /// Traits.
209  enum
210  {
211  /// Size of the class in bytes.
212  Size = sizeof(Mantissa)
213  };
214 
215  /// \return a human-readable presentation.
217  std::string toString() const
218  {
219  return toStr(*this);
220  }
221 
222  /// Default (zero) initialization.
225  : mantissa_()
226  {
227  }
228 
229  /// Explicitly initializes the instance from the mantissa value.
231  : mantissa_(mantissa)
232  {
233  }
234 
235  /// Initializes the instance as a copy of the given one.
237  : mantissa_(other.mantissa_)
238  {
239  }
240 
241  /// \return mantissa.
243  {
244  return mantissa_;
245  }
246 
247  /// \return exponent.
249  {
250  return Exponent();
251  }
252 
253  /// Re-initializes the instance as a copy of the given one.
255  {
256  mantissa_ = other.mantissa_;
257 
258  return *this;
259  }
260 
261  /// Serialize to the given data buffer.
262  void serialize(void* addr) const ONIXS_B3_BOE_NOTHROW
263  {
264  assert(addr);
265 
266  std::memcpy(addr, &mantissa_, sizeof(mantissa_));
267  }
268 };
269 
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
A real number with a floating exponent.
Definition: Decimal.h:32
MantissaType Mantissa
Mantissa component type.
Definition: Decimal.h:203
#define ONIXS_B3_BOE_NOTHROW
Definition: Compiler.h:182
constexpr FloatingPointDecimal(const FloatingPointDecimal &other) noexcept
Initializes the instance as a copy of the given one.
Definition: Decimal.h:93
std::string toStr(const FloatingPointDecimal< Mantissa, Exponent > &)
Serializes a floating-point decimal into a string.
constexpr FixedPointDecimal() noexcept
Default (zero) initialization.
Definition: Decimal.h:224
ExponentType Exponent
Exponent component type.
Definition: Decimal.h:59
#define ONIXS_B3_BOE_CONSTEXPR
Definition: Compiler.h:185
MantissaType Mantissa
Mantissa component type.
Definition: Decimal.h:56
constexpr FixedPointDecimal(Mantissa mantissa) noexcept
Explicitly initializes the instance from the mantissa value.
Definition: Decimal.h:230
Exponent exponent() const noexcept
Definition: Decimal.h:248
#define ONIXS_B3_BOE_NODISCARD
Definition: Compiler.h:191
ExponentType Exponent
Exponent component type.
Definition: Decimal.h:206
FloatingPointDecimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the fixed point decimal.
Definition: Decimal.h:132
constexpr FloatingPointDecimal(Mantissa mantissa, Exponent exponent) noexcept
Explicitly initializes the instance from the mantissa and exponent values.
Definition: Decimal.h:85
constexpr FloatingPointDecimal() noexcept
Default (zero) initialization.
Definition: Decimal.h:77
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
constexpr FixedPointDecimal(const FixedPointDecimal &other) noexcept
Initializes the instance as a copy of the given one.
Definition: Decimal.h:236
void serialize(void *addr) const noexcept
Serialize to the given data buffer.
Definition: Decimal.h:262
FloatingPointDecimal(const FloatingPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the floating point decimal.
Definition: Decimal.h:124
Mantissa mantissa() const noexcept
Definition: Decimal.h:242
void serialize(void *addr) const noexcept
Serializes to the given data buffer.
Definition: Decimal.h:170