From 33b3b48c74c3bf7eb893ca8c8d0365fc4474ae6a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 11 Jun 2015 19:27:42 +1000 Subject: [PATCH] io: rename fd_ref to fd --- io.cpp | 37 ++++++++++++++++--------------------- io.hpp | 16 +++++++++------- io_posix.hpp | 2 +- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/io.cpp b/io.cpp index e7029eba..4023e88c 100644 --- a/io.cpp +++ b/io.cpp @@ -55,14 +55,14 @@ access_to_cflags (access_t a) { //---------------------------------------------------------------------------- std::unique_ptr util::slurp (const boost::filesystem::path& path) { - fd_ref fd(path, ACCESS_READ); + fd out (path, ACCESS_READ); // Calculate the total file size - off_t size = lseek (fd, 0, SEEK_END); + off_t size = lseek (out, 0, SEEK_END); if (size == (off_t)-1) throw errno_error(); - if (lseek (fd, 0, SEEK_SET) == (off_t)-1) + if (lseek (out, 0, SEEK_SET) == (off_t)-1) throw errno_error (); // Allocate a buffer, and keep reading until it's full. We provide a null @@ -75,7 +75,7 @@ util::slurp (const boost::filesystem::path& path) { char *cursor = buffer.get(); while (remaining) { - ssize_t consumed = ::read (fd, cursor, remaining); + ssize_t consumed = ::read (out, cursor, remaining); if (consumed == -1) throw errno_error(); CHECK_GT ( consumed, 0); @@ -95,12 +95,12 @@ util::write (const boost::filesystem::path &path, const T *data, size_t len) { CHECK_GT (len, 0); CHECK (data); - fd_ref fd (path, ACCESS_WRITE); + fd out (path, ACCESS_WRITE); const char *cursor = reinterpret_cast (data); size_t remaining = len * sizeof (T); while (remaining) { - ssize_t consumed = ::write (fd, cursor, remaining); + ssize_t consumed = ::write (out, cursor, remaining); if (consumed < 0) errno_error::throw_code (); @@ -115,39 +115,34 @@ template void util::write (const boost::filesystem::path&, const uint8_ //---------------------------------------------------------------------------- -fd_ref::fd_ref (int _fd): - fd (_fd) +fd::fd (int _fd): + m_fd (_fd) { - if (fd < 0) + if (_fd < 0) throw std::invalid_argument ("invalid descriptor"); } -fd_ref::fd_ref (const boost::filesystem::path &path, access_t access): +fd::fd (const boost::filesystem::path &path, access_t access): #ifdef PLATFORM_WIN32 // Windows requires the use of 'string ()' to convert to char datatype // rather than the typical wchar - fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY, 0660)) + m_fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY, 0660)) #else - fd (open (path.native ().c_str (), access_to_cflags (access), 0660)) + m_fd (open (path.native ().c_str (), access_to_cflags (access), 0660)) #endif { - if (fd < 0) + if (m_fd < 0) util::errno_error::throw_code (); } -fd_ref::~fd_ref () { - CHECK (fd >= 0); - close (fd); +fd::~fd () { + CHECK (m_fd >= 0); + close (m_fd); } -fd_ref::operator int (void) const - { return fd; } - - - //---------------------------------------------------------------------------- int indenter::overflow (int ch) { diff --git a/io.hpp b/io.hpp index 8333cb46..3d3e03f2 100644 --- a/io.hpp +++ b/io.hpp @@ -51,15 +51,17 @@ namespace util { ///------------------------------------------------------------------------ /// A simple RAII wrapper for file descriptors - struct fd_ref { - public: - int fd; + struct fd { + public: + explicit fd (int); + explicit fd (const boost::filesystem::path&, access_t); - explicit fd_ref (int _fd); - explicit fd_ref (const boost::filesystem::path&, access_t access); - ~fd_ref (); + ~fd (); - operator int (void) const; + operator int (void) const { return m_fd; } + + private: + int m_fd; }; diff --git a/io_posix.hpp b/io_posix.hpp index 3df417f4..e5b427f4 100644 --- a/io_posix.hpp +++ b/io_posix.hpp @@ -23,7 +23,7 @@ namespace util { namespace detail { namespace posix { class mapped_file { private: - fd_ref m_fd; + fd m_fd; uint8_t *m_data; size_t m_size;