Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
defines.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
23 #ifndef MCR_DEFINES_H_
24 #define MCR_DEFINES_H_
25 
26 #ifdef __cplusplus
27 #include <cerrno>
28 #include <ctime>
29 #include <cstdbool>
30 #include <cstdint>
31 #ifdef MCR_DEBUG
32  #include <cassert>
33 #endif
34 extern "C" {
35 #else
36 #include <errno.h>
37 #include <time.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #ifdef MCR_DEBUG
41  #include <assert.h>
42 #endif
43 #endif
44 
45 /* Doxygen in-doxy-comment references */
46 #ifndef reterr
47 
50  #define reterr int
51  #undef reterr
52 #endif
53 
54 #ifndef retind
55 
56  #define retind size_t
57  #undef retind
58 #endif
59 
60 #ifndef retid
61 
62  #define retid size_t
63  #undef retid
64 #endif
65 
66 #ifndef retcmp
67 
70  #define retcmp int
71  #undef retcmp
72 #endif
73 
74 #ifndef opt
75 
76  #define opt
77  #undef opt
78 #endif
79 
80 #ifndef ctor
81 
85  #define ctor
86  #undef ctor
87 #endif
88 #ifndef dtor
89 
93  #define dtor
94  #undef dtor
95 #endif
96 
97 #ifndef mcr_cpp_only
98 
99  #define mcr_cpp_only
100  #undef mcr_cpp_only
101 #endif
102 
103 #ifndef mcr_platform
104 
105  #define mcr_platform
106  #undef mcr_platform
107 #endif
108 
109 #ifndef mcr_future
110 
111  #define mcr_future
112  #undef mcr_future
113 #endif
114 
115 #ifndef MCR_PLATFORM_DEFINES_H
116 
118  #define MCR_PLATFORM_DEFINES_H
119  #undef MCR_PLATFORM_DEFINES_H
120 #endif
121 
122 /* Library export */
123 #ifdef MCR_PLATFORM_WINDOWS
124  #define MCR_EXPORT __declspec(dllexport)
125  #define MCR_IMPORT __declspec(dllimport)
126 #else
127  #define MCR_EXPORT __attribute__((visibility("default")))
128  #define MCR_IMPORT __attribute__((visibility("default")))
129 #endif
130 
131 #ifndef MCR_API
132  #ifdef MCR_STATIC
133  #define MCR_API
134  #else
135  #ifdef LIBMACRO_LIBRARY
136  #define MCR_API MCR_EXPORT
137  #else
138  #define MCR_API MCR_IMPORT
139  #endif
140  #endif
141 #endif
142 
143 #ifndef MCR_INLINE
144  #ifdef _MSC_VER
145 
147  #define MCR_INLINE __inline
148  #else
149  #define MCR_INLINE inline
150  #endif
151 #endif
152 
153 
154 /* Rest of file is helpers and finally the platform definitions. */
155 
156 
157 #define MCR_STR_HELPER(x) #x
158 
161 #define MCR_STR(x) MCR_STR_HELPER(x)
162 
163 #define MCR_EXP_HELPER(x) x
164 
165 #define MCR_EXP(x) MCR_EXP_HELPER(x)
166 
167 #define MCR_TOKEN_HELPER(x, y) x ##y
168 #define MCR_SNAKE_HELPER(x, y) x ##_ ##y
169 
170 #define MCR_TOKEN(x, y) MCR_TOKEN_HELPER(x, y)
171 
172 #define MCR_SNAKE(x, y) MCR_SNAKE_HELPER(x, y)
173 
174 #ifndef MCR_LINE
175 
176  #define MCR_LINE __FILE__ "(" MCR_STR(__LINE__) ")"
177 #endif
178 
179 #ifndef MCR_PLATFORM
180  #define MCR_PLATFORM none
181  #pragma message "Warning: MCR_PLATFORM is not defined. Please provide MCR_PLATFORM as a preprocessor definition for your platform."
182  #include "mcr/warn.h"
183 #endif
184 #ifndef UNUSED
185 
186  #define UNUSED(x) ((void)(x))
187 #endif
188 #ifndef MCR_CMP
189 
195  #define MCR_CMP(lhs, rhs) (((lhs) > (rhs)) - ((lhs) < (rhs)))
196 #endif
197 #ifndef MCR_CMP_CAST
198 
205  #define MCR_CMP_CAST(casting, lhs, rhs) MCR_CMP(casting(lhs), casting(rhs))
206 #endif
207 #ifndef MCR_CMP_PTR
208 
218 #define MCR_CMP_PTR(T, lhsPtr, rhsPtr) (lhsPtr && rhsPtr ? \
219  MCR_CMP(*mcr_castpt(T, lhsPtr), *mcr_castpt(T, rhsPtr)) : \
220  MCR_CMP(lhsPtr, rhsPtr))
221 #endif
222 
223 #ifndef MCR_OFFSET
224 
230 #define MCR_OFFSET(structDecl, memberName, outSizeVariable) \
231 { \
232  structDecl *__internalStruct = mcr_null; \
233  outSizeVariable = mcr_cast(size_t, &(__internalStruct).memberName); \
234 }
235 #endif
236 
238 #ifndef mcr_cast
239  #ifdef __cplusplus
240 
242  #define mcr_cast(T, obj) static_cast<T>(obj)
243 
246  #define mcr_castpt(T, obj) reinterpret_cast<T *>(obj)
247 
248  #define mcr_null nullptr
249  #else
250  #define mcr_cast(T, obj) ((T)(obj))
251  #define mcr_castpt(T, obj) ((T *)(obj))
252  #define mcr_null NULL
253  #endif
254 #endif
255 /* Debugging definitions */
256 #ifndef mcr_ddo
257  #ifdef MCR_DEBUG
258 
259  #define mcr_ddo(stuff) stuff
260  #else
261  /* Empty blocks. Non-empty interferes with one-liners. */
262  #define mcr_ddo(stuff) {}
263  #endif
264 #endif
265 /* Do function pointer if defined */
266 #ifndef mcr_ifdo
267 #define mcr_ifdo(fnPtr, ...) \
268  if (fnPtr) { \
269  (fnPtr)(__VA_ARGS__); \
270  }
271 #endif
272 /* Do function pointer if defined and return result */
273 #ifndef mcr_ifreturndo
274 #define mcr_ifreturndo(fnPtr, ...) \
275  if (fnPtr) { \
276  return (fnPtr)(__VA_ARGS__); \
277  }
278 #endif
279 #ifndef mcr_dprint
280 
281  #define mcr_dprint(...) mcr_ddo(printf(__VA_ARGS__))
282 #endif
283 #ifndef mcr_dexit
284 
285  #define mcr_dexit mcr_ddo({ mcr_dmsg; exit(mcr_err); })
286 #endif
287 #ifndef mcr_dmsg
288 
292 #define mcr_dmsg \
293 mcr_ddo(fprintf(stderr, "Error %d: " MCR_LINE ", %s: %s.\n", \
294  mcr_err, mcr_timestamp(), mcr_error_str(mcr_err)))
295 #endif
296 #ifndef mcr_dassert
297 
298  #define mcr_dassert(expression) mcr_ddo(assert(expression))
299 #endif
300 #ifndef mcr_arrlen
301 
306  #define mcr_arrlen(arr) (sizeof(arr) / sizeof(*(arr)))
307 #endif
308 #ifndef mcr_errno
309 
314 #define mcr_errno(fallbackError) { \
315  mcr_err = errno; \
316  if (!mcr_err) \
317  mcr_err = (fallbackError); \
318  mcr_dmsg; \
319 }
320 #endif
321 #ifndef mcr_error_return
322 
326 #define mcr_error_return { \
327  mcr_dmsg; \
328  return mcr_err; \
329 }
330 #endif
331 #ifndef mcr_error_set
332 
333 #define mcr_error_set(errorNumber) { \
334  mcr_err = (errorNumber); \
335  mcr_dmsg; \
336 }
337 #endif
338 #ifndef mcr_error_set_return
339 
343 #define mcr_error_set_return(errorNumber) { \
344  mcr_err = (errorNumber); \
345  mcr_dmsg; \
346  return errorNumber; \
347 }
348 #endif
349 
350 #ifndef ZERO
351  #define ZERO(inst) memset(&(inst), 0, sizeof(inst))
352 #endif
353 #ifndef ZERO_PTR
354  #define ZERO_PTR(ptr) memset(ptr, 0, sizeof(*(ptr)))
355 #endif
356 
357 #ifndef dmsg
358 
359  #define dmsg mcr_dmsg
360 #endif
361 #ifndef dassert
362 
363  #define dassert(expr) mcr_dassert(expr)
364 #endif
365 #ifndef arrlen
366 
367  #define arrlen(arr) mcr_arrlen(arr)
368 #endif
369 #ifndef mset_error
370 
371  #define mset_error(errorNumber) mcr_error_set(errorNumber)
372 #endif
373 #ifndef error_set_return
374 
375  #define error_set_return(errorNumber) mcr_error_set_return(errorNumber)
376 #endif
377 
379 #define MCR_MOD_ANY ((unsigned int) ~0)
380 
381 #ifndef MCR_THREAD_MAX
382 
387  #define MCR_THREAD_MAX 0x10
388 #endif
389 
390 #ifndef MCR_MACRO_JOIN_TIMEOUT
391 
394  #define MCR_MACRO_JOIN_TIMEOUT 2
395 #endif
396 
397 #ifndef MCR_MAX_PAUSE_COUNT
398 
400  #define MCR_MAX_PAUSE_COUNT 5
401 #endif
402 
404 #define MCR_HIDECHO_ANY mcr_cast(size_t, ~0)
405 
406 #define MCR_KEY_ANY mcr_cast(int, 0)
407 
408 #ifndef mcr_throwif
409 
415 #define mcr_throwif(condition, errorNumber) \
416  if (condition) { \
417  mcr_dassert(condition); \
418  ::mcr::throwError(MCR_LINE, errorNumber); \
419  }
420 #endif
421 
422 #ifndef mcr_assert_receive_function
423 
427 #define mcr_assert_receive_function(receiver, receiveFnc) \
428  mcr_throwif(!receiveFnc, EFAULT); \
429  mcr_throwif((receiverFnc == mcr_Trigger_receive \
430  || receiverFnc == mcr_Macro_receive) && !receiver, EINVAL);
431 #endif
432 
434 #define MCR_DECL_INTERFACE(className) \
435  className() = default; \
436  className(const className &) = default; \
437  virtual ~className() = default; \
438  className &operator =(const className &) = default;
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #ifdef MCR_PLATFORM_DEFINES_H
445 #include MCR_PLATFORM_DEFINES_H
446 #endif
447 #ifndef MCR_PLATFORM_H
448 #define MCR_PLATFORM_H MCR_STR(mcr/MCR_PLATFORM/p_libmacro.h)
449 #endif
450 
451 #endif
Raise a compiler warning. Usage: #include "mcr/warn.h"