SerializableSectionPairRow.cpp 3.9 KB

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