Loading...
Searching...
No Matches
v8-maybe.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_MAYBE_H_
6#define INCLUDE_V8_MAYBE_H_
7
8#include <type_traits>
9#include <utility>
10
11#include "cppgc/internal/conditional-stack-allocated.h" // NOLINT(build/include_directory)
12#include "v8-internal.h" // NOLINT(build/include_directory)
13#include "v8config.h" // NOLINT(build/include_directory)
14
15namespace v8 {
16
17namespace internal {
18struct NullMaybeType {};
19
21} // namespace internal
22
23namespace api_internal {
24// Called when ToChecked is called on an empty Maybe.
26} // namespace api_internal
27
38template <class T>
40 public:
41 constexpr Maybe() = default;
42
44
45 V8_INLINE bool IsNothing() const { return !has_value_; }
46 V8_INLINE bool IsJust() const { return has_value_; }
47
52 V8_INLINE bool IsEmpty() const { return IsNothing(); }
53
57 V8_INLINE T ToChecked() const { return FromJust(); }
58
63 V8_INLINE void Check() const {
65 }
66
71 V8_WARN_UNUSED_RESULT V8_INLINE bool To(T* out) const {
72 if (V8_LIKELY(IsJust())) *out = value_;
73 return IsJust();
74 }
75
82 if (V8_LIKELY(IsJust())) *out = std::move(value_);
83 return IsJust();
84 }
85
90 V8_INLINE T FromJust() const& {
92 return value_;
93 }
94
101 return std::move(value_);
102 }
103
108 V8_INLINE T FromMaybe(const T& default_value) const {
109 return has_value_ ? value_ : default_value;
110 }
111
112 V8_INLINE bool operator==(const Maybe& other) const {
113 return (IsJust() == other.IsJust()) &&
114 (!IsJust() || FromJust() == other.FromJust());
115 }
116
117 V8_INLINE bool operator!=(const Maybe& other) const {
118 return !operator==(other);
119 }
120
121 private:
122 explicit Maybe(const T& t) : has_value_(true), value_(t) {}
123 explicit Maybe(T&& t) : has_value_(true), value_(std::move(t)) {}
124
125 bool has_value_ = false;
126 T value_;
127
128 template <class U>
129 friend Maybe<U> Just(const U& u);
130 template <class U, std::enable_if_t<!std::is_lvalue_reference_v<U>>*>
131 friend Maybe<U> Just(U&& u);
132};
133
134template <class T>
135inline constexpr Maybe<T> Nothing() {
136 return {};
137}
138
139template <class T>
140inline Maybe<T> Just(const T& t) {
141 return Maybe<T>(t);
142}
143
144// Don't use forwarding references here but instead use two overloads.
145// Forwarding references only work when type deduction takes place, which is not
146// the case for callsites such as Just<Type>(t).
147template <class T, std::enable_if_t<!std::is_lvalue_reference_v<T>>* = nullptr>
148inline Maybe<T> Just(T&& t) {
149 return Maybe<T>(std::move(t));
150}
151
152// A template specialization of Maybe<T> for the case of T = void.
153template <>
154class Maybe<void> {
155 public:
156 constexpr Maybe() = default;
157
158 V8_INLINE bool IsNothing() const { return !is_valid_; }
159 V8_INLINE bool IsJust() const { return is_valid_; }
160
161 V8_INLINE bool operator==(const Maybe& other) const {
162 return IsJust() == other.IsJust();
163 }
164
165 V8_INLINE bool operator!=(const Maybe& other) const {
166 return !operator==(other);
167 }
168
169 private:
170 struct JustTag {};
171
172 explicit Maybe(JustTag) : is_valid_(true) {}
173
174 bool is_valid_ = false;
175
176 friend Maybe<void> JustVoid();
177};
178
180
181} // namespace v8
182
183#endif // INCLUDE_V8_MAYBE_H_
Definition: conditional-stack-allocated.h:19
bool operator==(const Maybe &other) const
Definition: v8-maybe.h:161
constexpr Maybe()=default
bool IsNothing() const
Definition: v8-maybe.h:158
bool operator!=(const Maybe &other) const
Definition: v8-maybe.h:165
bool IsJust() const
Definition: v8-maybe.h:159
Definition: v8-maybe.h:39
T FromMaybe(const T &default_value) const
Definition: v8-maybe.h:108
Maybe(internal::NullMaybeType)
Definition: v8-maybe.h:43
bool operator!=(const Maybe &other) const
Definition: v8-maybe.h:117
bool IsNothing() const
Definition: v8-maybe.h:45
constexpr Maybe()=default
T FromJust() const &
Definition: v8-maybe.h:90
friend Maybe< U > Just(U &&u)
T FromJust() &&
Definition: v8-maybe.h:99
bool IsEmpty() const
Definition: v8-maybe.h:52
bool MoveTo(T *out) &&
Definition: v8-maybe.h:81
void Check() const
Definition: v8-maybe.h:63
bool To(T *out) const
Definition: v8-maybe.h:71
friend Maybe< U > Just(const U &u)
bool IsJust() const
Definition: v8-maybe.h:46
bool operator==(const Maybe &other) const
Definition: v8-maybe.h:112
T ToChecked() const
Definition: v8-maybe.h:57
void FromJustIsNothing()
constexpr NullMaybeType kNullMaybe
Definition: v8-maybe.h:20
Definition: libplatform.h:15
constexpr Maybe< T > Nothing()
Definition: v8-maybe.h:135
Maybe< void > JustVoid()
Definition: v8-maybe.h:179
Maybe< T > Just(const T &t)
Definition: v8-maybe.h:140
Definition: v8-maybe.h:18
#define V8_EXPORT
Definition: v8config.h:849
#define V8_INLINE
Definition: v8config.h:513
#define V8_LIKELY(condition)
Definition: v8config.h:674
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:684
#define V8_UNLIKELY(condition)
Definition: v8config.h:673