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
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
/*! Scope-based automatic deletion of objects/arrays.
|
2008-04-14 02:33:44 +04:00
|
|
|
ObjectDeleter - deletes an object
|
|
|
|
ArrayDeleter - deletes an array
|
|
|
|
MemoryDeleter - free()s malloc()ed memory
|
|
|
|
CObjectDeleter - calls an arbitrary specified destructor function
|
2010-08-10 01:23:05 +04:00
|
|
|
FileDescriptorCloser - closes a file descriptor
|
2007-07-31 20:20:37 +04:00
|
|
|
*/
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
#include <stdlib.h>
|
2010-08-09 20:30:24 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-07-31 19:34:28 +04:00
|
|
|
inline C *operator->() const
|
|
|
|
{
|
|
|
|
return fObject;
|
|
|
|
}
|
|
|
|
|
2008-04-14 02:33:44 +04:00
|
|
|
protected:
|
2005-03-11 20:44:55 +03:00
|
|
|
C *fObject;
|
|
|
|
DeleteFunc fDelete;
|
2020-05-15 09:55:51 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
AutoDeleter(const AutoDeleter&);
|
|
|
|
AutoDeleter& operator=(const AutoDeleter&);
|
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) {}
|
2020-06-07 11:09:56 +03:00
|
|
|
|
|
|
|
inline C& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
return this->Get()[index];
|
|
|
|
}
|
2003-09-28 19:13:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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) {}
|
|
|
|
};
|
|
|
|
|
2008-04-14 02:33:44 +04:00
|
|
|
|
|
|
|
// CObjectDeleter
|
|
|
|
|
|
|
|
template<typename Type, typename DestructorReturnType>
|
|
|
|
struct CObjectDelete
|
|
|
|
{
|
|
|
|
inline void operator()(Type *object)
|
|
|
|
{
|
2008-07-29 05:48:38 +04:00
|
|
|
if (fDestructor != NULL && object != NULL)
|
2008-04-14 02:33:44 +04:00
|
|
|
fDestructor(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
inline void operator=(Destructor destructor)
|
|
|
|
{
|
2008-04-20 06:54:38 +04:00
|
|
|
fDestructor = destructor;
|
2008-04-14 02:33:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DestructorReturnType (*fDestructor)(Type*);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Type, typename DestructorReturnType = void>
|
|
|
|
struct CObjectDeleter
|
|
|
|
: AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> >
|
|
|
|
{
|
|
|
|
typedef AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> > Base;
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
CObjectDeleter(Destructor destructor) : Base()
|
|
|
|
{
|
|
|
|
Base::fDelete = destructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
CObjectDeleter(Type *object, Destructor destructor) : Base(object)
|
|
|
|
{
|
|
|
|
Base::fDelete = destructor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-04-20 06:54:38 +04:00
|
|
|
|
|
|
|
// MethodDeleter
|
|
|
|
|
|
|
|
template<typename Type, typename DestructorReturnType>
|
|
|
|
struct MethodDelete
|
|
|
|
{
|
|
|
|
inline void operator()(Type *object)
|
|
|
|
{
|
2008-07-29 05:48:38 +04:00
|
|
|
if (fDestructor && object != NULL)
|
2008-04-20 06:54:38 +04:00
|
|
|
(object->*fDestructor)();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
inline void operator=(Destructor destructor)
|
|
|
|
{
|
|
|
|
fDestructor = destructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DestructorReturnType (Type::*fDestructor)();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type, typename DestructorReturnType = void>
|
|
|
|
struct MethodDeleter
|
|
|
|
: AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> >
|
|
|
|
{
|
|
|
|
typedef AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> > Base;
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
MethodDeleter(Destructor destructor) : Base()
|
|
|
|
{
|
|
|
|
Base::fDelete = destructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Destructor>
|
|
|
|
MethodDeleter(Type *object, Destructor destructor) : Base(object)
|
|
|
|
{
|
|
|
|
Base::fDelete = destructor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2010-08-10 01:23:05 +04:00
|
|
|
// FileDescriptorCloser
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2010-08-10 01:23:05 +04:00
|
|
|
struct FileDescriptorCloser {
|
2011-07-01 03:36:46 +04:00
|
|
|
inline FileDescriptorCloser()
|
|
|
|
:
|
|
|
|
fDescriptor(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-10 01:23:05 +04:00
|
|
|
inline FileDescriptorCloser(int descriptor)
|
2010-08-09 20:30:24 +04:00
|
|
|
:
|
|
|
|
fDescriptor(descriptor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-10 01:23:05 +04:00
|
|
|
inline ~FileDescriptorCloser()
|
2011-07-01 03:36:46 +04:00
|
|
|
{
|
|
|
|
SetTo(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetTo(int descriptor)
|
2010-08-09 20:30:24 +04:00
|
|
|
{
|
|
|
|
if (fDescriptor >= 0)
|
|
|
|
close(fDescriptor);
|
2011-07-01 03:36:46 +04:00
|
|
|
|
|
|
|
fDescriptor = descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unset()
|
|
|
|
{
|
|
|
|
SetTo(-1);
|
2010-08-09 20:30:24 +04:00
|
|
|
}
|
|
|
|
|
2020-04-18 14:49:19 +03:00
|
|
|
inline int Get()
|
|
|
|
{
|
|
|
|
return fDescriptor;
|
|
|
|
}
|
|
|
|
|
2011-07-01 03:36:46 +04:00
|
|
|
inline int Detach()
|
2010-08-09 20:30:24 +04:00
|
|
|
{
|
2011-07-01 03:36:46 +04:00
|
|
|
int descriptor = fDescriptor;
|
2010-08-09 20:30:24 +04:00
|
|
|
fDescriptor = -1;
|
2011-07-01 03:36:46 +04:00
|
|
|
return descriptor;
|
2010-08-09 20:30:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fDescriptor;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2013-09-10 02:36:54 +04:00
|
|
|
using ::BPrivate::ObjectDeleter;
|
|
|
|
using ::BPrivate::ArrayDeleter;
|
|
|
|
using ::BPrivate::MemoryDeleter;
|
|
|
|
using ::BPrivate::CObjectDeleter;
|
|
|
|
using ::BPrivate::MethodDeleter;
|
|
|
|
using ::BPrivate::FileDescriptorCloser;
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
|
|
|
|
#endif // _AUTO_DELETER_H
|