From 4758f9e97294ed62d50a1bcf8848b7714b76237a Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Mon, 11 Nov 2024 23:10:53 -0500 Subject: [PATCH] refactor: change `DEF_EX` macro's underlying implementation to template --- .clang-tidy | 7 +++++- src/common/ex.hh | 65 ++++++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 689c8aa3..1447cfea 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -17,7 +17,6 @@ Checks: > portability-*, readability-*, -bugprone-easily-swappable-parameters, - -bugprone-reserved-identifier, -cppcoreguidelines-owning-memory, -cppcoreguidelines-prefer-member-initializer, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, @@ -44,5 +43,11 @@ CheckOptions: value: 1 - key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors value: 1 + - key: modernize-avoid-c-arrays.AllowStringArrays + value: 1 + - key: cppcoreguidelines-avoid-c-arrays.AllowStringArrays + value: 1 + - key: hicpp-avoid-c-arrays.AllowStringArrays + value: 1 ... diff --git a/src/common/ex.hh b/src/common/ex.hh index 233f8c40..aec624b0 100644 --- a/src/common/ex.hh +++ b/src/common/ex.hh @@ -4,22 +4,17 @@ #pragma once #include +#include + +// clang-format off /// A way to declare an exception class fast /// Do like this: /// DEF_EX( exErrorInFoo, "An error in foo encountered", std::exception ) /// DEF_EX( exFooNotFound, "Foo was not found", exErrorInFoo ) - -#define DEF_EX( exName, exDescription, exParent ) \ - class exName: public exParent \ - { \ - public: \ - virtual const char * what() const noexcept \ - { \ - return ( exDescription ); \ - } \ - virtual ~exName() noexcept {} \ - }; +#define DEF_EX( exName, exDescription, exParent ) \ + constexpr static char ExStr_## exName[] = exDescription; \ + using exName = defineEx< exParent, ExStr_## exName >; /// Same as DEF_EX, but takes a runtime string argument, which gets concatenated /// with the description. @@ -29,20 +24,36 @@ /// throw exCantOpen( "example.txt" ); /// /// what() would return "can't open file example.txt" +/// +#define DEF_EX_STR( exName, exDescription, exParent ) \ + constexpr static char ExStr_## exName[] = exDescription; \ + using exName = defineExStr< exParent, ExStr_## exName >; -#define DEF_EX_STR( exName, exDescription, exParent ) \ - class exName: public exParent \ - { \ - std::string value; \ - \ - public: \ - explicit exName( std::string const & value_ ): \ - value( std::string( exDescription ) + " " + value_ ) \ - { \ - } \ - virtual const char * what() const noexcept \ - { \ - return value.c_str(); \ - } \ - virtual ~exName() noexcept {} \ - }; +// clang-format on + +template< typename ParentEx, const char * description > +class defineEx: public ParentEx +{ +public: + virtual const char * what() const noexcept + { + return description; + } +}; + +template< typename ParentEx, const char * description > +class defineExStr: public ParentEx +{ +public: + explicit defineExStr( std::string const & message_ ): + message( fmt::format( "{} {}", description, message_ ) ) + { + } + virtual const char * what() const noexcept + { + return message.c_str(); + } + +private: + std::string message; +};