diff --git a/DLL/Common/Socket.h b/DLL/Common/Socket.h index caa95d8..3a9d52c 100644 --- a/DLL/Common/Socket.h +++ b/DLL/Common/Socket.h @@ -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; +using Socket = UniqueHandle; } \ No newline at end of file diff --git a/DLL/Common/UniqueHandle.h b/DLL/Common/UniqueHandle.h index 86798e3..028b266 100644 --- a/DLL/Common/UniqueHandle.h +++ b/DLL/Common/UniqueHandle.h @@ -1,87 +1,18 @@ #pragma once +#include #include #include namespace winlirc { - template - 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 - 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 + struct GenericTraits { + using type = HandleType; + static inline constexpr auto close = Close; + static inline constexpr type invalid() noexcept { return InvalidValue; } }; + + template + using UniqueHandle = winrt::handle_type>; } diff --git a/DLL/DirectInput/directinput.cpp b/DLL/DirectInput/directinput.cpp index 8a2dd36..d27cce4 100644 --- a/DLL/DirectInput/directinput.cpp +++ b/DLL/DirectInput/directinput.cpp @@ -12,14 +12,7 @@ #include #include -struct HwndTraits -{ - typedef HWND HandleType; - constexpr static HandleType invalidValue() noexcept { return nullptr; } - static void close(HandleType h) noexcept { ::DestroyWindow(h); } -}; - -using Window = winlirc::UniqueHandle; +using Window = winlirc::UniqueHandle; CComPtr g_di; CComPtr g_diJoystick; @@ -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(); diff --git a/DLL/UDP/Server.cpp b/DLL/UDP/Server.cpp index 8861f45..759c6dd 100644 --- a/DLL/UDP/Server.cpp +++ b/DLL/UDP/Server.cpp @@ -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"); @@ -66,7 +66,7 @@ void Server::deinit() { KillThread(exitThread,threadHandle); - server.reset(); + server = {}; SAFE_CLOSE_HANDLE(exitThread); WSACleanup(); diff --git a/bootstrap.bat b/bootstrap.bat index 5ef1f4e..f089124 100644 --- a/bootstrap.bat +++ b/bootstrap.bat @@ -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 diff --git a/winlirc/Plugin.cpp b/winlirc/Plugin.cpp index 466d1bd..92d1584 100644 --- a/winlirc/Plugin.cpp +++ b/winlirc/Plugin.cpp @@ -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) diff --git a/winlirc/Plugin.h b/winlirc/Plugin.h index 5723e15..3047668 100644 --- a/winlirc/Plugin.h +++ b/winlirc/Plugin.h @@ -32,5 +32,5 @@ struct Plugin bool canRecord() const noexcept; plugin_interface interface_ = { 0 }; - Dll dllFile_; + winlirc::Dll dllFile_; }; diff --git a/winlirc/dll.h b/winlirc/dll.h index 2cb7aab..5c061d8 100644 --- a/winlirc/dll.h +++ b/winlirc/dll.h @@ -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; + using Dll = UniqueHandle; +} diff --git a/winlirc/server.cpp b/winlirc/server.cpp index 3a4db9b..2297836 100644 --- a/winlirc/server.cpp +++ b/winlirc/server.cpp @@ -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 @@ -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 */ diff --git a/winlirc/tests/CMakeLists.txt b/winlirc/tests/CMakeLists.txt index 2bd6f1a..74f3106 100644 --- a/winlirc/tests/CMakeLists.txt +++ b/winlirc/tests/CMakeLists.txt @@ -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) diff --git a/winlirc/tests/unique_handle_test.cpp b/winlirc/tests/unique_handle_test.cpp deleted file mode 100644 index a1813e0..0000000 --- a/winlirc/tests/unique_handle_test.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include - -using namespace winlirc; - -struct TestTraits -{ - using HandleType = int; - static constexpr HandleType invalidValue() noexcept { return 10; } - static void close(HandleType h) noexcept { } -}; - -using TestHandle = UniqueHandle; - -struct UniqueHandleTest : testing::Test -{ - static void SetUpTestSuite() { } - void SetUp() override { } - -}; - -TEST_F(UniqueHandleTest, default_handle_is_invalid) -{ - TestHandle h; - EXPECT_FALSE(h); -} - -TEST_F(UniqueHandleTest, handle_with_invalid_value_is_not_valid) -{ - TestHandle h{ TestTraits::invalidValue() }; - EXPECT_FALSE(h); -} - -TEST_F(UniqueHandleTest, handle_with_valid_value_is_valid) -{ - TestHandle h{ TestTraits::invalidValue()+1 }; - EXPECT_TRUE(h); -} - -TEST_F(UniqueHandleTest, release_invalidates) -{ - TestHandle h{ TestTraits::invalidValue() + 1 }; - h.release(); - EXPECT_FALSE(h); -} - -TEST_F(UniqueHandleTest, release_returns_old_value) -{ - TestHandle h{ TestTraits::invalidValue() + 1 }; - EXPECT_EQ(TestTraits::invalidValue() + 1, h.release()); -} - -struct TestTraits2 -{ - using HandleType = int; - static int invalidValue() { return 12; } - static void close(int h) { ++closeCount; } - static int closeCount; -}; -int TestTraits2::closeCount = 0; - -TEST_F(UniqueHandleTest, valid_handle_is_closed) -{ - TestTraits2::closeCount = 0; - { - UniqueHandle h{ TestTraits2::invalidValue() + 1 }; - } - EXPECT_EQ(1, TestTraits2::closeCount); -} - -TEST_F(UniqueHandleTest, valid_handle_is_not_closed_after_release) -{ - TestTraits2::closeCount = 0; - { - UniqueHandle h{ TestTraits2::invalidValue() + 1 }; - h.release(); - EXPECT_FALSE(h); - } - EXPECT_EQ(0, TestTraits2::closeCount); -}