home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gcc-2.3.3-src.lha / src / amiga / gcc-2.3.3 / objc / object.m < prev    next >
Encoding:
Text File  |  1994-02-07  |  5.0 KB  |  287 lines

  1. /* This file contains the implementation of class Object.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "tconfig.h"
  22. #include "gstdarg.h"
  23. #include "object.h"
  24. #include "objc-proto.h"
  25.  
  26. #include <errno.h>
  27. #ifndef errno
  28. extern int errno;
  29. #endif
  30.  
  31. #define CLASS(class)  ((Class_t)class)
  32.  
  33.  
  34. @implementation Object
  35.  
  36.  
  37. + new
  38. {
  39.   return class_createInstance (CLASS (self));
  40. }
  41.  
  42.  
  43. + free {  return nil; }
  44. - free {  return object_dispose (self); }
  45.  
  46.  
  47. - copy {  return [self shallowCopy]; }
  48.  
  49.  
  50. - shallowCopy
  51. {
  52.   return object_copy (self);
  53. }
  54.  
  55.  
  56. - deepCopy
  57. {
  58.   return class_createInstance ([self class]);
  59. }
  60.  
  61.  
  62. + (Class_t)class      { return CLASS (self); }
  63. + (Class_t)superClass { return CLASS (self)->super_class; }
  64. - (Class_t)class      { return isa; }
  65. - (Class_t)superClass { return isa->super_class; }
  66. - (const char *)name  { return object_getClassName (self); }
  67. - self                { return self; }
  68.  
  69.  
  70. - (unsigned int)hash
  71. {
  72.   return (unsigned int)self; /* gak!  Not portable. */
  73. }
  74.  
  75.  
  76. - (BOOL)isEqual:anObject
  77. {
  78.   return self == anObject ;
  79. }
  80.  
  81.  
  82. - (BOOL)isKindOf:(Class_t)aClassObject
  83. {
  84.   Class_t class;
  85.  
  86.   for (class = isa; class; class = class->super_class)
  87.     if (class == aClassObject)
  88.       return YES;
  89.  
  90.   return NO;
  91. }
  92.  
  93.  
  94. - (BOOL)isMemberOf:(Class_t)aClassObject
  95. {
  96.   return isa == aClassObject ;
  97. }
  98.  
  99.  
  100. - (BOOL)isKindOfGivenName:(const char*)aClassName
  101. {
  102.   Class_t   class;
  103.  
  104.   for (class = isa; class; class = class->super_class)
  105.     if (!strcmp (class_getClassName (class), aClassName))
  106.       return YES;
  107.  
  108.   return NO;
  109. }
  110.  
  111.  
  112. - (BOOL)isMemberOfGivenName:(const char*)aClassName
  113. {
  114.   return !strcmp ([self name], aClassName);
  115. }
  116.  
  117.  
  118. + (BOOL)instancesRespondTo:(SEL)aSel
  119. {
  120.   if (class_getInstanceMethod (CLASS (self), aSel))
  121.     return YES;
  122.   
  123.   return NO;
  124. }
  125.  
  126.  
  127. - (BOOL)respondsTo:(SEL)aSel
  128. {
  129.   if (class_getInstanceMethod (isa, aSel))
  130.     return YES;
  131.  
  132.   return NO;
  133. }
  134.  
  135.  
  136. - perform:(SEL)aSel
  137. {
  138.   return (*((IMP)objc_msgSend (self, aSel)))(self, aSel);
  139. }
  140.  
  141.  
  142. - perform:(SEL)aSel with:aObject
  143. {
  144.   return (*((IMP)objc_msgSend (self, aSel)))(self, aSel, aObject);
  145. }
  146.  
  147.  
  148. + poseAs:(Class_t)aClassObject
  149. {
  150.   return class_poseAs (self, aClassObject);
  151. }
  152.  
  153.  
  154. - subclassResponsibility:(SEL)aSel
  155. {
  156.   return [self error:"subclass should override %s", aSel];
  157. }
  158.  
  159.  
  160. - notImplemented:(SEL)aSel
  161. {
  162.   return [self error:"method %s not implemented", aSel];
  163. }
  164.  
  165.  
  166. - doesNotRecognize:(SEL)aSel
  167. {
  168.   return [self error:"%s does not recognize %s", [self name], aSel];
  169. }
  170.  
  171. - error:(const char*)aString, ...
  172. {
  173. #define FMT "error: %s (instance)\n%s\n"
  174.  
  175.   char  fmt[strlen (FMT)
  176.         + strlen (isa->name)
  177.         + strlen (aString) + 8];
  178.   va_list ap;
  179.   
  180.   sprintf (fmt, FMT, isa->name, aString);
  181.   va_start (ap, aString);
  182.   (*_error)(self, fmt, ap);
  183.   va_end (ap);
  184.  
  185. #undef FMT
  186.   return self;
  187. }
  188.  
  189.  
  190. + error:(const char*)aString, ...
  191. {
  192. #define FMT "error: %s (class)\n%s\n"
  193.  
  194.   char  fmt[strlen (FMT)
  195.         + strlen (isa->name)
  196.         + strlen (aString) + 8];
  197.   va_list ap;
  198.  
  199.   sprintf (fmt, FMT, isa->name, aString);
  200.   va_start (ap, aString);
  201.   (*_error)(self, fmt, ap);
  202.   va_end (ap);
  203.  
  204. #undef FMT
  205.   return self;
  206. }
  207.  
  208.  
  209. - storeOn:(int)aFd
  210. {
  211.   int   len;
  212.   long  version = [[self class] version];
  213.   
  214.   if ((len = write (aFd, "#", strlen ("#"))) != -1)
  215.     if ((len = write (aFd, [self name], strlen ([self name]) + 1)) != -1)
  216.       len = write (aFd, &version, sizeof (version));
  217.   
  218.   if (len == -1)
  219.     [self error:"error passivating object, errno=%d", errno];
  220.  
  221.   return self;
  222. }
  223.  
  224.  
  225. + readFrom:(int)aFd
  226. {
  227.   id    aObj = nil;
  228.   char  objName[256];
  229.   int   len;
  230.   
  231.   
  232.   if ((len = read (aFd, &objName, strlen ("#"))) != -1)
  233.     if (objName[0] == '#') {
  234.       long  version;
  235.       int   i = 0;
  236.       
  237.       /* Read the object's class. */
  238.       do {
  239.         len = read (aFd, &objName[i], sizeof (char));
  240.       } while (objName[i++] && (len != -1));
  241.     
  242.       /* Get its version. */
  243.       if (len != -1)
  244.         len = read (aFd, &version, sizeof (version));
  245.       
  246.       /* No errors???
  247.      Then create a object. */
  248.       if (len != -1) {
  249.     aObj = class_createInstance (objc_getClass (objName));
  250.           
  251.     /* If the object was 
  252.        successfully created then
  253.        tell it to dearchive
  254.        itself. */
  255.     if (aObj)
  256.       [aObj readFrom:aFd];
  257.       }
  258.     }
  259.     
  260.   if (len == -1)
  261.     [self error:"error activating object, errno=%d", errno];
  262.   
  263.   return aObj;
  264. }
  265.  
  266.  
  267. - readFrom:(int)aFd { return self; }
  268.  
  269.  
  270. + (int)version
  271. {
  272.   return class_getVersion (CLASS (self));
  273. }
  274.  
  275.  
  276. + setVersion:(int)aVersion
  277. {
  278.   class_setVersion (CLASS (self), aVersion);
  279.   
  280.   return self;
  281. }
  282.  
  283.  
  284. @end
  285.  
  286.  
  287.