Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2008, 2009 JRuby project Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 
 package org.jruby.ext.ffi;
 
 import org.jruby.Ruby;
Defines a C callback's parameters and return type.
 
 @JRubyClass(name = "FFI::CallbackInfo", parent = "FFI::Type")
 public class CallbackInfo extends Type {
     public static final String CLASS_NAME = "CallbackInfo";
    
    
The arity of this function.
 
     protected final Arity arity;
 
     protected final Type[] parameterTypes;
     protected final Type returnType;
     protected final boolean stdcall;


    
Creates a CallbackInfo class for a ruby runtime

Parameters:
runtime The runtime to create the class for
module The module to place the class in
Returns:
The newly created ruby class
 
     public static RubyClass createCallbackInfoClass(Ruby runtimeRubyModule module) {
         RubyClass result = module.defineClassUnder(,
                 module.getClass("Type"),
                 .);
         result.defineAnnotatedMethods(CallbackInfo.class);
         result.defineAnnotatedConstants(CallbackInfo.class);
 
         module.getClass("Type").setConstant("Function"result);
         return result;
     }
    
    
Creates a new CallbackInfo instance.

Parameters:
runtime The runtime to create the instance for
klazz The ruby class of the CallbackInfo instance
returnType The return type of the callback
paramTypes The parameter types of the callback
 
     public CallbackInfo(Ruby runtimeRubyClass klazzType returnTypeType[] paramTypesboolean stdcall) {
         super(runtimeklazz.);
         this. = Arity.fixed(paramTypes.length);
         this. = paramTypes;
         this. = returnType;
         this. = stdcall;
     }

    
CallbackInfo.new

Parameters:
context The current ruby thread context
klass The ruby class of the CallbackInfo instance
returnType The ruby return type
_paramTypes An array containing the ruby parameter types
Returns:
A new CallbackInfo instance
    @JRubyMethod(name = "new", meta = true, required = 2, optional = 1)
    public static final IRubyObject newCallbackInfo(ThreadContext contextIRubyObject klass,
            IRubyObject[] args)
    {
        IRubyObject returnType = args[0], paramTypes = args[1];
        if (!(returnType instanceof Type)) {
            throw context.runtime.newTypeError("wrong argument type "
                    + returnType.getMetaClass().getName() + " (expected FFI::Type)");
        }
        if (!(paramTypes instanceof RubyArray)) {
            throw context.runtime.newTypeError("wrong argument type "
                    + paramTypes.getMetaClass().getName() + " (expected Array)");
        }
        Type[] nativeParamTypes = new Type[((RubyArray)paramTypes).size()];
        for (int i = 0; i < nativeParamTypes.length; ++i) {
            IRubyObject obj = ((RubyArrayparamTypes).entry(i);
            if (!(obj instanceof Type)) {
                throw context.runtime.newTypeError("wrong argument type "
                        + obj.getMetaClass().getName() + " (expected array of FFI::Type)");
            }
            nativeParamTypes[i] = (Typeobj;
        }
        boolean stdcall = false;
        if (args.length > 2) {
            if (!(args[2] instanceof RubyHash)) {
                throw context.runtime.newTypeError("wrong argument type "
                        + args[3].getMetaClass().getName() + " (expected Hash)");
            }
            RubyHash hash = (RubyHashargs[2];
            stdcall = "stdcall".equals(hash.get(context.runtime.newSymbol("convention")));
        }
        
        try {
            return new CallbackInfo(context.runtime, (RubyClassklass,
                    (TypereturnTypenativeParamTypesstdcall);
        } catch (UnsatisfiedLinkError ex) {
            return context.runtime.getNil();
        }
    }
    
    
Returns the org.jruby.runtime.Arity of this function.

Returns:
The Arity of the native function.
    public final Arity getArity() {
        return ;
    }

    
Gets the native return type the callback should return

Returns:
The native return type
    public final Type getReturnType() {
        return ;
    }

    
Gets the ruby parameter types of the callback

Returns:
An array of the parameter types
    public final Type[] getParameterTypes() {
        return ;
    }
    public final boolean isStdcall() {
        return ;
    }
    @JRubyMethod(name = "to_s")
    public final IRubyObject to_s(ThreadContext context) {
        StringBuilder sb = new StringBuilder();
        sb.append("#<FFI::CallbackInfo [ ");
        for (int i = 0; i < .; ++i) {
            sb.append([i].toString().toLowerCase());
            if (i < (. - 1)) {
                sb.append(", ");
            }
        }
        sb.append(" ], " + .toString().toLowerCase() + ">");
        return context.runtime.newString(sb.toString());
    }
    @Override
    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("CallbackInfo[parameters=[");
        for (int i = 0; i < .; ++i) {
            sb.append([i].toString().toLowerCase());
            if (i < (. - 1)) {
                sb.append(", ");
            }
        }
        sb.append("] return=" + .toString().toLowerCase() + "]");
        return sb.toString();
    }
    public final IRubyObject result_type(ThreadContext context) {
        return ;
    }
    public final IRubyObject param_types(ThreadContext context) {
        return RubyArray.newArray(context.runtime);
    }
New to GrepCode? Check out our FAQ X