Object-Oriented Programming Using SOM and DSOM:A Complement to C++:Dynamic Class Loading

From EDM2
Jump to: navigation, search

Suppose you are building a graphical user interface (GUI) builder that supports reuseable parts. You might have a push-button control, a list-box control, or some other user-defined controls. Each of these controls are implemented as objects that exhibit different behavior. A typical user will ask the GUI builder to create controls and use them in the applications. Since not all the controls are predefined at compile time, it will be hard to use C++ to implement such a system because C++ requires the class name to be known at compile time. What we need is a system that supports the dynamic loading of classes that are unknown at compile time.

SOM provides methods that let you dynamically load a class and create a class object when the name of the class is unknown at compile time. This is possible because, in SOM, classes are objects at run-time, as opposed to C++, where classes are types, which are fixed at compile time. The SOMClassMgr class provides two methods for dynamically loading and creating class objects: somFindClsl nFile and somFind Class.

The somFindCislnFile Method

The somFindClslnFile method is the more restrictive of the two. It requires the specific name of the DLL in which your class resides. The following shows the syntax for the method in C.

myClass=_somFindCislnFile(SOMCiassMgrObject, //global instance
                          classId,           //somld for the class
                          classMajorVersion, //class major version number
                          classMinorVersion, //class minor version number
                          dllname);          //DLL filename

The parameter SOMClassMgrObject is a pointer to the instance SOMClassMgr. There is only one instance of SOMClassMgr, and it is created during SOM initialization. The parameter classld is a somld that represents the name of the class. It can be obtained by passing the name of the class to the function somldFromString. The parameters classMajorVersion and classMinorVersion are normally set to 0, unless you want to check against the version numbers of the class. The parameter dllname specifies the name of the DLL that contains the class. You must specify the complete pathname for the DLL. The DLL must also be placed in one of the directories specified on the LIBPATH statement in your config.sys file.

The DYNALOAD program shown in Figure 4.5 uses somFindClslnFile to dynamically load the DLL domestic that we built in the previous section. It takes the name of the class as an input parameter. Ifthe class is created, it will invoke the display method on the animal to display its characteristics.

We run the program with different input.

I am a Dog
I am a Cat
Can't load class Bird
#include <iostream.h>
#include <Som.xh>
#include <somcm.xh>

main(int argc, char ·argvQ, char *envpO)
  SOMClass *myClass;
  somId classId;
  //I Initialize SOM run-time environment
  somEnvironmentNew() ;

  classld = somldFromString( argv[1] );
  myClass = SOMCiassMgrObject->somFindCislnFile(classld,

  if (myClass)
    SOMObject *myObj;
    Environment *ev;

    ev = somGetGiobaiEnvironment();
    myObJ = myClass->somNew();
    myObj-:>somDispatch( (som Token*)O,
                          somld FromString("display"),
                          ev) ;
     cout << wcan't load class .<< argv[1] << ''\n";

Figure 4.5 The DYNALOAD program

The DYNALOAD program illustrates how one can invoke a method, when the language bindings for a class are not available. Once the class object is created, the som.New method is called to create an instance of the class. The somDispatch method can then be called to invoke a method on the object.

The somFindCiass Method

Another method that can be used to create a class object is somFindClass. The somFindClass method is similar to somFindClslnFile, except that you do not have to specify the DLL name. The C syntax for somFindClass is shown below:

myClass = _somFindCiass(SOMClassMgrObject,  //global instance
                        classld,            //somld for the class
                        classMajorVersion,  //class major version number
                        classMinorVersion); //class minor version number

There are a few things to note when using somFindClass. The somFindClass method uses somLocateClassFile to obtain the name of the file that contains the class. The default implementation of somLocateClassFile checks the Interface Repository for the value of the dllname modifier of the class. We discussed the setting of the dllname modifier in Section 4.2.1 on page 79. To populate the Interface Repository, run the SOM compiler with the -u option. The following command shows how you can update the Interface Repository with the Cat and the Dog IDL files.

sc -sir -u cat.idl dog.idl

The Interface Repository is a database that maintains information about classes described in IDL files. It is used by the Distributed, Persistence, and Replication Framework. We will look at some of the programming interfaces in Chapter 8.

Note that if the dllname modifier is not specified, then somLocateClassFile will return the class name as the DLL name. If your class does not reside in a DLL that has the same name, then you will get an error indicating that the class is not found. Therefore, you should always explicitly set the dllname modifier, or follow the convention: the name of the class is the same as the name of the DLL that contains the class.