Skip to content

Commit

Permalink
Use std::system_error consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
dechamps committed May 26, 2024
1 parent f05ee9f commit 71aab1a
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 60 deletions.
1 change: 0 additions & 1 deletion src/flexasio/FlexASIO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ add_library(FlexASIO_control_panel STATIC EXCLUDE_FROM_ALL control_panel.cpp)
target_link_libraries(FlexASIO_control_panel
PRIVATE FlexASIO_log
PRIVATE FlexASIOUtil_windows_com
PRIVATE FlexASIOUtil_windows_error
PRIVATE FlexASIOUtil_windows_registry
PRIVATE FlexASIOUtil_windows_string
PRIVATE dechamps_CMakeUtils_version
Expand Down
9 changes: 5 additions & 4 deletions src/flexasio/FlexASIO/control_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "control_panel.h"

#include "../FlexASIOUtil/windows_com.h"
#include "../FlexASIOUtil/windows_error.h"
#include "../FlexASIOUtil/windows_registry.h"
#include "../FlexASIOUtil/windows_string.h"

Expand All @@ -14,6 +13,8 @@

#include <Windows.h>

#include <system_error>

namespace flexasio {

namespace {
Expand All @@ -34,7 +35,7 @@ namespace flexasio {

Log() << "Executing: " << ConvertToUTF8(file);
if (!::ShellExecuteW(windowHandle, NULL, file.c_str(), NULL, NULL, SW_SHOWNORMAL))
throw std::runtime_error("Execution failed: " + GetWindowsErrorString(::GetLastError()));
throw std::system_error(::GetLastError(), std::system_category(), "ShellExecuteW failed");
}

std::wstring GetStringRegistryValue(HKEY registryKey, LPCWSTR valueName) {
Expand All @@ -49,7 +50,7 @@ namespace flexasio {
value.resize(valueSize);
continue;
}
if (regQueryValueError != ERROR_SUCCESS) throw std::runtime_error("Unable to query string registry value: " + GetWindowsErrorString(regQueryValueError));
if (regQueryValueError != ERROR_SUCCESS) throw std::system_error(regQueryValueError, std::system_category(), "Unable to query string registry value");
Log() << "Registry value size: " << valueSize;
if (valueType != REG_SZ) throw std::runtime_error("Expected string registry value type, got " + std::to_string(valueType));
value.resize(valueSize);
Expand All @@ -67,7 +68,7 @@ namespace flexasio {
UniqueHKEY OpenFlexAsioGuiInstallRegistryKey() {
HKEY registryKey;
const auto regOpenKeyError = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Fabrikat\\FlexASIOGUI\\Install", {}, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &registryKey);
if (regOpenKeyError != ERROR_SUCCESS) throw std::runtime_error("Unable to open FlexASIOGUI registry key: " + GetWindowsErrorString(regOpenKeyError));
if (regOpenKeyError != ERROR_SUCCESS) throw std::system_error(regOpenKeyError, std::system_category(), "Unable to open FlexASIOGUI registry key");
return UniqueHKEY(registryKey);
}

Expand Down
11 changes: 0 additions & 11 deletions src/flexasio/FlexASIOUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@ target_link_libraries(FlexASIOUtil_portaudio
add_library(FlexASIOUtil_shell STATIC shell.cpp)

add_library(FlexASIOUtil_windows_com STATIC windows_com.cpp)
target_link_libraries(FlexASIOUtil_windows_com
PRIVATE FlexASIOUtil_windows_error
)

add_library(FlexASIOUtil_windows_error STATIC windows_error.cpp)

add_library(FlexASIOUtil_windows_registry STATIC windows_registry.cpp)
target_link_libraries(FlexASIOUtil_windows_registry
PRIVATE FlexASIOUtil_windows_error
)

add_library(FlexASIOUtil_windows_string STATIC windows_string.cpp)
target_link_libraries(FlexASIOUtil_windows_string
PRIVATE FlexASIOUtil_windows_error
)
5 changes: 2 additions & 3 deletions src/flexasio/FlexASIOUtil/windows_com.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "windows_com.h"

#include "windows_error.h"

#include <stdexcept>
#include <system_error>

namespace flexasio {

COMInitializer::COMInitializer(DWORD coInit) {
const auto coInitializeResult = CoInitializeEx(NULL, coInit);
if (FAILED(coInitializeResult))
throw std::runtime_error("Failed to initialize COM: " + GetWindowsErrorString(coInitializeResult));
throw std::system_error(coInitializeResult, std::system_category(), "Failed to initialize COM");
}

COMInitializer::~COMInitializer() {
Expand Down
21 changes: 0 additions & 21 deletions src/flexasio/FlexASIOUtil/windows_error.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions src/flexasio/FlexASIOUtil/windows_error.h

This file was deleted.

5 changes: 2 additions & 3 deletions src/flexasio/FlexASIOUtil/windows_registry.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "windows_registry.h"

#include "windows_error.h"

#include <stdexcept>
#include <system_error>

namespace flexasio {

void HKEYDeleter::operator()(HKEY hkey) const {
const auto regCloseKeyError = ::RegCloseKey(hkey);
if (regCloseKeyError != ERROR_SUCCESS)
throw std::runtime_error("Unable to close registry key: " + GetWindowsErrorString(regCloseKeyError));
throw std::system_error(regCloseKeyError, std::system_category(), "Unable to close registry key");
}

}
11 changes: 5 additions & 6 deletions src/flexasio/FlexASIOUtil/windows_string.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#include "windows_string.h"

#include "windows_error.h"

#include <Windows.h>

#include <stdexcept>
#include <system_error>

namespace flexasio {

std::string ConvertToUTF8(std::wstring_view input) {
if (input.size() == 0) return {};

const auto size = ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, input.data(), static_cast<int>(input.size()), NULL, 0, NULL, NULL);
if (size <= 0) throw std::runtime_error("Unable to get size for string conversion to UTF-8: " + GetWindowsErrorString(::GetLastError()));
if (size <= 0) throw std::system_error(::GetLastError(), std::system_category(), "Unable to get size for string conversion to UTF-8");

std::string result(size, 0);
if (::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, input.data(), int(input.size()), result.data(), int(result.size()), NULL, NULL) != int(result.size()))
throw std::runtime_error("Unable to convert string to UTF-8: " + GetWindowsErrorString(::GetLastError()));
throw std::system_error(::GetLastError(), std::system_category(), "Unable to convert string to UTF-8");
return result;
}

std::wstring ConvertFromUTF8(std::string_view input) {
if (input.size() == 0) return {};

const auto size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, input.data(), int(input.size()), NULL, 0);
if (size <= 0) throw std::runtime_error("Unable to get size for string conversion from UTF-8: " + GetWindowsErrorString(::GetLastError()));
if (size <= 0) throw std::system_error(::GetLastError(), std::system_category(), "Unable to get size for string conversion from UTF-8");

std::wstring result(size, 0);
if (::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, input.data(), int(input.size()), result.data(), int(result.size())) != int(result.size()))
throw std::runtime_error("Unable to convert string from UTF-8: " + GetWindowsErrorString(::GetLastError()));
throw std::system_error(::GetLastError(), std::system_category(), "Unable to convert string from UTF-8");
return result;
}

Expand Down

0 comments on commit 71aab1a

Please sign in to comment.