#include <OTC/memory/reaper.hh> template<class T> class OTC_Reaper {
public:
inline OTC_Reaper();
inline ~OTC_Reaper();
inline void grab(T* theData);
inline void release();
};
OTC_Reaper
assists in ensuring that single objects
allocated from the free store are deleted when the stack is
unwound as a result of an exception. This is achieved by using
an instance of this class as a handle to an object allocated using
new. Once an object is grabbed using an instance of this class,
the section of code in which an exception could be raised is
executed. If an exception does occur then the instance of this
class will be destroyed and the object deleted. If no exception
occurs a call can be made to release the object, when the destructor
is finally called the object would not be deleted.
void function()
{
OTC_Reaper<Object> xxxObject;
Object* theObject = new Object;
OTCLIB_ASSERT(theObject != 0);
xxxObject.grab(theObject);
... code which could throw an exception
xxxObject.release();
}
inline OTC_Reaper();
0
.
inline ~OTC_Reaper();
0
, the object is deleted.
inline void grab(T* theData);
theData
.
If the handle was set to point at another
object, it will now point to the new
object and the first object will not be
changed.
inline void release();
0
. This is equivalent
to calling grab(0)
.
OTC_VecReaper
, OTC_MallocReaper