Kernel SDK Developer's Guide > Run-Time Type Identification > Understanding the Dictionary of Classes
Understanding the Dictionary of Classes

The RTTI implementation includes a special global dictionary object that stores the list of all classes that were registered in the application. This dictionary associates the pointer to the class describing instance with the class name. The association <pointer — name> is the item of the class dictionary. The class name is the keyword. Additionally, the dictionary matches the integer number with each item that is the class ID. All items form the collection of the class describing instances; the instances are placed in free memory.

When an application registers the class using the rxInit() method, the newOdRxClass() global function creates a new class describing instance and adds a new item in the class dictionary with a keyword that equals the class name. When an application unregisters the class using the rxUninit() method, the deleteOdRxClass() global function releases the class describing instance and removes its item from the class dictionary.

To get the class dictionary, use the odrxClassDictionary() global function to return the smart pointer to the dictionary of the registered classes. For example:

OdRxDictionaryPtr  pClsDict = odrxClassDictionary();

The class dictionary can manipulate items using the class name or the class ID. If you know the class name (key), you can get the class ID. If you know the class ID, you can get the class name (key). If you know either the class name or the class ID, you can get the class describing instance. You can also check whether the class name or class ID exists in the class dictionary.

To get the class ID for a known class name, use the idAt() method which requires the class name as an argument of the OdString type and returns the class ID as a value of the OdUInt32 type. For example:

odPrintConsoleString(L"\nThe key \"%s\" has the ID = %d", L"OdValue", pClsDict->idAt(L"OdValue"));

To get the class name for a known class ID, use the keyAt() method which requires the class ID as an argument of the OdUInt32 type and returns the class name of the OdString type. For example:

odPrintConsoleString(L"\nThe ID %d has the key \"%s\"", 10, pClsDict->keyAt(10).c_str());

If the specified class ID does not exist in the class dictionary, the keyAt() method generates the aNotApplicable exception. If the specified class name does not exist in the class dictionary, the idAt() method returns a (–1) value.

To check whether the class name or class ID exists in the dictionary, use the has() method which requires the class name or class ID as an argument and returns True when the key or ID exists in the dictionary or returns False otherwise. For example:

void PrintKeyExist(const OdString& key)
{
  OdRxDictionaryPtr  pClsDict = odrxClassDictionary();
  odPrintConsoleString(L"\nThe dictionary %s the \"%s\" key", ((pClsDict->has(key) == true) ? L"has" : L"does not have"), key);
}  
void PrintIdExist(const OdUInt32 id)
{
  OdRxDictionaryPtr  pClsDict = odrxClassDictionary();
  odPrintConsoleString(L"\nThe dictionary %s the ID = %d", ((pClsDict->has(id) == true) ? L"has" : L"does not have"), id);
}

When the has() method or idAt() method searches for the class name, the search is case-insensitive by default ("MyClass" and "MYCLASS" are equivalent). The isCaseSensitive() method returns True when searching keys is case-sensitive or False when it is case-insensitive. For example:

odPrintConsoleString(L"\nThe finding of keys is case-%s", ((pClsDict->isCaseSensitive()) ? L"sensitive" : L"insensitive"));

To get the number of registered classes, use the numEntries() method which does not have arguments and returns the number of items as an integer value. For example:

odPrintConsoleString(L"\nThe dictionary contains %d classes", pClsDict->numEntries());

To get the class describing instance for the known class name or class ID, use the getAt() method which requires the class name or class ID as an argument and returns the non-typified smart pointer to the class describing instance. For example:

void PrintClassInfo(const OdUInt32 id)
{
  OdRxDictionaryPtr  pClsDict = odrxClassDictionary();

  if(pClsDict->has(id) == false)
  {
    odPrintConsoleString(L"\nID = %d  is not found in the dictionary", id);
  }
  else
  { OdRxClassPtr pClsPtr = (OdRxClassPtr) pClsDict->getAt(id);

    odPrintConsoleString(L"\nClass Describing Instance [%x]", pClsPtr);
    odPrintConsoleString(L"\nClass Name = %s", pClsPtr->name().c_str());
    odPrintConsoleString(L"\nProxy flags = %x", pClsPtr->proxyFlags());
    odPrintConsoleString(L"\nDXF Class Name = %s", pClsPtr->dxfName().c_str());
    odPrintConsoleString(L"\nParent Class Name = %s", pClsPtr->myParent()->name().c_str());
    odPrintConsoleString(L"\nApplication Class Name = %s", pClsPtr->appName().c_str());
  }
}

To iterate through the class dictionary, use the dictionary iterator created by the newIterator() method as it is described in the Iterating through Items of a Dictionary topic.

The class dictionary also inherits the following from the OdRxDictionary class:

  • putAt() method — Adds a new item in the class dictionary and associates the specified class describing instance with the class name.
  • resetKey() method — Sets the specified key for the specified ID instead of the old key.
  • remove() method — Removes the item with the specified key or ID from the class dictionary but does not delete the associated class describing instance.

You should not use these methods for modifying the class dictionary because it can disturb the register and unregister procedures that perform additional actions for classes subject to the implementation of the rxInit() and rxUninit() static methods. The rxInit() method of the class must register the class and must add its item to the class dictionary. The rxUninit() method of the class must unregister the class and must remove its item from the class dictionary. If you otherwise directly modify the class dictionary in the program, conflict and errors can occur.

See Also

RTTI Technology

Overview of Classes that Implement RTTI

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.