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 api_internal {
18// Called when ToChecked is called on an empty Maybe.
20} // namespace api_internal
21
32template <class T>
34 public:
35 V8_INLINE bool IsNothing() const { return !has_value_; }
36 V8_INLINE bool IsJust() const { return has_value_; }
37
41 V8_INLINE T ToChecked() const { return FromJust(); }
42
47 V8_INLINE void Check() const {
49 }
50
55 V8_WARN_UNUSED_RESULT V8_INLINE bool To(T* out) const {
56 if (V8_LIKELY(IsJust())) *out = value_;
57 return IsJust();
58 }
59
64 V8_INLINE T FromJust() const& {
66 return value_;
67 }
68
75 return std::move(value_);
76 }
77
82 V8_INLINE T FromMaybe(const T& default_value) const {
83 return has_value_ ? value_ : default_value;
84 }
85
86 V8_INLINE bool operator==(const Maybe& other) const {
87 return (IsJust() == other.IsJust()) &&
88 (!IsJust() || FromJust() == other.FromJust());
89 }
90
91 V8_INLINE bool operator!=(const Maybe& other) const {
92 return !operator==(other);
93 }
94
95 private:
96 Maybe() : has_value_(false) {}
97 explicit Maybe(const T& t) : has_value_(true), value_(t) {}
98 explicit Maybe(T&& t) : has_value_(true), value_(std::move(t)) {}
99
100 bool has_value_;
101 T value_;
102
103 template <class U>
104 friend Maybe<U> Nothing();
105 template <class U>
106 friend Maybe<U> Just(const U& u);
107 template <class U, std::enable_if_t<!std::is_lvalue_reference_v<U>>*>
108 friend Maybe<U> Just(U&& u);
109};
110
111template <class T>
113 return Maybe<T>();
114}
115
116template <class T>
117inline Maybe<T> Just(const T& t) {
118 return Maybe<T>(t);
119}
120
121// Don't use forwarding references here but instead use two overloads.
122// Forwarding references only work when type deduction takes place, which is not
123// the case for callsites such as Just<Type>(t).
124template <class T, std::enable_if_t<!std::is_lvalue_reference_v<T>>* = nullptr>
125inline Maybe<T> Just(T&& t) {
126 return Maybe<T>(std::move(t));
127}
128
129// A template specialization of Maybe<T> for the case of T = void.
130template <>
131class Maybe<void> {
132 public:
133 V8_INLINE bool IsNothing() const { return !is_valid_; }
134 V8_INLINE bool IsJust() const { return is_valid_; }
135
136 V8_INLINE bool operator==(const Maybe& other) const {
137 return IsJust() == other.IsJust();
138 }
139
140 V8_INLINE bool operator!=(const Maybe& other) const {
141 return !operator==(other);
142 }
143
144 private:
145 struct JustTag {};
146
147 Maybe() : is_valid_(false) {}
148 explicit Maybe(JustTag) : is_valid_(true) {}
149
150 bool is_valid_;
151
152 template <class U>
153 friend Maybe<U> Nothing();
154 friend Maybe<void> JustVoid();
155};
156
158
159} // namespace v8
160
161#endif // INCLUDE_V8_MAYBE_H_
Definition: conditional-stack-allocated.h:19
bool operator==(const Maybe &other) const
Definition: v8-maybe.h:136
bool IsNothing() const
Definition: v8-maybe.h:133
bool operator!=(const Maybe &other) const
Definition: v8-maybe.h:140
bool IsJust() const
Definition: v8-maybe.h:134
Definition: v8-maybe.h:33
friend Maybe< U > Nothing()
Definition: v8-maybe.h:112
T FromMaybe(const T &default_value) const
Definition: v8-maybe.h:82
bool operator!=(const Maybe &other) const
Definition: v8-maybe.h:91
bool IsNothing() const
Definition: v8-maybe.h:35
T FromJust() const &
Definition: v8-maybe.h:64
friend Maybe< U > Just(U &&u)
T FromJust() &&
Definition: v8-maybe.h:73
void Check() const
Definition: v8-maybe.h:47
bool To(T *out) const
Definition: v8-maybe.h:55
friend Maybe< U > Just(const U &u)
bool IsJust() const
Definition: v8-maybe.h:36
bool operator==(const Maybe &other) const
Definition: v8-maybe.h:86
T ToChecked() const
Definition: v8-maybe.h:41
void FromJustIsNothing()
Definition: libplatform.h:15
Maybe< T > Nothing()
Definition: v8-maybe.h:112
Maybe< void > JustVoid()
Definition: v8-maybe.h:157
Maybe< T > Just(const T &t)
Definition: v8-maybe.h:117
#define V8_EXPORT
Definition: v8config.h:800
#define V8_INLINE
Definition: v8config.h:500
#define V8_LIKELY(condition)
Definition: v8config.h:661
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:671
#define V8_UNLIKELY(condition)
Definition: v8config.h:660