xref: /llvm-project/libc/src/__support/error_or.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
19beb8d11SMichael Jones //===-- A data structure for returning an error or a value ------*- C++ -*-===//
29beb8d11SMichael Jones //
39beb8d11SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49beb8d11SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
59beb8d11SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69beb8d11SMichael Jones //
79beb8d11SMichael Jones //===----------------------------------------------------------------------===//
89beb8d11SMichael Jones 
9270547f3SGuillaume Chatelet #ifndef LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H
10270547f3SGuillaume Chatelet #define LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H
119beb8d11SMichael Jones 
129beb8d11SMichael Jones #include "src/__support/CPP/expected.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
149beb8d11SMichael Jones 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
169beb8d11SMichael Jones 
179beb8d11SMichael Jones template <class T> using ErrorOr = cpp::expected<T, int>;
189beb8d11SMichael Jones 
199beb8d11SMichael Jones using Error = cpp::unexpected<int>;
209beb8d11SMichael Jones 
219beb8d11SMichael Jones // template <typename T> struct ErrorOr {
229beb8d11SMichael Jones //   union {
239beb8d11SMichael Jones //     T value;
249beb8d11SMichael Jones //     int error;
259beb8d11SMichael Jones //   };
269beb8d11SMichael Jones //   bool is_error;
279beb8d11SMichael Jones 
289beb8d11SMichael Jones //   constexpr ErrorOr(T value) : value(value), is_error(false) {}
299beb8d11SMichael Jones //   constexpr ErrorOr(int error, bool is_error)
309beb8d11SMichael Jones //       : error(error), is_error(is_error) {}
319beb8d11SMichael Jones 
329beb8d11SMichael Jones //   constexpr bool has_error() { return is_error; }
339beb8d11SMichael Jones 
349beb8d11SMichael Jones //   constexpr operator bool() { return is_error; }
359beb8d11SMichael Jones //   constexpr operator T() { return value; }
369beb8d11SMichael Jones // };
379beb8d11SMichael Jones 
38*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
399beb8d11SMichael Jones 
40270547f3SGuillaume Chatelet #endif // LLVM_LIBC_SRC___SUPPORT_ERROR_OR_H
41