External buffer for nbind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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