Common: add on_scope_exit utility for scope-based resource management

This commit is contained in:
李通洲
2026-01-13 18:33:02 +08:00
parent 49fc95689d
commit e6bd1c340f
5 changed files with 23 additions and 37 deletions
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <utility>
#include <type_traits>
template <typename Fn>
struct on_scope_exit {
static_assert(std::is_nothrow_move_constructible<Fn>::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;
};
@@ -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 <typename Fn>
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)
{
+1 -9
View File
@@ -4,20 +4,12 @@ extern "C" {
}
#include "common/windows/com.hpp"
#include "common/windows/unicode.hpp"
#include "common/windows/util.hpp"
#include <initguid.h>
#include <mfapi.h>
#include <mfidl.h>
template <typename Fn>
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)
{
+1 -9
View File
@@ -6,6 +6,7 @@ extern "C" {
#include "detection/gpu/gpu.h"
#include "detection/gpu/gpu_driver_specific.h"
}
#include "common/windows/util.hpp"
#include <wsl/winadapter.h>
#include <directx/dxcore.h>
@@ -16,15 +17,6 @@ extern "C" {
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
template <typename Fn>
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)
{
+1 -10
View File
@@ -7,22 +7,13 @@ extern "C"
}
#include "common/windows/com.hpp"
#include "common/windows/util.hpp"
#include <utility>
#include <windows.h>
#include <tlhelp32.h>
#include <shlobj.h>
#include <propkey.h>
template <typename Fn>
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)
{