/********************************************************************
The following demos how to link a C function (DisplayInfo_r) to a
class (person) and then have future instances (john, mary) of the
class invoke the inherited function. Classes and instances are things
in db.
********************************************************************/
void FunctionLinkAndInherit_Demo()
{
// Create person in db
int* pPerson = T_create();
T_relate(&pPerson, rCls, &pThing_g);
T_Name_relate(&pPerson, _T("person"));
// Link a C function (DisplayInfo_r) to person in db
T_Fn_relate(pPerson, DisplayInfo_r, _T("displayInfo"));
// Create age in db
int* pAge = T_create();
T_relate(&pAge, rCls, &pThing_g);
T_Name_relate(&pAge, _T("age"));
// Create color in db
int* pColor = T_create();
T_relate(&pColor, rCls, &pThing_g);
T_Name_relate(&pColor, _T("color"));
// Create john, an instance of person.
// Set his age to 35.
int* pJohn = T_create();
T_relate(&pJohn, rCls, &pPerson);
T_Name_relate(&pJohn, _T("john"));
T_Val_relate(&pJohn, &pAge, _T("35"));
// Create mary, an instance of person.
// Set her age to 25, and color to brown.
int* pMary = T_create();
T_relate(&pMary, rCls, &pPerson);
T_Name_relate(&pMary, _T("mary"));
T_Val_relate(&pMary, &pAge, _T("25"));
T_Val_relate(&pMary, &pColor, _T("brown"));
// Get thing for function displayInfo
int* pFnDispInfo = R_Dest(Str_getDefT(_T("displayInfo")));
// Have john execute function displayInfo
// which he inherits from his class (person).
// Following displays "john is 35." in a dialog box.
T_Fn_execute_r(pJohn, pFnDispInfo);
// Have mary execute function displayInfo
// which she inherits from her class (person).
// Following displays "mary is 25. mary is brown."
T_Fn_execute_r(pMary, pFnDispInfo);
}
/********************************************************************
Displays pT's values. (ie 'john is 35. john is brown.')
If pT has no values, displays pT's name. (ie 'john')
********************************************************************/
ERR_CODE DisplayInfo_r(int* pT)
{
TCHAR sT[kNmSz_g+1] = _T("");
T_Name_get(pT, sT, kNmSz_g);
BOOL hasVals_b = FALSE;
TCHAR sInfo[kNmSz_g+1] = _T("");
int* pX = pT;
while (*(++pX)){
if (rIs == R_Type_get(pX)){
int* pVal = R_Dest(pX);
// Skip names
if (T_AncOf_b(pName_g, pVal)){
continue;
}
hasVals_b = TRUE;
TCHAR sVal[kNmSz_g+1] = _T("");
T_Name_get(pVal, sVal, kNmSz_g);
TCHAR sSent[kNmSz_g+1] = _T("");
_stprintf(sSent, _T("%s is %s.\n"), sT, sVal);
_tcscat(sInfo, sSent);
}
}
if (hasVals_b){
MessageBox(NULL, sInfo, kAppName_sg, MB_OK);
}
else{
MessageBox(NULL, sT, kAppName_sg, MB_OK);
}
return kErrNone_g;
}
To have john execute the inherited function via the GUI (v4.6.3):
1. Select thing/person/john.
2. Ctrl+E (execute).
3. Select thing/function/displayInfo.
4. Ctrl+V (paste).
For more info, see
www.xdb1.com/Example/Ex006.asp