data_size.h 1.8 KB

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