From 92227c77a4eed4c12f97f7916041ace4d5ca02d4 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 8 Jul 2024 13:45:23 -0700 Subject: [PATCH] Improve support for non-POSIX platforms more --- include/fmt/format-inl.h | 20 ++++++++++++++------ test/scan.h | 5 ++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index c1653bb438eb..a887483b6f46 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1443,17 +1443,24 @@ template struct span { size_t size; }; -#ifdef _WIN32 -inline void flockfile(FILE* f) { _lock_file(f); } -inline void funlockfile(FILE* f) { _unlock_file(f); } -inline int getc_unlocked(FILE* f) { return _fgetc_nolock(f); } +template auto flockfile(F* f) -> decltype(_lock_file(f)) { + _lock_file(f); +} +template auto funlockfile(F* f) -> decltype(_unlock_file(f)) { + _unlock_file(f); +} + +#ifndef getc_unlocked +template auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) { + return _fgetc_nolock(f); +} #endif template struct has_flockfile : std::false_type {}; template -struct has_flockfile(nullptr)))>> +struct has_flockfile()))>> : std::true_type {}; // A FILE wrapper. F is FILE defined as a template parameter to make system API @@ -1658,7 +1665,8 @@ class file_print_buffer::value>> ~file_print_buffer() { file_.advance_write_buffer(size()); 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_); } }; diff --git a/test/scan.h b/test/scan.h index 1f1cda74ef47..baa932bcf313 100644 --- a/test/scan.h +++ b/test/scan.h @@ -192,7 +192,10 @@ class file_scan_buffer final : public scan_buffer { flockfile(f); fill(); } - ~file_scan_buffer() { funlockfile(file_); } + ~file_scan_buffer() { + FILE* f = file_; + funlockfile(f); + } }; } // namespace detail