Skip to content

Commit 193fe11

Browse files
committed
Remove template-based type of TraceableException
1 parent 51ef54b commit 193fe11

File tree

5 files changed

+21
-42
lines changed

5 files changed

+21
-42
lines changed

src/ystdlib/error_handling/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ cpp_library(
44
PUBLIC_HEADERS
55
ErrorCode.hpp
66
TraceableException.hpp
7-
types.hpp
87
utils.hpp
98
TESTS_SOURCES
109
test/constants.hpp

src/ystdlib/error_handling/TraceableException.hpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <system_error>
99
#include <utility>
1010

11-
#include "types.hpp"
1211
#include "utils.hpp"
1312

1413
namespace ystdlib::error_handling {
@@ -23,43 +22,42 @@ namespace ystdlib::error_handling {
2322
* @see std::source_location::function_name()
2423
* @see std::source_location::line()
2524
*/
26-
template <ErrorCodeType ErrorType = std::error_code>
2725
class TraceableException : public std::exception {
2826
public:
2927
// Constructors
3028
explicit TraceableException(
31-
ErrorType error_code,
29+
std::error_code error_code,
3230
std::source_location const& where = std::source_location::current()
3331
)
34-
: m_error_code{std::move(error_code)},
32+
: m_error_code{error_code},
3533
m_where{where} {
3634
std::ostringstream oss;
3735
oss << where;
3836
m_what = oss.str();
3937
}
4038

4139
explicit TraceableException(
42-
ErrorType error_code,
40+
std::error_code error_code,
4341
std::string what,
4442
std::source_location const& where = std::source_location::current()
4543
)
46-
: m_error_code{std::move(error_code)},
44+
: m_error_code{error_code},
4745
m_what{std::move(what)},
4846
m_where{where} {}
4947

5048
// Methods implementing std::exception
5149
[[nodiscard]] auto what() const noexcept -> char const* override { return m_what.c_str(); }
5250

5351
// Methods
54-
[[nodiscard]] auto error_code() const -> ErrorType { return m_error_code; }
52+
[[nodiscard]] auto error_code() const -> std::error_code { return m_error_code; }
5553

5654
[[nodiscard]] auto what() -> std::string& { return m_what; }
5755

5856
[[nodiscard]] auto where() const noexcept -> std::source_location const& { return m_where; }
5957

6058
private:
6159
// Variables
62-
ErrorType m_error_code;
60+
std::error_code m_error_code;
6361
std::string m_what;
6462
std::source_location m_where;
6563
};
@@ -70,13 +68,8 @@ class TraceableException : public std::exception {
7068
*/
7169
// NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
7270
#define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(T) \
73-
class T : public ystdlib::error_handling::TraceableException<> { \
74-
using ystdlib::error_handling::TraceableException<>::TraceableException; \
75-
}
76-
77-
#define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION_WITH_ERROR_TYPE(T, E) \
78-
class T : public ystdlib::error_handling::TraceableException<E> { \
79-
using ystdlib::error_handling::TraceableException<E>::TraceableException; \
71+
class T : public ystdlib::error_handling::TraceableException { \
72+
using ystdlib::error_handling::TraceableException::TraceableException; \
8073
}
8174
// NOLINTEND(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
8275

src/ystdlib/error_handling/test/test_TraceableException.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
#include <cstring>
33

44
#include <ystdlib/error_handling/TraceableException.hpp>
5-
#include <ystdlib/error_handling/types.hpp>
65

76
#include <catch2/catch_test_macros.hpp>
87

98
#include "types.hpp"
109

11-
using ystdlib::error_handling::ErrorCodeType;
1210
using ystdlib::error_handling::TraceableException;
1311

1412
namespace {
@@ -41,14 +39,14 @@ class Worker {
4139
} // namespace ystdlib::error_handling::test
4240

4341
namespace {
44-
template <ErrorCodeType ErrorType = std::error_code, typename Callable>
45-
[[nodiscard]] auto capture_exception(Callable&& f) -> TraceableException<ErrorType>;
42+
template <typename Callable>
43+
[[nodiscard]] auto capture_exception(Callable&& f) -> TraceableException;
4644

47-
template <ErrorCodeType ErrorType, typename Callable>
48-
auto capture_exception(Callable&& f) -> TraceableException<ErrorType> {
45+
template <typename Callable>
46+
auto capture_exception(Callable&& f) -> TraceableException {
4947
try {
5048
std::forward<Callable>(f)();
51-
} catch (TraceableException<ErrorType>& e) {
49+
} catch (TraceableException& e) {
5250
return e;
5351
}
5452
assert(false && "The function is expected to throw.");
@@ -57,11 +55,11 @@ auto capture_exception(Callable&& f) -> TraceableException<ErrorType> {
5755

5856
namespace ystdlib::error_handling::test {
5957
TEST_CASE("test_traceable_exception", "[error_handling][TraceableException]") {
60-
auto const ex_success{capture_exception<>(Worker::execute_with_success)};
58+
auto const ex_success{capture_exception(Worker::execute_with_success)};
6159
REQUIRE(std::string{ex_success.where().file_name()}.ends_with(cCurrentFileName));
6260
REQUIRE((0 == std::strcmp(ex_success.where().function_name(), cSuccessFuncName)));
6361

64-
auto const ex_failure{capture_exception<>(Worker::execute_with_failure)};
62+
auto const ex_failure{capture_exception(Worker::execute_with_failure)};
6563
REQUIRE((0 == std::strcmp(ex_failure.what(), cCustomFailureDescription)));
6664
REQUIRE(std::string{ex_failure.where().file_name()}.ends_with(cCurrentFileName));
6765
REQUIRE((0 == std::strcmp(ex_failure.where().function_name(), cFailureFuncName)));

src/ystdlib/error_handling/types.hpp

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

src/ystdlib/error_handling/utils.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
#include <source_location>
66

77
namespace ystdlib::error_handling {
8+
/**
9+
* Writes a formatted representation of `std::source_location` to the output stream.
10+
*
11+
* @param os
12+
* @param where
13+
*/
814
inline auto operator<<(std::ostream& os, std::source_location const& where) -> std::ostream& {
915
return os << where.file_name() << "(" << where.line() << ":" << where.column()
1016
<< "), function `" << where.function_name() << "`";

0 commit comments

Comments
 (0)