State.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-05-28
  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. 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 (_connectionId.empty() || _connectedState == nullptr)
  45. {
  46. throw ls_std::IllegalArgumentException{};
  47. }
  48. else
  49. {
  50. if (!this->_hasConnection(_connectionId))
  51. {
  52. connection = std::make_shared<ls_std::StateConnection>(_connectionId, _connectedState->getId());
  53. added = this->connectedStates.insert({_connectionId, connection}).second;
  54. }
  55. }
  56. return added;
  57. }
  58. bool ls_std::State::addStateConnection(const std::shared_ptr<ls_std::StateConnection> &_connection)
  59. {
  60. bool added{};
  61. if (_connection != nullptr)
  62. {
  63. added = this->connectedStates.insert({_connection->getConnectionId(), _connection}).second;
  64. }
  65. else
  66. {
  67. throw ls_std::IllegalArgumentException{};
  68. }
  69. return added;
  70. }
  71. void ls_std::State::clearConnections()
  72. {
  73. this->_clearConnections();
  74. }
  75. std::unordered_map<ls_std::StateConnectionId, std::shared_ptr<ls_std::StateConnection>> ls_std::State::getConnectedStates()
  76. {
  77. return this->connectedStates;
  78. }
  79. ls_std::StateId ls_std::State::getId()
  80. {
  81. return this->id;
  82. }
  83. bool ls_std::State::hasConnection(const ls_std::StateConnectionId &_connectionId)
  84. {
  85. return this->_hasConnection(_connectionId);
  86. }
  87. void ls_std::State::setId(const ls_std::StateId& _id)
  88. {
  89. this->_assignStateId(_id);
  90. }
  91. void ls_std::State::setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable)
  92. {
  93. this->_assignSerializable(_serializable);
  94. }
  95. void ls_std::State::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
  96. {
  97. if (_serializable == nullptr)
  98. {
  99. throw ls_std::IllegalArgumentException{};
  100. }
  101. this->serializable = _serializable;
  102. }
  103. void ls_std::State::_assignStateId(const ls_std::StateId &_id)
  104. {
  105. if (_id.empty())
  106. {
  107. throw ls_std::IllegalArgumentException{};
  108. }
  109. this->id = _id;
  110. }
  111. void ls_std::State::_clearConnections()
  112. {
  113. this->connectedStates.clear();
  114. }
  115. bool ls_std::State::_hasConnection(const ls_std::StateConnectionId &_connectionId)
  116. {
  117. return this->connectedStates.find(_connectionId) != this->connectedStates.end();
  118. }