Next: , Previous: , Up: Classes   [Index]


Method

Method Class

The 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);
  }
}

Instance Variables

methodName

A String object that contains the method’s name.

methodSelector

A String object that contains the name of the method’s selector.

returnClass

A String object that contains the name of the method’s return class.

rcvrClassObject

A Symbol that refers to the method’s receiver class object.

methodFn

A Symbol that contains the address of the method’s function call.

methodSource

A String that contains the method’s source code.

methodParameters

A List that contains the method’s paramter definitions.

nParams

An Integer that contains the number of parameters the method requires.

varargs

An Integer that is either TRUE or FALSE depending on whether the method takes a variable number of arguments.

nArgs

An Integer that contains the number of arguments the method is called with.

errorLine

An Integer that contains the first line of the method in a class library or program input.

errorColumn

An Integer that contains the first column of the method in a class library or program input.

argFrameTop

An Integer that contains the stack index of the method’s first argument.

rcvrFrameTop

An Integer that contains the stack index of the method’s receiver.

imported

An Integer that is either TRUE or FALSE depending on whether the method is imported from a class library.

queued

An Integer that is either TRUE or FALSE depending on whether a method is queued for output.

methodArgs

A List of references to the method’s arguments.

localObjects

A List of references to the method’s local objects.

userObjects

A List of references to objects created during the method’s execution.

localCVARs

A List of references to the method’s local C variables.

isInitialized

A 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).

Instance Methods

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.


Next: , Previous: , Up: Classes   [Index]