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 <string> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 19 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 20 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 21 | #include "rtc_base/units/unit_base.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 24 | // DataSize is a class represeting a count of bytes. |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 25 | class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 27 | DataSize() = delete; |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 28 | static constexpr DataSize Infinity() { return PlusInfinity(); } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 29 | template <int64_t bytes> |
| 30 | static constexpr DataSize Bytes() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 31 | return FromStaticValue<bytes>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 32 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 33 | |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 34 | template <typename T> |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 35 | static DataSize bytes(T bytes) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 36 | static_assert(std::is_arithmetic<T>::value, ""); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 37 | return FromValue(bytes); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 38 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 39 | template <typename T = int64_t> |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 40 | T bytes() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 41 | return ToValue<T>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | constexpr int64_t bytes_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 45 | return ToValueOr(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 46 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 47 | |
| 48 | private: |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 49 | friend class rtc_units_impl::UnitBase<DataSize>; |
| 50 | using RelativeUnit::RelativeUnit; |
| 51 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 52 | }; |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 53 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 54 | std::string ToString(DataSize value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 55 | inline std::string ToLogString(DataSize value) { |
| 56 | return ToString(value); |
| 57 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 58 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 59 | #ifdef UNIT_TEST |
| 60 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 61 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 62 | DataSize value) { |
| 63 | return stream << ToString(value); |
| 64 | } |
| 65 | #endif // UNIT_TEST |
| 66 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 67 | } // namespace webrtc |
| 68 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 69 | #endif // API_UNITS_DATA_SIZE_H_ |