<commit_before>package com.bridj;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.nio.*;

/**
 *
 * @author Olivier
 */
public abstract class PointerIO<T> {
	final Type targetType;
	final Class<?> typedPointerClass;
	final int targetSize, targetAlignment = -1;
	
	public PointerIO(Type targetType, int targetSize, Class<?> typedPointerClass) {
		this.targetType = targetType;
		this.targetSize = targetSize;
		this.typedPointerClass = typedPointerClass;
	}
	abstract T get(Pointer<T> pointer, long byteOffset);
	abstract void set(Pointer<T> pointer, long byteOffset, T value);
	
	PointerIO<Pointer<T>> getReferenceIO() {
		return new CommonPointerIOs.PointerPointerIO<T>(this);
	}
	public int getTargetSize() {
		return targetSize;
	}
	public int getTargetAlignment() { 
		return targetAlignment < 0 ? getTargetSize() : targetAlignment;
	}
	public boolean isTypedPointer() {
		return typedPointerClass != null;
	}
	public Class<?> getTypedPointerClass() {
		return typedPointerClass;
	}
	public Type getTargetType() {
		return targetType;
	}
	
	static Class<?> getClass(Type type) {
		if (type instanceof Class<?>)
			return (Class<?>)type;
		if (type instanceof ParameterizedType)
			return getClass(((ParameterizedType)type).getRawType());
		return null;
	}
	
	static PointerIO pointerInstance;
    public synchronized static PointerIO<Pointer> getPointerInstance() {
        if (pointerInstance == null)
            pointerInstance = getPointerInstance((PointerIO<?>)null);
        return pointerInstance;
    }
    
	public static <T> PointerIO<Pointer<T>> getPointerInstance(Type target) {
		return getPointerInstance((PointerIO<T>)getInstance(target));
	}
	public static <T> PointerIO<Pointer<T>> getPointerInstance(PointerIO<T> targetIO) {
		return new CommonPointerIOs.PointerPointerIO<T>(targetIO);
	}
    public synchronized static <S extends StructObject> PointerIO<S> getInstance(StructIO s) {
        return new CommonPointerIOs.StructPointerIO(s);
    }
    static Map<Type, PointerIO<?>> ios = new HashMap<Type, PointerIO<?>>();
	public synchronized static <P> PointerIO<P> getInstance(Type type) {
		PointerIO io = ios.get(type);
        if (io == null) {
            final Class<?> cl = (type instanceof Class) ? (Class)type : (type instanceof ParameterizedType) ? (Class)((ParameterizedType)type).getRawType() : null;
    	
            #foreach ($prim in $primitivesNoBool)
            #if ($velocityCount > 1) else #end
            if (type == ${prim.WrapperName}.TYPE || type == ${prim.WrapperName}.class)
                io = CommonPointerIOs.${prim.Name}IO;
            #end
			else if (cl != null && Pointer.class.equals(cl))
                io = getPointerInstance();
            else if (cl != null && TypedPointer.class.isAssignableFrom(cl))
            	io = new CommonPointerIOs.TypedPointerPointerIO((Class<? extends TypedPointer>)cl);
            else if (cl != null && SizeT.class.isAssignableFrom(cl))
                io = CommonPointerIOs.sizeTIO;
			else if (cl != null && StructObject.class.isAssignableFrom(cl))
				io = getInstance(StructIO.getInstance((Class)cl, type, (CRuntime)BridJ.getRuntime(cl)));
            else if (cl != null && Callback.class.isAssignableFrom(cl))
				io = new CommonPointerIOs.CallbackPointerIO(cl);
            else
            throw new UnsupportedOperationException("Cannot create pointer io to type " + type + ((type instanceof Class) && ((Class)type).getSuperclass() != null ? " (parent type : " + ((Class)type).getSuperclass().getName() + ")" : ""));
            	//return null; // TODO throw here ?

            if (io == null)
            	throw new RuntimeException("Failed to create pointer io to type " + type);
            ios.put(type, io);
        }
        return io;
    }

    static PointerIO<SizeT> sizeTInstance;

    public static PointerIO<SizeT> getSizeTInstance() {
        if (sizeTInstance == null)
            sizeTInstance = getInstance(SizeT.class);
        return sizeTInstance;
	}

    #foreach ($prim in $primitivesNoBool)
    static PointerIO<${prim.WrapperName}> ${prim.Name}Instance;
    #end

