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 #ifdef UNIT_TEST
15 #include <ostream>  // no-presubmit-check TODO(webrtc:8982)
16 #endif              // UNIT_TEST
17 
18 #include <string>
19 #include <type_traits>
20 
21 #include "rtc_base/units/unit_base.h"
22 
23 namespace webrtc {
24 // DataSize is a class represeting a count of bytes.
25 class DataSize final : public rtc_units_impl::RelativeUnit<DataSize> {
26  public:
27   template <typename T>
Bytes(T value)28   static constexpr DataSize Bytes(T value) {
29     static_assert(std::is_arithmetic<T>::value, "");
30     return FromValue(value);
31   }
Infinity()32   static constexpr DataSize Infinity() { return PlusInfinity(); }
33 
34   DataSize() = delete;
35 
36   template <typename T = int64_t>
bytes()37   constexpr T bytes() const {
38     return ToValue<T>();
39   }
40 
bytes_or(int64_t fallback_value)41   constexpr int64_t bytes_or(int64_t fallback_value) const {
42     return ToValueOr(fallback_value);
43   }
44 
45  private:
46   friend class rtc_units_impl::UnitBase<DataSize>;
47   using RelativeUnit::RelativeUnit;
48   static constexpr bool one_sided = true;
49 };
50 
51 std::string ToString(DataSize value);
ToLogString(DataSize value)52 inline std::string ToLogString(DataSize value) {
53   return ToString(value);
54 }
55 
56 #ifdef UNIT_TEST
57 inline 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 
64 }  // namespace webrtc
65 
66 #endif  // API_UNITS_DATA_SIZE_H_
67