Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions DLL/Common/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,5 @@

namespace winlirc
{
struct SocketTraits
{
using HandleType = SOCKET;

static HandleType invalidValue() noexcept
{
return INVALID_SOCKET;
}

static void close(HandleType h) noexcept
{
if (h != invalidValue())
::closesocket(h);
}
};

using Socket = UniqueHandle<SocketTraits>;
using Socket = UniqueHandle<SOCKET, ::closesocket, INVALID_SOCKET>;
}
87 changes: 9 additions & 78 deletions DLL/Common/UniqueHandle.h
Original file line number Diff line number Diff line change
@@ -1,87 +1,18 @@
#pragma once

#include <winrt/base.h>
#include <concepts>
#include <utility>

namespace winlirc
{
template <typename T>
concept HandleTraits =
requires(T x, T::HandleType h)
{
T::invalidValue();
T::close(h);
};

/// UniqueHandle is wrapper around a handle value and manages the
/// ownership of this handle. A handle could be file handle, window
/// handle, event, ...
///
/// @param HandleTraits - contains info about the handle being wrapped
/// HandleType - type of handle value
/// HandleType invalidValue() - function that returns the invalid
/// value for the handle
/// unspecified_type close(HandleType) - function to close a valid handle
///
template <HandleTraits T>
class UniqueHandle
{
protected:
using Traits = T;
using HandleType = typename Traits::HandleType;

public:
UniqueHandle(UniqueHandle const&) = delete;
void operator=(UniqueHandle const&) = delete;

/// Construct UniqueHandle, assume ownerhip of \a h.
explicit UniqueHandle(HandleType h = Traits::invalidValue()) noexcept
: handle_(h)
{ }

~UniqueHandle() noexcept
{
reset();
}

UniqueHandle(UniqueHandle&& other) noexcept
: handle_(other.release())
{ }

UniqueHandle& operator=(UniqueHandle&& rhs) noexcept
{
reset(rhs.release());
return *this;
}

HandleType get() const noexcept { return handle_; }

explicit operator bool() const noexcept
{
return handle_ != Traits::invalidValue();
}

/// Give up ownership of the handle.
/// @post get() == nullptr
HandleType release() noexcept
{
return std::exchange(handle_, Traits::invalidValue());
}

/// Give up ownership of the current handle, assume ownership of replacement.
/// @post get() == replacement
void reset(HandleType replacement = Traits::invalidValue()) noexcept
{
if (handle_ != replacement)
{
if (handle_ != Traits::invalidValue())
Traits::close(handle_);
handle_ = replacement;
}
}

private:

HandleType handle_;
template <typename HandleType, auto Close, HandleType InvalidValue = HandleType{}>
struct GenericTraits {
using type = HandleType;
static inline constexpr auto close = Close;
static inline constexpr type invalid() noexcept { return InvalidValue; }
};

template <typename HandleType, auto Close, HandleType InvalidValue = HandleType{} >
using UniqueHandle = winrt::handle_type<GenericTraits<HandleType, Close, InvalidValue>>;
}
11 changes: 2 additions & 9 deletions DLL/DirectInput/directinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
#include <utility>
#include <vector>

struct HwndTraits
{
typedef HWND HandleType;
constexpr static HandleType invalidValue() noexcept { return nullptr; }
static void close(HandleType h) noexcept { ::DestroyWindow(h); }
};

using Window = winlirc::UniqueHandle<HwndTraits>;
using Window = winlirc::UniqueHandle<HWND, ::DestroyWindow>;

CComPtr<IDirectInput8> g_di;
CComPtr<IDirectInputDevice8> g_diJoystick;
Expand Down Expand Up @@ -99,7 +92,7 @@ WL_API void deinit()
if (g_initialized)
{
g_diJoystick.Release();
g_window.reset();
g_window = {};
g_di.Release();
g_initialized = false;
::CoUninitialize();
Expand Down
4 changes: 2 additions & 2 deletions DLL/UDP/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int Server::init() {
return 0; // win sock version not supported
}

server.reset(socket(AF_INET, SOCK_DGRAM, 0));
server.attach(socket(AF_INET, SOCK_DGRAM, 0));

if(!server) {
//printf("failed socket\n");
Expand Down Expand Up @@ -66,7 +66,7 @@ void Server::deinit() {

KillThread(exitThread,threadHandle);

server.reset();
server = {};
SAFE_CLOSE_HANDLE(exitThread);

WSACleanup();
Expand Down
2 changes: 1 addition & 1 deletion bootstrap.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if not exist vcpkg.exe (call bootstrap-vcpkg.bat)
popd

mkdir %build_dir%
echo "*" > %build_dir%\.gitignore
echo * > %build_dir%\.gitignore

for /f "usebackq tokens=*" %%a in (`where cmake.exe`) do (
set cmake=%%a
Expand Down
2 changes: 1 addition & 1 deletion winlirc/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Plugin::Plugin(std::wstring const& path) noexcept
: dllFile_ { LoadLibraryW(path.c_str()) }
{
if (Dll& d = dllFile_)
if (auto& d = dllFile_)
{
auto getPluginInterface = (GetPluginInterfaceFunction)GetProcAddress(d.get(), "getPluginInterface");
if (getPluginInterface)
Expand Down
2 changes: 1 addition & 1 deletion winlirc/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ struct Plugin
bool canRecord() const noexcept;

plugin_interface interface_ = { 0 };
Dll dllFile_;
winlirc::Dll dllFile_;
};
17 changes: 3 additions & 14 deletions winlirc/dll.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@

#include "../DLL/Common/UniqueHandle.h"

struct DllTraits
namespace winlirc
{
using HandleType = HMODULE;
static constexpr HandleType invalidValue() noexcept
{
return nullptr;
}

static void close(HandleType h) noexcept
{
::FreeLibrary(h);
}
};

using Dll = winlirc::UniqueHandle<DllTraits>;
using Dll = UniqueHandle<HMODULE, ::FreeLibrary>;
}
6 changes: 3 additions & 3 deletions winlirc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ void Cserver::stopServer()

for (auto& client : m_clients)
{
client.reset();
client = {};
}

m_server.reset();
m_server = {};
}

void Cserver::sendToClients(std::string_view s) const
Expand Down Expand Up @@ -244,7 +244,7 @@ void Cserver::ThreadProc()
if (bytes == 0 || bytes == SOCKET_ERROR)
{
/* Connection was closed or something's screwy */
m_clients[i].reset();
m_clients[i] = {};
WL_DEBUG("Client connection %d closed\n", i);
}
else /* bytes > 0, we read data */
Expand Down
1 change: 0 additions & 1 deletion winlirc/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ add_executable(winlirc_tests
test.cpp
config_test.cpp
remote_test.cpp
unique_handle_test.cpp
../config.cpp
../wl_string.cpp)

Expand Down
81 changes: 0 additions & 81 deletions winlirc/tests/unique_handle_test.cpp

This file was deleted.