peary/utils/type.hpp

Utility functions and classes for typing. More…

Namespaces

Name
peary
peary::utils

Detailed Description

Utility functions and classes for typing.

Copyright: Copyright (c) 2026 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Source code

  
#pragma once

#include <memory>
#include <string>
#include <typeinfo>

#if __has_include(<cxxabi.h>)
#include <cxxabi.h>
#endif

namespace peary::utils {

    inline std::string demangle(const std::type_info& type, bool keep_namespace = true) {
        std::string name;
#if __has_include(<cxxabi.h>)
        // Try using cxxabi
        int status = -1;
        const std::unique_ptr<char, void (*)(void*)> res {abi::__cxa_demangle(type.name(), nullptr, nullptr, &status),
                                                          std::free};
        if(status == 0) {
            name = res.get();
        }
#else
        // Fallback using mangled name
        name = type.name();
#endif
        if(!keep_namespace) {
            if(auto pos = name.rfind(':'); pos != std::string::npos) {
                return name.substr(pos + 1);
            }
        }

        return name;
    }

} // namespace peary::utils
  

Updated on 2026-01-30 at 22:01:05 +0100