SerializableSectionPairRowListValue.cpp 3.1 KB

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