blob: b4cbb652f3c907c097196eb5f722e9643a19f319 [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
Sebastian Jansson2afd2812018-08-23 14:44:05 +020014#ifdef UNIT_TEST
15#include <ostream> // no-presubmit-check TODO(webrtc:8982)
16#endif // UNIT_TEST
17
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:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020027 DataSize() = delete;
Sebastian Jansson72bba622018-11-19 11:17:12 +010028 static constexpr DataSize Infinity() { return PlusInfinity(); }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020029 template <int64_t bytes>
30 static constexpr DataSize Bytes() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010031 return FromStaticValue<bytes>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020032 }
Sebastian Jansson942b3602018-05-30 15:47:44 +020033
34 template <
35 typename T,
Sebastian Jansson72bba622018-11-19 11:17:12 +010036 typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
Sebastian Jansson942b3602018-05-30 15:47:44 +020037 static DataSize bytes(T bytes) {
Sebastian Jansson72bba622018-11-19 11:17:12 +010038 return FromValue(bytes);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020039 }
Sebastian Jansson942b3602018-05-30 15:47:44 +020040 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010041 typename std::enable_if<std::is_arithmetic<T>::value, T>::type bytes() const {
42 return ToValue<T>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020043 }
44
45 constexpr int64_t bytes_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010046 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020047 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020048
49 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010050 friend class rtc_units_impl::UnitBase<DataSize>;
51 using RelativeUnit::RelativeUnit;
52 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020053};
Sebastian Jansson942b3602018-05-30 15:47:44 +020054
Sebastian Jansson72bba622018-11-19 11:17:12 +010055std::string ToString(DataSize value);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020056
Sebastian Jansson2afd2812018-08-23 14:44:05 +020057#ifdef UNIT_TEST
58inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
59 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
60 DataSize value) {
61 return stream << ToString(value);
62}
63#endif // UNIT_TEST
64
Sebastian Jansson30bd4032018-04-13 13:56:17 +020065} // namespace webrtc
66
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020067#endif // API_UNITS_DATA_SIZE_H_