Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse 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 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) 2010, 2011 Wayne Meissner 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 EPL, 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 EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 package org.jruby.ext.ffi;
 import java.util.Map;
 import org.jruby.Ruby;
Represents a C enum
 @JRubyClass(name="FFI::Enum", parent="Object")
 public final class Enum extends RubyObject {
     private final IRubyObject nativeType;
     private final RubyHash kv_map;
     private volatile IRubyObject tag;
     private volatile Map<RubySymbolRubyIntegersymbolToValue = new IdentityHashMap<RubySymbolRubyInteger>();
     private volatile IntHash<RubySymbolvalueToSymbol = new IntHash<RubySymbol>();
     public static RubyClass createEnumClass(Ruby runtimeRubyModule ffiModule) {
         RubyClass enumClass = ffiModule.defineClassUnder("Enum"runtime.getObject(),
         return enumClass;
     private static final class Allocator implements ObjectAllocator {
         private static final ObjectAllocator INSTANCE = new Allocator();
         public final IRubyObject allocate(Ruby runtimeRubyClass klass) {
             return new Enum(runtimeklass);
     private Enum(Ruby runtimeRubyClass klass) {
          = runtime.getModule("FFI").getClass("Type").getConstant("INT");
          = RubyHash.newHash(runtime);
          = runtime.getNil();
     @JRubyMethod(name = "initialize", visibility = .)
     public final IRubyObject initialize(ThreadContext contextIRubyObject valuesIRubyObject tag) {
         this. = tag;
         return initialize(contextvalues);
     @JRubyMethod(name = "initialize", visibility = .)
     public final IRubyObject initialize(ThreadContext contextIRubyObject values) {
         if (!(values instanceof RubyArray)) {
             throw context.runtime.newTypeError(valuescontext.runtime.getArray());
         RubyArray ary = (RubyArrayvalues;
        Map<RubySymbolRubyIntegers2v = new IdentityHashMap<RubySymbolRubyInteger>();
        IRubyObject prevConstant = null;
        int nextValue = 0;
        for (int i = 0; i < ary.size(); i++) {
            IRubyObject v = ary.entry(i);
            if (v instanceof RubySymbol) {
                s2v.put((RubySymbolv, RubyFixnum.newFixnum(context.runtimenextValue));
                prevConstant = v;
            } else if (v instanceof RubyFixnum) {
                if (prevConstant == null) {
                    throw context.runtime.newArgumentError("invalid enum sequence - no symbol for value "
                            + v);
                s2v.put((RubySymbolprevConstant, (RubyFixnumv);
                nextValue = (int) ((RubyIntegerv).getLongValue() + 1;
            } else {
                throw context.runtime.newTypeError(vcontext.runtime.getSymbol());
         = new IdentityHashMap<RubySymbolRubyInteger>(s2v);
         = new IntHash<RubySymbol>(.size());
        for (Map.Entry<RubySymbolRubyIntegere : .entrySet()) {
            .fastASet(e.getKey(), e.getValue());
            .put((inte.getValue().getLongValue(), e.getKey());
        return this;
    @JRubyMethod(name = { "[]""find" })
    public final IRubyObject find(ThreadContext contextIRubyObject query) {
        if (query instanceof RubySymbol) {
            IRubyObject value = .fastARef(query);
            return value != null ? value : context.runtime.getNil();
        } else if (query instanceof RubyInteger) {
            RubySymbol symbol = .get((int) ((RubyIntegerquery).getLongValue());
            return symbol != null ? symbol : context.runtime.getNil();
        } else {
            return context.runtime.getNil();
    @JRubyMethod(name = { "symbol_map""to_h""to_hash" })
    public final IRubyObject symbol_map(ThreadContext context) {
        return .dup(context);
    @JRubyMethod(name = { "symbols" })
    public final IRubyObject symbols(ThreadContext context) {
        return .keys();
    @JRubyMethod(name = { "tag" })
    public final IRubyObject tag(ThreadContext context) {
        return ;
    @JRubyMethod(name = "native_type")
    public final IRubyObject native_type(ThreadContext context) {
        return ;
    @JRubyMethod(name = "to_native")
    public final IRubyObject to_native(ThreadContext contextIRubyObject nameIRubyObject ctx) {
        RubyInteger value;
        if (name instanceof RubyFixnum) {
            return name;
        } else if (name instanceof RubySymbol && (value = .get(name)) != null) {
            return value;
        } else if (name instanceof RubyInteger) {
            return name;
        } else if (name.respondsTo("to_int")) {
            return name.convertToInteger();
        } else {
            throw name.getRuntime().newArgumentError("invalid enum value, " + name.inspect());
    @JRubyMethod(name = "from_native")
    public final IRubyObject from_native(ThreadContext contextIRubyObject valueIRubyObject ctx) {
        RubySymbol sym;
        if (value instanceof RubyInteger && (sym = .get((int) ((RubyIntegervalue).getLongValue())) != null) {
            return sym;
        return value;
    @JRubyMethod(name = "reference_required?")
    public IRubyObject reference_required_p(ThreadContext context) {
        return context.runtime.getFalse();
New to GrepCode? Check out our FAQ X