chiark / gitweb /
360afa83907a9c1b4e2eccae023308eefdb9f633
[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 /* --- @SOD__HAVE_VARARGS_MACROS@ --- *
38  *
39  * Use:         Defined if the compiler supports C99-style variadic macros.
40  *
41  *              This is more complicated than just checking the value of
42  *              @__STDC_VERSION__@ because GCC has traditionally claimed C89
43  *              by default, but provides the functionality anyway unless it's
44  *              been explicitly turned off.
45  */
46
47 #if __STDC_VERSION__ >= 199901
48    /* The feature exists.  All is well with the world. */
49
50 #  define SOD__HAVE_VARARGS_MACROS
51
52 #elif __GNUC__ >= 3
53    /* We're using GCC, which is trying to deny it but we don't believe it.
54     * Unfortunately there's a fly in the ointment: if `-pedantic' -- or,
55     * worse, `-pedantic-errors' -- is set, then GCC will warn about these
56     * macros being defined, and there isn't a way to detect pedantry from the
57     * preprocessor.
58     *
59     * We must deploy bodges.  There doesn't seem to be a good way to suppress
60     * particular warnings from the preprocessor: in particular, messing about
61     * with `pragma GCC diagnostic' doesn't help.  So we're left with this
62     * hack: just declare all Sod-generated header files which try to do
63     * varargs macro things to be `system headers', which means that GCC's
64     * preprocessor will let them get away with all manner of nefarious stuff.
65     */
66
67 #  define SOD__HAVE_VARARGS_MACROS
68 #  define SOD__VARARGS_MACROS_PREAMBLE _Pragma("GCC system_header")
69
70 #endif
71
72 /* Make sure this gratuitous hack is understood, at least vacuously. */
73 #ifndef SOD__VARARGS_MACROS_PREAMBLE
74 #  define SOD__VARARGS_MACROS_PREAMBLE
75 #endif
76
77 /* We're going to want to make use of this ourselves. */
78 SOD__VARARGS_MACROS_PREAMBLE
79
80 /* --- @SOD__CAR@ --- *
81  *
82  * Arguments:   @...@ = a nonempty list of arguments
83  *
84  * Returns:     The first argument only.
85  */
86
87 #ifdef SOD__HAVE_VARARGS_MACROS
88 #  define SOD__CAR(...) SOD__CARx(__VA_LIST__, _)
89 #  define SOD__CARx(a, ...) a
90 #endif
91
92 /*----- Header files ------------------------------------------------------*/
93
94 #include <stdarg.h>
95 #include <stddef.h>
96
97 #include "sod-base.h"
98
99 /*----- Data structures ---------------------------------------------------*/
100
101 /* A skeletal vtable structure.  At the beginning of every ichain is a
102  * pointer to one of these.
103  */
104 struct sod_vtable {
105   const SodClass *_class;               /* Pointer to class object */
106   size_t _base;                         /* Offset to instance base */
107 };
108
109 /* A skeletal instance structure.  Every instance pointer points to one of
110  * these.
111  */
112 struct sod_instance {
113   const struct sod_vtable *_vt;         /* Pointer to (chain's) vtable */
114 };
115
116 /* Information about a particular chain of superclasses.  In each class,
117  * there's a pointer to an array of these.  If you search hard enough, you'll
118  * be able to find out a fair amount of information about an instance and its
119  * class.
120  */
121 struct sod_chain {
122   size_t n_classes;                     /* Number of classes in chain */
123   const SodClass *const *classes;       /* Vector of classes, head first */
124   size_t off_ichain;                    /* Offset of ichain from base */
125   const struct sod_vtable *vt;          /* Chain's vtable pointer */
126   size_t ichainsz;                      /* Size of the ichain structure */
127 };
128
129 /*----- Infrastructure macros ---------------------------------------------*/
130
131 /* --- @SOD_XCHAIN@ --- *
132  *
133  * Arguments:   @chead@ = nickname of target chain's head
134  *              @obj@ = pointer to an instance chain
135  *
136  * Returns:     Pointer to target chain, as a @void *@.
137  *
138  * Use:         Utility for implementing cross-chain upcasts.  It's probably
139  *              not that clever to use this macro directly; it's used to make
140  *              the automatically-generated upcast macros more palatable.
141  */
142
143 #define SOD_XCHAIN(chead, obj)                                          \
144   ((void *)((char *)(obj) + (obj)->_vt->_off_##chead))
145
146 /* --- @SOD_OFFSETDIFF@ --- *
147  *
148  * Arguments:   @type@ = a simple (i.e., declaratorless) type name
149  *              @mema, memb@ = members of @type@
150  *
151  * Returns:     The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
152  *
153  * Use:         Computes a signed offset between structure members.
154  */
155
156 #define SOD_OFFSETDIFF(type, mema, memb)                                \
157   ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
158
159 /* --- @SOD_ILAYOUT@ --- *
160  *
161  * Arguments:   @cls@ = name of a class
162  *              @chead@ = nickname of chain head of @cls@
163  *              @obj@ = pointer to the @chead@ ichain of an (exact) instance
164  *                      of @cls@
165  *
166  * Returns:     A pointer to the instance's base, cast as a pointer to the
167  *              ilayout structure.
168  *
169  * Use:         Finds an instance's base address given a pointer to one of
170  *              its ichains, if you know precisely the instance's class and
171  *              which chain you're pointing to.  If you don't, then (a) you
172  *              want @SOD_INSTBASE@ below, and (b) you'll have the wrong
173  *              ilayout anyway.
174  *
175  *              This macro is not intended to be used directly outside of
176  *              automatically generated effective method and trampoline
177  *              functions, which have the kinds of specific knowledge
178  *              necessary to use it safely.
179  */
180
181 #define SOD_ILAYOUT(cls, chead, obj)                                    \
182   ((struct cls##__ilayout *)                                            \
183    ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
184
185 /*----- Utility macros ----------------------------------------------------*/
186
187 /* --- @SOD_CLASSOF@ --- *
188  *
189  * Arguments:   @p@ = pointer to an instance chain
190  *
191  * Returns:     A pointer to the instance's class, as a @const SodClass *@.
192  */
193
194 #define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
195
196 /* --- @SOD_INSTBASE@ --- *
197  *
198  * Arguments:   @obj@ = pointer to an instance (i.e., the address of one of
199  *                      its instance chains)
200  *
201  * Returns:     The base address of @obj@'s instance layout, as a @void *@.
202  *
203  * Use:         Finds the base address of an instance.  If you know the
204  *              dynamic class of the object then @SOD_ILAYOUT@ is faster.  If
205  *              you don't, this is the right macro, but your options for
206  *              doing something sensible with the result are limited, mostly
207  *              to simple memory management operations such as freeing or
208  *              zeroizing the instance structure.
209  */
210
211 #define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
212
213 /* --- @SOD_CONVERT@ --- *
214  *
215  * Arguments:   @cls@ = a class type name
216  *              @const void *obj@ = a pointer to an instance
217  *
218  * Returns:     Pointer to appropriate instance ichain, or null if the
219  *              instance isn't of the specified class.
220  *
221  * Use:         This is a simple wrapper around the @sod_convert@, which
222  *              you should see for full details.  It accepts a class type
223  *              name rather than a pointer to a class object, and arranges to
224  *              return a pointer of the correct type.
225  */
226
227 #define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
228
229 /* --- @SOD_DECL@ --- *
230  *
231  * Arguments:   @cls_@ = a class type name
232  *              @var_@ = a variable name
233  *
234  * Use:         Declare @var_@ as a pointer to an initialized instance of
235  *              @cls_@ with automatic lifetime.
236  */
237
238 #define SOD_DECL(cls_, var_)                                            \
239   struct cls_##__ilayout var_##__layout;                                \
240   cls_ *var_ = cls_##__class->cls.init(&var_##__layout)
241
242 /*----- Functions provided ------------------------------------------------*/
243
244 /* --- @sod_subclassp@ --- *
245  *
246  * Arguments:   @const SodClass *sub, *super@ = pointers to two classes
247  *
248  * Returns:     Nonzero if @c@ is a subclass of @d@.
249  */
250
251 extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
252
253 /* --- @sod_convert@ --- *
254  *
255  * Arguments:   @const SodClass *cls@ = desired class object
256  *              @const void *obj@ = pointer to instance
257  *
258  * Returns:     Pointer to appropriate ichain of object, or null if the
259  *              instance isn't of the specified class.
260  *
261  * Use:         General down/cross-casting function.
262  *
263  *              Upcasts can be performed efficiently using the automatically
264  *              generated macros.  In particular, upcasts within a chain are
265  *              trivial; cross-chain upcasts require information from vtables
266  *              but are fairly fast.  This function is rather slower, but is
267  *              much more general.
268  *
269  *              Suppose we have an instance of a class C, referred to by a
270  *              pointer to an instance of one of C's superclasses S.  If T
271  *              is some other superclass of C then this function will return
272  *              a pointer to C suitable for use as an instance of T.  If T
273  *              is not a superclass of C, then the function returns null.
274  *              (If the pointer doesn't point to an instance of some class
275  *              then the behaviour is undefined.)  Note that you don't need
276  *              to know what either C or S actually are.
277  */
278
279 extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
280
281 /*----- That's all, folks -------------------------------------------------*/
282
283 #ifdef __cplusplus
284   }
285 #endif
286
287 #endif