| 1 | // i_dont_like_the_default_duration_behavior.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/chrono.hpp> |
| 30 | #include <boost/type_traits.hpp> |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | namespace I_dont_like_the_default_duration_behavior |
| 35 | { |
| 36 | |
| 37 | // Here's how you override the duration's default constructor to do anything you want (in this case zero) |
| 38 | |
| 39 | template <class R> |
| 40 | class zero_default |
| 41 | { |
| 42 | public: |
| 43 | typedef R rep; |
| 44 | |
| 45 | private: |
| 46 | rep rep_; |
| 47 | public: |
| 48 | zero_default(rep i = 0) : rep_(i) {} |
| 49 | operator rep() const {return rep_;} |
| 50 | |
| 51 | zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;} |
| 52 | zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;} |
| 53 | zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;} |
| 54 | zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;} |
| 55 | |
| 56 | zero_default operator+ () const {return *this;} |
| 57 | zero_default operator- () const {return zero_default(-rep_);} |
| 58 | zero_default& operator++() {++rep_; return *this;} |
| 59 | zero_default operator++(int) {return zero_default(rep_++);} |
| 60 | zero_default& operator--() {--rep_; return *this;} |
| 61 | zero_default operator--(int) {return zero_default(rep_--);} |
| 62 | |
| 63 | friend zero_default operator+(zero_default x, zero_default y) {return x += y;} |
| 64 | friend zero_default operator-(zero_default x, zero_default y) {return x -= y;} |
| 65 | friend zero_default operator*(zero_default x, zero_default y) {return x *= y;} |
| 66 | friend zero_default operator/(zero_default x, zero_default y) {return x /= y;} |
| 67 | |
| 68 | friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;} |
| 69 | friend bool operator!=(zero_default x, zero_default y) {return !(x == y);} |
| 70 | friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;} |
| 71 | friend bool operator<=(zero_default x, zero_default y) {return !(y < x);} |
| 72 | friend bool operator> (zero_default x, zero_default y) {return y < x;} |
| 73 | friend bool operator>=(zero_default x, zero_default y) {return !(x < y);} |
| 74 | }; |
| 75 | |
| 76 | typedef boost::chrono::duration<zero_default<long long>, boost::nano > nanoseconds; |
| 77 | typedef boost::chrono::duration<zero_default<long long>, boost::micro > microseconds; |
| 78 | typedef boost::chrono::duration<zero_default<long long>, boost::milli > milliseconds; |
| 79 | typedef boost::chrono::duration<zero_default<long long> > seconds; |
| 80 | typedef boost::chrono::duration<zero_default<long long>, boost::ratio<60> > minutes; |
| 81 | typedef boost::chrono::duration<zero_default<long long>, boost::ratio<3600> > hours; |
| 82 | |
| 83 | void test() |
| 84 | { |
| 85 | milliseconds ms; |
| 86 | std::cout << ms.count() << '\n'; |
| 87 | } |
| 88 | |
| 89 | } // I_dont_like_the_default_duration_behavior |
| 90 | |
| 91 | |
| 92 | int main() |
| 93 | { |
| 94 | I_dont_like_the_default_duration_behavior::test(); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | |