v8 10.2.154 (node 18.16.0)
V8 is Google's open source JavaScript engine
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 "v8-internal.h" // NOLINT(build/include_directory)
9#include "v8config.h" // NOLINT(build/include_directory)
10
11namespace v8 {
12
13namespace api_internal {
14// Called when ToChecked is called on an empty Maybe.
16} // namespace api_internal
17
28template <class T>
29class Maybe {
30 public:
31 V8_INLINE bool IsNothing() const { return !has_value_; }
32 V8_INLINE bool IsJust() const { return has_value_; }
33
37 V8_INLINE T ToChecked() const { return FromJust(); }
38
43 V8_INLINE void Check() const {
45 }
46
51 V8_WARN_UNUSED_RESULT V8_INLINE bool To(T* out) const {
52 if (V8_LIKELY(IsJust())) *out = value_;
53 return IsJust();
54 }
55
60 V8_INLINE T FromJust() const {
62 return value_;
63 }
64
69 V8_INLINE T FromMaybe(const T& default_value) const {
70 return has_value_ ? value_ : default_value;
71 }
72
73 V8_INLINE bool operator==(const Maybe& other) const {
74 return (IsJust() == other.IsJust()) &&
75 (!IsJust() || FromJust() == other.FromJust());
76 }
77
78 V8_INLINE bool operator!=(const Maybe& other) const {
79 return !operator==(other);
80 }
81
82 private:
83 Maybe() : has_value_(false) {}
84 explicit Maybe(const T& t) : has_value_(true), value_(t) {}
85
86 bool has_value_;
87 T value_;
88
89 template <class U>
90 friend Maybe<U> Nothing();
91 template <class U>
92 friend Maybe<U> Just(const U& u);
93};
94
95template <class T>
96inline Maybe<T> Nothing() {
97 return Maybe<T>();
98}
99
100template <class T>
101inline Maybe<T> Just(const T& t) {
102 return Maybe<T>(t);
103}
104
105// A template specialization of Maybe<T> for the case of T = void.
106template <>
107class Maybe<void> {
108 public:
109 V8_INLINE bool IsNothing() const { return !is_valid_; }
110 V8_INLINE bool IsJust() const { return is_valid_; }
111
112 V8_INLINE bool operator==(const Maybe& other) const {
113 return IsJust() == other.IsJust();
114 }
115
116 V8_INLINE bool operator!=(const Maybe& other) const {
117 return !operator==(other);
118 }
119
120 private:
121 struct JustTag {};
122
123 Maybe() : is_valid_(false) {}
124 explicit Maybe(JustTag) : is_valid_(true) {}
125
126 bool is_valid_;
127
128 template <class U>
129 friend Maybe<U> Nothing();
130 friend Maybe<void> JustVoid();
131};
132
134
135} // namespace v8
136
137#endif // INCLUDE_V8_MAYBE_H_
V8_INLINE bool IsNothing() const
Definition v8-maybe.h:109
V8_INLINE bool operator!=(const Maybe &other) const
Definition v8-maybe.h:116
V8_INLINE bool operator==(const Maybe &other) const
Definition v8-maybe.h:112
V8_INLINE bool IsJust() const
Definition v8-maybe.h:110
friend Maybe< U > Nothing()
Definition v8-maybe.h:96
V8_INLINE bool IsNothing() const
Definition v8-maybe.h:31
V8_INLINE bool operator!=(const Maybe &other) const
Definition v8-maybe.h:78
V8_WARN_UNUSED_RESULT V8_INLINE bool To(T *out) const
Definition v8-maybe.h:51
V8_INLINE void Check() const
Definition v8-maybe.h:43
friend Maybe< U > Just(const U &u)
V8_INLINE bool operator==(const Maybe &other) const
Definition v8-maybe.h:73
V8_INLINE T FromJust() const
Definition v8-maybe.h:60
V8_INLINE bool IsJust() const
Definition v8-maybe.h:32
V8_INLINE T ToChecked() const
Definition v8-maybe.h:37
V8_INLINE T FromMaybe(const T &default_value) const
Definition v8-maybe.h:69
V8_EXPORT void FromJustIsNothing()
Maybe< void > JustVoid()
Definition v8-maybe.h:133
Maybe< T > Just(const T &t)
Definition v8-maybe.h:101
Maybe< T > Nothing()
Definition v8-maybe.h:96
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425
#define V8_LIKELY(condition)
Definition v8config.h:489
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:499
#define V8_UNLIKELY(condition)
Definition v8config.h:488