basic.h
Go to the documentation of this file.
1
14#ifndef CCGL_BASIC_H
15#define CCGL_BASIC_H
16
18#ifndef NDEBUG
19#ifndef _DEBUG
20#define _DEBUG
21#endif /* _DEBUG */
22#endif /* NDEBUG */
23
25#if defined(_WIN64) || defined(__x86_64) || defined(__LP64__)
26#define CPP_64
27#endif
28
30#if defined _MSC_VER
31#define CPP_MSVC
32#endif /* _MSC_VER */
33
35#if defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC)
36#define CPP_ICC
37#endif /* __INTEL_COMPILER */
38
40#if defined(__GNUC__)
41#define CPP_GCC
43#if defined(__APPLE__)
44#define CPP_APPLE
45#endif /* __APPLE__ */
46#endif /* __GNUC__ */
47
48#include <stdint.h>
49#include <memory>
50#include <stdexcept>
51#include <cfloat>
52#include <map>
53#include <string>
54#include <cstring> // strcasecmp in GCC
56#if defined WINDOWS
57// For MSVC and MINGW64 in Windows OS
58// #define _WINSOCKAPI_ // In order to stop windows.h including winsock.h
59// _WINSOCKAPI_ is defined by <winsock2.h>
60#include <winsock2.h>
61#include <windows.h>
62#endif /* WINDOWS */
63
64#if defined CPP_GCC
65#include <dirent.h>
66#include <unistd.h>
67#include <sys/types.h>
68#include <sys/stat.h>
69#include <sys/time.h>
70#include <fcntl.h>
71#include <cerrno>
72#endif /* CPP_GCC */
73
74using std::string;
75
76// define some macro for string related built-in functions
77#ifdef CPP_MSVC
78#define stringcat strcat_s
79#define stringcpy strcpy_s
80#define strprintf sprintf_s
81#define stringtoken strtok_s
82#define stringscanf sscanf_s
83#else
84#define stringcat strcat
85#define stringcpy strcpy
86#define strprintf snprintf
87#define stringtoken strtok_r
88#define stringscanf sscanf
89#endif /* CPP_MSVC */
90
91#if defined(__MINGW32_MAJOR_VERSION) || defined(__MINGW64_VERSION_MAJOR)
92#define MINGW
93#endif
94
95#if defined(MINGW) || defined(_MSC_VER)
96#define strcasecmp _stricmp
97#endif /* MINGW or MSVC */
98
99#if defined(__clang__) && defined(__apple_build_version__)
100// Apple Clang
101#if ((__clang_major__ * 100) + __clang_minor__) >= 400
102#if __has_feature(cxx_noexcept)
103#define HAS_NOEXCEPT
104#endif /* NOEXCEPT */
105#if __has_feature(cxx_override_control)
106#define HAS_OVERRIDE
107#endif /* OVERRIDE */
108#endif /* Apple Clang */
109#elif defined(__clang__)
110// Clang
111#if ((__clang_major__ * 100) + __clang_minor__) >= 304
112#if __has_feature(cxx_noexcept)
113#define HAS_NOEXCEPT
114#endif /* NOEXCEPT */
115#if __has_feature(cxx_override_control)
116#define HAS_OVERRIDE
117#endif /* OVERRIDE */
118#if __has_feature(cxx_variadic_templates)
119#define HAS_VARIADIC_TEMPLATES
120#endif /* VARIADIC_TEMPLATES */
121#endif /* Clang */
122#elif defined(CPP_ICC)
123// Intel C++
124#if ((__INTEL_COMPILER >= 1400) && (__INTEL_COMPILER != 9999)) || (__ICL >= 1400)
125#define HAS_NOEXCEPT
126#define HAS_OVERRIDE
127#define HAS_VARIADIC_TEMPLATES
128#endif /* Intel C++ */
129#elif defined(CPP_GCC)
130// GNU GCC
131#if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 && (__cplusplus >= 201103L || (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GXX_EXPERIMENTAL_CXX0X__))
132#define HAS_NOEXCEPT
133#define HAS_OVERRIDE
134#define HAS_VARIADIC_TEMPLATES
135#endif /* GCC */
136#elif defined(_MSC_VER)
137// MS Visual C++
138#if _MSC_VER >= 1900
139#define HAS_NOEXCEPT
140#endif /* Visual Studio 2015 or later */
141#if _MSC_VER >= 1800
142#define HAS_VARIADIC_TEMPLATES
143#endif /* Visual Studio 2013 or later */
144#if _MSC_VER>= 1600
145#define HAS_OVERRIDE
146#endif /* Visual Studio 2010 or later */
147#endif /* Figure out HAS_NOEXCEPT, HAS_VARIADIC_TEMPLATES, and HAS_OVERRIDE or not */
148
150#ifdef HAS_NOEXCEPT
151#define NOEXCEPT noexcept
152#else
153#define NOEXCEPT throw()
154#endif /* HAS_NOEXCEPT */
155
157#ifdef HAS_OVERRIDE
158#define OVERRIDE override
159#else
160#define OVERRIDE
161#endif /* HAS_OVERRIDE */
162
163/*
164* Avoid the compile error on MSVC like this:
165* warning C4251: 'CLASS_TEST::m_structs':
166* class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class
167* refers to http://www.cnblogs.com/duboway/p/3332057.html
168*/
169#ifdef MSVC
170#define DLL_STL_LIST(STL_API, STL_TYPE) \
171 template class STL_API std::allocator< STL_TYPE >; \
172 template class STL_API std::vector<STL_TYPE, std::allocator< STL_TYPE > >;
173#endif /* MSVC */
174
175#ifdef USE_GDAL
176/* Ignore warning on Windows MSVC compiler caused by GDAL.
177* refers to http://blog.csdn.net/liminlu0314/article/details/8227518
178*/
179#if defined(_MSC_VER) && (_MSC_VER >= 1400)
180#pragma warning(disable: 4100 4190 4251 4275 4305 4309 4819 4996)
181#endif /* Ignore warnings of GDAL */
182#endif /* USE_GDAL */
183
188namespace ccgl {
189#if defined CPP_MSVC
192typedef signed __int8 vint8_t;
194typedef unsigned __int8 vuint8_t;
196typedef signed __int16 vint16_t;
198typedef unsigned __int16 vuint16_t;
200typedef signed __int32 vint32_t;
202typedef unsigned __int32 vuint32_t;
204typedef signed __int64 vint64_t;
206typedef unsigned __int64 vuint64_t;
207#else
208typedef int8_t vint8_t;
209typedef uint8_t vuint8_t;
210typedef int16_t vint16_t;
211typedef uint16_t vuint16_t;
212typedef int32_t vint32_t;
213typedef uint32_t vuint32_t;
214typedef int64_t vint64_t;
215typedef uint64_t vuint64_t;
216#endif
217
218#ifdef _WIN32
220#define LLD "%I64d"
221#define LLU "%I64u"
222#else
223#define LLD "%lld"
224#define LLU "%llu"
225#endif
226
227#ifdef CPP_64
228typedef vint64_t vint;
229typedef vint64_t vsint;
230typedef vuint64_t vuint;
231#else
232typedef vint32_t vint;
233typedef vint32_t vsint;
234typedef vuint32_t vuint;
235#endif
237typedef vint64_t pos_t;
238
242
244#ifndef NODATA_VALUE
245#define NODATA_VALUE (-9999.)
246#endif /* NODATA_VALUE */
247
249#ifndef MISSINGFLOAT
250#define MISSINGFLOAT (-1 * FLT_MAX)
251#endif /* MISSINGFLOAT */
252
254#ifndef MAXIMUMFLOAT
255#define MAXIMUMFLOAT FLT_MAX
256#endif /* MAXIMUMFLOAT */
257
259#ifndef PATH_MAX
260#define PATH_MAX 1024
261#endif /* PATH_MAX */
262
264#ifndef UTIL_ZERO
265#define UTIL_ZERO 1.0e-6
266#endif /* UTIL_ZERO */
267
269#ifndef PI
270#define PI 3.14159265358979323846
271#endif /* PI */
272
274#ifndef MINI_SLOPE
275#define MINI_SLOPE 0.0001
276#endif /* MINI_SLOPE */
277
278#ifdef MSVC
279#if _MSC_VER <= 1600
280#define isnan(x) ((x) != (x))
281#define isinf(x) (!_finite(x) && !_isnan(x))
282#endif
283#endif
284
285#ifdef WINDOWS
286#define SEP '\\'
287#define SEPSTR "\\"
288#ifndef MSVC
289#define LIBPREFIX "lib"
290#endif
291#define LIBSUFFIX ".dll"
292#else
293#define SEP '/'
294#define SEPSTR "/"
295#define LIBPREFIX "lib"
296#endif /* Windows */
297#ifdef LINUX
298#define LIBSUFFIX ".so"
299#elif defined(MACOS) || defined(MACOSX)
300#define LIBSUFFIX ".dylib"
301#endif /* Linux and macOS */
302
304#ifdef _DEBUG
305#define POSTFIX "d"
306#endif
308#ifdef RELWITHDEBINFO
309#define POSTFIX "rd"
310#endif
312#ifdef MINSIZEREL
313#define POSTFIX "s"
314#endif
316#ifndef POSTFIX
317#define POSTFIX ""
318#endif
319
323
325#define CVT_INT(param) static_cast<int>((param))
327#define CVT_SIZET(param) static_cast<size_t>((param))
329#define CVT_FLT(param) static_cast<float>((param))
331#define CVT_DBL(param) static_cast<double>((param))
333#define CVT_TIMET(param) static_cast<time_t>((param))
335#define CVT_CHAR(param) static_cast<char>((param))
337#define CVT_STR(param) static_cast<string>((param))
338
340#define CVT_VINT(param) static_cast<vint>((param))
342#define CVT_VSINT(param) static_cast<vsint>((param))
344#define CVT_VUINT(param) static_cast<vuint>((param))
346#define CVT_VUINT64(param) static_cast<vuint64_t>((param))
347
349typedef std::map<string, string> STRING_MAP;
350
352typedef std::map<string, double> STRDBL_MAP;
353
354#ifdef CPP_64
355#define ITOA_S _i64toa_s
356#define ITOW_S _i64tow_s
357#define I64TOA_S _i64toa_s
358#define I64TOW_S _i64tow_s
359#define UITOA_S _ui64toa_s
360#define UITOW_S _ui64tow_s
361#define UI64TOA_S _ui64toa_s
362#define UI64TOW_S _ui64tow_s
363#else
364#define ITOA_S _itoa_s
365#define ITOW_S _itow_s
366#define I64TOA_S _i64toa_s
367#define I64TOW_S _i64tow_s
368#define UITOA_S _ui64toa_s
369#define UITOW_S _ui64tow_s
370#define UI64TOA_S _ui64toa_s
371#define UI64TOW_S _ui64tow_s
372#endif
373
386private:
387 NotCopyable(const NotCopyable&);
388
389 NotCopyable& operator=(const NotCopyable&);
390public:
391 NotCopyable();
392};
393
398class Object {
399public:
400 virtual ~Object();
401};
402
408public:
409 virtual ~Interface();
410};
411
416class ModelException: public std::exception {
417public:
424 ModelException(const string& class_name, const string& function_name, const string& msg);
425
430 string ToString();
431
436 const char* what() const NOEXCEPT OVERRIDE;
437
438private:
439 std::runtime_error runtime_error_;
440};
441
446bool IsIpAddress(const char* ip);
447
453void Log(const string& msg, const string& logpath = "debugInfo.log");
454
463
468
473void SetOpenMPThread(int n);
474
479void StatusMessage(const char* msg);
480
485void StatusMessage(const string& msg);
486
491inline void SleepMs(const int millisecs) {
492#ifdef WINDOWS
493 Sleep(millisecs);
494#else
495 usleep(millisecs * 1000); // usleep takes sleep time_funcs in us (1 millionth of a second)
496#endif
497}
498
499} /* namespace ccgl */
500#endif /* CCGL_BASIC_H */
#define NOEXCEPT
A compatible reference to noexcept or throw() if not supported by the compiler.
Definition: basic.h:153
#define OVERRIDE
A compatible reference to override or blank if not supported by the compiler.
Definition: basic.h:160
Base type of all interfaces.
Definition: basic.h:407
virtual ~Interface()
Interface.
Definition: basic.cpp:34
Print the exception message.
Definition: basic.h:416
string ToString()
Construct error information (string version)
Definition: basic.cpp:42
const char * what() const NOEXCEPT OVERRIDE
Overload function to construct error information.
Definition: basic.cpp:46
ModelException(const string &class_name, const string &function_name, const string &msg)
Constructor.
Definition: basic.cpp:37
Base class for classes that cannot be copied.
Definition: basic.h:385
NotCopyable()
NotCopyable implementation.
Definition: basic.cpp:19
Base of all classes.
Definition: basic.h:398
virtual ~Object()
DefaultClass.
Definition: basic.cpp:30
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17
void Log(const string &msg, const string &logpath)
Writes an entry to the log file.
Definition: basic.cpp:78
int GetAvailableThreadNum()
Detect the available threads number.
Definition: basic.cpp:103
std::map< string, string > STRING_MAP
Map of string key and string value.
Definition: basic.h:349
void SetDefaultOpenMPThread()
Set the default omp thread number if necessary.
Definition: basic.cpp:115
vint64_t pos_t
Signed integer representing position.
Definition: basic.h:237
bool IsIpAddress(const char *ip)
Check if the IP address is valid.
Definition: basic.cpp:50
void StatusMessage(const char *msg)
Print status messages for Debug.
Definition: basic.cpp:136
std::map< string, double > STRDBL_MAP
Map of string key and double value.
Definition: basic.h:352
void SleepMs(const int millisecs)
Sleep milliseconds.
Definition: basic.h:491
void SetOpenMPThread(const int n)
Set the omp thread number by given thread number.
Definition: basic.cpp:127