From 75a1f1e5e71c6accf7a124a99f35b84aea4f3eb5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 2 Jul 2015 16:34:17 +1000 Subject: [PATCH] io: add more write overloads --- io.cpp | 39 ++++++++++++++++++++++++++++++--------- io.hpp | 36 +++++++++++++++++++++--------------- io.ipp | 12 ++++++++++++ 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/io.cpp b/io.cpp index bb5436a2..d6734a2c 100644 --- a/io.cpp +++ b/io.cpp @@ -88,16 +88,20 @@ util::slurp (const boost::filesystem::path& path) { return buffer; } -//---------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- template void -util::write (const boost::filesystem::path &path, const T *data, size_t len) { - CHECK_GT (len, 0); - CHECK (data); +util::write (const fd &out, + const T *restrict first, + const T *restrict last) +{ + CHECK (first); + CHECK (last); + CHECK_LE (first, last); - fd out (path, ACCESS_WRITE); - const char *cursor = reinterpret_cast (data); - size_t remaining = len * sizeof (T); + const char *restrict cursor = reinterpret_cast (first); + size_t remaining = sizeof (T) * (last - first); while (remaining) { ssize_t consumed = ::write (out, cursor, remaining); @@ -110,8 +114,25 @@ util::write (const boost::filesystem::path &path, const T *data, size_t len) { } -template void util::write (const boost::filesystem::path&, const char*, size_t); -template void util::write (const boost::filesystem::path&, const uint8_t*, size_t); +template void util::write (const fd&, const char*, const char*); +template void util::write (const fd&, const int8_t*, const int8_t*); +template void util::write (const fd&, const uint8_t*, const uint8_t*); + + +//----------------------------------------------------------------------------- +template +void +util::write (const boost::filesystem::path &path, + const T *restrict first, + const T *restrict last) +{ + write (fd (path, ACCESS_WRITE), first, last); +} + + +template void util::write (const boost::filesystem::path&, const char*, const char*); +template void util::write (const boost::filesystem::path&, const int8_t*, const int8_t*); +template void util::write (const boost::filesystem::path&, const uint8_t*, const uint8_t*); //---------------------------------------------------------------------------- diff --git a/io.hpp b/io.hpp index 938d9fca..11860944 100644 --- a/io.hpp +++ b/io.hpp @@ -40,17 +40,6 @@ namespace util { }; - /// Reads an entire file into memory. Caller frees the result. Guarantees a - /// null trailing byte. - std::vector - slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&); - - - template - void - write (const boost::filesystem::path &, const T *data, size_t len); - - ///------------------------------------------------------------------------ /// A simple RAII wrapper for file descriptors struct fd { @@ -67,12 +56,13 @@ namespace util { }; + //------------------------------------------------------------------------- #ifdef PLATFORM_WIN32 - struct handle_ref : util::nocopy { + struct handle : util::nocopy { public: - explicit handle_ref (HANDLE); - explicit handle_ref (); - ~handle_ref (); + explicit handle (HANDLE); + explicit handle (); + ~handle (); void reset (HANDLE); @@ -83,6 +73,22 @@ namespace util { #endif + /// Reads an entire file into memory. Caller frees the result. Guarantees a + /// null trailing byte. + std::vector + slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&); + + + template + void write (const fd&, const T *restrict data, size_t count); + + template + void write (const fd&, const T *restrict first, const T *restrict last); + + template + void write (const boost::filesystem::path &, const T *restrict first, const T *restrict last); + + //------------------------------------------------------------------------- class indenter : public std::streambuf { protected: diff --git a/io.ipp b/io.ipp index e13f5d3c..84812d50 100644 --- a/io.ipp +++ b/io.ipp @@ -6,12 +6,23 @@ #endif namespace util { + //------------------------------------------------------------------------- + template + void + write (const fd &_fd, const T *restrict data, size_t count) + { + auto first = reinterpret_cast (data); + write (_fd, first, first + sizeof (T) * count); + } + + //------------------------------------------------------------------------- template indented::indented (const T &_data): data (_data) { ; } + //------------------------------------------------------------------------- template indented make_indented (const T &_data) { @@ -19,6 +30,7 @@ namespace util { } + //------------------------------------------------------------------------- template std::ostream& operator<< (std::ostream &os, const util::indented &&v) {