|
24 | 24 | * used across all ZDK libraries. |
25 | 25 | * |
26 | 26 | * License: MIT |
| 27 | + * Author: Zuhaitz |
| 28 | + * Repository: https://github.com/z-libs/z-core |
| 29 | + * Version: 1.0.0 |
27 | 30 | */ |
28 | 31 |
|
29 | 32 | #ifndef ZCOMMON_H |
|
36 | 39 | #include <string.h> |
37 | 40 |
|
38 | 41 | // Return codes and error handling. |
39 | | - |
40 | | -// Success. |
41 | 42 | #define Z_OK 0 |
42 | 43 | #define Z_FOUND 1 // Element found (positive). |
43 | | - |
44 | | -// Generic errors. |
45 | 44 | #define Z_ERR -1 // Generic error. |
46 | | - |
47 | | -// Resource errors. |
48 | 45 | #define Z_ENOMEM -2 // Out of memory (malloc/realloc failed). |
49 | | - |
50 | | -// Access errors. |
51 | 46 | #define Z_EOOB -3 // Out of bounds / range error. |
52 | 47 | #define Z_EEMPTY -4 // Container is empty. |
53 | 48 | #define Z_ENOTFOUND -5 // Element not found. |
54 | | - |
55 | | -// Logic errors. |
56 | 49 | #define Z_EINVAL -6 // Invalid argument / parameter. |
57 | | -#define Z_EEXIST -7 // Element already exists (for example, unique keys). |
| 50 | +#define Z_EEXIST -7 // Element already exists. |
58 | 51 |
|
59 | 52 | // Memory management. |
60 | 53 |
|
|
72 | 65 |
|
73 | 66 | // Compiler extensions and optimization. |
74 | 67 |
|
75 | | -/* * We check for GCC/Clang features to enable RAII-style cleanup and optimization hints. |
76 | | - * Define Z_NO_EXTENSIONS to disable this manually. |
77 | | - */ |
78 | | -#if !defined(Z_NO_EXTENSIONS) && (defined(__GNUC__) || defined(__clang__)) |
| 68 | +// Type inference (typeof) |
| 69 | +#ifdef __cplusplus |
| 70 | +# include <type_traits> |
| 71 | +# define Z_TYPEOF(x) typename std::remove_reference<decltype(x)>::type |
| 72 | +# define Z_HAS_TYPEOF 1 |
| 73 | +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L |
| 74 | +# define Z_TYPEOF(x) typeof(x) |
| 75 | +# define Z_HAS_TYPEOF 1 |
| 76 | +#elif defined(__GNUC__) || defined(__clang__) || defined(__TINYC__) |
| 77 | +# define Z_TYPEOF(x) __typeof__(x) |
| 78 | +# define Z_HAS_TYPEOF 1 |
| 79 | +#else |
| 80 | +# define Z_HAS_TYPEOF 0 |
| 81 | +#endif |
| 82 | + |
| 83 | +// Extensions (cleanup, attributes, branch prediction) |
| 84 | +#if !defined(Z_NO_EXTENSIONS) && (defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)) |
79 | 85 |
|
80 | 86 | # define Z_HAS_CLEANUP 1 |
81 | | - |
82 | | - // RAII cleanup (destructors). |
83 | | - // Usage: zvec_autofree(Int) v = zvec_init(Int); |
84 | 87 | # define Z_CLEANUP(func) __attribute__((cleanup(func))) |
85 | | - |
86 | | - // Warn if the return value (e.g., an Error Result) is ignored. |
87 | 88 | # define Z_NODISCARD __attribute__((warn_unused_result)) |
88 | 89 |
|
89 | | - // Branch prediction hints for the compiler. |
90 | | -# define Z_LIKELY(x) __builtin_expect(!!(x), 1) |
91 | | -# define Z_UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 90 | + // TCC supports attributes but NOT __builtin_expect |
| 91 | +# if defined(__TINYC__) |
| 92 | +# define Z_LIKELY(x) (x) |
| 93 | +# define Z_UNLIKELY(x) (x) |
| 94 | +# else |
| 95 | +# define Z_LIKELY(x) __builtin_expect(!!(x), 1) |
| 96 | +# define Z_UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 97 | +# endif |
92 | 98 |
|
93 | 99 | #else |
94 | | - |
| 100 | + // Fallback for MSVC or strict standard C. |
95 | 101 | # define Z_HAS_CLEANUP 0 |
96 | 102 | # define Z_CLEANUP(func) |
97 | 103 | # define Z_NODISCARD |
|
110 | 116 | #define DEFINE_LIST_TYPE(T, Name) |
111 | 117 | #define DEFINE_MAP_TYPE(Key, Val, Name) |
112 | 118 | #define DEFINE_STABLE_MAP_TYPE(Key, Val, Name) |
| 119 | +#define DEFINE_TREE_TYPE(Key, Val, Name) |
113 | 120 |
|
114 | 121 | // Token concatenation macros (useful for unique variable names in macros). |
115 | 122 | #define Z_CONCAT_(a, b) a ## b |
|
0 commit comments