macros.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: macros.h,v 1.9 2003/08/26 08:04:05 oliver Exp $
00005 //
00006 
00007 #ifndef BALL_COMMON_MACROS_H
00008 #define BALL_COMMON_MACROS_H
00009 
00010 #ifndef BALL_CONFIG_CONFIG_H
00011 # include <BALL/CONFIG/config.h>
00012 #endif
00013 
00014 #ifndef BALL_COMMON_CONSTANTS_H
00015 # include <BALL/COMMON/constants.h>
00016 #endif
00017 
00018 #ifndef BALL_COMMON_RTTI_H
00019 # include <BALL/COMMON/rtti.h>
00020 #endif
00021 
00022 #include <math.h>     // needed for fabs
00023 #include <typeinfo>   // needed for typeid
00024 
00025 #define BALL_MAX(a, b)                           (((a) < (b)) ? (b) : (a))
00026 #define BALL_MAX3(x, y, z)                       ((x) > (y) ? BALL_MAX(x, z) : BALL_MAX(y, z))
00027 #define BALL_MIN(a, b)                           (((a) > (b)) ? (b) : (a))
00028 #define BALL_MIN3(x, y, z)                       ((x) < (y) ? BALL_MIN(x, z) : BALL_MIN(y, z))
00029 #define BALL_ABS(x)                              (((x) >= 0) ? (x) : -(x))
00030 #define BALL_SGN(x)                              (((x) < 0) ? -1 : ((x) == 0) ? 0 : 1)
00031 #define BALL_ODD(x)                              (((x) % 2)!=0)
00032 
00033 #define BALL_INT_ODD(x)                          (((x) & 0x1) == 1)
00034 #define BALL_INT_EVEN(x)                         (((x) & 0x1) == 0)
00035 
00036 #define BALL_REAL_ROUND                          BALL::Maths::round
00037 #define BALL_REAL_EQUAL(x, y, e)                 (fabs((x) - (y)) <= e)
00038 #define BALL_REAL_NOT_EQUAL(x, y, e)             (fabs((x) - (y)) > e)
00039 #define BALL_REAL_LESS(x, y, e)                  (((x) - (y)) < -e)
00040 #define BALL_REAL_LESS_OR_EQUAL(x, y, e)         (((x) - (y)) <= e)
00041 #define BALL_REAL_GREATER(x, y, e)               (((x) - (y)) > e)
00042 #define BALL_REAL_GREATER_OR_EQUAL(x, y, e)      (((x) - (y)) >= -e)
00043 #define BALL_REAL_ABS(x)                         fabs(x)
00044 #define BALL_REAL_SGN(x)                         BALL_SGN(x)
00045 #define BALL_REAL_ODD(x)                         (((x) % 2) != 0)
00046 #define BALL_REAL_EVEN(x)                        (((x) % 2) == 0)
00047 #define BALL_REAL_FLOOR(x)                       (long)((x) > 0 ? (x): ((x) == (long)(x) ? (x) : (x) - 1))
00048 #define BALL_REAL_CEILING(x)                     (long)((x) < 0 ? (x): ((x) == (long)(x) ? (x) : (x) + 1))
00049 #define BALL_REAL_ROUND_INT(x)                   ((x) > 0 ? (int)(x + 0.5) : -(int)(0.5 - x))
00050 
00051 
00052 // The following macros assume BALL_CHAR_BITS is one of either 8, 16, or 32
00053 #define BALL_CHAR_BITS                           BALL_CHAR_SIZE * 8
00054 #define BALL_CHAR_MASK                           BALL_CHAR_BITS - 1
00055 #define BALL_CHAR_SHIFT                          ((BALL_CHAR_BITS == 8) ? 3 : (BALL_CHAR_BITS == 16) ? 4 : 5)
00056 #define BALL_CHAR_ALL_BITS_SET                   ((BALL_CHAR_BITS == 8) ? 0xFF : (BALL_CHAR_BITS == 16) ? 0xFFFF : 0xFFFFFFFF)
00057 #define BALL_CHAR_ALL_BITS_CLEARED               ((BALL_CHAR_BITS == 8) ? 0x00 : (BALL_CHAR_BITS == 16) ? 0x0000 : 0x00000000)
00058 #define BALL_NUMBER_OF_BYTES(bits)               (((bits) + BALL_CHAR_MASK) >> BALL_CHAR_SHIFT)
00059 
00060 #define BALL_SIZEOF_ARRAY(a)                     (sizeof(a) / sizeof(*(a)))
00061 
00062 #define BALL_BITARRAY_SIZE(number_of_bits)       (((number_of_bits - 1) >> BALL_CHAR_SHIFT) + 1)
00063 #define BALL_BITARRAY_CLEAR_BIT(array, x)        ((array)[(x) >> BALL_CHAR_SHIFT] &= ~(1 << ((x) & BALL_CHAR_MASK)))
00064 #define BALL_BITARRAY_SET_BIT(array, x)          ((array)[(x) >> BALL_CHAR_SHIFT] |=  (1 << ((x) & BALL_CHAR_MASK)))
00065 #define BALL_BITARRAY_TOGGLE_BIT(array, x)       ((array)[(x) >> BALL_CHAR_SHIFT] ^=  (1 << ((x) & BALL_CHAR_MASK)))
00066 #define BALL_BITARRAY_IS_BIT_SET(array, x)       (((array)[(x) >> BALL_CHAR_SHIFT] &   (1 << ((x) & BALL_CHAR_MASK))) != 0)
00067 
00068 #define BALL_BIT(bit)                            (1 << (bit))
00069 #define BALL_BIT_SET(bitset, bit)                ((bitset) |= (1 << (bit)))
00070 #define BALL_BIT_SET_ALL(bitset)                 ((bitset) = -1)
00071 #define BALL_BIT_SET_ALL_TO(bitset, bit)         ((bitset) |= ~(-1 << (bit + 1)))
00072 #define BALL_BIT_SET_ALL_FROM(bitset, bit)       ((bitset) |= (-1 << (bit)))
00073 #define BALL_BIT_CLEAR(bitset, bit)              ((bitset) &= ~(1 << (bit)))
00074 #define BALL_BIT_CLEAR_ALL(bitset)               ((bitset) = 0)
00075 #define BALL_BIT_CLEAR_ALL_TO(bitset, bit)       ((bitset) &= (-1 << (bit + 1)))
00076 #define BALL_BIT_CLEAR_ALL_FROM(bitset, bit)     ((bitset) &= ~(-1 << (bit)))
00077 #define BALL_BIT_IS_SET(bitset, bit)             ((bitset) & (1 << (bit)))
00078 #define BALL_BIT_IS_CLEARED(bitset, bit)         !((bitset) & (1 << (bit)))
00079 
00080 #define BALL_ANGLE_RADIAN_TO_DEGREE(rad_angle)   (180.0 / ::BALL::Constants::PI * (rad_angle))
00081 #define BALL_ANGLE_DEGREE_TO_RADIAN(deg_angle)   (::BALL::Constants::PI / 180.0 * (deg_angle))
00082 
00083 #define BALL_OFFSET_OF(struct_name, struct_var_name)   ((long)&(((struct_name*)0)->struct_var_name))
00084   
00085 #define BALL_DUMP_DEPTH(os, depth)               for (dump_indent_depth_ = 0; dump_indent_depth_ < depth; ++dump_indent_depth_) { os << "    "; }
00086 #define BALL_DUMP_STREAM_PREFIX(os)              Size dump_indent_depth_ = 0;
00087 
00088 #define BALL_DUMP_HEADER(os,cl,ob)               os << "Object: " << (void *)ob << " is instance of class: " << streamClassName(typeid(*ob)) << std::endl;
00089 #define BALL_DUMP_CLASS_HEADER(os,cl,ob)         os << "Object: " << (void *)ob << " is instance of class: " << #cl << ::std::endl;
00090 #define BALL_DUMP_STREAM_SUFFIX(os)              
00091 
00092 #endif // BALL_COMMON_MACROS_H