Merge branch 'master' into doc
[sod] / lib / sod.3
CommitLineData
47de28ae
MW
1.\" -*-nroff-*-
2.\"
3.\" The Sod runtime library
4.\"
5.\" (c) 2015 Straylight/Edgeware
6.\"
7.
8.\"----- Licensing notice ---------------------------------------------------
9.\"
10.\" This file is part of the Sensble Object Design, an object system for C.
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.\"\X'tty: sgr 1'
27.\" String definitions and font selection.
28.ie t \{\
29. ds o \(bu
30. if \n(.g .fam P
31.\}
32.el \{\
33. ds o o
34.\}
35.
36.\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left
37.de hP
38.IP
39\h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
40..
41.
42.\"--------------------------------------------------------------------------
43.TH sod 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
44.
45.SH NAME
46sod \- Sensible Object Design runtime library
47.
48.\"--------------------------------------------------------------------------
49.SH SYNOPSIS
50.B #include <sod/sod.h>
51.PP
52.B void *\c
53.B SOD_XCHAIN(\c
54.IB chead ,
55.BI "const " cls " *" obj );
56.br
57.B ptrdiff_t
58.B SOD_OFFSETDIFF(\c
59.IB type ,
60.IB mema ,
61.IB memb );
62.br
63.IB cls "__ilayout *" \c
64.B SOD_ILAYOUT(\c
65.IB cls ,
66.IB chead ,
67.BI "const void *" obj );
68.br
69.B SOD_CAR(\c
70.IB arg ,
71.RB ... );
72.PP
73.B const void *\c
74.B SOD_CLASSOF(\c
75.BI "const " cls " *" obj );
76.br
77.B void *\c
78.B SOD_INSTBASE(\c
79.BI "const " cls " *" obj );
80.br
81.IB cls " *" \c
82.B SOD_CONVERT(\c
83.IB cls ,
84.BI "const void *" obj );
85.br
86.B SOD_DECL(\c
87.IB cls ,
88.IB var );
89.PP
90.B int
91.B sod_subclassp(\c
92.BI "const SodClass *" sub ,
93.BI "const SodClass *" super );
94.br
95.B int
96.B sod_convert(\c
97.BI "const SodClass *" cls ,
98.BI "const void *" obj );
99.
100.\"--------------------------------------------------------------------------
101.SH DESCRIPTION
102.
103The functions and macros defined here generally expect that
104instances and classes inherit from the standard
105.B SodObject
106root object.
107While the translator can (at some effort) support alternative roots,
108they will require different run-time support machinery.
109.
110.SS Infrastructure macros
111These macros are mostly intended for use in code
112generated by the Sod translator.
113Others may find them useful for special effects,
114but they can be tricky to understand and use correctly
115and can't really be recommended for general use.
116.PP
117The
118.B SOD_XCHAIN
119macro performs a `cross-chain upcast'.
120Given a pointer
121.I cls
122.BI * obj
123to an instance of a class of type
124.I cls
125and the nickname
126.I chead
127of the least specific class in one of
128.IR cls 's
129superclass chains which does not contain
130.I cls
131itself,
132.B SOD_XCHAIN(\c
133.IB chead ,
134.IB obj )
135returns the address of that chain's storage
136within the instance layout as a raw
137.B void *
138pointer.
139(Note that
140.I cls
141is not mentioned explicitly.)
142This macro is used by the generated
143.IB CLASS __CONV_ CLS
144conversion macros,
145which you are encouraged to use instead where possible.
146.PP
147The
148.B SOD_OFFSETDIFF
149macro returns the signed offset between
150two members of a structure or union type.
151Given a structure or union type
152.IR type ,
153and two member names
154.I mema
155and
156.IR memb ,
157then
158.B SOD_OFFSETDIFF(\c
159.IB type ,
160.IB mema ,
161.IB memb )
162gives the difference, in bytes,
163between the objects
164.IB x . mema
165and
166.IB x . memb
167for any object
168.I x
169of type
170.IR type .
171This macro is used internally when generating vtables
172and is not expected to be very useful elsewhere.
173.PP
174The
175.B SOD_ILAYOUT
176macro recovers the instance layout base address
177from a pointer to one of its instance chains.
178Specifically, given a class name
179.IR cls ,
180the nickname
181.I chead
182of the least specific class in one of
183.IR cls 's
184superclass chains,
185and a pointer
186.I obj
187to the instance storage for the chain containing
188.I chead
189within an exact instance of
190.I cls
191(i.e., not an instance of any proper subclass),
192.B SOD_ILAYOUT(\c
193.IB cls ,
194.IB chead ,
195.IB obj )
196returns the a pointer to the layout structure containing
197.IB obj .
198This macro is used internally in effective method bodies
199and is not expected to be very useful elsewhere
200since it's unusual to have such specific knowledge
201about the dynamic type of an instance.
202The
203.B SOD_INSTBASE
204macro (described below) is more suited to general use.
205.PP
206The
207.B SOD_CAR
208macro accepts one or more arguments
209and expands to just its first argument,
210discarding the others.
211It is only defined if the C implementation
212advertises support for C99.
213It is used in the definitions of message convenience macros
214for messages which accept a variable number of arguments
215but no required arguments,
216and is exported because the author has found such a thing useful in
217other contexts.
218.
219.SS Utility macros
220The following macros are expected to be useful
221in Sod method definitions and client code.
222.PP
223The
224.B SOD_CLASSOF
225macro returns the class object describing an instance's dynamic class.
226Given a pointer
227.BI "const " cls " *" obj
228to an instance,
229.BI SOD_CLASSOF( obj )
230returns a pointer to
231.IR obj 's
232dynamic class,
233which
234(assuming
235.I obj
236is typed correctly in the first place)
237will be a subclass of
238.IR cls .
239(If you wanted the class object for
240.I cls
241itself,
242it's called
243.IB cls __class \fR.)
244.PP
245The
246.B SOD_INSTBASE
247macro finds the base address of an instance's layout.
248Given a pointer
249.BI "const " cls " *" obj
250to an instance,
251.BI SOD_INSTBASE( obj )
252returns the base address of the storage allocated to
253.IR obj .
254This is useful if you want to free a dynamically allocated instance,
255for example.
256This macro needs to look up an offset in
257.IR obj 's
258vtable to do its work.
259Compare
260.B SOD_ILAYOUT
261above,
262which is faster but requires
263precise knowledge of the instance's dynamic class.
264.PP
265The
266.B SOD_CONVERT
267macro performs general conversions
268(up-, down-, and cross-casts) on instance pointers.
269Given a class name
270.I cls
271and a pointer
272.BI "const void *" obj
273to an instance,
274.B SOD_CONVERT(\c
275.IB cls ,
276.IB obj )
277returns an appropriately converted pointer to
278.I obj
279if
280.I obj
281is indeed an instance of (some subclass of)
282.IR cls ;
283otherwise it returns a null pointer.
284This macro is a simple wrapper around the
285.B sod_convert
286function described below,
287which is useful in the common case that
288the target class is known statically.
289.PP
290The
291.B SOD_DECL
292macro declares and initializes an instance
293with automatic storage duration.
294Given a class name
295.I cls
296and an identifier
297.IR var ,
298.B SOD_DECL(\c
299.IB cls ,
300.IB var )
301declares
302.I var
303to be a pointer to an instance of
304.IR cls .
305The instance is initialized in the sense that
306its vtable and class pointers have been set up,
307and slots for which initializers are defined
308are set to the appropriate initial values.
309The instance has automatic storage duration:
310pointers to it will become invalid when control
311exits the scope of the declaration.
312.
313.SS Functions
314The following functions are provided.
315.PP
316The
317.B sod_subclassp
318function answers whether one class
319.I sub
320is actually a subclass of another class
321.IR super .
322.B sod_subclassp(\c
323.IB sub ,
324.IB super )
325returns nonzero if and only if
326.I sub
327is a subclass of
328.IR super .
329This involves a run-time trawl through the class structures:
330while some effort has been made to make it perform well
331it's still not very fast.
332.PP
333The
334.B sod_convert
335function performs general conversions
336(up-, down-, and cross-casts) on instance pointers.
337Given a class pointer
338.I cls
339and an instance pointer
340.IR obj ,
341.B sod_convert(\c
342.IB cls ,
343.IB obj )
344returns an appropriately converted pointer to
345.I obj
346in the case that
347.I obj
348is an instance of (some subclass of)
349.IR cls ;
350otherwise it returns null.
351This involves a run-time trawl through the class structures:
352while some effort has been made to make it perform well
353it's still not very fast.
354For upcasts (where
355.I cls
356is a superclass of the static type of
357.IR obj )
358the automatically defined conversion macros should be used instead,
359because they're much faster and can't fail.
360When the target class is known statically,
361it's slightly more convenient to use the
362.B SOD_CONVERT
363macro instead.
364.
365.\"--------------------------------------------------------------------------
366.SH SEE ALSO
367.BR sod-structs (3).
368.
369.\"--------------------------------------------------------------------------
370.SH AUTHOR
371Mark Wooding, <mdw@distorted.org.uk>
372.
373.\"----- That's all, folks --------------------------------------------------