Commit 375fe3a0 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron

AutoObj: optimise AutoPtr when empty; forget() now applies to all instances

parent a85cdd7f
......@@ -211,38 +211,51 @@ template <class T, bool array=false>
class AutoPtr
{
public:
AutoPtr()
{ d = new AutoPtrData(); }
AutoPtr(T *ptr)
{ d = new AutoPtrData(ptr); }
AutoPtr(T *ptr = NULL)
{
d = ptr ? new AutoPtrData(ptr) : NULL;
}
AutoPtr(const AutoPtr& o)
{ d = o.getData(); }
{
d = o.getData();
}
~AutoPtr()
{ putData(); }
{
putData();
}
T *getPtr() const
{ return d->getPtr(); }
{
return d ? d->getPtr() : NULL;
}
void setPtr(T *ptr)
{
putData();
d = new AutoPtrData(ptr);
putData();
d = ptr ? new AutoPtrData(ptr) : NULL;
}
operator T*() const
{ return getPtr(); }
{
return getPtr();
}
T *operator ->() const
{ return getPtr(); }
{
return getPtr();
}
T& operator[](int i)
{ return getPtr()[i]; }
{
return getPtr()[i];
}
const T& operator[](int i) const
{ return getPtr()[i]; }
{
return getPtr()[i];
}
AutoPtr& operator =(T *ptr)
......@@ -260,13 +273,18 @@ public:
}
void free()
{ d->free(); }
{
if (d)
d->free();
setPtr(NULL);
}
T *forget()
{
T *ptr = d->forget();
putData();
d = new AutoPtrData();
{
T *ptr = getPtr();
if (d)
d->forget();
setPtr(NULL);
return ptr;
}
......@@ -275,7 +293,7 @@ private:
class AutoPtrData
{
public:
AutoPtrData(T *ptr = NULL) : p(ptr)
AutoPtrData(T *ptr) : p(ptr), f(false)
{}
~AutoPtrData()
......@@ -296,7 +314,7 @@ private:
void free()
{
if (!p)
if (!p || f)
return;
if (array)
delete [] p;
......@@ -307,31 +325,30 @@ private:
T *forget()
{
T *ptr = getPtr();
if (c.count() == 1)
p = NULL;
return ptr;
f = true;
return p;
}
T *getPtr() const
{ return p; }
void setPtr(T *ptr)
{ p = ptr; }
{
return p;
}
private:
T *p;
AutoCounter c;
bool f;
};
AutoPtrData *getData() const
{ return d->get(); }
{
return d ? d->get() : NULL;
}
void putData()
{
if (d->put())
if (d && d->put())
delete d;
d = NULL;
}
AutoPtrData *d;
......
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