SerializableSectionPairRow.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2023-02-11
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/io/section-pair/evaluator/SectionPairRowListValueArgumentEvaluator.hpp>
  12. #include <ls-std/io/section-pair/evaluator/SectionPairRowSingleValueArgumentEvaluator.hpp>
  13. #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
  14. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  15. #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
  16. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
  17. using ls::standard::core::Class;
  18. using ls::standard::core::NullPointerArgumentEvaluator;
  19. using ls::standard::core::type::byte_field;
  20. using ls::standard::io::SectionPairRow;
  21. using ls::standard::io::SectionPairRowListValue;
  22. using ls::standard::io::SectionPairRowListValueArgumentEvaluator;
  23. using ls::standard::io::SectionPairRowSingleValue;
  24. using ls::standard::io::SectionPairRowSingleValueArgumentEvaluator;
  25. using ls::standard::io::SerializableSectionPairParameter;
  26. using ls::standard::io::SerializableSectionPairRow;
  27. using std::dynamic_pointer_cast;
  28. using std::shared_ptr;
  29. using std::string;
  30. SerializableSectionPairRow::SerializableSectionPairRow(const SerializableSectionPairParameter &_parameter) : Class("SerializableSectionPairRow"), parameter(_parameter)
  31. {
  32. const string message = this->getClassName() + ": model reference is null!";
  33. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  34. }
  35. SerializableSectionPairRow::~SerializableSectionPairRow() noexcept = default;
  36. shared_ptr<Class> SerializableSectionPairRow::getValue() const
  37. {
  38. return this->parameter.getValue();
  39. }
  40. byte_field SerializableSectionPairRow::marshal()
  41. {
  42. const byte_field data = this->_marshalKey();
  43. const 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. const 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() const
  61. {
  62. string serializedKey{};
  63. const 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) const
  75. {
  76. SectionPairRowListValueArgumentEvaluator{_data}.evaluate();
  77. const string::size_type separatorPosition = _data.find(':');
  78. const string newLine = this->parameter.getNewLine();
  79. if (separatorPosition != string::npos)
  80. {
  81. const shared_ptr<SectionPairRow> row = dynamic_pointer_cast<SectionPairRow>(this->parameter.getValue());
  82. row->setKey(_data.substr(0, separatorPosition));
  83. const 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) const
  88. {
  89. SectionPairRowSingleValueArgumentEvaluator{_data}.evaluate();
  90. if (const string::size_type position = _data.find('='); position != string::npos)
  91. {
  92. const 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. }