| 1 |
'\" |
| 2 |
'\" Copyright (c) 2007 Donal K. Fellows |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH Tcl_Method 3 0.1 TclOO "TclOO Library Functions" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
Tcl_ClassSetConstructor, Tcl_ClassSetDestructor, Tcl_MethodDeclarerClass, Tcl_MethodDeclarerObject, Tcl_MethodIsPublic, Tcl_MethodIsType, Tcl_MethodName, Tcl_NewInstanceMethod, Tcl_NewMethod, Tcl_ObjectContextInvokeNext, Tcl_ObjectContextIsFiltering, Tcl_ObjectContextMethod, Tcl_ObjectContextObject, Tcl_ObjectContextSkippedArgs \- manipulate methods and method-call contexts |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tclOO.h>\fR |
| 16 |
.sp |
| 17 |
Tcl_Method |
| 18 |
\fBTcl_NewMethod\fR(\fIinterp, class, nameObj, isPublic, |
| 19 |
methodTypePtr, clientData\fR) |
| 20 |
.sp |
| 21 |
Tcl_Method |
| 22 |
\fBTcl_NewInstanceMethod\fR(\fIinterp, object, nameObj, isPublic, |
| 23 |
methodTypePtr, clientData\fR) |
| 24 |
.sp |
| 25 |
\fBTcl_ClassSetConstructor\fR(\fIinterp, class, method\fR) |
| 26 |
.sp |
| 27 |
\fBTcl_ClassSetDestructor\fR(\fIinterp, class, method\fR) |
| 28 |
.sp |
| 29 |
Tcl_Class |
| 30 |
\fBTcl_MethodDeclarerClass\fR(\fImethod\fR) |
| 31 |
.sp |
| 32 |
Tcl_Object |
| 33 |
\fBTcl_MethodDeclarerObject\fR(\fImethod\fR) |
| 34 |
.sp |
| 35 |
Tcl_Obj * |
| 36 |
\fBTcl_MethodName\fR(\fImethod\fR) |
| 37 |
.sp |
| 38 |
int |
| 39 |
\fBTcl_MethodIsPublic\fR(\fImethod\fR) |
| 40 |
.sp |
| 41 |
int |
| 42 |
\fBTcl_MethodIsType\fR(\fImethod, methodTypePtr, clientDataPtr\fR) |
| 43 |
.sp |
| 44 |
int |
| 45 |
\fBTcl_ObjectContextInvokeNext\fR(\fIinterp, context, objc, objv, skip\fR) |
| 46 |
.sp |
| 47 |
int |
| 48 |
\fBTcl_ObjectContextIsFiltering\fR(\fIcontext\fR) |
| 49 |
.sp |
| 50 |
Tcl_Method |
| 51 |
\fBTcl_ObjectContextMethod\fR(\fIcontext\fR) |
| 52 |
.sp |
| 53 |
Tcl_Object |
| 54 |
\fBTcl_ObjectContextObject\fR(\fIcontext\fR) |
| 55 |
.sp |
| 56 |
int |
| 57 |
\fBTcl_ObjectContextSkippedArgs\fR(\fIcontext\fR) |
| 58 |
.SH ARGUMENTS |
| 59 |
.AS ClientData clientData in |
| 60 |
.AP Tcl_Interp *interp in/out |
| 61 |
The interpreter holding the object or class to create or update a method in. |
| 62 |
.AP Tcl_Object object in |
| 63 |
The object to create the method in. |
| 64 |
.AP Tcl_Class class in |
| 65 |
The class to create the method in. |
| 66 |
.AP Tcl_Obj *nameObj in |
| 67 |
The name of the method to create. Should not be NULL unless creating |
| 68 |
constructors or destructors. |
| 69 |
.AP int isPublic in |
| 70 |
A flag saying what the visibility of the method is. The only supported public |
| 71 |
values of this flag are 0 for a non-exported method, and 1 for an exported |
| 72 |
method. |
| 73 |
.AP Tcl_MethodType *methodTypePtr in |
| 74 |
A description of the type of the method to create, or the type of method to |
| 75 |
compare against. |
| 76 |
.AP ClientData clientData in |
| 77 |
A piece of data that is passed to the implementation of the method without |
| 78 |
interpretation. |
| 79 |
.AP ClientData *clientDataPtr out |
| 80 |
A pointer to a variable in which to write the \fIclientData\fR value supplied |
| 81 |
when the method was created. If NULL, the \fIclientData\fR value will not be |
| 82 |
retrieved. |
| 83 |
.AP Tcl_Method method in |
| 84 |
A reference to a method to query. |
| 85 |
.AP Tcl_ObjectContext context in |
| 86 |
A reference to a method-call context. Note that client code \fImust not\fR |
| 87 |
retain a reference to a context. |
| 88 |
.AP int objc in |
| 89 |
The number of arguments to pass to the method implementation. |
| 90 |
.AP "Tcl_Obj *const" *objv in |
| 91 |
An array of arguments to pass to the method implementation. |
| 92 |
.AP int skip in |
| 93 |
The number of arguments passed to the method implementation that do not |
| 94 |
represent "real" arguments. |
| 95 |
.BE |
| 96 |
.SH DESCRIPTION |
| 97 |
.PP |
| 98 |
A method is an operation carried out on an object that is associated with the |
| 99 |
object. Every method must be attached to either an object or a class; methods |
| 100 |
attached to a class are associated with all instances (direct and indirect) of |
| 101 |
that class. |
| 102 |
.PP |
| 103 |
Given a method, the entity that declared it can be found using |
| 104 |
\fBTcl_MethodDeclarerClass\fR which returns the class that the method is |
| 105 |
attached to (or NULL if the method is not attached to any class) and |
| 106 |
\fBTcl_MethodDeclarerObject\fR which returns the object that the method is |
| 107 |
attached to (or NULL if the method is not attached to an object). The name of |
| 108 |
the method can be retrieved with \fBTcl_MethodName\fR and whether the method |
| 109 |
is exported is retrieved with \fBTcl_MethodIsPublic\fR. The type of the method |
| 110 |
can also be introspected upon to a limited degree; the function |
| 111 |
\fBTcl_MethodIsType\fR returns whether a method is of a particular type, |
| 112 |
assigning the per-method \fIclientData\fR to the variable pointed to by |
| 113 |
\fIclientDataPtr\fR if (that is non-NULL) if the type is matched. |
| 114 |
.SS "METHOD CREATION" |
| 115 |
.PP |
| 116 |
Methods are created by \fBTcl_NewMethod\fR and \fBTcl_NewInstanceMethod\fR, |
| 117 |
which |
| 118 |
create a method attached to a class or an object respectively. In both cases, |
| 119 |
the \fInameObj\fR argument gives the name of the method to create, the |
| 120 |
\fIisPublic\fR argument states whether the method should be exported |
| 121 |
initially, the \fImethodTypePtr\fR argument describes the implementation of |
| 122 |
the method (see the \fBMETHOD TYPES\fR section below) and the \fIclientData\fR |
| 123 |
argument gives some implementation-specific data that is passed on to the |
| 124 |
implementation of the method when it is called. |
| 125 |
.PP |
| 126 |
When the \fInameObj\fR argument to \fBTcl_NewMethod\fR is NULL, an |
| 127 |
unnamed method is created, which is used for constructors and destructors. |
| 128 |
Constructors should be installed into their class using the |
| 129 |
\fBTcl_ClassSetConstructor\fR function, and destructors (which must not |
| 130 |
require any arguments) should be installed into their class using the |
| 131 |
\fBTcl_ClassSetDestructor\fR function. Unnamed methods should not be used for |
| 132 |
any other purpose, and named methods should not be used as either constructors |
| 133 |
or destructors. Also note that a NULL \fImethodTypePtr\fR is used to provide |
| 134 |
internal signaling, and should not be used in client code. |
| 135 |
.SS "METHOD CALL CONTEXTS" |
| 136 |
.PP |
| 137 |
When a method is called, a method-call context reference is passed in as one |
| 138 |
of the arguments to the implementation function. This context can be inspected |
| 139 |
to provide information about the caller, but should not be retained beyond the |
| 140 |
moment when the method call terminates. |
| 141 |
.PP |
| 142 |
The method that is being called can be retrieved from the context by using |
| 143 |
\fBTcl_ObjectContextMethod\fR, and the object that caused the method to be |
| 144 |
invoked can be retrieved with \fBTcl_ObjectContextObject\fR. The number of |
| 145 |
arguments that are to be skipped (e.g. the object name and method name in a |
| 146 |
normal method call) is read with \fBTcl_ObjectContextSkippedArgs\fR, and the |
| 147 |
context can also report whether it is working as a filter for another method |
| 148 |
through \fBTcl_ObjectContextIsFiltering\fR. |
| 149 |
.PP |
| 150 |
During the execution of a method, the method implementation may choose to |
| 151 |
invoke the stages of the method call chain that come after the current method |
| 152 |
implementation. This (the core of the \fBnext\fR command) is done using |
| 153 |
\fBTcl_ObjectContextInvokeNext\fR. Note that this function does not manipulate |
| 154 |
the call-frame stack, unlike the \fBnext\fR command; if the method |
| 155 |
implementation has pushed one or more extra frames on the stack as part of its |
| 156 |
implementation, it is also responsible for temporarily popping those frames |
| 157 |
from the stack while the \fBTcl_ObjectContextInvokeNext\fR function is |
| 158 |
executing. Note also that the method-call context is \fInever\fR deleted |
| 159 |
during the execution of this function. |
| 160 |
.SH "METHOD TYPES" |
| 161 |
.PP |
| 162 |
The types of methods are described by a pointer to a Tcl_MethodType structure, |
| 163 |
which is defined as: |
| 164 |
.PP |
| 165 |
.CS |
| 166 |
typedef struct { |
| 167 |
int \fIversion\fR; |
| 168 |
const char *\fIname\fR; |
| 169 |
Tcl_MethodCallProc *\fIcallProc\fR; |
| 170 |
Tcl_MethodDeleteProc *\fIdeleteProc\fR; |
| 171 |
Tcl_CloneProc *\fIcloneProc\fR; |
| 172 |
} \fBTcl_MethodType\fR; |
| 173 |
.CE |
| 174 |
.PP |
| 175 |
The \fIversion\fR field allows for future expansion of the structure, and |
| 176 |
should always be declared equal to TCL_OO_METHOD_VERSION_CURRENT. The |
| 177 |
\fIname\fR field provides a human-readable name for the type, and is the value |
| 178 |
that is exposed via the \fBinfo class methodtype\fR and |
| 179 |
\fBinfo object methodtype\fR Tcl commands. |
| 180 |
.PP |
| 181 |
The \fIcallProc\fR field gives a function that is called when the method is |
| 182 |
invoked; it must never be NULL. |
| 183 |
.PP |
| 184 |
The \fIdeleteProc\fR field gives a function that is used to delete a |
| 185 |
particular method, and is called when the method is replaced or removed; if |
| 186 |
the field is NULL, it is assumed that the method's \fIclientData\fR needs no |
| 187 |
special action to delete. |
| 188 |
.PP |
| 189 |
The \fIcloneProc\fR field is either a function that is used to copy a method's |
| 190 |
\fIclientData\fR (as part of \fBTcl_CopyObjectInstance\fR) or NULL to indicate |
| 191 |
that the \fIclientData\fR can just be copied directly. |
| 192 |
.SS "TCL_METHODCALLPROC FUNCTION SIGNATURE" |
| 193 |
.PP |
| 194 |
Functions matching this signature are called when the method is invoked. |
| 195 |
.PP |
| 196 |
.CS |
| 197 |
typedef int \fBTcl_MethodCallProc\fR( |
| 198 |
ClientData \fIclientData\fR, |
| 199 |
Tcl_Interp *\fIinterp\fR, |
| 200 |
Tcl_ObjectContext \fIobjectContext\fR, |
| 201 |
int \fIobjc\fR, |
| 202 |
Tcl_Obj *const *\fIobjv\fR); |
| 203 |
.CE |
| 204 |
.PP |
| 205 |
The \fIclientData\fR argument to a Tcl_MethodCallProc is the value that was |
| 206 |
given when the method was created, the \fIinterp\fR is a place in which to |
| 207 |
execute scripts and access variables as well as being where to put the result |
| 208 |
of the method, and the \fIobjc\fR and \fIobjv\fR fields give the parameter |
| 209 |
objects to the method. The calling context of the method can be discovered |
| 210 |
through the \fIobjectContext\fR argument, and the return value from a |
| 211 |
Tcl_MethodCallProc is any Tcl return code (e.g. TCL_OK, TCL_ERROR). |
| 212 |
.SS "TCL_METHODDELETEPROC FUNCTION SIGNATURE" |
| 213 |
.PP |
| 214 |
Functions matching this signature are used when a method is deleted, whether |
| 215 |
through a new method being created or because the object or class is deleted. |
| 216 |
.PP |
| 217 |
.CS |
| 218 |
typedef void \fBTcl_MethodDeleteProc\fR( |
| 219 |
ClientData \fIclientData\fR); |
| 220 |
.CE |
| 221 |
.PP |
| 222 |
The \fIclientData\fR argument to a Tcl_MethodDeleteProc will be the same as |
| 223 |
the value passed to the \fIclientData\fR argument to \fBTcl_NewMethod\fR or |
| 224 |
\fBTcl_NewInstanceMethod\fR when the method was created. |
| 225 |
.SS "TCL_CLONEPROC FUNCTION SIGNATURE" |
| 226 |
.PP |
| 227 |
Functions matching this signature are used to copy a method when the object or |
| 228 |
class is copied using \fBTcl_CopyObjectInstance\fR (or \fBoo::copy\fR). |
| 229 |
.PP |
| 230 |
.CS |
| 231 |
typedef int \fBTcl_CloneProc\fR( |
| 232 |
Tcl_Interp *\fIinterp\fR, |
| 233 |
ClientData \fIoldClientData\fR, |
| 234 |
ClientData *\fInewClientDataPtr\fR); |
| 235 |
.CE |
| 236 |
.PP |
| 237 |
The \fIinterp\fR argument gives a place to write an error message when the |
| 238 |
attempt to clone the object is to fail, in which case the clone procedure must |
| 239 |
also return TCL_ERROR; it should return TCL_OK otherwise. |
| 240 |
The \fIoldClientData\fR field to a Tcl_CloneProc gives the value from the |
| 241 |
method being copied from, and the \fInewClientDataPtr\fR field will point to |
| 242 |
a variable in which to write the value for the method being copied to. |
| 243 |
.SH "SEE ALSO" |
| 244 |
Class(3), oo::class(n), oo::define(n), oo::object(n) |
| 245 |
.SH KEYWORDS |
| 246 |
constructor, method, object |
| 247 |
|
| 248 |
.\" Local variables: |
| 249 |
.\" mode: nroff |
| 250 |
.\" fill-column: 78 |
| 251 |
.\" End: |