//===- llvm/Support/ErrorOr.h - Error Smart Pointer -------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// /// Provides ErrorOr<T> smart pointer. /// //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_ERROROR_H #define LLVM_SUPPORT_ERROROR_H #include "llvm/Support/AlignOf.h" #include <cassert> #include <system_error> #include <type_traits> #include <utility> namespace llvm { /// Represents either an error or a value T. /// /// ErrorOr<T> is a pointer-like class that represents the result of an /// operation. The result is either an error, or a value of type T. This is /// designed to emulate the usage of returning a pointer where nullptr indicates /// failure. However instead of just knowing that the operation failed, we also /// have an error_code and optional user data that describes why it failed. /// /// It is used like the following. /// \code /// ErrorOr<Buffer> getBuffer(); /// /// auto buffer = getBuffer(); /// if (error_code ec = buffer.getError()) /// return ec; /// buffer->write("adena"); /// \endcode /// /// /// Implicit conversion to bool returns true if there is a usable value. The /// unary * and -> operators provide pointer like access to the value. Accessing /// the value when there is an error has undefined behavior. /// /// When T is a reference type the behavior is slightly different. The reference /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and /// there is special handling to make operator -> work as if T was not a /// reference. /// /// T cannot be a rvalue reference. template<class T> class ErrorOr { … }; template <class T, class E> std::enable_if_t<std::is_error_code_enum<E>::value || std::is_error_condition_enum<E>::value, bool> operator==(const ErrorOr<T> &Err, E Code) { … } } // end namespace llvm #endif // LLVM_SUPPORT_ERROROR_H