Skip to content

Commit ead7513

Browse files
committed
Enable move constructor/assignment for ref-counted pointers
1 parent de32a77 commit ead7513

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/memory/SharedPtr.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,20 @@ namespace Sass {
3737
, dbg(false)
3838
#endif
3939
{
40-
refcounter = 0;
41-
#ifdef DEBUG_SHARED_PTR
42-
if (taint) all.push_back(this);
43-
#endif
44-
};
45-
46-
SharedObj::~SharedObj() {
47-
#ifdef DEBUG_SHARED_PTR
48-
if (dbg) std::cerr << "Destruct " << this << "\n";
49-
if(!all.empty()) { // check needed for MSVC (no clue why?)
50-
all.erase(std::remove(all.begin(), all.end(), this), all.end());
51-
}
52-
#endif
53-
};
54-
40+
refcounter = 0;
41+
#ifdef DEBUG_SHARED_PTR
42+
if (taint) all.push_back(this);
43+
#endif
44+
};
5545

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

5755
void SharedPtr::decRefCount() {
5856
if (node) {

src/memory/SharedPtr.hpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ namespace Sass {
8686

8787

8888
class SharedPtr {
89-
private:
89+
protected:
9090
SharedObj* node;
91-
private:
91+
protected:
9292
void decRefCount();
9393
void incRefCount();
9494
public:
@@ -97,16 +97,16 @@ namespace Sass {
9797
: node(NULL) {};
9898
// the create constructor
9999
SharedPtr(SharedObj* ptr);
100-
// copy assignment operator
101-
SharedPtr& operator=(const SharedPtr& rhs);
102-
// move assignment operator
103-
/* SharedPtr& operator=(SharedPtr&& rhs); */
104100
// the copy constructor
105101
SharedPtr(const SharedPtr& obj);
106102
// the move constructor
107-
/* SharedPtr(SharedPtr&& obj); */
108-
// destructor
109-
~SharedPtr();
103+
SharedPtr(SharedPtr&& obj);
104+
// copy assignment operator
105+
SharedPtr& operator=(const SharedPtr& obj);
106+
// move assignment operator
107+
SharedPtr& operator=(SharedPtr&& obj);
108+
// pure virtual destructor
109+
virtual ~SharedPtr() = 0;
110110
public:
111111
SharedObj* obj () const {
112112
return node;
@@ -146,6 +146,29 @@ namespace Sass {
146146
: SharedPtr(node) {};
147147
SharedImpl(const T& node)
148148
: SharedPtr(node) {};
149+
// the copy constructor
150+
SharedImpl(const SharedImpl<T>& impl)
151+
: SharedPtr(impl.node) {};
152+
// the move constructor
153+
SharedImpl(SharedImpl<T>&& impl)
154+
: SharedPtr(impl.node) {};
155+
// copy assignment operator
156+
SharedImpl<T>& operator=(const SharedImpl<T>& rhs) {
157+
if (node) decRefCount();
158+
node = rhs.node;
159+
incRefCount();
160+
return *this;
161+
}
162+
// move assignment operator
163+
SharedImpl<T>& operator=(SharedImpl<T>&& rhs) {
164+
// don't move our self
165+
if (this != &rhs) {
166+
if (node) decRefCount();
167+
node = std::move(rhs.node);
168+
rhs.node = NULL;
169+
}
170+
return *this;
171+
}
149172
~SharedImpl() {};
150173
public:
151174
operator T*() const {

0 commit comments

Comments
 (0)