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