mksqlite  2.5
A MATLAB interface to SQLite
global.hpp
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #ifndef _GLOBAL_H
20 #define _GLOBAL_H
21 #endif
22 
23 /* Common global definitions */
24 
25 #if defined( MATLAB_MEX_FILE ) /* MATLAB MEX file */
26  /* solve the 'error C2371: 'char16_t' : redefinition; different basic types' problem */
27  /* ref: http://www.mathworks.com/matlabcentral/newsreader/view_thread/281754 */
28  /* ref: http://connect.microsoft.com/VisualStudio/feedback/details/498952/vs2010-iostream-is-incompatible-with-matlab-matrix-h */
29  #ifdef _WIN32
30  #include <yvals.h>
31  #if (_MSC_VER >= 1600)
32  #define __STDC_UTF_16__
33  #endif
34 /* Following solution doesn't work with Matlab R2011b, MSVC2010, Win7 64bit
35  #ifdef _CHAR16T
36  #define CHAR16_T
37  #endif
38 */
39  #endif
40  #include "mex.h"
41 #endif
42 
43 #ifdef _WIN32
44  #define WIN32_LEAN_AND_MEAN
45  #define STRICT
46  #define NOMINMAX
47  #include <windows.h>
48  #undef min
49  #undef max
50 #else // linux
51  #include <string.h>
52  #include <ctype.h>
53  #define _strcmpi strcasecmp
54  #define _strnicmp strncasecmp
55  #define _snprintf snprintf
56  #define _vsnprintf vsnprintf
57  #define _strdup strdup
58  #define _copysign copysign
59 #endif
60 
61 #include "config.h"
62 #include <cstddef>
63 #include <cmath>
64 #include <cassert>
65 #include <climits>
66 
67 // Patch for Mac:
68 // Tested on Mac OSX 10.9.2, Malab R2014a, 64 bit (Stefan Balke)
69 #if defined(__APPLE__) || defined(TARGET_OS_X)
70  // todo: Which header is needed on mac?
71  //#include <tr1/cstdint>
72  #include <cstdint>
73 #else
74  #include <cstdint>
75 #endif
76 
109 // define standard memory handlers used by mksqlite
110 #undef MEM_ALLOC
111 #undef MEM_REALLOC
112 #undef MEM_FREE
113 
114 #if 0
115  // mxCalloc() and mxFree() are extremely slow!
116  #define MEM_ALLOC( count, bytes ) ((void*)mxMalloc( (count) * (bytes) ))
117  #define MEM_FREE( ptr ) mxFree( (void*)ptr )
118  #define MEM_REALLOC( ptr, bytes ) ((void*)mxRealloc( (void*)ptr, bytes ))
119 #else
120  // Global memory allocator
121  #define MEM_ALLOC( count, bytes ) ( (void*)new char[(count) * (bytes)] )
122  // Global memory deallocator
123  #define MEM_FREE( ptr ) ( delete[] ptr )
124  // Global memory deallocator
125  #define MEM_REALLOC( ptr, size ) HC_ASSERT_ERROR
126 #endif
127 
128 // definition how matrix arrays are managed
129 #if 1 && defined( MATLAB_MEX_FILE )
130  #define MAT_ARRAY_TYPE mxArray
131  #define MAT_ALLOC( m, n, typeID ) mxCreateNumericMatrix( m, n, typeID, mxREAL )
132  #define MAT_FREE( ptr ) mxDestroyArray( ptr )
133 #else
134  // \todo not supported yet
135  struct tagNumericArray;
136  #define MAT_ARRAY_TYPE tagNumericArray
137  #define MAT_ALLOC( m, n, typeID ) tagNumericArray::Create( m, n, typeID )
138  #define MAT_FREE( ptr ) tagNumericArray::FreeArray( ptr )
139 #endif
140 
146 #if CONFIG_USE_HEAP_CHECK
147  // redefine memory (de-)allocators, if heap checking is on
148  // memory macros MEM_ALLOC, MEM_REALLOC and MEM_FREE were used by heap_check.hpp
149  #include "heap_check.hpp"
150 
151  // Now redirect memory macros to heap checking functions
152  #undef MEM_ALLOC
153  #undef MEM_REALLOC
154  #undef MEM_FREE
155 
156  #define MEM_ALLOC(n,s) (HeapCheck.New( (n) * (s), __FILE__, __FUNCTION__, /*notes*/ "", __LINE__ ))
157  #define MEM_REALLOC(p,s) (HeapCheck.Realloc( (void*)(p), (s), __FILE__, __FUNCTION__, /*notes*/ "", __LINE__ ))
158  #define MEM_FREE(p) (HeapCheck.Free( (void*)(p) ))
159  #define WALKHEAP (HeapCheck.Walk())
160  #define FREEHEAP (HeapCheck.Release())
161 #else
162  #define HC_COMP_ASSERT(exp)
163  #define HC_ASSERT(exp)
164  #define HC_ASSERT_ERROR
165  #define HC_NOTES(ptr,notes)
166  #define WALKHEAP
167  #define FREEHEAP
168 #endif // CONFIG_USE_HEAP_CHECK
169 
170 
176 #if defined( MATLAB_MEX_FILE )
177  #define DBL_ISFINITE mxIsFinite
178  #define DBL_ISINF mxIsInf
179  #define DBL_ISNAN mxIsNaN
180  #define DBL_INF mxGetInf()
181  #define DBL_NAN mxGetNaN()
182  #define DBL_EPS mxGetEps()
183 #else
184  #if defined( _WIN32 )
185  // MSVC2010
186  #define DBL_INF (_Inf._Double) /* <ymath.h> -?-/double */
187  #define DBL_NAN (_Nan._Double) /* <ymath.h> -?-/double */
188 // #define DBL_EPS DBL_EPS
189  #define DBL_ISFINITE(x) _finite(x) /* <float.h> -?-/double */
190  #define DBL_ISNAN(x) _isnan(x) /* <float.h> -?-/double */
191  #define DBL_ISINF(x) (!DBL_ISFINITE(x) && !DBL_ISNAN(x))
192  #else
193  // gcc
194  #define DBL_INF INFINITY /* <cmath> implementation defined */
195  #define DBL_NAN NAN /* <cmath> implementation defined */
196 // #define DBL_EPS DBL_EPS
197  #if 0
198  #define DBL_ISFINITE(x) (((x)-(x)) == 0.0)
199  #define DBL_ISNAN(x) ((x)!=(x))
200  #define DBL_ISINF(x) (!DBL_ISFINITE(x) && !DBL_ISNAN(x))
201  #else
202  #define DBL_ISFINITE(x) isfinite(x) /* <cmath> float/double */
203  #define DBL_ISNAN(x) isnan(x) /* <cmath> float/double */
204  #define DBL_ISINF(x) isinf(x) /* <cmath> float/double */
205  #endif
206  #endif
207 #endif
208 
219 #define SQLITE_VERSION_STRING SQLITE_VERSION
220 #define DEELX_VERSION_STRING "1.3"
221 
223 /* early bind of serializing functions (earlier MATLAB versions only) */
224 #if defined( MATLAB_MEX_FILE )
225  #if defined( CONFIG_EARLY_BIND_SERIALIZE )
226  extern "C" mxArray* mxSerialize(const mxArray*);
227  extern "C" mxArray* mxDeserialize(const void*, size_t);
228  #endif
229  extern mxArray *mxCreateSharedDataCopy(const mxArray *pr);
230  #define PRINTF mexPrintf
231 #endif
232 
233 typedef unsigned char byte;
234 
235 
236 
237 #if defined( MAIN_MODULE )
238 
239 
240 /* common global states */
241 
247 int g_compression_level = CONFIG_COMPRESSION_LEVEL;
248 const char* g_compression_type = CONFIG_COMPRESSION_TYPE;
249 int g_compression_check = CONFIG_COMPRESSION_CHECK;
255 const double g_NaN = DBL_NAN;
256 
258 #if defined( MATLAB_MEX_FILE )
259  int g_namelengthmax = 63; // (mxMAXNAM-1)
261 
264 
267 
270 
273 
276 
277 #endif // defined( MATLAB_MEX_FILE )
278 
279 #endif // defined( MAIN_MODULE )
280 
int g_param_wrapping
Wrap parameters.
Definition: global.hpp:275
mxArray * mxSerialize(const mxArray *)
Serialize a MATLAB array.
#define CONFIG_COMPRESSION_TYPE
"blosc", "blosclz", "qlin16", "qlog16" or NULL (for default)
Definition: config.h:29
mxArray * mxDeserialize(const void *, size_t)
Deserialize a MATLAB array.
#define CONFIG_STREAMING
Allow streaming to convert MATLAB variables into byte streams.
Definition: config.h:60
Global configuration settings and defaults.
#define CONFIG_COMPRESSION_LEVEL
compression level: Using compression on typed blobs when > 0
Definition: config.h:28
unsigned char byte
byte type
Definition: global.hpp:233
#define CONFIG_NULL_AS_NAN
use NaN instead of NULL values by default
Definition: config.h:24
int g_result_type
Data organisation of returning query results.
Definition: global.hpp:272
#define CONFIG_CHECK_4_UNIQUE_FIELDS
ensure unique fields in query return structure by default
Definition: config.h:57
int g_streaming
Flag: Allow streaming.
Definition: global.hpp:269
int g_namelengthmax
MATALAB specific globals.
Definition: global.hpp:260
int g_convertUTF8
Flag: String representation (utf8 or ansi)
Definition: global.hpp:253
#define CONFIG_PARAM_WRAPPING
Wrap parameters.
Definition: config.h:75
const double g_NaN
global NaN representation
Definition: global.hpp:255
#define CONFIG_CONVERT_UTF8
Convert UTF-8 to ascii, otherwise set slCharacterEncoding(&#39;UTF-8&#39;)
Definition: config.h:35
int g_NULLasNaN
Flag: return NULL as NaN.
Definition: global.hpp:263
#define CONFIG_RESULT_TYPE
Data organisation of query results.
Definition: config.h:72
Memory leak detection helper.
#define CONFIG_COMPRESSION_CHECK
Flag: check compressed against original data.
Definition: config.h:32
mxArray * mxCreateSharedDataCopy(const mxArray *pr)
Create a "shadowed" MATLAB array.
int g_check4uniquefields
Flag: Check for unique fieldnames.
Definition: global.hpp:266