2007-07-31 20:20:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2001-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2003-09-28 19:13:12 +04:00
|
|
|
#ifndef _AUTO_DELETER_H
|
|
|
|
#define _AUTO_DELETER_H
|
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
/*! Scope-based automatic deletion of objects/arrays.
|
|
|
|
ObjectDeleter - deletes an object
|
|
|
|
ArrayDeleter - deletes an array
|
|
|
|
MemoryDeleter - free()s malloc()ed memory
|
|
|
|
*/
|
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
// AutoDeleter
|
|
|
|
|
2005-03-11 20:44:55 +03:00
|
|
|
template<typename C, typename DeleteFunc>
|
2003-09-28 19:13:12 +04:00
|
|
|
class AutoDeleter {
|
|
|
|
public:
|
|
|
|
inline AutoDeleter()
|
|
|
|
: fObject(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline AutoDeleter(C *object)
|
|
|
|
: fObject(object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ~AutoDeleter()
|
|
|
|
{
|
|
|
|
fDelete(fObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetTo(C *object)
|
|
|
|
{
|
2005-03-11 20:44:55 +03:00
|
|
|
if (object != fObject) {
|
|
|
|
fDelete(fObject);
|
|
|
|
fObject = object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unset()
|
|
|
|
{
|
|
|
|
SetTo(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Delete()
|
|
|
|
{
|
|
|
|
SetTo(NULL);
|
2003-09-28 19:13:12 +04:00
|
|
|
}
|
|
|
|
|
2007-07-07 05:41:55 +04:00
|
|
|
inline C *Get() const
|
|
|
|
{
|
|
|
|
return fObject;
|
|
|
|
}
|
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
inline C *Detach()
|
|
|
|
{
|
|
|
|
C *object = fObject;
|
|
|
|
fObject = NULL;
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2005-03-11 20:44:55 +03:00
|
|
|
C *fObject;
|
|
|
|
DeleteFunc fDelete;
|
2003-09-28 19:13:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ObjectDeleter
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
struct ObjectDelete
|
|
|
|
{
|
|
|
|
inline void operator()(C *object)
|
|
|
|
{
|
|
|
|
delete object;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
|
|
|
|
{
|
|
|
|
ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
|
|
|
|
ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ArrayDeleter
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
struct ArrayDelete
|
|
|
|
{
|
|
|
|
inline void operator()(C *array)
|
|
|
|
{
|
|
|
|
delete[] array;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
|
|
|
|
{
|
|
|
|
ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
|
|
|
|
ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// MemoryDeleter
|
|
|
|
|
|
|
|
struct MemoryDelete
|
|
|
|
{
|
|
|
|
inline void operator()(void *memory)
|
|
|
|
{
|
|
|
|
free(memory);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
|
|
|
|
{
|
|
|
|
MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
|
|
|
|
MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::ObjectDeleter;
|
|
|
|
using BPrivate::ArrayDeleter;
|
|
|
|
using BPrivate::MemoryDeleter;
|
|
|
|
|
|
|
|
#endif // _AUTO_DELETER_H
|