SectionPairSection.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-13
  6. * Changed: 2023-02-19
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  11. #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
  12. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  13. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  14. #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
  15. #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
  16. #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
  17. ls::std::io::SectionPairSection::SectionPairSection(const ls::std::io::section_pair_identifier &_sectionId) : ls::std::core::Class("SectionPairSection")
  18. {
  19. this->_setSectionId(_sectionId);
  20. }
  21. ls::std::io::SectionPairSection::~SectionPairSection() = default;
  22. void ls::std::io::SectionPairSection::add(const section_pair_row_list_element &_row)
  23. {
  24. ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_row)}.evaluate();
  25. this->_rowExistenceCheck(_row->getKey());
  26. this->rows.push_back(_row);
  27. }
  28. void ls::std::io::SectionPairSection::clear()
  29. {
  30. this->rows.clear();
  31. }
  32. ls::std::io::section_pair_row_list_element ls::std::io::SectionPairSection::get(size_t _index)
  33. {
  34. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->rows.size()}.evaluate();
  35. ls::std::io::section_pair_row_list_element element{};
  36. size_t index{};
  37. for (const auto &_element : this->rows)
  38. {
  39. if (index == _index)
  40. {
  41. element = _element;
  42. break;
  43. }
  44. ++index;
  45. }
  46. return element;
  47. }
  48. ls::std::io::section_pair_row_list ls::std::io::SectionPairSection::getList()
  49. {
  50. return this->rows;
  51. }
  52. size_t ls::std::io::SectionPairSection::getRowAmount()
  53. {
  54. return this->rows.size();
  55. }
  56. ls::std::io::section_pair_identifier ls::std::io::SectionPairSection::getSectionId()
  57. {
  58. return this->sectionId;
  59. }
  60. ls::std::core::type::byte_field ls::std::io::SectionPairSection::marshal()
  61. {
  62. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  63. return this->serializable->marshal();
  64. }
  65. void ls::std::io::SectionPairSection::reserveNewLine(const ::std::string &_reservedNewLine)
  66. {
  67. this->reservedNewLine = _reservedNewLine;
  68. }
  69. void ls::std::io::SectionPairSection::setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  70. {
  71. this->_setSectionId(_sectionId);
  72. }
  73. void ls::std::io::SectionPairSection::unmarshal(const ls::std::core::type::byte_field &_data)
  74. {
  75. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  76. this->serializable->unmarshal(_data);
  77. }
  78. void ls::std::io::SectionPairSection::_createSerializable()
  79. {
  80. ls::std::io::SerializableSectionPairParameter parameter{};
  81. parameter.setValue(shared_from_this());
  82. if (!this->reservedNewLine.empty())
  83. {
  84. parameter.setNewLine(this->reservedNewLine);
  85. }
  86. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairSection>(parameter);
  87. }
  88. bool ls::std::io::SectionPairSection::_hasRow(const ls::std::io::section_pair_identifier &_key)
  89. {
  90. bool rowExists{};
  91. for (const auto &_row : this->rows)
  92. {
  93. if (_row->getKey() == _key)
  94. {
  95. rowExists = true;
  96. break;
  97. }
  98. }
  99. return rowExists;
  100. }
  101. void ls::std::io::SectionPairSection::_rowExistenceCheck(const ls::std::io::section_pair_identifier &_key)
  102. {
  103. if (this->_hasRow(_key))
  104. {
  105. ::std::string message = this->getClassName() + ": row key \"" + _key + "\" already exists in section \"" + this->sectionId + "\"!";
  106. throw ls::std::core::IllegalArgumentException{message};
  107. }
  108. }
  109. void ls::std::io::SectionPairSection::_setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  110. {
  111. ls::std::core::EmptyStringArgumentEvaluator{_sectionId}.evaluate();
  112. ls::std::io::SectionPairIdentifierArgumentEvaluator(_sectionId).evaluate();
  113. this->sectionId = _sectionId;
  114. }