blob: 1c9c8870c0b61ffe543bc952fb8c1fdab754cd99 [file] [log] [blame]
#pragma once
#include <unistd.h>
namespace phosphor
{
namespace power
{
namespace util
{
/**
* @class FileDescriptor
*
* Closes the file descriptor on destruction
*/
class FileDescriptor
{
public:
FileDescriptor() = default;
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor& operator=(const FileDescriptor&) = delete;
FileDescriptor(FileDescriptor&&) = delete;
FileDescriptor& operator=(FileDescriptor&&) = delete;
/**
* Constructor
*
* @param[in] fd - File descriptor
*/
FileDescriptor(int fd) : fd(fd)
{
}
~FileDescriptor()
{
if (fd >= 0)
{
close(fd);
}
}
int operator()()
{
return fd;
}
operator bool() const
{
return fd != -1;
}
void set(int descriptor)
{
if (fd >= 0)
{
close(fd);
}
fd = descriptor;
}
private:
/**
* File descriptor
*/
int fd = -1;
};
} // namespace util
} // namespace power
} // namespace phosphor