SectionPairSection.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_element SectionPairSection::get(const section_pair_identifier &_key)
  68. {
  69. return this->_get(_key);
  70. }
  71. section_pair_row_list SectionPairSection::getList()
  72. {
  73. return this->rows;
  74. }
  75. size_t SectionPairSection::getRowAmount()
  76. {
  77. return this->rows.size();
  78. }
  79. section_pair_identifier SectionPairSection::getSectionId()
  80. {
  81. return this->sectionId;
  82. }
  83. bool SectionPairSection::hasRow(const section_pair_identifier &_key)
  84. {
  85. return this->_hasRow(_key);
  86. }
  87. byte_field SectionPairSection::marshal()
  88. {
  89. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  90. return this->serializable->marshal();
  91. }
  92. void SectionPairSection::reserveNewLine(const string &_reservedNewLine)
  93. {
  94. this->reservedNewLine = _reservedNewLine;
  95. }
  96. void SectionPairSection::setSectionId(const section_pair_identifier &_sectionId)
  97. {
  98. this->_setSectionId(_sectionId);
  99. }
  100. void SectionPairSection::unmarshal(const byte_field &_data)
  101. {
  102. ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  103. this->serializable->unmarshal(_data);
  104. }
  105. void SectionPairSection::_createSerializable()
  106. {
  107. SerializableSectionPairParameter parameter{};
  108. parameter.setValue(shared_from_this());
  109. if (!this->reservedNewLine.empty())
  110. {
  111. parameter.setNewLine(this->reservedNewLine);
  112. }
  113. this->serializable = make_shared<SerializableSectionPairSection>(parameter);
  114. }
  115. section_pair_row_list_element SectionPairSection::_get(const section_pair_identifier &_key)
  116. {
  117. section_pair_row_list_element element{};
  118. for (const auto &_row : this->rows)
  119. {
  120. if (_row->getKey() == _key)
  121. {
  122. element = _row;
  123. break;
  124. }
  125. }
  126. return element;
  127. }
  128. bool SectionPairSection::_hasRow(const section_pair_identifier &_key)
  129. {
  130. bool rowExists{};
  131. for (const auto &_row : this->rows)
  132. {
  133. if (_row->getKey() == _key)
  134. {
  135. rowExists = true;
  136. break;
  137. }
  138. }
  139. return rowExists;
  140. }
  141. void SectionPairSection::_rowExistenceCheck(const section_pair_identifier &_key)
  142. {
  143. if (this->_hasRow(_key))
  144. {
  145. string message = this->getClassName() + ": row key \"" + _key + "\" already exists in section \"" + this->sectionId + "\"!";
  146. throw IllegalArgumentException{SectionPairMessageFormatter::getFormattedMessage(message)};
  147. }
  148. }
  149. void SectionPairSection::_setSectionId(const section_pair_identifier &_sectionId)
  150. {
  151. EmptyStringArgumentEvaluator{_sectionId}.evaluate();
  152. SectionPairIdentifierArgumentEvaluator(_sectionId).evaluate();
  153. this->sectionId = _sectionId;
  154. }