Debian Codegraph @

src/dmlc-core/include/dmlc/base.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file base.h
4 * \brief defines configuration macros
5 */
6 #ifndef DMLC_BASE_H_
7 #define DMLC_BASE_H_
8
9 /*! \brief whether use glog for logging */
10 #ifndef DMLC_USE_GLOG
11 #define DMLC_USE_GLOG 0
12 #endif
13
14 /*
15 * The preprocessor definition DMLC_USE_LOGGING_LIBRARY determines whether to
16 * use a user-defined logging library. If defined, dmlc will not define the
17 * macros CHECK() and LOG() and instead locate CHECK() and LOG() from the value
18 * of DMLC_USE_LOGGING_LIBRARY. The DMLC_USE_LOGGING_LIBRARY macro shall be of
19 * form <my_logging.h>:
20 *
21 * #define DMLC_USE_LOGGING_LIBRARY <my_logging.h>
22 *
23 * Make sure to define CHECK() and LOG() macros in the provided header;
24 * otherwise the build will fail.
25 */
26
27 /*!
28 * \brief whether throw dmlc::Error instead of
29 * directly calling abort when FATAL error occured
30 * NOTE: this may still not be perfect.
31 * do not use FATAL and CHECK in destructors
32 */
33 #ifndef DMLC_LOG_FATAL_THROW
34 #define DMLC_LOG_FATAL_THROW 1
35 #endif
36
37 /*!
38 * \brief whether always log a message before throw
39 * This can help identify the error that cannot be catched.
40 */
41 #ifndef DMLC_LOG_BEFORE_THROW
42 #define DMLC_LOG_BEFORE_THROW 0
43 #endif
44
45 /*!
46 * \brief Whether to use customized logger,
47 * whose output can be decided by other libraries.
48 */
49 #ifndef DMLC_LOG_CUSTOMIZE
50 #define DMLC_LOG_CUSTOMIZE 0
51 #endif
52
53 /*!
54 * \brief Whether to enable debug logging feature.
55 */
56 #ifndef DMLC_LOG_DEBUG
57 #ifdef NDEBUG
58 #define DMLC_LOG_DEBUG 0
59 #else
60 #define DMLC_LOG_DEBUG 1
61 #endif
62 #endif
63
64 /*!
65 * \brief Whether to disable date message on the log.
66 */
67 #ifndef DMLC_LOG_NODATE
68 #define DMLC_LOG_NODATE 0
69 #endif
70
71 /*! \brief whether compile with hdfs support */
72 #ifndef DMLC_USE_HDFS
73 #define DMLC_USE_HDFS 0
74 #endif
75
76 /*! \brief whether compile with s3 support */
77 #ifndef DMLC_USE_S3
78 #define DMLC_USE_S3 0
79 #endif
80
81 /*! \brief whether or not use parameter server */
82 #ifndef DMLC_USE_PS
83 #define DMLC_USE_PS 0
84 #endif
85
86 /*! \brief whether or not use c++11 support */
87 #ifndef DMLC_USE_CXX11
88 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER)
89 #define DMLC_USE_CXX11 1
90 #else
91 #define DMLC_USE_CXX11 (__cplusplus >= 201103L)
92 #endif
93 #endif
94
95 /*! \brief strict CXX11 support */
96 #ifndef DMLC_STRICT_CXX11
97 #if defined(_MSC_VER)
98 #define DMLC_STRICT_CXX11 1
99 #else
100 #define DMLC_STRICT_CXX11 (__cplusplus >= 201103L)
101 #endif
102 #endif
103
104 /*! \brief Whether cxx11 thread local is supported */
105 #ifndef DMLC_CXX11_THREAD_LOCAL
106 #if defined(_MSC_VER)
107 #define DMLC_CXX11_THREAD_LOCAL (_MSC_VER >= 1900)
108 #elif defined(__clang__)
109 #define DMLC_CXX11_THREAD_LOCAL (__has_feature(cxx_thread_local))
110 #else
111 #define DMLC_CXX11_THREAD_LOCAL (__cplusplus >= 201103L)
112 #endif
113 #endif
114
115 /*! \brief Whether to use modern thread local construct */
116 #ifndef DMLC_MODERN_THREAD_LOCAL
117 #define DMLC_MODERN_THREAD_LOCAL 1
118 #endif
119
120 /*! \brief whether RTTI is enabled */
121 #ifndef DMLC_ENABLE_RTTI
122 #define DMLC_ENABLE_RTTI 1
123 #endif
124
125 /*! \brief whether use fopen64 */
126 #ifndef DMLC_USE_FOPEN64
127 #define DMLC_USE_FOPEN64 1
128 #endif
129
130 /// check for C++11 support
131 #if DMLC_USE_CXX11
132 #if (!defined(_MSC_VER) && __cplusplus < 201103L) || (defined(_MSC_VER) && _MSC_VER < 1900)
133 // MSVC doesn't support __cplusplus macro properly until MSVC 2017
134 // We want to also support MSVC 2015, so manually check _MSC_VER
135
136 #pragma message("Compiling without c++11, some features may be disabled")
137 #undef DMLC_USE_CXX11
138 #define DMLC_USE_CXX11 0
139
140 #endif // (!defined(_MSC_VER) && __cplusplus < 201103L) || (defined(_MSC_VER) && _MSC_VER < 1900)
141 #endif // DMLC_USE_CXX11
142
143 /*!
144 * \brief Use little endian for binary serialization
145 * if this is set to 0, use big endian.
146 */
147 #ifndef DMLC_IO_USE_LITTLE_ENDIAN
148 #define DMLC_IO_USE_LITTLE_ENDIAN 1
149 #endif
150
151 /*!
152 * \brief Enable std::thread related modules,
153 * Used to disable some module in mingw compile.
154 */
155 #ifndef DMLC_ENABLE_STD_THREAD
156 #define DMLC_ENABLE_STD_THREAD DMLC_USE_CXX11
157 #endif
158
159 /*! \brief whether enable regex support, actually need g++-4.9 or higher*/
160 #ifndef DMLC_USE_REGEX
161 #define DMLC_USE_REGEX DMLC_STRICT_CXX11
162 #endif
163
164 /*! \brief helper macro to supress unused warning */
165 #if defined(__GNUC__)
166 #define DMLC_ATTRIBUTE_UNUSED __attribute__((unused))
167 #else
168 #define DMLC_ATTRIBUTE_UNUSED
169 #endif
170
171 /*! \brief helper macro to supress Undefined Behavior Sanitizer for a specific function */
172 #if defined(__clang__)
173 #define DMLC_SUPPRESS_UBSAN __attribute__((no_sanitize("undefined")))
174 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
175 #define DMLC_SUPPRESS_UBSAN __attribute__((no_sanitize_undefined))
176 #else
177 #define DMLC_SUPPRESS_UBSAN
178 #endif
179
180 /*! \brief helper macro to generate string concat */
181 #define DMLC_STR_CONCAT_(__x, __y) __x##__y
182 #define DMLC_STR_CONCAT(__x, __y) DMLC_STR_CONCAT_(__x, __y)
183
184 /*!
185 * \brief Disable copy constructor and assignment operator.
186 *
187 * If C++11 is supported, both copy and move constructors and
188 * assignment operators are deleted explicitly. Otherwise, they are
189 * only declared but not implemented. Place this macro in private
190 * section if C++11 is not available.
191 */
192 #ifndef DISALLOW_COPY_AND_ASSIGN
193 #if DMLC_USE_CXX11
194 #define DISALLOW_COPY_AND_ASSIGN(T) \
195 T(T const &) = delete; \
196 T(T &&) = delete; \
197 T &operator=(T const &) = delete; \
198 T &operator=(T &&) = delete
199 #else
200 #define DISALLOW_COPY_AND_ASSIGN(T) \
201 T(T const &); \
202 T &operator=(T const &)
203 #endif
204 #endif
205
206 #ifdef __APPLE__
207 #define off64_t off_t
208 #endif
209
210 #ifdef _MSC_VER
211 #if _MSC_VER < 1900
212 // NOTE: sprintf_s is not equivalent to snprintf,
213 // they are equivalent when success, which is sufficient for our case
214 #define snprintf sprintf_s
215 #define vsnprintf vsprintf_s
216 #endif
217 #else
218 #ifdef _FILE_OFFSET_BITS
219 #if _FILE_OFFSET_BITS == 32
220 #pragma message("Warning: FILE OFFSET BITS defined to be 32 bit")
221 #endif
222 #endif
223
224 extern "C" {
225 #include <sys/types.h>
226 }
227 #endif
228
229 #ifdef _MSC_VER
230 //! \cond Doxygen_Suppress
231 typedef signed char int8_t;
232 typedef __int16 int16_t;
233 typedef __int32 int32_t;
234 typedef __int64 int64_t;
235 typedef unsigned char uint8_t;
236 typedef unsigned __int16 uint16_t;
237 typedef unsigned __int32 uint32_t;
238 typedef unsigned __int64 uint64_t;
239 //! \endcond
240 #else
241 #include <inttypes.h>
242 #endif
243 #include <string>
244 #include <vector>
245
246 #if defined(_MSC_VER) && _MSC_VER < 1900
247 #define noexcept_true throw()
248 #define noexcept_false
249 #define noexcept(a) noexcept_##a
250 #endif
251
252 #if defined(_MSC_VER)
253 #define DMLC_NO_INLINE __declspec(noinline)
254 #else
255 #define DMLC_NO_INLINE __attribute__((noinline))
256 #endif
257
258 #if defined(__GNUC__) || defined(__clang__)
259 #define DMLC_ALWAYS_INLINE inline __attribute__((__always_inline__))
260 #elif defined(_MSC_VER)
261 #define DMLC_ALWAYS_INLINE __forceinline
262 #else
263 #define DMLC_ALWAYS_INLINE inline
264 #endif
265
266 #if DMLC_USE_CXX11
267 #define DMLC_THROW_EXCEPTION noexcept(false)
268 #define DMLC_NO_EXCEPTION noexcept(true)
269 #else
270 #define DMLC_THROW_EXCEPTION
271 #define DMLC_NO_EXCEPTION
272 #endif
273
274 /*! \brief namespace for dmlc */
275 namespace dmlc {
276 /*!
277 * \brief safely get the beginning address of a vector
278 * \param vec input vector
279 * \return beginning address of a vector
280 */
281 template <typename T>
282 inline T *BeginPtr(std::vector<T> &vec) { // NOLINT(*)
283 if (vec.size() == 0) {
284 return NULL;
285 } else {
286 return &vec[0];
287 }
288 }
289 /*!
290 * \brief get the beginning address of a const vector
291 * \param vec input vector
292 * \return beginning address of a vector
293 */
294 template <typename T>
295 inline const T *BeginPtr(const std::vector<T> &vec) {
296 if (vec.size() == 0) {
297 return NULL;
298 } else {
299 return &vec[0];
300 }
301 }
302 /*!
303 * \brief get the beginning address of a string
304 * \param str input string
305 * \return beginning address of a string
306 */
307 inline char *BeginPtr(std::string &str) { // NOLINT(*)
308 if (str.length() == 0) {
309 return NULL;
310 }
311 return &str[0];
312 }
313 /*!
314 * \brief get the beginning address of a const string
315 * \param str input string
316 * \return beginning address of a string
317 */
318 inline const char *BeginPtr(const std::string &str) {
319 if (str.length() == 0) {
320 return NULL;
321 }
322 return &str[0];
323 }
324 } // namespace dmlc
325
326 #if defined(_MSC_VER) && _MSC_VER < 1900
327 #define constexpr const
328 #define alignof __alignof
329 #endif
330
331 /* If fopen64 is not defined by current machine,
332 replace fopen64 with std::fopen. Also determine ability to print stack trace
333 for fatal error and define DMLC_LOG_STACK_TRACE if stack trace can be
334 produced. Always keep this include directive at the bottom of dmlc/base.h */
335 #ifdef DMLC_CORE_USE_CMAKE
336 #include <dmlc/build_config.h>
337 #else
338 #include <dmlc/build_config_default.h>
339 #endif
340
341 #endif // DMLC_BASE_H_