lib/sod.3: Add missing documentation.
[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 94.PP
a142609c
MW
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
bacf2ba6
MW
113.B void
114.B sod_teardown(\c
115.BI "void *" p );
116.br
4e3a7cb2
MW
117.B SOD_DECL(\c
118.IB cls ,
119.IB var );
bacf2ba6
MW
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 );
4e3a7cb2 139.PP
47de28ae
MW
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.
4e3a7cb2
MW
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.
47de28ae
MW
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
47de28ae
MW
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.
4e3a7cb2
MW
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.
47de28ae 243.
4e3a7cb2
MW
244.SS Classes
245The following macros and functions
246query the runtime relationhips between
247instances and classes.
47de28ae
MW
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
4e3a7cb2
MW
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'.
47de28ae 297Given a pointer
4e3a7cb2
MW
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.
47de28ae
MW
323.PP
324The
325.B SOD_CONVERT
4e3a7cb2
MW
326macro
327and
328.B sod_convert
329function
330perform general conversions
47de28ae 331(up-, down-, and cross-casts) on instance pointers.
4e3a7cb2 332Given a class
47de28ae
MW
333.I cls
334and a pointer
335.BI "const void *" obj
336to an instance,
4e3a7cb2 337they return an appropriately converted pointer to
47de28ae
MW
338.I obj
339if
340.I obj
341is indeed an instance of (some subclass of)
342.IR cls ;
4e3a7cb2
MW
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
47de28ae 369.B sod_convert
4e3a7cb2
MW
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.
47de28ae
MW
378.PP
379The
a142609c
MW
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
bacf2ba6
MW
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
47de28ae
MW
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.
bacf2ba6
MW
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.
483.PP
484The
485.B SOD_MAKE
486macro,
487and the
488.B sod_make
489and
490.B sod_makev
491functions,
492construct and return a pointer to a new instance of a class.
493.PP
494The direct class for the new instance is specified as a class name to
495.BR SOD_MAKE ,
496or as a class object to the functions.
497.PP
498Keyword arguments for the initialization message may be provided.
499The
500.B SOD_MAKE
501macro expects a single preprocessor-time argument
502which is a use of one of
503.B KWARGS
504or
505.B NO_KWARGS
506(see
507.BR keyword (3));
508the
509.B sod_init
510function expects the keywords as
511a variable-length argument tail;
512and
513.B sod_makev
514expects the keywords to be passed indirectly,
515through the captured argument-tail cursor
516.IR ap .
517.PP
518Storage for the new instance will have been allocated
519using the standard
520.BR malloc (3)
521function.
522The easiest way to destroy the instance,
523when it is no longer needed,
524is probably to call the
525.B sod_destroy
526function.
527.PP
528The return value is an instance pointer for the class
529.IR cls ;
530the
531.B SOD_MAKE
532macro will have converted it to the correct type,
533so it should probably be used where possible.
534.PP
535The
536.B sod_destroy
537function tears down and frees an instance allocated using
538.BR malloc (3).
539.PP
540The pointer
541.I p
542should be an instance pointer,
543i.e., a pointer to any of an instance's chains.
544The instance is torn down,
545by sending it the
546.B obj.teardown
547message.
548If the instance reports itself ready for deallocation,
549then its storage is released using
550.BR free (3).
551The return value is the value returned by the
552.B obj.teardown
553message.
47de28ae 554.
47de28ae
MW
555.\"--------------------------------------------------------------------------
556.SH SEE ALSO
a142609c 557.BR keyword (3),
9ed8eb2a 558.BR sod (1),
47de28ae
MW
559.BR sod-structs (3).
560.
561.\"--------------------------------------------------------------------------
562.SH AUTHOR
563Mark Wooding, <mdw@distorted.org.uk>
564.
565.\"----- That's all, folks --------------------------------------------------