diff --git a/src/common/windows/util.hpp b/src/common/windows/util.hpp new file mode 100644 index 000000000..9891d638c --- /dev/null +++ b/src/common/windows/util.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +template +struct on_scope_exit { + static_assert(std::is_nothrow_move_constructible::value, + "Fn must be nothrow move constructible"); + + explicit on_scope_exit(Fn &&fn) noexcept + : _fn(std::move(fn)) {}; + on_scope_exit(const on_scope_exit&) = delete; + on_scope_exit& operator=(const on_scope_exit&) = delete; + ~on_scope_exit() noexcept { this->_fn(); } + +private: + Fn _fn; +}; diff --git a/src/detection/bluetooth/bluetooth_windows.cpp b/src/detection/bluetooth/bluetooth_windows.cpp index 069084b24..f4ac9c9c7 100644 --- a/src/detection/bluetooth/bluetooth_windows.cpp +++ b/src/detection/bluetooth/bluetooth_windows.cpp @@ -4,18 +4,10 @@ extern "C" } #include "common/windows/wmi.hpp" #include "common/windows/unicode.hpp" +#include "common/windows/util.hpp" STDAPI InitVariantFromStringArray(_In_reads_(cElems) PCWSTR *prgsz, _In_ ULONG cElems, _Out_ VARIANT *pvar); -template -struct on_scope_exit { - on_scope_exit(Fn &&fn): _fn(std::move(fn)) {} - ~on_scope_exit() { this->_fn(); } - -private: - Fn _fn; -}; - extern "C" const char* ffBluetoothDetectBattery(FFlist* devices) { diff --git a/src/detection/camera/camera_windows.cpp b/src/detection/camera/camera_windows.cpp index a1b9f9763..91221374f 100644 --- a/src/detection/camera/camera_windows.cpp +++ b/src/detection/camera/camera_windows.cpp @@ -4,20 +4,12 @@ extern "C" { } #include "common/windows/com.hpp" #include "common/windows/unicode.hpp" +#include "common/windows/util.hpp" #include #include #include -template -struct on_scope_exit { - on_scope_exit(Fn &&fn): _fn(std::move(fn)) {} - ~on_scope_exit() { this->_fn(); } - -private: - Fn _fn; -}; - extern "C" const char* ffDetectCamera(FF_MAYBE_UNUSED FFlist* result) { diff --git a/src/detection/gpu/gpu_wsl.cpp b/src/detection/gpu/gpu_wsl.cpp index f2351a9e0..f000194e1 100644 --- a/src/detection/gpu/gpu_wsl.cpp +++ b/src/detection/gpu/gpu_wsl.cpp @@ -6,6 +6,7 @@ extern "C" { #include "detection/gpu/gpu.h" #include "detection/gpu/gpu_driver_specific.h" } +#include "common/windows/util.hpp" #include #include @@ -16,15 +17,6 @@ extern "C" { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" -template -struct on_scope_exit { - on_scope_exit(Fn &&fn): _fn(std::move(fn)) {} - ~on_scope_exit() { this->_fn(); } - -private: - Fn _fn; -}; - extern "C" const char* ffGPUDetectByDirectX(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* gpus) { diff --git a/src/detection/wm/wm_windows.cpp b/src/detection/wm/wm_windows.cpp index 793d57d51..84ee1bde2 100644 --- a/src/detection/wm/wm_windows.cpp +++ b/src/detection/wm/wm_windows.cpp @@ -7,22 +7,13 @@ extern "C" } #include "common/windows/com.hpp" +#include "common/windows/util.hpp" -#include #include #include #include #include -template -struct on_scope_exit { - on_scope_exit(Fn &&fn): _fn(std::move(fn)) {} - ~on_scope_exit() { this->_fn(); } - -private: - Fn _fn; -}; - extern "C" const char* ffDetectWMPlugin(FFstrbuf* pluginName) {