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