FileMapping

Encapsulated management of a memory-mapped file.

Constructors

this
this(char* filename)

Open filename and map it in memory. If Datum is const, opens for read-only and maps the content in memory; no error is issued if the file does not exist. This makes it easy to treat a non-existing file as empty.

Destructor

~this
~this()

Frees resources associated with this mapping. However, it does not deallocate the name.

Members

Functions

active
bool active()

Queries whether this is currently associated with a file.

close
void close()

Frees resources associated with this mapping. However, it does not deallocate the name. Reinitializes this as a fresh object that can be reused.

discard
bool discard()

Deletes the underlying file and frees all resources associated. Reinitializes this as a fresh object that can be reused.

length
size_t length()

Queries the length of the file associated with this mapping. If not active, returns 0.

moveToFile
bool moveToFile(char* filename)

Unconditionally and destructively moves the underlying file to filename. If the operation succeeds, returns true. Upon failure, prints a message to stderr and returns false. In all cases it closes the underlying file.

opSlice
auto opSlice()

Get a slice to the contents of the entire file.

resize
void resize(size_t size)

Resizes the file and mapping to the specified size.

Parameters

Datum

the mapped data type: Use a POD of size 1 for read/write mapping and a const version thereof for read-only mapping. Other primitive types should work, but have not been yet tested.

Meta