SerializableJSONStateMachine.cpp 2.7 KB

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