-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can extend PointerWrapper for typesafe pointer. Example: public interface TestLib { CustomPointer ptr_malloc(@size_t int size); void ptr_free(CustomPointer ptr); class CustomPointer extends PointerWrapper { private CustomPointer(Pointer pointer) { super(pointer); } } } }
- Loading branch information
Showing
5 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package jnr.ffi; | ||
|
||
/** | ||
* A wrapper on {@link jnr.ffi.Pointer} | ||
* <p> | ||
* Extend to use as typesafe pointer. Example: | ||
* <pre> | ||
* {@code | ||
* public interface TestLib { | ||
* CustomPointer ptr_malloc(@size_t int size); | ||
* void ptr_free(CustomPointer ptr); | ||
* | ||
* class CustomPointer extends PointerWrapper { | ||
* private CustomPointer(Pointer pointer) { | ||
* super(pointer); | ||
* } | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public abstract class PointerWrapper { | ||
private final Pointer pointer; | ||
|
||
protected PointerWrapper(Pointer pointer) { | ||
this.pointer = pointer; | ||
} | ||
|
||
public Pointer pointer() { | ||
return pointer; | ||
} | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
src/main/java/jnr/ffi/provider/converters/PointerWrapperFromNativeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jnr.ffi.provider.converters; | ||
|
||
import jnr.ffi.Pointer; | ||
import jnr.ffi.PointerWrapper; | ||
import jnr.ffi.mapper.FromNativeContext; | ||
import jnr.ffi.mapper.FromNativeConverter; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
/** | ||
* @author Andrew Yefanov. | ||
* @since 30.08.2017. | ||
*/ | ||
public class PointerWrapperFromNativeConverter implements FromNativeConverter<PointerWrapper, Pointer> { | ||
|
||
private final Constructor<PointerWrapper> constructor; | ||
|
||
private PointerWrapperFromNativeConverter(Constructor constructor) { | ||
this.constructor = constructor; | ||
} | ||
|
||
public static FromNativeConverter<PointerWrapper, Pointer> getInstance(Class<PointerWrapper> wrapperClass) { | ||
try { | ||
Constructor<PointerWrapper> constructor = wrapperClass.getConstructor(Pointer.class); | ||
if (!constructor.isAccessible()) { | ||
constructor.setAccessible(true); | ||
} | ||
return new PointerWrapperFromNativeConverter(constructor); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException(wrapperClass.getName() + " has no constructor that accepts jnr.ffi.Pointer"); | ||
} | ||
} | ||
|
||
@Override | ||
public PointerWrapper fromNative(Pointer nativeValue, FromNativeContext context) { | ||
try { | ||
return nativeValue == null ? null : constructor.newInstance(nativeValue); | ||
} catch (InstantiationException e) { | ||
throw new RuntimeException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<Pointer> nativeType() { | ||
return Pointer.class; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/jnr/ffi/provider/converters/PointerWrapperToNativeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jnr.ffi.provider.converters; | ||
|
||
import jnr.ffi.Pointer; | ||
import jnr.ffi.PointerWrapper; | ||
import jnr.ffi.mapper.ToNativeContext; | ||
import jnr.ffi.mapper.ToNativeConverter; | ||
|
||
/** | ||
* @author Andrew Yefanov. | ||
* @since 30.08.2017. | ||
*/ | ||
public class PointerWrapperToNativeConverter implements ToNativeConverter<PointerWrapper, Pointer> { | ||
|
||
public static final PointerWrapperToNativeConverter INSTANCE = new PointerWrapperToNativeConverter(); | ||
|
||
public static ToNativeConverter<PointerWrapper, Pointer> getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public Pointer toNative(PointerWrapper value, ToNativeContext context) { | ||
return value == null ? null : value.pointer(); | ||
} | ||
|
||
@Override | ||
public Class<Pointer> nativeType() { | ||
return Pointer.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jnr.ffi; | ||
|
||
import jnr.ffi.mapper.AnnotatedMappedTypeTest; | ||
import jnr.ffi.types.size_t; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.TestCase.assertSame; | ||
|
||
/** | ||
* @author Andrew Yefanov. | ||
* @since 30.08.2017. | ||
*/ | ||
public class PointerWrapperTest { | ||
private static final class CustomPointer extends PointerWrapper { | ||
private CustomPointer(Pointer pointer) { | ||
super(pointer); | ||
} | ||
} | ||
|
||
public static interface TestLib { | ||
CustomPointer ptr_malloc(@size_t int size); | ||
void ptr_free(AnnotatedMappedTypeTest.CustomPointer ptr); | ||
} | ||
|
||
static AnnotatedMappedTypeTest.TestLib testlib; | ||
static Runtime runtime; | ||
|
||
@BeforeClass | ||
public static void setUpClass() throws Exception { | ||
testlib = TstUtil.loadTestLib(AnnotatedMappedTypeTest.TestLib.class); | ||
runtime = Runtime.getRuntime(testlib); | ||
} | ||
|
||
@Test | ||
public void returnsInstanceOfCorrectClass() { | ||
assertSame(AnnotatedMappedTypeTest.CustomPointer.class, testlib.ptr_malloc(1).getClass()); | ||
} | ||
|
||
@Test public void toNative() { | ||
testlib.ptr_free(testlib.ptr_malloc(1)); | ||
} | ||
|
||
} |