    #foreach ($prim in $primitivesNoBool)
    public static PointerIO<${prim.WrapperName}> get${prim.CapName}Instance() {
        if (${prim.Name}Instance == null)
            ${prim.Name}Instance = getInstance(${prim.WrapperName}.class);<commit_msg>BridJ: PointerIO.getInstance no longer throws.<commit_after>package com.bridj;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.nio.*;

/**
 *
 * @author Olivier
 */
public abstract class PointerIO<T> {
	final Type targetType;
	final Class<?> typedPointerClass;
	final int targetSize, targetAlignment = -1;
	
	public PointerIO(Type targetType, int targetSize, Class<?> typedPointerClass) {
		this.targetType = targetType;
		this.targetSize = targetSize;
		this.typedPointerClass = typedPointerClass;
	}
	abstract T get(Pointer<T> pointer, long byteOffset);
	abstract void set(Pointer<T> pointer, long byteOffset, T value);
	
	PointerIO<Pointer<T>> getReferenceIO() {
		return new CommonPointerIOs.PointerPointerIO<T>(this);
	}
	public int getTargetSize() {
		return targetSize;
	}
	public int getTargetAlignment() { 
		return targetAlignment < 0 ? getTargetSize() : targetAlignment;
	}
	public boolean isTypedPointer() {
		return typedPointerClass != null;
	}
	public Class<?> getTypedPointerClass() {
		return typedPointerClass;
	}
	public Type getTargetType() {
		return targetType;
	}
	
	static Class<?> getClass(Type type) {
		if (type instanceof Class<?>)
			return (Class<?>)type;
		if (type instanceof ParameterizedType)
			return getClass(((ParameterizedType)type).getRawType());
		return null;
	}
	
	static PointerIO pointerInstance;
    public synchronized static PointerIO<Pointer> getPointerInstance() {
        if (pointerInstance == null)
            pointerInstance = getPointerInstance((PointerIO<?>)null);
        return pointerInstance;
    }
    
	public static <T> PointerIO<Pointer<T>> getPointerInstance(Type target) {
		return getPointerInstance((PointerIO<T>)getInstance(target));
	}
	public static <T> PointerIO<Pointer<T>> getPointerInstance(PointerIO<T> targetIO) {
		return new CommonPointerIOs.PointerPointerIO<T>(targetIO);
	}
    public synchronized static <S extends StructObject> PointerIO<S> getInstance(StructIO s) {
        return new CommonPointerIOs.StructPointerIO(s);
    }
    static Map<Type, PointerIO<?>> ios = new HashMap<Type, PointerIO<?>>();
	public synchronized static <P> PointerIO<P> getInstance(Type type) {
		PointerIO io = ios.get(type);
        if (io == null) {
            final Class<?> cl = (type instanceof Class) ? (Class)type : (type instanceof ParameterizedType) ? (Class)((ParameterizedType)type).getRawType() : null;
    	
            #foreach ($prim in $primitivesNoBool)
            #if ($velocityCount > 1) else #end
            if (type == ${prim.WrapperName}.TYPE || type == ${prim.WrapperName}.class)
                io = CommonPointerIOs.${prim.Name}IO;
            #end
			else if (cl != null && Pointer.class.equals(cl))
                io = getPointerInstance();
            else if (cl != null && TypedPointer.class.isAssignableFrom(cl))
            	io = new CommonPointerIOs.TypedPointerPointerIO((Class<? extends TypedPointer>)cl);
            else if (cl != null && SizeT.class.isAssignableFrom(cl))
                io = CommonPointerIOs.sizeTIO;
			else if (cl != null && StructObject.class.isAssignableFrom(cl))
				io = getInstance(StructIO.getInstance((Class)cl, type, (CRuntime)BridJ.getRuntime(cl)));
            else if (cl != null && Callback.class.isAssignableFrom(cl))
				io = new CommonPointerIOs.CallbackPointerIO(cl);
            //else
            //throw new UnsupportedOperationException("Cannot create pointer io to type " + type + ((type instanceof Class) && ((Class)type).getSuperclass() != null ? " (parent type : " + ((Class)type).getSuperclass().getName() + ")" : ""));
            	//return null; // TODO throw here ?

            //if (io == null)
            //	throw new RuntimeException("Failed to create pointer io to type " + type);
            ios.put(type, io);
        }
        return io;
    }

    static PointerIO<SizeT> sizeTInstance;

    public static PointerIO<SizeT> getSizeTInstance() {
        if (sizeTInstance == null)
            sizeTInstance = getInstance(SizeT.class);
        return sizeTInstance;
	}

    #foreach ($prim in $primitivesNoBool)
    static PointerIO<${prim.WrapperName}> ${prim.Name}Instance;
    #end

    #foreach ($prim in $primitivesNoBool)
    public static PointerIO<${prim.WrapperName}> get${prim.CapName}Instance() {
        if (${prim.Name}Instance == null)
            ${prim.Name}Instance = getInstance(${prim.WrapperName}.class);
{'fraction_non_alphanumeric': 0.12507061349000112, 'fraction_numerical': 0.0006778895040108463, 'mean_word_length': 3.5232498722534493, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961017', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2540, 'n_words': 633}