2 pybind11/detail/typeid.h: Compiler-independent access to type identifiers
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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.
21 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
22 NAMESPACE_BEGIN(detail)
23 /// Erase all occurrences of a substring
24 inline void erase_all(std::string &string, const std::string &search) {
25 for (size_t pos = 0;;) {
26 pos = string.find(search, pos);
27 if (pos == std::string::npos) break;
28 string.erase(pos, search.length());
32 PYBIND11_NOINLINE inline void clean_type_id(std::string &name) {
35 std::unique_ptr<char, void (*)(void *)> res {
36 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free };
40 detail::erase_all(name, "class ");
41 detail::erase_all(name, "struct ");
42 detail::erase_all(name, "enum ");
44 detail::erase_all(name, "pybind11::");
48 /// Return a string representation of a C++ type
49 template <typename T> static std::string type_id() {
50 std::string name(typeid(T).name());
51 detail::clean_type_id(name);
55 NAMESPACE_END(PYBIND11_NAMESPACE)