Skip to content

Commit 301731e

Browse files
committed
Replaced tinkerforge_exception with std::system_error and tinkerforge_error_category.
1 parent 0a0b7a9 commit 301731e

12 files changed

+151
-144
lines changed

power_overwhelming/include/visus/pwrowg/tinkerforge_display.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class POWER_OVERWHELMING_API tinkerforge_display final {
6565
/// is <c>nullptr</c>.</exceptions>
6666
/// <exception cref="std::bad_alloc">If the required resources could not
6767
/// be allocated.</exceptions>
68-
/// <exception cref="tinkerforge_exception">If the connection to the
68+
/// <exception cref="std::system_error">If the connection to the
6969
/// master brick could not be established.</exception>
7070
tinkerforge_display(_In_z_ const char *uid,
7171
_In_opt_z_ const char *host,

power_overwhelming/src/tinkerforge_configuration.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "string_functions.h"
1515
#include "tinkerforge_bricklet.h"
16-
#include "tinkerforge_exception.h"
1716
#include "tinkerforge_scope.h"
1817

1918

power_overwhelming/src/tinkerforge_display.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "tinkerforge_bricklet.h"
1414
#include "tinkerforge_display_impl.h"
15-
#include "tinkerforge_exception.h"
15+
#include "tinkerforge_error_category.h"
1616

1717

1818
/*
@@ -66,9 +66,7 @@ void PWROWG_NAMESPACE::tinkerforge_display::clear(void) {
6666
}
6767

6868
auto status = ::lcd_128x64_clear_display(&this->_impl->bricklet);
69-
if (status < 0) {
70-
throw PWROWG_DETAIL_NAMESPACE::tinkerforge_exception(status);
71-
}
69+
detail::throw_if_tinkerforge_failed(status);
7270
}
7371

7472

@@ -91,9 +89,7 @@ void PWROWG_NAMESPACE::tinkerforge_display::print(
9189

9290
auto status = ::lcd_128x64_draw_text(&this->_impl->bricklet, x, y, font,
9391
colour, text);
94-
if (status < 0) {
95-
throw PWROWG_DETAIL_NAMESPACE::tinkerforge_exception(status);
96-
}
92+
detail::throw_if_tinkerforge_failed(status);
9793
}
9894

9995

power_overwhelming/src/tinkerforge_exception.cpp renamed to power_overwhelming/src/tinkerforge_error_category.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
// <copyright file="tinkerforge_exception.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2021 - 2025 Visualisierungsinstitut der Universität Stuttgart.
1+
// <copyright file="tinkerforge_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>
55
// <author>Christoph Müller</author>
66

7-
#include "tinkerforge_exception.h"
7+
#include "tinkerforge_error_category.h"
8+
9+
#include <string>
810

911
#include <ip_connection.h>
1012

1113

12-
/// <summary>
13-
/// Get a human-readable error description for the given Tinkerforge error code.
14-
/// </summary>
15-
/// <param name="code"></param>
16-
/// <returns></returns>
17-
static std::string tinkerforge_to_string(const int code) {
18-
switch (code) {
14+
/*
15+
* PWROWG_DETAIL_NAMESPACE::tinkerforge_error_category::default_error_condition
16+
*/
17+
std::error_condition
18+
PWROWG_DETAIL_NAMESPACE::tinkerforge_error_category::default_error_condition(
19+
int status) const noexcept {
20+
return std::error_condition(status, tinkerforge_category());
21+
}
22+
23+
24+
/*
25+
* PWROWG_DETAIL_NAMESPACE::message
26+
*/
27+
std::string PWROWG_DETAIL_NAMESPACE::tinkerforge_error_category::message(
28+
int status) const {
29+
switch (status) {
1930
case E_OK:
2031
return "The operation completed successfully.";
2132

@@ -71,13 +82,32 @@ static std::string tinkerforge_to_string(const int code) {
7182
return "The response has an unexpected length.";
7283

7384
default:
74-
return std::to_string(code);
85+
return std::to_string(status);
7586
}
7687
}
7788

89+
90+
/*
91+
* PWROWG_DETAIL_NAMESPACE::tinkerforge_category
92+
*/
93+
const std::error_category& PWROWG_DETAIL_NAMESPACE::tinkerforge_category(
94+
void) noexcept {
95+
static const tinkerforge_error_category retval;
96+
return retval;
97+
}
98+
99+
78100
/*
79-
* PWROWG_DETAIL_NAMESPACE::tinkerforge_exception::tinkerforge_exception
101+
* PWROWG_DETAIL_NAMESPACE::throw_if_tinkerforge_failed
80102
*/
81-
PWROWG_DETAIL_NAMESPACE::tinkerforge_exception::tinkerforge_exception(
82-
const value_type code)
83-
: std::runtime_error(tinkerforge_to_string(code).c_str()), _code(code) { }
103+
void PWROWG_DETAIL_NAMESPACE::throw_if_tinkerforge_failed(
104+
_In_ const int status,
105+
_In_opt_z_ const char *message) {
106+
if (status < 0) {
107+
if (message == nullptr) {
108+
throw std::system_error(status, tinkerforge_category());
109+
} else {
110+
throw std::system_error(status, tinkerforge_category(), message);
111+
}
112+
}
113+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// <copyright file="tinkerforge_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_TINKERFORGE_ERROR_CATEGORY_H)
8+
#define _PWROWG_TINKERFORGE_ERROR_CATEGORY_H
9+
#pragma once
10+
11+
#include <system_error>
12+
13+
#include "visus/pwrowg/api.h"
14+
15+
16+
PWROWG_DETAIL_NAMESPACE_BEGIN
17+
18+
/// <summary>
19+
/// An error category for Tinkerforge errors.
20+
/// </summary>
21+
class PWROWG_TEST_API tinkerforge_error_category final
22+
: 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 Tinkerforge 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 "Tinkerforge";
50+
}
51+
};
52+
53+
54+
/// <summary>
55+
/// Answer the one and only <see cref="tinkerforge_error_category" />.
56+
/// </summary>
57+
/// <returns>The only instance of <see cref="tinkerforge_error_category" />.
58+
/// </returns>
59+
PWROWG_TEST_API const std::error_category& tinkerforge_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_tinkerforge_failed(_In_ const int status,
71+
_In_opt_z_ const char *message = nullptr);
72+
73+
PWROWG_DETAIL_NAMESPACE_END
74+
75+
#endif /* !defined(_PWROWG_TINKERFORGE_ERROR_CATEGORY_H) */

power_overwhelming/src/tinkerforge_exception.h

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

power_overwhelming/src/tinkerforge_scope.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <WinSock2.h>
1515
#endif /* defined(_WIN32) */
1616

17-
#include "tinkerforge_exception.h"
17+
#include "tinkerforge_error_category.h"
1818

1919

2020
/*
@@ -54,7 +54,7 @@ PWROWG_DETAIL_NAMESPACE::tinkerforge_scope::data::data(
5454
if (status < 0) {
5555
// Must deallocate if throwing after ipcon_create!
5656
::ipcon_destroy(&this->connection);
57-
throw tinkerforge_exception(status);
57+
throw std::system_error(status, tinkerforge_category());
5858
}
5959
}
6060

@@ -68,7 +68,7 @@ PWROWG_DETAIL_NAMESPACE::tinkerforge_scope::data::data(
6868
if (status < 0) {
6969
// Same as above: manually cleanup resources.
7070
::ipcon_destroy(&this->connection);
71-
throw tinkerforge_exception(status);
71+
throw std::system_error(status, tinkerforge_category());
7272
}
7373
}
7474
}

power_overwhelming/src/tinkerforge_scope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class PWROWG_TEST_API tinkerforge_scope final {
5151
/// </param>
5252
/// <param name="port">The port on which the Brick daemon is listening.
5353
/// </param>
54-
/// <exception cref="tinkerforge_exception">In case the connection could
54+
/// <exception cref="std::system_error">In case the connection could
5555
/// not be established.</exception>
5656
tinkerforge_scope(const std::string& host, const std::uint16_t port);
5757

0 commit comments

Comments
 (0)