Improve FileDescriptorCloser

* Add SetTo()/Unset() methods and no-argument constructor.
* Detach() returns the FD now.
This commit is contained in:
Ingo Weinhold 2011-07-01 01:36:46 +02:00
parent efb6462777
commit d046296e85

View File

@ -224,6 +224,12 @@ struct MethodDeleter
// FileDescriptorCloser // FileDescriptorCloser
struct FileDescriptorCloser { struct FileDescriptorCloser {
inline FileDescriptorCloser()
:
fDescriptor(-1)
{
}
inline FileDescriptorCloser(int descriptor) inline FileDescriptorCloser(int descriptor)
: :
fDescriptor(descriptor) fDescriptor(descriptor)
@ -232,13 +238,27 @@ struct FileDescriptorCloser {
inline ~FileDescriptorCloser() inline ~FileDescriptorCloser()
{ {
if (fDescriptor >= 0) SetTo(-1);
close(fDescriptor);
} }
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; fDescriptor = -1;
return descriptor;
} }
private: private: