From 23f4c6347ea24cf619dba573e83790e73d81d5ad Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 23 Jun 2023 07:09:24 +0200 Subject: GlueGen Struct [16]: Add support for pointer-pointer and function-pointer values See documentation and unit test test2.h, Test2FuncPtr.java and Test3PtrStorage.java --- .../test/junit/generation/Test2FuncPtr.java | 16 +++ .../test/junit/generation/Test3PtrStorage.java | 139 +++++++++++++++++++++ .../jogamp/gluegen/test/junit/generation/test2.c | 38 +++++- .../jogamp/gluegen/test/junit/generation/test2.cfg | 9 +- .../jogamp/gluegen/test/junit/generation/test2.h | 43 +++++-- 5 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java (limited to 'src/junit/com') diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java index 66c7178..3503fa3 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java @@ -114,6 +114,14 @@ public class Test2FuncPtr extends BaseClass { Assert.assertEquals(404, ud2.getBalance()); Assert.assertEquals("Jane Doe", ud2.getName()); } + // Check func-ptr are original + { + final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); + final long funcB1 = options.getCustomFuncB1(); + final long funcB2 = options.getCustomFuncB2(); + Assert.assertEquals(funcBOrigs[0], funcB1); + Assert.assertEquals(funcBOrigs[1], funcB2); + } Assert.assertEquals(101, options.CustomFuncB1(ud1)); Assert.assertEquals(404, options.CustomFuncB1(ud2)); Assert.assertEquals(-101, options.CustomFuncB2(ud1)); @@ -125,6 +133,14 @@ public class Test2FuncPtr extends BaseClass { options.setCustomFuncB1(funcB2); options.setCustomFuncB2(funcB1); } + // Check func-ptr are switched + { + final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); + final long funcB1 = options.getCustomFuncB1(); + final long funcB2 = options.getCustomFuncB2(); + Assert.assertEquals(funcBOrigs[1], funcB1); + Assert.assertEquals(funcBOrigs[0], funcB2); + } Assert.assertEquals(-101, options.CustomFuncB1(ud1)); Assert.assertEquals(-404, options.CustomFuncB1(ud2)); Assert.assertEquals(101, options.CustomFuncB2(ud1)); diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java new file mode 100644 index 0000000..2bf82a8 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java @@ -0,0 +1,139 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import java.io.IOException; +import java.nio.IntBuffer; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.common.nio.ElementBuffer; +import com.jogamp.common.os.NativeLibrary; +import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2Impl; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +/** + * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer.. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class Test3PtrStorage extends BaseClass { + + static NativeLibrary dynamicLookupHelper; + + /** + * Verifies loading of the new library. + */ + @BeforeClass + public static void chapter__TestLoadLibrary() throws Exception { + BindingJNILibLoader.loadBindingtest2(); + dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test2FuncPtr.class.getClassLoader(), true); + Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); + + Bindingtest2Impl.resetProcAddressTable(dynamicLookupHelper); + } + + /** + * Verifies unloading of the new library. + */ + @AfterClass + public static void chapter0XTestUnloadLibrary() throws Exception { + Assert.assertNotNull(dynamicLookupHelper); + dynamicLookupHelper.close(); + dynamicLookupHelper = null; + } + + + /** + * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer + */ + @Test + public void chapter01() throws Exception { + Assert.assertEquals(false, T2_PointerStorage.usesNativeCode()); + + final Bindingtest2 bt2 = new Bindingtest2Impl(); + final T2_PointerStorage store = bt2.createT2PointerStorage(); + // final T2_PointerStorage store = T2_PointerStorage.create(); + final long[] int32PtrArray = store.getInt32PtrArray(0, new long[10], 0, 10); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + { + Assert.assertEquals(10, int32PtrArray.length); + System.err.print("int32PtrArray[10] = { "); + for(int i=0; i 3, 4, 5, 6, 7, 8, 9 + store.setInt32PtrPtr(true, int32PtrArray, 8, 3, 2); // -> 3, 4, 5, 8, 9, 8, 9 + store.setInt32PtrPtr(true, int32PtrArray, 0, 5, 2); // -> 3, 4, 5, 8, 9, 0, 1 + final long[] int32PtrPtr = store.getInt32PtrPtr(0, new long[7], 0, 7); // 3, 4, 5, 8, 9, 0, 1 + { + System.err.print("int32PtrPtr[7] = { "); + for(int i=0; i #include #include #include +typedef struct { + int value; +} T2_UndefStruct; + +#include "test2.h" + static int32_t CustomFuncA1(void* aptr) { (void)aptr; return 0xa001; @@ -33,6 +38,8 @@ int Initialize(T2_InitializeOptions* Options) { *( (T2_CustomFuncA*) &Options->CustomFuncA2 ) = CustomFuncA2; // yuck: real yuck Options->CustomFuncB1 = CustomFuncB1; Options->CustomFuncB2 = CustomFuncB2; + Options->customFuncBVariants[0] = CustomFuncB1; + Options->customFuncBVariants[1] = CustomFuncB2; Options->OverrideThreadAffinity = NULL; } @@ -52,4 +59,31 @@ int Release(T2_InitializeOptions* Options) { Options->CustomFuncB2 = NULL; } +static int32_t StaticInt32Array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; +static T2_UndefStruct StaticUndefStructArray[] = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 } }; + +T2_PointerStorage * createT2PointerStorage() { + T2_PointerStorage * s = calloc(1, sizeof(T2_PointerStorage)); + for(int i=0; i<10; ++i) { + s->int32PtrArray[i] = &StaticInt32Array[i]; + } + s->undefStructPtr = &StaticUndefStructArray[0]; + for(int i=0; i<10; ++i) { + s->undefStructPtrArray[i] = &StaticUndefStructArray[i]; + } + + for(int i=0; i<10; ++i) { + s->customFuncAVariantsArray[i] = ( i %2 == 0 ) ? CustomFuncA1 : CustomFuncA2; + } + for(int i=0; i<10; ++i) { + s->customFuncBVariantsArray[i] = ( i %2 == 0 ) ? CustomFuncB1 : CustomFuncB2; + } + return s; +} + +void destroyT2PointerStorage(T2_PointerStorage * s) { + assert(NULL!=s); + memset(s, 0, sizeof(T2_PointerStorage)); + free(s); +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg index 7c31341..d40ac9a 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg @@ -20,7 +20,10 @@ ForceProcAddressGen __ALL__ # LocalProcAddressCallingConvention __ALL__ MYAPIENTRY Opaque long void* -Opaque long T2_Anonymous* + +# Undefined struct forward declaration, implementation secret: 'struct T2_UndefStruct;' +Opaque long T2_UndefStruct* +Ignore T2_UndefStruct EmitStruct T2_ThreadAffinity StructPackage T2_ThreadAffinity com.jogamp.gluegen.test.junit.generation @@ -29,6 +32,9 @@ EmitStruct T2_UserData StructPackage T2_UserData com.jogamp.gluegen.test.junit.generation ReturnsStringOnly T2_UserData.name +EmitStruct T2_PointerStorage +StructPackage T2_PointerStorage com.jogamp.gluegen.test.junit.generation + EmitStruct T2_InitializeOptions StructPackage T2_InitializeOptions com.jogamp.gluegen.test.junit.generation ReturnsStringOnly T2_InitializeOptions.ProductName @@ -40,6 +46,7 @@ MaxOneElement T2_InitializeOptions.OverrideThreadAffinity CustomCCode #include "test2.h" Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 +Import com.jogamp.gluegen.test.junit.generation.T2_PointerStorage Import com.jogamp.gluegen.test.junit.generation.T2_InitializeOptions Import com.jogamp.gluegen.test.junit.generation.T2_ThreadAffinity Import com.jogamp.gluegen.test.junit.generation.T2_UserData diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h index 0817dd9..75c7ffa 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h @@ -2,23 +2,43 @@ #include #include -typedef struct T2_Anonymous* T2_AnonPtr; +// Opaque long T2_UndefStruct* +// struct T2_UndefStruct; // undefined struct forward declaration, implementation secret +typedef struct T2_UndefStruct* T2_UndefStructPtr; -typedef struct { - int32_t ApiVersion; - uint64_t NetworkWork; - T2_AnonPtr anonPtr; -} T2_ThreadAffinity; +typedef int32_t ( * T2_CustomFuncA)(void* aptr); typedef struct { int32_t balance; const char* name; } T2_UserData; -typedef int32_t ( * T2_CustomFuncA)(void* aptr); - typedef int32_t ( * T2_CustomFuncB)(T2_UserData* pUserData); +typedef struct { + int32_t* int32PtrArray[10]; + int32_t** int32PtrPtr; + + T2_UndefStructPtr undefStructPtr; + T2_UndefStructPtr undefStructPtrArray[10]; + T2_UndefStructPtr* undefStructPtrPtr; + const T2_UndefStructPtr* constUndefStructPtrPtr; + + T2_CustomFuncA customFuncAVariantsArray[10]; + T2_CustomFuncA* customFuncAVariantsArrayPtr; + + T2_CustomFuncB customFuncBVariantsArray[10]; + T2_CustomFuncB* customFuncBVariantsArrayPtr; +} T2_PointerStorage; + +T2_PointerStorage * createT2PointerStorage(); +void destroyT2PointerStorage(T2_PointerStorage * s); + +typedef struct { + int32_t ApiVersion; + uint64_t NetworkWork; +} T2_ThreadAffinity; + typedef struct { const char* ProductName; const char* ProductVersion; @@ -30,9 +50,16 @@ typedef struct { const T2_CustomFuncA CustomFuncA2; T2_CustomFuncB CustomFuncB1; T2_CustomFuncB CustomFuncB2; + T2_CustomFuncB customFuncBVariants[2]; T2_ThreadAffinity* OverrideThreadAffinity; } T2_InitializeOptions; extern int Initialize(T2_InitializeOptions* Options); extern int Release(T2_InitializeOptions* Options); + +typedef int32_t ( * T2_CallbackFunc)(size_t id, size_t msg_len, const char* msg, void* userParam); + +void AddMessageCallback(T2_CallbackFunc func, void* userParam); +void RemoveMessageCallback(T2_CallbackFunc func, void* userParam); +void InjectMessageCallback(size_t id, size_t msg_len, const char* msg); -- cgit v1.2.3