MCPcopy Create free account
hub / github.com/nodejs/nan / Maybe

Class Maybe

nan.h:242–293  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

240
241template<typename T>
242class Maybe {
243 public:
244 inline bool IsNothing() const { return !has_value_; }
245 inline bool IsJust() const { return has_value_; }
246
247 inline T ToChecked() const { return FromJust(); }
248 inline void Check() const { FromJust(); }
249
250 inline bool To(T* out) const {
251 if (IsJust()) *out = value_;
252 return IsJust();
253 }
254
255 inline T FromJust() const {
256#if defined(V8_ENABLE_CHECKS)
257 assert(IsJust() && "FromJust is Nothing");
258#endif // V8_ENABLE_CHECKS
259 return value_;
260 }
261
262 inline T FromMaybe(const T& default_value) const {
263 return has_value_ ? value_ : default_value;
264 }
265
266 inline bool operator==(const Maybe &other) const {
267 return (IsJust() == other.IsJust()) &&
268 (!IsJust() || FromJust() == other.FromJust());
269 }
270
271 inline bool operator!=(const Maybe &other) const {
272 return !operator==(other);
273 }
274
275#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
276 (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
277 // Allow implicit conversions from v8::Maybe<T> to Nan::Maybe<T>.
278 Maybe(const v8::Maybe<T>& that) // NOLINT(runtime/explicit)
279 : has_value_(that.IsJust())
280 , value_(that.FromMaybe(T())) {}
281#endif
282
283 private:
284 Maybe() : has_value_(false) {}
285 explicit Maybe(const T& t) : has_value_(true), value_(t) {}
286 bool has_value_;
287 T value_;
288
289 template<typename U>
290 friend Maybe<U> Nothing();
291 template<typename U>
292 friend Maybe<U> Just(const U& u);
293};
294
295template<typename T>
296inline Maybe<T> Nothing() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected