Two smartpointer designs

P

paalkrebs

I've made two smartpointers that I'd like to share. Improvements and
comments are welcome.

The key to both is that there should be no access to the managed
object, and all operations should be done on the smartpointers. Both
are designed in a similar way, where a userclass inherits from a
template and the smartpointer is defined as an internal class of the
template;

The first one is an automatic refcount smartpointer where the object
pointed to keeps track of the current refcount, and the object gets
destroyed if the refcount goes to 0. All refcount management is
automatic.

The second is called a safepointer, and the managed object will
automatically clear all safepointers which references it when
destroyed. Destruction is done through a smartpointer's destroy
function.



I'm including a testfile which shows how they are used, followed by
the 2 template files:


// ConsoleTests.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include "../../common/autorefcount.h"
#include "../../common/safepointer.h"

class A;
class A : public TAutoRefable<A*>
{
public:
static ARPtr Create(int x){return ARPtr(new A(x));} // create an
instance of A

int GetX(){return mX;}
private:
A(int x) : mX(x){} // keep constructors private in inherited classes,
and use Create to instanciate

int mX;
};

void TestAutoRefPointer(){
A::ARPtr c;
bool valid = c.IsValid(); // false

c = A::Create(1);
if (c.IsValid()) { // true
int x = c->GetX();
}

DWORD refcount = c.GetRefCount();// refcount = 1

{
A::ARPtr d(c);
refcount = c.GetRefCount();// refcount = 2
{
A::ARPtr e;
e = c;
refcount = c.GetRefCount();// refcount = 3
}
refcount = c.GetRefCount();// refcount = 2
}
refcount = c.GetRefCount();// refcount = 1

valid = c.IsValid(); // true
c.Clear(); // refcount = 0, object gets destroyed
refcount = c.GetRefCount();// refcount = 0
valid = c.IsValid(); // false

}


class S;
class S : public TSafeObj<S*>
{
public:
static SPtr Create(int x){return SPtr(new S(x));} // create an
instance of S
int GetX(){return mX;}

private:
S(int x) : TSafeObj<S*>(5), mX(x){}// keep constructors private in
inherited classes, and use Create to instanciate
int mX;
};


void TestSafePointer(){
S::SPtr c;

bool valid = c.IsValid(); // false

c = S::Create(1);
if (c.IsValid()){ // true
int x = c->GetX();
}

S::SPtr d(c);
S::SPtr e;
e = c;

d.Destroy(); // destroys object, and clears all SafePointers to it


if (c.IsValid() || d.IsValid() || e.IsValid()) {// false
int x = c->GetX();
}
}


int _tmain(int argc, _TCHAR* argv[])
{
TestAutoRefPointer();
TestSafePointer();
return 0;
}


// autorefcount - ptr with auto refcount

#pragma once
#include "comdef.h"


template <class T> class TAutoRefable
{
public:
class ARPtr
{
public:
ARPtr() : mPtr(NULL){}
ARPtr(T p) : mPtr(p) {
if (p) {
p->AddRef();
}
}
ARPtr(const ARPtr& s) : mPtr(s.mPtr){
if (mPtr) {
mPtr->AddRef();
}
}
~ARPtr()
{
if (mPtr){
mPtr->SubRef();
}
}
DWORD GetRefCount(){return (mPtr) ? mPtr->mRefCount : 0;}

T operator -> (){return mPtr;}
ARPtr& operator =(const ARPtr& that){
if (this != &that) {
if (mPtr){
mPtr->SubRef(); // deletes object if refcount goes to 0, so mPtr
can point to freed mem after this.
}
if (that.mPtr) {
that.mPtr->AddRef();
}
mPtr = that.mPtr;
}
return *this;
}

void Clear(){
if (mPtr){
mPtr->SubRef();
}
mPtr = NULL;
}

bool IsValid(){return mPtr != NULL;}

private:
T mPtr;
};

// initial values for event and subsciber dynamic arrays
static ARPtr Create(){return ARPtr(NULL);} // override this with more
interesting actions
protected: // keep constructors and destructors protected/private in
inherited classes
TAutoRefable() : mRefCount(0){}
virtual ~TAutoRefable(){}

private:
friend ARPtr;
void AddRef(){mRefCount++;}
void SubRef(){--mRefCount; if (mRefCount == 0) delete this;}

DWORD mRefCount;
};


// SafePtr - clears all safeptrs when object is destroyed

#pragma once
#include "comdef.h"
#include "objectarray.h"

template <class T> class TSafeObj
{
public:
class SPtr
{
public:

SPtr() : mPtr(NULL){}
SPtr(T p) : mPtr(p) {if (mPtr) mPtr->AddSPtr(this);}
SPtr(const SPtr& s) : mPtr(s.mPtr){
if (mPtr) mPtr->AddSPtr(this);
}
~SPtr(){if (mPtr) mPtr->RemoveSPtr(this);}

void Destroy(){
if (mPtr){
mPtr->RemoveSPtr(this);
delete mPtr; // mPtr clears out other sptrs
mPtr = NULL;
}
}


T operator -> (){return mPtr;}
SPtr& operator =(const SPtr& that){
if (this != &that) {
if (mPtr) mPtr->RemoveSPtr(this);
if (that.mPtr) that.mPtr->AddSPtr(this);
mPtr = that.mPtr;
}
return *this;
}

void Clear(){
if (mPtr) mPtr->RemoveSPtr(this);
mPtr = NULL;
}

bool IsValid(){return mPtr != NULL;}

private:
friend TSafeObj;
T mPtr;

};

static SPtr Create(){return SPtr(NULL);} // override with more useful
actions

protected: // keep destructor and constructor protected/private in
inherited classes
friend SPtr;

TSafeObj(DWORD ptrCount) : mSafePtrs(ptrCount){} // set up initial
pointercount

virtual ~TSafeObj(){
for (DWORD i = 0; i < mSafePtrs.Size(); ++i) mSafePtrs->mPtr =
NULL;
}
private:
typedef TObjectArray<SPtr*> SafePtrArray;

void AddSPtr(SPtr* sptr){mSafePtrs.Add(sptr);}
void RemoveSPtr(SPtr* sptr){mSafePtrs.RemoveObj(sptr);}

SafePtrArray mSafePtrs; // Sptrs to this object
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top