State.cpp 2.5 KB

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