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