Skip to content

Commit

Permalink
add pretty type for pybind
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Aug 8, 2024
1 parent 23ef432 commit 187c20e
Show file tree
Hide file tree
Showing 4 changed files with 909 additions and 0 deletions.
32 changes: 32 additions & 0 deletions backend/src/Utilities/Demangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//******************************************************************************
// Includes
//******************************************************************************

#include "Utilities/Demangle.hpp"

/// \cond
// #define DEMANGLE_USE_BOOST
/// \endcond

#if defined(DEMANGLE_USE_BOOST)
#include <boost/core/demangle.hpp>
#elif defined(__clang__) || defined(__GNUG__)
#include <cxxabi.h>
#endif

#if defined(DEMANGLE_USE_BOOST)
std::string demangle(char const* name) { return boost::core::demangle(name); }
#elif defined(__clang__) || defined(__GNUG__)
// else we inline some logic of boost::core::demangle
std::string demangle(char const* name) {
int status(-1);
char* demangled_name = abi::__cxa_demangle(name, nullptr, nullptr, &status);
// status == 0 is good
std::string class_name(status ? name : demangled_name);
// Be a good boy and free
std::free(demangled_name); // NOLINT
return class_name;
}
#else
std::string demangle(char const* name) { return name; }
#endif
19 changes: 19 additions & 0 deletions backend/src/Utilities/Demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************

#include <string>

//******************************************************************************
/*!\brief Returns the demangled name of a compilation symbol
// \ingroup UtilitiesGroup
//
// \usage
\code
auto demangled_name = demangle(typeid(int).name());
\endcode
*/
std::string demangle(char const* name);
//******************************************************************************
34 changes: 34 additions & 0 deletions backend/src/Utilities/PrettyType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Reused from SpECTRE : https://spectre-code.org/
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Utilities/PrettyType.hpp"

#include <cstddef>
#include <regex>

namespace pretty_type {

namespace detail {

std::string extract_short_name(std::string name) {
// Remove all template arguments
const std::regex template_pattern("<[^<>]*>");
size_t previous_size = 0;
while (name.size() != previous_size) {
previous_size = name.size();
name = std::regex_replace(name, template_pattern, "");
}

// Remove namespaces, etc.
const size_t last_colon = name.rfind(':');
if (last_colon != std::string::npos) {
name.replace(0, last_colon + 1, "");
}

return name;
}

} // namespace detail

} // namespace pretty_type
Loading

0 comments on commit 187c20e

Please sign in to comment.