1*385f2b93SAndroid Build Coastguard Worker // Copyright 2022 The Chromium Authors. All rights reserved.
2*385f2b93SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*385f2b93SAndroid Build Coastguard Worker // found in the LICENSE file.
4*385f2b93SAndroid Build Coastguard Worker
5*385f2b93SAndroid Build Coastguard Worker #pragma once
6*385f2b93SAndroid Build Coastguard Worker
7*385f2b93SAndroid Build Coastguard Worker #include "base/time/time.h"
8*385f2b93SAndroid Build Coastguard Worker
9*385f2b93SAndroid Build Coastguard Worker // Android's external/libchrome directory is out of date.
10*385f2b93SAndroid Build Coastguard Worker // Add missing templates here as a temporary solution
11*385f2b93SAndroid Build Coastguard Worker namespace base {
12*385f2b93SAndroid Build Coastguard Worker
13*385f2b93SAndroid Build Coastguard Worker /**
14*385f2b93SAndroid Build Coastguard Worker * Workaround for the error in unit tests: ISO C++20 considers use of overloaded
15*385f2b93SAndroid Build Coastguard Worker * operator '==' (with operand types 'const base::TimeTicks'
16*385f2b93SAndroid Build Coastguard Worker * and 'const base::TimeTicks') to be ambiguous despite there being a unique
17*385f2b93SAndroid Build Coastguard Worker * best viable function [-Werror,-Wambiguous-reversed-operator]
18*385f2b93SAndroid Build Coastguard Worker */
19*385f2b93SAndroid Build Coastguard Worker bool operator==(const TimeTicks& t1, const TimeTicks& t2);
20*385f2b93SAndroid Build Coastguard Worker
21*385f2b93SAndroid Build Coastguard Worker namespace time_internal {
22*385f2b93SAndroid Build Coastguard Worker
23*385f2b93SAndroid Build Coastguard Worker // clang-format off
24*385f2b93SAndroid Build Coastguard Worker template <typename T>
25*385f2b93SAndroid Build Coastguard Worker using EnableIfIntegral = typename std::
26*385f2b93SAndroid Build Coastguard Worker enable_if<std::is_integral<T>::value || std::is_enum<T>::value, int>::type;
27*385f2b93SAndroid Build Coastguard Worker template <typename T>
28*385f2b93SAndroid Build Coastguard Worker using EnableIfFloat =
29*385f2b93SAndroid Build Coastguard Worker typename std::enable_if<std::is_floating_point<T>::value, int>::type;
30*385f2b93SAndroid Build Coastguard Worker
31*385f2b93SAndroid Build Coastguard Worker } // namespace time_internal
32*385f2b93SAndroid Build Coastguard Worker
33*385f2b93SAndroid Build Coastguard Worker
34*385f2b93SAndroid Build Coastguard Worker template <typename T, time_internal::EnableIfIntegral<T> = 0>
Seconds(T n)35*385f2b93SAndroid Build Coastguard Worker constexpr TimeDelta Seconds(T n) {
36*385f2b93SAndroid Build Coastguard Worker return TimeDelta::FromInternalValue(
37*385f2b93SAndroid Build Coastguard Worker ClampMul(static_cast<int64_t>(n), Time::kMicrosecondsPerSecond));
38*385f2b93SAndroid Build Coastguard Worker }
39*385f2b93SAndroid Build Coastguard Worker template <typename T, time_internal::EnableIfIntegral<T> = 0>
Milliseconds(T n)40*385f2b93SAndroid Build Coastguard Worker constexpr TimeDelta Milliseconds(T n) {
41*385f2b93SAndroid Build Coastguard Worker return TimeDelta::FromInternalValue(
42*385f2b93SAndroid Build Coastguard Worker ClampMul(static_cast<int64_t>(n), Time::kMicrosecondsPerMillisecond));
43*385f2b93SAndroid Build Coastguard Worker }
44*385f2b93SAndroid Build Coastguard Worker template <typename T, time_internal::EnableIfFloat<T> = 0>
Seconds(T n)45*385f2b93SAndroid Build Coastguard Worker constexpr TimeDelta Seconds(T n) {
46*385f2b93SAndroid Build Coastguard Worker return TimeDelta::FromInternalValue(
47*385f2b93SAndroid Build Coastguard Worker saturated_cast<int64_t>(n * Time::kMicrosecondsPerSecond));
48*385f2b93SAndroid Build Coastguard Worker }
49*385f2b93SAndroid Build Coastguard Worker template <typename T, time_internal::EnableIfFloat<T> = 0>
Milliseconds(T n)50*385f2b93SAndroid Build Coastguard Worker constexpr TimeDelta Milliseconds(T n) {
51*385f2b93SAndroid Build Coastguard Worker return TimeDelta::FromInternalValue(
52*385f2b93SAndroid Build Coastguard Worker saturated_cast<int64_t>(n * Time::kMicrosecondsPerMillisecond));
53*385f2b93SAndroid Build Coastguard Worker }
54*385f2b93SAndroid Build Coastguard Worker
55*385f2b93SAndroid Build Coastguard Worker } // namespace base
56