blob: 74ab19e664e843edef6546c032bc6ac10556b788 [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
14#include <stdint.h>
15#include <cmath>
16#include <limits>
17#include <string>
18
19#include "rtc_base/checks.h"
20
21namespace webrtc {
22namespace data_size_impl {
23constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max();
Sebastian Jansson30bd4032018-04-13 13:56:17 +020024} // namespace data_size_impl
25
Sebastian Jansson66fa5352018-04-30 16:54:57 +020026// DataSize is a class represeting a count of bytes.
Sebastian Jansson30bd4032018-04-13 13:56:17 +020027class DataSize {
28 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020029 DataSize() = delete;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020030 static DataSize Zero() { return DataSize(0); }
31 static DataSize Infinity() {
32 return DataSize(data_size_impl::kPlusInfinityVal);
33 }
34 static DataSize bytes(int64_t bytes) {
35 RTC_DCHECK_GE(bytes, 0);
36 return DataSize(bytes);
37 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020038 int64_t bytes() const {
39 RTC_DCHECK(IsFinite());
40 return bytes_;
41 }
42 int64_t kilobytes() const { return (bytes() + 500) / 1000; }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020043 bool IsZero() const { return bytes_ == 0; }
44 bool IsInfinite() const { return bytes_ == data_size_impl::kPlusInfinityVal; }
Sebastian Jansson3b69b192018-05-07 13:51:51 +020045 bool IsFinite() const { return !IsInfinite(); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020046 DataSize operator-(const DataSize& other) const {
47 return DataSize::bytes(bytes() - other.bytes());
48 }
49 DataSize operator+(const DataSize& other) const {
50 return DataSize::bytes(bytes() + other.bytes());
51 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020052 DataSize& operator-=(const DataSize& other) {
53 bytes_ -= other.bytes();
54 return *this;
55 }
56 DataSize& operator+=(const DataSize& other) {
57 bytes_ += other.bytes();
58 return *this;
59 }
60 bool operator==(const DataSize& other) const {
61 return bytes_ == other.bytes_;
62 }
63 bool operator!=(const DataSize& other) const {
64 return bytes_ != other.bytes_;
65 }
66 bool operator<=(const DataSize& other) const {
67 return bytes_ <= other.bytes_;
68 }
69 bool operator>=(const DataSize& other) const {
70 return bytes_ >= other.bytes_;
71 }
72 bool operator>(const DataSize& other) const { return bytes_ > other.bytes_; }
73 bool operator<(const DataSize& other) const { return bytes_ < other.bytes_; }
74
75 private:
76 explicit DataSize(int64_t bytes) : bytes_(bytes) {}
77 int64_t bytes_;
78};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020079inline DataSize operator*(const DataSize& size, const double& scalar) {
80 return DataSize::bytes(std::round(size.bytes() * scalar));
81}
Sebastian Jansson30bd4032018-04-13 13:56:17 +020082inline DataSize operator*(const double& scalar, const DataSize& size) {
83 return size * scalar;
84}
Sebastian Jansson66fa5352018-04-30 16:54:57 +020085inline DataSize operator*(const DataSize& size, const int64_t& scalar) {
86 return DataSize::bytes(size.bytes() * scalar);
87}
Sebastian Jansson30bd4032018-04-13 13:56:17 +020088inline DataSize operator*(const int64_t& scalar, const DataSize& size) {
89 return size * scalar;
90}
Sebastian Jansson66fa5352018-04-30 16:54:57 +020091inline DataSize operator*(const DataSize& size, const int32_t& scalar) {
92 return DataSize::bytes(size.bytes() * scalar);
93}
Sebastian Jansson30bd4032018-04-13 13:56:17 +020094inline DataSize operator*(const int32_t& scalar, const DataSize& size) {
95 return size * scalar;
96}
Sebastian Jansson66fa5352018-04-30 16:54:57 +020097inline DataSize operator/(const DataSize& size, const int64_t& scalar) {
98 return DataSize::bytes(size.bytes() / scalar);
99}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200100
101std::string ToString(const DataSize& value);
102
103} // namespace webrtc
104
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200105#endif // API_UNITS_DATA_SIZE_H_