Skip to content

Commit

Permalink
Enable move constructor/assignment for ref-counted pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Feb 17, 2017
1 parent de32a77 commit df7830b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
28 changes: 13 additions & 15 deletions src/memory/SharedPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,20 @@ namespace Sass {
, dbg(false)
#endif
{
refcounter = 0;
#ifdef DEBUG_SHARED_PTR
if (taint) all.push_back(this);
#endif
};

SharedObj::~SharedObj() {
#ifdef DEBUG_SHARED_PTR
if (dbg) std::cerr << "Destruct " << this << "\n";
if(!all.empty()) { // check needed for MSVC (no clue why?)
all.erase(std::remove(all.begin(), all.end(), this), all.end());
}
#endif
};

refcounter = 0;
#ifdef DEBUG_SHARED_PTR
if (taint) all.push_back(this);
#endif
};

SharedObj::~SharedObj() {
#ifdef DEBUG_SHARED_PTR
if (dbg) std::cerr << "Destruct " << this << "\n";
if(!all.empty()) { // check needed for MSVC (no clue why?)
all.erase(std::remove(all.begin(), all.end(), this), all.end());
}
#endif
};

void SharedPtr::decRefCount() {
if (node) {
Expand Down
39 changes: 30 additions & 9 deletions src/memory/SharedPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ namespace Sass {


class SharedPtr {
private:
protected:
SharedObj* node;
private:
protected:
void decRefCount();
void incRefCount();
public:
Expand All @@ -97,16 +97,16 @@ namespace Sass {
: node(NULL) {};
// the create constructor
SharedPtr(SharedObj* ptr);
// copy assignment operator
SharedPtr& operator=(const SharedPtr& rhs);
// move assignment operator
/* SharedPtr& operator=(SharedPtr&& rhs); */
// the copy constructor
SharedPtr(const SharedPtr& obj);
// the move constructor
/* SharedPtr(SharedPtr&& obj); */
// destructor
~SharedPtr();
SharedPtr(SharedPtr&& obj);
// copy assignment operator
SharedPtr& operator=(const SharedPtr& obj);
// move assignment operator
SharedPtr& operator=(SharedPtr&& obj);
// pure virtual destructor
virtual ~SharedPtr() = 0;
public:
SharedObj* obj () const {
return node;
Expand Down Expand Up @@ -146,6 +146,27 @@ namespace Sass {
: SharedPtr(node) {};
SharedImpl(const T& node)
: SharedPtr(node) {};
// the copy constructor
SharedImpl(const SharedImpl<T>& impl)
: SharedPtr(impl.node) {};
// the move constructor
SharedImpl(SharedImpl<T>&& impl)
: SharedPtr(impl.node) {};
// copy assignment operator
SharedImpl<T>& operator=(const SharedImpl<T>& rhs) {
node = rhs.node;
incRefCount();
return *this;
}
// move assignment operator
SharedImpl<T>& operator=(SharedImpl<T>&& rhs) {
// don't move our self
if (this != &rhs) {
node = std::move(rhs.node);
rhs.node = NULL;
}
return *this;
}
~SharedImpl() {};
public:
operator T*() const {
Expand Down

0 comments on commit df7830b

Please sign in to comment.