vars.am, */Makefile.am: Improve silent-rules building.
[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
MW
11 *
12 * SOD is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * SOD 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 General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with SOD; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27#ifndef SOD_H
28#define SOD_H
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34/*----- Header files ------------------------------------------------------*/
35
36#include <stdarg.h>
37#include <stddef.h>
38
ddee4bb1 39#include "sod-base.h"
1f1d88f5
MW
40
41/*----- Data structures ---------------------------------------------------*/
42
43/* A skeletal vtable structure. At the beginning of every ichain is a
44 * pointer to one of these.
45 */
46struct sod_vtable {
dea4d055 47 const SodClass *_class; /* Pointer to class object */
1f1d88f5
MW
48 size_t _base; /* Offset to instance base */
49};
50
51/* A skeletal instance structure. Every instance pointer points to one of
52 * these.
53 */
54struct sod_instance {
55 struct sod_vtable *_vt; /* Pointer to (chain's) vtable */
56};
57
58/* Information about a particular chain of superclasses. In each class,
59 * there's a pointer to an array of these. If you search hard enough, you'll
60 * be able to find out a fair amount of information about an instance and its
61 * class.
62 */
63struct sod_chain {
64 size_t n_classes; /* Number of classes in chain */
65 const SodClass *const *classes; /* Vector of classes, head first */
66 size_t off_ichain; /* Offset of ichain from base */
67 const struct sod_vtable *vt; /* Chain's vtable pointer */
68 size_t ichainsz; /* Size of the ichain structure */
69};
70
71/*----- Infrastructure macros ---------------------------------------------*/
72
73/* --- @SOD_XCHAIN@ --- *
74 *
75 * Arguments: @chead@ = nickname of target chain's head
6a590fd0 76 * @obj@ = pointer to an instance chain
1f1d88f5
MW
77 *
78 * Returns: Pointer to target chain, as a @char *@.
79 *
80 * Use: Utility for implementing cross-chain upcasts. It's probably
81 * not that clever to use this macro directly; it's used to make
82 * the automatically-generated upcast macros more palatable.
83 */
84
6a590fd0 85#define SOD_XCHAIN(chead, obj) ((char *)(obj) + (obj)->_vt->_off_##chead)
1f1d88f5 86
a07d8d00
MW
87/* --- @SOD_OFFSETDIFF@ --- *
88 *
89 * Arguments: @type@ = a simple (i.e., declaratorless) type name
90 * @mema, memb@ = members of @type@
91 *
92 * Returns: The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
93 *
94 * Use: Computes a signed offset between structure members.
95 */
96
97#define SOD_OFFSETDIFF(type, mema, memb) \
98 ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
99
1f1d88f5
MW
100/* --- @SOD_ILAYOUT@ --- *
101 *
102 * Arguments: @cls@ = name of a class
103 * @chead@ = nickname of chain head of @cls@
6a590fd0
MW
104 * @obj@ = pointer to the @chead@ ichain of an (exact) instance
105 * of @cls@
1f1d88f5
MW
106 *
107 * Returns: A pointer to the instance's base, cast as a pointer to the
108 * ilayout structure.
109 *
110 * Use: Finds an instance's base address given a pointer to one of
111 * its ichains, if you know precisely the instance's class and
45c53484
MW
112 * which chain you're pointing to. If you don't, then (a) you
113 * want @SOD_INSTBASE@ below, and (b) you'll have the wrong
1f1d88f5
MW
114 * ilayout anyway.
115 *
116 * This macro is not intended to be used directly outside of
117 * automatically generated effective method and trampoline
118 * functions, which have the kinds of specific knowledge
119 * necessary to use it safely.
120 */
121
6a590fd0 122#define SOD_ILAYOUT(cls, chead, obj) \
1f1d88f5 123 ((struct cls##__ilayout *) \
6a590fd0
MW
124 ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
125
126/*----- Utility macros ----------------------------------------------------*/
127
128/* --- @SOD_CLASSOF@ --- *
129 *
130 * Arguments: @p@ = pointer to an instance chain
131 *
132 * Returns: A pointer to the instance's class, as a const SodClass.
133 */
134
135#define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
1f1d88f5 136
45c53484
MW
137/* --- @SOD_INSTBASE@ --- *
138 *
6a590fd0
MW
139 * Arguments: @obj@ = pointer to an instance (i.e., the address of one of
140 * its instance chains)
45c53484 141 *
6a590fd0 142 * Returns: The base address of @obj@'s instance layout, as a @void *@.
45c53484
MW
143 *
144 * Use: Finds the base address of an instance. If you know the
145 * dynamic class of the object then @SOD_ILAYOUT@ is faster. If
146 * you don't, this is the right macro, but your options for
147 * doing something sensible with the result are limited, mostly
148 * to simple memory management operations such as freeing or
149 * zeroizing the instance structure.
150 */
151
6a590fd0 152#define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
77027cca 153
6a590fd0 154/* --- @SOD_CONVERT@ --- *
77027cca 155 *
6a590fd0
MW
156 * Arguments: @cls@ = a class type name
157 * @const void *obj@ = a pointer to an instance
77027cca 158 *
6a590fd0
MW
159 * Returns: Pointer to appropriate instance ichain, or null if the
160 * instance isn't of the specified class.
161 *
162 * Use: This is a simple wrapper around the @sod_convert@, which
163 * you should see for full details. It accepts a class type
164 * name rather than a pointer to a class object, and arranges to
165 * return a pointer of the correct type.
77027cca
MW
166 */
167
6a590fd0 168#define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
77027cca 169
1f1d88f5
MW
170/*----- Functions provided ------------------------------------------------*/
171
77027cca
MW
172/* --- @sod_subclassp@ --- *
173 *
dea4d055 174 * Arguments: @const SodClass *sub, *super@ = pointers to two classes
77027cca
MW
175 *
176 * Returns: Nonzero if @c@ is a subclass of @d@.
177 */
178
dea4d055 179extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
77027cca 180
1f1d88f5
MW
181/* --- @sod_convert@ --- *
182 *
183 * Arguments: @const SodClass *cls@ = desired class object
184 * @const void *obj@ = pointer to instance
185 *
186 * Returns: Pointer to appropriate ichain of object, or null if the
187 * instance isn't of the specified class.
188 *
189 * Use: General down/cross-casting function.
190 *
191 * Upcasts can be performed efficiently using the automatically
dea4d055 192 * generated macros. In particular, upcasts within a chain are
1f1d88f5
MW
193 * trivial; cross-chain upcasts require information from vtables
194 * but are fairly fast. This function is rather slower, but is
195 * much more general.
196 *
197 * Suppose we have an instance of a class C, referred to by a
dea4d055 198 * pointer to an instance of one of C's superclasses S. If T
1f1d88f5 199 * is some other superclass of C then this function will return
dea4d055 200 * a pointer to C suitable for use as an instance of T. If T
1f1d88f5
MW
201 * is not a superclass of C, then the function returns null.
202 * (If the pointer doesn't point to an instance of some class
203 * then the behaviour is undefined.) Note that you don't need
dea4d055 204 * to know what either C or S actually are.
1f1d88f5
MW
205 */
206
6a590fd0 207extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
1f1d88f5
MW
208
209/*----- That's all, folks -------------------------------------------------*/
210
211#ifdef __cplusplus
212 }
213#endif
214
215#endif