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