The use of SOM to mirror the C++ object

Written by Gordon Zeglinski

Introduction
A few issues back, we looked at encapsulating the profile file API in C++. In this issue, we will use SOM to mirror the C++ object.

Recall the C++ call definition: The above definition will be mirrored using SOM.

The SOM Profile Class Definition
Following is the SOM IDL definition for the profile class and its metaclass. This listing is from the file INIObjs.idl. We'll look at this in detail later. Pretty impressive stuff huh?

The M_INIObj Metaclass
As mentioned in the last issue, the metaclass provides the constructor and class methods. We provide 2 constructors, CreateFromOpen and INICreate. INICreate takes the HAB and the name of the profile file; its implementation follows. In the above snippet, the bold text is the code added to the stubs created by the SOM compiler. The constructor CreateFromOpen is provided to create an instance of a INIObj give a handle of an already open profile file.

The implementation section of the M_INIObj metaclass contains the declaration of two class variables, UserINI and SystemINI. These variables will be used to store an instance of INIObj for the System and User INI files. They are accessed by the class methods GetUserINI and GetUserINI respectively. Below is the implementation for the GetUserINI method. The above snippet illustrates how to access class data using somThis. Now to take care of a few details. Notice that we tested UserINI against 0 to see if we created an instance before. How do we know it will be zero if we previously didn't create an instance of INIObj for it? Also note that we create a new instance of INIObj. Where does it get deleted? For the answer to these two questions, we look at the implementation section of the metaclass again. We see: somInit:override; // just a test for parent call macros somUninit:override; // clean up any instances we make

These methods take care of the initialization and destruction of the metaclass data members. Their implementation follows. That's it for the metaclass. As an exercise for the reader, modify the constructor CreateFromOpen so that it fails to create an instance of INIObj if it is passed a handle to either the system or user INI.

The INIObj Class
Now that we've seen the metaclass, we can start looking at the INIObj class. Going back to the SOM interface definition listing at the start of this section and comparing it to the C++ code, we see that there is nearly a 1 to 1 correlation between the functions. We won't cover what each function does, but merely explore the "highlights". The SOM version includes two additional functions GetStringNoDef and GetIntNoDef. These methods are necessary because in SOM there's no way to set default arguments and each function must be uniquely named. It's not CORBA compliant to use pointers as parameters. Thus, we define the type buffType, typedef sequence buffType;. A sequence is similar to an "array/pointer" in C except it can have a maximum bound. Again, we override the somInit and somUninit to zero the instance variables and to provide clean up. Following is the code for somUninit. The above routine calls the parent somUninit methods and then performs it's clean up. If DidCreate variable is non-zero, then the instance opened the profile file and should close it when destroyed.

Testing The Code
Now that we've seen how to implement the SOM object, let's see how to use it. The following code is the main routine taken from the file MAIN.CPP. Using our C++ based wrapper previously defined, the code would look like: There are only two major differences between the C++ and SOM version (at this level). First, the SOM version uses a metaclass. Therefore, the SOM version needs code to create and delete the metaclass. Second, C++ allows default parameters so the GetString function can be used in several locations.

Wrapping Things Up
This concludes another thrilling episode of SOM exploration. The reader is encouraged to see how the implementation of the C++ members and SOM members differ. In particular, see how the inout parameters in SOM differ from the reference parameters in C++.