SectionPairSection.cpp 5.0 KB

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