io: templatise write on output data type

This commit is contained in:
Danny Robson 2014-12-30 18:37:36 +11:00
parent debdd77610
commit 46686dbc64
2 changed files with 10 additions and 4 deletions

11
io.cpp
View File

@ -92,14 +92,15 @@ util::slurp (const boost::filesystem::path& path) {
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
template <typename T>
void void
util::write (const boost::filesystem::path &path, const char *data, size_t len) { util::write (const boost::filesystem::path &path, const T *data, size_t len) {
CHECK_SOFT (len > 0); CHECK_SOFT (len > 0);
CHECK_HARD (data); CHECK_HARD (data);
fd_ref fd (path, ACCESS_WRITE); fd_ref fd (path, ACCESS_WRITE);
const char *cursor = data; const char *cursor = reinterpret_cast<const char*> (data);
size_t remaining = len; size_t remaining = len * sizeof (T);
while (remaining) { while (remaining) {
ssize_t consumed = ::write (fd, cursor, remaining); ssize_t consumed = ::write (fd, cursor, remaining);
@ -111,6 +112,10 @@ util::write (const boost::filesystem::path &path, const char *data, size_t len)
} }
} }
template void write<char> (const boost::filesystem::path&, const char*, size_t);
template void write<uint8_t> (const boost::filesystem::path&, const uint8_t*, size_t);
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
fd_ref::fd_ref (int _fd): fd_ref::fd_ref (int _fd):
fd (_fd) fd (_fd)

3
io.hpp
View File

@ -47,8 +47,9 @@ namespace util {
std::unique_ptr<char []> std::unique_ptr<char []>
slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&); slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&);
template <typename T>
void void
write (const boost::filesystem::path &, const char *data, size_t len); write (const boost::filesystem::path &, const T *data, size_t len);
///------------------------------------------------------------------------ ///------------------------------------------------------------------------