OpendTect  6.6
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  RCS: $Id$
10 ________________________________________________________________________
11 
12  Some very commonly used macros.
13 
14 -*/
15 
16 #include "basicmod.h"
17 #include "odversion.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 fT,class eT>
36 inline bool isFPZero( fT v, eT eps ) { return v < eps && v > -eps; }
37 
38 template <class T1,class T2,class eT>
39 inline bool isFPEqual( T1 v1, T2 v2, eT eps ) { return isFPZero(v1-v2,eps); }
40 
41 template <class T>
42 inline T getLimited( T v, T min, T max )
43 { return v<min ? min : (v>max ? max : v); }
44 
45 template <class T>
46 inline void Swap( T& a, T& b ) { std::swap(a,b); }
47 
48 template <class T>
49 inline T* getNonConst( const T* t )
50 { return const_cast<T*>( t ); }
51 
52 template <class T>
53 inline T& getNonConst( const T& t )
54 { return const_cast<T&>( t ); }
55 
56 
57 #define mRounded(typ,x) roundOff<typ>( x )
58 #define mNINT32(x) mRounded( od_int32, x )
59 #define mNINT64(x) mRounded( od_int64, x )
60 
61 #define mMAX(x,y) ( (x)>(y) ? (x) : (y) )
62 #define mMIN(x,y) ( (x)<(y) ? (x) : (y) )
63 #define mMaxLimited(v,lim) ( (v)<(lim) ? (v) : (lim) )
64 #define mMinLimited(v,lim) ( (v)>(lim) ? (v) : (lim) )
65 
66 #define mIsZero(x,eps) isFPZero( x, eps )
67 #define mIsEqual(x,y,eps) isFPEqual( x, y, eps )
68 #define mIsEqualWithUdf(x,y,e) ((mIsUdf(x) && mIsUdf(y)) || mIsEqual(x,y,e) )
69 #define mDefEpsF (1e-10f)
70 #define mDefEpsD (1e-10)
71 #define mDefEps mDefEpsD
72 
73 
74 //--- Math-related constants
75 
76 #ifndef M_PI
77 # define M_PI 3.14159265358979323846
78 #endif
79 #ifndef M_PIl
80 # define M_PIl 3.1415926535897932384626433832795029L
81 #endif
82 #ifndef M_2PI
83 # define M_2PI 6.28318530717958647692
84 #endif
85 #ifndef M_PI_2
86 # define M_PI_2 1.57079632679489661923
87 #endif
88 #ifndef M_PI_4
89 # define M_PI_4 0.78539816339744830962
90 #endif
91 #ifndef M_SQRT2
92 # define M_SQRT2 1.41421356237309504880168872421
93 #endif
94 #ifndef M_SQRT1_2
95 # define M_SQRT1_2 0.70710678118654752440
96 #endif
97 #ifndef M_PIf
98 # define M_PIf 3.14159265358979323846f
99 #endif
100 #ifndef M_2PIf
101 # define M_2PIf 6.28318530717958647692f
102 #endif
103 #ifndef M_PI_2f
104 # define M_PI_2f 1.57079632679489661923f
105 #endif
106 #ifndef M_PI_4f
107 # define M_PI_4f 0.78539816339744830962f
108 #endif
109 #ifndef M_SQRT2f
110 # define M_SQRT2f 1.41421356237309504880168872421f
111 #endif
112 #ifndef M_SQRT1_2f
113 # define M_SQRT1_2f 0.70710678118654752440f
114 #endif
115 
116 #ifndef MAXFLOAT
117 # define MAXFLOAT 3.4028234663852886e+38F
118 #endif
119 #ifndef MAXDOUBLE
120 # define MAXDOUBLE 1.7976931348623157e+308
121 #endif
122 
123 #define mFromFeetFactorF 0.3048f
124 #define mFromFeetFactorD 0.3048
125 #define mToFeetFactorF 3.2808399f
126 #define mToFeetFactorD 3.28083989501312336
127 #define mToPercent(f) (mIsUdf(f) ? f : f*100)
128 #define mFromPercent(f) (mIsUdf(f) ? f : f*0.01)
129 #define mDeg2RadD 0.017453292519943292
130 #define mRad2DegD 57.295779513082323
131 #define mDeg2RadF 0.017453292519943292f
132 #define mRad2DegF 57.295779513082323f
133 
134 
135 //--- C++ tools
136 
137 #define mCast(tp,v) ((tp)(v))
138 #define cCast(tp,v) const_cast< tp >( v )
139 #define dCast(tp,v) dynamic_cast< tp >( v )
140 #define rCast(tp,v) reinterpret_cast< tp >( v )
141 #define sCast(tp,v) static_cast< tp >( v )
142 
143 #define mNonConst(x) getNonConst( x )
144 #define mSelf() mNonConst( *this )
145 
146 #define mUseType(scope,typ) typedef scope::typ typ
147 # define mDynamicCast(typ,out,in) out = dynamic_cast< typ >( in );
148 # define mDynamicCastGet(typ,out,in) typ mDynamicCast(typ,out,in)
149 
150 #define mDefSetupClssMemb(clss,typ,memb) \
151  typ memb##_; \
152  clss& memb( typ val ) { memb##_ = val; return *this; }
153 
154 #define mDefSetupMemb(typ,memb) mDefSetupClssMemb(Setup,typ,memb)
155 
156 
157 
158 //--- Covering Windows problems, mainly DLL export/import stuff
159 
160 #ifdef __win__
161 # include <stdio.h>
162 # undef small
163 # ifndef __func__
164 # define __func__ __FUNCTION__
165 # endif
166 #endif
167 
168 #ifdef __msvc__
169 # include "msvcdefs.h"
170 #else
171 # define dll_export
172 # define dll_import
173 # define mMaxFilePathLength 255
174 #endif
175 
176 #define mExp( module ) Export_##module
177 #define mExpClass( module ) class mExp( module )
178 #define mExpStruct( module ) struct mExp( module )
179 
180 #define mGlobal( module ) mExp( module )
181 #define mClass( module ) class
182 #define mStruct( module ) mExpStruct( module )
183 #define mExtern( module ) extern mExp( module )
184 #define mExternC( module) extern "C" mExp( module )
185 
186 #define mExportInst( mod, tp ) Extern_##mod tp mExp(mod)
187 #define mExportTemplClassInst(mod) mExportInst(mod,template class)
188 
189 
190 //--- Local static variable initialization. This is an MT Windows problem.
191 
192 #ifdef __win__
193 namespace Threads
194 {
195  mGlobal(Basic) bool lockSimpleSpinWaitLock(volatile int& lock);
196  mGlobal(Basic) void unlockSimpleSpinLock(volatile int& lock);
197 }
198 #endif
199 
200 #define mLockStaticInitLock( nm )
201 #define mUnlockStaticInitLock( nm )
202 
203 #define mDefineStaticLocalObject( type, var, init ) \
204 static type var init;
205 
206 
207 //--- Single-shot initialization support
208 
209 namespace Threads
210 {
211 #if mODVersion < 700
212  mGlobal(Basic) bool atomicSetIfValueIs(volatile int&,int,int,int* );
213  //Force developer to use three arguments
214 #else
215  mGlobal(Basic) bool atomicSetIfValueIs(volatile int&,int,int,int* = 0 );
216 #endif
217 }
218 
220 
221 #define mIfNotFirstTime(act) \
222 { \
223  static volatile int _already_visited_ = 0; \
224  if ( !Threads::atomicSetIfValueIs( _already_visited_, \
225  0, 1, 0 ) ) \
226  act; \
227 }
228 
229 
230 //--- Qt class and namespace handling
231 
232 #ifndef QT_NAMESPACE
233 # define mFDQtclass(cls) class cls;
234 # define mQtclass(cls) ::cls
235 # define mUseQtnamespace
236 #else
237 # define mFDQtclass(cls) namespace QT_NAMESPACE { class cls; }
238 # define mQtclass(cls) ::QT_NAMESPACE::cls
239 # define mUseQtnamespace using namespace ::QT_NAMESPACE;
240 #endif
241 
242 
243 //--- Large array allocation
244 
246 #define mTryAlloc(var,stmt) \
247 { try { var = new stmt; } catch ( std::bad_alloc ) { var = nullptr; } }
248 
249 #define mTryAllocPtrMan(var,stmt) \
250 { try { var = new stmt; } catch ( std::bad_alloc ) { var.set( nullptr ); } }
251 
253 #define mDeclareAndTryAlloc(tp,var,stmt) \
254  tp var; \
255  mTryAlloc(var,stmt)
256 
258 #define mGetIdxArr(tp,var,sz) \
259  tp* var; \
260  mTryAlloc(var,tp [sz]) \
261  if ( var ) \
262  for ( tp idx=0; idx<sz; idx++ ) \
263  var[idx] = idx
264 
265 
266 
285 #define mDoArrayPtrOperation( type, arr, operation, arrsz, ptrinc ) \
286 { \
287  type* __curptr = arr; \
288  for ( const type* __stopptr = __curptr + arrsz; \
289  __curptr!=__stopptr; \
290  __curptr ptrinc ) \
291  { \
292  *__curptr operation; \
293  } \
294 }
295 
296 
297 #if defined __win32__
298 #define mMaxContiguousMemSize 0x20000000 // 512 MB (OS limit)
299 #elif defined __win64__
300 #define mMaxContiguousMemSize 0x800000000 // 32 GB (OS limit)
301 #elif defined __mac__
302 #define mMaxContiguousMemSize 0x800000000 // 32 GB (arbitrary)
303 #else
304 #define mMaxContiguousMemSize 0x4000000000 // 256 GB (arbitrary)
305 #endif
306 
307 
308 // C library functions
309 #ifdef __msvc__
310 # define od_sprintf sprintf_s
311 # define od_sscanf sscanf_s
312 #else
313 # define od_sprintf snprintf
314 # define od_sscanf sscanf
315 #endif
Swap
void Swap(T &a, T &b)
Definition: commondefs.h:46
Threads::unlockSimpleSpinLock
void unlockSimpleSpinLock(volatile int &lock)
mGlobal
#define mGlobal(module)
Definition: commondefs.h:180
isFPEqual
bool isFPEqual(T1 v1, T2 v2, eT eps)
Definition: commondefs.h:39
roundOff
RT roundOff(T x)
Definition: commondefs.h:27
getLimited
T getLimited(T v, T min, T max)
Definition: commondefs.h:42
msvcdefs.h
Strat::RT
const RefTree & RT()
Threads::atomicSetIfValueIs
bool atomicSetIfValueIs(volatile int &val, int curval, int newval, int *actualvalptr)
plfdefs.h
swap
void swap(BufferStringSet &o1, BufferStringSet &o2)
Definition: bufstringset.h:154
Threads
interface to threads that should be portable.
Definition: atomic.h:23
isFPZero
bool isFPZero(fT v, eT eps)
Definition: commondefs.h:36
getNonConst
T * getNonConst(const T *t)
Definition: commondefs.h:49
Threads::lockSimpleSpinWaitLock
bool lockSimpleSpinWaitLock(volatile int &lock)

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