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; JSGlobalContextRef m_context;
public: public:
Protected() : m_context(nullptr) {}
Protected(const Protected<JSGlobalContextRef> &other) : Protected(other.m_context) {} Protected(const Protected<JSGlobalContextRef> &other) : Protected(other.m_context) {}
Protected(Protected<JSGlobalContextRef> &&other) : m_context(other.m_context) { Protected(Protected<JSGlobalContextRef> &&other) : m_context(other.m_context) {
other.m_context = nullptr; other.m_context = nullptr;
@ -43,6 +44,9 @@ class Protected<JSGlobalContextRef> {
operator JSGlobalContextRef() const { operator JSGlobalContextRef() const {
return m_context; return m_context;
} }
operator bool() const {
return m_context != nullptr;
}
}; };
template<> template<>
@ -51,6 +55,7 @@ class Protected<JSValueRef> {
JSValueRef m_value; JSValueRef m_value;
public: public:
Protected() {}
Protected(const Protected<JSValueRef> &other) : Protected(other.m_context, other.m_value) {} 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) { Protected(Protected<JSValueRef> &&other) : m_context(other.m_context), m_value(other.m_value) {
other.m_context = nullptr; other.m_context = nullptr;
@ -67,11 +72,15 @@ class Protected<JSValueRef> {
operator JSValueRef() const { operator JSValueRef() const {
return m_value; return m_value;
} }
operator bool() const {
return m_value != nullptr;
}
}; };
template<> template<>
class Protected<JSObjectRef> : public Protected<JSValueRef> { class Protected<JSObjectRef> : public Protected<JSValueRef> {
public: public:
Protected() : Protected<JSValueRef>() {}
Protected(const Protected<JSObjectRef> &other) : Protected<JSValueRef>(other) {} Protected(const Protected<JSObjectRef> &other) : Protected<JSValueRef>(other) {}
Protected(Protected<JSObjectRef> &&other) : Protected<JSValueRef>(std::move(other)) {} Protected(Protected<JSObjectRef> &&other) : Protected<JSValueRef>(std::move(other)) {}
Protected(JSContextRef ctx, JSObjectRef value) : Protected<JSValueRef>(ctx, value) {} 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; Nan::Persistent<MemberType, v8::CopyablePersistentTraits<MemberType>> m_value;
public: public:
Protected() {}
Protected(v8::Local<MemberType> value) : m_value(value) {} Protected(v8::Local<MemberType> value) : m_value(value) {}
operator v8::Local<MemberType>() const { operator v8::Local<MemberType>() const {
return Nan::New(m_value); return Nan::New(m_value);
} }
operator bool() const {
return m_value.isEmpty();
}
bool operator==(const v8::Local<MemberType> &other) const { bool operator==(const v8::Local<MemberType> &other) const {
return m_value == other; return m_value == other;
} }
@ -55,6 +59,7 @@ namespace js {
template<> template<>
class Protected<node::Types::GlobalContext> : public node::Protected<v8::Context> { class Protected<node::Types::GlobalContext> : public node::Protected<v8::Context> {
public: public:
Protected() : node::Protected<v8::Context>() {}
Protected(v8::Local<v8::Context> ctx) : node::Protected<v8::Context>(ctx) {} Protected(v8::Local<v8::Context> ctx) : node::Protected<v8::Context>(ctx) {}
operator v8::Isolate*() const { operator v8::Isolate*() const {
@ -65,18 +70,21 @@ class Protected<node::Types::GlobalContext> : public node::Protected<v8::Context
template<> template<>
class Protected<node::Types::Value> : public node::Protected<v8::Value> { class Protected<node::Types::Value> : public node::Protected<v8::Value> {
public: public:
Protected() : node::Protected<v8::Value>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Value> value) : node::Protected<v8::Value>(value) {} Protected(v8::Isolate* isolate, v8::Local<v8::Value> value) : node::Protected<v8::Value>(value) {}
}; };
template<> template<>
class Protected<node::Types::Object> : public node::Protected<v8::Object> { class Protected<node::Types::Object> : public node::Protected<v8::Object> {
public: public:
Protected() : node::Protected<v8::Object>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Object> object) : node::Protected<v8::Object>(object) {} Protected(v8::Isolate* isolate, v8::Local<v8::Object> object) : node::Protected<v8::Object>(object) {}
}; };
template<> template<>
class Protected<node::Types::Function> : public node::Protected<v8::Function> { class Protected<node::Types::Function> : public node::Protected<v8::Function> {
public: public:
Protected() : node::Protected<v8::Function>() {}
Protected(v8::Isolate* isolate, v8::Local<v8::Function> object) : node::Protected<v8::Function>(object) {} Protected(v8::Isolate* isolate, v8::Local<v8::Function> object) : node::Protected<v8::Function>(object) {}
}; };