Memory-mapped files stream
Provide read-write access to memory-mapped files on Windows and POSIX systems.
|
fmstream provides an interface to read/write data from/to memory-mapped files as input/output streams. More...
#include <fmstream.h>
Public Member Functions | |
fmstream () | |
Construct object. | |
fmstream (const char *path_name, std::streamsize max_length=0, std::streamoff offset=0) | |
Construct object and open or create a file. | |
virtual | ~fmstream () |
Destructs the fmstream object. | |
filemappingbuf * | rdbuf () const |
Get the associated filemappingbuf object. | |
bool | is_open () const |
Check if a file is open. | |
void | open (const char *path_name, std::streamsize max_length=0, std::streamoff offset=0) |
Open file. | |
void | close () |
Close file. | |
const void * | ptellg () |
Get position of the get pointer. | |
void * | ptellp () |
Get position of the put pointer. | |
std::istream & | pseekg (const void *ptr) |
Sets the position of the get pointer. | |
std::ostream & | pseekp (void *ptr) |
Sets the position of the put pointer. | |
const void * | data () const |
Get the read-only base address of the mapping. | |
void * | data () |
Get the base address of the mapping. | |
std::streamsize | size () const |
Get the maximum byte length of the mapping. | |
C++11 | |
The following methods requires some features introduced by the latest revision of the C++ standard (2011). Older compilers may not support it. | |
fmstream (fmstream &&rhs_stream) | |
Move constructor (requires C++11). | |
fmstream & | operator= (fmstream &&rhs_stream) |
Move assignment (requires C++11). | |
void | swap (fmstream &stream) |
Swap internals (requires C++11). | |
fmstream provides an interface to read/write data from/to memory-mapped files as input/output streams.
The objects of this class maintain internally a pointer to a filemappingbuf object that can be obtained by calling member rdbuf. The file to be associated with the stream can be specified either as a parameter in the constructor or by calling member open. After all necessary operations on a file have been performed, it can be closed (or disassociated) by calling member close. Once closed, the same file stream object may be used to open another file. The member function is_open can be used to determine whether the stream object is currently associated with a file. fmstream can be used in place of std::fstream.
fmstream::fmstream | ( | ) |
Construct object.
Constructs an object of the fstream class. This implies the initialization of the associated filemappingbuf object and the call to the constructor of its base class with the filemappingbuf object as parameter.
|
explicit |
Construct object and open or create a file.
Constructs an object of the fstream class. This implies the initialization of the associated filemappingbuf object and the call to the constructor of its base class with the filemappingbuf object as parameter. The stream is associated with a physical file as if a call to the member function open with the same parameters was made. If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
path_name | C-string contains the name of the file to be opened or created. |
max_length | Maximum length of the file mapping. If this parameter are 0, the maximum length of the file mapping object is equal to the current size of the file. |
offset | File offset where the mapping begins. They must also match the memory allocation granularity of the system. That is, the offset must be a multiple of the allocation granularity. To obtain the offset granularity of the system, use filemapping::offset_granularity(). |
fmstream::fmstream | ( | fmstream && | rhs_stream | ) |
Move constructor (requires C++11).
Acquires the contents of rhs_stream, by move-assigning its members and base classes.
rhs_stream | File stream to move. rhs_stream becomes of invalid state after the operation. |
void fmstream::close | ( | ) |
Close file.
Closes the file currently associated with the object and disassociates it. The function fails if no file is currently open (associated) with this object. On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
const void * fmstream::data | ( | ) | const |
Get the read-only base address of the mapping.
The function fails if no file is currently open (associated) with this object.
void * fmstream::data | ( | ) |
Get the base address of the mapping.
The function fails if no file is currently open (associated) with this object.
bool fmstream::is_open | ( | ) | const |
Check if a file is open.
Returns true if the stream is currently associated with a file, and false otherwise. The stream is associated with a file if either a previous call to member open succeeded or if the object was successfully constructed using the parameterized constructor, and close has not been called since.
void fmstream::open | ( | const char * | path_name, |
std::streamsize | max_length = 0 , |
||
std::streamoff | offset = 0 |
||
) |
Open file.
Opens a file whose name is path_name, associating its content with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode. If the object already has a file associated (open), this function fails. If the file do not exist, the file is created with the 'offset + max_length' size. If the size of the opened file is less than 'offset + max_length', the file growing. If the size of the opened file is greater than 'offset + max_length', the file is not truncated. An attempt to map a file with a length of 0 fails. On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
path_name | C-string contains the name of the file to be opened. |
max_length | Maximum length of the file mapping. If this parameter are 0, the maximum length of the file mapping object is equal to the current size of the file. |
offset | File offset where the mapping begins. They must also match the memory allocation granularity of the system. That is, the offset must be a multiple of the allocation granularity. To obtain the offset granularity of the system, use filemapping::offset_granularity(). |
Move assignment (requires C++11).
Closes the source stream (as if member close was called), and then acquires the contents of rhs_stream.
rhs_stream | File stream to move. rhs_stream becomes of invalid state after the operation. |
std::istream & fmstream::pseekg | ( | const void * | ptr | ) |
Sets the position of the get pointer.
The get pointer determines the next location to be read in the source associated to the stream. The function fails if no file is currently open (associated) with this object. On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
ptr | New absolute position for the input pointer. |
std::ostream & fmstream::pseekp | ( | void * | ptr | ) |
Sets the position of the put pointer.
The put pointer determines the location in the output sequence where the next output operation is going to take place. The function fails if no file is currently open (associated) with this object. On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
ptr | New absolute position for the output pointer. |
const void * fmstream::ptellg | ( | ) |
Get position of the get pointer.
The get pointer determines the next location in the input sequence to be read by the next input operation. The function fails if no file is currently open (associated) with this object.
void * fmstream::ptellp | ( | ) |
Get position of the put pointer.
The put pointer determines the location in the output sequence where the next output operation is going to take place. The function fails if no file is currently open (associated) with this object.
filemappingbuf * fmstream::rdbuf | ( | ) | const |
Get the associated filemappingbuf object.
Returns a pointer to the filemappingbuf object associated with the stream.
std::streamsize fmstream::size | ( | ) | const |
Get the maximum byte length of the mapping.
The function fails if no file is currently open (associated) with this object.
void fmstream::swap | ( | fmstream & | stream | ) |
Swap internals (requires C++11).
Exchanges the state of the stream with those of other.
stream | fmstream to exchange the state with. |