Class CPointerMutable<T>

  • Type Parameters:
    T - target type of the pointer.

    public class CPointerMutable<T>
    extends CPointer<T>
    This class is the mutable variant of CPointer.

    Mutable pointers allow in-place modification of the pointers address and thereby advanced pointer arithmetics (better runtime performance). Please note, that modifications to the address will not be reflected in the memory region this pointer originated from.

    You receive a mutable variant of a pointer either by using one of the copy constructors CPointerMutable(CPointer) or CPointerMutable(CPointer, long) or by calling the method CPointer.mutable().

    Pointer Arithmetics

    Read documentation of CPointer first, to understand this section.

    CPointerMutable inherits the methods of CPointer. Since references to objects of CPointerMutable can be assigned to CPointer, the method plus(int) has to behave exactly like CPointer.plus(int) and return a new instance of CPointerMutable.

    The method add(int) now provides the functionality of plus(int) with in-place modification, meaning the address of the pointer object, on which the method was called, will be changed afterwards.

    Furthermore, mutable pointers support direct modification of their address by use of the methods assign(long) and assign(CPointer).

    Author:
    homac
    • Constructor Detail

      • CPointerMutable

        public CPointerMutable​(CPointer<T> pointer)
        Constructor to turn a pointer into a mutable pointer.
        Parameters:
        pointer -
      • CPointerMutable

        public CPointerMutable​(CPointer<T> pointer,
                               long address)
    • Method Detail

      • add

        public CPointerMutable<T> add​(int increment)
        add(int) is different to plus(int). While add(int) modifies the address in-place plus(int) returns a new instance with the result of the addition. Equivalent to
         int* p;
         (p += increment);
         
        Value of the pointer will be changed in place.
        Parameters:
        increment -
        Returns:
        same pointer instance with modified address
      • plus

        public CPointerMutable<T> plus​(int value)
                                throws java.io.IOException
        add(int) is different to plus(int). While add(int) modifies the address in-place plus(int) returns new instance with the result of the addition. Allows (almost) equivalent handling as operator.
         int *p, *a;
         a = p = .. ;
         a = (p+1)+1;
         
        same as
         CPointerMutable p, a;
         p = a = .. ;
         p = (p.plus(1)).plus(1);
         
        where the post condition
          post condition: (p != a)
         
        holds.
        Overrides:
        plus in class CPointer<T>
        Parameters:
        value - value to be added to the address, multiplied by sizeof(targetType).
        Returns:
        new instance of this pointer with an address+=(targetSize*value)
        Throws:
        java.io.IOException
      • assign

        public void assign​(long address)
                    throws java.io.IOException
        Equivalent to
         int* p;
         int* a = ..;
         p = a;
        
        Parameters:
        address -
        Throws:
        java.io.IOException
      • assign

        public void assign​(CPointer<T> address)
                    throws java.io.IOException
        Equivalent to
         int* p;
         int* a = ..;
         p = a;
        
        Parameters:
        address - new address
        Throws:
        java.io.IOException
      • value

        public long value()
        Returns the value of the address.