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:
parent
6f14c7c65b
commit
9045e16476
17
io.cpp
17
io.cpp
@ -130,23 +130,6 @@ template std::vector<char> util::slurp (FILE*);
|
|||||||
template std::vector<std::byte> 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
|
int
|
||||||
indenter::overflow (int ch) {
|
indenter::overflow (int ch) {
|
||||||
|
27
io.hpp
27
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<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
|
/// writes all data from the provided view into the file-like-object
|
||||||
///
|
///
|
||||||
/// writing will continually iterate until the entire buffer has been
|
/// writing will continually iterate until the entire buffer has been
|
||||||
|
8
io.ipp
8
io.ipp
@ -23,14 +23,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace util {
|
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>
|
template <typename T>
|
||||||
indented<T>::indented (const T &_data):
|
indented<T>::indented (const T &_data):
|
||||||
|
46
posix/fd.hpp
46
posix/fd.hpp
@ -68,33 +68,45 @@ namespace util::posix {
|
|||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
template <typename IteratorA, typename IteratorB>
|
template <typename ValueT>
|
||||||
[[gnu::warn_unused_result]]
|
[[nodiscard]]
|
||||||
std::enable_if_t<
|
ValueT*
|
||||||
sizeof (typename std::iterator_traits<IteratorA>::value_type) == 1,
|
read (util::view<ValueT*> dst)
|
||||||
IteratorA
|
|
||||||
>
|
|
||||||
read (util::view<IteratorA, IteratorB> 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>
|
template <typename ValueT>
|
||||||
[[gnu::warn_unused_result]]
|
[[nodiscard]]
|
||||||
std::enable_if_t<
|
auto
|
||||||
sizeof (typename std::iterator_traits<IteratorA>::value_type) == 1,
|
write (const util::view<ValueT*> &src)
|
||||||
IteratorA
|
|
||||||
>
|
|
||||||
write (util::view<IteratorA, IteratorB> 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);
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user