00001 /*
00002 File: Basics.h
00003
00004 Function: Basic definitions for all files. Contains type definitions,
00005 assertion and debugging facilities, and miscellaneous
00006 useful template functions.
00007
00008 Author(s): Andrew Willmott
00009
00010 Copyright: (c) 1995-2000, Andrew Willmott
00011
00012 Notes: This header is affected by the following defines:
00013
00014 CL_CHECKING - Include code for assertions,
00015 range errors and warnings.
00016 CL_DEBUG - Enables misc. debugging statements.
00017 CL_FLOAT - Use floats for real numbers. (Doubles
00018 are the default.)
00019 CL_NO_BOOL - There is no bool type.
00020 CL_NO_TF - true and false are not predefined.
00021 */
00022
00023 #ifndef __Basics__
00024 #define __Basics__
00025
00026 #include "cl/CLConfig.h"
00027 #include <iostream.h>
00028 #include <math.h>
00029
00030
00031 // --- Basic types -------------------------------------------------------------
00032
00033 typedef void Void;
00034 typedef float Float;
00035 typedef double Double;
00036
00037 typedef int Int;
00038 typedef long Long;
00039
00040 typedef signed int SInt;
00041 typedef signed long SLong;
00042 typedef signed short SInt16;
00043 typedef signed long SInt32;
00044
00045 typedef unsigned int UInt;
00046 typedef unsigned long ULong;
00047 typedef unsigned short UInt16;
00048 typedef unsigned int UInt32;
00049 #ifdef CL_64_BIT
00050 typedef signed long SInt64;
00051 typedef unsigned long UInt64;
00052 #endif
00053
00054 #ifndef CL_64_BIT
00055 typedef signed long SAddrInt;
00056 typedef unsigned long UAddrInt;
00057 #else
00058 typedef signed long long SAddrInt;
00059 typedef unsigned long long UAddrInt;
00060 #endif
00061 // Integral type that is the same size as a pointer.
00062
00063 typedef unsigned char Byte;
00064 typedef unsigned char SByte;
00065 typedef char Char;
00066
00067 #ifndef CL_FLOAT
00068 typedef Double Real;
00069 #else
00070 typedef Float Real;
00071 #endif
00072
00073 #define SELF (*this) // A syntactic convenience.
00074
00075
00076 // --- Boolean type ------------------------------------------------------------
00077
00078 // X11 #defines 'Bool' -- typical.
00079
00080 #ifdef Bool
00081 #undef Bool
00082 #endif
00083
00084 #ifndef CL_NO_BOOL
00085 // if the compiler implements the bool type...
00086 typedef bool Bool;
00087 #else
00088 // if not, make up our own.
00089 class Bool
00090 {
00091 public:
00092
00093 Bool() : val(0) {};
00094 Bool(Int b) : val(b) {};
00095
00096 operator Int() { return val; };
00097
00098 private:
00099 Int val;
00100 };
00101 #ifdef CL_NO_TF
00102 enum {false, true};
00103 #endif
00104 #endif
00105
00106
00107 // --- Assertions and Range checking -------------------------------------------
00108
00109 #define _Error(e) _Assert(false, e, __FILE__, __LINE__)
00110 #define _Warning(w) _Expect(false, w, __FILE__, __LINE__)
00111
00112 #if defined(CL_CHECKING) || defined(VL_CHECKING)
00113 #define Assert(b, e) _Assert(b, e, __FILE__, __LINE__)
00114 // Assert that b is true. e is an error message to be printed if b
00115 // is false.
00116 #define Expect(b, w) _Expect(b, w, __FILE__, __LINE__)
00117 // Prints warning w if b is false
00118 #define CheckRange(i, l, u, r) _CheckRange(i, l, u, r, __FILE__, __LINE__)
00119 // Checks whether i is in the range [lowerBound, upperBound).
00120 #else
00121 #define Assert(b, e)
00122 #define Expect(b, w)
00123 #define CheckRange(a, l, u, r)
00124 #endif
00125
00126 Void _Assert(Int condition, const Char *errorMessage, const Char *file, Int line);
00127 Void _Expect(Int condition, const Char *warningMessage, const Char *file, Int line);
00128 Void _CheckRange(Int i, Int lowerBound, Int upperBound, const Char *rangeMessage,
00129 const Char *file, Int line);
00130
00131
00132 // --- Inlines -----------------------------------------------------------------
00133
00134 template<class Value>
00135 inline Value Min(Value x, Value y)
00136 {
00137 if (x <= y)
00138 return(x);
00139 else
00140 return(y);
00141 };
00142
00143 template<class Value>
00144 inline Value Max(Value x, Value y)
00145 {
00146 if (x >= y)
00147 return(x);
00148 else
00149 return(y);
00150 };
00151
00152 template<class Value>
00153 inline Void Swap(Value &x, Value &y)
00154 {
00155 Value t;
00156
00157 t = x;
00158 x = y;
00159 y = t;
00160 };
00161
00162 template<class Value>
00163 inline Value Mix(Value x, Value y, Real s)
00164 {
00165 return(x + (y - x) * s);
00166 };
00167
00168 template<class Value>
00169 inline Value Clip(Value x, Value min, Value max)
00170 {
00171 if (x < min)
00172 return(min);
00173 else if (x > max)
00174 return(max);
00175 else
00176 return(x);
00177 };
00178
00179 template<class Value>
00180 inline Value sqr(Value x)
00181 {
00182 return(x * x);
00183 };
00184
00185 #endif