iash  v0.5
Library to provide a bash-like shell in an application
CommandDispatcher Class Reference

Class to register and execute Commands in an iash shell. More...

#include <CommandDispatcher.h>

Public Member Functions

 CommandDispatcher (iash *parent)
 Initializes a CommandDispatcher with the given shell parent. More...
 
void registerCommand (Command *cmd)
 Registers a given Command with this CommandDispatcher instance. More...
 
template<typename ExtCommand , typename... Args>
void registerCommand (Args &&... args)
 Registers the given Command type with this CommandDispatcher instance. More...
 
CommandunregisterCommand (const std::string &name)
 Removes the command with the given name from the registry, and removes any of its registered aliases. More...
 
const CommandunregisterCommand (const Command *cmd)
 Removes the command at the given memory address. More...
 
int dispatch (const UserCommand *userCmd)
 Searches the command and alias registries and calls the appropriate Command. More...
 

Detailed Description

Class to register and execute Commands in an iash shell.

iash has an internal instance of this class, there is typically no need to instantiate it manually.

iash does not provide direct access to this API through its own API, but commands can be registered through iash::addCommand.

Since
0.5

Constructor & Destructor Documentation

◆ CommandDispatcher()

CommandDispatcher::CommandDispatcher ( iash parent)

Initializes a CommandDispatcher with the given shell parent.

Parameters
parentthe shell this CommandDispatcher belongs to

Member Function Documentation

◆ dispatch()

int CommandDispatcher::dispatch ( const UserCommand userCmd)

Searches the command and alias registries and calls the appropriate Command.

Parameters
userCmdthe user's input command
Returns
the return value of the Command::run for the given command, or 127 if no command was found

◆ registerCommand() [1/2]

void CommandDispatcher::registerCommand ( Command cmd)

Registers a given Command with this CommandDispatcher instance.

This will call Command::init to set the parent shell and add the Command's name and aliases to the command registry.

This function requires a dynamically allocated Command, passed like so:

CommandDispatcher disp (&shell);
disp.registerCommand(new MyCommand);

The CommandDispatcher will automatically free the allocated memory, so there is no need to somehow catch the pointer to the allocated memory.

If a Command instance with the same name and type is already registered with this class, this function will not add the new instance. If the classes must be replaced, the old one should be unregistered first.

This is not the recommended method to use when registering commands. CommandDispatcher::registerCommand() should be used instead.

Parameters
cmda dynamically allocated Command object to register

◆ registerCommand() [2/2]

template<typename ExtCommand , typename... Args>
void CommandDispatcher::registerCommand ( Args &&...  args)
inline

Registers the given Command type with this CommandDispatcher instance.

This function will allocate the memory for the Command internally; all the client must do is provide the type:

CommandDispatcher disp (&shell);
disp.registerCommand<MyCommand>();

If a Command instance of the same type is already registered with this class, this function will not add the new instance. If the classes must be replaced, the old one should be unregistered first.

Parameters
argsthe arguments to pass to the constructor
Template Parameters
ExtCommandthe Command type to register
Argsthe types of the arguments to pass to the Command constructor

◆ unregisterCommand() [1/2]

Command* CommandDispatcher::unregisterCommand ( const std::string &  name)

Removes the command with the given name from the registry, and removes any of its registered aliases.

Note that, since Commands are typically created with dynamic memory, you must catch the pointer returned by this function and delete it.

Parameters
namethe name of the Command to unregister
Returns
a pointer to the unregistered Command object

◆ unregisterCommand() [2/2]

const Command * CommandDispatcher::unregisterCommand ( const Command cmd)

Removes the command at the given memory address.

Note that, since Commands are dynamically allocated, the Command should be deleted after this call.

Parameters
cmda pointer to the Command to unregister
Returns
a pointer to the unregistered Command object

The documentation for this class was generated from the following files: