3 * Sensible Object Design header file
5 * (c) 2009 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Sensible Object Design, an object system for C.
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.
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.
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,
35 /*----- Preliminary utilities ---------------------------------------------*/
37 /* --- @SOD__HAVE_VARARGS_MACROS@ --- *
39 * Use: Defined if the compiler supports C99-style variadic macros.
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.
47 #if __STDC_VERSION__ >= 199901
48 /* The feature exists. All is well with the world. */
50 # define SOD__HAVE_VARARGS_MACROS
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
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.
67 # define SOD__HAVE_VARARGS_MACROS
68 # define SOD__VARARGS_MACROS_PREAMBLE _Pragma("GCC system_header")
72 /* Make sure this gratuitous hack is understood, at least vacuously. */
73 #ifndef SOD__VARARGS_MACROS_PREAMBLE
74 # define SOD__VARARGS_MACROS_PREAMBLE
77 /* We're going to want to make use of this ourselves. */
78 SOD__VARARGS_MACROS_PREAMBLE
80 /* --- @SOD__CAR@ --- *
82 * Arguments: @...@ = a nonempty list of arguments
84 * Returns: The first argument only.
87 #ifdef SOD__HAVE_VARARGS_MACROS
88 # define SOD__CAR(...) SOD__CARx(__VA_LIST__, _)
89 # define SOD__CARx(a, ...) a
92 /*----- Header files ------------------------------------------------------*/
99 /*----- Data structures ---------------------------------------------------*/
101 /* A skeletal vtable structure. At the beginning of every ichain is a
102 * pointer to one of these.
105 const SodClass *_class; /* Pointer to class object */
106 size_t _base; /* Offset to instance base */
109 /* A skeletal instance structure. Every instance pointer points to one of
112 struct sod_instance {
113 const struct sod_vtable *_vt; /* Pointer to (chain's) vtable */
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
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 */
129 /*----- Infrastructure macros ---------------------------------------------*/
131 /* --- @SOD_XCHAIN@ --- *
133 * Arguments: @chead@ = nickname of target chain's head
134 * @obj@ = pointer to an instance chain
136 * Returns: Pointer to target chain, as a @void *@.
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.
143 #define SOD_XCHAIN(chead, obj) \
144 ((void *)((char *)(obj) + (obj)->_vt->_off_##chead))
146 /* --- @SOD_OFFSETDIFF@ --- *
148 * Arguments: @type@ = a simple (i.e., declaratorless) type name
149 * @mema, memb@ = members of @type@
151 * Returns: The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
153 * Use: Computes a signed offset between structure members.
156 #define SOD_OFFSETDIFF(type, mema, memb) \
157 ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
159 /* --- @SOD_ILAYOUT@ --- *
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
166 * Returns: A pointer to the instance's base, cast as a pointer to the
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
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.
181 #define SOD_ILAYOUT(cls, chead, obj) \
182 ((struct cls##__ilayout *) \
183 ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
185 /*----- Utility macros ----------------------------------------------------*/
187 /* --- @SOD_CLASSOF@ --- *
189 * Arguments: @p@ = pointer to an instance chain
191 * Returns: A pointer to the instance's class, as a @const SodClass *@.
194 #define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
196 /* --- @SOD_INSTBASE@ --- *
198 * Arguments: @obj@ = pointer to an instance (i.e., the address of one of
199 * its instance chains)
201 * Returns: The base address of @obj@'s instance layout, as a @void *@.
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.
211 #define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
213 /* --- @SOD_CONVERT@ --- *
215 * Arguments: @cls@ = a class type name
216 * @const void *obj@ = a pointer to an instance
218 * Returns: Pointer to appropriate instance ichain, or null if the
219 * instance isn't of the specified class.
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.
227 #define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
229 /* --- @SOD_DECL@ --- *
231 * Arguments: @cls_@ = a class type name
232 * @var_@ = a variable name
234 * Use: Declare @var_@ as a pointer to an initialized instance of
235 * @cls_@ with automatic lifetime.
238 #define SOD_DECL(cls_, var_) \
239 struct cls_##__ilayout var_##__layout; \
240 cls_ *var_ = cls_##__class->cls.init(&var_##__layout)
242 /*----- Functions provided ------------------------------------------------*/
244 /* --- @sod_subclassp@ --- *
246 * Arguments: @const SodClass *sub, *super@ = pointers to two classes
248 * Returns: Nonzero if @c@ is a subclass of @d@.
251 extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
253 /* --- @sod_convert@ --- *
255 * Arguments: @const SodClass *cls@ = desired class object
256 * @const void *obj@ = pointer to instance
258 * Returns: Pointer to appropriate ichain of object, or null if the
259 * instance isn't of the specified class.
261 * Use: General down/cross-casting function.
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
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.
279 extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
281 /*----- That's all, folks -------------------------------------------------*/