Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit57dec20

Browse files
committed
Add C++ template versions of C macros
1 parent2af4a9c commit57dec20

File tree

1 file changed

+71
-35
lines changed

1 file changed

+71
-35
lines changed

‎api/Common.h

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,82 @@ typedef enum {
3737
#defineSERIAL0x0
3838
#defineDISPLAY0x1
3939

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

44-
#ifndef radians
45-
#defineradians(deg) ((deg)*DEG_TO_RAD)
46-
#endif
44+
#ifdef __cplusplus
45+
46+
template<classT,classL>
47+
automin(const T& a,const L& b) -> decltype((b < a) ? b : a)
48+
{
49+
return (b < a) ? b : a;
50+
}
51+
52+
template<classT,classL>
53+
automax(const T& a,const L& b) -> decltype((b < a) ? b : a)
54+
{
55+
return (a < b) ? b : a;
56+
}
57+
58+
59+
template<classT,classU,classV>
60+
autoconstrain(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<classT>
66+
autoradians(const T& deg) -> decltype(deg * DEG_TO_RAD)
67+
{
68+
return deg * DEG_TO_RAD;
69+
}
70+
71+
template<classT>
72+
autodegrees(const T& rad) -> decltype(rad * RAD_TO_DEG)
73+
{
74+
return rad * RAD_TO_DEG;
75+
}
4776

48-
#ifndef degrees
49-
#definedegrees(rad) ((rad)*RAD_TO_DEG)
77+
template<classT>
78+
autosq(const T& x) -> decltype(x*x)
79+
{
80+
return x*x;
81+
}
82+
#else
83+
#ifndef constrain
84+
#defineconstrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
85+
#endif
86+
87+
#ifndef radians
88+
#defineradians(deg) ((deg)*DEG_TO_RAD)
89+
#endif
90+
91+
#ifndef degrees
92+
#definedegrees(rad) ((rad)*RAD_TO_DEG)
93+
#endif
94+
95+
#ifndef sq
96+
#definesq(x) ((x)*(x))
97+
#endif
98+
99+
#ifndef min
100+
#definemin(a,b) \
101+
({__typeof__ (a) _a = (a); \
102+
__typeof__ (b) _b = (b); \
103+
_a < _b ? _a : _b; })
104+
#endif
105+
106+
#ifndef max
107+
#definemax(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-
#definesq(x) ((x)*(x))
114+
#ifdef __cplusplus
115+
extern"C"{
54116
#endif
55117

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

122-
#ifdef __cplusplus
123-
template<classT,classL>
124-
automin(const T& a,const L& b) -> decltype((b < a) ? b : a)
125-
{
126-
return (b < a) ? b : a;
127-
}
128-
129-
template<classT,classL>
130-
automax(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-
#definemin(a,b) \
137-
({__typeof__ (a) _a = (a); \
138-
__typeof__ (b) _b = (b); \
139-
_a < _b ? _a : _b; })
140-
#endif
141-
#ifndef max
142-
#definemax(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)

[8]ページ先頭

©2009-2025 Movatter.jp