forked from UrielCh/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectWrap.h
180 lines (148 loc) · 4.74 KB
/
ObjectWrap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "utils.h"
#include "ArrayConverter.h"
#include "ArrayOfArraysConverter.h"
#include "InstanceConverter.h"
#include "TryCatch.h"
#include <functional>
#ifndef __FF_OBJECT_WRAP_H__
#define __FF_OBJECT_WRAP_H__
namespace FF {
template<class TClass>
class ObjectWrapBase {
public:
static TClass* unwrapClassPtrUnchecked(v8::Local<v8::Value> jsVal) {
return unwrapNanObjectWrap<TClass>(jsVal);
}
static TClass* unwrapThis(Nan::NAN_METHOD_ARGS_TYPE info) {
return unwrapClassPtrUnchecked(info.This());
}
static TClass* unwrapThis(Nan::NAN_GETTER_ARGS_TYPE info) {
return unwrapClassPtrUnchecked(info.This());
}
static TClass* unwrapThis(Nan::NAN_SETTER_ARGS_TYPE info) {
return unwrapClassPtrUnchecked(info.This());
}
};
template<class TClass, class T>
class ObjectWrapTemplate : public ObjectWrapBase<TClass> {
public:
typedef ObjectWrapBase<TClass> super;
T self;
void setNativeObject(T obj) { self = obj; }
typedef InstanceConverterImpl<TClass, T> ConverterImpl;
typedef AbstractConverter<ConverterImpl> Converter;
template<class CastType>
class WithCastConverter : public AbstractConverter<InstanceConverterImpl<TClass, CastType>> {};
template<class ElementCastType>
class ArrayWithCastConverter : public ArrayConverterTemplate<ConverterImpl, ElementCastType> {};
template<class ElementCastType>
class ArrayOfArraysWithCastConverter : public ArrayOfArraysConverterTemplate<ConverterImpl, ElementCastType> {};
typedef ArrayWithCastConverter<T> ArrayConverter;
typedef ArrayOfArraysWithCastConverter<T> ArrayOfArraysConverter;
template<class WorkerImpl>
static void syncBinding(std::string name, Nan::NAN_METHOD_ARGS_TYPE info) {
auto worker = std::make_shared<WorkerImpl>();
worker->setup(unwrapSelf(info));
FF::executeSyncBinding(
worker,
std::string(TClass::getClassName()) + "::" + name,
info
);
}
template<class WorkerImpl>
static void asyncBinding(std::string name, Nan::NAN_METHOD_ARGS_TYPE info) {
auto worker = std::make_shared<WorkerImpl>();
worker->setup(unwrapSelf(info));
FF::executeAsyncBinding(
worker,
std::string(TClass::getClassName()) + "::" + name + "Async",
info
);
}
static bool hasInstance(v8::Local<v8::Value> jsVal) {
return TClass::ConverterImpl::assertType(jsVal);
}
static T unwrapSelf(Nan::NAN_METHOD_ARGS_TYPE info) {
return unwrapSelf(info.This());
}
static T unwrapSelf(Nan::NAN_GETTER_ARGS_TYPE info) {
return unwrapSelf(info.This());
}
static T unwrapSelf(Nan::NAN_SETTER_ARGS_TYPE info) {
return unwrapSelf(info.This());
}
protected:
typedef TClass ClassType;
template<class TPropertyConverter>
static void setter(
const char* setterName,
Nan::NAN_SETTER_ARGS_TYPE info,
v8::Local<v8::Value> value,
void(*setProperty)(TClass*, typename TPropertyConverter::Type)
) {
FF::TryCatch tryCatch(setterName);
typename TPropertyConverter::Type val;
if (TPropertyConverter::unwrapTo(&val, value)) {
return tryCatch.reThrow();
}
setProperty(super::unwrapThis(info), val);
}
template<class TPropertyConverter>
static void getter(
Nan::NAN_GETTER_ARGS_TYPE info,
typename TPropertyConverter::Type(*getProperty)(TClass*)
) {
info.GetReturnValue().Set(TPropertyConverter::wrap(getProperty(super::unwrapThis(info))));
}
template<class ConstructorImpl>
static void constructorBinding(Nan::NAN_METHOD_ARGS_TYPE info) {
FF::executeSyncBinding(
std::make_shared<ConstructorImpl>(info),
std::string(TClass::getClassName()) + "::Constructor",
info
);
};
class ConstructorBase : public BindingBase, public ISyncWorker {
public:
bool applyUnwrappers(Nan::NAN_METHOD_ARGS_TYPE info) {
if (!info.IsConstructCall()) {
Nan::ThrowError("constructor has to be called with \"new\" keyword");
return true;
}
return BindingBase::applyUnwrappers(info);
}
v8::Local<v8::Value> getReturnValue(Nan::NAN_METHOD_ARGS_TYPE info) {
TClass* self = new TClass();
self->Wrap(info.Holder());
self->setNativeObject(nativeObject);
modifySelf(self);
return info.Holder();
}
std::string execute() {
try {
nativeObject = executeBinding();
return "";
}
catch (std::exception &e) {
return std::string(e.what());
}
}
protected:
T nativeObject;
std::function<T(void)> executeBinding;
std::function<void(TClass* self)> modifySelf = [](TClass*) {};
};
private:
static T unwrapSelf(v8::Local<v8::Object> thisObj) {
return super::unwrapClassPtrUnchecked(thisObj)->self;
}
};
template<class TClass, class T>
class ObjectWrap : public ObjectWrapTemplate<TClass, T>, public Nan::ObjectWrap {
public:
void Wrap(v8::Local<v8::Object> object) {
Nan::ObjectWrap::Wrap(object);
}
};
}
#endif