type.h 2 KB
Newer Older
1
2
3
4
/**
 * @file
 * @brief Tags for type dispatching and run time type identification
 * @copyright Copyright (c) 2017 CERN and the Allpix Squared authors.
5
6
 * 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
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * Intergovernmental Organization or submit itself to any jurisdiction.
 */

#ifndef CORRYVRECKAN_TYPE_H
#define CORRYVRECKAN_TYPE_H

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

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

namespace corryvreckan {
    /**
     * @brief Tag for specific type
22
     * @note This tag is needed in the \ref ::corryvreckan namespace due to ADL lookup
23
24
25
26
     */
    template <typename T> struct type_tag {};
    /**
     * @brief Empty tag
27
     * @note This tag is needed in the \ref ::corryvreckan namespace due to ADL lookup
28
29
30
31
32
33
34
35
36
37
38
39
40
41
     */
    struct empty_tag {};

#ifdef __GNUG__
    // Only demangled for GNU compiler
    inline std::string demangle(const char* name, bool keep_corryvreckan = 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 corryvreckan tag if necessary
            std::string str = res.get();
            if(!keep_corryvreckan && str.find("corryvreckan::") == 0) {
42
                return str.substr(14);
43
44
45
46
47
48
49
50
51
52
            }
            return str;
        }
        return name;
    }

#else
    /**
     * @brief Demangle the type to human-readable form if it is mangled
     * @param name The possibly mangled name
53
     * @param keep_corryvreckan If true the corryvreckan namespace tag will be kept, otherwise it is removed
54
55
56
57
58
59
     */
    inline std::string demangle(const char* name, bool keep_corryvreckan = false) { return name; }
#endif
} // namespace corryvreckan

#endif /* CORRYVRECKAN_TYPE_H */