SerializableSectionPairRow.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-11
  6. * Changed: 2023-02-19
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/section-pair/evaluator/SectionPairRowSingleValueArgumentEvaluator.hpp>
  11. #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  13. #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
  14. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
  15. ls::std::io::SerializableSectionPairRow::SerializableSectionPairRow(const SerializableSectionPairParameter &_parameter) : ls::std::core::Class("SerializableSectionPairRow")
  16. {
  17. ::std::string message = this->getClassName() + ": model reference is null!";
  18. ls::std::core::NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  19. this->parameter = _parameter;
  20. }
  21. ls::std::io::SerializableSectionPairRow::~SerializableSectionPairRow() = default;
  22. ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRow::getValue()
  23. {
  24. return this->parameter.getValue();
  25. }
  26. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRow::marshal()
  27. {
  28. ls::std::core::type::byte_field data = this->_marshalKey();
  29. ::std::shared_ptr<SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->parameter.getValue());
  30. row->getValue()->reserveNewLine(this->parameter.getNewLine());
  31. return data + row->getValue()->marshal();
  32. }
  33. void ls::std::io::SerializableSectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
  34. {
  35. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->parameter.getValue());
  36. row->getValue()->reserveNewLine(this->parameter.getNewLine());
  37. if (row->isSingleValue())
  38. {
  39. this->_unmarshalSingleValue(_data);
  40. }
  41. if (row->isList())
  42. {
  43. this->_unmarshalListValue(_data);
  44. }
  45. }
  46. ::std::string ls::std::io::SerializableSectionPairRow::_marshalKey()
  47. {
  48. ::std::string serializedKey{};
  49. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->parameter.getValue());
  50. if (row->isSingleValue())
  51. {
  52. serializedKey = row->getKey() + "=";
  53. }
  54. if (row->isList())
  55. {
  56. serializedKey = row->getKey() + ":" + this->parameter.getNewLine();
  57. }
  58. return serializedKey;
  59. }
  60. void ls::std::io::SerializableSectionPairRow::_unmarshalListValue(const ls::std::core::type::byte_field &_data)
  61. {
  62. ::std::string::size_type separatorPosition = _data.find(':');
  63. ::std::string newLine = this->parameter.getNewLine();
  64. if (separatorPosition != ::std::string::npos)
  65. {
  66. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->parameter.getValue());
  67. row->setKey(_data.substr(0, separatorPosition));
  68. ::std::string::size_type newLinePosition = _data.find(newLine) + (newLine.size() - 1);
  69. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(row->getValue())->unmarshal(_data.substr(newLinePosition + 1));
  70. }
  71. }
  72. void ls::std::io::SerializableSectionPairRow::_unmarshalSingleValue(const ls::std::core::type::byte_field &_data)
  73. {
  74. ls::std::io::SectionPairRowSingleValueArgumentEvaluator{_data}.evaluate();
  75. ::std::string::size_type position = _data.find('=');
  76. if (position != ::std::string::npos)
  77. {
  78. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->parameter.getValue());
  79. row->setKey(_data.substr(0, position));
  80. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(row->getValue())->unmarshal(_data.substr(position + 1));
  81. }
  82. }