clang 23.0.0git
OptionalUnsigned.h
Go to the documentation of this file.
1//===- OptionalUnsigned.h - simple optional index-----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Defines clang::OptionalUnsigned.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
15#define LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
16
17#include <cassert>
18#include <llvm/ADT/STLForwardCompat.h>
19#include <optional>
20
21namespace clang {
22
23template <class T> struct OptionalUnsigned {
25 typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>,
26 llvm::type_identity<T>>::type;
27 static_assert(std::is_unsigned_v<underlying_type>);
28
29 constexpr OptionalUnsigned(std::nullopt_t) : Rep(0) {}
30 OptionalUnsigned(T Val) : Rep(static_cast<underlying_type>(Val) + 1) {
31 assert(has_value());
32 }
33 OptionalUnsigned(int) = delete;
34
35 constexpr static OptionalUnsigned
37 return {std::nullopt, Rep};
38 }
39 constexpr underlying_type toInternalRepresentation() const { return Rep; }
40
41 constexpr bool has_value() const { return Rep != 0; }
42
43 explicit constexpr operator bool() const { return has_value(); }
44 T operator*() const {
45 assert(has_value());
46 return static_cast<T>(Rep - 1);
47 }
48
49 T value_or(T Def) const { return has_value() ? operator*() : Def; }
50
51 friend constexpr bool operator==(OptionalUnsigned LHS, OptionalUnsigned RHS) {
52 return LHS && RHS ? *LHS == *RHS : bool(LHS) == bool(RHS);
53 }
54 friend constexpr bool operator!=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
55 return !(LHS == RHS);
56 }
57
58 friend constexpr bool operator<(OptionalUnsigned LHS, OptionalUnsigned RHS) {
59 return LHS != RHS && (!LHS || (RHS && *LHS < *RHS));
60 }
61 friend constexpr bool operator<=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
62 return LHS == RHS || LHS < RHS;
63 }
64 friend constexpr bool operator>=(OptionalUnsigned LHS, OptionalUnsigned RHS) {
65 return !(LHS < RHS);
66 }
67 friend constexpr bool operator>(OptionalUnsigned LHS, OptionalUnsigned RHS) {
68 return !(LHS <= RHS);
69 }
70
71private:
72 constexpr OptionalUnsigned(std::nullopt_t, underlying_type Rep) : Rep(Rep) {};
73
75};
76
78
79} // namespace clang
80
81#endif // LLVM_CLANG_BASIC_OPTIONAL_UNSIGNED_H
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
OptionalUnsigned< unsigned > UnsignedOrNone
friend constexpr bool operator>(OptionalUnsigned LHS, OptionalUnsigned RHS)
constexpr bool has_value() const
constexpr underlying_type toInternalRepresentation() const
friend constexpr bool operator!=(OptionalUnsigned LHS, OptionalUnsigned RHS)
static constexpr OptionalUnsigned fromInternalRepresentation(underlying_type Rep)
friend constexpr bool operator==(OptionalUnsigned LHS, OptionalUnsigned RHS)
typename std::conditional_t< std::is_enum_v< T >, std::underlying_type< T >, llvm::type_identity< T > >::type underlying_type
friend constexpr bool operator<(OptionalUnsigned LHS, OptionalUnsigned RHS)
friend constexpr bool operator>=(OptionalUnsigned LHS, OptionalUnsigned RHS)
constexpr OptionalUnsigned(std::nullopt_t)
friend constexpr bool operator<=(OptionalUnsigned LHS, OptionalUnsigned RHS)
OptionalUnsigned(int)=delete