forked from Softmotions/iowow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasedefs.h
239 lines (202 loc) · 6.26 KB
/
basedefs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef BASEDEFS_H
#define BASEDEFS_H
/**************************************************************************************************
* IOWOW library
*
* MIT License
*
* Copyright (c) 2012-2021 Softmotions Ltd <info@softmotions.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*************************************************************************************************/
/**
* @file
* @brief Very basic definitions.
* @author Anton Adamansky (adamansky@softmotions.com)
*/
#ifdef __cplusplus
#define IW_EXTERN_C_START extern "C" {
#define IW_EXTERN_C_END }
#else
#define IW_EXTERN_C_START
#define IW_EXTERN_C_END
#endif
#define IW_XSTR(s) IW_STR(s)
#define IW_STR(s) #s
#define IW_MAX(X__, Y__) ({ __typeof__(X__) x = (X__); __typeof__(Y__) y = (Y__); x < y ? y : x; })
#define IW_MIN(X__, Y__) ({ __typeof__(X__) x = (X__); __typeof__(Y__) y = (Y__); x < y ? x : y; })
#if (defined(_WIN32) || defined(_WIN64))
#if (defined(IW_NODLL) || defined(IW_STATIC))
#define IW_EXPORT
#else
#ifdef IW_API_EXPORTS
#define IW_EXPORT __declspec(dllexport)
#else
#define IW_EXPORT __declspec(dllimport)
#endif
#endif
#else
#if __GNUC__ >= 4
#define IW_EXPORT __attribute__((visibility("default")))
#else
#define IW_EXPORT
#endif
#endif
#if defined(__GNUC__)
#define IW_INLINE static inline __attribute__((always_inline))
#else
#define IW_INLINE static inline
#endif
#define IW_SOFT_INLINE static inline
#if __GNUC__ >= 4
#define WUR __attribute__((__warn_unused_result__))
#define IW_ALLOC __attribute__((malloc)) __attribute__((warn_unused_result))
#else
#define WUR
#define IW_ALLOC
#endif
#define IW_ARR_STATIC static
#define IW_ARR_CONST const
#ifdef _WIN32
#include <windows.h>
#define INVALIDHANDLE(_HNDL) \
(((_HNDL) == INVALID_HANDLE_VALUE) || (_HNDL) == NULL)
#else
typedef int HANDLE;
#define INVALID_HANDLE_VALUE (-1)
#define INVALIDHANDLE(_HNDL) ((_HNDL) < 0 || (_HNDL) == UINT16_MAX)
#endif
#define IW_ERROR_START 70000
#ifdef _WIN32
#define IW_PATH_CHR '\\'
#define IW_PATH_STR "\\"
#define IW_LINE_SEP "\r\n"
#else
#define IW_PATH_CHR '/'
#define IW_PATH_STR "/"
#define IW_LINE_SEP "\n"
#endif
#define ZGO(label__, val__) \
({ __typeof__(val__) v__ = (val__); \
if (!v__) goto label__; \
v__; })
#define ZRET(ret__, val__) \
({ __typeof__(val__) v__ = (val__); \
if (!v__) return ret__; \
v__; })
#ifdef __GNUC__
#define RCGO(rc__, label__) if (__builtin_expect((!!(rc__)), 0)) goto label__
#else
#define RCGO(rc__, label__) if (rc__) goto label__
#endif
#define RCIF(res__, rc__, rcv__, label__) \
if (res__) { \
rc__ = (rcv__); \
goto label__; \
}
#define RCHECK(rc__, label__, expr__) \
rc__ = expr__; \
RCGO(rc__, label__)
#define RCC(rc__, label__, expr__) RCHECK(rc__, label__, expr__)
#ifndef RCGA
#define RCGA(v__, label__) \
if (!(v__)) { \
rc = iwrc_set_errno(IW_ERROR_ALLOC, errno); \
goto label__; \
}
#endif
#ifndef RCA
#define RCA(v__, label__) RCGA(v__, label__)
#endif
#ifndef RCB
#define RCB(label__, v__) RCGA(v__, label__)
#endif
#ifndef RCN
#define RCN(label__, v__) \
if ((v__) < 0) { \
rc = iwrc_set_errno(IW_ERROR_ERRNO, errno); \
goto label__; \
}
#endif
#ifndef RCT
#define RCT(label__, val__) \
({ __typeof__(val__) v__ = (val__); \
if (v__) { \
rc = iwrc_set_errno(IW_ERROR_THREADING_ERRNO, v__); \
goto label__; \
} \
})
#endif
#ifdef __GNUC__
#define RCRET(rc__) if (__builtin_expect((!!(rc__)), 0)) return (rc__)
#else
#define RCRET(rc__) if (rc__) return (rc__)
#endif
#define RCR(expr__) \
({ iwrc rc__ = (expr__); RCRET(rc__); 0; })
#ifdef __GNUC__
#define RCBREAK(rc__) if (__builtin_expect((!!(rc__)), 0)) break
#else
#define RCBREAK(rc__) if (rc__) break
#endif
#ifdef __GNUC__
#define RCONT(rc__) if (__builtin_expect((!!(rc__)), 0)) continue
#else
#define RCONT(rc__) if (rc__) continue
#endif
#ifndef MIN
#define MIN(a_, b_) ((a_) < (b_) ? (a_) : (b_))
#endif
#ifndef MAX
#define MAX(a_, b_) ((a_) > (b_) ? (a_) : (b_))
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/types.h>
#ifdef _WIN32
typedef _locale_t locale_t;
#endif
#if defined(__GNUC__) || defined(__clang__)
#define IW_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define IW_DEPRECATED __declspec(deprecated)
#else
#define IW_DEPRECATED
#endif
/**
* @brief The operation result status code.
*
* Zero status code `0` indicates <em>operation success</em>
*
* Status code can embed an `errno` code as operation result.
* In this case `uint32_t iwrc_strip_errno(iwrc *rc)` used
* to fetch embedded errno.
*
* @see iwlog.h
*/
typedef uint64_t iwrc;
/**
* @brief A rational number.
*/
typedef struct IW_RNUM {
int32_t n; /**< Numerator */
int32_t dn; /**< Denometator */
} IW_RNUM;
#endif