typeid.h revision 12391:ceeca8b41e4b
1/* 2 pybind11/detail/typeid.h: Compiler-independent access to type identifiers 3 4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 6 All rights reserved. Use of this source code is governed by a 7 BSD-style license that can be found in the LICENSE file. 8*/ 9 10#pragma once 11 12#include <cstdio> 13#include <cstdlib> 14 15#if defined(__GNUG__) 16#include <cxxabi.h> 17#endif 18 19NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 20NAMESPACE_BEGIN(detail) 21/// Erase all occurrences of a substring 22inline void erase_all(std::string &string, const std::string &search) { 23 for (size_t pos = 0;;) { 24 pos = string.find(search, pos); 25 if (pos == std::string::npos) break; 26 string.erase(pos, search.length()); 27 } 28} 29 30PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { 31#if defined(__GNUG__) 32 int status = 0; 33 std::unique_ptr<char, void (*)(void *)> res { 34 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free }; 35 if (status == 0) 36 name = res.get(); 37#else 38 detail::erase_all(name, "class "); 39 detail::erase_all(name, "struct "); 40 detail::erase_all(name, "enum "); 41#endif 42 detail::erase_all(name, "pybind11::"); 43} 44NAMESPACE_END(detail) 45 46/// Return a string representation of a C++ type 47template <typename T> static std::string type_id() { 48 std::string name(typeid(T).name()); 49 detail::clean_type_id(name); 50 return name; 51} 52 53NAMESPACE_END(PYBIND11_NAMESPACE) 54