SectionPairSection.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-24
  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. SectionPairSection::SectionPairSection(const section_pair_identifier &_sectionId) : Class("SectionPairSection")
  40. {
  41. this->_setSectionId(_sectionId);
  42. }
  43. SectionPairSection::~SectionPairSection() noexcept = default;
  44. void SectionPairSection::add(const section_pair_row_list_element &_row)
  45. {
  46. NullPointerArgumentEvaluator{reinterpret_pointer_cast<void>(_row)}.evaluate();
  47. this->_rowExistenceCheck(_row->getKey());
  48. this->rows.push_back(_row);
  49. }
  50. void SectionPairSection::clear()
  51. {
  52. this->rows.clear();
  53. }
  54. section_pair_row_list_element SectionPairSection::get(size_t _index)
  55. {
  56. IndexOutOfBoundsEvaluator{_index, this->rows.size()}.evaluate();
  57. section_pair_row_list_element element{};
  58. size_t index{};
  59. for (const auto &_element : this->rows)
  60. {
  61. if (index == _index)
  62. {
  63. element = _element;
  64. break;
  65. }
  66. ++index;
  67. }
  68. return element;
  69. }
  70. section_pair_row_list_element SectionPairSection::get(const section_pair_identifier &_key)
  71. {
  72. return this->_get(_key);
  73. }
  74. section_pair_row_list SectionPairSection::getList()
  75. {
  76. return this->rows;
  77. }
  78. size_t SectionPairSection::getRowAmount()
  79. {
  80. return this->rows.size();
  81. }
  82. section_pair_identifier SectionPairSection::getSectionId()
  83. {
  84. return this->sectionId;
  85. }
  86. bool SectionPairSection::hasRow(const section_pair_identifier &_key)
  87. {
  88. return this->_hasRow(_key);
  89. }
  90. byte_field SectionPairSection::marshal()
  91. {
  92. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  93. return this->serializable->marshal();
  94. }
  95. void SectionPairSection::reserveNewLine(const string &_reservedNewLine)
  96. {
  97. this->reservedNewLine = _reservedNewLine;
  98. }
  99. void SectionPairSection::setSectionId(const section_pair_identifier &_sectionId)
  100. {
  101. this->_setSectionId(_sectionId);
  102. }
  103. void SectionPairSection::unmarshal(const byte_field &_data)
  104. {
  105. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  106. this->serializable->unmarshal(_data);
  107. }
  108. void SectionPairSection::_createSerializable()
  109. {
  110. SerializableSectionPairParameter parameter{};
  111. parameter.setValue(shared_from_this());
  112. if (!this->reservedNewLine.empty())
  113. {
  114. parameter.setNewLine(this->reservedNewLine);
  115. }
  116. this->serializable = make_shared<SerializableSectionPairSection>(parameter);
  117. }
  118. section_pair_row_list_element SectionPairSection::_get(const section_pair_identifier &_key)
  119. {
  120. const auto &iterator = find_if(this->rows.begin(), this->rows.end(), [_key](const shared_ptr<SectionPairRow> &_row) { return _row->getKey() == _key; });
  121. return iterator != this->rows.end() ? *iterator : nullptr;
  122. }
  123. bool SectionPairSection::_hasRow(const section_pair_identifier &_key)
  124. {
  125. return any_of(this->rows.begin(), this->rows.end(), [_key](const shared_ptr<SectionPairRow> &_row) { return _row->getKey() == _key; });
  126. }
  127. void SectionPairSection::_rowExistenceCheck(const section_pair_identifier &_key)
  128. {
  129. if (this->_hasRow(_key))
  130. {
  131. string message = this->getClassName() + ": row key \"" + _key + "\" already exists in section \"" + this->sectionId + "\"!";
  132. throw IllegalArgumentException{SectionPairMessageFormatter::getFormattedMessage(message)};
  133. }
  134. }
  135. void SectionPairSection::_setSectionId(const section_pair_identifier &_sectionId)
  136. {
  137. EmptyStringArgumentEvaluator{_sectionId}.evaluate();
  138. SectionPairIdentifierArgumentEvaluator(_sectionId).evaluate();
  139. this->sectionId = _sectionId;
  140. }