home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Palettes / TTools / TToolsPalette / Utilities.subproj / ClassAdditions.m < prev    next >
Encoding:
Text File  |  1993-11-09  |  1.8 KB  |  81 lines

  1. /* ClassAdditions.m
  2.  * Written By:  Thomas Burkholder
  3.  *
  4.  * You may freely copy, distribute, and reuse the code in this example.
  5.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  6.  * fitness for any particular use.
  7.  */
  8.  
  9. #import "ClassAdditions.h"
  10. #import "StorageAdditions.h"  // category that adds addElementIfAbsent:
  11. #import <objc/objc-class.h>
  12. #import <objc/objc-runtime.h>
  13. #import <objc/List.h>
  14. #import <objc/Storage.h>
  15. #import <string.h>
  16.  
  17. @implementation Object (ClassAdditions)
  18.  
  19. + (BOOL)isPoser
  20. {
  21.     return ([self name][0] == '%');
  22. }
  23.  
  24. + realSuperclass
  25. {
  26.     id o;
  27.     // Superclass that isn't a poser
  28.     for(o=[self superclass];(o && [o isPoser]);o = [o superclass]);
  29.     return o;
  30. }
  31.  
  32. + (int)subclasses:(id)list allowPosers:(BOOL)yn
  33.                             filterWith:(CAObjectFilterFunc)aFunc
  34. {
  35.     NXHashTable *p;
  36.     NXHashState state;
  37.     void *value;
  38.     int count = 0;
  39.  
  40.     p = objc_getClasses();
  41.     state = NXInitHashState(p);
  42.  
  43.     while (NXNextHashState(p,&state,&value)) {
  44.         if (((!yn && (([(id)value realSuperclass]==[self class])&&
  45.                 (![(id)value isPoser])) ) ||
  46.                 ((yn) && ([(id)value superclass] == self))) && 
  47.                 (!aFunc || aFunc((id)value,self))) {
  48.                     count++;
  49.                     if (list)
  50.                         [list addObject:(id)value];
  51.         }
  52.     }
  53.     return count;
  54. }
  55.  
  56. + (int)methodSelectors:(id)storage includeAncestors:(BOOL)yn
  57.                         filterWith:(CAElementFilterFunc)aFunc;
  58. {
  59.     struct objc_method_list *methods;
  60.     SEL selector;
  61.     BOOL c;
  62.     int i;
  63.     int total = 0;
  64.     struct objc_class *s;
  65.     
  66.     [storage empty];
  67.     for(s=self,c=YES;(c && s);s = [s superclass],c=yn) {
  68.         for(methods = s->methods;methods;methods=methods->method_next) {
  69.             for(i=0;(i<methods->method_count);i++) {
  70.                 selector = (SEL)(methods->method_list[i].method_name);
  71.                 if (aFunc(self, selector)) {
  72.                     [storage addElementIfAbsent:&selector];
  73.                     total++;
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     return total;
  79. }
  80.  
  81. @end