aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2023-07-04 11:36:29 +0200
committerSven Gothel <[email protected]>2023-07-04 11:36:29 +0200
commita599b852a041ba3d80b43981589ac1390979dac2 (patch)
tree038c5e3a6a03dae16ae83893ae2a91e76357daa7 /doc
parent6d53b4b1dd07006e7af0e540b2c2e6ee6e1746d5 (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 'doc')
-rw-r--r--doc/GlueGen_Mapping.html302
-rw-r--r--doc/GlueGen_Mapping.md227
2 files changed, 464 insertions, 65 deletions
diff --git a/doc/GlueGen_Mapping.html b/doc/GlueGen_Mapping.html
index 706eda4..d1b5b04 100644
--- a/doc/GlueGen_Mapping.html
+++ b/doc/GlueGen_Mapping.html
@@ -449,19 +449,28 @@
<li><a href="#struct-function-pointer-support">Struct Function-Pointer
Support</a></li>
</ul></li>
- <li><a href="#java-callback-from-native-c-api-support">Java Callback
- from Native C-API Support</a>
+ <li><a href="#java-callback">Java Callback</a>
<ul>
- <li><a href="#required-libraryonload">Required
- <em>LibraryOnLoad</em></a></li>
+ <li><a href="#implementation-details">Implementation Details</a></li>
+ <li><a href="#javacallback-userparam-mapping"><em>JavaCallback</em>
+ <em>UserParam</em> Mapping</a></li>
<li><a href="#javacallback-configuration"><em>JavaCallback</em>
Configuration</a></li>
+ <li><a
+ href="#javacallback-generated-interfaces-classes-and-methods"><em>JavaCallback</em>
+ Generated Interfaces, Classes and Methods</a></li>
<li><a href="#javacallback-notes"><em>JavaCallback</em> Notes</a></li>
<li><a href="#javacallback-example-1">JavaCallback Example 1</a></li>
<li><a href="#javacallback-example-2a-default-keyclass">JavaCallback
Example 2a (Default <em>KeyClass</em>)</a></li>
<li><a href="#javacallback-example-2b-custom-keyclass">JavaCallback
Example 2b (Custom <em>KeyClass</em>)</a></li>
+ <li><a
+ href="#javacallback-example-11a-homogeneous-struct-type">JavaCallback
+ Example 11a (<em>Homogeneous Struct Type</em>)</a></li>
+ <li><a
+ href="#javacallback-example-11b-heterogeneous-pointerstruct-type">JavaCallback
+ Example 11b (<em>Heterogeneous Pointer/Struct Type</em>)</a></li>
</ul></li>
<li><a href="#misc-configurations">Misc Configurations</a>
<ul>
@@ -531,11 +540,10 @@ as <a href="#struct-mapping">map native data structures</a> to be fully
accessible from Java™ including potential calls to <a
href="#struct-function-pointer-support">embedded function
pointer</a>.</p>
-<p>GlueGen supports <a
-href="#java-callback-from-native-c-api-support">registering Java™
-callback methods</a> to receive asynchronous and off-thread native
-toolkit events, where a generated native callback function dispatches
-the events to Java™.</p>
+<p>GlueGen supports <a href="#java-callback">registering Java™ callback
+methods</a> to receive asynchronous and off-thread native toolkit
+events, where a generated native callback function dispatches the events
+to Java™.</p>
<p>GlueGen also supports <a
href="#oo-style-api-interface-mapping">producing an OO-Style API
mapping</a> like <a href="../../jogl/doc/uml/html/index.html">JOGL's
@@ -1076,7 +1084,7 @@ variable</li>
function pointer, see <a href="#struct-function-pointer-support">Struct
Function-Pointer Support</a> below.</li>
<li><em>Java Callback from Native Code</em>, see <a
-href="#java-callback-from-native-c-api-support">section below</a></li>
+href="#java-callback">section below</a></li>
</ul>
<p>A field may be a direct aggregation, i.e. instance, within the struct
including an array or a reference to a single element or array via a
@@ -1947,24 +1955,79 @@ StructPackage T2_InitializeOptions com.jogamp.gluegen.test.junit.generation</cod
/** Interface to C language function: &lt;br&gt; &lt;code&gt;int32_t CustomFuncB1(T2_UserData * pUserData)&lt;/code&gt;&lt;br&gt; */
public final int CustomFuncB1(T2_UserData pUserData) { .. } </code></pre>
-<h2 id="java-callback-from-native-c-api-support">Java Callback from
-Native C-API Support</h2>
+<h2 id="java-callback">Java Callback</h2>
<p>GlueGen supports registering Java callback methods to receive
asynchronous and off-thread native toolkit events, where a generated
native callback function dispatches the events to Java.</p>
-<h3 id="required-libraryonload">Required <em>LibraryOnLoad</em></h3>
-<p>Note that <a
-href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad Bindingtest2</code></a>
-must be specified in exactly one native code-unit. It provides code to
-allow the generated native callback-function to attach the current
-thread to the <code>JavaVM*</code> generating a new
-<code>JNIEnv*</code>in daemon mode - or just to retrieve the thread's
-<code>JNIEnv*</code>, if already attached to the
-<code>JavaVM*</code>.</p>
+<h3 id="implementation-details">Implementation Details</h3>
+<p>Implementation generates a static Java callback dispatcher for each
+defined <code>SetCallbackFunction</code>, which gets invoked by the
+generated native static counterpart with all arguments required.</p>
+<p>The <em>static callback</em> utilizes its own synchronization for
+thread-safety and fetches the required data set stored at
+<code>SetCallbackFunction</code> to dispatch the call to the users'
+<code>CallbackFunction</code>.<br />
+In case the callback has been removed already, the <em>static
+callback</em> simply bails out quietly.</p>
+<p>The native code does not create, release or manage heap memory and
+therefore is considered safe.</p>
+<h3 id="javacallback-userparam-mapping"><em>JavaCallback</em>
+<em>UserParam</em> Mapping</h3>
+<p>Usually the same <code>UserParam</code> type is used in both items
+(or hooks), <code>SetCallbackFunctionName</code> and
+<code>CallbackFunctionType</code>, which we call a homogeneous
+<code>UserParam</code> mapping.</p>
+<p>Even in a homogeneous <code>UserParam</code> mapping, handling of the
+<code>UserParam</code> value might differ in the native binding
+code.</p>
+<p>To specify a non homogeneous <code>UserParam</code> mapping, i.e.
+heterogeneous <code>UserParam</code> mapping, the <code>UserParam</code>
+index of the <code>SetCallbackFunction</code> must be <a
+href="#javacallback-configuration">set in the configuration</a>.</p>
+<p>The following mappings are supported.</p>
+<h4 id="pure-java-object-user-type-default">Pure Java Object User Type
+(default)</h4>
+<p>A pure Java <em>Object type</em> is used for both,
+<code>SetCallbackFunctionName</code> and
+<code>CallbackFunctionType</code>.</p>
+<p>It's a homogeneous <code>UserParam</code> mapping, where the native
+side receives a simple unique ID and shall not dereference the
+<em>pointer</em>.</p>
+<p>The static Java callback dispatcher fetches the Java
+<code>UserParam</code> <em>Object</em> from the key-mapped data
+value.</p>
+<h4 id="struct-type-user-param-homogeneous">Struct Type User Param
+(Homogeneous)</h4>
+<p>A <a href="#struct-mapping">GlueGen generated <em>Struct
+type</em></a> is used for both, <code>SetCallbackFunctionName</code> and
+<code>CallbackFunctionType</code>.</p>
+<p>It's a homogeneous <code>UserParam</code> mapping, where the native
+side receives the actual native struct address.</p>
+<p>The static Java callback dispatcher dereferences the received native
+struct address (<em>long</em>), i.e. rebuilding the <em>struct
+Object</em> to be passed to the users'
+<code>CallbackFunction</code>.</p>
+<h4 id="struct-type-user-param-heterogeneous">Struct Type User Param
+(Heterogeneous)</h4>
+<p>An anonymous pointer (<em>long</em>) for
+<code>SetCallbackFunctionName</code> and a <a
+href="#struct-mapping">GlueGen generated <em>struct type</em></a> for
+<code>CallbackFunctionType</code> is being used.</p>
+<p>It's a heterogeneous <code>UserParam</code> mapping, where the
+toolkit is expected to place the given anonymous pointer inside the
+defined <em>struct type</em> passed to the
+<code>CallbackFunction</code>.</p>
+<p>The <code>SetCallback-UserParamIndex</code> for the different
+parameter-type is <a href="#javacallback-configuration">set in the
+configuration</a>.</p>
+<p>The static Java callback dispatcher dereferences the received native
+struct address (<em>long</em>), i.e. rebuilding the <em>struct
+Object</em> to be passed to the users'
+<code>CallbackFunction</code>.</p>
<h3 id="javacallback-configuration"><em>JavaCallback</em>
Configuration</h3>
<p>Configuration directives are as follows:</p>
-<pre><code>JavaCallbackDef &lt;SetCallbackFunctionName&gt; &lt;CallbackFunctionType&gt; &lt;CallbackFunction-UserParamIndex&gt; [&lt;SetCallback-KeyClassName&gt;]
+<pre><code>JavaCallbackDef &lt;SetCallbackFunctionName&gt; [&lt;SetCallback-UserParamIndex&gt;] &lt;CallbackFunctionType&gt; &lt;CallbackFunction-UserParamIndex&gt; [&lt;SetCallback-KeyClassName&gt;]
JavaCallbackKey &lt;SetCallbackFunctionName&gt; (SetCallback-ParamIdx)*</code></pre>
<p><code>JavaCallbackDef</code> and <code>JavaCallbackKey</code> use the
name of the <code>SetCallbackFunction</code> as its first attribute, as
@@ -1973,6 +2036,11 @@ it is core to the semantic mapping of all resources.</p>
<ul>
<li><code>SetCallbackFunction</code>: <code>SetCallbackFunction</code>
name of the native toolkit API responsible to set the callback</li>
+<li><code>SetCallback-UserParamIndex</code>: Optional
+<code>UserParam</code> parameter-index of the
+<code>SetCallbackFunction</code>, allowing to <a
+href="#struct-type-user-param-heterogeneous">indicate a heterogeneous
+<code>UserParam</code></a></li>
<li><code>CallbackFunctionType</code>: The native toolkit API
typedef-name of the function-pointer-type, aka the callback type
name</li>
@@ -2012,9 +2080,35 @@ callback scope, i.e. the callback and all resources will be mapped to
this key. The optional <code>SetCallback-KeyClass</code> may override
this semantic.</li>
</ul>
-<h4
+<h4 id="custom-setcallback-keyclass">Custom
+<code>SetCallback-KeyClass</code></h4>
+<p>The <code>SetCallback-KeyClass</code> is the optional user-written
+hash-map-key definition and shall handle all key parameter of the
+<code>SetCallbackFunction</code> as defined via
+<code>JavaCallbackKey</code>, see above.</p>
+<p><code>SetCallback-KeyClass</code> may be used to add external
+key-components, e.g. current-thread or a toolkit dependent context.</p>
+<p>The <code>SetCallback-KeyClass</code> shall implement the following
+hash-map-key standard methods</p>
+<ul>
+<li><code>boolean equals(Object)</code></li>
+<li><code>int hashCode()</code></li>
+<li><code>SetCallback-KeyClassName(...)</code> constructor receiving all
+key parameter of <code>SetCallbackFunction</code> as defined via
+<code>JavaCallbackKey</code>, see above.</li>
+</ul>
+<h4 id="required-libraryonload">Required <em>LibraryOnLoad</em></h4>
+<p>Note that <a
+href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad Bindingtest2</code></a>
+must be specified in exactly one native code-unit. It provides code to
+allow the generated native callback-function to attach the current
+thread to the <code>JavaVM*</code> generating a new
+<code>JNIEnv*</code>in daemon mode - or just to retrieve the thread's
+<code>JNIEnv*</code>, if already attached to the
+<code>JavaVM*</code>.</p>
+<h3
id="javacallback-generated-interfaces-classes-and-methods"><em>JavaCallback</em>
-Generated Interfaces, Classes and Methods</h4>
+Generated Interfaces, Classes and Methods</h3>
<p>The public <code>CallbackFunction</code> interface is generated.</p>
<p>The default public <code>SetCallback-KeyClass</code> is generated if
keys are used and no custom class is specified, see above.</p>
@@ -2068,23 +2162,6 @@ and <em>releaseAll<code>SetCallbackFunctionName</code>()</em> methods
are not the <em>proper toolkit API way</em> to remove the callback, try
to use original <code>SetCallbackFunctionName</code> API method instead
using a <code>null</code> <code>CallbackFunction</code> reference.</p>
-<h4 id="custom-setcallback-keyclass">Custom
-<code>SetCallback-KeyClass</code></h4>
-<p>The <code>SetCallback-KeyClass</code> is the optional user-written
-hash-map-key definition and shall handle all key parameter of the
-<code>SetCallbackFunction</code> as defined via
-<code>JavaCallbackKey</code>, see above.</p>
-<p><code>SetCallback-KeyClass</code> may be used to add external
-key-components, e.g. current-thread or a toolkit dependent context.</p>
-<p>The <code>SetCallback-KeyClass</code> shall implement the following
-hash-map-key standard methods</p>
-<ul>
-<li><code>boolean equals(Object)</code></li>
-<li><code>int hashCode()</code></li>
-<li><code>SetCallback-KeyClassName(...)</code> constructor receiving all
-key parameter of <code>SetCallbackFunction</code> as defined via
-<code>JavaCallbackKey</code>, see above.</li>
-</ul>
<h3 id="javacallback-notes"><em>JavaCallback</em> Notes</h3>
<p>Please consider the following <em>currently enabled</em> constraints
using JavaCallback</p>
@@ -2118,7 +2195,11 @@ and the native callback dispatcher <strong>are thread-safe</strong></li>
<li>...</li>
</ul>
<h3 id="javacallback-example-1">JavaCallback Example 1</h3>
-<p>This is a generic example.</p>
+<p>This examples demonstrates a <a
+href="#pure-java-object-user-type-default">homogeneous <em>Java
+Object</em> <code>UserParam</code> mapping</a> with a <a
+href="#javacallback-key-definition">globally scoped</a>
+<code>CallbackFunction</code> and <code>UserParam</code>.</p>
<p>The callback <code>T2_CallbackFunc01</code> has global scope, i.e. is
not mapped to any key and can be only set globally.</p>
<p>C-API header snippet:</p>
@@ -2187,6 +2268,11 @@ thread to the <code>JavaVM*</code> generating a new
public void InjectMessageCallback01(long id, String msg);</code></pre>
<h3 id="javacallback-example-2a-default-keyclass">JavaCallback Example
2a (Default <em>KeyClass</em>)</h3>
+<p>This examples demonstrates a <a
+href="#pure-java-object-user-type-default">homogeneous <em>Java
+Object</em> <code>UserParam</code> mapping</a> with a <a
+href="#javacallback-key-definition">key-mapped</a>
+<code>CallbackFunction</code> and <code>UserParam</code>.</p>
<p>This examples is derived from OpenAL's
<code>AL_SOFT_callback_buffer</code> extension.</p>
<p>The callback <code>ALBUFFERCALLBACKTYPESOFT</code> is mapped to
@@ -2278,6 +2364,11 @@ buffer.</p>
public void alEventCallbackInject(int eventType, int object, int param, String msg); </code></pre>
<h3 id="javacallback-example-2b-custom-keyclass">JavaCallback Example 2b
(Custom <em>KeyClass</em>)</h3>
+<p>This examples demonstrates a <a
+href="#pure-java-object-user-type-default">homogeneous <em>Java
+Object</em> <code>UserParam</code> mapping</a> with a <a
+href="#custom-setcallback-keyclass">custom <em>KeyClass</em></a> to map
+<code>CallbackFunction</code> and <code>UserParam</code>.</p>
<p>Same as example 2a, but implementing a custom
<code>SetCallback-KeyClass</code>.</p>
<p>Instead of <code>Callback0</code>, the unit <code>test2.*</code> uses
@@ -2316,6 +2407,133 @@ which uses one key, i.e. <code>buffer</code>.</p>
return &quot;CustomALKey[this &quot;+toHexString(System.identityHashCode(this))+&quot;, buffer &quot;+buffer+&quot;]&quot;;
}
}</code></pre>
+<h3 id="javacallback-example-11a-homogeneous-struct-type">JavaCallback
+Example 11a (<em>Homogeneous Struct Type</em>)</h3>
+<p>This examples demonstrates a <a
+href="#struct-type-user-param-homogeneous">homogeneous <em>Struct</em>
+<code>UserParam</code> mapping</a> with a <a
+href="#javacallback-key-definition">key-mapped</a>
+<code>CallbackFunction</code> and <code>UserParam</code>.</p>
+<p>The callback <code>T2_CallbackFunc11</code> is passed by the toolkit
+to the <code>CallbackFunction</code> and by the user to the registration
+method <code>MessageCallback11b(..)</code>.</p>
+<p>C-API Header snipped</p>
+<pre><code> typedef struct {
+ int32_t ApiVersion;
+ void* Data;
+ long i;
+ long r;
+ size_t id;
+ } T2_Callback11UserType;
+
+ typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam, long val);
+
+ void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam);
+ void MessageCallback11aInject(size_t id, long val); </code></pre>
+<p>and the following GlueGen configuration</p>
+<pre><code> JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1
+ JavaCallbackKey MessageCallback11a 0</code></pre>
+<p>leading to the following interface</p>
+<pre><code> /** JavaCallback interface: T2_CallbackFunc11 -&gt; void (*T2_CallbackFunc11)(size_t id, const T2_Callback11UserType * usrParam, long val) */
+ public static interface T2_CallbackFunc11 {
+ /** Interface to C language function: &lt;br&gt; &lt;code&gt;void callback(size_t id, const T2_Callback11UserType * usrParam, long val)&lt;/code&gt;&lt;br&gt;Alias for: &lt;code&gt;T2_CallbackFunc11&lt;/code&gt; */
+ public void callback(long id, T2_Callback11UserType usrParam, long val);
+ }
+
+ ...
+
+ public static class MessageCallback11aKey { ... }
+
+ ...
+
+ /** Returns set of Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public Set&lt;MessageCallback11aKey&gt; getMessageCallback11aKeys();
+
+ /** Returns whether callback Key { long id } is mapped for &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public boolean isMessageCallback11aMapped(MessageCallback11aKey key);
+
+ /** Returns T2_CallbackFunc11 callback mapped to Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public T2_CallbackFunc11 getMessageCallback11a(MessageCallback11aKey key);
+
+ /** Returns user-param mapped to Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public Object getMessageCallback11aUserParam(MessageCallback11aKey key);
+
+ /** Releases all callback data mapped via Key { long id } skipping toolkit API. Favor passing `null` callback ref to &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public int releaseAllMessageCallback11a();
+
+ /** Releases callback data mapped to Key { long id } skipping toolkit API. Favor passing `null` callback ref to &lt;br&gt; &lt;code&gt; void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)&lt;/code&gt; */
+ public void releaseMessageCallback11a(MessageCallback11aKey key);
+
+ /** Entry point (through function pointer) to C language function: &lt;br&gt; &lt;code&gt;void MessageCallback11a(size_t id, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType * usrParam)&lt;/code&gt;&lt;br&gt; */
+ public void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam);
+
+ /** Entry point (through function pointer) to C language function: &lt;br&gt; &lt;code&gt;void MessageCallback11aInject(size_t id, long val)&lt;/code&gt;&lt;br&gt; */
+ public void MessageCallback11aInject(long id, long val); </code></pre>
+<h3
+id="javacallback-example-11b-heterogeneous-pointerstruct-type">JavaCallback
+Example 11b (<em>Heterogeneous Pointer/Struct Type</em>)</h3>
+<p>This examples demonstrates a <a
+href="#struct-type-user-param-heterogeneous">heterogeneous
+<em>Struct</em> <code>UserParam</code> mapping</a> with a <a
+href="#javacallback-key-definition">key-mapped</a>
+<code>CallbackFunction</code> and <code>UserParam</code>.</p>
+<p>The callback <code>T2_CallbackFunc11</code> is managed by the toolkit
+and passed to the callback function, while user passes a <em>void</em>
+with the registration method <code>MessageCallback11b(..)</code>. The
+toolkit associates the users' <code>void*</code> pointer with the
+<code>T2_CallbackFunc11</code>.</p>
+<p>C-API Header snipped</p>
+<pre><code> typedef struct {
+ int32_t ApiVersion;
+ void* Data;
+ long i;
+ long r;
+ size_t id;
+ } T2_Callback11UserType;
+
+ typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam, long val);
+
+ void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data);
+ void MessageCallback11bInject(size_t id, long val);</code></pre>
+<p>and the following GlueGen configuration</p>
+<pre><code> JavaCallbackDef MessageCallback11b 2 T2_CallbackFunc11 1
+ JavaCallbackKey MessageCallback11b 0</code></pre>
+<p>leading to the following interface</p>
+<pre><code> /** JavaCallback interface: T2_CallbackFunc11 -&gt; void (*T2_CallbackFunc11)(size_t id, const T2_Callback11UserType * usrParam, long val) */
+ public static interface T2_CallbackFunc11 {
+ /** Interface to C language function: &lt;br&gt; &lt;code&gt;void callback(size_t id, const T2_Callback11UserType * usrParam, long val)&lt;/code&gt;&lt;br&gt;Alias for: &lt;code&gt;T2_CallbackFunc11&lt;/code&gt; */
+ public void callback(long id, T2_Callback11UserType usrParam, long val);
+ }
+
+ ...
+
+ public static class MessageCallback11bKey { ... }
+
+ ...
+
+ /** Returns set of Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public Set&lt;MessageCallback11bKey&gt; getMessageCallback11bKeys();
+
+ /** Returns whether callback Key { long id } is mapped for &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public boolean isMessageCallback11bMapped(MessageCallback11bKey key);
+
+ /** Returns T2_CallbackFunc11 callback mapped to Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public T2_CallbackFunc11 getMessageCallback11b(MessageCallback11bKey key);
+
+ /** Returns user-param mapped to Key { long id } for &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public Object getMessageCallback11bUserParam(MessageCallback11bKey key);
+
+ /** Releases all callback data mapped via Key { long id } skipping toolkit API. Favor passing `null` callback ref to &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public int releaseAllMessageCallback11b();
+
+ /** Releases callback data mapped to Key { long id } skipping toolkit API. Favor passing `null` callback ref to &lt;br&gt; &lt;code&gt; void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)&lt;/code&gt; */
+ public void releaseMessageCallback11b(MessageCallback11bKey key);
+
+ /** Entry point (through function pointer) to C language function: &lt;br&gt; &lt;code&gt;void MessageCallback11b(size_t id, T2_CallbackFunc11 cbFunc, void * Data)&lt;/code&gt;&lt;br&gt; */
+ public void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data);
+
+ /** Entry point (through function pointer) to C language function: &lt;br&gt; &lt;code&gt;void MessageCallback11bInject(size_t id, long val)&lt;/code&gt;&lt;br&gt; */
+ public void MessageCallback11bInject(long id, long val); </code></pre>
<p><em>TODO: Enhance documentation</em></p>
<h2 id="misc-configurations">Misc Configurations</h2>
<h3
diff --git a/doc/GlueGen_Mapping.md b/doc/GlueGen_Mapping.md
index 1ab79b2..6453e64 100644
--- a/doc/GlueGen_Mapping.md
+++ b/doc/GlueGen_Mapping.md
@@ -40,7 +40,7 @@ GlueGen can produce native foreign function bindings to Java™ as well as
[map native data structures](#struct-mapping) to be fully accessible from Java™ including
potential calls to [embedded function pointer](#struct-function-pointer-support).
-GlueGen supports [registering Java™ callback methods](#java-callback-from-native-c-api-support)
+GlueGen supports [registering Java™ callback methods](#java-callback)
to receive asynchronous and off-thread native toolkit events,
where a generated native callback function dispatches the events to Java™.
@@ -299,7 +299,7 @@ A *Struct* may utilize the following data types for its fields
* See [*String Mapping*](#string-mapping) above.
* *Struct*, i.e. an aggregated or referenced compound variable
* *Function Pointer*, a *typedef*'ed and set callable function pointer, see [Struct Function-Pointer Support](#struct-function-pointer-support) below.
-* *Java Callback from Native Code*, see [section below](#java-callback-from-native-c-api-support)
+* *Java Callback from Native Code*, see [section below](#java-callback)
A field may be a direct aggregation, i.e. instance, within the struct including an array
or a reference to a single element or array via a pointer.
@@ -765,21 +765,58 @@ and similar to `T2_CustomFuncB customFuncB1`
public final int CustomFuncB1(T2_UserData pUserData) { .. }
```
-## Java Callback from Native C-API Support
+## Java Callback
GlueGen supports registering Java callback methods
to receive asynchronous and off-thread native toolkit events,
where a generated native callback function dispatches the events to Java.
-### Required *LibraryOnLoad*
-Note that [`LibraryOnLoad Bindingtest2`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit.
-It provides code to allow the generated native callback-function to attach the current thread to the `JavaVM*` generating a new `JNIEnv*`in daemon mode -
-or just to retrieve the thread's `JNIEnv*`, if already attached to the `JavaVM*`.
+### Implementation Details
+Implementation 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.
+
+### *JavaCallback* *UserParam* Mapping
+Usually the same `UserParam` type is used in both items (or hooks), `SetCallbackFunctionName` and `CallbackFunctionType`,
+which we call a homogeneous `UserParam` mapping.
+
+Even in a homogeneous `UserParam` mapping, handling of the `UserParam` value might differ in the native binding code.
+
+To specify a non homogeneous `UserParam` mapping, i.e. heterogeneous `UserParam` mapping,
+the `UserParam` index of the `SetCallbackFunction` must be [set in the configuration](#javacallback-configuration).
+
+The following mappings are supported.
+
+#### Pure Java Object User Type (default)
+A pure Java *Object type* is used for both, `SetCallbackFunctionName` and `CallbackFunctionType`.
+
+It's a homogeneous `UserParam` mapping, where the native side receives a simple unique ID and shall not dereference the *pointer*.
+
+The static Java callback dispatcher fetches the Java `UserParam` *Object* from the key-mapped data value.
+
+#### Struct Type User Param (Homogeneous)
+A [GlueGen generated *Struct type*](#struct-mapping) is used for both, `SetCallbackFunctionName` and `CallbackFunctionType`.
+
+It's a homogeneous `UserParam` mapping, where the native side receives the actual native struct address.
+
+The static Java callback dispatcher dereferences the received native struct address (*long*), i.e. rebuilding the *struct Object* to be passed to the users' `CallbackFunction`.
+
+#### Struct Type User Param (Heterogeneous)
+An anonymous pointer (*long*) for `SetCallbackFunctionName` and a [GlueGen generated *struct type*](#struct-mapping) for `CallbackFunctionType` is being used.
+
+It's a heterogeneous `UserParam` mapping, where the toolkit is expected to place the given anonymous pointer inside the defined *struct type* passed to the `CallbackFunction`.
+
+The `SetCallback-UserParamIndex` for the different parameter-type is [set in the configuration](#javacallback-configuration).
+
+The static Java callback dispatcher dereferences the received native struct address (*long*), i.e. rebuilding the *struct Object* to be passed to the users' `CallbackFunction`.
### *JavaCallback* Configuration
Configuration directives are as follows:
- JavaCallbackDef <SetCallbackFunctionName> <CallbackFunctionType> <CallbackFunction-UserParamIndex> [<SetCallback-KeyClassName>]
+ JavaCallbackDef <SetCallbackFunctionName> [<SetCallback-UserParamIndex>] <CallbackFunctionType> <CallbackFunction-UserParamIndex> [<SetCallback-KeyClassName>]
JavaCallbackKey <SetCallbackFunctionName> (SetCallback-ParamIdx)*
`JavaCallbackDef` and `JavaCallbackKey` use the name of the `SetCallbackFunction` as its first attribute,
@@ -787,6 +824,7 @@ as it is core to the semantic mapping of all resources.
`JavaCallbackDef` attributes:
- `SetCallbackFunction`: `SetCallbackFunction` name of the native toolkit API responsible to set the callback
+- `SetCallback-UserParamIndex`: Optional `UserParam` parameter-index of the `SetCallbackFunction`, allowing to [indicate a heterogeneous `UserParam`](#struct-type-user-param-heterogeneous)
- `CallbackFunctionType`: The native toolkit API typedef-name of the function-pointer-type, aka the callback type name
- `CallbackFunction-UserParamIndex`: The `userParam` parameter-index of the `CallbackFunctionType`
- `SetCallback-KeyClassName`: Name of an optional user-implemented `SetCallback-KeyClass`, providing the hash-map-key - see below
@@ -811,7 +849,24 @@ Key arguments must match in `SetCallbackFunction` to remove a previously set `Ca
- `SetCallbackFunction`: `SetCallbackFunction` name of the native toolkit API responsible to set the callback
- `SetCallback-ParamIdx`: List of parameter indices of the `SetCallbackFunction`, denoting the key(s) limiting the callback scope, i.e. the callback and all resources will be mapped to this key. The optional `SetCallback-KeyClass` may override this semantic.
-#### *JavaCallback* Generated Interfaces, Classes and Methods
+#### Custom `SetCallback-KeyClass`
+
+The `SetCallback-KeyClass` is the optional user-written hash-map-key definition
+and shall handle all key parameter of the `SetCallbackFunction` as defined via `JavaCallbackKey`, see above.
+
+`SetCallback-KeyClass` may be used to add external key-components, e.g. current-thread or a toolkit dependent context.
+
+The `SetCallback-KeyClass` shall implement the following hash-map-key standard methods
+- `boolean equals(Object)`
+- `int hashCode()`
+- `SetCallback-KeyClassName(...)` constructor receiving all key parameter of `SetCallbackFunction` as defined via `JavaCallbackKey`, see above.
+
+#### Required *LibraryOnLoad*
+Note that [`LibraryOnLoad Bindingtest2`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit.
+It provides code to allow the generated native callback-function to attach the current thread to the `JavaVM*` generating a new `JNIEnv*`in daemon mode -
+or just to retrieve the thread's `JNIEnv*`, if already attached to the `JavaVM*`.
+
+### *JavaCallback* Generated Interfaces, Classes and Methods
The public `CallbackFunction` interface is generated.
@@ -838,19 +893,6 @@ If no `SetCallback-KeyClass` is used, the additional *maintenance* methods are:
Note that the *release`SetCallbackFunctionName`(\*)* and *releaseAll`SetCallbackFunctionName`()* methods are not the *proper toolkit API way* to remove the callback,
try to use original `SetCallbackFunctionName` API method instead using a `null` `CallbackFunction` reference.
-#### Custom `SetCallback-KeyClass`
-
-The `SetCallback-KeyClass` is the optional user-written hash-map-key definition
-and shall handle all key parameter of the `SetCallbackFunction` as defined via `JavaCallbackKey`, see above.
-
-`SetCallback-KeyClass` may be used to add external key-components, e.g. current-thread or a toolkit dependent context.
-
-The `SetCallback-KeyClass` shall implement the following hash-map-key standard methods
-- `boolean equals(Object)`
-- `int hashCode()`
-- `SetCallback-KeyClassName(...)` constructor receiving all key parameter of `SetCallbackFunction` as defined via `JavaCallbackKey`, see above.
-
-
### *JavaCallback* Notes
Please consider the following *currently enabled* constraints using JavaCallback
- Only one interface callback-method binding is allowed for a native callback function, e.g. `T2_CallbackFunc01` (see above)
@@ -866,7 +908,7 @@ Please consider the following *currently enabled* constraints using JavaCallback
- ...
### JavaCallback Example 1
-This is a generic example.
+This examples demonstrates a [homogeneous *Java Object* `UserParam` mapping](#pure-java-object-user-type-default) with a [globally scoped](#javacallback-key-definition) `CallbackFunction` and `UserParam`.
The callback `T2_CallbackFunc01` has global scope, i.e. is not mapped to any key and can be only set globally.
@@ -941,6 +983,8 @@ public interface Bindingtest2 {
### JavaCallback Example 2a (Default *KeyClass*)
+This examples demonstrates a [homogeneous *Java Object* `UserParam` mapping](#pure-java-object-user-type-default) with a [key-mapped](#javacallback-key-definition) `CallbackFunction` and `UserParam`.
+
This examples is derived from OpenAL's `AL_SOFT_callback_buffer` extension.
The callback `ALBUFFERCALLBACKTYPESOFT` is mapped to `buffer` name, i.e. one callback can be set for each buffer.
@@ -1040,6 +1084,8 @@ leading to the following interface
### JavaCallback Example 2b (Custom *KeyClass*)
+This examples demonstrates a [homogeneous *Java Object* `UserParam` mapping](#pure-java-object-user-type-default) with a [custom *KeyClass*](#custom-setcallback-keyclass) to map `CallbackFunction` and `UserParam`.
+
Same as example 2a, but implementing a custom `SetCallback-KeyClass`.
Instead of `Callback0`, the unit `test2.*` uses `Callback1` to differentiate this case.
@@ -1080,6 +1126,141 @@ which uses one key, i.e. `buffer`.
}
```
+### JavaCallback Example 11a (*Homogeneous Struct Type*)
+
+This examples demonstrates a [homogeneous *Struct* `UserParam` mapping](#struct-type-user-param-homogeneous) with a [key-mapped](#javacallback-key-definition) `CallbackFunction` and `UserParam`.
+
+The callback `T2_CallbackFunc11` is passed by the toolkit to the `CallbackFunction` and by the user to the registration method `MessageCallback11b(..)`.
+
+C-API Header snipped
+```
+ typedef struct {
+ int32_t ApiVersion;
+ void* Data;
+ long i;
+ long r;
+ size_t id;
+ } T2_Callback11UserType;
+
+ typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam, long val);
+
+ void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam);
+ void MessageCallback11aInject(size_t id, long val);
+```
+
+and the following GlueGen configuration
+```
+ JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1
+ JavaCallbackKey MessageCallback11a 0
+```
+
+leading to the following interface
+```
+ /** JavaCallback interface: T2_CallbackFunc11 -> void (*T2_CallbackFunc11)(size_t id, const T2_Callback11UserType * usrParam, long val) */
+ public static interface T2_CallbackFunc11 {
+ /** Interface to C language function: <br> <code>void callback(size_t id, const T2_Callback11UserType * usrParam, long val)</code><br>Alias for: <code>T2_CallbackFunc11</code> */
+ public void callback(long id, T2_Callback11UserType usrParam, long val);
+ }
+
+ ...
+
+ public static class MessageCallback11aKey { ... }
+
+ ...
+
+ /** Returns set of Key { long id } for <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public Set<MessageCallback11aKey> getMessageCallback11aKeys();
+
+ /** Returns whether callback Key { long id } is mapped for <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public boolean isMessageCallback11aMapped(MessageCallback11aKey key);
+
+ /** Returns T2_CallbackFunc11 callback mapped to Key { long id } for <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public T2_CallbackFunc11 getMessageCallback11a(MessageCallback11aKey key);
+
+ /** Returns user-param mapped to Key { long id } for <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public Object getMessageCallback11aUserParam(MessageCallback11aKey key);
+
+ /** Releases all callback data mapped via Key { long id } skipping toolkit API. Favor passing `null` callback ref to <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public int releaseAllMessageCallback11a();
+
+ /** Releases callback data mapped to Key { long id } skipping toolkit API. Favor passing `null` callback ref to <br> <code> void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam)</code> */
+ public void releaseMessageCallback11a(MessageCallback11aKey key);
+
+ /** Entry point (through function pointer) to C language function: <br> <code>void MessageCallback11a(size_t id, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType * usrParam)</code><br> */
+ public void MessageCallback11a(long id, T2_CallbackFunc11 cbFunc, T2_Callback11UserType usrParam);
+
+ /** Entry point (through function pointer) to C language function: <br> <code>void MessageCallback11aInject(size_t id, long val)</code><br> */
+ public void MessageCallback11aInject(long id, long val);
+```
+
+### JavaCallback Example 11b (*Heterogeneous Pointer/Struct Type*)
+
+This examples demonstrates a [heterogeneous *Struct* `UserParam` mapping](#struct-type-user-param-heterogeneous) with a [key-mapped](#javacallback-key-definition) `CallbackFunction` and `UserParam`.
+
+The callback `T2_CallbackFunc11` is managed by the toolkit and passed to the callback function, while user passes a *void* with the registration method `MessageCallback11b(..)`. The toolkit associates the users' `void*` pointer with the `T2_CallbackFunc11`.
+
+
+C-API Header snipped
+```
+ typedef struct {
+ int32_t ApiVersion;
+ void* Data;
+ long i;
+ long r;
+ size_t id;
+ } T2_Callback11UserType;
+
+ typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam, long val);
+
+ void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data);
+ void MessageCallback11bInject(size_t id, long val);
+```
+
+and the following GlueGen configuration
+```
+ JavaCallbackDef MessageCallback11b 2 T2_CallbackFunc11 1
+ JavaCallbackKey MessageCallback11b 0
+```
+
+leading to the following interface
+```
+ /** JavaCallback interface: T2_CallbackFunc11 -> void (*T2_CallbackFunc11)(size_t id, const T2_Callback11UserType * usrParam, long val) */
+ public static interface T2_CallbackFunc11 {
+ /** Interface to C language function: <br> <code>void callback(size_t id, const T2_Callback11UserType * usrParam, long val)</code><br>Alias for: <code>T2_CallbackFunc11</code> */
+ public void callback(long id, T2_Callback11UserType usrParam, long val);
+ }
+
+ ...
+
+ public static class MessageCallback11bKey { ... }
+
+ ...
+
+ /** Returns set of Key { long id } for <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public Set<MessageCallback11bKey> getMessageCallback11bKeys();
+
+ /** Returns whether callback Key { long id } is mapped for <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public boolean isMessageCallback11bMapped(MessageCallback11bKey key);
+
+ /** Returns T2_CallbackFunc11 callback mapped to Key { long id } for <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public T2_CallbackFunc11 getMessageCallback11b(MessageCallback11bKey key);
+
+ /** Returns user-param mapped to Key { long id } for <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public Object getMessageCallback11bUserParam(MessageCallback11bKey key);
+
+ /** Releases all callback data mapped via Key { long id } skipping toolkit API. Favor passing `null` callback ref to <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public int releaseAllMessageCallback11b();
+
+ /** Releases callback data mapped to Key { long id } skipping toolkit API. Favor passing `null` callback ref to <br> <code> void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data)</code> */
+ public void releaseMessageCallback11b(MessageCallback11bKey key);
+
+ /** Entry point (through function pointer) to C language function: <br> <code>void MessageCallback11b(size_t id, T2_CallbackFunc11 cbFunc, void * Data)</code><br> */
+ public void MessageCallback11b(long id, T2_CallbackFunc11 cbFunc, long Data);
+
+ /** Entry point (through function pointer) to C language function: <br> <code>void MessageCallback11bInject(size_t id, long val)</code><br> */
+ public void MessageCallback11bInject(long id, long val);
+```
+
*TODO: Enhance documentation*
## Misc Configurations