Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:12

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 
0024 /// Erase all occurrences of a substring
0025 inline void erase_all(std::string &string, const std::string &search) {
0026     for (size_t pos = 0;;) {
0027         pos = string.find(search, pos);
0028         if (pos == std::string::npos) {
0029             break;
0030         }
0031         string.erase(pos, search.length());
0032     }
0033 }
0034 
0035 PYBIND11_NOINLINE void clean_type_id(std::string &name) {
0036 #if defined(__GNUG__)
0037     int status = 0;
0038     std::unique_ptr<char, void (*)(void *)> res{
0039         abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status), std::free};
0040     if (status == 0) {
0041         name = res.get();
0042     }
0043 #else
0044     detail::erase_all(name, "class ");
0045     detail::erase_all(name, "struct ");
0046     detail::erase_all(name, "enum ");
0047 #endif
0048     detail::erase_all(name, "pybind11::");
0049 }
0050 
0051 inline std::string clean_type_id(const char *typeid_name) {
0052     std::string name(typeid_name);
0053     detail::clean_type_id(name);
0054     return name;
0055 }
0056 
0057 PYBIND11_NAMESPACE_END(detail)
0058 
0059 /// Return a string representation of a C++ type
0060 template <typename T>
0061 static std::string type_id() {
0062     return detail::clean_type_id(typeid(T).name());
0063 }
0064 
0065 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)