Skip to content

Commit 169c8c0

Browse files
authored
Make all file methods inline (#222)
Similarly to #221, make all `file` methods inline
1 parent a90e22e commit 169c8c0

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

include/tmp/file

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@
77
#define TMP_FILE_H
88

99
#include <cstddef>
10+
#include <cstdio>
1011
#include <filesystem>
1112
#include <fstream>
1213
#include <ios>
1314
#include <istream>
15+
#include <memory>
1416

1517
#ifdef __GLIBCXX__
1618
#include <ext/stdio_filebuf.h>
1719
#endif
1820

1921
namespace tmp {
2022

23+
/// Implementation-defined handle type to the file
24+
#if defined(_WIN32)
25+
using file_native_handle = void*; // HANDLE
26+
#elif __has_include(<unistd.h>)
27+
using file_native_handle = int; // POSIX file descriptor
28+
#else
29+
#error "Target platform not supported"
30+
#endif
31+
32+
extern "C++" std::FILE* create_file();
33+
extern "C++" file_native_handle get_native_handle(std::FILE* file) noexcept;
34+
2135
/// tmp::file is a smart handle that manages a binary temporary file, ensuring
2236
/// its deletion when the handle goes out of scope
2337
///
@@ -53,17 +67,27 @@ namespace tmp {
5367
class file : public std::iostream {
5468
public:
5569
/// Implementation-defined handle type to the file
56-
#if defined(_WIN32)
57-
using native_handle_type = void*; // HANDLE
58-
#elif __has_include(<unistd.h>)
59-
using native_handle_type = int; // POSIX file descriptor
60-
#else
61-
#error "Target platform not supported"
62-
#endif
70+
using native_handle_type = file_native_handle;
6371

6472
/// Creates and opens a binary temporary file as if by POSIX `tmpfile`
6573
/// @throws std::filesystem::filesystem_error if cannot create a file
66-
explicit file();
74+
explicit file()
75+
: std::iostream(std::addressof(sb)),
76+
underlying(create_file(), &std::fclose) {
77+
#if defined(_MSC_VER)
78+
sb = std::filebuf(underlying.get());
79+
#elif defined(_LIBCPP_VERSION)
80+
sb.__open(fileno(underlying.get()), binary | in | out);
81+
#else
82+
sb = __gnu_cxx::stdio_filebuf<char>(underlying.get(), binary | in | out);
83+
#endif
84+
85+
if (!sb.is_open()) {
86+
std::error_code ec = std::make_error_code(std::io_errc::stream);
87+
throw std::filesystem::filesystem_error("Cannot create a temporary file",
88+
ec);
89+
}
90+
}
6791

6892
file(const file&) = delete;
6993
file& operator=(const file&) = delete;
@@ -87,7 +111,9 @@ public:
87111

88112
/// Returns an implementation-defined handle to this file
89113
/// @returns The underlying implementation-defined handle
90-
native_handle_type native_handle() const noexcept;
114+
native_handle_type native_handle() const noexcept {
115+
return get_native_handle(underlying.get());
116+
}
91117

92118
/// Closes and deletes this file
93119
~file() noexcept override = default;

src/abi.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#ifndef TMP_ABI_H
55
#define TMP_ABI_H
66

7+
#include <cstdio>
78
#include <filesystem>
89
#include <string_view>
910

@@ -15,10 +16,16 @@
1516

1617
namespace tmp {
1718

18-
std::filesystem::path abi create_directory(std::string_view prefix);
19-
void abi remove_all(const std::filesystem::path& path) noexcept;
19+
auto abi create_directory(std::string_view prefix) -> std::filesystem::path;
20+
auto abi remove_all(const std::filesystem::path& path) noexcept -> void;
2021

21-
class abi file;
22+
auto abi create_file() -> std::FILE*;
23+
auto abi get_native_handle(std::FILE* file) noexcept ->
24+
#if defined(_WIN32)
25+
void*;
26+
#elif __has_include(<unistd.h>)
27+
int;
28+
#endif
2229
} // namespace tmp
2330

2431
#endif // TMP_ABI_H

src/directory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace tmp {
2424
namespace {
25+
2526
namespace fs = std::filesystem;
2627

2728
/// Checks if the given prefix is valid to attach to a temporary directory name

src/file.cpp

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#endif
2424

2525
namespace tmp {
26-
namespace {
2726

2827
namespace fs = std::filesystem;
2928

@@ -35,22 +34,6 @@ static_assert(std::is_trivially_copyable_v<file::native_handle_type>);
3534
static_assert(std::is_same_v<HANDLE, file::native_handle_type>);
3635
#endif
3736

38-
#ifndef _MSC_VER
39-
/// Open mode for binary temporary files
40-
constexpr auto mode = std::ios::binary | std::ios::in | std::ios::out;
41-
#endif
42-
43-
/// Returns an implementation-defined handle to the file
44-
/// @param[in] file The file to the native handle for
45-
/// @returns The underlying implementation-defined handle
46-
file::native_handle_type get_native_handle(std::FILE* file) noexcept {
47-
#ifdef _WIN32
48-
return reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file)));
49-
#else
50-
return fileno(file);
51-
#endif
52-
}
53-
5437
/// Creates and opens a binary temporary file as if by POSIX `tmpfile`
5538
/// @returns A pointer to the file stream associated with the temporary file
5639
/// @throws fs::filesystem_error if cannot create a temporary file
@@ -63,26 +46,15 @@ std::FILE* create_file() {
6346

6447
return file;
6548
}
66-
} // namespace
6749

68-
file::file()
69-
: std::iostream(std::addressof(sb)),
70-
underlying(create_file(), &std::fclose) {
71-
#if defined(_MSC_VER)
72-
sb = std::filebuf(underlying.get());
73-
#elif defined(_LIBCPP_VERSION)
74-
sb.__open(get_native_handle(underlying.get()), mode);
50+
/// Returns an implementation-defined handle to the file
51+
/// @param[in] file The file to the native handle for
52+
/// @returns The underlying implementation-defined handle
53+
file::native_handle_type get_native_handle(std::FILE* file) noexcept {
54+
#ifdef _WIN32
55+
return reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(file)));
7556
#else
76-
sb = __gnu_cxx::stdio_filebuf<char>(underlying.get(), mode);
57+
return fileno(file);
7758
#endif
78-
79-
if (!sb.is_open()) {
80-
std::error_code ec = std::make_error_code(std::io_errc::stream);
81-
throw fs::filesystem_error("Cannot create a temporary file", ec);
82-
}
83-
}
84-
85-
file::native_handle_type file::native_handle() const noexcept {
86-
return get_native_handle(underlying.get());
8759
}
8860
} // namespace tmp

0 commit comments

Comments
 (0)