com.jniwrapper
Class Callback

java.lang.Object
  |
  +--com.jniwrapper.Parameter
        |
        +--com.jniwrapper.Callback

public abstract class Callback
extends Parameter

Callback is the superclass for all classes representing callback functions. A class nested from Callback must override method callback().

Example of usage:

  final public class CMultiply extends Callback
  {
    public Int a = new Int();
    public Int b = new Int();
    public Int retVal = new Int();
    public CMultiply()
    {
      init(new CParameter[] {a, b}, retVal);
    }
    public void callback()
    {
      retVal.setValue(a.getValue()*b.getValue());
    }
  }

  void myMethod()
  {
     Function.call("userLib", "userFunc", null, new CMultiply());
  }
 
Expected native code usage is:
  typedef int CMultiply(int a, int b);

  void userFunc(CMultiply *func)
  {
     int val = func(2, 2);
  }
 

NOTE: current implementation of callback is NOT thread-safe.


Constructor Summary
protected Callback()
          Constructs a callback instance.
  Callback(Parameter[] params, Parameter retVal)
          Constructs a callback instance.
 
Method Summary
abstract  void callback()
          This method is called when callback function is called.
 java.lang.Object clone()
          Cloning is not supported by callbacks.
 void dispose()
          Release resources associated with this callback.
 byte getCallingConvention()
          Returns callback calling convention.
 int getLength()
          Returns the length of native side parameter required to represent this callback.
protected  Parameter[] getParameters()
           
protected  Parameter getReturnValue()
           
protected  void init(Parameter[] params, Parameter retVal)
          Initializes callback parameters.
 void read(DataBuffer stackBuffer, int offset, boolean invokedByCallback)
          Does nothing.
 void setCallingConvention(byte callingConvention)
          Sets calling convention that is used to invoke this callback.
 java.lang.String toString()
           
 void write(DataBuffer stackBuffer, int offset, boolean invokedByCallback)
          Writes pointer to the native callback function that redirects to this callback object.
 
Methods inherited from class com.jniwrapper.Parameter
acceptIOPerformer, asReturnValue, equals, getAlignedLength, getAlignmentRequirement, getDataBuffer, getDataBufferOffset, indent, pop, push, read, setDataBuffer, toByteArray, write
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Callback

protected Callback()
Constructs a callback instance. If a subclass uses this method is used to construct a callback, it must then invoke init() method to initialize callback arguments/return.


Callback

public Callback(Parameter[] params,
                Parameter retVal)
Constructs a callback instance.

Parameters:
params - callback function arguments
retVal - callback function return value
Method Detail

init

protected void init(Parameter[] params,
                    Parameter retVal)
Initializes callback parameters.

Parameters:
params - callback function arguments
retVal - callback function return value

getParameters

protected Parameter[] getParameters()

getReturnValue

protected Parameter getReturnValue()

callback

public abstract void callback()
This method is called when callback function is called.

Parameters of function are stored in variables specified during callback initialization. Overriden method must store return value to the return value variable specified during initialization.


getLength

public int getLength()
Returns the length of native side parameter required to represent this callback. Usually equals to the length of a pointer.

Specified by:
getLength in class Parameter

clone

public java.lang.Object clone()
Cloning is not supported by callbacks. This method throws an UnsupportedOperationException.

Specified by:
clone in class Parameter
Throws:
java.lang.UnsupportedOperationException - always

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

dispose

public void dispose()
Release resources associated with this callback. This has to be done explicitly when the callback is no longer in use. Callback objects will not be GCed until this method is called.


write

public void write(DataBuffer stackBuffer,
                  int offset,
                  boolean invokedByCallback)
           throws MemoryAccessViolationException
Writes pointer to the native callback function that redirects to this callback object.

Specified by:
write in class Parameter
MemoryAccessViolationException

read

public void read(DataBuffer stackBuffer,
                 int offset,
                 boolean invokedByCallback)
          throws MemoryAccessViolationException
Does nothing.

Specified by:
read in class Parameter
MemoryAccessViolationException

setCallingConvention

public void setCallingConvention(byte callingConvention)
Sets calling convention that is used to invoke this callback.


getCallingConvention

public byte getCallingConvention()
Returns callback calling convention.