xref: /llvm-project/libcxx/include/__filesystem/filesystem_error.h (revision f1ea0b11ca03d0f8b5785b7ec92d2eb673eeb4b9)
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 
10 #ifndef _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
11 #define _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
12 
13 #include <__availability>
14 #include <__config>
15 #include <__filesystem/path.h>
16 #include <__memory/shared_ptr.h>
17 #include <__system_error/error_code.h>
18 #include <__system_error/system_error.h>
19 #include <__utility/forward.h>
20 #include <__verbose_abort>
21 #include <iosfwd>
22 #include <new>
23 #include <string>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 #ifndef _LIBCPP_CXX03_LANG
30 
31 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
32 
33 class _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_EXPORTED_FROM_ABI filesystem_error : public system_error {
34 public:
35   _LIBCPP_INLINE_VISIBILITY
36   filesystem_error(const string& __what, error_code __ec)
37       : system_error(__ec, __what),
38         __storage_(make_shared<_Storage>(path(), path())) {
39     __create_what(0);
40   }
41 
42   _LIBCPP_INLINE_VISIBILITY
43   filesystem_error(const string& __what, const path& __p1, error_code __ec)
44       : system_error(__ec, __what),
45         __storage_(make_shared<_Storage>(__p1, path())) {
46     __create_what(1);
47   }
48 
49   _LIBCPP_INLINE_VISIBILITY
50   filesystem_error(const string& __what, const path& __p1, const path& __p2,
51                    error_code __ec)
52       : system_error(__ec, __what),
53         __storage_(make_shared<_Storage>(__p1, __p2)) {
54     __create_what(2);
55   }
56 
57   _LIBCPP_INLINE_VISIBILITY
58   const path& path1() const noexcept { return __storage_->__p1_; }
59 
60   _LIBCPP_INLINE_VISIBILITY
61   const path& path2() const noexcept { return __storage_->__p2_; }
62 
63   _LIBCPP_HIDE_FROM_ABI filesystem_error(const filesystem_error&) = default;
64   ~filesystem_error() override; // key function
65 
66   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
67   const char* what() const noexcept override {
68     return __storage_->__what_.c_str();
69   }
70 
71   void __create_what(int __num_paths);
72 
73 private:
74   struct _LIBCPP_HIDDEN _Storage {
75     _LIBCPP_INLINE_VISIBILITY
76     _Storage(const path& __p1, const path& __p2) : __p1_(__p1), __p2_(__p2) {}
77 
78     path __p1_;
79     path __p2_;
80     string __what_;
81   };
82   shared_ptr<_Storage> __storage_;
83 };
84 
85 // TODO(ldionne): We need to pop the pragma and push it again after
86 //                filesystem_error to work around PR41078.
87 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
88 
89 template <class... _Args>
90 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
91 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
92 void __throw_filesystem_error(_Args&&... __args) {
93   throw filesystem_error(_VSTD::forward<_Args>(__args)...);
94 }
95 #else
96 void __throw_filesystem_error(_Args&&...) {
97     _LIBCPP_VERBOSE_ABORT("filesystem_error was thrown in -fno-exceptions mode");
98 }
99 #endif
100 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
101 
102 _LIBCPP_END_NAMESPACE_FILESYSTEM
103 
104 #endif // _LIBCPP_CXX03_LANG
105 
106 #endif // _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
107