From 57dec209f2ab7642443c3e70c609f9209bf68f6f Mon Sep 17 00:00:00 2001 From: Keating Reid Date: Fri, 5 Feb 2021 10:26:38 -0500 Subject: [PATCH 1/4] Add C++ template versions of C macros --- api/Common.h | 106 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 35 deletions(-) diff --git a/api/Common.h b/api/Common.h index 6e12a74c..60c6561c 100644 --- a/api/Common.h +++ b/api/Common.h @@ -37,20 +37,82 @@ typedef enum { #define SERIAL 0x0 #define DISPLAY 0x1 -#ifndef constrain -#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) +#ifdef __cplusplus +} // extern "C" #endif -#ifndef radians -#define radians(deg) ((deg)*DEG_TO_RAD) -#endif +#ifdef __cplusplus + + template + auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) + { + return (b < a) ? b : a; + } + + template + auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) + { + return (a < b) ? b : a; + } + + + template + auto constrain(const T& amt, const U& low, const V& high) -> decltype((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + { + return (amt)<(low)?(low):((amt)>(high)?(high):(amt)); + } + + template + auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD) + { + return deg * DEG_TO_RAD; + } + + template + auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG) + { + return rad * RAD_TO_DEG; + } -#ifndef degrees -#define degrees(rad) ((rad)*RAD_TO_DEG) + template + auto sq(const T& x) -> decltype(x*x) + { + return x*x; + } +#else + #ifndef constrain + #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + #endif + + #ifndef radians + #define radians(deg) ((deg)*DEG_TO_RAD) + #endif + + #ifndef degrees + #define degrees(rad) ((rad)*RAD_TO_DEG) + #endif + + #ifndef sq + #define sq(x) ((x)*(x)) + #endif + + #ifndef min + #define min(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) + #endif + + #ifndef max + #define max(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) + #endif #endif -#ifndef sq -#define sq(x) ((x)*(x)) +#ifdef __cplusplus +extern "C"{ #endif typedef void (*voidFuncPtr)(void); @@ -119,32 +181,6 @@ void loop(void); } // extern "C" #endif -#ifdef __cplusplus - template - auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) - { - return (b < a) ? b : a; - } - - template - auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) - { - return (a < b) ? b : a; - } -#else -#ifndef min -#define min(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) -#endif -#ifndef max -#define max(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) -#endif -#endif #ifdef __cplusplus From be460bc8ebe15f70e8f2270e7a090bcbf3ee9857 Mon Sep 17 00:00:00 2001 From: Keating Reid Date: Fri, 5 Feb 2021 10:33:49 -0500 Subject: [PATCH 2/4] make C macros evaluate arguments once; reduce unnecessary parentheses --- api/Common.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/api/Common.h b/api/Common.h index 60c6561c..a29dc532 100644 --- a/api/Common.h +++ b/api/Common.h @@ -44,22 +44,22 @@ typedef enum { #ifdef __cplusplus template - auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) + auto min(const T& a, const L& b) -> decltype(b < a ? b : a) { return (b < a) ? b : a; } template - auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) + auto max(const T& a, const L& b) -> decltype(b < a ? b : a) { return (a < b) ? b : a; } template - auto constrain(const T& amt, const U& low, const V& high) -> decltype((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + auto constrain(const T& amt, const U& low, const V& high) -> decltype(amt < low ? low : (amt > high ? high : amt)) { - return (amt)<(low)?(low):((amt)>(high)?(high):(amt)); + return amt < low ? low : (amt > high ? high : amt); } template @@ -81,19 +81,29 @@ typedef enum { } #else #ifndef constrain - #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + #define constrain(amt,low,high) \ + ({ __typeof__ (amt) _amt = (amt); \ + __typeof__ (low) _low = (low); \ + __typeof__ (high) _high = (high); \ + _amt < _low ? _low : (_amt > _high ? _high :_amt); } #endif #ifndef radians - #define radians(deg) ((deg)*DEG_TO_RAD) + #define radians(deg) \ + ({ __typeof__ (deg) _deg = deg; \ + _deg * DEG_TO_RAD; }) #endif #ifndef degrees - #define degrees(rad) ((rad)*RAD_TO_DEG) + #define degrees(rad) \ + ({ __typeof__ (rad) _rad = rad; \ + _rad * RAD_TO_DEG; }) #endif #ifndef sq - #define sq(x) ((x)*(x)) + #define sq(x) \ + ({ __typeof__ (x) _x = x; \ + _x * _x; }) #endif #ifndef min From 18f50953c4644b2c5cb90f20cf9f7427ece02647 Mon Sep 17 00:00:00 2001 From: Keating Reid Date: Wed, 17 Feb 2021 14:45:21 -0500 Subject: [PATCH 3/4] minimize diff; fix missing paren --- api/Common.h | 96 +++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/api/Common.h b/api/Common.h index a29dc532..78170a27 100644 --- a/api/Common.h +++ b/api/Common.h @@ -37,44 +37,29 @@ typedef enum { #define SERIAL 0x0 #define DISPLAY 0x1 -#ifdef __cplusplus -} // extern "C" -#endif #ifdef __cplusplus +} // extern "C" - template - auto min(const T& a, const L& b) -> decltype(b < a ? b : a) - { - return (b < a) ? b : a; - } - - template - auto max(const T& a, const L& b) -> decltype(b < a ? b : a) - { - return (a < b) ? b : a; - } - - - template + template auto constrain(const T& amt, const U& low, const V& high) -> decltype(amt < low ? low : (amt > high ? high : amt)) { return amt < low ? low : (amt > high ? high : amt); } - - template + + template auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD) { return deg * DEG_TO_RAD; } - template + template auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG) { return rad * RAD_TO_DEG; } - template + template auto sq(const T& x) -> decltype(x*x) { return x*x; @@ -82,47 +67,33 @@ typedef enum { #else #ifndef constrain #define constrain(amt,low,high) \ - ({ __typeof__ (amt) _amt = (amt); \ - __typeof__ (low) _low = (low); \ - __typeof__ (high) _high = (high); \ - _amt < _low ? _low : (_amt > _high ? _high :_amt); } + ({ __typeof__ (amt) _amt = (amt); \ + __typeof__ (low) _low = (low); \ + __typeof__ (high) _high = (high); \ + _amt < _low ? _low : (_amt > _high ? _high :_amt); }) #endif - + #ifndef radians #define radians(deg) \ - ({ __typeof__ (deg) _deg = deg; \ - _deg * DEG_TO_RAD; }) + ({ __typeof__ (deg) _deg = deg; \ + _deg * DEG_TO_RAD; }) #endif - + #ifndef degrees #define degrees(rad) \ - ({ __typeof__ (rad) _rad = rad; \ - _rad * RAD_TO_DEG; }) + ({ __typeof__ (rad) _rad = rad; \ + _rad * RAD_TO_DEG; }) #endif - + #ifndef sq #define sq(x) \ - ({ __typeof__ (x) _x = x; \ - _x * _x; }) - #endif - - #ifndef min - #define min(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) - #endif - - #ifndef max - #define max(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) + ({ __typeof__ (x) _x = x; \ + _x * _x; }) #endif #endif #ifdef __cplusplus -extern "C"{ +extern "C" { #endif typedef void (*voidFuncPtr)(void); @@ -189,8 +160,32 @@ void loop(void); #ifdef __cplusplus } // extern "C" -#endif + template + auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) + { + return (b < a) ? b : a; + } + + template + auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) + { + return (a < b) ? b : a; + } +#else + #ifndef min + #define min(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) + #endif + #ifndef max + #define max(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) + #endif +#endif #ifdef __cplusplus @@ -213,3 +208,4 @@ void randomSeed(unsigned long); long map(long, long, long, long, long); #endif // __cplusplus + From 585f67a7147712a1c8eb577ec7faaa349515d90e Mon Sep 17 00:00:00 2001 From: Keating Reid Date: Wed, 17 Feb 2021 14:51:58 -0500 Subject: [PATCH 4/4] remove trailing newline --- api/Common.h | 1 - 1 file changed, 1 deletion(-) diff --git a/api/Common.h b/api/Common.h index 78170a27..bacae0b0 100644 --- a/api/Common.h +++ b/api/Common.h @@ -208,4 +208,3 @@ void randomSeed(unsigned long); long map(long, long, long, long, long); #endif // __cplusplus -