From cf9836f2a281891aecd3176af06082baad9e2246 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Sun, 12 Jul 2020 13:29:58 +0200 Subject: [PATCH] refactored mapped_file to no longer be derived from std::istream --- libscio.hpp | 8 ++++---- mapped_file.cpp | 9 ++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/libscio.hpp b/libscio.hpp index 598ceed..3e7a4ec 100644 --- a/libscio.hpp +++ b/libscio.hpp @@ -16,7 +16,6 @@ #include #include #include -#include namespace sc { @@ -282,7 +281,7 @@ namespace sc { byte_order _saved_byte_order {byte_order::undefined}; }; - class mapped_file : public std::istream { + class mapped_file { public: mapped_file(); mapped_file(const std::string& path); @@ -295,20 +294,21 @@ namespace sc { mapped_file(mapped_file&& other); mapped_file& operator=(mapped_file&& other); + // cleanup: mapped_file is RAII class for memory mapping and file descriptor ~mapped_file(); // it's an error to open an already opened mapped_file + // open() is meant to be used after default constructor void open(const std::string& path); // direct (read-only) access to mapped memory - const char* mapping() const { return _mapping; } + char* mapping() const { return _mapping; } size_t size() const { return _length; } private: int _fd {-1}; char* _mapping {nullptr}; size_t _length {0}; - std::stringbuf _membuf; }; } diff --git a/mapped_file.cpp b/mapped_file.cpp index 1592c76..b9c02eb 100644 --- a/mapped_file.cpp +++ b/mapped_file.cpp @@ -18,9 +18,7 @@ using namespace sc::io; -mapped_file::mapped_file() : std::istream {&_membuf} {} - -mapped_file::mapped_file(const std::string& path) : std::istream {&_membuf} { +mapped_file::mapped_file(const std::string& path) { open(path); } @@ -36,16 +34,14 @@ mapped_file::~mapped_file() { } } -mapped_file::mapped_file(mapped_file&& other) : std::istream {std::move(other)} { +mapped_file::mapped_file(mapped_file&& other) { _fd = other._fd; _mapping = other._mapping; _length = other._length; - _membuf = std::move(other._membuf); other._fd = -1; other._mapping = nullptr; other._length = 0; - other._membuf.pubsetbuf(nullptr, 0); } mapped_file& mapped_file::operator=(mapped_file &&other) { @@ -65,5 +61,4 @@ void mapped_file::open(const std::string& path) { _length = st.st_size; _mapping = reinterpret_cast(mmap(nullptr, _length, PROT_READ, MAP_PRIVATE, _fd, 0)); if (_mapping == MAP_FAILED) throw std::runtime_error("could not map file"); - _membuf.pubsetbuf(_mapping, _length); }