Skip to content

Commit 135c314

Browse files
authored
Split private functions sources (#225)
Since all symbols exported from the library are functions that don't share common code, it makes sense to separate their definitions into separate source files Now each exported function has its own source file
1 parent 3bee72e commit 135c314

File tree

7 files changed

+65
-51
lines changed

7 files changed

+65
-51
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Checks: >
2222
-cppcoreguidelines-pro-type-vararg,
2323
-misc-const-correctness,
2424
-misc-include-cleaner,
25+
-modernize-deprecated-headers,
2526
-modernize-return-braced-init-list,
2627
-modernize-use-auto,
2728
-modernize-use-nodiscard,

meson.build

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ filesystem_dep = filesystem_proj.get_variable('filesystem_dep')
1212

1313
tmp = library(
1414
'tmp',
15-
'src/file.cpp',
16-
'src/directory.cpp',
15+
'src/create_directory.cpp',
16+
'src/create_file.cpp',
17+
'src/get_native_handle.cpp',
18+
'src/remove_all.cpp',
1719
install: true,
1820
include_directories: include_directory,
1921
gnu_symbol_visibility: 'hidden',

src/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
find_package(Filesystem REQUIRED)
22

3-
add_library(${PROJECT_NAME} file.cpp directory.cpp)
3+
add_library(${PROJECT_NAME}
4+
create_directory.cpp
5+
create_file.cpp
6+
get_native_handle.cpp
7+
remove_all.cpp)
8+
49
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
510
target_link_libraries(${PROJECT_NAME} PUBLIC std::filesystem)
611

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
#include "abi.hpp"
55

6-
#include <tmp/directory>
7-
86
#include <filesystem>
97
#include <string_view>
108
#include <system_error>
11-
#include <utility>
129

1310
#ifdef _WIN32
1411
#define UNICODE
@@ -66,7 +63,7 @@ fs::path make_path(std::string_view prefix) {
6663
/// Creates a temporary directory in the current user's temporary directory
6764
/// @param prefix A prefix to attach to the temporary directory name
6865
/// @returns A path to the created temporary directory
69-
/// @throws fs::filesystem_error if cannot create a temporary directory
66+
/// @throws fs::filesystem_error if cannot create a temporary directory
7067
/// @throws std::invalid_argument if the prefix contains a directory separator
7168
fs::path create_directory(std::string_view prefix) {
7269
if (!is_prefix_valid(prefix)) {
@@ -95,21 +92,4 @@ fs::path create_directory(std::string_view prefix) {
9592

9693
return path;
9794
}
98-
99-
/// Deletes a path recursively, ignoring any errors
100-
/// @param path The path to delete
101-
void remove_all(const fs::path& path) noexcept {
102-
try {
103-
if (!path.empty()) {
104-
// Calling the `std::error_code` overload of `fs::remove_all` should be
105-
// more optimal here since it would not require creating
106-
// a `fs::filesystem_error` message before we suppress the exception
107-
std::error_code ec;
108-
fs::remove_all(path, ec);
109-
}
110-
} catch (...) {
111-
// Do nothing: if we failed to delete the temporary directory,
112-
// the system should do it later
113-
}
114-
}
11595
} // namespace tmp::detail

src/file.cpp renamed to src/create_file.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,15 @@
55

66
#include "abi.hpp"
77

8-
#include <tmp/file>
9-
8+
#include <cerrno>
109
#include <cstdio>
1110
#include <filesystem>
1211
#include <system_error>
13-
#include <type_traits>
14-
15-
#ifdef _WIN32
16-
#include <Windows.h>
17-
#include <io.h>
18-
#endif
1912

2013
namespace tmp::detail {
2114

2215
namespace fs = std::filesystem;
2316

24-
// Confirm that native_handle_type matches `TriviallyCopyable` named requirement
25-
static_assert(std::is_trivially_copyable_v<file::native_handle_type>);
26-
27-
#ifdef _WIN32
28-
// Confirm that `HANDLE` is as implemented in `file`
29-
static_assert(std::is_same_v<HANDLE, file::native_handle_type>);
30-
#endif
31-
3217
/// Creates and opens a binary temporary file as if by POSIX `tmpfile`
3318
/// @returns A pointer to the file stream associated with the temporary file
3419
/// @throws fs::filesystem_error if cannot create a temporary file
@@ -41,15 +26,4 @@ std::FILE* create_file() {
4126

4227
return file;
4328
}
44-
45-
/// Returns an implementation-defined handle to the file
46-
/// @param[in] file The file to get the native handle for
47-
/// @returns The underlying implementation-defined handle
48-
file::native_handle_type get_native_handle(std::FILE* file) noexcept {
49-
#ifdef _WIN32
50-
return reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file)));
51-
#else
52-
return fileno(file);
53-
#endif
54-
}
5529
} // namespace tmp::detail

src/get_native_handle.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-FileCopyrightText: (c) 2024 Ilya Andreev <[email protected]>
2+
// SPDX-License-Identifier: MIT
3+
4+
#include "abi.hpp"
5+
6+
#include <tmp/file>
7+
8+
#include <cstdio>
9+
#include <stdio.h>
10+
11+
#ifdef _WIN32
12+
#include <Windows.h>
13+
#include <io.h>
14+
#endif
15+
16+
namespace tmp::detail {
17+
18+
/// Returns an implementation-defined handle to the file
19+
/// @param[in] file The file to get the native handle for
20+
/// @returns The underlying implementation-defined handle
21+
file::native_handle_type get_native_handle(std::FILE* file) noexcept {
22+
#ifdef _WIN32
23+
return reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file)));
24+
#else
25+
return fileno(file);
26+
#endif
27+
}
28+
} // namespace tmp::detail

src/remove_all.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: (c) 2024 Ilya Andreev <[email protected]>
2+
// SPDX-License-Identifier: MIT
3+
4+
#include "abi.hpp"
5+
6+
#include <filesystem>
7+
#include <system_error>
8+
9+
namespace tmp::detail {
10+
11+
/// Deletes a path recursively, ignoring any errors
12+
/// @param path The path to delete
13+
void remove_all(const std::filesystem::path& path) noexcept {
14+
try {
15+
if (!path.empty()) {
16+
std::error_code ec;
17+
std::filesystem::remove_all(path, ec);
18+
}
19+
} catch (...) {
20+
// Do nothing: if we failed to delete the temporary directory,
21+
// the system should do it later
22+
}
23+
}
24+
} // namespace tmp::detail

0 commit comments

Comments
 (0)