| 6 | * that can be found in the LICENSE file in the root of the source |
| 7 | * tree. An additional intellectual property rights grant can be found |
| 8 | * in the file PATENTS. All contributing project authors may |
| 9 | * be found in the AUTHORS file in the root of the source tree. |
| 10 | */ |
| 11 | #ifndef RTC_BASE_STRONG_ALIAS_H_ |
| 12 | #define RTC_BASE_STRONG_ALIAS_H_ |
| 13 |
| 14 | #include <type_traits> |
| 15 | #include <utility> |
| 16 |
| 17 | namespace webrtc { |
| 18 |
| 19 | // This is a copy of |
| 20 | // https://source.chromium.org/chromium/chromium/src/+/main:base/types/strong_alias.h |
| 21 | // as the API (and internals) are using type-safe integral identifiers, but this |
| 22 | // library can't depend on that file. The ostream operator has been removed |
| 23 | // per WebRTC library conventions, and the underlying type is exposed. |
| 24 |
| 25 | template <typename TagType, typename TheUnderlyingType> |
| 26 | class StrongAlias { |
| 27 | public: |
| 28 | using UnderlyingType = TheUnderlyingType; |
| 29 | constexpr StrongAlias() = default; |
| 30 | constexpr explicit StrongAlias(const UnderlyingType& v) : value_(v) {} |
| 31 | constexpr explicit StrongAlias(UnderlyingType&& v) noexcept |
| 32 | : value_(std::move(v)) {} |
| 33 |
| 34 | constexpr UnderlyingType* operator->() { return &value_; } |
| 35 | constexpr const UnderlyingType* operator->() const { return &value_; } |
| 36 |
| 37 | constexpr UnderlyingType& operator*() & { return value_; } |
| 38 | constexpr const UnderlyingType& operator*() const& { return value_; } |
| 39 | constexpr UnderlyingType&& operator*() && { return std::move(value_); } |
| 40 | constexpr const UnderlyingType&& operator*() const&& { |
| 41 | return std::move(value_); |
| 42 | } |
| 43 |
| 44 | constexpr UnderlyingType& value() & { return value_; } |
| 45 | constexpr const UnderlyingType& value() const& { return value_; } |
| 46 | constexpr UnderlyingType&& value() && { return std::move(value_); } |
| 47 | constexpr const UnderlyingType&& value() const&& { return std::move(value_); } |
| 48 |
| 49 | constexpr explicit operator const UnderlyingType&() const& { return value_; } |
| 50 |
| 51 | constexpr bool operator==(const StrongAlias& other) const { |
| 52 | return value_ == other.value_; |
| 53 | } |
| 54 | constexpr bool operator!=(const StrongAlias& other) const { |
| 55 | return value_ != other.value_; |
| 56 | } |
| 57 | constexpr bool operator<(const StrongAlias& other) const { |
| 58 | return value_ < other.value_; |
| 59 | } |
| 60 | constexpr bool operator<=(const StrongAlias& other) const { |
| 61 | return value_ <= other.value_; |
| 62 | } |
| 63 | constexpr bool operator>(const StrongAlias& other) const { |
| 64 | return value_ > other.value_; |
| 65 | } |
| 66 | constexpr bool operator>=(const StrongAlias& other) const { |
| 67 | return value_ >= other.value_; |
| 68 | } |
| 69 |
| 70 | protected: |
| 71 | UnderlyingType value_; |
| 72 | }; |
| 73 |
| 74 | } // namespace webrtc |
| 75 |
| 76 | #endif // RTC_BASE_STRONG_ALIAS_H_ |
| 77 |