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