home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscClassVariable.m < prev    next >
Encoding:
Text File  |  1994-03-17  |  2.1 KB  |  92 lines

  1. //
  2. //    MiscClassVariable.m -- a class to implement true class variables
  3. //        Written by Mike Ferris (c) 1994 by Mike Ferris.
  4. //        Modified from original MOKit "MOClassVariable" class by Don Yacktman.
  5. //                Version 1.0.  All rights reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the author
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14.  
  15.  
  16. #import "misckit/MiscClassVariable.h"
  17. #import <objc/objc-runtime.h>
  18.  
  19. #define CLASS_VERSION        0
  20. #define CLASS_NAME            "MiscClassVariable"
  21.  
  22. @implementation MiscClassVariable
  23.  
  24. + initialize
  25. // Set the version.
  26. {
  27.     if (self == objc_lookUpClass(CLASS_NAME))  {
  28.         [self setVersion:CLASS_VERSION];
  29.     }
  30.     return self;
  31. }
  32.  
  33. - init
  34. // Just call the DI.
  35. {
  36.     return [self initDoesFreeValues:NO];
  37. }
  38.  
  39. - initDoesFreeValues:(BOOL)flag
  40. // Designated Initializer.
  41. // Allocate the hashtable used to store the values.
  42. {
  43.     [super init];
  44.     ht = [[HashTable allocFromZone:[self zone]] 
  45.                 initKeyDesc:"%" valueDesc:"@"];
  46.     doesFreeValues = flag;
  47.     return self;
  48. }
  49.  
  50. - free
  51. // Free the hashtable (objects too, if we're supposed to).
  52. {
  53.     if (doesFreeValues)  [ht freeObjects];
  54.     [ht free];
  55.     return [super free];
  56. }
  57.  
  58. - setObject:obj forClass:(Class)class
  59. // The old value for the class (if any) is freed if doesFreeValues is YES, 
  60. // otherwise it returns the previous value.  Returns nil if doesFreeValues is
  61. // YES.
  62. {
  63.     NXAtom className = NXUniqueString([class name]);
  64.     id prevVal = nil;
  65.     
  66.     if (obj)  {
  67.         // insert the new value
  68.         prevVal = (id)[ht insertKey:className value:obj];
  69.     }  else  {
  70.         // no new value, just remove the old
  71.         prevVal = (id)[ht valueForKey:className];
  72.         if (prevVal)  [ht removeKey:className];
  73.     }
  74.     
  75.     if (doesFreeValues)  {
  76.         if (prevVal)  [prevVal free];
  77.         return nil;
  78.     }  else  {
  79.         return prevVal;
  80.     }
  81. }
  82.  
  83. - getObjectForClass:(Class)class
  84. // Look up the value for the given class.  Returns nil if no value has 
  85. // been set.
  86. {
  87.     NXAtom className = NXUniqueString([class name]);
  88.     return (id)[ht valueForKey:className];
  89. }
  90.  
  91. @end
  92.