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
|
2020-07-02 09:16:53 +03:00
|
|
|
HandleDeleter - use arbitrary handle type and destructor function
|
|
|
|
FileDescriptorCloser - closes a file descriptor, based on HandleDeleter
|
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>
|
2020-12-09 20:59:47 +03:00
|
|
|
#include <SupportDefs.h>
|
2010-08-09 20:30:24 +04:00
|
|
|
|
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()
|
|
|
|
{
|
2020-12-04 17:17:04 +03:00
|
|
|
DeleteFunc destructor;
|
|
|
|
destructor(fObject);
|
2003-09-28 19:13:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetTo(C *object)
|
|
|
|
{
|
2005-03-11 20:44:55 +03:00
|
|
|
if (object != fObject) {
|
2020-12-04 17:17:04 +03:00
|
|
|
DeleteFunc destructor;
|
|
|
|
destructor(fObject);
|
2005-03-11 20:44:55 +03:00
|
|
|
fObject = object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unset()
|
|
|
|
{
|
|
|
|
SetTo(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Delete()
|
|
|
|
{
|
|
|
|
SetTo(NULL);
|
2003-09-28 19:13:12 +04:00
|
|
|
}
|
|
|
|
|
2020-12-09 20:59:47 +03:00
|
|
|
inline bool IsSet() const
|
|
|
|
{
|
|
|
|
return fObject != NULL;
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
|
2020-12-04 16:17:42 +03:00
|
|
|
template<typename Type, typename DestructorReturnType,
|
|
|
|
DestructorReturnType (*Destructor)(Type*)>
|
2008-04-14 02:33:44 +04:00
|
|
|
struct CObjectDelete
|
|
|
|
{
|
|
|
|
inline void operator()(Type *object)
|
|
|
|
{
|
2020-12-04 16:17:42 +03:00
|
|
|
if (object != NULL)
|
|
|
|
Destructor(object);
|
2008-04-14 02:33:44 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-04 16:17:42 +03:00
|
|
|
template<typename Type, typename DestructorReturnType,
|
|
|
|
DestructorReturnType (*Destructor)(Type*)>
|
2008-04-14 02:33:44 +04:00
|
|
|
struct CObjectDeleter
|
2020-12-04 16:17:42 +03:00
|
|
|
: AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType, Destructor> >
|
2008-04-14 02:33:44 +04:00
|
|
|
{
|
2020-12-04 16:17:42 +03:00
|
|
|
typedef AutoDeleter<Type,
|
|
|
|
CObjectDelete<Type, DestructorReturnType, Destructor> > Base;
|
2008-04-14 02:33:44 +04:00
|
|
|
|
2020-12-04 16:17:42 +03:00
|
|
|
CObjectDeleter() : Base()
|
2008-04-14 02:33:44 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:17:42 +03:00
|
|
|
CObjectDeleter(Type *object) : Base(object)
|
2008-04-14 02:33:44 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-04-20 06:54:38 +04:00
|
|
|
|
|
|
|
// MethodDeleter
|
|
|
|
|
2020-12-04 16:53:08 +03:00
|
|
|
template<typename Type, typename DestructorReturnType,
|
|
|
|
DestructorReturnType (Type::*Destructor)()>
|
2008-04-20 06:54:38 +04:00
|
|
|
struct MethodDelete
|
|
|
|
{
|
|
|
|
inline void operator()(Type *object)
|
|
|
|
{
|
2020-12-04 16:53:08 +03:00
|
|
|
if (object != NULL)
|
|
|
|
(object->*Destructor)();
|
2008-04-20 06:54:38 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-12-04 16:53:08 +03:00
|
|
|
template<typename Type, typename DestructorReturnType,
|
|
|
|
DestructorReturnType (Type::*Destructor)()>
|
2008-04-20 06:54:38 +04:00
|
|
|
struct MethodDeleter
|
2020-12-04 16:53:08 +03:00
|
|
|
: AutoDeleter<Type, MethodDelete<Type, DestructorReturnType, Destructor> >
|
2008-04-20 06:54:38 +04:00
|
|
|
{
|
2020-12-04 16:53:08 +03:00
|
|
|
typedef AutoDeleter<Type,
|
|
|
|
MethodDelete<Type, DestructorReturnType, Destructor> > Base;
|
2008-04-20 06:54:38 +04:00
|
|
|
|
2020-12-04 16:53:08 +03:00
|
|
|
MethodDeleter() : Base()
|
2008-04-20 06:54:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:53:08 +03:00
|
|
|
MethodDeleter(Type *object) : Base(object)
|
2008-04-20 06:54:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
// HandleDeleter
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2020-12-09 20:59:47 +03:00
|
|
|
struct StatusHandleChecker
|
|
|
|
{
|
|
|
|
inline bool operator()(status_t handle)
|
|
|
|
{
|
|
|
|
return handle >= B_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
template<typename C, typename DestructorResult,
|
2020-12-09 20:59:47 +03:00
|
|
|
DestructorResult (*Destructor)(C), C nullValue = -1,
|
|
|
|
typename Checker = StatusHandleChecker>
|
2020-07-02 09:16:53 +03:00
|
|
|
class HandleDeleter {
|
|
|
|
public:
|
|
|
|
inline HandleDeleter()
|
|
|
|
: fHandle(nullValue)
|
2011-07-01 03:36:46 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
inline HandleDeleter(C handle)
|
|
|
|
: fHandle(handle)
|
2010-08-09 20:30:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
inline ~HandleDeleter()
|
2011-07-01 03:36:46 +04:00
|
|
|
{
|
2020-12-09 20:59:47 +03:00
|
|
|
if (IsSet())
|
|
|
|
Destructor(fHandle);
|
2011-07-01 03:36:46 +04:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
inline void SetTo(C handle)
|
2010-08-09 20:30:24 +04:00
|
|
|
{
|
2020-07-02 09:16:53 +03:00
|
|
|
if (handle != fHandle) {
|
2020-12-09 20:59:47 +03:00
|
|
|
if (IsSet())
|
|
|
|
Destructor(fHandle);
|
2020-07-02 09:16:53 +03:00
|
|
|
fHandle = handle;
|
|
|
|
}
|
2011-07-01 03:36:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unset()
|
|
|
|
{
|
2020-07-02 09:16:53 +03:00
|
|
|
SetTo(nullValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Delete()
|
|
|
|
{
|
|
|
|
SetTo(nullValue);
|
2010-08-09 20:30:24 +04:00
|
|
|
}
|
|
|
|
|
2020-12-09 20:59:47 +03:00
|
|
|
inline bool IsSet() const
|
|
|
|
{
|
|
|
|
Checker isHandleSet;
|
|
|
|
return isHandleSet(fHandle);
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
inline C Get() const
|
2020-04-18 14:49:19 +03:00
|
|
|
{
|
2020-07-02 09:16:53 +03:00
|
|
|
return fHandle;
|
2020-04-18 14:49:19 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
inline C Detach()
|
2010-08-09 20:30:24 +04:00
|
|
|
{
|
2020-07-02 09:16:53 +03:00
|
|
|
C handle = fHandle;
|
|
|
|
fHandle = nullValue;
|
|
|
|
return handle;
|
2010-08-09 20:30:24 +04:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
protected:
|
|
|
|
C fHandle;
|
|
|
|
|
2010-08-09 20:30:24 +04:00
|
|
|
private:
|
2020-07-02 09:16:53 +03:00
|
|
|
HandleDeleter(const HandleDeleter&);
|
|
|
|
HandleDeleter& operator=(const HandleDeleter&);
|
2010-08-09 20:30:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-02 09:16:53 +03:00
|
|
|
// FileDescriptorCloser
|
|
|
|
|
|
|
|
typedef HandleDeleter<int, int, close, -1> FileDescriptorCloser;
|
|
|
|
|
|
|
|
|
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;
|
2020-07-02 09:16:53 +03:00
|
|
|
using ::BPrivate::HandleDeleter;
|
2013-09-10 02:36:54 +04:00
|
|
|
using ::BPrivate::FileDescriptorCloser;
|
2010-08-09 20:30:24 +04:00
|
|
|
|
2003-09-28 19:13:12 +04:00
|
|
|
|
|
|
|
#endif // _AUTO_DELETER_H
|