SerializableSectionPairRow.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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-13
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/NewLine.hpp>
  11. #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  13. #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
  14. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
  15. ls::std::io::SerializableSectionPairRow::SerializableSectionPairRow(const ::std::shared_ptr<ls::std::core::Class> &_value)
  16. {
  17. this->_setValue(_value);
  18. }
  19. ls::std::io::SerializableSectionPairRow::~SerializableSectionPairRow() = default;
  20. ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRow::getValue()
  21. {
  22. return this->value;
  23. }
  24. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRow::marshal()
  25. {
  26. ls::std::core::type::byte_field data = this->_marshalKey();
  27. return data + ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value)->getValue()->marshal();
  28. }
  29. void ls::std::io::SerializableSectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
  30. {
  31. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
  32. if (row->isSingleValue())
  33. {
  34. this->_unmarshalSingleValue(_data);
  35. }
  36. if (row->isList())
  37. {
  38. this->_unmarshalListValue(_data);
  39. }
  40. }
  41. ::std::string ls::std::io::SerializableSectionPairRow::_marshalKey()
  42. {
  43. ::std::string serializedKey{};
  44. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
  45. if (row->isSingleValue())
  46. {
  47. serializedKey = row->getKey() + "=";
  48. }
  49. if (row->isList())
  50. {
  51. serializedKey = row->getKey() + ":" + ls::std::io::NewLine::get();
  52. }
  53. return serializedKey;
  54. }
  55. void ls::std::io::SerializableSectionPairRow::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
  56. {
  57. ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
  58. this->value = _value;
  59. }
  60. void ls::std::io::SerializableSectionPairRow::_unmarshalListValue(const ls::std::core::type::byte_field &_data)
  61. {
  62. ::std::string::size_type separatorPosition = _data.find(':');
  63. ls::std::io::section_pair_identifier identifier{};
  64. if (separatorPosition != ::std::string::npos)
  65. {
  66. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
  67. row->setKey(_data.substr(0, separatorPosition));
  68. ::std::string::size_type newLinePosition = _data.find(NewLine::get()) + (NewLine::get().size() - 1);
  69. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(row->getValue())->unmarshal(_data.substr(newLinePosition + 1));
  70. }
  71. }
  72. void ls::std::io::SerializableSectionPairRow::_unmarshalSingleValue(const ls::std::core::type::byte_field &_data)
  73. {
  74. ::std::string::size_type position = _data.find('=');
  75. ls::std::io::section_pair_identifier identifier{};
  76. if (position != ::std::string::npos)
  77. {
  78. ::std::shared_ptr<ls::std::io::SectionPairRow> row = ::std::dynamic_pointer_cast<ls::std::io::SectionPairRow>(this->value);
  79. row->setKey(_data.substr(0, position));
  80. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(row->getValue())->unmarshal(_data.substr(position + 1));
  81. }
  82. }