Update README.md and example.cpp

This commit is contained in:
mandreyel 2018-10-25 09:15:15 +02:00
parent 43ee283c01
commit 99db68866c
2 changed files with 51 additions and 32 deletions

View File

@ -24,6 +24,11 @@ There are three ways to map a file into memory:
```c++
mio::mmap_source mmap(path, offset, size_to_map);
```
or you can omit the `offset` and `size_to_map` arguments, in which case the
entire file is mapped:
```c++
mio::mmap_source mmap(path);
```
- Using the factory function:
```c++
@ -37,6 +42,10 @@ std::error_code error;
mio::mmap_source mmap;
mmap.map(path, offset, size_to_map, error);
```
or:
```c++
mmap.map(path, error);
```
Moreover, in each case, you can provide either some string type for the file's path, or you can use an existing, valid file handle.
```c++
@ -69,19 +78,8 @@ types for functions where character strings are expected (e.g. path parameters).
#include <algorithm>
#include <fstream>
int handle_error(const std::error_code& error)
{
const auto& errmsg = error.message();
std::printf("error mapping file: %s, exiting...\n", errmsg.c_str());
return error.value();
}
void allocate_file(const std::string& path, const int size)
{
std::ofstream file(path);
std::string s(size, '0');
file << s;
}
int handle_error(const std::error_code& error);
void allocate_file(const std::string& path, const int size);
int main()
{
@ -124,14 +122,30 @@ int main()
// effect as if the destructor had been invoked.
rw_mmap.unmap();
// Now create the same mapping, but in read-only mode.
mio::mmap_source ro_mmap = mio::make_mmap_source(
path, 0, mio::map_entire_file, error);
// Now create the same mapping, but in read-only mode. Note that calling the
// overload without the offset and file length parameters maps the entire
// file.
mio::mmap_source ro_mmap;
ro_mmap.map(path, error);
if (error) { return handle_error(error); }
const int the_answer_to_everything = ro_mmap[answer_index];
assert(the_answer_to_everything == 42);
}
int handle_error(const std::error_code& error)
{
const auto& errmsg = error.message();
std::printf("error mapping file: %s, exiting...\n", errmsg.c_str());
return error.value();
}
void allocate_file(const std::string& path, const int size)
{
std::ofstream file(path);
std::string s(size, '0');
file << s;
}
```
`mio::basic_mmap` is move-only, but if multiple copies to the same mapping are needed, use `mio::basic_shared_mmap` which has `std::shared_ptr` semantics and has the same interface as `mio::basic_mmap`.

View File

@ -5,19 +5,8 @@
#include <algorithm>
#include <fstream>
int handle_error(const std::error_code& error)
{
const auto& errmsg = error.message();
std::printf("error mapping file: %s, exiting...\n", errmsg.c_str());
return error.value();
}
void allocate_file(const std::string& path, const int size)
{
std::ofstream file(path);
std::string s(size, '0');
file << s;
}
int handle_error(const std::error_code& error);
void allocate_file(const std::string& path, const int size);
int main()
{
@ -60,11 +49,27 @@ int main()
// effect as if the destructor had been invoked.
rw_mmap.unmap();
// Now create the same mapping, but in read-only mode.
mio::mmap_source ro_mmap = mio::make_mmap_source(
path, 0, mio::map_entire_file, error);
// Now create the same mapping, but in read-only mode. Note that calling the
// overload without the offset and file length parameters maps the entire
// file.
mio::mmap_source ro_mmap;
ro_mmap.map(path, error);
if (error) { return handle_error(error); }
const int the_answer_to_everything = ro_mmap[answer_index];
assert(the_answer_to_everything == 42);
}
int handle_error(const std::error_code& error)
{
const auto& errmsg = error.message();
std::printf("error mapping file: %s, exiting...\n", errmsg.c_str());
return error.value();
}
void allocate_file(const std::string& path, const int size)
{
std::ofstream file(path);
std::string s(size, '0');
file << s;
}