AutoDeleter: add IsSet method

Change-Id: I70eb43a288ec9c02471aa21ce5618f0fa2399bd7
Reviewed-on: https://review.haiku-os.org/c/haiku/+/3486
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
X512 2020-12-10 02:59:47 +09:00 committed by Jérôme Duval
parent 0b10216c7c
commit d1ca0fcc6a

View File

@ -18,6 +18,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <SupportDefs.h>
namespace BPrivate { namespace BPrivate {
@ -63,6 +64,11 @@ public:
SetTo(NULL); SetTo(NULL);
} }
inline bool IsSet() const
{
return fObject != NULL;
}
inline C *Get() const inline C *Get() const
{ {
return fObject; return fObject;
@ -214,8 +220,17 @@ struct MethodDeleter
// HandleDeleter // HandleDeleter
struct StatusHandleChecker
{
inline bool operator()(status_t handle)
{
return handle >= B_OK;
}
};
template<typename C, typename DestructorResult, template<typename C, typename DestructorResult,
DestructorResult (*Destructor)(C), C nullValue = -1> DestructorResult (*Destructor)(C), C nullValue = -1,
typename Checker = StatusHandleChecker>
class HandleDeleter { class HandleDeleter {
public: public:
inline HandleDeleter() inline HandleDeleter()
@ -230,13 +245,15 @@ public:
inline ~HandleDeleter() inline ~HandleDeleter()
{ {
Destructor(fHandle); if (IsSet())
Destructor(fHandle);
} }
inline void SetTo(C handle) inline void SetTo(C handle)
{ {
if (handle != fHandle) { if (handle != fHandle) {
Destructor(fHandle); if (IsSet())
Destructor(fHandle);
fHandle = handle; fHandle = handle;
} }
} }
@ -251,6 +268,12 @@ public:
SetTo(nullValue); SetTo(nullValue);
} }
inline bool IsSet() const
{
Checker isHandleSet;
return isHandleSet(fHandle);
}
inline C Get() const inline C Get() const
{ {
return fHandle; return fHandle;