SectionPairSection.cpp 5.0 KB

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