PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/internal/Dispatcher.cpp

https://github.com/amcjen/ncore
C++ | 107 lines | 69 code | 22 blank | 16 comment | 12 complexity | a01ff1657c1ac6a581201093586fd2ea MD5 | raw file
  1. // STL includes
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. #include <stdexcept>
  6. #include <algorithm>
  7. // C includes
  8. // Library includes
  9. // Project includes
  10. #include <Parser.h>
  11. #include <IDispatchable.h>
  12. #include <Dispatcher.h>
  13. using namespace std;
  14. /****************************************************************************/
  15. void Dispatcher::call_help(const objectmap_t::value_type& _item)
  16. {
  17. Parser help;
  18. help.resize(2);
  19. help.at(0) = "help";
  20. help.at(1) = _item.first;
  21. (_item.second)->runCommand(help);
  22. }
  23. /****************************************************************************/
  24. bool Dispatcher::execute_new(const Parser& _commands) const
  25. {
  26. if ( ! _commands.size() )
  27. throw new runtime_error("Command string empty");
  28. string command = _commands.at(0);
  29. // Redirect 'help <xxx>' to the appropriate command handler.
  30. if ( command == "help" )
  31. {
  32. if ( _commands.size() < 2 )
  33. {
  34. // Send a 'help' to every single command on the list
  35. for_each(objectmap.begin(),objectmap.end(),call_help);
  36. return true;
  37. }
  38. command = _commands.at(1);
  39. }
  40. // Do we know about this command?
  41. if ( ! objectmap.count( command ) )
  42. {
  43. // If not, do we have a default?
  44. if ( objectmap.count( "(default)" ) )
  45. {
  46. // If so, try handling this command
  47. if ( ! objectmap.at("(default)")->runCommand(_commands) )
  48. throw new runtime_error("Command not found");
  49. else
  50. return true;
  51. }
  52. else
  53. throw new runtime_error("Command not found");
  54. }
  55. return objectmap.at(command)->runCommand(_commands);
  56. }
  57. /****************************************************************************/
  58. bool Dispatcher::execute(const std::string& _str) const
  59. {
  60. return execute_new(Parser(_str));
  61. }
  62. /****************************************************************************/
  63. bool Dispatcher::add(IDispatchable* obj)
  64. {
  65. if ( ! obj )
  66. throw new runtime_error("Object is NULL");
  67. Parser list(obj->getCommands());
  68. if ( ! list.size() )
  69. throw new runtime_error("Object has no commands");
  70. Parser::const_iterator it = list.begin();
  71. while ( it != list.end() )
  72. {
  73. if ( objectmap.count( *it ) )
  74. throw new runtime_error("Command already registered");
  75. objectmap[*it++] = obj;
  76. }
  77. return true;
  78. }
  79. /****************************************************************************/
  80. void Dispatcher::clear(void)
  81. {
  82. objectmap.clear();
  83. }
  84. /****************************************************************************/
  85. // vim:cin:ai:sts=2 sw=2 ft=cpp