The basic rules for invoking methods and accessing object data can be summarized as follows:
These rules can be illustrated by introducing three new methods for the Dog class that relate to more dog characteristics and getting and setting behaviors. Because dogs can be characterized by their breed, a user should be able to get and set the breed for a dog. It also would be desirable to display the characteristics of a dog. In the following sample code, the action of getting and setting a dog's breed corresponds to the prototype for the getBreed and setBreed methods in the DOG.IDL file.
/* * A class defining a set of objects (dogs) */ #ifndef dog4_idl #define dog4_idl #include <somobj.idl> interface Dog : SOMObject { attribute string breed; // The breed for the dog void display(); // Display the characteristics for this dog. // Show the breed and make the dog speak. void bark(); // Have the dog bark #ifdef __SOMIDL__ implementation { releaseorder: _get_breed, _set_breed, display, bark; //# Class modifiers callstyle = oidl; // For compatibility with SOM1 majorversion = 1; minorversion = 2; }; #endif /* __SOMIDL__ */ }; #endif /* dog4_idl */
The action of displaying a dog's characteristics corresponds to the prototype for the display method. This example also introduces the use of an attribute. Each dog has its own copy of the attribute associated with it. The breed of a dog is stored in a string called "breed". By default, SOM provides a _get_breed method to return the contents of the breed and a _set_breed method to set the contents of breed. If no set method was used, breed should be defined as "readonly attribute" string breed.
From the new DOG.IDL file, the SOM compiler generates a new C-language source-program template, as shown in the following sample code. This template is used to complete the implementation of the new Dog class.
/* * This file was generated by the SOM compiler and Emitter Framework. * Generated using: SOM Emitter emitctm: 2.38 */ #ifndef SOM_Module_dog_Source #define SOM_Module_dog_Source #endif #define Dog_Class_Source #include "dog.ih" #include <stdio.h> /* Needed because of printf */ /* * Display the characteristics for this dog. * Show the breed and make the dog speak. */ SOM_Scope void SOMLINK display(Dog *somSelf) { DogData *somThis = DogGetData(somSelf); DogMethodDebug("Dog","display"); /* Note that there are two underscores before */ /* the get_breed method */ printf("\nMy breed is %s.\n", __get_breed(somSelf)); printf("I say:\n"); /* Prefix method name with either an underscore */ /* ( _ ) or somThis */ _bark(somSelf); } /* * Have the dog bark */ SOM_Scope void SOMLINK bark(Dog *somSelf) { DogData *somThis = DogGetData(somSelf); DogMethodDebug("Dog","bark"); printf("Unknown dog noise\n"); /* Speak! */ }
The first thing that is noticeable in this sample code is that the attribute declarations are not present. They have been placed in the class-implementation file (DOG.IH) by the SOM compiler. As before, DOG.IH contains all the definitions of the Dog class data, macros, and functions.
The _set_breed and _get_breed methods, also in the DOG.IH file, operate on the string "breed" and refer to it by prefixing the string name with somThis.
The display method requires the dog's characteristics, that is, its breed and its bark, to be displayed. To do this, the display method calls both the _get_breed and bark methods, which are referenced by prefixing the method name with the underscore ( _ ) character.
Finally, as in the previous implementation of the Dog class, the Include file, STDIO.H, is added to the source program to resolve the reference to the C-library routine, printf, which is used to display the dog's characteristics. The SOM function, somPrintf, can be used in place of printf; no additional Include file is required in this case. In fact, it is recommended that SOM class implementations use the SOM functions, where appropriate, instead of the C-library functions. SOM functions offer flexibility and replaceability.