Skip to content

Commit 3ef9001

Browse files
authored
Move private declarations to namespace detail (#223)
Functions used in the inline function bodies should not be used by end users
1 parent b451fcb commit 3ef9001

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

include/tmp/directory

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace tmp {
1515

16+
namespace detail {
1617
extern "C++" std::filesystem::path create_directory(std::string_view prefix);
1718
extern "C++" void remove_all(const std::filesystem::path& path) noexcept;
19+
} // namespace detail
1820

1921
/// tmp::directory is a smart handle that manages a temporary directory,
2022
/// ensuring its recursive deletion when the handle goes out of scope
@@ -50,7 +52,7 @@ public:
5052
/// @throws std::filesystem::filesystem_error if cannot create a directory
5153
/// @throws std::invalid_argument if the prefix contains a directory separator
5254
explicit directory(std::string_view prefix = "")
53-
: pathobject(create_directory(prefix)) {}
55+
: pathobject(detail::create_directory(prefix)) {}
5456

5557
directory(const directory&) = delete;
5658
directory& operator=(const directory&) = delete;
@@ -67,7 +69,7 @@ public:
6769
/// @param other Another directory that will be moved from
6870
/// @returns `*this`
6971
directory& operator=(directory&& other) noexcept {
70-
remove_all(*this);
72+
detail::remove_all(*this);
7173
pathobject = std::exchange(other.pathobject, std::filesystem::path());
7274

7375
return *this;
@@ -94,7 +96,7 @@ public:
9496

9597
/// Deletes this directory recursively
9698
~directory() noexcept {
97-
remove_all(*this);
99+
detail::remove_all(*this);
98100
}
99101

100102
private:

include/tmp/file

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020

2121
namespace tmp {
2222

23-
/// Implementation-defined handle type to the file
23+
namespace detail {
24+
extern "C++" std::FILE* create_file();
2425
#if defined(_WIN32)
25-
using file_native_handle = void*; // HANDLE
26+
extern "C++" void* get_native_handle(std::FILE* file) noexcept;
2627
#elif __has_include(<unistd.h>)
27-
using file_native_handle = int; // POSIX file descriptor
28-
#else
29-
#error "Target platform not supported"
28+
extern "C++" int get_native_handle(std::FILE* file) noexcept;
3029
#endif
31-
32-
extern "C++" std::FILE* create_file();
33-
extern "C++" file_native_handle get_native_handle(std::FILE* file) noexcept;
30+
} // namespace detail
3431

3532
/// tmp::file is a smart handle that manages a binary temporary file, ensuring
3633
/// its deletion when the handle goes out of scope
@@ -68,13 +65,14 @@ template<class charT, class traits = std::char_traits<charT>>
6865
class basic_file : public std::basic_iostream<charT, traits> {
6966
public:
7067
/// Implementation-defined handle type to the file
71-
using native_handle_type = file_native_handle;
68+
using native_handle_type =
69+
std::invoke_result_t<decltype(detail::get_native_handle), std::FILE*>;
7270

7371
/// Creates and opens a binary temporary file as if by POSIX `tmpfile`
7472
/// @throws std::filesystem::filesystem_error if cannot create a file
7573
explicit basic_file()
7674
: std::basic_iostream<charT, traits>(std::addressof(sb)),
77-
underlying(create_file(), &std::fclose),
75+
underlying(detail::create_file(), &std::fclose),
7876
sb(open_filebuf(underlying.get())) {}
7977

8078
basic_file(const basic_file&) = delete;
@@ -100,7 +98,7 @@ public:
10098
/// Returns an implementation-defined handle to this file
10199
/// @returns The underlying implementation-defined handle
102100
native_handle_type native_handle() const noexcept {
103-
return get_native_handle(underlying.get());
101+
return detail::get_native_handle(underlying.get());
104102
}
105103

106104
/// Closes and deletes this file
@@ -128,7 +126,7 @@ private:
128126
#if defined(_MSC_VER)
129127
sb = std::basic_filebuf<charT, traits>(file);
130128
#elif defined(_LIBCPP_VERSION)
131-
sb.__open(get_native_handle(file), mode);
129+
sb.__open(detail::get_native_handle(file), mode);
132130
#else
133131
sb = __gnu_cxx::stdio_filebuf<charT, traits>(file, mode);
134132
#endif

src/abi.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define abi __attribute__((visibility("default")))
1515
#endif
1616

17-
namespace tmp {
17+
namespace tmp::detail {
1818

1919
auto abi create_directory(std::string_view prefix) -> std::filesystem::path;
2020
auto abi remove_all(const std::filesystem::path& path) noexcept -> void;
@@ -25,7 +25,9 @@ auto abi get_native_handle(std::FILE* file) noexcept ->
2525
void*;
2626
#elif __has_include(<unistd.h>)
2727
int;
28+
#else
29+
#error "Target platform not supported"
2830
#endif
29-
} // namespace tmp
31+
} // namespace tmp::detail
3032

3133
#endif // TMP_ABI_H

src/directory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <unistd.h>
2121
#endif
2222

23-
namespace tmp {
23+
namespace tmp::detail {
2424
namespace {
2525

2626
namespace fs = std::filesystem;
@@ -112,4 +112,4 @@ void remove_all(const fs::path& path) noexcept {
112112
// the system should do it later
113113
}
114114
}
115-
} // namespace tmp
115+
} // namespace tmp::detail

src/file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <io.h>
1818
#endif
1919

20-
namespace tmp {
20+
namespace tmp::detail {
2121

2222
namespace fs = std::filesystem;
2323

@@ -52,4 +52,4 @@ file::native_handle_type get_native_handle(std::FILE* file) noexcept {
5252
return fileno(file);
5353
#endif
5454
}
55-
} // namespace tmp
55+
} // namespace tmp::detail

0 commit comments

Comments
 (0)