xref: /freebsd/contrib/llvm-project/libcxx/include/__filesystem/filesystem_error.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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 <iosfwd>
18 #include <new>
19 #include <system_error>
20 #include <type_traits>
21 
22 #ifndef _LIBCPP_CXX03_LANG
23 
24 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
25 
26 class _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_EXCEPTION_ABI filesystem_error : public system_error {
27 public:
28   _LIBCPP_INLINE_VISIBILITY
29   filesystem_error(const string& __what, error_code __ec)
30       : system_error(__ec, __what),
31         __storage_(make_shared<_Storage>(path(), path())) {
32     __create_what(0);
33   }
34 
35   _LIBCPP_INLINE_VISIBILITY
36   filesystem_error(const string& __what, const path& __p1, error_code __ec)
37       : system_error(__ec, __what),
38         __storage_(make_shared<_Storage>(__p1, path())) {
39     __create_what(1);
40   }
41 
42   _LIBCPP_INLINE_VISIBILITY
43   filesystem_error(const string& __what, const path& __p1, const path& __p2,
44                    error_code __ec)
45       : system_error(__ec, __what),
46         __storage_(make_shared<_Storage>(__p1, __p2)) {
47     __create_what(2);
48   }
49 
50   _LIBCPP_INLINE_VISIBILITY
51   const path& path1() const noexcept { return __storage_->__p1_; }
52 
53   _LIBCPP_INLINE_VISIBILITY
54   const path& path2() const noexcept { return __storage_->__p2_; }
55 
56   filesystem_error(const filesystem_error&) = default;
57   ~filesystem_error() override; // key function
58 
59   _LIBCPP_INLINE_VISIBILITY
60   const char* what() const noexcept override {
61     return __storage_->__what_.c_str();
62   }
63 
64   void __create_what(int __num_paths);
65 
66 private:
67   struct _LIBCPP_HIDDEN _Storage {
68     _LIBCPP_INLINE_VISIBILITY
69     _Storage(const path& __p1, const path& __p2) : __p1_(__p1), __p2_(__p2) {}
70 
71     path __p1_;
72     path __p2_;
73     string __what_;
74   };
75   shared_ptr<_Storage> __storage_;
76 };
77 
78 // TODO(ldionne): We need to pop the pragma and push it again after
79 //                filesystem_error to work around PR41078.
80 _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
81 
82 template <class... _Args>
83 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
84 #ifndef _LIBCPP_NO_EXCEPTIONS
85 void __throw_filesystem_error(_Args&&... __args) {
86   throw filesystem_error(_VSTD::forward<_Args>(__args)...);
87 }
88 #else
89 void __throw_filesystem_error(_Args&&...) {
90   _VSTD::abort();
91 }
92 #endif
93 _LIBCPP_AVAILABILITY_FILESYSTEM_POP
94 
95 _LIBCPP_END_NAMESPACE_FILESYSTEM
96 
97 #endif // _LIBCPP_CXX03_LANG
98 
99 #endif // _LIBCPP___FILESYSTEM_FILESYSTEM_ERROR_H
100