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

  1.  
  2.  
  3. /* OS/2 Developer Magazine, Issue : Spring 93, page : 94-102 */
  4. /* Article title : Method Resolution in SOM                  */
  5. /* Authors : Roger Sessions and Nurcan Coskun                */
  6.  
  7. /* student.csc file: */
  8. include <somobj.sc>
  9.  
  10. class:
  11.   Student;
  12.  
  13. parent:
  14.   SOMObject;
  15.  
  16. data:
  17.   char  id╒16■;      /* student id */
  18.   char  name╒32■;    /* student name */
  19.  
  20. methods:
  21.   override  somInit;
  22.  
  23.   void  setUpStudent(char *id, char *name), name lookup;
  24.   -- sets up a new student.
  25.  
  26.   void  resetStudentId(char *id);
  27.   -- resets student id.
  28.  
  29.   void  resetStudentName(char *name);
  30.   -- resets student name.
  31.  
  32.   void  printStudentInfo();
  33.   -- prints the student information.
  34.  
  35.   char  *getStudentType();
  36.   -- returns the student type.
  37.  
  38.   char  *getStudentId();
  39.   -- returns the student id.
  40.  
  41. --------------------------------------------------------------------------
  42. /* student.c file: */
  43.  
  44. #define Student_Class_Source
  45. #define SOM_NoTest
  46. #include "student.ih"
  47.  
  48. SOM_Scope void   SOMLINK somInit(Student *somSelf)
  49. {
  50.     StudentData *somThis = StudentGetData(somSelf);
  51.     parent_somInit(somSelf);
  52.     _id╒0■ = _name╒0■ = '\0';
  53. }
  54.  
  55. SOM_Scope void   SOMLINK setUpStudent(Student *somSelf,
  56.         char *id, char *name)
  57. {
  58.     StudentData *somThis = StudentGetData(somSelf);
  59.     strcpy(_id, id);
  60.     strcpy(_name, name);
  61. }
  62.  
  63. SOM_Scope void   SOMLINK printStudentInfo(Student *somSelf)
  64. {
  65.     StudentData *somThis = StudentGetData(somSelf);
  66.     printf("\n    Id         : %s \n", _id);
  67.     printf("    Name       : %s \n", _name);
  68.     printf("    Type       : %s \n", _getStudentType(somSelf));
  69. }
  70.  
  71. SOM_Scope char *  SOMLINK getStudentType(Student *somSelf)
  72. {
  73.     StudentData *somThis = StudentGetData(somSelf);
  74.     static char *type = "student";
  75.     return (type);
  76. }
  77.  
  78. SOM_Scope char *  SOMLINK getStudentId(Student *somSelf)
  79. {
  80.     StudentData *somThis = StudentGetData(somSelf);
  81.     return (_id);
  82. }
  83.  
  84. SOM_Scope void   SOMLINK resetStudentId(Student *somSelf,
  85.         char *id)
  86. {
  87.     StudentData *somThis = StudentGetData(somSelf);
  88.     StudentMethodDebug("Student","resetStudentId");
  89.     strcpy(_id, id);
  90. }
  91.  
  92. SOM_Scope void   SOMLINK resetStudentName(Student *somSelf,
  93.         char *name)
  94. {
  95.     StudentData *somThis = StudentGetData(somSelf);
  96.     StudentMethodDebug("Student","resetStudentName");
  97.     strcpy(_name, name);
  98. }
  99. --------------------------------------------------------------------------
  100. /* stdinter.c file: */
  101. /* Student class interpreter code */
  102.  
  103. #include "student.h"
  104. #include <stdio.h>
  105. #define MAX_LENGTH 80
  106.  
  107. static void getLine(s, msg) /* get line into s */
  108. char s╒■;
  109. char *msg;
  110. {
  111.   int i,c;
  112.   printf(msg);
  113.   for (i=0; i<(MAX_LENGTH-1) && (c=getchar()) != EOF && c!= '\n'; ++i)
  114.     s╒i■ = c;
  115.   s╒i■ = '\0';
  116. }
  117.  
  118. main()
  119. {
  120.    int i, argNo;
  121.    char *str, *arg;
  122.    char *argList = NULL;
  123.    char line╒MAX_LENGTH■;
  124.    char method╒MAX_LENGTH■;
  125.    char msg╒MAX_LENGTH■;
  126.    Student *student = StudentNew();
  127.  
  128.    /* display the menu options and read the operation */
  129.      getLine(line, "\n> Menu Options are : 'm(message)' , 'q(quit)'\n$");
  130.      while(line╒0■ == 'm'){
  131.        /* query the message name */
  132.          getLine(method, "> Enter method name:\n$");
  133.        /* query the number of arguments */
  134.          getLine(line, "> Enter the number of arguments:\n$");
  135.          argNo = atoi(line);
  136.        /* query the arguments */
  137.          argList = (char *) malloc((sizeof (char *)) * argNo);
  138.          for(i=0; i < argNo; ++i){
  139.            sprintf(msg, "> Enter the argument╒%d■:\n$", i);
  140.            arg = (char *) malloc(MAX_LENGTH);
  141.            getLine(arg, msg);
  142.            *(char **)(argList + (i * sizeof(char *))) = arg;
  143.          }
  144.        /* query the return type */
  145.          getLine(line, "> Enter the return type:\n$");
  146.        /* send the message to student object */
  147.          if(line╒0■ == 's'){
  148.            str = (char *) SOMObject_somDispatchA(student,
  149.                           somIdFromString(method), "", argList);
  150.            printf("==> Return string from <%s> method is : <%s>.\n",
  151.                                                         method, str);
  152.          }
  153.          else{
  154.            SOMObject_somDispatchV(student, somIdFromString(method),
  155.                                                       "", argList);
  156.          }
  157.        /* free the argList */
  158.          for(i=0; i < argNo; ++i){
  159.            arg = *(char **)(argList + (i * sizeof(char *)));
  160.            free(arg);
  161.          }
  162.          free(argList);
  163.          argList = NULL;
  164.        /* query the next operation */
  165.          getLine(line, "\n> Menu Options are : 'm(message)' , 'q(quit)'\n$");
  166.    }
  167.    _somFree(student);
  168. }
  169. --------------------------------------------------------------------------
  170. /* main.c file: */
  171. /* Demonstration of name lookup resolution */
  172.  
  173. #include "student.h"
  174.  
  175. typedef void (*methodType1) (SOMObject *, char *, char *);
  176. typedef char * (*methodType2) (SOMObject *);
  177.  
  178. /* The following functions can be used to invoke methods
  179.    with a fixed signature */
  180.  
  181. sendMessage1(char *method, SOMObject *target, char *str1, char *str2)
  182. {
  183.     somMethodProc *methodPtr;
  184.     _somFindMethod(_somGetClass(target), somIdFromString(method),
  185.                        &methodPtr);
  186.     ((methodType1)methodPtr)(target,str1, str2);
  187. }
  188.  
  189. char *sendMessage2(char *method, SOMObject *target)
  190. {
  191.     somMethodProc *methodPtr;
  192.     _somFindMethod(_somGetClass(target), somIdFromString(method),
  193.                        &methodPtr);
  194.     return(((methodType2)methodPtr)(target));
  195. }
  196.  
  197. main()
  198. {
  199.    Student *student1 = StudentNew();
  200.    Student *student2 = StudentNew();
  201.    char *id, *type;
  202.  
  203.    /* Offset resolution */
  204.    _setUpStudent(student1, "599600", "David Brown");
  205.    _printStudentInfo(student1);
  206.  
  207.    /* Name lookup resolution */
  208.    sendMessage1("setUpStudent", student2, "120045", "Janet Smith");
  209.    _printStudentInfo(student2);
  210.  
  211.    id = sendMessage2("getStudentId", student2);
  212.    printf("Student Id = %s \n", id);
  213.  
  214.    type = sendMessage2("getStudentType", student2);
  215.    printf("Student Type = %s \n", type);
  216. }
  217. --------------------------------------------------------------------------
  218.  
  219.