Using C++ Objects in SOM

Written by Gordon Zeglinski

Introduction
Having a large collection of C++ objects, I was motivated to see if and how I could use these objects within a SOM object. To keep things nice and familiar, let's use the profile (INI) file manipulation objects as our test case.

Using C++ Objects in SOM
In the second SOM article, we compared SOM and C++ versions of objects designed to manipulate profile files. In each of these objects we went to the OS/2 API to access the profile file. This time, the SOM object will use the C++ object to manipulate the profile file.

The hardest part of this task is determining how to define things in a manner that SOM will accept. Utilizing structures from other languages in SOM seems somewhat cumbersome to me. Pointer types are not supported by CORBA, which makes sense given the cross platform nature of CORBA. Ideally, we want some way of storing a pointer to the instance of the C++ object within our SOM object. Recall that in the IDL file, when you declare an instance of a SOM object within another object, that instance is accessed through a pointer in the C/C++ bindings. Also in SOM, you can declare an object "type" without defining it's body. What if we declared an object type and then later included C++ headers that defined a C++ object as the formentioned "object type"?

Using this idea, we create the following IDL file. The line interface ProfileFile; declares a "SOM object" with the same name as the "C++ Object" which manipulates the profile files. This object name is then used within the SOM object definition to declare an instance of ProfileFile. The passthru section of the SOM object definition, is used to include the C++ header files that declare the C++ object, ProfileFile. The C++ SOM bindings allow SOM and C++ objects to be intermixed without any major syntax differences. Following is the source code for MAIN.CPP. The new operator is used to create an instance of the SOM object, SOM_CPPini. Thereafter, the -> operator is used to access its member functions. Following is a snippet of code from the file SOMCPP.CPP. Remember that somThis->INIFile is a pointer to an instance of a C++ object. Outside of the new ProfileFile(FileName,0); statement, the instantizing the C++ and SOM objects is the same. This difference is due to the different way SOM and C++ handle constructors. Methods are called using the same syntax. This allows for a fairly consistent style when mixing C++ and SOM objects.

Wrapping Things Up
Although a fairly simple example was presented here, we have seen that C++ and SOM objects can be intermixed within a program and still maintain good readability. C++ objects can be used as data members of SOM objects to provide lower level support for the SOM methods. This type of interoperability allows one to exploit each OOP model for it's unique features. C++ is still faster than SOM, but SOM provides a much richer environment. In future articles, we will look at DSOM and the new 2.1 SOM toolkit.