OnixS C++ CME MDP Premium Market Data Handler 5.10.2
Users' manual and API documentation
Loading...
Searching...
No Matches
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
31template <class MantissaType, class ExponentType> class FixedPointDecimal;
32template <class MantissaType, class ExponentType> class FloatingPointDecimal;
33template <class Mantissa, class Exponent> std::string toStr(const FixedPointDecimal<Mantissa, Exponent>&);
34template <class Mantissa, class Exponent> std::string toStr(const FloatingPointDecimal<Mantissa, Exponent>&);
35
37template <class MantissaType, class ExponentType>
39{
40 MantissaType mantissa_;
41 ExponentType exponent_;
42
43public:
46 struct MemberTraits
47 {
48 enum
49 {
50 Count = 2
51 };
52
53 typedef MantissaType FirstArgType;
54
55 typedef ExponentType SecondArgType;
56 };
57
59 typedef MantissaType Mantissa;
60
62 typedef ExponentType Exponent;
63
65 enum
66 {
68 Size = sizeof(Mantissa) + sizeof(Exponent)
69 };
70
73 std::string toString() const
74 {
75 return toStr(*this);
76 }
77
79 constexpr FloatingPointDecimal() noexcept
80 : mantissa_()
81 , exponent_()
82 {
83 }
84
87 : mantissa_(mantissa)
88 , exponent_(exponent)
89 {
90 }
91
93 constexpr FloatingPointDecimal(const FloatingPointDecimal& other) noexcept
94 : mantissa_(other.mantissa_)
95 , exponent_(other.exponent_)
96 {
97 }
98
100 Mantissa mantissa() const noexcept
101 {
102 return mantissa_;
103 }
104
106 Exponent exponent() const noexcept
107 {
108 return exponent_;
109 }
110
113 {
114 mantissa_ = other.mantissa_;
115 exponent_ = other.exponent_;
116
117 return *this;
118 }
119
121 template <class OtherMantissa, class OtherExponent>
123 : mantissa_(other.mantissa())
124 , exponent_(other.exponent())
125 {
126 }
127
129 template <class OtherMantissa, class OtherExponent>
131 : mantissa_(other.mantissa())
132 , exponent_(other.exponent())
133 {
134 }
135
137 template <class OtherMantissa, class OtherExponent>
139 {
140 mantissa_ = other.mantissa();
141 exponent_ = other.exponent();
142
143 return *this;
144 }
145
147 template <class OtherMantissa, class OtherExponent>
149 {
150 mantissa_ = other.mantissa();
151 exponent_ = other.exponent();
152
153 return *this;
154 }
155
157 void serialize(void* addr) const noexcept
158 {
159 assert(addr);
160
161 std::memcpy(addr, &mantissa_, sizeof(mantissa_));
162 addr = advanceByBytes(addr, sizeof(mantissa_));
163
164 std::memcpy(addr, &exponent_, sizeof(exponent_));
165 }
166};
167
169template <class MantissaType, class ExponentType>
171{
172 // Only mantissa is stored.
173 MantissaType mantissa_;
174
175public:
178 struct MemberTraits
179 {
180 enum
181 {
182 Count = 1
183 };
184
185 typedef MantissaType FirstArgType;
186 };
187
189 typedef MantissaType Mantissa;
190
192 typedef ExponentType Exponent;
193
195 enum
196 {
198 Size = sizeof(Mantissa)
199 };
200
203 std::string toString() const
204 {
205 return toStr(*this);
206 }
207
209 constexpr FixedPointDecimal() noexcept
210 : mantissa_()
211 {
212 }
213
215 explicit constexpr FixedPointDecimal(Mantissa mantissa) noexcept
216 : mantissa_(mantissa)
217 {
218 }
219
221 constexpr FixedPointDecimal(const FixedPointDecimal& other) noexcept
222 : mantissa_(other.mantissa_)
223 {
224 }
225
227 Mantissa mantissa() const noexcept
228 {
229 return mantissa_;
230 }
231
233 Exponent exponent() const noexcept
234 {
235 return Exponent();
236 }
237
240 {
241 mantissa_ = other.mantissa_;
242
243 return *this;
244 }
245
247 void serialize(void* addr) const noexcept
248 {
249 assert(addr);
250
251 std::memcpy(addr, &mantissa_, sizeof(mantissa_));
252 }
253};
254
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:57
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:58
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:150
ExponentType Exponent
Exponent component type.
Definition Decimal.h:192
constexpr FixedPointDecimal(const FixedPointDecimal &other) noexcept
Initializes the instance as a copy of the given one.
Definition Decimal.h:221
constexpr FixedPointDecimal(Mantissa mantissa) noexcept
Explicitly initializes the instance from the mantissa value.
Definition Decimal.h:215
FixedPointDecimal & operator=(const FixedPointDecimal &other) noexcept
Re-initializes the instance as a copy of the given one.
Definition Decimal.h:239
void serialize(void *addr) const noexcept
Serialize to the given data buffer.
Definition Decimal.h:247
MantissaType Mantissa
Mantissa component type.
Definition Decimal.h:189
constexpr FixedPointDecimal() noexcept
Default (zero) initialization.
Definition Decimal.h:209
ExponentType Exponent
Exponent component type.
Definition Decimal.h:62
FloatingPointDecimal & operator=(const FloatingPointDecimal &other) noexcept
Re-initializes the instance as copy of the given one.
Definition Decimal.h:112
constexpr FloatingPointDecimal() noexcept
Default (zero) initialization.
Definition Decimal.h:79
constexpr FloatingPointDecimal(Mantissa mantissa, Exponent exponent) noexcept
Explicitly initializes the instance from the mantissa and exponent values.
Definition Decimal.h:86
FloatingPointDecimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the fixed point decimal.
Definition Decimal.h:130
FloatingPointDecimal & operator=(const FloatingPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Re-initializes instance as a copy of the floating point value.
Definition Decimal.h:138
constexpr FloatingPointDecimal(const FloatingPointDecimal &other) noexcept
Initializes the instance as a copy of the given one.
Definition Decimal.h:93
void serialize(void *addr) const noexcept
Serializes to the given data buffer.
Definition Decimal.h:157
MantissaType Mantissa
Mantissa component type.
Definition Decimal.h:59
FloatingPointDecimal & operator=(const FixedPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Re-initializes instance as a copy of the fixed point value.
Definition Decimal.h:148
FloatingPointDecimal(const FloatingPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the floating point decimal.
Definition Decimal.h:122
void toStr(std::string &, Int8)
Serializes given integer into a string.