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