From cb12d4fd1f685af2956499a1e7d846e9cbb0c38c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 29 Oct 2015 10:48:54 +1100 Subject: [PATCH] io: remove redundant write overloads --- io.cpp | 13 ++++--------- io.hpp | 7 +------ io.ipp | 18 +++++------------- io_posix.cpp | 3 ++- json/flat.cpp.rl | 2 +- json/tree.cpp | 2 +- netpbm.cpp | 2 +- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/io.cpp b/io.cpp index 8933b263..cdbb14c4 100644 --- a/io.cpp +++ b/io.cpp @@ -73,18 +73,13 @@ util::slurp (const boost::filesystem::path& path) { //----------------------------------------------------------------------------- -template void util::write (const fd &out, - const T *restrict first, - const T *restrict last) + const void *restrict data, + size_t bytes) { - CHECK (first); - CHECK (last); - CHECK_LE (first, last); - - const char *restrict cursor = reinterpret_cast (first); - size_t remaining = sizeof (T) * (last - first); + const char *restrict cursor = reinterpret_cast (data); + size_t remaining = bytes; while (remaining) { ssize_t consumed = ::write (out, cursor, remaining); diff --git a/io.hpp b/io.hpp index 68f7eca5..e4710a47 100644 --- a/io.hpp +++ b/io.hpp @@ -74,8 +74,7 @@ namespace util { slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&); - template - void write (const fd&, const T *restrict data, size_t count); + void write (const fd&, const void *restrict data, size_t bytes); template void write (const fd&, const T &data); @@ -83,10 +82,6 @@ namespace util { 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 c4ff35d7..2546d793 100644 --- a/io.ipp +++ b/io.ipp @@ -9,26 +9,18 @@ namespace util { //------------------------------------------------------------------------- template void - write (const fd &_fd, const T *restrict data, size_t count) + write (const fd &_fd, const T &data) { - auto first = reinterpret_cast (data); - write (_fd, first, first + sizeof (T) * count); + write (_fd, &data, sizeof (T)); } + //------------------------------------------------------------------------- template void - write (const fd &_fd, const T &data) + write (const fd &_fd, const T *restrict first, const T *restrict last) { - write (_fd, &data, 1); - } - - //------------------------------------------------------------------------- - inline void - write (const fd &_fd, const void *_data, size_t _bytes) - { - auto data = reinterpret_cast (_data); - write (_fd, data, data + _bytes); + write (_fd, first, (last - first) * sizeof (T)); } //------------------------------------------------------------------------- diff --git a/io_posix.cpp b/io_posix.cpp index a63bf8b8..ba34f757 100644 --- a/io_posix.cpp +++ b/io_posix.cpp @@ -34,7 +34,8 @@ mapped_file::mapped_file (const char *_path, int fflags, int mflags): //---------------------------------------------------------------------------- -mapped_file::~mapped_file () { +mapped_file::~mapped_file () +{ CHECK (m_data != NULL); munmap (m_data, m_size); } diff --git a/json/flat.cpp.rl b/json/flat.cpp.rl index 95190681..4e467eb4 100644 --- a/json/flat.cpp.rl +++ b/json/flat.cpp.rl @@ -169,7 +169,7 @@ json::flat::parse (const char *first, const char *last) std::vector json::flat::parse (const boost::filesystem::path &path) { - util::mapped_file f (path); + util::mapped_file f (path.string ().c_str ()); return parse ((const char *)f.cbegin (), (const char*)f.cend ()); } diff --git a/json/tree.cpp b/json/tree.cpp index 76268ea8..80c8a467 100644 --- a/json/tree.cpp +++ b/json/tree.cpp @@ -170,7 +170,7 @@ parse (std::vector::const_iterator first, std::unique_ptr json::tree::parse (const boost::filesystem::path &path) { - util::mapped_file f (path); + util::mapped_file f (path.string ().c_str ()); return parse ((const char*)f.cbegin (), (const char*)f.cend ()); } diff --git a/netpbm.cpp b/netpbm.cpp index 5a44bf4f..a80a0ce7 100644 --- a/netpbm.cpp +++ b/netpbm.cpp @@ -28,7 +28,7 @@ util::image::buffer<1,uint8_t> util::pgm::read (const boost::filesystem::path &path) { - util::mapped_file raw (path); + util::mapped_file raw (path.string ().c_str ()); std::ifstream cooked (path.string (), std::ios::binary); char magic[2];