home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12118a < prev    next >
Text File  |  1991-10-21  |  2KB  |  76 lines

  1. /* LISTING 10 - CIRCLE.C */
  2.  
  3. /*  CIRCLE PACKAGE - contains CIRCLE typedef,
  4.        private data and action package
  5.        definitions  */
  6. #include <stdlib.h>
  7.  
  8.  
  9. /* CIRCLE type has only two elements */
  10. typedef struct circle 
  11.    {
  12.    /* void * pointer to private data */
  13.    void *pprivate;  
  14.  
  15.    /* pcact is used as a pointer to an
  16.          array of action functions */
  17.    int (**pcact)();
  18.    } CIRCLE;
  19.  
  20.  
  21. /* private data struct is static */
  22. static struct circle_pri_data 
  23.    {
  24.    int color;
  25.    };
  26.  
  27.  
  28. /* get and setcolor member functions */
  29. static int cir_get_color(CIRCLE *pc)
  30.    {
  31.    return
  32.        ((struct circle_pri_data *)
  33.          (pc->pprivate))->color;
  34.    }
  35.  
  36. static int cir_set_color(CIRCLE *pc, int col)
  37.    {
  38.    ((struct circle_pri_data *)
  39.          (pc->pprivate))->color = col;
  40.    }
  41.  
  42.  
  43. /* now define and initialize action package */
  44. static (*cact[])() =
  45.    { circle_getcolor, circle_setcolor };
  46.  
  47.  
  48. /* the constructor is public, callable from
  49.       another file the constructor creates
  50.       space for the private data, and inits
  51.       the new circle's action pointer to the
  52.       action package */
  53. constructor(CIRCLE *pc, int color)
  54.    {
  55.    /* first make space for the private data */    
  56.    if (!(pc->pprivate =
  57.       malloc(sizeof(struct circle_pri_data))))
  58.          exit(-1);
  59.  
  60.    /* cast pprivate to point to private data
  61.       to set color */
  62.    ((struct circle_pri_data *)
  63.       (pc->pprivate))->color = color;
  64.  
  65.    /* finally, hook up the new circle to the
  66.       action package */
  67.    pc->pact = cact;
  68.    }
  69.  
  70. /* the public destructor frees the allocated
  71.       private storage */
  72. destructor(CIRCLE *pc)   {
  73.    free(pc->pprivate);
  74.    }
  75.  
  76.