Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ object function to function pointer
    text
    copied!<p>I'm using a C library inside my C++ app. The library has a function with the following signature:</p> <pre><code>void awe_webview_set_callback_js_callback(awe_webview* webview, void (*callback)(awe_webview* caller, const awe_string* object_name, const awe_string* callback_name, const awe_jsarray* arguments)); </code></pre> <p>I'm trying to set a function as a call back and I'd like to be able to use the following class member function</p> <pre><code>void BattleScreen::HandleWebViewCallbacks(awe_webview* WebView, const awe_string* object, const awe_string* callback, const awe_jsarray* arguments) { //handling code } </code></pre> <p>I can't bind it directly and based on here <a href="http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2" rel="nofollow">http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2</a> I have a possible solution where I'd create a static member to handle the callback (since based on that site, it should be fine) and add a static instance of this class for the static member to call on.</p> <p>i.e. add the following to BattleScreen:</p> <pre><code>static BattleScreen* callbacktarget; static BattleScreen::TopLevelHandleWebViewCallbacks(awe_webview* WebView, const awe_string* object, const awe_string* callback, const awe_jsarray* arguments) { callbacktarget-&gt;HandleWebviewCallbacks(WebView, object, callback, arguments); } </code></pre> <p>bind it in the constructor like so:</p> <pre><code>awe_webview_set_callback_js_callback(this-&gt;GetWebView(), static_cast&lt;void (*)(awe_webview*, const awe_string*, const awe_string*, const awe_jsarray*)&gt;(&amp;BattleScreen::TopLevelHandleWebViewCallbacks)); </code></pre> <p>and assign the object to callbacktarget in the constructor.</p> <pre><code>BattleScreen::callbacktarget = this; </code></pre> <p>The problem is I have no way of knowing how many of these classes I will have at any one time (It'll be minimal but possibly greater then 1). I considered making the callbacktarget a vector of BattleScreen* that i can iterate through inside TopLevelHandleWebViewCallbacks and compare like so:</p> <pre><code>if (callbacktargets[index]-&gt;GetWebview() == WebView) { callbacktargets[index]-&gt;HandleWebviewCallbacks(WebView, object, callback, arguments); } </code></pre> <p>but the problem here is that I'm only comparing the awe_webview pointers which seems like a really bad idea. The library is closed source and the awe_webview's are C constructs so I can't see what makes them up and if there are any properties that would make a more suitable comparison. Is there a good solution to this? </p> <p>If I'm being unclear or you need additional information let me know.</p> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload