mio 1.1.0
Loading...
Searching...
No Matches
shared_mmap.hpp
Go to the documentation of this file.
1/* Copyright 2017 https://github.com/mandreyel
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
4 * software and associated documentation files (the "Software"), to deal in the Software
5 * without restriction, including without limitation the rights to use, copy, modify,
6 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7 * permit persons to whom the Software is furnished to do so, subject to the following
8 * conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies
11 * or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
16 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
18 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20
21/*
22 * Copyright 2026 Maxtek Consulting
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 * SOFTWARE.
41 */
42
43#ifndef MIO_SHARED_MMAP_HPP
44#define MIO_SHARED_MMAP_HPP
45
46#include "mio/mmap.hpp"
47
48#include <system_error> // std::error_code
49#include <memory> // std::shared_ptr
50
51namespace mio {
52
60template <access_mode AccessMode, typename ByteT>
62{
63 using impl_type = basic_mmap<AccessMode, ByteT>;
64 std::shared_ptr<impl_type> pimpl_;
65
66public:
71 using pointer = typename impl_type::pointer;
74 using iterator = typename impl_type::iterator;
80 using mmap_type = impl_type;
81
82 basic_shared_mmap() = default;
87
91
94 pimpl_ = std::make_shared<mmap_type>(std::move(mmap));
95 return *this;
96 }
97
99 explicit basic_shared_mmap(std::shared_ptr<mmap_type> mmap)
100 : pimpl_(std::move(mmap)) {}
101
103 basic_shared_mmap& operator=(std::shared_ptr<mmap_type> mmap) {
104 pimpl_ = std::move(mmap);
105 return *this;
106 }
107
108#ifdef __cpp_exceptions
114 template <typename String>
115 explicit basic_shared_mmap(const String& path, const size_type& offset = 0,
117 std::error_code error;
119 if(error) {
120 throw std::system_error(error);
121 }
122 }
123
129 explicit basic_shared_mmap(const handle_type& handle, const size_type& offset = 0,
131 std::error_code error;
133 if(error) {
134 throw std::system_error(error);
135 }
136 }
137#endif // __cpp_exceptions
138
145
147 std::shared_ptr<mmap_type> get_shared_ptr() {
148 return pimpl_;
149 } // cppcheck-suppress unusedFunction
150
157 return pimpl_ ? pimpl_->file_handle() : invalid_handle;
158 }
159
161 return pimpl_ ? pimpl_->mapping_handle() : invalid_handle;
162 }
163
165 bool is_open() const noexcept { return pimpl_ && pimpl_->is_open(); }
166
172 bool empty() const noexcept { return !pimpl_ || pimpl_->empty(); }
173
180 size_type size() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
181 size_type length() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
182 size_type mapped_length() const noexcept { return pimpl_ ? pimpl_->mapped_length() : 0; }
183
188 template <access_mode A = AccessMode,
189 typename = typename std::enable_if<A == access_mode::write>::type>
191 return pimpl_->data();
192 }
193 const_pointer data() const noexcept { return pimpl_ ? pimpl_->data() : nullptr; }
194
199 iterator begin() noexcept { return pimpl_->begin(); }
200 const_iterator begin() const noexcept { return pimpl_->begin(); }
201 const_iterator cbegin() const noexcept { return pimpl_->cbegin(); }
202
207 template <access_mode A = AccessMode,
208 typename = typename std::enable_if<A == access_mode::write>::type>
210 return pimpl_->end();
211 }
212 const_iterator end() const noexcept { return pimpl_->end(); }
213 const_iterator cend() const noexcept { return pimpl_->cend(); }
214
220 template <access_mode A = AccessMode,
221 typename = typename std::enable_if<A == access_mode::write>::type>
223 return pimpl_->rbegin();
224 }
225 const_reverse_iterator rbegin() const noexcept { return pimpl_->rbegin(); }
226 const_reverse_iterator crbegin() const noexcept { return pimpl_->crbegin(); }
227
232 template <access_mode A = AccessMode,
233 typename = typename std::enable_if<A == access_mode::write>::type>
235 return pimpl_->rend();
236 }
237 const_reverse_iterator rend() const noexcept { return pimpl_->rend(); }
238 const_reverse_iterator crend() const noexcept { return pimpl_->crend(); }
239
245 reference operator[](const size_type& i) noexcept { return (*pimpl_)[i]; }
246 const_reference operator[](const size_type& i) const noexcept { return (*pimpl_)[i]; }
247
268 template <typename String>
269 void map(const String& path, const size_type& offset, const size_type& length,
270 std::error_code& error) {
271 map_impl(path, offset, length, error);
272 }
273
286 template <typename String>
287 void map(const String& path, std::error_code& error) {
288 map_impl(path, 0, map_entire_file, error);
289 }
290
310 void map(const handle_type& handle, const size_type& offset, const size_type& length,
311 std::error_code& error) {
312 map_impl(handle, offset, length, error);
313 }
314
326 void map(const handle_type& handle, std::error_code& error) {
327 map_impl(handle, 0, map_entire_file, error);
328 }
329
339 void unmap() {
340 if(pimpl_)
341 pimpl_->unmap();
342 }
343
344 void swap(basic_shared_mmap& other) { pimpl_.swap(other.pimpl_); }
345
347 template <access_mode A = AccessMode,
348 typename = typename std::enable_if<A == access_mode::write>::type>
349 void sync(std::error_code& error) {
350 if(pimpl_)
351 pimpl_->sync(error);
352 }
353
356 friend bool operator==(const basic_shared_mmap& a, const basic_shared_mmap& b) {
357 return a.pimpl_ == b.pimpl_;
358 }
359
360 friend bool operator!=(const basic_shared_mmap& a, const basic_shared_mmap& b) {
361 return !(a == b);
362 }
363
364 friend bool operator<(const basic_shared_mmap& a, const basic_shared_mmap& b) {
365 return a.pimpl_ < b.pimpl_;
366 }
367
368 friend bool operator<=(const basic_shared_mmap& a, const basic_shared_mmap& b) {
369 return a.pimpl_ <= b.pimpl_;
370 }
371
372 friend bool operator>(const basic_shared_mmap& a, const basic_shared_mmap& b) {
373 return a.pimpl_ > b.pimpl_;
374 }
375
376 friend bool operator>=(const basic_shared_mmap& a, const basic_shared_mmap& b) {
377 return a.pimpl_ >= b.pimpl_;
378 }
379
380private:
381 template <typename MappingToken>
382 void map_impl(const MappingToken& token, const size_type& offset, const size_type& length,
383 std::error_code& error) {
384 if(!pimpl_) {
386 if(error) {
387 return;
388 }
389 pimpl_ = std::make_shared<mmap_type>(std::move(mmap));
390 } else {
391 pimpl_->map(token, offset, length, error);
392 }
393 }
394};
395
400template <typename ByteT>
402
407template <typename ByteT>
409
416
419
420} // namespace mio
421
422#endif // MIO_SHARED_MMAP_HPP
Definition shared_mmap.hpp:62
friend bool operator!=(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:360
const_iterator begin() const noexcept
Definition shared_mmap.hpp:200
const_reference operator[](const size_type &i) const noexcept
Definition shared_mmap.hpp:246
void map(const String &path, std::error_code &error)
Definition shared_mmap.hpp:287
typename impl_type::const_reference const_reference
Definition shared_mmap.hpp:70
size_type length() const noexcept
Definition shared_mmap.hpp:181
friend bool operator<(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:364
typename impl_type::pointer pointer
Definition shared_mmap.hpp:71
void swap(basic_shared_mmap &other)
Definition shared_mmap.hpp:344
size_type mapped_length() const noexcept
Definition shared_mmap.hpp:182
typename impl_type::handle_type handle_type
Definition shared_mmap.hpp:79
const_iterator cend() const noexcept
Definition shared_mmap.hpp:213
void sync(std::error_code &error)
Definition shared_mmap.hpp:349
size_type size() const noexcept
Definition shared_mmap.hpp:180
const_iterator end() const noexcept
Definition shared_mmap.hpp:212
iterator begin() noexcept
Definition shared_mmap.hpp:199
friend bool operator==(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:356
void map(const handle_type &handle, const size_type &offset, const size_type &length, std::error_code &error)
Definition shared_mmap.hpp:310
typename impl_type::const_reverse_iterator const_reverse_iterator
Definition shared_mmap.hpp:77
typename impl_type::value_type value_type
Definition shared_mmap.hpp:67
bool is_open() const noexcept
Definition shared_mmap.hpp:165
std::shared_ptr< mmap_type > get_shared_ptr()
Definition shared_mmap.hpp:147
const_pointer data() const noexcept
Definition shared_mmap.hpp:193
typename impl_type::const_iterator const_iterator
Definition shared_mmap.hpp:75
typename impl_type::reverse_iterator reverse_iterator
Definition shared_mmap.hpp:76
iterator end() noexcept
Definition shared_mmap.hpp:209
~basic_shared_mmap()=default
typename impl_type::const_pointer const_pointer
Definition shared_mmap.hpp:72
void map(const String &path, const size_type &offset, const size_type &length, std::error_code &error)
Definition shared_mmap.hpp:269
basic_shared_mmap(std::shared_ptr< mmap_type > mmap)
Definition shared_mmap.hpp:99
void unmap()
Definition shared_mmap.hpp:339
typename impl_type::reference reference
Definition shared_mmap.hpp:69
const_reverse_iterator rend() const noexcept
Definition shared_mmap.hpp:237
impl_type mmap_type
Definition shared_mmap.hpp:80
reverse_iterator rend() noexcept
Definition shared_mmap.hpp:234
pointer data() noexcept
Definition shared_mmap.hpp:190
basic_shared_mmap & operator=(mmap_type &&mmap)
Definition shared_mmap.hpp:93
friend bool operator<=(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:368
friend bool operator>=(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:376
basic_shared_mmap & operator=(basic_shared_mmap &&)=default
typename impl_type::difference_type difference_type
Definition shared_mmap.hpp:73
handle_type mapping_handle() const noexcept
Definition shared_mmap.hpp:160
const_iterator cbegin() const noexcept
Definition shared_mmap.hpp:201
const_reverse_iterator crbegin() const noexcept
Definition shared_mmap.hpp:226
bool empty() const noexcept
Definition shared_mmap.hpp:172
const_reverse_iterator rbegin() const noexcept
Definition shared_mmap.hpp:225
friend bool operator>(const basic_shared_mmap &a, const basic_shared_mmap &b)
Definition shared_mmap.hpp:372
basic_shared_mmap & operator=(std::shared_ptr< mmap_type > mmap)
Definition shared_mmap.hpp:103
typename impl_type::iterator_category iterator_category
Definition shared_mmap.hpp:78
basic_shared_mmap(basic_shared_mmap &&)=default
typename impl_type::iterator iterator
Definition shared_mmap.hpp:74
typename impl_type::size_type size_type
Definition shared_mmap.hpp:68
const_reverse_iterator crend() const noexcept
Definition shared_mmap.hpp:238
basic_shared_mmap(mmap_type &&mmap)
Definition shared_mmap.hpp:89
basic_shared_mmap(const basic_shared_mmap &)=default
handle_type file_handle() const noexcept
Definition shared_mmap.hpp:156
basic_shared_mmap & operator=(const basic_shared_mmap &)=default
void map(const handle_type &handle, std::error_code &error)
Definition shared_mmap.hpp:326
reference operator[](const size_type &i) noexcept
Definition shared_mmap.hpp:245
reverse_iterator rbegin() noexcept
Definition shared_mmap.hpp:222
Definition string_util.hpp:48
access_mode
Definition page.hpp:59
@ map_entire_file
Definition mmap.hpp:68
Definition mmap.hpp:83
std::random_access_iterator_tag iterator_category
Definition mmap.hpp:95
pointer iterator
Definition mmap.hpp:91
size_t size_type
Definition mmap.hpp:85
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition mmap.hpp:94
const_pointer const_iterator
Definition mmap.hpp:92
const value_type * const_pointer
Definition mmap.hpp:89
std::reverse_iterator< iterator > reverse_iterator
Definition mmap.hpp:93
std::ptrdiff_t difference_type
Definition mmap.hpp:90
const value_type & const_reference
Definition mmap.hpp:87
value_type * pointer
Definition mmap.hpp:88
value_type & reference
Definition mmap.hpp:86
file_handle_type handle_type
Definition mmap.hpp:96
ByteT value_type
Definition mmap.hpp:84