From d1ca0fcc6aa350039646db748e30f23b8d84d116 Mon Sep 17 00:00:00 2001 From: X512 Date: Thu, 10 Dec 2020 02:59:47 +0900 Subject: [PATCH] AutoDeleter: add IsSet method Change-Id: I70eb43a288ec9c02471aa21ce5618f0fa2399bd7 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3486 Reviewed-by: Adrien Destugues --- headers/private/shared/AutoDeleter.h | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) 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;