2020
2121namespace 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>>
6865class basic_file : public std ::basic_iostream<charT, traits> {
6966public:
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
0 commit comments