Realize console in CS

xiaoxiao2021-03-05  56

The console in the CS is not very COOL.

Enter 1 command can perform a certain action (a bit like returning to DOS, huh, huh ~)

Some people will think it is better to directly achieve 1 GUI

But to achieve 1 GUI may be more difficult than achieving 1 console

Sometimes it is not as good as the console in functional and speed

The simplest method of displaying information (which is 1 big pile string) is to use COUT output, which can also be displayed directly from the window but every person's method is different. I can't have a hard rule ( If this is not good, it will increase the coupling), so I write this part of the contents of the interface function void Printlog (const st:: string & str, unsigned long loglevel); parameter is 1 string and 1 Loglevel Loglevel The role is to compare the importance of this string may be an error message 哟 ~ This can be displayed with different parties to display the information as for how to display the reader to do it.

Then the input command is of course a string but we are usually 1 character input (nonsense =. =) But the program internal call can be used directly with the string, so it is necessary to implement the two functions. Void INPUTCOMMAND. Const std :: string & strcommand; Void InputCommand (Char Command);

The end is how to execute the command? ? Of course, it is a call function, but the first is to load the function and command. This container is best to use hash_map. It is looking for a fast disadvantage that it is slow when we need it. The execution command is fast and inserted. Usually done at the beginning of the program

Another problem is that the call function is a normal function. It is easy to call the member function. Some people will want to inherit 1 virtual base class. Then put all the classes to call the member function to inherit this virtual base. Class but do this is a bit ugly, but it is gave up the ordinary function, inheriting this virtual base class and then calling this normal function when implementing, but some classes can't be changed or it is written? It can also be "annoyed" with this method but only one word can be used.

But is there a better way? ? Of course, if you have anything about me, I will think that this 1 is getting more than a simpler. It is said that "commission" is actually 1 list of 1 函 1 1 1 库Things, but I use my method, of course, is a simple method ^ _ ^

The container is like this. Hash_map

The parameters of the imitation function can of course set themselves.

And the parameters I set are (const st: string &, std :: string ") Return value is unsigned long is the type of information (such as error message)

The first parameter is input is that we will add a few parameters after tapping the keyboard and then add a few parameters and then process the second one to process the second is the return information. It can be displayed immediately.

The imitation function is like this delegate

First come 1 piece of code first

Template Class functorManager {public: typedef functor function; public: PUBLIC:

BOOL RegisterFunctor (const st :: string & strname, functor & function) {return m_functor_list.insert (std :: make_pair (strname, function). second;}

Bool UnregisterFunctor (const st :: string & strfunctorname) {returnctor_functor_list.rase (strfunctorname) == 1;}

Functor * ActivationFunctor (const std :: string & strFunctorName) {Functor_List_Iterator_t i = m_Functor_List.find (strFunctorName); if (i == m_Functor_List.end ()) {return 0;} return & ((* i) .second); } protected: typedef std :: hash_map Functor_List_t; typedef typename std :: hash_map :: iterator Functor_List_Iterator_t; Functor_List_t m_Functor_List;

}; See the name is the Class to manage the imitation function is 1 Base Class

template class Console: public FunctorManager > {public: typedef Console MyType; typedef PrintPolicy PrintType; typedef Delegate function_t; enum {console_log_LOLVEL_INFORMATION,

Console_log_level_command,

Console_log_level_warning,

Console_log_level_error};

Public: // Constructor CommandSIGN is the command to have a note bar //, please register 1 member function to output all the name console (char commandsign = '/'): m_commandsign (commandsign) {function_t __printallcommand This, console :: printallCommand;

Bool B = RegisterFunctor ("Print All Command", __printallCommand; assert (b);} ~ console () {} public: // Enter the command void inputcommand (const std :: string & strcommand) {// Convert THIS to The THIS // of the derived class can be called the function of the fault, which can call the function of the fault, PRINTPOLICY * PT = Static_Cast (this);

// Command is empty if (strcommand.empty ()) {return;} // command is "/" {"{"} (strcommand [0]! = M_commandsign) {Pt-> printlog (strcommand, console_log_LOG_LOARNING) Return;}

pT-> PrintLog (strCommand, CONSOLE_LOG_LEVEL_COMMAND); // separate commands and parameters std :: string :: size_type next_space = strCommand.find ( ''); std :: string CommandHead = strCommand.substr (1, next_space-1- 1); // Execute the function of the command called function function_t * pfun = actiVationFunctor (strcommand); if (pfun) {std :: string Outlog; unsigned long loglevel = (* pfun);

Pt-> Printlog (Outlog, Loglevel);} else {Pt-> Printlog ("Have no this command", console_log_level_warning);}}

Protected: // Prints the name of the order to unsigned longprintallcommand (const st :: string & in, std :: string& out) {assert (in == "print all command");

Functor_list_iterator_t i = m_functor_list.begin (); functor_list_iterator_t = m_functor_list.end (); for (; i! = IEND; i) {static_cast (this) -> Printlog ((* i) .first, Console_log_level_information;

OUT = "Print All Command Complate."; Return Console_log_level_information;

PRIVATE: const char m_commandsign; // Command's expression};

The implementation code is not very simple. Hehe ~ This is the full relying on STL and my nanjected imitation function (sometimes I really don't feel like there is no STL's day. "

Using a very simple inherited this class and then implements Printlog, you can Class Myconsole: Public Console {Void Printlog (Const std :: string & str, unsigned long loglevel) {...}};

Full text

转载请注明原文地址:https://www.9cbs.com/read-31988.html

New Post(0)