You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
FixedPointsArduino/src/FixedPoints/UFixedBase.h

64 lines
2.3 KiB

#pragma once
#include "Details.h"
FIXED_POINTS_BEGIN_NAMESPACE
namespace FIXED_POINTS_DETAILS
{
template< unsigned Integer, unsigned Fraction >
class UFixedBase
{
public:
using IntegerType = FIXED_POINTS_DETAILS::LeastInt<Integer + 1>;
using FractionType = FIXED_POINTS_DETAILS::LeastUInt<Fraction>;
using InternalType = FIXED_POINTS_DETAILS::LeastUInt<Integer + Fraction>;
constexpr const static unsigned long long Scale = 1ULL << Fraction;
protected:
class RawType
{
private:
const InternalType value;
public:
constexpr inline explicit RawType(const InternalType & value) : value(value) {}
constexpr inline explicit operator InternalType(void) const { return this->value; }
};
protected:
InternalType value;
constexpr UFixedBase(void) : value(0) {}
constexpr UFixedBase(const RawType & value) : value(static_cast<InternalType>(value)) {}
public:
constexpr UFixedBase(const IntegerLiteral & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteral, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const IntegerLiteralU & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteralU, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const IntegerLiteralL & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteralL, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const IntegerLiteralUL & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteralUL, InternalType>>(value) << Fraction)) {}
constexpr UFixedBase(const IntegerLiteralLL & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteralLL, InternalType>>(value) << Fraction)) {}
constexpr UFixedBase(const IntegerLiteralULL & value)
: value(static_cast<InternalType>(static_cast< LargerType<IntegerLiteralULL, InternalType> >(value) << Fraction)) {}
constexpr UFixedBase(const DecimalLiteral & value)
: value(static_cast<InternalType>(value * Scale)) {}
constexpr UFixedBase(const DecimalLiteralF & value)
: value(static_cast<InternalType>(value * Scale)) {}
constexpr UFixedBase(const DecimalLiteralL & value)
: value(static_cast<InternalType>(value * Scale)) {}
};
}
FIXED_POINTS_END_NAMESPACE