Overture  Version 25
kk_defines.hh
Go to the documentation of this file.
1 #ifndef __KK_DEFINES_HH__
2 #define __KK_DEFINES_HH__
3 
8 #include <ctime>
9 #include <cstdlib>
10 #include <limits.h>
11 #include <float.h>
12 #include <exception>
13 #include <string>
14 
18 #undef RANGE_CHK
19 #undef KK_DBG_EXEC
20 #ifdef KK_DEBUG
21 
23 #define RANGE_CHK(x,base,bound) if ( x<base || x>bound ) throw KK::RangeErr();
24 #define KK_DBG_EXEC(x) x;
25 
26 #else
27 
29 #define RANGE_CHK(x,base,bound)
30 #define KK_DBG_EXEC(x) ;
31 
32 #endif
33 
35 #ifndef NULL
36 const int NULL = 0;
37 #endif
38 
40 #ifndef EXPLICIT
41 #define EXPLICIT explicit
42 #endif
43 
45 #ifndef RESTRICT
46 #define RESTRICT __restrict__
47 #endif
48 
50 #ifndef RESTRICT_THIS
51 #define RESTRICT_THIS RESTRICT
52 #endif
53 
57 
58 namespace KK {
59 
60 #ifndef real
61 
62  typedef double real;
63 #endif
64 
65 #ifndef REAL_MAX
66 
67  const real REAL_MAX = DBL_MAX;
68 #endif
69 #ifndef REAL_MIN
70 
71  const real REAL_MIN = DBL_MIN;
72 #endif
73 #ifndef REAL_EPSILON
74 
75  const real REAL_EPSILON = DBL_EPSILON;
76 #endif
77 
79  class Err : public std::exception
80  {
81 
82  public:
84  Err() : msg("") { }
86  Err(std::string s) : msg(s) { }
87 
89  virtual ~Err() throw() {}
91  virtual std::string repr() const { return msg ; }
93  std::string msg;
94 
95  };
96 
103  template < class E, class B >
104  inline
105  void
106  Assert ( B assertion, std::string s = "" )
107  {
108 #ifdef KK_DEBUG
109  if ( ! assertion )
110  {
111  E e;
112  e.msg += s;
113 #ifdef KK_ABORTASSERT
114  abort();
115 #else
116  throw e;
117 #endif
118  }
119 #endif
120  }
121 
128 
129  template < class E, class B >
130  inline
131  void
132  Assert ( B assertion, E exception)
133  {
134 #ifdef KK_DEBUG
135 #ifdef KK_ABORTASSERT
136  if ( !assertion ) abort();
137 #else
138  if ( ! assertion ) throw exception;
139 #endif
140 #endif
141  }
142 
149  template < class E, class B >
150  inline
151  void
152  AssertAlways ( B assertion, std::string s = "" )
153  {
154  if ( ! assertion )
155  {
156  E e;
157  e.msg += s;
158 #ifdef KK_ABORTASSERT
159  abort();
160 #else
161  throw e;
162 #endif
163  }
164  }
165 
172 
173  template < class E, class B >
174  inline
175  void
176  AssertAlways ( B assertion, E exception)
177  {
178 #ifdef KK_ABORTASSERT
179  if ( !assertion ) abort();
180 #else
181  if ( ! assertion ) throw exception;
182 #endif
183  }
184 
186  class RangeErr : public Err
187  {
188  public:
189  RangeErr( std::string s="" ) : Err(s) { }
190  };
191 
193  inline real getcpu() { return real(std::clock())/real(CLOCKS_PER_SEC); }
194 
196  inline real getrandom()
197  {
198  return real((double(RAND_MAX)-std::rand())/double(RAND_MAX));
199  }
200 
202  inline int round(const real r)
203  { return int( r > 0. ? int(r+.5) : int(r-.5) ); }
204 
205 }
206 
207 #endif