Skip to content

Instantly share code, notes, and snippets.

@eendeego
Last active December 11, 2015 03:28
Show Gist options
  • Save eendeego/4537826 to your computer and use it in GitHub Desktop.
Save eendeego/4537826 to your computer and use it in GitHub Desktop.
Clean way to access node's typed arrays.
template<class C> class TypedArrayWrapper {
private:
Local<Object> array;
Handle<Object> buffer;
int offset;
public:
TypedArrayWrapper(const Local<Value>& arg) :
array(arg->ToObject()),
buffer(array->Get(String::New("buffer"))->ToObject()),
offset(array->Get(String::New("byteOffset"))->Int32Value()) {
}
inline __attribute__((always_inline)) C* const pointer() {
return (C*) &((char*) buffer->GetIndexedPropertiesExternalArrayData())[offset];
}
inline __attribute__((always_inline)) int length() {
return array->Get(String::New("length"))->Uint32Value();
}
};
Handle<Value> openvg::SetFV(const Arguments& args) {
HandleScope scope;
CheckArgs2(setFV, type, Int32, Float32Array, Object);
TypedArrayWrapper<VGfloat> values(args[1]);
vgSetfv((VGParamType) args[0]->Int32Value(),
values.length(),
values.pointer());
return Undefined();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment