Skip to content

Instantly share code, notes, and snippets.

@Luthaf
Last active October 2, 2017 20:33
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 Luthaf/19c8fa71c862f93f416cb6d6ddf2d41c to your computer and use it in GitHub Desktop.
Save Luthaf/19c8fa71c862f93f416cb6d6ddf2d41c to your computer and use it in GitHub Desktop.
External buffer for nbind
#include <cstddef>
/// A non-owning memory view in a C++ std::vector<double> like object.
class ExternalBuffer {
public:
ExternalBuffer(double* data, size_t length):
data_(data), length_(length) {}
void* data() {
return reinterpret_cast<void*>(data_);
}
size_t byte_length() {
return length_ * sizeof(double);
}
size_t length() {
return length_;
}
private:
double* data_;
/// The actual
size_t length_;
};
#if defined(BUILDING_NODE_EXTENSION)
#include "nbind/api.h"
#include <nan.h>
#include <node_version.h>
namespace nbind {
template <> struct BindingType<ExternalBuffer> {
typedef ExternalBuffer Type;
static bool checkType(WireType /*unused*/) {
return false;
}
static Type fromWireType(WireType /*unused*/) {
NBIND_ERR("can not create a BorrowedBuffer from a JS object");
return Type(nullptr, 0);
}
static WireType toWireType(Type &&arg) {
auto buffer = v8::ArrayBuffer::New(
v8::Isolate::GetCurrent(), arg.data(), arg.byte_length()
);
return v8::Float64Array::New(buffer, 0, arg.length());
}
};
#elif defined(__EMSCRIPTEN__)
// TODO
#endif
} // namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment