Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
scoped_change_directory.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_SCOPED_CHANGE_DIRECTORY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_SCOPED_CHANGE_DIRECTORY_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
9#include "arene/base/filesystem/path_string.hpp"
10#include "arene/base/string_view.hpp"
11// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15namespace arene {
16namespace base {
17
18namespace filesystem {
19
20/// @brief A RAII helper to temporarily change the working directory of the process.
21///
22/// The working directory is saved on construction, before being set to the new path. It is then restored to the old
23/// value on destruction.
24///
25/// @note Use of this on multiple threads is inherently racy as there is only one working directory for the whole
26/// process.
27class scoped_change_directory {
28 public:
29 /// @brief Save the working directory and set it to the new path
30 /// @param new_path The new working directory
31 /// @throws std::system_error if an error occurs setting or retrieving the working directory
32 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
33 explicit scoped_change_directory(null_terminated_string_view const new_path);
34
35 /// @brief Restore the working directory to the value saved on construction if possible.
36 ~scoped_change_directory();
37
38 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
39 /// @brief Not copyable
40 scoped_change_directory(scoped_change_directory const&) = delete;
41 /// @brief Not copyable
42 auto operator=(scoped_change_directory const&) -> scoped_change_directory& = delete;
43 /// @brief Not movable
44 scoped_change_directory(scoped_change_directory&&) = delete;
45 /// @brief Not movable
46 auto operator=(scoped_change_directory&&) -> scoped_change_directory& = delete;
47 // parasoft-end-suppress CERT_C-EXP37-a-3
48
49 private:
50 /// @brief Buffer for previous working directory
51 path_buffer old_dir_{};
52};
53
54} // namespace filesystem
55} // namespace base
56} // namespace arene
57
58#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_SCOPED_CHANGE_DIRECTORY_HPP_
Definition directory_handle.cpp:39
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10