State.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-07-14
  7. *
  8. * */
  9. #include <ls_std/logic/State.hpp>
  10. #include <ls_std/exception/NullPointerException.hpp>
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::State::State(const ls_std::StateId& _id) : ls_std::Class("State")
  13. {
  14. this->_assignStateId(_id);
  15. }
  16. bool ls_std::State::addStateConnection(const ls_std::StateConnectionId &_connectionId, const std::shared_ptr<ls_std::State> &_connectedState)
  17. {
  18. bool added{};
  19. std::shared_ptr<ls_std::StateConnection> connection{};
  20. if (_connectionId.empty() || _connectedState == nullptr)
  21. {
  22. throw ls_std::IllegalArgumentException{};
  23. }
  24. else
  25. {
  26. if (!this->_hasConnection(_connectionId))
  27. {
  28. connection = std::make_shared<ls_std::StateConnection>(_connectionId, _connectedState->getId());
  29. added = this->connectedStates.insert({_connectionId, connection}).second;
  30. }
  31. }
  32. return added;
  33. }
  34. bool ls_std::State::addStateConnection(const std::shared_ptr<ls_std::StateConnection> &_connection)
  35. {
  36. bool added{};
  37. if (_connection != nullptr)
  38. {
  39. added = this->connectedStates.insert({_connection->getConnectionId(), _connection}).second;
  40. }
  41. else
  42. {
  43. throw ls_std::IllegalArgumentException{};
  44. }
  45. return added;
  46. }
  47. void ls_std::State::clearConnections()
  48. {
  49. this->_clearConnections();
  50. }
  51. std::unordered_map<ls_std::StateConnectionId, std::shared_ptr<ls_std::StateConnection>> ls_std::State::getConnectedStates()
  52. {
  53. return this->connectedStates;
  54. }
  55. ls_std::StateId ls_std::State::getId()
  56. {
  57. return this->id;
  58. }
  59. bool ls_std::State::hasConnection(const ls_std::StateConnectionId &_connectionId)
  60. {
  61. return this->_hasConnection(_connectionId);
  62. }
  63. void ls_std::State::setId(const ls_std::StateId& _id)
  64. {
  65. this->_assignStateId(_id);
  66. }
  67. void ls_std::State::_assignStateId(const ls_std::StateId &_id)
  68. {
  69. if (_id.empty())
  70. {
  71. throw ls_std::IllegalArgumentException{};
  72. }
  73. this->id = _id;
  74. }
  75. void ls_std::State::_clearConnections()
  76. {
  77. this->connectedStates.clear();
  78. }
  79. bool ls_std::State::_hasConnection(const ls_std::StateConnectionId &_connectionId)
  80. {
  81. return this->connectedStates.find(_connectionId) != this->connectedStates.end();
  82. }