From 39894b5619a963a0d046c934a001ef8e5a05d035 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 26 Dec 2017 17:26:53 +1100 Subject: [PATCH] fd: add read/write methods for views --- posix/fd.cpp | 35 +++++++++++++++++++++++++++-------- posix/fd.hpp | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/posix/fd.cpp b/posix/fd.cpp index f6415801..e2e79d03 100644 --- a/posix/fd.cpp +++ b/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): 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 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) + ); } diff --git a/posix/fd.hpp b/posix/fd.hpp index e74abb43..c5ca7228 100644 --- a/posix/fd.hpp +++ b/posix/fd.hpp @@ -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); - [[gnu::warn_unused_result]] ssize_t read (util::view); - [[gnu::warn_unused_result]] ssize_t write (const void *buf, size_t count); - [[gnu::warn_unused_result]] ssize_t write (util::view); - [[gnu::warn_unused_result]] ssize_t write (util::view); + + + //--------------------------------------------------------------------- + template + [[gnu::warn_unused_result]] + std::enable_if_t< + sizeof (typename std::iterator_traits::value_type) == 1, + IteratorA + > + read (util::view dst) + { + return dst.begin () + read (&*std::data (dst), std::size (dst)); + } + + + //--------------------------------------------------------------------- + template + [[gnu::warn_unused_result]] + std::enable_if_t< + sizeof (typename std::iterator_traits::value_type) == 1, + IteratorA + > + write (util::view 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; }