SerializableSectionPairRow.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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-23
  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")
  30. {
  31. string message = this->getClassName() + ": model reference is null!";
  32. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  33. this->parameter = _parameter;
  34. }
  35. SerializableSectionPairRow::~SerializableSectionPairRow() noexcept = default;
  36. shared_ptr<Class> SerializableSectionPairRow::getValue()
  37. {
  38. return this->parameter.getValue();
  39. }
  40. byte_field SerializableSectionPairRow::marshal()
  41. {
  42. byte_field data = this->_marshalKey();
  43. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  44. row->getValue()->reserveNewLine(this->parameter.getNewLine());
  45. return data + row->getValue()->marshal();
  46. }
  47. void SerializableSectionPairRow::unmarshal(const byte_field &_data)
  48. {
  49. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  50. row->getValue()->reserveNewLine(this->parameter.getNewLine());
  51. if (row->isSingleValue())
  52. {
  53. this->_unmarshalSingleValue(_data);
  54. }
  55. if (row->isList())
  56. {
  57. this->_unmarshalListValue(_data);
  58. }
  59. }
  60. string SerializableSectionPairRow::_marshalKey()
  61. {
  62. string serializedKey{};
  63. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  64. if (row->isSingleValue())
  65. {
  66. serializedKey = row->getKey() + "=";
  67. }
  68. if (row->isList())
  69. {
  70. serializedKey = row->getKey() + ":" + this->parameter.getNewLine();
  71. }
  72. return serializedKey;
  73. }
  74. void SerializableSectionPairRow::_unmarshalListValue(const byte_field &_data)
  75. {
  76. SectionPairRowListValueArgumentEvaluator{_data}.evaluate();
  77. string::size_type separatorPosition = _data.find(':');
  78. string newLine = this->parameter.getNewLine();
  79. if (separatorPosition != string::npos)
  80. {
  81. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  82. row->setKey(_data.substr(0, separatorPosition));
  83. string::size_type newLinePosition = _data.find(newLine) + (newLine.size() - 1);
  84. dynamic_pointer_cast<SectionPairRowListValue>(row->getValue())->unmarshal(_data.substr(newLinePosition + 1));
  85. }
  86. }
  87. void SerializableSectionPairRow::_unmarshalSingleValue(const byte_field &_data)
  88. {
  89. SectionPairRowSingleValueArgumentEvaluator{_data}.evaluate();
  90. string::size_type position = _data.find('=');
  91. if (position != string::npos)
  92. {
  93. shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  94. row->setKey(_data.substr(0, position));
  95. dynamic_pointer_cast<SectionPairRowSingleValue>(row->getValue())->unmarshal(_data.substr(position + 1));
  96. }
  97. }