File indexing completed on 2025-02-23 05:15:02
0001 /* 0002 pybind11/detail/typeid.h: Compiler-independent access to type identifiers 0003 0004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 0005 0006 All rights reserved. Use of this source code is governed by a 0007 BSD-style license that can be found in the LICENSE file. 0008 */ 0009 0010 #pragma once 0011 0012 #include <cstdio> 0013 #include <cstdlib> 0014 0015 #if defined(__GNUG__) 0016 #include <cxxabi.h> 0017 #endif 0018 0019 #include "common.h" 0020 0021 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 0022 PYBIND11_NAMESPACE_BEGIN(detail) 0023 /// Erase all occurrences of a substring 0024 inline void erase_all(std::string &string, const std::string &search) { 0025 for (size_t pos = 0;;) { 0026 pos = string.find(search, pos); 0027 if (pos == std::string::npos) break; 0028 string.erase(pos, search.length()); 0029 } 0030 } 0031 0032 PYBIND11_NOINLINE void clean_type_id(std::string &name) { 0033 #if defined(__GNUG__) 0034 int status = 0; 0035 std::unique_ptr<char, void (*)(void *)> res { 0036 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free }; 0037 if (status == 0) 0038 name = res.get(); 0039 #else 0040 detail::erase_all(name, "class "); 0041 detail::erase_all(name, "struct "); 0042 detail::erase_all(name, "enum "); 0043 #endif 0044 detail::erase_all(name, "pybind11::"); 0045 } 0046 PYBIND11_NAMESPACE_END(detail) 0047 0048 /// Return a string representation of a C++ type 0049 template <typename T> static std::string type_id() { 0050 std::string name(typeid(T).name()); 0051 detail::clean_type_id(name); 0052 return name; 0053 } 0054 0055 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)