All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jnr.ffi.provider.jffi.StructByReferenceResultConverterFactory Maven / Gradle / Ivy

There is a newer version: 2.2.16
Show newest version
package jnr.ffi.provider.jffi;

import jnr.ffi.*;
import jnr.ffi.mapper.FromNativeContext;
import jnr.ffi.mapper.FromNativeConverter;
import jnr.ffi.provider.converters.StructByReferenceFromNativeConverter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

final class StructByReferenceResultConverterFactory {
    private final Map, FromNativeConverter> converters
            = new ConcurrentHashMap, FromNativeConverter>();
    
    private final AsmClassLoader classLoader;
    private final boolean asmEnabled;

    public StructByReferenceResultConverterFactory(AsmClassLoader classLoader, boolean asmEnabled) {
        this.classLoader = classLoader;
        this.asmEnabled = asmEnabled;
    }

    public final FromNativeConverter get(Class structClass,
                                                                    FromNativeContext fromNativeContext) {
        FromNativeConverter converter = converters.get(structClass);
        if (converter == null) {
            synchronized (converters) {
                if ((converter = converters.get(structClass)) == null) {
                    converters.put(structClass, converter = createConverter(fromNativeContext.getRuntime(), structClass, fromNativeContext));
                }
            }
        }

        return converter;
    }
    
    private FromNativeConverter createConverter(jnr.ffi.Runtime runtime,
                                                                           Class structClass,
                                                                           FromNativeContext fromNativeContext) {
        return asmEnabled
            ? AsmStructByReferenceFromNativeConverter.newStructByReferenceConverter(runtime, structClass, 0, classLoader)
            : StructByReferenceFromNativeConverter.getInstance(structClass, fromNativeContext);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy