SectionPairSection.cpp 4.2 KB

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