src/core/utils/type.h

Tags for type dispatching and run time type identification. More…

Namespaces

Name
allpix
Helper class to hold support layers for a detector model.

Classes

Name
struct allpix::type_tag
Tag for specific type.
struct allpix::empty_tag
Empty tag.

Functions

Name
std::string demangle(const char * name, bool keep_allpix =false)
Demangle the type to human-readable form if it is mangled.

Detailed Description

Tags for type dispatching and run time type identification.

Copyright: Copyright (c) 2017-2024 CERN and the Allpix Squared authors. This software is distributed under the terms of the MIT License, copied verbatim in the file “LICENSE.md”. In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction. SPDX-License-Identifier: MIT

Functions Documentation

function demangle

inline std::string demangle(
    const char * name,
    bool keep_allpix =false
)

Demangle the type to human-readable form if it is mangled.

Parameters:

  • name The possibly mangled name
  • keep_allpix If true the allpix namespace tag will be kept, otherwise it is removed

Source code


#ifndef ALLPIX_TYPE_H
#define ALLPIX_TYPE_H

#include <cstdlib>
#include <cxxabi.h>
#include <memory>
#include <string>

// TODO: This should be reworked to show complex types in a better way

namespace allpix {
    template <typename T> struct type_tag {};
    struct empty_tag {};

#ifdef __GNUG__
    // Only demangled for GNU compiler
    inline std::string demangle(const char* name, bool keep_allpix = false) {
        // Try to demangle
        int status = -1;
        std::unique_ptr<char, void (*)(void*)> res{abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free};

        if(status == 0) {
            // Remove allpix tag if necessary
            std::string str = res.get();
            if(!keep_allpix && str.find("allpix::") == 0) {
                return str.substr(8);
            }
            return str;
        }
        return name;
    }

#else
    inline std::string demangle(const char* name, bool keep_allpix = false) { return name; }
#endif
} // namespace allpix

#endif /* ALLPIX_TYPE_H */

Updated on 2024-12-13 at 08:31:37 +0000