From: Joel Rosdahl Date: Sat, 30 Sep 2023 17:40:33 +0000 (+0200) Subject: refactor: Move Finalizer to util X-Git-Tag: v4.9~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f88e60844c4e2470b9e08faa6ca0f437309b0d12;p=thirdparty%2Fccache.git refactor: Move Finalizer to util --- diff --git a/src/InodeCache.cpp b/src/InodeCache.cpp index 4add4bd9e..26339b589 100644 --- a/src/InodeCache.cpp +++ b/src/InodeCache.cpp @@ -19,7 +19,6 @@ #include "InodeCache.hpp" #include "Config.hpp" -#include "Finalizer.hpp" #include "Hash.hpp" #include "Logging.hpp" #include "Util.hpp" @@ -27,6 +26,7 @@ #include #include +#include #include #include #include @@ -352,7 +352,7 @@ InodeCache::create_new_file(const std::string& filename) return false; } - Finalizer temp_file_remover([&] { unlink(tmp_file->path.c_str()); }); + util::Finalizer temp_file_remover([&] { unlink(tmp_file->path.c_str()); }); if (!fd_is_on_known_to_work_file_system(*tmp_file->fd)) { return false; diff --git a/src/Util.cpp b/src/Util.cpp index 9281775fd..9099d0a5f 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -24,7 +24,6 @@ #include "Win32Util.hpp" #include -#include #include #include #include diff --git a/src/ccache.cpp b/src/ccache.cpp index 201d4844b..7d037e5cc 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -24,7 +24,6 @@ #include "Context.hpp" #include "Depfile.hpp" #include "File.hpp" -#include "Finalizer.hpp" #include "Hash.hpp" #include "Logging.hpp" #include "MiniTrace.hpp" @@ -51,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -2335,7 +2335,7 @@ cache_compilation(int argc, const char* const* argv) ctx.initialize(std::move(argv_parts.compiler_and_args), argv_parts.config_settings); SignalHandler signal_handler(ctx); - Finalizer finalizer([&ctx] { finalize_at_exit(ctx); }); + util::Finalizer finalizer([&ctx] { finalize_at_exit(ctx); }); initialize(ctx, argv, argv_parts.masquerading_as_compiler); diff --git a/src/core/common.cpp b/src/core/common.cpp index 72acaab61..54f49e088 100644 --- a/src/core/common.cpp +++ b/src/core/common.cpp @@ -19,9 +19,9 @@ #include "common.hpp" #include -#include #include #include +#include #include #include #include @@ -133,7 +133,7 @@ send_to_console(const Context& ctx, std::string_view text, int fd) // newlines a second time since we treat output as binary data. Make sure to // switch to binary mode. int oldmode = _setmode(fd, _O_BINARY); - Finalizer binary_mode_restorer([=] { _setmode(fd, oldmode); }); + util::Finalizer binary_mode_restorer([=] { _setmode(fd, oldmode); }); #endif if (ctx.args_info.strip_diagnostics_colors) { diff --git a/src/execute.cpp b/src/execute.cpp index 63f64a51c..2c2ae6e7b 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -49,10 +50,6 @@ # include #endif -#ifdef _WIN32 -# include "Finalizer.hpp" -#endif - namespace fs = util::filesystem; #ifdef _WIN32 @@ -223,7 +220,7 @@ win32execute(const char* path, std::string full_path = Win32Util::add_exe_suffix(path); fs::path tmp_file_path; - Finalizer tmp_file_remover([&tmp_file_path] { + util::Finalizer tmp_file_remover([&tmp_file_path] { if (!tmp_file_path.empty()) { util::remove(tmp_file_path); } diff --git a/src/util/DirEntry.cpp b/src/util/DirEntry.cpp index ca686050f..8dad7e939 100644 --- a/src/util/DirEntry.cpp +++ b/src/util/DirEntry.cpp @@ -18,10 +18,10 @@ #include "DirEntry.hpp" -#include #include #include #include +#include #include #include @@ -138,7 +138,7 @@ win32_stat_impl(const char* path, return false; } - Finalizer closer([&] { CloseHandle(handle); }); + util::Finalizer closer([&] { CloseHandle(handle); }); switch (GetFileType(handle)) { case FILE_TYPE_DISK: { diff --git a/src/Finalizer.hpp b/src/util/Finalizer.hpp similarity index 89% rename from src/Finalizer.hpp rename to src/util/Finalizer.hpp index c5d2033c1..baea58af0 100644 --- a/src/Finalizer.hpp +++ b/src/util/Finalizer.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2021 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -20,6 +20,8 @@ #include +namespace util { + class Finalizer { public: @@ -28,7 +30,7 @@ public: private: std::function m_finalizer; -}; +}; // namespace class Finalizer inline Finalizer::Finalizer(std::function finalizer) : m_finalizer(finalizer) @@ -39,3 +41,5 @@ inline Finalizer::~Finalizer() { m_finalizer(); } + +} // namespace util diff --git a/src/util/file.cpp b/src/util/file.cpp index 8882fa5df..76d2b18c2 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -18,13 +18,13 @@ #include "file.hpp" -#include #include #include #include #include #include #include +#include #include #include #include diff --git a/unittest/test_bsdmkstemp.cpp b/unittest/test_bsdmkstemp.cpp index 30aaa7fe4..f0cdb4192 100644 --- a/unittest/test_bsdmkstemp.cpp +++ b/unittest/test_bsdmkstemp.cpp @@ -16,10 +16,10 @@ // this program; if not, write to the Free Software Foundation, Inc., 51 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -#include "../src/Finalizer.hpp" #include "TestUtil.hpp" #include +#include #include #include "third_party/doctest.h" @@ -104,7 +104,8 @@ TEST_CASE("bsd_mkstemps") ++rand_iter; }); - Finalizer reset_random_source([] { bsd_mkstemp_set_random_source(nullptr); }); + util::Finalizer reset_random_source( + [] { bsd_mkstemp_set_random_source(nullptr); }); SUBCASE("successful") { diff --git a/unittest/test_util_DirEntry.cpp b/unittest/test_util_DirEntry.cpp index a9426f4ae..ebd349d6b 100644 --- a/unittest/test_util_DirEntry.cpp +++ b/unittest/test_util_DirEntry.cpp @@ -18,8 +18,8 @@ #include "TestUtil.hpp" -#include #include +#include #include #include #include @@ -588,7 +588,7 @@ TEST_CASE("Win32 Pending Delete" * doctest::skip(running_under_wine())) FILE_ATTRIBUTE_NORMAL, nullptr); REQUIRE_MESSAGE(handle != INVALID_HANDLE_VALUE, "err=" << GetLastError()); - Finalizer cleanup([&] { CloseHandle(handle); }); + util::Finalizer cleanup([&] { CloseHandle(handle); }); // Mark file as deleted. This puts it into a "pending delete" state that // will persist until the handle is closed. Until the file is closed, new @@ -621,7 +621,7 @@ TEST_CASE("Win32 No Sharing") FILE_ATTRIBUTE_NORMAL, nullptr); REQUIRE_MESSAGE(handle != INVALID_HANDLE_VALUE, "err=" << GetLastError()); - Finalizer cleanup([&] { CloseHandle(handle); }); + util::Finalizer cleanup([&] { CloseHandle(handle); }); // Sanity check we can't open the file for read/write access. REQUIRE(!util::read_file("file"));