Improve support for non-POSIX platforms more

This commit is contained in:
Victor Zverovich 2024-07-08 13:45:23 -07:00
parent 486838f26f
commit 92227c77a4
2 changed files with 18 additions and 7 deletions

View File

@ -1443,17 +1443,24 @@ template <typename T> struct span {
size_t size; size_t size;
}; };
#ifdef _WIN32 template <typename F> auto flockfile(F* f) -> decltype(_lock_file(f)) {
inline void flockfile(FILE* f) { _lock_file(f); } _lock_file(f);
inline void funlockfile(FILE* f) { _unlock_file(f); } }
inline int getc_unlocked(FILE* f) { return _fgetc_nolock(f); } template <typename F> auto funlockfile(F* f) -> decltype(_unlock_file(f)) {
_unlock_file(f);
}
#ifndef getc_unlocked
template <typename F> auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) {
return _fgetc_nolock(f);
}
#endif #endif
template <typename F = FILE, typename Enable = void> template <typename F = FILE, typename Enable = void>
struct has_flockfile : std::false_type {}; struct has_flockfile : std::false_type {};
template <typename F> template <typename F>
struct has_flockfile<F, void_t<decltype(flockfile(static_cast<F*>(nullptr)))>> struct has_flockfile<F, void_t<decltype(flockfile(&std::declval<F&>()))>>
: std::true_type {}; : std::true_type {};
// A FILE wrapper. F is FILE defined as a template parameter to make system API // A FILE wrapper. F is FILE defined as a template parameter to make system API
@ -1658,7 +1665,8 @@ class file_print_buffer<F, enable_if_t<has_flockfile<F>::value>>
~file_print_buffer() { ~file_print_buffer() {
file_.advance_write_buffer(size()); file_.advance_write_buffer(size());
bool flush = file_.needs_flush(); bool flush = file_.needs_flush();
funlockfile(file_); F* f = file_; // Make funlockfile depend on the template parameter F
funlockfile(f); // for the system API detection to work.
if (flush) fflush(file_); if (flush) fflush(file_);
} }
}; };

View File

@ -192,7 +192,10 @@ class file_scan_buffer final : public scan_buffer {
flockfile(f); flockfile(f);
fill(); fill();
} }
~file_scan_buffer() { funlockfile(file_); } ~file_scan_buffer() {
FILE* f = file_;
funlockfile(f);
}
}; };
} // namespace detail } // namespace detail