Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_DATA_SIZE_H_ |
| 12 | #define API_UNITS_DATA_SIZE_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame^] | 14 | #ifdef UNIT_TEST |
| 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 16 | #endif // UNIT_TEST |
| 17 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <cmath> |
| 20 | #include <limits> |
| 21 | #include <string> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 22 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 23 | |
| 24 | #include "rtc_base/checks.h" |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace data_size_impl { |
| 29 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 30 | } // namespace data_size_impl |
| 31 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 32 | // DataSize is a class represeting a count of bytes. |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 33 | class DataSize { |
| 34 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 35 | DataSize() = delete; |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 36 | static constexpr DataSize Zero() { return DataSize(0); } |
| 37 | static constexpr DataSize Infinity() { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 38 | return DataSize(data_size_impl::kPlusInfinityVal); |
| 39 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 40 | template <int64_t bytes> |
| 41 | static constexpr DataSize Bytes() { |
| 42 | static_assert(bytes >= 0, ""); |
| 43 | static_assert(bytes < data_size_impl::kPlusInfinityVal, ""); |
| 44 | return DataSize(bytes); |
| 45 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 46 | |
| 47 | template < |
| 48 | typename T, |
| 49 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 50 | static DataSize bytes(T bytes) { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 51 | RTC_DCHECK_GE(bytes, 0); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 52 | RTC_DCHECK_LT(bytes, data_size_impl::kPlusInfinityVal); |
| 53 | return DataSize(rtc::dchecked_cast<int64_t>(bytes)); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 54 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 55 | |
| 56 | template <typename T, |
| 57 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 58 | nullptr> |
| 59 | static DataSize bytes(T bytes) { |
| 60 | if (bytes == std::numeric_limits<T>::infinity()) { |
| 61 | return Infinity(); |
| 62 | } else { |
| 63 | RTC_DCHECK(!std::isnan(bytes)); |
| 64 | RTC_DCHECK_GE(bytes, 0); |
| 65 | RTC_DCHECK_LT(bytes, data_size_impl::kPlusInfinityVal); |
| 66 | return DataSize(rtc::dchecked_cast<int64_t>(bytes)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | template <typename T = int64_t> |
| 71 | typename std::enable_if<std::is_integral<T>::value, T>::type bytes() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 72 | RTC_DCHECK(IsFinite()); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 73 | return rtc::dchecked_cast<T>(bytes_); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 74 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 75 | |
| 76 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 77 | constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type |
| 78 | bytes() const { |
| 79 | return IsInfinite() ? std::numeric_limits<T>::infinity() : bytes_; |
| 80 | } |
| 81 | |
| 82 | constexpr int64_t bytes_or(int64_t fallback_value) const { |
| 83 | return IsFinite() ? bytes_ : fallback_value; |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 86 | constexpr bool IsZero() const { return bytes_ == 0; } |
| 87 | constexpr bool IsInfinite() const { |
| 88 | return bytes_ == data_size_impl::kPlusInfinityVal; |
| 89 | } |
| 90 | constexpr bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 91 | DataSize operator-(const DataSize& other) const { |
| 92 | return DataSize::bytes(bytes() - other.bytes()); |
| 93 | } |
| 94 | DataSize operator+(const DataSize& other) const { |
| 95 | return DataSize::bytes(bytes() + other.bytes()); |
| 96 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 97 | DataSize& operator-=(const DataSize& other) { |
| 98 | bytes_ -= other.bytes(); |
| 99 | return *this; |
| 100 | } |
| 101 | DataSize& operator+=(const DataSize& other) { |
| 102 | bytes_ += other.bytes(); |
| 103 | return *this; |
| 104 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 105 | constexpr double operator/(const DataSize& other) const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 106 | return bytes<double>() / other.bytes<double>(); |
| 107 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 108 | constexpr bool operator==(const DataSize& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 109 | return bytes_ == other.bytes_; |
| 110 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 111 | constexpr bool operator!=(const DataSize& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 112 | return bytes_ != other.bytes_; |
| 113 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 114 | constexpr bool operator<=(const DataSize& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 115 | return bytes_ <= other.bytes_; |
| 116 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 117 | constexpr bool operator>=(const DataSize& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 118 | return bytes_ >= other.bytes_; |
| 119 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 120 | constexpr bool operator>(const DataSize& other) const { |
| 121 | return bytes_ > other.bytes_; |
| 122 | } |
| 123 | constexpr bool operator<(const DataSize& other) const { |
| 124 | return bytes_ < other.bytes_; |
| 125 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 126 | |
| 127 | private: |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 128 | explicit constexpr DataSize(int64_t bytes) : bytes_(bytes) {} |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 129 | int64_t bytes_; |
| 130 | }; |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 131 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 132 | inline DataSize operator*(const DataSize& size, const double& scalar) { |
| 133 | return DataSize::bytes(std::round(size.bytes() * scalar)); |
| 134 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 135 | inline DataSize operator*(const double& scalar, const DataSize& size) { |
| 136 | return size * scalar; |
| 137 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 138 | inline DataSize operator*(const DataSize& size, const int64_t& scalar) { |
| 139 | return DataSize::bytes(size.bytes() * scalar); |
| 140 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 141 | inline DataSize operator*(const int64_t& scalar, const DataSize& size) { |
| 142 | return size * scalar; |
| 143 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 144 | inline DataSize operator*(const DataSize& size, const int32_t& scalar) { |
| 145 | return DataSize::bytes(size.bytes() * scalar); |
| 146 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 147 | inline DataSize operator*(const int32_t& scalar, const DataSize& size) { |
| 148 | return size * scalar; |
| 149 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 150 | inline DataSize operator/(const DataSize& size, const int64_t& scalar) { |
| 151 | return DataSize::bytes(size.bytes() / scalar); |
| 152 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 153 | |
| 154 | std::string ToString(const DataSize& value); |
| 155 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame^] | 156 | #ifdef UNIT_TEST |
| 157 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 158 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 159 | DataSize value) { |
| 160 | return stream << ToString(value); |
| 161 | } |
| 162 | #endif // UNIT_TEST |
| 163 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 164 | } // namespace webrtc |
| 165 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 166 | #endif // API_UNITS_DATA_SIZE_H_ |