refactored mapped_file to no longer be derived from std::istream
This commit is contained in:
parent
fc264ed860
commit
cf9836f2a2
@ -16,7 +16,6 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace sc {
|
namespace sc {
|
||||||
|
|
||||||
@ -282,7 +281,7 @@ namespace sc {
|
|||||||
byte_order _saved_byte_order {byte_order::undefined};
|
byte_order _saved_byte_order {byte_order::undefined};
|
||||||
};
|
};
|
||||||
|
|
||||||
class mapped_file : public std::istream {
|
class mapped_file {
|
||||||
public:
|
public:
|
||||||
mapped_file();
|
mapped_file();
|
||||||
mapped_file(const std::string& path);
|
mapped_file(const std::string& path);
|
||||||
@ -295,20 +294,21 @@ namespace sc {
|
|||||||
mapped_file(mapped_file&& other);
|
mapped_file(mapped_file&& other);
|
||||||
mapped_file& operator=(mapped_file&& other);
|
mapped_file& operator=(mapped_file&& other);
|
||||||
|
|
||||||
|
// cleanup: mapped_file is RAII class for memory mapping and file descriptor
|
||||||
~mapped_file();
|
~mapped_file();
|
||||||
|
|
||||||
// it's an error to open an already opened 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);
|
void open(const std::string& path);
|
||||||
|
|
||||||
// direct (read-only) access to mapped memory
|
// direct (read-only) access to mapped memory
|
||||||
const char* mapping() const { return _mapping; }
|
char* mapping() const { return _mapping; }
|
||||||
size_t size() const { return _length; }
|
size_t size() const { return _length; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _fd {-1};
|
int _fd {-1};
|
||||||
char* _mapping {nullptr};
|
char* _mapping {nullptr};
|
||||||
size_t _length {0};
|
size_t _length {0};
|
||||||
std::stringbuf _membuf;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,7 @@
|
|||||||
|
|
||||||
using namespace sc::io;
|
using namespace sc::io;
|
||||||
|
|
||||||
mapped_file::mapped_file() : std::istream {&_membuf} {}
|
mapped_file::mapped_file(const std::string& path) {
|
||||||
|
|
||||||
mapped_file::mapped_file(const std::string& path) : std::istream {&_membuf} {
|
|
||||||
open(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;
|
_fd = other._fd;
|
||||||
_mapping = other._mapping;
|
_mapping = other._mapping;
|
||||||
_length = other._length;
|
_length = other._length;
|
||||||
_membuf = std::move(other._membuf);
|
|
||||||
|
|
||||||
other._fd = -1;
|
other._fd = -1;
|
||||||
other._mapping = nullptr;
|
other._mapping = nullptr;
|
||||||
other._length = 0;
|
other._length = 0;
|
||||||
other._membuf.pubsetbuf(nullptr, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mapped_file& mapped_file::operator=(mapped_file &&other) {
|
mapped_file& mapped_file::operator=(mapped_file &&other) {
|
||||||
@ -65,5 +61,4 @@ void mapped_file::open(const std::string& path) {
|
|||||||
_length = st.st_size;
|
_length = st.st_size;
|
||||||
_mapping = reinterpret_cast<char*>(mmap(nullptr, _length, PROT_READ, MAP_PRIVATE, _fd, 0));
|
_mapping = reinterpret_cast<char*>(mmap(nullptr, _length, PROT_READ, MAP_PRIVATE, _fd, 0));
|
||||||
if (_mapping == MAP_FAILED) throw std::runtime_error("could not map file");
|
if (_mapping == MAP_FAILED) throw std::runtime_error("could not map file");
|
||||||
_membuf.pubsetbuf(_mapping, _length);
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user