Bart Janssens / SVL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Basics.h Source File

Basics.h

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