minor improvements

This commit is contained in:
Bob Polis 2020-07-11 12:41:20 +02:00
parent a0906c8a08
commit 261ef4356b
2 changed files with 237 additions and 227 deletions

View File

@ -282,21 +282,26 @@ namespace sc {
byte_order _saved_byte_order {byte_order::undefined};
};
class mapped_file : std::istream {
class mapped_file : public std::istream {
public:
mapped_file() = default;
mapped_file();
mapped_file(const std::string& path);
// no copying
mapped_file(const mapped_file&) = delete;
mapped_file& operator=(const mapped_file&) = delete;
// moving allowed
mapped_file(mapped_file&& other);
mapped_file& operator=(mapped_file&& other);
~mapped_file();
// it's an error to open an already opened mapped_file
void open(const std::string& path);
// direct (read-only) access to mapped memory
const char* mapping() const { return _mapping; }
size_t size() const { return _length; }
private:

View File

@ -18,6 +18,8 @@
using namespace sc::io;
mapped_file::mapped_file() : std::istream {&_membuf} {}
mapped_file::mapped_file(const std::string& path) : std::istream {&_membuf} {
open(path);
}
@ -47,12 +49,15 @@ mapped_file::mapped_file(mapped_file&& other) {
}
mapped_file& mapped_file::operator=(mapped_file &&other) {
if (&other != this) {
mapped_file mf {std::move(other)};
std::swap(mf, *this);
}
return *this;
}
void mapped_file::open(const std::string& path) {
if (_fd != -1) throw std::runtime_error("already open");
_fd = ::open(path.c_str(), O_RDONLY);
throw_if_min1_msg(_fd, "could not open file");
struct stat st;