home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / geom / extend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  3.4 KB  |  142 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include "string.h"
  13. #include "geomclass.h"
  14. #include <stdarg.h>
  15.  
  16. static struct extmethods {
  17.     char *methodname;
  18.     GeomExtFunc *defaultfunc;
  19. } *extmethods = NULL;
  20.  
  21. static int n_extmethods = 1, max_extmethods = 0;
  22.  
  23. int
  24. GeomMethodSel(char *methodname)
  25. {
  26.     register struct extmethods *m;
  27.     register int i;
  28.  
  29.     for(i = 1; i < n_extmethods; i++)
  30.       if((m = &extmethods[i])->methodname != NULL &&
  31.         strcmp(m->methodname, methodname) == 0)
  32.     return i;
  33.     return 0;
  34. }
  35.  
  36. int
  37. GeomNewMethod(char *methodname, GeomExtFunc *defaultfunc)
  38. {
  39.     register struct extmethod *m;
  40.     register int sel;
  41.     int oldmax = max_extmethods;
  42.  
  43.     sel = GeomMethodSel(methodname);
  44.     if(sel > 0)
  45.     return 0;
  46.     sel = n_extmethods++;
  47.     if(sel >= oldmax) {
  48.     extmethods = (oldmax == 0)
  49.         ? OOGLNewNE(struct extmethods, (max_extmethods = 7),
  50.         "Extension methods")
  51.         : OOGLRenewNE(struct extmethods, extmethods, (max_extmethods *= 2),
  52.         "Extension methods");
  53.     bzero(&extmethods[oldmax],
  54.         (max_extmethods - oldmax) * sizeof(struct extmethods));
  55.     }
  56.     extmethods[sel].defaultfunc = defaultfunc;
  57.     extmethods[sel].methodname = strdup(methodname);
  58.     return sel;
  59. }
  60.  
  61. char *
  62. GeomMethodName(int sel)
  63. {
  64.     struct extmethods *m;
  65.  
  66.     return (sel <= 0 || sel >= n_extmethods)
  67.     ? NULL : extmethods[sel].methodname;
  68. }
  69.  
  70. GeomExtFunc *
  71. GeomSpecifyMethod( int sel, GeomClass *Class, GeomExtFunc *func )
  72. {
  73.     int oldmax;
  74.     int need;
  75.     GeomExtFunc *oldfunc;
  76.  
  77.     if(Class == NULL || sel <= 0 || sel >= n_extmethods)
  78.     return NULL;
  79.  
  80.     oldmax = Class->n_extensions;
  81.     if(sel >= oldmax) {
  82.     need = (oldmax == 0) ? 7 : oldmax*2;
  83.     if(need <= sel) need = sel+1;
  84.     Class->extensions = (oldmax == 0)
  85.         ? OOGLNewNE(GeomExtFunc *,
  86.             need, "Extension func vector")
  87.         : OOGLRenewNE(GeomExtFunc *, Class->extensions,
  88.             need, "Extension func vector"); 
  89.     Class->n_extensions = need;
  90.     bzero(&Class->extensions[oldmax],
  91.         (need - oldmax) * sizeof(GeomExtFunc *));
  92.     }
  93.     oldfunc = Class->extensions[sel];
  94.     Class->extensions[sel] = func;
  95.     return oldfunc;
  96. }
  97.  
  98. void *
  99. GeomCall(int sel, Geom *geom, ...)
  100. {
  101.     register GeomClass *C;
  102.     register GeomExtFunc *ext = NULL;
  103.     void *result = NULL;
  104.     va_list args;
  105.  
  106.     if(geom == NULL || sel <= 0 || sel >= n_extmethods)
  107.     return NULL;
  108.  
  109.     C = geom->Class;
  110.     while(sel >= C->n_extensions || (ext = C->extensions[sel]) == NULL) {
  111.     if((C = C->super) == NULL) {
  112.         ext = extmethods[sel].defaultfunc;
  113.         break;
  114.     }
  115.     }
  116.     if(ext) {
  117.     va_start(args, geom);
  118.     result = (*ext)(sel, geom, args);
  119.     va_end(args);
  120.     }
  121.     return result;
  122. }
  123.  
  124. void *
  125. GeomCallV(int sel, Geom *geom, va_list args)
  126. {
  127.     register GeomClass *C;
  128.     register GeomExtFunc *ext = NULL;
  129.  
  130.     if(geom == NULL || sel <= 0 || sel >= n_extmethods)
  131.     return NULL;
  132.  
  133.     C = geom->Class;
  134.     while(sel >= C->n_extensions || (ext = C->extensions[sel]) == NULL) {
  135.     if((C = C->super) == NULL) {
  136.         ext = extmethods[sel].defaultfunc;
  137.         break;
  138.     }
  139.     }
  140.     return ext ? (*ext)(sel, geom, args) : NULL;
  141. }
  142.