Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/support/element-impl.h

raw
1 #ifndef CARYLL_SUPPORT_ELEMENT_IMPL_H
2 #define CARYLL_SUPPORT_ELEMENT_IMPL_H
3
4 #include "caryll/element.h"
5 #include "otfcc-alloc.h"
6
7 #ifndef __CARYLL_INLINE__
8 #ifdef _MSC_VER
9 #define __CARYLL_INLINE__ __forceinline /* use __forceinline (VC++ specific) */
10 #else
11 #define __CARYLL_INLINE__ inline /* use standard inline */
12 #endif
13 #endif
14
15 #define __NARG__(...) __NARG_I_(__VA_ARGS__, __RSEQ_N())
16 #define __NARG_I_(...) __ARG_N(__VA_ARGS__)
17 #define __ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, \
18 _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, \
19 _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
20 _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, N, ...) \
21 N
22 #define __RSEQ_N() \
23 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
24 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, \
25 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
26
27 // general definition for any function name
28 #define __CARYLL_VFUNC__2(name, n) name##n
29 #define __CARYLL_VFUNC__1(name, n) __CARYLL_VFUNC__2(name, n)
30 #define __CARYLLVFUNC(func, ...) __CARYLL_VFUNC__1(func, __NARG__(__VA_ARGS__))(__VA_ARGS__)
31
32 #define caryll_trivialInit(T) \
33 static __CARYLL_INLINE__ void T##_init(MODIFY T *x) { \
34 memset(x, 0, sizeof(T)); \
35 }
36 #define caryll_nonTrivialInit(T, __fn_init) \
37 static __CARYLL_INLINE__ void T##_init(MODIFY T *x) { \
38 (__fn_init)(x); \
39 }
40 #define caryll_trivialEmpty(T) \
41 static __CARYLL_INLINE__ T T##_empty() { \
42 T x; \
43 T##_init(&x); \
44 return x; \
45 }
46 #define caryll_trivialCreate(T) \
47 static __CARYLL_INLINE__ T *T##_create() { \
48 T *x = (T *)malloc(sizeof(T)); \
49 T##_init(x); \
50 return x; \
51 }
52 #define caryll_trivialCopy(T) \
53 static __CARYLL_INLINE__ void T##_copy(MODIFY T *dst, const T *src) { \
54 memcpy(dst, src, sizeof(T)); \
55 }
56 #define caryll_nonTrivialCopy(T, __fn_copy) \
57 static __CARYLL_INLINE__ void T##_copy(MODIFY T *dst, const T *src) { \
58 (__fn_copy)(dst, src); \
59 }
60 #define caryll_trivialMove(T) \
61 static __CARYLL_INLINE__ void T##_move(MODIFY T *dst, T *src) { \
62 memcpy(dst, src, sizeof(T)); \
63 T##_init(src); \
64 }
65 #define caryll_nonTrivialMove(T, __fn_move) \
66 static __CARYLL_INLINE__ void T##_move(MODIFY T *dst, T *src) { \
67 (__fn_move)(dst, src); \
68 T##_init(src); \
69 }
70 #define caryll_trivialDispose(T) \
71 static __CARYLL_INLINE__ void T##_dispose(MODIFY T *x) { \
72 /* trivial */ \
73 }
74 #define caryll_nonTrivialDispose(T, __fn_dispose) \
75 static __CARYLL_INLINE__ void T##_dispose(MODIFY T *x) { \
76 (__fn_dispose)(x); \
77 }
78 #define caryll_trivialFree(T) \
79 static __CARYLL_INLINE__ void T##_free(MOVE T *x) { \
80 if (!x) return; \
81 T##_dispose(x); \
82 __caryll_free(x); \
83 }
84 #define caryll_trivialDup(T) \
85 static __CARYLL_INLINE__ T T##_dup(const T src) { \
86 T dst; \
87 T##_copy(&dst, &src); \
88 return dst; \
89 }
90 #define caryll_trivialReplace(T) \
91 static __CARYLL_INLINE__ void T##_replace(MODIFY T *dst, MOVE const T src) { \
92 T##_dispose(dst); \
93 memcpy(dst, &src, sizeof(T)); \
94 } \
95 static __CARYLL_INLINE__ void T##_copyReplace(MODIFY T *dst, const T src) { \
96 T##_dispose(dst); \
97 T##_copy(dst, &src); \
98 }
99
100 #define caryll_standardTypeFn1(T) \
101 caryll_trivialInit(T) \
102 caryll_trivialCopy(T) \
103 caryll_trivialDispose(T) \
104 caryll_trivialMove(T) \
105 caryll_trivialReplace(T)
106 #define caryll_standardTypeFn2(T, __fn_dispose) \
107 caryll_trivialInit(T) \
108 caryll_trivialCopy(T) \
109 caryll_nonTrivialDispose(T, __fn_dispose) \
110 caryll_trivialMove(T) \
111 caryll_trivialReplace(T)
112 #define caryll_standardTypeFn3(T, __fn_init, __fn_dispose) \
113 caryll_nonTrivialInit(T, __fn_init) \
114 caryll_trivialCopy(T) \
115 caryll_nonTrivialDispose(T, __fn_dispose) \
116 caryll_trivialMove(T) \
117 caryll_trivialReplace(T)
118 #define caryll_standardTypeFn4(T, __fn_init, __fn_copy, __fn_dispose) \
119 caryll_nonTrivialInit(T, __fn_init) \
120 caryll_nonTrivialCopy(T, __fn_copy) \
121 caryll_nonTrivialDispose(T, __fn_dispose) \
122 caryll_trivialMove(T) \
123 caryll_trivialReplace(T)
124 #define caryll_standardTypeFn5(T, __fn_init, __fn_copy, __fn_dispose, __fn_move) \
125 caryll_nonTrivialInit(T, __fn_init) \
126 caryll_nonTrivialCopy(T, __fn_copy) \
127 caryll_nonTrivialDispose(T, __fn_dispose) \
128 caryll_nonTrivialMove(T, __fn_move) \
129 caryll_trivialReplace(T)
130
131 #define caryll_standardTypeMethods(T) \
132 .init = T##_init, .copy = T##_copy, .dispose = T##_dispose, .move = T##_move, \
133 .replace = T##_replace, .copyReplace = T##_copyReplace
134 #define caryll_standardRefTypeMethods(T) \
135 caryll_standardTypeMethods(T), .create = T##_create, .free = T##_free
136 #define caryll_standardValTypeMethods(T) \
137 caryll_standardTypeMethods(T), .empty = T##_empty, .dup = T##_dup
138
139 #define caryll_standardTypeFn(...) __CARYLLVFUNC(caryll_standardTypeFn, __VA_ARGS__)
140
141 #ifdef __GNUC__
142
143 // GCC requires adding ## before __VA_ARGS__.
144 // Make it happy.
145
146 #define caryll_standardRefTypeFn(T, ...) \
147 caryll_standardTypeFn(T, ##__VA_ARGS__); \
148 caryll_trivialCreate(T); \
149 caryll_trivialFree(T);
150 #define caryll_standardValTypeFn(T, ...) \
151 caryll_standardTypeFn(T, ##__VA_ARGS__); \
152 caryll_trivialEmpty(T); \
153 caryll_trivialDup(T);
154
155 #define caryll_standardType(T, __name, ...) \
156 caryll_standardTypeFn(T, ##__VA_ARGS__); \
157 caryll_ElementInterfaceOf(T) __name = {caryll_standardTypeMethods(T)};
158 #define caryll_standardRefType(T, __name, ...) \
159 caryll_standardRefTypeFn(T, ##__VA_ARGS__); \
160 caryll_ElementInterfaceOf(T) __name = {caryll_standardRefTypeMethods(T)};
161 #define caryll_standardValType(T, __name, ...) \
162 caryll_standardValTypeFn(T, ##__VA_ARGS__); \
163 caryll_ElementInterfaceOf(T) __name = {caryll_standardValTypeMethods(T)};
164
165 #else
166
167 #define caryll_standardRefTypeFn(T, ...) \
168 caryll_standardTypeFn(T, __VA_ARGS__) \
169 caryll_trivialCreate(T) \
170 caryll_trivialFree(T)
171 #define caryll_standardValTypeFn(T, ...) \
172 caryll_standardTypeFn(T, __VA_ARGS__) \
173 caryll_trivialEmpty(T) \
174 caryll_trivialDup(T)
175
176 #define caryll_standardType(T, __name, ...) \
177 caryll_standardTypeFn(T, __VA_ARGS__) \
178 caryll_ElementInterfaceOf(T) __name = {caryll_standardTypeMethods(T)}
179 #define caryll_standardRefType(T, __name, ...) \
180 caryll_standardRefTypeFn(T, __VA_ARGS__) \
181 caryll_ElementInterfaceOf(T) __name = {caryll_standardRefTypeMethods(T)}
182 #define caryll_standardValType(T, __name, ...) \
183 caryll_standardValTypeFn(T, __VA_ARGS__) \
184 caryll_ElementInterfaceOf(T) __name = {caryll_standardValTypeMethods(T)}
185
186 #endif
187
188 //// Traits
189
190 // Show
191 #define caryll_ShowFns(T, _fn_Show) \
192 static __CARYLL_INLINE__ void T##_show(const T a) { \
193 return (_fn_Show)(a); \
194 }
195 #define caryll_ShowAssigns(T) .show = T##_show
196
197 // Ord
198 #define caryll_OrdFns(T, _fnCompare) \
199 static __CARYLL_INLINE__ int T##_compare(const T a, const T b) { \
200 return (_fnCompare)(a, b); \
201 } \
202 static __CARYLL_INLINE__ int T##_compareRef(const T *a, const T *b) { \
203 return (_fnCompare)(*a, *b); \
204 }
205 #define caryll_OrdEqFns(T, _fnCompare) \
206 caryll_OrdFns(T, _fnCompare) \
207 static __CARYLL_INLINE__ bool T##_equal(const T a, const T b) { \
208 return !(_fnCompare)(a, b); \
209 }
210 #define caryll_OrdEqAssigns(T) \
211 .compare = T##_compare, .compareRef = T##_compareRef, .equal = T##_equal
212
213 // Monoid
214 #define caryll_MonoidFns(T, _fn_neutral, _fn_inplacePlus) \
215 static __CARYLL_INLINE__ T T##_neutral() { \
216 return (_fn_neutral)(); \
217 } \
218 static __CARYLL_INLINE__ void T##_inplacePlus(MODIFY T *a, const T b) { \
219 (_fn_inplacePlus)(a, b); \
220 } \
221 static __CARYLL_INLINE__ T T##_plus(const T a, const T b) { \
222 T result = (_fn_neutral)(); \
223 (_fn_inplacePlus)(&result, a); \
224 (_fn_inplacePlus)(&result, b); \
225 return result; \
226 }
227 #define caryll_MonoidAssigns(T) \
228 .neutral = T##_neutral, .inplacePlus = T##_inplacePlus, .plus = T##_plus
229
230 // Group
231 #define caryll_GroupFns(T, _fn_inplaceNegate) \
232 static __CARYLL_INLINE__ void T##_inplaceNegate(MODIFY T *a) { \
233 (_fn_inplaceNegate)(a); \
234 } \
235 static __CARYLL_INLINE__ T T##_negate(const T a) { \
236 T result; \
237 T##_copy(&result, &a); \
238 T##_inplaceNegate(&result); \
239 return result; \
240 } \
241 static __CARYLL_INLINE__ void T##_inplaceMinus(MODIFY T *a, const T b) { \
242 T tb = T##_negate(b); \
243 (T##_inplacePlus)(a, tb); \
244 T##_dispose(&tb); \
245 } \
246 static __CARYLL_INLINE__ T T##_minus(const T a, const T b) { \
247 T result = (T##_neutral)(); \
248 (T##_inplacePlus)(&result, a); \
249 (T##_inplaceMinus)(&result, b); \
250 return result; \
251 }
252 #define caryll_GroupAssigns(T) \
253 .inplaceNegate = T##_inplaceNegate, .negate = T##_negate, .inplaceMinus = T##_inplaceMinus, \
254 .minus = T##_minus
255
256 // Module
257 #define caryll_ModuleFns(T, TS, _fn_inplaceScale) \
258 static __CARYLL_INLINE__ void T##_inplaceScale(MODIFY T *a, TS b) { \
259 (_fn_inplaceScale)(a, b); \
260 } \
261 static __CARYLL_INLINE__ T T##_scale(const T a, TS b) { \
262 T result; \
263 T##_copy(&result, &a); \
264 T##_inplaceScale(&result, b); \
265 return result; \
266 } \
267 static __CARYLL_INLINE__ void T##_inplacePlusScale(MODIFY T *a, TS b, const T c) { \
268 T x = T##_scale(c, b); \
269 T##_inplacePlus(a, x); \
270 T##_dispose(&x); \
271 }
272 #define caryll_ModuleAssigns(T) \
273 .inplaceScale = T##_inplaceScale, .scale = T##_scale, .inplacePlusScale = T##_inplacePlusScale
274
275 #endif