Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Jul 5, 2024
1 parent e6c74d8 commit 58d6181
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/utki/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ void utki::log(const std::function<void(std::ostream&)>& 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
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions src/utki/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/utki/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/utki/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename exception_type>
void throw_with_nested(exception_type exception)
{
Expand Down
8 changes: 8 additions & 0 deletions src/utki/sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ SOFTWARE.

#include <iterator>

#include "config.hpp"

namespace utki {

#if CFG_CPP < 20
namespace sort_internal {

template <typename random_iter_type>
Expand Down Expand Up @@ -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.
Expand All @@ -121,7 +125,11 @@ constexpr void quick_sort(random_iter_type first, random_iter_type last, compara
template <class random_iter_type, class comparator_type = std::less<>>
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
12 changes: 4 additions & 8 deletions tests/unit/src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,27 @@ 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);
});

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);
});
Expand Down

0 comments on commit 58d6181

Please sign in to comment.