Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package org.jruby.ext.ffi;
  
  import java.util.Map;
  import org.jruby.Ruby;
 
 @JRubyClass(name = "FFI::Type", parent = "Object")
 public abstract class Type extends RubyObject {
     private static final java.util.Locale LOCALE = ...;
     protected final NativeType nativeType;

    
Size of this type in bytes
 
     protected final int size;

    
Minimum alignment of this type in bytes
 
     protected final int alignment;
 
     public static RubyClass createTypeClass(Ruby runtimeRubyModule ffiModule) {
         RubyClass typeClass = ffiModule.defineClassUnder("Type"runtime.getObject(),
                 .);
         typeClass.defineAnnotatedMethods(Type.class);
         typeClass.defineAnnotatedConstants(Type.class);
 
         RubyClass builtinClass = typeClass.defineClassUnder("Builtin"typeClass,
                 .);
         builtinClass.defineAnnotatedMethods(Builtin.class);
         
         RubyModule nativeType = ffiModule.defineModuleUnder("NativeType");
 
         
         defineBuiltinType(runtimebuiltinClass."char""schar""int8""sint8");
         defineBuiltinType(runtimebuiltinClass."uchar""uint8");
         defineBuiltinType(runtimebuiltinClass."short""sshort""int16""sint16");
         defineBuiltinType(runtimebuiltinClass."ushort""uint16");
         defineBuiltinType(runtimebuiltinClass."int""sint""int32""sint32");
         defineBuiltinType(runtimebuiltinClass."uint""uint32");
         defineBuiltinType(runtimebuiltinClass."long_long""slong_long""int64""sint64");
         defineBuiltinType(runtimebuiltinClass."ulong_long""uint64");
         defineBuiltinType(runtimebuiltinClass."long""slong");
         defineBuiltinType(runtimebuiltinClass."ulong");
         defineBuiltinType(runtimebuiltinClass."float""float32");
         defineBuiltinType(runtimebuiltinClass."double""float64");
         
         for (NativeType t : NativeType.values()) {
             if (!builtinClass.hasConstant(t.name())) {
                 try {
                     Type b = new Builtin(runtimebuiltinClasstt.name().toLowerCase());
                     builtinClass.defineConstant(t.name().toUpperCase(), b);
                 } catch (UnsupportedOperationException ex) {
                 }
 
             }
         }
 
         //
         // Add aliases in Type::*, NativeType::* and FFI::TYPE_*
         //
         for (Map.Entry<StringRubyModule.ConstantEntryc : builtinClass.getConstantMap().entrySet()) {
             if (c.getValue(). instanceof Type.Builtin) {
                 typeClass.defineConstant(c.getKey(), c.getValue().);
                 nativeType.defineConstant(c.getKey(), c.getValue().);
                 ffiModule.defineConstant("TYPE_" + c.getKey(), c.getValue().);
             }
         }
 
         RubyClass arrayTypeClass = typeClass.defineClassUnder("Array"typeClass,
                 .);
         arrayTypeClass.defineAnnotatedMethods(Type.Array.class);
 
         return typeClass;
     }
 
     private static final void defineBuiltinType(Ruby runtimeRubyClass builtinClassNativeType nativeTypeString... names) {
         try {
             if (names.length > 0) {
                 for (String n : names) {
                     builtinClass.setConstant(n.toUpperCase(),
                             new Builtin(runtimebuiltinClassnativeTypen.toLowerCase()));
                 }
             } else {
                 builtinClass.setConstant(nativeType.name(),
                         new Builtin(runtimebuiltinClassnativeTypenativeType.name().toLowerCase()));
             }
         } catch (UnsupportedOperationException ex) {
         }
    }
    public static final RubyClass getTypeClass(Ruby runtime) {
        return runtime.getFFI().;
    }

    
Initializes a new Type instance.
    protected Type(Ruby runtimeRubyClass klassNativeType typeint sizeint alignment) {
        super(runtimeklass);
        this. = type;
        this. = size;
        this. = alignment;
    }

    
Initializes a new Type instance.
    protected Type(Ruby runtimeRubyClass klassNativeType type) {
        super(runtimeklass);
        this. = type;
        this. = getNativeSize(type);
        this. = getNativeAlignment(type);
    }

    
Gets the native type of this Type when passed as a parameter

Returns:
The native type of this Type.
    public final NativeType getNativeType() {
        return ;
    }

    
Gets the native size of this Type in bytes

Returns:
The native size of this Type.
    public final int getNativeSize() {
        return ;
    }

    
Gets the native alignment of this Type in bytes

Returns:
The native alignment of this Type.
    public final int getNativeAlignment() {
        return ;
    }

    
Gets the native size of this Type in bytes

Parameters:
context The Ruby thread context.
Returns:
The native size of this Type.
    @JRubyMethod(name = "size")
    public IRubyObject size(ThreadContext context) {
        return context.runtime.newFixnum(getNativeSize());
    }

    
Gets the native alignment of this Type in bytes

Parameters:
context The Ruby thread context.
Returns:
The native alignment of this Type.
    @JRubyMethod(name = "alignment")
    public IRubyObject alignment(ThreadContext context) {
        return context.runtime.newFixnum(getNativeAlignment());
    }
    @JRubyClass(name = "FFI::Type::Builtin", parent = "FFI::Type")
    public final static class Builtin extends Type {
        private final RubySymbol sym;

        
Initializes a new BuiltinType instance.
        private Builtin(Ruby runtimeRubyClass klassNativeType nativeTypeString symName) {
            super(runtimeklassnativeType, Type.getNativeSize(nativeType), Type.getNativeAlignment(nativeType));
            this. = runtime.newSymbol(symName);
        }
        @JRubyMethod(name = "to_s")
        public final IRubyObject to_s(ThreadContext context) {
            return RubyString.newString(context.runtime,
                    String.format("#<FFI::Type::Builtin:%s size=%d alignment=%d>",
                    .name(), ));
        }
        
        @Override
        public final String toString() {
            return .name();
        }
        @Override
        public boolean equals(Object obj) {
            return (obj instanceof Builtin) && ((Builtinobj)..equals();
        }
        @Override
        public int hashCode() {
            int hash = 5;
            hash = 23 * hash + .hashCode();
            return hash;
        }
        @JRubyMethod
        public final IRubyObject to_sym(ThreadContext context) {
            return ;
        }
        @Override
        @JRubyMethod(name = "==", required = 1)
        public IRubyObject op_equal(ThreadContext contextIRubyObject obj) {
            return context.runtime.newBoolean(this.equals(obj));
        }
        @Override
        @JRubyMethod(name = "equal?", required = 1)
        public IRubyObject equal_p(ThreadContext contextIRubyObject obj) {
            return context.runtime.newBoolean(this.equals(obj));
        }
        
        @JRubyMethod(name = "eql?", required = 1)
        public IRubyObject eql_p(ThreadContext contextIRubyObject obj) {
            return context.runtime.newBoolean(this.equals(obj));
        }
    }
    @JRubyClass(name = "FFI::Type::Array", parent = "FFI::Type")
    public final static class Array extends Type {
        private final Type componentType;
        private final int length;

        
Initializes a new Type.Array instance.
        public Array(Ruby runtimeRubyClass klassType componentTypeint length) {
            super(runtimeklass.componentType.getNativeSize() * lengthcomponentType.getNativeAlignment());
            this. = componentType;
            this. = length;
        }

        
Initializes a new Type.Array instance.
        public Array(Ruby runtimeType componentTypeint length) {
            this(runtimegetTypeClass(runtime).getClass("Array"), componentTypelength);
        }
        public final Type getComponentType() {
            return ;
        }
        public final int length() {
            return ;
        }
        @JRubyMethod(name = "new", required = 2, meta = true)
        public static final IRubyObject newInstance(ThreadContext contextIRubyObject klassIRubyObject componentTypeIRubyObject length) {
            if (!(componentType instanceof Type)) {
                throw context.runtime.newTypeError(componentTypegetTypeClass(context.runtime));
            }
            return new Array(context.runtime, (RubyClassklass, (TypecomponentType, RubyNumeric.fix2int(length));
        }
        @JRubyMethod
        public final IRubyObject length(ThreadContext context) {
            return context.runtime.newFixnum();
        }
        @JRubyMethod
        public final IRubyObject elem_type(ThreadContext context) {
            return ;
        }
    }
    private static final boolean isPrimitive(NativeType type) {
        switch (type) {
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
            case :
                return true;
            default:
                return false;
        }
    }
    private static final int getNativeAlignment(NativeType type) {
        return isPrimitive(type) ? Factory.getInstance().alignmentOf(type) : 1;
    }
    private static final int getNativeSize(NativeType type) {
        return isPrimitive(type) ? Factory.getInstance().sizeOf(type) : 0;
    }