doc/runtime.tex: Suggest asserting that a stack-allocated object tore down.
[sod] / lib / sod.3
... / ...
CommitLineData
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 Sensible 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 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.
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 Library General Public License for more details.
21.\"
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.
26.
27.\" Highlight using terminal escapes, rather than overstriking.
28.\"\X'tty: sgr 1'
29.
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 );
71.br
72.B void *\c
73.B SOD_INSTBASE(\c
74.BI "const " cls " *" obj );
75.PP
76.B const void *\c
77.B SOD_CLASSOF(\c
78.BI "const " cls " *" obj );
79.br
80.B int
81.B sod_subclassp(\c
82.BI "const SodClass *" sub ,
83.BI "const SodClass *" super );
84.PP
85.IB cls " *" \c
86.B SOD_CONVERT(\c
87.IB cls ,
88.BI "const void *" obj );
89.br
90.B int
91.B sod_convert(\c
92.BI "const SodClass *" cls ,
93.BI "const void *" obj );
94.PP
95.IB cls " *" \c
96.B SOD_INIT(\c
97.IB cls ,
98.BI "void *" p ,
99.IB keywords );
100.br
101.B void *\c
102.B sod_init(\c
103.BI "const SodClass *" cls ,
104.BI "void *" p ,
105.B ...);
106.br
107.B void *\c
108.B sod_initv(\c
109.BI "const SodClass *" cls ,
110.BI "void *" p ,
111.BI "va_list " ap );
112.br
113.B void
114.B sod_teardown(\c
115.BI "void *" p );
116.br
117.B SOD_DECL(\c
118.IB cls ,
119.IB var );
120.br
121.IB cls " *" \c
122.B SOD_MAKE(\c
123.IB cls ,
124.IB keywords );
125.br
126.B void *\c
127.B sod_make(\c
128.BI "const SodClass *" cls ,
129.B ...);
130.br
131.B void *\c
132.B sod_makev(\c
133.BI "const SodClass *" cls ,
134.BI "va_list " ap );
135.br
136.B int
137.B sod_destroy(\c
138.BI "void *" p );
139.PP
140.
141.\"--------------------------------------------------------------------------
142.SH DESCRIPTION
143.
144The functions and macros defined here generally expect that
145instances and classes inherit from the standard
146.B SodObject
147root object.
148While the translator can (at some effort) support alternative roots,
149they will require different run-time support machinery.
150.
151.SS Layout utilities
152The following macros are useful in
153finding one's way around an instance layout structure,
154given various levels of information about
155what kind of object one is dealing with,
156or for computing the tables which are used for
157this kind of navigation.
158.PP
159These macros are mostly intended for use in
160code generated by the Sod translator.
161Others may find them useful for special effects,
162but they can be tricky to understand and use correctly
163and can't really be recommended for general use.
164.PP
165The
166.B SOD_OFFSETDIFF
167macro returns the signed offset between
168two members of a structure or union type.
169Given a structure or union type
170.IR type ,
171and two member names
172.I mema
173and
174.IR memb ,
175then
176.B SOD_OFFSETDIFF(\c
177.IB type ,
178.IB mema ,
179.IB memb )
180gives the difference, in bytes,
181between the objects
182.IB x . mema
183and
184.IB x . memb
185for any object
186.I x
187of type
188.IR type .
189This macro is used internally when generating vtables
190and is not expected to be very useful elsewhere.
191.PP
192The
193.B SOD_ILAYOUT
194macro recovers the instance layout base address
195from a pointer to one of its instance chains.
196Specifically, given a class name
197.IR cls ,
198the nickname
199.I chead
200of the least specific class in one of
201.IR cls 's
202superclass chains,
203and a pointer
204.I obj
205to the instance storage for the chain containing
206.I chead
207within an exact instance of
208.I cls
209(i.e., not an instance of any proper subclass),
210.B SOD_ILAYOUT(\c
211.IB cls ,
212.IB chead ,
213.IB obj )
214returns the a pointer to the layout structure containing
215.IB obj .
216This macro is used internally in effective method bodies
217and is not expected to be very useful elsewhere
218since it's unusual to have such specific knowledge
219about the dynamic type of an instance.
220The
221.B SOD_INSTBASE
222macro (described below) is more suited to general use.
223.PP
224The
225.B SOD_INSTBASE
226macro finds the base address of an instance's layout.
227Given a pointer
228.BI "const " cls " *" obj
229to an instance,
230.BI SOD_INSTBASE( obj )
231returns the base address of the storage allocated to
232.IR obj .
233This is useful if you want to free a dynamically allocated instance,
234for example.
235This macro needs to look up an offset in
236.IR obj 's
237vtable to do its work.
238Compare
239.B SOD_ILAYOUT
240above,
241which is faster but requires
242precise knowledge of the instance's dynamic class.
243.
244.SS Classes
245The following macros and functions
246query the runtime relationhips between
247instances and classes.
248.PP
249The
250.B SOD_CLASSOF
251macro returns the class object describing an instance's dynamic class.
252Given a pointer
253.BI "const " cls " *" obj
254to an instance,
255.BI SOD_CLASSOF( obj )
256returns a pointer to
257.IR obj 's
258dynamic class,
259which
260(assuming
261.I obj
262is typed correctly in the first place)
263will be a subclass of
264.IR cls .
265(If you wanted the class object for
266.I cls
267itself,
268it's called
269.IB cls __class \fR.)
270.PP
271The
272.B sod_subclassp
273function answers whether one class
274.I sub
275is actually a subclass of another class
276.IR super .
277.B sod_subclassp(\c
278.IB sub ,
279.IB super )
280returns nonzero if and only if
281.I sub
282is a subclass of
283.IR super .
284This involves a run-time trawl through the class structures:
285while some effort has been made to make it perform well
286it's still not very fast.
287.
288.SS Conversions
289The following macros and functions are used
290to convert instance pointers of some (static) type
291into instance pointers of other static types
292to the same instance.
293.PP
294The
295.B SOD_XCHAIN
296macro performs a `cross-chain upcast'.
297Given a pointer
298.I cls
299.BI * obj
300to an instance of a class of type
301.I cls
302and the nickname
303.I chead
304of the least specific class in one of
305.IR cls 's
306superclass chains which does not contain
307.I cls
308itself,
309.B SOD_XCHAIN(\c
310.IB chead ,
311.IB obj )
312returns the address of that chain's storage
313within the instance layout as a raw
314.B void *
315pointer.
316(Note that
317.I cls
318is not mentioned explicitly.)
319This macro is used by the generated
320.IB cls __CONV_ c
321conversion macros,
322which you are encouraged to use instead where possible.
323.PP
324The
325.B SOD_CONVERT
326macro
327and
328.B sod_convert
329function
330perform general conversions
331(up-, down-, and cross-casts) on instance pointers.
332Given a class
333.I cls
334and a pointer
335.BI "const void *" obj
336to an instance,
337they return an appropriately converted pointer to
338.I obj
339if
340.I obj
341is indeed an instance of (some subclass of)
342.IR cls ;
343otherwise they return a null pointer.
344.PP
345The
346.B SOD_CONVERT
347macro expects
348.I cls
349to be a class name;
350the
351.B sod_convert
352function
353expects a pointer to a class object instead.
354.PP
355This involves a run-time trawl through the class structures:
356while some effort has been made to make it perform well
357it's still not very fast.
358For upcasts (where
359.I cls
360is a superclass of the static type of
361.IR obj )
362the automatically defined conversion macros should be used instead,
363because they're much faster and can't fail.
364.PP
365When the target class is known statically,
366it's slightly more convenient to use the
367.B SOD_CONVERT
368macro rather than the
369.B sod_convert
370function,
371since the class object name is longer and uglier,
372and the macro returns a pointer of the correct type.
373.
374.SS Instance lifecycle
375The following macros and functions
376manage the standard steps along
377an instance's lifecycle.
378.PP
379The
380.B SOD_INIT
381macro,
382and the
383.B sod_init
384and
385.B sod_initv
386functions,
387imprint and initialize an instance of a class
388.I cls
389in the storage starting at address
390.IR p .
391.PP
392The direct class for the new instance is specified as
393a class name to
394.BR SOD_INIT ,
395or a pointer to a class object to the functions.
396.PP
397Keyword arguments for the initialization message may be provided.
398The
399.B SOD_INIT
400macro expects a single preprocessor-time argument
401which is a use of one of
402.B KWARGS
403or
404.B NO_KWARGS
405(see
406.BR keyword (3));
407the
408.B sod_init
409function expects the keywords as
410a variable-length argument tail;
411and
412.B sod_initv
413expects the keywords to be passed indirectly,
414through the captured argument-tail cursor
415.IR ap .
416.PP
417The return value is an instance pointer for the class
418.IR cls ;
419the
420.B SOD_INIT
421macro will have converted it to the correct type,
422so it should probably be used where possible.
423In fact, this is guaranteed to be equal to
424.I p
425by the layout rules described in
426.BR sod-structs (3).
427.PP
428The
429.B sod_teardown
430function tears down an instance of a class,
431releasing any resources it holds.
432.PP
433This function is a very thin wrapper around sending the
434.B obj.teardown
435message.
436It returns an integer flag.
437A zero value means that the instance is safe to deallocate.
438A nonzero value means that the instance should not be deallocated,
439and that it is safe for the caller to simply forget about it.
440(For example, the object may maintain a reference count,
441and knows that other references remain active.)
442.PP
443The
444.B SOD_DECL
445macro declares and initializes an instance
446with automatic storage duration.
447Given a class name
448.I cls
449and an identifier
450.IR var ,
451.B SOD_DECL(\c
452.IB cls ,
453.IB var )
454declares
455.I var
456to be a pointer to an instance of
457.IR cls .
458The instance is initialized in the sense that
459its vtable and class pointers have been set up,
460and slots for which initializers are defined
461are set to the appropriate initial values.
462The instance has automatic storage duration:
463pointers to it will become invalid when control
464exits the scope of the declaration.
465.PP
466Keyword arguments for the initialization message may be provided.
467The macro expects a single preprocessor-time argument
468which is a use of one of
469.B KWARGS
470or
471.B NO_KWARGS
472(see
473.BR keyword (3)).
474.PP
475The instance has automatic storage duration:
476pointers to it will become invalid
477when control exits the scope of the declaration.
478If necessary,
479the instance should be torn down before this happens,
480using the
481.B sod_teardown
482function.
483It may be appropriate to
484.BR assert (3)
485that the object is ready for deallocation at this time.
486.PP
487The
488.B SOD_MAKE
489macro,
490and the
491.B sod_make
492and
493.B sod_makev
494functions,
495construct and return a pointer to a new instance of a class.
496.PP
497The direct class for the new instance is specified as a class name to
498.BR SOD_MAKE ,
499or as a class object to the functions.
500.PP
501Keyword arguments for the initialization message may be provided.
502The
503.B SOD_MAKE
504macro expects a single preprocessor-time argument
505which is a use of one of
506.B KWARGS
507or
508.B NO_KWARGS
509(see
510.BR keyword (3));
511the
512.B sod_init
513function expects the keywords as
514a variable-length argument tail;
515and
516.B sod_makev
517expects the keywords to be passed indirectly,
518through the captured argument-tail cursor
519.IR ap .
520.PP
521Storage for the new instance will have been allocated
522using the standard
523.BR malloc (3)
524function.
525The easiest way to destroy the instance,
526when it is no longer needed,
527is probably to call the
528.B sod_destroy
529function.
530.PP
531The return value is an instance pointer for the class
532.IR cls ;
533the
534.B SOD_MAKE
535macro will have converted it to the correct type,
536so it should probably be used where possible.
537.PP
538The
539.B sod_destroy
540function tears down and frees an instance allocated using
541.BR malloc (3).
542.PP
543The pointer
544.I p
545should be an instance pointer,
546i.e., a pointer to any of an instance's chains.
547The instance is torn down,
548by sending it the
549.B obj.teardown
550message.
551If the instance reports itself ready for deallocation,
552then its storage is released using
553.BR free (3).
554The return value is the value returned by the
555.B obj.teardown
556message.
557.
558.\"--------------------------------------------------------------------------
559.SH SEE ALSO
560.BR keyword (3),
561.BR sod (1),
562.BR sod-structs (3).
563.
564.\"--------------------------------------------------------------------------
565.SH AUTHOR
566Mark Wooding, <mdw@distorted.org.uk>
567.
568.\"----- That's all, folks --------------------------------------------------