diff options
author | Sven Gothel <[email protected]> | 2023-07-04 11:36:29 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-07-04 11:36:29 +0200 |
commit | a599b852a041ba3d80b43981589ac1390979dac2 (patch) | |
tree | 038c5e3a6a03dae16ae83893ae2a91e76357daa7 /src/junit | |
parent | 6d53b4b1dd07006e7af0e540b2c2e6ee6e1746d5 (diff) |
GlueGen JavaCallback: Revised: Static Java callback dispatcher, dropping native heap, support Struct UserParam ...
Implementation now generates a static Java callback dispatcher for each defined SetCallbackFunction, which gets invoked by the generated native static counterpart with all arguments required.
The static callback utilizes its own synchronization for thread-safety and fetches the required data set stored at SetCallbackFunction to dispatch the call to the users' CallbackFunction.
In case the callback has been removed already, the static callback simply bails out quietly.
The native code does not create, release or manage heap memory and therefore is considered safe.
+++
Further Struct Type UserParam are now supported including Heterogeneous UserParam mapping (read GlueGen_Mapping.*).
+++
Cleaned up code by extracting all JavaCallback emitter code into JavaCallbackEmitter class in one place,
leaving JavaMethodbindingEmitter and CMethodbindingEmitter mostly in their original stage (non-convoluted).
In this regard, I had to refactor a few function, i.e. moving CMethodbindingEmitter.getJNIMangledArg(..)
into JavaType.appendDescriptor(..) and JavaType.appendJNIDescriptor(..) while reusing the toJNIMethodDescriptor(..) conversion.
Test4JavaCallback covers and passes all cases.
Diffstat (limited to 'src/junit')
4 files changed, 517 insertions, 27 deletions
diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test4JavaCallback.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4JavaCallback.java index 406008a..8c1d726 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/Test4JavaCallback.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4JavaCallback.java @@ -34,7 +34,10 @@ import java.util.Set; import com.jogamp.common.os.NativeLibrary; import com.jogamp.gluegen.test.junit.generation.Bindingtest2.ALBUFFERCALLBACKTYPESOFT; import com.jogamp.gluegen.test.junit.generation.Bindingtest2.AlBufferCallback0Key; +import com.jogamp.gluegen.test.junit.generation.Bindingtest2.MessageCallback11aKey; +import com.jogamp.gluegen.test.junit.generation.Bindingtest2.MessageCallback11bKey; import com.jogamp.gluegen.test.junit.generation.Bindingtest2.T2_CallbackFunc01; +import com.jogamp.gluegen.test.junit.generation.Bindingtest2.T2_CallbackFunc11; import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2Impl; import org.junit.AfterClass; @@ -197,8 +200,9 @@ public class Test4JavaCallback extends BaseClass { @Override public void callback(final int buffer, final Object userptr, final int sampledata, final int numbytes) { final MyUserParam02 myUserParam = (MyUserParam02)userptr; - id_res[0] = sampledata + numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata + numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res; myUserParam.buffer = buffer; System.err.println("chapter02.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); } @@ -207,8 +211,9 @@ public class Test4JavaCallback extends BaseClass { @Override public void callback(final int buffer, final Object userptr, final int sampledata, final int numbytes) { final MyUserParam02 myUserParam = (MyUserParam02)userptr; - id_res[0] = sampledata * numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata * numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res; myUserParam.buffer = buffer; System.err.println("chapter02.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); } @@ -402,8 +407,9 @@ public class Test4JavaCallback extends BaseClass { @Override public void callback(final int buffer, final Object userptr, final int sampledata, final int numbytes) { final MyUserParam02 myUserParam = (MyUserParam02)userptr; - id_res[0] = sampledata + numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata + numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res;; myUserParam.buffer = buffer; System.err.println("chapter03.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); } @@ -412,8 +418,9 @@ public class Test4JavaCallback extends BaseClass { @Override public void callback(final int buffer, final Object userptr, final int sampledata, final int numbytes) { final MyUserParam02 myUserParam = (MyUserParam02)userptr; - id_res[0] = sampledata * numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata * numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res; myUserParam.buffer = buffer; System.err.println("chapter03.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); } @@ -633,8 +640,9 @@ public class Test4JavaCallback extends BaseClass { myUserParam.throwPreAction = false; throw new RuntimeException("Exception test.pre: chapter04.myCallback01"); } - id_res[0] = sampledata + numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata + numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res; myUserParam.buffer = buffer; System.err.println("chapter04.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); if( myUserParam.throwPostAction ) { @@ -651,8 +659,9 @@ public class Test4JavaCallback extends BaseClass { myUserParam.throwPreAction = false; throw new RuntimeException("Exception test.pre: chapter04.myCallback02"); } - id_res[0] = sampledata * numbytes + myUserParam.i; - myUserParam.j = id_res[0]; + final long res = sampledata * numbytes + myUserParam.i; + id_res[0] = res; + myUserParam.j = res; myUserParam.buffer = buffer; System.err.println("chapter04.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); if( myUserParam.throwPostAction ) { @@ -872,6 +881,404 @@ public class Test4JavaCallback extends BaseClass { } } + /** + * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11a() + * using the default MessageCallback11aKey class. + */ + @Test + public void chapter11a() throws Exception { + final Bindingtest2 bt2 = new Bindingtest2Impl(); + + final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; + final long userParam02Ptr = 0xC0FFEEDEADBEAFL; + + final long[] id_res = { -1 }; + final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val + usrParam.getI(); + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11a.myCallback01: id "+id+", val "+val); + } + }; + final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val * usrParam.getI(); + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11a.myCallback02: id "+id+", val "+val); + } + }; + final int id1 = 1; + final int id2 = 2; + final int id3 = 3; + final MessageCallback11aKey id1Key = new MessageCallback11aKey(id1); + final MessageCallback11aKey id2Key = new MessageCallback11aKey(id2); + final MessageCallback11aKey id3Key = new MessageCallback11aKey(id3); + final T2_Callback11UserType userParam01 = T2_Callback11UserType.create(); // native toolkit should set API version + userParam01.setData(userParam01Ptr); + userParam01.setI(1); + final T2_Callback11UserType userParam02 = T2_Callback11UserType.create(); // native toolkit should set API version + userParam02.setData(userParam02Ptr); + userParam02.setI(2); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 0, userParam01.getR()); + Assert.assertEquals( 0, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 0, userParam02.getR()); + Assert.assertEquals( 0, userParam02.getId()); + + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); + + // 1st mapping: buffer1 -> myCallback01, userParam01 + bt2.MessageCallback11a(id1, myCallback01, userParam01); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); + { + final Set<MessageCallback11aKey> keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, userParam02 + bt2.MessageCallback11a(id2, myCallback02, userParam02); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(myCallback02, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(2, bt2.getMessageCallback11aKeys().size()); + { + final Set<MessageCallback11aKey> keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(true, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11aInject(id1, 10); // buffer1 -> myCallback01, userParam01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 10+1, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 10+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 0, userParam02.getR()); + Assert.assertEquals( 0, userParam02.getId()); + } + { + bt2.MessageCallback11aInject(id2, 10); // buffer2 -> myCallback02, userParam02 + Assert.assertEquals( 10*2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 10+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 10*2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + + // Switch the callback function for buffer2 -> myCallback01, userParam02 + bt2.MessageCallback11a(id2, myCallback01, userParam02); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + + { + bt2.MessageCallback11aInject(id1, 11); // buffer1 -> myCallback01, userParam01 + Assert.assertEquals( 11+1, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 10*2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11aInject(id2, 22); // buffer2 -> myCallback01, userParam02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 22+2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 22+2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + + // Just release the buffer2 callback and mapped resources + bt2.MessageCallback11a(id2, null, userParam02); // usrptr is not key, only id is key! + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); + { + final Set<MessageCallback11aKey> keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.MessageCallback11a(id1, null, null); // usrptr is not key, only id is key! + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); + { + final Set<MessageCallback11aKey> keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(false, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + bt2.MessageCallback11aInject(id2, 5); // unmapped, no change in data + Assert.assertEquals( 22+2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 22+2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + } + + /** + * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11b() + * using the default MessageCallback11bKey class. + */ + @Test + public void chapter11b() throws Exception { + final Bindingtest2 bt2 = new Bindingtest2Impl(); + + final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; + final long userParam02Ptr = 0xC0FFEEDEADBEAFL; + + final long[] id_res = { -1 }; + final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val + id; + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11b.myCallback01: id "+id+", val "+val); + } + }; + final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val * id; + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11b.myCallback02: id "+id+", val "+val); + } + }; + final int id1 = 1; + final int id2 = 2; + final int id3 = 3; + final MessageCallback11bKey id1Key = new MessageCallback11bKey(id1); + final MessageCallback11bKey id2Key = new MessageCallback11bKey(id2); + final MessageCallback11bKey id3Key = new MessageCallback11bKey(id3); + + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); + + // 1st mapping: buffer1 -> myCallback01, userParam01Ptr + bt2.MessageCallback11b(id1, myCallback01, userParam01Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); + { + final Set<MessageCallback11bKey> keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, userParam02Ptr + bt2.MessageCallback11b(id2, myCallback02, userParam02Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(2, bt2.getMessageCallback11bKeys().size()); + { + final Set<MessageCallback11bKey> keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(true, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11bInject(id1, 10); // buffer1 -> myCallback01, userParam01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 10+1, id_res[0]); + } + { + bt2.MessageCallback11bInject(id2, 10); // buffer2 -> myCallback02, userParam02 + Assert.assertEquals( 10*2, id_res[0]); + } + + // Switch the callback function for buffer2 -> myCallback01, userParam02Ptr + bt2.MessageCallback11b(id2, myCallback01, userParam02Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + + { + bt2.MessageCallback11bInject(id1, 11); // buffer1 -> myCallback01, userParam01 + Assert.assertEquals( 11+1, id_res[0]); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11bInject(id2, 22); // buffer2 -> myCallback01, userParam02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 22+2, id_res[0]); + } + + // Just release the buffer2 callback and mapped resources + bt2.MessageCallback11b(id2, null, userParam02Ptr); // usrptr is not key, only id is key! + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); + { + final Set<MessageCallback11bKey> keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.MessageCallback11b(id1, null, 0); // usrptr is not key, only id is key! + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); + { + final Set<MessageCallback11bKey> keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(false, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + bt2.MessageCallback11bInject(id2, 5); // unmapped, no change in data + Assert.assertEquals( 22+2, id_res[0]); + } + } + public static class CustomMessageCallback11Key { public CustomMessageCallback11Key() { } diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.c b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.c index 7a7cb43..28fb3aa 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.c +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.c @@ -34,16 +34,16 @@ T2_PointerStorage * createT2PointerStorage() { for(int i=0; i<10; ++i) { s->int32PtrArray[i] = &StaticInt32Array[i]; } - s->undefStructPtr = &StaticUndefStructArray[0]; + s->undefStructPtr = (T2_UndefStructPtr) &StaticUndefStructArray[0]; for(int i=0; i<10; ++i) { - s->undefStructPtrArray[i] = &StaticUndefStructArray[i]; + s->undefStructPtrArray[i] = (T2_UndefStructPtr) &StaticUndefStructArray[i]; } for(int i=0; i<10; ++i) { - s->customFuncAVariantsArray[i] = ( i %2 == 0 ) ? CustomFuncA1 : CustomFuncA2; + s->customFuncAVariantsArray[i] = (T2_CustomFuncA) ( ( i %2 == 0 ) ? CustomFuncA1 : CustomFuncA2 ); } for(int i=0; i<10; ++i) { - s->customFuncBVariantsArray[i] = ( i %2 == 0 ) ? CustomFuncB1 : CustomFuncB2; + s->customFuncBVariantsArray[i] = (T2_CustomFuncB) ( ( i %2 == 0 ) ? CustomFuncB1 : CustomFuncB2 ); } return s; } @@ -61,8 +61,8 @@ int Initialize(T2_InitializeOptions* Options) { strncpy((char*)Options->ProductVersion, "Product Version", 100); // yuck: nonsense-warning Options->ApiVersion = 1; Options->Reserved1 = (void*) 0x0000CAFFEEBEEFUL; - Options->CustomFuncA1 = CustomFuncA1; - *( (T2_CustomFuncA*) &Options->CustomFuncA2 ) = CustomFuncA2; // yuck: real yuck + Options->CustomFuncA1 = (T2_CustomFuncA) CustomFuncA1; + *( (T2_CustomFuncA*) &Options->CustomFuncA2 ) = (T2_CustomFuncA) CustomFuncA2; // yuck: real yuck Options->CustomFuncB1 = CustomFuncB1; Options->CustomFuncB2 = CustomFuncB2; Options->customFuncBVariants[0] = CustomFuncB1; @@ -148,6 +148,8 @@ void alBufferCallback0(int buffer /* key */, int format, int freq, ALBUFFERCALLB void alBufferCallback0Inject(int buffer, int sampledata, int numbytes) { if( buffer < 0 || MAX_AL_BUFFER <= buffer ) { fprintf(stderr, "Error: alBufferCallback0Inject: buffer not in range [0..%d), is %d\n", MAX_AL_BUFFER, buffer); + fflush(NULL); + return; } if( NULL != alBufferCallback0_callback[buffer] ) { fprintf(stderr, "XXX alBufferCallback0Inject: buffer %d, func %p, user %p\n", buffer, alBufferCallback0_callback[buffer], alBufferCallback0_userptr[buffer]); @@ -176,6 +178,8 @@ void alBufferCallback1(int buffer /* key */, int format, int freq, ALBUFFERCALLB void alBufferCallback1Inject(int buffer, int sampledata, int numbytes) { if( buffer < 0 || MAX_AL_BUFFER <= buffer ) { fprintf(stderr, "Error: alBufferCallback1Inject: buffer not in range [0..%d), is %d\n", MAX_AL_BUFFER, buffer); + fflush(NULL); + return; } if( NULL != alBufferCallback1_callback[buffer] ) { fprintf(stderr, "XXX alBufferCallback1Inject: buffer %d, func %p, user %p\n", buffer, alBufferCallback1_callback[buffer], alBufferCallback1_userptr[buffer]); @@ -184,3 +188,69 @@ void alBufferCallback1Inject(int buffer, int sampledata, int numbytes) { } } +// +// +// + +static const int MAX_C11_BUFFER = 5; + +static T2_CallbackFunc11 MessageCallback11a_callback[] = { NULL, NULL, NULL, NULL, NULL }; +static T2_Callback11UserType* MessageCallback11a_userptr[] = { NULL, NULL, NULL, NULL, NULL }; + +void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam) { + if( id < 0 || MAX_C11_BUFFER <= id ) { + fprintf(stderr, "Error: MessageCallback11a: id not in range [0..%d), is %d\n", MAX_C11_BUFFER, id); + } else { + MessageCallback11a_callback[id] = cbFunc; + MessageCallback11a_userptr[id] = (T2_Callback11UserType*)usrParam; + if( NULL != usrParam ) { + MessageCallback11a_userptr[id]->ApiVersion = 42; + } + fprintf(stderr, "XXX MessageCallback11a id %d -> func %p, user %p\n", id, cbFunc, usrParam); + } + fflush(NULL); +} +void MessageCallback11aInject(size_t id, long val) { + if( id < 0 || MAX_C11_BUFFER <= id ) { + fprintf(stderr, "Error: MessageCallback11aInjecta: id not in range [0..%d), is %d\n", MAX_C11_BUFFER, id); + fflush(NULL); + return; + } + if( NULL != MessageCallback11a_callback[id] ) { + fprintf(stderr, "XXX MessageCallback11aInjecta: id %d, func %p, user %p\n", id, MessageCallback11a_callback[id], MessageCallback11a_userptr[id]); + fflush(NULL); + (*MessageCallback11a_callback[id])(id, MessageCallback11a_userptr[id], val); + } +} + +// +// +// + +static T2_CallbackFunc11 MessageCallback11b_callback[] = { NULL, NULL, NULL, NULL, NULL }; +static T2_Callback11UserType MessageCallback11b_userptr[]; + +void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data) { + if( id < 0 || MAX_C11_BUFFER <= id ) { + fprintf(stderr, "Error: MessageCallback11b: id not in range [0..%d), is %d\n", MAX_C11_BUFFER, id); + } else { + MessageCallback11b_callback[id] = cbFunc; + MessageCallback11b_userptr[id].ApiVersion = 42; + MessageCallback11b_userptr[id].Data = Data; + fprintf(stderr, "XXX MessageCallback11b id %d -> func %p, user %p\n", id, cbFunc, Data); + } + fflush(NULL); +} +void MessageCallback11bInject(size_t id, long val) { + if( id < 0 || MAX_C11_BUFFER <= id ) { + fprintf(stderr, "Error: MessageCallback11bInject: id not in range [0..%d), is %d\n", MAX_C11_BUFFER, id); + fflush(NULL); + return; + } + if( NULL != MessageCallback11b_callback[id] ) { + fprintf(stderr, "XXX MessageCallback11bInject: id %d, func %p, user %p\n", id, MessageCallback11b_callback[id], &MessageCallback11b_userptr[id]); + fflush(NULL); + (*MessageCallback11b_callback[id])(id, &MessageCallback11b_userptr[id], val); + } +} + 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 472471c..83045cb 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg @@ -158,14 +158,20 @@ JavaCallbackKey alBufferCallback1 0 # Begin JavaCallback # -# typedef void ( * T2_CallbackFunc11)(const T2_Callback11UserType* usrParam); -# void MessageCallback11(T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); -# void InjectMessageCallback11(size_t id, const char* msg); -ArgumentIsString InjectMessageCallback11 1 -JavaCallbackDef MessageCallback11 T2_CallbackFunc11 0 com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomMessageCallback11Key +# typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam); +# void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); +# void MessageCallback11aInject(size_t id); +#JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1 com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomMessageCallback11Key +JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1 +JavaCallbackKey MessageCallback11a 0 # # End JavaCallback +# void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data); +# void MessageCallback11bInject(size_t id); +JavaCallbackDef MessageCallback11b 2 T2_CallbackFunc11 1 +JavaCallbackKey MessageCallback11b 0 + CustomCCode #include "test2.h" Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 @@ -173,6 +179,7 @@ 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 +Import com.jogamp.gluegen.test.junit.generation.T2_Callback11UserType CustomJavaCode Bindingtest2Impl private static Bindingtest2ProcAddressTable _table = new Bindingtest2ProcAddressTable(); CustomJavaCode Bindingtest2Impl public static void resetProcAddressTable(DynamicLookupHelper lookup) { 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 d067390..183f905 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.h @@ -89,15 +89,21 @@ void alBufferCallback1(int buffer /* key */, int format, int freq, ALBUFFERCALLB void alBufferCallback1Inject(int buffer, int sampledata, int numbytes); // -// T2_CallbackFunc11 +// T2_CallbackFunc11[ab] // typedef struct { int32_t ApiVersion; void* Data; + long i; + long r; + size_t id; } T2_Callback11UserType; -typedef void ( * T2_CallbackFunc11)(const T2_Callback11UserType* usrParam); +typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam, long val); -void MessageCallback11(T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); -void InjectMessageCallback11(size_t id, const char* msg); +void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); +void MessageCallback11aInject(size_t id, long val); + +void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data); +void MessageCallback11bInject(size_t id, long val); |