Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 
   */
  
  package org.jruby.ext.ffi;
  
  
  import org.jruby.Ruby;
A type which represents a conversion to/from a native type.
 
 @JRubyClass(name="FFI::Type::Mapped", parent="FFI::Type")
 public final class MappedType extends Type {
     private final Type realType;
     private final IRubyObject converter;
     private final DynamicMethod toNativeMethodfromNativeMethod;
     private final boolean isReferenceRequired;
 
     public static RubyClass createConverterTypeClass(Ruby runtimeRubyModule ffiModule) {
         RubyClass convClass = ffiModule.getClass("Type").defineClassUnder("Mapped"ffiModule.getClass("Type"),
                 .);
         convClass.defineAnnotatedMethods(MappedType.class);
         convClass.defineAnnotatedConstants(MappedType.class);
 
 
         return convClass;
     }
 
     private MappedType(Ruby runtimeRubyClass klassType nativeTypeIRubyObject converter,
             DynamicMethod toNativeMethodDynamicMethod fromNativeMethodboolean isRefererenceRequired) {
         super(runtimeklass.nativeType.getNativeSize(), nativeType.getNativeAlignment());
         this. = nativeType;
         this. = converter;
         this. = toNativeMethod;
         this. = fromNativeMethod;
         this. = isRefererenceRequired;
     }
 
     @JRubyMethod(name = "new", meta = true)
     public static final IRubyObject newMappedType(ThreadContext contextIRubyObject klassIRubyObject converter) {
         if (!converter.respondsTo("native_type")) {
             throw context.runtime.newNoMethodError("converter needs a native_type method""native_type"converter.getMetaClass());
         }
         
         DynamicMethod toNativeMethod = converter.getMetaClass().searchMethod("to_native");
         if (toNativeMethod.isUndefined()) {
             throw context.runtime.newNoMethodError("converter needs a to_native method""to_native"converter.getMetaClass());
         }
 
         if (toNativeMethod.getArity().required() < 1 || toNativeMethod.getArity().required() > 2) {
             throw context.runtime.newArgumentError("to_native should accept one or two arguments");
         }
 
         DynamicMethod fromNativeMethod = converter.getMetaClass().searchMethod("from_native");
         if (fromNativeMethod.isUndefined()) {
             throw context.runtime.newNoMethodError("converter needs a from_native method""from_native"converter.getMetaClass());
         }
 
         if (fromNativeMethod.getArity().required() < 1 || fromNativeMethod.getArity().required() > 2) {
             throw context.runtime.newArgumentError("from_native should accept one or two arguments");
         }
 
         Type nativeType;
         try {
             nativeType = (Typeconverter.callMethod(context"native_type");
         } catch (ClassCastException ex) {
             throw context.runtime.newTypeError("native_type did not return instance of FFI::Type");
         }
 
         boolean isReferenceRequired;
         if (converter.respondsTo("reference_required?")) {
             isReferenceRequired = converter.callMethod(context"reference_required?").isTrue();
 
         } else {
             switch (nativeType.nativeType) {
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                 case :
                     isReferenceRequired = false;
                    break;
                default:
                    isReferenceRequired = true;
                    break;
            }
        }
        return new MappedType(context.runtime, (RubyClassklassnativeTypeconverter,
                toNativeMethodfromNativeMethodisReferenceRequired);
    }
    
    public final Type getRealType() {
        return ;
    }
    public final boolean isReferenceRequired() {
        return ;
    }
    public final boolean isPostInvokeRequired() {
        return false;
    }
    public final IRubyObject native_type(ThreadContext context) {
        return ;
    }
    public final IRubyObject from_native(ThreadContext contextIRubyObject valueIRubyObject ctx) {
        return fromNative(contextvalue);
    }
    public final IRubyObject to_native(ThreadContext contextIRubyObject valueIRubyObject ctx) {
        return toNative(contextvalue);
    }
    private static IRubyObject callConversionMethod(ThreadContext contextDynamicMethod methodIRubyObject converter,
                                                    String methodNameIRubyObject value) {
        if (method.getArity().required() == 2) {
            return method.call(contextconverterconverter.getMetaClass(),
                    methodNamevaluecontext.runtime.getNil());
        } else {
            return method.call(contextconverterconverter.getMetaClass(),
                    methodNamevalue);
        }
    }
    public final IRubyObject fromNative(ThreadContext contextIRubyObject value) {
        return callConversionMethod(context"from_native"value);
    }
    public final IRubyObject toNative(ThreadContext contextIRubyObject value) {
        return callConversionMethod(context"to_native"value);
    }
New to GrepCode? Check out our FAQ X