home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / object.st < prev    next >
Text File  |  1992-02-15  |  8KB  |  337 lines

  1. "======================================================================
  2. |
  3. |   Object Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         25 Mar 91      Added -> operator.
  34. |
  35. | sbb          5 Nov 90      Fixed bug with addDependent: -- syntax error.
  36. |
  37. | sbyrne     26 Apr 90      Fixed shallowCopy to send new messages to the
  38. |                         object's class instead of the object itself.
  39. |
  40. | sbyrne     19 Sep 89      Converted to use real method categories.
  41. |
  42. | sbyrne      4 Jul 89      Added support for dependence relationships.
  43. |                         (-: how appropriate: July 4 is when the US declared
  44. |                             its INdependence from England :-)
  45. |
  46. | sbyrne     25 Apr 89      created.
  47. |
  48. "
  49.  
  50. Object comment: 
  51. 'I am the root of the Smalltalk class system. 
  52. All classes in the system are subclasses of me.' !
  53.  
  54.  
  55. !Object methodsFor: 'Relational operators'!
  56.  
  57. ~= anObject
  58.     ^(self = anObject) == false
  59. !
  60.  
  61. ~~ anObject
  62.     ^(self == anObject) == false 
  63. !
  64.  
  65. isNil
  66.     ^false
  67. !
  68.  
  69. notNil
  70.     ^true
  71. !!
  72.  
  73.  
  74.  
  75. !Object methodsFor: 'testing functionality'!
  76.  
  77. isKindOf: aClass
  78.     ^(self isMemberOf: aClass) or:
  79.         [ self class inheritsFrom: aClass ]
  80. !
  81.  
  82. isMemberOf: aClass
  83.     "Returns true if the receiver is an instance of the class 'aClass'"
  84.     ^self class == aClass
  85. !!
  86.  
  87.  
  88.  
  89. !Object methodsFor: 'copying'!
  90.  
  91. copy
  92.     ^self shallowCopy
  93. !
  94.  
  95. shallowCopy
  96.     | class aCopy |
  97.     class _ self class.
  98.     class isVariable
  99.         ifTrue: [ aCopy _ class basicNew: self basicSize ]
  100.     ifFalse: [ aCopy _ class basicNew ].
  101.  
  102.     " copy the instance variables (if any) "
  103.     1 to: class instSize do:
  104.         [ :i | aCopy instVarAt: i
  105.                  put: (self instVarAt: i) ].
  106.  
  107.     " copy the indexed variables (if any) "
  108.     class isVariable
  109.         ifTrue: [ 1 to: self basicSize do:
  110.                 [ :i | aCopy basicAt: i
  111.                          put: (self basicAt: i) ] ].
  112.     ^aCopy
  113. !
  114.  
  115. deepCopy
  116.     | class aCopy |
  117.     class _ self class.
  118.     class isVariable
  119.         ifTrue: [ aCopy _ class basicNew: self basicSize ]
  120.     ifFalse: [ aCopy _ class basicNew ].
  121.  
  122.     " copy the instance variables (if any) "
  123.     1 to: class instSize do:
  124.         [ :i | aCopy instVarAt: i
  125.                  put: (self instVarAt: i) deepCopy ].
  126.  
  127.     " copy the indexed variables (if any) "
  128.     class isVariable
  129.         ifTrue: [ 1 to: self basicSize do:
  130.                 [ :i | aCopy basicAt: i
  131.                          put: (self basicAt: i) deepCopy ] ].
  132.     ^aCopy
  133. !!
  134.  
  135.  
  136.  
  137. !Object methodsFor: 'class type methods'!
  138.  
  139. species
  140.     ^self class
  141. !
  142.  
  143. yourself
  144.     ^self
  145. !!
  146.  
  147.  
  148.  
  149. !Object methodsFor: 'dependents access'!
  150.  
  151. addDependent: anObject
  152.     | dependencies |
  153.     dependencies _ Smalltalk dependenciesAt: self.
  154.     dependencies isNil ifTrue:
  155.         [ dependencies _ Set new.
  156.       (Smalltalk at: #Dependencies) at: self put: dependencies ].
  157.     dependencies add: anObject
  158. !
  159.  
  160. removeDependent: anObject
  161.     | dependencies |
  162.     dependencies _ Smalltalk dependenciesAt: self.
  163.     dependencies notNil ifTrue:
  164.         [ dependencies remove: anObject ifAbsent: [] ]
  165. !
  166.  
  167. dependents
  168.     | dependencies |
  169.     dependencies _ Smalltalk dependenciesAt: self.
  170.     dependencies isNil ifTrue: [ dependencies _ Set new ].
  171.     ^dependencies asOrderedCollection
  172. !
  173.  
  174. release
  175.     " +++ I'm not sure that this is the right thing to do here; the book is
  176.       so vague... "
  177.     (Smalltalk at: #Dependencies) removeKey: self
  178. !!
  179.  
  180.  
  181.  
  182. !Object methodsFor: 'change and update'!
  183.  
  184. changed
  185.     self changed: self
  186. !
  187.  
  188. changed: aParameter
  189.     | dependencies |
  190.     dependencies _ Smalltalk dependenciesAt: self.
  191.     dependencies notNil ifTrue:
  192.         [ dependencies do:
  193.         [ :dependent | dependent update: aParameter ] ]
  194. !
  195.  
  196. update: aParameter
  197.     "Default behavior is to do nothing"
  198. !
  199.  
  200. broadcast: aSymbol
  201.     | dependencies |
  202.     dependencies _ Smalltalk dependenciesAt: self.
  203.     dependencies notNil ifTrue:
  204.         [ dependencies do:
  205.         [ :dependent | dependent perform: aSymbol ] ]
  206. !
  207.  
  208. broadcast: aSymbol with: anObject
  209.     | dependencies |
  210.     dependencies _ Smalltalk dependenciesAt: self.
  211.     dependencies notNil ifTrue:
  212.         [ dependencies do:
  213.         [ :dependent | dependent perform: aSymbol with: anObject ] ]
  214. ! !
  215.  
  216.  
  217.  
  218.  
  219. !Object methodsFor: 'syntax shortcuts'!
  220.  
  221. -> anObject
  222.     "Creates a new instance of Association with the receiver being the key
  223.      and the argument becoming the value"
  224.     ^Association key: self value: anObject
  225. ! !
  226.  
  227.  
  228.  
  229.  
  230. !Object methodsFor: 'printing'!
  231.  
  232. printString
  233.     | stream |
  234.     stream _ WriteStream on: (String new: 0).
  235.     self printOn: stream.
  236.     ^stream contents
  237. !
  238.  
  239. printOn: aStream
  240.     | article nameString |
  241.     nameString _ self classNameString.
  242.     article _ nameString first isVowel ifTrue: [ 'an ' ] ifFalse: [ 'a ' ].
  243.     aStream nextPutAll: article;
  244.         nextPutAll: nameString
  245. !
  246.  
  247. print
  248.     self printOn: stdout
  249. !
  250.  
  251. printNl
  252.     self print.
  253.     stdout nl
  254. !!
  255.  
  256.  
  257.  
  258. !Object methodsFor: 'storing'!
  259.  
  260. storeString
  261.     | stream |
  262.     stream _ WriteStream on: (String new: 0).
  263.     self storeOn: stream.
  264.     ^stream contents
  265. !
  266.  
  267. storeOn: aStream
  268.     | class hasSemi |
  269.     class _ self class.
  270.     aStream nextPut: $(.
  271.     aStream nextPutAll: self classNameString.
  272.     hasSemi _ false.
  273.     class isVariable
  274.         ifTrue: [ aStream nextPutAll: ' basicNew: '.
  275.               self basicSize printOn: aStream ]
  276.         ifFalse: [ aStream nextPutAll: ' basicNew' ].
  277.     1 to: class instSize do:
  278.         [ :i | aStream nextPutAll: ' instVarAt: '.
  279.            i printOn: aStream.
  280.            aStream nextPutAll: ' put: '.
  281.            (self instVarAt: i) storeOn: aStream.
  282.            aStream nextPut: $;.
  283.            hasSemi _ true ].
  284.     class isVariable ifTrue: 
  285.         [ 1 to: self basicSize do:
  286.         [ :i | aStream nextPutAll: ' basicAt: '.
  287.                i printOn: aStream.
  288.            aStream nextPutAll: ' put: '.
  289.            (self basicAt: i) storeOn: aStream.
  290.            aStream nextPut: $;.
  291.            hasSemi _ true ] ].
  292.     hasSemi ifTrue: [ aStream nextPutAll: ' yourself' ].
  293.     aStream nextPut: $)
  294. !
  295.  
  296. store
  297.     self storeOn: stdout
  298. !
  299.  
  300. storeNl
  301.     self store.
  302.     stdout nl
  303. !!
  304.  
  305.  
  306.  
  307. !Object methodsFor: 'debugging'!
  308.  
  309. inspect
  310.     | class instVars instVal |
  311.     class _ self class.
  312.     instVars _ class allInstVarNames.
  313.     stdout nextPutAll: 'An instance of '.
  314.     class printNl.
  315.     1 to: instVars size do:
  316.         [ :i | stdout nextPutAll: '  ';
  317.            nextPutAll: (instVars at: i);
  318.            nextPutAll: ': '.
  319.            (self instVarAt: i)  printNl ]
  320. !!
  321.  
  322.  
  323.  
  324. !Object methodsFor: 'private'!
  325.  
  326. classNameString
  327.     | name |
  328.     name _ self class name.
  329.     name isNil
  330.         ifTrue: [ name _ self name , ' class' ].
  331.     ^name
  332.  
  333. !!
  334.  
  335.