From 5eadcbadf32706723556ed3805ba1b701a3d4124 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Wed, 7 Apr 2021 04:21:14 +0100 Subject: [PATCH] Add integer conversion operators (#94) Adds operators for converting to all the main integer types. --- src/FixedPoints/SFixed.h | 13 ++++- src/FixedPoints/SFixedMemberFunctions.h | 64 ++++++++++++++++++++++++- src/FixedPoints/UFixed.h | 13 ++++- src/FixedPoints/UFixedMemberFunctions.h | 64 ++++++++++++++++++++++++- 4 files changed, 150 insertions(+), 4 deletions(-) diff --git a/src/FixedPoints/SFixed.h b/src/FixedPoints/SFixed.h index 4d93cf7..93e8154 100644 --- a/src/FixedPoints/SFixed.h +++ b/src/FixedPoints/SFixed.h @@ -98,7 +98,18 @@ public: constexpr IntegerType getInteger() const; constexpr FractionType getFraction() const; - constexpr explicit operator IntegerType() const; + constexpr explicit operator char() const; + constexpr explicit operator unsigned char() const; + constexpr explicit operator signed char() const; + constexpr explicit operator unsigned short int() const; + constexpr explicit operator signed short int() const; + constexpr explicit operator unsigned int() const; + constexpr explicit operator signed int() const; + constexpr explicit operator unsigned long int() const; + constexpr explicit operator signed long int() const; + constexpr explicit operator unsigned long long int() const; + constexpr explicit operator signed long long int() const; + constexpr explicit operator float() const; constexpr explicit operator double() const; constexpr explicit operator long double() const; diff --git a/src/FixedPoints/SFixedMemberFunctions.h b/src/FixedPoints/SFixedMemberFunctions.h index 69f35e5..97c57d7 100644 --- a/src/FixedPoints/SFixedMemberFunctions.h +++ b/src/FixedPoints/SFixedMemberFunctions.h @@ -147,11 +147,73 @@ constexpr typename SFixed::FractionType SFixed -constexpr SFixed::operator IntegerType() const +constexpr SFixed::operator char() const { return this->getInteger(); } +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator unsigned char() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator signed char() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator unsigned short int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator signed short int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator unsigned int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator signed int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator unsigned long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator signed long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator unsigned long long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr SFixed::operator signed long long int() const +{ + return this->getInteger(); +} + + + template< unsigned Integer, unsigned Fraction > constexpr SFixed::operator float() const { diff --git a/src/FixedPoints/UFixed.h b/src/FixedPoints/UFixed.h index 4444f55..0ded491 100644 --- a/src/FixedPoints/UFixed.h +++ b/src/FixedPoints/UFixed.h @@ -99,7 +99,18 @@ public: constexpr IntegerType getInteger() const; constexpr FractionType getFraction() const; - constexpr explicit operator IntegerType() const; + constexpr explicit operator char() const; + constexpr explicit operator unsigned char() const; + constexpr explicit operator signed char() const; + constexpr explicit operator unsigned short int() const; + constexpr explicit operator signed short int() const; + constexpr explicit operator unsigned int() const; + constexpr explicit operator signed int() const; + constexpr explicit operator unsigned long int() const; + constexpr explicit operator signed long int() const; + constexpr explicit operator unsigned long long int() const; + constexpr explicit operator signed long long int() const; + constexpr explicit operator float() const; constexpr explicit operator double() const; constexpr explicit operator long double() const; diff --git a/src/FixedPoints/UFixedMemberFunctions.h b/src/FixedPoints/UFixedMemberFunctions.h index 00def81..56b82db 100644 --- a/src/FixedPoints/UFixedMemberFunctions.h +++ b/src/FixedPoints/UFixedMemberFunctions.h @@ -147,11 +147,73 @@ constexpr typename UFixed::FractionType UFixed -constexpr UFixed::operator IntegerType() const +constexpr UFixed::operator char() const { return this->getInteger(); } +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator unsigned char() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator signed char() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator unsigned short int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator signed short int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator unsigned int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator signed int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator unsigned long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator signed long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator unsigned long long int() const +{ + return this->getInteger(); +} + +template< unsigned Integer, unsigned Fraction > +constexpr UFixed::operator signed long long int() const +{ + return this->getInteger(); +} + + + template< unsigned Integer, unsigned Fraction > constexpr UFixed::operator float() const {