Share
 
 

Runtime Class Identification

Every class in the ObjectARX hierarchy that is derived from AcRxObject has a corresponding class descriptor object, which is an instance of AcRxClass that holds information for runtime type identification. The class descriptor object, gpDesc, is a static data member of the class—for example, AcDbEllipse::gpDesc. Class descriptor objects are created at initialization, when classes are registered with ObjectARX and are added to a system-level dictionary, acrxClassDictionary. The macros described here facilitate the declaration and implementation of certain functions related to runtime identification and initialization functions. These include the class initialization routine as well as the desc(), cast(),isKindOf(), and isA() functions for the custom class.

Important functions provided by the AcRxObject class for runtime type identification include the following:

  • desc(), a static member function that returns the class descriptor object of a particular (known) class.
  • cast(), a static member function that returns an object of the specified type, or NULL if the object is not of the required class (or a derived class).
  • isKindOf() returns whether an object belongs to the specified class (or a derived class).
  • isA() returns the class descriptor object of an object whose class is unknown.

When you want to know what class an object is, use AcRxObject::isA(). This function returns the class descriptor object (an instance of AcRxClass) for a database object. Its signature is

AcRxClass* isA() const;

When you already know what class an object is, you can use the desc() function to obtain the class descriptor object:

static AcRxClass* desc();

The following example looks for instances of AcDbEllipse or any class derived from it, using isKindOf() and the AcDbEllipse::desc() static member function:

AcDbEntity* curEntity = somehowGetAndOpenAnEntity();
if (curEntity->isKindOf(AcDbEllipse::desc())) {
   // Got some kind of AcDbEllipse instance.
}

This example shows another way of looking for instances of AcDbEllipse, or any class derived from it, using the AcDbEllipse::cast() static member function:

AcDbEllipse* ellipseEntity = AcDbEllipse::cast(curEntity);
if (ellipseEntity != NULL) {
   // Got some kind of AcDbEllipse instance.
}

The following example looks for instances of AcDbEllipse, but not instances of classes derived from AcDbEllipse, using isA() and AcDbEllipse::desc():

if (curEntity->isA() == AcDbEllipse::desc()) {
   // Got an AcDbEllipse, no more, no less.

Was this information helpful?