fd: add reset and release methods

This commit is contained in:
Danny Robson 2017-12-26 17:25:58 +11:00
parent ac685cfdd2
commit fe979fc2a9
2 changed files with 40 additions and 3 deletions

View File

@ -94,13 +94,43 @@ fd::stat (void) const
ssize_t
fd::read (void *buffer, size_t count)
{
return error::try_value (
::read (m_fd, buffer, count)
);
error::try_value (::close (m_fd));
m_fd = -1;
}
//-----------------------------------------------------------------------------
void
fd::reset (void)
{
if (m_fd >= 0) {
close ();
m_fd = -1;
}
}
//-----------------------------------------------------------------------------
void
fd::reset (int rhs)
{
if (m_fd >= 0)
close ();
m_fd = rhs;
}
//-----------------------------------------------------------------------------
int
fd::release (void)
{
int tmp = m_fd;
m_fd = -1;
return tmp;
}
///////////////////////////////////////////////////////////////////////////////
ssize_t
fd::read (util::view<char *> dst)
{

View File

@ -53,6 +53,13 @@ namespace util::posix {
///////////////////////////////////////////////////////////////////////
struct ::stat stat (void) const;
///////////////////////////////////////////////////////////////////////
void close (void);
void reset (int);
void reset (void);
int release (void);
//---------------------------------------------------------------------
[[gnu::warn_unused_result]] ssize_t read (void *buf, size_t count);
[[gnu::warn_unused_result]] ssize_t read (util::view<char*>);