Friendlier error messages from data unit classes.

By explicitly checking that the template argument is arithmetic, we
avoid exposing internal implementation details in the error message.

Bug: webrtc:9709
Change-Id: Ib1c4b46076af36fe0c4aead968487bb441d03b9a
Reviewed-on: https://webrtc-review.googlesource.com/c/112422
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25853}
This commit is contained in:
Sebastian Jansson
2018-11-30 10:02:44 +01:00
committed by Commit Bot
parent 286ee0123e
commit 0c3f4d3709
4 changed files with 11 additions and 4 deletions

View File

@ -52,10 +52,12 @@ class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
} }
template <typename T> template <typename T>
static constexpr DataRate bps(T bits_per_second) { static constexpr DataRate bps(T bits_per_second) {
static_assert(std::is_arithmetic<T>::value, "");
return FromValue(bits_per_second); return FromValue(bits_per_second);
} }
template <typename T> template <typename T>
static constexpr DataRate kbps(T kilobits_per_sec) { static constexpr DataRate kbps(T kilobits_per_sec) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(kilobits_per_sec); return FromFraction<1000>(kilobits_per_sec);
} }
template <typename T = int64_t> template <typename T = int64_t>

View File

@ -31,14 +31,13 @@ class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> {
return FromStaticValue<bytes>(); return FromStaticValue<bytes>();
} }
template < template <typename T>
typename T,
typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
static DataSize bytes(T bytes) { static DataSize bytes(T bytes) {
static_assert(std::is_arithmetic<T>::value, "");
return FromValue(bytes); return FromValue(bytes);
} }
template <typename T = int64_t> template <typename T = int64_t>
typename std::enable_if<std::is_arithmetic<T>::value, T>::type bytes() const { T bytes() const {
return ToValue<T>(); return ToValue<T>();
} }

View File

@ -46,14 +46,17 @@ class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
} }
template <typename T> template <typename T>
static TimeDelta seconds(T seconds) { static TimeDelta seconds(T seconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000000>(seconds); return FromFraction<1000000>(seconds);
} }
template <typename T> template <typename T>
static TimeDelta ms(T milliseconds) { static TimeDelta ms(T milliseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(milliseconds); return FromFraction<1000>(milliseconds);
} }
template <typename T> template <typename T>
static TimeDelta us(T microseconds) { static TimeDelta us(T microseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromValue(microseconds); return FromValue(microseconds);
} }
template <typename T = int64_t> template <typename T = int64_t>

View File

@ -45,14 +45,17 @@ class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> {
template <typename T> template <typename T>
static Timestamp seconds(T seconds) { static Timestamp seconds(T seconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000000>(seconds); return FromFraction<1000000>(seconds);
} }
template <typename T> template <typename T>
static Timestamp ms(T milliseconds) { static Timestamp ms(T milliseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromFraction<1000>(milliseconds); return FromFraction<1000>(milliseconds);
} }
template <typename T> template <typename T>
static Timestamp us(T microseconds) { static Timestamp us(T microseconds) {
static_assert(std::is_arithmetic<T>::value, "");
return FromValue(microseconds); return FromValue(microseconds);
} }
template <typename T = int64_t> template <typename T = int64_t>