Creating an interactive tooltip in C ++

I have a program that should read commands from the console and, depending on the command, perform one of several actions. Here is what I still have:

void ConwayView::listening_commands() { string command; do { cin >> command; if ("tick" == command) { // to do } else if ("start" == command) { // to do for start } ... } while (EXIT != command); } 

Using switch instead of if helps a little if there are a large number of commands. What templates do you propose to use to provide an interactive command line?

+4
source share
6 answers

If the number of commands is small and the possible parameters are really small, you can continue to work with the switch housing!

If the number of teams increases, consider a team design template (which is IMHO a kind of masked strategy template : cf Use a strategy template and a command template to distinguish between command templates and strategies).

If most of your teams share part of the same behavior, don't forget the template template template .

If the complexity of creating objects for your team increases (i.e. there is a difficulty in decoding / understanding your command line input), you should start looking for an interpreter design pattern

If you find some complexity when designing using the interpreter template (if the interpreter needs too much work, you see syntax problems, etc.), then you should probably look at DSL, a domain-specific language , and create your own A language that fits (and only fits) your own inputs.

+3
source

There are several ways to solve this problem, and it is debatable that the solution is "right." If I decided to solve it for my work, I would create a table of custom structure. Sort of:

 struct CommandStruct { char *command; int (*commandHandler)(/*params*/); } commandTable[] = { { "tick", tickCommand }, { "start", startCommand }, ... }; 

Then my processing cycle will go through each element of this table, looking for the correct match, for example:

 for (int i = 0; i < TABLE_SIZE; ++i) { if (command == commandTable[i].command) { /* using whatever proper comparison is, of course */ commandTable[i].commandHandler(/*params*/); break; } } 
+2
source

Not quite a template, but often a good approach:

 #include <map> #include <functional> #include <string> #include <iostream> typedef std::map< std::string, std::function<void(void)> > command_dict; // ^^^^^^^^ // the signature of your commands. probably should have an error code. void command1() { std::cout << "commanda" << std::endl; } void command2() { std::cout << "commandb" << std::endl; } void command3() { std::cout << "commandc" << std::endl; } int main() { command_dict c; c["a"] = &command1; c["b"] = &command2; c["c"] = &command3; std::string input; while(std::getline(std::cin, input)) { // quit the program with ctrl-d auto it = c.find(input); if(it != end(c)) { (it->second)(); // execute the command } else { std::cout << "command \"" << input << "\" not known" << std::endl; } } } 
+2
source

The if - else ladder is fine.

In principle, it can be replaced with map<string, Function> , but it does not give anything for this particular case (added complexity without much gain, even with a large number of teams).

When I wrote this initially, I forgot to mention:

  • Make your command handlers separate functions.

If you do not use t, then the if - else ladder can become quite dirty and hellip; The map solution requires separate functions and therefore may seem a little cleaner than the all-right-here staircase if - else . But these are really separate functions, which then provide clarity, while map slightly detracts (adds extra work to maintain the map and an additional level of indirection to deal with it).

On the other hand, since β€œreading commands from the console” means interactive input, with the user in the picture, you do not want to read two or more commands from the same input line. Because it would get a hint and may seem rather perplexing to the user. Therefore, instead of reading the input word at a time using >> , use std::getline to read the complete input line at a time.

0
source

use the new and improved way to prepare the command: int happy_time = 5; int a = happy_time; int muddy_dirt = 1; int b = muddy_dirt; int c = happy_time * muddy_dirt // really dirty stuff

This is probably the least difficult way to do this.

0
source

You should use a database such as access if your team is large.

-1
source

Source: https://habr.com/ru/post/1445504/