Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Created December 31, 2015 05:26
Show Gist options
  • Save xaxxon/ba231db4647822332e4b to your computer and use it in GitHub Desktop.
Save xaxxon/ba231db4647822332e4b to your computer and use it in GitHub Desktop.
template<typename T>
struct CastToNative {};
template<>
struct CastToNative<int> {
int operator()(Local<Value> value){return value->ToInteger()->Value();}
};
template<>
struct CastToNative<char *> {
char * operator()(Local<Value> value){return *v8::String::Utf8Value(value);}
};
template<>
struct CastToNative<const char *> {
const char * operator()(Local<Value> value){return CastToNative<char *>()(value);}
};
template<>
struct CastToNative<double> {
double operator()(Local<Value> value){return value->ToNumber()->Value();}
};
template<>
struct CastToNative<std::string> {
std::string operator()(Local<Value> value){return std::string(CastToNative<char *>()(value));}
};
@xaxxon
Copy link
Author

xaxxon commented Dec 31, 2015

Is this the right/best way to unbox a type? other than maybe using a macro for DRY?

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