Skip to content

Commit 57dec20

Browse files
committed
Add C++ template versions of C macros
1 parent 2af4a9c commit 57dec20

File tree

1 file changed

+71
-35
lines changed

1 file changed

+71
-35
lines changed

api/Common.h

+71-35
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,82 @@ typedef enum {
3737
#define SERIAL 0x0
3838
#define DISPLAY 0x1
3939

40-
#ifndef constrain
41-
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
40+
#ifdef __cplusplus
41+
} // extern "C"
4242
#endif
4343

44-
#ifndef radians
45-
#define radians(deg) ((deg)*DEG_TO_RAD)
46-
#endif
44+
#ifdef __cplusplus
45+
46+
template<class T, class L>
47+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
48+
{
49+
return (b < a) ? b : a;
50+
}
51+
52+
template<class T, class L>
53+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
54+
{
55+
return (a < b) ? b : a;
56+
}
57+
58+
59+
template<class T, class U, class V>
60+
auto constrain(const T& amt, const U& low, const V& high) -> decltype((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
61+
{
62+
return (amt)<(low)?(low):((amt)>(high)?(high):(amt));
63+
}
64+
65+
template<class T>
66+
auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD)
67+
{
68+
return deg * DEG_TO_RAD;
69+
}
70+
71+
template<class T>
72+
auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG)
73+
{
74+
return rad * RAD_TO_DEG;
75+
}
4776

48-
#ifndef degrees
49-
#define degrees(rad) ((rad)*RAD_TO_DEG)
77+
template<class T>
78+
auto sq(const T& x) -> decltype(x*x)
79+
{
80+
return x*x;
81+
}
82+
#else
83+
#ifndef constrain
84+
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
85+
#endif
86+
87+
#ifndef radians
88+
#define radians(deg) ((deg)*DEG_TO_RAD)
89+
#endif
90+
91+
#ifndef degrees
92+
#define degrees(rad) ((rad)*RAD_TO_DEG)
93+
#endif
94+
95+
#ifndef sq
96+
#define sq(x) ((x)*(x))
97+
#endif
98+
99+
#ifndef min
100+
#define min(a,b) \
101+
({ __typeof__ (a) _a = (a); \
102+
__typeof__ (b) _b = (b); \
103+
_a < _b ? _a : _b; })
104+
#endif
105+
106+
#ifndef max
107+
#define max(a,b) \
108+
({ __typeof__ (a) _a = (a); \
109+
__typeof__ (b) _b = (b); \
110+
_a > _b ? _a : _b; })
111+
#endif
50112
#endif
51113

52-
#ifndef sq
53-
#define sq(x) ((x)*(x))
114+
#ifdef __cplusplus
115+
extern "C"{
54116
#endif
55117

56118
typedef void (*voidFuncPtr)(void);
@@ -119,32 +181,6 @@ void loop(void);
119181
} // extern "C"
120182
#endif
121183

122-
#ifdef __cplusplus
123-
template<class T, class L>
124-
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
125-
{
126-
return (b < a) ? b : a;
127-
}
128-
129-
template<class T, class L>
130-
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
131-
{
132-
return (a < b) ? b : a;
133-
}
134-
#else
135-
#ifndef min
136-
#define min(a,b) \
137-
({ __typeof__ (a) _a = (a); \
138-
__typeof__ (b) _b = (b); \
139-
_a < _b ? _a : _b; })
140-
#endif
141-
#ifndef max
142-
#define max(a,b) \
143-
({ __typeof__ (a) _a = (a); \
144-
__typeof__ (b) _b = (b); \
145-
_a > _b ? _a : _b; })
146-
#endif
147-
#endif
148184

149185
#ifdef __cplusplus
150186

0 commit comments

Comments
 (0)