Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Created March 15, 2012 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piscisaureus/2045459 to your computer and use it in GitHub Desktop.
Save piscisaureus/2045459 to your computer and use it in GitHub Desktop.
template <typename T>
class UvHandle {
T handle_;
public:
T* handle() { return &handle_; }
protected:
static void OnClose();
static Value* Close(uint32_t argc, Arguments& argv);
};
template <typename T>
void UvHandle<T>::OnClose() {
onClose->Call(0, NULL);
onClose.Unwrap();
Unref();
}
template <typename T>
Value* UvHandle<T>::Close(uint32_t argc, Arguments& argv) {
if (argc > 1 && argv[1]->Is<Function>()) {
onClose.Wrap(argv[1]->As<Function>());
}
uv_close((uv_handle_t*) handle(), luv_on_close);
Ref();
return Nil::New();
}
class UvTimer
: public UvHandle<uv_timer_t> {
public:
static void init(Object* uv) {
Object* handle = Object::New();
uv->Set("Handle", handle);
handle->Set("close", Function::New(Close));
}
};
class UvPipe
: public UvHandle<uv_timer_t> {
static void init(Object* uv) {
Object* handle = Object::New();
uv->Set("Handle", handle);
handle->Set("close", Function::New(Close));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment