Method ClassThe Method class provides a virtual mechanism for objects to
maintain references to methods and also to call the methods.
A Method instance variables can contain all of the
data of an actual method. Much of the method’s instance
data is used only by the Ctalk front end, and the instance
data may or may not be needed at run time when a
Method object is created or used. This class defines
the instance variables anyway in case a program needs them.
Note that defining a Method object does not include
the method receiver’s class library. The program must
already have included a method’s receiver class, either by
constructing an object of that class, or by including the
class with the require keyword.
Note also that, in this version, Method object calls
are only tested with a receiver, so you may want to use a
virtual method as part of an expression, you might want to
write a wrapper method for it, as in this example. The
methodObjectMessage method is defined in class
Object. See Object.
String instanceMethod selfConcat (String __arg) {
self = self + __arg;
return NULL;
}
int main () {
Method new m;
String new s;
Exception new e;
s = "Hello,";
m definedInstanceMethod "String", "selfConcat";
m withArg " world!";
s methodObjectMessage m; /* methodObjectMessage is defined */
/* in Object class. */
if (e pending) {
e handle;
} else {
printf ("%s\n", s);
}
}
methodNamemethodSelectorA String object that contains the name of the method’s
selector.
returnClassA String object that contains the name of the method’s return
class.
rcvrClassObjectmethodFnA Symbol that contains the address of the method’s function
call.
methodSourcemethodParametersnParamsAn Integer that contains the number of parameters the method
requires.
varargsAn Integer that is either TRUE or FALSE depending
on whether the method takes a variable number of arguments.
nArgsAn Integer that contains the number of arguments the method is
called with.
errorLineAn Integer that contains the first line of the method in a
class library or program input.
errorColumnAn Integer that contains the first column of the method in a
class library or program input.
argFrameTopAn Integer that contains the stack index of the method’s first
argument.
rcvrFrameTopAn Integer that contains the stack index of the method’s receiver.
importedAn Integer that is either TRUE or FALSE depending
on whether the method is imported from a class library.
queuedAn Integer that is either TRUE or FALSE depending
on whether a method is queued for output.
methodArgslocalObjectsuserObjectsA List of references to objects created during the method’s
execution.
localCVARsisInitializedA Boolean that is True only if the Method object
has been initialized to refer to an actual method. This variable
should only be set by the methods definedInstanceMethod and
definedClassMethod (in Object class).
definedClassMethod (String classname, String name)Initialize the receiver with the class method named by the arguments.
definedInstanceMethod (String classname, String name)Initialize the receiver with the instance method named by the arguments.
withArg (Object method_argument)Define an argument for the receiver method.
This method normally precedes a methodObjectMessage call. For
examples of its use, refer to the methodObjectMessage section.
See methodObjectMessage.
setCallbackName (String name)Sets the receiver object’s name to the name of the callback, which is generally set when the program is compiled. This does not change the name of the actual method, only the name by which the Method object that represents it is referred to.
Here is a slightly hypothetical example of the steps that use
setCallbackName to set up a callback method.
/*
* 1. In MyClass, the callback here is defined as an instance
* variable.
*/
MyClass instanceVariable myCallback Method NULL;
...
/*
* 2. Also in MyClass, define a method to configure the callback.
*/
MyClass instanceMethod onEvent (String methodClassName,
String methodName) {
Method new callbackMethod;
callbackMethod definedInstanceMethod methodClassName, methodName;
/* This sets the class and name of callbackMethod to the name
of the callback instance variable defined above, at the
start of the class. */
callbackMethod setCallbackName "myCallback";
self addInstanceVariable "myCallback", callbackMethod;
}
/*
* 3. In the program's source file, define the callback method
* itself.
*/
MyClass instanceMethod callbackMethod (void) {
... do something ...
}
...
/*
* 4. Also in the program's source file, set up the callback
* method during program initialization.
*/
int main () {
MyClass new myProgram;
...
myProgram onEvent "MyClass", "callbackMethod";
...
}
For a working example, refer to the class GLXCanvasPane, which
uses this process to assign callbacks.