SerializableXMLState.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-20
  6. * Changed: 2020-09-20
  7. *
  8. * *//*
  9. #include "SerializableXMLState.hpp"
  10. #include "../../../boxing/String.hpp"
  11. #include "SerializableXMLStateConnection.hpp"
  12. ls_std::SerializableXMLState::SerializableXMLState(std::shared_ptr<ls_std::State> _value) :
  13. Class("SerializableXMLState"),
  14. value(std::move(_value))
  15. {}
  16. ls_std::byte_field ls_std::SerializableXMLState::marshal()
  17. {
  18. this->_update();
  19. tinyxml2::XMLPrinter printer {};
  20. this->document.Print(&printer);
  21. return printer.CStr();
  22. }
  23. void ls_std::SerializableXMLState::unmarshal(const ls_std::byte_field &_data)
  24. {
  25. ls_std::String data {_data};
  26. this->document.Parse(data.getByteData().data());
  27. tinyxml2::XMLElement* root = this->document.RootElement();
  28. if(root != nullptr ) {
  29. this->_unmarshalConnections(root);
  30. this->_unmarshalId(root);
  31. }
  32. }
  33. std::shared_ptr<ls_std::State> ls_std::SerializableXMLState::getValue()
  34. {
  35. return this->value;
  36. }
  37. void ls_std::SerializableXMLState::setValue(std::shared_ptr<ls_std::State> _value)
  38. {
  39. this->value = std::move(_value);
  40. this->_clear();
  41. }
  42. void ls_std::SerializableXMLState::_clear()
  43. {
  44. this->document.Clear();
  45. }
  46. void ls_std::SerializableXMLState::_unmarshalConnections(tinyxml2::XMLElement *_root)
  47. {
  48. tinyxml2::XMLNode* connections = _root->FirstChildElement("connections");
  49. if(connections != nullptr) {
  50. tinyxml2::XMLNode* connection = connections->FirstChildElement("connection");
  51. tinyxml2::XMLPrinter printer{};
  52. this->value->clearConnections();
  53. do {
  54. if (connection != nullptr) {
  55. connection->Accept(&printer);
  56. ls_std::SerializableXMLStateConnection serializable{std::make_shared<ls_std::StateConnection>("", "")};
  57. serializable.unmarshal(printer.CStr());
  58. this->value->addStateConnection(serializable.getValue());
  59. connection = connection->NextSibling();
  60. printer.ClearBuffer();
  61. }
  62. }
  63. while(connection != nullptr);
  64. }
  65. }
  66. void ls_std::SerializableXMLState::_unmarshalId(tinyxml2::XMLElement* _root)
  67. {
  68. const tinyxml2::XMLAttribute *attribute = _root->FindAttribute("id");
  69. if(attribute != nullptr) {
  70. this->value->setId(attribute->Value());
  71. }
  72. }
  73. void ls_std::SerializableXMLState::_update()
  74. {
  75. this->_clear();
  76. tinyxml2::XMLNode* root = this->document.NewElement("state");
  77. this->document.InsertFirstChild(root);
  78. root->ToElement()->SetAttribute("id", this->value->getId().c_str());
  79. this->_updateStates();
  80. }
  81. void ls_std::SerializableXMLState::_updateStates()
  82. {
  83. tinyxml2::XMLNode* connectionsNode = this->document.NewElement("connections");
  84. tinyxml2::XMLElement* stateNode = this->document.FirstChildElement("state");
  85. for(const auto& connection : this->value->getConnectedStates()) {
  86. ls_std::byte_field data = ls_std::SerializableXMLStateConnection {connection.second}.marshal();
  87. tinyxml2::XMLDocument connectionNodeDocument {};
  88. connectionNodeDocument.Parse(data.c_str());
  89. tinyxml2::XMLNode* connectionNode = connectionNodeDocument.RootElement();
  90. connectionsNode->InsertFirstChild(connectionNode);
  91. }
  92. stateNode->InsertEndChild(connectionsNode);
  93. }
  94. */