peary::utils::Dispatcher
peary::utils::Dispatcher
Command dispatcher class. More…
#include <peary/utils/dispatcher.hpp>
Public Types
| Name | |
|---|---|
| using std::function< std::string(const std::vector< std::string > &)> | NativeInterface Native interface for command functions. |
Public Functions
| Name | |
|---|---|
| void | add(std::string name, NativeInterface func, std::size_t nargs)Add a command with a native interface. |
| template <typename R ,typename… Args> void | add(std::string name, std::function< R(Args…)> func)Register a command with a function that takes arguments and returns a value. |
| template <typename R ,typename… Args> void | add(std::string name, R(*)(Args…) func)Register a command with a function pointer that takes arguments and returns a value. |
| template <typename T ,typename R ,typename… Args> void | add(std::string name, R(T::*)(Args…) member_func, T * t)Register a command with a member function of a class. |
| std::string | call(const std::string & name, const std::vector< std::string > & args)Call a registered command with the given name and arguments. |
| std::vector< std::pair< std::string, std::size_t > > | commands() constGet a list of registered commands. |
Detailed Description
class peary::utils::Dispatcher;
Command dispatcher class.
This class is used to register and call commands with a variable number of string arguments.
Public Types Documentation
using NativeInterface
using peary::utils::Dispatcher::NativeInterface = std::function<std::string(const std::vector<std::string>&)>;
Native interface for command functions.
This type is used to define the interface for functions that can be registered
Public Functions Documentation
function add
inline void add(
std::string name,
NativeInterface func,
std::size_t nargs
)
Add a command with a native interface.
Parameters:
- name The name of the command to be registered
- func The function to be registered, which must implement the native interface
- nargs The number of arguments that the command expects
function add
template <typename R ,
typename... Args>
inline void add(
std::string name,
std::function< R(Args...)> func
)
Register a command with a function that takes arguments and returns a value.
Parameters:
- name The name of the command to be registered
- func The function to be registered, which must take the specified arguments and return a value
Template Parameters:
- R The return type of the function
- Args The types of the arguments that the function takes
All argument types must be constructible via the std::istream& operator>>(...) formatted input operator and the return type must be support the std::ostream& operator<<(...) formatted output operator.
function add
template <typename R ,
typename... Args>
inline void add(
std::string name,
R(*)(Args...) func
)
Register a command with a function pointer that takes arguments and returns a value.
Parameters:
- name The name of the command to be registered
- func The function pointer to be registered, which must take the specified arguments and return a value
Template Parameters:
- R The return type of the function
- Args The types of the arguments that the function takes
All argument types must be constructible via the std::istream& operator>>(...) formatted input operator and the return type must be support the std::ostream& operator<<(...) formatted output operator.
function add
template <typename T ,
typename R ,
typename... Args>
inline void add(
std::string name,
R(T::*)(Args...) member_func,
T * t
)
Register a command with a member function of a class.
Parameters:
- name The name of the command to be registered
- member_func The member function pointer to be registered, which must take the specified arguments and
- t A pointer to the instance of the class that contains the member function, which must be valid when return a value
Template Parameters:
- T The type of the class that contains the member function
- R The return type of the member function
- Args The types of the arguments that the member function takes
All argument types must be constructible via the std::istream& operator>>(...) formatted input operator and the return type must be support the std::ostream& operator<<(...) formatted output operator.
function call
inline std::string call(
const std::string & name,
const std::vector< std::string > & args
)
Call a registered command with the given name and arguments.
Parameters:
- name The name of the command to be called
- args The arguments to be passed to the command
function commands
inline std::vector< std::pair< std::string, std::size_t > > commands() const
Get a list of registered commands.
Return: A vector of pairs, where each pair contains the command name and the number of arguments it expects
Updated on 2025-11-14 at 11:31:23 +0100