Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/core/include/console.hpp

54 lines
1.5 KiB
C++
Raw Normal View History

2020-09-20 22:37:15 -04:00
#pragma once
#include <string_view>
#include <functional>
#include <vector>
#include <variant>
2020-09-22 22:43:30 -04:00
#include <string>
2020-09-20 22:37:15 -04:00
namespace prism::console {
enum class argument_type {
2020-09-20 22:37:15 -04:00
String,
Integer,
Boolean
};
struct console_argument {
explicit console_argument(std::string_view string) : data(string.data()) {}
explicit console_argument(bool data) : data(data) {}
explicit console_argument(int data) : data(data) {}
[[nodiscard]] argument_type query_type() const {
if (std::holds_alternative<std::string>(data))
return argument_type::String;
else if (std::holds_alternative<int>(data))
return argument_type::Integer;
else if (std::holds_alternative<bool>(data))
return argument_type::Boolean;
2020-09-22 22:43:30 -04:00
}
2020-09-22 22:43:30 -04:00
std::variant<std::string, int, bool> data;
};
2020-09-20 22:37:15 -04:00
using arguments = std::vector<console_argument>;
using function_ptr = std::function<void(console::arguments)>;
struct argument_format {
explicit argument_format(const int num_arguments) : num_arguments(num_arguments) {}
2020-09-20 22:37:15 -04:00
int num_arguments = 0;
std::vector<argument_type> argument_types;
2020-09-20 22:37:15 -04:00
};
void
register_command(std::string_view name, argument_format expected_format, function_ptr function);
void invoke_command(std::string_view name, arguments arguments);
void parse_and_invoke_command(std::string_view command);
void register_variable(std::string_view name, bool &variable);
}