💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_DEBUG_CLI_HPP
  20. #define ANTKEEPER_DEBUG_CLI_HPP
  21. #include <functional>
  22. #include <map>
  23. #include <sstream>
  24. #include <string>
  25. #include <tuple>
  26. namespace debug {
  27. /**
  28. * Minimal command-line interpreter.
  29. */
  30. class cli
  31. {
  32. public:
  33. /**
  34. * Interprets a command line as a function invocation.
  35. *
  36. * @param line Command line.
  37. * @return Stringified return value of the command function.
  38. */
  39. std::string interpret(const std::string& line) const;
  40. /**
  41. * Registers a command with the CLI.
  42. *
  43. * @tparam T Command function return type.
  44. * @tparam Args Command function argument types.
  45. * @param name Command name.
  46. * @param function Command function.
  47. */
  48. /// @{
  49. template <class T, class... Args>
  50. void register_command(const std::string& name, const std::function<T(Args...)>& function);
  51. template <class T, class... Args>
  52. void register_command(const std::string& name, T (*function)(Args...));
  53. /// @}
  54. /**
  55. * Unregisters a command from the CLI.
  56. *
  57. * @param name Command name.
  58. */
  59. void unregister_command(const std::string& name);
  60. private:
  61. /// String-wrapped function object
  62. typedef std::function<std::string(const std::string&)> command_type;
  63. /**
  64. * Parses a single argument from a string stream.
  65. *
  66. * @param stream String stream from which an argument should be parsed.
  67. */
  68. template <class T>
  69. static T parse(std::istringstream& stream);
  70. /**
  71. * Wraps a function in an interpreter function that parses strings as arguments then executes the wrapped function with the parsed arguments.
  72. *
  73. * @param function Function to wrap.
  74. * @return Wrapped function.
  75. */
  76. template <class T, class... Args>
  77. static command_type wrap(const std::function<T(Args...)>& function);
  78. std::map<std::string, command_type> commands;
  79. };
  80. template <class T, class... Args>
  81. void cli::register_command(const std::string& name, const std::function<T(Args...)>& function)
  82. {
  83. commands[name] = wrap(function);
  84. }
  85. template <class T, class... Args>
  86. void cli::register_command(const std::string& name, T (*function)(Args...))
  87. {
  88. commands[name] = wrap(std::function(function));
  89. }
  90. template <class T>
  91. T cli::parse(std::istringstream& stream)
  92. {
  93. T value;
  94. stream >> value;
  95. return value;
  96. }
  97. template <class T, class... Args>
  98. typename cli::command_type cli::wrap(const std::function<T(Args...)>& function)
  99. {
  100. return std::bind(
  101. [function](const std::string& line) -> std::string
  102. {
  103. //std::size_t argument_count = sizeof...(Args);
  104. // Parse string into tuple of arguments
  105. std::istringstream istream(line);
  106. std::tuple<Args...> arguments{parse<Args>(istream)...};
  107. // Invoke function with arguments and save the result
  108. T result = std::apply(function, arguments);
  109. // Return function result as string
  110. std::ostringstream ostream;
  111. ostream << result;
  112. return ostream.str();
  113. },
  114. std::placeholders::_1);
  115. }
  116. } // namespace debug
  117. #endif // ANTKEEPER_DEBUG_CLI_HPP