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