💿🐜 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.

157 lines
4.0 KiB

  1. /*
  2. * Copyright (C) 2023 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 <sstream>
  23. #include <string>
  24. #include <tuple>
  25. #include <type_traits>
  26. #include <unordered_map>
  27. namespace debug {
  28. /**
  29. * Minimal command-line interpreter.
  30. */
  31. class cli
  32. {
  33. public:
  34. /**
  35. * Interprets a command line as a function invocation.
  36. *
  37. * @param line Command line.
  38. * @return Stringified return value of the command function.
  39. */
  40. std::string interpret(const std::string& line) const;
  41. /**
  42. * Registers a command with the CLI.
  43. *
  44. * @tparam T Command function return type.
  45. * @tparam Args Command function argument types.
  46. *
  47. * @param name Command name.
  48. * @param function Command function.
  49. */
  50. /// @{
  51. template <class T, class... Args>
  52. void register_command(const std::string& name, const std::function<T(Args...)>& function);
  53. template <class T, class... Args>
  54. void register_command(const std::string& name, T (*function)(Args...));
  55. /// @}
  56. /**
  57. * Unregisters a command from the CLI.
  58. *
  59. * @param name Command name.
  60. */
  61. void unregister_command(const std::string& name);
  62. private:
  63. /// String-wrapped function object
  64. typedef std::function<std::string(const std::string&)> command_type;
  65. /**
  66. * Parses a single argument from a string stream.
  67. *
  68. * @tparam T Argument type.
  69. *
  70. * @param stream String stream from which an argument should be parsed.
  71. */
  72. template <class T>
  73. [[nodiscard]] static T parse(std::istringstream& stream);
  74. /**
  75. * Wraps a function in an interpreter function that parses strings as arguments, then executes the wrapped function with the parsed arguments.
  76. *
  77. * @tparam T Function return type.
  78. * @tparam Args Function argument types.
  79. *
  80. * @param function Function to wrap.
  81. *
  82. * @return Wrapped function.
  83. */
  84. template <class T, class... Args>
  85. [[nodiscard]] static command_type wrap(std::function<T(Args...)> function);
  86. std::unordered_map<std::string, command_type> commands;
  87. };
  88. template <class T, class... Args>
  89. void cli::register_command(const std::string& name, const std::function<T(Args...)>& function)
  90. {
  91. commands[name] = wrap(function);
  92. }
  93. template <class T, class... Args>
  94. void cli::register_command(const std::string& name, T (*function)(Args...))
  95. {
  96. commands[name] = wrap(std::function(function));
  97. }
  98. template <class T>
  99. T cli::parse(std::istringstream& stream)
  100. {
  101. T value;
  102. stream >> value;
  103. return value;
  104. }
  105. template <class T, class... Args>
  106. typename cli::command_type cli::wrap(std::function<T(Args...)> function)
  107. {
  108. return std::bind
  109. (
  110. [function](const std::string& line) -> std::string
  111. {
  112. //constexpr std::size_t argument_count = sizeof...(Args);
  113. // Parse string into tuple of arguments
  114. std::istringstream istream(line);
  115. std::tuple<Args...> arguments{parse<Args>(istream)...};
  116. if constexpr(std::is_void_v<T>)
  117. {
  118. // Invoke function with parsed arguments
  119. std::apply(function, arguments);
  120. // Return empty string
  121. return std::string();
  122. }
  123. else
  124. {
  125. // Invoke function with parsed arguments and save the result
  126. T result = std::apply(function, arguments);
  127. // Return invocation result as a string
  128. std::ostringstream ostream;
  129. ostream << result;
  130. return ostream.str();
  131. }
  132. },
  133. std::placeholders::_1
  134. );
  135. }
  136. } // namespace debug
  137. #endif // ANTKEEPER_DEBUG_CLI_HPP