SectionPairSection.cpp 4.4 KB

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