Skip to content

Commit 0a0b7a9

Browse files
committed
Replace adl_exception with std::system_error and adl_error_category.
1 parent cb7566a commit 0a0b7a9

File tree

12 files changed

+157
-134
lines changed

12 files changed

+157
-134
lines changed

power_overwhelming/src/adl_exception.cpp renamed to power_overwhelming/src/adl_error_category.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
// <copyright file="adl_exception.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2021 - 2025 Visualisierungsinstitut der Universität Stuttgart.
1+
// <copyright file="adl_error_category.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2+
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
5+
// <author>Christoph Müller</author>
56

6-
#include "adl_exception.h"
7+
#if defined(POWER_OVERWHELMING_WITH_ADL)
8+
#include "adl_error_category.h"
9+
10+
#include <string>
711

812
#include <adl_defines.h>
913

1014

11-
/// <summary>
12-
/// Get a human-readable error description for the given ADL error code.
13-
/// </summary>
14-
/// <param name="code"></param>
15-
/// <returns></returns>
16-
static std::string adl_to_string(const int code) {
17-
switch (code) {
15+
/*
16+
* PWROWG_DETAIL_NAMESPACE::adl_error_category::default_error_condition
17+
*/
18+
std::error_condition
19+
PWROWG_DETAIL_NAMESPACE::adl_error_category::default_error_condition(
20+
int status) const noexcept {
21+
return std::error_condition(status, adl_category());
22+
}
23+
24+
25+
/*
26+
* PWROWG_DETAIL_NAMESPACE::message
27+
*/
28+
std::string PWROWG_DETAIL_NAMESPACE::adl_error_category::message(
29+
int status) const {
30+
switch (status) {
1831
case ADL_OK_WAIT:
1932
return "Try again later.";
2033

@@ -77,34 +90,34 @@ static std::string adl_to_string(const int code) {
7790
return "The graphics drvier is incompatible with ADL.";
7891

7992
default:
80-
return std::to_string(code);
93+
return std::to_string(status);
8194
}
8295
}
8396

8497

8598
/*
86-
* PWROWG_DETAIL_NAMESPACE::adl_exception::check_error
99+
* PWROWG_DETAIL_NAMESPACE::adl_category
87100
*/
88-
bool PWROWG_DETAIL_NAMESPACE::adl_exception::check_error(
89-
_In_ const value_type status) {
90-
return (status < 0);
101+
const std::error_category& PWROWG_DETAIL_NAMESPACE::adl_category(
102+
void) noexcept {
103+
static const adl_error_category retval;
104+
return retval;
91105
}
92106

93107

94108
/*
95-
* PWROWG_DETAIL_NAMESPACE::adl_exception::throw_on_error
109+
* PWROWG_DETAIL_NAMESPACE::throw_if_adl_failed
96110
*/
97-
void PWROWG_DETAIL_NAMESPACE::adl_exception::throw_on_error(
98-
_In_ const value_type status) {
111+
void PWROWG_DETAIL_NAMESPACE::throw_if_adl_failed(
112+
_In_ const int status,
113+
_In_opt_z_ const char *message) {
99114
if (status < 0) {
100-
throw adl_exception(status);
115+
if (message == nullptr) {
116+
throw std::system_error(status, adl_category());
117+
} else {
118+
throw std::system_error(status, adl_category(), message);
119+
}
101120
}
102121
}
103122

104-
105-
/*
106-
* PWROWG_DETAIL_NAMESPACE::adl_exception::adl_exception
107-
*/
108-
PWROWG_DETAIL_NAMESPACE::adl_exception::adl_exception(
109-
_In_ const value_type code)
110-
: std::runtime_error(adl_to_string(code).c_str()), _code(code) { }
123+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// <copyright file="adl_error_category.h" company="Visualisierungsinstitut der Universität Stuttgart">
2+
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
3+
// Licensed under the MIT licence. See LICENCE file for details.
4+
// </copyright>
5+
// <author>Christoph Müller</author>
6+
7+
#if !defined(_PWROWG_ADL_ERROR_CATEGORY_H)
8+
#define _PWROWG_ADL_ERROR_CATEGORY_H
9+
#pragma once
10+
11+
#include <system_error>
12+
13+
#include "visus/pwrowg/api.h"
14+
15+
#if defined(POWER_OVERWHELMING_WITH_ADL)
16+
17+
PWROWG_DETAIL_NAMESPACE_BEGIN
18+
19+
/// <summary>
20+
/// An error category for ADL errors.
21+
/// </summary>
22+
class PWROWG_TEST_API adl_error_category final : public std::error_category {
23+
24+
public:
25+
26+
using std::error_category::error_category;
27+
28+
/// <summary>
29+
/// Converts the error code into a portable description.
30+
/// </summary>
31+
/// <param name="status">The error code to be converted.</param>
32+
/// <returns>The portable description of the ADL error.</returns>
33+
std::error_condition default_error_condition(
34+
_In_ int status) const noexcept override;
35+
36+
/// <summary>
37+
/// Convert the given error code into a string.
38+
/// </summary>
39+
/// <param name="status">The error code to get the message for.</param>
40+
/// <returns>The error message associated with the error code.
41+
/// </returns>
42+
std::string message(_In_ int status) const override;
43+
44+
/// <summary>
45+
/// Answer the name of the error category.
46+
/// </summary>
47+
/// <returns>The name of the category.</returns>
48+
inline const char *name(void) const noexcept override {
49+
return "ADL";
50+
}
51+
};
52+
53+
54+
/// <summary>
55+
/// Answer the one and only <see cref="adl_error_category" />.
56+
/// </summary>
57+
/// <returns>The only instance of <see cref="adl_error_category" />.
58+
/// </returns>
59+
PWROWG_TEST_API const std::error_category& adl_category(void) noexcept;
60+
61+
/// <summary>
62+
/// Check whether <paramref name="status" /> indicates failure, and if so,
63+
/// throw an exception.
64+
/// </summary>
65+
/// <param name="status">The status code to be checked.</param>
66+
/// <param name="message">An optional error message to be included in the
67+
/// exception.</param>
68+
/// <exception cref="std::system_error">If <paramref name="status" /> indicates
69+
/// failure.</exception>
70+
PWROWG_TEST_API void throw_if_adl_failed(_In_ const int status,
71+
_In_opt_z_ const char *message = nullptr);
72+
73+
PWROWG_DETAIL_NAMESPACE_END
74+
75+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */
76+
#endif /* !defined(_PWROWG_ADL_ERROR_CATEGORY_H) */

power_overwhelming/src/adl_exception.h

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

power_overwhelming/src/adl_scope.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// </copyright>
55
// <author>Christoph Müller</author>
66

7+
#if defined(POWER_OVERWHELMING_WITH_ADL)
78
#include "adl_scope.h"
89

910
#include <cassert>
1011
#include <cstdint>
1112

12-
#include "adl_exception.h"
13+
#include "adl_error_category.h"
1314
#include "amd_display_library.h"
1415

1516

@@ -37,9 +38,7 @@ void __stdcall PWROWG_DETAIL_NAMESPACE::adl_scope::deallocate(void **buffer) {
3738
PWROWG_DETAIL_NAMESPACE::adl_scope::adl_scope(void) : _handle(0) {
3839
auto status = amd_display_library::instance().ADL2_Main_Control_Create(
3940
adl_scope::allocate, 1, &this->_handle);
40-
if (status != ADL_OK) {
41-
throw adl_exception(status);
42-
}
41+
throw_if_adl_failed(status);
4342
}
4443

4544

@@ -49,3 +48,5 @@ PWROWG_DETAIL_NAMESPACE::adl_scope::adl_scope(void) : _handle(0) {
4948
PWROWG_DETAIL_NAMESPACE::adl_scope::~adl_scope(void) {
5049
amd_display_library::instance().ADL2_Main_Control_Destroy(this->_handle);
5150
}
51+
52+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */

power_overwhelming/src/adl_scope.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#if !defined(_PWROWG_ADL_SCOPE_H)
77
#define _PWROWG_ADL_SCOPE_H
88
#pragma once
9+
#if defined(POWER_OVERWHELMING_WITH_ADL)
910

1011
#include <adl_sdk.h>
1112

@@ -42,7 +43,7 @@ class PWROWG_TEST_API adl_scope final {
4243
/// <summary>
4344
/// Initialises a new instance.
4445
/// </summary>
45-
/// <exception cref="adl_exception">If the ADL could not be loaded.
46+
/// <exception cref="std::system_error">If the ADL could not be loaded.
4647
/// </exception>
4748
adl_scope(void);
4849

@@ -71,4 +72,5 @@ class PWROWG_TEST_API adl_scope final {
7172

7273
PWROWG_DETAIL_NAMESPACE_END
7374

75+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */
7476
#endif /* !defined(_PWROWG_ADL_SCOPE_H) */

power_overwhelming/src/adl_sensor.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55

6+
#if defined(POWER_OVERWHELMING_WITH_ADL)
67
#include "adl_sensor.h"
78

89
#include <algorithm>
910
#include <cassert>
1011
#include <cstring>
1112
#include <vector>
1213

14+
#include "adl_error_category.h"
1315
#include "adl_utils.h"
1416
#include "sensor_array_impl.h"
1517
#include "sensor_description_builder.h"
@@ -304,7 +306,7 @@ PWROWG_DETAIL_NAMESPACE::adl_sensor::adl_sensor(
304306
this->_scope,
305307
this->_adapter_index,
306308
&this->_device);
307-
adl_exception::throw_on_error(status);
309+
throw_if_adl_failed(status);
308310
}
309311

310312
// Store all the sources we are interested in the startup block that will
@@ -327,7 +329,7 @@ PWROWG_DETAIL_NAMESPACE::adl_sensor::adl_sensor(
327329
&this->_start_input,
328330
&this->_start_output,
329331
this->_device);
330-
adl_exception::throw_on_error(status);
332+
throw_if_adl_failed(status);
331333
}
332334
}
333335

@@ -447,6 +449,7 @@ bool PWROWG_DETAIL_NAMESPACE::adl_sensor::specialise(
447449
return true;
448450
}
449451

452+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */
450453

451454

452455

power_overwhelming/src/adl_sensor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if !defined(_PWROWG_ADL_SENSOR_H)
88
#define _PWROWG_ADL_SENSOR_H
99
#pragma once
10+
#if defined(POWER_OVERWHELMING_WITH_ADL)
1011

1112
#include <chrono>
1213
#include <list>
@@ -19,7 +20,6 @@
1920
#include "visus/pwrowg/sensor_filters.h"
2021

2122
#include "adl_scope.h"
22-
#include "adl_exception.h"
2323
#include "amd_display_library.h"
2424
#include "sensor_description_builder.h"
2525
#include "sensor_utilities.h"
@@ -127,7 +127,7 @@ class PWROWG_TEST_API adl_sensor final {
127127
/// <param name="index">The index of the first of the
128128
/// <paramref name="sources" />.</param>
129129
/// <returns></returns>
130-
/// <exception cref="adl_exception">If the specified device was not
130+
/// <exception cref="std::system_error">If the specified device was not
131131
/// found, or another error occurred in ADL.</exception>
132132
static inline std::shared_ptr<adl_sensor> from_index(
133133
_In_ const int adapter_index,
@@ -154,7 +154,7 @@ class PWROWG_TEST_API adl_sensor final {
154154
/// <exception cref="std::invalid_argument">If <paramref name="udid" />
155155
/// is <c>nullptr</c> or if it did not match exactly one device.
156156
/// </exception>
157-
/// <exception cref="adl_exception">If the specified device was not
157+
/// <exception cref="std::system_error">If the specified device was not
158158
/// found, or another error occurred in ADL.</exception>
159159
static std::shared_ptr<adl_sensor> from_udid(
160160
_In_z_ const char *udid,
@@ -176,7 +176,7 @@ class PWROWG_TEST_API adl_sensor final {
176176
/// <exception cref="std::invalid_argument">If <paramref name="udid" />
177177
/// is <c>nullptr</c> or if it did not match exactly one device.
178178
/// </exception>
179-
/// <exception cref="adl_exception">If the specified device was not
179+
/// <exception cref="std::system_error">If the specified device was not
180180
/// found, or another error occurred in ADL.</exception>
181181
static inline std::shared_ptr<adl_sensor> from_udid(
182182
_In_z_ const wchar_t *udid,
@@ -300,4 +300,5 @@ PWROWG_DETAIL_NAMESPACE_END
300300

301301
#include "adl_sensor.inl"
302302

303+
#endif /* defined(POWER_OVERWHELMING_WITH_ADL) */
303304
#endif /* defined(_PWROWG_ADL_SENSOR_H) */

0 commit comments

Comments
 (0)