Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package org.jruby.ext.ffi;
  
  import java.nio.ByteOrder;
  
  import org.jruby.*;
 
 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 pointerClass = module.defineClassUnder("Pointer",
                 module.getClass(.),
                 . ? new ReifyingAllocator(Pointer.class) : .);
 
         pointerClass.defineAnnotatedMethods(Pointer.class);
         pointerClass.defineAnnotatedConstants(Pointer.class);
         pointerClass.setReifiedClass(Pointer.class);
         pointerClass.kindOf = new RubyModule.KindOf() {
             @Override
             public boolean isKindOf(IRubyObject objRubyModule type) {
                 return obj instanceof Pointer && super.isKindOf(objtype); 
             }
         };
 
         module.defineClassUnder("NullPointerError"runtime.getRuntimeError(),
                 runtime.getRuntimeError().getAllocator());
 
         // Add Pointer::NULL as a constant
         Pointer nullPointer = new Pointer(runtimepointerClassnew NullMemoryIO(runtime));
         pointerClass.setConstant("NULL"nullPointer);
         
         runtime.getNilClass().addMethod("to_ptr"new NilToPointerMethod(runtime.getNilClass(), nullPointer));
 
         return pointerClass;
     }
 
     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 runtime.getFFI().;
     }
 
     public Pointer(Ruby runtimeRubyClass klazz) {
         super(runtimeklazzruntime.getFFI().getNullMemoryIO(), 0);
     }
 
     public Pointer(Ruby runtimeMemoryIO io) {
         this(runtimegetPointerClass(runtime), io);
     }
     public Pointer(Ruby runtimeMemoryIO iolong sizeint typeSize) {
         this(runtimegetPointerClass(runtime), iosizetypeSize);
     }
     protected Pointer(Ruby runtimeRubyClass klassMemoryIO io) {
         super(runtimeklassio.);
     }
     protected Pointer(Ruby runtimeRubyClass klassMemoryIO iolong size) {
         super(runtimeklassiosize);
     }
     protected Pointer(Ruby runtimeRubyClass klassMemoryIO 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()) ? getMemoryIO() : new SwappedMemoryIO(runtimegetMemoryIO()),
                 );
     }
     
     @JRubyMethod(name = "size", meta = true, visibility = )
     public static IRubyObject size(ThreadContext contextIRubyObject recv) {
         return RubyFixnum.newFixnum(context.getRuntime(), Factory.getInstance().sizeOf(.));
     }
 
     @JRubyMethod(name = { "initialize" }, visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject address) {
        setMemoryIO(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) {
        setMemoryIO(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 getMemoryIO().address();
    }
    @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),
                getMemoryIO().slice(offset),
                 == . ? . :  - offset);
    }
    @Override
    protected AbstractMemory slice(Ruby runtimelong offsetlong size) {
        return new Pointer(runtimegetPointerClass(runtime),
                getMemoryIO().slice(offsetsize), size);
    }
    protected Pointer getPointer(Ruby runtimelong offset) {
        return new Pointer(runtimegetPointerClass(runtime), getMemoryIO().getMemoryIO(offset), .);
    }
    private static final class NilToPointerMethod extends DynamicMethod {
        private static final Arity ARITY = .;
        private final Pointer nullPointer;
        private NilToPointerMethod(RubyModule implementationClassPointer nullPointer) {
            super(implementationClass..);
            this. = nullPointer;
        }
        @Override
        public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule clazzString nameIRubyObject[] argsBlock block) {
            .checkArity(context.runtimeargs);
            return ;
        }
        @Override
        public IRubyObject call(ThreadContext contextIRubyObject selfRubyModule klazzString name) {
            return ;
        }
        @Override
        public DynamicMethod dup() {
            return this;
        }
    }
New to GrepCode? Check out our FAQ X