15 #include <unordered_map>
79 using EvaluatorRef = std::unique_ptr<class Evaluator, EvaluatorDeleter>;
135 return !(*
this == other);
140 template <
typename ReturnType,
typename ContextType>
147 template <
typename DispatchReturnType,
typename DispatchContextType>
156 std::unordered_map <Parser::CommandId, ExecutorType>
handlers;
190 std::ostream &
dump (
const std::string &intro,
const CommandId command_id, std::ostream &target = std::clog)
const;
214 template <
typename DispatchReturnType,
typename DispatchContextType>
224 template <
typename DispatchReturnType,
typename DispatchContextType>
229 for (
auto &it : defs) {
231 if (mapping_check [it.command_id]) {
232 assert (mapping_check [it.command_id] == parser_number);
234 mapping_check [it.command_id] = parser_number;
237 handlers [it.command_id] = handler;
246 template <
typename DispatchReturnType,
typename DispatchContextType>
250 interpreter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) };
251 addHandler (defs, handler);
257 template <
typename DispatchReturnType,
typename DispatchContextType>
259 parser->addStatements (defs);
260 addHandler (defs, handler);
268 template <
typename DispatchReturnType,
typename DispatchContextType>
272 return handlers.at (command_id) (command_id, parameters, context);
279 template <
typename DispatchReturnType,
typename DispatchContextType>
Generic data type.
Definition: parsnip.h:81
static const struct Parsnip::Data::dictionary_t Dictionary
Dictionary flag type/value.
Schema component for validating dictionaries.
Definition: parsnip_schema.h:307
Definition: parsnip_command.h:148
void addStatements(const Parser::Definitions &defs, ExecutorType handler)
Add statements to the parser and register their related handler.
Definition: parsnip_command.h:258
void addHandler(const Parser::Definitions &defs, const ExecutorType &handler)
Register handlers with the dispatcher.
Definition: parsnip_command.h:225
ReturnType operator()(Parser::CommandId command_id, const Parsnip::Data ¶meters, DispatchContextType context) const
Dispatch a command to the executor/handler function.
Definition: parsnip_command.h:269
DispatchContextType ContextType
Definition: parsnip_command.h:153
int parser_number
Definition: parsnip_command.h:158
DispatchReturnType ReturnType
Definition: parsnip_command.h:152
std::unordered_map< Parser::CommandId, int > mapping_check
Definition: parsnip_command.h:159
Dispatcher(const ParserRef &parser)
Construct a dispatcher.
Definition: parsnip_command.h:215
std::unordered_map< Parser::CommandId, ExecutorType > handlers
Definition: parsnip_command.h:156
const ParserRef parser
Definition: parsnip_command.h:150
void addHandler(const Parser::Definitions &defs, Interpreter< DispatchReturnType, DispatchContextType > *interpreter)
Register handlers with the dispatcher.
Definition: parsnip_command.h:247
std::function< ReturnType(Parser::CommandId, const Parsnip::Data &, ContextType)> ExecutorType
Definition: parsnip_command.h:154
An option was supplied twice to an iterating option parser.
Definition: parsnip_command.h:58
DuplicateOption()
Definition: parsnip_command.h:60
DuplicateOption(const std::string &value)
Definition: parsnip_command.h:61
Evaluator base class.
Definition: parsnip_evaluate.h:30
Class representing issues related to serialization & parsing.
Definition: parsnip.h:41
More tokens were expected.
Definition: parsnip_command.h:44
IncompleteCommand()
Definition: parsnip_command.h:46
IncompleteCommand(const std::string &value)
Definition: parsnip_command.h:47
Definition: parsnip_command.h:141
virtual ReturnType interpret(Parser::CommandId command_id, const Parsnip::Data ¶meters, ContextType context)=0
A token encountered was not one of the expected keywords.
Definition: parsnip_command.h:37
InvalidValue()
Definition: parsnip_command.h:39
InvalidValue(const std::string &value)
Definition: parsnip_command.h:40
A schema is not defined for a certain command ID.
Definition: parsnip_command.h:65
NoSchemaDefined(std::string command)
Definition: parsnip_command.h:68
NoSchemaDefined(const int command)
Definition: parsnip_command.h:69
NoSchemaDefined()
Definition: parsnip_command.h:67
Not numeric: A command pattern specified a number, but the token was not valid number.
Definition: parsnip_command.h:30
NotNumeric()
Definition: parsnip_command.h:32
NotNumeric(const std::string &value)
Definition: parsnip_command.h:33
NumberOutOfRange: The number was outside the range defined in the command pattern.
Definition: parsnip_command.h:23
NumberOutOfRange()
Definition: parsnip_command.h:25
NumberOutOfRange(const std::string &value)
Definition: parsnip_command.h:26
Evaluate remainder with a different parser.
Definition: parsnip_evaluate.h:315
A class for parsing command line options (name-value pairs, for instance).
Definition: parsnip_command.h:82
void addOptions(const Definitions &defs, class Parser *=nullptr)
Register new option patterns.
Definition: parsnip_command.cpp:454
bool operator==(const OptionParser &) const
Definition: parsnip_command.cpp:468
EvaluatorRef evaluator
Definition: parsnip_command.h:86
std::vector< const char * > Definitions
Definition: parsnip_command.h:90
A class for parsing command lines.
Definition: parsnip_command.h:100
std::string StringType
Definition: parsnip_command.h:110
OptionParsers option_parsers
Definition: parsnip_command.h:106
std::vector< Definition > Definitions
Definition: parsnip_command.h:117
bool operator!=(const Parser &other) const
Definition: parsnip_command.h:134
std::unordered_map< std::string, OptionParserRef > OptionParsers
Definition: parsnip_command.h:105
Result evaluate(const StringType &command) const
Evaluate a command.
Definition: parsnip_command.cpp:424
void addStatements(const Definitions &defs)
Add statements to the parser.
Definition: parsnip_command.cpp:399
bool operator==(const Parser &) const
Check if a parser is identical to another parser.
Definition: parsnip_command.cpp:435
EvaluatorRef evaluator
Definition: parsnip_command.h:104
int CommandId
Definition: parsnip_command.h:109
virtual ~Parser()=default
void addOptionParser(const std::string &name, const OptionParserRef &add)
Register an options parser for use.
Definition: parsnip_command.cpp:415
There were extra tokens at the end of the command pattern.
Definition: parsnip_command.h:51
RunOnCommand(const std::string &value)
Definition: parsnip_command.h:54
RunOnCommand()
Definition: parsnip_command.h:53
Definition: parsnip_schema.h:28
Smart pointer for schemas, with unusual ability to be copied.
Definition: parsnip.h:601
Schema for one JSON object.
Definition: parsnip.h:612
static const Dependencies NoDependencies
Definition: parsnip.h:617
std::set< std::string > Dependencies
Definition: parsnip.h:616
std::unordered_map< class OptionParser *, SchemaRef > OptionSchemas
Definition: parsnip.h:615
A schema built to validate JSON messages based on command line patterns.
Definition: parsnip_command.h:171
SchemaBaseRef & getSchemaForCommand(CommandId command_id)
Get a schema by ID.
Definition: parsnip_schema.cpp:1263
void addMember(const CommandId, const char *name, const SchemaBase &schema, bool mandatory=false, const Dependencies &dependencies=Schema::NoDependencies)
Modify a schema by adding a new dictionary member.
Definition: parsnip_schema.cpp:1319
SchemaSet(const class Parser &from)
Construct schemas from a parser.
Definition: parsnip_schema.cpp:1226
void integrateSchema(const int, const class DictionarySchema &)
Callback function, invoked when generating schemas from a Parser.
Definition: parsnip_schema.cpp:1285
Schema::OptionSchemas OptionSchemas
Definition: parsnip_command.h:175
Schema::Dependencies Dependencies
Definition: parsnip_command.h:176
std::ostream & dump(const std::string &intro, const CommandId command_id, std::ostream &target=std::clog) const
Dump the schema in human-readable format.
Definition: parsnip_schema.cpp:1305
void removeMember(const CommandId, const char *name)
Modify a schema by removing a dictionary member.
Definition: parsnip_schema.cpp:1354
void validate(const CommandId, const Data &) const
Validate data against a command's schema.
Definition: parsnip_schema.cpp:1297
OptionSchemas option_schemas
Schemas for each of the option parsers.
Definition: parsnip_command.h:186
void replaceMember(const CommandId, const char *name, const SchemaBase &schema)
Modify a schema by replacing dictionary member.
Definition: parsnip_schema.cpp:1347
int CommandId
Definition: parsnip_command.h:173
std::unordered_map< CommandId, SchemaBaseRef > CommandSchemas
Definition: parsnip_command.h:174
CommandSchemas schemas
Schemas for each unique command ID.
Definition: parsnip_command.h:179
Connection to a pianod client, along with context and state of that connection.
Definition: connection.h:54
A response collector/aggregator.
Definition: response.h:197
uint32_t value
Definition: audiooutput.cpp:68
Serialization and parsing library.
Definition: mediaunit.h:26
std::shared_ptr< OptionParser > OptionParserRef
Definition: parsnip_command.h:97
const std::string EmptyString
Definition: parsnip_command.cpp:128
std::unique_ptr< class Evaluator, EvaluatorDeleter > EvaluatorRef
Definition: parsnip_command.h:79
std::shared_ptr< SchemaSet > SchemaSetRef
Definition: parsnip_command.h:199
std::shared_ptr< Parser > ParserRef
Definition: parsnip_command.h:138
This does what std::default_delete would do, but is required since we haven't defined Evaluator yet.
Definition: parsnip_command.h:76
void operator()(class Evaluator *free_me)
Definition: parsnip_evaluate.cpp:31
Definition: parsnip_command.h:113
CommandId command_id
Definition: parsnip_command.h:114
const char * statement
Definition: parsnip_command.h:115
Definition: parsnip_command.h:120
CommandId command_id
< ID of command pattern that was matched.
Definition: parsnip_command.h:122
Parsnip::Data parameters
Parameters collected from the command line.
Definition: parsnip_command.h:124