555 lines
9.7 KiB
C++
555 lines
9.7 KiB
C++
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is
|
|
// protected by copyright law and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable
|
|
// OnixS Software Services Agreement (the Agreement) and Customer end user license
|
|
// agreements granting a non-assignable, non-transferable and non-exclusive license
|
|
// to use the software for it's own data processing purposes under the terms defined
|
|
// in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or
|
|
// reproduction of any part of this source code or associated reference material
|
|
// to any other location for further reproduction or redistribution, and any
|
|
// amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in
|
|
// accordance with the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <OnixS/CME/MDH/Integral.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
|
|
/// Represents real number with constant exponent.
|
|
///
|
|
/// CME Market Data Platform currently operates prices
|
|
/// and scaling factors as real numbers with constant exponent.
|
|
/// This class implements concept of fixed point decimal and
|
|
/// lets to dramatically improve performance of book-related
|
|
/// operations.
|
|
template
|
|
<
|
|
class MantissaType,
|
|
class ExponentType
|
|
>
|
|
class FixedPointDecimal
|
|
{
|
|
// Only mantissa is stored.
|
|
MantissaType mantissa_;
|
|
|
|
public:
|
|
/// Aliases mantissa component type.
|
|
typedef MantissaType Mantissa;
|
|
|
|
/// Aliases exponent component type.
|
|
typedef ExponentType Exponent;
|
|
|
|
/// A few traits.
|
|
enum
|
|
{
|
|
/// Size of class in bytes.
|
|
///
|
|
/// Instantiations of given class are used
|
|
/// while manipulating binary messages. Regular
|
|
/// composites expose given characteristics so
|
|
/// as this template class does.
|
|
Size = sizeof(Mantissa)
|
|
};
|
|
|
|
/// Default (zero) initialization.
|
|
FixedPointDecimal()
|
|
: mantissa_()
|
|
{
|
|
}
|
|
|
|
/// Explicitly initializes instance from its mantissa value.
|
|
explicit
|
|
FixedPointDecimal(
|
|
Mantissa mantissa)
|
|
: mantissa_(mantissa)
|
|
{
|
|
}
|
|
|
|
/// Initializes instance as copy of the other one.
|
|
FixedPointDecimal(
|
|
const FixedPointDecimal& other)
|
|
: mantissa_(other.mantissa_)
|
|
{
|
|
}
|
|
|
|
/// Returns mantissa of given decimal.
|
|
Mantissa mantissa() const
|
|
{
|
|
return mantissa_;
|
|
}
|
|
|
|
/// Returns exponent of given decimal.
|
|
Exponent exponent() const
|
|
{
|
|
return Exponent();
|
|
}
|
|
|
|
/// Re-initializes instance as copy of the other one.
|
|
FixedPointDecimal&
|
|
operator =(
|
|
const FixedPointDecimal& other)
|
|
{
|
|
mantissa_ = other.mantissa_;
|
|
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator ==(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() == right.mantissa();
|
|
}
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator !=(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() != right.mantissa();
|
|
}
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator <(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() < right.mantissa();
|
|
}
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator >(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() > right.mantissa();
|
|
}
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator <=(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() <= right.mantissa();
|
|
}
|
|
|
|
/// Compares two fixed-point decimals.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
bool
|
|
operator >=(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return left.mantissa() >= right.mantissa();
|
|
}
|
|
|
|
//
|
|
|
|
/// A real number with floating exponent.
|
|
ONIXS_CMEMDH_LTWT_CLASS Decimal
|
|
{
|
|
public:
|
|
/// Aliases mantissa component type.
|
|
typedef Int64 Mantissa;
|
|
|
|
/// Aliases exponent component type.
|
|
typedef Int32 Exponent;
|
|
|
|
/// Default (zero) initialization.
|
|
Decimal()
|
|
: mantissa_(0)
|
|
, exponent_(0)
|
|
{
|
|
}
|
|
|
|
/// Explicitly initializes instance from its mantissa value.
|
|
Decimal(
|
|
Mantissa mantissa,
|
|
Exponent exponent)
|
|
: mantissa_(mantissa)
|
|
, exponent_(exponent)
|
|
{
|
|
}
|
|
|
|
/// Initializes instance as copy of the other one.
|
|
Decimal(
|
|
const Decimal& other)
|
|
: mantissa_(other.mantissa_)
|
|
, exponent_(other.exponent_)
|
|
{
|
|
}
|
|
|
|
/// Initializes instance from
|
|
/// the fixed point decimal.
|
|
template
|
|
<
|
|
class OtherMantissa,
|
|
class OtherExponent
|
|
>
|
|
Decimal(
|
|
const FixedPointDecimal
|
|
<OtherMantissa, OtherExponent>& other)
|
|
: mantissa_(other.mantissa())
|
|
, exponent_(other.exponent())
|
|
{
|
|
}
|
|
|
|
/// Returns mantissa of given decimal.
|
|
Mantissa mantissa() const
|
|
{
|
|
return mantissa_;
|
|
}
|
|
|
|
/// Returns exponent of given decimal.
|
|
Exponent exponent() const
|
|
{
|
|
return exponent_;
|
|
}
|
|
|
|
/// Re-initializes instance as copy of the other one.
|
|
Decimal&
|
|
operator =(
|
|
const Decimal& other)
|
|
{
|
|
mantissa_ = other.mantissa_;
|
|
exponent_ = other.exponent_;
|
|
|
|
return *this;
|
|
}
|
|
|
|
/// Re-initializes instance as a
|
|
/// copy of the fixed point value.
|
|
template
|
|
<
|
|
class OtherMantissa,
|
|
class OtherExponent
|
|
>
|
|
Decimal&
|
|
operator =(
|
|
const FixedPointDecimal
|
|
<OtherMantissa, OtherExponent>& other)
|
|
{
|
|
mantissa_ = other.mantissa();
|
|
exponent_ = other.exponent();
|
|
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
Mantissa mantissa_;
|
|
Exponent exponent_;
|
|
};
|
|
|
|
//
|
|
|
|
ONIXS_CMEMDH_EXPORTED
|
|
bool
|
|
operator ==(
|
|
const Decimal& left,
|
|
const Decimal& right);
|
|
|
|
ONIXS_CMEMDH_EXPORTED
|
|
bool
|
|
operator !=(
|
|
const Decimal& left,
|
|
const Decimal& right);
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator ==(
|
|
const Decimal& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return (left == Decimal(right));
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator ==(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const Decimal& right)
|
|
{
|
|
return (Decimal(left) == right);
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator !=(
|
|
const Decimal& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return (left != Decimal(right));
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator !=(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const Decimal& right)
|
|
{
|
|
return (Decimal(left) != right);
|
|
}
|
|
|
|
ONIXS_CMEMDH_EXPORTED
|
|
bool
|
|
operator <(
|
|
const Decimal& left,
|
|
const Decimal& right);
|
|
|
|
ONIXS_CMEMDH_EXPORTED
|
|
bool
|
|
operator <=(
|
|
const Decimal& left,
|
|
const Decimal& right);
|
|
|
|
inline
|
|
bool
|
|
operator >(
|
|
const Decimal& left,
|
|
const Decimal& right)
|
|
{
|
|
return (right < left);
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator <(
|
|
const Decimal& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return (left < Decimal(right));
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator <(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const Decimal& right)
|
|
{
|
|
return (Decimal(left) < right);
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator >(
|
|
const Decimal& left,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& right)
|
|
{
|
|
return (left > Decimal(right));
|
|
}
|
|
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
bool
|
|
operator >(
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& left,
|
|
const Decimal& right)
|
|
{
|
|
return (Decimal(left) > right);
|
|
}
|
|
|
|
/// Serializes decimal presented by
|
|
/// mantissa and exponent into a string.
|
|
ONIXS_CMEMDH_EXPORTED
|
|
void
|
|
decimalToStr(
|
|
std::string&,
|
|
Int64,
|
|
Int32);
|
|
|
|
inline
|
|
void
|
|
toStr(
|
|
std::string& str,
|
|
const Decimal& number)
|
|
{
|
|
decimalToStr
|
|
(
|
|
str,
|
|
number.mantissa(),
|
|
number.exponent()
|
|
);
|
|
}
|
|
|
|
inline
|
|
std::string
|
|
toStr(
|
|
const Decimal& number)
|
|
{
|
|
std::string str;
|
|
|
|
toStr(str, number);
|
|
|
|
return str;
|
|
}
|
|
|
|
/// Serializes fixed-point decimal into a string.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
void
|
|
toStr(
|
|
std::string& str,
|
|
const
|
|
FixedPointDecimal
|
|
<Mantissa, Exponent>& number)
|
|
{
|
|
toStr(
|
|
str,
|
|
Decimal(number));
|
|
}
|
|
|
|
/// Serializes fixed-point decimal into a string.
|
|
template
|
|
<
|
|
class Mantissa,
|
|
class Exponent
|
|
>
|
|
inline
|
|
std::string
|
|
toStr(
|
|
const
|
|
FixedPointDecimal
|
|
<
|
|
Mantissa,
|
|
Exponent
|
|
>& number)
|
|
{
|
|
std::string str;
|
|
|
|
toStr(str, number);
|
|
|
|
return str;
|
|
}
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_END
|