#ifndef MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP #define MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP #include "argument_parser.hpp" #include #include #include #include #include #include namespace migraphx { namespace driver { inline namespace MIGRAPHX_INLINE_NS { inline auto& get_commands() { static std::unordered_map args)>> m; return m; } template std::string compute_command_name() { static const std::string& tname = get_type_name(); 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); return name; } template const std::string& command_name() { static const std::string& name = compute_command_name(); return name; } template void run_command(std::vector args, bool add_help = false) { T x; argument_parser ap; if(add_help) ap(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help()); x.parse(ap); if(ap.parse(std::move(args))) return; x.run(); } template int auto_register_command() { auto& m = get_commands(); m[command_name()] = [](std::vector args) { run_command(args, true); }; return 0; } template 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; }; #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wglobal-constructors" #endif template int command::static_register = auto_register_command(); // NOLINT } // namespace MIGRAPHX_INLINE_NS } // namespace driver } // namespace migraphx #endif