fd: add read/write methods for views

This commit is contained in:
Danny Robson 2017-12-26 17:26:53 +11:00
parent fe979fc2a9
commit 39894b5619
2 changed files with 57 additions and 13 deletions

View File

@ -45,6 +45,25 @@ fd::fd (fd &&rhs):
}
//-----------------------------------------------------------------------------
fd&
fd::operator= (fd &&rhs)
{
close ();
std::swap (m_fd, rhs.m_fd);
return *this;
}
//-----------------------------------------------------------------------------
fd&
fd::operator= (int rhs)
{
reset (rhs);
return *this;
}
///////////////////////////////////////////////////////////////////////////////
fd::fd (int _fd):
m_fd (_fd)
@ -74,8 +93,7 @@ fd::~fd ()
{
if (m_fd < 0)
return;
error::try_code (close (m_fd));
close ();
}
@ -84,15 +102,14 @@ struct ::stat
fd::stat (void) const
{
struct stat buf;
if (fstat (m_fd, &buf))
error::throw_code ();
error::try_value (fstat (m_fd, &buf));
return buf;
}
///////////////////////////////////////////////////////////////////////////////
ssize_t
fd::read (void *buffer, size_t count)
void
fd::close (void)
{
error::try_value (::close (m_fd));
m_fd = -1;
@ -132,9 +149,11 @@ fd::release (void)
///////////////////////////////////////////////////////////////////////////////
ssize_t
fd::read (util::view<char *> dst)
fd::read (void *buffer, size_t count)
{
return read (std::data (dst), std::size (dst));
return error::try_value (
::read (m_fd, buffer, count)
);
}

View File

@ -34,6 +34,8 @@ namespace util::posix {
fd (const std::experimental::filesystem::path &path, int flags, mode_t);
fd (fd &&);
fd& operator= (fd &&);
fd& operator= (int);
// The int constructor steals the fd. So don't pass in something that
// you don't want closed at destruct time. This should really only be
@ -62,16 +64,39 @@ namespace util::posix {
//---------------------------------------------------------------------
[[gnu::warn_unused_result]] ssize_t read (void *buf, size_t count);
[[gnu::warn_unused_result]] ssize_t read (util::view<char*>);
[[gnu::warn_unused_result]] ssize_t read (util::view<std::byte*>);
[[gnu::warn_unused_result]] ssize_t write (const void *buf, size_t count);
[[gnu::warn_unused_result]] ssize_t write (util::view<const char*>);
[[gnu::warn_unused_result]] ssize_t write (util::view<const std::byte*>);
//---------------------------------------------------------------------
template <typename IteratorA, typename IteratorB>
[[gnu::warn_unused_result]]
std::enable_if_t<
sizeof (typename std::iterator_traits<IteratorA>::value_type) == 1,
IteratorA
>
read (util::view<IteratorA, IteratorB> dst)
{
return dst.begin () + read (&*std::data (dst), std::size (dst));
}
//---------------------------------------------------------------------
template <typename IteratorA, typename IteratorB>
[[gnu::warn_unused_result]]
std::enable_if_t<
sizeof (typename std::iterator_traits<IteratorA>::value_type) == 1,
IteratorA
>
write (util::view<IteratorA, IteratorB> src)
{
return src.begin () + write (&*std::data (src), std::size (src));
}
//---------------------------------------------------------------------
[[gnu::warn_unused_result]] off_t lseek (off_t offset, int whence);
///////////////////////////////////////////////////////////////////////
operator int (void) const;
int native (void) { return m_fd; }