command.hpp 2.06 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
20
21
22
23
#include <vector>

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

inline auto& get_commands()
{
    static std::unordered_map<std::string, std::function<void(std::vector<std::string> args)>> m;
    return m;
}

Paul's avatar
Paul committed
24
template <class T>
Paul's avatar
Paul committed
25
std::string compute_command_name()
Paul's avatar
Paul committed
26
{
Paul's avatar
Paul committed
27
    static const std::string& tname = get_type_name<T>();
Paul's avatar
Paul committed
28
29
30
31
32
    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
33
34
35
36
37
38
39
40
    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
41
42
}

Paul's avatar
Paul committed
43
44
template <class T>
void run_command(std::vector<std::string> args, bool add_help = false)
Paul's avatar
Paul committed
45
46
47
{
    T x;
    argument_parser ap;
Paul's avatar
Paul committed
48
    if(add_help)
Paul's avatar
Paul committed
49
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help());
Paul's avatar
Paul committed
50
    x.parse(ap);
Paul's avatar
Paul committed
51
    if(ap.parse(std::move(args)))
Paul's avatar
Paul committed
52
53
54
55
        return;
    x.run();
}

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

template <class T>
struct command
{
    static int static_register;
    // 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
74
75
76
77
78
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
template <class T>
int command<T>::static_register = auto_register_command<T>(); // NOLINT

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

#endif