diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2022-03-09 13:25:34 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2022-03-09 15:58:02 +0300 |
commit | 36744d8fb4a69ba1b1580d31b1c55458a7bc2210 (patch) | |
tree | 8828be25c8f96558a8f44c3fd94b499e05e2793a /src/common | |
parent | 04cb07ed9e18cd1aefaad4a1add9622d894e0f86 (diff) |
Move OcHandle from vfs module to core Utility::Handle
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/utility.h | 53 | ||||
-rw-r--r-- | src/common/utility_win.cpp | 27 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/common/utility.h b/src/common/utility.h index 2490a338e..821c6d861 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -274,6 +274,59 @@ OCSYNC_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcUtility) Q_DISABLE_COPY(NtfsPermissionLookupRAII); }; + + class OCSYNC_EXPORT Handle + { + public: + /** + * A RAAI for Windows Handles + */ + Handle() = default; + explicit Handle(HANDLE h); + explicit Handle(HANDLE h, std::function<void(HANDLE)> &&close); + + Handle(const Handle &) = delete; + Handle &operator=(const Handle &) = delete; + + Handle(Handle &&other) + { + std::swap(_handle, other._handle); + std::swap(_close, other._close); + } + + Handle &operator=(Handle &&other) + { + if (this != &other) { + std::swap(_handle, other._handle); + std::swap(_close, other._close); + } + return *this; + } + + ~Handle(); + + HANDLE &handle() + { + return _handle; + } + + void close(); + + explicit operator bool() const + { + return _handle != INVALID_HANDLE_VALUE; + } + + operator HANDLE() const + { + return _handle; + } + + private: + HANDLE _handle = INVALID_HANDLE_VALUE; + std::function<void(HANDLE)> _close; + }; + #endif template <class E> diff --git a/src/common/utility_win.cpp b/src/common/utility_win.cpp index a76bf97ef..56002b01c 100644 --- a/src/common/utility_win.cpp +++ b/src/common/utility_win.cpp @@ -319,4 +319,31 @@ Utility::NtfsPermissionLookupRAII::~NtfsPermissionLookupRAII() qt_ntfs_permission_lookup--; } + +Utility::Handle::Handle(HANDLE h, std::function<void(HANDLE)> &&close) + : _handle(h) + , _close(std::move(close)) +{ +} + +Utility::Handle::Handle(HANDLE h) + : _handle(h) + , _close(&CloseHandle) +{ +} + +Utility::Handle::~Handle() +{ + close(); +} + +void Utility::Handle::close() +{ + if (_handle != INVALID_HANDLE_VALUE) { + _close(_handle); + _handle = INVALID_HANDLE_VALUE; + } +} + + } // namespace OCC |