bug: created sub-nameSpace Foo::Foo,
when a class was created in an existing Foo nameSpace.
(i.e. newNameSpace -> Foo
newClass Foo:Class1
newClass Foo::Class2)
"
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.
"
"{ Package: 'stx:libbasic' }"
Object subclass:#NameSpace
instanceVariableNames:'category'
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 thisNamespace|
(aFullNameSpacePathName includes:$:) ifTrue:[
"/ old style
list := aFullNameSpacePathName asCollectionOfSubstringsSeparatedByAll:'..'.
] ifFalse:[
"/ new style
list := aFullNameSpacePathName asCollectionOfSubstringsSeparatedBy:$..
].
"/ 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:[
thisNamespace isNameSpace ifTrue:[
x := thisNamespace at:key.
] ifFalse:[
thisNamespace isBehavior ifTrue:[
x := thisNamespace privateClassesAt:key.
].
].
x isNil ifTrue:[
thisNamespace isNameSpace ifTrue:[
x := thisNamespace name:key.
] ifFalse:[
x :=
self subclass:key
instanceVariableNames:''
classVariableNames:''
poolDictionaries:''
privateIn:thisNamespace.
].
"/ nameSpaces are not in any package (yet)
x setPackage:nil.
].
thisNamespace := x.
]
].
^ 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.
]
].
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'
inEnvironment:Smalltalk.
"/ 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:'Compatibility-VW5.4'!
defineClass: name
superclass: superclass
indexedType: indexed
private: private
instanceVariableNames: instVars
classInstanceVariableNames: classInstVars
imports: imports
category: category
^ self
defineClass: name
superclass: superclass
indexedType: indexed
private: private
instanceVariableNames: instVars
classInstanceVariableNames: classInstVars
imports: imports
category: category
attributes: nil
!
defineClass: name
superclass: superclassOrName
indexedType: indexed
private: private
instanceVariableNames: instVars
classInstanceVariableNames: classInstVars
imports: imports
category: category
attributes: annotations
"VW5i compatibility class/namespace creation"
|variable words pointers superclass|
variable := words := pointers := false.
indexed ~~ #none ifTrue:[
self shouldImplement.
].
superclassOrName isSymbol ifTrue:[
superclass := Smalltalk at:superclassOrName.
superclass isNil ifTrue:[
self error:'missing superclass: ' , superclassOrName.
]
] ifFalse:[
superclass := superclassOrName
].
^ superclass value class
name:name
inEnvironment: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:'accessing'!
allClasses
|classes|
classes := IdentitySet new.
self allClassesDo:[:aClass | classes add:aClass].
^ classes
"Modified: 20.12.1996 / 15:34:50 / cg"
!
allClassesWithAllPrivateClasses
|classes|
classes := IdentitySet new.
self allClassesDo:[: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:'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"
!
allClassesDo:aBlock
"enumerate all classes in this namespace"
Smalltalk allClassesDo:[: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:'fileOut-xml'!
fileOutXMLDefinitionOn:aStream
"redefined to generate another definition message"
self == NameSpace ifTrue:[
super fileOutXMLDefinitionOn:aStream
] ifFalse:[
aStream nextPutLine:'<name-space>'.
aStream nextPutLine:' <name>' , self name , '</name>'.
aStream nextPutLine:' <environment>Smalltalk</environment>'.
aStream nextPutLine:' <private>false</private>'.
aStream nextPutLine:' <imports>Smalltalk.*</imports>'.
aStream nextPutLine:' <category>none</category>'.
aStream nextPutLine:'</name-space>'.
]
! !
!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"
^ self allNamespacesIn:Smalltalk
!
allNamespacesIn:anEnvironment
"return a list of all namespaces"
|set|
set := IdentitySet with:anEnvironment.
anEnvironment allClassesDo:[:aClass |
(aClass isNameSpace
and:[aClass ~~ NameSpace
and:[aClass ~~ anEnvironment
and:[aClass ~~ Smalltalk]]]) ifTrue:[
set add:aClass
]
].
^ set
!
hasNamespaces
"return true - if I support sub-namespaces"
^ false
!
isNameSpace
"return true - I am a namespace"
self == NameSpace ifTrue:[^ false].
^ true
!
isTopLevelNameSpace
^ (self name includes:$:) not
!
isTopLevelNamespace
"obsolete - use isTopLevelNameSpace"
<resource:#obsolete>
^ (self name includes:$:) not
! !
!NameSpace class methodsFor:'documentation'!
version
^ '$Header: /cvs/stx/stx/libbasic/NameSpace.st,v 1.51 2003-08-27 17:51:29 cg Exp $'
! !