-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExample1.cpp
More file actions
67 lines (46 loc) · 1.76 KB
/
Example1.cpp
File metadata and controls
67 lines (46 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2013 John R. Bandela
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "asio_helper.hpp"
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
template<class F>
void get_async_value(boost::asio::io_service& io, F f){
asio_helper::do_async(io,[f,&io](asio_helper::async_helper h){
h.post(std::bind(f,42));
});
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
try{
// This allows us to do our await magic
asio_helper::do_async(io,[&](asio_helper::async_helper helper){
// Notice how we can use a real loop
for(int i = 0; i < 5; i++){
// Call the async function with the results of make_callback
auto ec = helper.await<asio_helper::handlers::wait_handler>(
[&](asio_helper::handlers::wait_handler::callback_type cb){
t.async_wait(cb);
});
// Print a message about the timer
std::cout << "Timer went off " << (i+1) << " times with ec = " << ec.message() << std::endl;
// Set up a new expiration for the timer
t.expires_from_now(boost::posix_time::seconds(1));
}
typedef asio_helper::handlers::handler<int> int_handler;
std::cout << helper.await<int_handler>(
[&](int_handler::callback_type cb){
get_async_value(io,cb);
}) << std::endl;
});
io.run();
}
catch(std::exception& e){
std::cerr << e.what() << "\n";
}
return 0;
}