SerializableSectionPairRowListValue.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <list>
  11. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  12. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  13. #include <ls-std/io/section-pair/SectionPairTypes.hpp>
  14. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  15. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
  16. #include <numeric>
  17. using ls::standard::core::Class;
  18. using ls::standard::core::EmptyStringArgumentEvaluator;
  19. using ls::standard::core::NullPointerArgumentEvaluator;
  20. using ls::standard::core::type::byte_field;
  21. using ls::standard::io::SectionPairRowListValue;
  22. using ls::standard::io::SerializableSectionPairParameter;
  23. using ls::standard::io::SerializableSectionPairRowListValue;
  24. using std::accumulate;
  25. using std::dynamic_pointer_cast;
  26. using std::list;
  27. using std::move;
  28. using std::shared_ptr;
  29. using std::string;
  30. using std::string_view;
  31. SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const SerializableSectionPairParameter &_parameter) : Class("SerializableSectionPairRowListValue"), parameter(_parameter)
  32. {
  33. const string message = this->getClassName() + ": model reference is null!";
  34. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  35. }
  36. SerializableSectionPairRowListValue::~SerializableSectionPairRowListValue() noexcept = default;
  37. shared_ptr<Class> SerializableSectionPairRowListValue::getValue() const
  38. {
  39. return this->parameter.getValue();
  40. }
  41. byte_field SerializableSectionPairRowListValue::marshal()
  42. {
  43. list<section_pair_row_value> listValue = dynamic_pointer_cast<SectionPairRowListValue>(this->getValue())->getList();
  44. return accumulate(listValue.begin(), listValue.end(), byte_field{}, [this](byte_field data, const byte_field &_value) { return ::move(data) + " " + _value + this->parameter.getNewLine(); });
  45. }
  46. void SerializableSectionPairRowListValue::unmarshal(const byte_field &_data)
  47. {
  48. EmptyStringArgumentEvaluator{_data}.evaluate();
  49. byte_field searchText = _data;
  50. const string newLine = this->parameter.getNewLine();
  51. while (!searchText.empty() && searchText != newLine)
  52. {
  53. const string::size_type positionOfNewLine = searchText.find(newLine);
  54. string line = _getLine(positionOfNewLine, searchText);
  55. line = line.substr(2);
  56. dynamic_pointer_cast<SectionPairRowListValue>(this->getValue())->add(line);
  57. this->_updateSearchText(positionOfNewLine, searchText);
  58. }
  59. }
  60. string SerializableSectionPairRowListValue::_getLine(const string::size_type _position, const string_view _searchText)
  61. {
  62. string line{};
  63. if (_position != string::npos)
  64. {
  65. line = _searchText.substr(0, _position);
  66. }
  67. else
  68. {
  69. line = _searchText;
  70. }
  71. return line;
  72. }
  73. void SerializableSectionPairRowListValue::_updateSearchText(const string::size_type _position, byte_field &_searchText) const
  74. {
  75. if (_position != string::npos)
  76. {
  77. _searchText = _searchText.substr(_position + this->parameter.getNewLine().size());
  78. }
  79. else
  80. {
  81. _searchText = "";
  82. }
  83. }