blob: 3f45113f63781d6b18255ca07d92abeb74473010 [file] [log] [blame]
Victor Boiviea8655192021-04-01 06:23:541/*
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 Boivieb9182302021-09-21 12:51:0511#ifndef RTC_BASE_STRONG_ALIAS_H_
12#define RTC_BASE_STRONG_ALIAS_H_
Victor Boiviea8655192021-04-01 06:23:5413
14#include <type_traits>
15#include <utility>
16
Victor Boivieb9182302021-09-21 12:51:0517namespace webrtc {
Victor Boiviea8655192021-04-01 06:23:5418
19// This is a copy of
Tony Herreb0ed1202021-07-22 15:40:4420// https://source.chromium.org/chromium/chromium/src/+/main:base/types/strong_alias.h
Victor Boiviea8655192021-04-01 06:23:5421// 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 Boivie83c726f2021-04-01 22:18:4823// per WebRTC library conventions, and the underlying type is exposed.
Victor Boiviea8655192021-04-01 06:23:5424
Victor Boivie83c726f2021-04-01 22:18:4825template <typename TagType, typename TheUnderlyingType>
Victor Boiviea8655192021-04-01 06:23:5426class StrongAlias {
27 public:
Victor Boivie83c726f2021-04-01 22:18:4828 using UnderlyingType = TheUnderlyingType;
Victor Boiviea8655192021-04-01 06:23:5429 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 Boiviea8655192021-04-01 06:23:5470 protected:
71 UnderlyingType value_;
72};
73
Victor Boivieb9182302021-09-21 12:51:0574} // namespace webrtc
Victor Boiviea8655192021-04-01 06:23:5475
Victor Boivieb9182302021-09-21 12:51:0576#endif // RTC_BASE_STRONG_ALIAS_H_