Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package org.jruby.ext.ffi;
  
  import java.nio.ByteOrder;
  import org.jruby.Ruby;
 
 import static org.jruby.runtime.Visibility.*;

C memory pointer operations.

This is an abstract class that defines Pointer operations

 
 @JRubyClass(name="FFI::Pointer", parent=.)
 public class Pointer extends AbstractMemory {
     public static RubyClass createPointerClass(Ruby runtimeRubyModule module) {
         RubyClass result = module.defineClassUnder("Pointer",
                 module.getClass(.),
                 .);
 
         result.defineAnnotatedMethods(Pointer.class);
         result.defineAnnotatedConstants(Pointer.class);
 
 
         module.defineClassUnder("NullPointerError"runtime.getRuntimeError(),
                 runtime.getRuntimeError().getAllocator());
 
         // Add Pointer::NULL as a constant
         result.setConstant("NULL"new Pointer(runtimeresultnew NullMemoryIO(runtime)));
 
         return result;
     }
 
     private static final class PointerAllocator implements ObjectAllocator {
         static final ObjectAllocator INSTANCE = new PointerAllocator();
 
         public IRubyObject allocate(Ruby runtimeRubyClass klazz) {
             return new Pointer(runtimeklazz);
         }
     }
 
     public static final Pointer getNull(Ruby runtime) {
         return (Pointerruntime.getFFI()..getConstant("NULL");
     }
 
     Pointer(Ruby runtimeRubyClass klazz) {
         super(runtimeklazzruntime.getFFI().getNullMemoryIO(), 0);
     }
 
     public Pointer(Ruby runtimeDirectMemoryIO io) {
         this(runtimegetPointerClass(runtime), io);
     }
     public Pointer(Ruby runtimeDirectMemoryIO iolong sizeint typeSize) {
         this(runtimegetPointerClass(runtime), iosizetypeSize);
     }
     protected Pointer(Ruby runtimeRubyClass klassDirectMemoryIO io) {
         super(runtimeklassio.);
     }
     protected Pointer(Ruby runtimeRubyClass klassDirectMemoryIO iolong size) {
         super(runtimeklassiosize);
     }
     protected Pointer(Ruby runtimeRubyClass klassDirectMemoryIO iolong sizeint typeSize) {
         super(runtimeklassiosizetypeSize);
     }
 
     public static final RubyClass getPointerClass(Ruby runtime) {
         return runtime.getFFI().;
     }
 
     public final AbstractMemory order(Ruby runtimeByteOrder order) {
         return new Pointer(runtime,
                 order.equals(getMemoryIO().order()) ? (DirectMemoryIOgetMemoryIO() : new SwappedMemoryIO(runtimegetMemoryIO()),
                 );
     }
 
     @JRubyMethod(name = { "initialize" }, visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject address) {
          = address instanceof Pointer
                 ? ((Pointeraddress).getMemoryIO()
                 : Factory.getInstance().wrapDirectMemory(context.runtime, RubyFixnum.num2long(address));
          = .;
          = 1;
 
         return this;
     }
 
     @JRubyMethod(name = { "initialize" }, visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject typeIRubyObject address) {
          = address instanceof Pointer
                ? ((Pointeraddress).getMemoryIO()
                : Factory.getInstance().wrapDirectMemory(context.runtime, RubyFixnum.num2long(address));
         = .;
         = calculateTypeSize(contexttype);
        return this;
    }
    
    
    @JRubyMethod(required = 1, visibility=)
    public IRubyObject initialize_copy(ThreadContext contextIRubyObject other) {
        if (this == other) {
            return this;
        }
        Pointer orig = (Pointerother;
        this. = orig.typeSize;
        this. = orig.size;
        setMemoryIO(orig.getMemoryIO().dup());
        return this;
    }


    
Tests if this Pointer represents the C NULL value.

Returns:
true if the address is NULL.
    @JRubyMethod(name = "null?")
    public IRubyObject null_p(ThreadContext context) {
        return context.runtime.newBoolean(getMemoryIO().isNull());
    }
    @Override
    @JRubyMethod(name = { "to_s""inspect" }, optional = 1)
    public IRubyObject to_s(ThreadContext contextIRubyObject[] args) {
        String s =  != .
                ? String.format("#<%s address=0x%x size=%s>"getMetaClass().getName(), getAddress(), )
                : String.format("#<%s address=0x%x>"getMetaClass().getName(), getAddress());
        return RubyString.newString(context.runtimes);
    }
    @JRubyMethod(name = { "address""to_i" })
    public IRubyObject address(ThreadContext context) {
        return context.runtime.newFixnum(getAddress());
    }

    
Gets the native memory address of this pointer.

Returns:
A long containing the native memory address.
    public final long getAddress() {
        return ((DirectMemoryIOgetMemoryIO()).getAddress();
    }
    @JRubyMethod(name = "==", required = 1)
    public IRubyObject op_equal(ThreadContext contextIRubyObject obj) {
        return context.runtime.newBoolean(this == obj
                || getAddress() == 0L && obj.isNil()
                || (obj instanceof Pointer && ((Pointerobj).getAddress() == getAddress()));
    }
    
    @Override
    protected AbstractMemory slice(Ruby runtimelong offset) {
        return new Pointer(runtimegetPointerClass(runtime),
                (DirectMemoryIOgetMemoryIO().slice(offset),
                 == . ? . :  - offset);
    }
    @Override
    protected AbstractMemory slice(Ruby runtimelong offsetlong size) {
        return new Pointer(runtimegetPointerClass(runtime),
                (DirectMemoryIOgetMemoryIO().slice(offsetsize), size);
    }
    protected Pointer getPointer(Ruby runtimelong offset) {
        return new Pointer(runtimegetPointerClass(runtime), getMemoryIO().getMemoryIO(offset), .);
    }
New to GrepCode? Check out our FAQ X