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