diff options
Diffstat (limited to 'include/pybind11/detail/typeid.h')
-rw-r--r-- | include/pybind11/detail/typeid.h | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/include/pybind11/detail/typeid.h b/include/pybind11/detail/typeid.h index 148889ff..a67b5213 100644 --- a/include/pybind11/detail/typeid.h +++ b/include/pybind11/detail/typeid.h @@ -13,29 +13,33 @@ #include <cstdlib> #if defined(__GNUG__) -#include <cxxabi.h> +# include <cxxabi.h> #endif #include "common.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) + /// Erase all occurrences of a substring inline void erase_all(std::string &string, const std::string &search) { for (size_t pos = 0;;) { pos = string.find(search, pos); - if (pos == std::string::npos) break; + if (pos == std::string::npos) { + break; + } string.erase(pos, search.length()); } } -PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { +PYBIND11_NOINLINE void clean_type_id(std::string &name) { #if defined(__GNUG__) int status = 0; - std::unique_ptr<char, void (*)(void *)> res { - abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free }; - if (status == 0) + std::unique_ptr<char, void (*)(void *)> res{ + abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free}; + if (status == 0) { name = res.get(); + } #else detail::erase_all(name, "class "); detail::erase_all(name, "struct "); @@ -43,13 +47,19 @@ PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { #endif detail::erase_all(name, "pybind11::"); } -PYBIND11_NAMESPACE_END(detail) -/// Return a string representation of a C++ type -template <typename T> static std::string type_id() { - std::string name(typeid(T).name()); +inline std::string clean_type_id(const char *typeid_name) { + std::string name(typeid_name); detail::clean_type_id(name); return name; } +PYBIND11_NAMESPACE_END(detail) + +/// Return a string representation of a C++ type +template <typename T> +static std::string type_id() { + return detail::clean_type_id(typeid(T).name()); +} + PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |