3 .\" The Sod runtime library
5 .\" (c) 2015 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the Sensible Object Design, an object system for C.
12 .\" SOD is free software; you can redistribute it and/or modify
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.
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
20 .\" GNU Library General Public License for more details.
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.
27 .\" Highlight using terminal escapes, rather than overstriking.
30 .\" String definitions and font selection.
39 .\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left
42 \h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
45 .\"--------------------------------------------------------------------------
46 .TH sod 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
49 sod \- Sensible Object Design runtime library
51 .\"--------------------------------------------------------------------------
53 .B #include <sod/sod.h>
58 .BI "const " cls " *" obj );
66 .IB cls "__ilayout *" \c
70 .BI "const void *" obj );
74 .BI "const " cls " *" obj );
78 .BI "const " cls " *" obj );
82 .BI "const SodClass *" sub ,
83 .BI "const SodClass *" super );
88 .BI "const void *" obj );
92 .BI "const SodClass *" cls ,
93 .BI "const void *" obj );
103 .BI "const SodClass *" cls ,
109 .BI "const SodClass *" cls ,
128 .BI "const SodClass *" cls ,
133 .BI "const SodClass *" cls ,
141 .\"--------------------------------------------------------------------------
144 The functions and macros defined here generally expect that
145 instances and classes inherit from the standard
148 While the translator can (at some effort) support alternative roots,
149 they will require different run-time support machinery.
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.
159 These macros are mostly intended for use in
160 code generated by the Sod translator.
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.
167 macro returns the signed offset between
168 two members of a structure or union type.
169 Given a structure or union type
180 gives the difference, in bytes,
189 This macro is used internally when generating vtables
190 and is not expected to be very useful elsewhere.
194 macro recovers the instance layout base address
195 from a pointer to one of its instance chains.
196 Specifically, given a class name
200 of the least specific class in one of
205 to the instance storage for the chain containing
207 within an exact instance of
209 (i.e., not an instance of any proper subclass),
214 returns the a pointer to the layout structure containing
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.
222 macro (described below) is more suited to general use.
226 macro finds the base address of an instance's layout.
228 .BI "const " cls " *" obj
230 .BI SOD_INSTBASE( obj )
231 returns the base address of the storage allocated to
233 This is useful if you want to free a dynamically allocated instance,
235 This macro needs to look up an offset in
237 vtable to do its work.
241 which is faster but requires
242 precise knowledge of the instance's dynamic class.
245 The following macros and functions
246 query the runtime relationhips between
247 instances and classes.
251 macro returns the class object describing an instance's dynamic class.
253 .BI "const " cls " *" obj
255 .BI SOD_CLASSOF( obj )
262 is typed correctly in the first place)
263 will be a subclass of
265 (If you wanted the class object for
269 .IB cls __class \fR.)
273 function answers whether one class
275 is actually a subclass of another class
280 returns nonzero if and only if
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.
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.
296 macro performs a `cross-chain upcast'.
300 to an instance of a class of type
304 of the least specific class in one of
306 superclass chains which does not contain
312 returns the address of that chain's storage
313 within the instance layout as a raw
318 is not mentioned explicitly.)
319 This macro is used by the generated
322 which you are encouraged to use instead where possible.
330 perform general conversions
331 (up-, down-, and cross-casts) on instance pointers.
335 .BI "const void *" obj
337 they return an appropriately converted pointer to
341 is indeed an instance of (some subclass of)
343 otherwise they return a null pointer.
353 expects a pointer to a class object instead.
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.
360 is a superclass of the static type of
362 the automatically defined conversion macros should be used instead,
363 because they're much faster and can't fail.
365 When the target class is known statically,
366 it's slightly more convenient to use the
368 macro rather than the
371 since the class object name is longer and uglier,
372 and the macro returns a pointer of the correct type.
374 .SS Instance lifecycle
375 The following macros and functions
376 manage the standard steps along
377 an instance's lifecycle.
387 imprint and initialize an instance of a class
389 in the storage starting at address
392 The direct class for the new instance is specified as
395 or a pointer to a class object to the functions.
397 Keyword arguments for the initialization message may be provided.
400 macro expects a single preprocessor-time argument
401 which is a use of one of
409 function expects the keywords as
410 a variable-length argument tail;
413 expects the keywords to be passed indirectly,
414 through the captured argument-tail cursor
417 The return value is an instance pointer for the class
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
425 by the layout rules described in
430 function tears down an instance of a class,
431 releasing any resources it holds.
433 This function is a very thin wrapper around sending the
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.)
445 macro declares and initializes an instance
446 with automatic storage duration.
456 to be a pointer to an instance of
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.
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
475 The instance has automatic storage duration:
476 pointers to it will become invalid
477 when control exits the scope of the declaration.
479 the instance should be torn down before this happens,
483 It may be appropriate to
485 that the object is ready for deallocation at this time.
495 construct and return a pointer to a new instance of a class.
497 The direct class for the new instance is specified as a class name to
499 or as a class object to the functions.
501 Keyword arguments for the initialization message may be provided.
504 macro expects a single preprocessor-time argument
505 which is a use of one of
513 function expects the keywords as
514 a variable-length argument tail;
517 expects the keywords to be passed indirectly,
518 through the captured argument-tail cursor
521 Storage for the new instance will have been allocated
525 The easiest way to destroy the instance,
526 when it is no longer needed,
527 is probably to call the
531 The return value is an instance pointer for the class
535 macro will have converted it to the correct type,
536 so it should probably be used where possible.
540 function tears down and frees an instance allocated using
545 should be an instance pointer,
546 i.e., a pointer to any of an instance's chains.
547 The instance is torn down,
551 If the instance reports itself ready for deallocation,
552 then its storage is released using
554 The return value is the value returned by the
558 .\"--------------------------------------------------------------------------
564 .\"--------------------------------------------------------------------------
566 Mark Wooding, <mdw@distorted.org.uk>
568 .\"----- That's all, folks --------------------------------------------------