blob: 90ef00da52a38d6c66fdd6b566151c999fdb85fb [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
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010034 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020035 static DataSize bytes(T bytes) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010036 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010037 return FromValue(bytes);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020038 }
Sebastian Jansson942b3602018-05-30 15:47:44 +020039 template <typename T = int64_t>
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010040 T bytes() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010041 return ToValue<T>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020042 }
43
44 constexpr int64_t bytes_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010045 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020046 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020047
48 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010049 friend class rtc_units_impl::UnitBase<DataSize>;
50 using RelativeUnit::RelativeUnit;
51 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020052};
Sebastian Jansson942b3602018-05-30 15:47:44 +020053
Sebastian Jansson72bba622018-11-19 11:17:12 +010054std::string ToString(DataSize value);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020055
Sebastian Jansson2afd2812018-08-23 14:44:05 +020056#ifdef UNIT_TEST
57inline 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}
62#endif // UNIT_TEST
63
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_