"
COPYRIGHT (c) 1996 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
Object subclass:#Namespace
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'Kernel-Classes'
!
!Namespace class methodsFor:'documentation'!
copyright
"
COPYRIGHT (c) 1996 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
!
documentation
"
A Namespace is actually a dummy class, providing a home
for its private classes.
Thus, internally, the same mechanism is used for classes in
a namespace and private classes.
This has two advantages:
- we only need one mechanism for both namespaces
and private classes
- there are no possible conflicts between a class
and a namespace named alike.
[author:]
Claus Gittinger
[see also:]
Behavior ClassDescription Class Metaclass
PrivateMetaclass
"
! !
!Namespace class methodsFor:'instance creation'!
fullName:aFullNameSpacePathName
"given a possibly nested name of a namespace, create all required
intermediate spaces (if not already existing) and return the
bottom-level space."
"/ break it up, check for all intermediate spaces to exist
"/ create them as required.
|list idx0 idx superSpace done thisNamespace|
list := OrderedCollection new.
idx0 := 1.
done := false.
[done] whileFalse:[
idx := aFullNameSpacePathName indexOf:$: startingAt:idx0.
(idx ~~ 0) ifTrue:[
(idx < aFullNameSpacePathName size and:[(aFullNameSpacePathName at:(idx+1)) == $:]) ifTrue:[
superSpace := aFullNameSpacePathName copyFrom:idx0 to:(idx-1).
list add:superSpace.
idx0 := idx +2.
] ifFalse:[
done := true
]
] ifFalse:[
done := true.
]
].
list add:(aFullNameSpacePathName copyFrom:idx0).
"/ now, look and create 'em
thisNamespace := nil.
list do:[:aName |
|key x|
key := aName asSymbol.
thisNamespace isNil ifTrue:[
(Smalltalk includesKey:key) ifTrue:[
thisNamespace := Smalltalk at:key.
(thisNamespace notNil
and:[thisNamespace isBehavior not]) ifTrue:[
self error:'name conflict: namespace ' , aName , ' vs. global'.
]
].
thisNamespace isNil ifTrue:[
thisNamespace := self name:key
]
] ifFalse:[
x := thisNamespace privateClassesAt:key.
x notNil ifTrue:[
thisNamespace := x
] ifFalse:[
thisNamespace :=
self subclass:key
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
privateIn:thisNamespace.
"/ nameSpaces are not in any package
thisNamespace setPackage:nil.
]
]
].
^ thisNamespace
"Created: 8.11.1996 / 13:41:59 / cg"
"Modified: 4.1.1997 / 16:50:59 / cg"
!
name:aStringOrSymbol
"create a new nameSpace, named aStringOrSymbol.
Notice, that the nameSpace is created in the current one -
dont get confused; we recommend, not to nest them too much."
|currentNameSpace newNamespace existing ok nameSym fullName|
ok := aStringOrSymbol first isLetter.
ok ifTrue:[
(aStringOrSymbol
findFirst:[:ch | (ch isLetterOrDigit or:[ch == $_]) not]
startingAt:2) ~~ 0
ifTrue:[
ok := false.
]
"/ 2 to:aStringOrSymbol size do:[:idx | |ch|
"/ ch := aStringOrSymbol at:idx.
"/ ok ifTrue:[
"/ ok := ch isLetterOrDigit or:[ch == $_].
"/ ]
"/ ]
].
ok ifFalse:[
self error:'invalid namespace name:''' , aStringOrSymbol printString , ''' (must be a valid identifier)'.
].
nameSym := aStringOrSymbol asSymbol.
self == Namespace ifTrue:[
currentNameSpace := Class nameSpaceQuerySignal query.
currentNameSpace isNil ifTrue:[currentNameSpace := Smalltalk].
fullName := nameSym
] ifFalse:[
currentNameSpace := self.
fullName := (self name , '::' , nameSym) asSymbol
].
(existing := currentNameSpace at:nameSym) notNil ifTrue:[
^ existing
].
newNamespace := self subclass:fullName
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
category:'uncategorized namespace'.
"/ nameSpaces are not in any package
newNamespace notNil ifTrue:[newNamespace setPackage:nil].
^ newNamespace
"
Namespace name:'foo'
(Namespace name:'foo') category:'my name space'
foo at:#bar put:(Metaclass new new)
(Namespace name:'foo') name:'bar'
"
"
Namespace name:'an-invalid++name'
Namespace name:'another:invalidName'
Namespace name:'another::invalidName'
"
"Modified: / 14.9.1997 / 09:46:59 / cg"
"Modified: / 18.3.1999 / 18:24:13 / stefan"
!
new
"catch new - namespaces are not to be created by the user"
self error:'namespaces are not to be created with new'
"Modified: 8.11.1996 / 21:38:00 / cg"
! !
!Namespace class methodsFor:'accessing'!
allClasses
|classes|
classes := IdentitySet new.
self allBehaviorsDo:[:aClass | classes add:aClass].
^ classes
"Modified: 20.12.1996 / 15:34:50 / cg"
!
allClassesWithAllPrivateClasses
|classes|
classes := IdentitySet new.
self allBehaviorsDo:[:aClass |
classes add:aClass.
aClass addAllPrivateClassesTo:classes.
].
^ classes
"Modified: 20.12.1996 / 15:34:50 / cg"
!
at:classNameSymbol
"return a class from the namespace defined by the receiver"
^ self privateClassesAt:classNameSymbol
"Modified: 8.11.1996 / 21:39:41 / cg"
!
at:classNameSymbol ifAbsent:exceptionBlock
"return a class or an alternative
from the namespace defined by the receiver"
|cls|
cls := self privateClassesAt:classNameSymbol.
cls isNil ifTrue:[
^ exceptionBlock value
].
^ cls
"Modified: 8.11.1996 / 21:40:01 / cg"
!
at:classNameSymbol put:aClass
"add a class to the namespace defined by the receiver"
^ self privateClassesAt:classNameSymbol put:aClass
"Modified: 8.11.1996 / 21:40:12 / cg"
!
classNamed:aString
"return the class with name aString, or nil if absent.
To get to the metaClass, append ' class' to the string."
^ Smalltalk classNamed:(self name , '::' , aString)
"Created: 9.9.1997 / 03:33:56 / cg"
!
includesKey:aClassNameStringOrSymbol
"{ Pragma: +optSpace }"
"return true if such a key is present"
|nmSym|
nmSym := (self name , '::' , aClassNameStringOrSymbol) asSymbolIfInterned.
nmSym isNil ifTrue:[^ false].
^ Smalltalk includesKey:nmSym.
! !
!Namespace class methodsFor:'defining'!
defineClass: name
superclass: superclass
indexedType: indexed
private: private
instanceVariableNames: instVars
classInstanceVariableNames: classInstVars
imports: imports
category: category
attributes: annotations
|variable words pointers|
variable := words := pointers := false.
indexed ~~ #none ifTrue:[
self halt.
].
^ superclass value class
name:name
in:self
subclassOf:superclass value
instanceVariableNames:instVars
variable:variable
words:words
pointers:pointers
classVariableNames:''
poolDictionaries:''
category:category
comment:nil
changed:true
classInstanceVariableNames:classInstVars
! !
!Namespace class methodsFor:'enumerating'!
allBehaviorsDo:aBlock
"enumerate all classes in this namespace"
Smalltalk allBehaviorsDo:[:aClass |
(aClass isBehavior and:[aClass isMeta not]) ifTrue:[
aClass nameSpace == self ifTrue:[
aBlock value:aClass
].
]
].
"Modified: / 18.3.1999 / 17:21:06 / cg"
!
keysDo:aBlock
"enumerate all class names in this namespace"
|prefix prefixLen|
prefix := self name , '::'.
prefixLen := prefix size.
Smalltalk keysAndValuesDo:[:aName :aClass |
|key|
(aName startsWith:prefix) ifTrue:[
key := (aName copyFrom:prefixLen+1) asSymbol.
aBlock value:key
]
].
"
Benchmarks keysDo:[:k | Transcript showCR:k]
"
! !
!Namespace class methodsFor:'fileOut'!
fileOutDefinitionOn:aStream
"redefined to generate another definition message"
self == Namespace ifTrue:[
super fileOutDefinitionOn:aStream
] ifFalse:[
aStream nextPutAll:('Namespace name:' , self name storeString)
]
"Modified: 8.11.1996 / 21:39:03 / cg"
"Created: 4.1.1997 / 20:36:32 / cg"
! !
!Namespace class methodsFor:'inspecting'!
inspectorClass
"{ Pragma: +optSpace }"
"redefined to launch a DictionaryInspector
(instead of the default Inspector)."
^ DictionaryInspectorView
! !
!Namespace class methodsFor:'printing & storing'!
displayString
"return a printed represenation - here, a reminder is appended,
that this is not a regular class"
self == Namespace ifTrue:[
^ super displayString
].
^ self name , ' (* namespace *)'
"Created: 8.11.1996 / 21:37:24 / cg"
"Modified: 20.12.1996 / 15:11:31 / cg"
! !
!Namespace class methodsFor:'queries'!
allNamespaces
"return a list of all namespaces"
|set|
set := IdentitySet with:Smalltalk.
Smalltalk allBehaviorsDo:[:aClass |
(aClass isNameSpace
and:[aClass ~~ Namespace
and:[aClass ~~ Smalltalk]]) ifTrue:[
set add:aClass
]
].
^ set
"Created: 2.1.1997 / 20:17:40 / cg"
!
isNameSpace
"return true - I am a namespace"
self == Namespace ifTrue:[^ false].
^ true
!
isTopLevelNamespace
^ (self name includes:$:) not
! !
!Namespace class methodsFor:'documentation'!
version
^ '$Header: /cvs/stx/stx/libbasic/NameSpace.st,v 1.35 2000-03-24 16:41:36 cg Exp $'
! !