command.hpp 1.33 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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>

#include <unordered_map>
#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
22
template <class T>
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
31
std::string command_name()
{
    static const std::string& name = get_type_name<T>();
    return name.substr(name.rfind("::") + 2);
}

template <class T>
int auto_register_command()
{
Paul's avatar
Paul committed
32
    auto& m              = get_commands();
Paul's avatar
Paul committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    m[command_name<T>()] = [](std::vector<std::string> args) {
        T x;
        argument_parser ap;
        x.parse(ap);
        ap.parse(args);
        x.run();
    };
    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>;
};

template <class T>
int command<T>::static_register = auto_register_command<T>(); // NOLINT

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

#endif