47 boost::iostreams::mapped_file_params params_;
48 boost::iostreams::mapped_file device_;
55#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
57 friend class boost::serialization::access;
62 void save(S &s,
const unsigned )
const {
63 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Super);
64 s & boost::serialization::make_nvp(
"path", params_.path);
65 s & boost::serialization::make_nvp(
"flags", params_.flags);
66 s & boost::serialization::make_nvp(
"mode", params_.mode);
67 s & boost::serialization::make_nvp(
"offset", params_.offset);
68 s & boost::serialization::make_nvp(
"length", params_.length);
69 s & boost::serialization::make_nvp(
"new_file_size", params_.new_file_size);
72 BOOST_STATIC_ASSERT(
sizeof hint >=
sizeof params_.hint);
73 hint = (boost::uint64_t)(params_.hint);
74 s & BOOST_SERIALIZATION_NVP(hint);
80 void load(S &s,
const unsigned ) {
81 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Super);
82 s & boost::serialization::make_nvp(
"path", params_.path);
83 s & boost::serialization::make_nvp(
"flags", params_.flags);
84 s & boost::serialization::make_nvp(
"mode", params_.mode);
85 s & boost::serialization::make_nvp(
"offset", params_.offset);
86 s & boost::serialization::make_nvp(
"length", params_.length);
87 s & boost::serialization::make_nvp(
"new_file_size", params_.new_file_size);
90 BOOST_STATIC_ASSERT(
sizeof hint >=
sizeof params_.hint);
91 s & BOOST_SERIALIZATION_NVP(hint);
92 params_.hint = (
const char*)hint;
94 device_.open(params_);
97 BOOST_SERIALIZATION_SPLIT_MEMBER();
100#ifdef SAWYER_HAVE_CEREAL
102 friend class cereal::access;
104 template<
class Archive>
105 void CEREAL_SAVE_FUNCTION_NAME(Archive &archive)
const {
106 archive(cereal::base_class<Super>());
107 archive(cereal::make_nvp(
"path", params_.path));
108 archive(cereal::make_nvp(
"flags", params_.flags));
109 archive(cereal::make_nvp(
"mode", params_.mode));
110 archive(cereal::make_nvp(
"offset", params_.offset));
111 archive(cereal::make_nvp(
"length", params_.length));
112 archive(cereal::make_nvp(
"new_file_size", params_.new_file_size));
114 boost::uint64_t hint;
115 BOOST_STATIC_ASSERT(
sizeof hint >=
sizeof params_.hint);
116 hint = (boost::uint64_t)(params_.hint);
117 archive(CEREAL_NVP(hint));
120 template<
class Archive>
121 void CEREAL_LOAD_FUNCTION_NAME(Archive &archive) {
122 archive(cereal::base_class<Super>());
123 archive(cereal::make_nvp(
"path", params_.path));
124 archive(cereal::make_nvp(
"flags", params_.flags));
125 archive(cereal::make_nvp(
"mode", params_.mode));
126 archive(cereal::make_nvp(
"offset", params_.offset));
127 archive(cereal::make_nvp(
"length", params_.length));
128 archive(cereal::make_nvp(
"new_file_size", params_.new_file_size));
130 boost::uint64_t hint;
131 BOOST_STATIC_ASSERT(
sizeof hint >=
sizeof params_.hint);
132 archive(CEREAL_NVP(hint));
133 params_.hint = (
const char*)hint;
135 device_.open(params_);
141 :
Super(
".MappedBuffer") {}
142 explicit MappedBuffer(
const boost::iostreams::mapped_file_params ¶ms)
143 :
Super(
".MappedBuffer"), params_(params), device_(params) {}
152 }
catch (
const std::ios_base::failure &e) {
153 if (boost::contains(e.what(),
"Invalid argument") &&
154 boost::filesystem::is_regular_file(params.path) &&
155 boost::filesystem::is_empty(params.path)) {
168 boost::iostreams::mapped_file::mapmode mode=boost::iostreams::mapped_file::readonly,
169 boost::intmax_t offset=0,
170 boost::iostreams::mapped_file::size_type length=boost::iostreams::mapped_file::max_length) {
171 boost::iostreams::mapped_file_params params(path.string());
173 params.length = length;
174 params.offset = offset;
184 Address nWritten = newBuffer->write((
const Value*)device_.data(), 0, this->size());
185 if (nWritten != this->
size()) {
186 throw std::runtime_error(
"MappedBuffer::copy() failed after copying " +
187 boost::lexical_cast<std::string>(nWritten) +
" of " +
188 boost::lexical_cast<std::string>(this->
size()) +
189 (1==this->
size()?
" value":
" values"));
195 return address >= device_.size() ?
Address(0) : (
Address(device_.size()) - address) /
sizeof(
Value);
199 if (n != this->
size())
200 throw std::runtime_error(
"resizing not allowed for MappedBuffer");
205 memcpy(buf, device_.const_data() + address, nread *
sizeof(
Value));
211 memcpy(device_.data() + address, buf, nwritten *
sizeof(
Value));
216 return (
Value*)device_.const_data();
static Buffer< A, T >::Ptr instance(const boost::filesystem::path &path, boost::iostreams::mapped_file::mapmode mode=boost::iostreams::mapped_file::readonly, boost::intmax_t offset=0, boost::iostreams::mapped_file::size_type length=boost::iostreams::mapped_file::max_length)
Map a file by name.