clang 22.0.0git
UnsignedOrNone.h
Go to the documentation of this file.
1//===- UnsignedOrNone.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::UnsignedOrNone.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_UNSIGNED_OR_NONE_H
15#define LLVM_CLANG_BASIC_UNSIGNED_OR_NONE_H
16
17#include <cassert>
18#include <optional>
19
20namespace clang {
21
23 constexpr UnsignedOrNone(std::nullopt_t) : Rep(0) {}
24 UnsignedOrNone(unsigned Val) : Rep(Val + 1) { assert(operator bool()); }
25 UnsignedOrNone(int) = delete;
26
27 constexpr static UnsignedOrNone fromInternalRepresentation(unsigned Rep) {
28 return {std::nullopt, Rep};
29 }
30 constexpr unsigned toInternalRepresentation() const { return Rep; }
31
32 explicit constexpr operator bool() const { return Rep != 0; }
33 unsigned operator*() const {
34 assert(operator bool());
35 return Rep - 1;
36 }
37
38 friend constexpr bool operator==(UnsignedOrNone LHS, UnsignedOrNone RHS) {
39 return LHS.Rep == RHS.Rep;
40 }
41 friend constexpr bool operator!=(UnsignedOrNone LHS, UnsignedOrNone RHS) {
42 return LHS.Rep != RHS.Rep;
43 }
44
45private:
46 constexpr UnsignedOrNone(std::nullopt_t, unsigned Rep) : Rep(Rep) {};
47
48 unsigned Rep;
49};
50
51} // namespace clang
52
53#endif // LLVM_CLANG_BASIC_UNSIGNED_OR_NONE_H
#define bool
Definition: gpuintrin.h:32
The JSON file list parser is used to communicate input to InstallAPI.
friend constexpr bool operator!=(UnsignedOrNone LHS, UnsignedOrNone RHS)
UnsignedOrNone(unsigned Val)
unsigned operator*() const
UnsignedOrNone(int)=delete
constexpr unsigned toInternalRepresentation() const
static constexpr UnsignedOrNone fromInternalRepresentation(unsigned Rep)
constexpr UnsignedOrNone(std::nullopt_t)
friend constexpr bool operator==(UnsignedOrNone LHS, UnsignedOrNone RHS)