home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_02 / 8n02049a < prev    next >
Text File  |  1990-03-01  |  2KB  |  68 lines

  1.  
  2.  
  3. *****Listing 3*****
  4.  
  5. 001  #include <stdlib.h>
  6. 002  #include <stdarg.h>
  7. 003  #include <stdio.h>
  8. 004  #include "utility.h"
  9. 005  #include "obj.h"
  10. 006  
  11. 007  void new_class(CLASS *class, CLASS *super_class,
  12. 008    int nbr_methods, int size)
  13. 009  {
  14. 010      int x;
  15. 011      class->nbr_methods = nbr_methods;
  16. 012      class->size = size;
  17. 013      class->method = 
  18. 014        (void (**)())malloc
  19. 015          ((unsigned)(nbr_methods * sizeof (void (*)())));
  20. 016      for (x = 0; x < nbr_methods; ++x)
  21. 017          class->method[x] = (void *)NULL;
  22. 018      if (super_class != NULL)
  23. 019          for (x = 0; x < super_class->nbr_methods &&
  24. 020            x < class->nbr_methods; ++x)
  25. 021              class->method[x] = super_class->method[x];
  26. 022  }
  27. 023  
  28. 024  void free_class(CLASS *class)
  29. 025  {
  30. 026      free(class->method);
  31. 027  }
  32. 028  
  33. 029  /* register a method with a class */
  34. 030  void reg_method(CLASS *class, int mth, void (*fcn)())
  35. 031  {
  36. 032      if (mth < 0 || mth >= class->nbr_methods)
  37. 033          fatal("attempting to register an invalid method");
  38. 034      class->method[mth] = fcn;
  39. 035  }
  40. 036  
  41. 037  /* initialize an object */
  42. 038  void new_object(OBJECT *obj, CLASS *class)
  43. 039  {
  44. 040      void *v;
  45. 041      obj->class = class;
  46. 042      v = malloc((unsigned)class->size);
  47. 043      if (v == NULL)
  48. 044          fatal("smalloc failed");
  49. 045      obj->data = (void *)((unsigned char *)v);
  50. 046  }
  51. 047  
  52. 048  /* send a message to an object */
  53. 049  void message(OBJECT *obj, int msg, ...)
  54. 050  {
  55. 051      va_list arg_ptr;
  56. 052      va_start(arg_ptr, msg);
  57. 053      if (obj->class->method[msg] == NULL)
  58. 054          fatal("no method for this class");
  59. 055      (*obj->class->method[msg])(obj, arg_ptr);
  60. 056      va_end(arg_ptr);
  61. 057  }
  62. 058  
  63. 059  /* free the data allocated for an object */
  64. 060  void free_object(OBJECT *obj)
  65. 061  {
  66. 062      free(obj->data);
  67. 063  }
  68.