Skip to content

Commit 71aab1a

Browse files
committed
Use std::system_error consistently
1 parent f05ee9f commit 71aab1a

File tree

8 files changed

+14
-60
lines changed

8 files changed

+14
-60
lines changed

src/flexasio/FlexASIO/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ add_library(FlexASIO_control_panel STATIC EXCLUDE_FROM_ALL control_panel.cpp)
4444
target_link_libraries(FlexASIO_control_panel
4545
PRIVATE FlexASIO_log
4646
PRIVATE FlexASIOUtil_windows_com
47-
PRIVATE FlexASIOUtil_windows_error
4847
PRIVATE FlexASIOUtil_windows_registry
4948
PRIVATE FlexASIOUtil_windows_string
5049
PRIVATE dechamps_CMakeUtils_version

src/flexasio/FlexASIO/control_panel.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "control_panel.h"
44

55
#include "../FlexASIOUtil/windows_com.h"
6-
#include "../FlexASIOUtil/windows_error.h"
76
#include "../FlexASIOUtil/windows_registry.h"
87
#include "../FlexASIOUtil/windows_string.h"
98

@@ -14,6 +13,8 @@
1413

1514
#include <Windows.h>
1615

16+
#include <system_error>
17+
1718
namespace flexasio {
1819

1920
namespace {
@@ -34,7 +35,7 @@ namespace flexasio {
3435

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

4041
std::wstring GetStringRegistryValue(HKEY registryKey, LPCWSTR valueName) {
@@ -49,7 +50,7 @@ namespace flexasio {
4950
value.resize(valueSize);
5051
continue;
5152
}
52-
if (regQueryValueError != ERROR_SUCCESS) throw std::runtime_error("Unable to query string registry value: " + GetWindowsErrorString(regQueryValueError));
53+
if (regQueryValueError != ERROR_SUCCESS) throw std::system_error(regQueryValueError, std::system_category(), "Unable to query string registry value");
5354
Log() << "Registry value size: " << valueSize;
5455
if (valueType != REG_SZ) throw std::runtime_error("Expected string registry value type, got " + std::to_string(valueType));
5556
value.resize(valueSize);
@@ -67,7 +68,7 @@ namespace flexasio {
6768
UniqueHKEY OpenFlexAsioGuiInstallRegistryKey() {
6869
HKEY registryKey;
6970
const auto regOpenKeyError = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Fabrikat\\FlexASIOGUI\\Install", {}, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &registryKey);
70-
if (regOpenKeyError != ERROR_SUCCESS) throw std::runtime_error("Unable to open FlexASIOGUI registry key: " + GetWindowsErrorString(regOpenKeyError));
71+
if (regOpenKeyError != ERROR_SUCCESS) throw std::system_error(regOpenKeyError, std::system_category(), "Unable to open FlexASIOGUI registry key");
7172
return UniqueHKEY(registryKey);
7273
}
7374

src/flexasio/FlexASIOUtil/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ target_link_libraries(FlexASIOUtil_portaudio
77
add_library(FlexASIOUtil_shell STATIC shell.cpp)
88

99
add_library(FlexASIOUtil_windows_com STATIC windows_com.cpp)
10-
target_link_libraries(FlexASIOUtil_windows_com
11-
PRIVATE FlexASIOUtil_windows_error
12-
)
13-
14-
add_library(FlexASIOUtil_windows_error STATIC windows_error.cpp)
1510

1611
add_library(FlexASIOUtil_windows_registry STATIC windows_registry.cpp)
17-
target_link_libraries(FlexASIOUtil_windows_registry
18-
PRIVATE FlexASIOUtil_windows_error
19-
)
2012

2113
add_library(FlexASIOUtil_windows_string STATIC windows_string.cpp)
22-
target_link_libraries(FlexASIOUtil_windows_string
23-
PRIVATE FlexASIOUtil_windows_error
24-
)

src/flexasio/FlexASIOUtil/windows_com.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#include "windows_com.h"
22

3-
#include "windows_error.h"
4-
53
#include <stdexcept>
4+
#include <system_error>
65

76
namespace flexasio {
87

98
COMInitializer::COMInitializer(DWORD coInit) {
109
const auto coInitializeResult = CoInitializeEx(NULL, coInit);
1110
if (FAILED(coInitializeResult))
12-
throw std::runtime_error("Failed to initialize COM: " + GetWindowsErrorString(coInitializeResult));
11+
throw std::system_error(coInitializeResult, std::system_category(), "Failed to initialize COM");
1312
}
1413

1514
COMInitializer::~COMInitializer() {

src/flexasio/FlexASIOUtil/windows_error.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/flexasio/FlexASIOUtil/windows_error.h

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#include "windows_registry.h"
22

3-
#include "windows_error.h"
4-
53
#include <stdexcept>
4+
#include <system_error>
65

76
namespace flexasio {
87

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

1514
}

src/flexasio/FlexASIOUtil/windows_string.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
#include "windows_string.h"
22

3-
#include "windows_error.h"
4-
53
#include <Windows.h>
64

75
#include <stdexcept>
6+
#include <system_error>
87

98
namespace flexasio {
109

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

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

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

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

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

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

0 commit comments

Comments
 (0)