Commit 6e189f5a authored by dcarney's avatar dcarney Committed by Commit bot

give UniquePersistent full move semantics

BUG=v8:3669
LOG=Y

Review URL: https://codereview.chromium.org/978783002

Cr-Commit-Position: refs/heads/master@{#27004}
parent 7f78e7b3
...@@ -665,8 +665,8 @@ template <class T> class PersistentBase { ...@@ -665,8 +665,8 @@ template <class T> class PersistentBase {
friend class Object; friend class Object;
explicit V8_INLINE PersistentBase(T* val) : val_(val) {} explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
PersistentBase(PersistentBase& other); // NOLINT PersistentBase(PersistentBase& other) = delete; // NOLINT
void operator=(PersistentBase&); void operator=(PersistentBase&) = delete;
V8_INLINE static T* New(Isolate* isolate, T* that); V8_INLINE static T* New(Isolate* isolate, T* that);
T* val_; T* val_;
...@@ -814,16 +814,11 @@ template <class T, class M> class Persistent : public PersistentBase<T> { ...@@ -814,16 +814,11 @@ template <class T, class M> class Persistent : public PersistentBase<T> {
*/ */
template<class T> template<class T>
class UniquePersistent : public PersistentBase<T> { class UniquePersistent : public PersistentBase<T> {
struct RValue {
V8_INLINE explicit RValue(UniquePersistent* obj) : object(obj) {}
UniquePersistent* object;
};
public: public:
/** /**
* A UniquePersistent with no storage cell. * A UniquePersistent with no storage cell.
*/ */
V8_INLINE UniquePersistent() : PersistentBase<T>(0) { } V8_INLINE UniquePersistent() : PersistentBase<T>(nullptr) {}
/** /**
* Construct a UniquePersistent from a Handle. * Construct a UniquePersistent from a Handle.
* When the Handle is non-empty, a new storage cell is created * When the Handle is non-empty, a new storage cell is created
...@@ -847,34 +842,32 @@ class UniquePersistent : public PersistentBase<T> { ...@@ -847,34 +842,32 @@ class UniquePersistent : public PersistentBase<T> {
/** /**
* Move constructor. * Move constructor.
*/ */
V8_INLINE UniquePersistent(RValue rvalue) V8_INLINE UniquePersistent(UniquePersistent&& other)
: PersistentBase<T>(rvalue.object->val_) { : PersistentBase<T>(other.val_) {
rvalue.object->val_ = 0; other.val_ = nullptr;
} }
V8_INLINE ~UniquePersistent() { this->Reset(); } V8_INLINE ~UniquePersistent() { this->Reset(); }
/** /**
* Move via assignment. * Move via assignment.
*/ */
template<class S> template <class S>
V8_INLINE UniquePersistent& operator=(UniquePersistent<S> rhs) { V8_INLINE UniquePersistent& operator=(UniquePersistent<S>&& rhs) {
TYPE_CHECK(T, S); TYPE_CHECK(T, S);
this->Reset(); if (this != &rhs) {
this->val_ = rhs.val_; this->Reset();
rhs.val_ = 0; this->val_ = rhs.val_;
rhs.val_ = nullptr;
}
return *this; return *this;
} }
/**
* Cast operator for moves.
*/
V8_INLINE operator RValue() { return RValue(this); }
/** /**
* Pass allows returning uniques from functions, etc. * Pass allows returning uniques from functions, etc.
*/ */
UniquePersistent Pass() { return UniquePersistent(RValue(this)); } UniquePersistent Pass() { return static_cast<UniquePersistent&&>(*this); }
private: private:
UniquePersistent(UniquePersistent&); UniquePersistent(UniquePersistent&) = delete;
void operator=(UniquePersistent&); void operator=(UniquePersistent&) = delete;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment