com.smardec.j2native
Class Union

java.lang.Object
  |
  +--com.smardec.j2native.Argument
        |
        +--com.smardec.j2native.Union
All Implemented Interfaces:
java.lang.Cloneable

public abstract class Union
extends Argument

The Union class represents a C union type. Function arguments of this type are initialized with several parameters representing the union members. The length of the union is equal to that of its longest member. A union represents only one of its members at a time. Users of this class should define which member the union represents by calling one of the setActiveMember methods.


Field Summary
 
Fields inherited from class com.smardec.j2native.Argument
JAVA_SIDE, NATIVE_SIDE
 
Constructor Summary
protected Union()
          Preconstructs a union.
protected Union(Argument[] members)
          Constructs a union with a given set of members.
 
Method Summary
 void fromBytesValue(byte[] bytes, int offset)
          Restores itself from provided array of bytes.
 Argument getActiveMember()
          Returns currently active member.
 int getLength()
          Returns length in bytes.
 Argument[] getMembers()
          Returns union members.
protected  void initialize(Argument[] members)
          Initialize union and sets union members.
protected  boolean isFixedLength()
          Identify if Argument is a fixed length variable.
protected  void read(long handle, int offset)
          Reads itself from the memory.
protected  void readFromStack(byte[] stack, int offset)
          Read itself from stack.
 void setActiveMember(Argument active)
          Defines the member of this union that is currently used.
 void setActiveMember(Argument active, boolean update)
          Defines the member of this union that is currently used.
 void setActiveMember(int index)
          Selects a new active member of the union by its index in the array passed on the initialization.
 void setActiveMember(int index, boolean update)
          Selects a new active member of the union by its index in the array passed on the initialization.
 byte[] toBytesValue()
          Encodes itself and returns it's representation as array of bytes.
protected  void toBytesValue(byte[] bytes, int offset)
          Encodes itself to the provided array of bytes.
protected  void update(byte side)
          This method is used to synchronize Java side and native part.
protected  void write(long handle, int offset)
          Writes itself to the memory.
protected  void writeToStack(byte[] stack, int offset)
          Writes itself to stack.
 
Methods inherited from class com.smardec.j2native.Argument
clone, finalize, free, getAlignedLength, getResultFlags, readFromRAM, restoreFromRAM, storeToRAM, writeToRAM
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Union

protected Union()
Preconstructs a union. Unions constructed with this constructor should be initialized using the initialize() method


Union

protected Union(Argument[] members)
Constructs a union with a given set of members. A constructed union initially has 0-index active member.

Method Detail

initialize

protected void initialize(Argument[] members)
Initialize union and sets union members.

Parameters:
members - union members

getMembers

public Argument[] getMembers()
Returns union members. WARNING: Do not modify contents of the returned array.


setActiveMember

public void setActiveMember(Argument active)
Defines the member of this union that is currently used. This method should be called before a value is assigned to that member.

Parameters:
active - a new active member

setActiveMember

public void setActiveMember(Argument active,
                            boolean update)
                     throws java.lang.IllegalStateException
Defines the member of this union that is currently used. This method should be called before a value is assigned to that member. If the update parameter is true, this member is read from the last value of this union. This is for the case when an active member can be determined only after the function call is completed. If the union contains pointers, it is recommended to set an active member to null before making such calls.

Parameters:
active - a new active member
update - if true, this member is read from the last value of this union
Throws:
java.lang.IllegalStateException - if internal structure is wrong

setActiveMember

public void setActiveMember(int index)
Selects a new active member of the union by its index in the array passed on the initialization.

Parameters:
index - the index of the new active member.

setActiveMember

public void setActiveMember(int index,
                            boolean update)
Selects a new active member of the union by its index in the array passed on the initialization. If the update parameter is true, this member is read from the last value of this union. This is for the case when an active member can be determined only after the function call is completed. If the union contains pointers, it is recommended to set an active member to null before making such calls.

Parameters:
index - the index of the new active member.
update - if true, this member is read from the last value of this union
Throws:
java.lang.IllegalStateException - if internal structure is wrong

getActiveMember

public Argument getActiveMember()
Returns currently active member.

Returns:
active member

update

protected void update(byte side)
Description copied from class: Argument
This method is used to synchronize Java side and native part.

Overrides:
update in class Argument
Parameters:
side - identifies part that should be updated

write

protected void write(long handle,
                     int offset)
Description copied from class: Argument
Writes itself to the memory. Exact memory address specified by params.

Specified by:
write in class Argument
Parameters:
handle - identifies location in memory where Argument will be written
offset - memory address offset

read

protected void read(long handle,
                    int offset)
Description copied from class: Argument
Reads itself from the memory. Exact memory address specified by params.

Specified by:
read in class Argument
Parameters:
handle - identifies location in memory from which Argument will be read
offset - memory address offset

writeToStack

protected void writeToStack(byte[] stack,
                            int offset)
Description copied from class: Argument
Writes itself to stack.

Specified by:
writeToStack in class Argument
Parameters:
stack - it's byte array, where Argument will be written
offset - offset in stack

readFromStack

protected void readFromStack(byte[] stack,
                             int offset)
Description copied from class: Argument
Read itself from stack.

Specified by:
readFromStack in class Argument
Parameters:
stack - it's byte array, from which Argument will be restored
offset - offset in stack

isFixedLength

protected boolean isFixedLength()
Description copied from class: Argument
Identify if Argument is a fixed length variable.

Specified by:
isFixedLength in class Argument
Returns:
true if it is so, and false otherwise

toBytesValue

public byte[] toBytesValue()
Description copied from class: Argument
Encodes itself and returns it's representation as array of bytes.

Specified by:
toBytesValue in class Argument
Returns:
byte[] representation of Argument

toBytesValue

protected void toBytesValue(byte[] bytes,
                            int offset)
Description copied from class: Argument
Encodes itself to the provided array of bytes.

Specified by:
toBytesValue in class Argument
Parameters:
bytes - array of bytes to which Argument will be written
offset - offset in array

fromBytesValue

public void fromBytesValue(byte[] bytes,
                           int offset)
Description copied from class: Argument
Restores itself from provided array of bytes.

Specified by:
fromBytesValue in class Argument
Parameters:
bytes - array of bytes from which Argument will be restored
offset - offset in array

getLength

public int getLength()
Description copied from class: Argument
Returns length in bytes.

Specified by:
getLength in class Argument
Returns:
an int


Copyright © 2004-2006 Smardec. All Rights Reserved.