New feature: proper object lifecycle protocol; init and teardown fragments.
[sod] / lib / sod-structs.3
CommitLineData
47de28ae
MW
1.\" -*-nroff-*-
2.\"
3.\" Description of the main Sod data structures
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-structs 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
47.
48.SH NAME
49sod-structs \- main Sod data structures
50.
51.\"--------------------------------------------------------------------------
52.SH SYNOPSIS
53.nf
54.ft B
55#include <sod/sod.h>
56
57typedef struct SodObject__ichain_obj SodObject;
58typedef struct SodClass__ichain_obj SodClass;
59
60struct sod_instance {
61\h'2n'const struct sod_vtable *_vt;
62};
63
64struct sod_vtable {
65\h'2n'const SodClass *_class;
66\h'2n'size_t _base;
67};
68
69struct SodObject__vt_obj {
70\h'2n'const SodClass *_class;
71\h'2n'size_t _base;
a142609c
MW
72\h'2n'struct SodObject__vtmsgs_obj {
73\h'4n'void (*init)(SodObject *\fIme\fB, ...);
74\h'4n'void (*init__v)(SodObject *\fIme\fB, va_list);
a42893dd 75\h'4n'int (*teardown)(SodObject *\fIme\fB);
a142609c 76\h'2n'} obj;
47de28ae
MW
77};
78
79struct SodObject__ilayout {
80\h'2n'union {
81\h'4n'struct SodObject__ichain_obj {
82\h'6n'const struct SodObject__vt_obj *_vt;
83\h'4n'};
84\h'2n'} obj;
85};
86
87extern const struct SodClass__ilayout SodObject__classobj;
88#define SodObject__class (&SodObject__classobj.obj.cls)
89
90struct SodClass__vt_obj {
91\h'2n'const SodClass *_class;
92\h'2n'size_t _base;
a142609c
MW
93\h'2n'struct SodClass__vtmsgs_obj {
94\h'4n'void (*init)(SodClass *\fIme\fB, ...);
95\h'4n'void (*init__v)(SodClass *\fIme\fB, va_list);
a42893dd 96\h'4n'int (*teardown)(SodClass *\fIme\fB);
a142609c 97\h'2n'} obj;
47de28ae
MW
98};
99
100struct SodObject__ilayout {
101\h'2n'union {
102\h'4n'struct SodClass__ichain_obj {
103\h'6n'const struct SodClass__vt_obj *_vt;
104\h'6n'struct SodClass__islots {
105\h'8n'const char *name;
106\h'8n'const char *nick;
107\h'8n'size_t initsz;
108\h'8n'void *(*imprint)(void *\fIp\fB);
47de28ae
MW
109\h'8n'size_t n_supers;
110\h'8n'const SodClass *const *supers;
111\h'8n'size_t n_cpl;
112\h'8n'const SodClass *const *cpl;
113\h'8n'const SodClass *link;
114\h'8n'const SodClass *head;
115\h'8n'size_t level;
116\h'8n'size_t n_chains;
117\h'8n'const struct sod_chain *chains;
118\h'8n'size_t off_islots;
119\h'8n'size_t islotsz;
120\h'6n'} cls;
121\h'4n'};
122\h'4n'SodObject obj;
123\h'2n'} obj;
124};
125
126struct sod_chain {
127\h'2n'size_t n_classes;
128\h'2n'const SodClass *const *classes;
129\h'2n'size_t off_ichain;
130\h'2n'const struct sod_vtable *vt;
131\h'2n'size_t ichainsz;
132};
133
134extern const struct SodClass__ilayout SodClass__classobj;
135#define SodClass__class (&SodClass__classobj.obj.cls)
136.fi
137.ft P
138.
139.\"--------------------------------------------------------------------------
140.SH DESCRIPTION
141.
142This page describes the structure and layout
143of standard Sod objects, classes and associated metadata.
144Note that Sod's object system is very flexible
145and it's possible for an extension
146to define a new root class
147which works very differently from the standard
148.B SodObject
149described here.
150.
151.\"--------------------------------------------------------------------------
152.SH COMMON INSTANCE STRUCTURE
153.
154As described below,
155a pointer to an instance actually points to an
156.I "instance chain"
157structure within the instances overall layout structure.
158.PP
159Instance chains contain slots and vtable pointers,
160as described below.
161All instances have the basic structure of a
162.BR "struct sod_instance" ,
163which has the following members.
164.TP
165.B "const struct sod_vtable *_vt"
166A pointer to a
167.IR vtable ,
168which has the basic structure of a
169.BR "struct sod_vtable" ,
170described below.
171.PP
172A vtable contains static metadata needed
173for efficient conversions and
174message dispatch,
175and pointers to the instance's class.
176Each chain points to a different vtable
177All vtables have the basic structure of a
178.BR "struct sod_vtable" ,
179which has the following members.
180.TP
181.B "const SodClass *_class"
182A pointer to the instance's class object.
183.TP
184.B "size_t _base;"
185The offset of this chain structure
186above the start of the overall instance layout, in bytes.
187Subtracting
188.B _base
189from the instance chain pointer
190finds the layout base address.
191.
192.\"--------------------------------------------------------------------------
193.SH BUILT-IN ROOT OBJECTS
194.
195This section describes the built-in classes
196.B SodObject
197and
198.BR SodClass ,
199which are the standard roots of the inheritance and metaclass graphs
200respectively.
201Specifically,
202.B SodObject
203has no direct superclasses,
204and
205.B SodClass
206is its own metaclass.
9c4a4110
MW
207It is not possible to define root classes in module files
208because of circularities:
47de28ae
MW
209.B SodObject
210has
211.B SodClass
9c4a4110
MW
212as its metaclass,
213and
47de28ae
MW
214.B SodClass
215is a subclass of
216.BR SodObject .
217Extensions can define additional root classes,
218but this is tricky,
219and not really to be recommended.
220.
221.SS The SodObject class
222The
223.B SodObject
a142609c 224class defines no slots.
47de28ae
MW
225Because
226.B SodObject
227has no direct superclasses,
228there is only one chain,
229and no inherited slots or messages,
230so the single chain contains only a vtable pointer.
231.PP
a142609c 232Since
47de28ae
MW
233.B SodClass
234also has only one chain,
235the vtable contains only the standard class pointer and offset-to-base
236members.
237In an actual instance of
238.B SodObject
239(why would you want one?)
240the class pointer contains the address of
241.B SodObject__class
242and the offset is zero.
a142609c
MW
243.PP
244The
245.B init
246message is used to initialize a newly allocated instance.
247.PP
248This message uses a custom method combination
249which works like the standard method combination
250except that default behaviour
251specific to the receiver's direct class
252is invoked if no primary or around method overrides.
253This default behaviour may be invoked multiple times
254if some method calls on its
255.B next_method
256function more than once.
257.PP
258This default behaviour is to initialize the instance's slots
a42893dd
MW
259using the defined slot initializers,
260and execute the initialization fragments.
261Each slot is initialized
a142609c
MW
262using the most specific applicable initializer,
263if any.
264Slots without an initializer
265are left uninitialized.
266.PP
a42893dd
MW
267Slots are initialized and initialization fragments executed together,
268a superclass at a time:
269first, the superclass's slots are initialized (if any);
270then the superclass's initialization fragments (if any) are executed,
271starting with the least specific superclass first.
272Slots and initialization fragments defined by the same class
273are processed in the order in which they appear in the class definition.
a142609c
MW
274.PP
275There are no standard keyword arguments;
276methods on subclasses are free to
277introduce their own in the usual way.
278.PP
279It is usual to provide complex initialization behaviour as
280.B after
281methods.
282This ensures that slots have been initialized as necessary
283before the method executes.
a42893dd
MW
284.PP
285The
286.B teardown
287message is used to tear down an instance which is no longer required.
288.PP
289The message returns an integer flag.
290A zero value means that the instance is safe to deallocate.
291A nonzero value means that the instance should not be deallocated,
292and that it is safe for the caller to simply forget about it.
293This simple protocol may be used, for example,
294to implement a reference-counting system.
295.PP
296This message uses a custom method combination
297which works like the standard method combination
298except that default behaviour is invoked if
299no primary or around method overrides.
300This default behaviour is to execute
301each superclass's teardown fragments,
302most specific first,
303and then return zero to indicate
304that the object is ready for deallocation.
305Teardown fragments defined by the same class
306are processed in the order in which they appear
307in the class definition.
308.PP
309It is usual to provide complex teardown behaviour as
310.B before
311methods.
312Logic to decide whether to allow deallocation
313is usually implemented as
314.B around
315methods.
47de28ae
MW
316.
317.SS The SodClass class
318The
319.B SodClass
320class defines no messages,
321but there are a number of slots.
322Its only direct superclass is
323.B SodObject
324and so (like its superclass) its vtable is trivial.
325.PP
326The slots defined are as follows.
327.TP
328.B const char *name;
329A pointer to the class's name.
330.TP
331.B const char *nick;
332A pointer to the class's nickname.
333.TP
334.B size_t initsz;
335The size in bytes required to store an instance of the class.
336.TP
337.BI "void *(*imprint)(void *" p );
338A pointer to a function:
339given a pointer
340.I p
341to at least
342.I initsz
343bytes of appropriately aligned memory,
344`imprint' this memory it so that it becomes a minimally functional
345instance of the class:
346all of the vtable and class pointers are properly initialized,
347but the slots are left untouched.
348The function returns its argument
349.IR p .
350.TP
47de28ae
MW
351.B size_t n_supers;
352The number of direct superclasses.
353(This is zero exactly in the case of
354.BR SodObject .)
355.TP
356.B const SodClass *const *supers;
357A pointer to an array of
358.I n_supers
359pointers to class objects
360listing the class's direct superclasses,
361in the order in which they were listed in the class definition.
362If
363.I n_supers
364is zero,
365then this pointer is null.
366.TP
367.B size_t n_cpl;
368The number of superclasses in the class's class precedence list.
369.TP
370.B const SodClass *const *cpl;
371A pointer to an array of pointers to class objects
372listing all of the class's superclasses,
373from most- to least-specific,
374starting with the class itself,
375so
376.IB c ->cls.cpl[0]
377=
378.I c
379for all class objects
380.IR c .
381.TP
382.B const SodClass *link;
383If the class is a chain head, then this is a null pointer;
384otherwise it points to the class's distinguished link superclass
385(which might or might not be a direct superclass).
386.TP
387.B const SodClass *head;
388A pointer to the least-specific class in this class's chain;
389so
390.IB c ->cls.head->cls.link
391is always null,
392and either
393.IB c ->cls.link
394is null
395(in which case
396.IB c ->cls.head
397=
398.IR c )
399or
400.IB c ->cls.head
401=
402.IB c ->cls.link->cls.head \fR.
403.TP
404.B size_t level;
405The number of less specific superclasses in this class's chain.
406If
407.IB c ->cls.link
408is null then
409.IB c ->cls.level
410is zero;
411otherwise
412.IB c ->cls.level
413=
414.IB c ->cls.link->cls.level
415+ 1.
416.TP
417.B size_t n_chains;
418The number of chains formed by the class's superclasses.
419.TP
420.B const struct sod_chain *chains;
421A pointer to an array of
422.B struct sod_chain
423structures (see below) describing the class's superclass chains,
424in decreasing order of specificity of their most specific classes.
425It is always the case that
426.IB c ->cls.chains[0].classes[ c ->cls.level]
427=
428.IR c .
429.TP
430.B size_t off_islots;
431The offset of the class's
432.B islots
433structure relative to its containing
434.B ichain
435structure.
436The class doesn't define any slots if and only if this is zero.
437(The offset can't be zero because the vtable pointer is at offset zero.)
438.TP
439.B size_t islotsz;
440The size required to store the class's direct slots,
441i.e., the size of its
442.B islots
443structure.
444The class doesn't define any slots if and only if this is zero.
445.PP
446The
447.B struct sod_chain
448structure describes an individual chain of superclasses.
449It has the following members.
450.TP
451.B size_t n_classes;
452The number of classes in the chain.
453This is always at least one.
454.TP
455.B const SodClass *const *classes;
456A pointer to an array of class pointers
457listing the classes in the chain from least- to most-specific.
458So
459.IB classes [ i ]->cls.head
460=
461.IB classes [0]
462for all
4630 \(<=
464.I i
465<
466.IR n_classes ,
467.IB classes [0]->cls.link
468is always null,
469and
470.IB classes [ i ]->cls.link
471=
472.IB classes [ "i\fR \- 1" ]
473if
4741 \(<=
475.I i
476<
477.IR n_classes .
478.TP
479.B size_t off_ichain;
480The size of the
481.B ichain
482structure for this chain.
483.TP
484.B const struct sod_vtable *vt;
485The vtable for this chain.
486(It is possible, therefore, to duplicate the behaviour of the
487.I imprint
488function by walking the chain structure.
489The
490.I imprint
491function is much faster, though.)
492.TP
493.B size_t ichainsz;
494The size of the
495.B ichain
496structure for this chain.
497.
498.\"--------------------------------------------------------------------------
499.SH CLASS AND VTABLE LAYOUT
500.
501The layout algorithms for Sod instances and vtables are nontrivial.
502They are defined here in full detail,
503since they're effectively fixed by Sod's ABI compatibility guarantees,
504so they might as well be documented for the sake of interoperating
505programs.
506.PP
507Unfortunately, the descriptions are rather complicated,
508and, for the most part not necessary to a working understanding of Sod.
509The skeleton structure definitions shown should be more than enough
510for readers attempting to make sense of the generated headers and tables.
511.PP
512In the description that follows,
513uppercase letters vary over class names,
514while the corresponding lowercase letters indicate the class nicknames.
515Throughout, we consider a class
516.I C
517(therefore with nickname
518.IR c ).
519.
520.SS Generic instance structure
521The entire state of an instance of
522.I C
523is contained in a single structure of type
524.B struct
525.IB C __ilayout \fR.
526.IP
527.nf
528.ft B
529struct \fIC\fB__ilayout {
530\h'2n'union \fIC\fB__ichainu_\fIh\fB {
531\h'4n'struct \fIC\fB__ichain_\fIh\fB {
532\h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
533\h'6n'struct \fIH\fB__islots \fIh\fB;
534\h'6n'\fR...\fB
535\h'6n'struct \fIC\fB__islots {
536\h'8n'\fItype\fB \fIslota\fB;
537\h'8n'\fR...\fB
538\h'6n'} \fIc\fB;
539\h'4n'} \fIc\fB;
540\h'4n'\fR...\fB
541\h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
542\h'2n'} \fIh\fB;
543\h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
544\h'2n'\fR...\fB
545};
546
547typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
548.ft P
549.fi
550.PP
551The set of superclasses of
552.IR C ,
553including itself,
554can be partitioned into chains
555by following their distinguished superclass links.
556(Formally, the chains are the equivalence classes determined by
557the reflexive, symmetric, transitive closure of
558the `links to' relation.)
559Chains are identified by naming their least specific classes;
560the least specific class in a chain is called the
561.IR "chain head" .
562Suppose that the chain head of the chain containing
563.I C
564itself is named
565.I H
566(though keep in mind that it's possible that
567.I H
568is in fact
569.I C
570itself.)
571.PP
572The
573.B ilayout
574structure contains one member for each of
575.IR C 's
576superclass chains.
577The first such member is
578.IP
579.B
580.B union
581.IB C __ichainu_ h
582.IB h ;
583.PP
584described below;
585this is followed by members
586.IP
587.B union
588.IB B __ichainu_ i
589.IB i ;
590.PP
591for each other chain,
592where
593.I I
594is the head
595and
596.I B
597the tail (most-specific) class of the chain.
598The members are in decreasing order
599of the specificity of the chains' most-specific classes.
600(Note that all but the first of these unions
601has already been defined as part of
602the definition of the corresponding
603.IR B .)
604.PP
605The
606.B ichainu
607union contains a member for each class in the chain.
608The first is
609.IP
610.B struct
611.IB C __ichain_ h
612.IB c ;
613.PP
614and this is followed by corresponding members
615.IP
616.B struct
617.IB A __ichain_ h
618.IB a ;
619.PP
620for each of
9c4a4110
MW
621.IR C 's
622superclasses
47de28ae
MW
623.IR A
624in the same chain in some (unimportant) order.
625A `pointer to
626.IR C '
627is always assumed
628(and, indeed, defined in C's type system)
629to be a pointer to the
630.B struct
631.IB C __ichain_ h \fR.
632.PP
633The
634.B ichain
635structure contains (in order), a pointer
636.IP
637.B const
638.B struct
639.IB C __vt_ h
640.B *_vt;
641.PP
642followed by a structure
643.IP
644.B struct
645.IB A __islots
646.IB a ;
647.PP
648for each superclass
649.I A
650of
651.IR C
652in the same chain which defines slots,
653from least- to most-specific;
654if
655.I C
656defines any slots,
657then the last member is
658.IP
659.B struct
660.IB C __islots
661.IB c ;
662.PP
663Finally, the
664.B islots
665structure simply contains one member for each slot defined by
666.I C
667in the order they appear in the class definition.
668.
669.SS Generic vtable structure
670As described above,
671each
672.B ichain
673structure of an instance's storage
674has a vtable pointer
675.IP
676.B const
677.B struct
678.IB C __vt_ h
679.B *_vt;
680.PP
681In general,
682the vtables for the different chains
683will have
684.I different
685structures.
686.PP
687The instance layout split neatly into disjoint chains.
688This is necessary because
689each
690.B ichain
691must have as a prefix the
692.B ichain
9c4a4110
MW
693for each superclass in the same chain,
694and each slot must be stored in exactly one place.
47de28ae
MW
695The layout of vtables doesn't have this second requirement:
696it doesn't matter that there are
697multiple method entry pointers
698for the same effective method
699as long as they all work correctly.
9c4a4110
MW
700Indeed, it's essential that they do,
701because each chain's method entry function
702will need to apply a different offset to the receiver pointer
703before invoking the effective method.
47de28ae
MW
704.PP
705A vtable for a class
706.I C
707with chain head
708.I H
709has the following general structure.
710.IP
711.nf
712.ft B
713union \fIC\fB__vtu_\fIh\fB {
714\h'2n'struct \fIC\fB__vt_\fIh\fB {
715\h'4n'const \fIP\fB *_class;
716\h'4n'size_t _base;
717\h'4n'\fR...\fB
718\h'4n'const \fIQ\fB *_cls_\fIj\fB;
719\h'4n'\fR...\fB
720\h'4n'ptrdiff_t _off_\fIi\fB;
721\h'4n'\fR...\fB
722\h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
723\h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
724\h'6n'\fR...\fB
725\h'4n'} \fIa\fB;
726\h'4n'\fR...\fB
727\h'2n'} \fIc\fB;
728};
729
730extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
731.ft P
732.fi
733.PP
734The outer layer is a
47de28ae
MW
735.B union
736.IB C __vtu_ h
47de28ae
MW
737containing a member
738.IP
739.B struct
740.IB A __vt_ h
741.IB a ;
742.PP
743for each of
744.IR C 's
745superclasses
746.I A
747in the same chain,
748with
749.I C
750itself listed first.
751This is mostly an irrelevant detail,
752whose purpose is to defend against malicious compilers:
753pointers are always to one of the inner
754.B vt
755structures.
756It's important only because it's the outer
757.B vtu
758union which is exported by name.
759Specifically, for each chain of
760.IR C 's
761superclasses
762there is an external object
763.IP
764.B const union
765.IB A __vtu_ i
766.IB C __vtable_ i ;
767.PP
768where
769.I A
770and
771.I I
772are respectively the most and least specific classes in the chain.
773.PP
774The first member in the
775.B vt
776structure is the
777.I root class pointer
778.IP
779.B const
780.IR P
781.B *_class;
782.PP
783Among the superclasses of
784.I C
785there must be exactly one class
786.I O
787which itself has no direct superclasses;
788this is the
789.I root superclass
790of
791.IR C .
792(This is a rule enforced by the Sod translator.)
793The metaclass
794.I R
795of
9c4a4110 796.I O
47de28ae
MW
797is then the
798.I root metaclass
799of
800.IR C .
801The
802.B _class
803member points to the
804.B ichain
805structure of most specific superclass
806.I P
807of
808.I M
809in the same chain as
810.IR R .
811.PP
812This is followed by the
813.I base offset
814.IP
815.B size_t
816.B _base;
817.PP
818which is simply the offset of the
819.B ichain
820structure from the instance base.
821.PP
822The rest of the vtable structure is populated
823by walking the superclass chain containing
824.I C
825as follows.
826For each such superclass
827.IR B ,
828in increasing order of specificity,
829walk the class precedence list of
830.IR B ,
831again starting with its least-specific superclass.
832(This complex procedure guarantees that
833the vtable structure for a class is a prefix of
834the vtable structure for any of its subclasses in the same chain.)
835.PP
836So, let
837.I A
838be some superclass of
839.I C
840which has been encountered during this traversal.
841.hP \*o
842Let
843.I N
844be the metaclass of
845.IR A .
846Examine the superclass chains of
847.I N
848in order of decreasing specificity of their most-specific classes.
849Let
850.I J
851be the chain head of such a chain,
852and let
853.I Q
854be the most specific superclass of
855.I M
856in the same chain as
857.IR J .
858Then, if there is currently no class pointer of type
9c4a4110 859.IR Q ,
47de28ae
MW
860then add a member
861.RS
862.IP
863.B const
864.I Q
865.BI *_cls_ j ;
866.PP
867to the vtable
868pointing to the appropriate
869.B islots
870structure within
871.IR M 's
872class object.
873.RE
874.hP \*o
875Examine the superclass chains of
876.I A
877in order of decreasing specificity of their most-specific classes.
878Let
879.I I
880be the chain head of such a chain.
881If there is currently no member
882.BI _off_ i
883then add a member
884.RS
885.IP
886.B ptrdiff_t
887.BI _off_ i ;
888.PP
889to the vtable,
890containing the (signed) offset from the
891.B ichain
892structure of the chain headed by
893.I h
894to that of the chain headed by
895.I i
896within the instance's layout.
897.RE
898.hP \*o
899If class
900.I A
901defines any messages,
902and there is currently no member
9c4a4110 903.IR a ,
47de28ae
MW
904then add a member
905.RS
906.IP
907.B struct
908.IB C __vtmsgs_ a
909.IB a ;
910.PP
911to the vtable.
912See below.
913.RE
914.PP
915Finally, the
916.B vtmsgs
917structures contain pointers to the effective method entry functions
918for the messages defined by a superclass.
919There may be more than one method entry for a message,
920but all of the entry pointers for a message appear together,
921and entry pointers for separate messages appear
922in the order in which the messages are defined.
923If the receiver class has no applicable primary method for a message
924then it's usual for the method entry pointer to be null
925(though, as with a lot of things in Sod,
926extensions may do something different).
927.PP
928For a standard message which takes a fixed number of arguments,
929defined as
930.IP
931.I tr
932.IB m ( \c
933.I t1
934.IB a1 ,
935.RB ... ,
936.I tn
937.IB an );
938.PP
939there is always a `main' entry point,
940.IP
941.I tr
942.BI (* m )( \c
943.I C
944.BI * me ,
945.I t1
946.IB a1 ,
947.RB ... ,
948.I tn
949.IB an );
950.PP
951For a standard message which takes a variable number of arguments,
952defined as
953.IP
954.I tr
955.IB m ( \c
956.I t1
957.IB a1 ,
958.RB ... ,
959.I tn
960.IB an ,
961.B ...);
962.PP
43073476
MW
963or a standard message which takes keyword arguments,
964defined as
965.IP
966.I tr
967.IB m ( \c
968.I t1
969.IB a1 ,
970.RB ... ,
971.I tn
972.IB an ?\&
973.IR tn +1
974.IR kn +1
975.RB [ =
976.IR dn +1] \c
977.B ,
978.I tm
979.I km
980.RB [ =
981.IR dm ] \c
982);
983.PP
47de28ae
MW
984two entry points are defined:
985the usual `main' entry point
986which accepts a variable number of
987arguments,
988and a `valist' entry point
989which accepts an argument of type
990.B va_list
43073476
MW
991in place of the variable portion of the argument list
992or keywords.
47de28ae
MW
993.IP
994.I tr
995.BI (* m )( \c
996.I C
997.BI * me ,
998.I t1
999.IB a1 ,
1000.RB ... ,
1001.I tn
1002.IB an ,
1003.B ...);
1004.br
1005.I tr
1006.BI (* m __v)( \c
1007.I C
1008.BI * me ,
1009.I t1
1010.IB a1 ,
1011.RB ... ,
1012.I tn
1013.IB an ,
1014.B va_list
1015.IB sod__ap );
1016.
1017.SS Additional definitions
1018In addition to the instance and vtable structures described above,
1019the following definitions are made for each class
1020.IR C .
1021.PP
1022For each message
1023.I m
1024directly defined by
1025.I C
1026there is a macro definition
1027.IP
1028.B #define
1029.IB C _ m ( me ,
1030.RB ... )
1031.IB me ->_vt-> c . m ( me ,
1032.RB ... )
1033.PP
1034which makes sending the message
1035.I m
1036to an instance of (any subclass of)
1037.I C
1038somewhat less ugly.
1039If
1040.I m
1041takes a variable number of arguments,
43073476 1042or keyword arguments,
47de28ae
MW
1043the macro is more complicated
1044and is only available in compilers advertising C99 support,
1045but the effect is the same.
1046For each variable-argument message,
1047there is also an additional macro
1048for calling the `valist' entry point.
1049.IP
1050.B #define
1051.IB C _ m __v( me ,
1052.RB ...,
1053.IB sod__ap )
1054.if !t \{\
1055\e
1056.br
1057\h'4m'\c
1058.\}
1059.IB me ->_vt-> c . m __v( me ,
1060.RB ...,
1061.IB sod__ap )
1062.PP
1063For each proper superclass
1064.I A
1065of
1066.IR C ,
1067there is a macro defined
1068.IP
1069.I A
1070.BI * C __CONV_ a ( C
1071.BI * _obj );
1072.PP
1073(named in
1074.IR "upper case" )
1075which converts a (static-type) pointer to
1076.I C
1077to a pointer to the same actual instance,
1078but statically typed as a pointer to
1079.IR A .
1080This is most useful when
1081.I A
1082is not in the same chain as
1083.I C
1084since in-chain upcasts are both trivial and rarely needed,
1085but the full set is defined for the sake of completeness.
1086.PP
1087Finally, the class object is defined as
1088.IP
1089.B extern const struct
1090.IB R __ilayout
1091.IB C __classobj;
1092.br
1093.B #define
1094.IB C __class
1095.BI (& C __classobj. j . r )
1096.PP
1097The exported symbol
1098.IB C __classobj
1099contains the entire class instance.
1100This is usually rather unwieldy.
1101The macro
1102.IB C __class
1103is usable as a pointer of type
1104.B const
1105.I R
1106.BR * ,
1107where
1108.I R
1109is the root metaclass of
1110.IR C ,
1111i.e., the metaclass of the least specific superclass of
1112.IR C ;
1113usually this is
1114.BR "const SodClass *" .
1115.
1116.\"--------------------------------------------------------------------------
1117.SH SEE ALSO
1118.BR sod (3).
1119.
1120.\"--------------------------------------------------------------------------
1121.SH AUTHOR
1122Mark Wooding, <mdw@distorted.org.uk>
1123.
1124.\"----- That's all, folks --------------------------------------------------