OnixS C++ CME iLink 3 Binary Order Entry Handler 1.18.9
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 { Count = 2 };
49
50 typedef MantissaType FirstArgType;
51
52 typedef ExponentType SecondArgType;
53 };
54
56 typedef MantissaType Mantissa;
57
59 typedef ExponentType Exponent;
60
62 enum
63 {
65 Size = sizeof(Mantissa) + sizeof(Exponent)
66 };
67
70 std::string toString() const
71 {
72 return toStr(*this);
73 }
74
78 : mantissa_()
79 , exponent_()
80 {
81 }
82
90
95 : mantissa_(other.mantissa_)
96 , exponent_(other.exponent_)
97 {
98 }
99
102 {
103 return mantissa_;
104 }
105
108 {
109 return exponent_;
110 }
111
114 {
115 mantissa_ = other.mantissa_;
116 exponent_ = other.exponent_;
117
118 return *this;
119 }
120
121
123 template<class OtherMantissa, class OtherExponent>
124 explicit FloatingPointDecimal(const FloatingPointDecimal <OtherMantissa, OtherExponent>& other) ONIXS_ILINK3_NOTHROW
125 : mantissa_(other.mantissa())
126 , exponent_(other.exponent())
127 {
128 }
129
131 template <class OtherMantissa, class OtherExponent>
132 explicit FloatingPointDecimal(const FixedPointDecimal <OtherMantissa, OtherExponent>& other) ONIXS_ILINK3_NOTHROW
133 : mantissa_(other.mantissa())
134 , exponent_(other.exponent())
135 {
136 }
137
139 template
140 <
141 class OtherMantissa,
142 class OtherExponent
143 >
145 operator =(
147 <OtherMantissa, OtherExponent>& other) ONIXS_ILINK3_NOTHROW
148 {
149 mantissa_ = other.mantissa();
150 exponent_ = other.exponent();
151
152 return *this;
153 }
154
156 template
157 <
158 class OtherMantissa,
159 class OtherExponent
160 >
161 FloatingPointDecimal& operator = (const FixedPointDecimal <OtherMantissa, OtherExponent>& other) ONIXS_ILINK3_NOTHROW
162 {
163 mantissa_ = other.mantissa();
164 exponent_ = other.exponent();
165
166 return *this;
167 }
168
170 void serialize(void* addr) const ONIXS_ILINK3_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
182template
183<
184 class MantissaType,
185 class ExponentType
186>
188{
189 // Only mantissa is stored.
190 MantissaType mantissa_;
191
192public:
195 struct MemberTraits
196 {
197 enum { Count = 1 };
198
199 typedef MantissaType FirstArgType;
200 };
201
203 typedef MantissaType Mantissa;
204
206 typedef ExponentType Exponent;
207
209 enum
210 {
212 Size = sizeof(Mantissa)
213 };
214
217 std::string toString() const
218 {
219 return toStr(*this);
220 }
221
225 : mantissa_()
226 {
227 }
228
234
237 : mantissa_(other.mantissa_)
238 {
239 }
240
243 {
244 return mantissa_;
245 }
246
249 {
250 return Exponent();
251 }
252
255 {
256 mantissa_ = other.mantissa_;
257
258 return *this;
259 }
260
262 void serialize(void* addr) const ONIXS_ILINK3_NOTHROW
263 {
264 assert(addr);
265
266 std::memcpy(addr, &mantissa_, sizeof(mantissa_));
267 }
268};
269
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_ILINK3_NODISCARD
Definition Compiler.h:185
#define ONIXS_ILINK3_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_ILINK3_NOTHROW
Definition Compiler.h:176
constexpr FixedPointDecimal(const FixedPointDecimal &other) noexcept
Initializes the instance as a copy of the given one.
Definition Decimal.h:236
constexpr FixedPointDecimal(Mantissa mantissa) noexcept
Explicitly initializes the instance from the mantissa value.
Definition Decimal.h:230
void serialize(void *addr) const noexcept
Serialize to the given data buffer.
Definition Decimal.h:262
constexpr FixedPointDecimal() noexcept
Default (zero) initialization.
Definition Decimal.h:224
A real number with a floating exponent.
Definition Decimal.h:39
constexpr FloatingPointDecimal() noexcept
Default (zero) initialization.
Definition Decimal.h:77
constexpr FloatingPointDecimal(Mantissa mantissa, Exponent exponent) noexcept
Explicitly initializes the instance from the mantissa and exponent values.
Definition Decimal.h:85
FloatingPointDecimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the fixed point decimal.
Definition Decimal.h:132
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:170
FloatingPointDecimal(const FloatingPointDecimal< OtherMantissa, OtherExponent > &other) noexcept
Initializes the instance from the floating point decimal.
Definition Decimal.h:124
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.