Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 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 | */ |
Victor Boivie | b918230 | 2021-09-21 12:51:05 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRONG_ALIAS_H_ |
| 12 | #define RTC_BASE_STRONG_ALIAS_H_ |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 13 | |
| 14 | #include <type_traits> |
| 15 | #include <utility> |
| 16 | |
Victor Boivie | b918230 | 2021-09-21 12:51:05 | [diff] [blame] | 17 | namespace webrtc { |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 18 | |
| 19 | // This is a copy of |
Tony Herre | b0ed120 | 2021-07-22 15:40:44 | [diff] [blame] | 20 | // https://source.chromium.org/chromium/chromium/src/+/main:base/types/strong_alias.h |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 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 |
Victor Boivie | 83c726f | 2021-04-01 22:18:48 | [diff] [blame] | 23 | // per WebRTC library conventions, and the underlying type is exposed. |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 24 | |
Victor Boivie | 83c726f | 2021-04-01 22:18:48 | [diff] [blame] | 25 | template <typename TagType, typename TheUnderlyingType> |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 26 | class StrongAlias { |
| 27 | public: |
Victor Boivie | 83c726f | 2021-04-01 22:18:48 | [diff] [blame] | 28 | using UnderlyingType = TheUnderlyingType; |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 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 | |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 70 | protected: |
| 71 | UnderlyingType value_; |
| 72 | }; |
| 73 | |
Victor Boivie | b918230 | 2021-09-21 12:51:05 | [diff] [blame] | 74 | } // namespace webrtc |
Victor Boivie | a865519 | 2021-04-01 06:23:54 | [diff] [blame] | 75 | |
Victor Boivie | b918230 | 2021-09-21 12:51:05 | [diff] [blame] | 76 | #endif // RTC_BASE_STRONG_ALIAS_H_ |