Allow protected values to be empty

This makes them much more friendly to being put inside of STL containers, like maps.
This commit is contained in:
Scott Kyle 2016-05-02 12:33:20 -07:00
parent dd23c66e69
commit 889b762566
2 changed files with 17 additions and 0 deletions

View File

@ -28,6 +28,7 @@ class Protected<JSGlobalContextRef> {
JSGlobalContextRef m_context;
public:
Protected() : m_context(nullptr) {}
Protected(const Protected<JSGlobalContextRef> &other) : Protected(other.m_context) {}
Protected(Protected<JSGlobalContextRef> &&other) : m_context(other.m_context) {
other.m_context = nullptr;
@ -43,6 +44,9 @@ class Protected<JSGlobalContextRef> {
operator JSGlobalContextRef() const {
return m_context;
}
operator bool() const {
return m_context != nullptr;
}
};
template<>
@ -51,6 +55,7 @@ class Protected<JSValueRef> {
JSValueRef m_value;
public:
Protected() {}
Protected(const Protected<JSValueRef> &other) : Protected(other.m_context, other.m_value) {}
Protected(Protected<JSValueRef> &&other) : m_context(other.m_context), m_value(other.m_value) {
other.m_context = nullptr;
@ -67,11 +72,15 @@ class Protected<JSValueRef> {
operator JSValueRef() const {
return m_value;
}
operator bool() const {
return m_value != nullptr;
}
};
template<>
class Protected<JSObjectRef> : public Protected<JSValueRef> {
public:
Protected() : Protected<JSValueRef>() {}
Protected(const Protected<JSObjectRef> &other) : Protected<JSValueRef>(other) {}
Protected(Protected<JSObjectRef> &&other) : Protected<JSValueRef>(std::move(other)) {}
Protected(JSContextRef ctx, JSObjectRef value) : Protected<JSValueRef>(ctx, value) {}

View File

@ -29,11 +29,15 @@ class Protected {
Nan::Persistent<MemberType, v8::CopyablePersistentTraits<MemberType>> m_value;
public:
Protected() {}
Protected(v8::Local<MemberType> value) : m_value(value) {}
operator v8::Local<MemberType>() const {
return Nan::New(m_value);
}
operator bool() const {
return m_value.isEmpty();
}
bool operator==(const v8::Local<MemberType> &other) const {
return m_value == other;
}
@ -55,6 +59,7 @@ namespace js {
template<>
class Protected<node::Types::GlobalContext> : public node::Protected<v8::Context> {
public:
Protected() : node::Protected<v8::Context>() {}
Protected(v8::Local<v8::Context> ctx) : node::Protected<v8::Context>(ctx) {}
operator v8::Isolate*() const {
@ -65,18 +70,21 @@ class Protected<node::Types::GlobalContext> : public node::Protected<v8::Context
template<>
class Protected<node::Types::Value> : public node::Protected<v8::Value> {
public:
Protected() : node::Protected<v8::Value>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Value> value) : node::Protected<v8::Value>(value) {}
};
template<>
class Protected<node::Types::Object> : public node::Protected<v8::Object> {
public:
Protected() : node::Protected<v8::Object>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Object> object) : node::Protected<v8::Object>(object) {}
};
template<>
class Protected<node::Types::Function> : public node::Protected<v8::Function> {
public:
Protected() : node::Protected<v8::Function>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Function> object) : node::Protected<v8::Function>(object) {}
};