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