OpendTect-6_4  6.4
commondefs.h
Go to the documentation of this file.
1 #ifndef commondefs_h
2 #define commondefs_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Bril
9  Date: Mar 2006
10  RCS: $Id$
11 ________________________________________________________________________
12 
13  Some very commonly used macros.
14 
15 -*/
16 
17 #include "basicmod.h"
18 #include "plfdefs.h"
19 
20 #include <stdexcept>
21 #include <new>
22 
23 
24 //--- FP rounding and equality
25 
26 template <class RT,class T>
27 inline RT roundOff( T x ) { return (RT)(x); }
28 
29 template <class RT>
30 inline RT roundOff( double x ) { return (RT) ((x)>0 ? (x)+.5 : (x)-.5); }
31 
32 template <class RT>
33 inline RT roundOff( float x ) { return (RT) ((x)>0 ? (x)+.5f : (x)-.5f); }
34 
35 template <class T>
36 inline void Swap( T& a, T& b ) { T tmp = a; a = b; b = tmp; }
37 
38 template <class fT,class eT>
39 inline bool isFPZero( fT v, eT eps ) { return v < eps && v > -eps; }
40 
41 template <class T1,class T2,class eT>
42 inline bool isFPEqual( T1 v1, T2 v2, eT eps ) { return isFPZero(v1-v2,eps); }
43 
44 #define mRounded(typ,x) roundOff<typ>( x )
45 #define mNINT32(x) mRounded( od_int32, x )
46 #define mNINT64(x) mRounded( od_int64, x )
47 
48 #define mMAX(x,y) ( (x)>(y) ? (x) : (y) )
49 #define mMIN(x,y) ( (x)<(y) ? (x) : (y) )
50 #define mMaxLimited(v,lim) ( (v)<(lim) ? (v) : (lim) )
51 #define mMinLimited(v,lim) ( (v)>(lim) ? (v) : (lim) )
52 
53 #define mIsZero(x,eps) isFPZero( x, eps )
54 #define mIsEqual(x,y,eps) isFPEqual( x, y, eps )
55 #define mIsEqualWithUdf(x,y,e) ((mIsUdf(x) && mIsUdf(y)) || mIsEqual(x,y,e) )
56 #define mDefEpsF (1e-10f)
57 #define mDefEpsD (1e-10)
58 #define mDefEps mDefEpsD
59 
60 
61 //--- Math-related constants
62 
63 #ifndef M_PI
64 # define M_PI 3.14159265358979323846
65 #endif
66 #ifndef M_PIl
67 # define M_PIl 3.1415926535897932384626433832795029L
68 #endif
69 #ifndef M_2PI
70 # define M_2PI 6.28318530717958647692
71 #endif
72 #ifndef M_PI_2
73 # define M_PI_2 1.57079632679489661923
74 #endif
75 #ifndef M_PI_4
76 # define M_PI_4 0.78539816339744830962
77 #endif
78 #ifndef M_SQRT2
79 # define M_SQRT2 1.41421356237309504880168872421
80 #endif
81 #ifndef M_SQRT1_2
82 # define M_SQRT1_2 0.70710678118654752440
83 #endif
84 #ifndef M_PIf
85 # define M_PIf 3.14159265358979323846f
86 #endif
87 #ifndef M_2PIf
88 # define M_2PIf 6.28318530717958647692f
89 #endif
90 #ifndef M_PI_2f
91 # define M_PI_2f 1.57079632679489661923f
92 #endif
93 #ifndef M_PI_4f
94 # define M_PI_4f 0.78539816339744830962f
95 #endif
96 #ifndef M_SQRT2f
97 # define M_SQRT2f 1.41421356237309504880168872421f
98 #endif
99 #ifndef M_SQRT1_2f
100 # define M_SQRT1_2f 0.70710678118654752440f
101 #endif
102 
103 #ifndef MAXFLOAT
104 # define MAXFLOAT 3.4028234663852886e+38F
105 #endif
106 #ifndef MAXDOUBLE
107 # define MAXDOUBLE 1.7976931348623157e+308
108 #endif
109 
110 #define mFromFeetFactorF 0.3048f
111 #define mFromFeetFactorD 0.3048
112 #define mToFeetFactorF 3.2808399f
113 #define mToFeetFactorD 3.28083989501312336
114 #define mToPercent(f) (mIsUdf(f) ? f : f*100)
115 #define mFromPercent(f) (mIsUdf(f) ? f : f*0.01)
116 #define mDeg2RadD 0.017453292519943292
117 #define mRad2DegD 57.295779513082323
118 #define mDeg2RadF 0.017453292519943292f
119 #define mRad2DegF 57.295779513082323f
120 
121 
122 //--- C++ tools
123 
124 #define mCast(tp,v) ((tp)(v))
125 #define cCast(tp,v) const_cast< tp >( v )
126 #define dCast(tp,v) dynamic_cast< tp >( v )
127 #define rCast(tp,v) reinterpret_cast< tp >( v )
128 #define sCast(tp,v) static_cast< tp >( v )
129 
130 # define mDynamicCast(typ,out,in) out = dynamic_cast< typ >( in );
131 # define mDynamicCastGet(typ,out,in) typ mDynamicCast(typ,out,in)
132 
133 #define mDefSetupClssMemb(clss,typ,memb) \
134  typ memb##_; \
135  clss& memb( typ val ) { memb##_ = val; return *this; }
136 
137 #define mDefSetupMemb(typ,memb) mDefSetupClssMemb(Setup,typ,memb)
138 
139 
140 
141 //--- Covering Windows problems, mainly DLL export/import stuff
142 
143 #ifdef __win__
144 # include <stdio.h>
145 # undef small
146 # ifndef __func__
147 # define __func__ __FUNCTION__
148 # endif
149 #endif
150 
151 #ifdef __msvc__
152 # include "msvcdefs.h"
153 #else
154 # define dll_export
155 # define dll_import
156 # define mMaxFilePathLength 255
157 #endif
158 
159 #define mExp( module ) Export_##module
160 #define mExpClass( module ) class mExp( module )
161 #define mExpStruct( module ) struct mExp( module )
162 
163 #define mGlobal( module ) mExp( module )
164 #define mClass( module ) class
165 #define mStruct( module ) mExpStruct( module )
166 #define mExtern( module ) extern mExp( module )
167 #define mExternC( module) extern "C" mExp( module )
168 
169 #define mExportInst( mod, tp ) Extern_##mod tp mExp(mod)
170 #define mExportTemplClassInst(mod) mExportInst(mod,template class)
171 
172 
173 //--- Local static variable initialization. This is an MT Windows problem.
174 
175 #ifdef __win__
176 namespace Threads
177 {
178  mGlobal(Basic) bool lockSimpleSpinWaitLock(volatile int& lock);
179  mGlobal(Basic) void unlockSimpleSpinLock(volatile int& lock);
180 }
181 
182 #define mLockStaticInitLock( nm ) \
183 static volatile int nm = 0; \
184 Threads::lockSimpleSpinWaitLock( nm )
185 
186 #define mUnlockStaticInitLock( nm ) \
187 Threads::unlockSimpleSpinLock( nm )
188 
189 #else
190 
191 #define mLockStaticInitLock( nm )
192 #define mUnlockStaticInitLock( nm )
193 
194 #endif
195 
196 #define mDefineStaticLocalObject( type, var, init ) \
197 mLockStaticInitLock( static##var##lck__ ); \
198 static type var init; \
199 mUnlockStaticInitLock( static##var##lck__ )
200 
201 
202 //--- Single-shot initialization support
203 
204 namespace Threads
205 {
206 #if mODVersion < 700
207  mGlobal(Basic) bool atomicSetIfValueIs(volatile int&,int,int,int* );
208  //Force developer to use three arguments
209 #else
210  mGlobal(Basic) bool atomicSetIfValueIs(volatile int&,int,int,int* = 0 );
211 #endif
212 }
213 
215 
216 #define mIfNotFirstTime(act) \
217 { \
218  static volatile int _already_visited_ = 0; \
219  if ( !Threads::atomicSetIfValueIs( _already_visited_, \
220  0, 1, 0 ) ) \
221  act; \
222 }
223 
224 
225 //--- Qt class and namespace handling
226 
227 #ifndef QT_NAMESPACE
228 # define mFDQtclass(cls) class cls;
229 # define mQtclass(cls) ::cls
230 # define mUseQtnamespace
231 #else
232 # define mFDQtclass(cls) namespace QT_NAMESPACE { class cls; }
233 # define mQtclass(cls) ::QT_NAMESPACE::cls
234 # define mUseQtnamespace using namespace ::QT_NAMESPACE;
235 #endif
236 
237 
238 //--- Large array allocation
239 
241 #define mTryAlloc(var,stmt) \
242 { try { var = new stmt; } catch ( std::bad_alloc ) { var = 0; } }
243 
244 #define mTryAllocPtrMan(var,stmt) \
245 { try { var = new stmt; } catch ( std::bad_alloc ) { var.set( 0 ); } }
246 
248 #define mDeclareAndTryAlloc(tp,var,stmt) \
249  tp var; \
250  mTryAlloc(var,stmt)
251 
253 #define mGetIdxArr(tp,var,sz) \
254  tp* var; \
255  mTryAlloc(var,tp [sz]) \
256  if ( var ) \
257  for ( tp idx=0; idx<sz; idx++ ) \
258  var[idx] = idx
259 
260 
261 
280 #define mDoArrayPtrOperation( type, arr, operation, arrsz, ptrinc ) \
281 { \
282  type* __curptr = arr; \
283  for ( const type* __stopptr = __curptr + arrsz; \
284  __curptr!=__stopptr; \
285  __curptr ptrinc ) \
286  { \
287  *__curptr operation; \
288  } \
289 }
290 
291 
292 #endif
#define mGlobal(module)
Definition: commondefs.h:163
bool isFPZero(fT v, eT eps)
Definition: commondefs.h:39
interface to threads that should be portable.
Definition: atomic.h:28
bool isFPEqual(T1 v1, T2 v2, eT eps)
Definition: commondefs.h:42
RT roundOff(T x)
Definition: commondefs.h:27
bool atomicSetIfValueIs(volatile int &val, int curval, int newval, int *actualvalptr)
Definition: atomic.h:99
void Swap(T &a, T &b)
Definition: commondefs.h:36
const RefTree & RT()
void unlockSimpleSpinLock(volatile int &lock)
bool lockSimpleSpinWaitLock(volatile int &lock)

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019