/* * Copyright 2018 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef RTC_BASE_UNITS_UNIT_BASE_H_ #define RTC_BASE_UNITS_UNIT_BASE_H_ #include <stdint.h> #include <algorithm> #include <cmath> #include <limits> #include <type_traits> #include "rtc_base/checks.h" #include "rtc_base/numerics/divide_round.h" #include "rtc_base/numerics/safe_conversions.h" namespace webrtc { namespace rtc_units_impl { // UnitBase is a base class for implementing custom value types with a specific // unit. It provides type safety and commonly useful operations. The underlying // storage is always an int64_t, it's up to the unit implementation to choose // what scale it represents. // // It's used like: // class MyUnit: public UnitBase<MyUnit> {...}; // // Unit_T is the subclass representing the specific unit. template <class Unit_T> class UnitBase { … }; // Extends UnitBase to provide operations for relative units, that is, units // that have a meaningful relation between values such that a += b is a // sensible thing to do. For a,b <- same unit. template <class Unit_T> class RelativeUnit : public UnitBase<Unit_T> { … }; template <class Unit_T> inline constexpr Unit_T operator*(double scalar, RelativeUnit<Unit_T> other) { … } template <class Unit_T> inline constexpr Unit_T operator*(int64_t scalar, RelativeUnit<Unit_T> other) { … } template <class Unit_T> inline constexpr Unit_T operator*(int32_t scalar, RelativeUnit<Unit_T> other) { … } template <class Unit_T> inline constexpr Unit_T operator*(size_t scalar, RelativeUnit<Unit_T> other) { … } template <class Unit_T> inline constexpr Unit_T operator-(RelativeUnit<Unit_T> other) { … } } // namespace rtc_units_impl } // namespace webrtc #endif // RTC_BASE_UNITS_UNIT_BASE_H_