Add conversions to and from double for units.
Bug: webrtc:8415 Change-Id: I6b1f7afb163daa327e45c51f1a3fb7cafbb1444e Reviewed-on: https://webrtc-review.googlesource.com/78183 Commit-Queue: Sebastian Jansson <srte@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23451}
This commit is contained in:
committed by
Commit Bot
parent
f859e55d9b
commit
942b360d82
@ -15,8 +15,10 @@
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/numerics/safe_conversions.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace data_size_impl {
|
||||
@ -31,15 +33,46 @@ class DataSize {
|
||||
static DataSize Infinity() {
|
||||
return DataSize(data_size_impl::kPlusInfinityVal);
|
||||
}
|
||||
static DataSize bytes(int64_t bytes) {
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
|
||||
static DataSize bytes(T bytes) {
|
||||
RTC_DCHECK_GE(bytes, 0);
|
||||
return DataSize(bytes);
|
||||
RTC_DCHECK_LT(bytes, data_size_impl::kPlusInfinityVal);
|
||||
return DataSize(rtc::dchecked_cast<int64_t>(bytes));
|
||||
}
|
||||
int64_t bytes() const {
|
||||
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_floating_point<T>::value>::type* =
|
||||
nullptr>
|
||||
static DataSize bytes(T bytes) {
|
||||
if (bytes == std::numeric_limits<T>::infinity()) {
|
||||
return Infinity();
|
||||
} else {
|
||||
RTC_DCHECK(!std::isnan(bytes));
|
||||
RTC_DCHECK_GE(bytes, 0);
|
||||
RTC_DCHECK_LT(bytes, data_size_impl::kPlusInfinityVal);
|
||||
return DataSize(rtc::dchecked_cast<int64_t>(bytes));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T = int64_t>
|
||||
typename std::enable_if<std::is_integral<T>::value, T>::type bytes() const {
|
||||
RTC_DCHECK(IsFinite());
|
||||
return bytes_;
|
||||
return rtc::dchecked_cast<T>(bytes_);
|
||||
}
|
||||
int64_t kilobytes() const { return (bytes() + 500) / 1000; }
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, T>::type bytes()
|
||||
const {
|
||||
if (IsInfinite()) {
|
||||
return std::numeric_limits<T>::infinity();
|
||||
} else {
|
||||
return bytes_;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsZero() const { return bytes_ == 0; }
|
||||
bool IsInfinite() const { return bytes_ == data_size_impl::kPlusInfinityVal; }
|
||||
bool IsFinite() const { return !IsInfinite(); }
|
||||
@ -57,6 +90,9 @@ class DataSize {
|
||||
bytes_ += other.bytes();
|
||||
return *this;
|
||||
}
|
||||
double operator/(const DataSize& other) const {
|
||||
return bytes<double>() / other.bytes<double>();
|
||||
}
|
||||
bool operator==(const DataSize& other) const {
|
||||
return bytes_ == other.bytes_;
|
||||
}
|
||||
@ -76,6 +112,7 @@ class DataSize {
|
||||
explicit DataSize(int64_t bytes) : bytes_(bytes) {}
|
||||
int64_t bytes_;
|
||||
};
|
||||
|
||||
inline DataSize operator*(const DataSize& size, const double& scalar) {
|
||||
return DataSize::bytes(std::round(size.bytes() * scalar));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user