Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /*
  2. ** EPITECH PROJECT, 2021
  3. ** cpp_d16
  4. ** File description:
  5. ** Created by Benoit HOFFMAN,
  6. */
  7.  
  8. #include <algorithm>
  9. #include "DomesticKoala.hpp"
  10.  
  11. DomesticKoala::DomesticKoala(const DomesticKoala &toCopy)
  12. : _koalaAction(toCopy._koalaAction)
  13. {
  14. _actions = new std::vector<methodPointer_t>;
  15. for (methodPointer_t ptr : *toCopy._actions)
  16. this->_actions->push_back(ptr);
  17. for (unsigned char index : toCopy._iterator)
  18. this->_iterator.push_back(index);
  19.  
  20. }
  21.  
  22. DomesticKoala &DomesticKoala::operator=(const DomesticKoala &toCopy)
  23. {
  24. _actions->clear();
  25. _iterator.clear();
  26. _koalaAction = toCopy._koalaAction;
  27. _actions = new std::vector<methodPointer_t>;
  28. for (methodPointer_t ptr : *toCopy._actions)
  29. this->_actions->push_back(ptr);
  30. for (unsigned char index : toCopy._iterator)
  31. this->_iterator.push_back(index);
  32. return *this;
  33. }
  34.  
  35. DomesticKoala::~DomesticKoala()
  36. {
  37. }
  38.  
  39. void DomesticKoala::learnAction(unsigned char command, methodPointer_t ptr)
  40. {
  41. _actions->push_back(ptr);
  42. _iterator.push_back(command);
  43. }
  44.  
  45. void DomesticKoala::unlearnAction(unsigned char command)
  46. {
  47.  
  48. unsigned int pos = static_cast<unsigned int>(
  49. std::find(_iterator.begin(), _iterator.end(), command)
  50. - _iterator.begin());
  51. if (pos < _iterator.size()) {
  52. _iterator.erase(_iterator.begin() + pos);
  53. _actions->erase(_actions->begin() + pos);
  54. }
  55. }
  56.  
  57. void DomesticKoala::doAction(unsigned char command, const std::string &param)
  58. {
  59. int pos = std::find(_iterator.begin(), _iterator.end(), command)
  60. - _iterator.begin();
  61. try {
  62. (_koalaAction.*_actions->at(pos))(param);
  63. }
  64. catch (const std::out_of_range &except) {
  65. }
  66. }
  67.  
  68. void DomesticKoala::setKoalaAction(KoalaAction &koalaAction)
  69. {
  70. _koalaAction = koalaAction;
  71. _actions->clear();
  72. _iterator.clear();
  73. }
  74.  
  75. DomesticKoala::DomesticKoala(KoalaAction &k) : _koalaAction(k),
  76. _actions(new std::vector<methodPointer_t>)
  77. {
  78. }
  79.  
  80. const std::vector<methodPointer_t> *DomesticKoala::getActions() const
  81. {
  82. return _actions;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement