SerializableSectionPairRow.cpp 3.9 KB

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