bad_expected_access.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
  10. #define _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H
  11. #include <__config>
  12. #include <__exception/exception.h>
  13. #include <__utility/move.h>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_PUSH_MACROS
  18. #include <__undef_macros>
  19. #if _LIBCPP_STD_VER >= 20
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _Err>
  22. class bad_expected_access;
  23. template <>
  24. class bad_expected_access<void> : public exception {
  25. protected:
  26. _LIBCPP_HIDE_FROM_ABI bad_expected_access() noexcept = default;
  27. _LIBCPP_HIDE_FROM_ABI bad_expected_access(const bad_expected_access&) = default;
  28. _LIBCPP_HIDE_FROM_ABI bad_expected_access(bad_expected_access&&) = default;
  29. _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(const bad_expected_access&) = default;
  30. _LIBCPP_HIDE_FROM_ABI bad_expected_access& operator=(bad_expected_access&&) = default;
  31. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_expected_access() override = default;
  32. public:
  33. // The way this has been designed (by using a class template below) means that we'll already
  34. // have a profusion of these vtables in TUs, and the dynamic linker will already have a bunch
  35. // of work to do. So it is not worth hiding the <void> specialization in the dylib, given that
  36. // it adds deployment target restrictions.
  37. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const char* what() const noexcept override { return "bad access to std::expected"; }
  38. };
  39. template <class _Err>
  40. class bad_expected_access : public bad_expected_access<void> {
  41. public:
  42. _LIBCPP_HIDE_FROM_ABI explicit bad_expected_access(_Err __e) : __unex_(std::move(__e)) {}
  43. _LIBCPP_HIDE_FROM_ABI _Err& error() & noexcept { return __unex_; }
  44. _LIBCPP_HIDE_FROM_ABI const _Err& error() const& noexcept { return __unex_; }
  45. _LIBCPP_HIDE_FROM_ABI _Err&& error() && noexcept { return std::move(__unex_); }
  46. _LIBCPP_HIDE_FROM_ABI const _Err&& error() const&& noexcept { return std::move(__unex_); }
  47. private:
  48. _Err __unex_;
  49. };
  50. _LIBCPP_END_NAMESPACE_STD
  51. #endif // _LIBCPP_STD_VER >= 23
  52. _LIBCPP_POP_MACROS
  53. #endif // _LIBCPP___EXPECTED_BAD_EXPECTED_ACCESS_H