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

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

There is a newer version: 2.2.17
Show newest version
/*
 * Copyright (C) 2011 Wayne Meissner
 *
 * This file is part of the JNR project.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jnr.ffi.provider.jffi;

import jnr.ffi.Pointer;
import jnr.ffi.mapper.ToNativeContext;
import jnr.ffi.mapper.ToNativeConverter;
import jnr.ffi.mapper.TypeMapper;
import jnr.ffi.provider.ClosureManager;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
final class NativeClosureManager implements ClosureManager {
    private volatile Map, NativeClosureFactory> factories = new HashMap, NativeClosureFactory>();
    private final NativeRuntime runtime;
    private final TypeMapper typeMapper;

    NativeClosureManager(NativeRuntime runtime, TypeMapper typeMapper) {
        this.runtime = runtime;
        this.typeMapper = typeMapper;
    }

    public  T newClosure(Class closureClass, T instance) {
        NativeClosureFactory factory = factories.get(closureClass);
        if (factory != null) {
            //return factory.newClosure(instance);
        }
        return null;
    }

    synchronized  NativeClosureFactory getClosureFactory(Class closureClass) {
        NativeClosureFactory factory = factories.get(closureClass);
        if (factory != null) {
            return factory;
        }

        factory = NativeClosureFactory.newClosureFactory(runtime, closureClass, typeMapper);
        factories.put(closureClass, factory);

        return factory;
    }

     ToNativeConverter newClosureSite(Class closureClass) {
        return new ClosureSite(getClosureFactory(closureClass));
    }

    private static final class ClosureSite implements ToNativeConverter {
        private final NativeClosureFactory factory;
        private NativeClosureFactory.ClosureReference closureReference = null;

        private ClosureSite(NativeClosureFactory factory) {
            this.factory = factory;
        }

        public Pointer toNative(T value, ToNativeContext context) {
            NativeClosureFactory.ClosureReference ref = closureReference;
            if (ref != null && ref.getCallable() == value) {
                return ref.getPointer();
            }

            ref = factory.getClosureReference(value);
            if (closureReference == null || closureReference.get() == null) {
                closureReference = ref;
            }

            return ref.getPointer();
        }

        public Class nativeType() {
            return Pointer.class;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy