pianod2
multisource multiuser scriptable networked music player
Public Types | Public Member Functions | Protected Attributes | Private Attributes | List of all members
Parsnip::Dispatcher< DispatchReturnType, DispatchContextType > Class Template Reference

#include <parsnip_command.h>

Public Types

using ReturnType = DispatchReturnType
 
using ContextType = DispatchContextType
 
using ExecutorType = std::function< ReturnType(Parser::CommandId, const Parsnip::Data &, ContextType)>
 

Public Member Functions

 Dispatcher (const ParserRef &parser)
 Construct a dispatcher. More...
 
void addHandler (const Parser::Definitions &defs, const ExecutorType &handler)
 Register handlers with the dispatcher. More...
 
void addHandler (const Parser::Definitions &defs, Interpreter< DispatchReturnType, DispatchContextType > *interpreter)
 Register handlers with the dispatcher. More...
 
void addStatements (const Parser::Definitions &defs, ExecutorType handler)
 Add statements to the parser and register their related handler. More...
 
ReturnType operator() (Parser::CommandId command_id, const Parsnip::Data &parameters, DispatchContextType context) const
 Dispatch a command to the executor/handler function. More...
 
ReturnType operator() (Parser::StringType command, ContextType context) const
 Parse and dispatch a command to the executor/handler function. More...
 

Protected Attributes

const ParserRef parser
 

Private Attributes

std::unordered_map< Parser::CommandId, ExecutorTypehandlers
 
int parser_number = 0
 
std::unordered_map< Parser::CommandId, int > mapping_check
 

Member Typedef Documentation

◆ ContextType

template<typename DispatchReturnType , typename DispatchContextType >
using Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::ContextType = DispatchContextType

◆ ExecutorType

template<typename DispatchReturnType , typename DispatchContextType >
using Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::ExecutorType = std::function <ReturnType (Parser::CommandId, const Parsnip::Data &, ContextType)>

◆ ReturnType

template<typename DispatchReturnType , typename DispatchContextType >
using Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::ReturnType = DispatchReturnType

Constructor & Destructor Documentation

◆ Dispatcher()

template<typename DispatchReturnType , typename DispatchContextType >
Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::Dispatcher ( const ParserRef par)

Construct a dispatcher.

Parameters
parA parser for evaluating command lines to be dispatched.

Member Function Documentation

◆ addHandler() [1/2]

template<typename DispatchReturnType , typename DispatchContextType >
void Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::addHandler ( const Parser::Definitions defs,
const ExecutorType handler 
)

Register handlers with the dispatcher.

If assertions aren't enabled, keeps a secondary map to ensure statements are not redefined with different handlers.

Parameters
defsThe statements handled by the handler.
handlerSomething to call when dispatching to defined statements.

◆ addHandler() [2/2]

template<typename DispatchReturnType , typename DispatchContextType >
void Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::addHandler ( const Parser::Definitions defs,
Interpreter< DispatchReturnType, DispatchContextType > *  interpreter 
)
inline

Register handlers with the dispatcher.

Parameters
defsThe statements handled by the handler.
interpreterAn Interpreter whose interpret routine will be used as the handler.

◆ addStatements()

template<typename DispatchReturnType , typename DispatchContextType >
void Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::addStatements ( const Parser::Definitions defs,
ExecutorType  handler 
)

Add statements to the parser and register their related handler.

Parameters
defsThe statements to add and register.
handlerSomething to call when dispatching to defined statements.

◆ operator()() [1/2]

template<typename DispatchReturnType , typename DispatchContextType >
DispatchReturnType Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::operator() ( Parser::CommandId  command_id,
const Parsnip::Data parameters,
DispatchContextType  context 
) const

Dispatch a command to the executor/handler function.

Parameters
command_idIdentifier for the requested handler.
parametersDetails passed to the handler.
contextThe caller's context.
Returns
Whatever value is returned by the handler.

◆ operator()() [2/2]

template<typename DispatchReturnType , typename DispatchContextType >
DispatchReturnType Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::operator() ( Parser::StringType  command,
ContextType  context 
) const

Parse and dispatch a command to the executor/handler function.

Parameters
commandThe command line to parse & execute.
contextThe caller's context.
Returns
Whatever value is returned by the handler.

Member Data Documentation

◆ handlers

template<typename DispatchReturnType , typename DispatchContextType >
std::unordered_map<Parser::CommandId, ExecutorType> Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::handlers
private

◆ mapping_check

template<typename DispatchReturnType , typename DispatchContextType >
std::unordered_map<Parser::CommandId, int> Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::mapping_check
private

◆ parser

template<typename DispatchReturnType , typename DispatchContextType >
const ParserRef Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::parser
protected

◆ parser_number

template<typename DispatchReturnType , typename DispatchContextType >
int Parsnip::Dispatcher< DispatchReturnType, DispatchContextType >::parser_number = 0
private

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