BALL  1.5.0
macros.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_COMMON_MACROS_H
6 #define BALL_COMMON_MACROS_H
7 
8 #include <BALL/CONFIG/config.h>
10 #include <BALL/COMMON/rtti.h>
11 
12 #include <chrono> // std::chrono::seconds
13 #include <cmath> // needed for fabs
14 #include <thread> // std::this_thread::sleep_for
15 #include <typeinfo> // needed for typeid
16 
17 namespace BALL
18 {
19  template<class T>
21  {
22  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
23  }
24 
25  template<class T>
27  {
28  return a < b ? b : a;
29  }
30 
31  template<class T>
33  {
34  return a > b ? (a > c ? a : c) : (b > c ? b : c);
35  }
36 
37  template<class T>
39  {
40  return a > b ? b : a;
41  }
42 
43  template<class T>
45  {
46  return a < b ? (a < c ? a : c) : (b < c ? b : c);
47  }
48 
49  template<class X>
51  {
52  return x >= 0 ? x : -x;
53  }
54 
55  template<class X>
57  {
58  return (x < 0 ? -1 : (x == 0) ? 0 : 1);
59  }
60 
61  template<class X>
63  {
64  return (x % 2) != 0;
65  }
66 
67  template<class X>
69  {
70  return (x & 0x1) == 1;
71  }
72 
73  template<class X>
75  {
76  return (x & 0x1) == 0;
77  }
78 
79  template<class X>
81  {
82  return (X)(x > 0 ? long(x + 0.5) : long(x - 0.5));
83  }
84 
85  template<class X, class Y, class E>
87  {
88  return std::fabs(x - y) <= e;
89  }
90 
91  template<class X, class Y, class E>
93  {
94  return std::fabs(x - y) > e;
95  }
96 
97  template<class X, class Y, class E>
99  {
100  return (x - y) < -e;
101  }
102 
103  template<class X, class Y, class E>
105  {
106  return (x - y) <= e;
107  }
108 
109  template<class X, class Y, class E>
111  {
112  return (x - y) > e;
113  }
114 
115  template<class X, class Y, class E>
117  {
118  return (x - y) >= -e;
119  }
120 
121  template<class X>
123  {
124  return std::fabs(x);
125  }
126 
127  template<class X>
129  {
130  return fmod(x, 2) != 0;
131  }
132 
133  template<class X>
135  {
136  return fmod(x, 2) == 0;
137  }
138 
139  template<class X>
141  {
142  return (long)(x > 0 ? x : (x == (long)(x) ? x : x - 1));
143  }
144 
145  template<class X>
147  {
148  return (long)(x < 0 ? x : (x == (long)(x) ? x : x + 1));
149  }
150 
151  template<class X>
153  {
154  return (x > 0 ? (int)(x + 0.5) : -(int)(0.5 - x));
155  }
156 
157  template<class T>
159  {
160  return T(BALL_CHAR_SIZE * 8);
161  }
162 
163  template<class T>
165  {
166  return T(BALL_CHAR_SIZE * 8) - 1;
167  }
168 
169  template<class T>
171  {
172  unsigned bits = BALL_CHAR_SIZE * 8;
173  return T(bits == 8 ? 3 : (bits == 16) ? 4 : 5);
174  }
175 
176  template<class T>
178  {
179  return ~T(0);
180  }
181 
182  template<class T>
184  {
185  return T(0);
186  }
187 
188  template<class T>
190  {
191  unsigned bits = BALL_CHAR_SIZE * 8;
192  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
193  return (x + bits - 1) >> shift;
194  }
195 
196  template<class T, std::size_t N>
198  {
199  return N;
200  }
201 
202  template<class T>
204  {
205  unsigned bits = BALL_CHAR_SIZE * 8;
206  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
207  return ((x - 1) >> shift) + 1;
208  }
209 
210  template<class BitArray, class X>
212  {
213  unsigned bits = BALL_CHAR_SIZE * 8;
214  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
215  a[x >> shift] &= ~(1 << (x & (bits - 1)));
216  }
217 
218  template<class BitArray, class X>
220  {
221  unsigned bits = BALL_CHAR_SIZE * 8;
222  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
223  a[x >> shift] |= (1 << (x & (bits - 1)));
224  }
225 
226  template<class BitArray, class X>
228  {
229  unsigned bits = BALL_CHAR_SIZE * 8;
230  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
231  a[x >> shift] ^= (1 << (x & (bits - 1)));
232  }
233 
234  template<class BitArray, class X>
236  {
237  unsigned bits = BALL_CHAR_SIZE * 8;
238  unsigned shift = (bits == 8 ? 3u : (bits == 16) ? 4u : 5u);
239  return (a[x >> shift] & (1 << (x & (bits - 1)))) != 0;
240  }
241 
242  template<class Bit>
244  {
245  return 1 << bit;
246  }
247 
248  template<class Bitset, class Bit>
250  {
251  bitset |= (1 << bit);
252  }
253 
254  template<class Bitset>
256  {
257  bitset = -1;
258  }
259 
260  template<class Bitset, class Bit>
262  {
263  bitset |= ~(-1 << (bit + 1));
264  }
265 
266  template<class Bitset, class Bit>
268  {
269  bitset |= (-1 << bit);
270  }
271 
272  template<class Bitset, class Bit>
274  {
275  bitset &= ~(1 << bit);
276  }
277 
278  template<class Bitset>
280  {
281  bitset = 0;
282  }
283 
284  template<class Bitset, class Bit>
286  {
287  bitset &= (-1 << (bit + 1));
288  }
289 
290  template<class Bitset, class Bit>
292  {
293  bitset &= ~(-1 << bit);
294  }
295 
296  template<class Bitset, class Bit>
298  {
299  return bitset & (1 << bit);
300  }
301 
302  template<class Bitset, class Bit>
304  {
305  return !(bitset & (1 << bit));
306  }
307 
308  template<class T>
310  {
311  return 180. / ::BALL::Constants::PI * rad_angle;
312  }
313 
314  template<class T>
316  {
317  return ::BALL::Constants::PI / 180. * deg_angle;
318  }
319 
320  template<class T>
322  {
323  return value;
324  }
325 }
326 
327 // Macro to block execution of current thread for at least x milliseconds
328 #define BALL_SLEEPFOR(x) BALL::ball_macro_sleepfor(x)
329 
330 #define BALL_MAX(a, b) BALL::ball_macro_max(a, b)
331 #define BALL_MAX3(x, y, z) BALL::ball_macro_max3(x, y, z)
332 #define BALL_MIN(a, b) BALL::ball_macro_min(a, b)
333 #define BALL_MIN3(x, y, z) BALL::ball_macro_min3(x, y ,z)
334 #define BALL_ABS(x) BALL::ball_macro_abs(x)
335 #define BALL_SGN(x) BALL::ball_macro_sgn(x)
336 #define BALL_ODD(x) BALL::ball_macro_odd(x)
337 
338 #define BALL_INT_ODD(x) BALL::ball_macro_int_odd(x)
339 #define BALL_INT_EVEN(x) BALL::ball_macro_int_even(x)
340 
341 #define BALL_REAL_ROUND(x) BALL::ball_macro_real_round(x)
342 #define BALL_REAL_EQUAL(x, y, e) BALL::ball_macro_real_equal(x, y, e)
343 #define BALL_REAL_NOT_EQUAL(x, y, e) BALL::ball_macro_real_not_equal(x, y, e)
344 #define BALL_REAL_LESS(x, y, e) BALL::ball_macro_real_less(x, y, e)
345 #define BALL_REAL_LESS_OR_EQUAL(x, y, e) BALL::ball_macro_real_less_or_equal(x, y, e)
346 #define BALL_REAL_GREATER(x, y, e) BALL::ball_macro_real_greater(x, y, e)
347 #define BALL_REAL_GREATER_OR_EQUAL(x, y, e) BALL::ball_macro_real_greater_or_equal(x, y, e)
348 #define BALL_REAL_ABS(x) BALL::ball_macro_real_abs(x)
349 #define BALL_REAL_SGN(x) BALL::ball_macro_sgn(x)
350 #define BALL_REAL_ODD(x) BALL::ball_macro_real_odd(x)
351 #define BALL_REAL_EVEN(x) BALL::ball_macro_real_even(x)
352 #define BALL_REAL_FLOOR(x) BALL::ball_macro_real_floor(x)
353 #define BALL_REAL_CEILING(x) BALL::ball_macro_real_ceiling(x)
354 #define BALL_REAL_ROUND_INT(x) BALL::ball_macro_real_round_int(x)
355 
356 
357 // The following macros assume BALL_CHAR_BITS is one of either 8, 16, or 32
358 #define BALL_CHAR_BITS BALL::ball_macro_char_bits<unsigned>()
359 #define BALL_CHAR_MASK BALL::ball_macro_char_mask<unsigned>()
360 #define BALL_CHAR_SHIFT BALL::ball_macro_char_shift<unsigned>()
361 #define BALL_CHAR_ALL_BITS_SET BALL::ball_macro_all_bits_set<char>()
362 #define BALL_CHAR_ALL_BITS_CLEARED BALL::ball_macro_all_bits_cleared<char>()
363 #define BALL_NUMBER_OF_BYTES(bits) BALL::ball_macro_number_of_bytes(bits)
364 
365 #define BALL_SIZEOF_ARRAY(a) BALL::ball_macro_sizeof_array(a)
366 
367 #define BALL_BITARRAY_SIZE(number_of_bits) BALL::ball_macro_bitarray_size(number_of_bits)
368 #define BALL_BITARRAY_CLEAR_BIT(array, x) BALL::ball_macro_bitarray_clear_bit(array, x)
369 #define BALL_BITARRAY_SET_BIT(array, x) BALL::ball_macro_bitarray_set_bit(array, x)
370 #define BALL_BITARRAY_TOGGLE_BIT(array, x) BALL::ball_macro_bitarray_toggle_bit(array, x)
371 #define BALL_BITARRAY_IS_BIT_SET(array, x) BALL::ball_macro_bitarray_is_bit_set(array, x)
372 
373 #define BALL_BIT(bit) BALL::ball_macro_bit(bit)
374 #define BALL_BIT_SET(bitset, bit) BALL::ball_macro_bit_set(bitset, bit)
375 #define BALL_BIT_SET_ALL(bitset) BALL::ball_macro_bit_set_all(bitset)
376 #define BALL_BIT_SET_ALL_TO(bitset, bit) BALL::ball_macro_bit_set_all_to(bitset, bit)
377 #define BALL_BIT_SET_ALL_FROM(bitset, bit) BALL::ball_macro_bit_set_all_from(bitset, bit)
378 #define BALL_BIT_CLEAR(bitset, bit) BALL::ball_macro_bit_clear(bitset, bit)
379 #define BALL_BIT_CLEAR_ALL(bitset) BALL::ball_macro_bit_clear_all(bitset)
380 #define BALL_BIT_CLEAR_ALL_TO(bitset, bit) BALL::ball_macro_bit_clear_all_to(bitset, bit)
381 #define BALL_BIT_CLEAR_ALL_FROM(bitset, bit) BALL::ball_macro_bit_clear_all_from(bitset, bit)
382 #define BALL_BIT_IS_SET(bitset, bit) BALL::ball_macro_bit_is_set(bitset, bit)
383 #define BALL_BIT_IS_CLEARED(bitset, bit) BALL::ball_macro_bit_is_cleared(bitset, bit)
384 
385 #define BALL_ANGLE_RADIAN_TO_DEGREE(rad_angle) BALL::ball_macro_angle_radian_to_degree(rad_angle)
386 #define BALL_ANGLE_DEGREE_TO_RADIAN(deg_angle) BALL::ball_macro_angle_degree_to_radian(deg_angle)
387 
388 #define BALL_OFFSET_OF(struct_name, struct_var_name) BALL::ball_macro_generic_echo((long)&(((struct_name*)0)->struct_var_name))
389 
390 #define BALL_DUMP_DEPTH(os, depth) for (dump_indent_depth_ = 0; dump_indent_depth_ < depth; ++dump_indent_depth_) { os << " "; }
391 #define BALL_DUMP_STREAM_PREFIX(os) Size dump_indent_depth_ = 0;
392 
393 #define BALL_DUMP_HEADER(os,cl,ob) os << "Object: " << (void *)ob << " is instance of class: " << streamClassName(typeid(*ob)) << std::endl;
394 #define BALL_DUMP_CLASS_HEADER(os,cl,ob) os << "Object: " << (void *)ob << " is instance of class: " << #cl << ::std::endl;
395 #define BALL_DUMP_STREAM_SUFFIX(os)
396 
397 #endif // BALL_COMMON_MACROS_H
BALL::ball_macro_bit_set
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_set(Bitset &bitset, Bit bit)
Definition: macros.h:249
BALL::ball_macro_sleepfor
void BALL_DEPRECATED BALL_INLINE ball_macro_sleepfor(T ms)
Definition: macros.h:20
BALL_INLINE
#define BALL_INLINE
Definition: debug.h:15
BALL::ball_macro_bitarray_clear_bit
void BALL_DEPRECATED BALL_INLINE ball_macro_bitarray_clear_bit(BitArray *a, X x)
Definition: macros.h:211
rtti.h
BALL::ball_macro_real_round
X BALL_DEPRECATED BALL_INLINE ball_macro_real_round(X x)
Definition: macros.h:80
BALL::ball_macro_max3
T BALL_DEPRECATED BALL_INLINE ball_macro_max3(T a, T b, T c)
Definition: macros.h:32
BALL::ball_macro_bit_clear_all
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_clear_all(Bitset &bitset)
Definition: macros.h:279
BALL::ball_macro_sizeof_array
std::size_t BALL_DEPRECATED BALL_INLINE ball_macro_sizeof_array(T(&)[N])
Definition: macros.h:197
BALL::ball_macro_all_bits_cleared
T BALL_DEPRECATED BALL_INLINE ball_macro_all_bits_cleared()
Definition: macros.h:183
BALL::ball_macro_bit_set_all
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_set_all(Bitset &bitset)
Definition: macros.h:255
BALL::ball_macro_bit_clear_all_from
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_clear_all_from(Bitset &bitset, Bit bit)
Definition: macros.h:291
BALL::ball_macro_odd
bool BALL_DEPRECATED BALL_INLINE ball_macro_odd(X x)
Definition: macros.h:62
BALL::ball_macro_char_shift
T BALL_DEPRECATED BALL_INLINE ball_macro_char_shift()
Definition: macros.h:170
BALL::ball_macro_bit_set_all_from
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_set_all_from(Bitset &bitset, Bit bit)
Definition: macros.h:267
BALL::ball_macro_int_odd
bool BALL_DEPRECATED BALL_INLINE ball_macro_int_odd(X x)
Definition: macros.h:68
BALL::ball_macro_char_bits
T BALL_DEPRECATED BALL_INLINE ball_macro_char_bits()
Definition: macros.h:158
BALL::ball_macro_sgn
int BALL_DEPRECATED BALL_INLINE ball_macro_sgn(X x)
Definition: macros.h:56
BALL::ball_macro_real_ceiling
long BALL_DEPRECATED BALL_INLINE ball_macro_real_ceiling(X x)
Definition: macros.h:146
BALL::ball_macro_bitarray_size
T BALL_DEPRECATED BALL_INLINE ball_macro_bitarray_size(T x)
Definition: macros.h:203
BALL::ball_macro_max
T BALL_DEPRECATED BALL_INLINE ball_macro_max(T a, T b)
Definition: macros.h:26
BALL
Definition: constants.h:13
BALL::ball_macro_char_mask
T BALL_DEPRECATED BALL_INLINE ball_macro_char_mask()
Definition: macros.h:164
BALL::ball_macro_real_equal
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_equal(X x, Y y, E e)
Definition: macros.h:86
BALL::ball_macro_all_bits_set
T BALL_DEPRECATED BALL_INLINE ball_macro_all_bits_set()
Definition: macros.h:177
BALL::ball_macro_real_even
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_even(X x)
Definition: macros.h:134
BALL::ball_macro_angle_degree_to_radian
T BALL_DEPRECATED BALL_INLINE ball_macro_angle_degree_to_radian(T deg_angle)
Definition: macros.h:315
BALL::Constants::c
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
BALL::ball_macro_real_greater
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_greater(X x, Y y, E e)
Definition: macros.h:110
BALL::ball_macro_number_of_bytes
T BALL_DEPRECATED BALL_INLINE ball_macro_number_of_bytes(T x)
Definition: macros.h:189
BALL::ball_macro_generic_echo
T BALL_DEPRECATED BALL_INLINE ball_macro_generic_echo(T value)
Definition: macros.h:321
constants.h
BALL::ball_macro_min3
T BALL_DEPRECATED BALL_INLINE ball_macro_min3(T a, T b, T c)
Definition: macros.h:44
BALL::ball_macro_real_greater_or_equal
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_greater_or_equal(X x, Y y, E e)
Definition: macros.h:116
BALL::ball_macro_bitarray_toggle_bit
void BALL_DEPRECATED BALL_INLINE ball_macro_bitarray_toggle_bit(BitArray *a, X x)
Definition: macros.h:227
BALL::ball_macro_bit_is_set
bool BALL_DEPRECATED BALL_INLINE ball_macro_bit_is_set(Bitset &bitset, Bit bit)
Definition: macros.h:297
BALL::ball_macro_bitarray_is_bit_set
bool BALL_DEPRECATED BALL_INLINE ball_macro_bitarray_is_bit_set(BitArray *a, X x)
Definition: macros.h:235
BALL::Bit
Definition: bitVector.h:43
BALL::ball_macro_bit_is_cleared
bool BALL_DEPRECATED BALL_INLINE ball_macro_bit_is_cleared(Bitset &bitset, Bit bit)
Definition: macros.h:303
BALL::ball_macro_int_even
bool BALL_DEPRECATED BALL_INLINE ball_macro_int_even(X x)
Definition: macros.h:74
BALL::ball_macro_bit_clear
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_clear(Bitset &bitset, Bit bit)
Definition: macros.h:273
BALL::ball_macro_real_odd
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_odd(X x)
Definition: macros.h:128
BALL::ball_macro_angle_radians_to_degree
T BALL_DEPRECATED BALL_INLINE ball_macro_angle_radians_to_degree(T rad_angle)
Definition: macros.h:309
BALL::ball_macro_abs
int BALL_DEPRECATED BALL_INLINE ball_macro_abs(X x)
Definition: macros.h:50
BALL::Constants::PI
BALL_EXTERN_VARIABLE const double PI
PI.
Definition: constants.h:35
BALL::ball_macro_min
T BALL_DEPRECATED BALL_INLINE ball_macro_min(T a, T b)
Definition: macros.h:38
BALL::ball_macro_bitarray_set_bit
void BALL_DEPRECATED BALL_INLINE ball_macro_bitarray_set_bit(BitArray *a, X x)
Definition: macros.h:219
BALL::Constants::E
BALL_EXTERN_VARIABLE const double E
Euler's number - base of the natural logarithm.
Definition: constants.h:38
BALL::ball_macro_real_round_int
int BALL_DEPRECATED BALL_INLINE ball_macro_real_round_int(X x)
Definition: macros.h:152
BALL::ball_macro_real_not_equal
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_not_equal(X x, Y y, E e)
Definition: macros.h:92
BALL::ball_macro_real_less
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_less(X x, Y y, E e)
Definition: macros.h:98
BALL::ball_macro_bit_clear_all_to
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_clear_all_to(Bitset &bitset, Bit bit)
Definition: macros.h:285
BALL::ball_macro_real_abs
X BALL_DEPRECATED BALL_INLINE ball_macro_real_abs(X x)
Definition: macros.h:122
BALL::ball_macro_bit_set_all_to
void BALL_DEPRECATED BALL_INLINE ball_macro_bit_set_all_to(Bitset &bitset, Bit bit)
Definition: macros.h:261
BALL::ball_macro_real_floor
long BALL_DEPRECATED BALL_INLINE ball_macro_real_floor(X x)
Definition: macros.h:140
BALL_DEPRECATED
#define BALL_DEPRECATED
Definition: COMMON/global.h:64
BALL::ball_macro_bit
unsigned BALL_DEPRECATED BALL_INLINE ball_macro_bit(Bit bit)
Definition: macros.h:243
BALL::ball_macro_real_less_or_equal
bool BALL_DEPRECATED BALL_INLINE ball_macro_real_less_or_equal(X x, Y y, E e)
Definition: macros.h:104