File tree Expand file tree Collapse file tree 4 files changed +22
-8
lines changed Expand file tree Collapse file tree 4 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,14 @@ project(error)
5
5
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic" )
6
6
set (CMAKE_CXX_STANDARD 11 )
7
7
8
+ include (cmake/CPM.cmake )
9
+ cpmaddpackage ("gh:fmtlib/fmt#10.0.0" )
10
+
8
11
add_library (error src/error.cpp )
9
12
target_include_directories (error PUBLIC include )
13
+ target_link_libraries (error PUBLIC fmt )
10
14
11
15
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
12
- include (cmake/CPM.cmake )
13
16
cpmaddpackage ("gh:TheLartians/Format.cmake@1.7.3" )
14
17
15
18
if (BUILD_TESTING )
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
+ #include < fmt/core.h>
4
+
3
5
#include < exception>
4
6
#include < string>
7
+ #include < utility>
5
8
6
9
namespace error {
7
10
@@ -14,10 +17,13 @@ class Error : public std::exception {
14
17
15
18
public:
16
19
/* *
17
- * @brief Constructs a new error with the given message.
18
- * @param message An error message.
20
+ * @brief Constructs a new error with the given format for the message.
21
+ * @param fmt A format string for the message.
22
+ * @param args Format arguments.
19
23
*/
20
- Error (const char * message);
24
+ template <typename ... T>
25
+ Error (fmt::format_string<T...> fmt, T&&... args)
26
+ : message(fmt::format(fmt, std::forward<T>(args)...)) {}
21
27
22
28
/* *
23
29
* @brief Returns the explanatory string.
Original file line number Diff line number Diff line change 2
2
3
3
namespace error {
4
4
5
- Error::Error (const char * message) : message(message) {}
6
-
7
5
const char * Error::what () const noexcept { return message.c_str (); }
8
6
9
7
} // namespace error
Original file line number Diff line number Diff line change 3
3
#include < string>
4
4
5
5
TEST_CASE (" Error Construction" ) {
6
- const error::Error err (" unknown error" );
7
- REQUIRE (std::string (" unknown error" ) == err.what ());
6
+ SECTION (" With one argument" ) {
7
+ const error::Error err (" unknown error" );
8
+ REQUIRE (std::string (" unknown error" ) == err.what ());
9
+ }
10
+
11
+ SECTION (" With one or more arguments" ) {
12
+ const error::Error err (" HTTP error {}" , 404 );
13
+ REQUIRE (std::string (" HTTP error 404" ) == err.what ());
14
+ }
8
15
}
9
16
10
17
TEST_CASE (" Error Throwing and Catching" ) {
You can’t perform that action at this time.
0 commit comments