From d046296e854527acbf177aad4478c64e5864fb1b Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 1 Jul 2011 01:36:46 +0200 Subject: [PATCH] Improve FileDescriptorCloser * Add SetTo()/Unset() methods and no-argument constructor. * Detach() returns the FD now. --- headers/private/shared/AutoDeleter.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/headers/private/shared/AutoDeleter.h b/headers/private/shared/AutoDeleter.h index 339ba43a62..aa2f041431 100644 --- a/headers/private/shared/AutoDeleter.h +++ b/headers/private/shared/AutoDeleter.h @@ -224,6 +224,12 @@ struct MethodDeleter // FileDescriptorCloser struct FileDescriptorCloser { + inline FileDescriptorCloser() + : + fDescriptor(-1) + { + } + inline FileDescriptorCloser(int descriptor) : fDescriptor(descriptor) @@ -232,13 +238,27 @@ struct FileDescriptorCloser { inline ~FileDescriptorCloser() { - if (fDescriptor >= 0) - close(fDescriptor); + SetTo(-1); } - inline void Detach() + inline void SetTo(int descriptor) { + if (fDescriptor >= 0) + close(fDescriptor); + + fDescriptor = descriptor; + } + + inline void Unset() + { + SetTo(-1); + } + + inline int Detach() + { + int descriptor = fDescriptor; fDescriptor = -1; + return descriptor; } private: