/* * 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 #include #include #include namespace OnixS { namespace FIX { typedef char Char; typedef short int Int16; typedef short unsigned int UInt16; typedef int Int32; typedef unsigned int UInt32; typedef long long Int64; typedef unsigned long long UInt64; typedef double Double; typedef Int64 DecimalMantissa; typedef Int32 DecimalExponent; /// The Decimal type for a better precision. class ONIXS_FIXENGINE_API Decimal { public: /// Initializes an instance from compound components. Decimal( DecimalMantissa mantissa = 0, DecimalExponent exponent = 0); /// Converts the Double value to a decimal. /// @param value The double precision floating-point value. /// @param precision Defines the conversion precision. explicit Decimal(Double value, size_t precision = 17); /// Returns the mantissa part of the decimal. DecimalMantissa mantissa() const; /// Updates the mantissa part of the decimal. void mantissa(DecimalMantissa); /// Returns the exponent part of the decimal. DecimalExponent exponent() const; /// Updates the exponent part of the decimal. void exponent(DecimalExponent); /// Compares two numbers. bool operator == (const Decimal &) const; /// Compares two numbers. bool operator != (const Decimal &) const; /// Compares two numbers. bool operator < (const Decimal &) const; /// Compares two numbers. bool operator > (const Decimal &) const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @throw The domain_error exception on a failure. operator Int32() const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @throw The domain_error exception on a failure. operator UInt32() const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @throw The domain_error exception on a failure. operator Int64() const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @throw The domain_error exception on a failure. operator UInt64() const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @throw The domain_error exception on a failure. operator Double() const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @return false if the conversion fails. bool toNumber(Int32 &) const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @return false if the conversion fails. bool toNumber(UInt32 &) const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @return false if the conversion fails. bool toNumber(Int64 &) const; /// Casts to the whole integer number as a regular /// floating point value is casted. /// @return false if the conversion fails. bool toNumber(UInt64 &) const; /// Casts to the floating point number. /// @return false if the conversion fails. bool toNumber(Double &) const; /// Appends the text presentation to the given string. void toString(std::string &) const; /// Returns the text presentation of the decimal. std::string toString() const; /// Attempts to parse the decimal value /// from its string/text presentation. /// @return false on the parsing failure. static bool tryParse( const char * buffer, size_t bufferSize, Decimal &); /// Parses the decimal from the string presentation. /// @throw The std::exception on a failure. static Decimal parse( const char * buffer, size_t bufferSize); private: /// The mantissa. DecimalMantissa mantissa_; /// The exponent. DecimalExponent exponent_; }; ONIXS_FIXENGINE_API std::ostream & operator<<(std::ostream &, Decimal const &); inline Decimal::Decimal( DecimalMantissa mantissa, DecimalExponent exponent) : mantissa_(mantissa), exponent_(exponent) { } inline DecimalMantissa Decimal::mantissa() const { return mantissa_; } inline void Decimal::mantissa( DecimalMantissa value) { mantissa_ = value; } inline DecimalExponent Decimal::exponent() const { return exponent_; } inline void Decimal::exponent( DecimalExponent value) { exponent_ = value; } inline Decimal::operator Int32() const { Int32 number; if(toNumber(number)) return number; throw std::domain_error( "Cannot cast value to target type. "); } inline Decimal::operator UInt32() const { UInt32 number; if(toNumber(number)) return number; throw std::domain_error( "Cannot cast value to target type. "); } inline Decimal::operator Int64() const { Int64 number; if(toNumber(number)) return number; throw std::domain_error( "Cannot cast value to target type. "); } inline Decimal::operator UInt64() const { UInt64 number; if(toNumber(number)) return number; throw std::domain_error( "Cannot cast value to target type. "); } inline Decimal::operator Double() const { Double number; if(toNumber(number)) return number; throw std::domain_error( "Cannot cast value to target type. "); } inline std::string Decimal::toString() const { std::string presentation; toString(presentation); return presentation; } /// The helper class for the conversion from a string to a number. struct ONIXS_FIXENGINE_API Number { static bool tryParse( const char * buffer, size_t bufferSize, Int32 & number); static bool tryParse( const char * buffer, size_t bufferSize, UInt32 & number); static bool tryParse( const char * buffer, size_t bufferSize, Int64 & number); static bool tryParse( const char * buffer, size_t bufferSize, UInt64 & number); static bool tryParse( const char * buffer, size_t bufferSize, Double & number); static bool tryParse( const char * buffer, size_t bufferSize, Decimal & number); }; } }