Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:07:35+00:00 2026-06-08T10:07:35+00:00

I have several very similar functions: v8::Handle<v8::Value> jsAudioPlay(const v8::Arguments &args) { Audio *audio =

  • 0

I have several very similar functions:

v8::Handle<v8::Value> jsAudioPlay(const v8::Arguments &args) {
    Audio *audio = static_cast<Audio*>(args.This()->GetPointerFromInternalField(0));
    if (audio != NULL) audio->play(get(args[0], 0));
    return args.This();
}

v8::Handle<v8::Value> jsAudioPause(const v8::Arguments &args) {
    Audio *audio = static_cast<Audio*>(args.This()->GetPointerFromInternalField(0));
    if (audio != NULL) audio->pause();
    return args.This();
}

v8::Handle<v8::Value> jsAudioLoop(const v8::Arguments &args) {
    Audio *audio = static_cast<Audio*>(args.This()->GetPointerFromInternalField(0));
    if (audio != NULL) audio->loop(get(args[0], -1));
    return args.This();
}

v8::Handle<v8::Value> jsAudioVolume(const v8::Arguments &args) {
    Audio *audio = static_cast<Audio*>(args.This()->GetPointerFromInternalField(0));
    if (audio != NULL) audio->volume(get(args[0], 1.0f));
    return args.This();
}

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:

typedef Handle<Value> (*InvocationCallback)(const Arguments& args);

template <class T> InvocationCallback FunctionWrapper ...;
template <class T> FunctionWrapper FunctionReal ...;
template <class T, class arg1> FunctionWrapper FunctionReal ...;
template <class T, class arg1, class arg2> FunctionWrapper FunctionReal ...;

I realize similar questions have been asked, but I can’t find an example of a template within a template like the above.


Update on 7/21/2012

Template:

template <class T> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) {
    T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
    if (t != NULL) t->volume(args[0]->NumberValue());
    return args.This();
}

Usage:

audio->PrototypeTemplate()->Set("Volume", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio>));

Now if I could only figure out how to pass &Audio::volume to the template, I’ll be in business.


Update on 7/24/2012

Refer to my answer for how I solved this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:07:36+00:00Added an answer on June 8, 2026 at 10:07 am

    My Templates:

    template <class T, class RT, RT(T::*f)()> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) {
        T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
        if (t != NULL) (t->*f)();
        return args.This();
    }
    template <class T, class RT, class A0, RT(T::*f)(A0)> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) {
        T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
        if (t != NULL) (t->*f)(jsConvert<A0>(args[0]));
        return args.This();
    }
    template <class T, class RT, class A0, class A1, RT(T::*f)(A0, A1)> v8::Handle<v8::Value> jsFunctionTemplate(const v8::Arguments &args) {
        T *t = static_cast<T*>(args.This()->GetPointerFromInternalField(0));
        if (t != NULL) (t->*f)(jsConvert<A0>(args[0]), jsConvert<A1>(args[1]));
        return args.This();
    }
    

    Usage:

    audio->PrototypeTemplate()->Set("Play", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, Audio*, int, &Audio::play>));
    audio->PrototypeTemplate()->Set("Pause", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, Audio*, &Audio::pause>));
    audio->PrototypeTemplate()->Set("Loop", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, Audio*, int, &Audio::loop>));
    audio->PrototypeTemplate()->Set("Volume", v8::FunctionTemplate::New(&jsFunctionTemplate<Audio, Audio*, float, &Audio::volume>));
    

    Now I need to figure out the two final steps: variable number of arguments, and properly templating jsConvert to return the desired type <A0>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SOLR index over several million full text documents. This works very
I have a problem very similar to this one , but slightly more involved.
I have a MyFunction that may take several datatypes, which are all very similar.
In my program I have several very similar drop-down menus all with the same
I have seen several very similar questions on stackoverflow, but haven't come across anything
I have several databases named very similar (my-db-1, my-db-2, my-db-3, my-db-4). I want to
I have several pages that are all very similar. They have some javascript rollover
I have several database tables that just contain a single column and very few
I have a table with several employees. They have the following columns empid,datecolumn1,is_valid. Very
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.