fd: add read/write methods for views
This commit is contained in:
parent
fe979fc2a9
commit
39894b5619
35
posix/fd.cpp
35
posix/fd.cpp
@ -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):
|
fd::fd (int _fd):
|
||||||
m_fd (_fd)
|
m_fd (_fd)
|
||||||
@ -74,8 +93,7 @@ fd::~fd ()
|
|||||||
{
|
{
|
||||||
if (m_fd < 0)
|
if (m_fd < 0)
|
||||||
return;
|
return;
|
||||||
|
close ();
|
||||||
error::try_code (close (m_fd));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -84,15 +102,14 @@ struct ::stat
|
|||||||
fd::stat (void) const
|
fd::stat (void) const
|
||||||
{
|
{
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
if (fstat (m_fd, &buf))
|
error::try_value (fstat (m_fd, &buf));
|
||||||
error::throw_code ();
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
ssize_t
|
void
|
||||||
fd::read (void *buffer, size_t count)
|
fd::close (void)
|
||||||
{
|
{
|
||||||
error::try_value (::close (m_fd));
|
error::try_value (::close (m_fd));
|
||||||
m_fd = -1;
|
m_fd = -1;
|
||||||
@ -132,9 +149,11 @@ fd::release (void)
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
ssize_t
|
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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
35
posix/fd.hpp
35
posix/fd.hpp
@ -34,6 +34,8 @@ namespace util::posix {
|
|||||||
fd (const std::experimental::filesystem::path &path, int flags, mode_t);
|
fd (const std::experimental::filesystem::path &path, int flags, mode_t);
|
||||||
|
|
||||||
fd (fd &&);
|
fd (fd &&);
|
||||||
|
fd& operator= (fd &&);
|
||||||
|
fd& operator= (int);
|
||||||
|
|
||||||
// The int constructor steals the fd. So don't pass in something that
|
// 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
|
// 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 (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 (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);
|
[[gnu::warn_unused_result]] off_t lseek (off_t offset, int whence);
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
operator int (void) const;
|
operator int (void) const;
|
||||||
int native (void) { return m_fd; }
|
int native (void) { return m_fd; }
|
||||||
|
Loading…
Reference in New Issue
Block a user