base_types.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00013 #ifndef BASE_TYPES_HH
00014 #define BASE_TYPES_HH 1
00015 
00016 #include "base_thread_types.hh"
00017 
00018 // base module is gfxsys-independent except for typedef GLfloat fp.
00019 #include "gfx_gfxsys.hh"
00020 
00021 // Basic types must be fast, prevent them having virtual functions.
00022 #define virtual VIRTUAL_METHODS_IN_BASIC_TYPES_IS_TOO_SLOW
00023 
00025 // Basic types.
00026 //
00027 #if ! BASE_OMIT_BOOL
00028 typedef unsigned char      Bool;  
00029 #endif
00030 typedef unsigned char      uchar;
00031 typedef unsigned int       uint;
00032 //typedef unsigned long    ulong;  // troublesome, so is #define ulong
00033 #if GFXSYS_OPENGL
00034 typedef GLfloat            fp;  // dependency: OpenGL code casts fp as GLfloat
00035 #else
00036 typedef float              fp;
00037 #endif
00038 #if SPEED
00039 typedef fp                 fpx;  // extended-precision float
00040 #else
00041 typedef double             fpx;
00042 #endif
00043 
00044 //------------------------------------------------------------------------------
00045 
00046 namespace base {
00047 
00049 // Exact-size types (limited use).
00050 // int64() cast or "LL" suffix is critical to compile 64-bit shifts correctly.
00051 #if ! NO_STDINT
00052 typedef int8_t             int8;
00053 typedef uint8_t            uint8;
00054 typedef int16_t            int16;
00055 typedef uint16_t           uint16;
00056 typedef int32_t            int32;
00057 typedef uint32_t           uint32;
00058 typedef int64_t            int64;   // C99
00059 typedef uint64_t           uint64;  // C99
00060 #else
00061 typedef char               int8;
00062 typedef unsigned char      uint8;
00063 typedef short              int16;
00064 typedef unsigned short     uint16;
00065 typedef int                int32;
00066 typedef unsigned int       uint32;
00067 typedef long long          int64;   // C99
00068 typedef unsigned long long uint64;  // C99
00069 #endif
00070 
00072 // Limited use: may result in unportable code.
00073 // FYI, on Linux x86-64, int is 32-bits, long is 64-bits.
00074 //
00075 const uint INT_BITS  = sizeof(int)   * 8;
00076 const uint LONG_BITS = sizeof(long)  * 8;
00077 const uint PTR_BITS  = sizeof(void*) * 8;
00078 
00080 class Void { };
00081 
00094 #define DECLARE_DISTINCT_TYPE( CLASS, T )                                                   \
00095 class CLASS                                                                                 \
00096 {                                                                                           \
00097 public:                                                                                     \
00098                 CLASS( void ) : mVal(0) { }  /* clients depends on zero */                  \
00099                 CLASS( T val ) : mVal(val) { }                                              \
00100                 operator T() const { return mVal; }                                         \
00101     CLASS&      operator=( const T& val ) { mVal = val; return *this; }                     \
00102     friend bool operator==( const CLASS& a, const CLASS& b ) { return a.mVal == b.mVal; }   \
00103     friend bool operator!=( const CLASS& a, const CLASS& b ) { return a.mVal != b.mVal; }   \
00104                 /* Arithmetic operators are omitted because of ambiguity. */                \
00105                 /* Rather, cast to fundamental type then do the math. */                    \
00106 private:                                                                                    \
00107     T   mVal;                                                                               \
00108 };
00109 
00110 // Fundamental types auto-initialized zero.
00111 DECLARE_DISTINCT_TYPE( bool0, bool )
00112 DECLARE_DISTINCT_TYPE( int0,  int  )
00113 DECLARE_DISTINCT_TYPE( uint0, uint )
00114 
00122 class Bits
00123 {
00124 public:
00125     Bits( uint bits = 0 ) : mBits(bits) { }
00126     bool    Get( uint idx ) const     { ASSERT(idx < BIT_CNT); return mBits & (1<<idx); }  // get bit (true or false)
00127     void    Set( uint idx )           { ASSERT(idx < BIT_CNT); mBits |= (1<<idx); }        // turn on bit
00128     void    Set( uint idx, bool set ) { if ( set ) Set(idx); else Clear(idx); }            // turn on/off bit
00129     void    Clear( uint idx )         { ASSERT(idx < BIT_CNT); mBits &= ~(1<<idx); }       // turn off bit
00130     Bits&   operator=( uint bits )    { mBits = bits; return *this; }  // assign from uint
00131             operator uint() const     { return mBits; }                // convert to uint
00132 private:
00133     uint                mBits;
00134     CLASS_CONST uint    BIT_CNT = 32;
00135 };
00136 
00158 struct Seconds
00159 {
00160     typedef int64 Int;
00161     Seconds( void )     : mSeconds(0) { }
00162     Seconds( Int secs ) : mSeconds(secs) { }
00163     operator Int() const { return mSeconds; }
00164     friend inline Seconds operator+( const Seconds& a, const Seconds& b ) { return a.mSeconds + b.mSeconds; }
00165     friend inline Seconds operator-( const Seconds& a, const Seconds& b ) { return a.mSeconds - b.mSeconds; }
00166 private:
00167     Int mSeconds;
00168 };
00169 
00171 struct Milliseconds
00172 {
00173     typedef int64 Int;
00174     Milliseconds( void )          : mMilliseconds(0) { }
00175     Milliseconds( Int millisecs ) : mMilliseconds(millisecs) { }
00176     Milliseconds( Seconds secs )  : mMilliseconds(Int(secs)*Int(THOUSAND)) { }
00177     operator Int() const { return mMilliseconds; }
00178     friend inline Milliseconds operator+( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds + b.mMilliseconds; }
00179     friend inline Milliseconds operator-( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds - b.mMilliseconds; }
00180     friend inline Milliseconds operator*( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds * b.mMilliseconds; }
00181     friend inline Milliseconds operator/( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds / b.mMilliseconds; }
00182     friend inline         bool operator<(  const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds <  b.mMilliseconds; }
00183     friend inline         bool operator<=( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds <= b.mMilliseconds; }
00184     friend inline         bool operator>(  const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds >  b.mMilliseconds; }
00185     friend inline         bool operator>=( const Milliseconds& a, const Milliseconds& b ) { return a.mMilliseconds >= b.mMilliseconds; }
00186     Milliseconds& operator+=( const Milliseconds& src ) { mMilliseconds += src.mMilliseconds; return *this; }
00187     Milliseconds& operator-=( const Milliseconds& src ) { mMilliseconds -= src.mMilliseconds; return *this; }
00188 private:
00189     Int mMilliseconds;
00190 };
00191 
00193 struct Microseconds
00194 {
00195     typedef int64 Int;
00196     Microseconds( void )                   : mMicroseconds(0) { }
00197     Microseconds( Int usecs )              : mMicroseconds(usecs) { }
00198     Microseconds( Milliseconds millisecs ) : mMicroseconds(Int(millisecs)*Int(THOUSAND)) { }
00199     Microseconds( Seconds secs )           : mMicroseconds(Int(secs)*Int(MILLION)) { }
00200     operator Int() const { return mMicroseconds; }
00201 private:
00202     Int mMicroseconds;
00203 };
00204 
00205 #undef virtual
00206 
00207 } // namespace base
00208 
00209 #endif // BASE_TYPES_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:07 2007