Index: doc/copy.n ================================================================== --- doc/copy.n +++ doc/copy.n @@ -12,23 +12,32 @@ oo::copy \- create copies of objects and classes .SH SYNOPSIS .nf package require TclOO -\fBoo::copy\fI sourceObject \fR?\fItargetObject\fR? +\fBoo::copy\fI sourceObject \fR?\fItargetObject\fR? ?\fItargetNamespace\fR? .fi .BE .SH DESCRIPTION .PP The \fBoo::copy\fR command creates a copy of an object or class. It takes the name of the object or class to be copied, \fIsourceObject\fR, and optionally the name of the object or class to create, \fItargetObject\fR, which will be -resolved relative to the current namespace if not an absolute qualified name. -If \fItargetObject\fR is omitted, a new name is chosen. The copied object will -be of the same class as the source object, and will have all its per-object -methods copied. If it is a class, it will also have all the class methods in -the class copied, but it will not have any of its instances copied. +resolved relative to the current namespace if not an absolute qualified name +and +.VS TIP473 +\fItargetNamespace\fR which is the name of the namespace where the object is +going to be created in. +If either \fItargetObject\fR or \fItargetNamespace\fR is omitted or is given +as the empty string, a new name is chosen. Names, unless specified, are +chosen with the same algorithm used by the \fBnew\fR method of +\fBoo::class\fR. +.VE TIP473 +The copied object will be of the same class as the source object, and will have +all its per-object methods copied. If it is a class, it will also have all the +class methods in the class copied, but it will not have any of its instances +copied. .PP .VS After the \fItargetObject\fR has been created and all definitions of its configuration (e.g., methods, filters, mixins) copied, the \fB\fR method of \fItargetObject\fR will be invoked, to allow for customization of Index: generic/tclOOBasic.c ================================================================== --- generic/tclOOBasic.c +++ generic/tclOOBasic.c @@ -1181,12 +1181,13 @@ int objc, Tcl_Obj *const *objv) { Tcl_Object oPtr, o2Ptr; - if (objc < 2 || objc > 3) { - Tcl_WrongNumArgs(interp, 1, objv, "sourceName ?targetName?"); + if (objc < 2 || objc > 4) { + Tcl_WrongNumArgs(interp, 1, objv, + "sourceName ?targetName? ?targetNamespace?"); return TCL_ERROR; } oPtr = Tcl_GetObjectFromObj(interp, objv[1]); if (oPtr == NULL) { @@ -1202,16 +1203,18 @@ */ if (objc == 2) { o2Ptr = Tcl_CopyObjectInstance(interp, oPtr, NULL, NULL); } else { - const char *name; + const char *name, *namespaceName; Tcl_DString buffer; name = TclGetString(objv[2]); Tcl_DStringInit(&buffer); - if (name[0]!=':' || name[1]!=':') { + if (name[0] == '\0') { + name = NULL; + } else if (name[0]!=':' || name[1]!=':') { Interp *iPtr = (Interp *) interp; if (iPtr->varFramePtr != NULL) { Tcl_DStringAppend(&buffer, iPtr->varFramePtr->nsPtr->fullName, -1); @@ -1218,11 +1221,30 @@ } TclDStringAppendLiteral(&buffer, "::"); Tcl_DStringAppend(&buffer, name, -1); name = Tcl_DStringValue(&buffer); } - o2Ptr = Tcl_CopyObjectInstance(interp, oPtr, name, NULL); + + /* + * Choose a unique namespace name if the user didn't supply one. + */ + + namespaceName = NULL; + if (objc == 4) { + namespaceName = TclGetString(objv[3]); + + if (namespaceName[0] == '\0') { + namespaceName = NULL; + } else if (Tcl_FindNamespace(interp, namespaceName, NULL, + 0) != NULL) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "%s refers to an existing namespace", namespaceName)); + return TCL_ERROR; + } + } + + o2Ptr = Tcl_CopyObjectInstance(interp, oPtr, name, namespaceName); Tcl_DStringFree(&buffer); } if (o2Ptr == NULL) { return TCL_ERROR; Index: tests/oo.test ================================================================== --- tests/oo.test +++ tests/oo.test @@ -2011,10 +2011,45 @@ } lappend result [$obj1 get] [$obj2 get] } -cleanup { FooClass destroy } -result {foo bar grill bar} +test oo-15.11 {OO: object cloning} -returnCodes error -body { + oo::copy +} -result {wrong # args: should be "oo::copy sourceName ?targetName? ?targetNamespace?"} +test oo-15.12 {OO: object cloning with target NS} -setup { + oo::class create Super + oo::class create Cls {superclass Super} +} -body { + namespace eval ::existing {} + oo::copy Cls {} ::existing +} -returnCodes error -cleanup { + Super destroy + catch {namespace delete ::existing} +} -result {::existing refers to an existing namespace} +test oo-15.13 {OO: object cloning with target NS} -setup { + oo::class create Super + oo::class create Cls {superclass Super} +} -body { + list [namespace exist ::dupens] [oo::copy Cls Cls2 ::dupens] [namespace exist ::dupens] +} -cleanup { + Super destroy +} -result {0 ::Cls2 1} +test oo-15.14 {OO: object cloning with target NS} -setup { + oo::class create Cls {export eval} + set result {} +} -body { + Cls create obj + obj eval { + proc test-15.14 {} {} + } + lappend result [info commands ::dupens::t*] + oo::copy obj obj2 ::dupens + lappend result [info commands ::dupens::t*] +} -cleanup { + Cls destroy +} -result {{} ::dupens::test-15.14} test oo-16.1 {OO: object introspection} -body { info object } -returnCodes 1 -result "wrong \# args: should be \"info object subcommand ?arg ...?\"" test oo-16.1.1 {OO: object introspection} -body {