07.12.2012 Views

Object-Oriented Programming With ANSI-C (pdf)

Object-Oriented Programming With ANSI-C (pdf)

Object-Oriented Programming With ANSI-C (pdf)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

110 __________________________________________________________________________<br />

9 Static Construction — Self-Organization<br />

order. In order to avoid trivial but hard to diagnose errors, we should provide a<br />

mechanism which performs these function calls automatically — our programs<br />

should be self-organizing.<br />

One solution is to use a linking technique, for example with the aid of a program<br />

such as munch, to produce an array with the addresses of all initialization<br />

functions and call each array element at the beginning of a main program. A function<br />

main() with a loop executing the array can be part of our project library, and<br />

each program starts with a function mainprog() which is called by main().<br />

Another solution is to let an initialization function return the initialized object. If<br />

the function is locked so that it does the actual work only once we can replace each<br />

reference to a static object by a call to its initialization function. Alternatively, we<br />

can use macros to produce the same effect more efficiently. Either way we can no<br />

longer take the address of a reference to a static object, but because the reference<br />

itself is a pointer value, this should hardly be necessary.<br />

9.6 Exercises<br />

The Class() macro is a more efficient, portable solution for automatic initialization of<br />

class descriptions than using functions. It is implemented by changing reports,<br />

class definitions, and application programs just as described above.<br />

munch may have to be ported to a new system. If it is used together with the<br />

Class() macro, for a production system we can remove the conditional from the<br />

macro and initialize all class descriptions with munch. How do we initialize things in<br />

the right order? Can ooc be used to help here (consult the manual in appendix C<br />

about option −M for occ)? What about cast() in a production system?<br />

All class descriptions should first show up in calls to cast(). We can define a<br />

fake class<br />

typedef const void * (* initializer) (void);<br />

% Class ClassInit: <strong>Object</strong> {<br />

initializer init;<br />

%}<br />

and use statically initialized instances as ‘‘uninitialized’’ class descriptions:<br />

static struct ClassInit _Point = {<br />

{ MAGIC, 0 }, /* <strong>Object</strong> without class description */<br />

initPoint /* initialization function */<br />

};<br />

const void * Point = & _Point;<br />

cast() can now discover a class description with a null class description pointer,<br />

assume that it is a struct ClassInit, and call the initialization function. While this<br />

solution reduces the number of unnecessary function calls, how does it influence<br />

the use of cast()?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!