Change namespace name

# move_namespace

# Change namespace name

Description:

This transformation allows to consistently change the namespace name.

The motivation for this transformation is to help move whole libraries or source sets from one namespace to another, for example to allow the coexistence of two different version of the same library.

Please use this transformation as a boilerplate for your own customized version.

For example, the following file:

    namespace boost
    {
    void foo();
    }

    void boost::foo() {/* ... */}

will be transformed into:

    namespace boom
    {
    void foo();
    }

    void boom::foo() {/* ... */}

Note: This transformation actually does not check whether the given identifier is indeed a namespace name and the replacement is performed systematically on all identifiers that match. Do not use it on code that overloads namespace names for other purposes.

Note: The transformation is performed in place, which means that the source files are modified.

Show source code for this transformation

Hide source code

    # change the namespace of all source files

    set oldNamespace "boost"
    set newNamespace "boom"

    foreach fileName [getSourceFileNames] {
        set tokens [getTokens $fileName 1 0 -1 -1 {}]
        set newFile [open $fileName "w"]
        foreach token $tokens {
            set tokenValue [lindex $token 0]
            set tokenType [lindex $token 3]
            if {$tokenType == "identifier" && $tokenValue == $oldNamespace} {
                set tokenValue $newNamespace
            }
            puts -nonewline $newFile $tokenValue
        }
        close $newFile
    }

Transformation index