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 | |
| 14 | #include <stdint.h> |
| 15 | #include <cmath> |
| 16 | #include <limits> |
| 17 | #include <string> |
| 18 | |
| 19 | #include "rtc_base/checks.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace data_size_impl { |
| 23 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 24 | } // namespace data_size_impl |
| 25 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 26 | // DataSize is a class represeting a count of bytes. |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 27 | class DataSize { |
| 28 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 29 | DataSize() = delete; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 30 | static DataSize Zero() { return DataSize(0); } |
| 31 | static DataSize Infinity() { |
| 32 | return DataSize(data_size_impl::kPlusInfinityVal); |
| 33 | } |
| 34 | static DataSize bytes(int64_t bytes) { |
| 35 | RTC_DCHECK_GE(bytes, 0); |
| 36 | return DataSize(bytes); |
| 37 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 38 | int64_t bytes() const { |
| 39 | RTC_DCHECK(IsFinite()); |
| 40 | return bytes_; |
| 41 | } |
| 42 | int64_t kilobytes() const { return (bytes() + 500) / 1000; } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 43 | bool IsZero() const { return bytes_ == 0; } |
| 44 | bool IsInfinite() const { return bytes_ == data_size_impl::kPlusInfinityVal; } |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 45 | bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 46 | DataSize operator-(const DataSize& other) const { |
| 47 | return DataSize::bytes(bytes() - other.bytes()); |
| 48 | } |
| 49 | DataSize operator+(const DataSize& other) const { |
| 50 | return DataSize::bytes(bytes() + other.bytes()); |
| 51 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 52 | DataSize& operator-=(const DataSize& other) { |
| 53 | bytes_ -= other.bytes(); |
| 54 | return *this; |
| 55 | } |
| 56 | DataSize& operator+=(const DataSize& other) { |
| 57 | bytes_ += other.bytes(); |
| 58 | return *this; |
| 59 | } |
| 60 | bool operator==(const DataSize& other) const { |
| 61 | return bytes_ == other.bytes_; |
| 62 | } |
| 63 | bool operator!=(const DataSize& other) const { |
| 64 | return bytes_ != other.bytes_; |
| 65 | } |
| 66 | bool operator<=(const DataSize& other) const { |
| 67 | return bytes_ <= other.bytes_; |
| 68 | } |
| 69 | bool operator>=(const DataSize& other) const { |
| 70 | return bytes_ >= other.bytes_; |
| 71 | } |
| 72 | bool operator>(const DataSize& other) const { return bytes_ > other.bytes_; } |
| 73 | bool operator<(const DataSize& other) const { return bytes_ < other.bytes_; } |
| 74 | |
| 75 | private: |
| 76 | explicit DataSize(int64_t bytes) : bytes_(bytes) {} |
| 77 | int64_t bytes_; |
| 78 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 79 | inline DataSize operator*(const DataSize& size, const double& scalar) { |
| 80 | return DataSize::bytes(std::round(size.bytes() * scalar)); |
| 81 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 82 | inline DataSize operator*(const double& scalar, const DataSize& size) { |
| 83 | return size * scalar; |
| 84 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 85 | inline DataSize operator*(const DataSize& size, const int64_t& scalar) { |
| 86 | return DataSize::bytes(size.bytes() * scalar); |
| 87 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 88 | inline DataSize operator*(const int64_t& scalar, const DataSize& size) { |
| 89 | return size * scalar; |
| 90 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 91 | inline DataSize operator*(const DataSize& size, const int32_t& scalar) { |
| 92 | return DataSize::bytes(size.bytes() * scalar); |
| 93 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 94 | inline DataSize operator*(const int32_t& scalar, const DataSize& size) { |
| 95 | return size * scalar; |
| 96 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 97 | inline DataSize operator/(const DataSize& size, const int64_t& scalar) { |
| 98 | return DataSize::bytes(size.bytes() / scalar); |
| 99 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 100 | |
| 101 | std::string ToString(const DataSize& value); |
| 102 | |
| 103 | } // namespace webrtc |
| 104 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 105 | #endif // API_UNITS_DATA_SIZE_H_ |