Skip to content

Commit 97db34d

Browse files
committed
replase functions
1 parent 17d9d7a commit 97db34d

1 file changed

Lines changed: 164 additions & 17 deletions

File tree

src/Tools.Easy.h

Lines changed: 164 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,180 @@
4343
/// @brief get size array
4444
#define getArraySize(X) (sizeof(X) / sizeof(*X))
4545

46-
#if 0
47-
4846
/// @brief
49-
#if defined(M_PI)
50-
#define PI (M_PI)
51-
#else
52-
#define PI (3.1415926535897932384626433832795028841971693993751)
47+
#if !defined(M_PI)
48+
#define M_PI (3.1415926535897932384626433832795028841971693993751)
5349
#endif
5450

5551
/// @brief
56-
#define RAD_TO_DEG (180. / PI)
52+
#define RAD_TO_DEG (180. / M_PI)
5753

5854
/// @brief
59-
#define DEG_TO_RAD (PI / 180.)
55+
#define DEG_TO_RAD (M_PI / 180.)
6056

61-
#ifdef abs
57+
#if defined(abs)
6258
#undef abs
6359
#endif
6460

65-
#define min(a, b) ((a) < (b) ? (a) : (b))
66-
#define max(a, b) ((a) > (b) ? (a) : (b))
67-
#define abs(x) ((x) > 0 ? (x) : -(x))
68-
#define constrain(amt, low, high) \
69-
((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
70-
#define round(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x) - 0.5))
71-
#define radians(deg) ((deg) * DEG_TO_RAD)
72-
#define degrees(rad) ((rad) * RAD_TO_DEG)
61+
#if defined(min)
62+
#undef min
63+
#endif
64+
65+
#if defined(max)
66+
#undef max
67+
#endif
68+
69+
#if defined(round)
70+
#undef round
71+
#endif
72+
73+
#if defined(sq)
74+
#undef sq
75+
#endif
76+
77+
#if defined(radians)
78+
#undef radians
79+
#endif
80+
81+
#if defined(degrees)
82+
#undef degrees
83+
#endif
84+
85+
#if defined(constrain)
86+
#undef constrain
87+
#endif
88+
89+
#if defined(__cplusplus)
90+
/// @brief
91+
/// @tparam T
92+
/// @param value
93+
/// @return
94+
template <class T>
95+
auto abs(const T &value) -> decltype(value > 0 ? value : -value) {
96+
return value > 0 ? value : -value;
97+
}
98+
99+
/// @brief
100+
/// @tparam T1
101+
/// @tparam T2
102+
/// @param value_a
103+
/// @param value_b
104+
/// @return
105+
template <class T1, class T2>
106+
auto min(const T1 &value_a, const T2 &value_b)
107+
-> decltype((value_b < value_a) ? value_b : value_a) {
108+
return (value_b < value_a) ? value_b : value_a;
109+
}
110+
111+
/// @brief
112+
/// @tparam T1
113+
/// @tparam T2
114+
/// @param value_a
115+
/// @param value_b
116+
/// @return
117+
template <class T1, class T2>
118+
auto max(const T1 &value_a, const T2 &value_b)
119+
-> decltype((value_b < value_a) ? value_b : value_a) {
120+
return (value_a < value_b) ? value_b : value_a;
121+
}
122+
123+
/// @brief
124+
/// @tparam T
125+
/// @param value
126+
/// @return
127+
template <class T> long round(const T &value) {
128+
return (long)(value >= 0 ? (value + 0.5) : (value - 0.5));
129+
}
130+
131+
/// @brief
132+
/// @tparam T
133+
/// @param value
134+
/// @return
135+
template <class T> auto sq(const T &value) -> decltype(value * value) {
136+
return value * value;
137+
}
138+
139+
/// @brief
140+
/// @tparam T
141+
/// @param deg
142+
/// @return
143+
template <class T> auto radians(const T &deg) -> decltype(deg * DEG_TO_RAD) {
144+
return deg * DEG_TO_RAD;
145+
}
146+
147+
/// @brief
148+
/// @tparam T
149+
/// @param rad
150+
/// @return
151+
template <class T> auto degrees(const T &rad) -> decltype(rad * RAD_TO_DEG) {
152+
return rad * RAD_TO_DEG;
153+
}
154+
155+
/// @brief
156+
/// @tparam T
157+
/// @tparam TL
158+
/// @tparam TH
159+
/// @param value
160+
/// @param low
161+
/// @param high
162+
/// @return
163+
template <class T, class TL, class TH>
164+
auto constrain(const T &value, const TL &low, const TH &high)
165+
-> decltype((value < low) ? low
166+
: (value > high) ? high
167+
: value) {
168+
return (value < low) ? low : (value > high) ? high : value;
169+
}
170+
171+
#else // __cplusplus
172+
173+
/// @brief
174+
#define abs(value) \
175+
({ \
176+
typeof(value) _value = (value); \
177+
_value > 0 ? _value : -_value; \
178+
})
179+
180+
/// @brief
181+
#define min(value_a, value_b) \
182+
({ \
183+
typeof(value_a) _value_a = (value_a); \
184+
typeof(value_b) _value_b = (value_b); \
185+
_value_a < _value_b ? _value_a : _value_b; \
186+
})
187+
188+
/// @brief
189+
#define max(value_a, value_b) \
190+
({ \
191+
typeof(value_a) _value_a = (value_a); \
192+
typeof(value_b) _value_b = (value_b); \
193+
_value_a > _value_b ? _value_a : _value_b; \
194+
})
195+
196+
/// @brief
197+
#define sq(value) \
198+
({ \
199+
typeof(value) _value = (value); \
200+
_value *_value; \
201+
})
202+
203+
/// @brief
204+
#define radians(deg) ((deg)*DEG_TO_RAD)
205+
206+
/// @brief
207+
#define degrees(rad) ((rad)*RAD_TO_DEG)
208+
209+
/// @brief
210+
#define constrain(value, low, high) \
211+
({ \
212+
typeof(value) _value = (value); \
213+
typeof(low) _low = (low); \
214+
typeof(high) _high = (high); \
215+
_value<_low ? _low : _value> _high ? _high : _value; \
216+
})
217+
218+
#endif // __cplusplus
219+
#if 0
73220
#define map(value, in_min, in_max, out_min, out_max) \
74221
((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
75222
#endif

0 commit comments

Comments
 (0)