mirror of
https://github.com/vimpunk/mio.git
synced 2025-12-06 16:57:01 +08:00
Update example
This commit is contained in:
parent
7bb887e88c
commit
742469621a
11
README.md
11
README.md
@ -58,6 +58,7 @@ However, mio does not check whether the provided file descriptor has the same ac
|
|||||||
#include <system_error> // for std::error_code
|
#include <system_error> // for std::error_code
|
||||||
#include <cstdio> // for std::printf
|
#include <cstdio> // for std::printf
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
int handle_error(const std::error_code& error)
|
int handle_error(const std::error_code& error)
|
||||||
{
|
{
|
||||||
@ -73,16 +74,14 @@ int main()
|
|||||||
std::error_code error;
|
std::error_code error;
|
||||||
mio::mmap_sink rw_mmap = mio::make_mmap_sink(
|
mio::mmap_sink rw_mmap = mio::make_mmap_sink(
|
||||||
"file.txt", 0, mio::map_entire_file, error);
|
"file.txt", 0, mio::map_entire_file, error);
|
||||||
if(error) { return handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
assert(rw_mmap.is_open());
|
|
||||||
assert(!rw_mmap.empty());
|
|
||||||
|
|
||||||
// You can use any iterator based function.
|
// You can use any iterator based function.
|
||||||
std::fill(rw_mmap.begin(), rw_mmap.end(), 0);
|
std::fill(rw_mmap.begin(), rw_mmap.end(), 0);
|
||||||
|
|
||||||
// Or manually iterate through the mapped region just as if it were any other
|
// Or manually iterate through the mapped region just as if it were any other
|
||||||
// container, and change each byte's value (since this is a read-write mapping).
|
// container, and change each byte's value (since this is a read-write mapping).
|
||||||
for(auto& b : rw_mmap) {
|
for (auto& b : rw_mmap) {
|
||||||
b += 10;
|
b += 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,7 +92,7 @@ int main()
|
|||||||
// Don't forget to flush changes to disk, which is NOT done by the destructor for
|
// Don't forget to flush changes to disk, which is NOT done by the destructor for
|
||||||
// more explicit control of this potentially expensive operation.
|
// more explicit control of this potentially expensive operation.
|
||||||
rw_mmap.sync(error);
|
rw_mmap.sync(error);
|
||||||
if(error) { handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
// We can then remove the mapping, after which rw_mmap will be in a default
|
// We can then remove the mapping, after which rw_mmap will be in a default
|
||||||
// constructed state, i.e. this has the same effect as if the destructor had been
|
// constructed state, i.e. this has the same effect as if the destructor had been
|
||||||
@ -103,7 +102,7 @@ int main()
|
|||||||
// Now create the same mapping, but in read-only mode.
|
// Now create the same mapping, but in read-only mode.
|
||||||
mio::mmap_source ro_mmap = mio::make_mmap_source(
|
mio::mmap_source ro_mmap = mio::make_mmap_source(
|
||||||
"file.txt", 0, mio::map_entire_file, error);
|
"file.txt", 0, mio::map_entire_file, error);
|
||||||
if(error) { return handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
const int the_answer_to_everything = ro_mmap[answer_index];
|
const int the_answer_to_everything = ro_mmap[answer_index];
|
||||||
assert(the_answer_to_everything == 42);
|
assert(the_answer_to_everything == 42);
|
||||||
|
|||||||
@ -18,14 +18,14 @@ int main()
|
|||||||
std::error_code error;
|
std::error_code error;
|
||||||
mio::mmap_sink rw_mmap = mio::make_mmap_sink(
|
mio::mmap_sink rw_mmap = mio::make_mmap_sink(
|
||||||
"file.txt", 0, mio::map_entire_file, error);
|
"file.txt", 0, mio::map_entire_file, error);
|
||||||
if(error) { return handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
// You can use any iterator based function.
|
// You can use any iterator based function.
|
||||||
std::fill(rw_mmap.begin(), rw_mmap.end(), 0);
|
std::fill(rw_mmap.begin(), rw_mmap.end(), 0);
|
||||||
|
|
||||||
// Or manually iterate through the mapped region just as if it were any other
|
// Or manually iterate through the mapped region just as if it were any other
|
||||||
// container, and change each byte's value (since this is a read-write mapping).
|
// container, and change each byte's value (since this is a read-write mapping).
|
||||||
for(auto& b : rw_mmap) {
|
for (auto& b : rw_mmap) {
|
||||||
b += 10;
|
b += 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ int main()
|
|||||||
// Don't forget to flush changes to disk, which is NOT done by the destructor for
|
// Don't forget to flush changes to disk, which is NOT done by the destructor for
|
||||||
// more explicit control of this potentially expensive operation.
|
// more explicit control of this potentially expensive operation.
|
||||||
rw_mmap.sync(error);
|
rw_mmap.sync(error);
|
||||||
if(error) { handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
// We can then remove the mapping, after which rw_mmap will be in a default
|
// We can then remove the mapping, after which rw_mmap will be in a default
|
||||||
// constructed state, i.e. this has the same effect as if the destructor had been
|
// constructed state, i.e. this has the same effect as if the destructor had been
|
||||||
@ -46,7 +46,7 @@ int main()
|
|||||||
// Now create the same mapping, but in read-only mode.
|
// Now create the same mapping, but in read-only mode.
|
||||||
mio::mmap_source ro_mmap = mio::make_mmap_source(
|
mio::mmap_source ro_mmap = mio::make_mmap_source(
|
||||||
"file.txt", 0, mio::map_entire_file, error);
|
"file.txt", 0, mio::map_entire_file, error);
|
||||||
if(error) { return handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
const int the_answer_to_everything = ro_mmap[answer_index];
|
const int the_answer_to_everything = ro_mmap[answer_index];
|
||||||
assert(the_answer_to_everything == 42);
|
assert(the_answer_to_everything == 42);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user