public final class QNativePointer extends Object
QNativePointer does type checking of pointers. Also, if the pointer is pointing to an array, you must also specify the array length; array bounds checking is enforced by QNativePointer. Any number of indirections are allowed (i.e., arrays can have any number of dimensions).
The QNativePointer will by default delete the internal pointer when being garbage collected. However, if the ownership of the pointer is given to a c++ class, you do not want this behavior. The AutoDeleteMode enum values defines the ways in which deletion of the pointer can be handled.
The data types that can be pointed to are defined by the Type enum. An allocation of an Integer pointer can, for example, be done like this:
QNativePointer ptr =
new QNativePointer(QNativePointer.Type.Int);
ptr.setIntValue(10);
An array of length 5 is created in the following way:
QNativePointer ptr = new QNativePointer(QNativePointer.Type.Int, 5);
for (int i = 0; i < 5, ++i)
ptr.setIntAt(i, i*i);
If you are creating a multi dimensional array, you have two possibilities. You can make QNativePointers of the Pointer type or specify the number indirections of a single QNativePointer. We recommend the second alternative since it creates type safe pointers. Here is an example using the first alternative:
QNativePointer ptr = new QNativePointer(QNativePointer.Type.Pointer, 2);
QNativePointer charArray1 = new QNativePointer(QNativePointer.Type.Char, 5);
ptr.setPointerAt(0, carArray1);
And here is the code for the second:
QNativePointer ptr = new QNativePointer(Type.Char, 5, 2);
ptr.setPointerAt(0, createCharPointer(myString));
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QNativePointer.AutoDeleteMode
The AutoDeleteMode enum describes how garbage collection of the QNativePointer handles the deletion of the native pointer.static class
QNativePointer.ReadOnlyNativePointerException
static class
QNativePointer.Type
The Type enum describe the Java types that can be used by a QNativePointer. -
Constructor Summary
Constructors Constructor Description QNativePointer(QNativePointer.Type type)
Creates a native pointer of the specifiedtype
.QNativePointer(QNativePointer.Type type, long size)
Creates a native pointer to an array withsize
length of the specifiedtype
.QNativePointer(QNativePointer.Type type, long size, int indirections)
Creates a native pointer of the specifiedtype
. -
Method Summary
Modifier and Type Method Description QNativePointer.AutoDeleteMode
autoDeleteMode()
Returns the auto-delete mode of the pointer.boolean[]
booleanArray()
If the native pointer is of boolean type, this function returns its values as array.boolean
booleanAt(long pos)
Returns the value of the native pointer at the specified position.boolean
booleanValue()
If the native pointer is of boolean type, this function returns its value.byte[]
byteArray()
If the native pointer is of byte type, this function returns its values as array.byte
byteAt(long pos)
Returns the value of the native pointer at the specified position.ByteBuffer
byteBuffer()
byte
byteValue()
If the native pointer is of byte type, this function returns its value.char[]
charArray()
If the native pointer is of char type, this function returns its values as array.char
charAt(long pos)
Returns the value of the native pointer at the specified position.CharBuffer
charBuffer()
char
charValue()
If the native pointer is of char type, this function returns its value.static QNativePointer
createCharPointer(String string)
Creates a char* from the input stringstatic QNativePointer
createCharPointerPointer(String[] strings)
Creates a char** native pointer from the array of input strings.void
delete()
This function deletes the internal pointer.void
deleteArray()
This function deletes elements in the array of this QNativePointer.double[]
doubleArray()
If the native pointer is of double type, this function returns its values as array.double
doubleAt(long pos)
Returns the value of the native pointer at the specified position.DoubleBuffer
doubleBuffer()
double
doubleValue()
If the native pointer is of double type, this function returns its value.float[]
floatArray()
If the native pointer is of float type, this function returns its values as array.float
floatAt(long pos)
Returns the value of the native pointer at the specified position.FloatBuffer
floatBuffer()
float
floatValue()
If the native pointer is of float type, this function returns its value.void
free()
This function deletes the internal pointer.static QNativePointer
fromArray(boolean[] data)
static QNativePointer
fromArray(byte[] data)
static QNativePointer
fromArray(char[] data)
static QNativePointer
fromArray(double[] data)
static QNativePointer
fromArray(float[] data)
static QNativePointer
fromArray(int[] data)
static QNativePointer
fromArray(long[] data)
static QNativePointer
fromArray(short[] data)
static QNativePointer
fromArray(Boolean[] data)
static QNativePointer
fromArray(Byte[] data)
static QNativePointer
fromArray(Character[] data)
static QNativePointer
fromArray(Double[] data)
static QNativePointer
fromArray(Float[] data)
static QNativePointer
fromArray(Integer[] data)
static QNativePointer
fromArray(Long[] data)
static QNativePointer
fromArray(Short[] data)
static QNativePointer
fromArray(String[] data)
static <T extends QtObjectInterface>
QNativePointerfromArray(T... array)
static QNativePointer
fromBuffer(Buffer buffer)
static QNativePointer
fromNative(long ptr, QNativePointer.Type type, long size, int indirections, boolean readOnly)
This function creates a QNativePointer from an existing c++ pointer.static <T extends QtObjectInterface>
QNativePointerfromObject(T object)
int
indirections()
Returns the number of indirections of the pointer.int[]
intArray()
If the native pointer is of int type, this function returns its values as array.int
intAt(long pos)
Returns the value of the native pointer at the specified position.IntBuffer
intBuffer()
int
intValue()
If the native pointer is of int type, this function returns its value.void
invalidate()
boolean
isNull()
Returns true if the native pointer is 0; otherwise false.boolean
isReadOnly()
long
knownSize()
long[]
longArray()
If the native pointer is of long type, this function returns its values as array.long
longAt(long pos)
Returns the value of the native pointer at the specified position.LongBuffer
longBuffer()
long
longValue()
If the native pointer is of long type, this function returns its value.<T extends QtObjectInterface>
Tobject(Class<T> valueType)
If the native pointer is of pointer type, this function returns its value matching the given valueType.<T extends QtObjectInterface>
T[]objectArray(Class<T> valueType)
If the native pointer is of pointer type, this function returns its value matching the given valueType.<T extends QtObjectInterface>
TobjectAt(Class<T> valueType, long pos)
Returns the value of the native pointer matching the given valueType at the specified position.QIODevice
openAsDevice(QIODeviceBase.OpenMode openMode)
QIODevice
openAsDevice(QIODeviceBase.OpenModeFlag... openMode)
long
pointer()
Returns the native pointer.QNativePointer
pointerAt(long pos)
Returns the value of the native pointer at the specified position.QNativePointer
pointerValue()
If the native pointer is of pointer type, this function returns its value.void
setAutoDeleteMode(QNativePointer.AutoDeleteMode autodelete)
This function sets the auto delete mode of the QNativePointer.void
setBooleanAt(long pos, boolean value)
Sets the value of the array element atpos
to which this native pointer points.void
setBooleanValue(boolean value)
Sets the value of this pointer tovalue
.void
setByteAt(long pos, byte value)
Sets the value of the array element atpos
to which this native pointer points.void
setByteValue(byte value)
Sets the value of this pointer tovalue
.void
setCharAt(long pos, char value)
Sets the value of the array element atpos
to which this native pointer points.void
setCharValue(char value)
Sets the value of this pointer tovalue
.void
setDoubleAt(long pos, double value)
Sets the value of the array element atpos
to which this native pointer points.void
setDoubleValue(double value)
Sets the value of this pointer tovalue
.void
setFloatAt(long pos, float value)
Sets the value of the array element atpos
to which this native pointer points.void
setFloatValue(float value)
Sets the value of this pointer tovalue
.void
setIntAt(long pos, int value)
Sets the value of the array element atpos
to which this native pointer points.void
setIntValue(int value)
Sets the value of this pointer tovalue
.void
setLongAt(long pos, long value)
Sets the value of the array element atpos
to which this native pointer points.void
setLongValue(long value)
Sets the value of this pointer tovalue
.<T extends QtObjectInterface>
voidsetObjectAt(long pos, T value)
Sets the value of the array element atpos
to which this native pointer points.<T extends QtObjectInterface>
voidsetObjectValue(T value)
Sets the value of this pointer tovalue
.void
setPointerAt(long pos, QNativePointer value)
Sets the value of the array element atpos
to which this native pointer points.void
setPointerValue(QNativePointer value)
Sets the value of this pointer tovalue
.void
setShortAt(long pos, short value)
Sets the value of the array element atpos
to which this native pointer points.void
setShortValue(short value)
Sets the value of this pointer tovalue
.void
setStringAt(long pos, String value)
Sets the value of the array element atpos
to which this native pointer points.void
setStringValue(String value)
Sets the value of this pointer tovalue
.void
setVerificationEnabled(boolean a)
Sets if the any accesses should be type verified or not.short[]
shortArray()
If the native pointer is of short type, this function returns its values as array.short
shortAt(long pos)
Returns the value of the native pointer at the specified position.ShortBuffer
shortBuffer()
short
shortValue()
If the native pointer is of short type, this function returns its value.String[]
stringArray()
If the native pointer is of string type, this function returns its values as array.String
stringAt(long pos)
Returns the value of the native pointer at the specified position.String
stringValue()
If the native pointer is of string type, this function returns its value.QNativePointer.Type
type()
Returns the type of the native pointer.boolean
verificationEnabled()
Returns if verification is enabled or not.
-
Constructor Details
-
QNativePointer
Creates a native pointer of the specifiedtype
. The object has an indirection of 1 and the internal pointer will be deleted when the QNativePointer object is deleted.- Parameters:
type
- the type of pointer to create.
-
QNativePointer
Creates a native pointer to an array withsize
length of the specifiedtype
. The pointer will have an indirection of 1 and be deleted when the QNativePointer is garbage collected.- Parameters:
type
- the type of pointer to create.size
- the size of the array.
-
QNativePointer
Creates a native pointer of the specifiedtype
. It will be an array ifsize
is larger than one and have an indirection ofindirections
. For instance, the following Java statement will create a**char
pointer with the first array dimension of length 5:QNativePointer ptr = new QNativePointer(QNativePointer.Type.Int, 5, 2);
- Parameters:
type
- the type of pointer to create.size
- the length of the array.indirections
- the number of indirections for the pointer.
-
-
Method Details
-
booleanValue
public boolean booleanValue()If the native pointer is of boolean type, this function returns its value. If it is an array of booleans, the first element is returned.- Returns:
- the value of the pointer.
-
byteValue
public byte byteValue()If the native pointer is of byte type, this function returns its value. If it is an array of bytes, the first element is returned.- Returns:
- the value of the pointer.
-
charValue
public char charValue()If the native pointer is of char type, this function returns its value. If it is an array of chars, the first element is returned.- Returns:
- the value of the pointer.
-
shortValue
public short shortValue()If the native pointer is of short type, this function returns its value. If it is an array of shorts, the first element is returned.- Returns:
- the value of the pointer.
-
intValue
public int intValue()If the native pointer is of int type, this function returns its value. If it is an array of ints, the first element is returned.- Returns:
- the value of the pointer.
-
longValue
public long longValue()If the native pointer is of long type, this function returns its value. If it is an array of longs, the first element is returned.- Returns:
- the value of the pointer.
-
floatValue
public float floatValue()If the native pointer is of float type, this function returns its value. If it is an array of floats, the first element is returned.- Returns:
- the value of the pointer.
-
doubleValue
public double doubleValue()If the native pointer is of double type, this function returns its value. If it is an array of doubles, the first element is returned.- Returns:
- the value of the pointer.
-
booleanArray
public boolean[] booleanArray()If the native pointer is of boolean type, this function returns its values as array.- Returns:
- the values of the pointer.
-
byteArray
public byte[] byteArray()If the native pointer is of byte type, this function returns its values as array.- Returns:
- the values of the pointer.
-
charArray
public char[] charArray()If the native pointer is of char type, this function returns its values as array.- Returns:
- the values of the pointer.
-
shortArray
public short[] shortArray()If the native pointer is of short type, this function returns its values as array.- Returns:
- the values of the pointer.
-
intArray
public int[] intArray()If the native pointer is of int type, this function returns its values as array.- Returns:
- the values of the pointer.
-
longArray
public long[] longArray()If the native pointer is of long type, this function returns its values as array.- Returns:
- the values of the pointer.
-
floatArray
public float[] floatArray()If the native pointer is of float type, this function returns its values as array.- Returns:
- the values of the pointer.
-
doubleArray
public double[] doubleArray()If the native pointer is of double type, this function returns its values as array.- Returns:
- the values of the pointer.
-
objectArray
If the native pointer is of pointer type, this function returns its value matching the given valueType.- Parameters:
valueType
- the type of the value- Returns:
- the value of the pointer.
-
pointerValue
If the native pointer is of pointer type, this function returns its value. If it is an array of pointers, the first element is returned.- Returns:
- the value of the pointer.
-
object
If the native pointer is of pointer type, this function returns its value matching the given valueType. If it is an array of pointers, the first element is returned.- Parameters:
valueType
- the type of the value- Returns:
- the value of the pointer.
-
stringValue
If the native pointer is of string type, this function returns its value. If it is an array of strings, the first element is returned.- Returns:
- the value of the pointer.
-
stringArray
If the native pointer is of string type, this function returns its values as array.- Returns:
- the values of the pointer.
-
setBooleanValue
public void setBooleanValue(boolean value)Sets the value of this pointer tovalue
. The type of the pointer must be boolean.- Parameters:
value
- the value to which the pointer is set.
-
setByteValue
public void setByteValue(byte value)Sets the value of this pointer tovalue
. The type of the pointer must be byte.- Parameters:
value
- the value to which the pointer is set.
-
setCharValue
public void setCharValue(char value)Sets the value of this pointer tovalue
. The type of the pointer must be char.- Parameters:
value
- the value to which the pointer is set.
-
setShortValue
public void setShortValue(short value)Sets the value of this pointer tovalue
. The type of the pointer must be short.- Parameters:
value
- the value to which the pointer is set.
-
setIntValue
public void setIntValue(int value)Sets the value of this pointer tovalue
. The type of the pointer must be int.- Parameters:
value
- the value to which the pointer is set.
-
setLongValue
public void setLongValue(long value)Sets the value of this pointer tovalue
. The type of the pointer must be long.- Parameters:
value
- the value to which the pointer is set.
-
setFloatValue
public void setFloatValue(float value)Sets the value of this pointer tovalue
. The type of the pointer must be float.- Parameters:
value
- the value to which the pointer is set.
-
setDoubleValue
public void setDoubleValue(double value)Sets the value of this pointer tovalue
. The type of the pointer must double.- Parameters:
value
- the value to which the pointer is set.
-
setPointerValue
Sets the value of this pointer tovalue
. The pointer must be of pointer type.- Parameters:
value
- the value to which the pointer is set.
-
setObjectValue
Sets the value of this pointer tovalue
. The pointer must be of pointer type.- Parameters:
value
- the value to which the pointer is set.
-
setStringValue
Sets the value of this pointer tovalue
. The pointer must point to a string.- Parameters:
value
- the value to which the pointer is set.
-
booleanAt
public boolean booleanAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
byteAt
public byte byteAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
charAt
public char charAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
shortAt
public short shortAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
intAt
public int intAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
longAt
public long longAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
floatAt
public float floatAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
doubleAt
public double doubleAt(long pos)Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
byteBuffer
-
charBuffer
-
shortBuffer
-
intBuffer
-
longBuffer
-
floatBuffer
-
doubleBuffer
-
pointerAt
Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
objectAt
Returns the value of the native pointer matching the given valueType at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
valueType
- the type of the valuepos
- the array index- Returns:
- the value of the pointer.
-
stringAt
Returns the value of the native pointer at the specified position. Ifpos
is larger than 1, QNativePointer will check that the position is within the array bounds.- Parameters:
pos
- the array index
-
setBooleanAt
public void setBooleanAt(long pos, boolean value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setByteAt
public void setByteAt(long pos, byte value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setCharAt
public void setCharAt(long pos, char value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setShortAt
public void setShortAt(long pos, short value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setIntAt
public void setIntAt(long pos, int value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setLongAt
public void setLongAt(long pos, long value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setFloatAt
public void setFloatAt(long pos, float value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setDoubleAt
public void setDoubleAt(long pos, double value)Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setPointerAt
Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setObjectAt
Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
setStringAt
Sets the value of the array element atpos
to which this native pointer points.- Parameters:
pos
- the array indexvalue
- the value to set the index to
-
type
Returns the type of the native pointer.- Returns:
- the data type of the native pointer
-
indirections
public int indirections()Returns the number of indirections of the pointer.- Returns:
- the number of indirections of the pointer
-
knownSize
public long knownSize() -
isNull
public boolean isNull()Returns true if the native pointer is 0; otherwise false.- Returns:
- true if the native pointer is 0.
-
autoDeleteMode
Returns the auto-delete mode of the pointer.- Returns:
- the auto-delete mode of this QNativePointer
-
setAutoDeleteMode
This function sets the auto delete mode of the QNativePointer. The internal pointer is deleted by default when the QNativePointer object is garbage collected, so you only need to call this function when you want to keep the pointer valid after the Java object is garbage collected.- Parameters:
autodelete
- the new auto delete mode.
-
free
public void free()This function deletes the internal pointer. Currently, all QNativePointers should be deleted using the delete() function. After the pointer has been deleted, you cannot use the same QNativePointer to allocate further data. Note also that the pointer will by default be deleted upon garbage collection. -
delete
public void delete()This function deletes the internal pointer. After the pointer has been deleted, you cannot allocate it again. The pointer is by default deleted when the QNativePointer object is garbage collected. -
deleteArray
public void deleteArray()This function deletes elements in the array of this QNativePointer. After the pointer has been deleted, you cannot use this QNativePointer object again. -
pointer
public long pointer()Returns the native pointer. The returned long is thevoid *
value in c++.- Returns:
- the native pointer.
-
fromNative
public static QNativePointer fromNative(long ptr, QNativePointer.Type type, long size, int indirections, boolean readOnly)This function creates a QNativePointer from an existing c++ pointer. The long is thevoid *
(i.e., address) value of the pointer. There are several ways of acquiring a native pointer. For instance, QNativePointer internal pointer is returned by pointer(), and QtJambiObject.nativeId() returns the c++ pointer to its Qt object.- Parameters:
ptr
- the void * value of the pointer.type
- the Type of the pointerindirections
- the number of pointer indirections- Returns:
- a QNativePointer object with ptr as the native pointer
-
fromArray
-
fromObject
-
fromBuffer
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
fromArray
-
verificationEnabled
public boolean verificationEnabled()Returns if verification is enabled or not.- Returns:
- true if verification is enabled; otherwise false.
-
isReadOnly
public boolean isReadOnly() -
setVerificationEnabled
public void setVerificationEnabled(boolean a)Sets if the any accesses should be type verified or not. By default this value is set to true, meaning that trying to access a char pointer as an int pointer will trigger an exception. Disabling this value allows complete access, but without any safety, so incorrect usage may lead to memory corruption in the C++ implementation.- Parameters:
a
- Set to true if verification should be enabled.
-
createCharPointerPointer
Creates a char** native pointer from the array of input strings.- Parameters:
strings
- the input strings- Returns:
- a char **
-
createCharPointer
Creates a char* from the input string- Parameters:
string
- The input string- Returns:
- The char*
-
openAsDevice
-
openAsDevice
-
invalidate
public final void invalidate()
-