home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lookup.zip / LOOKUP.TXT
Text File  |  1993-05-20  |  10KB  |  365 lines

  1.  
  2. /* OS/2 Developer Magazine, Issue : Spring 93, page : 103-112 */
  3. /* Article title : Using Name Lookup Resolution in SOM       */
  4. /* Author : Nurcan Coskun                                   */
  5.  
  6. /* ---------------------Begin Figure 1------------------------------------ */
  7.  
  8. /* Figure 1 : Class definition and implementation files for <NamedObject1> */
  9.  
  10. /* Class Definition File(namedo1.csc) */
  11.  
  12. include <somobj.sc>
  13.  
  14. class:
  15.   NamedObject1;
  16.  
  17. parent:
  18.   SOMObject;
  19.  
  20. data:
  21.   char name╒32■;
  22.  
  23. methods:
  24.  
  25.   void  setName(char *name);
  26.   char *getName();
  27. /* ----------------------------------------------------------------------- */
  28. /* Class Implementation File(namedo1.c) */
  29.  
  30. #define NamedObject1_Class_Source
  31. #include "namedo1.ih"
  32.  
  33. SOM_Scope void   SOMLINK setName(NamedObject1 *somSelf,
  34.                 char *name)
  35. {
  36.     NamedObject1Data *somThis = NamedObject1GetData(somSelf);
  37.     strcpy(_name, name);
  38. }
  39.  
  40. SOM_Scope char *  SOMLINK getName(NamedObject1 *somSelf)
  41. {
  42.     NamedObject1Data *somThis = NamedObject1GetData(somSelf);
  43.     return _name;
  44. }
  45.  
  46. /* -------------------End Figure 1---------------------------------------- */
  47.  
  48. /* -------------------Begin Figure 2-------------------------------------- */
  49.  
  50. /* Figure 2 :Class definition and implementation files for <FormatObject1> */
  51.  
  52. /* Class Definition File(formato1.csc) */
  53.  
  54. include <somobj.sc>
  55.  
  56. class:
  57.   FormatObject1;
  58.  
  59. parent:
  60.   SOMObject;
  61.  
  62. methods:
  63.  
  64.   void formatName(char *text, char *buffer);
  65.   -- Formats an ascii data stream.
  66.  
  67. /* ----------------------------------------------------------------------- */
  68.  
  69. /* Class Implementation File(formato1.c) */
  70.  
  71. #define FormatObject1_Class_Source
  72. #include "formato1.ih"
  73. #include <ctype.h>
  74.  
  75. SOM_Scope void   SOMLINK formatName(FormatObject1 *somSelf,
  76.                 char *text, char *buffer)
  77. {
  78.     int i;
  79.     /* Convert the text to upper case */
  80.     for(i=0; i<strlen(text); i++){
  81.       buffer╒i■ = toupper(text╒i■);
  82.     }
  83.     buffer╒i■ = '\0';
  84. }
  85.  
  86. /* -----------------------End Figure 2------------------------------------ */
  87.  
  88. /* -----------------------Begin Figure 3 --------------------------------- */
  89.  
  90. /* Figure 3 : Class definition and implementation files for <PrintObject> */
  91.  
  92. /* Class Definition File(printo.csc) */
  93.  
  94. include <somobj.sc>
  95.  
  96. class:
  97.   PrintObject;
  98.  
  99. parent:
  100.   SOMObject;
  101.  
  102. passthru: C.h, after;
  103.   #include "formato1.h"
  104.   #include "namedo1.h"
  105. endpassthru;
  106.  
  107. data:
  108.   FormatObject1 *formatter;
  109.  
  110. methods:
  111.  
  112.   override somInit;
  113.   void     printData(NamedObject1 *object);
  114.  
  115. /* ---------------------------------------------------------------- */
  116.  
  117. /* Class Implementation File(printo.c) */
  118.  
  119. #define PrintObject_Class_Source
  120. #include "printo.ih"
  121.  
  122. SOM_Scope void   SOMLINK somInit(PrintObject *somSelf)
  123. {
  124.     PrintObjectData *somThis = PrintObjectGetData(somSelf);
  125.     _formatter = FormatObject1New();
  126.     parent_somInit(somSelf);
  127. }
  128.  
  129. SOM_Scope void   SOMLINK printData(PrintObject *somSelf,
  130.                 NamedObject1 *object)
  131. {
  132.     PrintObjectData *somThis = PrintObjectGetData(somSelf);
  133.     char *data;
  134.     char  buffer╒32■;
  135.     data = _getName(object);
  136.     _formatName(_formatter, data, buffer);
  137.     printf("Formatted form for <%s> is <%s>\n", data, buffer);
  138. }
  139.  
  140.  
  141. /* ------------------End Figure 3---------------------------------------- */
  142.  
  143. /* ----------------Begin Figure 4---------------------------------------- */
  144.  
  145. /* Figure 4 :Class def. and imp. files for <PrintObjectImproved> */
  146.  
  147. /* Class Definition File(printoi.csc) */
  148.  
  149. include <somobj.sc>
  150.  
  151. class:
  152.   PrintObjectImproved;
  153.  
  154. parent:
  155.   SOMObject;
  156.  
  157. data:
  158.   SOMObject *formatter;
  159.  
  160. methods:
  161.  
  162.   override   somInit;
  163.  
  164.   void       setFormatter(SOMObject *formatter);
  165.   -- Parameter 'formatter' must support a method with
  166.   -- this signature:
  167.   --   typedef void formatDataTD (SOMObject *, char *, char *);
  168.  
  169.   SOMObject *getFormatter();
  170.  
  171.   void       printObjectData(SOMObject *object,
  172.                        char *getDataMethodName, char *formatDataMethodName);
  173.   -- Parameter 'object' must support a method with this
  174.   -- signature:
  175.   --   typedef char * getDataTD (SOMObject *);
  176.  
  177. /* ----------------------------------------------------------------------- */
  178.  
  179. /* Class Implementation File(printoi.c) */
  180.  
  181. #define PrintObjectImproved_Class_Source
  182. #include "printoi.ih"
  183.  
  184. typedef char * getDataTD (SOMObject *);
  185. typedef void   formatDataTD (SOMObject *, char *, char *);
  186.  
  187. SOM_Scope void   SOMLINK somInit(PrintObjectImproved *somSelf)
  188. {
  189.     PrintObjectImprovedData *somThis = PrintObjectImprovedGetData(somSelf);
  190.     _formatter = NULL;
  191.     parent_somInit(somSelf);
  192. }
  193.  
  194. SOM_Scope void   SOMLINK setFormatter(PrintObjectImproved *somSelf,
  195.                 SOMObject *formatter)
  196. {
  197.     PrintObjectImprovedData *somThis = PrintObjectImprovedGetData(somSelf);
  198.     _formatter = formatter;
  199. }
  200.  
  201. SOM_Scope SOMObject *  SOMLINK getFormatter(PrintObjectImproved *somSelf)
  202. {
  203.     PrintObjectImprovedData *somThis = PrintObjectImprovedGetData(somSelf);
  204.     return (_formatter);
  205. }
  206.  
  207. SOM_Scope void   SOMLINK printObjectData(PrintObjectImproved *somSelf,
  208.                 SOMObject *object,
  209.                 char *getDataMethodName,
  210.                 char *formatDataMethodName)
  211. {
  212.     PrintObjectImprovedData *somThis = PrintObjectImprovedGetData(somSelf);
  213.     char *data;
  214.     char  buffer╒32■;
  215.     somId methodId;
  216.     somMethodProc *m;
  217.  
  218.     methodId = somIdFromString(getDataMethodName);
  219.     _somFindMethodOk(SOM_GetClass(object), methodId, &m);
  220.     data = ((getDataTD *)m) (object);
  221.  
  222.     methodId = somIdFromString(formatDataMethodName);
  223.     _somFindMethodOk(SOM_GetClass(_formatter), methodId, &m);
  224.     ((formatDataTD *)m) (_formatter, data, buffer);
  225.  
  226.     printf("Formatted form for <%s> is <%s>\n", data, buffer);
  227.  
  228. }
  229. /* ------------------End Figure 4----------------------------------------- */
  230.  
  231. /* -----------------Begin Figure 5---------------------------------------- */
  232.  
  233. /* Figure 5 : Class definition and implementation files for <NamedObject2> */
  234.  
  235. /* Class Definition File(namedo2.csc) */
  236.  
  237. include <somobj.sc>
  238.  
  239. class:
  240.   NamedObject2;
  241.  
  242. parent:
  243.   SOMObject;
  244.  
  245. data:
  246.   char objectData╒32■;
  247.  
  248. methods:
  249.  
  250.   void  setObjectData(char *objectData);
  251.   char *getObjectData();
  252.  
  253. /* ----------------------------------------------------------------------- */
  254.  
  255. /* Class Implementation File(namedo2.c) */
  256.  
  257. #define NamedObject2_Class_Source
  258. #include "namedo2.ih"
  259.  
  260. SOM_Scope void   SOMLINK setObjectData(NamedObject2 *somSelf,
  261.                 char *objectData)
  262. {
  263.     NamedObject2Data *somThis = NamedObject2GetData(somSelf);
  264.     strcpy(_objectData, objectData);
  265. }
  266.  
  267. SOM_Scope char *  SOMLINK getObjectData(NamedObject2 *somSelf)
  268. {
  269.     NamedObject2Data *somThis = NamedObject2GetData(somSelf);
  270.     return (_objectData);
  271. }
  272.  
  273. /* --------------------End Figure 5--------------------------------------- */
  274.  
  275. /* ------------------Begin Figure 6---------------------------------------- */
  276.  
  277. Figure 6 : Class definition and implementation files for <FormatObject2>
  278.  
  279. /* Class Definition File(formato2.csc) */
  280.  
  281. include <somobj.sc>
  282.  
  283. class:
  284.   FormatObject2;
  285.  
  286. parent:
  287.   SOMObject;
  288.  
  289. methods:
  290.  
  291.   void formatObjectData(char *text, char *buffer);
  292.   -- Format ascii data stream.
  293.  
  294. /* -------------------------------------------------------------------- */
  295.  
  296. /* Class Implementation File(formato2.c) */
  297.  
  298. #define FormatObject2_Class_Source
  299. #include "formato2.ih"
  300. #include <ctype.h>
  301.  
  302. SOM_Scope void   SOMLINK formatObjectData(FormatObject2 *somSelf,
  303.                 char *text, char *buffer)
  304. {
  305.     int i;
  306.     /* Convert the text to lower case */
  307.     for(i=0; i<strlen(text); i++){
  308.       buffer╒i■ = tolower(text╒i■);
  309.     }
  310.     buffer╒i■ = '\0';
  311. }
  312.  
  313. /* --------------------End Figure 6--------------------------------------- */
  314.  
  315. /* --------------------Begin Figure 7------------------------------------- */
  316.  
  317. /* Figure 7 : Client code and output. */
  318.  
  319. Main  Program:
  320. --------------
  321.  
  322. #include "namedo1.h"
  323. #include "formato1.h"
  324. #include "printo.h"
  325. #include "printoi.h"
  326. #include "namedo2.h"
  327. #include "formato2.h"
  328.  
  329. void main(){
  330.   NamedObject1 *namedo1 = NamedObject1New();
  331.   FormatObject1 *formato1 = FormatObject1New();
  332.   PrintObject *printo = PrintObjectNew();
  333.   PrintObjectImproved *printoi = PrintObjectImprovedNew();
  334.   NamedObject2 *namedo2 = NamedObject2New();
  335.   FormatObject2 *formato2 = FormatObject2New();
  336.  
  337.   /* <PrintObject> objects can only be used with */
  338.   /* <NamedObject1> and <FormatObject1> objects  */
  339.   _setName(namedo1, "Object Data One");
  340.   _printData(printo, namedo1);
  341.  
  342.   /* use <PrintObjectImproved> objects with      */
  343.   /* <NamedObject1> and <FormatObject1> objects  */
  344.   _setName(namedo1, "Object Data Two");
  345.   _setFormatter(printoi, formato1);
  346.   _printObjectData(printoi, namedo1, "getName", "formatName");
  347.  
  348.   /* use <PrintObjectImproved> objects with      */
  349.   /* <NamedObject2> and <FormatObject2> objects  */
  350.   _setObjectData(namedo2, "Object Data Three");
  351.   _setFormatter(printoi, formato2);
  352.   _printObjectData(printoi, namedo2, "getObjectData", "formatObjectData");
  353.  
  354. }
  355.  
  356. Output:
  357. -------
  358. Formatted form for <Object Data One> is <OBJECT DATA ONE>
  359. Formatted form for <Object Data Two> is <OBJECT DATA TWO>
  360. Formatted form for <Object Data Three> is <object data three>
  361.  
  362. /* ------------------End Figure 7--------------------------------------- */
  363.  
  364.  
  365.