1 | #pragma once
|
---|
2 |
|
---|
3 | #include <boost/asio.hpp>
|
---|
4 | #include <boost/chrono.hpp>
|
---|
5 |
|
---|
6 | namespace boost {
|
---|
7 | namespace asio {
|
---|
8 |
|
---|
9 | /**
|
---|
10 | Extension to boost::asio to allow use of a monotonic clock via boost::chrono::steady_clock with the
|
---|
11 | asio::basic_deadline_timer. Monotonic clocks are immune to adjustments of the system clock.
|
---|
12 | The underlying clock and time_type are chrono::steady_clock, but the durations are in posix_time::time_duration
|
---|
13 | in order to achieve a uniform and familiar interface.
|
---|
14 | */
|
---|
15 | template <>
|
---|
16 | struct time_traits<boost::chrono::steady_clock>
|
---|
17 | {
|
---|
18 | /**
|
---|
19 | The underlying time type is based on chrono::steady_clock's time_point.
|
---|
20 | */
|
---|
21 | typedef boost::chrono::steady_clock::time_point time_type;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | But the duration is based upon posix_time's time_duration, just like the ASIO built-in deadline_timer.
|
---|
25 | */
|
---|
26 | typedef boost::posix_time::time_duration duration_type;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | Accessing the clock yields the current time from the steady_clock.
|
---|
30 | */
|
---|
31 | static time_type now()
|
---|
32 | {
|
---|
33 | return boost::chrono::steady_clock::now();
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | Form a new time point (in Chrono) by adding in a time_duration (from posix_time), using the thing they have in
|
---|
38 | common -- microseconds.
|
---|
39 | */
|
---|
40 | static time_type add(const time_type& time, const duration_type& duration)
|
---|
41 | {
|
---|
42 | return time + boost::chrono::microseconds(duration.total_microseconds());
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | Subtract two time points (in Chrono) to yield a time_duration (from posix_time), using the thing they have in
|
---|
47 | common -- microseconds.
|
---|
48 | */
|
---|
49 | static duration_type subtract(const time_type& timeLhs, const time_type& timeRhs)
|
---|
50 | {
|
---|
51 | boost::chrono::microseconds oChronoDuration_us(
|
---|
52 | boost::chrono::duration_cast<boost::chrono::microseconds>(timeLhs - timeRhs));
|
---|
53 | return boost::posix_time::microseconds(oChronoDuration_us.count());
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | Test whether one chrono time is less than another.
|
---|
58 | */
|
---|
59 | static bool less_than(const time_type& timeLhs, const time_type& timeRhs)
|
---|
60 | {
|
---|
61 | return timeLhs < timeRhs;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | Convert to posix_time::time_duration type, since this is what ASIO needs under the hood. Since we're already
|
---|
66 | representing durations in posix_time::time_duration, this is a mere pass-through.
|
---|
67 | */
|
---|
68 | static boost::posix_time::time_duration to_posix_duration(const duration_type& duration)
|
---|
69 | {
|
---|
70 | return duration;
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | Convenience typedef for pairing the steady_clock time_traits (above) with a basic_deadline_timer yielding
|
---|
76 | a ready-to-use deadline timer that is based on a monotonic time that is immune to system clock changes.
|
---|
77 | A Boosty name has been chosen, as it is expected that in the near future boost::asio will have this built in.
|
---|
78 | */
|
---|
79 | typedef boost::asio::basic_deadline_timer<boost::chrono::steady_clock> monotonic_deadline_timer;
|
---|
80 |
|
---|
81 | } // namespace asio
|
---|
82 | } // namespace boost
|
---|