com.jniwrapper
Class Callback

java.lang.Object
  extended by com.jniwrapper.Parameter
      extended by com.jniwrapper.Callback
All Implemented Interfaces:
PointerParameter

public abstract class Callback
extends Parameter
implements PointerParameter

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

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:The current implementation of callback is NOT thread-safe.


Field Summary
 
Fields inherited from class com.jniwrapper.Parameter
LICENSE_TYPE, V
 
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.
protected  void callback(Parameter[] parameters, Parameter returnValue)
           
 java.lang.Object clone()
          Cloning is not supported by callbacks.
protected  Parameter createReturnValueByFields()
           
 void dispose()
          Releases resources associated with this callback.
 byte getCallingConvention()
          Returns callback calling convention.
 java.lang.String getDebugInfo()
          Return debug info for JNIWrapper parameter.
 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.
 boolean isNull()
          Tests if this pointer is null.
protected  boolean isUseFieldsForCallbackParameters()
           
 void read(DataBuffer stackBuffer, int offset, boolean invokedByCallback)
          Does nothing.
protected  void restoreState()
           
protected  void saveState()
           
 void setCallingConvention(byte callingConvention)
          Sets calling convention that is used to invoke this callback.
protected  void setUseFieldsForCallbackParameters(boolean useFieldsForCallbackParameters)
           
 Pointer.Void toPointer()
          Converts this callback to void pointer.
 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, dataBufferAssigned, equals, getAlignedLength, getAlignmentRequirement, getDataBuffer, getDataBufferOffset, hashCode, indent, pop, push, read, read, setDataBuffer, toByteArray, write
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Callback

protected Callback()
Constructs a callback instance. If a subclass uses this method to construct a callback, it must then invoke the 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

isUseFieldsForCallbackParameters

protected boolean isUseFieldsForCallbackParameters()

setUseFieldsForCallbackParameters

protected final void setUseFieldsForCallbackParameters(boolean useFieldsForCallbackParameters)

init

protected final 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 the function are stored in variables specified during callback initialization. Overriden method must store return value to the return value variable specified during initialization.


callback

protected void callback(Parameter[] parameters,
                        Parameter returnValue)

createReturnValueByFields

protected Parameter createReturnValueByFields()

saveState

protected void saveState()

restoreState

protected void restoreState()

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 the UnsupportedOperationException.

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

dispose

public void dispose()
Releases 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
Throws:
MemoryAccessViolationException

read

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

Specified by:
read in class Parameter
Throws:
MemoryAccessViolationException

setCallingConvention

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


getCallingConvention

public byte getCallingConvention()
Returns callback calling convention.


getDebugInfo

public java.lang.String getDebugInfo()
Description copied from class: Parameter
Return debug info for JNIWrapper parameter.

Specified by:
getDebugInfo in class Parameter
Returns:
debug info

toPointer

public Pointer.Void toPointer()
Converts this callback to void pointer.

Returns:
void pointer to this callback

isNull

public boolean isNull()
Description copied from interface: PointerParameter
Tests if this pointer is null.

Specified by:
isNull in interface PointerParameter
Returns:
true if this pointer is null; false otherwise