# Provides a simple object oriented interface using
# SWIG's low level interface.
#
# Contributed by Erik Bierwagen and Paul Saxe
#

proc new {objectType handle_r args} {

    # Creates a new SWIG object of the given type, 
    # returning a handle in the variable "handle_r".
    #
    # Also creates a procedure for the object and a trace on 
    # the handle variable that deletes the object when the
    # handle varibale is overwritten or unset

    upvar $handle_r handle

    #
    # Create the new object
    #

    eval set handle \[new_$objectType $args\]

    #
    # Set up the object procedure
    #

    proc $handle {cmd args} "eval ${objectType}_\$cmd $handle \$args"

    #
    # And the trace ...
    #

    uplevel trace variable $handle_r uw "{deleteObject $objectType $handle}"

    # 
    # Return the handle so that 'new' can be used as an argument to a procedure
    #

    return $handle
}

proc deleteObject {objectType handle name element op} {

    #
    # Check that the object handle has a reasonable form
    #

    if {![regexp {_[0-9a-f]*_(.+)_p} $handle]} {
	error "deleteObject: not a valid object handle: $handle"
    }

    #
    # Remove the object procedure
    #

    catch {rename $handle {}}

    #
    # Delete the object
    #

    delete_$objectType $handle
}

proc delete {handle_r} {

    #
    # A synonym for unset that is more familiar to C++ programmers
    #

    uplevel unset $handle_r
}
