Class MemoryMappedFile

Class Documentation

class MemoryMappedFile

This class is a wrapper to os-dependend file mapping implementations. It supports to map a read-only file into virtual address space. After successful opening, pointers to the begin/end of the virtual address space are accessible via cbegin() / cend(). All file and mapping handlers are freed automatically.

This wrapper is non-copyable but can be cheaply moved and swapped.

Public Types

using value_type = char
using const_iterator = const value_type*

Public Functions

MemoryMappedFile()

Creates a MemoryMappedFile instance in an unmapped state. A call to open() is required.

explicit MemoryMappedFile(const std::string &path)

Invokes open(path) automatically.

~MemoryMappedFile()

Invokes close.

MemoryMappedFile(const MemoryMappedFile&) = delete

It’s non copy-able.

MemoryMappedFile &operator=(const MemoryMappedFile&) = delete
MemoryMappedFile(MemoryMappedFile &&o) noexcept

Takes over the mapping (if any) from o, leaves o in a “closed” state ready to be opened again.

MemoryMappedFile &operator=(MemoryMappedFile &&o) noexcept

If *this currently holds a mapping, it is closed and replaced the other’s state which in turn is left closed.

void open(const std::string &file)

Opens the file and maps it to cbegin()cend() Opening an empty file is considered an error.

void close() noexcept

If a file is mapped, it is closed. Otherwise, operation is carried out.

Note

This function is automatically called by the destructor.

inline const_iterator cbegin() const

If a file is opened, a random-access iterator to the first byte mapped is returned. If no file is opened, nullptr is returned.

inline const_iterator cend() const

Analogously to cbegin()

inline size_t size() const

Number of bytes mapped.