SectionPairSection.cpp 3.7 KB

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