filesystem_error.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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___FILESYSTEM_FILESYSTEM_ERROR_H
  10. #define _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
  11. #include <__availability>
  12. #include <__config>
  13. #include <__filesystem/path.h>
  14. #include <__memory/shared_ptr.h>
  15. #include <iosfwd>
  16. #include <new>
  17. #include <system_error>
  18. #include <type_traits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. #ifndef _LIBCPP_CXX03_LANG
  23. _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
  24. class _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_EXCEPTION_ABI filesystem_error : public system_error {
  25. public:
  26. _LIBCPP_INLINE_VISIBILITY
  27. filesystem_error(const string& __what, error_code __ec)
  28. : system_error(__ec, __what),
  29. __storage_(make_shared<_Storage>(path(), path())) {
  30. __create_what(0);
  31. }
  32. _LIBCPP_INLINE_VISIBILITY
  33. filesystem_error(const string& __what, const path& __p1, error_code __ec)
  34. : system_error(__ec, __what),
  35. __storage_(make_shared<_Storage>(__p1, path())) {
  36. __create_what(1);
  37. }
  38. _LIBCPP_INLINE_VISIBILITY
  39. filesystem_error(const string& __what, const path& __p1, const path& __p2,
  40. error_code __ec)
  41. : system_error(__ec, __what),
  42. __storage_(make_shared<_Storage>(__p1, __p2)) {
  43. __create_what(2);
  44. }
  45. _LIBCPP_INLINE_VISIBILITY
  46. const path& path1() const noexcept { return __storage_->__p1_; }
  47. _LIBCPP_INLINE_VISIBILITY
  48. const path& path2() const noexcept { return __storage_->__p2_; }
  49. filesystem_error(const filesystem_error&) = default;
  50. ~filesystem_error() override; // key function
  51. _LIBCPP_INLINE_VISIBILITY
  52. const char* what() const noexcept override {
  53. return __storage_->__what_.c_str();
  54. }
  55. void __create_what(int __num_paths);
  56. private:
  57. struct _LIBCPP_HIDDEN _Storage {
  58. _LIBCPP_INLINE_VISIBILITY
  59. _Storage(const path& __p1, const path& __p2) : __p1_(__p1), __p2_(__p2) {}
  60. path __p1_;
  61. path __p2_;
  62. string __what_;
  63. };
  64. shared_ptr<_Storage> __storage_;
  65. };
  66. // TODO(ldionne): We need to pop the pragma and push it again after
  67. // filesystem_error to work around PR41078.
  68. _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  69. template <class... _Args>
  70. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  71. #ifndef _LIBCPP_NO_EXCEPTIONS
  72. void __throw_filesystem_error(_Args&&... __args) {
  73. throw filesystem_error(_VSTD::forward<_Args>(__args)...);
  74. }
  75. #else
  76. void __throw_filesystem_error(_Args&&...) {
  77. _VSTD::abort();
  78. }
  79. #endif
  80. _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  81. _LIBCPP_END_NAMESPACE_FILESYSTEM
  82. #endif // _LIBCPP_CXX03_LANG
  83. #endif // _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H