SectionPairSection.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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-17
  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/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
  15. #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
  16. #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
  17. ls::std::io::SectionPairSection::SectionPairSection(const ls::std::io::section_pair_identifier &_sectionId) : ls::std::core::Class("SectionPairSection")
  18. {
  19. this->_setSectionId(_sectionId);
  20. }
  21. ls::std::io::SectionPairSection::~SectionPairSection() = default;
  22. void ls::std::io::SectionPairSection::add(const section_pair_row_list_element &_row)
  23. {
  24. ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_row)}.evaluate();
  25. this->_rowExistenceCheck(_row->getKey());
  26. this->rows.push_back(_row);
  27. }
  28. void ls::std::io::SectionPairSection::clear()
  29. {
  30. this->rows.clear();
  31. }
  32. ls::std::io::section_pair_row_list_element ls::std::io::SectionPairSection::get(size_t _index)
  33. {
  34. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->rows.size()}.evaluate();
  35. ls::std::io::section_pair_row_list_element element{};
  36. size_t index{};
  37. for (const auto &_element : this->rows)
  38. {
  39. if (index == _index)
  40. {
  41. element = _element;
  42. break;
  43. }
  44. ++index;
  45. }
  46. return element;
  47. }
  48. ls::std::io::section_pair_row_list ls::std::io::SectionPairSection::getList()
  49. {
  50. return this->rows;
  51. }
  52. size_t ls::std::io::SectionPairSection::getRowAmount()
  53. {
  54. return this->rows.size();
  55. }
  56. ls::std::io::section_pair_identifier ls::std::io::SectionPairSection::getSectionId()
  57. {
  58. return this->sectionId;
  59. }
  60. ls::std::core::type::byte_field ls::std::io::SectionPairSection::marshal()
  61. {
  62. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  63. return this->serializable->marshal();
  64. }
  65. void ls::std::io::SectionPairSection::setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  66. {
  67. this->_setSectionId(_sectionId);
  68. }
  69. void ls::std::io::SectionPairSection::unmarshal(const ls::std::core::type::byte_field &_data)
  70. {
  71. ls::std::core::ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  72. this->serializable->unmarshal(_data);
  73. }
  74. void ls::std::io::SectionPairSection::_createSerializable()
  75. {
  76. ls::std::io::SerializableSectionPairParameter parameter{};
  77. parameter.setValue(shared_from_this());
  78. this->serializable = ::std::make_shared<ls::std::io::SerializableSectionPairSection>(parameter);
  79. }
  80. bool ls::std::io::SectionPairSection::_hasRow(const ls::std::io::section_pair_identifier &_key)
  81. {
  82. bool rowExists{};
  83. for (const auto &_row : this->rows)
  84. {
  85. if (_row->getKey() == _key)
  86. {
  87. rowExists = true;
  88. break;
  89. }
  90. }
  91. return rowExists;
  92. }
  93. void ls::std::io::SectionPairSection::_rowExistenceCheck(const ls::std::io::section_pair_identifier &_key)
  94. {
  95. if (this->_hasRow(_key))
  96. {
  97. ::std::string message = this->getClassName() + ": row key \"" + _key + "\" already exists in section \"" + this->sectionId + "\"!";
  98. throw ls::std::core::IllegalArgumentException{message};
  99. }
  100. }
  101. void ls::std::io::SectionPairSection::_setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  102. {
  103. ls::std::core::EmptyStringArgumentEvaluator{_sectionId}.evaluate();
  104. ls::std::io::SectionPairIdentifierArgumentEvaluator(_sectionId, this->getClassName() + ": argument \"_sectionId\" contains invalid characters!").evaluate();
  105. this->sectionId = _sectionId;
  106. }