Skip to content

Commit 517b4be

Browse files
authored
Make all directory methods inline (#221)
Given the simplicity of `directory` methods implementation, making the methods `inline` should not impact build times, but provides the benefits of `inline`
1 parent cedad44 commit 517b4be

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Checks: >
2525
-modernize-use-auto,
2626
-modernize-use-nodiscard,
2727
-modernize-use-trailing-return-type,
28-
-readability-named-parameter
28+
-readability-named-parameter,
29+
-readability-redundant-declaration
2930
3031
CheckOptions:
3132
readability-identifier-length.IgnoredParameterNames: 'ec|to'

include/tmp/directory

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include <cstddef>
1010
#include <filesystem>
1111
#include <string_view>
12+
#include <utility>
1213

1314
namespace tmp {
1415

16+
extern "C++" std::filesystem::path create_directory(std::string_view prefix);
17+
extern "C++" void remove_all(const std::filesystem::path& path) noexcept;
18+
1519
/// tmp::directory is a smart handle that manages a temporary directory,
1620
/// ensuring its recursive deletion when the handle goes out of scope
1721
///
@@ -45,7 +49,29 @@ public:
4549
/// @param prefix A prefix to add to the temporary directory name
4650
/// @throws std::filesystem::filesystem_error if cannot create a directory
4751
/// @throws std::invalid_argument if the prefix contains a directory separator
48-
explicit directory(std::string_view prefix = "");
52+
explicit directory(std::string_view prefix = "")
53+
: pathobject(create_directory(prefix)) {}
54+
55+
directory(const directory&) = delete;
56+
directory& operator=(const directory&) = delete;
57+
58+
/// Moves the ownership of the directory managed by `other` to a new handle
59+
/// @note After the move, `other` is not associated with a directory
60+
/// @param other Another directory that will be moved from
61+
directory(directory&& other) noexcept
62+
: pathobject(std::exchange(other.pathobject, std::filesystem::path())) {}
63+
64+
/// Deletes the managed temporary directory, then moves the ownership
65+
/// of the directory managed by `other` to `this`
66+
/// @note After the assignment, `other` is not associated with a directory
67+
/// @param other Another directory that will be moved from
68+
/// @returns `*this`
69+
directory& operator=(directory&& other) noexcept {
70+
remove_all(*this);
71+
pathobject = std::exchange(other.pathobject, std::filesystem::path());
72+
73+
return *this;
74+
}
4975

5076
/// Returns the path of this directory
5177
/// @returns The full path of this directory
@@ -67,12 +93,9 @@ public:
6793
}
6894

6995
/// Deletes this directory recursively
70-
~directory() noexcept;
71-
72-
directory(directory&&) noexcept;
73-
directory& operator=(directory&&) noexcept;
74-
directory(const directory&) = delete;
75-
directory& operator=(const directory&) = delete;
96+
~directory() noexcept {
97+
remove_all(*this);
98+
}
7699

77100
private:
78101
/// This directory path

src/abi.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#ifndef TMP_ABI_H
55
#define TMP_ABI_H
66

7+
#include <filesystem>
8+
#include <string_view>
9+
710
#ifdef _WIN32
811
#define abi __declspec(dllexport)
912
#else
@@ -12,7 +15,9 @@
1215

1316
namespace tmp {
1417

15-
class abi directory;
18+
std::filesystem::path abi create_directory(std::string_view prefix);
19+
void abi remove_all(const std::filesystem::path& path) noexcept;
20+
1621
class abi file;
1722
} // namespace tmp
1823

src/directory.cpp

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222

2323
namespace tmp {
2424
namespace {
25-
2625
namespace fs = std::filesystem;
2726

2827
/// Checks if the given prefix is valid to attach to a temporary directory name
29-
/// @param[in] prefix The prefix to check validity for
28+
/// @param prefix The prefix to check validity for
3029
/// @returns `true` if the prefix is valid, `false` otherwise
3130
bool is_prefix_valid(const fs::path& prefix) {
3231
// We also need to check that the prefix does not contain a root path
@@ -38,7 +37,7 @@ bool is_prefix_valid(const fs::path& prefix) {
3837
#ifdef _WIN32
3938
/// Creates a temporary path with the given prefix
4039
/// @note prefix must be valid
41-
/// @param[in] prefix A prefix to attach to the path pattern
40+
/// @param prefix A prefix to attach to the path pattern
4241
/// @returns A unique temporary path
4342
fs::path make_path(std::string_view prefix) {
4443
GUID guid;
@@ -61,9 +60,10 @@ fs::path make_path(std::string_view prefix) {
6160
return path;
6261
}
6362
#endif
63+
} // namespace
6464

6565
/// Creates a temporary directory in the current user's temporary directory
66-
/// @param[in] prefix A prefix to attach to the temporary directory name
66+
/// @param prefix A prefix to attach to the temporary directory name
6767
/// @returns A path to the created temporary directory
6868
/// @throws fs::filesystem_error if cannot create a temporary directory
6969
/// @throws std::invalid_argument if the prefix contains a directory separator
@@ -95,38 +95,20 @@ fs::path create_directory(std::string_view prefix) {
9595
return path;
9696
}
9797

98-
/// Deletes a directory recursively, ignoring any errors
99-
/// @param[in] directory The directory to delete
100-
void remove_directory(const directory& directory) noexcept {
98+
/// Deletes a path recursively, ignoring any errors
99+
/// @param path The path to delete
100+
void remove_all(const fs::path& path) noexcept {
101101
try {
102-
if (!directory.path().empty()) {
102+
if (!path.empty()) {
103103
// Calling the `std::error_code` overload of `fs::remove_all` should be
104104
// more optimal here since it would not require creating
105105
// a `fs::filesystem_error` message before we suppress the exception
106106
std::error_code ec;
107-
fs::remove_all(directory, ec);
107+
fs::remove_all(path, ec);
108108
}
109109
} catch (...) {
110110
// Do nothing: if we failed to delete the temporary directory,
111111
// the system should do it later
112112
}
113113
}
114-
} // namespace
115-
116-
directory::directory(std::string_view prefix)
117-
: pathobject(create_directory(prefix)) {}
118-
119-
directory::~directory() noexcept {
120-
remove_directory(*this);
121-
}
122-
123-
directory::directory(directory&& other) noexcept
124-
: pathobject(std::exchange(other.pathobject, fs::path())) {}
125-
126-
directory& directory::operator=(directory&& other) noexcept {
127-
remove_directory(*this);
128-
129-
pathobject = std::exchange(other.pathobject, fs::path());
130-
return *this;
131-
}
132114
} // namespace tmp

0 commit comments

Comments
 (0)