mini-llvm 0.1.0
Loading...
Searching...
No Matches
OptionalReference.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <cassert>
6#include <optional>
7#include <type_traits>
8#include <utility>
9
10namespace mini_llvm {
11
12template <typename T>
14 static_assert(std::is_object_v<T>);
15
16public:
17 using value_type = T;
18
19 constexpr OptionalReference() noexcept : value_(nullptr) {}
20
21 constexpr OptionalReference(std::nullopt_t) noexcept : value_(nullptr) {}
22
23 template <typename U>
24 requires std::is_convertible_v<U &, T &>
25 constexpr OptionalReference(U &value) noexcept : value_(&value) {}
26
27 constexpr OptionalReference(const OptionalReference &other) noexcept = default;
28
29 template <typename U>
30 requires std::is_convertible_v<U &, T &>
31 constexpr OptionalReference(const OptionalReference<U> &other) noexcept : value_(other.value_) {}
32
33 constexpr OptionalReference &operator=(const OptionalReference &other) noexcept = default;
34
35 explicit constexpr operator bool() const noexcept {
36 return value_ != nullptr;
37 }
38
39 constexpr bool has_value() const noexcept {
40 return value_ != nullptr;
41 }
42
43 constexpr T *operator->() const noexcept {
44 assert(value_ != nullptr);
45 return value_;
46 }
47
48 constexpr T &operator*() const noexcept {
49 assert(value_ != nullptr);
50 return *value_;
51 }
52
53 constexpr T &value() const {
54 if (!value_) {
55 throw std::bad_optional_access();
56 }
57 return *value_;
58 }
59
60 template <typename U = std::remove_cv_t<T>>
61 requires std::is_convertible_v<U &&, std::remove_cv_t<T>>
62 constexpr std::remove_cv_t<T> value_or(U &&defaultValue) const {
63 if (!value_) {
64 return static_cast<std::remove_cv_t<T>>(std::forward<U>(defaultValue));
65 }
66 return *value_;
67 }
68
69 constexpr void reset() noexcept {
70 value_ = nullptr;
71 }
72
73 constexpr void swap(OptionalReference &other) noexcept {
74 std::swap(value_, other.value_);
75 }
76
77private:
78 T *value_;
79
80 template <typename> friend class OptionalReference;
81};
82
83template <typename T>
85
86template <typename T>
88 lhs.swap(rhs);
89}
90
91} // namespace mini_llvm
Definition OptionalReference.h:13
constexpr void swap(OptionalReference &other) noexcept
Definition OptionalReference.h:73
constexpr bool has_value() const noexcept
Definition OptionalReference.h:39
constexpr T * operator->() const noexcept
Definition OptionalReference.h:43
constexpr std::remove_cv_t< T > value_or(U &&defaultValue) const
Definition OptionalReference.h:62
constexpr T & value() const
Definition OptionalReference.h:53
constexpr void reset() noexcept
Definition OptionalReference.h:69
friend class OptionalReference
Definition OptionalReference.h:80
T value_type
Definition OptionalReference.h:17
constexpr OptionalReference & operator=(const OptionalReference &other) noexcept=default
constexpr OptionalReference(std::nullopt_t) noexcept
Definition OptionalReference.h:21
constexpr OptionalReference(U &value) noexcept
Definition OptionalReference.h:25
constexpr OptionalReference(const OptionalReference< U > &other) noexcept
Definition OptionalReference.h:31
constexpr OptionalReference(const OptionalReference &other) noexcept=default
constexpr OptionalReference() noexcept
Definition OptionalReference.h:19
constexpr T & operator*() const noexcept
Definition OptionalReference.h:48
Definition GraphColoringAllocator.h:13
void swap(FileHandle &lhs, FileHandle &rhs) noexcept
Definition FileHandle.h:61
OptionalReference(T &) -> OptionalReference< T >