To create a class object when not using the C/C++ language bindings for the class, or when the class name is not known at compile time:
The somEnvironmentNew function initializes the SOM run-time environment. That is, it creates the four primitive SOM objects (SOMClass, SOMObject, SOMClassMgr, and the SOMClassMgrObject), and it initializes SOM global variables. The function takes no arguments and returns a pointer to the SOMClassMgrObject.
Note: Although somEnvironmentNew must be called before using other SOM functions and methods, explicitly calling somEnvironmentNew is usually not necessary when using the C/C++ bindings, because the macros for <className>NewClass, <className>New , and <className>Renew call it automatically, as does the new operator for C++. Calling somEnvironmentNew repeatedly does no harm.
After the SOM run-time environment has been initialized, the methods somFindClass and somFindClsInFile can be used to create a class object. These methods must be invoked on the class manager, which is pointed to by the global variable SOMClassMgrObject. (It is also returned as the result of somEnvironmentNew.)
The somFindClass method takes the following arguments:
classId
The version numbers are checked against the version numbers built into the class library to determine if the class is compatible with the client's expectations.
The somFindClass method dynamically loads the DLL containing the class's implementation, if needed, creates the class object (unless it already exists) by invoking its <className>NewClass procedure, and returns a pointer to it. If the class could not be created, somFindClass returns NULL. For example, the following C code fragment creates the class "Hello" and stores a pointer to it in "myClass":
SOMClassMgr cm = somEnvironmentNew(); somId classId = somIdFromString("Hello"); SOMClass myClass = _somFindClass(SOMClassMgrObject, classId Hello_MajorVersion, Hello_MinorVersion); ... SOMFree(classId);
The somFindClass method uses somLocateClassFile to get the name of the library file containing the class. If the class was defined with a "dllname" class modifier, then somLocateClassFile returns that file name; otherwise, it assumes that the class name is the name of the library file. The somFindClsInFile method is similar to somFindClass, except that it takes an additional (final) argument-the name of the library file containing the class. The somFindClsInFile method is useful when a class is packaged in a DLL along with other classes and the "dllname" class modifier has not been given in the class's IDL specification.
Warning: On AIX, the somFindClass and somFindClsInFile methods should not be used to create a class whose implementation is statically linked with the client program. Instead, the class object should be created using the <className>NewClass procedure provided by the class's .h/.xh header file. Static linkage is not created by including usage bindings in a program, but by use of the offset-resolution method-invocation macros.
Invoking methods without corresponding class usage bindings
This topic builds on the preceding discussion, and illustrates how a client program can apply dynamic SOM mechanisms to utilize classes and objects for which specific usage bindings are not available. This process can be applied when a class implementor did not ship the C/C++ language bindings. Furthermore, the process allows more programming flexibility, because it is not necessary to know the class and method names at compile time in order to access them at run time. (At run time, however, you must be able to provide the method arguments, either explicitly or via a va_list, and provide a generalized way to handle return values.) As an example application, a programmer might create an online class viewer that can access many classes without requiring usage bindings for all those classes, and the person using the viewer can select class names at run time.
As another aspect of flexibility, a code sequence similar to the following C++ example could be re-used to access any class or method. After getting the somId for a class name, the example uses the somFindClass method to create the class object. The somNew method is then invoked to create an instance of the specified class, and the somDispatch method is used to invoke a method on the object.
#include <stdio.h> #include <somcls.xh> int main() { SOMClass *classobj; somId tempId; somId methId; SOMObject *s2; Environment * main_ev = somGetGlobalEnvironment(); tempId = SOM_IdFromString("myClassName"); classobj = SOMClassMgrObject->somFindClass(tempId,0,0); SOMFree(tempId); if (NULL==classobj) { printf("somFindClass could not find the selected class\n"); } else { s2 = (SOMObject *) (classobj->somNew()); methId = somIdFromString("sayHello"); if (s2->somDispatch((somToken *) 0, methId, s2, ev)) printf("Method successfully called.\n"); } return 0; }