1 /* 2 * Copyright 2019 The Chromium Authors. All rights reserved. 3 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 4 * 5 * Use of this source code is governed by a BSD-style license 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; StrongAlias(const UnderlyingType & v)30 constexpr explicit StrongAlias(const UnderlyingType& v) : value_(v) {} StrongAlias(UnderlyingType && 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 value()44 constexpr UnderlyingType& value() & { return value_; } value()45 constexpr const UnderlyingType& value() const& { return value_; } value()46 constexpr UnderlyingType&& value() && { return std::move(value_); } 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