src/frontend.lisp: The command-line options aren't crazy any more.
[sod] / lib / sod.h
CommitLineData
1f1d88f5
MW
1/* -*-c-*-
2 *
3 * Sensible Object Design header file
4 *
5 * (c) 2009 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
dea4d055 10 * This file is part of the Sensble Object Design, an object system for C.
1f1d88f5 11 *
7d21069e
MW
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.
1f1d88f5 16 *
7d21069e 17 * The SOD Runtime is distributed in the hope that it will be useful,
1f1d88f5
MW
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7d21069e 20 * GNU Library General Public License for more details.
1f1d88f5 21 *
7d21069e
MW
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.
1f1d88f5
MW
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
ddee4bb1 40#include "sod-base.h"
1f1d88f5
MW
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 */
47struct sod_vtable {
dea4d055 48 const SodClass *_class; /* Pointer to class object */
1f1d88f5
MW
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 */
55struct sod_instance {
56 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 */
64struct 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
6a590fd0 77 * @obj@ = pointer to an instance chain
1f1d88f5
MW
78 *
79 * Returns: Pointer to target chain, as a @char *@.
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
6a590fd0 86#define SOD_XCHAIN(chead, obj) ((char *)(obj) + (obj)->_vt->_off_##chead)
1f1d88f5 87
a07d8d00
MW
88/* --- @SOD_OFFSETDIFF@ --- *
89 *
90 * Arguments: @type@ = a simple (i.e., declaratorless) type name
91 * @mema, memb@ = members of @type@
92 *
93 * Returns: The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
94 *
95 * Use: Computes a signed offset between structure members.
96 */
97
98#define SOD_OFFSETDIFF(type, mema, memb) \
99 ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
100
1f1d88f5
MW
101/* --- @SOD_ILAYOUT@ --- *
102 *
103 * Arguments: @cls@ = name of a class
104 * @chead@ = nickname of chain head of @cls@
6a590fd0
MW
105 * @obj@ = pointer to the @chead@ ichain of an (exact) instance
106 * of @cls@
1f1d88f5
MW
107 *
108 * Returns: A pointer to the instance's base, cast as a pointer to the
109 * ilayout structure.
110 *
111 * Use: Finds an instance's base address given a pointer to one of
112 * its ichains, if you know precisely the instance's class and
45c53484
MW
113 * which chain you're pointing to. If you don't, then (a) you
114 * want @SOD_INSTBASE@ below, and (b) you'll have the wrong
1f1d88f5
MW
115 * ilayout anyway.
116 *
117 * This macro is not intended to be used directly outside of
118 * automatically generated effective method and trampoline
119 * functions, which have the kinds of specific knowledge
120 * necessary to use it safely.
121 */
122
6a590fd0 123#define SOD_ILAYOUT(cls, chead, obj) \
1f1d88f5 124 ((struct cls##__ilayout *) \
6a590fd0
MW
125 ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
126
6bc944c3
MW
127/* --- @SOD__CAR@ --- *
128 *
129 * Arguments: @...@ = a nonempty list of arguments
130 *
131 * Returns: The first argument only.
132 */
133
134#if __STDC_VERSION__ >= 199901
135# define SOD__CAR(...) SOD__CARx(__VA_LIST__, _)
136# define SOD__CARx(a, ...) a
137#endif
138
6a590fd0
MW
139/*----- Utility macros ----------------------------------------------------*/
140
141/* --- @SOD_CLASSOF@ --- *
142 *
143 * Arguments: @p@ = pointer to an instance chain
144 *
145 * Returns: A pointer to the instance's class, as a const SodClass.
146 */
147
148#define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
1f1d88f5 149
45c53484
MW
150/* --- @SOD_INSTBASE@ --- *
151 *
6a590fd0
MW
152 * Arguments: @obj@ = pointer to an instance (i.e., the address of one of
153 * its instance chains)
45c53484 154 *
6a590fd0 155 * Returns: The base address of @obj@'s instance layout, as a @void *@.
45c53484
MW
156 *
157 * Use: Finds the base address of an instance. If you know the
158 * dynamic class of the object then @SOD_ILAYOUT@ is faster. If
159 * you don't, this is the right macro, but your options for
160 * doing something sensible with the result are limited, mostly
161 * to simple memory management operations such as freeing or
162 * zeroizing the instance structure.
163 */
164
6a590fd0 165#define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
77027cca 166
6a590fd0 167/* --- @SOD_CONVERT@ --- *
77027cca 168 *
6a590fd0
MW
169 * Arguments: @cls@ = a class type name
170 * @const void *obj@ = a pointer to an instance
77027cca 171 *
6a590fd0
MW
172 * Returns: Pointer to appropriate instance ichain, or null if the
173 * instance isn't of the specified class.
174 *
175 * Use: This is a simple wrapper around the @sod_convert@, which
176 * you should see for full details. It accepts a class type
177 * name rather than a pointer to a class object, and arranges to
178 * return a pointer of the correct type.
77027cca
MW
179 */
180
6a590fd0 181#define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
77027cca 182
267dd3e7
MW
183/* --- @SOD_DECL@ --- *
184 *
185 * Arguments: @cls_@ = a class type name
186 * @var_@ = a variable name
187 *
188 * Use: Declare @var_@ as a pointer to an initialized instance of
189 * @cls_@ with automatic lifetime.
190 */
191
192#define SOD_DECL(cls_, var_) \
193 struct cls_##__ilayout var_##__layout; \
194 cls_ *var_ = cls_##__class->cls.init(&var_##__layout)
195
1f1d88f5
MW
196/*----- Functions provided ------------------------------------------------*/
197
77027cca
MW
198/* --- @sod_subclassp@ --- *
199 *
dea4d055 200 * Arguments: @const SodClass *sub, *super@ = pointers to two classes
77027cca
MW
201 *
202 * Returns: Nonzero if @c@ is a subclass of @d@.
203 */
204
dea4d055 205extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
77027cca 206
1f1d88f5
MW
207/* --- @sod_convert@ --- *
208 *
209 * Arguments: @const SodClass *cls@ = desired class object
210 * @const void *obj@ = pointer to instance
211 *
212 * Returns: Pointer to appropriate ichain of object, or null if the
213 * instance isn't of the specified class.
214 *
215 * Use: General down/cross-casting function.
216 *
217 * Upcasts can be performed efficiently using the automatically
dea4d055 218 * generated macros. In particular, upcasts within a chain are
1f1d88f5
MW
219 * trivial; cross-chain upcasts require information from vtables
220 * but are fairly fast. This function is rather slower, but is
221 * much more general.
222 *
223 * Suppose we have an instance of a class C, referred to by a
dea4d055 224 * pointer to an instance of one of C's superclasses S. If T
1f1d88f5 225 * is some other superclass of C then this function will return
dea4d055 226 * a pointer to C suitable for use as an instance of T. If T
1f1d88f5
MW
227 * is not a superclass of C, then the function returns null.
228 * (If the pointer doesn't point to an instance of some class
229 * then the behaviour is undefined.) Note that you don't need
dea4d055 230 * to know what either C or S actually are.
1f1d88f5
MW
231 */
232
6a590fd0 233extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
1f1d88f5
MW
234
235/*----- That's all, folks -------------------------------------------------*/
236
237#ifdef __cplusplus
238 }
239#endif
240
241#endif