Skip to content

Commit

Permalink
Move OcHandle from vfs module to core Utility::Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Mar 9, 2022
1 parent 95c9b4d commit 868df3c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
27 changes: 27 additions & 0 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 868df3c

Please sign in to comment.