...
 
Commits (3)
......@@ -67,10 +67,10 @@ public:
{ d = new AutoLockData(mutex, state); }
AutoLock(const AutoLock& o)
{ d = o.getData(); }
{ d = o.d->get(); }
~AutoLock()
{ putData(); }
{ d->put(); }
void lock()
{ d->lock(); }
......@@ -92,8 +92,8 @@ public:
AutoLock& operator =(const AutoLock& o)
{
AutoLockData *od = o.getData(); // protects against "a = a"
putData();
AutoLockData *od = o.d->get(); // protects against "a = a"
d->put();
d = od;
return *this;
}
......@@ -125,9 +125,10 @@ private:
return this;
}
bool put()
void put()
{
return c.put();
if (c.put())
delete this;
}
void lock()
......@@ -169,16 +170,6 @@ private:
bool ul_at_end;
};
AutoLockData *getData() const
{ return d->get(); }
void putData()
{
if (d->put())
delete d;
d = NULL;
}
AutoLockData *d;
};
......@@ -211,38 +202,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,22 +264,30 @@ public:
}
void free()
{ d->free(); }
{
if (d)
d->free();
setPtr(NULL);
}
T *forget()
{
T *ptr = d->forget();
putData();
d = new AutoPtrData();
{
T *ptr = d ? d->forget() : NULL;
setPtr(NULL);
return ptr;
}
int count() const
{
return d ? d->count() : 0;
}
private:
class AutoPtrData
{
public:
AutoPtrData(T *ptr = NULL) : p(ptr)
AutoPtrData(T *ptr) : p(ptr), f(false)
{}
~AutoPtrData()
......@@ -289,14 +301,15 @@ private:
return this;
}
bool put()
void put()
{
return c.put();
if (c.put())
delete this;
}
void free()
{
if (!p)
if (!p || f)
return;
if (array)
delete [] p;
......@@ -307,31 +320,35 @@ private:
T *forget()
{
T *ptr = getPtr();
if (c.count() == 1)
p = NULL;
return ptr;
f = true;
return p;
}
T *getPtr() const
{ return p; }
{
return p;
}
void setPtr(T *ptr)
{ p = ptr; }
int count() const
{
return c.count();
}
private:
T *p;
AutoCounter c;
bool f;
};
AutoPtrData *getData() const
{ return d->get(); }
{
return d ? d->get() : NULL;
}
void putData()
{
if (d->put())
delete d;
d = NULL;
if (d)
d->put();
}
AutoPtrData *d;
......