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