Generic method handler

<< Click to Display Table of Contents >>

Navigation:  Using Delphi classes and functions >

Generic method handler

Generic method handlers are intended for processing several class methods or properties in one handler. When class has registered generic method handler, all calls to class methods or object instance methods and properties are first processed by generic handler.

For example it can be used for implementing SOAP/REST client class where all methods calls will be passed directly to a server.

To register generic method handlers, declare procedure of the following type:

 

function(const Sender: TScriptParser; const Instance: TObject;
  const MethodName: stringvar Params: TScriptStack; StackTop, 

  AParamCount: integer; IsSetter: boolean; var Res: Variant): boolean;

 

and set GenericHandler property of the class:

 

  HtScriptGlobal.RegisterClass('create()', @TTestClass.Create, 

    TTestClass).GenericHandler := MyGeneric;

 

When IsSetter is true, method is property setter and new value is passed in Res variable, in other cases method should return value in Res.

Handler should return true if method is processed successfully, otherwise standard method/property processing will be used.

Generic handler will be used even when it is registered to object class ancestor, but only first found handler will be executed. For example:

 

type
  C1 = class;
  C2 = class(C1);
  C3 = class(C2);

 

if generic handlers are registered for C1 and C2 and instance is of class C3, only handler for C2 will be called.

For class methods (including constructor) Instance parameter contains nil.