chiark / gitweb /
5a76f930a6fafc30cbac9efa11a0d9f6790f994e
[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 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdarg.h>
38 #include <stddef.h>
39
40 #include "sod-base.h"
41
42 /*----- Data structures ---------------------------------------------------*/
43
44 /* A skeletal vtable structure.  At the beginning of every ichain is a
45  * pointer to one of these.
46  */
47 struct sod_vtable {
48   const SodClass *_class;               /* Pointer to class object */
49   size_t _base;                         /* Offset to instance base */
50 };
51
52 /* A skeletal instance structure.  Every instance pointer points to one of
53  * these.
54  */
55 struct sod_instance {
56   const struct sod_vtable *_vt;         /* Pointer to (chain's) vtable */
57 };
58
59 /* Information about a particular chain of superclasses.  In each class,
60  * there's a pointer to an array of these.  If you search hard enough, you'll
61  * be able to find out a fair amount of information about an instance and its
62  * class.
63  */
64 struct sod_chain {
65   size_t n_classes;                     /* Number of classes in chain */
66   const SodClass *const *classes;       /* Vector of classes, head first */
67   size_t off_ichain;                    /* Offset of ichain from base */
68   const struct sod_vtable *vt;          /* Chain's vtable pointer */
69   size_t ichainsz;                      /* Size of the ichain structure */
70 };
71
72 /*----- Infrastructure macros ---------------------------------------------*/
73
74 /* --- @SOD_XCHAIN@ --- *
75  *
76  * Arguments:   @chead@ = nickname of target chain's head
77  *              @obj@ = pointer to an instance chain
78  *
79  * Returns:     Pointer to target chain, as a @void *@.
80  *
81  * Use:         Utility for implementing cross-chain upcasts.  It's probably
82  *              not that clever to use this macro directly; it's used to make
83  *              the automatically-generated upcast macros more palatable.
84  */
85
86 #define SOD_XCHAIN(chead, obj)                                          \
87   ((void *)((char *)(obj) + (obj)->_vt->_off_##chead))
88
89 /* --- @SOD_OFFSETDIFF@ --- *
90  *
91  * Arguments:   @type@ = a simple (i.e., declaratorless) type name
92  *              @mema, memb@ = members of @type@
93  *
94  * Returns:     The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
95  *
96  * Use:         Computes a signed offset between structure members.
97  */
98
99 #define SOD_OFFSETDIFF(type, mema, memb)                                \
100   ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
101
102 /* --- @SOD_ILAYOUT@ --- *
103  *
104  * Arguments:   @cls@ = name of a class
105  *              @chead@ = nickname of chain head of @cls@
106  *              @obj@ = pointer to the @chead@ ichain of an (exact) instance
107  *                      of @cls@
108  *
109  * Returns:     A pointer to the instance's base, cast as a pointer to the
110  *              ilayout structure.
111  *
112  * Use:         Finds an instance's base address given a pointer to one of
113  *              its ichains, if you know precisely the instance's class and
114  *              which chain you're pointing to.  If you don't, then (a) you
115  *              want @SOD_INSTBASE@ below, and (b) you'll have the wrong
116  *              ilayout anyway.
117  *
118  *              This macro is not intended to be used directly outside of
119  *              automatically generated effective method and trampoline
120  *              functions, which have the kinds of specific knowledge
121  *              necessary to use it safely.
122  */
123
124 #define SOD_ILAYOUT(cls, chead, obj)                                    \
125   ((struct cls##__ilayout *)                                            \
126    ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
127
128 /* --- @SOD_CAR@ --- *
129  *
130  * Arguments:   @...@ = a nonempty list of arguments
131  *
132  * Returns:     The first argument only.
133  */
134
135 #if __STDC_VERSION__ >= 199901
136 #  define SOD_CAR(...) SOD__CARx(__VA_LIST__, _)
137 #  define SOD__CARx(a, ...) a
138 #endif
139
140 /*----- Utility macros ----------------------------------------------------*/
141
142 /* --- @SOD_CLASSOF@ --- *
143  *
144  * Arguments:   @p@ = pointer to an instance chain
145  *
146  * Returns:     A pointer to the instance's class, as a const SodClass.
147  */
148
149 #define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
150
151 /* --- @SOD_INSTBASE@ --- *
152  *
153  * Arguments:   @obj@ = pointer to an instance (i.e., the address of one of
154  *                      its instance chains)
155  *
156  * Returns:     The base address of @obj@'s instance layout, as a @void *@.
157  *
158  * Use:         Finds the base address of an instance.  If you know the
159  *              dynamic class of the object then @SOD_ILAYOUT@ is faster.  If
160  *              you don't, this is the right macro, but your options for
161  *              doing something sensible with the result are limited, mostly
162  *              to simple memory management operations such as freeing or
163  *              zeroizing the instance structure.
164  */
165
166 #define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
167
168 /* --- @SOD_CONVERT@ --- *
169  *
170  * Arguments:   @cls@ = a class type name
171  *              @const void *obj@ = a pointer to an instance
172  *
173  * Returns:     Pointer to appropriate instance ichain, or null if the
174  *              instance isn't of the specified class.
175  *
176  * Use:         This is a simple wrapper around the @sod_convert@, which
177  *              you should see for full details.  It accepts a class type
178  *              name rather than a pointer to a class object, and arranges to
179  *              return a pointer of the correct type.
180  */
181
182 #define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
183
184 /* --- @SOD_DECL@ --- *
185  *
186  * Arguments:   @cls_@ = a class type name
187  *              @var_@ = a variable name
188  *
189  * Use:         Declare @var_@ as a pointer to an initialized instance of
190  *              @cls_@ with automatic lifetime.
191  */
192
193 #define SOD_DECL(cls_, var_)                                            \
194   struct cls_##__ilayout var_##__layout;                                \
195   cls_ *var_ = cls_##__class->cls.init(&var_##__layout)
196
197 /*----- Functions provided ------------------------------------------------*/
198
199 /* --- @sod_subclassp@ --- *
200  *
201  * Arguments:   @const SodClass *sub, *super@ = pointers to two classes
202  *
203  * Returns:     Nonzero if @c@ is a subclass of @d@.
204  */
205
206 extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
207
208 /* --- @sod_convert@ --- *
209  *
210  * Arguments:   @const SodClass *cls@ = desired class object
211  *              @const void *obj@ = pointer to instance
212  *
213  * Returns:     Pointer to appropriate ichain of object, or null if the
214  *              instance isn't of the specified class.
215  *
216  * Use:         General down/cross-casting function.
217  *
218  *              Upcasts can be performed efficiently using the automatically
219  *              generated macros.  In particular, upcasts within a chain are
220  *              trivial; cross-chain upcasts require information from vtables
221  *              but are fairly fast.  This function is rather slower, but is
222  *              much more general.
223  *
224  *              Suppose we have an instance of a class C, referred to by a
225  *              pointer to an instance of one of C's superclasses S.  If T
226  *              is some other superclass of C then this function will return
227  *              a pointer to C suitable for use as an instance of T.  If T
228  *              is not a superclass of C, then the function returns null.
229  *              (If the pointer doesn't point to an instance of some class
230  *              then the behaviour is undefined.)  Note that you don't need
231  *              to know what either C or S actually are.
232  */
233
234 extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
235
236 /*----- That's all, folks -------------------------------------------------*/
237
238 #ifdef __cplusplus
239   }
240 #endif
241
242 #endif