From 58d6181daac56ccc22d1b22ed9446ba9f1bbae98 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sat, 6 Jul 2024 01:23:53 +0300 Subject: [PATCH] refactoring --- src/utki/debug.cpp | 8 ++++---- src/utki/debug.hpp | 8 ++++---- src/utki/exception.cpp | 4 ++-- src/utki/exception.hpp | 6 ++++++ src/utki/sort.hpp | 8 ++++++++ tests/unit/src/debug.cpp | 12 ++++-------- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/utki/debug.cpp b/src/utki/debug.cpp index c9d9a67f..baecdf79 100644 --- a/src/utki/debug.cpp +++ b/src/utki/debug.cpp @@ -112,14 +112,14 @@ void utki::log(const std::function& print) #endif } -std::string utki::demangle(const char* name) +std::string utki::demangle(const std::type_info& type_info) { #if CFG_COMPILER == CFG_COMPILER_GCC || CFG_COMPILER == CFG_COMPILER_CLANG // NOLINTNEXTLINE(cppcoreguidelines-init-variables) int status; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) auto demangled_name = abi::__cxa_demangle( - name, + type_info.name(), nullptr, // let __cxa_demangle() allocate memory buffer for us nullptr, // not interested in allocated memory buffer size &status @@ -143,9 +143,9 @@ std::string utki::demangle(const char* name) case -3: // one of the arguments is invalid [[fallthrough]]; default: - return {name}; + return {type_info.name()}; } #else - return {name}; + return {type_info.name()}; #endif } diff --git a/src/utki/debug.hpp b/src/utki/debug.hpp index c8fb661c..c458cc49 100644 --- a/src/utki/debug.hpp +++ b/src/utki/debug.hpp @@ -294,18 +294,18 @@ namespace utki { /** * @brief Demangle C++ type name. * Tries to demangle C++ type name. - * Implementation us4es compiler specific API for demangling. + * Implementation uses compiler specific API for demangling. * For unknown compilers it just returns the name as is. * * Example: * @code{.cpp} * some_class a; - * auto name = utki::demangle(typeid(a).name()); + * auto name = utki::demangle(typeid(a)); * @endcode * - * @param name - C++ type name to demangle. + * @param type_info - C++ type info of the type to demangle. * @return Demangled name. */ -std::string demangle(const char* name); +std::string demangle(const std::type_info& type_info); } // namespace utki diff --git a/src/utki/exception.cpp b/src/utki/exception.cpp index cb8e1a90..24c0d895 100644 --- a/src/utki/exception.cpp +++ b/src/utki/exception.cpp @@ -49,7 +49,7 @@ std::string utki::current_exception_to_string(std::string_view indentation) std::stringstream ss; ss << indentation << #if CFG_COMPILER == CFG_COMPILER_GCC || CFG_COMPILER == CFG_COMPILER_CLANG - demangle(abi::__cxa_current_exception_type()->name()); + demangle(*abi::__cxa_current_exception_type()); #else "unknown exception"s; #endif @@ -75,7 +75,7 @@ std::string utki::current_exception_to_string(std::string_view indentation) std::string utki::to_string(const std::exception& e, std::string_view indentation) { std::stringstream ss; - ss << indentation << demangle(typeid(e).name()) << ": " << e.what(); + ss << indentation << demangle(typeid(e)) << ": " << e.what(); try { std::rethrow_if_nested(e); diff --git a/src/utki/exception.hpp b/src/utki/exception.hpp index 38889fb3..b2486b02 100644 --- a/src/utki/exception.hpp +++ b/src/utki/exception.hpp @@ -119,6 +119,12 @@ class stacked_exception : public exception } }; +/** + * @brief Throw exception with nested current exception. + * This is a replacement of std::throw_with_nested() which + * prints all nested exceptions information via it's what() member function. + * @param exception - exception to throw. + */ template void throw_with_nested(exception_type exception) { diff --git a/src/utki/sort.hpp b/src/utki/sort.hpp index 7a0a78b7..d22fdef1 100644 --- a/src/utki/sort.hpp +++ b/src/utki/sort.hpp @@ -33,8 +33,11 @@ SOFTWARE. #include +#include "config.hpp" + namespace utki { +#if CFG_CPP < 20 namespace sort_internal { template @@ -107,6 +110,7 @@ constexpr void quick_sort(random_iter_type first, random_iter_type last, compara } } // namespace sort_internal +#endif // CFG_CPP < 20 /** * @brief sort algorithm. @@ -121,7 +125,11 @@ constexpr void quick_sort(random_iter_type first, random_iter_type last, compara template > constexpr void sort(random_iter_type first, random_iter_type last, comparator_type comp = comparator_type{}) { +#if CFG_CPP >= 20 + std::sort(first, last, comp); +#else utki::sort_internal::quick_sort(first, last, comp); +#endif } } // namespace utki diff --git a/tests/unit/src/debug.cpp b/tests/unit/src/debug.cpp index 17c0996a..12e45089 100644 --- a/tests/unit/src/debug.cpp +++ b/tests/unit/src/debug.cpp @@ -59,15 +59,13 @@ const tst::set set("debug", [](tst::suite& suite) { suite.add("demangle__class", []() { test_class a; - auto mangled_name = std::string(typeid(a).name()); - - auto name = utki::demangle(mangled_name.c_str()); + auto name = utki::demangle(typeid(a)); auto expected = #if CFG_COMPILER == CFG_COMPILER_GCC || CFG_COMPILER == CFG_COMPILER_CLANG "test_class"s; #else - mangled_name; + std::string(typeid(a).name()); #endif tst::check_eq(name, expected, SL); }); @@ -75,15 +73,13 @@ const tst::set set("debug", [](tst::suite& suite) { suite.add("demangle__unsigned", []() { unsigned a = 0; - auto mangled_name = std::string(typeid(a).name()); - - auto name = utki::demangle(mangled_name.c_str()); + auto name = utki::demangle(typeid(a)); auto expected = #if CFG_COMPILER == CFG_COMPILER_GCC || CFG_COMPILER == CFG_COMPILER_CLANG "unsigned int"s; #else - mangled_name; + std::string(typeid(a).name()); #endif tst::check_eq(name, expected, SL); });