forked from UrielCh/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractConverter.h
125 lines (110 loc) · 3.19 KB
/
AbstractConverter.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
#include "utils.h"
#ifndef __FF_ABSTRACT_CONVERTER_H__
#define __FF_ABSTRACT_CONVERTER_H__
namespace FF {
/*
ConverterImpl implements:
static std::string getTypeName()
static v8::Local<v8::Value> wrap(ConverterImpl::Type val)
static bool unwrap(ConverterImpl::Type* pVal, v8::Local<v8::Value> jsVal)
static ConverterImpl::Type unwrapUnchecked(v8::Local<v8::Value> jsVal)
*/
template <class ConverterImpl>
class AbstractConverter {
public:
typedef typename ConverterImpl::Type Type;
static bool assertType(v8::Local<v8::Value> jsVal) {
return ConverterImpl::assertType(jsVal);
}
static v8::Local<v8::Value> wrap(Type val) {
return ConverterImpl::wrap(val);
}
static Type unwrapUnchecked(v8::Local<v8::Value> jsVal) {
return ConverterImpl::unwrapUnchecked(jsVal);
}
static bool unwrapTo(Type* val, v8::Local<v8::Value> jsVal) {
if (ConverterImpl::unwrap(val, jsVal)) {
Nan::ThrowError(FF::newString(
std::string("failed to unwrap value, expected ")
+ std::string(ConverterImpl::getTypeName())
));
return true;
}
return false;
}
static bool arg(int argN, Type* val, Nan::NAN_METHOD_ARGS_TYPE info) {
Nan::TryCatch tryCatch;
if (!hasArg(info, argN) || ConverterImpl::unwrap(val, info[argN])) {
if (tryCatch.HasCaught()) {
tryCatch.ReThrow();
}
else {
Nan::ThrowError(FF::newString(
std::string("expected argument ")
+ std::to_string(argN)
+ std::string(" to be of type ")
+ std::string(ConverterImpl::getTypeName())
));
tryCatch.ReThrow();
}
return true;
}
return false;
}
static bool optArg(int argN, Type* val, Nan::NAN_METHOD_ARGS_TYPE info) {
if (hasArg(info, argN) && info[argN]->IsFunction()) {
return false;
}
Nan::TryCatch tryCatch;
if (hasArg(info, argN) && ConverterImpl::unwrap(val, info[argN])) {
if (tryCatch.HasCaught()) {
tryCatch.ReThrow();
}
else {
Nan::ThrowError(FF::newString(
std::string("expected argument ")
+ std::to_string(argN)
+ std::string(" to be of type ")
+ std::string(ConverterImpl::getTypeName())
));
tryCatch.ReThrow();
}
return true;
}
return false;
}
static bool prop(Type* val, const char* prop, v8::Local<v8::Object> opts) {
if (!Nan::HasOwnProperty(opts, Nan::New(prop).ToLocalChecked()).FromJust()) {
Nan::ThrowError(FF::newString(
std::string("expected object to have property: ")
+ std::string(prop)
));
return true;
}
return AbstractConverter::optProp(val, prop, opts);
}
static bool optProp(Type* val, const char* prop, v8::Local<v8::Object> opts) {
Nan::TryCatch tryCatch;
if (
Nan::HasOwnProperty(opts, Nan::New(prop).ToLocalChecked()).FromJust()
&& ConverterImpl::unwrap(val, Nan::Get(opts, Nan::New(prop).ToLocalChecked()).ToLocalChecked())
) {
if (tryCatch.HasCaught()) {
tryCatch.ReThrow();
}
else {
Nan::ThrowError(FF::newString(
std::string("expected property ")
+ std::string(prop)
+ std::string(" to be of type ")
+ std::string(ConverterImpl::getTypeName())
));
tryCatch.ReThrow();
}
return true;
}
return false;
}
};
}
#endif