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