From a368eda73af9413fc2ef53c31aab2581320013ec Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 15 Sep 2016 21:00:41 -0400 Subject: [PATCH] Hide implicit conversions sampleCount <-> floating point, -> integer --- include/audacity/Types.h | 266 +++++++++------------------------------ 1 file changed, 60 insertions(+), 206 deletions(-) diff --git a/include/audacity/Types.h b/include/audacity/Types.h index 67f6b4d3a..1614cc7dd 100644 --- a/include/audacity/Types.h +++ b/include/audacity/Types.h @@ -63,12 +63,24 @@ public: static_assert(sizeof(type) == 8, "Wrong width of sampleCount"); sampleCount () : value { 0 } {} + + // Allow implicit conversion from integral types sampleCount ( type v ) : value { v } {} + sampleCount ( unsigned long long v ) : value ( v ) {} + sampleCount ( int v ) : value { v } {} + sampleCount ( unsigned v ) : value { v } {} + sampleCount ( long v ) : value { v } {} + sampleCount ( unsigned long v ) : value { v } {} + + // Beware implicit conversions from floating point values! + // Otherwise the meaning of binary operators with sampleCount change + // their meaning when sampleCount is not an alias! + explicit sampleCount ( float f ) : value ( f ) {} + explicit sampleCount ( double d ) : value ( d ) {} + sampleCount ( const sampleCount& ) = default; sampleCount &operator= ( const sampleCount& ) = default; - operator type () const { return value; } - float as_float() const { return value; } double as_double() const { return value; } @@ -86,6 +98,8 @@ public: sampleCount &operator /= (sampleCount b) { value /= b.value; return *this; } sampleCount &operator %= (sampleCount b) { value %= b.value; return *this; } + sampleCount operator - () const { return -value; } + sampleCount &operator ++ () { ++value; return *this; } sampleCount operator ++ (int) { sampleCount result{ *this }; ++value; return result; } @@ -98,220 +112,60 @@ private: type value; }; -namespace std +inline bool operator == (sampleCount a, sampleCount b) { - inline sampleCount min (sampleCount a, sampleCount b) - { - return a < b ? a : b; - } - - inline sampleCount max (sampleCount a, sampleCount b) - { - return a > b ? a : b; - } + return a.as_long_long() == b.as_long_long(); } -inline -sampleCount operator + (sampleCount a, sampleCount b) -{ return (long long)a + (long long)b; } +inline bool operator != (sampleCount a, sampleCount b) +{ + return !(a == b); +} -inline -sampleCount operator + (sampleCount a, size_t b) -{ return (long long)a + b; } +inline bool operator < (sampleCount a, sampleCount b) +{ + return a.as_long_long() < b.as_long_long(); +} -inline -sampleCount operator + (size_t a, sampleCount b) -{ return a + (long long)b; } +inline bool operator >= (sampleCount a, sampleCount b) +{ + return !(a < b); +} -inline -sampleCount operator + (sampleCount a, int b) -{ return (long long)a + b; } +inline bool operator > (sampleCount a, sampleCount b) +{ + return b < a; +} -inline -sampleCount operator + (int a, sampleCount b) -{ return a + (long long)b; } +inline bool operator <= (sampleCount a, sampleCount b) +{ + return !(b < a); +} -inline -sampleCount operator + (sampleCount a, long b) -{ return (long long)a + b; } +inline sampleCount operator + (sampleCount a, sampleCount b) +{ + return sampleCount{ a } += b; +} -inline -sampleCount operator + (long a, sampleCount b) -{ return a + (long long)b; } +inline sampleCount operator - (sampleCount a, sampleCount b) +{ + return sampleCount{ a } -= b; +} -inline -sampleCount operator + (sampleCount a, long long b) -{ return (long long)a + b; } +inline sampleCount operator * (sampleCount a, sampleCount b) +{ + return sampleCount{ a } *= b; +} -inline -sampleCount operator + (long long a, sampleCount b) -{ return a + (long long)b; } +inline sampleCount operator / (sampleCount a, sampleCount b) +{ + return sampleCount{ a } /= b; +} -inline -sampleCount operator + (sampleCount a, unsigned b) -{ return (long long)a + b; } - -inline -sampleCount operator + (unsigned a, sampleCount b) -{ return a + (long long)b; } - - - - -inline -sampleCount operator - (sampleCount a, sampleCount b) -{ return (long long)a - (long long)b; } - -inline -sampleCount operator - (sampleCount a, size_t b) -{ return (long long)a - b; } - -inline -sampleCount operator - (size_t a, sampleCount b) -{ return a - (long long)b; } - -inline -sampleCount operator - (sampleCount a, int b) -{ return(long long) a - b; } - -inline -sampleCount operator - (int a, sampleCount b) -{ return a - (long long)b; } - -inline -sampleCount operator - (sampleCount a, long b) -{ return (long long)a - b; } - -inline -sampleCount operator - (long a, sampleCount b) -{ return a - (long long)b; } - -inline -sampleCount operator - (sampleCount a, long long b) -{ return (long long)a - b; } - -inline -sampleCount operator - (long long a, sampleCount b) -{ return a - (long long)b; } - -inline -sampleCount operator - (sampleCount a, unsigned b) -{ return (long long)a - b; } - -inline -sampleCount operator - (unsigned a, sampleCount b) -{ return a - (long long)b; } - - - -inline -sampleCount operator * (sampleCount a, sampleCount b) -{ return (long long)a * (long long)b; } - -inline -sampleCount operator * (sampleCount a, int b) -{ return (long long)a * b; } - -inline -sampleCount operator * (int a, sampleCount b) -{ return a * (long long)b; } - -inline -sampleCount operator * (sampleCount a, unsigned b) -{ return (long long)a * b; } - -inline -sampleCount operator * (unsigned a, sampleCount b) -{ return a * (long long)b; } - -inline -sampleCount operator * (sampleCount a, long b) -{ return (long long)a * b; } - -inline -sampleCount operator * (long a, sampleCount b) -{ return a * (long long)b; } - -inline -sampleCount operator * (sampleCount a, unsigned long b) -{ return (long long)a * b; } - -inline -sampleCount operator * (unsigned long a, sampleCount b) -{ return a * (long long)b; } - -inline -sampleCount operator * (sampleCount a, long long b) -{ return (long long)a * b; } - -inline -sampleCount operator * (long long a, sampleCount b) -{ return a * (long long)b; } - - - -inline -sampleCount operator / (sampleCount a, sampleCount b) -{ return (long long)a / (long long)b; } - -inline -sampleCount operator / (sampleCount a, int b) -{ return (long long)a / b; } - -inline -sampleCount operator / (sampleCount a, size_t b) -{ return (long long)a / b; } - -inline -double operator / (sampleCount a, double b) -{ return (long long)a / b; } - -inline -float operator / (float a, sampleCount b) -{ return a / (long long)b; } - -inline -double operator / (double a, sampleCount b) -{ return a / (long long)b; } - - - -inline -double operator + (sampleCount a, double b) -{ return (long long)a + b; } - -inline -double operator + (double a, sampleCount b) -{ return a + (long long)b; } - -inline -double operator - (sampleCount a, double b) -{ return (long long)a - b; } - -inline -double operator - (double a, sampleCount b) -{ return a - (long long)b; } - -inline -double operator * (sampleCount a, double b) -{ return (long long)a * b; } - -inline -double operator * (double a, sampleCount b) -{ return a * (long long)b; } - - -inline -float operator - (float a, sampleCount b) -{ return a - (long long)b; } - -inline -float operator * (float a, sampleCount b) -{ return a * (long long)b; } - -inline -float operator * (sampleCount a, float b) -{ return (long long)a * b; } +inline sampleCount operator % (sampleCount a, sampleCount b) +{ + return sampleCount{ a } %= b; +} // ---------------------------------------------------------------------------- // Function returning the minimum of a sampleCount and a size_t, @@ -320,9 +174,9 @@ float operator * (sampleCount a, float b) inline size_t limitSampleBufferSize( size_t bufferSize, sampleCount limit ) { - return static_cast ( + return std::min( sampleCount( bufferSize ), std::max( sampleCount(0), limit ) ) - ); + .as_size_t(); } // ----------------------------------------------------------------------------