data_size.h (1693B)
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 11 #ifndef API_UNITS_DATA_SIZE_H_ 12 #define API_UNITS_DATA_SIZE_H_ 13 14 #include <cstdint> 15 #include <string> 16 #include <type_traits> 17 18 #include "rtc_base/system/rtc_export.h" 19 #include "rtc_base/units/unit_base.h" // IWYU pragma: export 20 21 namespace webrtc { 22 // DataSize is a class represeting a count of bytes. 23 class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> { 24 public: 25 template <typename T> 26 static constexpr DataSize Bytes(T value) { 27 static_assert(std::is_arithmetic<T>::value, ""); 28 return FromValue(value); 29 } 30 static constexpr DataSize Infinity() { return PlusInfinity(); } 31 32 constexpr DataSize() = default; 33 34 template <typename Sink> 35 friend void AbslStringify(Sink& sink, DataSize value); 36 37 template <typename T = int64_t> 38 constexpr T bytes() const { 39 return ToValue<T>(); 40 } 41 42 constexpr int64_t bytes_or(int64_t fallback_value) const { 43 return ToValueOr(fallback_value); 44 } 45 46 private: 47 friend class rtc_units_impl::UnitBase<DataSize>; 48 using RelativeUnit::RelativeUnit; 49 static constexpr bool one_sided = true; 50 }; 51 52 RTC_EXPORT std::string ToString(DataSize value); 53 54 template <typename Sink> 55 void AbslStringify(Sink& sink, DataSize value) { 56 sink.Append(ToString(value)); 57 } 58 59 } // namespace webrtc 60 61 #endif // API_UNITS_DATA_SIZE_H_