Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Created December 30, 2015 03:37
Show Gist options
  • Save xaxxon/4371d36837ce0c0a3c91 to your computer and use it in GitHub Desktop.
Save xaxxon/4371d36837ce0c0a3c91 to your computer and use it in GitHub Desktop.
template<typename METHOD_TYPE>
struct RunMethod {};
template<typename RETURN_TYPE, typename CLASS_TYPE, typename ... PARAMETERS>
struct RunMethod<RETURN_TYPE(CLASS_TYPE::*)(PARAMETERS...)>{
typedef RETURN_TYPE(CLASS_TYPE::*METHOD_TYPE)(PARAMETERS...);
CLASS_TYPE object;
METHOD_TYPE method;
RunMethod(CLASS_TYPE object, METHOD_TYPE method) : object(object), method(method) {}
void operator()(const v8::FunctionCallbackInfo<v8::Value> & info, PARAMETERS... parameters) {
RETURN_TYPE return_value = (object.*method)(parameters...);
CastToJS<RETURN_TYPE> cast;
info.GetReturnValue().Set(cast(info.GetIsolate(), return_value));
}
};
template<typename CLASS_TYPE, typename ... PARAMETERS>
struct RunMethod<void(CLASS_TYPE::*)(PARAMETERS...)> {
typedef void(CLASS_TYPE::*METHOD_TYPE)(PARAMETERS...);
CLASS_TYPE object;
METHOD_TYPE method;
RunMethod(CLASS_TYPE object, METHOD_TYPE method) : object(object), method(method) {}
void operator()(const v8::FunctionCallbackInfo<v8::Value> &, PARAMETERS... parameters) {
(object.*method)(parameters...);
}
};
@xaxxon
Copy link
Author

xaxxon commented Dec 30, 2015

Should I create a separate templatized base class for both of these to inherit from that has "object" and "method" as well as a pure virtual operator() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment