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();
somVaBuf vb;
va_list push, args;
somId setId = somIdFromString("_set_keyval");
somId getId = somIdFromString("_get_keyval");
somId prtId = somIdFromString("somPrintSelf");
vb = (somVaBuf)somVaBuf_create(NULL, 0);
somVaBuf_add(vb, (char *)&myKey, tk_ulong);
somVaBuf_add(vb, (char *)&k1, tk_long);
somVaBuf_get_valist(vb, &args);
/* va_list invocation of setkey and getkey */
SOMObject_somDispatch(myKey, (somToken *)0, setId, args);
somVaBuf_get_valist(vb, &args);
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 *)&obj, prtId, myKey, 0);
printf("somPrintSelf on myKey as a SOMObject:\n");
_somClassDispatch(myKey, _SOMObject, (somToken *)&obj, prtId, myKey, 0);
SOMFree(setId);
SOMFree(getId);
SOMFree(prtId);
_somFree(myKey);
somVaBuf_destroy(vb);
}
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}