OnixS C++ CME MDP Streamlined Market Data Handler 1.2.0
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
23#include <string>
24
26
28
36template
37<
38 class MantissaType,
39 class ExponentType
40>
42{
43 // Only mantissa is stored.
44 MantissaType mantissa_;
45
46public:
48 typedef MantissaType Mantissa;
49
51 typedef ExponentType Exponent;
52
54 enum
55 {
62 Size = sizeof(Mantissa)
63 };
64
67 : mantissa_()
68 {
69 }
70
72 explicit
75 : mantissa_(mantissa)
76 {
77 }
78
81 const FixedPointDecimal& other)
82 : mantissa_(other.mantissa_)
83 {
84 }
85
88 {
89 return mantissa_;
90 }
91
94 {
95 return Exponent();
96 }
97
100 operator =(
101 const FixedPointDecimal& other)
102 {
103 mantissa_ = other.mantissa_;
104
105 return *this;
106 }
107};
108
110template
111<
112 class Mantissa,
113 class Exponent
114>
115bool
116operator ==(
117 const
119 <Mantissa, Exponent>& left,
120 const
122 <Mantissa, Exponent>& right)
123{
124 return left.mantissa() == right.mantissa();
125}
126
128template
129<
130 class Mantissa,
131 class Exponent
132>
133bool
134operator !=(
135 const
137 <Mantissa, Exponent>& left,
138 const
140 <Mantissa, Exponent>& right)
141{
142 return left.mantissa() != right.mantissa();
143}
144
146template
147<
148 class Mantissa,
149 class Exponent
150>
151bool
152operator <(
153 const
155 <Mantissa, Exponent>& left,
156 const
158 <Mantissa, Exponent>& right)
159{
160 return left.mantissa() < right.mantissa();
161}
162
164template
165<
166 class Mantissa,
167 class Exponent
168>
169bool
170operator >(
171 const
173 <Mantissa, Exponent>& left,
174 const
176 <Mantissa, Exponent>& right)
177{
178 return left.mantissa() > right.mantissa();
179}
180
182template
183<
184 class Mantissa,
185 class Exponent
186>
187bool
188operator <=(
189 const
191 <Mantissa, Exponent>& left,
192 const
194 <Mantissa, Exponent>& right)
195{
196 return left.mantissa() <= right.mantissa();
197}
198
200template
201<
202 class Mantissa,
203 class Exponent
204>
205bool
206operator >=(
207 const
209 <Mantissa, Exponent>& left,
210 const
212 <Mantissa, Exponent>& right)
213{
214 return left.mantissa() >= right.mantissa();
215}
216
218typedef Int64 DecimalMantissa;
219
222
225{
226 DecimalMantissa mantissa_;
227 DecimalExponent exponent_;
228
229public:
231 typedef
234
236 typedef
239
242 : mantissa_(0)
243 , exponent_(0)
244 {
245 }
246
251 : mantissa_(mantissa)
252 , exponent_(exponent)
253 {
254 }
255
258 const Decimal& other)
259 : mantissa_(other.mantissa_)
260 , exponent_(other.exponent_)
261 {
262 }
263
266 template
267 <
268 class OtherMantissa,
269 class OtherExponent
270 >
273 <OtherMantissa, OtherExponent>& other)
274 : mantissa_(other.mantissa())
275 , exponent_(other.exponent())
276 {
277 }
278
281 {
282 return mantissa_;
283 }
284
287 {
288 return exponent_;
289 }
290
292 Decimal&
293 operator =(
294 const Decimal& other)
295 {
296 mantissa_ = other.mantissa_;
297 exponent_ = other.exponent_;
298
299 return *this;
300 }
301
304 template
305 <
306 class OtherMantissa,
307 class OtherExponent
308 >
309 Decimal&
310 operator =(
312 <OtherMantissa, OtherExponent>& other)
313 {
314 mantissa_ = other.mantissa();
315 exponent_ = other.exponent();
316
317 return *this;
318 }
319};
320
321//
322
324bool
325operator ==(
326 const Decimal& left,
327 const Decimal& right);
328
330bool
331operator !=(
332 const Decimal& left,
333 const Decimal& right);
334
335template
336<
337 class Mantissa,
338 class Exponent
339>
340inline
341bool
342operator ==(
343 const Decimal& left,
344 const
346 <Mantissa, Exponent>& right)
347{
348 return (left == Decimal(right));
349}
350
351template
352<
353 class Mantissa,
354 class Exponent
355>
356inline
357bool
358operator ==(
359 const
361 <Mantissa, Exponent>& left,
362 const Decimal& right)
363{
364 return (Decimal(left) == right);
365}
366
367template
368<
369 class Mantissa,
370 class Exponent
371>
372inline
373bool
374operator !=(
375 const Decimal& left,
376 const
378 <Mantissa, Exponent>& right)
379{
380 return (left != Decimal(right));
381}
382
383template
384<
385 class Mantissa,
386 class Exponent
387>
388inline
389bool
390operator !=(
391 const
393 <Mantissa, Exponent>& left,
394 const Decimal& right)
395{
396 return (Decimal(left) != right);
397}
398
400bool
401operator <(
402 const Decimal& left,
403 const Decimal& right);
404
406bool
407operator <=(
408 const Decimal& left,
409 const Decimal& right);
410
411inline
412bool
413operator >(
414 const Decimal& left,
415 const Decimal& right)
416{
417 return (right < left);
418}
419
420template
421<
422 class Mantissa,
423 class Exponent
424>
425inline
426bool
427operator <(
428 const Decimal& left,
429 const
431 <Mantissa, Exponent>& right)
432{
433 return (left < Decimal(right));
434}
435
436template
437<
438 class Mantissa,
439 class Exponent
440>
441inline
442bool
443operator <(
444 const
446 <Mantissa, Exponent>& left,
447 const Decimal& right)
448{
449 return (Decimal(left) < right);
450}
451
452template
453<
454 class Mantissa,
455 class Exponent
456>
457inline
458bool
459operator >(
460 const Decimal& left,
461 const
463 <Mantissa, Exponent>& right)
464{
465 return (left > Decimal(right));
466}
467
468template
469<
470 class Mantissa,
471 class Exponent
472>
473inline
474bool
475operator >(
476 const
478 <Mantissa, Exponent>& left,
479 const Decimal& right)
480{
481 return (Decimal(left) > right);
482}
483
485bool
487 Decimal&,
488 const char*,
489 size_t);
490
494void
496 std::string&,
497 Int64,
498 Int32);
499
500inline
501void
503 std::string& str,
504 const Decimal& number)
505{
507 (
508 str,
509 number.mantissa(),
510 number.exponent()
511 );
512}
513
514inline
515std::string
517 const Decimal& number)
518{
519 std::string str;
520
521 toStr(str, number);
522
523 return str;
524}
525
527template
528<
529 class Mantissa,
530 class Exponent
531>
532inline
533void
535 std::string& str,
536 const
538 <Mantissa, Exponent>& number)
539{
540 toStr(
541 str,
542 Decimal(number));
543}
544
546template
547<
548 class Mantissa,
549 class Exponent
550>
551inline
552std::string
554 const
556 <
557 Mantissa,
558 Exponent
559 >& number)
560{
561 std::string str;
562
563 toStr(str, number);
564
565 return str;
566}
567
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:169
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition Bootstrap.h:173
#define ONIXS_CMESTREAMLINEDMDH_LTWT_CLASS
Definition Bootstrap.h:111
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED
Definition Compiler.h:160
A real number with floating exponent.
Definition Decimal.h:225
DecimalExponent Exponent
Aliases exponent component type.
Definition Decimal.h:238
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition Decimal.h:280
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:248
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition Decimal.h:257
Decimal(const FixedPointDecimal< OtherMantissa, OtherExponent > &other)
Initializes instance from the fixed point decimal.
Definition Decimal.h:271
Decimal()
Default (zero) initialization.
Definition Decimal.h:241
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition Decimal.h:233
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:286
Represents real number with constant exponent.
Definition Decimal.h:42
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:73
FixedPointDecimal()
Default (zero) initialization.
Definition Decimal.h:66
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:93
FixedPointDecimal(const FixedPointDecimal &other)
Initializes instance as copy of the other one.
Definition Decimal.h:80
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition Decimal.h:221
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition Decimal.h:218
void decimalToStr(std::string &, Int64, Int32)
Serializes decimal presented by mantissa and exponent into a string.
void toStr(std::string &str, const Decimal &number)
Definition Decimal.h:502
bool fromStr(Decimal &, const char *, size_t)