State.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-04-30
  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(ls_std::StateId _id)
  13. : ls_std::Class("State"),
  14. id(std::move(_id))
  15. {}
  16. ls_std::byte_field ls_std::State::marshal()
  17. {
  18. ls_std::byte_field data{};
  19. if (this->serializable != nullptr)
  20. {
  21. data = this->serializable->marshal();
  22. }
  23. else
  24. {
  25. throw ls_std::NullPointerException{};
  26. }
  27. return data;
  28. }
  29. void ls_std::State::unmarshal(const ls_std::byte_field &_data)
  30. {
  31. if (this->serializable != nullptr)
  32. {
  33. this->serializable->unmarshal(_data);
  34. }
  35. else
  36. {
  37. throw ls_std::NullPointerException{};
  38. }
  39. }
  40. bool ls_std::State::addStateConnection(const ls_std::StateConnectionId &_connectionId, const std::shared_ptr<ls_std::State> &_connectedState)
  41. {
  42. bool added{};
  43. std::shared_ptr<ls_std::StateConnection> connection{};
  44. if (_connectedState != nullptr && !this->_hasConnection(_connectionId))
  45. {
  46. connection = std::make_shared<ls_std::StateConnection>(_connectionId, _connectedState->getId());
  47. this->connectedStates.insert({_connectionId, connection});
  48. added = true;
  49. }
  50. return added;
  51. }
  52. bool ls_std::State::addStateConnection(const std::shared_ptr<ls_std::StateConnection> &_connection)
  53. {
  54. bool added{};
  55. if (_connection != nullptr)
  56. {
  57. this->connectedStates.insert({_connection->getConnectionId(), _connection});
  58. added = this->_hasConnection(_connection->getConnectionId());
  59. }
  60. return added;
  61. }
  62. void ls_std::State::clearConnections()
  63. {
  64. this->_clearConnections();
  65. }
  66. std::unordered_map<ls_std::StateConnectionId, std::shared_ptr<ls_std::StateConnection>> ls_std::State::getConnectedStates()
  67. {
  68. return this->connectedStates;
  69. }
  70. ls_std::StateId ls_std::State::getId()
  71. {
  72. return this->id;
  73. }
  74. bool ls_std::State::hasConnection(const ls_std::StateConnectionId &_connectionId)
  75. {
  76. return this->_hasConnection(_connectionId);
  77. }
  78. void ls_std::State::setId(ls_std::StateId _id)
  79. {
  80. this->id = std::move(_id);
  81. }
  82. void ls_std::State::setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable)
  83. {
  84. if (_serializable == nullptr)
  85. {
  86. throw ls_std::IllegalArgumentException{};
  87. }
  88. this->serializable = std::move(_serializable);
  89. }
  90. void ls_std::State::_clearConnections()
  91. {
  92. this->connectedStates.clear();
  93. }
  94. bool ls_std::State::_hasConnection(const ls_std::StateConnectionId &_connectionId)
  95. {
  96. return this->connectedStates.find(_connectionId) != this->connectedStates.end();
  97. }