Re: Nearest Common Ancestor Report (XDb1's $1000 Challenge)

From: Neo <neo55592_at_hotmail.com>
Date: 11 Jun 2004 12:34:32 -0700
Message-ID: <4b45d3ad.0406111134.4fb54ab2_at_posting.google.com>


> imagine, user creates function and associates it with the class.

/********************************************************************
The following demos how to link a C function (DisplayInfo) to a class (person) and then have future instances (ie john, mary) of the class invoke the inherited function:
********************************************************************/
void FunctionInheritance_Demo()
{

// Create person

      int* pPerson = T_create();
      T_relate(&pPerson, rCls, &pThing_g);
      T_Name_relate(&pPerson, _T("person"));

// Link a C function (DisplayInfo) to person in db.
// Function DisplayInfo is shown below

      T_Fn_relate(pPerson, _T("displayInfo"), DisplayInfo);

// Create age

      int* pAge = T_create();
      T_relate(&pAge, rCls, &pThing_g);
      T_Name_relate(&pAge, _T("age"));


// Create color
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(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(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(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).
Received on Fri Jun 11 2004 - 21:34:32 CEST

Original text of this message