SerializableJsonStateMachine.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-17
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <ls_std/serialization/json/logic/SerializableJsonStateMachine.hpp>
  10. #include <ls_std/serialization/json/logic/SerializableJsonState.hpp>
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::SerializableJsonStateMachine::SerializableJsonStateMachine(const std::shared_ptr<ls_std::StateMachine> &_value) : ls_std::Class("SerializableJsonStateMachine")
  13. {
  14. this->_assignValue(_value);
  15. }
  16. ls_std::byte_field ls_std::SerializableJsonStateMachine::marshal()
  17. {
  18. this->_update();
  19. return this->jsonObject.dump();
  20. }
  21. void ls_std::SerializableJsonStateMachine::unmarshal(const ls_std::byte_field &_data)
  22. {
  23. this->jsonObject = nlohmann::json::parse(_data);
  24. this->_unmarshalStates();
  25. this->_unmarshalCurrentState();
  26. this->value->setMemory(this->jsonObject["memory"]);
  27. this->value->setName(this->jsonObject["name"]);
  28. }
  29. std::shared_ptr<ls_std::StateMachine> ls_std::SerializableJsonStateMachine::getValue()
  30. {
  31. return this->value;
  32. }
  33. void ls_std::SerializableJsonStateMachine::setValue(const std::shared_ptr<ls_std::StateMachine> &_value)
  34. {
  35. this->_assignValue(_value);
  36. }
  37. void ls_std::SerializableJsonStateMachine::_assignValue(const std::shared_ptr<ls_std::StateMachine> &_value)
  38. {
  39. if (_value == nullptr)
  40. {
  41. throw ls_std::IllegalArgumentException{};
  42. }
  43. this->value = _value;
  44. }
  45. void ls_std::SerializableJsonStateMachine::_unmarshalCurrentState()
  46. {
  47. if (this->jsonObject.contains("currentState"))
  48. {
  49. this->value->setStartState(this->jsonObject["currentState"]);
  50. }
  51. }
  52. void ls_std::SerializableJsonStateMachine::_unmarshalStates()
  53. {
  54. for (const auto &serializedState : this->jsonObject["states"])
  55. {
  56. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("");
  57. ls_std::SerializableJsonState{state}.unmarshal(serializedState.dump());
  58. this->value->addState(state);
  59. }
  60. }
  61. void ls_std::SerializableJsonStateMachine::_update()
  62. {
  63. this->jsonObject = {{"memory", this->value->getMemory()},
  64. {"name", this->value->getName()}};
  65. this->_updateCurrentState();
  66. this->_updateStates();
  67. }
  68. void ls_std::SerializableJsonStateMachine::_updateCurrentState()
  69. {
  70. if (this->value->getCurrentState() != nullptr)
  71. {
  72. this->jsonObject["currentState"] = this->value->getCurrentState()->getId();
  73. }
  74. }
  75. void ls_std::SerializableJsonStateMachine::_updateStates()
  76. {
  77. std::string jsonString{};
  78. for (const auto &state : this->value->getStates())
  79. {
  80. jsonString = ls_std::SerializableJsonState{state.second}.marshal();
  81. this->jsonObject["states"][state.first] = nlohmann::json::parse(jsonString);
  82. }
  83. }