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