I overkilled RefCount. Too tired. Heres the proper fix.
This commit is contained in:
parent
5385eb9070
commit
a1b78a1e6d
1 changed files with 19 additions and 40 deletions
|
@ -25,7 +25,6 @@
|
||||||
namespace FbTk {
|
namespace FbTk {
|
||||||
|
|
||||||
/// holds a pointer with reference counting, similar to std:auto_ptr
|
/// holds a pointer with reference counting, similar to std:auto_ptr
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
class RefCount {
|
class RefCount {
|
||||||
public:
|
public:
|
||||||
|
@ -40,7 +39,7 @@ public:
|
||||||
Pointer *operator -> () const { return get(); }
|
Pointer *operator -> () const { return get(); }
|
||||||
Pointer *get() const { return m_data; }
|
Pointer *get() const { return m_data; }
|
||||||
#ifdef NOT_USED
|
#ifdef NOT_USED
|
||||||
/// @return number of referenses
|
/// @return number of references
|
||||||
unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); }
|
unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); }
|
||||||
#endif
|
#endif
|
||||||
private:
|
private:
|
||||||
|
@ -48,43 +47,33 @@ private:
|
||||||
void incRefCount();
|
void incRefCount();
|
||||||
/// decrease reference count
|
/// decrease reference count
|
||||||
void decRefCount();
|
void decRefCount();
|
||||||
/// decrease refcount count
|
|
||||||
void decRefCountCount();
|
|
||||||
Pointer *m_data; ///< data holder
|
Pointer *m_data; ///< data holder
|
||||||
mutable unsigned int *m_refcount; ///< holds reference counting
|
mutable unsigned int *m_refcount; ///< holds reference counting
|
||||||
|
|
||||||
// This one counts the number of active references pointing to the m_refcount data!
|
|
||||||
// when it reaches zero, *then* we can delete it, otherwise someone else might check it.
|
|
||||||
mutable unsigned int *m_refcount_count; ///< holds reference counting
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// implementation
|
// implementation
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
RefCount<Pointer>::RefCount():m_data(0), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) {
|
RefCount<Pointer>::RefCount():m_data(0), m_refcount(new unsigned int(0)) {
|
||||||
|
incRefCount(); // it really counts how many things are storing m_refcount
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
RefCount<Pointer>::RefCount(RefCount<Pointer> ©):
|
RefCount<Pointer>::RefCount(RefCount<Pointer> ©):
|
||||||
m_data(copy.m_data),
|
m_data(copy.m_data),
|
||||||
m_refcount(copy.m_refcount),
|
m_refcount(copy.m_refcount) {
|
||||||
m_refcount_count(copy.m_refcount_count) {
|
|
||||||
(*m_refcount_count)++;
|
|
||||||
incRefCount();
|
incRefCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
RefCount<Pointer>::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) {
|
RefCount<Pointer>::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)) {
|
||||||
incRefCount();
|
incRefCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
RefCount<Pointer>::RefCount(const RefCount<Pointer> ©):
|
RefCount<Pointer>::RefCount(const RefCount<Pointer> ©):
|
||||||
m_data(copy.m_data),
|
m_data(copy.m_data),
|
||||||
m_refcount(copy.m_refcount),
|
m_refcount(copy.m_refcount) {
|
||||||
m_refcount_count(copy.m_refcount_count) {
|
|
||||||
(*m_refcount_count)++;
|
|
||||||
incRefCount();
|
incRefCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,8 +86,6 @@ template <typename Pointer>
|
||||||
RefCount<Pointer> &RefCount<Pointer>::operator = (const RefCount<Pointer> ©) {
|
RefCount<Pointer> &RefCount<Pointer>::operator = (const RefCount<Pointer> ©) {
|
||||||
decRefCount(); // dec current ref count
|
decRefCount(); // dec current ref count
|
||||||
m_refcount = copy.m_refcount; // set new ref count
|
m_refcount = copy.m_refcount; // set new ref count
|
||||||
m_refcount_count = copy.m_refcount_count;
|
|
||||||
(*m_refcount_count)++;
|
|
||||||
m_data = copy.m_data; // set new data pointer
|
m_data = copy.m_data; // set new data pointer
|
||||||
incRefCount(); // inc new ref count
|
incRefCount(); // inc new ref count
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -109,35 +96,27 @@ RefCount<Pointer> &RefCount<Pointer>::operator = (Pointer *p) {
|
||||||
decRefCount();
|
decRefCount();
|
||||||
m_data = p; // set data pointer
|
m_data = p; // set data pointer
|
||||||
m_refcount = new unsigned int(0); // create new counter
|
m_refcount = new unsigned int(0); // create new counter
|
||||||
m_refcount_count = new unsigned int(1);
|
|
||||||
incRefCount();
|
incRefCount();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
void RefCount<Pointer>::decRefCount() {
|
void RefCount<Pointer>::decRefCount() {
|
||||||
if (m_refcount != 0) {
|
if (m_refcount == 0)
|
||||||
(*m_refcount)--;
|
return;
|
||||||
if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this
|
if (*m_refcount == 0) { // already zero, then delete refcount
|
||||||
if (m_data != 0)
|
|
||||||
delete m_data;
|
|
||||||
m_data = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
decRefCountCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Pointer>
|
|
||||||
void RefCount<Pointer>::decRefCountCount() {
|
|
||||||
if (*m_refcount_count == 0)
|
|
||||||
return; // shouldnt happen
|
|
||||||
(*m_refcount_count)--;
|
|
||||||
if (*m_refcount_count == 0) {
|
|
||||||
delete m_refcount;
|
delete m_refcount;
|
||||||
delete m_refcount_count;
|
m_refcount = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
(*m_refcount)--;
|
||||||
|
if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this
|
||||||
|
if (m_data != 0)
|
||||||
|
delete m_data;
|
||||||
|
m_data = 0;
|
||||||
|
delete m_refcount;
|
||||||
|
m_refcount = 0;
|
||||||
}
|
}
|
||||||
m_refcount = 0;
|
|
||||||
m_refcount_count = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Pointer>
|
template <typename Pointer>
|
||||||
|
|
Loading…
Reference in a new issue