command.hpp 3.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
26
27
28
29
30
#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
31
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
32
33

#include <unordered_map>
Paul's avatar
Paul committed
34
#include <utility>
Paul's avatar
Paul committed
35
36
37
38
39
40
41
42
#include <vector>

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {

inline auto& get_commands()
{
43
    // NOLINTNEXTLINE
Paul's avatar
Paul committed
44
45
46
47
    static std::unordered_map<std::string, std::function<void(std::vector<std::string> args)>> m;
    return m;
}

Paul's avatar
Paul committed
48
template <class T>
Paul's avatar
Paul committed
49
std::string compute_command_name()
Paul's avatar
Paul committed
50
{
Paul's avatar
Paul committed
51
    static const std::string& tname = get_type_name<T>();
Paul's avatar
Paul committed
52
53
54
55
56
    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
57
58
59
60
61
62
63
64
    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
65
66
}

Paul's avatar
Paul committed
67
68
template <class T>
void run_command(std::vector<std::string> args, bool add_help = false)
Paul's avatar
Paul committed
69
70
71
{
    T x;
    argument_parser ap;
Paul's avatar
Paul committed
72
    if(add_help)
Paul's avatar
Paul committed
73
        ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help());
Paul's avatar
Paul committed
74
    x.parse(ap);
Paul's avatar
Paul committed
75
    if(ap.parse(std::move(args)))
Paul's avatar
Paul committed
76
77
78
79
        return;
    x.run();
}

Paul's avatar
Paul committed
80
81
82
template <class T>
int auto_register_command()
{
Paul's avatar
Paul committed
83
    auto& m              = get_commands();
Paul's avatar
Paul committed
84
    m[command_name<T>()] = [](std::vector<std::string> args) { run_command<T>(args, true); };
Paul's avatar
Paul committed
85
86
87
88
89
90
    return 0;
}

template <class T>
struct command
{
91
    static const int static_register;
Paul's avatar
Paul committed
92
93
94
95
96
97
    // 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
98
99
100
101
102
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

Paul's avatar
Paul committed
103
template <class T>
104
const int command<T>::static_register = auto_register_command<T>(); // NOLINT
Paul's avatar
Paul committed
105
106
107
108
109
110

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

#endif