Tue, 07 Aug 2018 20:34:32 +0300
added constexprs
src/generics/enums.h | file | annotate | diff | comparison | revisions | |
src/generics/functions.h | file | annotate | diff | comparison | revisions |
--- a/src/generics/enums.h Sat Aug 04 21:46:58 2018 +0300 +++ b/src/generics/enums.h Tue Aug 07 20:34:32 2018 +0300 @@ -62,7 +62,7 @@ * Casts an enum into its underlying type. */ template<typename T> -typename std::underlying_type<T>::type& enum_cast(T& enu) +constexpr typename std::underlying_type<T>::type& enum_cast(T& enu) { return *reinterpret_cast<typename std::underlying_type<T>::type*>(&enu); } @@ -71,7 +71,7 @@ * Returns whether an enum value is within proper bounds. */ template<typename Enum> -bool valueInEnum(Enum enumerator) +constexpr bool valueInEnum(Enum enumerator) { auto index = enum_cast(enumerator); return index >= EnumLimits<Enum>::First and index <= EnumLimits<Enum>::Last;
--- a/src/generics/functions.h Sat Aug 04 21:46:58 2018 +0300 +++ b/src/generics/functions.h Tue Aug 07 20:34:32 2018 +0300 @@ -25,7 +25,7 @@ * Returns whether the argument is reasonably close to zero. */ template<typename T> -bool isZero(T a) +constexpr bool isZero(T a) { return qFuzzyCompare(a + 1.0, 1.0); } @@ -43,7 +43,7 @@ } template<> -inline int squared<int>(int value) +constexpr int squared<int>(int value) { return value * value; } @@ -68,13 +68,13 @@ // http://stackoverflow.com/a/18204188/3629665 template<typename T> -inline int rotl10(T x) +constexpr int rotl10(T x) { return (((x) << 10) | (((x) >> 22) & 0x000000ff)); } template<typename T> -inline int rotl20(T x) +constexpr int rotl20(T x) { return (((x) << 20) | (((x) >> 12) & 0x000000ff)); } @@ -82,12 +82,13 @@ // // Get the amount of elements in something. // -template<typename T, size_t N> -int countof(T(&)[N]) +template<typename T, int N> +constexpr int countof(T(&)[N]) { return N; } +[[maybe_unused]] static inline int countof(const QString& string) { return string.length(); @@ -122,7 +123,7 @@ * From: https://stackoverflow.com/q/1903954 */ template<typename T> -int sign(T value) +constexpr int sign(T value) { return (0 < value) - (value < 0); } @@ -131,7 +132,7 @@ * Returns the maximum of a single parameter (the parameter itself). */ template <typename T> -T max(T a) +constexpr T max(T a) { return a; } @@ -140,7 +141,7 @@ * Returns the maximum of two parameters. */ template <typename T> -T max(T a, T b) +constexpr T max(T a, T b) { return a > b ? a : b; } @@ -149,7 +150,7 @@ * Returns the maximum of n parameters. */ template <typename T, typename... Rest> -T max(T a, Rest&&... rest) +constexpr T max(T a, Rest&&... rest) { return max(a, max(rest...)); } @@ -158,7 +159,7 @@ * Returns the minimum of a single parameter (the parameter itself). */ template <typename T> -T min(T a) +constexpr T min(T a) { return a; } @@ -167,7 +168,7 @@ * Returns the minimum of two parameters. */ template <typename T> -T min(T a, T b) +constexpr T min(T a, T b) { return a < b ? a : b; } @@ -176,7 +177,7 @@ * Returns the minimum of n parameters. */ template <typename T, typename... Rest> -T min(T a, Rest&&... rest) +constexpr T min(T a, Rest&&... rest) { return min(a, min(rest...)); } @@ -217,7 +218,7 @@ * Returns the empty sum. (recursion base) */ template<typename T> -T sum() +constexpr T sum() { return {}; } @@ -226,7 +227,7 @@ * Returns the sum of n arguments. */ template<typename T, typename... Rest> -T sum(const T& arg, Rest&&... rest) +constexpr T sum(const T& arg, Rest&&... rest) { return arg + sum<T>(rest...); }