blob: 6817e24c2696507497d09452702ecc3de12b3a14 [file] [log] [blame]
Sebastian Jansson30bd4032018-04-13 13:56:17 +02001/*
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 Jansson6fae6ec2018-05-08 10:43:18 +020011#ifndef API_UNITS_DATA_SIZE_H_
12#define API_UNITS_DATA_SIZE_H_
Sebastian Jansson30bd4032018-04-13 13:56:17 +020013
Andrey Logvinb95d90b2020-12-09 12:49:39 +000014#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020015#include <ostream> // no-presubmit-check TODO(webrtc:8982)
Andrey Logvinb95d90b2020-12-09 12:49:39 +000016#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020017
Sebastian Jansson30bd4032018-04-13 13:56:17 +020018#include <string>
Sebastian Jansson942b3602018-05-30 15:47:44 +020019#include <type_traits>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020020
Sebastian Jansson72bba622018-11-19 11:17:12 +010021#include "rtc_base/units/unit_base.h"
Sebastian Jansson30bd4032018-04-13 13:56:17 +020022
23namespace webrtc {
Sebastian Jansson66fa5352018-04-30 16:54:57 +020024// DataSize is a class represeting a count of bytes.
Sebastian Jansson72bba622018-11-19 11:17:12 +010025class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020026 public:
Danil Chapovalove638ada2020-02-17 15:00:07 +010027 template <typename T>
28 static constexpr DataSize Bytes(T value) {
29 static_assert(std::is_arithmetic<T>::value, "");
30 return FromValue(value);
31 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010032 static constexpr DataSize Infinity() { return PlusInfinity(); }
Danil Chapovalove638ada2020-02-17 15:00:07 +010033
34 DataSize() = delete;
Sebastian Jansson942b3602018-05-30 15:47:44 +020035
Sebastian Jansson942b3602018-05-30 15:47:44 +020036 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010037 constexpr T bytes() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010038 return ToValue<T>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020039 }
40
41 constexpr int64_t bytes_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010042 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020043 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020044
45 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010046 friend class rtc_units_impl::UnitBase<DataSize>;
47 using RelativeUnit::RelativeUnit;
48 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020049};
Sebastian Jansson942b3602018-05-30 15:47:44 +020050
Sebastian Jansson72bba622018-11-19 11:17:12 +010051std::string ToString(DataSize value);
Sebastian Janssonb1138622019-04-11 16:48:15 +020052inline std::string ToLogString(DataSize value) {
53 return ToString(value);
54}
Sebastian Jansson30bd4032018-04-13 13:56:17 +020055
Andrey Logvinb95d90b2020-12-09 12:49:39 +000056#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020057inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
58 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
59 DataSize value) {
60 return stream << ToString(value);
61}
Andrey Logvinb95d90b2020-12-09 12:49:39 +000062#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020063
Sebastian Jansson30bd4032018-04-13 13:56:17 +020064} // namespace webrtc
65
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020066#endif // API_UNITS_DATA_SIZE_H_