Callbacks in C++


destroyer of type is restorer

problem:

You need to take some typed data and pack it into a format that contains no type information (e.g. char array, or cast pointer type to void*). How do you extract this data later in a type safe way?

context:

forces:

solution:

Create a single object that will both write to and read from the typeless data. When storing data, this object will be the last to see the actual type of the data. When restoring the data, the object that was the last to see the data type information will be the object that casts the typeless data to having the correct type again. Often this object will be templated by the actual type of the data to create a general type destroyer and restorer.

forces resolved:

design rational:

There are times, such as when aggressively factoring a template, that it is handy to write a class in terms of void pointers, or create a class that can store information in a fixed size array. Another object, usually a template, can provide a type safe veneer over the generically typed object. The type safe layer converts to and from the generic types. Only the type safe layer can access the generically typed layer, so it knows exactly what is stored in the generic layer and can perform the casts without worrying.

[View/Add Comments]


Callbacks in C++
Copyright © 1996-2000 Paul Jakubik
Created: 26 July 1996. Last update: 26 November 2000.
pauljakubik@yahoo.com