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

  1. "======================================================================
  2. |
  3. |   ClassDescription 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         10 Nov 90      Implemented compile:classified: and
  36. |              compile:classified:notifying:.
  37. |
  38. | sbb         21 Sep 90      Fixed the implementation of instVarNames to just
  39. |              return the variables defined by the current class,
  40. |              and added implementatinos of allInstVarNames and
  41. |              subclassInstVarNames.
  42. |
  43. | sbyrne     23 Sep 89      fileOutCategory: is dangerous, so I make it write to
  44. |              a subdirectory called './categories'.
  45. |
  46. | sbyrne     25 Apr 89      created.
  47. |
  48. "
  49.  
  50. Behavior subclass: #ClassDescription
  51.      instanceVariableNames: 'name comment instanceVariables category'
  52.      classVariableNames: ''
  53.      poolDictionaries: ''
  54.      category: nil
  55. !
  56.  
  57. ClassDescription comment: 
  58. 'My instances record information generally attributed to classes and
  59. metaclasses; namely, the class name, class comment (you wouldn''t be
  60. reading this if it weren''t for me), a list of the instance variables
  61. of the class, and the class category.  I provide methods that
  62. access classes by category, and allow whole categories of classes to be
  63. filed out to external disk files.' !
  64.  
  65.  
  66.  
  67. !ClassDescription methodsFor: 'accessing class description'!
  68.  
  69. name
  70.     ^name
  71. !
  72.  
  73. comment
  74.     ^comment
  75. !
  76.  
  77. comment: aString
  78.     comment _ aString
  79. !
  80.  
  81. addInstVarName: aString
  82.     instanceVariables _ instanceVariables copyWith: aString
  83. !
  84.  
  85. removeInstVarName: aString
  86.     instanceVariables _ instanceVariables copyWithout: aString
  87. !!
  88.  
  89.  
  90.  
  91. !ClassDescription methodsFor: 'organization of messages and classes'!
  92.  
  93. category
  94.     ^category
  95. !
  96.  
  97. category: aString
  98.     aString isNil
  99.     ifTrue: [ category _ nil ]
  100.     ifFalse: [ category _ aString asSymbol ]
  101. !
  102.  
  103. removeCategory: aString
  104.     | selector method category |
  105.     methodDictionary isNil
  106.         ifTrue: [ ^self ].
  107.     category _ aString asSymbol.
  108.     methodDictionary associationsDo:
  109.         [ :assoc | method _ assoc key.
  110.                method methodCategory = category
  111.                ifTrue: [ methodDictionary remove: assoc ] ].
  112. !
  113.  
  114. whichCategoryIncludesSelector: selector
  115.     | method |
  116.     methodDictionary isNil
  117.         ifTrue: [ ^nil ].
  118.     method _ methodDictionary at: selector.
  119.     ^method methodCategory
  120. !!
  121.  
  122.  
  123.  
  124. !ClassDescription methodsFor: 'copying'!
  125.  
  126. copy: selector from: aClass
  127.     | method |
  128.     method _ aClass compiledMethodAt: selector.
  129.     methodDictionary at: selector put: method.
  130. !
  131.  
  132. copy: selector from: aClass classified: categoryName
  133.     | method |
  134.     method _ (aClass compiledMethodAt: selector) deepCopy.
  135.     method methodCategory: categoryName.
  136.     methodDictionary at: selector put: method
  137. !
  138.  
  139. copyAll: arrayOfSelectors from: class
  140.     arrayOfSelectors do:
  141.     [ :selector | self copy: selector
  142.                from: class ]
  143. !
  144.  
  145. copyAll: arrayOfSelectors from: class classified: categoryName
  146.     arrayOfSelectors do:
  147.     [ :selector | self copy: selector
  148.                from: class
  149.                classified: categoryName ]
  150. !
  151.  
  152. copyAllCategoriesFrom: aClass
  153.     | method |
  154.     aClass selectors do:
  155.     [ :selector | self copy: selector from: aClass ]
  156. !
  157.  
  158. copyCategory: categoryName from: aClass
  159.     | method |
  160.     aClass selectors do:
  161.     [ :selector | method _ aClass compiledMethodAt: selector.
  162.               method methodCategory = categoryName
  163.               ifTrue: [ self copy: selector from: aClass ] ]
  164. !
  165.  
  166. copyCategory: categoryName from: aClass classified: newCategoryName
  167.     | method |
  168.     aClass selectors do:
  169.     [ :selector | method _ aClass compiledMethodAt: selector.
  170.               method methodCategory = categoryName
  171.               ifTrue: [ self copy: selector
  172.                      from: aClass
  173.                      classified: newCategoryName ] ]
  174. !!
  175.  
  176.  
  177.  
  178. !ClassDescription methodsFor: 'compiling'!
  179.  
  180. compile: code classified: categoryName
  181.     | method |
  182.     method _ self compile: code.
  183.     method methodCategory: categoryName.
  184.     ^method
  185. !
  186.  
  187. compile: code classified: categoryName notifying: requestor
  188.     | method |
  189.     method _ self compile: code notifying: requestor.
  190.     method methodCategory: categoryName.
  191.     ^method
  192. !!
  193.  
  194.  
  195.  
  196. !ClassDescription methodsFor: 'accessing instances and variables'!
  197.  
  198. instVarNames
  199.     | superVars |
  200.     superClass isNil
  201.     ifTrue: [ ^instanceVariables copy ]
  202.     ifFalse: [ superVars _ superClass allInstVarNames.
  203.            ^instanceVariables copyFrom: superVars size+1 
  204.                       to: instanceVariables size ]
  205. !
  206.  
  207. subclassInstVarNames
  208.     | varNameSet |
  209.     varNameSet _ Set new.
  210.     self subclasses do: 
  211.     [ :class | Set addAll: (class instVarNames) ].
  212.     ^varNameSet
  213. !
  214.  
  215. allInstVarNames
  216.     ^instanceVariables
  217.  
  218. !!
  219.  
  220.  
  221.  
  222. !ClassDescription methodsFor: 'printing'!
  223.  
  224. classVariableString
  225.     self subclassResponsibility
  226. !
  227.  
  228. instanceVariableString
  229.     | aString |
  230.     instanceVariables isNil ifTrue: [ ^'' ].
  231.     aString _ String new: 0.
  232.     instanceVariables do: [ :instVarName | aString _ aString ,
  233.                                               instVarName , ' ' ].
  234.     ^aString
  235. !
  236.  
  237. sharedVariableString
  238.     self subclassResponsibility
  239. !!
  240.  
  241.  
  242.  
  243. !ClassDescription methodsFor: 'filing'!
  244.  
  245. fileOutOn: aFileStream
  246.     | categories now |
  247.     categories _ Set new.
  248.     methodDictionary isNil ifTrue: [ ^self ].
  249.     methodDictionary do:
  250.     [ :method | categories add: (method methodCategory) ].
  251.     aFileStream nextPutAll: '''Filed out from ';
  252.     nextPutAll: Version;
  253.     nextPutAll: ' on '.
  254.     now _ Date dateAndTimeNow.
  255.     aFileStream print: (now at: 1);
  256.     nextPutAll:  '  ';
  257.     print: (now at: 2);
  258.     nextPutAll: ' GMT''!'; nl; nl.
  259.     categories asSortedCollection do:
  260.         [ :category | self emitCategory: category toStream: aFileStream ]
  261. !
  262.  
  263. fileOutCategory: categoryName
  264.     | aFileStream fileName |
  265.     name notNil
  266.         ifTrue: [ fileName _ name ]
  267.     ifFalse: [ fileName _ (self instanceClass name) , '-class' ].
  268.     fileName _ './categories/', fileName , '.st' .
  269.     aFileStream _ FileStream open: fileName mode: 'w'.
  270.     self emitCategory: categoryName toStream: aFileStream.
  271.     aFileStream close
  272. !!
  273.  
  274.  
  275.  
  276. !ClassDescription methodsFor: 'private'!
  277.  
  278. emitCategory: category toStream: aFileStream
  279.     "I write legal Smalltalk load syntax definitions of all of my methods
  280.      are in the 'category' category to the aFileStream"
  281.     aFileStream nextPutAll: '!';
  282.     print: self;
  283.     nextPutAll: ' methodsFor: ''';
  284.     nextPutAll: category;
  285.     nextPutAll: '''!'.
  286.     methodDictionary notNil
  287.       ifTrue: [ methodDictionary do:
  288.               [ :method | (method methodCategory) = category
  289.                       ifTrue: [ aFileStream nextPutAll: '
  290.  
  291. '                                ;
  292.                         nextPutAll: method methodSourceString;
  293.                         nextPutAll: '!' ] ] ].
  294.     aFileStream nextPutAll: '!
  295.  
  296. '
  297.  
  298. !
  299.  
  300. setName: aSymbol
  301.     name _ aSymbol
  302. !
  303.  
  304. setInstanceVariables: instVariableArray
  305.     instanceVariables _ instVariableArray
  306. !
  307.  
  308. setSuperclass: aClass
  309.     "Set the superclass of the receiver to be 'aClass'.  Also adds the receiver
  310.      as a subclass of 'aClass'"
  311.     self superclass == aClass
  312.     ifTrue: [ ^self ].    "don't need to change anything"
  313.     self superclass notNil    "remove any old knowledge of this class"
  314.     ifTrue: [ self superclass removeSubclass: self ].
  315.     self superclass: aClass.
  316.     aClass addSubclass: self
  317. ! !
  318.