Refactor
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
class UniqueHandle
|
||||
{
|
||||
public:
|
||||
explicit UniqueHandle(HANDLE handle = NULL) : mHandle(handle) {}
|
||||
~UniqueHandle() { reset(); }
|
||||
|
||||
UniqueHandle(const UniqueHandle&) = delete;
|
||||
UniqueHandle& operator=(const UniqueHandle&) = delete;
|
||||
|
||||
UniqueHandle(UniqueHandle&& other) noexcept : mHandle(other.release()) {}
|
||||
|
||||
UniqueHandle& operator=(UniqueHandle&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
reset(other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
HANDLE get() const { return mHandle; }
|
||||
bool valid() const { return mHandle != NULL && mHandle != INVALID_HANDLE_VALUE; }
|
||||
|
||||
HANDLE release()
|
||||
{
|
||||
HANDLE handle = mHandle;
|
||||
mHandle = NULL;
|
||||
return handle;
|
||||
}
|
||||
|
||||
void reset(HANDLE handle = NULL)
|
||||
{
|
||||
if (valid())
|
||||
CloseHandle(mHandle);
|
||||
mHandle = handle;
|
||||
}
|
||||
|
||||
private:
|
||||
HANDLE mHandle;
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <winsock2.h>
|
||||
|
||||
class UniqueSocket
|
||||
{
|
||||
public:
|
||||
explicit UniqueSocket(SOCKET socket = INVALID_SOCKET) : mSocket(socket) {}
|
||||
~UniqueSocket() { reset(); }
|
||||
|
||||
UniqueSocket(const UniqueSocket&) = delete;
|
||||
UniqueSocket& operator=(const UniqueSocket&) = delete;
|
||||
|
||||
UniqueSocket(UniqueSocket&& other) noexcept : mSocket(other.release()) {}
|
||||
|
||||
UniqueSocket& operator=(UniqueSocket&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
reset(other.release());
|
||||
return *this;
|
||||
}
|
||||
|
||||
SOCKET get() const { return mSocket; }
|
||||
bool valid() const { return mSocket != INVALID_SOCKET; }
|
||||
|
||||
SOCKET release()
|
||||
{
|
||||
SOCKET socket = mSocket;
|
||||
mSocket = INVALID_SOCKET;
|
||||
return socket;
|
||||
}
|
||||
|
||||
void reset(SOCKET socket = INVALID_SOCKET)
|
||||
{
|
||||
if (valid())
|
||||
closesocket(mSocket);
|
||||
mSocket = socket;
|
||||
}
|
||||
|
||||
private:
|
||||
SOCKET mSocket;
|
||||
};
|
||||
Reference in New Issue
Block a user