| 1 | // min_time_point.cpp ----------------------------------------------------------// |
| 2 | |
| 3 | // Copyright 2008 Howard Hinnant |
| 4 | // Copyright 2008 Beman Dawes |
| 5 | // Copyright 2009 Vicente J. Botet Escriba |
| 6 | |
| 7 | // Distributed under the Boost Software License, Version 1.0. |
| 8 | // See http://www.boost.org/LICENSE_1_0.txt |
| 9 | |
| 10 | /* |
| 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which |
| 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. |
| 13 | Many thanks to Howard for making his code available under the Boost license. |
| 14 | The original code was modified to conform to Boost conventions and to section |
| 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. |
| 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. |
| 17 | |
| 18 | time2_demo contained this comment: |
| 19 | |
| 20 | Much thanks to Andrei Alexandrescu, |
| 21 | Walter Brown, |
| 22 | Peter Dimov, |
| 23 | Jeff Garland, |
| 24 | Terry Golubiewski, |
| 25 | Daniel Krugler, |
| 26 | Anthony Williams. |
| 27 | */ |
| 28 | |
| 29 | #include <boost/chrono/typeof/boost/chrono/chrono.hpp> |
| 30 | #include <boost/type_traits.hpp> |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | using namespace boost::chrono; |
| 35 | |
| 36 | template <class Rep, class Period> |
| 37 | void print_duration(std::ostream& os, duration<Rep, Period> d) |
| 38 | { |
| 39 | os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n" ; |
| 40 | } |
| 41 | |
| 42 | namespace my_ns { |
| 43 | // Example min utility: returns the earliest time_point |
| 44 | // Being able to *easily* write this function is a major feature! |
| 45 | template <class Clock, class Duration1, class Duration2> |
| 46 | inline |
| 47 | typename boost::common_type<time_point<Clock, Duration1>, |
| 48 | time_point<Clock, Duration2> >::type |
| 49 | min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point<Clock, Duration1> t1, time_point<Clock, Duration2> t2) |
| 50 | { |
| 51 | return t2 < t1 ? t2 : t1; |
| 52 | } |
| 53 | } |
| 54 | void test_min() |
| 55 | { |
| 56 | #if 1 |
| 57 | typedef time_point<system_clock, |
| 58 | boost::common_type<system_clock::duration, seconds>::type> T1; |
| 59 | typedef time_point<system_clock, |
| 60 | boost::common_type<system_clock::duration, nanoseconds>::type> T2; |
| 61 | typedef boost::common_type<T1, T2>::type T3; |
| 62 | /*auto*/ T1 t1 = system_clock::now() + seconds(3); |
| 63 | /*auto*/ T2 t2 = system_clock::now() + nanoseconds(3); |
| 64 | /*auto*/ T3 t3 = (my_ns::min)(t1, t2); |
| 65 | #else |
| 66 | BOOST_AUTO(t1, system_clock::now() + seconds(3)); |
| 67 | BOOST_AUTO(t2, system_clock::now() + nanoseconds(3)); |
| 68 | BOOST_AUTO(t3, (min)(t1, t2)); |
| 69 | #endif |
| 70 | print_duration(os&: std::cout, d: t1 - t3); |
| 71 | print_duration(os&: std::cout, d: t2 - t3); |
| 72 | } |
| 73 | |
| 74 | int main() |
| 75 | { |
| 76 | test_min(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | |