SectionPairDocument.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-15
  6. * Changed: 2023-02-15
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
  13. ls::std::io::SectionPairDocument::SectionPairDocument() : ls::std::core::Class("SectionPairDocument")
  14. {}
  15. ls::std::io::SectionPairDocument::~SectionPairDocument() = default;
  16. void ls::std::io::SectionPairDocument::add(const section_pair_document_section_list_element &_section)
  17. {
  18. ::std::string message = this->getClassName() + ": add section attempt failed, since \"_section\" argument is null!";
  19. ls::std::core::NullPointerArgumentEvaluator(::std::reinterpret_pointer_cast<void>(_section), message).evaluate();
  20. this->_checkSectionExistence(_section->getSectionId());
  21. this->sections.push_back(_section);
  22. }
  23. ls::std::io::section_pair_document_section_list_element ls::std::io::SectionPairDocument::get(size_t _index)
  24. {
  25. ls::std::io::section_pair_document_section_list_element element{};
  26. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->sections.size()}.evaluate();
  27. size_t index{};
  28. for (const auto &_element : this->sections)
  29. {
  30. if (_index == index)
  31. {
  32. element = _element;
  33. break;
  34. }
  35. ++index;
  36. }
  37. return element;
  38. }
  39. size_t ls::std::io::SectionPairDocument::getAmountOfSections()
  40. {
  41. return this->sections.size();
  42. }
  43. ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
  44. {
  45. return this->sections;
  46. }
  47. void ls::std::io::SectionPairDocument::_checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId)
  48. {
  49. if (this->_hasSection(_sectionId))
  50. {
  51. throw ls::std::core::IllegalArgumentException{this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!"};
  52. }
  53. }
  54. bool ls::std::io::SectionPairDocument::_hasSection(const ls::std::io::section_pair_identifier &_identifier)
  55. {
  56. bool sectionExists{};
  57. for (const auto &_section : this->sections)
  58. {
  59. if (_section->getSectionId() == _identifier)
  60. {
  61. sectionExists = true;
  62. break;
  63. }
  64. }
  65. return sectionExists;
  66. }