SectionPairDocument.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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-16
  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. ::std::string ls::std::io::SectionPairDocument::getHeader()
  44. {
  45. return this->header;
  46. }
  47. ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
  48. {
  49. return this->sections;
  50. }
  51. void ls::std::io::SectionPairDocument::_checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId)
  52. {
  53. if (this->_hasSection(_sectionId))
  54. {
  55. throw ls::std::core::IllegalArgumentException{this->getClassName() + "section ID \"" + _sectionId + "\" already exists in document!"};
  56. }
  57. }
  58. bool ls::std::io::SectionPairDocument::_hasSection(const ls::std::io::section_pair_identifier &_identifier)
  59. {
  60. bool sectionExists{};
  61. for (const auto &_section : this->sections)
  62. {
  63. if (_section->getSectionId() == _identifier)
  64. {
  65. sectionExists = true;
  66. break;
  67. }
  68. }
  69. return sectionExists;
  70. }