lib/sod.h: Introduce `SOD_RECKLESS' feature macro to inhibit checking.
[sod] / lib / sod.h
1 /* -*-c-*-
2 *
3 * Sensible Object Design header file
4 *
5 * (c) 2009 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Sensible Object Design, an object system for C.
11 *
12 * The SOD Runtime Library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * The SOD Runtime is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with SOD; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 #ifndef SOD_H
29 #define SOD_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Preliminary utilities ---------------------------------------------*/
36
37 /* Various hacks for checking compiler versions. */
38 #define SOD__GCC_P(maj, min) \
39 (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
40
41 #ifdef __GNUC__
42 # define SOD__EXTENSION __extension__
43 #else
44 # define SOD__EXTENSION
45 #endif
46
47 /* --- @SOD__HAVE_VARARGS_MACROS@ --- *
48 *
49 * Use: Defined if the compiler supports C99-style variadic macros.
50 *
51 * This is more complicated than just checking the value of
52 * @__STDC_VERSION__@ because GCC has traditionally claimed C89
53 * by default, but provides the functionality anyway unless it's
54 * been explicitly turned off.
55 */
56
57 #if __STDC_VERSION__ >= 199901
58 /* The feature exists. All is well with the world. */
59
60 # define SOD__HAVE_VARARGS_MACROS
61
62 #elif SOD__GCC_P(3, 0)
63 /* We're using GCC, which is trying to deny it but we don't believe it.
64 * Unfortunately there's a fly in the ointment: if `-pedantic' -- or,
65 * worse, `-pedantic-errors' -- is set, then GCC will warn about these
66 * macros being defined, and there isn't a way to detect pedantry from the
67 * preprocessor.
68 *
69 * We must deploy bodges. There doesn't seem to be a good way to suppress
70 * particular warnings from the preprocessor: in particular, messing about
71 * with `pragma GCC diagnostic' doesn't help. So we're left with this
72 * hack: just declare all Sod-generated header files which try to do
73 * varargs macro things to be `system headers', which means that GCC's
74 * preprocessor will let them get away with all manner of nefarious stuff.
75 */
76
77 # define SOD__HAVE_VARARGS_MACROS
78 # define SOD__VARARGS_MACROS_PREAMBLE _Pragma("GCC system_header")
79
80 #endif
81
82 /* Make sure this gratuitous hack is understood, at least vacuously. */
83 #ifndef SOD__VARARGS_MACROS_PREAMBLE
84 # define SOD__VARARGS_MACROS_PREAMBLE
85 #endif
86
87 /* We're going to want to make use of this ourselves. */
88 SOD__VARARGS_MACROS_PREAMBLE
89
90 /* --- @SOD__ALIGNOF@ --- *
91 *
92 * Arguments: @type@ = a C type name, consisting of declaration specifiers
93 * and `*[QUALIFIERS]' declarator operators
94 *
95 * Returns: A sufficient alignment for objects of the given @type@, as a
96 * @size_t@.
97 */
98
99 #if __STDC_VERSION__ >= 201112
100 # define SOD__ALIGNOF(type) _Alignof(type)
101 #elif SOD__GCC_P(4, 7)
102 # define SOD__ALIGNOF(type) __extension__ _Alignof(type)
103 #elif defined(__GNUC__)
104 # define SOD__ALIGNOF(type) __alignof__(type)
105 #else
106 # define SOD__ALIGNOF(type) \
107 offsetof(struct { char sod__x; type sod__y; }, sod__y)
108 #endif
109
110 /* --- @SOD__IGNORE@ --- *
111 *
112 * Arguments: @var@ = some variable name
113 *
114 * Use: Suppress any warning that @var@ isn't used.
115 */
116
117 #define SOD__IGNORE(var) ((void)(var))
118
119 /* --- @SOD__PARANOIA@ --- *
120 *
121 * Arguments: @cond@ = a condition to check
122 * @conseq@ = a thing to evaluate to if the check passes
123 * @alt@ = a thing to do if the check fails
124 *
125 * Use: Check to make sure something is good at runtime, unless
126 * disabled.
127 */
128
129 #if SOD_RECKLESS
130 # define SOD__PARANOIA(cond, conseq, alt) (conseq)
131 #else
132 # define SOD__PARANOIA(cond, conseq, alt) ((cond) ? (conseq) : (alt))
133 #endif
134
135 /* --- @SOD__CAR@ --- *
136 *
137 * Arguments: @...@ = a nonempty list of arguments
138 *
139 * Returns: The first argument only.
140 */
141
142 #ifdef SOD__HAVE_VARARGS_MACROS
143 # define SOD__CAR(...) SOD__CARx(__VA_ARGS__, _)
144 # define SOD__CARx(a, ...) a
145 #endif
146
147 /*----- Header files ------------------------------------------------------*/
148
149 #include <stdarg.h>
150 #include <stddef.h>
151
152 #include "keyword.h"
153 #include "sod-base.h"
154
155 /*----- Data structures ---------------------------------------------------*/
156
157 /* A skeletal vtable structure. At the beginning of every ichain is a
158 * pointer to one of these.
159 */
160 struct sod_vtable {
161 const SodClass *_class; /* Pointer to class object */
162 size_t _base; /* Offset to instance base */
163 };
164
165 /* A skeletal instance structure. Every instance pointer points to one of
166 * these.
167 */
168 struct sod_instance {
169 const struct sod_vtable *_vt; /* Pointer to (chain's) vtable */
170 };
171
172 /* Information about a particular chain of superclasses. In each class,
173 * there's a pointer to an array of these. If you search hard enough, you'll
174 * be able to find out a fair amount of information about an instance and its
175 * class.
176 */
177 struct sod_chain {
178 size_t n_classes; /* Number of classes in chain */
179 const SodClass *const *classes; /* Vector of classes, head first */
180 size_t off_ichain; /* Offset of ichain from base */
181 const struct sod_vtable *vt; /* Chain's vtable pointer */
182 size_t ichainsz; /* Size of the ichain structure */
183 };
184
185 /*----- Infrastructure macros ---------------------------------------------*/
186
187 /* --- @SOD_XCHAIN@ --- *
188 *
189 * Arguments: @chead@ = nickname of target chain's head
190 * @obj@ = pointer to an instance chain
191 *
192 * Returns: Pointer to target chain, as a @void *@.
193 *
194 * Use: Utility for implementing cross-chain upcasts. It's probably
195 * not that clever to use this macro directly; it's used to make
196 * the automatically-generated upcast macros more palatable.
197 */
198
199 #define SOD_XCHAIN(chead, obj) \
200 ((void *)((char *)(obj) + (obj)->_vt->_off_##chead))
201
202 /* --- @SOD_OFFSETDIFF@ --- *
203 *
204 * Arguments: @type@ = a simple (i.e., declaratorless) type name
205 * @mema, memb@ = members of @type@
206 *
207 * Returns: The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
208 *
209 * Use: Computes a signed offset between structure members.
210 */
211
212 #define SOD_OFFSETDIFF(type, mema, memb) \
213 ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
214
215 /* --- @SOD_ILAYOUT@ --- *
216 *
217 * Arguments: @cls@ = name of a class
218 * @chead@ = nickname of chain head of @cls@
219 * @obj@ = pointer to the @chead@ ichain of an (exact) instance
220 * of @cls@
221 *
222 * Returns: A pointer to the instance's base, cast as a pointer to the
223 * ilayout structure.
224 *
225 * Use: Finds an instance's base address given a pointer to one of
226 * its ichains, if you know precisely the instance's class and
227 * which chain you're pointing to. If you don't, then (a) you
228 * want @SOD_INSTBASE@ below, and (b) you'll have the wrong
229 * ilayout anyway.
230 *
231 * This macro is not intended to be used directly outside of
232 * automatically generated effective method and trampoline
233 * functions, which have the kinds of specific knowledge
234 * necessary to use it safely.
235 */
236
237 #define SOD_ILAYOUT(cls, chead, obj) \
238 ((struct cls##__ilayout *) \
239 ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
240
241 /*----- Utility macros ----------------------------------------------------*/
242
243 /* --- @SOD_CLASSOF@ --- *
244 *
245 * Arguments: @p@ = pointer to an instance chain
246 *
247 * Returns: A pointer to the instance's class, as a @const SodClass *@.
248 */
249
250 #define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
251
252 /* --- @SOD_INSTBASE@ --- *
253 *
254 * Arguments: @obj@ = pointer to an instance (i.e., the address of one of
255 * its instance chains)
256 *
257 * Returns: The base address of @obj@'s instance layout, as a @void *@.
258 *
259 * Use: Finds the base address of an instance. If you know the
260 * dynamic class of the object then @SOD_ILAYOUT@ is faster. If
261 * you don't, this is the right macro, but your options for
262 * doing something sensible with the result are limited, mostly
263 * to simple memory management operations such as freeing or
264 * zeroizing the instance structure.
265 */
266
267 #define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
268
269 /* --- @SOD_CONVERT@ --- *
270 *
271 * Arguments: @cls@ = a class type name
272 * @const void *obj@ = a pointer to an instance
273 *
274 * Returns: Pointer to appropriate instance ichain, or null if the
275 * instance isn't of the specified class.
276 *
277 * Use: This is a simple wrapper around the @sod_convert@, which
278 * you should see for full details. It accepts a class type
279 * name rather than a pointer to a class object, and arranges to
280 * return a pointer of the correct type.
281 */
282
283 #define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
284
285 /* --- @SOD_INIT@ --- *
286 *
287 * Arguments: @cls@ = a class type name
288 * @p@ = pointer to storage to initialize
289 * @keys@ = a @KWARGS(...)@ keyword argument sequence
290 *
291 * Use: Initializes raw storage as an instance of @cls@.
292 */
293
294 #define SOD_INIT(cls, p, keys) ((cls *)sod_init(cls##__class, (p), keys))
295
296 /* --- @SOD_MAKE@ --- *
297 *
298 * Arguments: @cls@ = a class type name
299 * @keys@ = a @KWARGS(...)@ keyword argument sequence
300 *
301 * Use: Allocates (using @malloc@) eand initializes storage to be an
302 * instance of @cls@. Returns a null pointer if allocation
303 * fails. Use @sod_destroy@ to release the instance.
304 */
305
306 #define SOD_MAKE(cls, keys) ((cls *)sod_make(cls##__class, keys))
307
308 /* --- @SOD_DECL@ --- *
309 *
310 * Arguments: @cls@ = a class type name
311 * @var@ = a variable name
312 * @keys@ = a @KWARGS(...)@ keyword argument sequence
313 *
314 * Use: Declare @var@ as a pointer to an initialized instance of
315 * @cls@ with automatic lifetime.
316 */
317
318 #define SOD_DECL(cls, var, keys) \
319 struct cls##__ilayout var##__layout; \
320 cls *var = (cls *)sod_init(cls##__class, &var##__layout, keys)
321
322 /*----- Functions provided ------------------------------------------------*/
323
324 /* --- @sod_subclassp@ --- *
325 *
326 * Arguments: @const SodClass *sub, *super@ = pointers to two classes
327 *
328 * Returns: Nonzero if @c@ is a subclass of @d@.
329 */
330
331 extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
332
333 /* --- @sod_convert@ --- *
334 *
335 * Arguments: @const SodClass *cls@ = desired class object
336 * @const void *obj@ = pointer to instance
337 *
338 * Returns: Pointer to appropriate ichain of object, or null if the
339 * instance isn't of the specified class.
340 *
341 * Use: General down/cross-casting function.
342 *
343 * Upcasts can be performed efficiently using the automatically
344 * generated macros. In particular, upcasts within a chain are
345 * trivial; cross-chain upcasts require information from vtables
346 * but are fairly fast. This function is rather slower, but is
347 * much more general.
348 *
349 * Suppose we have an instance of a class C, referred to by a
350 * pointer to an instance of one of C's superclasses S. If T
351 * is some other superclass of C then this function will return
352 * a pointer to C suitable for use as an instance of T. If T
353 * is not a superclass of C, then the function returns null.
354 * (If the pointer doesn't point to an instance of some class
355 * then the behaviour is undefined.) Note that you don't need
356 * to know what either C or S actually are.
357 */
358
359 extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
360
361 /* --- @sod_init@, @sod_initv@ --- *
362 *
363 * Arguments: @const SodClass *cls@ = class object for new instance
364 * @void *p@ = pointer to storage for new instance
365 * @va_list ap, ...@ = initialization keyword arguments
366 *
367 * Returns: Pointer to the initialized instance.
368 *
369 * Use: Initializes an instance in pre-allocated storage, and returns
370 * a pointer to it.
371 *
372 * This function will imprint the storage, and then send an
373 * `initialize' message to the fresh instance containing the
374 * provided keyword arguments.
375 *
376 * It's usually convenient to use the macro @SOD_INIT@ rather
377 * than calling @sod_init@ directly.
378 */
379
380 extern KWCALL void *sod_init(const SodClass */*cls*/, void */*p*/, ...);
381 extern void *sod_initv(const SodClass */*cls*/, void */*p*/, va_list /*ap*/);
382
383 /* --- @sod_make@, @sod_makev@ --- *
384 *
385 * Arguments: @const SodClass *cls@ = class object for new instance
386 * @va_list ap, ...@ = initialization keyword arguments
387 *
388 * Returns: Pointer to the newly-allocated initialized instance, or null.
389 *
390 * Use: Allocates storage for a new instance, initializes it, and
391 * returns a pointer to it. If allocation fails, a null pointer
392 * is returned instead.
393 *
394 * This function will allocate the storage using @malloc@, and
395 * then initialize it as for @sod_init@.
396 *
397 * It's usually convenient to use the macro @SOD_MAKE@ rather
398 * than calling @sod_make@ directly.
399 *
400 * (This function is not available in freestanding environments
401 * lacking @malloc@ and @free@.)
402 */
403
404 extern KWCALL void *sod_make(const SodClass */*cls*/, ...);
405 extern void *sod_makev(const SodClass */*cls*/, va_list /*ap*/);
406
407 /* --- @sod_teardown@ --- *
408 *
409 * Arguments: @void *p@ = pointer to an instance to be torn down
410 *
411 * Returns: Zero if the object is torn down; nonzero if it refused for
412 * some reason.
413 *
414 * Use: Invokes the instance's `teardown' method to release any held
415 * resources.
416 *
417 * If this function returns nonzero, then the object is still
418 * active, and may still hold important resources. This is not
419 * intended to be a failure condition: failures in teardown are
420 * usually unrecoverable (or very hard to recover from) and
421 * should probably cause the program to abort. A refusal, on
422 * the other hand, means that the object is still live and
423 * shouldn't be deallocated, but that this is a normal situation
424 * and the caller shouldn't worry about it.
425 */
426
427 extern int sod_teardown(void */*p*/);
428
429 /* --- @sod_destroy@ --- *
430 *
431 * Arguments: @void *p@ = pointer to an instance to be torn down, or null
432 *
433 * Returns: Zero if the object was freed; nonzero if it refused for some
434 * reason.
435 *
436 * Use: Invokes the instance's `teardown' method to release any held
437 * resources, and then calls @free@ to release the instance's
438 * storage. See @sod_teardown@ for details, especially
439 * regarding the return value's meaning.
440 *
441 * If @p@ is null, then this function does nothing except
442 * returns zero.
443 *
444 * (This function is not available in freestanding environments
445 * lacking @malloc@ and @free@.)
446 */
447
448 extern int sod_destroy(void */*p*/);
449
450 /*----- That's all, folks -------------------------------------------------*/
451
452 #ifdef __cplusplus
453 }
454 #endif
455
456 #endif