NameSpace.st
author Claus Gittinger <cg@exept.de>
Thu, 02 Jan 1997 18:47:18 +0100
changeset 2025 9ab95f23af33
parent 2024 71a088db4583
child 2030 94800ed9cdc5
permissions -rw-r--r--
*** empty log message ***

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

    ^ thisNamespace

    "Created: 8.11.1996 / 13:41:59 / cg"
    "Modified: 20.12.1996 / 15:17:07 / cg"
!

name:aStringOrSymbol
    "create a new nameSpace, named aStringOrSymbol"

    |newNamespace existing ok|

    ok := aStringOrSymbol first isLetter.
    ok ifTrue:[
        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.
    ].

    (existing := Smalltalk at:aStringOrSymbol asSymbol) notNil ifTrue:[
        ^ existing
    ].

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

    ^ newNamespace

    "
     Namespace name:'foo'
     (Namespace name:'foo') category:'my name space'
     foo at:#bar put:(Metaclass new new)
    "

    "Modified: 2.1.1997 / 18:41:22 / 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"
! !

!Namespace class methodsFor:'enumerating'!

allBehaviorsDo:aBlock
    "enumerate all classes in this namespace"

    self privateClassesDo:aBlock

    "Created: 26.10.1996 / 12:29:01 / cg"
    "Modified: 8.11.1996 / 21:39:20 / cg"
! !

!Namespace class methodsFor:'fileOut'!

basicFileOutDefinitionOn:aStream
    "redefined to generate another definition message"

    self == Namespace ifTrue:[
        super basicFileOutDefinitionOn:aStream
    ] ifFalse:[
        aStream nextPutAll:('Namespace name:' , self name storeString)
    ]

    "Modified: 8.11.1996 / 21:39:03 / 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'!

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.14 1997-01-02 17:47:18 cg Exp $'
! !