command.hpp 2.28 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
#ifndef MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP
#define MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP

#include "argument_parser.hpp"

#include <migraphx/config.hpp>
#include <migraphx/type_name.hpp>
Paul's avatar
Paul committed
8
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
9
10

#include <unordered_map>
Paul's avatar
Paul committed
11
#include <utility>
Paul's avatar
Paul committed
12
13
14
15
16
17
18
19
#include <vector>

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

inline auto& get_commands()
{
20
    // NOLINTNEXTLINE
Paul's avatar
Format  
Paul committed
21
22
23
24
    static std::unordered_map<
        std::string,
        std::function<void(const std::string& exe_name, std::vector<std::string> args)>>
        m;
Paul's avatar
Paul committed
25
26
27
    return m;
}

Paul's avatar
Paul committed
28
template <class T>
Paul's avatar
Paul committed
29
std::string compute_command_name()
Paul's avatar
Paul committed
30
{
Paul's avatar
Paul committed
31
    static const std::string& tname = get_type_name<T>();
Paul's avatar
Paul committed
32
33
34
35
36
    auto name                       = tname.substr(tname.rfind("::") + 2);
    if(ends_with(name, "_command"))
        name = name.substr(0, name.size() - 8);
    if(ends_with(name, "_cmd"))
        name = name.substr(0, name.size() - 4);
Paul's avatar
Paul committed
37
38
39
40
41
42
43
44
    return name;
}

template <class T>
const std::string& command_name()
{
    static const std::string& name = compute_command_name<T>();
    return name;
Paul's avatar
Paul committed
45
46
}

Paul's avatar
Paul committed
47
template <class T>
Paul's avatar
Paul committed
48
void run_command(const std::string& exe_name, std::vector<std::string> args, bool add_help = false)
Paul's avatar
Paul committed
49
50
51
{
    T x;
    argument_parser ap;
Paul's avatar
Paul committed
52
    ap.set_exe_name(exe_name + " " + command_name<T>());
Paul's avatar
Paul committed
53
    if(add_help)
Paul's avatar
Paul committed
54
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help());
Paul's avatar
Paul committed
55
    x.parse(ap);
Paul's avatar
Paul committed
56
    if(ap.parse(std::move(args)))
Paul's avatar
Paul committed
57
58
59
60
        return;
    x.run();
}

Paul's avatar
Paul committed
61
62
63
template <class T>
int auto_register_command()
{
Paul's avatar
Paul committed
64
    auto& m              = get_commands();
Paul's avatar
Format  
Paul committed
65
66
67
    m[command_name<T>()] = [](const std::string& exe_name, std::vector<std::string> args) {
        run_command<T>(exe_name, args, true);
    };
Paul's avatar
Paul committed
68
69
70
71
72
73
    return 0;
}

template <class T>
struct command
{
74
    static const int static_register;
Paul's avatar
Paul committed
75
76
77
78
79
80
    // This typedef ensures that the static member will be instantiated if
    // the class itself is instantiated
    using static_register_type =
        std::integral_constant<decltype(&static_register), &static_register>;
};

Paul's avatar
Paul committed
81
82
83
84
85
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

Paul's avatar
Paul committed
86
template <class T>
87
const int command<T>::static_register = auto_register_command<T>(); // NOLINT
Paul's avatar
Paul committed
88
89
90
91
92
93

} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

#endif