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

  1. "======================================================================
  2. |
  3. |   Class 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         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbb         22 Sep 90      Implemented classVarNames and allClassVarNames.
  36. |              Implemented sharedPools and allSharedPools.
  37. |
  38. | sbyrne     16 May 90      Improved error checking: you now cannot create a
  39. |              subclass of a class whose type is not compatible
  40. |              (i.e. non-variable subclass of a variable byte
  41. |              class).
  42. |
  43. | sbyrne     16 May 90      Minor changes to support preserving class definitions
  44. |              as long as possible (i.e. if you re-invoke the class
  45. |              definition method, it tries to re-use the existing
  46. |              class if possible).
  47. |
  48. | sbyrne     13 Jan 90      Began experimental addition of actual class
  49. |              definitions. 
  50. |
  51. | sbyrne     25 Apr 89      created.
  52. |
  53. "
  54.  
  55. ClassDescription subclass: #Class
  56.          instanceVariableNames: 'classVariables sharedPools'
  57.          classVariableNames: ''
  58.          poolDictionaries: ''
  59.          category: nil
  60. !
  61.  
  62. Class comment: 
  63. 'I am THE class object.  My instances are the classes of the system' !
  64.  
  65.  
  66.  
  67. !Class methodsFor: 'accessing instances and variables'!
  68.  
  69. addClassVarName: aString
  70.     | sym |
  71.     sym _ aString asSymbol.    "### maybe this should remain a string? "
  72.     (classVariables includesKey: sym)
  73.         ifTrue: [ ^self error: 'class variable already present' ]
  74.     ifFalse: [ classVariables at: sym put: nil ]
  75. !
  76.  
  77. removeClassVarName: aString
  78.     "Removes the class variable from the class, error if not present, or
  79.      still in use."
  80.     | sym |
  81.     sym _ aString asSymbol.    "### maybe this should remain a string? "
  82.     " ### test for use in sub method "
  83.     (classVariables includesKey: sym)
  84.         ifTrue: [ classVariables removeKey: sym ]
  85.     ifFalse: [ self error: 'class variable not present' ]
  86. !
  87.  
  88. classPool
  89.     ^classVariables
  90. !
  91.  
  92. classVarNames
  93.     | superVarNames |
  94.     self superclass isNil
  95.     ifTrue: [ ^self allClassVarNames ].
  96.     superVarNames _ self superclass allClassVarNames.
  97.     ^(self allClassVarNames) removeAll: superVarNames
  98. !
  99.  
  100. allClassVarNames
  101.     | s |
  102.     s _ Set new.
  103.     classVariables keysDo: [ :key | s add: key ].
  104.     ^s
  105. !
  106.  
  107. addSharedPool: aDictionary
  108.     (sharedPools includes: aDictionary)
  109.         ifTrue: [ ^self error: 'Attempt to add an already-present shared pool' ].
  110.     sharedPools _ sharedPools copyWith: aDictionary
  111. !
  112.  
  113. removeSharedPool: aDictionary
  114.     (sharedPools includes: aDictionary)
  115.         ifFalse: [ ^self error: 'Attempt to remove non-existent shared pool' ].
  116.     sharedPools _ sharedPools copyWithout: aDictionary
  117. !
  118.  
  119. sharedPools
  120.     | superPools |
  121.     self superclass isNil
  122.     ifTrue: [ ^self allSharedPools ].
  123.     superPools _ self superclass allSharedPools.
  124.     ^(self allSharedPools) removeAll: superPools
  125. !
  126.  
  127. allSharedPools
  128.     | s |
  129.     s _ Set new.
  130.     sharedPools do:
  131.     [ :sp | s add: (Smalltalk keyAtValue: sp) ].
  132.     ^s
  133. !
  134.  
  135. initialize            "redefined in children (?)"
  136.     ^self
  137. !!
  138.  
  139.  
  140.  
  141. !Class methodsFor: 'testing'!
  142.  
  143. = aClass
  144.     "Returns true if the two class objects are to be considered equal."
  145.     ^name = aClass name
  146. !!
  147.  
  148.  
  149.  
  150. !Class methodsFor: 'instance creation'!
  151.  
  152.  
  153. subclass: classNameString
  154.     instanceVariableNames: stringInstVarNames
  155.     classVariableNames: stringOfClassVarNames
  156.     poolDictionaries: stringOfPoolNames
  157.     category: categoryNameString
  158.     | meta |
  159.     KernelInitialized ifFalse: [ ^nil ].
  160.     self isVariable
  161.     ifTrue: [ ^self error: 'cannot create a non-variable subclass 
  162. of a variable class' ].
  163.     meta _ self metaclassFor: classNameString.
  164.     ^meta name: classNameString
  165.       environment: Smalltalk
  166.       subclassOf: self
  167.       instanceVariableNames: stringInstVarNames
  168.       variable: false
  169.       words: true
  170.       pointers: true
  171.       classVariableNames: stringOfClassVarNames
  172.       poolDictionaries: stringOfPoolNames
  173.       category: categoryNameString
  174.       comment: nil
  175.       changed: nil
  176. !
  177.  
  178. variableSubclass: classNameString
  179.     instanceVariableNames: stringInstVarNames
  180.     classVariableNames: stringOfClassVarNames
  181.     poolDictionaries: stringOfPoolNames
  182.     category: categoryNameString
  183.     | meta |
  184.     KernelInitialized ifFalse: [ ^nil ].
  185.     self isVariable & (self isWords | self isBytes) 
  186.     ifTrue: [ ^self error: 'cannot create a variable subclass from a 
  187. non-pointer variable parent class' ].
  188.     meta _ self metaclassFor: classNameString.
  189.     ^meta name: classNameString
  190.       environment: Smalltalk
  191.       subclassOf: self
  192.       instanceVariableNames: stringInstVarNames
  193.       variable: true
  194.       words: true
  195.       pointers: true
  196.       classVariableNames: stringOfClassVarNames
  197.       poolDictionaries: stringOfPoolNames
  198.         category: categoryNameString
  199.       comment: nil
  200.         changed: nil
  201. !
  202.  
  203. variableWordSubclass: classNameString
  204.   instanceVariableNames: stringInstVarNames
  205.   classVariableNames: stringOfClassVarNames
  206.   poolDictionaries: stringOfPoolNames
  207.   category: categoryNameString
  208.     | meta |
  209.     KernelInitialized ifFalse: [ ^nil ].
  210.     self isVariable & (self isPointers | self isBytes) 
  211.     ifTrue: [ ^self error: 'cannot create a word subclass from a non-word 
  212. variable parent class' ].
  213.     meta _ self metaclassFor: classNameString.
  214.     ^meta name: classNameString
  215.             environment: Smalltalk
  216.         subclassOf: self
  217.         instanceVariableNames: stringInstVarNames
  218.         variable: true
  219.         words: true
  220.         pointers: false
  221.             classVariableNames: stringOfClassVarNames
  222.         poolDictionaries: stringOfPoolNames
  223.         category: categoryNameString
  224.         comment: nil
  225.         changed: nil
  226. !
  227.  
  228. variableByteSubclass: classNameString
  229.   instanceVariableNames: stringInstVarNames
  230.   classVariableNames: stringOfClassVarNames
  231.   poolDictionaries: stringOfPoolNames
  232.   category: categoryNameString
  233.     | meta |
  234.     KernelInitialized ifFalse: [ ^nil ].
  235.     self isVariable & (self isPointers | self isWords)
  236.     ifTrue: [ ^self error: 'Cannot create variable byte subclass from 
  237. non-byte parent class' ].
  238.     meta _ self metaclassFor: classNameString.
  239.     ^meta name: classNameString
  240.             environment: Smalltalk
  241.         subclassOf: self
  242.         instanceVariableNames: stringInstVarNames
  243.         variable: true
  244.         words: false
  245.         pointers: false
  246.             classVariableNames: stringOfClassVarNames
  247.         poolDictionaries: stringOfPoolNames
  248.         category: categoryNameString
  249.         comment: nil
  250.         changed: nil
  251. ! !
  252.  
  253.  
  254.  
  255. !Class methodsFor: 'printing'!
  256.  
  257. printOn: aStream
  258.     self name printOn: aStream
  259. !
  260.  
  261. storeOn: aStream
  262.     self printOn: aStream
  263. ! !
  264.  
  265.  
  266.  
  267. !Class methodsFor: 'private'!
  268.  
  269. setClassVariables: aDictionary
  270.     classVariables _ aDictionary
  271. !
  272.  
  273. setSharedPools: aDictionary
  274.     sharedPools _ aDictionary
  275. !
  276.  
  277. metaclassFor: classNameString
  278.     | className class |
  279.     className _ classNameString asSymbol.
  280.     class _ Smalltalk at: className 
  281.               ifAbsent: [ nil ].
  282.     class isNil
  283.     ifTrue: [ ^Metaclass subclassOf: self class ].
  284.     ^class class
  285. ! !
  286.