Skip to content

Instantly share code, notes, and snippets.

@ned14
Created August 28, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ned14/392461b85e73add13e30 to your computer and use it in GitHub Desktop.
Save ned14/392461b85e73add13e30 to your computer and use it in GitHub Desktop.
// Set a dispatcher as current for this thread
afio::current_dispatcher_guard h(afio::make_dispatcher().get());
// Config a write gather
std::vector<asio::const_buffer> buffers;
buffers.push_back(asio::const_buffer("He", 2));
buffers.push_back(asio::const_buffer("ll", 2));
buffers.push_back(asio::const_buffer("o ", 2));
buffers.push_back(asio::const_buffer("Wo", 2));
buffers.push_back(asio::const_buffer("rl", 2));
buffers.push_back(asio::const_buffer("d\n", 2));
using handle_ptr = std::shared_ptr<handle>
handle_ptr file;
future<handle_ptr> opened=afio::async_file(
"example_file.txt",
afio::file_flags::create | afio::file_flags::read_write);
future<void> resized=afio::async_truncate(opened, 12);
future<void> written=afio::async_write(depends(resized, opened), buffers, 0);
future<void> synced=afio::async_sync(depends(written, opened));
// Note this return type is not possible due to ASIO scatter gather buffers, but I am mapping
// your original exactly
shared_future<std::vector<char>> data_future=afio::async_read(depends(synced, opened), 12, 0);
future<void> closed = afio::async_close(depends(data_future, opened));
future<void> deleted = async::async_rmfile(depends(closed, opened));
std::vector<char> data = data_future.get();
std::string contents(buffer.begin(), buffer.end());
std::cout << "Contents of file is '" << contents << "'" << std::endl;
deleted.wait();
// Or if you care about possible errors, call .get() instead of wait().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment