1-136 SOMobjects Base Toolkit: Programmer’s Reference Manual
C Example
Given class Key that has an attribute keyval of type long and an overridden method for
somPrintSelf that prints the value of the attribute (as well as the information printed by
SOMObject’s implementation of somPrintSelf), the following client code invokes methods
on Key objects using somDispatch and somClassDispatch. (The Key class was defined
with the callstyle=oidl class modifier, so the Environment argument is not required of its
methods.)
#include <key.h>
main()
{
SOMObject obj;
long k1 = 7, k2;
Key myKey = KeyNew();
va_list push, args = SOMMalloc(8);
somId setId = somIdFromString(”_set_keyval”);
somId getId = somIdFromString(”_get_keyval”);
somId prtId = somIdFromString(”_somPrintSelf”);
/* va_list invocation of setkey and getkey : */
push = args;
va_arg(push, SOMObject) = myKey;
va_arg(push, long) = k1;
SOMObject_somDispatch(myKey,(somToken*)0,setId,args);
push = args;
va_arg(push, SOMObject) = myKey;
SOMObject_somDispatch(myKey,(somToken*)&k2,getId,args);
printf(”va_list _set_keyval and _get_keyval: %i\n”, k2);
/* varargs invocation of setkey and getkey : */
_somDispatch(myKey, (somToken*)0, setId, myKey, k1);
_somDispatch(myKey, (somToken*)&k2, getId, myKey);
printf(”varargs _set_keyval and _get_keyval: %i\n”, k2);
/* illustrate somclassDispatch ”casting” (use varargs form) */
printf(”somPrintSelf on myKey as a Key:\n”);
_somClassDispatch(myKey,_Key,(somToken*)&obj2,prtId,myKey,0);
printf(”somPrintSelf on myKey as a SOMObject:\n”);
_somClassDispatch(myKey,_SOMObject,(somToken*)&obj,prtId,myKey,0)
;
SOMFree(args); SOMFree(setId); SOMFree(getId); SOMFree(prtId);
_somFree(myKey);
}
This program produces the following output:
va_list _set_keyval and _get_keyval: 7
varargs _set_keyval and _get_keyval: 7
somPrintSelf on myKey as a Key:
{An instance of class Key at address 2005B2F8}
–– with key value 7
somPrintSelf on myKey as a SOMObject:
{An instance of class Key at address 2005B2F8}
Original Class
SOMObject
Related Information
Functions: somApply
Kommentare zu diesen Handbüchern