From 165dbb3cbbe4d70c07107207facf984feb37f315 Mon Sep 17 00:00:00 2001 From: paolo Date: Tue, 21 Jul 2009 14:48:47 +0000 Subject: [PATCH] 2009-07-21 Paolo Carlini * include/std/chrono (duration<>::operator%=, operator%): Add, per DR 934. * testsuite/20_util/duration/arithmetic/dr934-1.cc: New. * testsuite/20_util/duration/arithmetic/dr934-2.cc: Likewise. * include/std/chrono (operator/): Simplify implementation. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@149856 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 9 ++ libstdc++-v3/include/std/chrono | 106 +++++++++++++-------- .../20_util/duration/arithmetic/dr934-1.cc | 48 ++++++++++ .../20_util/duration/arithmetic/dr934-2.cc | 54 +++++++++++ 4 files changed, 175 insertions(+), 42 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-1.cc create mode 100644 libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-2.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 3d706caec8f..84e1edc57a8 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2009-07-21 Paolo Carlini + + * include/std/chrono (duration<>::operator%=, operator%): + Add, per DR 934. + * testsuite/20_util/duration/arithmetic/dr934-1.cc: New. + * testsuite/20_util/duration/arithmetic/dr934-2.cc: Likewise. + + * include/std/chrono (operator/): Simplify implementation. + 2009-07-20 Benjamin Kosnik * doc/xml/manual/intro.xml: Escape '&', validate. diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index d18f2777f51..aa4888d9a9d 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -154,7 +154,7 @@ namespace std /// treat_as_floating_point template - struct treat_as_floating_point + struct treat_as_floating_point : is_floating_point<_Rep> { }; @@ -211,7 +211,7 @@ namespace std duration() = default; template - explicit duration(_Rep2 const& __rep) + explicit duration(const _Rep2& __rep) : __r(static_cast(__rep)) { static_assert(is_convertible<_Rep2,rep>::value @@ -244,29 +244,29 @@ namespace std { return *this; } duration - operator-() const + operator-() const { return duration(-__r); } duration& - operator++() + operator++() { ++__r; return *this; } duration - operator++(int) + operator++(int) { return duration(__r++); } duration& - operator--() - { + operator--() + { --__r; return *this; } duration - operator--(int) + operator--(int) { return duration(__r--); } duration& @@ -292,11 +292,30 @@ namespace std duration& operator/=(const rep& __rhs) - { + { __r /= __rhs; return *this; } + // DR 934. + template + typename enable_if::value, + duration&>::type + operator%=(const rep& __rhs) + { + __r %= __rhs; + return *this; + } + + template + typename enable_if::value, + duration&>::type + operator%=(const duration& __d) + { + __r %= __d.count(); + return *this; + } + // 20.8.3.4 special values // TODO: These should be constexprs. static const duration @@ -310,8 +329,8 @@ namespace std static const duration max() { return duration(duration_values::max()); } - - private: + + private: rep __r; }; @@ -351,46 +370,49 @@ namespace std inline duration::type, _Period> operator*(const _Rep2& __s, const duration<_Rep1, _Period>& __d) { return __d * __s; } - - template - struct __division_impl; - + template - struct __division_impl, _Rep2, - typename enable_if::value>::type> + inline duration::value, _Rep2>::type>::type, _Period> + operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) { - typedef typename common_type<_Rep1, _Rep2>::type __cr; - typedef - duration::type, _Period> __rt; - - static __rt - __divide(const duration<_Rep1, _Period>& __d, const _Rep2& __s) - { return duration<__cr, _Period>(__d) /= __s; } - }; + typedef typename common_type<_Rep1, _Rep2>::type __cr; + return duration<__cr, _Period>(__d) /= __s; + } - template - struct __division_impl, - duration<_Rep2, _Period2>> + template + inline typename common_type<_Rep1, _Rep2>::type + operator/(const duration<_Rep1, _Period1>& __lhs, + const duration<_Rep2, _Period2>& __rhs) { typedef typename common_type, duration<_Rep2, _Period2>>::type __ct; - typedef typename common_type<_Rep1, _Rep2>::type __rt; + return __ct(__lhs).count() / __ct(__rhs).count(); + } - static __rt - __divide(const duration<_Rep1, _Period1>& __lhs, - const duration<_Rep2, _Period2>& __rhs) - { return __ct(__lhs).count() / __ct(__rhs).count(); } - }; - - template - inline typename __division_impl, _Up>::__rt - operator/(const duration<_Rep, _Period>& __d, const _Up& __u) + // DR 934. + template + inline duration::value, _Rep2>::type>::type, _Period> + operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) { - return - __division_impl, _Up>::__divide(__d, __u); + typedef typename common_type<_Rep1, _Rep2>::type __cr; + return duration<__cr, _Period>(__d) %= __s; } - + + template + inline typename common_type, + duration<_Rep2, _Period2>>::type + operator%(const duration<_Rep1, _Period1>& __lhs, + const duration<_Rep2, _Period2>& __rhs) + { + typedef typename common_type, + duration<_Rep2, _Period2>>::type __ct; + return __ct(__lhs) %= __rhs; + } + // comparisons template diff --git a/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-1.cc b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-1.cc new file mode 100644 index 00000000000..decf94c884c --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-1.cc @@ -0,0 +1,48 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } + +// Copyright (C) 2009 Free Software Foundation +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +class ClockTime +{ + typedef std::chrono::hours hours; + typedef std::chrono::minutes minutes; + typedef std::chrono::seconds seconds; + +public: + hours hours_; + minutes minutes_; + seconds seconds_; + + template + explicit + ClockTime(const std::chrono::duration& d) + : hours_ (std::chrono::duration_cast (d)), + minutes_(std::chrono::duration_cast(d % hours(1))), + seconds_(std::chrono::duration_cast(d % minutes(1))) { } +}; + +// DR 934. +void test01() +{ + std::chrono::duration d; + ClockTime ct(d); +} diff --git a/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-2.cc b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-2.cc new file mode 100644 index 00000000000..ada8ba573e2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr934-2.cc @@ -0,0 +1,54 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } + +// Copyright (C) 2009 Free Software Foundation +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +// DR 934. +void +test01() +{ + bool test __attribute__((unused)) = true; + using namespace std::chrono; + + const duration d0(17); + duration d3(d0); + d3 %= 5; + VERIFY( d3.count() == 2 ); + + const duration d4(7); + duration d5(d0); + d5 %= d4; + VERIFY( d5.count() == 3 ); + + const duration d6 = d0 % 6; + VERIFY( d6.count() == 5 ); + + const duration d7(11); + const duration d8 = d0 % d7; + VERIFY( d8.count() == 6 ); +} + +int +main() +{ + test01(); + return 0; +} -- 2.11.0