casa
$Rev:20696$
|
Primitive Run Time Type Information (RTTI) More...
#include <Register.h>
Public Member Functions | |
template<class t > | |
uInt | Register (const t *) |
This is a templated function which takes a pointer to any class as a parameter. |
Primitive Run Time Type Information (RTTI)
Public interface
This function is called Register because the user is registering a type with the run-time type mechanism.
This function returns a unique unsigned integer (uInt) for each type of pointer the user passes in as a parameter. It will always return the same number for a given type during a particular execution. The unsigned integer which is returned can be use to identify the particular type.
Warning: This function does not work correctly for multiple inheritance, but ONLY for single inheritance; In addition, the type number which is returned is NOT unique across program executions;
This RTTI mechanism is simple, it does not require extra functions to be added to the classes which are to be identified, and it is similar to the RTTI mechanism which will be a part of the C++ language in the future.
To be useful, however, this mechanism must be used as part of the implementation of a virtual member function. For example:
#include <casa/Utilities/Register.h> #include <iostream> class foo { public: virtual uInt type() { return Register(this);}}; class bar : public foo { public: uInt type() { return Register(this);}}; main() { foo *b = new bar(); foo *f = new foo(); cout << "f: type()=" << f->type() << " Register()=" << Register(f) << endl; cout << "b: type()=" << b->type() << " Register()=" << Register(b) << endl; }
The output of this code would look something like:
f: type()=1 Register()=1 b: type()=2 Register()=1
Without the virtual function, type()
, the output of Register()
is deceiving and of little use.
Needed a simple type identification mechanism for the Notice class. This was necessary so that multiple notices could be distinguished. It can be replaced by the future standard RTTI.
Definition at line 106 of file Register.h.
uInt casa::Register_global_functions_register::Register | ( | const t * | ) |
This is a templated function which takes a pointer to any class as a parameter.
The parameter's type is then used to generate a unique id. The parameter is a pointer rather than a value for efficiency reasons. With a value parameter, it would be difficult to do things like:
Register(static_cast<MyClass*>(0));
to find the Register
type id for a random class.