From 9045e164769890e5c0d91a136fc0af4a98b3b8a4 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 31 Jan 2018 19:30:48 +1100 Subject: [PATCH] io: expose only view style read/write from io module Allow the destination file-like object to implement whatever style writers and readers they want, but don't expose those directly via the io module. --- io.cpp | 17 ----------------- io.hpp | 27 --------------------------- io.ipp | 8 -------- posix/fd.hpp | 46 +++++++++++++++++++++++++++++----------------- 4 files changed, 29 insertions(+), 69 deletions(-) diff --git a/io.cpp b/io.cpp index 79783a34..71383615 100644 --- a/io.cpp +++ b/io.cpp @@ -130,23 +130,6 @@ template std::vector util::slurp (FILE*); template std::vector util::slurp (FILE*); -/////////////////////////////////////////////////////////////////////////////// -void -util::write (const posix::fd &out, - const void *restrict data, - size_t bytes) -{ - const char *restrict cursor = reinterpret_cast (data); - size_t remaining = bytes; - - while (remaining) { - ssize_t consumed = posix::error::try_value (::write (out, cursor, remaining)); - - remaining -= util::cast::sign (consumed); - cursor += util::cast::sign (consumed); - } -} - ////////////////////////////////////////////////////////////////////////////// int indenter::overflow (int ch) { diff --git a/io.hpp b/io.hpp index 0931be5b..a2126d25 100644 --- a/io.hpp +++ b/io.hpp @@ -51,33 +51,6 @@ namespace util { /////////////////////////////////////////////////////////////////////////// - void write (const posix::fd&, const void *restrict data, size_t bytes); - - - //------------------------------------------------------------------------- - template < - typename ValueT, - typename = std::enable_if_t< - sizeof (util::view) == 1, - void - > - > - void - write (const posix::fd &dst, util::view data) - { - write (dst, std::data (data), std::size (data)); - } - - - //------------------------------------------------------------------------- - template - void write (const posix::fd &_fd, const T &data) - { - return write (_fd, make_view (data)); - } - - - ///------------------------------------------------------------------------ /// writes all data from the provided view into the file-like-object /// /// writing will continually iterate until the entire buffer has been diff --git a/io.ipp b/io.ipp index 0dc00ebb..2c3b1737 100644 --- a/io.ipp +++ b/io.ipp @@ -23,14 +23,6 @@ #endif namespace util { - //------------------------------------------------------------------------- - template - void - write (const posix::fd &_fd, const T *restrict first, const T *restrict last) - { - write (_fd, first, (last - first) * sizeof (T)); - } - //------------------------------------------------------------------------- template indented::indented (const T &_data): diff --git a/posix/fd.hpp b/posix/fd.hpp index c5ca7228..db8ac4d8 100644 --- a/posix/fd.hpp +++ b/posix/fd.hpp @@ -68,33 +68,45 @@ namespace util::posix { //--------------------------------------------------------------------- - template - [[gnu::warn_unused_result]] - std::enable_if_t< - sizeof (typename std::iterator_traits::value_type) == 1, - IteratorA - > - read (util::view dst) + template + [[nodiscard]] + ValueT* + read (util::view dst) { - return dst.begin () + read (&*std::data (dst), std::size (dst)); + return dst.begin () + read ( + &*std::data (dst), std::size (dst) * sizeof (ValueT) + ); } //--------------------------------------------------------------------- - template - [[gnu::warn_unused_result]] - std::enable_if_t< - sizeof (typename std::iterator_traits::value_type) == 1, - IteratorA - > - write (util::view src) + template + [[nodiscard]] + auto + write (const util::view &src) { - return src.begin () + write (&*std::data (src), std::size (src)); + return const_cast ( + write ( + util::view (src) + ) + ); } //--------------------------------------------------------------------- - [[gnu::warn_unused_result]] off_t lseek (off_t offset, int whence); + template + [[nodiscard]] + const ValueT* + write (const util::view &src) + { + return src.begin () + write ( + &*std::data (src), std::size (src) * sizeof (ValueT) + ); + } + + + //--------------------------------------------------------------------- + [[nodiscard]] off_t lseek (off_t offset, int whence); ///////////////////////////////////////////////////////////////////////