OnixS C++ B3 Binary UMDF Market Data Handler  1.3.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 Mantissa, class Exponent> std::string toStr(const FixedPointDecimal <Mantissa, Exponent>&);
33 
34 /// A real number with a constant exponent.
35 template
36 <
37  class MantissaType,
38  class ExponentType
39 >
41 {
42  // Only mantissa is stored.
43  MantissaType mantissa_;
44 
45 public:
46  /// \private
47  /// Init traits.
48  struct MemberTraits
49  {
50  enum { Count = 1 };
51 
52  typedef MantissaType FirstArgType;
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)
66  };
67 
68  /// \return a human-readable presentation.
69  ONIXS_B3_UMDF_MD_NODISCARD
70  std::string toString() const
71  {
72  return toStr(*this);
73  }
74 
75  /// Default (zero) initialization.
78  : mantissa_()
79  {
80  }
81 
82  /// Explicitly initializes the instance from the mantissa value.
84  : mantissa_(mantissa)
85  {
86  }
87 
88  /// Initializes the instance as a copy of the given one.
90  : mantissa_(other.mantissa_)
91  {
92  }
93 
94  /// \return mantissa.
96  {
97  return mantissa_;
98  }
99 
100  /// \return exponent.
102  {
103  return Exponent();
104  }
105 
106  /// Re-initializes the instance as a copy of the given one.
108  {
109  mantissa_ = other.mantissa_;
110 
111  return *this;
112  }
113 
114  /// Serialize to the given data buffer.
115  void serialize(void* addr) const ONIXS_B3_UMDF_MD_NOTHROW
116  {
117  assert(addr);
118 
119  std::memcpy(addr, &mantissa_, sizeof(mantissa_));
120  }
121 };
122 
123 /// A nullable real number with a constant exponent.
124 template
125 <
126  class MantissaType,
127  class ExponentType,
128  class NullMantissaType
129 >
130 class NullableFixedPointDecimal : public FixedPointDecimal<MantissaType, ExponentType>
131 {
132 public:
133  /// Default (zero) initialization.
136  : FixedPointDecimal<MantissaType, ExponentType>()
137  {
138  }
139 
140  /// Explicitly initializes the instance from the mantissa value.
143  {
144  }
145 
147  static NullMantissaType nullMantissa() ONIXS_B3_UMDF_MD_NOTHROW
148  {
149  return NullMantissaType();
150  }
151 
154  {
155  return NullableFixedPointDecimal(nullMantissa());
156  }
157 
158 
159  /// \return whether the value is `Null`.
160  bool isNull() const { return nullMantissa() == this->mantissa(); }
161 };
162 
ONIXS_B3_UMDF_MD_NODISCARD std::string toString() const
Definition: Decimal.h:70
ExponentType Exponent
Exponent component type.
Definition: Decimal.h:59
FixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:77
#define ONIXS_B3_UMDF_MD_NOTHROW
Definition: Compiler.h:114
#define ONIXS_B3_UMDF_MD_CONSTEXPR
Definition: Compiler.h:120
NullableFixedPointDecimal()
Default (zero) initialization.
Definition: Decimal.h:135
NullableFixedPointDecimal(MantissaType mantissa)
Explicitly initializes the instance from the mantissa value.
Definition: Decimal.h:141
FixedPointDecimal(const FixedPointDecimal &other)
Initializes the instance as a copy of the given one.
Definition: Decimal.h:89
void serialize(void *addr) const
Serialize to the given data buffer.
Definition: Decimal.h:115
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_END
Definition: ABI.h:151
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes the instance from the mantissa value.
Definition: Decimal.h:83
A nullable real number with a constant exponent.
Definition: Decimal.h:130
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:146
MantissaType Mantissa
Mantissa component type.
Definition: Decimal.h:56