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.
This commit is contained in:
Danny Robson 2018-01-31 19:30:48 +11:00
parent 6f14c7c65b
commit 9045e16476
4 changed files with 29 additions and 69 deletions

17
io.cpp
View File

@ -130,23 +130,6 @@ template std::vector<char> util::slurp (FILE*);
template std::vector<std::byte> util::slurp (FILE*);
///////////////////////////////////////////////////////////////////////////////
void
util::write (const posix::fd &out,
const void *restrict data,
size_t bytes)
{
const char *restrict cursor = reinterpret_cast<const char*> (data);
size_t remaining = bytes;
while (remaining) {
ssize_t consumed = posix::error::try_value (::write (out, cursor, remaining));
remaining -= util::cast::sign<size_t> (consumed);
cursor += util::cast::sign<size_t> (consumed);
}
}
//////////////////////////////////////////////////////////////////////////////
int
indenter::overflow (int ch) {

27
io.hpp
View File

@ -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<const ValueT*>) == 1,
void
>
>
void
write (const posix::fd &dst, util::view<const ValueT*> data)
{
write (dst, std::data (data), std::size (data));
}
//-------------------------------------------------------------------------
template <typename T>
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

8
io.ipp
View File

@ -23,14 +23,6 @@
#endif
namespace util {
//-------------------------------------------------------------------------
template <typename T>
void
write (const posix::fd &_fd, const T *restrict first, const T *restrict last)
{
write (_fd, first, (last - first) * sizeof (T));
}
//-------------------------------------------------------------------------
template <typename T>
indented<T>::indented (const T &_data):

View File

@ -68,33 +68,45 @@ namespace util::posix {
//---------------------------------------------------------------------
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)
template <typename ValueT>
[[nodiscard]]
ValueT*
read (util::view<ValueT*> dst)
{
return dst.begin () + read (&*std::data (dst), std::size (dst));
return dst.begin () + read (
&*std::data (dst), std::size (dst) * sizeof (ValueT)
);
}
//---------------------------------------------------------------------
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)
template <typename ValueT>
[[nodiscard]]
auto
write (const util::view<ValueT*> &src)
{
return src.begin () + write (&*std::data (src), std::size (src));
return const_cast<ValueT*> (
write (
util::view<const ValueT*> (src)
)
);
}
//---------------------------------------------------------------------
[[gnu::warn_unused_result]] off_t lseek (off_t offset, int whence);
template <typename ValueT>
[[nodiscard]]
const ValueT*
write (const util::view<const ValueT*> &src)
{
return src.begin () + write (
&*std::data (src), std::size (src) * sizeof (ValueT)
);
}
//---------------------------------------------------------------------
[[nodiscard]] off_t lseek (off_t offset, int whence);
///////////////////////////////////////////////////////////////////////