summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/my.n60
-rw-r--r--generic/tclOO.c34
-rw-r--r--generic/tclOOScript.h2
3 files changed, 72 insertions, 24 deletions
diff --git a/doc/my.n b/doc/my.n
index 26d861a..262186f 100644
--- a/doc/my.n
+++ b/doc/my.n
@@ -9,30 +9,45 @@
.BS
'\" Note: do not modify the .SH NAME line immediately below!
.SH NAME
-my \- invoke any method of current object
+my, myclass \- invoke any method of current object or its class
.SH SYNOPSIS
.nf
package require TclOO
\fBmy\fI methodName\fR ?\fIarg ...\fR?
+\fBmyclass\fI methodName\fR ?\fIarg ...\fR?
.fi
.BE
.SH DESCRIPTION
.PP
The \fBmy\fR command is used to allow methods of objects to invoke methods
-of the object (or its class). In particular, the set of valid values for
+of the object (or its class),
+.VS TIP478
+and he \fBmyclass\fR command is used to allow methods of objects to invoke
+methods of the current class of the object \fIas an object\fR.
+.VE TIP478
+In particular, the set of valid values for
\fImethodName\fR is the set of all methods supported by an object and its
superclasses, including those that are not exported
.VS TIP500
and private methods of the object or class when used within another method
defined by that object or class.
.VE TIP500
-The object upon which the method is invoked is the one that owns the namespace
-that the \fBmy\fR command is contained in initially (\fBNB:\fR the link
+.PP
+The object upon which the method is invoked via \fBmy\fR is the one that owns
+the namespace that the \fBmy\fR command is contained in initially (\fBNB:\fR the link
remains if the command is renamed), which is the currently invoked object by
default.
+.VS TIP478
+Similarly, the object on which the method is invoked via \fBmyclass\fR is the
+object that is the current class of the object that owns the namespace that
+the \fBmyclass\fR command is contained in initially. As with \fBmy\fR, the
+link remains even if the command is renamed into another namespace, and
+defaults to being the manufacturing class of the current object.
+.VE TIP478
.PP
-Each object has its own \fBmy\fR command, contained in its instance namespace.
+Each object has its own \fBmy\fR and \fBmyclass\fR commands, contained in its
+instance namespace.
.SH EXAMPLES
.PP
This example shows basic use of \fBmy\fR to use the \fBvariables\fR method of
@@ -54,7 +69,8 @@ o count \fI\(-> prints "3"\fR
.PP
This example shows how you can use \fBmy\fR to make callbacks to private
methods from outside the object (from a \fBtrace\fR), using
-\fBnamespace code\fR to enter the correct context:
+\fBnamespace code\fR to enter the correct context. (See the \fBcallback\fR
+command for the recommended way of doing this.)
.PP
.CS
oo::class create HasCallback {
@@ -73,6 +89,38 @@ set o [HasCallback new]
trace add variable xyz write [$o makeCallback]
set xyz "called" \fI\(-> prints "callback: xyz {} write"\fR
.CE
+.PP
+.VS TIP478
+This example shows how to access a private method of a class from an instance
+of that class. (See the \fBclassmethod\fR declaration in \fBoo::define\fR for
+a higher level interface for doing this.)
+.PP
+.CS
+oo::class create CountedSteps {
+ self {
+ variable count
+ method Count {} {
+ return [incr count]
+ }
+ }
+ method advanceTwice {} {
+ puts "in [self] step A: [\fBmyclass\fR Count]"
+ puts "in [self] step B: [\fBmyclass\fR Count]"
+ }
+}
+
+CountedSteps create x
+CountedSteps create y
+x advanceTwice \fI\(-> prints "in ::x step A: 1"\fR
+ \fI\(-> prints "in ::x step B: 2"\fR
+y advanceTwice \fI\(-> prints "in ::y step A: 3"\fR
+ \fI\(-> prints "in ::y step B: 4"\fR
+x advanceTwice \fI\(-> prints "in ::x step A: 5"\fR
+ \fI\(-> prints "in ::x step B: 6"\fR
+y advanceTwice \fI\(-> prints "in ::y step A: 7"\fR
+ \fI\(-> prints "in ::y step B: 8"\fR
+.CE
+.VE TIP478
.SH "SEE ALSO"
next(n), oo::object(n), self(n)
.SH KEYWORDS
diff --git a/generic/tclOO.c b/generic/tclOO.c
index 8229dc1..630e977 100644
--- a/generic/tclOO.c
+++ b/generic/tclOO.c
@@ -108,7 +108,7 @@ static int MyClassObjCmd(ClientData clientData,
static int MyClassNRObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const *objv);
-static void MyClassDeletedCmd(ClientData clientData);
+static void MyClassDeleted(ClientData clientData);
/*
* Methods in the oo::object and oo::class classes. First, we define a helper
@@ -754,9 +754,9 @@ AllocObject(
oPtr->myCommand = TclNRCreateCommandInNs(interp, "my", oPtr->namespacePtr,
PrivateObjectCmd, PrivateNRObjectCmd, oPtr, MyDeleted);
- oPtr->myclassCommand = TclNRCreateCommandInNs(interp, " :my:class",
+ oPtr->myclassCommand = TclNRCreateCommandInNs(interp, "myclass",
oPtr->namespacePtr, MyClassObjCmd, MyClassNRObjCmd, oPtr,
- MyClassDeletedCmd);
+ MyClassDeleted);
return oPtr;
}
@@ -784,12 +784,12 @@ SquelchCachedName(
/*
* ----------------------------------------------------------------------
*
- * MyDeleted --
+ * MyDeleted, MyClassDeleted --
*
- * This callback is triggered when the object's [my] command is deleted
- * by any mechanism. It just marks the object as not having a [my]
- * command, and so prevents cleanup of that when the object itself is
- * deleted.
+ * These callbacks are triggered when the object's [my] or [myclass]
+ * commands are deleted by any mechanism. They just mark the object as
+ * not having a [my] command or [myclass] command, and so prevent cleanup
+ * of those commands when the object itself is deleted.
*
* ----------------------------------------------------------------------
*/
@@ -803,6 +803,14 @@ MyDeleted(
oPtr->myCommand = NULL;
}
+
+static void
+MyClassDeleted(
+ ClientData clientData)
+{
+ Object *oPtr = clientData;
+ oPtr->myclassCommand = NULL;
+}
/*
* ----------------------------------------------------------------------
@@ -2501,7 +2509,7 @@ TclOOInvokeObject(
/*
* ----------------------------------------------------------------------
*
- * MyClassObjCmd, MyClassNRObjCmd, MyClassDeletedCmd --
+ * MyClassObjCmd, MyClassNRObjCmd --
*
* Special trap door to allow an object to delegate simply to its class.
*
@@ -2534,14 +2542,6 @@ MyClassNRObjCmd(
return TclOOObjectCmdCore(oPtr->selfCls->thisPtr, interp, objc, objv, 0,
NULL);
}
-
-static void
-MyClassDeletedCmd(
- ClientData clientData)
-{
- Object *oPtr = clientData;
- oPtr->myclassCommand = NULL;
-}
/*
* ----------------------------------------------------------------------
diff --git a/generic/tclOOScript.h b/generic/tclOOScript.h
index d96ee76..4b58337 100644
--- a/generic/tclOOScript.h
+++ b/generic/tclOOScript.h
@@ -85,7 +85,7 @@ static const char *tclOOSetupScript =
" ::oo::define [::oo::DelegateName $cls] method $name $args $body\n"
" }\n"
" # Make the connection by forwarding\n"
-" tailcall forward $name { :my:class} $name\n"
+" tailcall forward $name myclass $name\n"
"}\n"
"proc ::oo::MixinClassDelegates {class} {\n"