Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/expected.h
213799 views
//===-- Holds an expected or unexpected value -------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H1011#include "src/__support/macros/attributes.h"12#include "src/__support/macros/config.h"1314namespace LIBC_NAMESPACE_DECL {15namespace cpp {1617// This is used to hold an unexpected value so that a different constructor is18// selected.19template <class T> class unexpected {20T value;2122public:23LIBC_INLINE constexpr explicit unexpected(T value) : value(value) {}24LIBC_INLINE constexpr T error() { return value; }25};2627template <class T> explicit unexpected(T) -> unexpected<T>;2829template <class T, class E> class expected {30union {31T exp;32E unexp;33};34bool is_expected;3536public:37LIBC_INLINE constexpr expected(T exp) : exp(exp), is_expected(true) {}38LIBC_INLINE constexpr expected(unexpected<E> unexp)39: unexp(unexp.error()), is_expected(false) {}4041LIBC_INLINE constexpr bool has_value() const { return is_expected; }4243LIBC_INLINE constexpr T &value() { return exp; }44LIBC_INLINE constexpr E &error() { return unexp; }45LIBC_INLINE constexpr const T &value() const { return exp; }46LIBC_INLINE constexpr const E &error() const { return unexp; }4748LIBC_INLINE constexpr operator bool() const { return is_expected; }4950LIBC_INLINE constexpr T &operator*() { return exp; }51LIBC_INLINE constexpr const T &operator*() const { return exp; }52LIBC_INLINE constexpr T *operator->() { return &exp; }53LIBC_INLINE constexpr const T *operator->() const { return &exp; }54};5556} // namespace cpp57} // namespace LIBC_NAMESPACE_DECL5859#endif // LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H606162