NameSpace.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Sep 1997 04:57:04 +0200
changeset 2908 b3be3629fcb1
parent 2583 16b1872c068d
child 2924 398bec06170f
permissions -rw-r--r--
vms stuff

"
 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.
"


'From Smalltalk/X, Version:3.1.9 on 9-sep-1997 at 3:45:43 pm'                   !

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|

    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)'.
        ^ nil.
    ].

    currentNameSpace := Class nameSpaceQuerySignal raise.
    (existing := currentNameSpace at:aStringOrSymbol asSymbol) notNil ifTrue:[
        ^ existing
    ].

    newNamespace := self subclass:aStringOrSymbol asSymbol
                         instanceVariableNames:''
                         classVariableNames:''
                         poolDictionaries:''
                         category:'uncategorized namespace'.

    "/ nameSpaces are not in any package
    newNamespace setPackage:nil.
    ^ newNamespace

    "
     Namespace name:'foo'
     (Namespace name:'foo') category:'my name space'
     foo at:#bar put:(Metaclass new new)
    "
    "
     Namespace name:'an-invalid++name'
     Namespace name:'another:invalidName'
     Namespace name:'another::invalidName'
    "

    "Modified: 16.1.1997 / 01:37:09 / cg"
!

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"
!

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"
! !

!Namespace class methodsFor:'enumerating'!

allBehaviorsDo:aBlock
    "enumerate all classes in this namespace"

    Smalltalk allBehaviorsDo:[:aClass |
        aClass isBehavior ifTrue:[
            aClass nameSpace == self ifTrue:[
                aBlock value:aClass
            ].
        ]
    ].

    "Modified: 23.4.1997 / 17:00:48 / cg"
! !

!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:'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

    "Created: 26.10.1996 / 11:13:36 / cg"
    "Modified: 20.12.1996 / 15:11:45 / cg"
! !

!Namespace class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/NameSpace.st,v 1.24 1997-09-09 02:56:59 cg Exp $'
! !