Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert a function wrapper in to templates?
    text
    copied!<p>I have several very similar functions:</p> <pre><code>v8::Handle&lt;v8::Value&gt; jsAudioPlay(const v8::Arguments &amp;args) { Audio *audio = static_cast&lt;Audio*&gt;(args.This()-&gt;GetPointerFromInternalField(0)); if (audio != NULL) audio-&gt;play(get(args[0], 0)); return args.This(); } v8::Handle&lt;v8::Value&gt; jsAudioPause(const v8::Arguments &amp;args) { Audio *audio = static_cast&lt;Audio*&gt;(args.This()-&gt;GetPointerFromInternalField(0)); if (audio != NULL) audio-&gt;pause(); return args.This(); } v8::Handle&lt;v8::Value&gt; jsAudioLoop(const v8::Arguments &amp;args) { Audio *audio = static_cast&lt;Audio*&gt;(args.This()-&gt;GetPointerFromInternalField(0)); if (audio != NULL) audio-&gt;loop(get(args[0], -1)); return args.This(); } v8::Handle&lt;v8::Value&gt; jsAudioVolume(const v8::Arguments &amp;args) { Audio *audio = static_cast&lt;Audio*&gt;(args.This()-&gt;GetPointerFromInternalField(0)); if (audio != NULL) audio-&gt;volume(get(args[0], 1.0f)); return args.This(); } </code></pre> <p>And I've been reading about C++ templates for hours and I'm convinced that it's possible to get rid of these functions and replace them with templates. I envision the end result will be something like:</p> <pre><code>typedef Handle&lt;Value&gt; (*InvocationCallback)(const Arguments&amp; args); template &lt;class T&gt; InvocationCallback FunctionWrapper ...; template &lt;class T&gt; FunctionWrapper FunctionReal ...; template &lt;class T, class arg1&gt; FunctionWrapper FunctionReal ...; template &lt;class T, class arg1, class arg2&gt; FunctionWrapper FunctionReal ...; </code></pre> <p>I realize similar questions have been asked, but I can't find an example of a template within a template like the above.</p> <hr> <p><strong>Update on 7/21/2012</strong></p> <p>Template:</p> <pre><code>template &lt;class T&gt; v8::Handle&lt;v8::Value&gt; jsFunctionTemplate(const v8::Arguments &amp;args) { T *t = static_cast&lt;T*&gt;(args.This()-&gt;GetPointerFromInternalField(0)); if (t != NULL) t-&gt;volume(args[0]-&gt;NumberValue()); return args.This(); } </code></pre> <p>Usage:</p> <pre><code>audio-&gt;PrototypeTemplate()-&gt;Set("Volume", v8::FunctionTemplate::New(&amp;jsFunctionTemplate&lt;Audio&gt;)); </code></pre> <p>Now if I could only figure out how to pass <code>&amp;Audio::volume</code> to the template, I'll be in business.</p> <hr> <p><strong>Update on 7/24/2012</strong></p> <p>Refer to my answer for how I solved this.</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