Permalink
Browse files

Expose createGestureGeneratorSync()

  • Loading branch information...
1 parent 414162d commit 72383511c0b8c75d918b3a21e38546a0f3ef5bce @jmendeth committed Apr 10, 2012
Showing with 58 additions and 19 deletions.
  1. +1 −1 src/Context.cpp
  2. +26 −15 src/GestureGenerator.cpp
  3. +1 −1 src/GestureGenerator.h
  4. +25 −1 src/wrapperUtils.cpp
  5. +5 −1 src/wrapperUtils.h
View
@@ -27,7 +27,7 @@ namespace node_xn {
using namespace node;
void Context::Init(XnContextPtr& handle) {
- xnInit(&handle);//TODO: check status
+ check( xnInit(&handle) );
}
void Context::OnConstruct() {
View
@@ -26,16 +26,16 @@ namespace node_xn {
using namespace v8;
using namespace node;
- void GestureGenerator::create(const Context& ctx, XnNodeHandle& handle, XnNodeQuery* query) {
- xnCreateGestureGenerator(ctx.ptr, &handle, query, NULL); //TODO: check status
+ void GestureGenerator::Create(const Context& ctx, XnNodeHandle& handle, XnNodeQuery* query) {
+ check( xnCreateGestureGenerator(ctx.ptr, &handle, query, NULL) );
}
Persistent<FunctionTemplate> INIT_GestureGenerator(Handle<Object> ctx, Persistent<FunctionTemplate> parent) {
HandleScope scope;
//1. Declare the class prototype
Local<FunctionTemplate> protoL = FunctionTemplate::New(new_default);
- Persistent<FunctionTemplate> proto = v8::Persistent<FunctionTemplate>::New(protoL);
+ Persistent<FunctionTemplate> proto = Persistent<FunctionTemplate>::New(protoL);
proto->InstanceTemplate()->SetInternalFieldCount(1);
proto->Inherit(parent);
proto->SetClassName(v8::String::NewSymbol("GestureGenerator"));
@@ -44,23 +44,34 @@ namespace node_xn {
//3. Bind methods
- //4. Finally, add the things to the target
- ctx->Set(v8::String::NewSymbol("GestureGenerator"), proto->GetFunction());
+ //4. Declare static factory method(s)
+ Local<FunctionTemplate> createL = FunctionTemplate::New(GestureGenerator::createGestureGeneratorSync, proto->GetFunction());
+ Persistent<FunctionTemplate> create = Persistent<FunctionTemplate>::New(createL);
+
+ //5. Finally, add the things to the target
+ ctx->Set(v8::String::NewSymbol("GestureGenerator"), proto->GetFunction());
+ ctx->Set(v8::String::NewSymbol("createGestureGeneratorSync"), create->GetFunction());
return proto;
}
-
+
Handle<Value> GestureGenerator::createGestureGeneratorSync(const Arguments& args) {
HandleScope scope;
+ try {
+
+ //Check for arguments
+ checkArgumentsLen(args, 1);
+
+ //Extract arguments
+ Context& ctx = *(Context::Unwrap<Context>(args[0]->ToObject()));
+
+ Handle<Object> instH = ((Function*)(*args.Data()))->NewInstance();
+ XnNodeHandle handle;
+ Create(ctx, handle, NULL);
+ (new GestureGenerator(handle))->Wrap(instH); //FIXME: should we use WrapperUtils wrap()?
+
+ return instH;
- //Extract arguments
- Context& ctx = *(Context::Unwrap<Context>(args[0]->ToObject()));
-
- Handle<Object> instH = ((Function*)(*args.Data()))->NewInstance();
- XnNodeHandle handle;
- create(ctx, handle, NULL);
- (new GestureGenerator(handle))->Wrap(instH); //FIXME: should we use wrap()?
-
- return instH;
+ } catch (Handle<Value> err) {return ThrowException(err);}
}
}
View
@@ -34,7 +34,7 @@ namespace node_xn {
class GestureGenerator: public Generator {
public:
/* Factory method(s) */
- static void create(const Context& ctx, XnNodeHandle& handle, XnNodeQuery* query);
+ static void Create(const Context& ctx, XnNodeHandle& handle, XnNodeQuery* query);
/* Copy ctor. */
inline GestureGenerator(const GestureGenerator& orig) : Generator(orig) {}
View
@@ -21,13 +21,37 @@
#include "wrapperUtils.h"
+#include <sstream>
+
namespace node_xn {
using namespace v8;
using namespace node;
//wrap
- //check
+
+ void check(XnStatus status) {
+ if (status == XN_STATUS_OK) return;
+
+ throw Exception::Error(v8::String::New(xnGetStatusString(status))); //TODO
+ }
+
+ void checkArgumentsLen(const Arguments& args, const int min) {
+ const int len = args.Length();
+ if (len >= min) return;
+
+ const char* mmsg = " arguments";
+ if (min == 1) mmsg = " argument";
+
+ const char* lmsg = " were";
+ if (len == 1) lmsg = " was";
+
+ std::stringstream msg;
+ msg << "Function needs " << min << mmsg << ", but " << len << lmsg << " provided.";
+
+ throw Exception::TypeError(v8::String::New(msg.str().data()));
+ }
+
//checkValid
Handle<Value> new_default(const Arguments& args) {
View
@@ -35,7 +35,11 @@ namespace node_xn {
using namespace node;
//wrap
- //check
+
+ void check(XnStatus status);
+
+ void checkArgumentsLen(const Arguments& args, const int min);
+
//checkValid
Handle<Value> new_default(const Arguments& args);

0 comments on commit 7238351

Please sign in to comment.