diff --git a/headers/private/shared/AutoDeleter.h b/headers/private/shared/AutoDeleter.h index cbf81b1de1..2d24368f2b 100644 --- a/headers/private/shared/AutoDeleter.h +++ b/headers/private/shared/AutoDeleter.h @@ -18,6 +18,7 @@ #include #include +#include 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 + 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;